Merged trunk up to rev41928
[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 void WM_menutype_freelink(MenuType* mt)
179 {
180         BLI_ghash_remove(menutypes_hash, mt->idname, NULL, (GHashValFreeFP)MEM_freeN);
181 }
182
183 /* called on initialize WM_init() */
184 void WM_menutype_init(void)
185 {
186         menutypes_hash= BLI_ghash_new(BLI_ghashutil_strhash, BLI_ghashutil_strcmp, "menutypes_hash gh");
187 }
188
189 void WM_menutype_free(void)
190 {
191         GHashIterator *iter= BLI_ghashIterator_new(menutypes_hash);
192
193         for( ; !BLI_ghashIterator_isDone(iter); BLI_ghashIterator_step(iter)) {
194                 MenuType *mt= BLI_ghashIterator_getValue(iter);
195                 if(mt->ext.free) {
196                         mt->ext.free(mt->ext.data);
197                 }
198         }
199         BLI_ghashIterator_free(iter);
200
201         BLI_ghash_free(menutypes_hash, NULL, (GHashValFreeFP)MEM_freeN);
202         menutypes_hash= NULL;
203 }
204
205 /* ****************************************** */
206
207 void WM_keymap_init(bContext *C)
208 {
209         wmWindowManager *wm= CTX_wm_manager(C);
210
211         /* create standard key configs */
212         if(!wm->defaultconf)
213                 wm->defaultconf= WM_keyconfig_new(wm, "Blender");
214         if(!wm->addonconf)
215                 wm->addonconf= WM_keyconfig_new(wm, "Blender Addon");
216         if(!wm->userconf)
217                 wm->userconf= WM_keyconfig_new(wm, "Blender User");
218         
219         /* initialize only after python init is done, for keymaps that
220            use python operators */
221         if(CTX_py_init_get(C) && (wm->initialized & WM_INIT_KEYMAP) == 0) {
222                 /* create default key config, only initialize once,
223                    it's persistent across sessions */
224                 if(!(wm->defaultconf->flag & KEYCONF_INIT_DEFAULT)) {
225                         wm_window_keymap(wm->defaultconf);
226                         ED_spacetypes_keymap(wm->defaultconf);
227
228                         wm->defaultconf->flag |= KEYCONF_INIT_DEFAULT;
229                 }
230
231                 WM_keyconfig_update_tag(NULL, NULL);
232                 WM_keyconfig_update(wm);
233
234                 wm->initialized |= WM_INIT_KEYMAP;
235         }
236 }
237
238 void WM_check(bContext *C)
239 {
240         wmWindowManager *wm= CTX_wm_manager(C);
241         
242         /* wm context */
243         if(wm==NULL) {
244                 wm= CTX_data_main(C)->wm.first;
245                 CTX_wm_manager_set(C, wm);
246         }
247         if(wm==NULL) return;
248         if(wm->windows.first==NULL) return;
249
250         if (!G.background) {
251                 /* case: fileread */
252                 if((wm->initialized & WM_INIT_WINDOW) == 0) {
253                         WM_keymap_init(C);
254                         WM_autosave_init(wm);
255                 }
256
257                 /* case: no open windows at all, for old file reads */
258                 wm_window_add_ghostwindows(C, wm);
259         }
260
261         /* case: fileread */
262         /* note: this runs in bg mode to set the screen context cb */
263         if((wm->initialized & WM_INIT_WINDOW) == 0) {
264                 ED_screens_initialize(wm);
265                 wm->initialized |= WM_INIT_WINDOW;
266         }
267 }
268
269 void wm_clear_default_size(bContext *C)
270 {
271         wmWindowManager *wm= CTX_wm_manager(C);
272         wmWindow *win;
273         
274         /* wm context */
275         if(wm==NULL) {
276                 wm= CTX_data_main(C)->wm.first;
277                 CTX_wm_manager_set(C, wm);
278         }
279         if(wm==NULL) return;
280         if(wm->windows.first==NULL) return;
281         
282         for(win= wm->windows.first; win; win= win->next) {
283                 win->sizex = 0;
284                 win->sizey = 0;
285                 win->posx = 0;
286                 win->posy = 0;
287         }
288
289 }
290
291 /* on startup, it adds all data, for matching */
292 void wm_add_default(bContext *C)
293 {
294         wmWindowManager *wm= alloc_libblock(&CTX_data_main(C)->wm, ID_WM, "WinMan");
295         wmWindow *win;
296         bScreen *screen= CTX_wm_screen(C); /* XXX from file read hrmf */
297         
298         CTX_wm_manager_set(C, wm);
299         win= wm_window_new(C);
300         win->screen= screen;
301         screen->winid= win->winid;
302         BLI_strncpy(win->screenname, screen->id.name+2, sizeof(win->screenname));
303         
304         wm->winactive= win;
305         wm->file_saved= 1;
306         wm_window_make_drawable(C, win); 
307 }
308
309
310 /* context is allowed to be NULL, do not free wm itself (library.c) */
311 void wm_close_and_free(bContext *C, wmWindowManager *wm)
312 {
313         wmWindow *win;
314         wmOperator *op;
315         wmKeyConfig *keyconf;
316
317         if(wm->autosavetimer)
318                 wm_autosave_timer_ended(wm);
319
320         while((win= wm->windows.first)) {
321                 BLI_remlink(&wm->windows, win);
322                 win->screen= NULL; /* prevent draw clear to use screen */
323                 wm_draw_window_clear(win);
324                 wm_window_free(C, wm, win);
325         }
326         
327         while((op= wm->operators.first)) {
328                 BLI_remlink(&wm->operators, op);
329                 WM_operator_free(op);
330         }
331
332         while((keyconf=wm->keyconfigs.first)) {
333                 BLI_remlink(&wm->keyconfigs, keyconf);
334                 WM_keyconfig_free(keyconf);
335         }
336
337         BLI_freelistN(&wm->queue);
338         
339         BLI_freelistN(&wm->paintcursors);
340         BLI_freelistN(&wm->drags);
341         
342         wm_reports_free(wm);
343         
344         if(C && CTX_wm_manager(C)==wm) CTX_wm_manager_set(C, NULL);
345 }
346
347 void wm_close_and_free_all(bContext *C, ListBase *wmlist)
348 {
349         wmWindowManager *wm;
350         
351         while((wm=wmlist->first)) {
352                 wm_close_and_free(C, wm);
353                 BLI_remlink(wmlist, wm);
354                 MEM_freeN(wm);
355         }
356 }
357
358 void WM_main(bContext *C)
359 {
360         while(1) {
361                 
362                 /* get events from ghost, handle window events, add to window queues */
363                 wm_window_process_events(C); 
364                 
365                 /* per window, all events to the window, screen, area and region handlers */
366                 wm_event_do_handlers(C);
367                 
368                 /* events have left notes about changes, we handle and cache it */
369                 wm_event_do_notifiers(C);
370                 
371                 /* execute cached changes draw */
372                 wm_draw_update(C);
373         }
374 }
375
376