2.5: added support for setting RNA properties in keymap item,
[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_windowmanager_types.h"
32
33 #include "MEM_guardedalloc.h"
34
35 #include "BLI_blenlib.h"
36
37 #include "BKE_blender.h"
38 #include "BKE_global.h"
39 #include "BKE_library.h"
40 #include "BKE_main.h"
41
42 #include "RNA_access.h"
43 #include "RNA_types.h"
44
45 #include "WM_api.h"
46 #include "WM_types.h"
47 #include "wm_window.h"
48 #include "wm_event_system.h"
49 #include "wm_event_types.h"
50
51 /* ***************** generic call, exported **************** */
52
53 static void keymap_event_set(wmKeymapItem *kmi, short type, short val, int modifier, short keymodifier)
54 {
55         kmi->type= type;
56         kmi->val= val;
57         kmi->keymodifier= keymodifier;
58         
59         if(modifier & KM_SHIFT)
60                 kmi->shift= 1;
61         else if(modifier & KM_SHIFT2)
62                 kmi->shift= 2;
63         if(modifier & KM_CTRL)
64                 kmi->ctrl= 1;
65         else if(modifier & KM_CTRL2)
66                 kmi->ctrl= 2;
67         if(modifier & KM_ALT)
68                 kmi->alt= 1;
69         else if(modifier & KM_ALT2)
70                 kmi->alt= 2;
71         if(modifier & KM_OSKEY)
72                 kmi->oskey= 1;
73         else if(modifier & KM_OSKEY2)
74                 kmi->oskey= 2;  
75 }
76
77 static void keymap_properties_set(wmKeymapItem *kmi)
78 {
79         wmOperatorType *ot;
80         
81         if(!kmi->ptr) {
82                 ot= WM_operatortype_find(kmi->idname);
83
84                 if(ot) {
85                         kmi->ptr= MEM_callocN(sizeof(PointerRNA), "wmKeymapItemPtr");
86                         RNA_pointer_create(NULL, NULL, ot->srna, kmi, kmi->ptr);
87                 }
88         }
89 }
90
91 /* if item was added, then bail out */
92 wmKeymapItem *WM_keymap_verify_item(ListBase *lb, char *idname, short type, short val, int modifier, short keymodifier)
93 {
94         wmKeymapItem *kmi;
95         
96         for(kmi= lb->first; kmi; kmi= kmi->next)
97                 if(strncmp(kmi->idname, idname, OP_MAX_TYPENAME)==0)
98                         break;
99         if(kmi==NULL) {
100                 kmi= MEM_callocN(sizeof(wmKeymapItem), "keymap entry");
101                 
102                 BLI_addtail(lb, kmi);
103                 BLI_strncpy(kmi->idname, idname, OP_MAX_TYPENAME);
104                 
105                 keymap_event_set(kmi, type, val, modifier, keymodifier);
106                 keymap_properties_set(kmi);
107         }
108         return kmi;
109 }
110
111 /* if item was added, then replace */
112 wmKeymapItem *WM_keymap_set_item(ListBase *lb, char *idname, short type, short val, int modifier, short keymodifier)
113 {
114         wmKeymapItem *kmi;
115         
116         for(kmi= lb->first; kmi; kmi= kmi->next)
117                 if(strncmp(kmi->idname, idname, OP_MAX_TYPENAME)==0)
118                         break;
119         if(kmi==NULL) {
120                 kmi= MEM_callocN(sizeof(wmKeymapItem), "keymap entry");
121         
122                 BLI_addtail(lb, kmi);
123                 BLI_strncpy(kmi->idname, idname, OP_MAX_TYPENAME);
124         }
125         keymap_event_set(kmi, type, val, modifier, keymodifier);
126         keymap_properties_set(kmi);
127         return kmi;
128 }
129
130 /* always add item */
131 wmKeymapItem *WM_keymap_add_item(ListBase *lb, char *idname, short type, short val, int modifier, short keymodifier)
132 {
133         wmKeymapItem *kmi= MEM_callocN(sizeof(wmKeymapItem), "keymap entry");
134         
135         BLI_addtail(lb, kmi);
136         BLI_strncpy(kmi->idname, idname, OP_MAX_TYPENAME);
137
138         keymap_event_set(kmi, type, val, modifier, keymodifier);
139         keymap_properties_set(kmi);
140         return kmi;
141 }
142
143 /* ****************** storage in WM ************ */
144
145 /* name id's are for storing general or multiple keymaps, 
146    space/region ids are same as DNA_space_types.h */
147 /* gets free'd in wm.c */
148
149 ListBase *WM_keymap_listbase(wmWindowManager *wm, const char *nameid, int spaceid, int regionid)
150 {
151         wmKeyMap *km;
152         
153         for(km= wm->keymaps.first; km; km= km->next)
154                 if(km->spaceid==spaceid && km->regionid==regionid)
155                         if(0==strncmp(nameid, km->nameid, KMAP_MAX_NAME))
156                                 break;
157
158         if(km==NULL) {
159                 km= MEM_callocN(sizeof(struct wmKeyMap), "keymap list");
160                 BLI_strncpy(km->nameid, nameid, KMAP_MAX_NAME);
161                 km->spaceid= spaceid;
162                 km->regionid= regionid;
163                 printf("added keymap %s %d %d\n", nameid, spaceid, regionid);
164                 BLI_addtail(&wm->keymaps, km);
165         }
166         
167         return &km->keymap;
168 }
169