Set Center Align in StringGadget : Vertical 'n' Horizontal

Just starting out? Need help? Post your questions and find answers here.
User avatar
Tomi
Enthusiast
Enthusiast
Posts: 270
Joined: Wed Sep 03, 2008 9:29 am

Set Center Align in StringGadget : Vertical 'n' Horizontal

Post by Tomi »

after search in En/Fr/De forums and mix some flags together, i'm reach to this code:

Code: Select all

If OpenWindow(0,0,0,500,500,"",#PB_Window_SystemMenu) 
 
    TextGadget(1,10,10,200,200,"Test 1 2 3 4 5 6 7 8",$340|$1|$20000) ;$20000=#PB_Text_Border and $1=PB_Text_Center
    StringGadget(2,10,250,200,200,"Test 1 2 3 4 5 6 7 8",$340|$1|$20000) ;$20000=#PB_Text_Border and $1=PB_Text_Center

  
Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow 
EndIf 
no problem in TextGadget , but don't work in StringGadget :roll:
if use PB_Text_Center individually in StringGadget, Horizontal=OK and Vertical=NO :?:
User avatar
Arctic Fox
Enthusiast
Enthusiast
Posts: 609
Joined: Sun Dec 21, 2008 5:02 pm
Location: Aarhus, Denmark

Post by Arctic Fox »

Sparkie's code for Windows (http://www.purebasic.fr/english/viewtop ... 448#116448)

Code: Select all

; ************************************************
; Code:   Verticaly and Horizontaly centered text
;         in a multiline StringGadget
; Author: Sparkie
; Date:   December 20, 2005
; OS:     Windows only
; ************************************************

; ************************************************
; Procedure: Center text in Multiline StringGadget
; ************************************************
Procedure StringGadgetVCenter(gadNum)
  ;--> Get line count of StringGadget
  lineCount = SendMessage_(GadgetID(gadNum), #EM_GETLINECOUNT, 0, 0)
  myText$ = GetGadgetText(gadNum)
  ;--> Get width and height of text on one line
  hdc = GetDC_(GadgetID(gadNum))
  GetTextExtentPoint32_(hdc, myText$, Len(myText$), @textXY.SIZE)
  ReleaseDC_(GadgetID(gadNum), hdc)
  ;--> Set rect coordinates for StringGadget
  eRect.RECT
  eRect\left = 0
  eRect\top = (GadgetHeight(gadNum) - textXY\cy*lineCount) / 2
  eRect\right = GadgetWidth(gadNum) - (eRect\left * 2)
  eRect\bottom = eRect\top + textXY\cy*lineCount
  SendMessage_(GadgetID(gadNum), #EM_SETRECT, 0, eRect)
EndProcedure
; ************************************************
; Main Window
; ************************************************
If OpenWindow(0, 0, 0, 300, 200, "StringGadget Centered Text", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  ;--> #ES_CENTER will not work with a singleline StringGadget on Win95/NT4
  StringGadget(0, 50, 10, 200, 80, "Vertical and Horizontal" + #CRLF$ + "Centered Text in" + #CRLF$ + "Multiline StringGadget", #ES_MULTILINE | #ES_CENTER)
  StringGadgetVCenter(0)
  Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
EndIf
End 
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

Yes for the text gadget, the style $340 harbors the #SS_CENTERIMAGE style which is the one which is centering vertically etc. There is no such style for string gadgets.

You need the #EM_SETRECT message as used by the code in Arctic Fox's post.
I may look like a mule, but I'm not a complete ass.
User avatar
Tomi
Enthusiast
Enthusiast
Posts: 270
Joined: Wed Sep 03, 2008 9:29 am

Post by Tomi »

mercii :D
The code is very good
if it was as a flag in StringGadget :shock: it's bettr :D
It's Record in my WishList 8)
***********************

@Sparkie
I'm gloom because you are not here for awhile :roll:
Have best time man



********************
and for srod,ts_soft, fluid and many good peoples if they be disappear here.
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re:

Post by Dude »

Just used Sparkie's code today but discovered it doesn't work for custom fonts (see below). Can someone please fix it? Thanks.

Code: Select all

Procedure StringGadgetVCenter(gadNum)
  ;--> Get line count of StringGadget
  lineCount = SendMessage_(GadgetID(gadNum), #EM_GETLINECOUNT, 0, 0)
  myText$ = GetGadgetText(gadNum)
  ;--> Get width and height of text on one line
  hdc = GetDC_(GadgetID(gadNum))
  GetTextExtentPoint32_(hdc, myText$, Len(myText$), @textXY.SIZE)
  ReleaseDC_(GadgetID(gadNum), hdc)
  ;--> Set rect coordinates for StringGadget
  eRect.RECT
  eRect\left = 0
  eRect\top = (GadgetHeight(gadNum) - textXY\cy*lineCount) / 2
  eRect\right = GadgetWidth(gadNum) - (eRect\left * 2)
  eRect\bottom = eRect\top + textXY\cy*lineCount
  SendMessage_(GadgetID(gadNum), #EM_SETRECT, 0, eRect)
EndProcedure

If OpenWindow(0, 0, 0, 300, 250, "StringGadget Centered Text", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)

  StringGadget(0, 10, 10, 280, 230, "Vertical and Horizontal" + #CRLF$ + "Centered Text in" + #CRLF$ + "Multiline StringGadget", #ES_MULTILINE | #ES_CENTER)

  SetGadgetFont(0, LoadFont(0, "Courier", 20)) ; This stops it showing the last line ("Multiline StringGadget").

  StringGadgetVCenter(0)

  Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow

EndIf
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4636
Joined: Sun Apr 12, 2009 6:27 am

Re: Set Center Align in StringGadget : Vertical 'n' Horizont

Post by RASHAD »

Hi

Code: Select all

Procedure StringGadgetVCenter(gadNum)
  lineCount = SendMessage_(GadgetID(gadNum), #EM_GETLINECOUNT, 0, 0)
  hdc = GetDC_(GadgetID(gadNum))
  SelectObject_(hdc,FontID(0))
  GetTextExtentPoint32_(hdc,"Qj",2,textXY.SIZE)
  ReleaseDC_(GadgetID(gadNum), hdc) 
  
  eRect.RECT  
  GetClientRect_(GadgetID(gadNum),eRect)
  eRect\top = (GadgetHeight(gadNum) - textXY\cy*lineCount) / 2
  eRect\bottom = eRect\top + textXY\cy*lineCount
  SendMessage_(GadgetID(gadNum), #EM_SETRECT, 0, eRect)
EndProcedure

LoadFont(0, "Courier", 20)
If OpenWindow(0, 0, 0, 300, 250, "StringGadget Centered Text", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)

  StringGadget(0, 10, 10, 280, 230, "Vertical and Horizontal" + #CRLF$ + "Centered Text in" + #CRLF$ + "Multiline StringGadget", #ES_MULTILINE | #ES_CENTER)

  SetGadgetFont(0,FontID(0) ) ; This stops it showing the last line ("Multiline StringGadget").

  StringGadgetVCenter(0)

  Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow

EndIf
Egypt my love
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: Set Center Align in StringGadget : Vertical 'n' Horizont

Post by Dude »

You da man! :lol:
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5342
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: Set Center Align in StringGadget : Vertical 'n' Horizont

Post by Kwai chang caine »

Thanks RASHAD, works great 8)
ImageThe happiness is a road...
Not a destination
User avatar
Blue
Addict
Addict
Posts: 864
Joined: Fri Oct 06, 2006 4:41 am
Location: Canada

Re: Set Center Align in StringGadget : Vertical 'n' Horizont

Post by Blue »

Hello RASHAD.

I've been playing around with your code and I came upon a surprising result.
GadgetWidth(#gadget, #PB_Gadget_RequiredSize) FAILs with a StringGadget

At first glance, it appears to me like a bug. Am I simply doing something wrong ?

Running on Windows 10 X64 v1903 (build 18875)

Code: Select all

; erasing this useless demo code, but keeping picture to which
; RASHAD answered "NO bug" (see messages below)
Image



New code, related to this topic

Code: Select all


; source : RASHAD
; https://www.purebasic.fr/english/viewtopic.php?f=12&t=72996&p=537864#p537864

;- constants
Enumeration 
  #BUTTON_go = 5 : #BUTTON_bye : #font_LABEL : #font_SiZE : #padding_CHOICE
EndEnumeration

Define fontSize = 32
Procedure Three_strings(abc.s)
  #minFONT = 6
  #maxFONT = 120

  Shared fontSize
  fontSize = Val(GetGadgetText(#font_SiZE))

  If fontSize < #minFONT : fontSize = #minFONT : SetGadgetText(#font_SiZE, Str(#minFONT))
  ElseIf fontSize > #maxFONT : fontSize = #maxFONT : SetGadgetText(#font_SiZE, Str(#maxFONT))
  EndIf

  LoadFont(0,"Tahoma",fontSize)
  Protected wPadding, hPadding
  
  If GetGadgetState(#padding_CHOICE)
    wPadding = 1 + fontSize*0.6   ; what would be a smarter value here ?
    hPadding = wPadding/3
  Else
    wPadding = 0
    hPadding = 2
  EndIf
   
  ;- 1 ****
  StringGadget(1, 10,10,1200,2,ABC)
  SetGadgetFont(1,FontID(0))
  h = GadgetHeight(1,#PB_Gadget_RequiredSize)
  ResizeGadget(1,#PB_Ignore,#PB_Ignore,#PB_Ignore,h+2)

  ;- 2 ****
  #dY = 6
  StringGadget(2, 10,GadgetY(1)+h+#dY,2,2,abc)
  SetGadgetFont(2,FontID(0))
  dummy = TextGadget(#PB_Any,0,0,0,0,abc)
    SetGadgetFont(dummy,FontID(0))
    h = hPadding + GadgetHeight(dummy, #PB_Gadget_RequiredSize)
    w = hPadding + GadgetWidth(dummy,  #PB_Gadget_RequiredSize)
  FreeGadget(dummy)
  ResizeGadget(2,#PB_Ignore,#PB_Ignore,w,h)

  ;- 3 ****
  StringGadget(3, 10,GadgetY(2)+h+#dY,w+wPadding,h,abc)
  SetGadgetFont(3,FontID(0))
EndProcedure

OpenWindow(0, 0, 0, 1220, 600, "StringGadget ", #PB_Window_SystemMenu|#PB_Window_ScreenCentered|#PB_Window_SizeGadget)
SetWindowColor(0,#White)

Define gX, gY, gW, gH
gH = 24 : gW = 100
gY = WindowHeight(0) - gH - 10
gX = 10

Define gFont = LoadFont(1,"Arial", 11)
SetGadgetFont(#PB_Default, gFont)
  
gX  = 10
  TextGadget(#font_LABEL, gX,gY,gW,gH,"Font size : ", #SS_CENTERIMAGE|#PB_Text_Right)
  SetGadgetColor(#font_LABEL, #PB_Gadget_BackColor, #White)
gX + gW 
  StringGadget(#font_SiZE, gX,gY,48,gH,Str(fontSize), #PB_String_Numeric)

gX + 54 : gW = 175
  CheckBoxGadget(#padding_CHOICE, gX,gY,gW,gH,"pad the StringGadgets")

gX + gW + 10 : gW = 100
  ButtonGadget(#BUTTON_go, gX,gY,gW,gH,"test it", #PB_Button_Default)
  AddKeyboardShortcut(0,#PB_Shortcut_Return, #BUTTON_go)

gX + gW + 10
  ButtonGadget(#BUTTON_bye, gX,gY,gW,gH,"Bye Bye !")

Three_strings("ABC pyj_WXYZ")

Repeat
  event = WaitWindowEvent()
  Select event
    Case #PB_Event_CloseWindow : End
    Case #PB_Event_Gadget, #PB_Event_Menu
      Select EventGadget()
        Case #BUTTON_go
          Three_strings("ABC pyj_WXYZ")
        Case #BUTTON_bye : Break
      EndSelect
  EndSelect
ForEver

End
Last edited by Blue on Wed Jun 12, 2019 5:06 pm, edited 4 times in total.
"That's not a bug..." said the programmer. "it's a feature! "
"Oh! I see..." replied the blind man.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4636
Joined: Sun Apr 12, 2009 6:27 am

Re: Set Center Align in StringGadget : Vertical 'n' Horizont

Post by RASHAD »

Hi Blue
I like your snapshot :)
Will ask you later which utilities you are using for that

With Static gadget like TextGadget() #PB_Gadget_RequiredSize will give you the right answer
With Dynamic gadget like StringGadget() #PB_Gadget_RequiredSize will give you the least expected width (For Caret only)
So you have to use #PB_Gadget_ActualSize
No bug
gH = GadgetHeight(gadget,#PB_Gadget_RequiredSize) ; OK
gW = GadgetWidth(gadget,#PB_Gadget_ActualSize) ;- ... FAIL <<<< BUG ???
ResizeGadget(gadget,#PB_Ignore,#PB_Ignore,gW,gH)
Egypt my love
User avatar
Blue
Addict
Addict
Posts: 864
Joined: Fri Oct 06, 2006 4:41 am
Location: Canada

Re: Set Center Align in StringGadget : Vertical 'n' Horizont

Post by Blue »

Thank you very much, RASHAD.
Your reply came surprisingly quickly.

Well, I've learned something precious tonight : there are static and dynamic text objects. Not sure what i can do with that precious knowledge, but i'll sleep smarter tonight. +1 for you !

Snapshot tool : FastStone Capture (https://www.faststone.org/FSCaptureDetail.htm)
Paid 20$ years ago for this tool, never regretted it. One of the most used tools in my toolbox.
"That's not a bug..." said the programmer. "it's a feature! "
"Oh! I see..." replied the blind man.
User avatar
Blue
Addict
Addict
Posts: 864
Joined: Fri Oct 06, 2006 4:41 am
Location: Canada

Re: Set Center Align in StringGadget : Vertical 'n' Horizont

Post by Blue »

Me again, RASHAD.

Could you take a look at the new code I posted above.
It's based around your suggestion to solve a problem with fitting text on the Mac.

If you have the time and patience, it would be nice of you to run the snippet and see if you can explain why the 2 StringGadget() differ in what their output.
"That's not a bug..." said the programmer. "it's a feature! "
"Oh! I see..." replied the blind man.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4636
Joined: Sun Apr 12, 2009 6:27 am

Re: Set Center Align in StringGadget : Vertical 'n' Horizont

Post by RASHAD »

Sorry mate
I do not have a Mac :mrgreen:
And I will never do (I hate it)
Egypt my love
User avatar
Blue
Addict
Addict
Posts: 864
Joined: Fri Oct 06, 2006 4:41 am
Location: Canada

Re: Set Center Align in StringGadget : Vertical 'n' Horizont

Post by Blue »

My bad, RASHAD.
My request was not worded clearly.

I do not have a Mac either. So my question had nothing to do with Apples and other fruits.

The example code posted above is strictly for Windows, based on your idea of using a dummy TextGadget() to obtain fitted dimensions for a piece of text. I was asking because the resulting aspect of the StringGadget() varies if the measurement is taken before or after creating the StringGadget().

That could have something to do with the DPi adjustments in Windows. I was wondering if you had come across this and if you knew the reason.
"That's not a bug..." said the programmer. "it's a feature! "
"Oh! I see..." replied the blind man.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4636
Joined: Sun Apr 12, 2009 6:27 am

Re: Set Center Align in StringGadget : Vertical 'n' Horizont

Post by RASHAD »

Hi Blue
For vPadding & hPadding you are depending on the font Size which can not be perfect
You see TTF is not that easy the characteristics of the font is changing dramatically with the font Size(Height,Ascent,Descent,External Leading & Internal Leading)
Our concern now is Height+External Leading+Internal Leading
I am telling you that to take care about it
But what you did is not bad after all and I do not have any other solution

For comparing between StringGadget() 2 & 3 I did not notice any difference
But please put your Procedures out of the main gui just for me :P
Egypt my love
Post Reply