Oolite 1.91.0.7646-241128-10e222e
Loading...
Searching...
No Matches
OOTextFieldHistoryManager.m
Go to the documentation of this file.
1/*
2
3 OOTextFieldHistoryManager.m
4
5
6 Oolite Debug Bundle
7
8 Copyright (C) 2007 Jens Ayton
9
10 Permission is hereby granted, free of charge, to any person obtaining a copy
11 of this software and associated documentation files (the "Software"), to deal
12 in the Software without restriction, including without limitation the rights
13 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 copies of the Software, and to permit persons to whom the Software is
15 furnished to do so, subject to the following conditions:
16
17 The above copyright notice and this permission notice shall be included in all
18 copies or substantial portions of the Software.
19
20 THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 SOFTWARE.
27
28 */
29
31#import "OOLogging.h"
32#import "OODebugUtilities.h"
33
34
35@interface OOTextFieldHistoryManager (Private)
36
37- (void)checkInvariant;
38- (void)maintainInvariant;
39- (void)moveHistoryCursorTo:(NSUInteger)newCursor fieldEditor:(NSTextView *)fieldEditor;
40- (void)moveHistoryCursorBy:(NSInteger)offset fieldEditor:(NSTextView *)fieldEditor;
41
42@end
43
44
45@implementation OOTextFieldHistoryManager
46
47
48- (BOOL) control:(NSControl *)control textView:(NSTextView *)textView doCommandBySelector:(SEL)commandSelector
49{
50 if (control == textField)
51 {
52 if (commandSelector == @selector(moveToBeginningOfParagraph:) ||
53 commandSelector == @selector(scrollPageUp:))
54 {
55 // Option-up arrow or page up. (For just up arrow, use moveUp:.)
56 [self moveHistoryCursorBy:1 fieldEditor:textView];
57 return YES;
58 }
59 else if (commandSelector == @selector(moveToEndOfParagraph:) ||
60 commandSelector == @selector(scrollPageDown:))
61 {
62 // Option-down arrow or page down. (For just down arrow, use moveDown:.)
63 [self moveHistoryCursorBy:-1 fieldEditor:textView];
64 return YES;
65 }
66 }
67 return NO;
68}
69
70
71- (id) init
72{
73 self = [super init];
74 if (self != nil)
75 {
76 _historyMaxSize = kDefaultHistorySize;
77 _history = [[NSMutableArray alloc] initWithCapacity:kDefaultHistorySize];
78 [self checkInvariant];
79 }
80 return self;
81}
82
83
84- (void) dealloc
85{
86 [_history release];
87
88 [super dealloc];
89}
90
91
92- (NSArray *) history
93{
94 return [[_history copy] autorelease];
95}
96
97
98- (void) setHistory:(NSArray *)history
99{
100 if (history == nil) [_history removeAllObjects];
101 else
102 {
103 _history = [history mutableCopy];
104 _historyCursor = 0;
105 [self maintainInvariant];
106 }
107 [self checkInvariant];
108}
109
110
111- (void) addToHistory:(NSString *)string
112{
113 [self checkInvariant];
114
115 if (_historyCurrSize == 0 || ![string isEqual:[_history objectAtIndex:_historyCurrSize - 1]])
116 {
117 [_history addObject:[[string copy] autorelease]];
118 }
119 _historyCursor = 0;
120 [_latest release];
121 _latest = nil;
122
123 [self maintainInvariant];
124}
125
126
127- (NSUInteger) historySize
128{
129 return _historyMaxSize;
130}
131
132
133- (void) setHistorySize:(NSUInteger)size
134{
135 _historyMaxSize = size;
136 [self maintainInvariant];
137}
138
139@end
140
141
142@implementation OOTextFieldHistoryManager (Private)
143
144- (void) checkInvariant
145{
146 NSAssert(_history != nil && // History buffer must exist
147 _historyCurrSize == [_history count] && // Size must be correct
148 ((_historyCurrSize <= _historyMaxSize) || (_historyMaxSize == 0)) && // Size must be in bounds
149 _historyCursor <= _historyCurrSize + 1, // Cursor must be in bounds
150 @"Invalid history buffer state in OOTextFieldHistoryManager.");
151}
152
153
154- (void) maintainInvariant
155{
156 _historyCurrSize = [_history count];
157
158 if (_historyMaxSize && (_historyMaxSize < _historyCurrSize))
159 {
160 [_history removeObjectsInRange:NSMakeRange(0, _historyCurrSize - _historyMaxSize)];
161 _historyCurrSize = _historyMaxSize;
162 }
163
164 [self checkInvariant];
165}
166
167
168- (void) moveHistoryCursorTo:(NSUInteger)newCursor fieldEditor:(NSTextView *)fieldEditor
169{
170 NSString *value = nil;
171 NSTextStorage *textStorage = nil;
172
173 if (_historyCurrSize < newCursor)
174 {
175 NSBeep();
176 return;
177 }
178
179 [self checkInvariant];
180 textStorage = [fieldEditor textStorage];
181
182 if (newCursor > 0)
183 {
184 NSUInteger index = _historyCurrSize - newCursor;
185 value = [_history objectAtIndex:index];
186 if (_historyCursor == 0) _latest = [[textStorage string] copy];
187 }
188 else
189 {
190 value = [_latest autorelease];
191 _latest = nil;
192 }
193
194 _historyCursor = newCursor;
195
196 [textStorage setString:value];
197 [textField selectText:self];
198
199 [self checkInvariant];
200}
201
202
203- (void) moveHistoryCursorBy:(NSInteger)offset fieldEditor:(NSTextView *)fieldEditor
204{
205 // Range check
206 if (((offset < 0) && (_historyCursor < (NSUInteger)-offset)) // Destination < 0
207 || (_historyCurrSize < (offset + _historyCursor))) // Destination > _historyCurrSize
208 {
209 NSBeep();
210 return;
211 }
212 [self moveHistoryCursorTo:_historyCursor + offset fieldEditor:fieldEditor];
213}
214
215@end
unsigned count
return nil
void moveHistoryCursorBy:fieldEditor:(NSInteger offset,[fieldEditor] NSTextView *fieldEditor)
voidpf void uLong size
Definition ioapi.h:134
voidpf uLong offset
Definition ioapi.h:140