d0319da593bf2393bde7d7850010df39265341d3
[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_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
47 #include "ED_screen.h"
48
49 /* ****************************************************** */
50 #define MAX_OP_REGISTERED       32
51
52 /* all operations get registered in the windowmanager here */
53 /* called on event handling by event_system.c */
54 void wm_operator_register(wmWindowManager *wm, wmOperator *op)
55 {
56         wmOperator *opc= MEM_mallocN(sizeof(wmOperator), "operator registry");
57         int tot;
58         
59         *opc= *op;
60         BLI_addtail(&wm->operators, opc);
61         
62         tot= BLI_countlist(&wm->operators);
63         
64         while(tot>MAX_OP_REGISTERED) {
65                 wmOperator *opt= wm->operators.first;
66                 BLI_remlink(&wm->operators, opt);
67                 MEM_freeN(opt);
68                 tot--;
69         }
70 }
71
72 /* **************** standard keymap for WM ********************** */
73
74 /* default keymap for windows and screens, only call once per WM */
75 static void wm_window_keymap(wmWindowManager *wm)
76 {
77         /* note, this doesn't replace existing keymap items */
78         WM_keymap_verify_item(&wm->windowkeymap, "WM_OT_window_duplicate", AKEY, KM_PRESS, 0, 0);
79         WM_keymap_verify_item(&wm->windowkeymap, "WM_OT_save_homefile", UKEY, KM_PRESS, KM_CTRL, 0);
80         WM_keymap_verify_item(&wm->windowkeymap, "WM_OT_window_fullscreen_toggle", FKEY, KM_PRESS, 0, 0);
81 }
82
83 /* ****************************************** */
84
85 void wm_check(bContext *C)
86 {
87         
88         /* wm context */
89         if(C->wm==NULL) C->wm= G.main->wm.first;
90         if(C->wm==NULL) return;
91         if(C->wm->windows.first==NULL) return;
92         
93         /* case: no open windows at all, for old file reads */
94         wm_window_add_ghostwindows(C->wm);
95         
96         if(C->window==NULL) {
97                 wm_window_make_drawable(C, C->wm->windrawable); 
98         }
99         
100         /* case: fileread */
101         if(C->wm->initialized==0) {
102                 
103                 wm_window_keymap(C->wm);
104                 ed_screen_keymap(C->wm);
105                 
106                 ED_screens_initialize(C->wm);
107                 C->wm->initialized= 1;
108         }
109 }
110
111 /* on startup, it adds all data, for matching */
112 void wm_add_default(bContext *C)
113 {
114         wmWindowManager *wm= alloc_libblock(&G.main->wm, ID_WM, "WinMan");
115         wmWindow *win;
116         
117         C->wm= wm;
118         
119         win= wm_window_new(C, C->screen);
120         wm->windrawable= win;
121         wm_window_make_drawable(C, win); 
122 }
123
124
125 /* context is allowed to be NULL, do net free wm itself (library.c) */
126 void wm_close_and_free(bContext *C, wmWindowManager *wm)
127 {
128         wmOperator *op;
129         wmWindow *win;
130         
131         while((win= wm->windows.first)) {
132                 BLI_remlink(&wm->windows, win);
133                 wm_window_free(C, win);
134         }
135         
136         op= wm->operators.first;
137         while(op) {
138                 /*
139                  * Need this, because if the operator don't have
140                  * properties also don't have group.
141                  */
142                 if(op->properties) {
143                         IDP_FreeGroup(op->properties);
144                         op->properties= NULL;
145                 }
146                 op= op->next;
147         }
148
149         BLI_freelistN(&wm->operators);
150         
151         BLI_freelistN(&wm->windowkeymap);
152         BLI_freelistN(&wm->screenkeymap);
153         
154         BLI_freelistN(&wm->queue);
155         
156         if(C && C->wm==wm) C->wm= NULL;
157 }
158
159 void wm_close_and_free_all(bContext *C, ListBase *wmlist)
160 {
161         wmWindowManager *wm;
162         
163         while((wm=wmlist->first)) {
164                 wm_close_and_free(C, wm);
165                 BLI_remlink(wmlist, wm);
166                 MEM_freeN(wm);
167         }
168 }
169
170 void WM_main(bContext *C)
171 {
172         while(1) {
173                 
174                 /* get events from ghost, handle window events, add to window queues */
175                 /* WM_init has assigned to ghost the bContext already */
176                 wm_window_process_events(1); 
177                 
178                 /* per window, all events to the window, screen, area and region handlers */
179                 wm_event_do_handlers(C);
180                 
181                 /* events have left notes about changes, we handle and cache it */
182                 wm_event_do_notifiers(C);
183                 
184                 /* execute cached changes draw */
185                 wm_draw_update(C);
186         }
187 }
188
189