Merging trunk up to r39145.
[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(wmWindowManager *wm)
141 {
142         wmOperator *op;
143         
144         while((op= wm->operators.first)) {
145                 BLI_remlink(&wm->operators, op);
146                 WM_operator_free(op);
147         }
148         
149         WM_main_add_notifier(NC_WM|ND_HISTORY, NULL);
150 }
151
152 /* ****************************************** */
153
154 static ListBase menutypes = {NULL, NULL}; /* global menutype list */
155
156 MenuType *WM_menutype_find(const char *idname, int quiet)
157 {
158         MenuType* mt;
159
160         if (idname[0]) {
161                 mt= BLI_findstring(&menutypes, idname, offsetof(MenuType, idname));
162                 if(mt)
163                         return mt;
164         }
165
166         if(!quiet)
167                 printf("search for unknown menutype %s\n", idname);
168
169         return NULL;
170 }
171
172 int WM_menutype_add(MenuType* mt)
173 {
174         BLI_addtail(&menutypes, mt);
175         return 1;
176 }
177
178 /* inefficient but only used for tooltip code */
179 int WM_menutype_contains(MenuType* mt)
180 {
181         return (mt != NULL && BLI_findindex(&menutypes, mt) != -1);
182 }
183
184 void WM_menutype_freelink(MenuType* mt)
185 {
186         BLI_freelinkN(&menutypes, mt);
187 }
188
189 void WM_menutype_free(void)
190 {
191         MenuType* mt= menutypes.first, *mt_next;
192
193         while(mt) {
194                 mt_next= mt->next;
195
196                 if(mt->ext.free)
197                         mt->ext.free(mt->ext.data);
198
199                 WM_menutype_freelink(mt);
200
201                 mt= mt_next;
202         }
203 }
204
205 /* ****************************************** */
206
207 void WM_keymap_init(bContext *C)
208 {
209         wmWindowManager *wm= CTX_wm_manager(C);
210
211         if(!wm->defaultconf)
212                 wm->defaultconf= WM_keyconfig_new(wm, "Blender");
213         if(!wm->addonconf)
214                 wm->addonconf= WM_keyconfig_new(wm, "Blender Addon");
215         if(!wm->userconf)
216                 wm->userconf= WM_keyconfig_new(wm, "Blender User");
217         
218         if(CTX_py_init_get(C) && (wm->initialized & WM_INIT_KEYMAP) == 0) {
219                 /* create default key config */
220                 wm_window_keymap(wm->defaultconf);
221                 ED_spacetypes_keymap(wm->defaultconf);
222
223                 WM_keyconfig_update_tag(NULL, NULL);
224                 WM_keyconfig_update(wm);
225
226                 wm->initialized |= WM_INIT_KEYMAP;
227         }
228 }
229
230 void WM_check(bContext *C)
231 {
232         wmWindowManager *wm= CTX_wm_manager(C);
233         
234         /* wm context */
235         if(wm==NULL) {
236                 wm= CTX_data_main(C)->wm.first;
237                 CTX_wm_manager_set(C, wm);
238         }
239         if(wm==NULL) return;
240         if(wm->windows.first==NULL) return;
241
242         if (!G.background) {
243                 /* case: fileread */
244                 if((wm->initialized & WM_INIT_WINDOW) == 0) {
245                         WM_keymap_init(C);
246                         WM_autosave_init(wm);
247                 }
248
249                 /* case: no open windows at all, for old file reads */
250                 wm_window_add_ghostwindows(C, wm);
251         }
252
253         /* case: fileread */
254         /* note: this runs in bg mode to set the screen context cb */
255         if((wm->initialized & WM_INIT_WINDOW) == 0) {
256                 ED_screens_initialize(wm);
257                 wm->initialized |= WM_INIT_WINDOW;
258         }
259 }
260
261 void wm_clear_default_size(bContext *C)
262 {
263         wmWindowManager *wm= CTX_wm_manager(C);
264         wmWindow *win;
265         
266         /* wm context */
267         if(wm==NULL) {
268                 wm= CTX_data_main(C)->wm.first;
269                 CTX_wm_manager_set(C, wm);
270         }
271         if(wm==NULL) return;
272         if(wm->windows.first==NULL) return;
273         
274         for(win= wm->windows.first; win; win= win->next) {
275                 win->sizex = 0;
276                 win->sizey = 0;
277                 win->posx = 0;
278                 win->posy = 0;
279         }
280
281 }
282
283 /* on startup, it adds all data, for matching */
284 void wm_add_default(bContext *C)
285 {
286         wmWindowManager *wm= alloc_libblock(&CTX_data_main(C)->wm, ID_WM, "WinMan");
287         wmWindow *win;
288         bScreen *screen= CTX_wm_screen(C); /* XXX from file read hrmf */
289         
290         CTX_wm_manager_set(C, wm);
291         win= wm_window_new(C);
292         win->screen= screen;
293         screen->winid= win->winid;
294         BLI_strncpy(win->screenname, screen->id.name+2, sizeof(win->screenname));
295         
296         wm->winactive= win;
297         wm->file_saved= 1;
298         wm_window_make_drawable(C, win); 
299 }
300
301
302 /* context is allowed to be NULL, do not free wm itself (library.c) */
303 void wm_close_and_free(bContext *C, wmWindowManager *wm)
304 {
305         wmWindow *win;
306         wmOperator *op;
307         wmKeyConfig *keyconf;
308
309         if(wm->autosavetimer)
310                 wm_autosave_timer_ended(wm);
311
312         while((win= wm->windows.first)) {
313                 BLI_remlink(&wm->windows, win);
314                 win->screen= NULL; /* prevent draw clear to use screen */
315                 wm_draw_window_clear(win);
316                 wm_window_free(C, wm, win);
317         }
318         
319         while((op= wm->operators.first)) {
320                 BLI_remlink(&wm->operators, op);
321                 WM_operator_free(op);
322         }
323
324         while((keyconf=wm->keyconfigs.first)) {
325                 BLI_remlink(&wm->keyconfigs, keyconf);
326                 WM_keyconfig_free(keyconf);
327         }
328
329         BLI_freelistN(&wm->queue);
330         
331         BLI_freelistN(&wm->paintcursors);
332         BLI_freelistN(&wm->drags);
333         
334         wm_reports_free(wm);
335         
336         if(C && CTX_wm_manager(C)==wm) CTX_wm_manager_set(C, NULL);
337 }
338
339 void wm_close_and_free_all(bContext *C, ListBase *wmlist)
340 {
341         wmWindowManager *wm;
342         
343         while((wm=wmlist->first)) {
344                 wm_close_and_free(C, wm);
345                 BLI_remlink(wmlist, wm);
346                 MEM_freeN(wm);
347         }
348 }
349
350 void WM_main(bContext *C)
351 {
352         while(1) {
353                 
354                 /* get events from ghost, handle window events, add to window queues */
355                 wm_window_process_events(C); 
356                 
357                 /* per window, all events to the window, screen, area and region handlers */
358                 wm_event_do_handlers(C);
359                 
360                 /* events have left notes about changes, we handle and cache it */
361                 wm_event_do_notifiers(C);
362                 
363                 /* execute cached changes draw */
364                 wm_draw_update(C);
365         }
366 }
367
368