WM: Add WM_menutype_poll function
[blender-staging.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  * Internal functions for managing UI registrable types (operator, UI and menu types)
31  *
32  * Also Blenders main event loop (WM_main)
33  */
34
35 #include <string.h>
36 #include <stddef.h>
37
38 #include "BLI_sys_types.h"
39
40 #include "DNA_windowmanager_types.h"
41
42 #include "MEM_guardedalloc.h"
43
44 #include "BLI_utildefines.h"
45 #include "BLI_blenlib.h"
46 #include "BLI_ghash.h"
47
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_draw.h"
61 #include "wm.h"
62
63 #include "ED_screen.h"
64
65 #ifdef WITH_PYTHON
66 #include "BPY_extern.h"
67 #endif
68
69 /* ****************************************************** */
70
71 #define MAX_OP_REGISTERED   32
72
73 void WM_operator_free(wmOperator *op)
74 {
75
76 #ifdef WITH_PYTHON
77         if (op->py_instance) {
78                 /* do this first in case there are any __del__ functions or
79                  * similar that use properties */
80                 BPY_DECREF_RNA_INVALIDATE(op->py_instance);
81         }
82 #endif
83
84         if (op->ptr) {
85                 op->properties = op->ptr->data;
86                 MEM_freeN(op->ptr);
87         }
88
89         if (op->properties) {
90                 IDP_FreeProperty(op->properties);
91                 MEM_freeN(op->properties);
92         }
93
94         if (op->reports && (op->reports->flag & RPT_FREE)) {
95                 BKE_reports_clear(op->reports);
96                 MEM_freeN(op->reports);
97         }
98
99         if (op->macro.first) {
100                 wmOperator *opm, *opmnext;
101                 for (opm = op->macro.first; opm; opm = opmnext) {
102                         opmnext = opm->next;
103                         WM_operator_free(opm);
104                 }
105         }
106         
107         MEM_freeN(op);
108 }
109
110 void WM_operator_free_all_after(wmWindowManager *wm, struct wmOperator *op)
111 {
112         op = op->next;
113         while (op != NULL) {
114                 wmOperator *op_next = op->next;
115                 BLI_remlink(&wm->operators, op);
116                 WM_operator_free(op);
117                 op = op_next;
118         }
119 }
120
121 /**
122  * Use with extreme care!,
123  * properties, customdata etc - must be compatible.
124  *
125  * \param op  Operator to assign the type to.
126  * \param ot  OperatorType to assign.
127  */
128 void WM_operator_type_set(wmOperator *op, wmOperatorType *ot)
129 {
130         /* not supported for Python */
131         BLI_assert(op->py_instance == NULL);
132
133         op->type = ot;
134         op->ptr->type = ot->srna;
135
136         /* ensure compatible properties */
137         if (op->properties) {
138                 PointerRNA ptr;
139
140                 WM_operator_properties_create_ptr(&ptr, ot);
141
142                 WM_operator_properties_default(&ptr, false);
143
144                 if (ptr.data) {
145                         IDP_SyncGroupTypes(op->properties, ptr.data, true);
146                 }
147
148                 WM_operator_properties_free(&ptr);
149         }
150 }
151
152 static void wm_reports_free(wmWindowManager *wm)
153 {
154         BKE_reports_clear(&wm->reports);
155         WM_event_remove_timer(wm, NULL, wm->reports.reporttimer);
156 }
157
158 /* all operations get registered in the windowmanager here */
159 /* called on event handling by event_system.c */
160 void wm_operator_register(bContext *C, wmOperator *op)
161 {
162         wmWindowManager *wm = CTX_wm_manager(C);
163         int tot = 0;
164
165         BLI_addtail(&wm->operators, op);
166
167         /* only count registered operators */
168         while (op) {
169                 wmOperator *op_prev = op->prev;
170                 if (op->type->flag & OPTYPE_REGISTER) {
171                         tot += 1;
172                 }
173                 if (tot > MAX_OP_REGISTERED) {
174                         BLI_remlink(&wm->operators, op);
175                         WM_operator_free(op);
176                 }
177                 op = op_prev;
178         }
179
180         /* so the console is redrawn */
181         WM_event_add_notifier(C, NC_SPACE | ND_SPACE_INFO_REPORT, NULL);
182         WM_event_add_notifier(C, NC_WM | ND_HISTORY, NULL);
183 }
184
185
186 void WM_operator_stack_clear(wmWindowManager *wm)
187 {
188         wmOperator *op;
189         
190         while ((op = BLI_pophead(&wm->operators))) {
191                 WM_operator_free(op);
192         }
193         
194         WM_main_add_notifier(NC_WM | ND_HISTORY, NULL);
195 }
196
197 /**
198  * This function is needed in the case when an addon id disabled
199  * while a modal operator it defined is running.
200  */
201 void WM_operator_handlers_clear(wmWindowManager *wm, wmOperatorType *ot)
202 {
203         wmWindow *win;
204         for (win = wm->windows.first; win; win = win->next) {
205                 ListBase *lb[2] = {&win->handlers, &win->modalhandlers};
206                 wmEventHandler *handler;
207                 int i;
208
209                 for (i = 0; i < 2; i++) {
210                         for (handler = lb[i]->first; handler; handler = handler->next) {
211                                 if (handler->op && handler->op->type == ot) {
212                                         /* don't run op->cancel because it needs the context,
213                                          * assume whoever unregisters the operator will cleanup */
214                                         handler->flag |= WM_HANDLER_DO_FREE;
215                                         WM_operator_free(handler->op);
216                                         handler->op = NULL;
217                                 }
218                         }
219                 }
220         }
221 }
222
223 /* ************ uiListType handling ************** */
224
225 static GHash *uilisttypes_hash = NULL;
226
227 uiListType *WM_uilisttype_find(const char *idname, bool quiet)
228 {
229         uiListType *ult;
230
231         if (idname[0]) {
232                 ult = BLI_ghash_lookup(uilisttypes_hash, idname);
233                 if (ult) {
234                         return ult;
235                 }
236         }
237
238         if (!quiet) {
239                 printf("search for unknown uilisttype %s\n", idname);
240         }
241
242         return NULL;
243 }
244
245 bool WM_uilisttype_add(uiListType *ult)
246 {
247         BLI_ghash_insert(uilisttypes_hash, ult->idname, ult);
248         return 1;
249 }
250
251 void WM_uilisttype_freelink(uiListType *ult)
252 {
253         bool ok;
254
255         ok = BLI_ghash_remove(uilisttypes_hash, ult->idname, NULL, MEM_freeN);
256
257         BLI_assert(ok);
258         (void)ok;
259 }
260
261 /* called on initialize WM_init() */
262 void WM_uilisttype_init(void)
263 {
264         uilisttypes_hash = BLI_ghash_str_new_ex("uilisttypes_hash gh", 16);
265 }
266
267 void WM_uilisttype_free(void)
268 {
269         GHashIterator gh_iter;
270
271         GHASH_ITER (gh_iter, uilisttypes_hash) {
272                 uiListType *ult = BLI_ghashIterator_getValue(&gh_iter);
273                 if (ult->ext.free) {
274                         ult->ext.free(ult->ext.data);
275                 }
276         }
277
278         BLI_ghash_free(uilisttypes_hash, NULL, MEM_freeN);
279         uilisttypes_hash = NULL;
280 }
281
282 /* ************ MenuType handling ************** */
283
284 static GHash *menutypes_hash = NULL;
285
286 MenuType *WM_menutype_find(const char *idname, bool quiet)
287 {
288         MenuType *mt;
289
290         if (idname[0]) {
291                 mt = BLI_ghash_lookup(menutypes_hash, idname);
292                 if (mt)
293                         return mt;
294         }
295
296         if (!quiet)
297                 printf("search for unknown menutype %s\n", idname);
298
299         return NULL;
300 }
301
302 bool WM_menutype_add(MenuType *mt)
303 {
304         BLI_ghash_insert(menutypes_hash, mt->idname, mt);
305         return true;
306 }
307
308 void WM_menutype_freelink(MenuType *mt)
309 {
310         bool ok;
311
312         ok = BLI_ghash_remove(menutypes_hash, mt->idname, NULL, MEM_freeN);
313
314         BLI_assert(ok);
315         (void)ok;
316 }
317
318 /* called on initialize WM_init() */
319 void WM_menutype_init(void)
320 {
321         /* reserve size is set based on blender default setup */
322         menutypes_hash = BLI_ghash_str_new_ex("menutypes_hash gh", 512);
323 }
324
325 void WM_menutype_free(void)
326 {
327         GHashIterator gh_iter;
328
329         GHASH_ITER (gh_iter, menutypes_hash) {
330                 MenuType *mt = BLI_ghashIterator_getValue(&gh_iter);
331                 if (mt->ext.free) {
332                         mt->ext.free(mt->ext.data);
333                 }
334         }
335
336         BLI_ghash_free(menutypes_hash, NULL, MEM_freeN);
337         menutypes_hash = NULL;
338 }
339
340 bool WM_menutype_poll(bContext *C, MenuType *mt)
341 {
342         if (mt->poll != NULL) {
343                 return mt->poll(C, mt);
344         }
345         return true;
346 }
347
348 /* ****************************************** */
349
350 void WM_keymap_init(bContext *C)
351 {
352         wmWindowManager *wm = CTX_wm_manager(C);
353
354         /* create standard key configs */
355         if (!wm->defaultconf)
356                 wm->defaultconf = WM_keyconfig_new(wm, "Blender");
357         if (!wm->addonconf)
358                 wm->addonconf = WM_keyconfig_new(wm, "Blender Addon");
359         if (!wm->userconf)
360                 wm->userconf = WM_keyconfig_new(wm, "Blender User");
361         
362         /* initialize only after python init is done, for keymaps that
363          * use python operators */
364         if (CTX_py_init_get(C) && (wm->initialized & WM_INIT_KEYMAP) == 0) {
365                 /* create default key config, only initialize once,
366                  * it's persistent across sessions */
367                 if (!(wm->defaultconf->flag & KEYCONF_INIT_DEFAULT)) {
368                         wm_window_keymap(wm->defaultconf);
369                         ED_spacetypes_keymap(wm->defaultconf);
370
371                         wm->defaultconf->flag |= KEYCONF_INIT_DEFAULT;
372                 }
373
374                 WM_keyconfig_update_tag(NULL, NULL);
375                 WM_keyconfig_update(wm);
376
377                 wm->initialized |= WM_INIT_KEYMAP;
378         }
379 }
380
381 void WM_check(bContext *C)
382 {
383         wmWindowManager *wm = CTX_wm_manager(C);
384         
385         /* wm context */
386         if (wm == NULL) {
387                 wm = CTX_data_main(C)->wm.first;
388                 CTX_wm_manager_set(C, wm);
389         }
390
391         if (wm == NULL || BLI_listbase_is_empty(&wm->windows)) {
392                 return;
393         }
394
395         if (!G.background) {
396                 /* case: fileread */
397                 if ((wm->initialized & WM_INIT_WINDOW) == 0) {
398                         WM_keymap_init(C);
399                         WM_autosave_init(wm);
400                 }
401
402                 /* case: no open windows at all, for old file reads */
403                 wm_window_ghostwindows_ensure(wm);
404         }
405
406         /* case: fileread */
407         /* note: this runs in bg mode to set the screen context cb */
408         if ((wm->initialized & WM_INIT_WINDOW) == 0) {
409                 ED_screens_initialize(wm);
410                 wm->initialized |= WM_INIT_WINDOW;
411         }
412 }
413
414 void wm_clear_default_size(bContext *C)
415 {
416         wmWindowManager *wm = CTX_wm_manager(C);
417         wmWindow *win;
418         
419         /* wm context */
420         if (wm == NULL) {
421                 wm = CTX_data_main(C)->wm.first;
422                 CTX_wm_manager_set(C, wm);
423         }
424
425         if (wm == NULL || BLI_listbase_is_empty(&wm->windows)) {
426                 return;
427         }
428         
429         for (win = wm->windows.first; win; win = win->next) {
430                 win->sizex = 0;
431                 win->sizey = 0;
432                 win->posx = 0;
433                 win->posy = 0;
434         }
435
436 }
437
438 /* on startup, it adds all data, for matching */
439 void wm_add_default(bContext *C)
440 {
441         wmWindowManager *wm = BKE_libblock_alloc(CTX_data_main(C), ID_WM, "WinMan", 0);
442         wmWindow *win;
443         bScreen *screen = CTX_wm_screen(C); /* XXX from file read hrmf */
444         
445         CTX_wm_manager_set(C, wm);
446         win = wm_window_new(C);
447         win->screen = screen;
448         screen->winid = win->winid;
449         BLI_strncpy(win->screenname, screen->id.name + 2, sizeof(win->screenname));
450         
451         wm->winactive = win;
452         wm->file_saved = 1;
453         wm_window_make_drawable(wm, win); 
454 }
455
456
457 /* context is allowed to be NULL, do not free wm itself (library.c) */
458 void wm_close_and_free(bContext *C, wmWindowManager *wm)
459 {
460         wmWindow *win;
461         wmOperator *op;
462         wmKeyConfig *keyconf;
463
464         if (wm->autosavetimer)
465                 wm_autosave_timer_ended(wm);
466
467         while ((win = BLI_pophead(&wm->windows))) {
468                 win->screen = NULL; /* prevent draw clear to use screen */
469                 wm_draw_window_clear(win);
470                 wm_window_free(C, wm, win);
471         }
472         
473         while ((op = BLI_pophead(&wm->operators))) {
474                 WM_operator_free(op);
475         }
476
477         while ((keyconf = BLI_pophead(&wm->keyconfigs))) {
478                 WM_keyconfig_free(keyconf);
479         }
480
481         BLI_freelistN(&wm->queue);
482         
483         BLI_freelistN(&wm->paintcursors);
484
485         WM_drag_free_list(&wm->drags);
486         
487         wm_reports_free(wm);
488         
489         if (C && CTX_wm_manager(C) == wm) CTX_wm_manager_set(C, NULL);
490 }
491
492 void wm_close_and_free_all(bContext *C, ListBase *wmlist)
493 {
494         wmWindowManager *wm;
495
496         while ((wm = wmlist->first)) {
497                 wm_close_and_free(C, wm);
498                 BLI_remlink(wmlist, wm);
499                 BKE_libblock_free_data(&wm->id, true);
500                 MEM_freeN(wm);
501         }
502 }
503
504 void WM_main(bContext *C)
505 {
506         /* Single refresh before handling events.
507          * This ensures we don't run operators before the depsgraph has been evaluated. */
508         wm_event_do_refresh_wm_and_depsgraph(C);
509
510         while (1) {
511                 
512                 /* get events from ghost, handle window events, add to window queues */
513                 wm_window_process_events(C); 
514                 
515                 /* per window, all events to the window, screen, area and region handlers */
516                 wm_event_do_handlers(C);
517                 
518                 /* events have left notes about changes, we handle and cache it */
519                 wm_event_do_notifiers(C);
520                 
521                 /* execute cached changes draw */
522                 wm_draw_update(C);
523         }
524 }
525
526