Recent

Author Topic: Need a multi-line button with centerable text?  (Read 5605 times)

CM630

  • Hero Member
  • *****
  • Posts: 1732
  • Не съм сигурен, че те разбирам.
    • http://sourceforge.net/u/cm630/profile/
Need a multi-line button with centerable text?
« on: March 08, 2018, 08:37:35 am »
I need to display multiple text lines in a button and I need to have the text in it horizontally and vertically centerred.
TButton does not support multiple lines. TSpeedButton does, but text is aligned to the left and I found no property to center it horizontally.
Is there a native Lazarus solution or will I need a third party component?
« Last Edit: June 03, 2025, 02:46:54 pm by CM630 »
Лазар 4,8 32 bit (sometimes 64 bit); FPC3,2,2

wp

  • Hero Member
  • *****
  • Posts: 13625
Re: Need a multi-line button with centerable text?
« Reply #1 on: March 08, 2018, 09:02:36 am »
TToggleBox does what you want. Just assign an OnClick handler which resets its "Checked" back to false to get the standard button behavior.

ASerge

  • Hero Member
  • *****
  • Posts: 2510
Re: Need a multi-line button with centerable text?
« Reply #2 on: March 08, 2018, 05:04:10 pm »
I need to display multiple text lines in a button and I need to have the text in it horizontally and vertically centerred.
TButton does not support multiple lines.
In windows support:
Code: Pascal  [Select][+][-]
  1. uses Windows;
  2.  
  3. procedure SetButtonMultiline(Button: TButtonControl; Multiline: Boolean);
  4. var
  5.   Wnd: HWND;
  6.   Style: DWORD;
  7. begin
  8.   Wnd := Button.Handle;
  9.   Style := GetWindowLongPtr(Wnd, GWL_STYLE);
  10.   if Multiline then
  11.     Style := Style or BS_MULTILINE
  12.   else
  13.     Style := Style and not BS_MULTILINE;
  14.   SetWindowLongPtr(Wnd, GWL_STYLE, Style);
  15. end;
  16.  
  17. procedure SetButtonAlignment(Button: TButtonControl;
  18.   HorizontalAlignment, VerticalAlignment: TAlignment);
  19. const
  20.   CHorizontalAlignment: array [TAlignment] of DWORD = (BS_LEFT, BS_RIGHT, BS_CENTER);
  21.   CHorizontalMask = BS_LEFT or BS_RIGHT or BS_CENTER;
  22.   CVerticalAlignment: array [TAlignment] of DWORD = (BS_TOP, BS_BOTTOM, BS_VCENTER);
  23.   CVerticalMask = BS_TOP or BS_BOTTOM or BS_VCENTER;
  24. var
  25.   Wnd: HWND;
  26.   Style: DWORD;
  27. begin
  28.   Wnd := Button.Handle;
  29.   Style := GetWindowLongPtr(Wnd, GWL_STYLE);
  30.   Style := Style and not CHorizontalMask or CHorizontalAlignment[HorizontalAlignment];
  31.   Style := Style and not CVerticalMask or CVerticalAlignment[VerticalAlignment];
  32.   SetWindowLongPtr(Wnd, GWL_STYLE, Style);
  33. end;
  34.  
  35. procedure TForm1.FormCreate(Sender: TObject);
  36. const
  37.   CButtonWidth = 120;
  38.   CButtonHeight = 80;
  39.   CAligmentLength = Ord(High(TAlignment)) - Ord(Low(TAlignment)) + 1;
  40.   CSpace = 8;
  41. var
  42.   Button: TButton;
  43.   H, V: TAlignment;
  44.   X: Integer = CSpace;
  45.   S: string;
  46. begin
  47.   Constraints.MinWidth := CAligmentLength * CAligmentLength * (CButtonWidth + CSpace) + CSpace;
  48.   for H in TAlignment do
  49.     for V in TAlignment do
  50.       begin
  51.         Button := TButton.Create(Self);
  52.         Button.Parent := Self;
  53.         Button.SetBounds(X, CSpace, CButtonWidth, CButtonHeight);
  54.         Inc(X, Button.Width + CSpace);
  55.         SetButtonMultiline(Button, True);
  56.         SetButtonAlignment(Button, H, V);
  57.         WriteStr(S, 'H=', H, #10, 'V=', V);
  58.         Button.Caption := S;
  59.       end;
  60. end;

Blaazen

  • Hero Member
  • *****
  • Posts: 3241
  • POKE 54296,15
    • Eye-Candy Controls
Re: Need a multi-line button with centerable text?
« Reply #3 on: March 08, 2018, 06:23:28 pm »
TECSpeedBtn from ECControls can do it with default settings.
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

CM630

  • Hero Member
  • *****
  • Posts: 1732
  • Не съм сигурен, че те разбирам.
    • http://sourceforge.net/u/cm630/profile/
Re: Need a multi-line button with centerable text?
« Reply #4 on: March 09, 2018, 08:45:08 am »
Thanks to all of you!
@WPs solution works for me and since it seems to be most straight forward, I have not tried the others.
Лазар 4,8 32 bit (sometimes 64 bit); FPC3,2,2

CM630

  • Hero Member
  • *****
  • Posts: 1732
  • Не съм сигурен, че те разбирам.
    • http://sourceforge.net/u/cm630/profile/
Re: [SOLVED] Need a multi-line button with centerable text?
« Reply #5 on: June 03, 2025, 02:46:39 pm »
I few days later, it occurs that the text of TToggleBoxs in Linux (GTK3) is not centred :-X, while it is in Windows.
« Last Edit: June 04, 2025, 07:43:58 am by CM630 »
Лазар 4,8 32 bit (sometimes 64 bit); FPC3,2,2

wp

  • Hero Member
  • *****
  • Posts: 13625
Re: Need a multi-line button with centerable text?
« Reply #6 on: June 03, 2025, 03:13:09 pm »
Since this thread is saveral years old, I tested TSpeedButton and TToggleButton on Windows, gtk2, gtk3, qt5 ant qt6 with a current Lazarus:

CM630

  • Hero Member
  • *****
  • Posts: 1732
  • Не съм сигурен, че те разбирам.
    • http://sourceforge.net/u/cm630/profile/
Re: Need a multi-line button with centerable text?
« Reply #7 on: June 03, 2025, 04:29:40 pm »
So is it a bug, or is that how it shall be?
Лазар 4,8 32 bit (sometimes 64 bit); FPC3,2,2

wp

  • Hero Member
  • *****
  • Posts: 13625
Re: Need a multi-line button with centerable text?
« Reply #8 on: June 03, 2025, 07:07:44 pm »
I would say: It's a bug... You should file a bug report so that somebody with gtk knowledge can take a look.

CM630

  • Hero Member
  • *****
  • Posts: 1732
  • Не съм сигурен, че те разбирам.
    • http://sourceforge.net/u/cm630/profile/
Re: Need a multi-line button with centerable text?
« Reply #9 on: June 04, 2025, 08:03:26 am »
I filed a bug report: https://gitlab.com/freepascal.org/lazarus/lazarus/-/issues/41693
Yet there is another difference between Windows and Linux, I think the bug is in Windows this time:
Autosizing multiple lines text in Windows makes the box wider, instead of taller, so I created another bug report: https://gitlab.com/freepascal.org/lazarus/lazarus/-/issues/41694
« Last Edit: June 04, 2025, 08:30:15 am by CM630 »
Лазар 4,8 32 bit (sometimes 64 bit); FPC3,2,2

bobonwhidbey

  • Hero Member
  • *****
  • Posts: 632
    • Double Dummy Solver - free download
Re: Need a multi-line button with centerable text?
« Reply #10 on: June 07, 2025, 09:12:58 pm »
Over the decades, many programmers have added various features to the simple TButton. In my Component Palette I counted more than 25 different buttons with BGRA Button Controls and EC-C being the most prolific button palettes. This must be daunting to one starting out with Laz. I like TBCButton for the most flexibility in design BUT it doesn't have the line wrapping capabilites discussed in this thread. Seems like TToggleBox has that problem solved as well as anyone.

It would be nice if there were an "unberButton" that would incorporate the vast majority of the features added by these various buttons. . . and then we could get rid of some of the extraneous components.

p.s. I haven't even mentioned RadioButtons which have a similar proliferation.
Lazarus 4.8 FPC 3.2.2 x86_64-win64-win32/win64

wp

  • Hero Member
  • *****
  • Posts: 13625
Re: Need a multi-line button with centerable text?
« Reply #11 on: June 08, 2025, 12:57:45 am »
A few days ago I committed changes to the win32 widgetset which makes TButton support line breaks and fixes Autosizing for it. Line breaks now are available for TButton in all widgetsets. We also have patches for justification and autosize issues in gtk2, qt/qt5/qt6 waiting for reviewing. See https://gitlab.com/freepascal.org/lazarus/lazarus/-/issues/41696

CM630

  • Hero Member
  • *****
  • Posts: 1732
  • Не съм сигурен, че те разбирам.
    • http://sourceforge.net/u/cm630/profile/
Re: Need a multi-line button with centerable text?
« Reply #12 on: June 09, 2025, 02:46:35 pm »
Thanks,
I saw the activity in gitlab. I suppose since these are fixes, they will be available in the next release of Lazarus.
Лазар 4,8 32 bit (sometimes 64 bit); FPC3,2,2

 

TinyPortal © 2005-2018