2.5
[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 "BLI_blenlib.h"
34
35 #include "BKE_blender.h"
36 #include "BKE_context.h"
37 #include "BKE_idprop.h"
38 #include "BKE_library.h"
39 #include "BKE_main.h"
40 #include "BKE_report.h"
41
42 #include "WM_api.h"
43 #include "WM_types.h"
44 #include "wm_window.h"
45 #include "wm_event_system.h"
46 #include "wm_event_types.h"
47 #include "wm_draw.h"
48 #include "wm.h"
49
50 #include "ED_screen.h"
51
52 #include "RNA_types.h"
53
54 /* ****************************************************** */
55
56 #define MAX_OP_REGISTERED       32
57
58 void WM_operator_free(wmOperator *op)
59 {
60         if(op->ptr) {
61                 op->properties= op->ptr->data;
62                 MEM_freeN(op->ptr);
63         }
64
65         if(op->properties) {
66                 IDP_FreeProperty(op->properties);
67                 MEM_freeN(op->properties);
68         }
69
70         if(op->reports) {
71                 BKE_reports_clear(op->reports);
72                 MEM_freeN(op->reports);
73         }
74
75         MEM_freeN(op);
76 }
77
78 /* all operations get registered in the windowmanager here */
79 /* called on event handling by event_system.c */
80 void wm_operator_register(wmWindowManager *wm, wmOperator *op)
81 {
82         int tot;
83
84         BLI_addtail(&wm->operators, op);
85         tot= BLI_countlist(&wm->operators);
86         
87         while(tot>MAX_OP_REGISTERED) {
88                 wmOperator *opt= wm->operators.first;
89                 BLI_remlink(&wm->operators, opt);
90                 WM_operator_free(opt);
91                 tot--;
92         }
93 }
94
95
96 /* ****************************************** */
97
98 void wm_check(bContext *C)
99 {
100         wmWindowManager *wm= CTX_wm_manager(C);
101         
102         /* wm context */
103         if(wm==NULL) {
104                 wm= CTX_data_main(C)->wm.first;
105                 CTX_wm_manager_set(C, wm);
106         }
107         if(wm==NULL) return;
108         if(wm->windows.first==NULL) return;
109         
110         /* case: no open windows at all, for old file reads */
111         wm_window_add_ghostwindows(wm);
112         
113         /* case: fileread */
114         if(wm->initialized==0) {
115                 
116                 wm_window_keymap(wm);
117                 ED_spacetypes_keymap(wm);
118                 
119                 ED_screens_initialize(wm);
120                 wm->initialized= 1;
121         }
122 }
123
124 /* on startup, it adds all data, for matching */
125 void wm_add_default(bContext *C)
126 {
127         wmWindowManager *wm= alloc_libblock(&CTX_data_main(C)->wm, ID_WM, "WinMan");
128         wmWindow *win;
129         bScreen *screen= CTX_wm_screen(C); /* XXX from file read hrmf */
130         
131         CTX_wm_manager_set(C, wm);
132         win= wm_window_new(C);
133         win->screen= screen;
134         screen->winid= win->winid;
135         BLI_strncpy(win->screenname, screen->id.name+2, 21);
136         
137         wm->winactive= win;
138         wm->file_saved= 1;
139         wm_window_make_drawable(C, win); 
140 }
141
142
143 /* context is allowed to be NULL, do not free wm itself (library.c) */
144 void wm_close_and_free(bContext *C, wmWindowManager *wm)
145 {
146         wmWindow *win;
147         wmOperator *op;
148         wmKeyMap *km;
149         wmKeymapItem *kmi;
150         
151         while((win= wm->windows.first)) {
152                 BLI_remlink(&wm->windows, win);
153                 wm_draw_window_clear(win);
154                 wm_window_free(C, win);
155         }
156         
157         while((op= wm->operators.first)) {
158                 BLI_remlink(&wm->operators, op);
159                 WM_operator_free(op);
160         }
161
162         while((km= wm->keymaps.first)) {
163                 for(kmi=km->keymap.first; kmi; kmi=kmi->next) {
164                         if(kmi->ptr) {
165                                 WM_operator_properties_free(kmi->ptr);
166                                 MEM_freeN(kmi->ptr);
167                         }
168                 }
169
170                 BLI_freelistN(&km->keymap);
171                 BLI_remlink(&wm->keymaps, km);
172                 MEM_freeN(km);
173         }
174         
175         BLI_freelistN(&wm->queue);
176         
177         BLI_freelistN(&wm->paintcursors);
178         
179         if(C && CTX_wm_manager(C)==wm) CTX_wm_manager_set(C, NULL);
180 }
181
182 void wm_close_and_free_all(bContext *C, ListBase *wmlist)
183 {
184         wmWindowManager *wm;
185         
186         while((wm=wmlist->first)) {
187                 wm_close_and_free(C, wm);
188                 BLI_remlink(wmlist, wm);
189                 MEM_freeN(wm);
190         }
191 }
192
193 void WM_main(bContext *C)
194 {
195         while(1) {
196                 
197                 /* get events from ghost, handle window events, add to window queues */
198                 wm_window_process_events(C); 
199                 
200                 /* per window, all events to the window, screen, area and region handlers */
201                 wm_event_do_handlers(C);
202                 
203                 /* events have left notes about changes, we handle and cache it */
204                 wm_event_do_notifiers(C);
205                 
206                 /* execute cached changes draw */
207                 wm_draw_update(C);
208         }
209 }
210
211