Simple multi purpose TextGadget()

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

Simple multi purpose TextGadget()

Post by RASHAD »

Hi all
- Left - Center - Right Text
- Different border style
- Any font size
And More

Code: Select all

 Global gw,gh,padw,padh
 
Procedure sTEXT(gad,text$,bColor,fColor,align,padw,padh,border)
  cont = ContainerGadget(#PB_Any,10,10,10,10,border)
    TextGadget(gad, 0,  0, 0, 0,text$, align)
  CloseGadgetList()
  SetGadgetFont(gad,FontID(0))
  gw = GadgetWidth(gad,#PB_Gadget_RequiredSize)
  gh = GadgetHeight(gad,#PB_Gadget_RequiredSize)   
  SetGadgetColor( cont,#PB_Gadget_BackColor,bColor)
  SetGadgetColor( gad,#PB_Gadget_BackColor,bColor)
  SetGadgetColor( gad,#PB_Gadget_FrontColor,fColor)
  ResizeGadget(cont,10,10,gw+padw*4,gh+padh*2)
  If align = #PB_Text_Center
    ResizeGadget(gad,padw,padh,gw+padw*2,gh)
  ElseIf align = #PB_Text_Right
    ResizeGadget(gad,padw*3,padh,gw,gh)
  Else
    ResizeGadget(gad,padw,padh,gw,gh)
  EndIf  
EndProcedure
 
If OpenWindow(0, 0, 0, 0, 0, "TextGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  fSize = 32
  LoadFont(0,"Georgia",fSize)
  padw = 20  ;Hal padding
  padh = 20  ;Val padding
  padh - fSize/4
  text$ = "TextGadget Center"+#CRLF$+"Second Line"
  sTEXT(1,text$,$CCFEFF,$2027FE,2,padw,padh,4)  ;align  : 0=left - 1=center - 2=right
                                                ;border : 0=Borderless - 1=Flat 2=Raised - 3=Single - 4=Double
  ResizeWindow(0,0,0,gw+padw*4+20,gh+padh*2+20)
  HideWindow(0,0,#PB_Window_ScreenCentered)
 
  Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
EndIf
Edit :Modified for better look
Egypt my love
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5342
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: Simple multi purpose TextGadget()

Post by Kwai chang caine »

Thanks for sharing 8)
ImageThe happiness is a road...
Not a destination
walbus
Addict
Addict
Posts: 929
Joined: Sat Mar 02, 2013 9:17 am

Re: Simple multi purpose TextGadget()

Post by walbus »

This is a good idea !

Before more weeks i would make the same

My solution was a little other
So, I look for the real font size with a trick and make the needed padding automatic, font size depending

A quickly code changing for testing

Code: Select all

Global gw, gh, pad

Procedure sTEXT(gad, pos_x, pos_y, text$, bColor, fColor, align, border)
  cont=ContainerGadget(#PB_Any, pos_x, pos_y, 0, 0, border)
  TextGadget(gad, 0, 0, 0, 0, Space(1), align)
  CloseGadgetList()
  SetGadgetFont(gad,FontID(0))
  pad=GadgetWidth(gad,#PB_Gadget_RequiredSize)
  SetGadgetText(gad, text$)
  gw=GadgetWidth(gad,#PB_Gadget_RequiredSize)
  gh=GadgetHeight(gad,#PB_Gadget_RequiredSize) 
  SetGadgetColor(cont,#PB_Gadget_BackColor, bColor)
  SetGadgetColor(gad,#PB_Gadget_BackColor, bColor)
  SetGadgetColor(gad,#PB_Gadget_FrontColor, fColor)
  ResizeGadget(cont, pos_x, pos_y, gw+pad*4, gh+pad*2)
  If align=#PB_Text_Center
    ResizeGadget(gad,pad,pad,gw+pad*2,gh)
  ElseIf align=#PB_Text_Right
    ResizeGadget(gad, pad*2, pad, gw, gh)
  Else
    ResizeGadget(gad, pad*2, pad, gw+pad*2, gh)
  EndIf  
EndProcedure

If OpenWindow(0, 0, 0, 0, 0, "TextGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  fSize=40
  LoadFont(0,"Georgia",fSize)
  text$="TextGadget Center"+#CRLF$+"Second Line"
  
  pos_x=30
  pos_y=30
  
  sTEXT(1, pos_x, pos_y, text$, $CCFEFF, $2027FE ,1 ,4)  ;align  : 0=left - 1=center - 2=right
                                           ;border : 0=Borderless - 1=Flat 2=Raised - 3=Single - 4=Double
  
  ResizeWindow(0, 0, 0, gw+pad*6+pos_x, gh+pad*4+pos_y)
  HideWindow(0, 0, #PB_Window_ScreenCentered)
  
  Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
EndIf
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5342
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: Simple multi purpose TextGadget()

Post by Kwai chang caine »

Nice too, thanks for sharing WALBUS 8)
ImageThe happiness is a road...
Not a destination
Post Reply