More testing with new system:
[blender.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.h"
52 #include "wm_window.h"
53 #include "wm_subwindow.h"
54 #include "wm_event_system.h"
55
56 static ListBase global_ops= {NULL, NULL};
57
58 /* ************ operator API, exported ********** */
59
60 wmOperatorType *WM_operatortype_find(const char *idname)
61 {
62         wmOperatorType *ot;
63         
64         for(ot= global_ops.first; ot; ot= ot->next) {
65                 if(strncmp(ot->idname, idname, OP_MAX_TYPENAME)==0)
66                    return ot;
67         }
68         printf("search for unknown operator %s\n", idname);
69         return NULL;
70 }
71
72 /* all ops in 1 list (for time being... needs evaluation later) */
73 void WM_operatortype_append(void (*opfunc)(wmOperatorType*))
74 {
75         wmOperatorType *ot;
76         
77         ot= MEM_callocN(sizeof(wmOperatorType), "operatortype");
78         ot->srna= RNA_def_struct(&BLENDER_RNA, "", "Operator", "");
79         opfunc(ot);
80         RNA_def_struct_identifier(ot->srna, ot->idname, ot->name);
81         BLI_addtail(&global_ops, ot);
82 }
83
84 /* ************ default ops, exported *********** */
85
86 int WM_operator_confirm(bContext *C, wmOperator *op, wmEvent *event)
87 {
88 //      if(okee(op->type->name)) {
89 //              return op->type->exec(C, op);
90 //      }
91         return 0;
92 }
93 int WM_operator_winactive(bContext *C)
94 {
95         if(C->window==NULL) return 0;
96         return 1;
97 }
98
99 /* ************ window / screen operator definitions ************** */
100
101 static void WM_OT_window_duplicate(wmOperatorType *ot)
102 {
103         ot->name= "Duplicate Window";
104         ot->idname= "WM_OT_window_duplicate";
105         
106         ot->invoke= NULL; //WM_operator_confirm;
107         ot->exec= wm_window_duplicate_op;
108         ot->poll= WM_operator_winactive;
109 }
110
111 static void WM_OT_save_homefile(wmOperatorType *ot)
112 {
113         ot->name= "Save User Settings";
114         ot->idname= "WM_OT_save_homefile";
115         
116         ot->invoke= NULL; //WM_operator_confirm;
117         ot->exec= WM_write_homefile;
118         ot->poll= WM_operator_winactive;
119         
120         ot->flag= OPTYPE_REGISTER;
121 }
122
123 static void WM_OT_window_fullscreen_toggle(wmOperatorType *ot)
124 {
125     ot->name= "Toggle Fullscreen";
126     ot->idname= "WM_OT_window_fullscreen_toggle";
127
128     ot->invoke= NULL;
129     ot->exec= wm_window_fullscreen_toggle_op;
130     ot->poll= WM_operator_winactive;
131 }
132
133 static void WM_OT_exit_blender(wmOperatorType *ot)
134 {
135         ot->name= "Exit Blender";
136         ot->idname= "WM_OT_exit_blender";
137
138         ot->invoke= NULL; /* do confirm stuff */
139         ot->exec= wm_exit_blender_op;
140         ot->poll= WM_operator_winactive;
141 }
142
143 /* ************ window gesture operator-callback definitions ************** */
144 /*
145  * These are default callbacks for use in operators requiring gesture input
146  */
147
148 /* **************** Border gesture *************** */
149
150 /* Border gesture has two types:
151    1) WM_GESTURE_CROSS_RECT: starts a cross, on mouse click it changes to border 
152    2) WM_GESTURE_RECT: starts immediate as a border, on mouse click or release it ends
153
154    It stores 4 values (xmin, xmax, ymin, ymax) and event it ended with (event_type)
155 */
156
157 static void border_select_apply(bContext *C, wmOperator *op, int event_type)
158 {
159         wmGesture *gesture= op->customdata;
160         rcti *rect= gesture->customdata;
161         
162         /* operator arguments and storage. */
163         RNA_int_set(op->ptr, "xmin", rect->xmin);
164         RNA_int_set(op->ptr, "ymin", rect->ymin);
165         RNA_int_set(op->ptr, "xmax", rect->xmax);
166         RNA_int_set(op->ptr, "ymax", rect->ymax);
167         
168         RNA_int_set(op->ptr, "event_type", event_type);
169         
170         op->type->exec(C, op);
171 }
172
173 static void border_select_end(bContext *C, wmOperator *op)
174 {
175         wmGesture *gesture= op->customdata;
176         
177         WM_gesture_end(C, gesture);     /* frees gesture itself, and unregisters from window */
178         op->customdata= NULL;
179         WM_event_remove_modal_handler(&C->window->handlers, op);
180         WM_event_add_notifier(C->wm, C->window, gesture->swinid, WM_NOTE_AREA_REDRAW, 0, NULL);
181         
182 }
183
184 int WM_border_select_invoke(bContext *C, wmOperator *op, wmEvent *event)
185 {
186         op->customdata= WM_gesture_new(C, event, WM_GESTURE_CROSS_RECT);
187
188         /* add modal handler */
189         WM_event_add_modal_handler(&C->window->handlers, op);
190         
191         WM_event_add_notifier(C->wm, C->window, C->screen->subwinactive, WM_NOTE_GESTURE_REDRAW, 0, NULL);
192
193         return OPERATOR_RUNNING_MODAL;
194 }
195
196 int WM_border_select_modal(bContext *C, wmOperator *op, wmEvent *event)
197 {
198         wmGesture *gesture= op->customdata;
199         rcti *rect= gesture->customdata;
200         int sx, sy;
201         
202         switch(event->type) {
203                 case MOUSEMOVE:
204                         
205                         wm_subwindow_getorigin(C->window, gesture->swinid, &sx, &sy);
206                         
207                         if(gesture->type==WM_GESTURE_CROSS_RECT && gesture->mode==0) {
208                                 rect->xmin= rect->xmax= event->x - sx;
209                                 rect->ymin= rect->ymax= event->y - sy;
210                         }
211                         else {
212                                 rect->xmax= event->x - sx;
213                                 rect->ymax= event->y - sy;
214                         }
215                         
216                         WM_event_add_notifier(C->wm, C->window, gesture->swinid, WM_NOTE_GESTURE_REDRAW, 0, NULL);
217
218                         break;
219                         
220                 case LEFTMOUSE:
221                 case MIDDLEMOUSE:
222                 case RIGHTMOUSE:
223                         if(event->val==1) {
224                                 if(gesture->type==WM_GESTURE_CROSS_RECT && gesture->mode==0) {
225                                         gesture->mode= 1;
226                                         WM_event_add_notifier(C->wm, C->window, gesture->swinid, WM_NOTE_GESTURE_REDRAW, 0, NULL);
227                                 }
228                         }
229                         else {
230                                 border_select_apply(C, op, event->type);
231                                 border_select_end(C, op);
232                                 return OPERATOR_FINISHED;
233                         }
234                         break;
235                 case ESCKEY:
236                         border_select_end(C, op);
237                         return OPERATOR_CANCELLED;
238         }
239         return OPERATOR_RUNNING_MODAL;
240 }
241
242 /* **************** Tweak gesture *************** */
243
244 static int tweak_gesture_invoke(bContext *C, wmOperator *op, wmEvent *event)
245 {
246         op->customdata= WM_gesture_new(C, event, WM_GESTURE_TWEAK);
247         
248         /* add modal handler */
249         WM_event_add_modal_handler(&C->window->handlers, op);
250         
251         WM_event_add_notifier(C->wm, C->window, C->screen->subwinactive, WM_NOTE_GESTURE_REDRAW, 0, NULL);
252         
253         return OPERATOR_RUNNING_MODAL;
254 }
255
256 static void tweak_gesture_end(bContext *C, wmOperator *op)
257 {
258         wmGesture *gesture= op->customdata;
259         
260         WM_gesture_end(C, gesture);     /* frees gesture itself, and unregisters from window */
261         op->customdata= NULL;
262         WM_event_remove_modal_handler(&C->window->handlers, op);
263         WM_event_add_notifier(C->wm, C->window, gesture->swinid, WM_NOTE_AREA_REDRAW, 0, NULL);
264         
265 }
266
267 static int tweak_gesture_modal(bContext *C, wmOperator *op, wmEvent *event)
268 {
269         wmGesture *gesture= op->customdata;
270         rcti *rect= gesture->customdata;
271         int sx, sy, val;
272         
273         switch(event->type) {
274                 case MOUSEMOVE:
275                         
276                         wm_subwindow_getorigin(C->window, gesture->swinid, &sx, &sy);
277                         
278                         rect->xmax= event->x - sx;
279                         rect->ymax= event->y - sy;
280                         
281                         if((val= wm_gesture_evaluate(C, gesture))) {
282                                 wmEvent event;
283                                         
284                                 event= *(C->window->eventstate);
285                                 event.type= EVT_TWEAK;
286                                 event.val= val;
287                                 /* mouse coords! */
288                                 wm_event_add(C->window, &event);
289                                 
290                                 tweak_gesture_end(C, op);
291                                 return OPERATOR_FINISHED;
292                         }
293                         else
294                                 WM_event_add_notifier(C->wm, C->window, gesture->swinid, WM_NOTE_GESTURE_REDRAW, 0, NULL);
295                         
296                         break;
297                         
298                 case LEFTMOUSE:
299                 case RIGHTMOUSE:
300                 case MIDDLEMOUSE:
301                         if(gesture->event_type==event->type) {
302                                 wm_gesture_evaluate(C, gesture);
303                                 tweak_gesture_end(C, op);
304                                 return OPERATOR_FINISHED;
305                         }
306                         break;
307         }
308         return OPERATOR_RUNNING_MODAL;
309 }
310
311 void WM_OT_tweak_gesture(wmOperatorType *ot)
312 {
313         ot->name= "Tweak Gesture";
314         ot->idname= "WM_OT_tweak_gesture";
315         
316         ot->invoke= tweak_gesture_invoke;
317         ot->modal= tweak_gesture_modal;
318
319         ot->poll= WM_operator_winactive;
320 }
321
322
323 /* ******************************************************* */
324  
325 /* called on initialize WM_exit() */
326 void wm_operatortype_free(void)
327 {
328         BLI_freelistN(&global_ops);
329 }
330
331 /* called on initialize WM_init() */
332 void wm_operatortype_init(void)
333 {
334         WM_operatortype_append(WM_OT_window_duplicate);
335         WM_operatortype_append(WM_OT_save_homefile);
336         WM_operatortype_append(WM_OT_window_fullscreen_toggle);
337         WM_operatortype_append(WM_OT_exit_blender);
338         WM_operatortype_append(WM_OT_tweak_gesture);
339 }
340
341 /* default keymap for windows and screens, only call once per WM */
342 void wm_window_keymap(wmWindowManager *wm)
343 {
344         /* note, this doesn't replace existing keymap items */
345         WM_keymap_verify_item(&wm->windowkeymap, "WM_OT_window_duplicate", AKEY, KM_PRESS, 0, 0);
346         WM_keymap_verify_item(&wm->windowkeymap, "WM_OT_save_homefile", UKEY, KM_PRESS, KM_CTRL, 0);
347         WM_keymap_verify_item(&wm->windowkeymap, "WM_OT_window_fullscreen_toggle", FKEY, KM_PRESS, 0, 0);
348         WM_keymap_verify_item(&wm->windowkeymap, "WM_OT_exit_blender", QKEY, KM_PRESS, KM_CTRL, 0);
349 }
350