COLLADA branch: merge from trunk -r 25745:26429.
[blender.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 #include "BKE_global.h"
47
48 #include "WM_api.h"
49 #include "WM_types.h"
50 #include "wm_window.h"
51 #include "wm_event_system.h"
52 #include "wm_event_types.h"
53 #include "wm_draw.h"
54 #include "wm.h"
55
56 #include "ED_screen.h"
57
58 #include "RNA_types.h"
59
60 /* ****************************************************** */
61
62 #define MAX_OP_REGISTERED       32
63
64 void WM_operator_free(wmOperator *op)
65 {
66         if(op->ptr) {
67                 op->properties= op->ptr->data;
68                 MEM_freeN(op->ptr);
69         }
70
71         if(op->properties) {
72                 IDP_FreeProperty(op->properties);
73                 MEM_freeN(op->properties);
74         }
75
76         if(op->reports && (op->reports->flag & RPT_FREE)) {
77                 BKE_reports_clear(op->reports);
78                 MEM_freeN(op->reports);
79         }
80
81         if(op->macro.first) {
82                 wmOperator *opm, *opmnext;
83                 for(opm= op->macro.first; opm; opm= opmnext) {
84                         opmnext = opm->next;
85                         WM_operator_free(opm);
86                 }
87         }
88         
89         MEM_freeN(op);
90 }
91
92 /* all operations get registered in the windowmanager here */
93 /* called on event handling by event_system.c */
94 void wm_operator_register(bContext *C, wmOperator *op)
95 {
96         wmWindowManager *wm= CTX_wm_manager(C);
97         int tot;
98         char *buf;
99
100         BLI_addtail(&wm->operators, op);
101         tot= BLI_countlist(&wm->operators);
102         
103         while(tot>MAX_OP_REGISTERED) {
104                 wmOperator *opt= wm->operators.first;
105                 BLI_remlink(&wm->operators, opt);
106                 WM_operator_free(opt);
107                 tot--;
108         }
109         
110         
111         /* Report the string representation of the operator */
112         buf = WM_operator_pystring(C, op->type, op->ptr, 1);
113         BKE_report(CTX_wm_reports(C), RPT_OPERATOR, buf);
114         MEM_freeN(buf);
115         
116         /* so the console is redrawn */
117         WM_event_add_notifier(C, NC_SPACE|ND_SPACE_CONSOLE_REPORT, NULL);
118 }
119
120
121 void WM_operator_stack_clear(bContext *C)
122 {
123         wmWindowManager *wm= CTX_wm_manager(C);
124         wmOperator *op;
125         
126         while((op= wm->operators.first)) {
127                 BLI_remlink(&wm->operators, op);
128                 WM_operator_free(op);
129         }
130         
131 }
132
133 /* ****************************************** */
134
135 static ListBase menutypes = {NULL, NULL}; /* global menutype list */
136
137 MenuType *WM_menutype_find(const char *idname, int quiet)
138 {
139         MenuType* mt;
140
141         if (idname[0]) {
142                 for(mt=menutypes.first; mt; mt=mt->next)
143                         if(strcmp(idname, mt->idname)==0)
144                                 return mt;
145         }
146
147         if(!quiet)
148                 printf("search for unknown menutype %s\n", idname);
149
150         return NULL;
151 }
152
153 int WM_menutype_add(MenuType* mt)
154 {
155         BLI_addtail(&menutypes, mt);
156         return 1;
157 }
158
159 void WM_menutype_freelink(MenuType* mt)
160 {
161         BLI_freelinkN(&menutypes, mt);
162 }
163
164 void WM_menutype_free(void)
165 {
166         MenuType* mt= menutypes.first, *mt_next;
167
168         while(mt) {
169                 mt_next= mt->next;
170
171                 if(mt->ext.free)
172                         mt->ext.free(mt->ext.data);
173
174                 WM_menutype_freelink(mt);
175
176                 mt= mt_next;
177         }
178 }
179
180 /* ****************************************** */
181
182 void WM_keymap_init(bContext *C)
183 {
184         wmWindowManager *wm= CTX_wm_manager(C);
185
186         if(!wm->defaultconf)
187                 wm->defaultconf= WM_keyconfig_add(wm, "Blender");
188         
189         if(wm && CTX_py_init_get(C) && (wm->initialized & WM_INIT_KEYMAP) == 0) {
190                 /* create default key config */
191                 wm_window_keymap(wm->defaultconf);
192                 ED_spacetypes_keymap(wm->defaultconf);
193                 WM_keyconfig_userdef(wm);
194
195                 wm->initialized |= WM_INIT_KEYMAP;
196         }
197 }
198
199 void WM_check(bContext *C)
200 {
201         wmWindowManager *wm= CTX_wm_manager(C);
202         
203         /* wm context */
204         if(wm==NULL) {
205                 wm= CTX_data_main(C)->wm.first;
206                 CTX_wm_manager_set(C, wm);
207         }
208         if(wm==NULL) return;
209         if(wm->windows.first==NULL) return;
210
211         if (!G.background) {
212                 /* case: fileread */
213                 if((wm->initialized & WM_INIT_WINDOW) == 0) {
214                         WM_keymap_init(C);
215                         WM_autosave_init(wm);
216                 }
217
218                 /* case: no open windows at all, for old file reads */
219                 wm_window_add_ghostwindows(wm);
220
221                 /* case: fileread */
222                 if((wm->initialized & WM_INIT_WINDOW) == 0) {
223                         ED_screens_initialize(wm);
224                         wm->initialized |= WM_INIT_WINDOW;
225                 }
226         }
227 }
228
229 void wm_clear_default_size(bContext *C)
230 {
231         wmWindowManager *wm= CTX_wm_manager(C);
232         wmWindow *win;
233         
234         /* wm context */
235         if(wm==NULL) {
236                 wm= CTX_data_main(C)->wm.first;
237                 CTX_wm_manager_set(C, wm);
238         }
239         if(wm==NULL) return;
240         if(wm->windows.first==NULL) return;
241         
242         for(win= wm->windows.first; win; win= win->next) {
243                 win->sizex = 0;
244                 win->sizey = 0;
245                 win->posx = 0;
246                 win->posy = 0;
247         }
248
249 }
250
251 /* on startup, it adds all data, for matching */
252 void wm_add_default(bContext *C)
253 {
254         wmWindowManager *wm= alloc_libblock(&CTX_data_main(C)->wm, ID_WM, "WinMan");
255         wmWindow *win;
256         bScreen *screen= CTX_wm_screen(C); /* XXX from file read hrmf */
257         
258         CTX_wm_manager_set(C, wm);
259         win= wm_window_new(C);
260         win->screen= screen;
261         screen->winid= win->winid;
262         BLI_strncpy(win->screenname, screen->id.name+2, 21);
263         
264         wm->winactive= win;
265         wm->file_saved= 1;
266         wm_window_make_drawable(C, win); 
267 }
268
269
270 /* context is allowed to be NULL, do not free wm itself (library.c) */
271 void wm_close_and_free(bContext *C, wmWindowManager *wm)
272 {
273         wmWindow *win;
274         wmOperator *op;
275         wmKeyConfig *keyconf;
276
277         if(wm->autosavetimer)
278                 wm_autosave_timer_ended(wm);
279
280         while((win= wm->windows.first)) {
281                 BLI_remlink(&wm->windows, win);
282                 win->screen= NULL; /* prevent draw clear to use screen */
283                 wm_draw_window_clear(win);
284                 wm_window_free(C, wm, win);
285         }
286         
287         while((op= wm->operators.first)) {
288                 BLI_remlink(&wm->operators, op);
289                 WM_operator_free(op);
290         }
291
292         while((keyconf=wm->keyconfigs.first)) {
293                 BLI_remlink(&wm->keyconfigs, keyconf);
294                 WM_keyconfig_free(keyconf);
295         }
296
297         BLI_freelistN(&wm->queue);
298         
299         BLI_freelistN(&wm->paintcursors);
300         BLI_freelistN(&wm->drags);
301         BKE_reports_clear(&wm->reports);
302         
303         if(C && CTX_wm_manager(C)==wm) CTX_wm_manager_set(C, NULL);
304 }
305
306 void wm_close_and_free_all(bContext *C, ListBase *wmlist)
307 {
308         wmWindowManager *wm;
309         
310         while((wm=wmlist->first)) {
311                 wm_close_and_free(C, wm);
312                 BLI_remlink(wmlist, wm);
313                 MEM_freeN(wm);
314         }
315 }
316
317 void WM_main(bContext *C)
318 {
319         while(1) {
320                 
321                 /* get events from ghost, handle window events, add to window queues */
322                 wm_window_process_events(C); 
323                 
324                 /* per window, all events to the window, screen, area and region handlers */
325                 wm_event_do_handlers(C);
326                 
327                 /* events have left notes about changes, we handle and cache it */
328                 wm_event_do_notifiers(C);
329                 
330                 /* execute cached changes draw */
331                 wm_draw_update(C);
332         }
333 }
334
335