2.5
[blender-staging.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(ListBase *lb, char *idname, short type, short val, int modifier, short keymodifier)
100 {
101         wmKeymapItem *kmi;
102         
103         for(kmi= lb->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(lb, 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(ListBase *lb, char *idname, short type, short val, int modifier, short keymodifier)
120 {
121         wmKeymapItem *kmi= MEM_callocN(sizeof(wmKeymapItem), "keymap entry");
122         
123         BLI_addtail(lb, 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 static wmKeyMap *wm_keymap_add(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                                 break;
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 ListBase *WM_keymap_listbase(wmWindowManager *wm, const char *nameid, short spaceid, short regionid)
158 {
159         wmKeyMap *km= wm_keymap_add(wm, nameid, spaceid, regionid);
160         return &km->keymap;
161 }
162
163 /* ****************** modal keymaps ************ */
164
165 /* modal maps get linked to a running operator, and filter the keys before sending to modal() callback */
166
167 wmKeyMap *WM_modalkeymap_add(wmWindowManager *wm, const char *nameid, EnumPropertyItem *items)
168 {
169         wmKeyMap *km= wm_keymap_add(wm, nameid, 0, 0);
170         km->is_modal= 1;
171         km->items= items;
172         
173         return km;
174 }
175
176 wmKeyMap *WM_modalkeymap_get(wmWindowManager *wm, const char *nameid)
177 {
178         wmKeyMap *km;
179         
180         for(km= wm->keymaps.first; km; km= km->next)
181                 if(km->is_modal)
182                         if(0==strncmp(nameid, km->nameid, KMAP_MAX_NAME))
183                                 break;
184         
185         return km;
186 }
187
188
189 void WM_modalkeymap_add_item(wmKeyMap *km, short type, short val, int modifier, short keymodifier, short value)
190 {
191         wmKeymapItem *kmi= MEM_callocN(sizeof(wmKeymapItem), "keymap entry");
192         
193         BLI_addtail(&km->keymap, kmi);
194         kmi->propvalue= value;
195         
196         keymap_event_set(kmi, type, val, modifier, keymodifier);
197 }
198
199 void WM_modalkeymap_assign(wmKeyMap *km, const char *opname)
200 {
201         wmOperatorType *ot= WM_operatortype_find(opname, 0);
202         
203         if(ot)
204                 ot->modalkeymap= km;
205         else
206                 printf("error: modalkeymap_assign, unknown operator %s\n", opname);
207 }
208
209
210 /* ***************** get string from key events **************** */
211
212 const char *WM_key_event_string(short type)
213 {
214         const char *name= NULL;
215         if(RNA_enum_name(event_type_items, (int)type, &name))
216                 return name;
217         
218         return "";
219 }
220
221 static char *wm_keymap_item_to_string(wmKeymapItem *kmi, char *str, int len)
222 {
223         char buf[100];
224
225         buf[0]= 0;
226
227         if(kmi->shift)
228                 strcat(buf, "Shift ");
229
230         if(kmi->ctrl)
231                 strcat(buf, "Ctrl ");
232
233         if(kmi->alt)
234                 strcat(buf, "Alt ");
235
236         if(kmi->oskey)
237                 strcat(buf, "Cmd ");
238
239         strcat(buf, WM_key_event_string(kmi->type));
240         BLI_strncpy(str, buf, len);
241
242         return str;
243 }
244
245 static wmKeymapItem *wm_keymap_item_find_handlers(ListBase *handlers, const char *opname, int opcontext, IDProperty *properties)
246 {
247         wmEventHandler *handler;
248         wmKeymapItem *kmi;
249
250         /* find keymap item in handlers */
251         for(handler=handlers->first; handler; handler=handler->next)
252                 if(handler->keymap)
253                         for(kmi=handler->keymap->first; kmi; kmi=kmi->next)
254                                 if(strcmp(kmi->idname, opname) == 0 && WM_key_event_string(kmi->type)[0])
255                                         if(kmi->ptr && IDP_EqualsProperties(properties, kmi->ptr->data))
256                                                 return kmi;
257         
258         return NULL;
259 }
260
261 static wmKeymapItem *wm_keymap_item_find(const bContext *C, const char *opname, int opcontext, IDProperty *properties)
262 {
263         wmKeymapItem *found= NULL;
264
265         /* look into multiple handler lists to find the item */
266         if(CTX_wm_window(C))
267                 found= wm_keymap_item_find_handlers(&CTX_wm_window(C)->handlers, opname, opcontext, properties);
268         
269
270         if(CTX_wm_area(C) && found==NULL)
271                 found= wm_keymap_item_find_handlers(&CTX_wm_area(C)->handlers, opname, opcontext, properties);
272
273         if(found==NULL) {
274                 if(ELEM(opcontext, WM_OP_EXEC_REGION_WIN, WM_OP_INVOKE_REGION_WIN)) {
275                         if(CTX_wm_area(C)) {
276                                 ARegion *ar= CTX_wm_area(C)->regionbase.first;
277                                 for(; ar; ar= ar->next)
278                                         if(ar->regiontype==RGN_TYPE_WINDOW)
279                                                 break;
280
281                                 if(ar)
282                                         found= wm_keymap_item_find_handlers(&ar->handlers, opname, opcontext, properties);
283                         }
284                 }
285                 else {
286                         if(CTX_wm_region(C))
287                                 found= wm_keymap_item_find_handlers(&CTX_wm_region(C)->handlers, opname, opcontext, properties);
288                 }
289         }
290         
291         return found;
292 }
293
294 char *WM_key_event_operator_string(const bContext *C, const char *opname, int opcontext, IDProperty *properties, char *str, int len)
295 {
296         wmKeymapItem *found= wm_keymap_item_find(C, opname, opcontext, properties);
297         
298         if(found) {
299                 wm_keymap_item_to_string(found, str, len);
300                 return str;
301         }
302
303         return NULL;
304 }
305
306 /* searches context and changes keymap item, if found */
307 void WM_key_event_operator_change(const bContext *C, const char *opname, int opcontext, IDProperty *properties, short key, short modifier)
308 {
309         wmKeymapItem *found= wm_keymap_item_find(C, opname, opcontext, properties);
310
311         if(found) {
312                 keymap_event_set(found, key, KM_PRESS, modifier, 0);
313         }
314 }
315
316 /* ********************* */
317
318 int WM_key_event_is_tweak(short type)
319 {
320         if(type>=EVT_TWEAK_L && type<=EVT_GESTURE)
321                 return 1;
322         return 0;
323 }
324
325