mirror of
https://gitlab.com/freepascal.org/lazarus/lazarus.git
synced 2025-08-14 06:59:14 +02:00
implement preferred size calculation for buttons and bitbtns
seperate text size calculation into general function git-svn-id: trunk@7746 -
This commit is contained in:
parent
14358611e6
commit
122df55eae
@ -96,6 +96,7 @@ procedure DisableApplicationWindows(Window: HWND);
|
|||||||
procedure EnableApplicationWindows(Window: HWND);
|
procedure EnableApplicationWindows(Window: HWND);
|
||||||
procedure AddToChangedMenus(Window: HWnd);
|
procedure AddToChangedMenus(Window: HWnd);
|
||||||
procedure RedrawMenus;
|
procedure RedrawMenus;
|
||||||
|
function MeasureText(const AWinControl: TWinControl; Text: string; var Width, Height: integer): boolean;
|
||||||
|
|
||||||
type
|
type
|
||||||
PDisableWindowsInfo = ^TDisableWindowsInfo;
|
PDisableWindowsInfo = ^TDisableWindowsInfo;
|
||||||
@ -1073,6 +1074,26 @@ begin
|
|||||||
end;
|
end;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
|
function MeasureText(const AWinControl: TWinControl; Text: string; var Width, Height: integer): boolean;
|
||||||
|
var
|
||||||
|
textSize: Windows.SIZE;
|
||||||
|
winHandle: HWND;
|
||||||
|
canvasHandle: HDC;
|
||||||
|
oldFontHandle: HFONT;
|
||||||
|
begin
|
||||||
|
winHandle := AWinControl.Handle;
|
||||||
|
canvasHandle := GetDC(winHandle);
|
||||||
|
oldFontHandle := SelectObject(canvasHandle, AWinControl.Font.Handle);
|
||||||
|
Result := Windows.GetTextExtentPoint32(canvasHandle, PChar(Text), Length(Text), textSize);
|
||||||
|
if Result then
|
||||||
|
begin
|
||||||
|
Width := textSize.cx;
|
||||||
|
Height := textSize.cy;
|
||||||
|
end;
|
||||||
|
SelectObject(canvasHandle, oldFontHandle);
|
||||||
|
ReleaseDC(winHandle, canvasHandle);
|
||||||
|
end;
|
||||||
|
|
||||||
|
|
||||||
{$IFDEF ASSERT_IS_ON}
|
{$IFDEF ASSERT_IS_ON}
|
||||||
{$UNDEF ASSERT_IS_ON}
|
{$UNDEF ASSERT_IS_ON}
|
||||||
|
@ -56,6 +56,8 @@ type
|
|||||||
TWin32WSBitBtn = class(TWSBitBtn)
|
TWin32WSBitBtn = class(TWSBitBtn)
|
||||||
class function CreateHandle(const AWinControl: TWinControl;
|
class function CreateHandle(const AWinControl: TWinControl;
|
||||||
const AParams: TCreateParams): HWND; override;
|
const AParams: TCreateParams): HWND; override;
|
||||||
|
class procedure GetPreferredSize(const AWinControl: TWinControl;
|
||||||
|
var PreferredWidth, PreferredHeight: integer); override;
|
||||||
class procedure SetBounds(const AWinControl: TWinControl;
|
class procedure SetBounds(const AWinControl: TWinControl;
|
||||||
const ALeft, ATop, AWidth, AHeight: integer); override;
|
const ALeft, ATop, AWidth, AHeight: integer); override;
|
||||||
class procedure SetGlyph(const ABitBtn: TCustomBitBtn; const AValue: TBitmap); override;
|
class procedure SetGlyph(const ABitBtn: TCustomBitBtn; const AValue: TBitmap); override;
|
||||||
@ -78,7 +80,7 @@ procedure DrawBitBtnImage(BitBtn: TCustomBitBtn; ButtonCaption: PChar);
|
|||||||
implementation
|
implementation
|
||||||
|
|
||||||
uses
|
uses
|
||||||
Win32Int, InterfaceBase;
|
Win32Int, InterfaceBase, Win32Proc;
|
||||||
|
|
||||||
{ TWin32WSButton }
|
{ TWin32WSButton }
|
||||||
|
|
||||||
@ -433,6 +435,40 @@ begin
|
|||||||
Result := Params.Window;
|
Result := Params.Window;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
|
procedure TWin32WSBitBtn.GetPreferredSize(const AWinControl: TWinControl;
|
||||||
|
var PreferredWidth, PreferredHeight: integer);
|
||||||
|
var
|
||||||
|
BitmapInfo: BITMAP; // Buffer for bitmap
|
||||||
|
BitBtn: TBitBtn absolute AWinControl;
|
||||||
|
Glyph: TBitmap;
|
||||||
|
spacing: integer;
|
||||||
|
begin
|
||||||
|
if MeasureText(AWinControl, AWinControl.Caption, PreferredWidth, PreferredHeight) then
|
||||||
|
begin
|
||||||
|
Glyph := BitBtn.Glyph;
|
||||||
|
if not Glyph.Empty then
|
||||||
|
begin
|
||||||
|
Windows.GetObject(Glyph.Handle, sizeof(BitmapInfo), @BitmapInfo);
|
||||||
|
if BitBtn.Spacing = -1 then
|
||||||
|
spacing := 8
|
||||||
|
else
|
||||||
|
spacing := BitBtn.Spacing;
|
||||||
|
if BitBtn.Layout in [blGlyphLeft, blGlyphRight] then
|
||||||
|
begin
|
||||||
|
Inc(PreferredWidth, spacing + BitmapInfo.bmWidth);
|
||||||
|
if BitmapInfo.bmHeight > PreferredHeight then
|
||||||
|
PreferredHeight := BitmapInfo.bmHeight;
|
||||||
|
end else begin
|
||||||
|
Inc(PreferredHeight, spacing + BitmapInfo.bmHeight);
|
||||||
|
if BitmapInfo.bmWidth > PreferredWidth then
|
||||||
|
PreferredWidth := BitmapInfo.bmWidth;
|
||||||
|
end;
|
||||||
|
end;
|
||||||
|
Inc(PreferredWidth, 20);
|
||||||
|
Inc(PreferredHeight, 12);
|
||||||
|
end;
|
||||||
|
end;
|
||||||
|
|
||||||
procedure TWin32WSBitBtn.SetBounds(const AWinControl: TWinControl;
|
procedure TWin32WSBitBtn.SetBounds(const AWinControl: TWinControl;
|
||||||
const ALeft, ATop, AWidth, AHeight: integer);
|
const ALeft, ATop, AWidth, AHeight: integer);
|
||||||
begin
|
begin
|
||||||
|
@ -217,6 +217,8 @@ type
|
|||||||
private
|
private
|
||||||
protected
|
protected
|
||||||
public
|
public
|
||||||
|
class procedure GetPreferredSize(const AWinControl: TWinControl;
|
||||||
|
var PreferredWidth, PreferredHeight: integer); override;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
{ TWin32WSCustomCheckBox }
|
{ TWin32WSCustomCheckBox }
|
||||||
@ -989,6 +991,18 @@ begin
|
|||||||
RecreateWnd(ACustomStaticText);
|
RecreateWnd(ACustomStaticText);
|
||||||
end;
|
end;
|
||||||
|
|
||||||
|
{ TWin32WSButtonControl }
|
||||||
|
|
||||||
|
procedure TWin32WSButtonControl.GetPreferredSize(const AWinControl: TWinControl;
|
||||||
|
var PreferredWidth, PreferredHeight: integer);
|
||||||
|
begin
|
||||||
|
if MeasureText(AWinControl, AWinControl.Caption, PreferredWidth, PreferredHeight) then
|
||||||
|
begin
|
||||||
|
Inc(PreferredWidth, 20);
|
||||||
|
Inc(PreferredHeight, 12);
|
||||||
|
end;
|
||||||
|
end;
|
||||||
|
|
||||||
{ TWin32WSCustomCheckBox }
|
{ TWin32WSCustomCheckBox }
|
||||||
|
|
||||||
function TWin32WSCustomCheckBox.CreateHandle(const AWinControl: TWinControl;
|
function TWin32WSCustomCheckBox.CreateHandle(const AWinControl: TWinControl;
|
||||||
@ -1016,26 +1030,16 @@ end;
|
|||||||
procedure TWin32WSCustomCheckBox.GetPreferredSize(const AWinControl: TWinControl;
|
procedure TWin32WSCustomCheckBox.GetPreferredSize(const AWinControl: TWinControl;
|
||||||
var PreferredWidth, PreferredHeight: integer);
|
var PreferredWidth, PreferredHeight: integer);
|
||||||
var
|
var
|
||||||
textSize: Windows.SIZE;
|
iconHeight: integer;
|
||||||
winHandle: HWND;
|
|
||||||
canvasHandle: HDC;
|
|
||||||
oldFontHandle: HFONT;
|
|
||||||
text: string;
|
|
||||||
begin
|
begin
|
||||||
winHandle := AWinControl.Handle;
|
if MeasureText(AWinControl, AWinControl.Caption, PreferredWidth, PreferredHeight) then
|
||||||
canvasHandle := GetDC(winHandle);
|
|
||||||
oldFontHandle := SelectObject(canvasHandle, AWinControl.Font.Handle);
|
|
||||||
text := AWinControl.Caption;
|
|
||||||
if GetTextExtentPoint32(canvasHandle, PChar(text), Length(text), textSize) then
|
|
||||||
begin
|
begin
|
||||||
// ~5 pixels spacing between checkbox and text, and 2 pixels margin for rounding error
|
// ~5 pixels spacing between checkbox and text, and 2 pixels margin for rounding error
|
||||||
PreferredWidth := GetSystemMetrics(SM_CXMENUCHECK) - GetSystemMetrics(SM_CXBORDER) + textSize.cx + 7;
|
Inc(PreferredWidth, GetSystemMetrics(SM_CXMENUCHECK) - GetSystemMetrics(SM_CXBORDER) + 7);
|
||||||
PreferredHeight := GetSystemMetrics(SM_CYMENUCHECK) - GetSystemMetrics(SM_CYBORDER);
|
iconHeight := GetSystemMetrics(SM_CYMENUCHECK) - GetSystemMetrics(SM_CYBORDER);
|
||||||
if textSize.cy > PreferredHeight then
|
if iconHeight > PreferredHeight then
|
||||||
PreferredHeight := textSize.cy;
|
PreferredHeight := iconHeight;
|
||||||
end;
|
end;
|
||||||
SelectObject(canvasHandle, oldFontHandle);
|
|
||||||
ReleaseDC(winHandle, canvasHandle);
|
|
||||||
end;
|
end;
|
||||||
|
|
||||||
function TWin32WSCustomCheckBox.RetrieveState(const ACustomCheckBox: TCustomCheckBox): TCheckBoxState;
|
function TWin32WSCustomCheckBox.RetrieveState(const ACustomCheckBox: TCustomCheckBox): TCheckBoxState;
|
||||||
@ -1133,7 +1137,7 @@ initialization
|
|||||||
RegisterWSComponent(TCustomMemo, TWin32WSCustomMemo);
|
RegisterWSComponent(TCustomMemo, TWin32WSCustomMemo);
|
||||||
// RegisterWSComponent(TEdit, TWin32WSEdit);
|
// RegisterWSComponent(TEdit, TWin32WSEdit);
|
||||||
// RegisterWSComponent(TMemo, TWin32WSMemo);
|
// RegisterWSComponent(TMemo, TWin32WSMemo);
|
||||||
// RegisterWSComponent(TButtonControl, TWin32WSButtonControl);
|
RegisterWSComponent(TButtonControl, TWin32WSButtonControl);
|
||||||
RegisterWSComponent(TCustomCheckBox, TWin32WSCustomCheckBox);
|
RegisterWSComponent(TCustomCheckBox, TWin32WSCustomCheckBox);
|
||||||
// RegisterWSComponent(TCheckBox, TWin32WSCheckBox);
|
// RegisterWSComponent(TCheckBox, TWin32WSCheckBox);
|
||||||
// RegisterWSComponent(TCheckBox, TWin32WSCheckBox);
|
// RegisterWSComponent(TCheckBox, TWin32WSCheckBox);
|
||||||
|
Loading…
Reference in New Issue
Block a user