Page 1 of 1
Horizontal Alignment in StringGadget
Posted: Sat Nov 13, 2021 6:34 pm
by sdawilson
I've been searching the forum for how to do this (sorry I'm still learning PB). I've come from VBA, so I'm used to simply setting the textalign property of a textbox to left, centre, right or justify. I can't find how to do this in PB. Have I missed something? Any help would be greatly appreciated.
Re: Horizontal Alignment in StringGadget
Posted: Sat Nov 13, 2021 7:12 pm
by infratec
It is not official, but ...
Code: Select all
If OpenWindow(0, 0, 0, 322, 205, "StringGadget Flags", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
StringGadget(0, 8, 10, 306, 20, "Normal StringGadget...", #PB_Text_Center)
StringGadget(1, 8, 35, 306, 20, "1234567", #PB_Text_Right)
Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
EndIf
You can use the flags of the TextGadget() (PB 5.73 windows)
Re: Horizontal Alignment in StringGadget
Posted: Sat Nov 13, 2021 8:25 pm
by sdawilson
Thank you so much... That works perfectly. Out of curiosity, why isn't it official? It seems to be a fairly basic property.
Re: Horizontal Alignment in StringGadget
Posted: Sat Nov 13, 2021 11:22 pm
by mk-soft
Not on all OS work this ...
Re: Horizontal Alignment in StringGadget
Posted: Sun Nov 14, 2021 2:13 am
by BarryG
sdawilson wrote: Sat Nov 13, 2021 8:25 pmwhy isn't it official?
Because PureBasic is cross-platform and this alignment isn't supported on Linux or Mac. One of PureBasic's design flaws, IMO. It sacrifices useful stuff just in case you want to compile a Linux/Mac version, even if you never have that intention. I love PureBasic immensely, but it means using API calls and flags/constants to make it do the basics at times.
Re: Horizontal Alignment in StringGadget
Posted: Sun Nov 14, 2021 9:45 am
by Shardik
infratec wrote: Sat Nov 13, 2021 7:12 pm
It is not official, but ...
Code: Select all
If OpenWindow(0, 0, 0, 322, 205, "StringGadget Flags", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
StringGadget(0, 8, 10, 306, 20, "Normal StringGadget...", #PB_Text_Center)
StringGadget(1, 8, 35, 306, 20, "1234567", #PB_Text_Right)
Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
EndIf
You can use the flags of the TextGadget() (PB 5.73 windows)
It's not a good practice to misuse PureBasic constants from a different gadget. PureBasic constants may change without official notice and will then break your code. Instead of this you should use the official Microsoft constants for the Windows edit control (aka StringGadget in PureBasic, the constants are even predefined in PureBasic) which will be as future-proof as possible:
#ES_CENTER instead of
#PB_Text_Center for the StringGadget
and
#ES_RIGHT instead of
#PB_Text_Right for the StringGadget
Re: Horizontal Alignment in StringGadget
Posted: Sun Nov 14, 2021 1:55 pm
by blueb
While horizontally centering text in string / text gadgets is relatively easy....
Centering the results
both horizontally
and vertically is sometimes desired, and that doesn't appear to be as easy.
Code: Select all
If OpenWindow(0, 0, 0, 270, 200, "Vertical Centering Gadget Text is Desired", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
StringGadget(0, 8, 10, 250, 40, "Normal StringGadget...", #ES_CENTER)
If LoadFont(0, "Arial Bold", 10)
SetGadgetFont(0, FontID(0)) ; format font for first gadget
EndIf
TextGadget(1, 10, 130, 250, 40, "TextGadget Centered With a Border", #PB_Text_Center | #PB_Text_Border)
Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
EndIf
Re: Horizontal Alignment in StringGadget
Posted: Sun Nov 14, 2021 2:44 pm
by mk-soft
To change the alignment afterwards, I wrote this time. Only GTK2 I do not get the TextGadget to run. GTK3 works.
Code: Select all
;-TOP
EnableExplicit
Enumeration Windows
#Main
EndEnumeration
Enumeration Gadgets
#MainString
#MainText
#MainButton_Left
#MainButton_Center
#MainButton_Right
EndEnumeration
Enumeration Status
#MainStatusBar
EndEnumeration
Enumeration
#TextLeft
#TextCenter
#TextRight
EndEnumeration
; ----
CompilerSelect #PB_Compiler_OS
CompilerCase #PB_OS_Windows
Procedure SetTextAligmnent(Gadget, Type)
Protected handle = GadgetID(Gadget)
Protected style = GetWindowLongPtr_(handle, #GWL_STYLE)
Select Type
Case #TextLeft
style = style & ~#ES_CENTER & ~#ES_RIGHT
Case #TextCenter
style = style & ~#ES_RIGHT | #ES_CENTER
Case #TextRight
style = style & ~#ES_CENTER | #ES_RIGHT
EndSelect
SetWindowLongPtr_(handle, #GWL_STYLE, style)
InvalidateRect_(handle, 0, #True)
EndProcedure
CompilerCase #PB_OS_Linux
ImportC ""
gtk_entry_set_alignment(*entry, xalign.f)
gtk_label_set_xalign(*label, xalign.f)
gtk_misc_set_alignment(*label, xalign.f, yalign.f)
EndImport
Procedure SetTextAligmnent(Gadget, Type)
Protected widget = GadgetID(Gadget)
Select GadgetType(Gadget)
Case #PB_GadgetType_String
Select Type
Case #TextLeft
gtk_entry_set_alignment(widget, 0.0)
Case #TextCenter
gtk_entry_set_alignment(widget, 0.5)
Case #TextRight
gtk_entry_set_alignment(widget, 1.0)
EndSelect
CompilerIf Subsystem("gtk2")
Case #PB_GadgetType_Text
Select Type
Case #TextLeft
gtk_misc_set_alignment(widget, 0.0, 0.0)
Case #TextCenter
gtk_misc_set_alignment(widget, 0.5, 0.0)
Case #TextRight
gtk_misc_set_alignment(widget, 1.0, 0.0)
EndSelect
CompilerElse
Case #PB_GadgetType_Text
Select Type
Case #TextLeft
gtk_label_set_xalign(widget, 0.0)
Case #TextCenter
gtk_label_set_xalign(widget, 0.5)
Case #TextRight
gtk_label_set_xalign(widget, 1.0)
EndSelect
CompilerEndIf
EndSelect
EndProcedure
CompilerCase #PB_OS_MacOS
Procedure SetTextAligmnent(Gadget, Type)
Select Type
Case #TextLeft
CocoaMessage(0, GadgetID(Gadget), "setAlignment:", #NSLeftTextAlignment)
Case #TextCenter
CocoaMessage(0, GadgetID(Gadget), "setAlignment:", #NSCenterTextAlignment)
Case #TextRight
CocoaMessage(0, GadgetID(Gadget), "setAlignment:", #NSRightTextAlignment)
EndSelect
EndProcedure
CompilerEndSelect
; ----
Procedure Main()
#MainStyle = #PB_Window_SystemMenu | #PB_Window_MaximizeGadget | #PB_Window_MinimizeGadget
If OpenWindow(#Main, #PB_Ignore, #PB_Ignore, 800, 600, "Window" , #MainStyle)
CreateStatusBar(#MainStatusBar, WindowID(#Main))
AddStatusBarField(#PB_Ignore)
StringGadget(#MainString, 10, 10, 400, 25, "StringGadget")
TextGadget(#MainText, 10, 45, 400, 25, "TextGadget")
ButtonGadget(#MainButton_Left, 10, 80, 100, 25, "Left")
ButtonGadget(#MainButton_Center, 120, 80, 100, 25, "Center")
ButtonGadget(#MainButton_Right, 230, 80, 100, 25, "Right")
Repeat
Select WaitWindowEvent()
Case #PB_Event_CloseWindow
Break
Case #PB_Event_Gadget
Select EventGadget()
Case #MainButton_Left
SetTextAligmnent(#MainString, #TextLeft)
SetTextAligmnent(#MainText, #TextLeft)
Case #MainButton_Center
SetTextAligmnent(#MainString, #TextCenter)
SetTextAligmnent(#MainText, #TextCenter)
Case #MainButton_Right
SetTextAligmnent(#MainString, #TextRight)
SetTextAligmnent(#MainText, #TextRight)
EndSelect
EndSelect
ForEver
EndIf
EndProcedure : Main()
Re: Horizontal Alignment in StringGadget
Posted: Sun Nov 14, 2021 6:17 pm
by Michael Vogel
blueb wrote: Sun Nov 14, 2021 1:55 pm
While horizontally centering text in string / text gadgets is relatively easy....
Centering the results
both horizontally
and vertically is sometimes desired, and that doesn't appear to be as easy.
You're right and trying to get perfect dialogs for multiple platforms is not an easy job...
...here's the minimum action you need in windows to do vertical centering (a lot of other solution can be found here in the forum):
Code: Select all
Procedure StringGadgetVCenter(gadget)
Protected hwndEdit
Protected hdc
Protected fsz.SIZE
Protected erect.RECT
Protected height
height=GadgetHeight(gadget)
hwndEdit=GadgetID(gadget)
hdc=GetDC_(hwndEdit)
SelectObject_(hdc,GetGadgetFont(Gadget))
GetTextExtentPoint32_(hdc,"ABC",3,fsz)
ReleaseDC_(hwndEdit,hdc)
GetClientRect_(hwndEdit,eRect)
eRect\left=2;
eRect\top=height>>1-fsz\cy<<4/20
eRect\bottom=height+fsz\cy>>1
SendMessage_(hwndEdit,#EM_SETRECT,0,eRect)
EndProcedure
If OpenWindow(0, 0, 0, 270, 200, "Vertical Centering Gadget Text is Desired", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
StringGadget(0, 8, 10, 250, 40, "Normal StringGadget...",#ES_MULTILINE)
If LoadFont(0, "Arial Bold", 10)
SetGadgetFont(0, FontID(0)) ; format font for first gadget
EndIf
StringGadgetVCenter(0)
TextGadget(1, 10, 130, 250, 40, "TextGadget Centered With a Border", #PB_Text_Center | #PB_Text_Border|#SS_CENTERIMAGE)
Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
EndIf
Re: Horizontal Alignment in StringGadget
Posted: Sun Nov 14, 2021 6:48 pm
by RASHAD
It maybe cross platform
It can be easy adapted also for Hal center
Code: Select all
If OpenWindow(0, 0, 0, 270, 200, "Vertical Centering Gadget Text is Desired", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
cont = ContainerGadget(#PB_Any,8,10,250,20,#PB_Container_Single)
SetGadgetColor(cont,#PB_Gadget_BackColor,$FFFFFF)
StringGadget(0, 0,0, 250, 20, "Normal StringGadget...",#PB_String_BorderLess)
CloseGadgetList()
If LoadFont(0, "Arial Bold", 16)
SetGadgetFont(0, FontID(0)) ; format font for first gadget
EndIf
H = GadgetHeight(0,#PB_Gadget_RequiredSize)
ResizeGadget(cont,#PB_Ignore,#PB_Ignore,#PB_Ignore,H+8)
ResizeGadget(0,#PB_Ignore,5,#PB_Ignore,H)
Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
EndIf
For Hal & Val centering :
Code: Select all
Procedure centerHV(gadget,font)
StartDrawing(WindowOutput(0))
DrawingFont(FontID(0))
text$ = GetGadgetText(0)
w = TextWidth(text$)
h = TextHeight(text$)
StopDrawing()
If text$ = ""
h = 20
EndIf
x = 240/2-w/2
y = 8
If x < 0
x = 0
EndIf
ResizeGadget(10,#PB_Ignore,#PB_Ignore,#PB_Ignore,h+y)
ResizeGadget(0,x ,y/2,#PB_Ignore,h)
EndProcedure
If OpenWindow(0, 0, 0, 270, 200, "Vertical Centering Gadget Text is Desired", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
ContainerGadget(10,10,10,250,20,#PB_Container_Single)
SetGadgetColor(10,#PB_Gadget_BackColor,$FFFFFF)
StringGadget(0, 0, 0, 1000, 20, "Normal StringGadget...",#PB_String_BorderLess)
CloseGadgetList()
If LoadFont(0, "Arial Bold", 12)
SetGadgetFont(0, FontID(0)) ; format font for first gadget
EndIf
centerHV(0,0)
Repeat
Select WaitWindowEvent()
Case #PB_Event_CloseWindow
Quit = 1
Case #PB_Event_Gadget
Select EventGadget()
Case 0
centerHV(0,0)
EndSelect
EndSelect
Until Quit = 1
EndIf
Re: Horizontal Alignment in StringGadget
Posted: Sun Nov 14, 2021 7:22 pm
by blueb
Thanks again Rashad

Re: Horizontal Alignment in StringGadget
Posted: Mon Nov 15, 2021 9:17 am
by Shardik
You may also take a look into
this thread in which cross-platform examples to vertically center the text in a TextGadget and StringGadget (doesn't work on Linux) were already discussed.
Re: Horizontal Alignment in StringGadget
Posted: Mon Nov 15, 2021 1:25 pm
by blueb
OK.. that was exactly what I was looking for.
Thanks Shardik
Re: Horizontal Alignment in StringGadget
Posted: Mon Nov 15, 2021 6:52 pm
by sdawilson
Thank you to you all on helping me on this topic. It's very generous of you all to spare the time. This is really useful.