File: GSKeyBindingTable.m

package info (click to toggle)
gnustep-gui 0.25.0-4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 13,088 kB
  • ctags: 3,417
  • sloc: objc: 153,800; ansic: 18,239; cpp: 579; yacc: 462; makefile: 143; sh: 5
file content (275 lines) | stat: -rw-r--r-- 7,431 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/* GSKeyBindingTable.m                    -*-objc-*-

   Copyright (C) 2002-2011 Free Software Foundation, Inc.

   Author: Nicola Pero <n.pero@mi.flashnet.it>
   Date: February 2002

   This file is part of the GNUstep GUI Library.

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with this library; see the file COPYING.LIB.
   If not, see <http://www.gnu.org/licenses/> or write to the 
   Free Software Foundation, 51 Franklin Street, Fifth Floor, 
   Boston, MA 02110-1301, USA.
*/ 

#import "AppKit/NSEvent.h"
#import "AppKit/NSInputManager.h"
#import "GSKeyBindingAction.h"
#import "GSKeyBindingTable.h"

@implementation GSKeyBindingTable : NSObject

- (void) loadBindingsFromDictionary: (NSDictionary *)dict
{
  NSEnumerator *e;
  id key;
  
  e = [dict keyEnumerator];
  while ((key = [e nextObject]) != nil)
    {
      [self bindKey: key  toAction: [dict objectForKey: key]];
    }
}

- (void) bindKey: (id)key  toAction: (id)action
{
  unichar character;
  unsigned int modifiers;
  GSKeyBindingAction *a = nil;
  GSKeyBindingTable *t = nil;
  BOOL isTable = NO;
  int i;

  /* First, try to determine what exactly is key :-) ... it might
     either be a simple string, "Control-f", or an array,
     ("Control-x", "Control-s").  We implement the case of arrays in
     terms of the case of strings.  */
  if ([key isKindOfClass: [NSArray class]])
    {
      if ([(NSArray *)key count] == 0)
	{
	  /* Ignore them.  */
	  return;
	}
      else if ([(NSArray *)key count] == 1)
	{
	  key = [key objectAtIndex: 0];
	}
      else
	{
	  /* Ok - simply convert that into a temporary dictionary
	     representation and store that.  Eg, key ("Control-x",
	     "Control-s", "Control-k") action "moveUp:" gets converted
	     into: key "Control-x" action { "Control-s" = {
	     "Control-k" = "moveUp:"; }; }.  */

	  /* Now start from the end of the array, and start building
	     the temporary dictionary structure going backwards.  */
	  id value = action;
	  int j;

	  for (j = [key count] - 1; j > 0; j--)
	    {
	      NSMutableDictionary *tmp = [NSMutableDictionary dictionary];
	      [tmp setObject: value  forKey: [key objectAtIndex: j]];
	      value = tmp;
	    }
	  key = [key objectAtIndex: 0];
	  action = value;
	}
    }
  

  if (![key isKindOfClass: [NSString class]])
    {
      NSLog (@"GSKeyBindingTable - key %@ is not a NSString!", key);
      return;
    }
  
  if (![NSInputManager parseKey: (NSString *)key 
		       intoCharacter: &character
		       andModifiers: &modifiers])
    {
      NSLog (@"GSKeyBindingTable - Could not bind key %@", key);
      return;
    }

  /* If it is not a function key, we automatically ignore the Shift
   * modifier.  You shouldn't use it unless you are describing a modification
   * of a function key.  The NSInputManager will ignore Shift modifiers
   * as well for non-function keys.  */
  if (modifiers & NSFunctionKeyMask)
    {
      /* Ignore all other modifiers when storing the keystroke modifiers.  */
      modifiers = modifiers & (NSShiftKeyMask 
			       | NSAlternateKeyMask 
			       | NSControlKeyMask 
			       | NSNumericPadKeyMask);
    }
  else
    {
      modifiers = modifiers & (NSAlternateKeyMask 
			       | NSControlKeyMask 
			       | NSNumericPadKeyMask);
    }


  /* Now build the associated action/table.  */
  if ([action isKindOfClass: [NSString class]])
    {
      /* "" as action means disable the keybinding.  It can be used to
	 override a previous keybinding.  */
      if ([(NSString *)action isEqualToString: @""])
	{
	  a = nil;
	}
      else
	{
	  a = [[GSKeyBindingActionSelector alloc] 
		initWithSelectorName: (NSString *)action];
	  AUTORELEASE (a);
	}
    }
  else if ([action isKindOfClass: [NSArray class]])
    {
      a = [[GSKeyBindingActionSelectorArray alloc]
	    initWithSelectorNames: (NSArray *)action];
      AUTORELEASE (a);
    }
  else if ([action isKindOfClass: [NSDictionary class]])
    {
      /* Don't load the keybindings from action yet ... load them
	 later on when we know if we need to create a new
	 GSKeyBindingTable or if we need to merge them into an
	 existing one.  */
      isTable = YES;
    }
  else if ([action isKindOfClass: [GSKeyBindingAction class]])
    {
      a = action;
    }

  /* Ok - at this point, we have all the elements ready, we just need
     to insert into the table.  */
    
  /* Check if there are already some bindings for this keystroke.  */
  for (i = 0; i < _bindingsCount; i++)
    {
      if ((_bindings[i].character == character)  
	  &&  (_bindings[i].modifiers == modifiers))
	{
	  /* Replace/override the existing action with the new one if
	     it's an action, or load the bindings into a (new or
	     existing) table if it's a table.  */
	  if (isTable)
	    {
	      /* If there was already a table, add keybindings to that
		 table.  */
	      if (_bindings[i].table != nil)
		{
		  t = _bindings[i].table;
		}
	      else
		{
		  /* Else, create a new one.  */
		  t = [[GSKeyBindingTable alloc] init];
		  AUTORELEASE (t);
		}
	      [t loadBindingsFromDictionary: (NSDictionary *)action];
	    }

	  ASSIGN (_bindings[i].action, a);
	  ASSIGN (_bindings[i].table, t);
	  return;
	}
    }

  /* Ok - new keystroke.  Create the table if needed.  */
  if (isTable)
    {
      t = [[GSKeyBindingTable alloc] init];
      AUTORELEASE (t);
      [t loadBindingsFromDictionary: (NSDictionary *)action];
    }

  /* Allocate memory for the new binding.  */
  if (_bindingsCount == 0)
    {
      _bindingsCount = 1;
      _bindings = malloc (sizeof (struct _GSKeyBinding));
    }
  else
    {
      _bindingsCount++;
      _bindings = realloc (_bindings, sizeof (struct _GSKeyBinding) 
				* _bindingsCount);
    }
  _bindings[_bindingsCount - 1].character = character;
  _bindings[_bindingsCount - 1].modifiers = modifiers;

  /* Don't use ASSIGN here because that uses the previous value of
     _bindings[_bindingsCount - 1] ... which is undefined.  */
  _bindings[_bindingsCount - 1].action = a;
  RETAIN (a);
  _bindings[_bindingsCount - 1].table = t;
  RETAIN (t);
}

- (BOOL) lookupKeyStroke: (unichar)character
	       modifiers: (int)flags
       returningActionIn: (GSKeyBindingAction **)action
		 tableIn: (GSKeyBindingTable **)table
{
  int i;
  
  for (i = 0; i < _bindingsCount; i++)
    {
      if (_bindings[i].character == character)
	{
	  if (_bindings[i].modifiers == flags)
	    {
	      if (_bindings[i].action == nil  &&  _bindings[i].table == nil)
		{
		  /* Found the keybinding, but it is disabled!  */
		  return NO;
		}
	      else
		{
		  *action = _bindings[i].action;
		  *table = _bindings[i].table;
		  return YES;
		}
	    }
	}
    }
  return NO;
}

- (void) dealloc
{
  int i;

  for (i = 0; i < _bindingsCount; i++)
    {
      RELEASE (_bindings[i].action);
      RELEASE (_bindings[i].table);
    }
  free (_bindings);
  [super dealloc];
}

@end