BidiUtils.pas 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. unit BidiUtils;
  2. {
  3. Inno Setup
  4. Copyright (C) 1997-2025 Jordan Russell
  5. Portions by Martijn Laan
  6. For conditions of distribution and use, see LICENSE.TXT.
  7. Bidi utility functions
  8. }
  9. interface
  10. uses
  11. Windows, SysUtils, Messages, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;
  12. procedure FlipControls(const AParentCtl: TWinControl);
  13. procedure FlipRect(var Rect: TRect; const ParentRect: TRect; const UseRightToLeft: Boolean);
  14. function IsParentFlipped(const AControl: TControl): Boolean;
  15. var
  16. { This callback should be set by the caller. Inno Setup: set by the Setup.SetupForm unit: }
  17. IsParentFlippedFunc: function(AControl: TControl): Boolean;
  18. implementation
  19. uses
  20. WinXPanels;
  21. procedure FlipRect(var Rect: TRect; const ParentRect: TRect; const UseRightToLeft: Boolean);
  22. var
  23. W: Integer;
  24. begin
  25. if UseRightToLeft then begin
  26. W := Rect.Right - Rect.Left;
  27. Rect.Left := ParentRect.Right - (Rect.Left - ParentRect.Left) - W;
  28. Rect.Right := Rect.Left + W;
  29. end;
  30. end;
  31. function IsParentFlipped(const AControl: TControl): Boolean;
  32. begin
  33. if Assigned(IsParentFlippedFunc) then
  34. Result := IsParentFlippedFunc(AControl)
  35. else
  36. Result := False;
  37. end;
  38. type
  39. TControlAccess = class(TControl);
  40. procedure FlipControls(const AParentCtl: TWinControl);
  41. begin
  42. if AParentCtl.ControlCount = 0 then
  43. Exit;
  44. AParentCtl.DisableAlign;
  45. try
  46. if AParentCtl is TStackPanel then begin
  47. const StackPanel = AParentCtl as TStackPanel;
  48. if StackPanel.Orientation = spoHorizontal then begin
  49. const Count = StackPanel.ControlCount;
  50. if Count > 1 then begin
  51. StackPanel.ControlCollection.BeginUpdate;
  52. try
  53. { Move the control at the back to the front, and the one before it to after it, etc }
  54. for var I := 0 to Count-2 do
  55. StackPanel.ControlCollection[Count-1].Index := I;
  56. finally
  57. StackPanel.ControlCollection.EndUpdate;
  58. end;
  59. end;
  60. end;
  61. end else begin
  62. const ParentWidth = AParentCtl.ClientWidth;
  63. for var I := 0 to AParentCtl.ControlCount-1 do begin
  64. const Ctl = AParentCtl.Controls[I];
  65. if (akLeft in Ctl.Anchors) and not (akRight in Ctl.Anchors) then
  66. Ctl.Anchors := Ctl.Anchors - [akLeft] + [akRight]
  67. else if not (akLeft in Ctl.Anchors) and (akRight in Ctl.Anchors) then begin
  68. { Before we can set Anchors to [akLeft, akTop] (which has a special
  69. 'no anchors' meaning to VCL), we first need to update the Explicit*
  70. properties so the control doesn't get moved back to an old position. }
  71. if Ctl.Anchors = [akTop, akRight] then
  72. TControlAccess(Ctl).UpdateExplicitBounds;
  73. Ctl.Anchors := Ctl.Anchors - [akRight] + [akLeft];
  74. end;
  75. Ctl.Left := ParentWidth - Ctl.Width - Ctl.Left;
  76. end;
  77. end;
  78. finally
  79. AParentCtl.EnableAlign;
  80. end;
  81. for var I := 0 to AParentCtl.ControlCount-1 do
  82. if AParentCtl.Controls[I] is TWinControl then
  83. FlipControls(TWinControl(AParentCtl.Controls[I]));
  84. end;
  85. end.