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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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 *****
31 #include "DNA_windowmanager_types.h"
33 #include "MEM_guardedalloc.h"
35 #include "GHOST_C-api.h"
37 #include "BLI_blenlib.h"
39 #include "BKE_blender.h"
40 #include "BKE_context.h"
41 #include "BKE_idprop.h"
42 #include "BKE_library.h"
44 #include "BKE_screen.h"
45 #include "BKE_report.h"
49 #include "wm_window.h"
50 #include "wm_event_system.h"
51 #include "wm_event_types.h"
55 #include "ED_screen.h"
57 #include "RNA_types.h"
59 /* ****************************************************** */
61 #define MAX_OP_REGISTERED 32
63 void WM_operator_free(wmOperator *op)
66 op->properties= op->ptr->data;
71 IDP_FreeProperty(op->properties);
72 MEM_freeN(op->properties);
75 if(op->reports && (op->reports->flag & RPT_FREE)) {
76 BKE_reports_clear(op->reports);
77 MEM_freeN(op->reports);
81 wmOperator *opm, *opmnext;
82 for(opm= op->macro.first; opm; opm= opmnext) {
84 WM_operator_free(opm);
91 /* all operations get registered in the windowmanager here */
92 /* called on event handling by event_system.c */
93 void wm_operator_register(bContext *C, wmOperator *op)
95 wmWindowManager *wm= CTX_wm_manager(C);
99 BLI_addtail(&wm->operators, op);
100 tot= BLI_countlist(&wm->operators);
102 while(tot>MAX_OP_REGISTERED) {
103 wmOperator *opt= wm->operators.first;
104 BLI_remlink(&wm->operators, opt);
105 WM_operator_free(opt);
110 /* Report the string representation of the operator */
111 buf = WM_operator_pystring(C, op->type, op->ptr, 1);
112 BKE_report(CTX_wm_reports(C), RPT_OPERATOR, buf);
115 /* so the console is redrawn */
116 WM_event_add_notifier(C, NC_SPACE|ND_SPACE_CONSOLE_REPORT, NULL);
120 void WM_operator_stack_clear(bContext *C)
122 wmWindowManager *wm= CTX_wm_manager(C);
125 while((op= wm->operators.first)) {
126 BLI_remlink(&wm->operators, op);
127 WM_operator_free(op);
132 /* ****************************************** */
134 static ListBase menutypes = {NULL, NULL}; /* global menutype list */
136 MenuType *WM_menutype_find(const char *idname, int quiet)
141 for(mt=menutypes.first; mt; mt=mt->next)
142 if(strcmp(idname, mt->idname)==0)
147 printf("search for unknown menutype %s\n", idname);
152 int WM_menutype_add(MenuType* mt)
154 BLI_addtail(&menutypes, mt);
158 void WM_menutype_freelink(MenuType* mt)
160 BLI_freelinkN(&menutypes, mt);
163 void WM_menutype_free(void)
167 for(mt= menutypes.first; mt; mt= mt->next) {
169 mt->ext.free(mt->ext.data);
171 WM_menutype_freelink(mt);
175 /* ****************************************** */
177 void WM_keymap_init(bContext *C)
179 wmWindowManager *wm= CTX_wm_manager(C);
182 wm->defaultconf= WM_keyconfig_add(wm, "Blender");
184 if(wm && CTX_py_init_get(C) && (wm->initialized & WM_INIT_KEYMAP) == 0) {
185 /* create default key config */
186 wm_window_keymap(wm->defaultconf);
187 ED_spacetypes_keymap(wm->defaultconf);
189 wm->initialized |= WM_INIT_KEYMAP;
192 WM_keyconfig_userdef(wm);
195 void wm_check(bContext *C)
197 wmWindowManager *wm= CTX_wm_manager(C);
201 wm= CTX_data_main(C)->wm.first;
202 CTX_wm_manager_set(C, wm);
205 if(wm->windows.first==NULL) return;
208 if((wm->initialized & WM_INIT_WINDOW) == 0)
211 /* case: no open windows at all, for old file reads */
212 wm_window_add_ghostwindows(wm);
215 if((wm->initialized & WM_INIT_WINDOW) == 0) {
216 ED_screens_initialize(wm);
217 wm->initialized |= WM_INIT_WINDOW;
221 void wm_clear_default_size(bContext *C)
223 wmWindowManager *wm= CTX_wm_manager(C);
228 wm= CTX_data_main(C)->wm.first;
229 CTX_wm_manager_set(C, wm);
232 if(wm->windows.first==NULL) return;
234 for(win= wm->windows.first; win; win= win->next) {
243 /* on startup, it adds all data, for matching */
244 void wm_add_default(bContext *C)
246 wmWindowManager *wm= alloc_libblock(&CTX_data_main(C)->wm, ID_WM, "WinMan");
248 bScreen *screen= CTX_wm_screen(C); /* XXX from file read hrmf */
250 CTX_wm_manager_set(C, wm);
251 win= wm_window_new(C);
253 screen->winid= win->winid;
254 BLI_strncpy(win->screenname, screen->id.name+2, 21);
258 wm_window_make_drawable(C, win);
262 /* context is allowed to be NULL, do not free wm itself (library.c) */
263 void wm_close_and_free(bContext *C, wmWindowManager *wm)
267 wmKeyConfig *keyconf;
269 while((win= wm->windows.first)) {
270 BLI_remlink(&wm->windows, win);
271 win->screen= NULL; /* prevent draw clear to use screen */
272 wm_draw_window_clear(win);
273 wm_window_free(C, win);
276 while((op= wm->operators.first)) {
277 BLI_remlink(&wm->operators, op);
278 WM_operator_free(op);
281 while((keyconf=wm->keyconfigs.first)) {
282 BLI_remlink(&wm->keyconfigs, keyconf);
283 WM_keyconfig_free(keyconf);
286 BLI_freelistN(&wm->queue);
288 BLI_freelistN(&wm->paintcursors);
290 if(C && CTX_wm_manager(C)==wm) CTX_wm_manager_set(C, NULL);
293 void wm_close_and_free_all(bContext *C, ListBase *wmlist)
297 while((wm=wmlist->first)) {
298 wm_close_and_free(C, wm);
299 BLI_remlink(wmlist, wm);
304 void WM_main(bContext *C)
308 /* get events from ghost, handle window events, add to window queues */
309 wm_window_process_events(C);
311 /* per window, all events to the window, screen, area and region handlers */
312 wm_event_do_handlers(C);
314 /* events have left notes about changes, we handle and cache it */
315 wm_event_do_notifiers(C);
317 /* execute cached changes draw */