Center SpinGadget Text?

Just starting out? Need help? Post your questions and find answers here.
novablue
Enthusiast
Enthusiast
Posts: 177
Joined: Sun Nov 27, 2016 6:38 am

Center SpinGadget Text?

Post by novablue »

I need help centering text of a spin gadget. I tried #PB_Text_Center and some Winapi codes for other gadget types but could not get it to work.
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Center SpinGadget Text?

Post by IdeasVacuum »

If the SpinGadget() is for numbers, then it's normal to display them right-justified.

You can define your own gadget very easily with either a TextGadget() or StringGadget() and two buttons. That way you have complete control of the behaviour. I have seen apps where the "gadget" is simply two buttons, values displayed on the buttons.
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4974
Joined: Sun Apr 12, 2009 6:27 am

Re: Center SpinGadget Text?

Post by RASHAD »

For Windows

Code: Select all

LoadFont(0,"Tahoma",12)

OpenWindow(0, 0, 0, 400, 300, "SpinGadget", #PB_Window_SystemMenu|#PB_Window_ScreenCentered)
SGh1 = StringGadget(1,10,10,60,24,"",#ES_CENTER|#ES_NUMBER)
SetGadgetColor(1,#PB_Gadget_BackColor,$C6FEFC)
SetGadgetColor(1,#PB_Gadget_FrontColor,$1B1BFE)
SetGadgetFont(1,FontID(0))
sph = CreateUpDownControl_(#WS_CHILD|#WS_BORDER| #WS_VISIBLE|#UDS_SETBUDDYINT|#UDS_ARROWKEYS| #UDS_ALIGNRIGHT,10,10,60,40,WindowID(0), 1, GetModuleHandle_(0),SGh1,100,0,50)

Repeat
  Select WaitWindowEvent()
      
      Case #PB_Event_CloseWindow
          Quit = 1
          
      Case #PB_Event_Gadget
        Select EventGadget()
          Case 1
            Debug Val(GetGadgetText(1))
        EndSelect
                    
  EndSelect 

Until Quit = 1
End
Egypt my love
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Center SpinGadget Text?

Post by IdeasVacuum »

Very neat solution Rashad 8)
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
novablue
Enthusiast
Enthusiast
Posts: 177
Joined: Sun Nov 27, 2016 6:38 am

Re: Center SpinGadget Text?

Post by novablue »

RASHAD wrote:For Windows

Code: Select all

LoadFont(0,"Tahoma",12)

OpenWindow(0, 0, 0, 400, 300, "SpinGadget", #PB_Window_SystemMenu|#PB_Window_ScreenCentered)
SGh1 = StringGadget(1,10,10,60,24,"",#ES_CENTER|#ES_NUMBER)
SetGadgetColor(1,#PB_Gadget_BackColor,$C6FEFC)
SetGadgetColor(1,#PB_Gadget_FrontColor,$1B1BFE)
SetGadgetFont(1,FontID(0))
sph = CreateUpDownControl_(#WS_CHILD|#WS_BORDER| #WS_VISIBLE|#UDS_SETBUDDYINT|#UDS_ARROWKEYS| #UDS_ALIGNRIGHT,10,10,60,40,WindowID(0), 1, GetModuleHandle_(0),SGh1,100,0,50)

Repeat
  Select WaitWindowEvent()
      
      Case #PB_Event_CloseWindow
          Quit = 1
          
      Case #PB_Event_Gadget
        Select EventGadget()
          Case 1
            Debug Val(GetGadgetText(1))
        EndSelect
                    
  EndSelect 

Until Quit = 1
End
Thanks, is it possible to make it behave like a spingadget without the #PB_Spin_Numeric flag, i just need it to trigger the up/down events but not change the number on its own.
Oma
Enthusiast
Enthusiast
Posts: 312
Joined: Thu Jun 26, 2014 9:17 am
Location: Germany

Re: Center SpinGadget Text?

Post by Oma »

Hi novablue,

just to try out the new router :wink: (and adding a linux solution for gtk2 + gtk3). Only for PureBasic SpinGadget (not for API version!)

Code: Select all

EnableExplicit

ImportC ""
	gtk_entry_set_alignment(*entry.GtkEntry, xalign.f)
EndImport

; Object constants
#Win_Main  = 0
#SpBG1     = 0

Global.i gEvent, gEventGadget, gEventType, gQuit

Procedure Create_WinMain()
	Protected *widget.GtkHBox, *entry.GtkEntry

	If OpenWindow(#Win_Main, 300, 200, 500, 200, "PB-SpinGadget text/number alignment", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
		SpinGadget    (#SpBG1, 5, 5, 100, 26, 0, 10);, #PB_Spin_Numeric)
		SetGadgetState(#SpBG1, 5)
		SetGadgetText (#SpBG1, "5")
		*widget= GadgetID(#SpBG1)
		*entry = g_list_nth_data_(gtk_container_get_children_(*widget), 0)
		gtk_entry_set_alignment(*entry, 0.5);  0.0= left aligned, 1.0= right aligned
	EndIf
EndProcedure

Create_WinMain()

Repeat
	gEvent= WaitWindowEvent()
	
	Select gEvent
		Case #PB_Event_CloseWindow
			gQuit= #True
			
		Case #PB_Event_Gadget
			If EventGadget() = #SpBG1
				gEventType= EventType() 
				If gEventType = #PB_EventType_Up
					Debug "up"
				ElseIf gEventType = #PB_EventType_Down
					Debug "down"
				EndIf
			EndIf
			
	EndSelect
	
Until gQuit
Regards Charly
PureBasic 5.4-5.7, Linux: (X/L/K)Ubuntus+Mint - Windows XP (32Bit)
PureBasic Linux-API-Library & Viewer: http://www.chabba.de
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4974
Joined: Sun Apr 12, 2009 6:27 am

Re: Center SpinGadget Text?

Post by RASHAD »

With a little efforts you can make it completely native Windows/Linux/Mac

Code: Select all

fsize = 12
LoadFont(0,"Tahoma",fsize)
gsize = fsize*2+4

OpenWindow(0, 0, 0, 400, 300, "SpinGadget", #PB_Window_SystemMenu|#PB_Window_ScreenCentered)
ContainerGadget(0,10,10,92,gsize)
  SpinGadget(1,70,0,18,gsize,0,1000)
  SGh2 = StringGadget(2,2,0,68,gsize,"VOID",#ES_CENTER)
  SetGadgetColor(2,#PB_Gadget_BackColor,$C6FEFC)
  SetGadgetColor(2,#PB_Gadget_FrontColor,$1B1BFE)
  SetGadgetFont(2,FontID(0))
CloseGadgetList()
Repeat
  Select WaitWindowEvent()
     
      Case #PB_Event_CloseWindow
          Quit = 1
          
      Case #PB_Event_Gadget
          Select EventGadget()
            Case 1
              Select EventType()
                Case  #PB_EventType_Up
                    SetGadgetText(2,"UP")
                
                Case  #PB_EventType_Down
                    SetGadgetText(2,"DOWN")
              EndSelect
          EndSelect
  EndSelect

Until Quit = 1
End
Native Solution:

Code: Select all

fsize = 12
LoadFont(0,"Tahoma",fsize)
gsize = fsize*2+4

Procedure settext(text$)
  StartDrawing(WindowOutput(0))
    DrawingFont(FontID(0))
    twidth = TextWidth(text$)
  StopDrawing()
  padleft = (GadgetWidth(2)-twidth)/2 
  StartDrawing(CanvasOutput(2))
    Box(0,0,GadgetWidth(2),GadgetHeight(2),$FFFFFF)
    DrawingFont(FontID(0))
    DrawingMode(#PB_2DDrawing_Transparent)
    DrawText(padleft,3,text$,$1B1BFE)
  StopDrawing()
EndProcedure

OpenWindow(0, 0, 0, 400, 300, "SpinGadget", #PB_Window_SystemMenu|#PB_Window_ScreenCentered)
ContainerGadget(0,10,10,92,gsize,#PB_Container_Flat)
  SpinGadget(1,70,0,21,gsize,0,1000)
  CanvasGadget(2,2,0,68,gsize)
  StartDrawing(WindowOutput(0))
    DrawingFont(FontID(0))
    twidth = TextWidth("VOID")
  StopDrawing()
  padleft = (GadgetWidth(2)-twidth)/2 
  StartDrawing(CanvasOutput(2))
    DrawingFont(FontID(0))
    DrawingMode(#PB_2DDrawing_Transparent)
    DrawText(padleft,3,"VOID",$1B1BFE)
  StopDrawing()
CloseGadgetList()
Repeat
  Select WaitWindowEvent()
     
      Case #PB_Event_CloseWindow
          Quit = 1
          
      Case #PB_Event_Gadget
          Select EventGadget()
            Case 1
              Select EventType()
                Case  #PB_EventType_Up
                    settext("UP")
                
                Case  #PB_EventType_Down
                    settext("DOWN")
              EndSelect
          EndSelect
  EndSelect

Until Quit = 1
End
Egypt my love
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Center SpinGadget Text?

Post by IdeasVacuum »

Another way, all OS:

Code: Select all

Enumeration
#Win
#Cntr
#Txt
#BtnUp
#BtnDn
#Font12R
EndEnumeration

LoadFont(#Font12R, "Arial", 12, #PB_Font_HighQuality)

Procedure Win()
;#-------------
Protected iFlags.i = #PB_Window_SystemMenu | #PB_Window_ScreenCentered

              If OpenWindow(#Win, 0, 0, 300, 150, "DIY Spin Gadget", iFlags)

                       SetGadgetFont(#PB_All, FontID(#Font12R))

                     ContainerGadget(#Cntr,   50, 50, 150, 42, #PB_Container_Flat)
                          TextGadget(#Txt,     5, 12, 120, 30, "", #PB_Text_Center)
                        ButtonGadget(#BtnUp, 128,  0,  20, 20, Chr($25B2))
                        ButtonGadget(#BtnDn, 128, 20,  20, 20, Chr($25BC))

                      SetGadgetColor(#Cntr, #PB_Gadget_BackColor,  RGB(255,255,255))
                      SetGadgetColor(#Txt,  #PB_Gadget_BackColor,  RGB(255,255,255))
                      SetGadgetColor(#Txt,  #PB_Gadget_FrontColor, RGB(010,010,010))

                     CloseGadgetList()
              EndIf
EndProcedure

Procedure Wait()
;#--------------
Protected iExit.i = #False

              Repeat

                      Select WaitWindowEvent(1)

                               Case #PB_Event_CloseWindow: iExit = #True

                               Case #PB_Event_Gadget

                                    Select EventGadget()

                                              Case #BtnUp: SetGadgetText(#Txt, "UP")
                                              Case #BtnDn: SetGadgetText(#Txt, "DOWN")
                                    EndSelect
                      EndSelect

              Until iExit = #True
EndProcedure

 Win()
Wait()
End
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
novablue
Enthusiast
Enthusiast
Posts: 177
Joined: Sun Nov 27, 2016 6:38 am

Re: Center SpinGadget Text?

Post by novablue »

Thanks, i will do it manually then, i was hoping for an easy winapi one liner to avoid coding mouse wheel and keyboard events and everything else so it behaves like a regular spingadget. ;)
Luca Concon
New User
New User
Posts: 3
Joined: Sat Jul 16, 2022 7:57 am

Center SpinGadget Text?

Post by Luca Concon »

Tahnks for te information.
It works well for me !
Old Linux user
Post Reply