Page 1 of 2

Set Center Align in StringGadget : Vertical 'n' Horizontal

Posted: Fri May 08, 2009 4:51 pm
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 :?:

Posted: Fri May 08, 2009 4:59 pm
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 

Posted: Fri May 08, 2009 5:03 pm
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.

Posted: Fri May 08, 2009 5:22 pm
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.

Re:

Posted: Wed Apr 25, 2018 5:29 am
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

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

Posted: Wed Apr 25, 2018 6:59 am
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

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

Posted: Wed Apr 25, 2018 8:44 am
by Dude
You da man! :lol:

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

Posted: Wed Apr 25, 2018 9:18 am
by Kwai chang caine
Thanks RASHAD, works great 8)

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

Posted: Tue Jun 11, 2019 6:26 am
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

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

Posted: Tue Jun 11, 2019 6:55 am
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)

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

Posted: Tue Jun 11, 2019 7:11 am
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.

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

Posted: Wed Jun 12, 2019 6:52 am
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.

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

Posted: Wed Jun 12, 2019 6:57 am
by RASHAD
Sorry mate
I do not have a Mac :mrgreen:
And I will never do (I hate it)

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

Posted: Wed Jun 12, 2019 1:29 pm
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.

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

Posted: Wed Jun 12, 2019 2:46 pm
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