2.5
[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_userdef_types.h"
34 #include "DNA_windowmanager_types.h"
35
36 #include "MEM_guardedalloc.h"
37
38 #include "BLI_blenlib.h"
39
40 #include "BKE_blender.h"
41 #include "BKE_context.h"
42 #include "BKE_idprop.h"
43 #include "BKE_library.h"
44 #include "BKE_global.h"
45 #include "BKE_main.h"
46 #include "BKE_utildefines.h"
47
48 #include "RNA_access.h"
49 #include "RNA_define.h"
50
51 #include "UI_interface.h"
52 #include "UI_resources.h"
53
54 #include "WM_api.h"
55 #include "WM_types.h"
56
57 #include "wm.h"
58 #include "wm_window.h"
59 #include "wm_subwindow.h"
60 #include "wm_event_system.h"
61
62 #include "ED_screen.h"
63
64 static ListBase global_ops= {NULL, NULL};
65
66 /* ************ operator API, exported ********** */
67
68 wmOperatorType *WM_operatortype_find(const char *idname)
69 {
70         wmOperatorType *ot;
71         
72         for(ot= global_ops.first; ot; ot= ot->next) {
73                 if(strncmp(ot->idname, idname, OP_MAX_TYPENAME)==0)
74                    return ot;
75         }
76         printf("search for unknown operator %s\n", idname);
77         return NULL;
78 }
79
80 /* all ops in 1 list (for time being... needs evaluation later) */
81 void WM_operatortype_append(void (*opfunc)(wmOperatorType*))
82 {
83         wmOperatorType *ot;
84         
85         ot= MEM_callocN(sizeof(wmOperatorType), "operatortype");
86         ot->srna= RNA_def_struct(&BLENDER_RNA, "", "OperatorProperties");
87         opfunc(ot);
88         RNA_def_struct_ui_text(ot->srna, ot->name, "DOC_BROKEN"); /* TODO - add a discription to wmOperatorType? */
89         RNA_def_struct_identifier(ot->srna, ot->idname);
90         BLI_addtail(&global_ops, ot);
91 }
92
93 /* ************ default op callbacks, exported *********** */
94
95 static void operator_callback(bContext *C, void *arg, int retval)
96 {
97         wmOperator *op= arg;
98         
99         if(op && retval > 0)
100                 op->type->exec(C, op);
101 }
102
103 void WM_error(bContext *C, char *str)
104 {
105         char buf[148], testbuf[128];
106         
107         BLI_strncpy(testbuf, str, 128);
108         sprintf(buf, "Error %%i%d%%t|%s", ICON_ERROR, testbuf);
109         uiPupmenu(C, 0, operator_callback, NULL, buf);
110         
111 }
112
113 int WM_operator_confirm(bContext *C, wmOperator *op, wmEvent *event)
114 {
115         char buf[512];
116         
117         sprintf(buf, "OK? %%i%d%%t|%s", ICON_HELP, op->type->name);
118         uiPupmenu(C, 0, operator_callback, op, buf);
119         
120         return 1;
121 }
122
123 int WM_operator_winactive(bContext *C)
124 {
125         if(CTX_wm_window(C)==NULL) return 0;
126         return 1;
127 }
128
129 /* ************ window / screen operator definitions ************** */
130
131 static void WM_OT_window_duplicate(wmOperatorType *ot)
132 {
133         ot->name= "Duplicate Window";
134         ot->idname= "WM_OT_window_duplicate";
135         
136         ot->invoke= WM_operator_confirm;
137         ot->exec= wm_window_duplicate_op;
138         ot->poll= WM_operator_winactive;
139 }
140
141 static void WM_OT_save_homefile(wmOperatorType *ot)
142 {
143         ot->name= "Save User Settings";
144         ot->idname= "WM_OT_save_homefile";
145         
146         ot->invoke= WM_operator_confirm;
147         ot->exec= WM_write_homefile;
148         ot->poll= WM_operator_winactive;
149         
150         ot->flag= OPTYPE_REGISTER;
151 }
152
153 /* ********* recent file *********** */
154
155 static void recent_filelist(char *pup)
156 {
157         struct RecentFile *recent;
158         int i, ofs= 0;
159         char *p;
160         
161         p= pup + sprintf(pup, "Open Recent%%t");
162         
163         if (G.sce[0]) {
164                 p+= sprintf(p, "|%s %%x%d", G.sce, 1);
165                 ofs = 1;
166         }
167         
168         for (recent = G.recent_files.first, i=0; (i<U.recent_files) && (recent); recent = recent->next, i++) {
169                 if (strcmp(recent->filename, G.sce)) {
170                         p+= sprintf(p, "|%s %%x%d", recent->filename, i+ofs+1);
171                 }
172         }
173 }
174
175 static void recentfile_callback(bContext *C, void *arg, int event)
176 {
177         
178         if(event>0) {
179                 if (G.sce[0] && (event==1))
180                         WM_read_file(C, G.sce);
181                 else {
182                         struct RecentFile *recent = BLI_findlink(&(G.recent_files), event-2);
183                         if(recent) {
184                                 WM_read_file(C, recent->filename);
185                         }
186                 }
187         }
188 }
189
190 static int wm_recentfile_invoke(bContext *C, wmOperator *op, wmEvent *event)
191 {
192         char pup[2048];
193         
194         recent_filelist(pup);
195         uiPupmenu(C, 0, recentfile_callback, op, pup);
196         
197         return 1;
198 }
199
200 static void WM_OT_open_recentfile(wmOperatorType *ot)
201 {
202         ot->name= "Open Recent File";
203         ot->idname= "WM_OT_open_recentfile";
204         
205         ot->invoke= wm_recentfile_invoke;
206         ot->poll= WM_operator_winactive;
207         
208         ot->flag= OPTYPE_REGISTER;
209 }
210
211 /* *********************** */
212
213 static void WM_OT_window_fullscreen_toggle(wmOperatorType *ot)
214 {
215     ot->name= "Toggle Fullscreen";
216     ot->idname= "WM_OT_window_fullscreen_toggle";
217
218     ot->invoke= WM_operator_confirm;
219     ot->exec= wm_window_fullscreen_toggle_op;
220     ot->poll= WM_operator_winactive;
221 }
222
223 static void WM_OT_exit_blender(wmOperatorType *ot)
224 {
225         ot->name= "Exit Blender";
226         ot->idname= "WM_OT_exit_blender";
227
228         ot->invoke= WM_operator_confirm;
229         ot->exec= wm_exit_blender_op;
230         ot->poll= WM_operator_winactive;
231 }
232
233 /* ************ window gesture operator-callback definitions ************** */
234 /*
235  * These are default callbacks for use in operators requiring gesture input
236  */
237
238 /* **************** Border gesture *************** */
239
240 /* Border gesture has two types:
241    1) WM_GESTURE_CROSS_RECT: starts a cross, on mouse click it changes to border 
242    2) WM_GESTURE_RECT: starts immediate as a border, on mouse click or release it ends
243
244    It stores 4 values (xmin, xmax, ymin, ymax) and event it ended with (event_type)
245 */
246
247 static void border_select_apply(bContext *C, wmOperator *op, int event_type)
248 {
249         wmGesture *gesture= op->customdata;
250         rcti *rect= gesture->customdata;
251         
252         if(rect->xmin > rect->xmax)
253                 SWAP(int, rect->xmin, rect->xmax);
254         if(rect->ymin > rect->ymax)
255                 SWAP(int, rect->ymin, rect->ymax);
256         
257         /* operator arguments and storage. */
258         RNA_int_set(op->ptr, "xmin", rect->xmin);
259         RNA_int_set(op->ptr, "ymin", rect->ymin);
260         RNA_int_set(op->ptr, "xmax", rect->xmax);
261         RNA_int_set(op->ptr, "ymax", rect->ymax);
262         
263         RNA_int_set(op->ptr, "event_type", event_type);
264         
265         op->type->exec(C, op);
266 }
267
268 static void border_select_end(bContext *C, wmOperator *op)
269 {
270         wmGesture *gesture= op->customdata;
271         
272         WM_gesture_end(C, gesture);     /* frees gesture itself, and unregisters from window */
273         op->customdata= NULL;
274
275         ED_area_tag_redraw(CTX_wm_area(C));
276         
277 }
278
279 int WM_border_select_invoke(bContext *C, wmOperator *op, wmEvent *event)
280 {
281         op->customdata= WM_gesture_new(C, event, WM_GESTURE_CROSS_RECT);
282
283         /* add modal handler */
284         WM_event_add_modal_handler(C, &CTX_wm_window(C)->handlers, op);
285         
286         WM_event_add_notifier(C, WM_NOTE_GESTURE_REDRAW, 0, NULL);
287
288         return OPERATOR_RUNNING_MODAL;
289 }
290
291 int WM_border_select_modal(bContext *C, wmOperator *op, wmEvent *event)
292 {
293         wmGesture *gesture= op->customdata;
294         rcti *rect= gesture->customdata;
295         int sx, sy;
296         
297         switch(event->type) {
298                 case MOUSEMOVE:
299                         
300                         wm_subwindow_getorigin(CTX_wm_window(C), gesture->swinid, &sx, &sy);
301                         
302                         if(gesture->type==WM_GESTURE_CROSS_RECT && gesture->mode==0) {
303                                 rect->xmin= rect->xmax= event->x - sx;
304                                 rect->ymin= rect->ymax= event->y - sy;
305                         }
306                         else {
307                                 rect->xmax= event->x - sx;
308                                 rect->ymax= event->y - sy;
309                         }
310                         
311                         WM_event_add_notifier(C, WM_NOTE_GESTURE_REDRAW, 0, NULL);
312
313                         break;
314                         
315                 case LEFTMOUSE:
316                 case MIDDLEMOUSE:
317                 case RIGHTMOUSE:
318                         if(event->val==1) {
319                                 if(gesture->type==WM_GESTURE_CROSS_RECT && gesture->mode==0) {
320                                         gesture->mode= 1;
321                                         WM_event_add_notifier(C, WM_NOTE_GESTURE_REDRAW, 0, NULL);
322                                 }
323                         }
324                         else {
325                                 border_select_apply(C, op, event->type);
326                                 border_select_end(C, op);
327                                 return OPERATOR_FINISHED;
328                         }
329                         break;
330                 case ESCKEY:
331                         border_select_end(C, op);
332                         return OPERATOR_CANCELLED;
333         }
334         return OPERATOR_RUNNING_MODAL;
335 }
336
337 /* **************** Tweak gesture *************** */
338
339 static int tweak_gesture_invoke(bContext *C, wmOperator *op, wmEvent *event)
340 {
341         op->customdata= WM_gesture_new(C, event, WM_GESTURE_TWEAK);
342         
343         /* add modal handler */
344         WM_event_add_modal_handler(C, &CTX_wm_window(C)->handlers, op);
345         
346         WM_event_add_notifier(C, WM_NOTE_GESTURE_REDRAW, 0, NULL);
347         
348         return OPERATOR_RUNNING_MODAL;
349 }
350
351 static void tweak_gesture_end(bContext *C, wmOperator *op)
352 {
353         wmGesture *gesture= op->customdata;
354         
355         WM_gesture_end(C, gesture);     /* frees gesture itself, and unregisters from window */
356         op->customdata= NULL;
357
358         ED_area_tag_redraw(CTX_wm_area(C));
359         
360 }
361
362 static int tweak_gesture_modal(bContext *C, wmOperator *op, wmEvent *event)
363 {
364         wmWindow *window= CTX_wm_window(C);
365         wmGesture *gesture= op->customdata;
366         rcti *rect= gesture->customdata;
367         int sx, sy, val;
368         
369         switch(event->type) {
370                 case MOUSEMOVE:
371                         
372                         wm_subwindow_getorigin(window, gesture->swinid, &sx, &sy);
373                         
374                         rect->xmax= event->x - sx;
375                         rect->ymax= event->y - sy;
376                         
377                         if((val= wm_gesture_evaluate(C, gesture))) {
378                                 wmEvent event;
379                                         
380                                 event= *(window->eventstate);
381                                 if(gesture->event_type==LEFTMOUSE)
382                                         event.type= EVT_TWEAK_L;
383                                 else if(gesture->event_type==RIGHTMOUSE)
384                                         event.type= EVT_TWEAK_R;
385                                 else
386                                         event.type= EVT_TWEAK_M;
387                                 event.val= val;
388                                 /* mouse coords! */
389                                 wm_event_add(window, &event);
390                                 
391                                 tweak_gesture_end(C, op);
392                                 return OPERATOR_FINISHED;
393                         }
394                         else
395                                 WM_event_add_notifier(C, WM_NOTE_GESTURE_REDRAW, 0, NULL);
396                         
397                         break;
398                         
399                 case LEFTMOUSE:
400                 case RIGHTMOUSE:
401                 case MIDDLEMOUSE:
402                         if(gesture->event_type==event->type) {
403                                 wm_gesture_evaluate(C, gesture);
404                                 tweak_gesture_end(C, op);
405                                 return OPERATOR_FINISHED;
406                         }
407                         break;
408         }
409         return OPERATOR_RUNNING_MODAL;
410 }
411
412 void WM_OT_tweak_gesture(wmOperatorType *ot)
413 {
414         ot->name= "Tweak Gesture";
415         ot->idname= "WM_OT_tweak_gesture";
416         
417         ot->invoke= tweak_gesture_invoke;
418         ot->modal= tweak_gesture_modal;
419
420         ot->poll= WM_operator_winactive;
421 }
422
423
424 /* ******************************************************* */
425  
426 /* called on initialize WM_exit() */
427 void wm_operatortype_free(void)
428 {
429         BLI_freelistN(&global_ops);
430 }
431
432 /* called on initialize WM_init() */
433 void wm_operatortype_init(void)
434 {
435         WM_operatortype_append(WM_OT_window_duplicate);
436         WM_operatortype_append(WM_OT_save_homefile);
437         WM_operatortype_append(WM_OT_window_fullscreen_toggle);
438         WM_operatortype_append(WM_OT_exit_blender);
439         WM_operatortype_append(WM_OT_tweak_gesture);
440         WM_operatortype_append(WM_OT_open_recentfile);
441 }
442
443 /* default keymap for windows and screens, only call once per WM */
444 void wm_window_keymap(wmWindowManager *wm)
445 {
446         ListBase *keymap= WM_keymap_listbase(wm, "Window", 0, 0);
447         
448         /* note, this doesn't replace existing keymap items */
449         WM_keymap_verify_item(keymap, "WM_OT_window_duplicate", AKEY, KM_PRESS, KM_CTRL|KM_ALT, 0);
450         WM_keymap_verify_item(keymap, "WM_OT_save_homefile", UKEY, KM_PRESS, KM_CTRL, 0);
451         WM_keymap_verify_item(keymap, "WM_OT_open_recentfile", OKEY, KM_PRESS, KM_CTRL, 0);
452         WM_keymap_verify_item(keymap, "WM_OT_window_fullscreen_toggle", FKEY, KM_PRESS, 0, 0);
453         WM_keymap_verify_item(keymap, "WM_OT_exit_blender", QKEY, KM_PRESS, KM_CTRL, 0);
454 }
455