NLA SoC: Merge from 2.5
[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                 /* 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 ListBase *WM_keymap_listbase(wmWindowManager *wm, const char *nameid, int spaceid, int 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->keymap;
153 }
154
155 /* ***************** get string from key events **************** */
156
157 const char *WM_key_event_string(short type)
158 {
159         const char *name= NULL;
160         if(RNA_enum_name(event_type_items, (int)type, &name))
161                 return name;
162         
163         return "";
164 }
165
166 static char *wm_keymap_item_to_string(wmKeymapItem *kmi, char *str, int len)
167 {
168         char buf[100];
169
170         buf[0]= 0;
171
172         if(kmi->shift)
173                 strcat(buf, "Shift ");
174
175         if(kmi->ctrl)
176                 strcat(buf, "Ctrl ");
177
178         if(kmi->alt)
179                 strcat(buf, "Alt ");
180
181         if(kmi->oskey)
182                 strcat(buf, "OS ");
183
184         strcat(buf, WM_key_event_string(kmi->type));
185         BLI_strncpy(str, buf, len);
186
187         return str;
188 }
189
190 static char *wm_keymap_item_find(ListBase *handlers, const char *opname, int opcontext, IDProperty *properties, char *str, int len)
191 {
192         wmEventHandler *handler;
193         wmKeymapItem *kmi;
194
195         /* find keymap item in handlers */
196         for(handler=handlers->first; handler; handler=handler->next)
197                 if(handler->keymap)
198                         for(kmi=handler->keymap->first; kmi; kmi=kmi->next)
199                                 if(strcmp(kmi->idname, opname) == 0 && WM_key_event_string(kmi->type)[0])
200                                         if(kmi->ptr && IDP_EqualsProperties(properties, kmi->ptr->data))
201                                                 return wm_keymap_item_to_string(kmi, str, len);
202         
203         return NULL;
204 }
205
206 char *WM_key_event_operator_string(const bContext *C, const char *opname, int opcontext, IDProperty *properties, char *str, int len)
207 {
208         char *found= NULL;
209
210         /* look into multiple handler lists to find the item */
211         if(CTX_wm_window(C))
212                 if((found= wm_keymap_item_find(&CTX_wm_window(C)->handlers, opname, opcontext, properties, str, len)))
213                         return found;
214
215         if(CTX_wm_area(C))
216                 if((found= wm_keymap_item_find(&CTX_wm_area(C)->handlers, opname, opcontext, properties, str, len)))
217                         return found;
218
219         if(ELEM(opcontext, WM_OP_EXEC_REGION_WIN, WM_OP_INVOKE_REGION_WIN)) {
220                 if(CTX_wm_area(C)) {
221                         ARegion *ar= CTX_wm_area(C)->regionbase.first;
222                         for(; ar; ar= ar->next)
223                                 if(ar->regiontype==RGN_TYPE_WINDOW)
224                                         break;
225
226                         if(ar)
227                                 if((found= wm_keymap_item_find(&ar->handlers, opname, opcontext, properties, str, len)))
228                                         return found;
229                 }
230         }
231         else {
232                 if(CTX_wm_region(C))
233                         if((found= wm_keymap_item_find(&CTX_wm_region(C)->handlers, opname, opcontext, properties, str, len)))
234                                 return found;
235         }
236
237         return NULL;
238 }
239