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