It is currently Tue Aug 20, 2019 9:08 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: Set Center Align in StringGadget : Vertical 'n' Horizontal
PostPosted: Fri May 08, 2009 4:51 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Wed Sep 03, 2008 9:29 am
Posts: 270
after search in En/Fr/De forums and mix some flags together, i'm reach to this code:

Code:
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 :?:


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Fri May 08, 2009 4:59 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Sun Dec 21, 2008 5:02 pm
Posts: 609
Location: Aarhus, Denmark
Sparkie's code for Windows (http://www.purebasic.fr/english/viewtopic.php?p=116448#116448)

Code:
; ************************************************
; 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


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Fri May 08, 2009 5:03 pm 
Offline
PureBasic Expert
PureBasic Expert

Joined: Wed Oct 29, 2003 4:35 pm
Posts: 10524
Location: Beyond the pale...
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.


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Fri May 08, 2009 5:22 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Wed Sep 03, 2008 9:29 am
Posts: 270
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.


Top
 Profile  
Reply with quote  
 Post subject: Re:
PostPosted: Wed Apr 25, 2018 5:29 am 
Offline
Addict
Addict

Joined: Mon Feb 16, 2015 2:49 pm
Posts: 1893
Just used Sparkie's code today but discovered it doesn't work for custom fonts (see below). Can someone please fix it? Thanks.

Code:
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


Top
 Profile  
Reply with quote  
 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: 3381
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: 3381
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: 3381
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: 3381
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  
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 19 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