StringGadget Centered Hal & Val cross platform

Share your advanced PureBasic knowledge/code with the community.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4635
Joined: Sun Apr 12, 2009 6:27 am

StringGadget Centered Hal & Val cross platform

Post by RASHAD »

It should be cross platform
Using only native commands
Tested with Windows only

Code: Select all

Global sh,cw,ch

Procedure type()
  Text$ = GetGadgetText(1)
  StartDrawing(WindowOutput(0))
    DrawingFont(FontID(0))
    w = TextWidth(text$)
  StopDrawing()     
  ResizeGadget(1,cw/2-w/2,ch/2-sh/2-2,w,sh)
EndProcedure

LoadFont(0,"Tahoma",24)
OpenWindow(0, 0, 0, 600, 400, "StringGadget ", #PB_Window_SystemMenu|#PB_Window_ScreenCentered)
SetWindowColor(0,#White)
StartDrawing(WindowOutput(0))
  DrawingFont(FontID(0))
  sh = TextHeight("Q")+2
  sw = TextWidth(" ")
StopDrawing()

ContainerGadget(0,10,10,580,60,#PB_Container_Flat)
  SetGadgetColor(0,#PB_Gadget_BackColor,$FFFFFF)
  cw = GadgetWidth(0)
  ch = GadgetHeight(0)
  StringGadget(1, cw/2,10,sw,sh," ",#PB_String_BorderLess)
  SetGadgetFont(1,FontID(0))
  SetGadgetColor(1,#PB_Gadget_FrontColor,$0000FF)  
CloseGadgetList()

SetGadgetFont(1,FontID(0))

SetActiveGadget(1)

BindGadgetEvent(1,@type())
Repeat
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
      Quit = 1
      
    Case #PB_Event_Gadget
      Select EventGadget()
         Case 1
          
      EndSelect
  EndSelect
Until Quit = 1
Edit : Modified #1
Last edited by RASHAD on Tue Jun 11, 2019 7:43 pm, edited 1 time in total.
Egypt my love
User avatar
Shardik
Addict
Addict
Posts: 1989
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: StringGadget Centered Hal & Val cross platform

Post by Shardik »

The vertical centering is indeed working cross-platform. But the first input character is not fully visible on Linux (Linux Mint 19.1 'Tessa' x64 with Cinnamon and GTK3)

Image

and on MacOS (MacOS 10.13.6 'High Sierra'):

Image
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4635
Joined: Sun Apr 12, 2009 6:27 am

Re: StringGadget Centered Hal & Val cross platform

Post by RASHAD »

Thanks Shardik
Previous post updated but I do things blindly

Played around with the other post maybe it will be OK with Mac

Code: Select all

LoadFont(0,"Tahoma",24)

OpenWindow(0, 0, 0, 600, 400, "StringGadget ", #PB_Window_SystemMenu|#PB_Window_ScreenCentered|#PB_Window_SizeGadget)
SetWindowColor(0,#White)
StringGadget(1, 10,10,580,40,"ABCDEJQ")
SetGadgetFont(1,FontID(0))
dummy = TextGadget(#PB_Any,0,0,0,0,"ABCDEJQ")
  SetGadgetFont(dummy,FontID(0))
  h = GadgetHeight(dummy,#PB_Gadget_RequiredSize)
FreeGadget(dummy)
ResizeGadget(1,10,10,#PB_Ignore,h+2)

Repeat

Until WaitWindowEvent() = #PB_Event_CloseWindow
Egypt my love
User avatar
Blue
Addict
Addict
Posts: 864
Joined: Fri Oct 06, 2006 4:41 am
Location: Canada

Re: StringGadget Centered Hal & Val cross platform

Post by Blue »

I'm not sure I follow here, RASHAD.
Why the detour through a dummy TextGadget() to get the fitted height since GadgetHeight(#gadget,#PB_Gadget_RequiredSize) works perfectly with StringGadget() ?

It's the fitted width that is a problem with StringGadget().
"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: 4635
Joined: Sun Apr 12, 2009 6:27 am

Re: StringGadget Centered Hal & Val cross platform

Post by RASHAD »

Hi Blue
Shardik in other thread reported that GadgetHeight() for StringGasget() did not work with Mac
So I am trying an alternative maybe you know :P
Sorry mate :wink:
Egypt my love
User avatar
Blue
Addict
Addict
Posts: 864
Joined: Fri Oct 06, 2006 4:41 am
Location: Canada

Re: StringGadget Centered Hal & Val cross platform

Post by Blue »

Thanks RASHAD.
Oh, yes, now I understand.
I had skipped that part of the question.

(I was the same in the army : always a step behind.
It must be genetic.)

But your suggestion to him led me to rewrite the code I posted last night in that other topic about a related question. This time I made use of the simple but effective idea you presented above (taking measurements via a dummy TextGadget()) and discovered that it is very effective. but it has its peculiarities too.

I'll erase my useless code of last night and post this more efficient one. And, as usual, i'll have a question or two for you. So don't go out tonight and don't fall asleep. You're needed at the shop.
"That's not a bug..." said the programmer. "it's a feature! "
"Oh! I see..." replied the blind man.
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5342
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: StringGadget Centered Hal & Val cross platform

Post by Kwai chang caine »

Works great here W7 x86 // v5.70 X86
Thanks for sharing 8)
ImageThe happiness is a road...
Not a destination
User avatar
Thorsten1867
Addict
Addict
Posts: 1366
Joined: Wed Aug 24, 2005 4:02 pm
Location: Germany

Re: StringGadget Centered Hal & Val cross platform

Post by Thorsten1867 »

StringEx - Module
(I have added support for centring and right-justification)
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: 4635
Joined: Sun Apr 12, 2009 6:27 am

Re: StringGadget Centered Hal & Val cross platform

Post by RASHAD »

@KCC
Thanks buddy

@Thorsten1867
Your modules and RSBasic also are very much appreciated
Good work indeed
Thanks
Egypt my love
Post Reply