* Updated blender 2.5 default setup
[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         MEM_freeN(op);
78 }
79
80 /* all operations get registered in the windowmanager here */
81 /* called on event handling by event_system.c */
82 void wm_operator_register(wmWindowManager *wm, wmOperator *op)
83 {
84         int tot;
85
86         BLI_addtail(&wm->operators, op);
87         tot= BLI_countlist(&wm->operators);
88         
89         while(tot>MAX_OP_REGISTERED) {
90                 wmOperator *opt= wm->operators.first;
91                 BLI_remlink(&wm->operators, opt);
92                 WM_operator_free(opt);
93                 tot--;
94         }
95 }
96
97
98 /* ****************************************** */
99
100 void wm_check(bContext *C)
101 {
102         wmWindowManager *wm= CTX_wm_manager(C);
103         
104         /* wm context */
105         if(wm==NULL) {
106                 wm= CTX_data_main(C)->wm.first;
107                 CTX_wm_manager_set(C, wm);
108         }
109         if(wm==NULL) return;
110         if(wm->windows.first==NULL) return;
111         
112         /* case: no open windows at all, for old file reads */
113         wm_window_add_ghostwindows(wm);
114         
115         /* case: fileread */
116         if(wm->initialized==0) {
117                 
118                 wm_window_keymap(wm);
119                 ED_spacetypes_keymap(wm);
120                 
121                 ED_screens_initialize(wm);
122                 wm->initialized= 1;
123         }
124 }
125
126 void wm_clear_default_size(bContext *C)
127 {
128         wmWindowManager *wm= CTX_wm_manager(C);
129         wmWindow *win;
130         
131         /* wm context */
132         if(wm==NULL) {
133                 wm= CTX_data_main(C)->wm.first;
134                 CTX_wm_manager_set(C, wm);
135         }
136         if(wm==NULL) return;
137         if(wm->windows.first==NULL) return;
138         
139         win = wm->windows.first;
140         win->sizex = 0;
141         win->sizey = 0;
142         win->posx = 0;
143         win->posy = 0;
144         win->windowstate= GHOST_kWindowStateMaximized;
145
146 }
147
148 /* on startup, it adds all data, for matching */
149 void wm_add_default(bContext *C)
150 {
151         wmWindowManager *wm= alloc_libblock(&CTX_data_main(C)->wm, ID_WM, "WinMan");
152         wmWindow *win;
153         bScreen *screen= CTX_wm_screen(C); /* XXX from file read hrmf */
154         
155         CTX_wm_manager_set(C, wm);
156         win= wm_window_new(C);
157         win->screen= screen;
158         screen->winid= win->winid;
159         BLI_strncpy(win->screenname, screen->id.name+2, 21);
160         
161         wm->winactive= win;
162         wm->file_saved= 1;
163         wm_window_make_drawable(C, win); 
164 }
165
166
167 /* context is allowed to be NULL, do not free wm itself (library.c) */
168 void wm_close_and_free(bContext *C, wmWindowManager *wm)
169 {
170         wmWindow *win;
171         wmOperator *op;
172         wmKeyMap *km;
173         wmKeymapItem *kmi;
174         
175         while((win= wm->windows.first)) {
176                 BLI_remlink(&wm->windows, win);
177                 win->screen= NULL; /* prevent draw clear to use screen */
178                 wm_draw_window_clear(win);
179                 wm_window_free(C, win);
180         }
181         
182         while((op= wm->operators.first)) {
183                 BLI_remlink(&wm->operators, op);
184                 WM_operator_free(op);
185         }
186
187         while((km= wm->keymaps.first)) {
188                 for(kmi=km->keymap.first; kmi; kmi=kmi->next) {
189                         if(kmi->ptr) {
190                                 WM_operator_properties_free(kmi->ptr);
191                                 MEM_freeN(kmi->ptr);
192                         }
193                 }
194
195                 BLI_freelistN(&km->keymap);
196                 BLI_remlink(&wm->keymaps, km);
197                 MEM_freeN(km);
198         }
199         
200         BLI_freelistN(&wm->queue);
201         
202         BLI_freelistN(&wm->paintcursors);
203         
204         if(C && CTX_wm_manager(C)==wm) CTX_wm_manager_set(C, NULL);
205 }
206
207 void wm_close_and_free_all(bContext *C, ListBase *wmlist)
208 {
209         wmWindowManager *wm;
210         
211         while((wm=wmlist->first)) {
212                 wm_close_and_free(C, wm);
213                 BLI_remlink(wmlist, wm);
214                 MEM_freeN(wm);
215         }
216 }
217
218 void WM_main(bContext *C)
219 {
220         while(1) {
221                 
222                 /* get events from ghost, handle window events, add to window queues */
223                 wm_window_process_events(C); 
224                 
225                 /* per window, all events to the window, screen, area and region handlers */
226                 wm_event_do_handlers(C);
227                 
228                 /* events have left notes about changes, we handle and cache it */
229                 wm_event_do_notifiers(C);
230                 
231                 /* execute cached changes draw */
232                 wm_draw_update(C);
233         }
234 }
235
236