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