/examples/autosize/childsizinglayout/mainunit.pas

http://github.com/graemeg/lazarus · Pascal · 111 lines · 65 code · 18 blank · 28 comment · 6 complexity · 1738f5cb2483cc03308d0b9c9d8e9308 MD5 · raw file

  1. { Copyright (C) 2005 Mattias Gaertner
  2. This source is free software; you can redistribute it and/or modifyit under
  3. the terms of the GNU General Public License as published bythe Free Software
  4. Foundation; either version 2 of the License, or(at your option) any later
  5. version.
  6. This code is distributed in the hope that it will be useful, butWITHOUT ANY
  7. WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR
  8. A PARTICULAR PURPOSE. See the GNUGeneral Public License for more details.
  9. A copy of the GNU General Public License is available on the WorldWide Web
  10. at <http://www.gnu.org/copyleft/gpl.html>. You can alsoobtain it by writing
  11. to the Free Software Foundation,Inc., 59 Temple Place - Suite 330, Boston,
  12. MA 02111-1307, USA.
  13. ---------------------------------------------------------------------------
  14. Abstract:
  15. Demonstrates LCL TWinControl.ChildSizing.Layout property.
  16. }
  17. unit MainUnit;
  18. {$mode objfpc}{$H+}
  19. interface
  20. uses
  21. Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls,
  22. Buttons, RTTICtrls, ExtCtrls, RTTIGrids;
  23. type
  24. { TChildsizingLayoutDemoForm }
  25. TChildsizingLayoutDemoForm = class(TForm)
  26. ChildSizingGroupBox: TGroupBox;
  27. LayoutGroupBox: TGroupBox;
  28. LayoutLabel: TLabel;
  29. LayoutTIRadioGroup: TTIRadioGroup;
  30. ButtonCountRadioGroup: TRadioGroup;
  31. ControlsPerLineTIRadioGroup: TTIRadioGroup;
  32. ChildSizingTIPropertyGrid: TTIPropertyGrid;
  33. procedure ButtonCountRadioGroupClick(Sender: TObject);
  34. procedure FormCreate(Sender: TObject);
  35. private
  36. public
  37. procedure SetButtonCount(NewCount: integer);
  38. end;
  39. var
  40. ChildsizingLayoutDemoForm: TChildsizingLayoutDemoForm;
  41. implementation
  42. {$R *.lfm}
  43. { TChildsizingLayoutDemoForm }
  44. procedure TChildsizingLayoutDemoForm.FormCreate(Sender: TObject);
  45. begin
  46. LayoutTIRadioGroup.Link.SetObjectAndProperty(LayoutGroupBox.ChildSizing,'Layout');
  47. ControlsPerLineTIRadioGroup.Link.SetObjectAndProperty(LayoutGroupBox.ChildSizing,'ControlsPerLine');
  48. ChildSizingTIPropertyGrid.TIObject:=LayoutGroupBox.ChildSizing;
  49. SetButtonCount(3);
  50. end;
  51. procedure TChildsizingLayoutDemoForm.SetButtonCount(NewCount: integer);
  52. var
  53. i: Integer;
  54. x: Integer;
  55. begin
  56. if NewCount=LayoutGroupBox.ControlCount then exit;
  57. if ButtonCountRadioGroup.Items.IndexOf(IntToStr(NewCount))<0 then
  58. NewCount:=StrToIntDef(ButtonCountRadioGroup.Items[2],3);
  59. LayoutGroupBox.DisableAlign;
  60. // create buttons
  61. for i:=0 to NewCount-1 do begin
  62. if LayoutGroupBox.ControlCount=i then begin
  63. with TButton.Create(Self) do begin
  64. if LayoutGroupBox.ChildSizing.Layout=cclNone then begin
  65. x:=i*20;
  66. SetBounds(x,x,Width,Height);
  67. end;
  68. Name:='Button'+IntToStr(i);
  69. Parent:=LayoutGroupBox;
  70. end;
  71. end;
  72. // set a caption of various length
  73. LayoutGroupBox.Controls[i].Caption:=
  74. copy(LayoutGroupBox.Controls[i].Name,1,(i mod 5)+3);
  75. end;
  76. // free unneeded buttons
  77. while LayoutGroupBox.ControlCount>NewCount do
  78. LayoutGroupBox.Controls[LayoutGroupBox.ControlCount-1].Free;
  79. LayoutGroupBox.EnableAlign;
  80. // make sure ButtonCountRadioGroup shows the correct count
  81. ButtonCountRadioGroup.ItemIndex:=
  82. ButtonCountRadioGroup.Items.IndexOf(IntToStr(NewCount));
  83. end;
  84. procedure TChildsizingLayoutDemoForm.ButtonCountRadioGroupClick(Sender: TObject);
  85. begin
  86. SetButtonCount(StrToIntDef(
  87. ButtonCountRadioGroup.Items[ButtonCountRadioGroup.ItemIndex],3));
  88. end;
  89. end.