Merging trunk up to revision 41245.
[blender.git] / source / blender / windowmanager / intern / wm.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version. 
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2007 Blender Foundation.
19  * All rights reserved.
20  *
21  * 
22  * Contributor(s): Blender Foundation
23  *
24  * ***** END GPL LICENSE BLOCK *****
25  */
26
27 /** \file blender/windowmanager/intern/wm.c
28  *  \ingroup wm
29  */
30
31
32 #include <string.h>
33 #include <stddef.h>
34
35 #include "BLO_sys_types.h"
36
37 #include "DNA_windowmanager_types.h"
38
39 #include "GHOST_C-api.h"
40
41 #include "MEM_guardedalloc.h"
42
43 #include "BLI_utildefines.h"
44 #include "BLI_blenlib.h"
45 #include "BLI_ghash.h"
46
47 #include "BKE_blender.h"
48 #include "BKE_context.h"
49 #include "BKE_idprop.h"
50 #include "BKE_library.h"
51 #include "BKE_main.h"
52 #include "BKE_screen.h"
53 #include "BKE_report.h"
54 #include "BKE_global.h"
55
56 #include "WM_api.h"
57 #include "WM_types.h"
58 #include "wm_window.h"
59 #include "wm_event_system.h"
60 #include "wm_event_types.h"
61 #include "wm_draw.h"
62 #include "wm.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 GHash *menutypes_hash= NULL;
155
156 MenuType *WM_menutype_find(const char *idname, int quiet)
157 {
158         MenuType* mt;
159
160         if (idname[0]) {
161                 mt= BLI_ghash_lookup(menutypes_hash, 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_ghash_insert(menutypes_hash, (void *)mt->idname, mt);
175         return 1;
176 }
177
178 /* inefficient but only used for tooltip code */
179 int WM_menutype_contains(MenuType* mt)
180 {
181         int found= FALSE;
182
183         if(mt) {
184                 GHashIterator *iter= BLI_ghashIterator_new(menutypes_hash);
185
186                 for( ; !BLI_ghashIterator_isDone(iter); BLI_ghashIterator_step(iter)) {
187                         if(mt == BLI_ghashIterator_getValue(iter)) {
188                                 found= TRUE;
189                                 break;
190                         }
191                 }
192                 BLI_ghashIterator_free(iter);
193         }
194
195         return found;
196 }
197
198 void WM_menutype_freelink(MenuType* mt)
199 {
200         BLI_ghash_remove(menutypes_hash, mt->idname, NULL, (GHashValFreeFP)MEM_freeN);
201 }
202
203 /* called on initialize WM_init() */
204 void WM_menutype_init(void)
205 {
206         menutypes_hash= BLI_ghash_new(BLI_ghashutil_strhash, BLI_ghashutil_strcmp, "menutypes_hash gh");
207 }
208
209 void WM_menutype_free(void)
210 {
211         GHashIterator *iter= BLI_ghashIterator_new(menutypes_hash);
212
213         for( ; !BLI_ghashIterator_isDone(iter); BLI_ghashIterator_step(iter)) {
214                 MenuType *mt= BLI_ghashIterator_getValue(iter);
215                 if(mt->ext.free) {
216                         mt->ext.free(mt->ext.data);
217                 }
218         }
219         BLI_ghashIterator_free(iter);
220
221         BLI_ghash_free(menutypes_hash, NULL, (GHashValFreeFP)MEM_freeN);
222         menutypes_hash= NULL;
223 }
224
225 /* ****************************************** */
226
227 void WM_keymap_init(bContext *C)
228 {
229         wmWindowManager *wm= CTX_wm_manager(C);
230
231         /* create standard key configs */
232         if(!wm->defaultconf)
233                 wm->defaultconf= WM_keyconfig_new(wm, "Blender");
234         if(!wm->addonconf)
235                 wm->addonconf= WM_keyconfig_new(wm, "Blender Addon");
236         if(!wm->userconf)
237                 wm->userconf= WM_keyconfig_new(wm, "Blender User");
238         
239         /* initialize only after python init is done, for keymaps that
240            use python operators */
241         if(CTX_py_init_get(C) && (wm->initialized & WM_INIT_KEYMAP) == 0) {
242                 /* create default key config, only initialize once,
243                    it's persistent across sessions */
244                 if(!(wm->defaultconf->flag & KEYCONF_INIT_DEFAULT)) {
245                         wm_window_keymap(wm->defaultconf);
246                         ED_spacetypes_keymap(wm->defaultconf);
247
248                         wm->defaultconf->flag |= KEYCONF_INIT_DEFAULT;
249                 }
250
251                 WM_keyconfig_update_tag(NULL, NULL);
252                 WM_keyconfig_update(wm);
253
254                 wm->initialized |= WM_INIT_KEYMAP;
255         }
256 }
257
258 void WM_check(bContext *C)
259 {
260         wmWindowManager *wm= CTX_wm_manager(C);
261         
262         /* wm context */
263         if(wm==NULL) {
264                 wm= CTX_data_main(C)->wm.first;
265                 CTX_wm_manager_set(C, wm);
266         }
267         if(wm==NULL) return;
268         if(wm->windows.first==NULL) return;
269
270         if (!G.background) {
271                 /* case: fileread */
272                 if((wm->initialized & WM_INIT_WINDOW) == 0) {
273                         WM_keymap_init(C);
274                         WM_autosave_init(wm);
275                 }
276
277                 /* case: no open windows at all, for old file reads */
278                 wm_window_add_ghostwindows(C, wm);
279         }
280
281         /* case: fileread */
282         /* note: this runs in bg mode to set the screen context cb */
283         if((wm->initialized & WM_INIT_WINDOW) == 0) {
284                 ED_screens_initialize(wm);
285                 wm->initialized |= WM_INIT_WINDOW;
286         }
287 }
288
289 void wm_clear_default_size(bContext *C)
290 {
291         wmWindowManager *wm= CTX_wm_manager(C);
292         wmWindow *win;
293         
294         /* wm context */
295         if(wm==NULL) {
296                 wm= CTX_data_main(C)->wm.first;
297                 CTX_wm_manager_set(C, wm);
298         }
299         if(wm==NULL) return;
300         if(wm->windows.first==NULL) return;
301         
302         for(win= wm->windows.first; win; win= win->next) {
303                 win->sizex = 0;
304                 win->sizey = 0;
305                 win->posx = 0;
306                 win->posy = 0;
307         }
308
309 }
310
311 /* on startup, it adds all data, for matching */
312 void wm_add_default(bContext *C)
313 {
314         wmWindowManager *wm= alloc_libblock(&CTX_data_main(C)->wm, ID_WM, "WinMan");
315         wmWindow *win;
316         bScreen *screen= CTX_wm_screen(C); /* XXX from file read hrmf */
317         
318         CTX_wm_manager_set(C, wm);
319         win= wm_window_new(C);
320         win->screen= screen;
321         screen->winid= win->winid;
322         BLI_strncpy(win->screenname, screen->id.name+2, sizeof(win->screenname));
323         
324         wm->winactive= win;
325         wm->file_saved= 1;
326         wm_window_make_drawable(C, win); 
327 }
328
329
330 /* context is allowed to be NULL, do not free wm itself (library.c) */
331 void wm_close_and_free(bContext *C, wmWindowManager *wm)
332 {
333         wmWindow *win;
334         wmOperator *op;
335         wmKeyConfig *keyconf;
336
337         if(wm->autosavetimer)
338                 wm_autosave_timer_ended(wm);
339
340         while((win= wm->windows.first)) {
341                 BLI_remlink(&wm->windows, win);
342                 win->screen= NULL; /* prevent draw clear to use screen */
343                 wm_draw_window_clear(win);
344                 wm_window_free(C, wm, win);
345         }
346         
347         while((op= wm->operators.first)) {
348                 BLI_remlink(&wm->operators, op);
349                 WM_operator_free(op);
350         }
351
352         while((keyconf=wm->keyconfigs.first)) {
353                 BLI_remlink(&wm->keyconfigs, keyconf);
354                 WM_keyconfig_free(keyconf);
355         }
356
357         BLI_freelistN(&wm->queue);
358         
359         BLI_freelistN(&wm->paintcursors);
360         BLI_freelistN(&wm->drags);
361         
362         wm_reports_free(wm);
363         
364         if(C && CTX_wm_manager(C)==wm) CTX_wm_manager_set(C, NULL);
365 }
366
367 void wm_close_and_free_all(bContext *C, ListBase *wmlist)
368 {
369         wmWindowManager *wm;
370         
371         while((wm=wmlist->first)) {
372                 wm_close_and_free(C, wm);
373                 BLI_remlink(wmlist, wm);
374                 MEM_freeN(wm);
375         }
376 }
377
378 void WM_main(bContext *C)
379 {
380         while(1) {
381                 
382                 /* get events from ghost, handle window events, add to window queues */
383                 wm_window_process_events(C); 
384                 
385                 /* per window, all events to the window, screen, area and region handlers */
386                 wm_event_do_handlers(C);
387                 
388                 /* events have left notes about changes, we handle and cache it */
389                 wm_event_do_notifiers(C);
390                 
391                 /* execute cached changes draw */
392                 wm_draw_update(C);
393         }
394 }
395
396