* Headers/AppKit/NSSearchFieldCell.h: Give the values assigned in

Cocoa to the enum.  Patch by: doug@riverrock.org
	* Source/NSSearchFieldCell.m: Implemented search menu template and
	recent items.  Patch by: doug@rivverrock.org
	* Source/NSSearchField.m: Call cell methods to use search menu.
	Patch by: doug@riverrock.org


git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/gui/trunk@28233 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
Gregory John Casamento 2009-04-19 04:50:04 +00:00
parent 80a3c7d56e
commit 144289ad4e
4 changed files with 162 additions and 38 deletions

View file

@ -1,3 +1,12 @@
2009-04-19 00:46-EDT Gregory John Casamento <greg.casamento@gmail.com>
* Headers/AppKit/NSSearchFieldCell.h: Give the values assigned in
Cocoa to the enum. Patch by: doug@riverrock.org
* Source/NSSearchFieldCell.m: Implemented search menu template and
recent items. Patch by: doug@rivverrock.org
* Source/NSSearchField.m: Call cell methods to use search menu.
Patch by: doug@riverrock.org
2009-04-19 00:23-EDT Gregory John Casamento <greg.casamento@gmail.com> 2009-04-19 00:23-EDT Gregory John Casamento <greg.casamento@gmail.com>
* Source/NSApplication.m: Make certain that a sheet is always placed * Source/NSApplication.m: Make certain that a sheet is always placed

View file

@ -37,15 +37,15 @@
enum enum
{ {
NSSearchFieldRecentsTitleMenuItemTag, NSSearchFieldRecentsTitleMenuItemTag = 1000,
NSSearchFieldRecentsMenuItemTag, NSSearchFieldRecentsMenuItemTag = 1001,
NSSearchFieldClearRecentsMenuItemTag, NSSearchFieldClearRecentsMenuItemTag = 1002,
NSSearchFieldNoRecentsMenuItemTag NSSearchFieldNoRecentsMenuItemTag = 1003
}; };
@interface NSSearchFieldCell : NSTextFieldCell @interface NSSearchFieldCell : NSTextFieldCell
{ {
NSArray *_recent_searches; NSMutableArray *_recent_searches;
NSString *_recents_autosave_name; NSString *_recents_autosave_name;
NSButtonCell *_search_button_cell; NSButtonCell *_search_button_cell;
NSButtonCell *_cancel_button_cell; NSButtonCell *_cancel_button_cell;

View file

@ -81,4 +81,42 @@ static Class usedCellClass;
[[self cell] setRecentsAutosaveName: name]; [[self cell] setRecentsAutosaveName: name];
} }
//
// Handling Events
//
- (void) mouseDown: (NSEvent*)theEvent
{
[[self cell] trackMouse: theEvent
inRect: [self bounds]
ofView: self
untilMouseUp: YES];
}
- (void)delete:(id)sender
{
[self setStringValue:@""]; // this may need to do more (like send action), but start here...
[[self cell] performClick:self];
}
// Cocoa only defines these methods on the cell, but nib loading targets the field itself
- (void) setSearchMenuTemplate:(NSMenu *)newTemplate
{
[[self cell] setSearchMenuTemplate:newTemplate];
}
- (void) setSendsWholeSearchString: (BOOL)flag
{
[[self cell] setSendsWholeSearchString: flag];
}
- (void) setSendsSearchStringImmediately: (BOOL)flag
{
[[self cell] setSendsSearchStringImmediately: flag];
}
- (void) setMaximumRecents: (int)max
{
[[self cell] setMaximumRecents: max];
}
@end /* NSSearchField */ @end /* NSSearchField */

View file

@ -34,6 +34,7 @@
#include <Foundation/NSNotification.h> #include <Foundation/NSNotification.h>
#include <Foundation/NSString.h> #include <Foundation/NSString.h>
#include <Foundation/NSUserDefaults.h> #include <Foundation/NSUserDefaults.h>
#include <AppKit/NSApplication.h>
#include <AppKit/NSButtonCell.h> #include <AppKit/NSButtonCell.h>
#include <AppKit/NSEvent.h> #include <AppKit/NSEvent.h>
#include <AppKit/NSImage.h> #include <AppKit/NSImage.h>
@ -61,7 +62,7 @@
if (self) if (self)
{ {
NSButtonCell *c; NSButtonCell *c;
NSMenu *template; // NSMenu *template;
c = [[NSButtonCell alloc] initImageCell: nil]; c = [[NSButtonCell alloc] initImageCell: nil];
[self setCancelButtonCell: c]; [self setCancelButtonCell: c];
@ -73,12 +74,15 @@
RELEASE(c); RELEASE(c);
[self resetSearchButtonCell]; [self resetSearchButtonCell];
/* Don't set the searchMenuTemplate unless it is explicitly set in code or by a nib connection
template = [self _buildTemplate]; template = [self _buildTemplate];
[self setSearchMenuTemplate: template]; [self setSearchMenuTemplate: template];
RELEASE(template); RELEASE(template);
*/
//_recent_searches = nil; //_recent_searches = [[NSMutableArray alloc] init];
//_recents_autosave_name = nil; //_recents_autosave_name = nil;
[self _loadSearches];
_max_recents = 10; _max_recents = 10;
} }
@ -102,7 +106,7 @@
c->_cancel_button_cell = [_cancel_button_cell copyWithZone: zone]; c->_cancel_button_cell = [_cancel_button_cell copyWithZone: zone];
c->_search_button_cell = [_search_button_cell copyWithZone: zone]; c->_search_button_cell = [_search_button_cell copyWithZone: zone];
c->_recent_searches = [_recent_searches copyWithZone: zone]; c->_recent_searches = [_recent_searches mutableCopyWithZone: zone];
c->_recents_autosave_name = [_recents_autosave_name copyWithZone: zone]; c->_recents_autosave_name = [_recents_autosave_name copyWithZone: zone];
c->_menu_template = [_menu_template copyWithZone: zone]; c->_menu_template = [_menu_template copyWithZone: zone];
@ -175,6 +179,12 @@
return _recents_autosave_name; return _recents_autosave_name;
} }
- (void) setRecentsAutosaveName: (NSString *)name
{
ASSIGN(_recents_autosave_name, name);
[self _loadSearches];
}
- (void) setRecentSearches: (NSArray *)searches - (void) setRecentSearches: (NSArray *)searches
{ {
int max; int max;
@ -185,14 +195,28 @@
id buffer[max]; id buffer[max];
[searches getObjects: buffer range: NSMakeRange(0, max)]; [searches getObjects: buffer range: NSMakeRange(0, max)];
searches = [NSArray arrayWithObjects: buffer count: max]; searches = [NSMutableArray arrayWithObjects: buffer count: max];
}
else
{
searches = [NSMutableArray arrayWithArray:searches];
} }
ASSIGN(_recent_searches, searches); ASSIGN(_recent_searches, searches);
[self _saveSearches];
} }
- (void) setRecentsAutosaveName: (NSString *)name - (void) addToRecentSearches:(NSString *)searchTerm
{ {
ASSIGN(_recents_autosave_name, name); if (!_recent_searches)
{
ASSIGN(_recent_searches, [NSMutableArray array]);
}
if (searchTerm != nil && [searchTerm length] > 0
&& [_recent_searches indexOfObject:searchTerm] == NSNotFound)
{
[_recent_searches addObject:searchTerm];
[self _saveSearches];
}
} }
- (NSMenu *) searchMenuTemplate - (NSMenu *) searchMenuTemplate
@ -203,6 +227,16 @@
- (void) setSearchMenuTemplate: (NSMenu *)menu - (void) setSearchMenuTemplate: (NSMenu *)menu
{ {
ASSIGN(_menu_template, menu); ASSIGN(_menu_template, menu);
if (menu)
{
[[self searchButtonCell] setTarget:self];
[[self searchButtonCell] setAction:@selector(_openPopup:)];
[[self searchButtonCell] sendActionOn:NSLeftMouseDownMask];
}
else
{
[self resetSearchButtonCell];
}
} }
- (NSButtonCell *) cancelButtonCell - (NSButtonCell *) cancelButtonCell
@ -256,8 +290,11 @@
[c setEditable: NO]; [c setEditable: NO];
[c setImagePosition: NSImageOnly]; [c setImagePosition: NSImageOnly];
[c setImage: [NSImage imageNamed: @"GSSearch"]]; [c setImage: [NSImage imageNamed: @"GSSearch"]];
[c setAction: [self action]]; // [c setAction: [self action]];
[c setTarget: [self target]]; // [c setTarget: [self target]];
[c setAction: @selector(performClick:)];
[c setTarget: self];
[c sendActionOn:NSLeftMouseUpMask];
[c setKeyEquivalent: @"\r"]; [c setKeyEquivalent: @"\r"];
} }
@ -328,6 +365,7 @@
- (void) endEditing: (NSText *)editor - (void) endEditing: (NSText *)editor
{ {
[self addToRecentSearches:[[[editor string] copy] autorelease]];
[super endEditing: editor]; [super endEditing: editor];
[[NSNotificationCenter defaultCenter] [[NSNotificationCenter defaultCenter]
removeObserver: self removeObserver: self
@ -504,8 +542,9 @@
NSMenuView *mr; NSMenuView *mr;
NSWindow *cvWin; NSWindow *cvWin;
NSRect cellFrame; NSRect cellFrame;
NSRect textRect;
int i; int i;
int recentCount = [_recent_searches count];
// NSRect textRect;
template = [self searchMenuTemplate]; template = [self searchMenuTemplate];
popupmenu = [[NSMenu alloc] init]; popupmenu = [[NSMenu alloc] init];
@ -514,42 +553,68 @@
for (i = 0; i < [template numberOfItems]; i++) for (i = 0; i < [template numberOfItems]; i++)
{ {
int tag; int tag;
NSMenuItem *item; NSMenuItem *item, *newItem = nil;
int count = [_recent_searches count];
item = (NSMenuItem*)[template itemAtIndex: i]; item = (NSMenuItem*)[template itemAtIndex: i];
tag = [item tag]; tag = [item tag];
if ((tag == NSSearchFieldRecentsTitleMenuItemTag) || if (tag == NSSearchFieldRecentsTitleMenuItemTag)
(tag == NSSearchFieldClearRecentsMenuItemTag) ||
((tag == NSSearchFieldRecentsMenuItemTag) && ((count == 0))))
{ {
NSMenuItem *copy; if (recentCount > 0) // only show items with this tag if there are recent searches
{
copy = [item copy]; newItem = [[item copy] autorelease];
[popupmenu addItem: copy]; }
RELEASE(copy); }
else if (tag == NSSearchFieldClearRecentsMenuItemTag)
{
if (recentCount > 0) // only show items with this tag if there are recent searches
{
newItem = [[item copy] autorelease];
[newItem setTarget:self];
[newItem setAction:@selector(_clearSearches:)];
}
}
else if (tag == NSSearchFieldNoRecentsMenuItemTag)
{
if (recentCount == 0) // only show items with this tag if there are NO recent searches
{
newItem = [[item copy] autorelease];
}
} }
else if (tag == NSSearchFieldRecentsMenuItemTag) else if (tag == NSSearchFieldRecentsMenuItemTag)
{ {
int j; int j;
for (j = 0; j < count; j++) for (j = 0; j < recentCount; j++)
{ {
[popupmenu addItemWithTitle: [_recent_searches objectAtIndex: j] NSMenuItem *searchItem = [popupmenu addItemWithTitle:
action: [item action] [_recent_searches objectAtIndex: j]
keyEquivalent: [item keyEquivalent]]; action:
@selector(_searchForRecent:)
keyEquivalent:
[item keyEquivalent]];
[searchItem setTarget:self];
} }
} }
else // copy all other items without special tags from the template into the popup
{
newItem = [[item copy] autorelease];
}
if (newItem != nil)
{
[popupmenu addItem:newItem];
}
} }
// Prepare to display the popup // Prepare to display the popup
cvWin = [_control_view window]; cvWin = [_control_view window];
cellFrame = [_control_view bounds]; cellFrame = [_control_view frame];
textRect = [self searchTextRectForBounds: cellFrame] ; cellFrame = [[_control_view superview] convertRect:cellFrame toView:nil]; // convert to window coordinates
cellFrame.origin = [cvWin convertBaseToScreen:cellFrame.origin]; // convert to screen coordinates
mr = [popupmenu menuRepresentation]; mr = [popupmenu menuRepresentation];
// Ask the MenuView to attach the menu to this rect // Ask the MenuView to attach the menu to this rect
[mr setWindowFrameForAttachingToRect: textRect [mr setWindowFrameForAttachingToRect: cellFrame
onScreen: [cvWin screen] onScreen: [cvWin screen]
preferredEdge: NSMinYEdge preferredEdge: NSMinYEdge
popUpSelectedItem: -1]; popUpSelectedItem: -1];
@ -557,9 +622,17 @@
// Last, display the window // Last, display the window
[[mr window] orderFrontRegardless]; [[mr window] orderFrontRegardless];
[mr mouseDown:[NSApp currentEvent]];
AUTORELEASE(popupmenu); AUTORELEASE(popupmenu);
} }
- (void) _searchForRecent: (id)sender
{
NSString *searchTerm = [sender title];
[(id)_control_view setStringValue:searchTerm];
[self performClick:self]; // do the search
}
- (void) _clearSearches: (id)sender - (void) _clearSearches: (id)sender
{ {
[self setRecentSearches: [NSArray array]]; [self setRecentSearches: [NSArray array]];
@ -569,19 +642,23 @@
{ {
NSArray *list; NSArray *list;
NSString *name = [self recentsAutosaveName]; NSString *name = [self recentsAutosaveName];
if (name)
{
list = [[NSUserDefaults standardUserDefaults] list = [[NSUserDefaults standardUserDefaults]
stringArrayForKey: name]; stringArrayForKey: name];
[self setRecentSearches: list]; [self setRecentSearches: list];
} }
}
- (void) _saveSearches - (void) _saveSearches
{ {
NSArray *list = [self recentSearches]; NSArray *list = [self recentSearches];
NSString *name = [self recentsAutosaveName]; NSString *name = [self recentsAutosaveName];
if (name && list)
{
[[NSUserDefaults standardUserDefaults] [[NSUserDefaults standardUserDefaults]
setObject: list forKey: name]; setObject: list forKey: name];
} }
}
@end /* NSSearchFieldCell Private */ @end /* NSSearchFieldCell Private */