TRadioGroup and TCheckGroup are now using ChildSizing properties

git-svn-id: trunk@8619 -
This commit is contained in:
mattias 2006-01-25 22:48:01 +00:00
parent 6c6535d395
commit 4572967898
4 changed files with 85 additions and 131 deletions

View File

@ -640,13 +640,13 @@ type
procedure Clicked(Sender: TObject);
procedure Changed(Sender: TObject);
procedure ItemEnter(Sender: TObject);
procedure PositionButtons;
procedure UpdateTabStops;
procedure SetAutoFill(const AValue: Boolean);
procedure SetColumnLayout(const AValue: TColumnLayout);
procedure ItemExit(Sender: TObject);
procedure ItemKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
procedure ItemResize(Sender: TObject);
procedure UpdateControlsPerLine;
protected
procedure InitializeWnd; override;
procedure UpdateRadioButtonStates; virtual;
@ -719,6 +719,7 @@ type
TCustomCheckGroup = class(TCustomGroupBox)
private
FAutoFill: boolean;
FButtonList: TList; // list of TCheckBox
FColumnLayout: TColumnLayout;
FCreatingWnd: boolean;
@ -729,12 +730,13 @@ type
function GetCheckEnabled(Index: integer): boolean;
procedure Clicked(Sender: TObject);
procedure DoClick(Index: integer);
procedure DoPositionButtons;
procedure ItemsChanged (Sender : TObject);
procedure SetAutoFill(const AValue: boolean);
procedure SetChecked(Index: integer; const AValue: boolean);
procedure SetCheckEnabled(Index: integer; const AValue: boolean);
procedure SetColumnLayout(const AValue: TColumnLayout);
procedure UpdateItems;
procedure UpdateControlsPerLine;
protected
procedure SetItems(Value: TStrings);
procedure SetColumns(Value: integer);
@ -748,6 +750,7 @@ type
destructor Destroy; override;
function Rows: integer;
public
property AutoFill: boolean read FAutoFill write SetAutoFill;
property Items: TStrings read FItems write SetItems;
property Checked[Index: integer]: boolean read GetChecked write SetChecked;
property CheckEnabled[Index: integer]: boolean read GetCheckEnabled write SetCheckEnabled;

View File

@ -32,6 +32,8 @@ begin
FButtonList := TList.Create;
FColumnLayout := clHorizontalThenVertical;
FColumns := 1;
ChildSizing.Layout:=cclLeftToRightThenTopToBottom;
ChildSizing.ControlsPerLine:=FColumns;
SetInitialBounds(0,0,250,200);
end;
@ -45,11 +47,30 @@ end;
procedure TCustomCheckGroup.ItemsChanged(Sender: TObject);
begin
UpdateItems;
UpdateControlsPerLine;
// TODO: Remove RecreateWnd
if HandleAllocated then RecreateWnd(Self);
OwnerFormDesignerModified(Self);
end;
procedure TCustomCheckGroup.SetAutoFill(const AValue: boolean);
begin
if FAutoFill=AValue then exit;
FAutoFill:=AValue;
DisableAlign;
try
if FAutoFill then begin
ChildSizing.EnlargeHorizontal:=crsHomogenousSpaceResize;
ChildSizing.EnlargeVertical:=crsHomogenousSpaceResize;
end else begin
ChildSizing.EnlargeHorizontal:=crsAnchorAligning;
ChildSizing.EnlargeVertical:=crsAnchorAligning;
end;
finally
EnableAlign;
end;
end;
procedure TCustomCheckGroup.Clicked(Sender: TObject);
var
Index: Integer;
@ -93,8 +114,18 @@ begin
CheckBox:=TCheckBox(FButtonList[i]);
CheckBox.Caption:=FItems[i];
end;
DoPositionButtons;
end;
procedure TCustomCheckGroup.UpdateControlsPerLine;
var
NewControlsPerLine: LongInt;
begin
if ChildSizing.Layout=cclLeftToRightThenTopToBottom then
NewControlsPerLine:=Max(1,FColumns)
else
NewControlsPerLine:=((FItems.Count-1) div Max(1,FColumns))+1;
ChildSizing.ControlsPerLine:=NewControlsPerLine;
//DebugLn('TCustomCheckGroup.UpdateControlsPerLine ',dbgs(ChildSizing.Layout=cclLeftToRightThenTopToBottom),' ',dbgs(ChildSizing.ControlsPerLine));
end;
function TCustomCheckGroup.GetCheckEnabled(Index: integer): boolean;
@ -116,7 +147,11 @@ procedure TCustomCheckGroup.SetColumnLayout(const AValue: TColumnLayout);
begin
if FColumnLayout=AValue then exit;
FColumnLayout:=AValue;
DoPositionButtons;
if FColumnLayout=clHorizontalThenVertical then
ChildSizing.Layout:=cclLeftToRightThenTopToBottom
else
ChildSizing.Layout:=cclTopToBottomThenLeftToRight;
UpdateControlsPerLine;
end;
function TCustomCheckGroup.GetChecked(Index: integer): boolean;
@ -126,50 +161,6 @@ begin
Result:=TCheckBox(FButtonList[Index]).Checked;
end;
procedure TCustomCheckGroup.DoPositionButtons;
var
i : integer;
CheckBox: TCheckBox;
nextTop : integer;
nextLeft: integer;
vertDist: integer;
horzDist: integer;
rbWidth : integer;
MaxRows: Integer;
begin
if (FItems<>nil) and (FItems.Count>0) and (FColumns>0) then begin
// position in rows and columns
vertDist := (Height - 20) DIV (((FItems.Count-1) DIV FColumns)+1);
horzDist := (Width - 20) DIV FColumns;
nextTop := 0;
nextLeft := 10;
rbWidth := horzDist;
MaxRows := (FItems.Count+FColumns-1) div FColumns;
i := 0;
while i < FItems.Count do begin
CheckBox := TCheckBox(FButtonList[i]);
CheckBox.SetBounds(nextLeft,nextTop,rbWidth,vertDist);
inc (i);
if FColumnLayout=clHorizontalThenVertical then begin
if (i mod FColumns) = 0 then begin
inc(nextTop, vertDist);
nextLeft := 10;
end else begin
inc(nextLeft, horzDist);
end;
end else begin
if (i mod MaxRows) = 0 then begin
inc(nextLeft, horzDist);
nextTop := 0;
end else begin
inc(nextTop, vertDist);
end;
end;
end;
end;
end;
procedure TCustomCheckGroup.SetChecked(Index: integer; const AValue: boolean);
begin
if (Index < -1) or (Index >= FItems.Count) then
@ -200,9 +191,7 @@ begin
if (Value < 1)
then raise Exception.Create('TCustomCheckGroup: Columns must be >= 1');
FColumns := Value;
DoPositionButtons;
// TODO: Remove RecreateWnd
if HandleAllocated then RecreateWnd(self);
UpdateControlsPerLine;
end;
end;
@ -260,7 +249,6 @@ end;
procedure TCustomCheckGroup.DoOnResize;
begin
DoPositionButtons;
inherited DoOnResize;
end;

View File

@ -71,6 +71,8 @@ begin
FButtonList := TList.Create;
FColumns := 1;
FColumnLayout := clHorizontalThenVertical;
ChildSizing.Layout:=cclLeftToRightThenTopToBottom;
ChildSizing.ControlsPerLine:=FColumns;
SetInitialBounds(0,0,250,200);
TabStop := True;
end;
@ -147,7 +149,6 @@ begin
Temp := TRadioButton(FButtonList[i]);
Temp.Caption := FItems[i];
Temp.Parent:=Self;
Temp.SetZOrder(false);
end;
with FHiddenButton do begin
FHiddenButton.Visible:=false;
@ -155,8 +156,6 @@ begin
FHiddenButton.HandleNeeded;
end;
PositionButtons;
for i:=0 to FItems.Count-1 do begin
Temp := TRadioButton(FButtonList[i]);
Temp.Checked := (i = FItemIndex);
@ -190,7 +189,19 @@ end;
procedure TCustomRadioGroup.ItemResize(Sender: TObject);
begin
PositionButtons;
end;
procedure TCustomRadioGroup.UpdateControlsPerLine;
var
NewControlsPerLine: LongInt;
begin
if ChildSizing.Layout=cclLeftToRightThenTopToBottom then
NewControlsPerLine:=Max(1,FColumns)
else
NewControlsPerLine:=((FItems.Count-1) div Max(1,FColumns))+1;
ChildSizing.ControlsPerLine:=NewControlsPerLine;
//DebugLn('TCustomRadioGroup.UpdateControlsPerLine ',dbgs(ChildSizing.ControlsPerLine),' ',dbgs(NewControlsPerLine),' FColumns=',dbgs(FColumns),' FItems.Count=',dbgs(FItems.Count),' ',dbgs(ChildSizing.Layout=cclLeftToRightThenTopToBottom));
end;
procedure TCustomRadioGroup.ItemKeyDown(Sender: TObject; var Key: Word;
@ -207,13 +218,13 @@ procedure TCustomRadioGroup.ItemKeyDown(Sender: TObject; var Key: Word;
Count := FButtonList.Count;
if FColumnLayout=clHorizontalThenVertical then begin
//add a row for ease wrapping
BlockSize := FColumns * (Rows+1);
StepSize := HorzDiff + VertDiff * FColumns;
BlockSize := Columns * (Rows+1);
StepSize := HorzDiff + VertDiff * Columns;
WrapOffSet := VertDiff;
end
else begin
//add a column for ease wrapping
BlockSize := (FColumns+1) * Rows;
BlockSize := (Columns+1) * Rows;
StepSize := HorzDiff * Rows + VertDiff;
WrapOffSet := HorzDiff;
end;
@ -248,6 +259,7 @@ end;
procedure TCustomRadioGroup.ItemsChanged (Sender : TObject);
begin
// TODO: Remove RecreateWnd
UpdateControlsPerLine;
if HandleAllocated and (not (csLoading in ComponentState)) then
RecreateWnd(Self);
OwnerFormDesignerModified(Self);
@ -262,14 +274,13 @@ end;
which the radiobuttons should be arranged.
Range: 1 .. ???
------------------------------------------------------------------------------}
procedure TCustomRadioGroup.SetColumns (value : integer);
procedure TCustomRadioGroup.SetColumns(value : integer);
begin
if Value <> FColumns then begin
if (Value < 1)
then raise Exception.Create('TCustomRadioGroup: Columns must be >= 1');
FColumns := Value;
if HandleAllocated and (not (csLoading in ComponentState)) then
PositionButtons;
UpdateControlsPerLine;
end;
end;
@ -285,6 +296,7 @@ begin
if (Value <> FItems) then
begin
FItems.Assign(Value);
UpdateControlsPerLine;
// TODO: Remove RecreateWnd
if HandleAllocated and (not (csLoading in ComponentState)) then
RecreateWnd(Self);
@ -365,7 +377,6 @@ end;
------------------------------------------------------------------------------}
procedure TCustomRadioGroup.Resize;
begin
if HandleAllocated then PositionButtons;
inherited Resize;
end;
@ -435,69 +446,6 @@ Begin
CheckItemIndexChanged;
end;
{------------------------------------------------------------------------------
Method: TCustomRadioGroup.PositionButtons
Params: none
Set bounds of radio buttons
------------------------------------------------------------------------------}
procedure TCustomRadioGroup.PositionButtons;
var
i: integer;
CurButton: TRadioButton;
nextTop: integer;
nextLeft: integer;
vertDist: integer;
horzDist: integer;
MaxRows: Integer;
UsedColumnCount: integer;
UsedRowCount: integer;
begin
if FButtonList.Count=0 then exit;
if TRadioButton(FButtonList[0]).AutoSizeDelayed then exit;
DisableAutoSizing;
try
UsedColumnCount:=FColumns;
if UsedColumnCount<1 then UsedColumnCount:=1;
if AutoFill and (UsedColumnCount>FButtonList.Count) then
UsedColumnCount:=FButtonList.Count;
UsedRowCount:=((FButtonList.Count-1) div UsedColumnCount)+1;
// position in rows and columns
vertDist := (Height - 20) DIV UsedRowCount;
horzDist := (Width - 20) DIV UsedColumnCount;
nextTop := 0;
nextLeft := 10;
MaxRows := (FButtonList.Count+FColumns-1) div FColumns;
i := 0;
while i < FButtonList.Count do begin
CurButton := TRadioButton(FButtonList[i]);
CurButton.SetBounds(nextLeft,nextTop,CurButton.Width,CurButton.Height);
inc (i);
if FColumnLayout=clHorizontalThenVertical then begin
if (i mod FColumns) = 0 then begin
inc(nextTop, vertDist);
nextLeft := 10;
end else begin
inc(nextLeft, horzDist);
end;
end else begin
if (i mod MaxRows) = 0 then begin
inc(nextLeft, horzDist);
nextTop := 0;
end else begin
inc(nextTop, vertDist);
end;
end;
end;
finally
EnableAutoSizing;
end;
end;
procedure TCustomRadioGroup.UpdateTabStops;
var
i: Integer;
@ -515,14 +463,29 @@ procedure TCustomRadioGroup.SetAutoFill(const AValue: Boolean);
begin
if FAutoFill=AValue then exit;
FAutoFill:=AValue;
PositionButtons;
DisableAlign;
try
if FAutoFill then begin
ChildSizing.EnlargeHorizontal:=crsHomogenousSpaceResize;
ChildSizing.EnlargeVertical:=crsHomogenousSpaceResize;
end else begin
ChildSizing.EnlargeHorizontal:=crsAnchorAligning;
ChildSizing.EnlargeVertical:=crsAnchorAligning;
end;
finally
EnableAlign;
end;
end;
procedure TCustomRadioGroup.SetColumnLayout(const AValue: TColumnLayout);
begin
if FColumnLayout=AValue then exit;
FColumnLayout:=AValue;
PositionButtons;
if FColumnLayout=clHorizontalThenVertical then
ChildSizing.Layout:=cclLeftToRightThenTopToBottom
else
ChildSizing.Layout:=cclTopToBottomThenLeftToRight;
UpdateControlsPerLine;
end;
{------------------------------------------------------------------------------

View File

@ -496,8 +496,8 @@ var
crsHomogenousSpaceResize:
if ChildCount[Orientation]>0 then begin
CurScale:=double(TargetSize);
CurOffset:=TargetSize;
Factor.Scale:=double(TargetSize);
Factor.Offset:=TargetSize;
ResizeableCount:=ChildCount[Orientation]+1;
end;