source: trunk/Cocoa/Pester/Source/NJRHistoryTrackingComboBox.m @ 39

Last change on this file since 39 was 39, checked in by Nicholas Riley, 20 years ago

Lots of wonderful changes for which I composed a detailed svn commit
message, which it proceeded to mangle yet AGAIN on failed commit.

File size: 2.2 KB
Line 
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    [self removeItemWithObjectValue: newValue];
63    [self insertItemWithObjectValue: newValue atIndex: 0];
64    if (oldIndex == NSNotFound) {
65        int numItems = [self numberOfItems];
66        while (numItems-- > NJRHistoryTrackingComboBoxMaxItems) {
67            [self removeItemAtIndex: numItems - 1];
68        }
69    }
70    [self _writeHistory];
71    return YES;
72}
73
74@end
Note: See TracBrowser for help on using the repository browser.