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 | [[NSNotificationCenter defaultCenter] addObserver: self
|
---|
24 | selector: @selector(textDidEndEditing:)
|
---|
25 | name: NSTextDidEndEditingNotification
|
---|
26 | object: self];
|
---|
27 | [self removeAllItems];
|
---|
28 | [self addItemsWithObjectValues: [[NSUserDefaults standardUserDefaults] stringArrayForKey: [self _defaultKey]]];
|
---|
29 | [self setItemHeight: [self itemHeight] + 2];
|
---|
30 | }
|
---|
31 |
|
---|
32 | - (void)_writeHistory;
|
---|
33 | {
|
---|
34 | NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
|
---|
35 | [defaults setObject: [self objectValues] forKey: [self _defaultKey]];
|
---|
36 | [defaults synchronize];
|
---|
37 | }
|
---|
38 |
|
---|
39 | - (IBAction)removeEntry:(id)sender;
|
---|
40 | {
|
---|
41 | int idx = [self indexOfSelectedItem];
|
---|
42 | if (idx == -1) {
|
---|
43 | [self selectItemWithObjectValue: [self stringValue]];
|
---|
44 | idx = [self indexOfSelectedItem];
|
---|
45 | }
|
---|
46 | if (idx != -1) [self removeItemAtIndex: idx];
|
---|
47 | [self setStringValue: @""];
|
---|
48 | [self _writeHistory];
|
---|
49 | }
|
---|
50 |
|
---|
51 | - (IBAction)clearAllEntries:(id)sender;
|
---|
52 | {
|
---|
53 | [self removeAllItems];
|
---|
54 | [self setStringValue: @""];
|
---|
55 | [self _writeHistory];
|
---|
56 | }
|
---|
57 |
|
---|
58 | - (BOOL)textShouldEndEditing:(NSText *)textObject;
|
---|
59 | {
|
---|
60 | NSString *newValue = [self stringValue];
|
---|
61 | int oldIndex = [self indexOfItemWithObjectValue: newValue];
|
---|
62 | if ([newValue length] == 0) return YES; // donÕt save empty entries
|
---|
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 |
---|