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                 /* defines? */
69                 if(modifier & KM_SHIFT)
70                         kmi->shift= 1;
71                 else if(modifier & KM_SHIFT2)
72                         kmi->shift= 2;
73                 if(modifier & KM_CTRL)
74                         kmi->ctrl= 1;
75                 else if(modifier & KM_CTRL2)
76                         kmi->ctrl= 2;
77                 if(modifier & KM_ALT)
78                         kmi->alt= 1;
79                 else if(modifier & KM_ALT2)
80                         kmi->alt= 2;
81                 if(modifier & KM_OSKEY)
82                         kmi->oskey= 1;
83                 else if(modifier & KM_OSKEY2)
84                         kmi->oskey= 2;  
85         }
86 }
87
88 static void keymap_properties_set(wmKeymapItem *kmi)
89 {
90         if(!kmi->ptr) {
91                 kmi->ptr= MEM_callocN(sizeof(PointerRNA), "wmKeymapItemPtr");
92                 WM_operator_properties_create(kmi->ptr, kmi->idname);
93         }
94 }
95
96 /* if item was added, then bail out */
97 wmKeymapItem *WM_keymap_verify_item(ListBase *lb, char *idname, short type, short val, int modifier, short keymodifier)
98 {
99         wmKeymapItem *kmi;
100         
101         for(kmi= lb->first; kmi; kmi= kmi->next)
102                 if(strncmp(kmi->idname, idname, OP_MAX_TYPENAME)==0)
103                         break;
104         if(kmi==NULL) {
105                 kmi= MEM_callocN(sizeof(wmKeymapItem), "keymap entry");
106                 
107                 BLI_addtail(lb, kmi);
108                 BLI_strncpy(kmi->idname, idname, OP_MAX_TYPENAME);
109                 
110                 keymap_event_set(kmi, type, val, modifier, keymodifier);
111                 keymap_properties_set(kmi);
112         }
113         return kmi;
114 }
115
116 /* always add item */
117 wmKeymapItem *WM_keymap_add_item(ListBase *lb, char *idname, short type, short val, int modifier, short keymodifier)
118 {
119         wmKeymapItem *kmi= MEM_callocN(sizeof(wmKeymapItem), "keymap entry");
120         
121         BLI_addtail(lb, kmi);
122         BLI_strncpy(kmi->idname, idname, OP_MAX_TYPENAME);
123
124         keymap_event_set(kmi, type, val, modifier, keymodifier);
125         keymap_properties_set(kmi);
126         return kmi;
127 }
128
129 /* ****************** storage in WM ************ */
130
131 /* name id's are for storing general or multiple keymaps, 
132    space/region ids are same as DNA_space_types.h */
133 /* gets free'd in wm.c */
134
135 static wmKeyMap *wm_keymap_add(wmWindowManager *wm, const char *nameid, short spaceid, short regionid)
136 {
137         wmKeyMap *km;
138         
139         for(km= wm->keymaps.first; km; km= km->next)
140                 if(km->spaceid==spaceid && km->regionid==regionid)
141                         if(0==strncmp(nameid, km->nameid, KMAP_MAX_NAME))
142                                 break;
143         
144         if(km==NULL) {
145                 km= MEM_callocN(sizeof(struct wmKeyMap), "keymap list");
146                 BLI_strncpy(km->nameid, nameid, KMAP_MAX_NAME);
147                 km->spaceid= spaceid;
148                 km->regionid= regionid;
149                 BLI_addtail(&wm->keymaps, km);
150         }
151         
152         return km;
153 }
154
155 ListBase *WM_keymap_listbase(wmWindowManager *wm, const char *nameid, short spaceid, short regionid)
156 {
157         wmKeyMap *km= wm_keymap_add(wm, nameid, spaceid, regionid);
158         return &km->keymap;
159 }
160
161 /* ****************** modal keymaps ************ */
162
163 /* modal maps get linked to a running operator, and filter the keys before sending to modal() callback */
164
165 wmKeyMap *WM_modalkeymap_add(wmWindowManager *wm, const char *nameid, EnumPropertyItem *items)
166 {
167         wmKeyMap *km= wm_keymap_add(wm, nameid, 0, 0);
168         km->is_modal= 1;
169         km->items= items;
170         
171         return km;
172 }
173
174 wmKeyMap *WM_modalkeymap_get(wmWindowManager *wm, const char *nameid)
175 {
176         wmKeyMap *km;
177         
178         for(km= wm->keymaps.first; km; km= km->next)
179                 if(km->is_modal)
180                         if(0==strncmp(nameid, km->nameid, KMAP_MAX_NAME))
181                                 break;
182         
183         return km;
184 }
185
186
187 void WM_modalkeymap_add_item(wmKeyMap *km, short type, short val, int modifier, short keymodifier, short value)
188 {
189         wmKeymapItem *kmi= MEM_callocN(sizeof(wmKeymapItem), "keymap entry");
190         
191         BLI_addtail(&km->keymap, kmi);
192         kmi->propvalue= value;
193         
194         keymap_event_set(kmi, type, val, modifier, keymodifier);
195 }
196
197 void WM_modalkeymap_assign(wmKeyMap *km, const char *opname)
198 {
199         wmOperatorType *ot= WM_operatortype_find(opname, 0);
200         
201         if(ot)
202                 ot->modalkeymap= km;
203         else
204                 printf("error: modalkeymap_assign, unknown operator %s\n", opname);
205 }
206
207
208 /* ***************** get string from key events **************** */
209
210 const char *WM_key_event_string(short type)
211 {
212         const char *name= NULL;
213         if(RNA_enum_name(event_type_items, (int)type, &name))
214                 return name;
215         
216         return "";
217 }
218
219 static char *wm_keymap_item_to_string(wmKeymapItem *kmi, char *str, int len)
220 {
221         char buf[100];
222
223         buf[0]= 0;
224
225         if(kmi->shift)
226                 strcat(buf, "Shift ");
227
228         if(kmi->ctrl)
229                 strcat(buf, "Ctrl ");
230
231         if(kmi->alt)
232                 strcat(buf, "Alt ");
233
234         if(kmi->oskey)
235                 strcat(buf, "OS ");
236
237         strcat(buf, WM_key_event_string(kmi->type));
238         BLI_strncpy(str, buf, len);
239
240         return str;
241 }
242
243 static char *wm_keymap_item_find(ListBase *handlers, const char *opname, int opcontext, IDProperty *properties, char *str, int len)
244 {
245         wmEventHandler *handler;
246         wmKeymapItem *kmi;
247
248         /* find keymap item in handlers */
249         for(handler=handlers->first; handler; handler=handler->next)
250                 if(handler->keymap)
251                         for(kmi=handler->keymap->first; kmi; kmi=kmi->next)
252                                 if(strcmp(kmi->idname, opname) == 0 && WM_key_event_string(kmi->type)[0])
253                                         if(kmi->ptr && IDP_EqualsProperties(properties, kmi->ptr->data))
254                                                 return wm_keymap_item_to_string(kmi, str, len);
255         
256         return NULL;
257 }
258
259 char *WM_key_event_operator_string(const bContext *C, const char *opname, int opcontext, IDProperty *properties, char *str, int len)
260 {
261         char *found= NULL;
262
263         /* look into multiple handler lists to find the item */
264         if(CTX_wm_window(C))
265                 if((found= wm_keymap_item_find(&CTX_wm_window(C)->handlers, opname, opcontext, properties, str, len)))
266                         return found;
267
268         if(CTX_wm_area(C))
269                 if((found= wm_keymap_item_find(&CTX_wm_area(C)->handlers, opname, opcontext, properties, str, len)))
270                         return found;
271
272         if(ELEM(opcontext, WM_OP_EXEC_REGION_WIN, WM_OP_INVOKE_REGION_WIN)) {
273                 if(CTX_wm_area(C)) {
274                         ARegion *ar= CTX_wm_area(C)->regionbase.first;
275                         for(; ar; ar= ar->next)
276                                 if(ar->regiontype==RGN_TYPE_WINDOW)
277                                         break;
278
279                         if(ar)
280                                 if((found= wm_keymap_item_find(&ar->handlers, opname, opcontext, properties, str, len)))
281                                         return found;
282                 }
283         }
284         else {
285                 if(CTX_wm_region(C))
286                         if((found= wm_keymap_item_find(&CTX_wm_region(C)->handlers, opname, opcontext, properties, str, len)))
287                                 return found;
288         }
289
290         return NULL;
291 }
292
293 /* ********************* */
294
295 int WM_key_event_is_tweak(short type)
296 {
297         if(type>=EVT_TWEAK_L && type<=EVT_GESTURE)
298                 return 1;
299         return 0;
300 }
301
302