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 100
|
---|
12 |
|
---|
13 | @implementation NJRHistoryTrackingComboBox
|
---|
14 |
|
---|
15 | - (NSString *)_defaultKey;
|
---|
16 | {
|
---|
17 | NSAssert([self tag] != 0, NSLocalizedString(@"Can't track history for combo box with tag 0: please set a tag", "Assertion for history tracking combo box if tag is 0"));
|
---|
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 | }
|
---|
30 |
|
---|
31 | - (void)_writeHistory;
|
---|
32 | {
|
---|
33 | NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
|
---|
34 | [defaults setObject: [self objectValues] forKey: [self _defaultKey]];
|
---|
35 | [defaults synchronize];
|
---|
36 | }
|
---|
37 |
|
---|
38 | - (void)_clearEntry;
|
---|
39 | {
|
---|
40 | [self setStringValue: @""];
|
---|
41 | // force bindings to update
|
---|
42 | [self textDidChange: [NSNotification notificationWithName: NSControlTextDidChangeNotification
|
---|
43 | object: [[self window] fieldEditor: NO forObject: self]]];
|
---|
44 | }
|
---|
45 |
|
---|
46 | - (IBAction)removeEntry:(id)sender;
|
---|
47 | {
|
---|
48 | int idx = [self indexOfSelectedItem];
|
---|
49 | if (idx == -1) {
|
---|
50 | [self selectItemWithObjectValue: [self stringValue]];
|
---|
51 | idx = [self indexOfSelectedItem];
|
---|
52 | }
|
---|
53 | if (idx != -1) [self removeItemAtIndex: idx];
|
---|
54 | [self _clearEntry];
|
---|
55 | [self _writeHistory];
|
---|
56 | }
|
---|
57 |
|
---|
58 | - (IBAction)clearAllEntries:(id)sender;
|
---|
59 | {
|
---|
60 | [self removeAllItems];
|
---|
61 | [self setStringValue: @""];
|
---|
62 | [self _clearEntry];
|
---|
63 | [self _writeHistory];
|
---|
64 | }
|
---|
65 |
|
---|
66 | - (BOOL)textShouldEndEditing:(NSText *)textObject;
|
---|
67 | {
|
---|
68 | NSString *newValue = [self stringValue];
|
---|
69 | int oldIndex = [self indexOfItemWithObjectValue: newValue];
|
---|
70 | if ([newValue length] == 0) return YES; // donÕt save empty entries
|
---|
71 | [self removeItemWithObjectValue: newValue];
|
---|
72 | [self insertItemWithObjectValue: newValue atIndex: 0];
|
---|
73 | if (oldIndex == NSNotFound) {
|
---|
74 | int numItems = [self numberOfItems];
|
---|
75 | while (numItems-- > NJRHistoryTrackingComboBoxMaxItems) {
|
---|
76 | [self removeItemAtIndex: numItems - 1];
|
---|
77 | }
|
---|
78 | }
|
---|
79 | [self _writeHistory];
|
---|
80 | return YES;
|
---|
81 | }
|
---|
82 |
|
---|
83 | @end |
---|