2.5
[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_global.h"
37 #include "BKE_library.h"
38 #include "BKE_main.h"
39 #include "BKE_idprop.h"
40
41 #include "WM_api.h"
42 #include "WM_types.h"
43 #include "wm_window.h"
44 #include "wm_event_system.h"
45 #include "wm_event_types.h"
46 #include "wm.h"
47
48 #include "ED_screen.h"
49
50 /* ****************************************************** */
51
52 #define MAX_OP_REGISTERED       32
53
54 void wm_operator_free(wmOperator *op)
55 {
56         if(op->properties) {
57                 IDP_FreeProperty(op->properties);
58                 
59                 /* This need change, when the idprop code only
60                  * need call IDP_FreeProperty. (check BKE_idprop.h) */
61                 MEM_freeN(op->properties);
62                 op->properties= NULL;
63         }
64
65         if(op->ptr)
66                 MEM_freeN(op->ptr);
67
68         MEM_freeN(op);
69 }
70
71 /* all operations get registered in the windowmanager here */
72 /* called on event handling by event_system.c */
73 void wm_operator_register(wmWindowManager *wm, wmOperator *op)
74 {
75         int tot;
76
77         BLI_addtail(&wm->operators, op);
78         tot= BLI_countlist(&wm->operators);
79         
80         while(tot>MAX_OP_REGISTERED) {
81                 wmOperator *opt= wm->operators.first;
82                 BLI_remlink(&wm->operators, opt);
83                 wm_operator_free(opt);
84                 tot--;
85         }
86 }
87
88
89 /* ****************************************** */
90
91 void wm_check(bContext *C)
92 {
93         
94         /* wm context */
95         if(C->wm==NULL) C->wm= G.main->wm.first;
96         if(C->wm==NULL) return;
97         if(C->wm->windows.first==NULL) return;
98         
99         /* case: no open windows at all, for old file reads */
100         wm_window_add_ghostwindows(C->wm);
101         
102         /* case: fileread */
103         if(C->wm->initialized==0) {
104                 
105                 wm_window_keymap(C->wm);
106                 ED_spacetypes_keymap(C->wm);
107                 
108                 ED_screens_initialize(C->wm);
109                 C->wm->initialized= 1;
110         }
111 }
112
113 /* on startup, it adds all data, for matching */
114 void wm_add_default(bContext *C)
115 {
116         wmWindowManager *wm= alloc_libblock(&G.main->wm, ID_WM, "WinMan");
117         wmWindow *win;
118         
119         C->wm= wm;
120         
121         win= wm_window_new(C);
122         win->screen= C->screen;
123         wm->winactive= win;
124         wm_window_make_drawable(C, win); 
125 }
126
127
128 /* context is allowed to be NULL, do not free wm itself (library.c) */
129 void wm_close_and_free(bContext *C, wmWindowManager *wm)
130 {
131         wmWindow *win;
132         wmOperator *op;
133         wmReport *report;
134         wmKeyMap *km;
135         wmKeymapItem *kmi;
136         
137         while((win= wm->windows.first)) {
138                 BLI_remlink(&wm->windows, win);
139                 wm_window_free(C, win);
140         }
141         
142         while((op= wm->operators.first)) {
143                 BLI_remlink(&wm->operators, op);
144                 wm_operator_free(op);
145         }
146
147         while((report= wm->reports.first)) {
148                 BLI_remlink(&wm->reports, report);
149                 wm_report_free(report);
150         }
151
152         while((km= wm->keymaps.first)) {
153                 for(kmi=km->keymap.first; kmi; kmi=kmi->next) {
154                         if(kmi->ptr)
155                                 MEM_freeN(kmi->ptr);
156
157                         if(kmi->properties) {
158                                 IDP_FreeProperty(kmi->properties);
159                                 MEM_freeN(kmi->properties);
160                         }
161                 }
162
163                 BLI_freelistN(&km->keymap);
164                 BLI_remlink(&wm->keymaps, km);
165                 MEM_freeN(km);
166         }
167         
168         BLI_freelistN(&wm->queue);
169         
170         if(C && C->wm==wm) C->wm= NULL;
171 }
172
173 void wm_close_and_free_all(bContext *C, ListBase *wmlist)
174 {
175         wmWindowManager *wm;
176         
177         while((wm=wmlist->first)) {
178                 wm_close_and_free(C, wm);
179                 BLI_remlink(wmlist, wm);
180                 MEM_freeN(wm);
181         }
182 }
183
184 void WM_main(bContext *C)
185 {
186         while(1) {
187                 
188                 /* get events from ghost, handle window events, add to window queues */
189                 /* WM_init has assigned to ghost the bContext already */
190                 wm_window_process_events(1); 
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