a068f84ae294f413b4928012d08464803a578c38
[blender-staging.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->reports->flag & RPT_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->defaultconf)
136                 wm->defaultconf= WM_keyconfig_add(wm, "Blender");
137         
138         if(wm && CTX_py_init_get(C) && (wm->initialized & WM_INIT_KEYMAP) == 0) {
139                 /* create default key config */
140                 wm_window_keymap(wm->defaultconf);
141                 ED_spacetypes_keymap(wm->defaultconf);
142
143                 wm->initialized |= WM_INIT_KEYMAP;
144         }
145
146         WM_keyconfig_userdef(wm);
147 }
148
149 void wm_check(bContext *C)
150 {
151         wmWindowManager *wm= CTX_wm_manager(C);
152         
153         /* wm context */
154         if(wm==NULL) {
155                 wm= CTX_data_main(C)->wm.first;
156                 CTX_wm_manager_set(C, wm);
157         }
158         if(wm==NULL) return;
159         if(wm->windows.first==NULL) return;
160
161         /* case: fileread */
162         if((wm->initialized & WM_INIT_WINDOW) == 0)
163                 WM_keymap_init(C);
164         
165         /* case: no open windows at all, for old file reads */
166         wm_window_add_ghostwindows(wm);
167         
168         /* case: fileread */
169         if((wm->initialized & WM_INIT_WINDOW) == 0) {
170                 ED_screens_initialize(wm);
171                 wm->initialized |= WM_INIT_WINDOW;
172         }
173 }
174
175 void wm_clear_default_size(bContext *C)
176 {
177         wmWindowManager *wm= CTX_wm_manager(C);
178         wmWindow *win;
179         
180         /* wm context */
181         if(wm==NULL) {
182                 wm= CTX_data_main(C)->wm.first;
183                 CTX_wm_manager_set(C, wm);
184         }
185         if(wm==NULL) return;
186         if(wm->windows.first==NULL) return;
187         
188         for(win= wm->windows.first; win; win= win->next) {
189                 win->sizex = 0;
190                 win->sizey = 0;
191                 win->posx = 0;
192                 win->posy = 0;
193         }
194
195 }
196
197 /* on startup, it adds all data, for matching */
198 void wm_add_default(bContext *C)
199 {
200         wmWindowManager *wm= alloc_libblock(&CTX_data_main(C)->wm, ID_WM, "WinMan");
201         wmWindow *win;
202         bScreen *screen= CTX_wm_screen(C); /* XXX from file read hrmf */
203         
204         CTX_wm_manager_set(C, wm);
205         win= wm_window_new(C);
206         win->screen= screen;
207         screen->winid= win->winid;
208         BLI_strncpy(win->screenname, screen->id.name+2, 21);
209         
210         wm->winactive= win;
211         wm->file_saved= 1;
212         wm_window_make_drawable(C, win); 
213 }
214
215
216 /* context is allowed to be NULL, do not free wm itself (library.c) */
217 void wm_close_and_free(bContext *C, wmWindowManager *wm)
218 {
219         wmWindow *win;
220         wmOperator *op;
221         wmKeyConfig *keyconf;
222         
223         while((win= wm->windows.first)) {
224                 BLI_remlink(&wm->windows, win);
225                 win->screen= NULL; /* prevent draw clear to use screen */
226                 wm_draw_window_clear(win);
227                 wm_window_free(C, win);
228         }
229         
230         while((op= wm->operators.first)) {
231                 BLI_remlink(&wm->operators, op);
232                 WM_operator_free(op);
233         }
234
235         while((keyconf=wm->keyconfigs.first)) {
236                 BLI_remlink(&wm->keyconfigs, keyconf);
237                 WM_keyconfig_free(keyconf);
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