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