/** NSPopUpButton Popup list class Copyright (C) 1996 Free Software Foundation, Inc. Author: Scott Christley Date: 1996 Author: Michael Hanni Date: June 1999 This file is part of the GNUstep GUI Library. This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; see the file COPYING.LIB. If not, see or write to the Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #import #import #import #import "AppKit/NSApplication.h" #import "AppKit/NSEvent.h" #import "AppKit/NSGraphics.h" #import "AppKit/NSKeyValueBinding.h" #import "AppKit/NSPopUpButton.h" #import "AppKit/NSPopUpButtonCell.h" #import "AppKit/NSMenu.h" #import "AppKit/NSMenuItem.h" #import "AppKit/NSMenuView.h" #import "AppKit/NSWindow.h" /* * class variables */ Class _nspopupbuttonCellClass = 0; /* * NSPopUpButton implementation */ @implementation NSPopUpButton /* * Class methods */ + (void) initialize { if (self == [NSPopUpButton class]) { // Initial version [self setVersion: 1]; [self setCellClass: [NSPopUpButtonCell class]]; [self exposeBinding: NSSelectedIndexBinding]; [self exposeBinding: NSSelectedObjectBinding]; [self exposeBinding: NSSelectedTagBinding]; [self exposeBinding: NSSelectedValueBinding]; [self exposeBinding: NSContentValuesBinding]; } } + (Class) cellClass { return _nspopupbuttonCellClass; } + (void) setCellClass: (Class)classId { _nspopupbuttonCellClass = classId; } /* * Initializing an NSPopUpButton */ - (id) init { return [self initWithFrame: NSZeroRect pullsDown: NO]; } - (id) initWithFrame: (NSRect)frameRect { return [self initWithFrame: frameRect pullsDown: NO]; } /**

Initialize and returns a new NSPopUpButton into the frame frameRect and specified by flag if the NSPopUpButton is a pull-down list

See Also: -setPullsDown: [NSView-initWithFrame:]

*/ - (id) initWithFrame: (NSRect)frameRect pullsDown: (BOOL)flag { if ( ! ( self = [super initWithFrame: frameRect] ) ) return nil; [self setPullsDown: flag]; return self; } /* In NSView, -menuForEvent: returns [self menu] as the context menu of the view. Since our -menu returns the menu for our pop-up, we need to override this to return nil to indicate that we have no context menu. */ - (NSMenu *)menuForEvent:(NSEvent *)theEvent { return nil; } - (void) setMenu: (NSMenu*)menu { [_cell setMenu: menu]; } - (NSMenu*) menu { return [_cell menu]; } /**

Sets whether the NSPopUpButton's cell has a pulls-down list ( YES ) or a pop-up list (NO)

See Also: -pullsDown [NSPopUpButtonCell-setPullsDown:]

*/ - (void) setPullsDown: (BOOL)flag { [_cell setPullsDown: flag]; } /**

Returns whether the NSPopUpButton's cell has a pulls-down list ( YES ) or a pop-up list (NO)

See Also: -setPullsDown: [NSPopUpButtonCell-pullsDown]

*/ - (BOOL) pullsDown { return [_cell pullsDown]; } - (void) setAutoenablesItems: (BOOL)flag { [_cell setAutoenablesItems: flag]; } - (BOOL) autoenablesItems { return [_cell autoenablesItems]; } /**

Inserts a new item with title as its title at the end of the list and synchronizes the NSPopUpButton's title with the title of the selected item.

See Also: [NSPopUpButtonCell-addItemWithTitle:] -synchronizeTitleAndSelectedItem

*/ - (void) addItemWithTitle: (NSString *)title { [_cell addItemWithTitle: title]; [self synchronizeTitleAndSelectedItem]; } /**

Inserts a new list of items with titles as titles at the end of the list and synchronizes the NSPopUpButton's title with the title of the selected item.

See Also: [NSPopUpButtonCell-addItemsWithTitles:] -synchronizeTitleAndSelectedItem

*/ - (void) addItemsWithTitles: (NSArray*)itemTitles { [_cell addItemsWithTitles: itemTitles]; [self synchronizeTitleAndSelectedItem]; } /**

Inserts a new item with title as its title at the specified index and synchronizes the NSPopUpButton's title with the title of the selected item.

See Also: [NSPopUpButtonCell-insertItemWithTitle:atIndex:] -synchronizeTitleAndSelectedItem

*/ - (void) insertItemWithTitle: (NSString*)title atIndex: (NSInteger)index { [_cell insertItemWithTitle: title atIndex: index]; [self synchronizeTitleAndSelectedItem]; } /**

Removes all items from the item list and synchronizes the NSPopUpButton's title with the title of the selected

See Also: [NSPopUpButtonCell-removeAllItems] -removeItemWithTitle: -synchronizeTitleAndSelectedItem

*/ - (void) removeAllItems { [_cell removeAllItems]; [self synchronizeTitleAndSelectedItem]; } /**

Removes the item specified with title as its title from the item list and synchronizes the NSPopUpButton's title with the title of the selected

See Also: [NSPopUpButtonCell-removeItemWithTitle:] -removeAllItems -removeItemAtIndex: -synchronizeTitleAndSelectedItem

*/ - (void) removeItemWithTitle: (NSString*)title { [_cell removeItemWithTitle: title]; [self synchronizeTitleAndSelectedItem]; } /**

Removes the item at the specified index index from the item list and synchronizes the NSPopUpButton's title with the title of the selected

See Also: [NSPopUpButtonCell-removeItemAtIndex:] -removeAllItems -removeItemWithTitle: -synchronizeTitleAndSelectedItem

*/ - (void) removeItemAtIndex: (NSInteger)index { [_cell removeItemAtIndex: index]; [self synchronizeTitleAndSelectedItem]; } /**

Returns the selected item

See Also: [NSPopUpButtonCell-selectedItem]

*/ - (id ) selectedItem { return [_cell selectedItem]; } /**

Returns the title of the selected item

See Also: [NSPopUpButtonCell-titleOfSelectedItem]

*/ - (NSString*) titleOfSelectedItem { return [_cell titleOfSelectedItem]; } /**

Returns the index of the selected item

See Also: [NSPopUpButtonCell-indexOfSelectedItem]

*/ - (NSInteger) indexOfSelectedItem { return [_cell indexOfSelectedItem]; } /**

Returns the tag of the selected item

See Also: [NSPopUpButtonCell-indexOfSelectedItem]

*/ - (NSInteger) selectedTag { return [[_cell selectedItem] tag]; } - (void) selectItem: (id )anObject { [_cell selectItem: anObject]; [self synchronizeTitleAndSelectedItem]; } /**

Select the item at index index and synchronizes the NSPopUpButton's title with the title of the selected

See Also: [NSPopUpButtonCell-selectItemAtIndex:] -synchronizeTitleAndSelectedItem

*/ - (void) selectItemAtIndex: (NSInteger)index { [_cell selectItemAtIndex: index]; [self synchronizeTitleAndSelectedItem]; } /**

Select the item with title title and synchronizes the NSPopUpButton's title with the title of the selected

See Also: [NSPopUpButtonCell-selectItemWithTitle:] -synchronizeTitleAndSelectedItem

*/ - (void) selectItemWithTitle: (NSString*)title { [_cell selectItemWithTitle: title]; [self synchronizeTitleAndSelectedItem]; } - (BOOL) selectItemWithTag: (NSInteger)tag { NSInteger index = [self indexOfItemWithTag: tag]; if (index >= 0) { [self selectItemAtIndex: index]; return YES; } else { return NO; } } /**

Returns the number of items in the item list

See Also: [NSPopUpButtonCell-numberOfItems]

*/ - (NSInteger) numberOfItems { return [_cell numberOfItems]; } - (NSArray*) itemArray { return [_cell itemArray]; } /**

Returns the NSMenuItem at index index or nil if index is out of range

See Also: [NSPopUpButtonCell-itemAtIndex:]

*/ - (id ) itemAtIndex: (NSInteger)index { return [_cell itemAtIndex: index]; } /**

Returns the item's title at index index

*/ - (NSString*) itemTitleAtIndex: (NSInteger)index { return [_cell itemTitleAtIndex: index]; } /**

Returns an array containing the items's titles

*/ - (NSArray*) itemTitles { return [_cell itemTitles]; } /**

Returns the NSMenuItem with title as its title

*/ - (id ) itemWithTitle: (NSString*)title { return [_cell itemWithTitle: title]; } /**

Returns the last NSMenuItem of the list

*/ - (id ) lastItem { return [_cell lastItem]; } - (NSInteger) indexOfItem: (id )anObject { return [_cell indexOfItem: anObject]; } /**

Returns the index of the item with tag as its tag. Returns -1 if the cell is not found

See Also: [NSPopUpButtonCell-indexOfItemWithTag:] -indexOfItemWithTitle: -indexOfItemWithRepresentedObject:

*/ - (NSInteger) indexOfItemWithTag: (NSInteger)tag { return [_cell indexOfItemWithTag: tag]; } /**

Returns the index of the item with title as its title. Returns -1 if the cell is not found

See Also: [NSPopUpButtonCell-indexOfItemWithTitle:] -indexOfItemWithTag: -indexOfItemWithRepresentedObject:

*/ - (NSInteger) indexOfItemWithTitle: (NSString*)title { return [_cell indexOfItemWithTitle: title]; } - (NSInteger) indexOfItemWithRepresentedObject: (id)anObject { return [_cell indexOfItemWithRepresentedObject: anObject]; } - (NSInteger) indexOfItemWithTarget: (id)target andAction: (SEL)actionSelector { return [_cell indexOfItemWithTarget: target andAction: actionSelector]; } - (void) setPreferredEdge: (NSRectEdge)edge { [_cell setPreferredEdge: edge]; } - (NSRectEdge) preferredEdge { return [_cell preferredEdge]; } - (void) setTitle: (NSString*)aString { [_cell setTitle: aString]; [self synchronizeTitleAndSelectedItem]; } - (void) synchronizeTitleAndSelectedItem { [_cell synchronizeTitleAndSelectedItem]; [self setNeedsDisplay: YES]; } - (BOOL) resignFirstResponder { [_cell dismissPopUp]; return [super resignFirstResponder]; } - (BOOL) performKeyEquivalent: (NSEvent*)theEvent { NSMenu *m = [self menu]; NSMenuItem *oldSelectedItem = (NSMenuItem *)[_cell selectedItem]; if (m != nil) { if ([m performKeyEquivalent: theEvent]) { // pullsDown does not change selected item if ([_cell pullsDown]) { [self selectItem: oldSelectedItem]; } else { /* If the key equivalent was performed, redisplay ourselves * to account for potential changes in the selected item. */ [self setNeedsDisplay: YES]; } return YES; } } return NO; } - (void) mouseDown: (NSEvent*)theEvent { [_cell trackMouse: theEvent inRect: [self bounds] ofView: self untilMouseUp: YES]; } - (void) keyDown: (NSEvent*)theEvent { // FIXME: This method also handles the key events for the popup menu window, // as menu windows cannot become key window. if ([self isEnabled]) { NSString *characters = [theEvent characters]; unichar character = 0; if ([characters length] > 0) { character = [characters characterAtIndex: 0]; } switch (character) { case NSNewlineCharacter: case NSEnterCharacter: case NSCarriageReturnCharacter: /* Handle Enter and Return keys only when the menu is visible. The button's action to pop up the menu is initiated only by the Space key similar to other buttons. */ { NSMenuView *menuView = [[_cell menu] menuRepresentation]; if ([[menuView window] isVisible] == NO) break; } case ' ': { NSInteger selectedIndex; NSMenuView *menuView; // Beep, as on OS, and then return. if ([[_cell menu] numberOfItems] == 0) { NSBeep(); return; } menuView = [[_cell menu] menuRepresentation]; if ([[menuView window] isVisible] == NO) { // Attach the popUp [_cell attachPopUpWithFrame: _bounds inView: self]; } else { selectedIndex = [menuView highlightedItemIndex]; if (selectedIndex >= 0) { [[_cell menu] performActionForItemAtIndex: selectedIndex]; } } } return; case '\e': [_cell dismissPopUp]; return; case NSUpArrowFunctionKey: { NSMenuView *menuView; NSInteger selectedIndex, numberOfItems; menuView = [[_cell menu] menuRepresentation]; selectedIndex = [menuView highlightedItemIndex]; numberOfItems = [self numberOfItems]; switch (selectedIndex) { case -1: selectedIndex = numberOfItems - 1; break; case 0: return; default: selectedIndex--; break; } [menuView setHighlightedItemIndex: selectedIndex]; } return; case NSDownArrowFunctionKey: { NSMenuView *menuView; NSInteger selectedIndex, numberOfItems; menuView = [[_cell menu] menuRepresentation]; selectedIndex = [menuView highlightedItemIndex]; numberOfItems = [self numberOfItems]; if (selectedIndex < numberOfItems-1) [menuView setHighlightedItemIndex: selectedIndex + 1]; } return; } } [super keyDown: theEvent]; } - (void) setValue: (id)anObject forKey: (NSString*)aKey { if ([aKey isEqual: NSSelectedIndexBinding]) { [self selectItemAtIndex: [anObject intValue]]; } else if ([aKey isEqual: NSSelectedTagBinding]) { [self selectItemWithTag: [anObject integerValue]]; } else if ([aKey isEqual: NSSelectedObjectBinding]) { // FIXME: This looks wrong to me [self selectItemWithTag: [anObject intValue]]; } else if ([aKey isEqual: NSSelectedValueBinding]) { [self selectItemWithTitle: anObject]; } else if ([aKey isEqual: NSContentValuesBinding]) { [self removeAllItems]; [self addItemsWithTitles: (NSArray*)anObject]; } else { [super setValue: anObject forKey: aKey]; } } - (id) valueForKey: (NSString*)aKey { if ([aKey isEqual: NSSelectedIndexBinding]) { return [NSNumber numberWithInt: [self indexOfSelectedItem]]; } else if ([aKey isEqual: NSSelectedTagBinding]) { return [NSNumber numberWithInteger: [self selectedTag]]; } else if ([aKey isEqual: NSSelectedObjectBinding]) { // FIXME return [NSNumber numberWithInt: [self selectedTag]]; } else if ([aKey isEqual: NSSelectedValueBinding]) { return [self titleOfSelectedItem]; } else if ([aKey isEqual: NSContentValuesBinding]) { return [self itemTitles]; } else { return [super valueForKey: aKey]; } } @end