API  0.9.10
CPResponder.j
Go to the documentation of this file.
1 /*
2  * CPResponder.j
3  * AppKit
4  *
5  * Created by Francisco Tolmasky.
6  * Copyright 2008, 280 North, Inc.
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
23 
24 
25 
38 
44 @implementation CPResponder : CPObject
45 {
46  CPMenu _menu;
47  CPResponder _nextResponder;
48 }
49 
50 // Changing the first responder
55 {
56  return NO;
57 }
58 
65 {
66  return YES;
67 }
68 
74 {
75  return YES;
76 }
77 
78 // Setting the next responder
83 - (void)setNextResponder:(CPResponder)aResponder
84 {
85  _nextResponder = aResponder;
86 }
87 
92 {
93  return _nextResponder;
94 }
95 
100 - (void)interpretKeyEvents:(CPArray)events
101 {
102  var index = 0,
103  count = [events count];
104 
105  for (; index < count; ++index)
106  {
107  var event = events[index],
108  modifierFlags = [event modifierFlags],
109  character = [event charactersIgnoringModifiers],
110  selectorNames = [CPKeyBinding selectorsForKey:character modifierFlags:modifierFlags];
111 
112  if (selectorNames)
113  {
114  for (var s = 0, scount = selectorNames.length; s < scount; s++)
115  {
116  var selector = selectorNames[s];
117  if (!selector)
118  continue;
119 
120  [self doCommandBySelector:CPSelectorFromString(selector)];
121  }
122  }
123  else if (!(modifierFlags & (CPCommandKeyMask | CPControlKeyMask)) && [self respondsToSelector:@selector(insertText:)])
124  [self insertText:[event characters]];
125  }
126 }
127 
132 - (void)mouseDown:(CPEvent)anEvent
133 {
134  [_nextResponder performSelector:_cmd withObject:anEvent];
135 }
136 
141 - (void)rightMouseDown:(CPEvent)anEvent
142 {
143  [_nextResponder performSelector:_cmd withObject:anEvent];
144 }
145 
151 - (void)mouseDragged:(CPEvent)anEvent
152 {
153  [_nextResponder performSelector:_cmd withObject:anEvent];
154 }
155 
160 - (void)mouseUp:(CPEvent)anEvent
161 {
162  [_nextResponder performSelector:_cmd withObject:anEvent];
163 }
164 
169 - (void)rightMouseUp:(CPEvent)anEvent
170 {
171  [_nextResponder performSelector:_cmd withObject:anEvent];
172 }
173 
178 - (void)mouseMoved:(CPEvent)anEvent
179 {
180  [_nextResponder performSelector:_cmd withObject:anEvent];
181 }
182 
183 - (void)mouseEntered:(CPEvent)anEvent
184 {
185  [_nextResponder performSelector:_cmd withObject:anEvent];
186 }
187 
192 - (void)mouseExited:(CPEvent)anEvent
193 {
194  [_nextResponder performSelector:_cmd withObject:anEvent];
195 }
196 
201 - (void)cursorUpdate:(CPEvent)anEvent
202 {
203  if (_nextResponder)
204  [_nextResponder performSelector:_cmd withObject:anEvent];
205  else
207 }
208 
213 - (void)scrollWheel:(CPEvent)anEvent
214 {
215  [_nextResponder performSelector:_cmd withObject:anEvent];
216 }
217 
222 - (void)keyDown:(CPEvent)anEvent
223 {
224  [_nextResponder performSelector:_cmd withObject:anEvent];
225 }
226 
231 - (void)keyUp:(CPEvent)anEvent
232 {
233  [_nextResponder performSelector:_cmd withObject:anEvent];
234 }
235 
240 - (void)flagsChanged:(CPEvent)anEvent
241 {
242  [_nextResponder performSelector:_cmd withObject:anEvent];
243 }
244 
258 - (BOOL)performKeyEquivalent:(CPEvent)anEvent
259 {
260  return NO;
261 }
262 
263 // Action Methods
268 - (void)insertLineBreak:(id)aSender
269 {
270  [self insertNewline:aSender];
271 }
272 
277 - (void)insertNewline:(id)aSender
278 {
279  [[self nextResponder] insertNewline:aSender];
280 }
281 
282 - (void)insertTab:(id)sender
283 {
284 }
285 
286 - (void)insertBackTab:(id)sender
287 {
288 }
289 
294 - (void)insertText:(CPString)aString
295 {
296 }
297 
298 // Dispatch methods
304 - (void)doCommandBySelector:(SEL)aSelector
305 {
306  if ([self respondsToSelector:aSelector])
307  [self performSelector:aSelector];
308  else
309  [_nextResponder doCommandBySelector:aSelector];
310 }
311 
319 - (BOOL)tryToPerform:(SEL)aSelector with:(id)anObject
320 {
321  if ([self respondsToSelector:aSelector])
322  {
323  [self performSelector:aSelector withObject:anObject];
324 
325  return YES;
326  }
327 
328  return [_nextResponder tryToPerform:aSelector with:anObject];
329 }
330 
331 // Managing a Responder's menu
332 
333 - (void)setMenu:(CPMenu)aMenu
334 {
335  _menu = aMenu;
336 }
337 
339 {
340  return _menu;
341 }
342 
343 // Getting the Undo Manager
348 {
349  return [_nextResponder performSelector:_cmd];
350 }
351 
352 // Terminating the responder chain
357 - (void)noResponderFor:(SEL)anEventSelector
358 {
359 }
360 
361 @end
362 
363 var CPResponderNextResponderKey = @"CPResponderNextResponderKey",
364  CPResponderMenuKey = @"CPResponderMenuKey";
365 
367 
373 - (id)initWithCoder:(CPCoder)aCoder
374 {
375  self = [super init];
376 
377  if (self)
378  {
379  [self setNextResponder:[aCoder decodeObjectForKey:CPResponderNextResponderKey]];
380  [self setMenu:[aCoder decodeObjectForKey:CPResponderMenuKey]];
381  }
382 
383  return self;
384 }
385 
390 - (void)encodeWithCoder:(CPCoder)aCoder
391 {
392  // This will come out nil on the other side with decodeObjectForKey:
393  if (_nextResponder !== nil)
394  [aCoder encodeConditionalObject:_nextResponder forKey:CPResponderNextResponderKey];
395 
396  [aCoder encodeObject:_menu forKey:CPResponderMenuKey];
397 }
398 
399 @end
Definition: CPMenu.h:2
CPPageDownKeyCode
Definition: CPResponder.j:32
CPCursor arrowCursor()
Definition: CPCursor.j:192
CPEscapeKeyCode
Definition: CPResponder.j:29
var CPResponderMenuKey
Definition: CPResponder.j:364
CPArray selectorsForKey:modifierFlags:(CPString aKey, [modifierFlags] unsigned aFlag)
Definition: CPKeyBinding.j:180
void setMenu:(CPMenu aMenu)
Definition: CPResponder.j:333
id performSelector:(SEL aSelector)
Definition: CPObject.j:374
CPMenu menu()
Definition: CPResponder.j:338
CPReturnKeyCode
Definition: CPResponder.j:28
var CPResponderNextResponderKey
Definition: CPResponder.j:363
An immutable string (collection of characters).
Definition: CPString.h:2
CPCommandKeyMask
void insertNewline:(id aSender)
Definition: CPResponder.j:277
CPPageUpKeyCode
Definition: CPResponder.j:31
CPResponder nextResponder()
Definition: CPResponder.j:91
void doCommandBySelector:(SEL aSelector)
Definition: CPResponder.j:304
CPLeftArrowKeyCode
Definition: CPResponder.j:33
CPRightArrowKeyCode
Definition: CPResponder.j:35
CPUpArrowKeyCode
Definition: CPResponder.j:34
CPDownArrowKeyCode
Definition: CPResponder.j:36
CPSpaceKeyCode
Definition: CPResponder.j:30
CPDeleteKeyCode
Definition: CPResponder.j:26
BOOL acceptsFirstResponder()
Definition: CPResponder.j:54
A general mechanism for user action "undo".
Definition: CPUndoManager.h:2
Defines methods for use when archiving & restoring (enc/decoding).
Definition: CPCoder.h:2
BOOL resignFirstResponder()
Definition: CPResponder.j:73
id init()
Definition: CPObject.j:145
CPDeleteForwardKeyCode
Definition: CPResponder.j:37
id performSelector:withObject:(SEL aSelector, [withObject] id anObject)
Definition: CPObject.j:385
void setNextResponder:(CPResponder aResponder)
Definition: CPResponder.j:83
CPControlKeyMask
void set()
Definition: CPCursor.j:122
Definition: CPEvent.h:2
CPTabKeyCode
Definition: CPResponder.j:27
BOOL becomeFirstResponder()
Definition: CPResponder.j:64
void insertText:(CPString aString)
Definition: CPResponder.j:294
CPUndoManager undoManager()
Definition: CPResponder.j:347