2.5/RNA:
[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 "DNA_windowmanager_types.h"
30
31 #include "MEM_guardedalloc.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_report.h"
43
44 #include "WM_api.h"
45 #include "WM_types.h"
46 #include "wm_window.h"
47 #include "wm_event_system.h"
48 #include "wm_event_types.h"
49 #include "wm_draw.h"
50 #include "wm.h"
51
52 #include "ED_screen.h"
53
54 #include "RNA_types.h"
55
56 /* ****************************************************** */
57
58 #define MAX_OP_REGISTERED       32
59
60 void WM_operator_free(wmOperator *op)
61 {
62         if(op->ptr) {
63                 op->properties= op->ptr->data;
64                 MEM_freeN(op->ptr);
65         }
66
67         if(op->properties) {
68                 IDP_FreeProperty(op->properties);
69                 MEM_freeN(op->properties);
70         }
71
72         if(op->reports) {
73                 BKE_reports_clear(op->reports);
74                 MEM_freeN(op->reports);
75         }
76
77         if(op->macro.first) {
78                 wmOperator *opm;
79                 for(opm= op->macro.first; opm; opm= opm->next) 
80                         WM_operator_free(opm);
81         }
82         
83         MEM_freeN(op);
84 }
85
86 /* all operations get registered in the windowmanager here */
87 /* called on event handling by event_system.c */
88 void wm_operator_register(bContext *C, wmOperator *op)
89 {
90         wmWindowManager *wm= CTX_wm_manager(C);
91         int tot;
92         char *buf;
93
94         BLI_addtail(&wm->operators, op);
95         tot= BLI_countlist(&wm->operators);
96         
97         while(tot>MAX_OP_REGISTERED) {
98                 wmOperator *opt= wm->operators.first;
99                 BLI_remlink(&wm->operators, opt);
100                 WM_operator_free(opt);
101                 tot--;
102         }
103         
104         
105         /* Report the string representation of the operator */
106         buf = WM_operator_pystring(C, op->type, op->ptr, 1);
107         BKE_report(CTX_wm_reports(C), RPT_OPERATOR, buf);
108         MEM_freeN(buf);
109         
110         /* so the console is redrawn */
111         WM_event_add_notifier(C, NC_CONSOLE|ND_CONSOLE_REPORT, NULL);
112 }
113
114
115 void WM_operator_stack_clear(bContext *C)
116 {
117         wmWindowManager *wm= CTX_wm_manager(C);
118         wmOperator *op;
119         
120         while((op= wm->operators.first)) {
121                 BLI_remlink(&wm->operators, op);
122                 WM_operator_free(op);
123         }
124         
125 }
126
127 /* ****************************************** */
128
129 void WM_keymap_init(bContext *C)
130 {
131         wmWindowManager *wm= CTX_wm_manager(C);
132
133         if(wm && CTX_py_init_get(C) && (wm->initialized & WM_INIT_KEYMAP) == 0) {
134                 wm_window_keymap(wm);
135                 ED_spacetypes_keymap(wm);
136
137                 wm->initialized |= WM_INIT_KEYMAP;
138         }
139 }
140
141 void wm_check(bContext *C)
142 {
143         wmWindowManager *wm= CTX_wm_manager(C);
144         
145         /* wm context */
146         if(wm==NULL) {
147                 wm= CTX_data_main(C)->wm.first;
148                 CTX_wm_manager_set(C, wm);
149         }
150         if(wm==NULL) return;
151         if(wm->windows.first==NULL) return;
152         
153         /* case: no open windows at all, for old file reads */
154         wm_window_add_ghostwindows(wm);
155         
156         /* case: fileread */
157         if((wm->initialized & WM_INIT_WINDOW) == 0) {
158                 
159                 WM_keymap_init(C);
160                 
161                 ED_screens_initialize(wm);
162                 wm->initialized |= WM_INIT_WINDOW;
163         }
164 }
165
166 void wm_clear_default_size(bContext *C)
167 {
168         wmWindowManager *wm= CTX_wm_manager(C);
169         wmWindow *win;
170         
171         /* wm context */
172         if(wm==NULL) {
173                 wm= CTX_data_main(C)->wm.first;
174                 CTX_wm_manager_set(C, wm);
175         }
176         if(wm==NULL) return;
177         if(wm->windows.first==NULL) return;
178         
179         for(win= wm->windows.first; win; win= win->next) {
180                 win->sizex = 0;
181                 win->sizey = 0;
182                 win->posx = 0;
183                 win->posy = 0;
184         }
185
186 }
187
188 /* on startup, it adds all data, for matching */
189 void wm_add_default(bContext *C)
190 {
191         wmWindowManager *wm= alloc_libblock(&CTX_data_main(C)->wm, ID_WM, "WinMan");
192         wmWindow *win;
193         bScreen *screen= CTX_wm_screen(C); /* XXX from file read hrmf */
194         
195         CTX_wm_manager_set(C, wm);
196         win= wm_window_new(C);
197         win->screen= screen;
198         screen->winid= win->winid;
199         BLI_strncpy(win->screenname, screen->id.name+2, 21);
200         
201         wm->winactive= win;
202         wm->file_saved= 1;
203         wm_window_make_drawable(C, win); 
204 }
205
206
207 /* context is allowed to be NULL, do not free wm itself (library.c) */
208 void wm_close_and_free(bContext *C, wmWindowManager *wm)
209 {
210         wmWindow *win;
211         wmOperator *op;
212         wmKeyMap *km;
213         wmKeymapItem *kmi;
214         
215         while((win= wm->windows.first)) {
216                 BLI_remlink(&wm->windows, win);
217                 win->screen= NULL; /* prevent draw clear to use screen */
218                 wm_draw_window_clear(win);
219                 wm_window_free(C, win);
220         }
221         
222         while((op= wm->operators.first)) {
223                 BLI_remlink(&wm->operators, op);
224                 WM_operator_free(op);
225         }
226
227         while((km= wm->keymaps.first)) {
228                 for(kmi=km->keymap.first; kmi; kmi=kmi->next) {
229                         if(kmi->ptr) {
230                                 WM_operator_properties_free(kmi->ptr);
231                                 MEM_freeN(kmi->ptr);
232                         }
233                 }
234
235                 BLI_freelistN(&km->keymap);
236                 BLI_remlink(&wm->keymaps, km);
237                 MEM_freeN(km);
238         }
239         
240         BLI_freelistN(&wm->queue);
241         
242         BLI_freelistN(&wm->paintcursors);
243         
244         if(C && CTX_wm_manager(C)==wm) CTX_wm_manager_set(C, NULL);
245 }
246
247 void wm_close_and_free_all(bContext *C, ListBase *wmlist)
248 {
249         wmWindowManager *wm;
250         
251         while((wm=wmlist->first)) {
252                 wm_close_and_free(C, wm);
253                 BLI_remlink(wmlist, wm);
254                 MEM_freeN(wm);
255         }
256 }
257
258 void WM_main(bContext *C)
259 {
260         while(1) {
261                 
262                 /* get events from ghost, handle window events, add to window queues */
263                 wm_window_process_events(C); 
264                 
265                 /* per window, all events to the window, screen, area and region handlers */
266                 wm_event_do_handlers(C);
267                 
268                 /* events have left notes about changes, we handle and cache it */
269                 wm_event_do_notifiers(C);
270                 
271                 /* execute cached changes draw */
272                 wm_draw_update(C);
273         }
274 }
275
276