Text Gadget --- Vertical Center?

Mac OSX specific forum
mark5009
User
User
Posts: 22
Joined: Sun Oct 19, 2014 10:47 pm

Text Gadget --- Vertical Center?

Post by mark5009 »

Hi.

Is it possible under OSX to vertically center text in a text gadget? Horizontal centering is handled by #PB_Text_Center, but if I change the font from the default and increase its size, the vertical centering is off (it is too high), making the display not quite perfect.

Any thoughts? Ideally something cross-platform would be great but I suspect this will be platform specific.

Thanks .. mark.
User avatar
Danilo
Addict
Addict
Posts: 3037
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

Re: Text Gadget --- Vertical Center?

Post by Danilo »

It may be the best to center the gadget yourself inside a container gadget (resize the Window to see it):

Code: Select all

Procedure OnResize()
    ResizeGadget(0,0,0,WindowWidth(0),WindowHeight(0))
    ResizeGadget(1,GadgetWidth(0)*0.5 - GadgetWidth(1)*0.5,
                   GadgetHeight(0)*0.5 - GadgetHeight(1)*0.5,#PB_Ignore,#PB_Ignore)
EndProcedure

If OpenWindow(0, 0, 0, 800, 600, "TextGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered | #PB_Window_SizeGadget)
    ContainerGadget(0,0,0,800,600)
        TextGadget(1, 0,  0, 0, 0, "TextGadget Center"+#CRLF$+"Line 2",#PB_Text_Center)
    CloseGadgetList()

    SetGadgetFont(1,LoadFont(0,"Arial",48))
    
    CocoaMessage(0, GadgetID(1), "sizeToFit")
    ResizeGadget(1,GadgetWidth(0)*0.5 - GadgetWidth(1)*0.5,
                   GadgetHeight(0)*0.5 - GadgetHeight(1)*0.5,#PB_Ignore,#PB_Ignore)
    
    BindEvent(#PB_Event_SizeWindow,@OnResize(),0)
    
    Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
EndIf
Works also with StringGadget:

Code: Select all

Procedure OnResize()
    ResizeGadget(0,0,0,WindowWidth(0),WindowHeight(0))
    ResizeGadget(1,GadgetWidth(0)*0.5 - GadgetWidth(1)*0.5,
                   GadgetHeight(0)*0.5 - GadgetHeight(1)*0.5,#PB_Ignore,#PB_Ignore)
EndProcedure

Procedure OnStringChanged()
    CocoaMessage(0, GadgetID(1), "sizeToFit")
    ResizeGadget(1,GadgetWidth(0)*0.5 - GadgetWidth(1)*0.5,
                 GadgetHeight(0)*0.5 - GadgetHeight(1)*0.5,#PB_Ignore,#PB_Ignore)
EndProcedure

If OpenWindow(0, 0, 0, 800, 600, "StringGadget AutoFit", #PB_Window_SystemMenu | #PB_Window_ScreenCentered | #PB_Window_SizeGadget)
    ContainerGadget(0,0,0,800,600)
        StringGadget(1, 0,  0, 0, 0, "StringGadget Center")
    CloseGadgetList()
    
    SetGadgetFont(1,LoadFont(0,"Arial",48))
    
    CocoaMessage(0, GadgetID(1), "sizeToFit")
    ResizeGadget(1,GadgetWidth(0)*0.5 - GadgetWidth(1)*0.5,
                   GadgetHeight(0)*0.5 - GadgetHeight(1)*0.5,#PB_Ignore,#PB_Ignore)
    
    BindGadgetEvent(1,@OnStringChanged(),#PB_EventType_Change)
    BindEvent(#PB_Event_SizeWindow,@OnResize(),0)
    
    Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
EndIf
mark5009
User
User
Posts: 22
Joined: Sun Oct 19, 2014 10:47 pm

Re: Text Gadget --- Vertical Center?

Post by mark5009 »

Danilo wrote:It may be the best to center the gadget yourself inside a container gadget (resize the Window to see it)
Many thanks, Danilo, I'll check it out.

.. mark.
Post Reply