Frame gadget with two colour title?

Just starting out? Need help? Post your questions and find answers here.
User avatar
Dreamland Fantasy
Enthusiast
Enthusiast
Posts: 335
Joined: Fri Jun 11, 2004 9:35 pm
Location: Glasgow, UK
Contact:

Frame gadget with two colour title?

Post by Dreamland Fantasy »

Hi there,

I don't think this will be doable, but is it possible to have a frame gadget that has two different colours for the title text? I'm looking to have an asterix in red prefixed to the title to denote that this section is required to be completed.

Kind regards,

Francis
firace
Addict
Addict
Posts: 946
Joined: Wed Nov 09, 2011 8:58 am

Re: Frame gadget with two colour title?

Post by firace »

Note that this is just a quick hack, tested under Windows. No idea how it behaves on other platforms.

Code: Select all

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

FrameGadget(2,230,23,180,118,"         Black ")
dummy = TextGadget(#PB_Any,0,0,0,0," Red ")
w = GadgetWidth(dummy,#PB_Gadget_RequiredSize)
h = GadgetHeight(dummy,#PB_Gadget_RequiredSize)
txt = TextGadget(#PB_Any,230+8,23,w,h," Red ")
SetGadgetColor(txt,#PB_Gadget_FrontColor, #Red)

Repeat : Until WaitWindowEvent()=#PB_Event_CloseWindow
User avatar
Dreamland Fantasy
Enthusiast
Enthusiast
Posts: 335
Joined: Fri Jun 11, 2004 9:35 pm
Location: Glasgow, UK
Contact:

Re: Frame gadget with two colour title?

Post by Dreamland Fantasy »

firace wrote: Sun Oct 09, 2022 11:54 pm Note that this is just a quick hack, tested under Windows. No idea how it behaves on other platforms.
Thanks for that. It's certainly better than what I have at the moment. :)

Kind regards,

Francis
User avatar
Dreamland Fantasy
Enthusiast
Enthusiast
Posts: 335
Joined: Fri Jun 11, 2004 9:35 pm
Location: Glasgow, UK
Contact:

Re: Frame gadget with two colour title?

Post by Dreamland Fantasy »

Hi there,

My implementation of the code:

Code: Select all

Enumeration
  #AppendFrameTitle_Prefix
  #AppendFrameTitle_Suffix
EndEnumeration

Procedure.i AppendFrameTitle(GadgetID.i, Text$, Color.l, Flag.l = #AppendFrameTitle_Prefix)
  
  Protected.l x = GadgetX(GadgetID) + DesktopUnscaledX(9), y = GadgetY(GadgetID)
  Protected.l w, h, sx
  Protected.i tmpGadgetID, NewGadgetID
  Protected OriginalText$ = GetGadgetText(GadgetID)
  
  tmpGadgetID = TextGadget(#PB_Any, 0, 0, 0, 0, Text$)
  w = GadgetWidth(tmpGadgetID, #PB_Gadget_RequiredSize) - DesktopUnscaledX(2)
  h = GadgetHeight(tmpGadgetID, #PB_Gadget_RequiredSize)
  FreeGadget(tmpGadgetID)
  tmpGadgetID = TextGadget(#PB_Any, 0, 0, 0, 0, OriginalText$)
  sx = GadgetWidth(tmpGadgetID, #PB_Gadget_RequiredSize) + x - DesktopUnscaledX(2)
  FreeGadget(tmpGadgetID)
    
  Select Flag
    Case #AppendFrameTitle_Suffix
      SetGadgetText(GadgetID, OriginalText$ + Text$)
      NewGadgetID = TextGadget(#PB_Any, sx, y, w, h, Text$)
    Default
      SetGadgetText(GadgetID, Text$ + OriginalText$)
      NewGadgetID = TextGadget(#PB_Any, x, y, w, h, Text$)
  EndSelect
  SetGadgetColor(NewGadgetID, #PB_Gadget_FrontColor, Color)
  
  ProcedureReturn NewGadgetID
  
EndProcedure

CompilerIf #PB_Compiler_IsMainFile
  
  OpenWindow(0,100,100,200,245,"Window", #PB_Window_SystemMenu)

  For gadget = 0 To 1
    FrameGadget(gadget, 5, 5 + gadget * 120, 190, 115,"Frame Window #" + gadget)
  Next

  AppendFrameTitle(0, "Prefix ", #Red)
  AppendFrameTitle(1, " Suffix", #Red, #AppendFrameTitle_Suffix)

  Repeat : Until WaitWindowEvent()=#PB_Event_CloseWindow

CompilerEndIf
It should produce the following output:

Image

If anyone has suggestions for improvements, please feel free to add. :)

Kind regards,

Francis
Post Reply