/**
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]
*/ - (idReturns 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: (idSelect 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:]
*/ - (idReturns 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
*/ - (idReturns the last NSMenuItem of the list
*/ - (idReturns 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