[5] | 1 | //
|
---|
| 2 | // NJRHistoryTrackingComboBox.m
|
---|
| 3 | // DockCam
|
---|
| 4 | //
|
---|
| 5 | // Created by Nicholas Riley on Fri Jun 28 2002.
|
---|
| 6 | // Copyright (c) 2002 Nicholas Riley. All rights reserved.
|
---|
| 7 | //
|
---|
| 8 |
|
---|
| 9 | #import "NJRHistoryTrackingComboBox.h"
|
---|
| 10 |
|
---|
| 11 | #define NJRHistoryTrackingComboBoxMaxItems 10
|
---|
| 12 |
|
---|
| 13 | @implementation NJRHistoryTrackingComboBox
|
---|
| 14 |
|
---|
| 15 | - (NSString *)_defaultKey;
|
---|
| 16 | {
|
---|
| 17 | NSAssert([self tag] != 0, @"CanÕt track history for combo box with tag 0: please set a tag");
|
---|
| 18 | return [NSString stringWithFormat: @"NJRHistoryTrackingComboBox tag %d", [self tag]];
|
---|
| 19 | }
|
---|
| 20 |
|
---|
| 21 | - (void)awakeFromNib;
|
---|
| 22 | {
|
---|
| 23 | NSAssert([self tag] != 0, @"CanÕt track history for combo box with tag 0: please set a tag");
|
---|
| 24 | [[NSNotificationCenter defaultCenter] addObserver: self
|
---|
| 25 | selector: @selector(textDidEndEditing:)
|
---|
| 26 | name: NSTextDidEndEditingNotification
|
---|
| 27 | object: self];
|
---|
| 28 | [self removeAllItems];
|
---|
| 29 | [self addItemsWithObjectValues: [[NSUserDefaults standardUserDefaults] stringArrayForKey: [self _defaultKey]]];
|
---|
| 30 | [self setItemHeight: [self itemHeight] + 2];
|
---|
| 31 | }
|
---|
| 32 |
|
---|
| 33 | - (void)_writeHistory;
|
---|
| 34 | {
|
---|
| 35 | NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
|
---|
| 36 | [defaults setObject: [self objectValues] forKey: [self _defaultKey]];
|
---|
| 37 | [defaults synchronize];
|
---|
| 38 | }
|
---|
| 39 |
|
---|
| 40 | - (IBAction)removeEntry:(id)sender;
|
---|
| 41 | {
|
---|
| 42 | int idx = [self indexOfSelectedItem];
|
---|
| 43 | if (idx == -1) {
|
---|
| 44 | [self selectItemWithObjectValue: [self stringValue]];
|
---|
| 45 | idx = [self indexOfSelectedItem];
|
---|
| 46 | }
|
---|
| 47 | if (idx != -1) [self removeItemAtIndex: idx];
|
---|
| 48 | [self setStringValue: @""];
|
---|
| 49 | [self _writeHistory];
|
---|
| 50 | }
|
---|
| 51 |
|
---|
| 52 | - (IBAction)clearAllEntries:(id)sender;
|
---|
| 53 | {
|
---|
| 54 | [self removeAllItems];
|
---|
| 55 | [self setStringValue: @""];
|
---|
| 56 | [self _writeHistory];
|
---|
| 57 | }
|
---|
| 58 |
|
---|
| 59 | - (BOOL)textShouldEndEditing:(NSText *)textObject;
|
---|
| 60 | {
|
---|
| 61 | NSString *newValue = [self stringValue];
|
---|
| 62 | int oldIndex = [self indexOfItemWithObjectValue: newValue];
|
---|
| 63 | [self removeItemWithObjectValue: newValue];
|
---|
| 64 | [self insertItemWithObjectValue: newValue atIndex: 0];
|
---|
| 65 | if (oldIndex == NSNotFound) {
|
---|
| 66 | int numItems = [self numberOfItems];
|
---|
| 67 | while (numItems-- > NJRHistoryTrackingComboBoxMaxItems) {
|
---|
| 68 | [self removeItemAtIndex: numItems - 1];
|
---|
| 69 | }
|
---|
| 70 | }
|
---|
| 71 | [self _writeHistory];
|
---|
| 72 | return YES;
|
---|
| 73 | }
|
---|
| 74 |
|
---|
| 75 | @end |
---|