4 * ***** BEGIN GPL LICENSE BLOCK *****
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.
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.
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.
20 * The Original Code is Copyright (C) 2007 Blender Foundation.
21 * All rights reserved.
24 * Contributor(s): Blender Foundation
26 * ***** END GPL LICENSE BLOCK *****
31 #include "DNA_screen_types.h"
32 #include "DNA_windowmanager_types.h"
34 #include "MEM_guardedalloc.h"
36 #include "BLI_blenlib.h"
38 #include "BKE_blender.h"
39 #include "BKE_context.h"
40 #include "BKE_idprop.h"
41 #include "BKE_library.h"
43 #include "BKE_utildefines.h"
45 #include "RNA_access.h"
46 #include "RNA_types.h"
47 #include "RNA_enum_types.h"
51 #include "wm_window.h"
52 #include "wm_event_system.h"
53 #include "wm_event_types.h"
55 /* ***************** generic call, exported **************** */
57 static void keymap_event_set(wmKeymapItem *kmi, short type, short val, int modifier, short keymodifier)
61 kmi->keymodifier= keymodifier;
63 if(modifier == KM_ANY) {
64 kmi->shift= kmi->ctrl= kmi->alt= kmi->oskey= KM_ANY;
69 if(modifier & KM_SHIFT)
71 else if(modifier & KM_SHIFT2)
73 if(modifier & KM_CTRL)
75 else if(modifier & KM_CTRL2)
79 else if(modifier & KM_ALT2)
81 if(modifier & KM_OSKEY)
83 else if(modifier & KM_OSKEY2)
88 static void keymap_properties_set(wmKeymapItem *kmi)
91 kmi->ptr= MEM_callocN(sizeof(PointerRNA), "wmKeymapItemPtr");
92 WM_operator_properties_create(kmi->ptr, kmi->idname);
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)
101 for(kmi= lb->first; kmi; kmi= kmi->next)
102 if(strncmp(kmi->idname, idname, OP_MAX_TYPENAME)==0)
105 kmi= MEM_callocN(sizeof(wmKeymapItem), "keymap entry");
107 BLI_addtail(lb, kmi);
108 BLI_strncpy(kmi->idname, idname, OP_MAX_TYPENAME);
110 keymap_event_set(kmi, type, val, modifier, keymodifier);
111 keymap_properties_set(kmi);
116 /* always add item */
117 wmKeymapItem *WM_keymap_add_item(ListBase *lb, char *idname, short type, short val, int modifier, short keymodifier)
119 wmKeymapItem *kmi= MEM_callocN(sizeof(wmKeymapItem), "keymap entry");
121 BLI_addtail(lb, kmi);
122 BLI_strncpy(kmi->idname, idname, OP_MAX_TYPENAME);
124 keymap_event_set(kmi, type, val, modifier, keymodifier);
125 keymap_properties_set(kmi);
129 /* ****************** storage in WM ************ */
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 */
135 static wmKeyMap *wm_keymap_add(wmWindowManager *wm, const char *nameid, short spaceid, short regionid)
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))
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);
155 ListBase *WM_keymap_listbase(wmWindowManager *wm, const char *nameid, short spaceid, short regionid)
157 wmKeyMap *km= wm_keymap_add(wm, nameid, spaceid, regionid);
161 /* ****************** modal keymaps ************ */
163 /* modal maps get linked to a running operator, and filter the keys before sending to modal() callback */
165 wmKeyMap *WM_modalkeymap_add(wmWindowManager *wm, const char *nameid, EnumPropertyItem *items)
167 wmKeyMap *km= wm_keymap_add(wm, nameid, 0, 0);
174 wmKeyMap *WM_modalkeymap_get(wmWindowManager *wm, const char *nameid)
178 for(km= wm->keymaps.first; km; km= km->next)
180 if(0==strncmp(nameid, km->nameid, KMAP_MAX_NAME))
187 void WM_modalkeymap_add_item(wmKeyMap *km, short type, short val, int modifier, short keymodifier, short value)
189 wmKeymapItem *kmi= MEM_callocN(sizeof(wmKeymapItem), "keymap entry");
191 BLI_addtail(&km->keymap, kmi);
192 kmi->propvalue= value;
194 keymap_event_set(kmi, type, val, modifier, keymodifier);
197 void WM_modalkeymap_assign(wmKeyMap *km, const char *opname)
199 wmOperatorType *ot= WM_operatortype_find(opname, 0);
204 printf("error: modalkeymap_assign, unknown operator %s\n", opname);
208 /* ***************** get string from key events **************** */
210 const char *WM_key_event_string(short type)
212 const char *name= NULL;
213 if(RNA_enum_name(event_type_items, (int)type, &name))
219 static char *wm_keymap_item_to_string(wmKeymapItem *kmi, char *str, int len)
226 strcat(buf, "Shift ");
229 strcat(buf, "Ctrl ");
237 strcat(buf, WM_key_event_string(kmi->type));
238 BLI_strncpy(str, buf, len);
243 static char *wm_keymap_item_find(ListBase *handlers, const char *opname, int opcontext, IDProperty *properties, char *str, int len)
245 wmEventHandler *handler;
248 /* find keymap item in handlers */
249 for(handler=handlers->first; handler; handler=handler->next)
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);
259 char *WM_key_event_operator_string(const bContext *C, const char *opname, int opcontext, IDProperty *properties, char *str, int len)
263 /* look into multiple handler lists to find the item */
265 if((found= wm_keymap_item_find(&CTX_wm_window(C)->handlers, opname, opcontext, properties, str, len)))
269 if((found= wm_keymap_item_find(&CTX_wm_area(C)->handlers, opname, opcontext, properties, str, len)))
272 if(ELEM(opcontext, WM_OP_EXEC_REGION_WIN, WM_OP_INVOKE_REGION_WIN)) {
274 ARegion *ar= CTX_wm_area(C)->regionbase.first;
275 for(; ar; ar= ar->next)
276 if(ar->regiontype==RGN_TYPE_WINDOW)
280 if((found= wm_keymap_item_find(&ar->handlers, opname, opcontext, properties, str, len)))
286 if((found= wm_keymap_item_find(&CTX_wm_region(C)->handlers, opname, opcontext, properties, str, len)))
293 /* ********************* */
295 int WM_key_event_is_tweak(short type)
297 if(type>=EVT_TWEAK_L && type<=EVT_GESTURE)