It is currently Mon Aug 19, 2019 3:10 pm

All times are UTC + 1 hour




Post new topic Reply to topic  [ 20 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: Re: Set Center Align in StringGadget : Vertical 'n' Horizont
PostPosted: Wed Apr 25, 2018 6:59 am 
Offline
PureBasic Expert
PureBasic Expert

Joined: Sun Apr 12, 2009 6:27 am
Posts: 3379
Hi
Code:
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


Top
 Profile  
Reply with quote  
 Post subject: Re: Set Center Align in StringGadget : Vertical 'n' Horizont
PostPosted: Wed Apr 25, 2018 8:44 am 
Offline
Addict
Addict

Joined: Mon Feb 16, 2015 2:49 pm
Posts: 1893
You da man! :lol:


Top
 Profile  
Reply with quote  
 Post subject: Re: Set Center Align in StringGadget : Vertical 'n' Horizont
PostPosted: Wed Apr 25, 2018 9:18 am 
Offline
Addict
Addict
User avatar

Joined: Sun Nov 05, 2006 11:42 pm
Posts: 4487
Location: Lyon - France
Thanks RASHAD, works great 8)

_________________
ImageThe happiness is a road...
Not a destination


Top
 Profile  
Reply with quote  
 Post subject: Re: Set Center Align in StringGadget : Vertical 'n' Horizont
PostPosted: Tue Jun 11, 2019 6:26 am 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Fri Oct 06, 2006 4:41 am
Posts: 671
Location: Canada
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:
; 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:

; 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

_________________
"That's not a bug..." said the programmer. "...It's a feature! "
"Oh! I see..." replied the blind man.


Last edited by Blue on Wed Jun 12, 2019 5:06 pm, edited 4 times in total.

Top
 Profile  
Reply with quote  
 Post subject: Re: Set Center Align in StringGadget : Vertical 'n' Horizont
PostPosted: Tue Jun 11, 2019 6:55 am 
Offline
PureBasic Expert
PureBasic Expert

Joined: Sun Apr 12, 2009 6:27 am
Posts: 3379
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
Quote:
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


Top
 Profile  
Reply with quote  
 Post subject: Re: Set Center Align in StringGadget : Vertical 'n' Horizont
PostPosted: Tue Jun 11, 2019 7:11 am 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Fri Oct 06, 2006 4:41 am
Posts: 671
Location: Canada
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.


Top
 Profile  
Reply with quote  
 Post subject: Re: Set Center Align in StringGadget : Vertical 'n' Horizont
PostPosted: Wed Jun 12, 2019 6:52 am 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Fri Oct 06, 2006 4:41 am
Posts: 671
Location: Canada
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.


Top
 Profile  
Reply with quote  
 Post subject: Re: Set Center Align in StringGadget : Vertical 'n' Horizont
PostPosted: Wed Jun 12, 2019 6:57 am 
Offline
PureBasic Expert
PureBasic Expert

Joined: Sun Apr 12, 2009 6:27 am
Posts: 3379
Sorry mate
I do not have a Mac :mrgreen:
And I will never do (I hate it)

_________________
Egypt my love


Top
 Profile  
Reply with quote  
 Post subject: Re: Set Center Align in StringGadget : Vertical 'n' Horizont
PostPosted: Wed Jun 12, 2019 1:29 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Fri Oct 06, 2006 4:41 am
Posts: 671
Location: Canada
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.


Top
 Profile  
Reply with quote  
 Post subject: Re: Set Center Align in StringGadget : Vertical 'n' Horizont
PostPosted: Wed Jun 12, 2019 2:46 pm 
Offline
PureBasic Expert
PureBasic Expert

Joined: Sun Apr 12, 2009 6:27 am
Posts: 3379
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


Top
 Profile  
Reply with quote  
 Post subject: Re: Set Center Align in StringGadget : Vertical 'n' Horizont
PostPosted: Wed Jun 12, 2019 4:36 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Fri Oct 06, 2006 4:41 am
Posts: 671
Location: Canada
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.


Top
 Profile  
Reply with quote  
 Post subject: Re: Set Center Align in StringGadget : Vertical 'n' Horizont
PostPosted: Wed Jun 12, 2019 5:21 pm 
Offline
PureBasic Expert
PureBasic Expert

Joined: Sun Apr 12, 2009 6:27 am
Posts: 3379
For vPadding & hPadding I do the same trial and error
For Procedure staff ,but I will not insist on that :mrgreen:
Code:
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


Top
 Profile  
Reply with quote  
 Post subject: Re: Set Center Align in StringGadget : Vertical 'n' Horizont
PostPosted: Wed Jun 12, 2019 5:27 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Fri Oct 06, 2006 4:41 am
Posts: 671
Location: Canada
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.


Top
 Profile  
Reply with quote  
 Post subject: Re: Set Center Align in StringGadget : Vertical 'n' Horizont
PostPosted: Wed Jun 12, 2019 6:04 pm 
Offline
Addict
Addict
User avatar

Joined: Wed Aug 24, 2005 4:02 pm
Posts: 861
Location: Germany
StringEx - Module
(I have added a attribute for padding)

_________________
Sorry for my English. My language is German.
(Translated with http://www.DeepL.com/Translator)

Download of PureBasic - Modules (GitHub)

[Windows 10 x64] [PB V5.7x]


Top
 Profile  
Reply with quote  
 Post subject: Re: Set Center Align in StringGadget : Vertical 'n' Horizont
PostPosted: Wed Jun 12, 2019 6:28 pm 
Offline
PureBasic Expert
PureBasic Expert

Joined: Sun Apr 12, 2009 6:27 am
Posts: 3379
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:
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


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 20 posts ]  Go to page 1, 2  Next

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 21 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye