NSSearchField

Hi all, after reading through Patrick’s thread titled “Filter Control” I started wondering if it would be too difficult to implement a native NSSearchField in Xojo…
Well after a few hours I came up with something that seems good.

NSSearch.xojo_binary_project

If anyone needs it, here it is. It would need some #If TargetCocoa tags if you were to use it in a Cocoa version of an X-platform app…
It has recent searches and autosave of the recents list (in your app’s preferences plist). You may need to add some error checking for real world use, as this is mainly a proof of concept, just to see if I could get it working…

I’m thinking I’d like to combine all of these little Cocoa classes I have into one project to be picked from… Not a monolithic MacOSLib sort of thing, just some easy to use Cocoa classes.

Thanks a lot for sharing, Jim!
I already use NSSearchField a lot but haven’t found yet how to influence its background color and border width, therefore it’s a bit limited yet. But this is probably a case of better understanding declares (or some introspection work to be done).

I like the idea of Cocoa classes on demand (or whatever you’d like to call them). Some portions of MacOSLib give deprecation warnings, so this could be a boost towards actuality for the Lib if you wouldn’t mind re-contributing stuff. After all, it’s a great tool but, yes, quite mighty in proportions as well.

Maybe it’d be good to make macoslib less monolithic.

Nice work Jim :slight_smile:

Ulrich, if you can’t get the Border/Colour thing going with declares then my Filter Control should let you do that. But you do lose the nativeness that Jim’s Declare based control gives you.

[quote=103541:@Ulrich Bogun]Thanks a lot for sharing, Jim!
I already use NSSearchField a lot but haven’t found yet how to influence its background color and border width, therefore it’s a bit limited yet. But this is probably a case of better understanding declares (or some introspection work to be done).
[/quote]

Changing the default behavior of built-in Cocoa controls

Anybody? :slight_smile:

You could always have a go yourself. You’ve been given the code for the main control.

Seems NSSearchField inherits from NSTextField which exposes SetBackgroundColor - that would be my starting point.

Have a play.

[quote=103702:@Patrick Delaney]Seems NSSearchField inherits from NSTextField which exposes SetBackgroundColor - that would be my starting point.

[/quote]
Oddly, the setting has no effect. You have to subclass the NSSearchFieldCell to override the drawInteriorWithFrame:inView: and paint the background yourself… then you have to call it on the superclass to get the buttons to draw… eek! I did it though!

Here’s the new version with backgroundcolor and textcolor

You could change the colors (or icons) of the buttons with cancelButtonCell and searchButtonCell… You’d have to pull the image and transform it to the color you want and reset it to the NSButtonCell etc… beyond my effort for today :wink:

I just uploaded a fixed version to reduce the drawing area to reveal the border area… I tried to stroke the bezierPath myself, but it wouldn’t draw…?


I just couldn’t help myself…
The same URL will download the multicolor version.

Amazing! Again a lot of thanks, Jim!

nicely done sir…

Fantastic work Jim. Great examples for learning.

Thank you, Jim, excellent work and a real help.

Your other controls would be very useful, too.

These declares should be added to the open event in NSSearchField project. To make it work correctly.

Declare sub setUsesSingleLineMode lib CocoaLib Selector "setUsesSingleLineMode:" (obj_id as Ptr, value as Boolean)
setUsesSingleLineMode(cell(myNSSearch), true)

declare sub setWraps lib CocoaLib Selector "setWraps:" (obj_id as Ptr, value as Boolean)
setWraps(cell(myNSSearch), false)

declare sub setScrollable lib CocoaLib Selector "setScrollable:" (obj_id as Ptr, value as Boolean)
setScrollable(cell(myNSSearch), true)

[quote=113607:@Richard Berglund]Declare sub setUsesSingleLineMode lib CocoaLib Selector “setUsesSingleLineMode:” (obj_id as Ptr, value as Boolean)
setUsesSingleLineMode(cell(myNSSearch), true)

declare sub setWraps lib CocoaLib Selector “setWraps:” (obj_id as Ptr, value as Boolean)
setWraps(cell(myNSSearch), false)

declare sub setScrollable lib CocoaLib Selector “setScrollable:” (obj_id as Ptr, value as Boolean)
setScrollable(cell(myNSSearch), true)[/quote]

adding those code into the open event of NSSearchField does not work. i change the code as show below

   Declare sub setUsesSingleLineMode lib "Cocoa" Selector "setUsesSingleLineMode:" (obj_id as Ptr, value as Boolean)
    setUsesSingleLineMode(myNSSearch, true)
    
    declare sub setWraps lib "Cocoa" Selector "setWraps:" (obj_id as Ptr, value as Boolean)
    setWraps(myNSSearch, false)
    
    declare sub setScrollable lib "Cocoa" Selector "setScrollable:" (obj_id as Ptr, value as Boolean)
    setScrollable(myNSSearch, true)

Weird - works for me.

[quote=103535:@jim mckay]Hi all, after reading through Patrick’s thread titled “Filter Control” I started wondering if it would be too difficult to implement a native NSSearchField in Xojo…
Well after a few hours I came up with something that seems good.

NSSearch.xojo_binary_project

If anyone needs it, here it is. It would need some #If TargetCocoa tags if you were to use it in a Cocoa version of an X-platform app…
It has recent searches and autosave of the recents list (in your app’s preferences plist). You may need to add some error checking for real world use, as this is mainly a proof of concept, just to see if I could get it working…

I’m thinking I’d like to combine all of these little Cocoa classes I have into one project to be picked from… Not a monolithic MacOSLib sort of thing, just some easy to use Cocoa classes.[/quote]

Jim, this is brilliant.

Thank you :slight_smile:

Richard: I found in my project a global string constant defining Cocoa as “CocoaLib” – probably from some example I included. Maybe it’s the same for you?

no wonder Ulrich

Jim: Having seen Paul‘s webinar yesterday I had to realize it still makes a huge difference if someone explains a programming concept or if it the facts are only written (I knew each of the topics from the manual and examples folder but Paul‘s explanations gave me a complete new outlook on many of the topics included).

Seeing that I am still not able to create something like your colored NSSearchfield on my own (trying to translate some Apple dev docs to Xojo shows me where pieces are still missing, knowledge-wise), I was wondering if you could be interested in giving a webinar on that. I know there‘s a good online resource out there (https://www.declaresub.com/ideclare/), but I suffer from the effect mentioned above: Some pieces of information are missing for me, so this all does‘t fall into places.

Other possibilities would be a MacOSLib pt. II webinar by Kem on that, or maybe Alwyn who is publishing great tutorials already would like to share some knowledge “personally”? I‘d really love to be able to contribute System calls to a library on my own, and I guess many more here think so too, so I can imagine such a webinar would be highly appreciated. Or does it already exist and I missed it?