Page 1 of 1

StringGadget Centered Hal & Val cross platform

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

Re: StringGadget Centered Hal & Val cross platform

Posted: Tue Jun 11, 2019 7:03 pm
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

Re: StringGadget Centered Hal & Val cross platform

Posted: Tue Jun 11, 2019 7:45 pm
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

Re: StringGadget Centered Hal & Val cross platform

Posted: Wed Jun 12, 2019 3:36 am
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().

Re: StringGadget Centered Hal & Val cross platform

Posted: Wed Jun 12, 2019 4:37 am
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:

Re: StringGadget Centered Hal & Val cross platform

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

Re: StringGadget Centered Hal & Val cross platform

Posted: Wed Jun 12, 2019 9:14 am
by Kwai chang caine
Works great here W7 x86 // v5.70 X86
Thanks for sharing 8)

Re: StringGadget Centered Hal & Val cross platform

Posted: Wed Jun 12, 2019 12:08 pm
by Thorsten1867
StringEx - Module
(I have added support for centring and right-justification)

Re: StringGadget Centered Hal & Val cross platform

Posted: Wed Jun 12, 2019 2:54 pm
by RASHAD
@KCC
Thanks buddy

@Thorsten1867
Your modules and RSBasic also are very much appreciated
Good work indeed
Thanks