PureBasic Forum
http://forums.purebasic.com/english/

StringGadget Centered Hal & Val cross platform
http://forums.purebasic.com/english/viewtopic.php?f=12&t=72996
Page 1 of 1

Author:  RASHAD [ Tue Jun 11, 2019 5:27 am ]
Post subject:  StringGadget Centered Hal & Val cross platform

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

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

Author:  Shardik [ Tue Jun 11, 2019 7:03 pm ]
Post subject:  Re: StringGadget Centered Hal & Val cross platform

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

Author:  RASHAD [ Tue Jun 11, 2019 7:45 pm ]
Post subject:  Re: StringGadget Centered Hal & Val cross platform

Thanks Shardik
Previous post updated but I do things blindly

Played around with the other post maybe it will be OK with Mac
Code:
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

Author:  Blue [ Wed Jun 12, 2019 3:36 am ]
Post subject:  Re: StringGadget Centered Hal & Val cross platform

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().

Author:  RASHAD [ Wed Jun 12, 2019 4:37 am ]
Post subject:  Re: StringGadget Centered Hal & Val cross platform

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:

Author:  Blue [ Wed Jun 12, 2019 6:37 am ]
Post subject:  Re: StringGadget Centered Hal & Val cross platform

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.

Author:  Kwai chang caine [ Wed Jun 12, 2019 9:14 am ]
Post subject:  Re: StringGadget Centered Hal & Val cross platform

Works great here W7 x86 // v5.70 X86
Thanks for sharing 8)

Author:  Thorsten1867 [ Wed Jun 12, 2019 12:08 pm ]
Post subject:  Re: StringGadget Centered Hal & Val cross platform

StringEx - Module
(I have added support for centring and right-justification)

Author:  RASHAD [ Wed Jun 12, 2019 2:54 pm ]
Post subject:  Re: StringGadget Centered Hal & Val cross platform

@KCC
Thanks buddy

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

Page 1 of 1 All times are UTC + 1 hour
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/