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