OptionGadget color

Just starting out? Need help? Post your questions and find answers here.
User avatar
rndrei
Enthusiast
Enthusiast
Posts: 181
Joined: Thu Dec 28, 2023 9:04 pm

OptionGadget color

Post by rndrei »

Can you tell me how to change the color of the text in the OptionGadget for macOS?
The error is given by the following example:

Code: Select all

If OpenWindow(0, 0, 0, 400, 200, "optiongadget color", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  OptionGadget(1, 20, 20, 200, 25, "Option")
  CocoaMessage(0, GadgetID(1), "setBackgroundColor:",  RGB(255,0,0))
  Repeat
    If WaitWindowEvent() = #PB_Event_CloseWindow
      Break
    EndIf
  ForEver
EndIf
User avatar
Shardik
Addict
Addict
Posts: 2065
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: OptionGadget color

Post by Shardik »

The following example allows you to set the color of text and background of an OptionGadget (successfully tested on MacOS 15.7.1 'Sequoia' with PB 6.21):

Code: Select all

EnableExplicit

Procedure SetOptionGadgetColor(GadgetID.I, ColorType.I, Color.I)
  Protected Alpha.CGFloat
  Protected AttributedTitle.I
  Protected Cell.I
  Protected Blue.CGFloat
  Protected Green.CGFloat
  Protected NSColor.I
  Protected Range.NSRange
  Protected Red.CGFloat

  If GadgetType(GadgetID) = #PB_GadgetType_Option
    Alpha = 1.0
    Red = Red(Color) / 255
    Green = Green(Color) / 255
    Blue = Blue(Color) / 255

    Select ColorType
      Case #PB_Gadget_BackColor
        Alpha = 1.0
        Red = Red(Color) / 255
        Green = Green(Color) / 255
        Blue = Blue(Color) / 255
        CocoaMessage(0, CocoaMessage(0, GadgetID(GadgetID), "cell"),
          "setBackgroundColor:", CocoaMessage(0, 0,
          "NSColor colorWithDeviceRed:@", @Red,
          "green:@", @Green,
          "blue:@", @Blue,
          "alpha:@", @Alpha))
      Case #PB_Gadget_FrontColor
        Cell = CocoaMessage(0, GadgetID(GadgetID), "cell")
        AttributedTitle = CocoaMessage(0, CocoaMessage(0, 0,
          "NSMutableAttributedString alloc"), "initWithAttributedString:",
          CocoaMessage(0, Cell, "attributedTitle"))
        Range\length = CocoaMessage(0, AttributedTitle, "length")
        NSColor = CocoaMessage(0, 0,
          "NSColor colorWithDeviceRed:@", @Red,
          "green:@", @Green,
          "blue:@", @Blue,
          "alpha:@", @Alpha)
        CocoaMessage(0, AttributedTitle,
          "addAttribute:$", @"NSColor",
          "value:", NSColor,
          "range:@", @Range)
        CocoaMessage(0, Cell,
          "setAttributedTitle:", AttributedTitle)
    EndSelect
  EndIf
EndProcedure

OpenWindow(0, 270, 100, 370, 70, "Colored OptionGadget")
OptionGadget(0, 10, 20, 350, 30,
  "OptionGadget with indigo background and khaki text")
SetOptionGadgetColor(0, #PB_Gadget_BackColor, $82004B)
SetOptionGadgetColor(0, #PB_Gadget_FrontColor, $8CE6F0)

Repeat
Until WaitWindowEvent() = #PB_Event_CloseWindow
Collection of cross-platform examples with API functions to extend PureBasic
User avatar
rndrei
Enthusiast
Enthusiast
Posts: 181
Joined: Thu Dec 28, 2023 9:04 pm

Re: OptionGadget color

Post by rndrei »

Thank you, everything works, and on macOS 15.6
Post Reply