Fix syntax for ID keyword.
[blender-staging.git] / source / blender / windowmanager / intern / wm.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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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 "GHOST_C-api.h"
36
37 #include "BLI_blenlib.h"
38
39 #include "BKE_blender.h"
40 #include "BKE_context.h"
41 #include "BKE_idprop.h"
42 #include "BKE_library.h"
43 #include "BKE_main.h"
44 #include "BKE_screen.h"
45 #include "BKE_report.h"
46 #include "BKE_global.h"
47
48 #include "WM_api.h"
49 #include "WM_types.h"
50 #include "wm_window.h"
51 #include "wm_event_system.h"
52 #include "wm_event_types.h"
53 #include "wm_draw.h"
54 #include "wm.h"
55
56 #include "ED_screen.h"
57 #include "BPY_extern.h"
58
59 #include "RNA_types.h"
60
61 /* ****************************************************** */
62
63 #define MAX_OP_REGISTERED       32
64
65 void WM_operator_free(wmOperator *op)
66 {
67         if(op->py_instance) {
68                 /* do this first incase there are any __del__ functions or
69                  * similar that use properties */
70                 BPY_DECREF(op->py_instance);
71         }
72
73         if(op->ptr) {
74                 op->properties= op->ptr->data;
75                 MEM_freeN(op->ptr);
76         }
77
78         if(op->properties) {
79                 IDP_FreeProperty(op->properties);
80                 MEM_freeN(op->properties);
81         }
82
83         if(op->reports && (op->reports->flag & RPT_FREE)) {
84                 BKE_reports_clear(op->reports);
85                 MEM_freeN(op->reports);
86         }
87
88         if(op->macro.first) {
89                 wmOperator *opm, *opmnext;
90                 for(opm= op->macro.first; opm; opm= opmnext) {
91                         opmnext = opm->next;
92                         WM_operator_free(opm);
93                 }
94         }
95         
96         MEM_freeN(op);
97 }
98
99 /* all operations get registered in the windowmanager here */
100 /* called on event handling by event_system.c */
101 void wm_operator_register(bContext *C, wmOperator *op)
102 {
103         wmWindowManager *wm= CTX_wm_manager(C);
104         int tot;
105         char *buf;
106
107         BLI_addtail(&wm->operators, op);
108         tot= BLI_countlist(&wm->operators);
109         
110         while(tot>MAX_OP_REGISTERED) {
111                 wmOperator *opt= wm->operators.first;
112                 BLI_remlink(&wm->operators, opt);
113                 WM_operator_free(opt);
114                 tot--;
115         }
116         
117         
118         /* Report the string representation of the operator */
119         buf = WM_operator_pystring(C, op->type, op->ptr, 1);
120         BKE_report(CTX_wm_reports(C), RPT_OPERATOR, buf);
121         MEM_freeN(buf);
122         
123         /* so the console is redrawn */
124         WM_event_add_notifier(C, NC_SPACE|ND_SPACE_CONSOLE_REPORT, NULL);
125 }
126
127
128 void WM_operator_stack_clear(bContext *C)
129 {
130         wmWindowManager *wm= CTX_wm_manager(C);
131         wmOperator *op;
132         
133         while((op= wm->operators.first)) {
134                 BLI_remlink(&wm->operators, op);
135                 WM_operator_free(op);
136         }
137         
138 }
139
140 /* ****************************************** */
141
142 static ListBase menutypes = {NULL, NULL}; /* global menutype list */
143
144 MenuType *WM_menutype_find(const char *idname, int quiet)
145 {
146         MenuType* mt;
147
148         if (idname[0]) {
149                 for(mt=menutypes.first; mt; mt=mt->next)
150                         if(strcmp(idname, mt->idname)==0)
151                                 return mt;
152         }
153
154         if(!quiet)
155                 printf("search for unknown menutype %s\n", idname);
156
157         return NULL;
158 }
159
160 int WM_menutype_add(MenuType* mt)
161 {
162         BLI_addtail(&menutypes, mt);
163         return 1;
164 }
165
166 void WM_menutype_freelink(MenuType* mt)
167 {
168         BLI_freelinkN(&menutypes, mt);
169 }
170
171 void WM_menutype_free(void)
172 {
173         MenuType* mt= menutypes.first, *mt_next;
174
175         while(mt) {
176                 mt_next= mt->next;
177
178                 if(mt->ext.free)
179                         mt->ext.free(mt->ext.data);
180
181                 WM_menutype_freelink(mt);
182
183                 mt= mt_next;
184         }
185 }
186
187 /* ****************************************** */
188
189 void WM_keymap_init(bContext *C)
190 {
191         wmWindowManager *wm= CTX_wm_manager(C);
192
193         if(!wm->defaultconf)
194                 wm->defaultconf= WM_keyconfig_add(wm, "Blender");
195         
196         if(wm && CTX_py_init_get(C) && (wm->initialized & WM_INIT_KEYMAP) == 0) {
197                 /* create default key config */
198                 wm_window_keymap(wm->defaultconf);
199                 ED_spacetypes_keymap(wm->defaultconf);
200                 WM_keyconfig_userdef(wm);
201
202                 wm->initialized |= WM_INIT_KEYMAP;
203         }
204 }
205
206 void WM_check(bContext *C)
207 {
208         wmWindowManager *wm= CTX_wm_manager(C);
209         
210         /* wm context */
211         if(wm==NULL) {
212                 wm= CTX_data_main(C)->wm.first;
213                 CTX_wm_manager_set(C, wm);
214         }
215         if(wm==NULL) return;
216         if(wm->windows.first==NULL) return;
217
218         if (!G.background) {
219                 /* case: fileread */
220                 if((wm->initialized & WM_INIT_WINDOW) == 0) {
221                         WM_keymap_init(C);
222                         WM_autosave_init(wm);
223                 }
224
225                 /* case: no open windows at all, for old file reads */
226                 wm_window_add_ghostwindows(wm);
227
228                 /* case: fileread */
229                 if((wm->initialized & WM_INIT_WINDOW) == 0) {
230                         ED_screens_initialize(wm);
231                         wm->initialized |= WM_INIT_WINDOW;
232                 }
233         }
234 }
235
236 void wm_clear_default_size(bContext *C)
237 {
238         wmWindowManager *wm= CTX_wm_manager(C);
239         wmWindow *win;
240         
241         /* wm context */
242         if(wm==NULL) {
243                 wm= CTX_data_main(C)->wm.first;
244                 CTX_wm_manager_set(C, wm);
245         }
246         if(wm==NULL) return;
247         if(wm->windows.first==NULL) return;
248         
249         for(win= wm->windows.first; win; win= win->next) {
250                 win->sizex = 0;
251                 win->sizey = 0;
252                 win->posx = 0;
253                 win->posy = 0;
254         }
255
256 }
257
258 /* on startup, it adds all data, for matching */
259 void wm_add_default(bContext *C)
260 {
261         wmWindowManager *wm= alloc_libblock(&CTX_data_main(C)->wm, ID_WM, "WinMan");
262         wmWindow *win;
263         bScreen *screen= CTX_wm_screen(C); /* XXX from file read hrmf */
264         
265         CTX_wm_manager_set(C, wm);
266         win= wm_window_new(C);
267         win->screen= screen;
268         screen->winid= win->winid;
269         BLI_strncpy(win->screenname, screen->id.name+2, 21);
270         
271         wm->winactive= win;
272         wm->file_saved= 1;
273         wm_window_make_drawable(C, win); 
274 }
275
276
277 /* context is allowed to be NULL, do not free wm itself (library.c) */
278 void wm_close_and_free(bContext *C, wmWindowManager *wm)
279 {
280         wmWindow *win;
281         wmOperator *op;
282         wmKeyConfig *keyconf;
283
284         if(wm->autosavetimer)
285                 wm_autosave_timer_ended(wm);
286
287         while((win= wm->windows.first)) {
288                 BLI_remlink(&wm->windows, win);
289                 win->screen= NULL; /* prevent draw clear to use screen */
290                 wm_draw_window_clear(win);
291                 wm_window_free(C, wm, win);
292         }
293         
294         while((op= wm->operators.first)) {
295                 BLI_remlink(&wm->operators, op);
296                 WM_operator_free(op);
297         }
298
299         while((keyconf=wm->keyconfigs.first)) {
300                 BLI_remlink(&wm->keyconfigs, keyconf);
301                 WM_keyconfig_free(keyconf);
302         }
303
304         BLI_freelistN(&wm->queue);
305         
306         BLI_freelistN(&wm->paintcursors);
307         BLI_freelistN(&wm->drags);
308         BKE_reports_clear(&wm->reports);
309         
310         if(C && CTX_wm_manager(C)==wm) CTX_wm_manager_set(C, NULL);
311 }
312
313 void wm_close_and_free_all(bContext *C, ListBase *wmlist)
314 {
315         wmWindowManager *wm;
316         
317         while((wm=wmlist->first)) {
318                 wm_close_and_free(C, wm);
319                 BLI_remlink(wmlist, wm);
320                 MEM_freeN(wm);
321         }
322 }
323
324 void WM_main(bContext *C)
325 {
326         while(1) {
327                 
328                 /* get events from ghost, handle window events, add to window queues */
329                 wm_window_process_events(C); 
330                 
331                 /* per window, all events to the window, screen, area and region handlers */
332                 wm_event_do_handlers(C);
333                 
334                 /* events have left notes about changes, we handle and cache it */
335                 wm_event_do_notifiers(C);
336                 
337                 /* execute cached changes draw */
338                 wm_draw_update(C);
339         }
340 }
341
342