Difference between revisions of "BidiMode"

From Free Pascal wiki
Jump to navigationJump to search
m (Fixed syntax highlighting)
 
(46 intermediate revisions by 7 users not shown)
Line 1: Line 1:
There is a languages like as (Arabic, Hebrew, Farsi ...) reading the words from right to left, and also the open the books from the right.
+
Some languages (Arabic, Hebrew, Farsi ...) write the characters from the right to the left, and adequate support for this must be implemented on Lazarus.
 +
 +
====BidiMode====
 +
BidiMode can have these settings:
 +
<syntaxhighlight lang="pascal">
 +
bdLeftToRight
 +
bdRightToLeft
 +
bdRightToLeftNoAlign
 +
bdRightToLeftReadingOnly
 +
</syntaxhighlight>
 +
 
 +
=====bdLeftToRight=====
 +
Default of BidiMode, which means read and order is normal "Left To Right"
 +
 
 +
=====bdRightToLeft=====
 +
Makes a control Right To Left reading text and right alignment depend a kind of the control,
 +
 
 +
In Delphi if there is Alignment property effect by reverse taLeft to taRight and vise versa, but in Lazarus we will reverse the alignment with FlipControls function, which is more consistent with Anchors and Align properties.
 +
Also bdRightToLeft takes a Scrollbar at the left and if there is a cell or menus they must ordered from the right.
 +
 
 +
  For Example
 +
  in bdLeftToRight:
 +
    Menu1, Menu2, Menu2
 +
  in bdRightToLeft:
 +
    Menu3, Menu2, Menu1
 +
 
 +
That means not just Right Alignment; it is Right To Left Order.
 +
bdRightToLeft must effect the text reading as depend on the OS if it is supported, the sentence words take as like Cells
  
Not just reading the words, it is every thing depend on the order (positions), for application there is a Menus and Controls that placed on the form it must have position from the right.
+
  in bdLeftToRight:
 +
    Word1 Word2 Word2
 +
  in bdRightToLeft:
 +
    Word3 Word2 Word1
  
TODO:picture of sample to compare
+
We are reading the Word1 first then Word2, We read from the Right to Left, but Draw Text is more complicated than normal Cells; in fact the function that draws the text cares about English/Latin words and draws it Left To Right, so if we mixed both languages in the same sentence, the draw function draws continual English words in Left To Right order to make it  readable, but symbols like ? or ! are considered as Right To Left characters.
  
==Add BidiMode or RightToLeft support to LCL components==
+
For testing purpose I used English word with a symbol to test Right To Left text reading
  
Most of OS now support RightToLeft but there is controls not supported yet or there is controls make by native language like as (TLabel, TGrid).
+
E.g. ''Word?''
so we have 3 kinds of control
 
  
# Standard Controls: it easy to make it just add some flag to switch it to RightToLeft (TEdit, TList, TComboBox, TCheckBox).
+
For special states or for more compatibility with Delphi there are other values (possible? used? handy?)
# Standard Controls not supported in OS: There is no idea just waiting the OS developers to implement it to support (TListView, TTreeView), or use native controls already support it.
 
# Native Controls: more hard work for make it Support RightToLeft, or we must add new control already have this features (TLabel, TGrid).
 
  
Make application Right to Left order have 4 phases
+
=====bdRightToLeftNoAlign=====
 +
It is Right to Left but not reversed (except the Alignment property),
 +
but in Lazarus worked when use FlipControls
  
=== Phase 1: Add BidiMode property to TControl===
+
=====bdRightToLeftReadingOnly=====
 +
It is Right to Left text reading only; scrollbar and alignment do not take effect (''??? remain left to right???'').
  
''TBidiMode already declaired in Classes''
+
===ParentBidiMode===
  
  property BiDiMode: TBiDiMode read FBiDiMode write SetBiDiMode stored IsBiDiModeStored;
+
If this is True the control inherits the BidiMode value from its parent.
  property ParentBiDiMode: Boolean read FParentBiDiMode write SetParentBiDiMode default True;
 
  
BidiMode must not stored if ParentBidiMode = True
+
===BidiMode for developers===
 +
When you build you own controls and try to make your control support Right to Left or BidiMode, you must not access the BiDiMode property directly. Instead you must use
  
  function TControl.IsBiDiModeStored: Boolean;
+
<syntaxhighlight lang="pascal">
  begin
+
function UseRightToLeftAlignment: Boolean; virtual;
    Result := not ParentBiDiMode;
+
function UseRightToLeftReading: Boolean; virtual;
  end;
+
function UseRightToLeftScrollBar: Boolean;
 +
function IsRightToLeft: Boolean;
 +
</syntaxhighlight>
  
'''Add virtual functions'''
+
Because some controls do not need to make changes to the alignment - for example TButton it always is centered, or TMainMenu which always is right aligned if it is Right to Left.
  
  function IsRightToLeft:Boolean; virtual;
 
  function UseRightToLeftAlignment: Boolean; virtual;
 
  function UseRightToLeftReading: Boolean; virtual;
 
  function UseRightToLeftScrollBar: Boolean; virtual;
 
  ''BiDiMode property in public and must published in every control need to RightToLeft''
 
  
In delphi there is UseRightToLeftAlignment and UseRightToLeftReading and UseRightToLeftScrollBar
+
Not just reading the words and cells, everything depends on the order (positions). For an application there is a Menus and Controls that when placed on the form must have positions starting from the right. It is like looking at a LeftToRight Form in a mirror. The flow of the Form must be from Right To Left.
but there is a Alignment property in TLabel, TEdit not have but we can implement an Alignment for it, so i will not use this technique for now.
 
 
 
===Phase 2: Modify Standard controls that supported by OS===
 
  
Add RightToLeft to Standard controls,
+
[[Image:BidiMode_Sample_1.png|Sample 1]]
  
''done here for Win32''
+
==Add BidiMode support to LCL components==
  
  TForm {done}
+
Most OSes now support RightToLeft but there are controls that are not supported yet or there is controls made by native language (''custom drawn?'') such as (TLabel, TGrid).
  TButton {done}
+
So we have 3 kinds of control:
  TEdit {done}
 
  TListBox {done}
 
  TComboBox {done}
 
  TCheckBox {done}
 
  TStaticText {done}
 
  TGroupBox {done}
 
  TRadioButton {done} (in Win32 need RecreateWnd in SetBidiMode)
 
  TRadioGroup {when write FlipControl function with be done}
 
  TCheckGroup
 
  TScrollbar {not supported by Win32} Delphi emulate it by change the position with virtual position.
 
  
  TMenu/TMenuItem {done}
+
# Standard Controls: it is easy to enable RTL support: just add some flag to switch it to RightToLeft (TEdit, TList, TComboBox, TCheckBox).
    [http://support.microsoft.com/kb/253308 there is a bug in windows]
+
# Standard Controls not supported by the OS: There is no idea just waiting the OS developers to implement it to support (TListView, TTreeView), or use native controls that already support it.
With Menus in Lazarus Win32 the developers force to make it OWNERDRAW, We want to modify DrawMenuItem functions {done}.
+
# Native Controls: hard work is needed to make it Support RightToLeft, or we must add new controls that already have this features (TLabel, TGrid).
 
  TApplication
 
  
TApplication must also have a BidiMode property because it a parent for all Forms, and i plan to make new property "AutoFlipControl",
+
=== Add BidiMode property to TControl===
  
  AutoFlipControl:Boolean;
+
''TBidiMode is already declared in Classes''
 +
<syntaxhighlight lang="pascal">property BiDiMode: TBiDiMode read FBiDiMode write SetBiDiMode stored IsBiDiModeStored;
 +
property ParentBiDiMode: Boolean read FParentBiDiMode write SetParentBiDiMode default True;</syntaxhighlight>
  
When Application.AutoFlipControl is True and change the Application.BidiMode value Application object send to all form to FlipControls.
+
BidiMode must not be stored if ParentBidiMode = True
it is usefull when changing the language from "Left To Right" language like as English to Arabic and vise versa.
+
<syntaxhighlight lang="pascal">
 +
function TControl.IsBiDiModeStored: Boolean;
 +
begin
 +
  Result := not ParentBiDiMode;
 +
end;</syntaxhighlight>
  
===Phase 3: Canvas and Graphic===
+
'''Override BidiMode functions'''
  
Native controls use a Canvas to draw it self, small control use TextOut, but the big one must redraw it from the Right.
+
<syntaxhighlight lang="pascal">
 +
function IsRightToLeft:Boolean; override;
 +
function UseRightToLeftAlignment: Boolean; override;
 +
function UseRightToLeftReading: Boolean; override;
 +
function UseRightToLeftScrollBar: Boolean; override;</syntaxhighlight>
  
''TODO: add image for right to left Grid.''
+
''BiDiMode property is public and must published in every control need to RightToLeft''
  
  TLabel {done}
+
===Bidi Controls Road Map ===
  TGrid {oh my god}
 
  
Before make TLabel work RightToLeft we must add to Canvas.TextStyle a new property "RightToLeft", TextOut and TextRect functions will use this property.
+
GTK2 You must build LCL with
 +
OPT=-dGTK2 OPT=-dGTK_2_8
  
Delphi use CanvasOrientation for mirror the control and maked RightToLeft, i hate use this because it flip the shadow and images (like as the Check) it is make the control ugly.
 
  
  For now TGrid can wait.
+
{| class="wikitable"
 +
! Component !! win32 !! gtk2 !! carbon !! qt !! wince !! cocoa
 +
|----
 +
|TForm
 +
|class="working"|Working
 +
|class="working"|Working
 +
|class="not"|Not Implemented
 +
|class="not"|Not Implemented
 +
|class="not"|Not Implemented
 +
|class="not"|Not Implemented
 +
|----
 +
|TLabel
 +
|class="working"|Working
 +
|class="working"|Working
 +
|class="not"|Not Implemented
 +
|class="not"|Not Implemented
 +
|class="not"|Not Implemented
 +
|class="not"|Not Implemented
 +
|----
 +
|TButton
 +
|class="working"|Working
 +
|class="partial"|Partially Implemented
 +
|class="not"|Not Implemented
 +
|class="not"|Not Implemented
 +
|class="not"|Not Implemented
 +
|class="not"|Not Implemented
 +
|----
 +
|TEdit
 +
|class="working"|Working
 +
|class="partial"|Partially Implemented
 +
|class="not"|Not Implemented
 +
|class="not"|Not Implemented
 +
|class="not"|Not Implemented
 +
|class="not"|Not Implemented
 +
|----
 +
|TListBox
 +
|class="working"|Working
 +
|class="partial"|Partially Implemented
 +
|class="not"|Not Implemented
 +
|class="not"|Not Implemented
 +
|class="not"|Not Implemented
 +
|class="not"|Not Implemented
 +
|----
 +
|TComboBox
 +
|class="working"|Working
 +
|class="partial"|Partially Implemented
 +
|class="not"|Not Implemented
 +
|class="not"|Not Implemented
 +
|class="not"|Not Implemented
 +
|class="not"|Not Implemented
 +
|----
 +
|TCheckBox
 +
|class="working"|Working
 +
|class="working"|Working
 +
|class="not"|Not Implemented
 +
|class="not"|Not Implemented
 +
|class="not"|Not Implemented
 +
|class="not"|Not Implemented
 +
|----
 +
|TStaticText
 +
|class="working"|Working
 +
|class="partial"|Partially Implemented
 +
|class="not"|Not Implemented
 +
|class="not"|Not Implemented
 +
|class="not"|Not Implemented
 +
|class="not"|Not Implemented
 +
|----
 +
|TGroupBox
 +
|class="working"|Working
 +
|class="working"|Working
 +
|class="not"|Not Implemented
 +
|class="not"|Not Implemented
 +
|class="not"|Not Implemented
 +
|class="not"|Not Implemented
 +
|----
 +
|TRadioButton
 +
|class="working"|Working
 +
|class="working"|Working
 +
|class="not"|Not Implemented
 +
|class="not"|Not Implemented
 +
|class="not"|Not Implemented
 +
|class="not"|Not Implemented
 +
|----
 +
|Menus
 +
|class="working"|Working
 +
|class="working"|Working
 +
|class="not"|Not Implemented
 +
|class="not"|Not Implemented
 +
|class="not"|Not Implemented
 +
|class="not"|Not Implemented
 +
|----
 +
|TBitBtn
 +
|class="partial"|Partially Implemented
 +
|class="working"|Working
 +
|class="not"|Not Implemented
 +
|class="not"|Not Implemented
 +
|class="not"|Not Implemented
 +
|class="not"|Not Implemented
 +
|----
 +
|TSpeedBtn
 +
|class="working"|Working
 +
|class="working"|Working
 +
|class="not"|Not Implemented
 +
|class="not"|Not Implemented
 +
|class="not"|Not Implemented
 +
|class="not"|Not Implemented
 +
|----
 +
|TRadioGroup
 +
|class="working"|Working
 +
|class="working"|Working
 +
|class="not"|Not Implemented
 +
|class="not"|Not Implemented
 +
|class="not"|Not Implemented
 +
|class="not"|Not Implemented
 +
|----
 +
|TCheckGroup
 +
|class="working"|Working
 +
|class="working"|Working
 +
|class="not"|Not Implemented
 +
|class="not"|Not Implemented
 +
|class="not"|Not Implemented
 +
|class="not"|Not Implemented
 +
|----
 +
|TGrid  
 +
|class="working"|Working
 +
|class="partial"|Partially Implemented
 +
|class="not"|Not Implemented
 +
|class="not"|Not Implemented
 +
|class="not"|Not Implemented
 +
|class="not"|Not Implemented
 +
|}
  
===Phase 4: Functions and Utils useful for multi language application===
+
===Functions and Utils useful for multi language application===
Like as
 
  
   FlipControls; virtual;
+
   '''TControl.FlipControls; virtual'''
 
   Change Right to Left and Left to Right in this properties (Left, Align, Anchors and Alignment)
 
   Change Right to Left and Left to Right in this properties (Left, Align, Anchors and Alignment)
  ''if We compare with Delphi(TM) Anchors and Alignment was excepted''
 
  Detect language is RightToLeft from current language
 
  
 
==How to test RightToLeft reading==
 
==How to test RightToLeft reading==
Create new form, then add TButton or TEdit set the caption to '''OK?''' you must add '''?''' or '''!''' or '''.''' to the last word with out space, now set the BidiMode to bdRightToLeft, you will see the ? in the left of word.
+
Create a new form, then add a TButton or TEdit. Set the caption to '''OK?'''. You must add '''?''' or '''!''' or '''.''' to the last word without a space. Now set the BidiMode to bdRightToLeft, and you should see the ? in the left of word if it works.
 +
 
 +
=BiDi support of the widgetsets=
 +
 
 +
==Carbon==
 +
 
 +
Write me.
 +
 
 +
==fpGui==
 +
 
 +
Write me.
 +
 
 +
==Gtk1==
 +
 
 +
Has almost no BiDi support. Is deprecated by Lazarus anyway, so no new development at all.
 +
 
 +
==Gtk2==
 +
 
 +
In TMemo, TEdit, TComboBox the popup menu contains the two sub menus ''Input Methods'' and ''Insert Unicode Control character''. Gtk2 uses for text rendering the '''Pango''' library, which has excellent Unicode support.
 +
 
 +
==Qt==
 +
 
 +
Write me.
 +
 
 +
==Windows==
 +
 
 +
In Windows 7 you can switch in TMemo, TEdit, TComboBox the base direction with Left-{{keypress|Ctrl}}+Right-{{keypress|Shift}} to RTL (right to left) and Right-{{keypress|Ctrl}}+Left-{{keypress|Shift}} to LTR (left to right).
 +
This also works in the IDE for most things, like in the Property Editor and the Strings Editor Dialog.
 +
 
 +
=References=
 +
 
 +
* [http://www.unicode.org/reports/tr9/ Unicode Bidirectional Algorithm]
 +
* [http://www.catch22.net/book/export/html/14 Design and Implementation of a Win32 Text Editor]
 +
 
 +
[[Category:Localization]]
 +
[[Category:Lazarus]]
 +
[[Category:LCL]]
 +
[[Category:Lazarus internals]]

Latest revision as of 02:27, 7 February 2020

Some languages (Arabic, Hebrew, Farsi ...) write the characters from the right to the left, and adequate support for this must be implemented on Lazarus.

BidiMode

BidiMode can have these settings:

bdLeftToRight
bdRightToLeft
bdRightToLeftNoAlign
bdRightToLeftReadingOnly
bdLeftToRight

Default of BidiMode, which means read and order is normal "Left To Right"

bdRightToLeft

Makes a control Right To Left reading text and right alignment depend a kind of the control,

In Delphi if there is Alignment property effect by reverse taLeft to taRight and vise versa, but in Lazarus we will reverse the alignment with FlipControls function, which is more consistent with Anchors and Align properties. Also bdRightToLeft takes a Scrollbar at the left and if there is a cell or menus they must ordered from the right.

 For Example
 in bdLeftToRight:
   Menu1, Menu2, Menu2
 in bdRightToLeft: 
   Menu3, Menu2, Menu1

That means not just Right Alignment; it is Right To Left Order. bdRightToLeft must effect the text reading as depend on the OS if it is supported, the sentence words take as like Cells

 in bdLeftToRight:
   Word1 Word2 Word2
 in bdRightToLeft: 
   Word3 Word2 Word1

We are reading the Word1 first then Word2, We read from the Right to Left, but Draw Text is more complicated than normal Cells; in fact the function that draws the text cares about English/Latin words and draws it Left To Right, so if we mixed both languages in the same sentence, the draw function draws continual English words in Left To Right order to make it readable, but symbols like ? or ! are considered as Right To Left characters.

For testing purpose I used English word with a symbol to test Right To Left text reading

E.g. Word?

For special states or for more compatibility with Delphi there are other values (possible? used? handy?)

bdRightToLeftNoAlign

It is Right to Left but not reversed (except the Alignment property), but in Lazarus worked when use FlipControls

bdRightToLeftReadingOnly

It is Right to Left text reading only; scrollbar and alignment do not take effect (??? remain left to right???).

ParentBidiMode

If this is True the control inherits the BidiMode value from its parent.

BidiMode for developers

When you build you own controls and try to make your control support Right to Left or BidiMode, you must not access the BiDiMode property directly. Instead you must use

function UseRightToLeftAlignment: Boolean; virtual;
function UseRightToLeftReading: Boolean; virtual;
function UseRightToLeftScrollBar: Boolean;
function IsRightToLeft: Boolean;

Because some controls do not need to make changes to the alignment - for example TButton it always is centered, or TMainMenu which always is right aligned if it is Right to Left.


Not just reading the words and cells, everything depends on the order (positions). For an application there is a Menus and Controls that when placed on the form must have positions starting from the right. It is like looking at a LeftToRight Form in a mirror. The flow of the Form must be from Right To Left.

Sample 1

Add BidiMode support to LCL components

Most OSes now support RightToLeft but there are controls that are not supported yet or there is controls made by native language (custom drawn?) such as (TLabel, TGrid). So we have 3 kinds of control:

  1. Standard Controls: it is easy to enable RTL support: just add some flag to switch it to RightToLeft (TEdit, TList, TComboBox, TCheckBox).
  2. Standard Controls not supported by the OS: There is no idea just waiting the OS developers to implement it to support (TListView, TTreeView), or use native controls that already support it.
  3. Native Controls: hard work is needed to make it Support RightToLeft, or we must add new controls that already have this features (TLabel, TGrid).

Add BidiMode property to TControl

TBidiMode is already declared in Classes

property BiDiMode: TBiDiMode read FBiDiMode write SetBiDiMode stored IsBiDiModeStored;
property ParentBiDiMode: Boolean read FParentBiDiMode write SetParentBiDiMode default True;

BidiMode must not be stored if ParentBidiMode = True

function TControl.IsBiDiModeStored: Boolean;
begin
  Result := not ParentBiDiMode;
end;

Override BidiMode functions

function IsRightToLeft:Boolean; override;
function UseRightToLeftAlignment: Boolean; override;
function UseRightToLeftReading: Boolean; override;
function UseRightToLeftScrollBar: Boolean; override;

BiDiMode property is public and must published in every control need to RightToLeft

Bidi Controls Road Map

GTK2 You must build LCL with OPT=-dGTK2 OPT=-dGTK_2_8


Component win32 gtk2 carbon qt wince cocoa
TForm Working Working Not Implemented Not Implemented Not Implemented Not Implemented
TLabel Working Working Not Implemented Not Implemented Not Implemented Not Implemented
TButton Working Partially Implemented Not Implemented Not Implemented Not Implemented Not Implemented
TEdit Working Partially Implemented Not Implemented Not Implemented Not Implemented Not Implemented
TListBox Working Partially Implemented Not Implemented Not Implemented Not Implemented Not Implemented
TComboBox Working Partially Implemented Not Implemented Not Implemented Not Implemented Not Implemented
TCheckBox Working Working Not Implemented Not Implemented Not Implemented Not Implemented
TStaticText Working Partially Implemented Not Implemented Not Implemented Not Implemented Not Implemented
TGroupBox Working Working Not Implemented Not Implemented Not Implemented Not Implemented
TRadioButton Working Working Not Implemented Not Implemented Not Implemented Not Implemented
Menus Working Working Not Implemented Not Implemented Not Implemented Not Implemented
TBitBtn Partially Implemented Working Not Implemented Not Implemented Not Implemented Not Implemented
TSpeedBtn Working Working Not Implemented Not Implemented Not Implemented Not Implemented
TRadioGroup Working Working Not Implemented Not Implemented Not Implemented Not Implemented
TCheckGroup Working Working Not Implemented Not Implemented Not Implemented Not Implemented
TGrid Working Partially Implemented Not Implemented Not Implemented Not Implemented Not Implemented

Functions and Utils useful for multi language application

 TControl.FlipControls; virtual
 Change Right to Left and Left to Right in this properties (Left, Align, Anchors and Alignment)

How to test RightToLeft reading

Create a new form, then add a TButton or TEdit. Set the caption to OK?. You must add ? or ! or . to the last word without a space. Now set the BidiMode to bdRightToLeft, and you should see the ? in the left of word if it works.

BiDi support of the widgetsets

Carbon

Write me.

fpGui

Write me.

Gtk1

Has almost no BiDi support. Is deprecated by Lazarus anyway, so no new development at all.

Gtk2

In TMemo, TEdit, TComboBox the popup menu contains the two sub menus Input Methods and Insert Unicode Control character. Gtk2 uses for text rendering the Pango library, which has excellent Unicode support.

Qt

Write me.

Windows

In Windows 7 you can switch in TMemo, TEdit, TComboBox the base direction with Left-Ctrl+Right- Shift to RTL (right to left) and Right-Ctrl+Left- Shift to LTR (left to right). This also works in the IDE for most things, like in the Property Editor and the Strings Editor Dialog.

References