The code Shardik posted is from a long time ago and still Carbon based instead of Cocoa.
When using Cocoa, you can use
setBackgroundColor:
Code: Select all
Procedure.i NSColorBGR(BGR.l)
Protected.CGFloat r, g, b, a = 1
Protected *byte.Ascii = @BGR
r = *byte\a / 255: *byte + 1
g = *byte\a / 255: *byte + 1
b = *byte\a / 255
ProcedureReturn CocoaMessage(0, 0, "NSColor colorWithCalibratedRed:@", @r, "green:@", @g, "blue:@", @b, "alpha:@", @a)
EndProcedure
Procedure.i NSColorRGB(RGB.l)
Protected.CGFloat r, g, b, a = 1
Protected *byte.Ascii = @RGB
b = *byte\a / 255: *byte + 1
g = *byte\a / 255: *byte + 1
r = *byte\a / 255
ProcedureReturn CocoaMessage(0, 0, "NSColor colorWithCalibratedRed:@", @r, "green:@", @g, "blue:@", @b, "alpha:@", @a)
EndProcedure
OpenWindow(0, 0, 0, 350, 38, "TextGadget with light green background", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
TextGadget(0, 10, 10, WindowWidth(0) - 20, WindowHeight(0) - 20, "The quick brown fox jumps over the lazy dog.", #PB_Text_Border)
CocoaMessage(0, GadgetID(0), "setBackgroundColor:", NSColorBGR($6BE194))
Repeat
Until WaitWindowEvent() = #PB_Event_CloseWindow
For a checkbox, you can set an attributed title.
This allows you also to only set part of the title to a different background color using the range property.
To do the same on a TextGadget (only part of the text a background color), you can use setAttributedStringValue: instead of setAttributedTitle:
Code: Select all
Procedure.i NSColorBGR(BGR.l)
Protected.CGFloat r, g, b, a = 1
Protected *byte.Ascii = @BGR
r = *byte\a / 255: *byte + 1
g = *byte\a / 255: *byte + 1
b = *byte\a / 255
ProcedureReturn CocoaMessage(0, 0, "NSColor colorWithCalibratedRed:@", @r, "green:@", @g, "blue:@", @b, "alpha:@", @a)
EndProcedure
Procedure.i NSColorRGB(RGB.l)
Protected.CGFloat r, g, b, a = 1
Protected *byte.Ascii = @RGB
b = *byte\a / 255: *byte + 1
g = *byte\a / 255: *byte + 1
r = *byte\a / 255
ProcedureReturn CocoaMessage(0, 0, "NSColor colorWithCalibratedRed:@", @r, "green:@", @g, "blue:@", @b, "alpha:@", @a)
EndProcedure
OpenWindow(0, 0, 0, 350, 38, "CheckBoxGadget with light green background", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
CheckBoxLabel.s = "The quick brown fox jumps over the lazy dog."
CheckBoxGadget(0, 10, 10, WindowWidth(0) - 20, WindowHeight(0) - 20, CheckBoxLabel)
AttributedTitle = CocoaMessage(0, CocoaMessage(0, 0, "NSMutableAttributedString alloc"), "initWithString:$", @CheckBoxLabel)
Range.NSRange\length = Len(CheckBoxLabel)
CocoaMessage(0, AttributedTitle, "addAttribute:$", @"NSBackgroundColor", "value:", NSColorBGR($6BE194), "range:@", @Range)
CocoaMessage(0, GadgetID(0), "setAttributedTitle:", AttributedTitle)
CocoaMessage(0, AttributedTitle, "release")
Repeat
Until WaitWindowEvent() = #PB_Event_CloseWindow