FrameGadgetCustom()

Share your advanced PureBasic knowledge/code with the community.
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

FrameGadgetCustom()

Post by Dude »

Here's a little procedure that creates a customised FrameGadget(), by letting you change the text color and font for it.

Credit: Based on code from Rashad here: http://www.purebasic.fr/english/viewtop ... 85#p513085

Image

Code: Select all

Procedure FrameGadgetCustom(gad,x,y,w,h,text$,color,fontid=0)
  If fontid
    SetGadgetFont(#PB_Default,fontid)
  EndIf
  tmp=TextGadget(#PB_Any,0,0,0,0,text$)
  tmpw=GadgetWidth(tmp,#PB_Gadget_RequiredSize)
  tmph=GadgetHeight(tmp,#PB_Gadget_RequiredSize)
  FreeGadget(tmp)
  FrameGadget(gad,x,y,w,h," ")
  gadnum=TextGadget(#PB_Any,x+10,y,tmpw+1,tmph,text$)
  SetGadgetColor(gadnum,#PB_Gadget_FrontColor,color)
  If fontid
    SetGadgetFont(#PB_Default,#PB_Default)
  EndIf
  ProcedureReturn gadnum
EndProcedure

OpenWindow(0,200,200,420,150,"Window",#PB_Window_SystemMenu)

PanelGadget(0,10,10,210,130)
AddGadgetItem(0,-1,"Panel")
FrameGadgetCustom(1,10,10,180,85,"Blue",#Blue,FontID(LoadFont(#PB_Any,"Broadway",20)))
CloseGadgetList()

FrameGadgetCustom(2,230,23,180,118,"Red",#Red)

Repeat : Until WaitWindowEvent()=#PB_Event_CloseWindow
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5342
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: FrameGadgetCustom()

Post by Kwai chang caine »

Very nice and useful
Works perfectly, thanks to you and RASHAD 8)
ImageThe happiness is a road...
Not a destination
User avatar
Thorsten1867
Addict
Addict
Posts: 1366
Joined: Wed Aug 24, 2005 4:02 pm
Location: Germany

[Module] CustomFrameModule.pbi

Post by Thorsten1867 »

Code: Select all

;/ === CustomFrameModule [PB 5.6x] ===
;/ Custom frame gadget
;/ October 2017 by Dude

DeclareModule CustomFrame
  Declare.i Gadget(GadgetID.i, X.i, Y.i, Width.i, Height.i, Text$, FontID=#PB_Ignore)
  Declare   SetColor(GadgetID.i, Color.i)
  Declare   SetText(GadgetID.i, Text$)
EndDeclareModule

Module CustomFrame
  
  EnableExplicit
  
  Structure TextStructure
    ID.i
    X.i
    Y.i
    Font.i
  EndStructure
  Global NewMap Text.TextStructure()
  
  Procedure.i Gadget(GadgetID.i, X.i, Y.i, Width.i, Height.i, Text$, FontID=#PB_Ignore)
    Define.i GId, GadgetH, GadgetW
    
    If FontID <> #PB_Ignore : SetGadgetFont(#PB_Default, FontID) : EndIf
    
    GId = TextGadget(#PB_Any, 0, 0, 0, 0, Text$)
    If GId
      GadgetW = GadgetWidth(GId,  #PB_Gadget_RequiredSize)
      GadgetH = GadgetHeight(GId, #PB_Gadget_RequiredSize)
      FreeGadget(GId)
    EndIf
    
    FrameGadget(GadgetID, X, Y, Width, Height, " ")
    
    GId = TextGadget(#PB_Any, X+10, Y, GadgetW+1, GadgetH, Text$)
    If GId
      AddMapElement(Text(), Str(GadgetID))
      Text()\ID    = GId
      Text()\X     = X+10
      Text()\Y     = Y
      Text()\Font  = FontID
    EndIf
    
    If FontID <> #PB_Ignore : SetGadgetFont(#PB_Default, #PB_Default) : EndIf
    
    ProcedureReturn GId
  EndProcedure

  Procedure SetColor(GadgetID.i, Color.i)
    Define GID$ = Str(GadgetID)
    SetGadgetColor(Text(GID$)\ID, #PB_Gadget_FrontColor, Color)
  EndProcedure
  
  Procedure SetText(GadgetID.i, Text$)
    Define GId.i, GadgetH, GadgetW, GID$ = Str(GadgetID)
   
    If Text(GID$)\Font <> #PB_Ignore : SetGadgetFont(#PB_Default, Text(GID$)\Font) : EndIf
    
    GId = TextGadget(#PB_Any, 0, 0, 0, 0, Text$)
    If GId
      GadgetW = GadgetWidth(GId,  #PB_Gadget_RequiredSize)
      GadgetH = GadgetHeight(GId, #PB_Gadget_RequiredSize)
      FreeGadget(GId)
    EndIf
    
    GId = Text(GID$)\ID
    ResizeGadget(GId, Text(GID$)\X, Text(GID$)\Y, GadgetW+1, GadgetH)
    SetGadgetText(GId, Text$)
  
    If Text(GID$)\Font <> #PB_Ignore : SetGadgetFont(#PB_Default, #PB_Default) : EndIf

  EndProcedure

EndModule  

CompilerIf #PB_Compiler_IsMainFile
  
  #Panel  = 0
  #Frame1 = 1
  #Frame2 = 2
  
  If OpenWindow(0,200,200,420,150,"Window",#PB_Window_SystemMenu)
  
    If PanelGadget(#Panel, 10, 10, 210, 130)
      AddGadgetItem(#Panel, -1, "Panel")
      CustomFrame::Gadget(#Frame1, 10, 10, 180, 85, "Blue", FontID(LoadFont(#PB_Any, "MV Boli", 18, 256)))
      CustomFrame::SetColor(#Frame1, #Blue)
      CloseGadgetList()
    EndIf
    
    CustomFrame::Gadget(#Frame2, 230, 23, 180, 118, "Red")
    CustomFrame::SetColor(#Frame2, #Red)
    
    MessageRequester("CustomFrameModule", "Change color and text.", #PB_MessageRequester_Ok)
    
    CustomFrame::SetText(#Frame1,  "New Text")
    CustomFrame::SetColor(#Frame2, #Green)
    
    Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
  
  EndIf

CompilerEndIf
Changed: CustomFrame::Gadget(GadgetID.i, X.i, Y.i, Width.i, Height.i, Text$, FontID=#PB_Ignore)
Added: CustomFrame::SetColor(GadgetID.i, Color.i)
Added: CustomFrame::SetText(GadgetID.i, Text$)
Translated with http://www.DeepL.com/Translator

Download of PureBasic - Modules
Download of PureBasic - Programs

[Windows 11 x64] [PB V5.7x]
User avatar
ar-s
Enthusiast
Enthusiast
Posts: 340
Joined: Sat Oct 06, 2007 11:20 pm
Location: France

Re: FrameGadgetCustom()

Post by ar-s »

Thanks Dude.

Adding BGcolor to the Dude procedure.
Could be usefull with a container for exemple.

Code: Select all

Procedure FrameGadgetCustom(gad,x,y,w,h,text$,color,bgcolor,fontid=0)
  If fontid
    SetGadgetFont(#PB_Default,fontid)
  EndIf
  tmp=TextGadget(#PB_Any,0,0,0,0,text$)
  tmpw=GadgetWidth(tmp,#PB_Gadget_RequiredSize)
  tmph=GadgetHeight(tmp,#PB_Gadget_RequiredSize)
  FreeGadget(tmp)
  FrameGadget(gad,x,y,w,h," ")
  gadnum=TextGadget(#PB_Any,x+7,y,tmpw+1,tmph,text$)
  SetGadgetColor(gadnum,#PB_Gadget_FrontColor,color)
  SetGadgetColor(gadnum,#PB_Gadget_BackColor,bgcolor)
  If fontid
    SetGadgetFont(#PB_Default,#PB_Default)
  EndIf
  ProcedureReturn gadnum
EndProcedure






#BGCOL = $AAAAAA
OpenWindow(0,200,200,420,150,"Window",#PB_Window_SystemMenu)
SetWindowColor(0,$00aacc)

ContainerGadget(2,5,5,410,140)
SetGadgetColor(2,#PB_Gadget_BackColor,$AAAAAA)

FrameGadgetCustom(1,10,10,180,85,"Blue",#Blue,#BGCOL,FontID(LoadFont(#PB_Any,"Broadway",20)))

CloseGadgetList()


Repeat : Until WaitWindowEvent()=#PB_Event_CloseWindow
~Ar-S~
My Image Hoster for PB users
My webSite (french) with PB apps : LDVMULTIMEDIA
PB - 3.x / 5.7x / 6 - W11 x64 - Ryzen 7 3700x / #Rpi4

Code: Select all

r3p347 : 7ry : un71l d0n3 = 1
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5342
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: FrameGadgetCustom()

Post by Kwai chang caine »

Thanks to all for sharing 8)
ImageThe happiness is a road...
Not a destination
Post Reply