improved autosizing of propmtpdialog

git-svn-id: trunk@7631 -
This commit is contained in:
mattias 2005-09-06 08:37:01 +00:00
parent 1419d84e3d
commit 48d7da49f8

View File

@ -210,23 +210,49 @@ end;
procedure TPromptDialog.LayoutDialog; procedure TPromptDialog.LayoutDialog;
Const Const
AVGBuffer : PChar = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890()|_ '; //AVGBuffer : PChar = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890()|_ ';
cBtnCalcWidth = 50; cBtnCalcWidth = 50;
cBtnCalcHeight = 13; cBtnCalcHeight = 13;
cBtnCalcSpace = 4; cBtnCalcSpace = 4;
cBtnCalcBorder = 4;
cBtnDist = 10;
var var
curBtn : Longint; // variable to loop through TMsgDlgButtons curBtn : Longint; // variable to loop through TMsgDlgButtons
cBtnWidth,
cBtnHeight,
curBtnWidth,
cMinLeft, cMinLeft,
cBtnDist,
ButtonLeft : integer; // left position of button(s) ButtonLeft : integer; // left position of button(s)
reqBtnWidth : integer; // width neccessary to display buttons reqBtnWidth : integer; // width neccessary to display buttons
reqWidth, reqHeight : integer; // width and height neccessary to display all reqWidth, reqHeight : integer; // width and height neccessary to display all
i : integer; i : integer;
ButtonIndex : integer; ButtonIndex : integer;
Avg : TPoint; MinBtnWidth: Integer; // minimum width for a single button
MinBtnHeight: Integer; // minimum height for a single button
CurButton: TBitBtn;
ButtonTop: Integer;
CurBtnSize: TPoint;
function GetButtonSize(AButton: TBitBtn): TPoint;
var
curBtnSize: tagSIZE;
begin
curBtnSize:=Canvas.TextExtent(AButton.Caption);
inc(curBtnSize.cx,AButton.Glyph.Width+4);
if curBtnSize.cy<AButton.Glyph.Height then
curBtnSize.cy:=AButton.Glyph.Height;
//debugln('GetButtonSize A ',AButton.Caption,' ',dbgs(curBtnSize.cx),',',dbgs(curBtnSize.cy));
inc(curBtnSize.cx,2*cBtnCalcBorder);
inc(curBtnSize.cy,2*cBtnCalcBorder);
if MinBtnHeight<curBtnSize.cy then
MinBtnHeight:=curBtnSize.cy
else if curBtnSize.cy<MinBtnHeight then
curBtnSize.cy:=MinBtnHeight;
if curBtnSize.cx < MinBtnWidth then
curBtnSize.cx := MinBtnWidth;
Result:=Point(curBtnSize.cx,curBtnSize.cy);
//debugln('GetButtonSize ',AButton.Caption,' ',dbgs(Result));
end;
begin begin
FillChar(TextStyle, SizeOf(TTextStyle), 0); FillChar(TextStyle, SizeOf(TTextStyle), 0);
@ -242,57 +268,18 @@ begin
If MSG = '' then If MSG = '' then
MSG := ' '; MSG := ' ';
// calculate the needed size for the text
TextBox := Rect(0,0, Screen.Width div 2,Screen.Height - 100); TextBox := Rect(0,0, Screen.Width div 2,Screen.Height - 100);
SelectObject(Canvas.Handle, GetStockObject(DEFAULT_GUI_FONT)); SelectObject(Canvas.Handle, GetStockObject(DEFAULT_GUI_FONT));
DrawText(Canvas.Handle, PChar(MSG), Length(MSG), DrawText(Canvas.Handle, PChar(MSG), Length(MSG),
TextBox, DT_WORDBREAK or DT_INTERNAL or DT_CALCRECT); TextBox, DT_WORDBREAK or DT_INTERNAL or DT_CALCRECT);
// calculate the width we need to display the buttons // calculate the width we need to display the buttons
GetTextExtentPoint(Canvas.Handle,AVGBuffer,StrLen(AVGBuffer),TSize(AVG)); MinBtnWidth:=75;
AVG.X := AVG.X div 52; MinBtnHeight:=25;
reqBtnWidth := 0; reqBtnWidth := 0;
cBtnWidth := (cBtnCalcWidth*Avg.X) div 5;
cBtnHeight := (cBtnCalcHeight*AVG.Y) div 8;
cBtnDist := (cBtnCalcSpace * Avg.X) div 4;
for curBtn := 0 to NumButtons - 1 do
begin
If (Buttons[curBtn] >= Low(DialogButtonKind)) and
(Buttons[curBtn] <= High(DialogButtonKind))
then
begin
curBtnWidth := Canvas.TextWidth(GetDialogButtonText(Buttons[curBtn]));
if curBtnWidth > cBtnWidth then
cBtnWidth := curBtnWidth;
Inc(reqBtnWidth, cBtnWidth + cBtnDist)
end;
end;
if reqBtnWidth > 0 then Dec(reqBtnWidth, cBtnDist);
Inc(cBtnDist, cBtnWidth);
// patch positions to center label and buttons // create the buttons, without positioning
reqWidth:= reqBtnWidth;
If FBitmap <> nil then
cMinLeft := cBitmapX + max(32,FBitmap.Width) + cLabelSpacing
else
cMinLeft := cLabelSpacing;
if reqWidth < (TextBox.Right + cMinLeft) then reqWidth:= TextBox.Right + cMinLeft;
ButtonLeft := ((reqWidth - reqBtnWidth) div 2) + cLabelSpacing;
reqHeight:= max(TextBox.Bottom, 32);
if (FBitmap <> nil) and (FBitmap.Height > reqHeight) then
reqHeight := FBitmap.Height;
OffsetRect(TextBox, ((reqWidth - cMinLeft - TextBox.Right) div 2) + cMinLeft, cLabelSpacing);
// set size of form
SetBounds(Left, Top, reqWidth + 2 * cLabelSpacing,
3 * cLabelSpacing + reqHeight + cBtnHeight);
// create the buttons
ButtonIndex := -1; ButtonIndex := -1;
for curBtn := 0 to NumButtons - 1 do for curBtn := 0 to NumButtons - 1 do
begin begin
@ -302,47 +289,83 @@ begin
begin begin
inc(ButtonIndex); inc(ButtonIndex);
with TBitBtn.Create(Self) do CurButton:=TBitBtn.Create(Self);
begin with CurButton do
Parent:= Self; begin
SetBounds (ButtonLeft, 2 * cLabelSpacing + reqHeight, cBtnWidth, cBtnHeight); Parent:= Self;
inc(ButtonLeft, cBtnDist); Layout := blGlyphLeft;
Layout := blGlyphLeft; OnKeyDown := @PromptDialogKeyDown;
OnKeyDown := @PromptDialogKeyDown; Case Buttons[curBtn] of
Case Buttons[curBtn] of idButtonYesToAll,
idButtonYesToAll, idButtonNoToAll :
idButtonNoToAll :
begin
Glyph.Handle := LoadStockPixmap(Buttons[curBtn]);
If Buttons[curBtn] = idButtonYesToAll then
begin begin
ModalResult := mrYesToAll; Glyph.Handle := LoadStockPixmap(Buttons[curBtn]);
Caption := rsmbYesToAll; If Buttons[curBtn] = idButtonYesToAll then
end begin
else ModalResult := mrYesToAll;
begin Caption := rsmbYesToAll;
ModalResult := mrNoToAll; end
Caption := rsmbNoToAll; else
begin
ModalResult := mrNoToAll;
Caption := rsmbNoToAll;
end;
end; end;
end; else
else Kind := DialogButtonKind[Buttons[curBtn]];
Kind := DialogButtonKind[Buttons[curBtn]]; end;
end; if Height < Glyph.Height + 5 then
if Height < Glyph.Height + 5 then Height := Glyph.Height + 5;
Height := Glyph.Height + 5;
if ButtonIndex = TheDefaultIndex then Default := true; if ButtonIndex = TheDefaultIndex then Default := true;
Visible:=true;
CurBtnSize:=GetButtonSize(CurButton);
if reqBtnWidth > 0 then inc(reqBtnWidth, cBtnDist);
Inc(reqBtnWidth, CurBtnSize.X);
end; end;
end; end;
end; end;
// calculate the complete width without the spacing around all
If FBitmap <> nil then
cMinLeft := cBitmapX + max(32,FBitmap.Width) + cLabelSpacing
else
cMinLeft := 0;
reqWidth := cMinLeft + TextBox.Right;
if reqWidth < reqBtnWidth then
reqWidth:= reqBtnWidth;
// center the text
OffsetRect(TextBox, cLabelSpacing + cMinLeft
+((reqWidth - cMinLeft - TextBox.Right) div 2),
cLabelSpacing);
// calculate the height of the text+icon
reqHeight:= max(TextBox.Bottom, 32);
if (FBitmap <> nil) and (FBitmap.Height > reqHeight) then
reqHeight := FBitmap.Height;
// set size of form
SetBounds(Left, Top, reqWidth + 2 * cLabelSpacing,
3 * cLabelSpacing + reqHeight + MinBtnHeight);
// calculate the left of the buttons
ButtonLeft := ((reqWidth - reqBtnWidth) div 2) + cLabelSpacing;
ButtonTop := reqHeight + 2*cLabelSpacing;
// position buttons and activate default
for i:=0 to ComponentCount-1 do begin for i:=0 to ComponentCount-1 do begin
if (Components[i] is TCustomBitBtn) if (Components[i] is TBitBtn) then begin
and (TCustomBitBtn(Components[i]).Default) then CurButton:=TBitBtn(Components[i]);
begin CurBtnSize:=GetButtonSize(CurButton);
ActiveControl:=TCustomBitBtn(Components[i]); CurButton.SetBounds(ButtonLeft,ButtonTop,CurBtnSize.X,CurBtnSize.Y);
DefaultControl:=TCustomBitBtn(Components[i]); inc(ButtonLeft,CurButton.Width+cBtnDist);
break;
if (CurButton.Default) then begin
ActiveControl:=CurButton;
DefaultControl:=CurButton;
end;
end; end;
end; end;
end; end;