2.5
[blender.git] / source / blender / editors / interface / keyval.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21  * All rights reserved.
22  *
23  * The Original Code is: all of this file.
24  *
25  * Contributor(s): none yet.
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  */
29
30 #include "stdio.h"
31 #include "ctype.h"
32 #include "string.h"
33
34 #include "BKE_global.h"
35 #include "BLI_blenlib.h"
36 #include "BLI_arithb.h"
37
38 #include "WM_types.h"
39
40 char *key_event_to_string(unsigned short event)
41 {
42
43         switch(event) {
44         case AKEY:
45                 return "A";
46                 break;
47         case BKEY:
48                 return "B";
49                 break;
50         case CKEY:
51                 return "C";
52                 break;
53         case DKEY:
54                 return "D";
55                 break;
56         case EKEY:
57                 return "E";
58                 break;
59         case FKEY:
60                 return "F";
61                 break;
62         case GKEY:
63                 return "G";
64                 break;
65         case HKEY:
66                 return "H";
67                 break;
68         case IKEY:
69                 return "I";
70                 break;
71         case JKEY:
72                 return "J";
73                 break;
74         case KKEY:
75                 return "K";
76                 break;
77         case LKEY:
78                 return "L";
79                 break;
80         case MKEY:
81                 return "M";
82                 break;
83         case NKEY:
84                 return "N";
85                 break;
86         case OKEY:
87                 return "O";
88                 break;
89         case PKEY:
90                 return "P";
91                 break;
92         case QKEY:
93                 return "Q";
94                 break;
95         case RKEY:
96                 return "R";
97                 break;
98         case SKEY:
99                 return "S";
100                 break;
101         case TKEY:
102                 return "T";
103                 break;
104         case UKEY:
105                 return "U";
106                 break;
107         case VKEY:
108                 return "V";
109                 break;
110         case WKEY:
111                 return "W";
112                 break;
113         case XKEY:
114                 return "X";
115                 break;
116         case YKEY:
117                 return "Y";
118                 break;
119         case ZKEY:
120                 return "Z";
121                 break;
122
123         case ZEROKEY:
124                 return "Zero";
125                 break;
126         case ONEKEY:
127                 return "One";
128                 break;
129         case TWOKEY:
130                 return "Two";
131                 break;
132         case THREEKEY:
133                 return "Three";
134                 break;
135         case FOURKEY:
136                 return "Four";
137                 break;
138         case FIVEKEY:
139                 return "Five";
140                 break;
141         case SIXKEY:
142                 return "Six";
143                 break;
144         case SEVENKEY:
145                 return "Seven";
146                 break;
147         case EIGHTKEY:
148                 return "Eight";
149                 break;
150         case NINEKEY:
151                 return "Nine";
152                 break;
153
154         case LEFTCTRLKEY:
155                 return "Leftctrl";
156                 break;
157         case LEFTALTKEY:
158                 return "Leftalt";
159                 break;
160         case    RIGHTALTKEY:
161                 return "Rightalt";
162                 break;
163         case    RIGHTCTRLKEY:
164                 return "Rightctrl";
165                 break;
166         case RIGHTSHIFTKEY:
167                 return "Rightshift";
168                 break;
169         case LEFTSHIFTKEY:
170                 return "Leftshift";
171                 break;
172
173         case ESCKEY:
174                 return "Esc";
175                 break;
176         case TABKEY:
177                 return "Tab";
178                 break;
179         case RETKEY:
180                 return "Ret";
181                 break;
182         case SPACEKEY:
183                 return "Space";
184                 break;
185         case LINEFEEDKEY:
186                 return "Linefeed";
187                 break;
188         case BACKSPACEKEY:
189                 return "Backspace";
190                 break;
191         case DELKEY:
192                 return "Del";
193                 break;
194         case SEMICOLONKEY:
195                 return "Semicolon";
196                 break;
197         case PERIODKEY:
198                 return "Period";
199                 break;
200         case COMMAKEY:
201                 return "Comma";
202                 break;
203         case QUOTEKEY:
204                 return "Quote";
205                 break;
206         case ACCENTGRAVEKEY:
207                 return "Accentgrave";
208                 break;
209         case MINUSKEY:
210                 return "Minus";
211                 break;
212         case SLASHKEY:
213                 return "Slash";
214                 break;
215         case BACKSLASHKEY:
216                 return "Backslash";
217                 break;
218         case EQUALKEY:
219                 return "Equal";
220                 break;
221         case LEFTBRACKETKEY:
222                 return "Leftbracket";
223                 break;
224         case RIGHTBRACKETKEY:
225                 return "Rightbracket";
226                 break;
227
228         case LEFTARROWKEY:
229                 return "Leftarrow";
230                 break;
231         case DOWNARROWKEY:
232                 return "Downarrow";
233                 break;
234         case RIGHTARROWKEY:
235                 return "Rightarrow";
236                 break;
237         case UPARROWKEY:
238                 return "Uparrow";
239                 break;
240
241         case PAD2:
242                 return "Pad2";
243                 break;
244         case PAD4:
245                 return "Pad4";
246                 break;
247         case PAD6:
248                 return "Pad6";
249                 break;
250         case PAD8:
251                 return "Pad8";
252                 break;
253         case PAD1:
254                 return "Pad1";
255                 break;
256         case PAD3:
257                 return "Pad3";
258                 break;
259         case PAD5:
260                 return "Pad5";
261                 break;
262         case PAD7:
263                 return "Pad7";
264                 break;
265         case PAD9:
266                 return "Pad9";
267                 break;
268
269         case PADPERIOD:
270                 return "Padperiod";
271                 break;
272         case PADSLASHKEY:
273                 return "Padslash";
274                 break;
275         case PADASTERKEY:
276                 return "Padaster";
277                 break;
278
279         case PAD0:
280                 return "Pad0";
281                 break;
282         case PADMINUS:
283                 return "Padminus";
284                 break;
285         case PADENTER:
286                 return "Padenter";
287                 break;
288         case PADPLUSKEY:
289                 return "Padplus";
290                 break;
291
292         case    F1KEY:
293                 return "F1";
294                 break;
295         case    F2KEY:
296                 return "F2";
297                 break;
298         case    F3KEY:
299                 return "F3";
300                 break;
301         case    F4KEY:
302                 return "F4";
303                 break;
304         case    F5KEY:
305                 return "F5";
306                 break;
307         case    F6KEY:
308                 return "F6";
309                 break;
310         case    F7KEY:
311                 return "F7";
312                 break;
313         case    F8KEY:
314                 return "F8";
315                 break;
316         case    F9KEY:
317                 return "F9";
318                 break;
319         case    F10KEY:
320                 return "F10";
321                 break;
322         case    F11KEY:
323                 return "F11";
324                 break;
325         case    F12KEY:
326                 return "F12";
327                 break;
328
329         case    PAUSEKEY:
330                 return "Pause";
331                 break;
332         case    INSERTKEY:
333                 return "Insert";
334                 break;
335         case    HOMEKEY:
336                 return "Home";
337                 break;
338         case    PAGEUPKEY:
339                 return "Pageup";
340                 break;
341         case    PAGEDOWNKEY:
342                 return "Pagedown";
343                 break;
344         case    ENDKEY:
345                 return "End";
346                 break;
347         }
348         
349         return "";
350 }
351
352 /* 
353  * Decodes key combination strings [qual1+[qual2+[...]]]keyname
354  * The '+'s may be replaced by '-' or ' ' characters to support different
355  * formats. No additional whitespace is allowed. The keyname may be an internal
356  * name, like "RETKEY", or a more common name, like "Return". Decoding is case-
357  * insensitive.
358  *
359  * Example strings: "Ctrl+L", "ALT-ESC", "Shift A"
360  *
361  * Returns 1 if successful. 
362  */
363 int decode_key_string(char *str, unsigned short *key, unsigned short *qual)
364 {
365         int i, prev, len, invalid=0;
366
367         len= strlen(str);
368         *key= *qual= 0;
369
370         /* Convert to upper case */
371         for (i=0; i<len; i++) {
372                 str[i]= toupper(str[i]);
373         }
374
375         /* Handle modifiers */
376         for (prev=i=0; i<len; i++) {
377                 if (str[i]==' ' || str[i]=='+' || str[i]=='-') {
378 // XXX                  if (!strncmp(str+prev, "CTRL", i-prev)) *qual |= LR_CTRLKEY;
379 //                      else if (!strncmp(str+prev, "ALT", i-prev)) *qual |= LR_ALTKEY;
380 //                      else if (!strncmp(str+prev, "SHIFT", i-prev)) *qual |= LR_SHIFTKEY;
381 //                      else if (!strncmp(str+prev, "COMMAND", i-prev)) *qual |= LR_COMMANDKEY;
382                         prev=i+1;
383                 }
384         }
385
386         /* Compare last part against key names */
387         if ((len-prev==1) || ((len-prev==4) && !strncmp(str+prev, "KEY", 3))) {
388                 
389                 if (str[prev]>='A' && str[prev]<='Z') {
390                         *key= str[prev]-'A'+AKEY;
391                 } else if (str[prev]>='0' && str[prev]<='9') {
392                         *key= str[prev]-'0'+ZEROKEY;
393                 } else {
394                         invalid= 1;
395                 }
396         
397         } else if (!strncmp(str+prev, "ZEROKEY", len-prev) || !strncmp(str+prev, "ZERO", len-prev)) {
398                 *key= ZEROKEY;
399         } else if (!strncmp(str+prev, "ONEKEY", len-prev) || !strncmp(str+prev, "ONE", len-prev)) {
400                 *key= ONEKEY;
401         } else if (!strncmp(str+prev, "TWOKEY", len-prev) || !strncmp(str+prev, "TWO", len-prev)) {
402                 *key= TWOKEY;
403         } else if (!strncmp(str+prev, "THREEKEY", len-prev) || !strncmp(str+prev, "THREE", len-prev)) {
404                 *key= THREEKEY;
405         } else if (!strncmp(str+prev, "FOURKEY", len-prev) || !strncmp(str+prev, "FOUR", len-prev)) {
406                 *key= FOURKEY;
407         } else if (!strncmp(str+prev, "FIVEKEY", len-prev) || !strncmp(str+prev, "FIVE", len-prev)) {
408                 *key= FIVEKEY;
409         } else if (!strncmp(str+prev, "SIZEKEY", len-prev) || !strncmp(str+prev, "SIX", len-prev)) {
410                 *key= SIXKEY;
411         } else if (!strncmp(str+prev, "SEVENKEY", len-prev) || !strncmp(str+prev, "SEVEN", len-prev)) {
412                 *key= SEVENKEY;
413         } else if (!strncmp(str+prev, "EIGHTKEY", len-prev) || !strncmp(str+prev, "EIGHT", len-prev)) {
414                 *key= EIGHTKEY;
415         } else if (!strncmp(str+prev, "NINEKEY", len-prev) || !strncmp(str+prev, "NINE", len-prev)) {
416                 *key= NINEKEY;
417
418         } else if (!strncmp(str+prev, "ESCKEY", len-prev) || !strncmp(str+prev, "ESC", len-prev)) {
419                 *key= ESCKEY;
420         } else if (!strncmp(str+prev, "TABKEY", len-prev) || !strncmp(str+prev, "TAB", len-prev)) {
421                 *key= TABKEY;
422         } else if (!strncmp(str+prev, "RETKEY", len-prev) || !strncmp(str+prev, "RETURN", len-prev) || !strncmp(str+prev, "ENTER", len-prev)) {
423                 *key= RETKEY;
424         } else if (!strncmp(str+prev, "SPACEKEY", len-prev) || !strncmp(str+prev, "SPACE", len-prev)) {
425                 *key= SPACEKEY;
426         } else if (!strncmp(str+prev, "LINEFEEDKEY", len-prev) || !strncmp(str+prev, "LINEFEED", len-prev)) {
427                 *key= LINEFEEDKEY;
428         } else if (!strncmp(str+prev, "BACKSPACEKEY", len-prev) || !strncmp(str+prev, "BACKSPACE", len-prev)) {
429                 *key= BACKSPACEKEY;
430         } else if (!strncmp(str+prev, "DELKEY", len-prev) || !strncmp(str+prev, "DELETE", len-prev)) {
431                 *key= DELKEY;
432         
433         } else if (!strncmp(str+prev, "SEMICOLONKEY", len-prev) || !strncmp(str+prev, "SEMICOLON", len-prev)) {
434                 *key= SEMICOLONKEY;
435         } else if (!strncmp(str+prev, "PERIODKEY", len-prev) || !strncmp(str+prev, "PERIOD", len-prev)) {
436                 *key= PERIODKEY;
437         } else if (!strncmp(str+prev, "COMMAKEY", len-prev) || !strncmp(str+prev, "COMMA", len-prev)) {
438                 *key= COMMAKEY;
439         } else if (!strncmp(str+prev, "QUOTEKEY", len-prev) || !strncmp(str+prev, "QUOTE", len-prev)) {
440                 *key= QUOTEKEY;
441         } else if (!strncmp(str+prev, "ACCENTGRAVEKEY", len-prev) || !strncmp(str+prev, "ACCENTGRAVE", len-prev)) {
442                 *key= ACCENTGRAVEKEY;
443         } else if (!strncmp(str+prev, "MINUSKEY", len-prev) || !strncmp(str+prev, "MINUS", len-prev)) {
444                 *key= MINUSKEY;
445         } else if (!strncmp(str+prev, "SLASHKEY", len-prev) || !strncmp(str+prev, "SLASH", len-prev)) {
446                 *key= SLASHKEY;
447         } else if (!strncmp(str+prev, "BACKSLASHKEY", len-prev) || !strncmp(str+prev, "BACKSLASH", len-prev)) {
448                 *key= BACKSLASHKEY;
449         } else if (!strncmp(str+prev, "EQUALKEY", len-prev) || !strncmp(str+prev, "EQUAL", len-prev)) {
450                 *key= EQUALKEY;
451         } else if (!strncmp(str+prev, "LEFTBRACKETKEY", len-prev) || !strncmp(str+prev, "LEFTBRACKET", len-prev)) {
452                 *key= LEFTBRACKETKEY;
453         } else if (!strncmp(str+prev, "RIGHTBRACKETKEY", len-prev) || !strncmp(str+prev, "RIGHTBRACKET", len-prev)) {
454                 *key= RIGHTBRACKETKEY;
455         } else if (!strncmp(str+prev, "DELKEY", len-prev) || !strncmp(str+prev, "DELETE", len-prev)) {
456                 *key= DELKEY;
457         
458         } else if (!strncmp(str+prev, "LEFTARROWKEY", len-prev) || !strncmp(str+prev, "LEFTARROW", len-prev)) {
459                 *key= LEFTARROWKEY;
460         } else if (!strncmp(str+prev, "DOWNARROWKEY", len-prev) || !strncmp(str+prev, "DOWNARROW", len-prev)) {
461                 *key= DOWNARROWKEY;
462         } else if (!strncmp(str+prev, "RIGHTARROWKEY", len-prev) || !strncmp(str+prev, "RIGHTARROW", len-prev)) {
463                 *key= RIGHTARROWKEY;
464         } else if (!strncmp(str+prev, "UPARROWKEY", len-prev) || !strncmp(str+prev, "UPARROW", len-prev)) {
465                 *key= UPARROWKEY;
466
467         } else if (!strncmp(str+prev, "PAD", 3)) {
468                 
469                 if (len-prev<=4) {
470                 
471                         if (str[prev]>='0' && str[prev]<='9') {
472                                 *key= str[prev]-'0'+ZEROKEY;
473                         } else {
474                                 invalid= 1;
475                         }
476                 
477                 } else if (!strncmp(str+prev+3, "PERIODKEY", len-prev-3) || !strncmp(str+prev+3, "PERIOD", len-prev-3)) {
478                         *key= PADPERIOD;
479                 } else if (!strncmp(str+prev+3, "SLASHKEY", len-prev-3) || !strncmp(str+prev+3, "SLASH", len-prev-3)) {
480                         *key= PADSLASHKEY;
481                 } else if (!strncmp(str+prev+3, "ASTERKEY", len-prev-3) || !strncmp(str+prev+3, "ASTERISK", len-prev-3)) {
482                         *key= PADASTERKEY;
483                 } else if (!strncmp(str+prev+3, "MINUSKEY", len-prev-3) || !strncmp(str+prev+3, "MINUS", len-prev-3)) {
484                         *key= PADMINUS;
485                 } else if (!strncmp(str+prev+3, "ENTERKEY", len-prev-3) || !strncmp(str+prev+3, "ENTER", len-prev-3)) {
486                         *key= PADENTER;
487                 } else if (!strncmp(str+prev+3, "PLUSKEY", len-prev-3) || !strncmp(str+prev+3, "PLUS", len-prev-3)) {
488                         *key= PADPLUSKEY;
489                 } else {
490                         invalid= 1;
491                 }
492
493         } else if (!strncmp(str+prev, "F1KEY", len-prev) || !strncmp(str+prev, "F1", len-prev)) {
494                 *key= F1KEY;
495         } else if (!strncmp(str+prev, "F2KEY", len-prev) || !strncmp(str+prev, "F2", len-prev)) {
496                 *key= F2KEY;
497         } else if (!strncmp(str+prev, "F3KEY", len-prev) || !strncmp(str+prev, "F3", len-prev)) {
498                 *key= F3KEY;
499         } else if (!strncmp(str+prev, "F4KEY", len-prev) || !strncmp(str+prev, "F4", len-prev)) {
500                 *key= F4KEY;
501         } else if (!strncmp(str+prev, "F5KEY", len-prev) || !strncmp(str+prev, "F5", len-prev)) {
502                 *key= F5KEY;
503         } else if (!strncmp(str+prev, "F6KEY", len-prev) || !strncmp(str+prev, "F6", len-prev)) {
504                 *key= F6KEY;
505         } else if (!strncmp(str+prev, "F7KEY", len-prev) || !strncmp(str+prev, "F7", len-prev)) {
506                 *key= F7KEY;
507         } else if (!strncmp(str+prev, "F8KEY", len-prev) || !strncmp(str+prev, "F8", len-prev)) {
508                 *key= F8KEY;
509         } else if (!strncmp(str+prev, "F9KEY", len-prev) || !strncmp(str+prev, "F9", len-prev)) {
510                 *key= F9KEY;
511         } else if (!strncmp(str+prev, "F10KEY", len-prev) || !strncmp(str+prev, "F10", len-prev)) {
512                 *key= F10KEY;
513         } else if (!strncmp(str+prev, "F11KEY", len-prev) || !strncmp(str+prev, "F11", len-prev)) {
514                 *key= F11KEY;
515         } else if (!strncmp(str+prev, "F12KEY", len-prev) || !strncmp(str+prev, "F12", len-prev)) {
516                 *key= F12KEY;
517
518         } else if (!strncmp(str+prev, "PAUSEKEY", len-prev) || !strncmp(str+prev, "PAUSE", len-prev)) {
519                 *key= PAUSEKEY;
520         } else if (!strncmp(str+prev, "INSERTKEY", len-prev) || !strncmp(str+prev, "INSERT", len-prev)) {
521                 *key= INSERTKEY;
522         } else if (!strncmp(str+prev, "HOMEKEY", len-prev) || !strncmp(str+prev, "HOME", len-prev)) {
523                 *key= HOMEKEY;
524         } else if (!strncmp(str+prev, "PAGEUPKEY", len-prev) || !strncmp(str+prev, "PAGEUP", len-prev)) {
525                 *key= PAGEUPKEY;
526         } else if (!strncmp(str+prev, "PAGEDOWNKEY", len-prev) || !strncmp(str+prev, "PAGEDOWN", len-prev)) {
527                 *key= PAGEDOWNKEY;
528         } else if (!strncmp(str+prev, "ENDKEY", len-prev) || !strncmp(str+prev, "END", len-prev)) {
529                 *key= ENDKEY;
530         
531         } else {
532                 invalid= 1;
533         }
534
535         if (!invalid && *key) {
536                 return 1;
537         }
538         
539         return 0;
540 }