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