The following new code example displays the search string even when the search field lost it's focus (tested successfully on MacOS 10.6.8 'Snow Leopard' with PB 5.44 x86 in ASCII and Unicode mode and on MacOS 10.7.5 'Lion' with PB 5.44 x86 and x64 in ASCII and Unicode mode):
Code: Select all
EnableExplicit
#NSFocusRingOnly = 0
ImportC ""
NSRectFill(*Rect.NSRect)
NSSetFocusRingStyle(FocusRingPlacement.I)
EndImport
Define Frame.NSRect
Define SearchField.I
Define SearchFieldCell.I
Define SubclassedSearchFieldCell.I
ProcedureC SubclassedSearchFieldCellCallback(SearchFieldCell.I, Selector.I,
x86.F, y86.F, Width86.F, Height86.F, SearchField.I, D1.D, D2.D, D3.D, D4.D,
x64.D, y64.D, Width64.D, Height64.D)
Protected BackColor.I
Protected CellFrame.NSRect
Protected Radius.CGFloat
CompilerIf #PB_Compiler_Processor = #PB_Processor_x64
CellFrame\origin\x = x64 + 1
CellFrame\origin\y = y64 + 3
CellFrame\size\width = Width64 - 2
CellFrame\size\height = Height64 - 5
CompilerElse
CellFrame\origin\x = x86 + 1
CellFrame\origin\y = y86 + 3
CellFrame\size\width = Width86 - 2
CellFrame\size\height = Height86 - 5
CompilerEndIf
; ----- Fill in back color
BackColor = CocoaMessage(0, 0, "NSColor yellowColor")
Radius = CellFrame\size\height / 2.0
CocoaMessage(0, BackColor, "setFill")
CocoaMessage(0, CocoaMessage(0, 0,
"NSBezierPath bezierPathWithRoundedRect:@", @CellFrame,
"xRadius:@", @Radius,
"yRadius:@", @Radius),
"fill")
; ----- Draw text content and buttons
CellFrame\origin\x - 1
CocoaMessage(0, SearchFieldCell,
"drawInteriorWithFrame:@", @CellFrame,
"inView:", SearchField)
; ----- Draw border around search field
Radius + 1
CocoaMessage(0, CocoaMessage(0, 0, "NSColor lightGrayColor"), "setStroke")
CocoaMessage(0, CocoaMessage(0, 0,
"NSBezierPath bezierPathWithRoundedRect:@", @CellFrame,
"xRadius:@", @Radius,
"yRadius:@", @Radius),
"stroke")
If CocoaMessage(0, SearchFieldCell, "showsFirstResponder")
; ----- Draw focus ring around search field
CocoaMessage(0, 0, "NSGraphicsContext saveGraphicsState")
NSSetFocusRingStyle(#NSFocusRingOnly)
CocoaMessage(0, CocoaMessage(0, 0,
"NSBezierPath bezierPathWithRoundedRect:@", @CellFrame,
"xRadius:@", @Radius,
"yRadius:@", @Radius),
"fill")
CocoaMessage(0, 0, "NSGraphicsContext restoreGraphicsState")
EndIf
EndProcedure
OpenWindow(0, 270, 100, 220, 75, "Colored NSSearchField")
StringGadget(0, 70, 40, 80, 25, "")
; ----- Create NSSearchField
Frame\origin\x = 10
Frame\origin\y = 10
Frame\size\width = WindowWidth(0) - 20
Frame\size\height = 25
SearchField = CocoaMessage(0, 0, "NSSearchField alloc")
CocoaMessage(0, SearchField, "initWithFrame:@", @Frame)
CocoaMessage(0, CocoaMessage(0, WindowID(0), "contentView"),
"addSubview:", SearchField)
; ----- Get NSSearchFieldCell
SearchFieldCell = CocoaMessage(0, SearchField, "cell")
; ----- Subclass NSSearchFieldCell
SubclassedSearchFieldCell = objc_allocateClassPair_(CocoaMessage(0,
SearchFieldCell, "class"), "SubclassedSearchFieldCell", 0)
object_setClass_(SearchFieldCell, SubclassedSearchFieldCell)
objc_registerClassPair_(SubclassedSearchFieldCell)
; ----- Set callback in SearchFieldCell for method drawWithFrame:inView:
class_addMethod_(SubclassedSearchFieldCell,
sel_registerName_("drawWithFrame:inView:"),
@SubclassedSearchFieldCellCallback(), "v@:@@")
Repeat
Until WaitWindowEvent() = #PB_Event_CloseWindow