Set Center Align in StringGadget : Vertical 'n' Horizontal

Just starting out? Need help? Post your questions and find answers here.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4664
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: 5357
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: 886
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: 4664
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: 886
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: 886
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: 4664
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: 886
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: 4664
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
User avatar
Blue
Addict
Addict
Posts: 886
Joined: Fri Oct 06, 2006 4:41 am
Location: Canada

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

Post by Blue »

Thank you for your input and interest, RASHAD.
RASHAD wrote:...you are depending on the font Size which can not be perfect
I realize that, but I arrived at that solution by trial and error. No great math theory or understanding behind what I'm doing here. It just works for 99.5% of the cases.
RASHAD wrote:For comparing between StringGadget() 2 & 3 I did not notice any difference
The reason you see no great difference is, of course, because of the corrective padding I apply when re-fitting the StringGadget(). My mistake. Since I wanted you to see the difference, i should have left out the padding entirely.
RASHAD wrote:...please put your Procedures out of the main gui just for me
Sorry, I don't know what you mean here.

But I've re-organized the code in a cleaner way and I've added a checkbox so you can see the difference with/without additional padding.
"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: 4664
Joined: Sun Apr 12, 2009 6:27 am

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

Post by RASHAD »

For vPadding & hPadding I do the same trial and error
For Procedure staff ,but I will not insist on that :mrgreen:

Code: Select all

Procedure
EndProcedure

Procedure
EndProcedure

LoadFont(0,"Tahoma",14)
OpenWindow(0, 0, 0, 600, 400, "StringGadget ", #PB_Window_SystemMenu|#PB_Window_ScreenCentered)
StringGadget(1, 10,10,580,40,"ABCDEJQ")

Repeat
Until WaitWindowEvent() = #PB_Event_CloseWindow

Egypt my love
User avatar
Blue
Addict
Addict
Posts: 886
Joined: Fri Oct 06, 2006 4:41 am
Location: Canada

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

Post by Blue »

OK, now I understand. :D

In my defense, i had corrected the code before you said anything. But I have a very good excuse for the sloppy posting of last night : it was 3h15 in the morning and I had both eyes in a single hole, looking no further than the tip of my nose.

Thanks RASHAD
Always enlightening to exchange with you.
"That's not a bug..." said the programmer. "it's a feature! "
"Oh! I see..." replied the blind man.
User avatar
Thorsten1867
Addict
Addict
Posts: 1366
Joined: Wed Aug 24, 2005 4:02 pm
Location: Germany

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

Post by Thorsten1867 »

StringEx - Module
(I have added a attribute for padding)
Translated with http://www.DeepL.com/Translator

Download of PureBasic - Modules
Download of PureBasic - Programs

[Windows 11 x64] [PB V5.7x]
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4664
Joined: Sun Apr 12, 2009 6:27 am

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

Post by RASHAD »

Hi Blue
I feel free after you informed me that you are Windows user
Next is what I think it is the proper alignment for StringGadget()

Code: Select all

Global h,w

Procedure CharH(gadget,font , text$)
  hdc = GetDC_(GadgetID(gadget))
  SelectObject_(hdc, FontID(font))
  GetTextMetrics_(hdc, @tm.TEXTMETRIC)
  h = tm\tmHeight + tm\tmInternalLeading + tm\tmExternalLeading
  GetTextExtentPoint32_(hdc , @text$ ,Len(text$),@s.SIZE)
  w = s\cx+tm\tmAveCharWidth
  ReleaseDC_(GadgetID(gadget), hdc)
EndProcedure

LoadFont(0,"Tahoma",14)
text$ = "ABCDEJQ"+" " ;Remember to add a space
OpenWindow(0, 0, 0, 600, 400, "StringGadget ", #PB_Window_SystemMenu|#PB_Window_ScreenCentered)
SetWindowColor(0,#White)
StringGadget(1, 10,10,580,40,Text$)
SetGadgetFont(1,FontID(0))
CharH(1,0 ,text$)

ResizeGadget(1,10,10,w,h)

Repeat

Until WaitWindowEvent() = #PB_Event_CloseWindow
Egypt my love
Post Reply