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