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