RNA
[blender-staging.git] / source / blender / windowmanager / intern / wm_operators.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 <string.h>
30
31 #include "DNA_ID.h"
32 #include "DNA_screen_types.h"
33 #include "DNA_windowmanager_types.h"
34
35 #include "MEM_guardedalloc.h"
36
37 #include "BLI_blenlib.h"
38
39 #include "BKE_blender.h"
40 #include "BKE_global.h"
41 #include "BKE_library.h"
42 #include "BKE_main.h"
43 #include "BKE_idprop.h"
44
45 #include "RNA_access.h"
46 #include "RNA_define.h"
47
48 #include "WM_api.h"
49 #include "WM_types.h"
50
51 #include "wm_window.h"
52 #include "wm_subwindow.h"
53 #include "wm_event_system.h"
54
55 static ListBase global_ops= {NULL, NULL};
56
57 /* ************ operator API, exported ********** */
58
59 wmOperatorType *WM_operatortype_find(const char *idname)
60 {
61         wmOperatorType *ot;
62         
63         for(ot= global_ops.first; ot; ot= ot->next) {
64                 if(strncmp(ot->idname, idname, OP_MAX_TYPENAME)==0)
65                    return ot;
66         }
67         return NULL;
68 }
69
70 /* all ops in 1 list (for time being... needs evaluation later) */
71 void WM_operatortype_append(void (*opfunc)(wmOperatorType*))
72 {
73         wmOperatorType *ot;
74         
75         ot= MEM_callocN(sizeof(wmOperatorType), "operatortype");
76         ot->rna= RNA_def_struct(&BLENDER_RNA, "", "Operator", "");
77         opfunc(ot);
78         RNA_def_struct_identifier(ot->rna, ot->idname, ot->name);
79         BLI_addtail(&global_ops, ot);
80 }
81
82 /* ************ default ops, exported *********** */
83
84 int WM_operator_confirm(bContext *C, wmOperator *op, wmEvent *event)
85 {
86 //      if(okee(op->type->name)) {
87 //              return op->type->exec(C, op);
88 //      }
89         return 0;
90 }
91 int WM_operator_winactive(bContext *C)
92 {
93         if(C->window==NULL) return 0;
94         return 1;
95 }
96
97 /* ************ window / screen operator definitions ************** */
98
99 static void WM_OT_window_duplicate(wmOperatorType *ot)
100 {
101         ot->name= "Duplicate Window";
102         ot->idname= "WM_OT_window_duplicate";
103         
104         ot->invoke= NULL; //WM_operator_confirm;
105         ot->exec= wm_window_duplicate_op;
106         ot->poll= WM_operator_winactive;
107 }
108
109 static void WM_OT_save_homefile(wmOperatorType *ot)
110 {
111         ot->name= "Save User Settings";
112         ot->idname= "WM_OT_save_homefile";
113         
114         ot->invoke= NULL; //WM_operator_confirm;
115         ot->exec= WM_write_homefile;
116         ot->poll= WM_operator_winactive;
117         
118         ot->flag= OPTYPE_REGISTER;
119 }
120
121 static void WM_OT_window_fullscreen_toggle(wmOperatorType *ot)
122 {
123     ot->name= "Toggle Fullscreen";
124     ot->idname= "WM_OT_window_fullscreen_toggle";
125
126     ot->invoke= NULL;
127     ot->exec= wm_window_fullscreen_toggle_op;
128     ot->poll= WM_operator_winactive;
129 }
130
131 static void WM_OT_exit_blender(wmOperatorType *ot)
132 {
133         ot->name= "Exit Blender";
134         ot->idname= "WM_OT_exit_blender";
135
136         ot->invoke= NULL; /* do confirm stuff */
137         ot->exec= wm_exit_blender_op;
138         ot->poll= WM_operator_winactive;
139 }
140
141 /* ************ window gesture operator-callback definitions ************** */
142 /*
143  * These are default callbacks for use in operators requiring gesture input
144  */
145
146 static void border_select_apply(bContext *C, wmOperator *op)
147 {
148         wmGesture *gesture= op->customdata;
149         rcti *rect= gesture->customdata;
150         
151         /* operator arguments and storage. */
152         RNA_int_default(op->rna, "xmin", rect->xmin);
153         RNA_int_default(op->rna, "ymin", rect->ymin);
154         RNA_int_default(op->rna, "xmax", rect->xmax);
155         RNA_int_default(op->rna, "ymax", rect->ymax);
156         
157         op->type->exec(C, op);
158 }
159
160 static void border_select_end(bContext *C, wmOperator *op)
161 {
162         wmGesture *gesture= op->customdata;
163         
164         WM_gesture_end(C, gesture);     /* frees gesture itself, and unregisters from window */
165         op->customdata= NULL;
166         WM_event_remove_modal_handler(&C->window->handlers, op);
167         WM_event_add_notifier(C->wm, C->window, gesture->swinid, WM_NOTE_AREA_REDRAW, 0, NULL);
168         
169 }
170
171 int WM_border_select_invoke(bContext *C, wmOperator *op, wmEvent *event)
172 {
173         op->customdata= WM_gesture_new(C, event, WM_GESTURE_CROSS_RECT);
174
175         /* add modal handler */
176         WM_event_add_modal_handler(&C->window->handlers, op);
177         
178         WM_event_add_notifier(C->wm, C->window, C->screen->subwinactive, WM_NOTE_GESTURE_REDRAW, 0, NULL);
179
180         return OPERATOR_RUNNING_MODAL;
181 }
182
183 int WM_border_select_modal(bContext *C, wmOperator *op, wmEvent *event)
184 {
185         wmGesture *gesture= op->customdata;
186         rcti *rect= gesture->customdata;
187         int sx, sy;
188         
189         switch(event->type) {
190                 case MOUSEMOVE:
191                         
192                         wm_subwindow_getorigin(C->window, gesture->swinid, &sx, &sy);
193                         
194                         if(gesture->type==WM_GESTURE_CROSS_RECT && gesture->mode==0) {
195                                 rect->xmin= rect->xmax= event->x - sx;
196                                 rect->ymin= rect->ymax= event->y - sy;
197                         }
198                         else {
199                                 rect->xmax= event->x - sx;
200                                 rect->ymax= event->y - sy;
201                         }
202                         
203                         WM_event_add_notifier(C->wm, C->window, gesture->swinid, WM_NOTE_GESTURE_REDRAW, 0, NULL);
204
205                         break;
206                         
207                 case LEFTMOUSE:
208                         if(event->val==1) {
209                                 if(gesture->type==WM_GESTURE_CROSS_RECT && gesture->mode==0) {
210                                         gesture->mode= 1;
211                                         WM_event_add_notifier(C->wm, C->window, gesture->swinid, WM_NOTE_GESTURE_REDRAW, 0, NULL);
212                                 }
213                         }
214                         else {
215                                 border_select_apply(C, op);
216                                 border_select_end(C, op);
217                                 return OPERATOR_FINISHED;
218                         }
219                         break;
220                 case ESCKEY:
221                         border_select_end(C, op);
222                         return OPERATOR_CANCELLED;
223         }
224         return OPERATOR_RUNNING_MODAL;
225 }
226
227 /* ******************************************************* */
228  
229 /* called on initialize WM_exit() */
230 void wm_operatortype_free(void)
231 {
232         BLI_freelistN(&global_ops);
233 }
234
235 /* called on initialize WM_init() */
236 void wm_operatortype_init(void)
237 {
238         WM_operatortype_append(WM_OT_window_duplicate);
239         WM_operatortype_append(WM_OT_save_homefile);
240         WM_operatortype_append(WM_OT_window_fullscreen_toggle);
241         WM_operatortype_append(WM_OT_exit_blender);
242 }
243
244 /* default keymap for windows and screens, only call once per WM */
245 void wm_window_keymap(wmWindowManager *wm)
246 {
247         /* note, this doesn't replace existing keymap items */
248         WM_keymap_verify_item(&wm->windowkeymap, "WM_OT_window_duplicate", AKEY, KM_PRESS, 0, 0);
249         WM_keymap_verify_item(&wm->windowkeymap, "WM_OT_save_homefile", UKEY, KM_PRESS, KM_CTRL, 0);
250         WM_keymap_verify_item(&wm->windowkeymap, "WM_OT_window_fullscreen_toggle", FKEY, KM_PRESS, 0, 0);
251         WM_keymap_verify_item(&wm->windowkeymap, "WM_OT_exit_blender", QKEY, KM_PRESS, KM_CTRL, 0);
252 }
253