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;
68 kmi->shift= kmi->ctrl= kmi->alt= kmi->oskey= 0;
71 if(modifier & KM_SHIFT)
73 else if(modifier & KM_SHIFT2)
75 if(modifier & KM_CTRL)
77 else if(modifier & KM_CTRL2)
81 else if(modifier & KM_ALT2)
83 if(modifier & KM_OSKEY)
85 else if(modifier & KM_OSKEY2)
90 static void keymap_properties_set(wmKeymapItem *kmi)
93 kmi->ptr= MEM_callocN(sizeof(PointerRNA), "wmKeymapItemPtr");
94 WM_operator_properties_create(kmi->ptr, kmi->idname);
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)
103 for(kmi= lb->first; kmi; kmi= kmi->next)
104 if(strncmp(kmi->idname, idname, OP_MAX_TYPENAME)==0)
107 kmi= MEM_callocN(sizeof(wmKeymapItem), "keymap entry");
109 BLI_addtail(lb, kmi);
110 BLI_strncpy(kmi->idname, idname, OP_MAX_TYPENAME);
112 keymap_event_set(kmi, type, val, modifier, keymodifier);
113 keymap_properties_set(kmi);
118 /* always add item */
119 wmKeymapItem *WM_keymap_add_item(ListBase *lb, char *idname, short type, short val, int modifier, short keymodifier)
121 wmKeymapItem *kmi= MEM_callocN(sizeof(wmKeymapItem), "keymap entry");
123 BLI_addtail(lb, kmi);
124 BLI_strncpy(kmi->idname, idname, OP_MAX_TYPENAME);
126 keymap_event_set(kmi, type, val, modifier, keymodifier);
127 keymap_properties_set(kmi);
131 /* ****************** storage in WM ************ */
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 */
137 static wmKeyMap *wm_keymap_add(wmWindowManager *wm, const char *nameid, short spaceid, short regionid)
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))
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);
157 ListBase *WM_keymap_listbase(wmWindowManager *wm, const char *nameid, short spaceid, short regionid)
159 wmKeyMap *km= wm_keymap_add(wm, nameid, spaceid, regionid);
163 /* ****************** modal keymaps ************ */
165 /* modal maps get linked to a running operator, and filter the keys before sending to modal() callback */
167 wmKeyMap *WM_modalkeymap_add(wmWindowManager *wm, const char *nameid, EnumPropertyItem *items)
169 wmKeyMap *km= wm_keymap_add(wm, nameid, 0, 0);
176 wmKeyMap *WM_modalkeymap_get(wmWindowManager *wm, const char *nameid)
180 for(km= wm->keymaps.first; km; km= km->next)
182 if(0==strncmp(nameid, km->nameid, KMAP_MAX_NAME))
189 void WM_modalkeymap_add_item(wmKeyMap *km, short type, short val, int modifier, short keymodifier, short value)
191 wmKeymapItem *kmi= MEM_callocN(sizeof(wmKeymapItem), "keymap entry");
193 BLI_addtail(&km->keymap, kmi);
194 kmi->propvalue= value;
196 keymap_event_set(kmi, type, val, modifier, keymodifier);
199 void WM_modalkeymap_assign(wmKeyMap *km, const char *opname)
201 wmOperatorType *ot= WM_operatortype_find(opname, 0);
206 printf("error: modalkeymap_assign, unknown operator %s\n", opname);
210 /* ***************** get string from key events **************** */
212 const char *WM_key_event_string(short type)
214 const char *name= NULL;
215 if(RNA_enum_name(event_type_items, (int)type, &name))
221 static char *wm_keymap_item_to_string(wmKeymapItem *kmi, char *str, int len)
228 strcat(buf, "Shift ");
231 strcat(buf, "Ctrl ");
239 strcat(buf, WM_key_event_string(kmi->type));
240 BLI_strncpy(str, buf, len);
245 static wmKeymapItem *wm_keymap_item_find_handlers(ListBase *handlers, const char *opname, int opcontext, IDProperty *properties, int compare_props)
247 wmEventHandler *handler;
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]) {
256 if(kmi->ptr && IDP_EqualsProperties(properties, kmi->ptr->data))
269 static wmKeymapItem *wm_keymap_item_find(const bContext *C, const char *opname, int opcontext, IDProperty *properties, int compare_props)
271 wmKeymapItem *found= NULL;
273 /* look into multiple handler lists to find the item */
275 found= wm_keymap_item_find_handlers(&CTX_wm_window(C)->handlers, opname, opcontext, properties, compare_props);
278 if(CTX_wm_area(C) && found==NULL)
279 found= wm_keymap_item_find_handlers(&CTX_wm_area(C)->handlers, opname, opcontext, properties, compare_props);
282 if(ELEM(opcontext, WM_OP_EXEC_REGION_WIN, WM_OP_INVOKE_REGION_WIN)) {
284 ARegion *ar= CTX_wm_area(C)->regionbase.first;
285 for(; ar; ar= ar->next)
286 if(ar->regiontype==RGN_TYPE_WINDOW)
290 found= wm_keymap_item_find_handlers(&ar->handlers, opname, opcontext, properties, compare_props);
295 found= wm_keymap_item_find_handlers(&CTX_wm_region(C)->handlers, opname, opcontext, properties, compare_props);
302 char *WM_key_event_operator_string(const bContext *C, const char *opname, int opcontext, IDProperty *properties, char *str, int len)
304 wmKeymapItem *found= wm_keymap_item_find(C, opname, opcontext, properties, 1);
307 found= wm_keymap_item_find(C, opname, opcontext, properties, 0);
310 wm_keymap_item_to_string(found, str, len);
317 /* searches context and changes keymap item, if found */
318 void WM_key_event_operator_change(const bContext *C, const char *opname, int opcontext, IDProperty *properties, short key, short modifier)
320 wmKeymapItem *found= wm_keymap_item_find(C, opname, opcontext, properties, 1);
323 found= wm_keymap_item_find(C, opname, opcontext, properties, 0);
326 keymap_event_set(found, key, KM_PRESS, modifier, 0);
330 /* ********************* */
332 int WM_key_event_is_tweak(short type)
334 if(type>=EVT_TWEAK_L && type<=EVT_GESTURE)