menus are now global (like operators), so for eg, the info add menu and the 3D add...
[blender-staging.git] / source / blender / windowmanager / intern / wm.c
1 /**
2  * $Id:
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
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. 
10  *
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.
15  *
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.
19  *
20  * The Original Code is Copyright (C) 2007 Blender Foundation.
21  * All rights reserved.
22  *
23  * 
24  * Contributor(s): Blender Foundation
25  *
26  * ***** END GPL LICENSE BLOCK *****
27  */
28
29 #include "string.h"
30
31 #include "DNA_windowmanager_types.h"
32
33 #include "MEM_guardedalloc.h"
34
35 #include "GHOST_C-api.h"
36
37 #include "BLI_blenlib.h"
38
39 #include "BKE_blender.h"
40 #include "BKE_context.h"
41 #include "BKE_idprop.h"
42 #include "BKE_library.h"
43 #include "BKE_main.h"
44 #include "BKE_screen.h"
45 #include "BKE_report.h"
46
47 #include "WM_api.h"
48 #include "WM_types.h"
49 #include "wm_window.h"
50 #include "wm_event_system.h"
51 #include "wm_event_types.h"
52 #include "wm_draw.h"
53 #include "wm.h"
54
55 #include "ED_screen.h"
56
57 #include "RNA_types.h"
58
59 /* ****************************************************** */
60
61 #define MAX_OP_REGISTERED       32
62
63 void WM_operator_free(wmOperator *op)
64 {
65         if(op->ptr) {
66                 op->properties= op->ptr->data;
67                 MEM_freeN(op->ptr);
68         }
69
70         if(op->properties) {
71                 IDP_FreeProperty(op->properties);
72                 MEM_freeN(op->properties);
73         }
74
75         if(op->reports && (op->reports->flag & RPT_FREE)) {
76                 BKE_reports_clear(op->reports);
77                 MEM_freeN(op->reports);
78         }
79
80         if(op->macro.first) {
81                 wmOperator *opm, *opmnext;
82                 for(opm= op->macro.first; opm; opm= opmnext) {
83                         opmnext = opm->next;
84                         WM_operator_free(opm);
85                 }
86         }
87         
88         MEM_freeN(op);
89 }
90
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)
94 {
95         wmWindowManager *wm= CTX_wm_manager(C);
96         int tot;
97         char *buf;
98
99         BLI_addtail(&wm->operators, op);
100         tot= BLI_countlist(&wm->operators);
101         
102         while(tot>MAX_OP_REGISTERED) {
103                 wmOperator *opt= wm->operators.first;
104                 BLI_remlink(&wm->operators, opt);
105                 WM_operator_free(opt);
106                 tot--;
107         }
108         
109         
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);
113         MEM_freeN(buf);
114         
115         /* so the console is redrawn */
116         WM_event_add_notifier(C, NC_SPACE|ND_SPACE_CONSOLE_REPORT, NULL);
117 }
118
119
120 void WM_operator_stack_clear(bContext *C)
121 {
122         wmWindowManager *wm= CTX_wm_manager(C);
123         wmOperator *op;
124         
125         while((op= wm->operators.first)) {
126                 BLI_remlink(&wm->operators, op);
127                 WM_operator_free(op);
128         }
129         
130 }
131
132 /* ****************************************** */
133
134 static ListBase menutypes = {NULL, NULL}; /* global menutype list */
135
136 MenuType *WM_menutype_find(const char *idname, int quiet)
137 {
138         MenuType* mt;
139
140         if (idname[0]) {
141                 for(mt=menutypes.first; mt; mt=mt->next)
142                         if(strcmp(idname, mt->idname)==0)
143                                 return mt;
144         }
145
146         if(!quiet)
147                 printf("search for unknown menutype %s\n", idname);
148
149         return NULL;
150 }
151
152 int WM_menutype_add(MenuType* mt)
153 {
154         BLI_addtail(&menutypes, mt);
155         return 1;
156 }
157
158 void WM_menutype_freelink(MenuType* mt)
159 {
160         BLI_freelinkN(&menutypes, mt);
161 }
162
163 void WM_menutype_free(void)
164 {
165         MenuType* mt;
166
167         for(mt= menutypes.first; mt; mt= mt->next) {
168                 if(mt->ext.free) {
169                         mt->ext.free(mt->ext.data);
170                 }
171                 WM_menutype_freelink(mt);
172         }
173 }
174
175 /* ****************************************** */
176
177 void WM_keymap_init(bContext *C)
178 {
179         wmWindowManager *wm= CTX_wm_manager(C);
180
181         if(!wm->defaultconf)
182                 wm->defaultconf= WM_keyconfig_add(wm, "Blender");
183         
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);
188
189                 wm->initialized |= WM_INIT_KEYMAP;
190         }
191
192         WM_keyconfig_userdef(wm);
193 }
194
195 void wm_check(bContext *C)
196 {
197         wmWindowManager *wm= CTX_wm_manager(C);
198         
199         /* wm context */
200         if(wm==NULL) {
201                 wm= CTX_data_main(C)->wm.first;
202                 CTX_wm_manager_set(C, wm);
203         }
204         if(wm==NULL) return;
205         if(wm->windows.first==NULL) return;
206
207         /* case: fileread */
208         if((wm->initialized & WM_INIT_WINDOW) == 0)
209                 WM_keymap_init(C);
210         
211         /* case: no open windows at all, for old file reads */
212         wm_window_add_ghostwindows(wm);
213         
214         /* case: fileread */
215         if((wm->initialized & WM_INIT_WINDOW) == 0) {
216                 ED_screens_initialize(wm);
217                 wm->initialized |= WM_INIT_WINDOW;
218         }
219 }
220
221 void wm_clear_default_size(bContext *C)
222 {
223         wmWindowManager *wm= CTX_wm_manager(C);
224         wmWindow *win;
225         
226         /* wm context */
227         if(wm==NULL) {
228                 wm= CTX_data_main(C)->wm.first;
229                 CTX_wm_manager_set(C, wm);
230         }
231         if(wm==NULL) return;
232         if(wm->windows.first==NULL) return;
233         
234         for(win= wm->windows.first; win; win= win->next) {
235                 win->sizex = 0;
236                 win->sizey = 0;
237                 win->posx = 0;
238                 win->posy = 0;
239         }
240
241 }
242
243 /* on startup, it adds all data, for matching */
244 void wm_add_default(bContext *C)
245 {
246         wmWindowManager *wm= alloc_libblock(&CTX_data_main(C)->wm, ID_WM, "WinMan");
247         wmWindow *win;
248         bScreen *screen= CTX_wm_screen(C); /* XXX from file read hrmf */
249         
250         CTX_wm_manager_set(C, wm);
251         win= wm_window_new(C);
252         win->screen= screen;
253         screen->winid= win->winid;
254         BLI_strncpy(win->screenname, screen->id.name+2, 21);
255         
256         wm->winactive= win;
257         wm->file_saved= 1;
258         wm_window_make_drawable(C, win); 
259 }
260
261
262 /* context is allowed to be NULL, do not free wm itself (library.c) */
263 void wm_close_and_free(bContext *C, wmWindowManager *wm)
264 {
265         wmWindow *win;
266         wmOperator *op;
267         wmKeyConfig *keyconf;
268         
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);
274         }
275         
276         while((op= wm->operators.first)) {
277                 BLI_remlink(&wm->operators, op);
278                 WM_operator_free(op);
279         }
280
281         while((keyconf=wm->keyconfigs.first)) {
282                 BLI_remlink(&wm->keyconfigs, keyconf);
283                 WM_keyconfig_free(keyconf);
284         }
285
286         BLI_freelistN(&wm->queue);
287         
288         BLI_freelistN(&wm->paintcursors);
289         
290         if(C && CTX_wm_manager(C)==wm) CTX_wm_manager_set(C, NULL);
291 }
292
293 void wm_close_and_free_all(bContext *C, ListBase *wmlist)
294 {
295         wmWindowManager *wm;
296         
297         while((wm=wmlist->first)) {
298                 wm_close_and_free(C, wm);
299                 BLI_remlink(wmlist, wm);
300                 MEM_freeN(wm);
301         }
302 }
303
304 void WM_main(bContext *C)
305 {
306         while(1) {
307                 
308                 /* get events from ghost, handle window events, add to window queues */
309                 wm_window_process_events(C); 
310                 
311                 /* per window, all events to the window, screen, area and region handlers */
312                 wm_event_do_handlers(C);
313                 
314                 /* events have left notes about changes, we handle and cache it */
315                 wm_event_do_notifiers(C);
316                 
317                 /* execute cached changes draw */
318                 wm_draw_update(C);
319         }
320 }
321
322