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