d62b24ed8065676d59834f939cfe479edd0d1515
[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
40 #include "WM_api.h"
41 #include "WM_types.h"
42 #include "wm_window.h"
43 #include "wm_event_system.h"
44 #include "wm_event_types.h"
45
46 /* ****************************************************** */
47 #define MAX_OP_REGISTERED       32
48
49 /* all operations get registered in the windowmanager here */
50 /* called on event handling by event_system.c */
51 void WM_operator_register(wmWindowManager *wm, wmOperator *op)
52 {
53         wmOperator *opc= MEM_mallocN(sizeof(wmOperator), "operator registry");
54         int tot;
55         
56         *opc= *op;
57         BLI_addtail(&wm->operators, opc);
58         
59         tot= BLI_countlist(&wm->operators);
60         
61         while(tot>MAX_OP_REGISTERED) {
62                 wmOperator *opt= wm->operators.first;
63                 BLI_remlink(&wm->operators, opt);
64                 MEM_freeN(opt);
65                 tot--;
66         }
67 }
68
69 /* **************** standard keymap for WM ********************** */
70
71 /* default keymap for windows and screens, only call once per WM */
72 static void wm_window_keymap(wmWindowManager *wm)
73 {
74         /* note, this doesn't replace existing keymap items */
75         WM_keymap_verify_item(&wm->windowkeymap, "WM_OT_window_duplicate", AKEY, KM_PRESS, 0, 0);
76         WM_keymap_verify_item(&wm->windowkeymap, "WM_OT_save_homefile", UKEY, KM_PRESS, KM_CTRL, 0);
77         WM_keymap_verify_item(&wm->windowkeymap, "WM_OT_window_fullscreen_toggle", FKEY, KM_PRESS, 0, 0);
78 }
79
80 /* ****************************************** */
81
82 void wm_check(bContext *C)
83 {
84         
85         /* wm context */
86         if(C->wm==NULL) C->wm= G.main->wm.first;
87         if(C->wm==NULL) return;
88         if(C->wm->windows.first==NULL) return;
89         
90         /* case: no open windows at all, for old file reads */
91         wm_window_add_ghostwindows(C->wm);
92         
93         if(C->window==NULL) C->window= C->wm->windrawable;
94         
95         if(C->wm->initialized==0) {
96                 wm_window_keymap(C->wm);
97                 C->wm->initialized= 1;
98         }
99 }
100
101 /* on startup, it adds all data, for matching */
102 void wm_add_default(bContext *C)
103 {
104         wmWindowManager *wm= alloc_libblock(&G.main->wm, ID_WM, "WinMan");
105         wmWindow *win;
106         
107         C->wm= wm;
108         
109         win= wm_window_new(C, C->screen);
110         wm->windrawable= win;
111         C->window= win;
112 }
113
114
115 /* context is allowed to be NULL, do net free wm itself (library.c) */
116 void wm_close_and_free(bContext *C, wmWindowManager *wm)
117 {
118         wmWindow *win;
119         
120         while((win= wm->windows.first)) {
121                 BLI_remlink(&wm->windows, win);
122                 wm_window_free(C, win);
123         }
124         
125         BLI_freelistN(&wm->operators);
126         BLI_freelistN(&wm->windowkeymap);
127         BLI_freelistN(&wm->screenkeymap);
128         
129         if(C && C->wm==wm) C->wm= NULL;
130 }
131
132 void wm_close_and_free_all(bContext *C, ListBase *wmlist)
133 {
134         wmWindowManager *wm;
135         
136         while((wm=wmlist->first)) {
137                 wm_close_and_free(C, wm);
138                 BLI_remlink(wmlist, wm);
139                 MEM_freeN(wm);
140         }
141 }
142
143 void WM_main(bContext *C)
144 {
145         while(1) {
146                 
147                 /* get events from ghost, handle window events, add to window queues */
148                 /* WM_init has assigned to ghost the bContext already */
149                 wm_window_process_events(1); 
150                 
151                 /* per window, all events to the window, screen, area and region handlers */
152                 wm_event_do_handlers(C);
153                 
154         }
155 }
156
157 /*      While (local_event) {
158 Update controller stack if active changed ()
159 Match event to an action()
160 Process_event()
161 Do_notifications()
162 Do_draw_updates()
163 }
164 */              
165