merge with trunk r38787
[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         
214         if(wm && CTX_py_init_get(C) && (wm->initialized & WM_INIT_KEYMAP) == 0) {
215                 /* create default key config */
216                 wm_window_keymap(wm->defaultconf);
217                 ED_spacetypes_keymap(wm->defaultconf);
218                 WM_keyconfig_userdef();
219
220                 wm->initialized |= WM_INIT_KEYMAP;
221         }
222 }
223
224 void WM_check(bContext *C)
225 {
226         wmWindowManager *wm= CTX_wm_manager(C);
227         
228         /* wm context */
229         if(wm==NULL) {
230                 wm= CTX_data_main(C)->wm.first;
231                 CTX_wm_manager_set(C, wm);
232         }
233         if(wm==NULL) return;
234         if(wm->windows.first==NULL) return;
235
236         if (!G.background) {
237                 /* case: fileread */
238                 if((wm->initialized & WM_INIT_WINDOW) == 0) {
239                         WM_keymap_init(C);
240                         WM_autosave_init(wm);
241                 }
242
243                 /* case: no open windows at all, for old file reads */
244                 wm_window_add_ghostwindows(C, wm);
245         }
246
247         /* case: fileread */
248         /* note: this runs in bg mode to set the screen context cb */
249         if((wm->initialized & WM_INIT_WINDOW) == 0) {
250                 ED_screens_initialize(wm);
251                 wm->initialized |= WM_INIT_WINDOW;
252         }
253 }
254
255 void wm_clear_default_size(bContext *C)
256 {
257         wmWindowManager *wm= CTX_wm_manager(C);
258         wmWindow *win;
259         
260         /* wm context */
261         if(wm==NULL) {
262                 wm= CTX_data_main(C)->wm.first;
263                 CTX_wm_manager_set(C, wm);
264         }
265         if(wm==NULL) return;
266         if(wm->windows.first==NULL) return;
267         
268         for(win= wm->windows.first; win; win= win->next) {
269                 win->sizex = 0;
270                 win->sizey = 0;
271                 win->posx = 0;
272                 win->posy = 0;
273         }
274
275 }
276
277 /* on startup, it adds all data, for matching */
278 void wm_add_default(bContext *C)
279 {
280         wmWindowManager *wm= alloc_libblock(&CTX_data_main(C)->wm, ID_WM, "WinMan");
281         wmWindow *win;
282         bScreen *screen= CTX_wm_screen(C); /* XXX from file read hrmf */
283         
284         CTX_wm_manager_set(C, wm);
285         win= wm_window_new(C);
286         win->screen= screen;
287         screen->winid= win->winid;
288         BLI_strncpy(win->screenname, screen->id.name+2, sizeof(win->screenname));
289         
290         wm->winactive= win;
291         wm->file_saved= 1;
292         wm_window_make_drawable(C, win); 
293 }
294
295
296 /* context is allowed to be NULL, do not free wm itself (library.c) */
297 void wm_close_and_free(bContext *C, wmWindowManager *wm)
298 {
299         wmWindow *win;
300         wmOperator *op;
301         wmKeyConfig *keyconf;
302
303         if(wm->autosavetimer)
304                 wm_autosave_timer_ended(wm);
305
306         while((win= wm->windows.first)) {
307                 BLI_remlink(&wm->windows, win);
308                 win->screen= NULL; /* prevent draw clear to use screen */
309                 wm_draw_window_clear(win);
310                 wm_window_free(C, wm, win);
311         }
312         
313         while((op= wm->operators.first)) {
314                 BLI_remlink(&wm->operators, op);
315                 WM_operator_free(op);
316         }
317
318         while((keyconf=wm->keyconfigs.first)) {
319                 BLI_remlink(&wm->keyconfigs, keyconf);
320                 WM_keyconfig_free(keyconf);
321         }
322
323         BLI_freelistN(&wm->queue);
324         
325         BLI_freelistN(&wm->paintcursors);
326         BLI_freelistN(&wm->drags);
327         
328         wm_reports_free(wm);
329         
330         if(C && CTX_wm_manager(C)==wm) CTX_wm_manager_set(C, NULL);
331 }
332
333 void wm_close_and_free_all(bContext *C, ListBase *wmlist)
334 {
335         wmWindowManager *wm;
336         
337         while((wm=wmlist->first)) {
338                 wm_close_and_free(C, wm);
339                 BLI_remlink(wmlist, wm);
340                 MEM_freeN(wm);
341         }
342 }
343
344 void WM_main(bContext *C)
345 {
346         while(1) {
347                 
348                 /* get events from ghost, handle window events, add to window queues */
349                 wm_window_process_events(C); 
350                 
351                 /* per window, all events to the window, screen, area and region handlers */
352                 wm_event_do_handlers(C);
353                 
354                 /* events have left notes about changes, we handle and cache it */
355                 wm_event_do_notifiers(C);
356                 
357                 /* execute cached changes draw */
358                 wm_draw_update(C);
359         }
360 }
361
362