7d25fb8172efe03d2f02ba12254ea859e4764853
[blender.git] / source / blender / windowmanager / intern / wm_keymap.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) 2007 Blender Foundation.
21  * All rights reserved.
22  *
23  * 
24  * Contributor(s): Blender Foundation
25  *
26  * ***** END GPL LICENSE BLOCK *****
27  */
28
29 #include <string.h>
30
31 #include "DNA_screen_types.h"
32 #include "DNA_windowmanager_types.h"
33
34 #include "MEM_guardedalloc.h"
35
36 #include "BLI_blenlib.h"
37
38 #include "BKE_blender.h"
39 #include "BKE_context.h"
40 #include "BKE_idprop.h"
41 #include "BKE_library.h"
42 #include "BKE_main.h"
43 #include "BKE_utildefines.h"
44
45 #include "RNA_access.h"
46 #include "RNA_types.h"
47 #include "RNA_enum_types.h"
48
49 #include "WM_api.h"
50 #include "WM_types.h"
51 #include "wm_window.h"
52 #include "wm_event_system.h"
53 #include "wm_event_types.h"
54
55 /* ***************** generic call, exported **************** */
56
57 static void keymap_event_set(wmKeymapItem *kmi, short type, short val, int modifier, short keymodifier)
58 {
59         kmi->type= type;
60         kmi->val= val;
61         kmi->keymodifier= keymodifier;
62         
63         if(modifier == KM_ANY) {
64                 kmi->shift= kmi->ctrl= kmi->alt= kmi->oskey= KM_ANY;
65         }
66         else {
67                 
68                 kmi->shift= kmi->ctrl= kmi->alt= kmi->oskey= 0;
69                 
70                 /* defines? */
71                 if(modifier & KM_SHIFT)
72                         kmi->shift= 1;
73                 else if(modifier & KM_SHIFT2)
74                         kmi->shift= 2;
75                 if(modifier & KM_CTRL)
76                         kmi->ctrl= 1;
77                 else if(modifier & KM_CTRL2)
78                         kmi->ctrl= 2;
79                 if(modifier & KM_ALT)
80                         kmi->alt= 1;
81                 else if(modifier & KM_ALT2)
82                         kmi->alt= 2;
83                 if(modifier & KM_OSKEY)
84                         kmi->oskey= 1;
85                 else if(modifier & KM_OSKEY2)
86                         kmi->oskey= 2;  
87         }
88 }
89
90 static void keymap_properties_set(wmKeymapItem *kmi)
91 {
92         if(!kmi->ptr) {
93                 kmi->ptr= MEM_callocN(sizeof(PointerRNA), "wmKeymapItemPtr");
94                 WM_operator_properties_create(kmi->ptr, kmi->idname);
95         }
96 }
97
98 /* if item was added, then bail out */
99 wmKeymapItem *WM_keymap_verify_item(wmKeyMap *keymap, char *idname, short type, short val, int modifier, short keymodifier)
100 {
101         wmKeymapItem *kmi;
102         
103         for(kmi= keymap->keymap.first; kmi; kmi= kmi->next)
104                 if(strncmp(kmi->idname, idname, OP_MAX_TYPENAME)==0)
105                         break;
106         if(kmi==NULL) {
107                 kmi= MEM_callocN(sizeof(wmKeymapItem), "keymap entry");
108                 
109                 BLI_addtail(&keymap->keymap, kmi);
110                 BLI_strncpy(kmi->idname, idname, OP_MAX_TYPENAME);
111                 
112                 keymap_event_set(kmi, type, val, modifier, keymodifier);
113                 keymap_properties_set(kmi);
114         }
115         return kmi;
116 }
117
118 /* always add item */
119 wmKeymapItem *WM_keymap_add_item(wmKeyMap *keymap, char *idname, short type, short val, int modifier, short keymodifier)
120 {
121         wmKeymapItem *kmi= MEM_callocN(sizeof(wmKeymapItem), "keymap entry");
122         
123         BLI_addtail(&keymap->keymap, kmi);
124         BLI_strncpy(kmi->idname, idname, OP_MAX_TYPENAME);
125
126         keymap_event_set(kmi, type, val, modifier, keymodifier);
127         keymap_properties_set(kmi);
128         return kmi;
129 }
130
131 /* ****************** storage in WM ************ */
132
133 /* name id's are for storing general or multiple keymaps, 
134    space/region ids are same as DNA_space_types.h */
135 /* gets free'd in wm.c */
136
137 wmKeyMap *WM_keymap_find(wmWindowManager *wm, const char *nameid, short spaceid, short regionid)
138 {
139         wmKeyMap *km;
140         
141         for(km= wm->keymaps.first; km; km= km->next)
142                 if(km->spaceid==spaceid && km->regionid==regionid)
143                         if(0==strncmp(nameid, km->nameid, KMAP_MAX_NAME))
144                                 return km;
145         
146         if(km==NULL) {
147                 km= MEM_callocN(sizeof(struct wmKeyMap), "keymap list");
148                 BLI_strncpy(km->nameid, nameid, KMAP_MAX_NAME);
149                 km->spaceid= spaceid;
150                 km->regionid= regionid;
151                 BLI_addtail(&wm->keymaps, km);
152         }
153         
154         return km;
155 }
156
157 /* ****************** modal keymaps ************ */
158
159 /* modal maps get linked to a running operator, and filter the keys before sending to modal() callback */
160
161 wmKeyMap *WM_modalkeymap_add(wmWindowManager *wm, const char *nameid, EnumPropertyItem *items)
162 {
163         wmKeyMap *km= WM_keymap_find(wm, nameid, 0, 0);
164         km->is_modal= 1;
165         km->items= items;
166         
167         return km;
168 }
169
170 wmKeyMap *WM_modalkeymap_get(wmWindowManager *wm, const char *nameid)
171 {
172         wmKeyMap *km;
173         
174         for(km= wm->keymaps.first; km; km= km->next)
175                 if(km->is_modal)
176                         if(0==strncmp(nameid, km->nameid, KMAP_MAX_NAME))
177                                 break;
178         
179         return km;
180 }
181
182
183 void WM_modalkeymap_add_item(wmKeyMap *km, short type, short val, int modifier, short keymodifier, short value)
184 {
185         wmKeymapItem *kmi= MEM_callocN(sizeof(wmKeymapItem), "keymap entry");
186         
187         BLI_addtail(&km->keymap, kmi);
188         kmi->propvalue= value;
189         
190         keymap_event_set(kmi, type, val, modifier, keymodifier);
191 }
192
193 void WM_modalkeymap_assign(wmKeyMap *km, const char *opname)
194 {
195         wmOperatorType *ot= WM_operatortype_find(opname, 0);
196         
197         if(ot)
198                 ot->modalkeymap= km;
199         else
200                 printf("error: modalkeymap_assign, unknown operator %s\n", opname);
201 }
202
203
204 /* ***************** get string from key events **************** */
205
206 const char *WM_key_event_string(short type)
207 {
208         const char *name= NULL;
209         if(RNA_enum_name(event_type_items, (int)type, &name))
210                 return name;
211         
212         return "";
213 }
214
215 static char *wm_keymap_item_to_string(wmKeymapItem *kmi, char *str, int len)
216 {
217         char buf[100];
218
219         buf[0]= 0;
220
221         if(kmi->shift)
222                 strcat(buf, "Shift ");
223
224         if(kmi->ctrl)
225                 strcat(buf, "Ctrl ");
226
227         if(kmi->alt)
228                 strcat(buf, "Alt ");
229
230         if(kmi->oskey)
231                 strcat(buf, "Cmd ");
232
233         strcat(buf, WM_key_event_string(kmi->type));
234         BLI_strncpy(str, buf, len);
235
236         return str;
237 }
238
239 static wmKeymapItem *wm_keymap_item_find_handlers(const bContext *C, ListBase *handlers, const char *opname, int opcontext, IDProperty *properties, int compare_props)
240 {
241         wmEventHandler *handler;
242         wmKeyMap *keymap;
243         wmKeymapItem *kmi;
244
245         /* find keymap item in handlers */
246         for(handler=handlers->first; handler; handler=handler->next) {
247                 keymap= handler->keymap;
248
249                 if(keymap && (!keymap->poll || keymap->poll((bContext*)C))) {
250                         for(kmi=keymap->keymap.first; kmi; kmi=kmi->next) {
251                                 if(strcmp(kmi->idname, opname) == 0 && WM_key_event_string(kmi->type)[0]) {
252                                         if(compare_props) {
253                                                 if(kmi->ptr && IDP_EqualsProperties(properties, kmi->ptr->data))
254                                                         return kmi;
255                                         }
256                                         else
257                                                 return kmi;
258                                 }
259                         }
260                 }
261         }
262         
263         return NULL;
264 }
265
266 static wmKeymapItem *wm_keymap_item_find(const bContext *C, const char *opname, int opcontext, IDProperty *properties, int compare_props)
267 {
268         wmKeymapItem *found= NULL;
269
270         /* look into multiple handler lists to find the item */
271         if(CTX_wm_window(C))
272                 found= wm_keymap_item_find_handlers(C, &CTX_wm_window(C)->handlers, opname, opcontext, properties, compare_props);
273         
274
275         if(CTX_wm_area(C) && found==NULL)
276                 found= wm_keymap_item_find_handlers(C, &CTX_wm_area(C)->handlers, opname, opcontext, properties, compare_props);
277
278         if(found==NULL) {
279                 if(ELEM(opcontext, WM_OP_EXEC_REGION_WIN, WM_OP_INVOKE_REGION_WIN)) {
280                         if(CTX_wm_area(C)) {
281                                 ARegion *ar= CTX_wm_area(C)->regionbase.first;
282                                 for(; ar; ar= ar->next)
283                                         if(ar->regiontype==RGN_TYPE_WINDOW)
284                                                 break;
285
286                                 if(ar)
287                                         found= wm_keymap_item_find_handlers(C, &ar->handlers, opname, opcontext, properties, compare_props);
288                         }
289                 }
290                 else {
291                         if(CTX_wm_region(C))
292                                 found= wm_keymap_item_find_handlers(C, &CTX_wm_region(C)->handlers, opname, opcontext, properties, compare_props);
293                 }
294         }
295         
296         return found;
297 }
298
299 char *WM_key_event_operator_string(const bContext *C, const char *opname, int opcontext, IDProperty *properties, char *str, int len)
300 {
301         wmKeymapItem *found= wm_keymap_item_find(C, opname, opcontext, properties, 1);
302
303         if(!found)
304                 found= wm_keymap_item_find(C, opname, opcontext, properties, 0);
305         
306         if(found) {
307                 wm_keymap_item_to_string(found, str, len);
308                 return str;
309         }
310
311         return NULL;
312 }
313
314 /* searches context and changes keymap item, if found */
315 void WM_key_event_operator_change(const bContext *C, const char *opname, int opcontext, IDProperty *properties, short key, short modifier)
316 {
317         wmKeymapItem *found= wm_keymap_item_find(C, opname, opcontext, properties, 1);
318
319         if(!found)
320                 found= wm_keymap_item_find(C, opname, opcontext, properties, 0);
321
322         if(found) {
323                 keymap_event_set(found, key, KM_PRESS, modifier, 0);
324         }
325 }
326
327 /* ********************* */
328
329 int WM_key_event_is_tweak(short type)
330 {
331         if(type>=EVT_TWEAK_L && type<=EVT_GESTURE)
332                 return 1;
333         return 0;
334 }
335
336