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