CocoaDev

Edit AllPages


I’ve left my starter code here, but would suggest interested programers checkout:

http://s.sudre.free.fr/Software/DevPotPourri.html

first. It implements the entire Mail.app search text field behavior.


Here’s some starter code for creating a Mail.app like search field (rounded, magnifying glass, close button…). But it doesn’t really work! I’ve seen a number of questions regarding this, hopefully we can get this fully functional, will probably save many people lots of time.

So here’s my start, (I?ve come to the conclusion that I don?t really understand NSCell?s yet) I would encourage people to clean it up and make it as “cocoa” as possible, I’m looking for a working well done search field, I don’t really care if it follows the design of my current partial solution. This code seems to draw mostly correctly, but I can?t seem to get mouse events and so can?t make the close button work. Also I haven?t added a popup menu for when the magnifying glass is clicked on, I don?t need that for my app, but it would still be nice to see how to do it.

I’ve provided a project builder project along with image resources for this code at: http://www.hogbay.com/software/SearchTextAreaExample.zip

// // SearchFieldCell.h // // Created by Jesse Grosjean on Thu Nov 21 2002. //

#import <Cocoa/Cocoa.h>

@interface SearchFieldCell : NSTextFieldCell { BOOL _showCancelButton; }

@end

// // SearchFieldCell.m // // Created by Jesse Grosjean on Thu Nov 21 2002. //

#import “SearchFieldCell.h”

@implementation SearchFieldCell

static NSImage *leftSearchCapImage = nil; static NSImage *middleSearchImage = nil; static NSImage *rightSearchCapImage = nil; static NSImage *rightSearchStopCapImage = nil; static NSImageCell *_leftCapCell; static NSImageCell *_middleCell; static NSImageCell *_rightCapCell; static NSImageCell *_rightStopCapCell; static float textOffset = 2.5; // yuck why do i use this?

// can’t seem to get any mouse tracking to work.

@end


You could probably subclass CCDTextField and/or CCDTextFieldCell and add some search-specific stuff pretty easily.