PageRenderTime 19ms CodeModel.GetById 12ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

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