2 * ***** BEGIN GPL LICENSE BLOCK *****
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.
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.
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.
18 * The Original Code is Copyright (C) 2007 Blender Foundation.
19 * All rights reserved.
22 * Contributor(s): Blender Foundation
24 * ***** END GPL LICENSE BLOCK *****
27 /** \file blender/windowmanager/intern/wm.c
35 #include "BLO_sys_types.h"
37 #include "DNA_windowmanager_types.h"
39 #include "GHOST_C-api.h"
41 #include "MEM_guardedalloc.h"
43 #include "BLI_utildefines.h"
44 #include "BLI_blenlib.h"
45 #include "BLI_ghash.h"
47 #include "BKE_blender.h"
48 #include "BKE_context.h"
49 #include "BKE_idprop.h"
50 #include "BKE_library.h"
52 #include "BKE_screen.h"
53 #include "BKE_report.h"
54 #include "BKE_global.h"
58 #include "wm_window.h"
59 #include "wm_event_system.h"
60 #include "wm_event_types.h"
64 #include "ED_screen.h"
67 #include "BPY_extern.h"
70 /* ****************************************************** */
72 #define MAX_OP_REGISTERED 32
74 void WM_operator_free(wmOperator *op)
78 if (op->py_instance) {
79 /* do this first in case there are any __del__ functions or
80 * similar that use properties */
81 BPY_DECREF(op->py_instance);
86 op->properties = op->ptr->data;
91 IDP_FreeProperty(op->properties);
92 MEM_freeN(op->properties);
95 if (op->reports && (op->reports->flag & RPT_FREE)) {
96 BKE_reports_clear(op->reports);
97 MEM_freeN(op->reports);
100 if (op->macro.first) {
101 wmOperator *opm, *opmnext;
102 for (opm = op->macro.first; opm; opm = opmnext) {
104 WM_operator_free(opm);
111 static void wm_reports_free(wmWindowManager *wm)
113 BKE_reports_clear(&wm->reports);
114 WM_event_remove_timer(wm, NULL, wm->reports.reporttimer);
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)
121 wmWindowManager *wm = CTX_wm_manager(C);
124 BLI_addtail(&wm->operators, op);
125 tot = BLI_countlist(&wm->operators);
127 while (tot > MAX_OP_REGISTERED) {
128 wmOperator *opt = wm->operators.first;
129 BLI_remlink(&wm->operators, opt);
130 WM_operator_free(opt);
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);
140 void WM_operator_stack_clear(wmWindowManager *wm)
144 while ((op = wm->operators.first)) {
145 BLI_remlink(&wm->operators, op);
146 WM_operator_free(op);
149 WM_main_add_notifier(NC_WM | ND_HISTORY, NULL);
152 /* ****************************************** */
154 static GHash *menutypes_hash = NULL;
156 MenuType *WM_menutype_find(const char *idname, int quiet)
161 mt = BLI_ghash_lookup(menutypes_hash, idname);
167 printf("search for unknown menutype %s\n", idname);
172 int WM_menutype_add(MenuType *mt)
174 BLI_ghash_insert(menutypes_hash, (void *)mt->idname, mt);
178 void WM_menutype_freelink(MenuType *mt)
180 BLI_ghash_remove(menutypes_hash, mt->idname, NULL, (GHashValFreeFP)MEM_freeN);
183 /* called on initialize WM_init() */
184 void WM_menutype_init(void)
186 menutypes_hash = BLI_ghash_new(BLI_ghashutil_strhash, BLI_ghashutil_strcmp, "menutypes_hash gh");
189 void WM_menutype_free(void)
191 GHashIterator *iter = BLI_ghashIterator_new(menutypes_hash);
193 for (; !BLI_ghashIterator_isDone(iter); BLI_ghashIterator_step(iter)) {
194 MenuType *mt = BLI_ghashIterator_getValue(iter);
196 mt->ext.free(mt->ext.data);
199 BLI_ghashIterator_free(iter);
201 BLI_ghash_free(menutypes_hash, NULL, (GHashValFreeFP)MEM_freeN);
202 menutypes_hash = NULL;
205 /* ****************************************** */
207 void WM_keymap_init(bContext *C)
209 wmWindowManager *wm = CTX_wm_manager(C);
211 /* create standard key configs */
212 if (!wm->defaultconf)
213 wm->defaultconf = WM_keyconfig_new(wm, "Blender");
215 wm->addonconf = WM_keyconfig_new(wm, "Blender Addon");
217 wm->userconf = WM_keyconfig_new(wm, "Blender User");
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);
228 wm->defaultconf->flag |= KEYCONF_INIT_DEFAULT;
231 WM_keyconfig_update_tag(NULL, NULL);
232 WM_keyconfig_update(wm);
234 wm->initialized |= WM_INIT_KEYMAP;
238 void WM_check(bContext *C)
240 wmWindowManager *wm = CTX_wm_manager(C);
244 wm = CTX_data_main(C)->wm.first;
245 CTX_wm_manager_set(C, wm);
247 if (wm == NULL) return;
248 if (wm->windows.first == NULL) return;
252 if ((wm->initialized & WM_INIT_WINDOW) == 0) {
254 WM_autosave_init(wm);
257 /* case: no open windows at all, for old file reads */
258 wm_window_add_ghostwindows(wm);
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;
269 void wm_clear_default_size(bContext *C)
271 wmWindowManager *wm = CTX_wm_manager(C);
276 wm = CTX_data_main(C)->wm.first;
277 CTX_wm_manager_set(C, wm);
279 if (wm == NULL) return;
280 if (wm->windows.first == NULL) return;
282 for (win = wm->windows.first; win; win = win->next) {
291 /* on startup, it adds all data, for matching */
292 void wm_add_default(bContext *C)
294 wmWindowManager *wm = BKE_libblock_alloc(&CTX_data_main(C)->wm, ID_WM, "WinMan");
296 bScreen *screen = CTX_wm_screen(C); /* XXX from file read hrmf */
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));
306 wm_window_make_drawable(C, win);
310 /* context is allowed to be NULL, do not free wm itself (library.c) */
311 void wm_close_and_free(bContext *C, wmWindowManager *wm)
315 wmKeyConfig *keyconf;
317 if (wm->autosavetimer)
318 wm_autosave_timer_ended(wm);
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);
327 while ((op = wm->operators.first)) {
328 BLI_remlink(&wm->operators, op);
329 WM_operator_free(op);
332 while ((keyconf = wm->keyconfigs.first)) {
333 BLI_remlink(&wm->keyconfigs, keyconf);
334 WM_keyconfig_free(keyconf);
337 BLI_freelistN(&wm->queue);
339 BLI_freelistN(&wm->paintcursors);
340 BLI_freelistN(&wm->drags);
344 if (C && CTX_wm_manager(C) == wm) CTX_wm_manager_set(C, NULL);
347 void wm_close_and_free_all(bContext *C, ListBase *wmlist)
351 while ((wm = wmlist->first)) {
352 wm_close_and_free(C, wm);
353 BLI_remlink(wmlist, wm);
358 void WM_main(bContext *C)
362 /* get events from ghost, handle window events, add to window queues */
363 wm_window_process_events(C);
365 /* per window, all events to the window, screen, area and region handlers */
366 wm_event_do_handlers(C);
368 /* events have left notes about changes, we handle and cache it */
369 wm_event_do_notifiers(C);
371 /* execute cached changes draw */