BidiMode

From Free Pascal wiki
Jump to navigationJump to search

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.

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.

TODO:picture of sample to compare

Add BidiMode or RightToLeft support to LCL components

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). so we have 3 kinds of control

  1. Standard Controls: it easy to make it just add some flag to switch it to RightToLeft (TEdit, TList, TComboBox, TCheckBox).
  2. 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.
  3. 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

Phase 1: Add BidiMode property to TControl

TBidiMode already declaired in Classes

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

BidiMode must not stored if ParentBidiMode = True

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

Add virtual functions

 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 but there is a Alignment property in TLabel, TEdit not have but we can implement an Alignment so i will not use this technique for now.

Phase 2: Modify Standard controls that supported by OS

Add RightToLeft to Standard controls,

 TButton {done}
 TEdit {done}
 TForm {done}
 TCheckBox
 TPanel
 TMenu/TMenuItem {there is a bug in windows}
   [1]
   With Menus in Lazarus Win32 the developers force to make it OWNERDRAW so that mean Menus not complete to be RightToLeft before jump to phase 3
 
 

Phase 3: Canvas and Graphic

Native controls use a Canvas to draw it self, small control use TextOut, but the big one must redraw it from the Right.

TODO: add image for right to left Grid.

 TLabel
 TGrid

Before make TLabel work RightToLeft we must add to Canvas.TextStyle a new property ("TextDirection", "RightToLeft" or "RTLReading" i must ask the LCL developer to choose one), TextOut and TextRect functions will use this property.

Delphi use CanvasOrientation for mirror the control and maked RightToLeft, i hate use this becuase it flip the shadow and images (like as the Check) it is make the control ugly.

 For now TGrid we can wait.
 

Phase 4: Functions and Utils useful for multi language application

Like as

 FlipControls; virtual;
 We must care about Anchors
 Detect language is RightToLeft from current language

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.