4 * ***** BEGIN GPL LICENSE BLOCK *****
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.
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.
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.
20 * The Original Code is Copyright (C) 2007 Blender Foundation.
21 * All rights reserved.
24 * Contributor(s): Blender Foundation
26 * ***** END GPL LICENSE BLOCK *****
29 /** \file blender/windowmanager/intern/wm.c
37 #include "BLO_sys_types.h"
39 #include "DNA_windowmanager_types.h"
41 #include "GHOST_C-api.h"
43 #include "MEM_guardedalloc.h"
45 #include "BLI_utildefines.h"
46 #include "BLI_blenlib.h"
47 #include "BLI_ghash.h"
49 #include "BKE_blender.h"
50 #include "BKE_context.h"
51 #include "BKE_idprop.h"
52 #include "BKE_library.h"
54 #include "BKE_screen.h"
55 #include "BKE_report.h"
56 #include "BKE_global.h"
60 #include "wm_window.h"
61 #include "wm_event_system.h"
62 #include "wm_event_types.h"
66 #include "ED_screen.h"
69 #include "BPY_extern.h"
72 /* ****************************************************** */
74 #define MAX_OP_REGISTERED 32
76 void WM_operator_free(wmOperator *op)
81 /* do this first incase there are any __del__ functions or
82 * similar that use properties */
83 BPY_DECREF(op->py_instance);
88 op->properties= op->ptr->data;
93 IDP_FreeProperty(op->properties);
94 MEM_freeN(op->properties);
97 if(op->reports && (op->reports->flag & RPT_FREE)) {
98 BKE_reports_clear(op->reports);
99 MEM_freeN(op->reports);
102 if(op->macro.first) {
103 wmOperator *opm, *opmnext;
104 for(opm= op->macro.first; opm; opm= opmnext) {
106 WM_operator_free(opm);
113 static void wm_reports_free(wmWindowManager *wm)
115 BKE_reports_clear(&wm->reports);
116 WM_event_remove_timer(wm, NULL, wm->reports.reporttimer);
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)
123 wmWindowManager *wm= CTX_wm_manager(C);
126 BLI_addtail(&wm->operators, op);
127 tot= BLI_countlist(&wm->operators);
129 while(tot>MAX_OP_REGISTERED) {
130 wmOperator *opt= wm->operators.first;
131 BLI_remlink(&wm->operators, opt);
132 WM_operator_free(opt);
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);
142 void WM_operator_stack_clear(wmWindowManager *wm)
146 while((op= wm->operators.first)) {
147 BLI_remlink(&wm->operators, op);
148 WM_operator_free(op);
151 WM_main_add_notifier(NC_WM|ND_HISTORY, NULL);
154 /* ****************************************** */
156 static GHash *menutypes_hash= NULL;
158 MenuType *WM_menutype_find(const char *idname, int quiet)
163 mt= BLI_ghash_lookup(menutypes_hash, idname);
169 printf("search for unknown menutype %s\n", idname);
174 int WM_menutype_add(MenuType* mt)
176 BLI_ghash_insert(menutypes_hash, (void *)mt->idname, mt);
180 /* inefficient but only used for tooltip code */
181 int WM_menutype_contains(MenuType* mt)
186 GHashIterator *iter= BLI_ghashIterator_new(menutypes_hash);
188 for( ; !BLI_ghashIterator_isDone(iter); BLI_ghashIterator_step(iter)) {
189 if(mt == BLI_ghashIterator_getValue(iter)) {
194 BLI_ghashIterator_free(iter);
200 void WM_menutype_freelink(MenuType* mt)
202 BLI_ghash_remove(menutypes_hash, mt->idname, NULL, (GHashValFreeFP)MEM_freeN);
205 /* called on initialize WM_init() */
206 void WM_menutype_init(void)
208 menutypes_hash= BLI_ghash_new(BLI_ghashutil_strhash, BLI_ghashutil_strcmp, "menutypes_hash gh");
211 void WM_menutype_free(void)
213 GHashIterator *iter= BLI_ghashIterator_new(menutypes_hash);
215 for( ; !BLI_ghashIterator_isDone(iter); BLI_ghashIterator_step(iter)) {
216 MenuType *mt= BLI_ghashIterator_getValue(iter);
218 mt->ext.free(mt->ext.data);
221 BLI_ghashIterator_free(iter);
223 BLI_ghash_free(menutypes_hash, NULL, (GHashValFreeFP)MEM_freeN);
224 menutypes_hash= NULL;
227 /* ****************************************** */
229 void WM_keymap_init(bContext *C)
231 wmWindowManager *wm= CTX_wm_manager(C);
233 /* create standard key configs */
235 wm->defaultconf= WM_keyconfig_new(wm, "Blender");
237 wm->addonconf= WM_keyconfig_new(wm, "Blender Addon");
239 wm->userconf= WM_keyconfig_new(wm, "Blender User");
241 /* initialize only after python init is done, for keymaps that
242 use python operators */
243 if(CTX_py_init_get(C) && (wm->initialized & WM_INIT_KEYMAP) == 0) {
244 /* create default key config, only initialize once,
245 it's persistent across sessions */
246 if(!(wm->defaultconf->flag & KEYCONF_INIT_DEFAULT)) {
247 wm_window_keymap(wm->defaultconf);
248 ED_spacetypes_keymap(wm->defaultconf);
250 wm->defaultconf->flag |= KEYCONF_INIT_DEFAULT;
253 WM_keyconfig_update_tag(NULL, NULL);
254 WM_keyconfig_update(wm);
256 wm->initialized |= WM_INIT_KEYMAP;
260 void WM_check(bContext *C)
262 wmWindowManager *wm= CTX_wm_manager(C);
266 wm= CTX_data_main(C)->wm.first;
267 CTX_wm_manager_set(C, wm);
270 if(wm->windows.first==NULL) return;
274 if((wm->initialized & WM_INIT_WINDOW) == 0) {
276 WM_autosave_init(wm);
279 /* case: no open windows at all, for old file reads */
280 wm_window_add_ghostwindows(C, wm);
284 /* note: this runs in bg mode to set the screen context cb */
285 if((wm->initialized & WM_INIT_WINDOW) == 0) {
286 ED_screens_initialize(wm);
287 wm->initialized |= WM_INIT_WINDOW;
291 void wm_clear_default_size(bContext *C)
293 wmWindowManager *wm= CTX_wm_manager(C);
298 wm= CTX_data_main(C)->wm.first;
299 CTX_wm_manager_set(C, wm);
302 if(wm->windows.first==NULL) return;
304 for(win= wm->windows.first; win; win= win->next) {
313 /* on startup, it adds all data, for matching */
314 void wm_add_default(bContext *C)
316 wmWindowManager *wm= alloc_libblock(&CTX_data_main(C)->wm, ID_WM, "WinMan");
318 bScreen *screen= CTX_wm_screen(C); /* XXX from file read hrmf */
320 CTX_wm_manager_set(C, wm);
321 win= wm_window_new(C);
323 screen->winid= win->winid;
324 BLI_strncpy(win->screenname, screen->id.name+2, sizeof(win->screenname));
328 wm_window_make_drawable(C, win);
332 /* context is allowed to be NULL, do not free wm itself (library.c) */
333 void wm_close_and_free(bContext *C, wmWindowManager *wm)
337 wmKeyConfig *keyconf;
339 if(wm->autosavetimer)
340 wm_autosave_timer_ended(wm);
342 while((win= wm->windows.first)) {
343 BLI_remlink(&wm->windows, win);
344 win->screen= NULL; /* prevent draw clear to use screen */
345 wm_draw_window_clear(win);
346 wm_window_free(C, wm, win);
349 while((op= wm->operators.first)) {
350 BLI_remlink(&wm->operators, op);
351 WM_operator_free(op);
354 while((keyconf=wm->keyconfigs.first)) {
355 BLI_remlink(&wm->keyconfigs, keyconf);
356 WM_keyconfig_free(keyconf);
359 BLI_freelistN(&wm->queue);
361 BLI_freelistN(&wm->paintcursors);
362 BLI_freelistN(&wm->drags);
366 if(C && CTX_wm_manager(C)==wm) CTX_wm_manager_set(C, NULL);
369 void wm_close_and_free_all(bContext *C, ListBase *wmlist)
373 while((wm=wmlist->first)) {
374 wm_close_and_free(C, wm);
375 BLI_remlink(wmlist, wm);
380 void WM_main(bContext *C)
384 /* get events from ghost, handle window events, add to window queues */
385 wm_window_process_events(C);
387 /* per window, all events to the window, screen, area and region handlers */
388 wm_event_do_handlers(C);
390 /* events have left notes about changes, we handle and cache it */
391 wm_event_do_notifiers(C);
393 /* execute cached changes draw */