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