1 /* Copyright (c) 2006-2007 Christopher J. W. Lloyd
3 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
5 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
7 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
9 #import <AppKit/NSSearchFieldCell.h>
10 #import <AppKit/NSButtonCell.h>
11 #import <AppKit/NSMenu.h>
12 #import <AppKit/NSImage.h>
13 #import <AppKit/NSTextField.h>
14 #import <AppKit/NSControl.h>
15 #import <AppKit/NSTextField.h>
16 #import <AppKit/NSRaise.h>
18 @implementation NSSearchFieldCell
20 -initWithCoder:(NSCoder *)coder {
21 [super initWithCoder:coder];
22 _searchButtonCell=[[NSButtonCell alloc] initImageCell:[NSImage imageNamed:@"NSSearchGlass"]];
23 [_searchButtonCell setImageScaling:NSImageScaleProportionallyUpOrDown];
24 _cancelButtonCell=[[NSButtonCell alloc] initImageCell:[NSImage imageNamed:@"NSStopProgressFreestandingTemplate"]];
25 [_cancelButtonCell setImageScaling:NSImageScaleProportionallyUpOrDown];
29 -initTextCell:(NSString *)string {
30 [super initTextCell:string];
31 _searchButtonCell=[[NSButtonCell alloc] initImageCell:[NSImage imageNamed:@"NSSearchGlass"]];
32 [_searchButtonCell setImageScaling:NSImageScaleProportionallyUpOrDown];
33 _cancelButtonCell=[[NSButtonCell alloc] initImageCell:[NSImage imageNamed:@"NSStopProgressFreestandingTemplate"]];
34 [_cancelButtonCell setImageScaling:NSImageScaleProportionallyUpOrDown];
39 [_searchButtonCell release];
40 [_cancelButtonCell release];
44 -(NSArray *)recentSearches {
45 return _recentSearches;
48 -(NSString *)recentsAutosaveName {
52 -(int)maximumRecents {
53 return _maximumRecents;
56 -(BOOL)sendsWholeSearchString {
57 return _sendsWholeSearchString;
60 -(BOOL)sendsSearchStringImmediately {
61 return _sendsSearchStringImmediately;
64 -(NSButtonCell *)searchButtonCell {
65 return _searchButtonCell;
68 -(NSButtonCell *)cancelButtonCell {
69 return _cancelButtonCell;
72 -(NSMenu *)searchMenuTemplate {
73 return _searchMenuTemplate;
76 -(void)setRecentSearches:(NSArray *)searches {
77 searches=[searches copy];
78 [_recentSearches release];
79 _recentSearches=searches;
82 -(void)setRecentsAutosaveName:(NSString *)name {
84 [_autosaveName release];
88 -(void)setMaximumRecents:(int)value {
89 _maximumRecents=value;
92 -(void)setSendsWholeSearchString:(BOOL)flag {
93 _sendsWholeSearchString=flag;
96 -(void)setSendsSearchStringImmediately:(BOOL)flag {
97 _sendsSearchStringImmediately=flag;
100 -(void)setSearchButtonCell:(NSButtonCell *)cell {
102 [_searchButtonCell release];
103 _searchButtonCell=cell;
106 -(void)setCancelButtonCell:(NSButtonCell *)cell {
108 [_cancelButtonCell release];
109 _cancelButtonCell=cell;
112 -(void)setSearchMenuTemplate:(NSMenu *)menu {
114 [_searchMenuTemplate release];
115 _searchMenuTemplate=menu;
118 -(NSRect)searchTextRectForBounds:(NSRect)rect {
119 return [self titleRectForBounds:rect];
122 -(NSRect)searchButtonRectForBounds:(NSRect)rect {
123 NSRect result=[self titleRectForBounds:rect];
125 result.size.width=result.size.height;
126 result.origin.x-=result.size.width;
131 -(NSRect)cancelButtonRectForBounds:(NSRect)rect {
132 NSRect result=[self titleRectForBounds:rect];
134 result.origin.x+=result.size.width;
135 result.size.width=result.size.height;
140 -(void)resetCancelButtonCell {
141 NSUnimplementedMethod();
144 -(void)resetSearchButtonCell {
145 NSUnimplementedMethod();
148 -(NSRect)titleRectForBounds:(NSRect)rect {
149 CGFloat radius=rect.size.height/2;
150 NSRect result=[super titleRectForBounds:rect];
152 result=NSInsetRect(result,radius,0);
157 -(BOOL)trackMouse:(NSEvent *)event inRect:(NSRect)frame ofView:(NSView *)view untilMouseUp:(BOOL)untilMouseUp {
158 if([_cancelButtonCell trackMouse:event inRect:[self cancelButtonRectForBounds:frame] ofView:view untilMouseUp:YES]){
159 [(NSControl *)view setStringValue:@""];
161 if ([view respondsToSelector:@selector(selectText:)])
162 [(NSTextField *)view selectText:nil];
166 return [super trackMouse:event inRect:frame ofView:view untilMouseUp:untilMouseUp];
169 -(void)drawWithFrame:(NSRect)frame inView:(NSView *)control {
170 [super drawWithFrame:frame inView:control];
172 [_searchButtonCell drawWithFrame:[self searchButtonRectForBounds:frame] inView:control];
173 if([[self attributedStringValue] length]>0)
174 [_cancelButtonCell drawWithFrame:[self cancelButtonRectForBounds:frame] inView:control];