2.5
[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_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 wmOperatorType *WM_operatortype_first(void)
81 {
82         return global_ops.first;
83 }
84
85 /* all ops in 1 list (for time being... needs evaluation later) */
86 void WM_operatortype_append(void (*opfunc)(wmOperatorType*))
87 {
88         wmOperatorType *ot;
89         
90         ot= MEM_callocN(sizeof(wmOperatorType), "operatortype");
91         ot->srna= RNA_def_struct(&BLENDER_RNA, "", "OperatorProperties");
92         opfunc(ot);
93         RNA_def_struct_ui_text(ot->srna, ot->name, "DOC_BROKEN"); /* TODO - add a discription to wmOperatorType? */
94         RNA_def_struct_identifier(ot->srna, ot->idname);
95         BLI_addtail(&global_ops, ot);
96 }
97
98 /* ************ default op callbacks, exported *********** */
99
100 static void operator_callback(bContext *C, void *arg, int retval)
101 {
102         wmOperator *op= arg;
103         
104         if(op && retval > 0)
105                 op->type->exec(C, op);
106         
107 }
108
109 /* call anywhere */
110 void WM_error(bContext *C, char *str)
111 {
112         char buf[148], testbuf[128];
113         
114         BLI_strncpy(testbuf, str, 128);
115         sprintf(buf, "Error %%i%d%%t|%s", ICON_ERROR, testbuf);
116         uiPupmenu(C, 0, operator_callback, NULL, buf);
117         
118 }
119
120 /* op->invoke */
121 int WM_operator_confirm(bContext *C, wmOperator *op, wmEvent *event)
122 {
123         char buf[512];
124         
125         sprintf(buf, "OK? %%i%d%%t|%s", ICON_HELP, op->type->name);
126         uiPupmenuOperator(C, 0, op, NULL, buf);
127         
128         return OPERATOR_RUNNING_MODAL;
129 }
130
131 /* op->poll */
132 int WM_operator_winactive(bContext *C)
133 {
134         if(CTX_wm_window(C)==NULL) return 0;
135         return 1;
136 }
137
138 /* ************ window / screen operator definitions ************** */
139
140 static void WM_OT_window_duplicate(wmOperatorType *ot)
141 {
142         ot->name= "Duplicate Window";
143         ot->idname= "WM_OT_window_duplicate";
144         
145         ot->invoke= WM_operator_confirm;
146         ot->exec= wm_window_duplicate_op;
147         ot->poll= WM_operator_winactive;
148 }
149
150 static void WM_OT_save_homefile(wmOperatorType *ot)
151 {
152         ot->name= "Save User Settings";
153         ot->idname= "WM_OT_save_homefile";
154         
155         ot->invoke= WM_operator_confirm;
156         ot->exec= WM_write_homefile;
157         ot->poll= WM_operator_winactive;
158         
159         ot->flag= OPTYPE_REGISTER;
160 }
161
162 /* ********* recent file *********** */
163
164 static void recent_filelist(char *pup)
165 {
166         struct RecentFile *recent;
167         int i, ofs= 0;
168         char *p;
169         
170         p= pup + sprintf(pup, "Open Recent%%t");
171         
172         if (G.sce[0]) {
173                 p+= sprintf(p, "|%s %%x%d", G.sce, 1);
174                 ofs = 1;
175         }
176         
177         for (recent = G.recent_files.first, i=0; (i<U.recent_files) && (recent); recent = recent->next, i++) {
178                 if (strcmp(recent->filename, G.sce)) {
179                         p+= sprintf(p, "|%s %%x%d", recent->filename, i+ofs+1);
180                 }
181         }
182 }
183
184 static int recentfile_exec(bContext *C, wmOperator *op)
185 {
186         int event= RNA_enum_get(op->ptr, "nr");
187         
188         if(event>0) {
189                 if (G.sce[0] && (event==1))
190                         WM_read_file(C, G.sce);
191                 else {
192                         struct RecentFile *recent = BLI_findlink(&(G.recent_files), event-2);
193                         if(recent) {
194                                 WM_read_file(C, recent->filename);
195                         }
196                 }
197         }
198         return 0;
199 }
200
201 static int wm_recentfile_invoke(bContext *C, wmOperator *op, wmEvent *event)
202 {
203         char pup[2048];
204         
205         recent_filelist(pup);
206         uiPupmenuOperator(C, 0, op, "nr", pup);
207         
208         return OPERATOR_RUNNING_MODAL;
209 }
210
211 static void WM_OT_open_recentfile(wmOperatorType *ot)
212 {
213         ot->name= "Open Recent File";
214         ot->idname= "WM_OT_open_recentfile";
215         
216         ot->invoke= wm_recentfile_invoke;
217         ot->exec= recentfile_exec;
218         ot->poll= WM_operator_winactive;
219         
220         ot->flag= OPTYPE_REGISTER;
221         
222         RNA_def_property(ot->srna, "nr", PROP_ENUM, PROP_NONE);
223
224 }
225
226 /* *********************** */
227
228 static void WM_OT_window_fullscreen_toggle(wmOperatorType *ot)
229 {
230     ot->name= "Toggle Fullscreen";
231     ot->idname= "WM_OT_window_fullscreen_toggle";
232
233     ot->invoke= WM_operator_confirm;
234     ot->exec= wm_window_fullscreen_toggle_op;
235     ot->poll= WM_operator_winactive;
236 }
237
238 static void WM_OT_exit_blender(wmOperatorType *ot)
239 {
240         ot->name= "Exit Blender";
241         ot->idname= "WM_OT_exit_blender";
242
243         ot->invoke= WM_operator_confirm;
244         ot->exec= wm_exit_blender_op;
245         ot->poll= WM_operator_winactive;
246 }
247
248 /* ************ window gesture operator-callback definitions ************** */
249 /*
250  * These are default callbacks for use in operators requiring gesture input
251  */
252
253 /* **************** Border gesture *************** */
254
255 /* Border gesture has two types:
256    1) WM_GESTURE_CROSS_RECT: starts a cross, on mouse click it changes to border 
257    2) WM_GESTURE_RECT: starts immediate as a border, on mouse click or release it ends
258
259    It stores 4 values (xmin, xmax, ymin, ymax) and event it ended with (event_type)
260 */
261
262 static void border_apply(bContext *C, wmOperator *op, int event_type)
263 {
264         wmGesture *gesture= op->customdata;
265         rcti *rect= gesture->customdata;
266         
267         if(rect->xmin > rect->xmax)
268                 SWAP(int, rect->xmin, rect->xmax);
269         if(rect->ymin > rect->ymax)
270                 SWAP(int, rect->ymin, rect->ymax);
271         
272         /* operator arguments and storage. */
273         RNA_int_set(op->ptr, "xmin", rect->xmin);
274         RNA_int_set(op->ptr, "ymin", rect->ymin);
275         RNA_int_set(op->ptr, "xmax", rect->xmax);
276         RNA_int_set(op->ptr, "ymax", rect->ymax);
277         if( RNA_struct_find_property(op->ptr, "event_type") )
278                 RNA_int_set(op->ptr, "event_type", event_type);
279         
280         op->type->exec(C, op);
281 }
282
283 static void border_end(bContext *C, wmOperator *op)
284 {
285         wmGesture *gesture= op->customdata;
286         
287         WM_gesture_end(C, gesture);     /* frees gesture itself, and unregisters from window */
288         op->customdata= NULL;
289
290         ED_area_tag_redraw(CTX_wm_area(C));
291         
292 }
293
294 int WM_border_select_invoke(bContext *C, wmOperator *op, wmEvent *event)
295 {
296         op->customdata= WM_gesture_new(C, event, WM_GESTURE_CROSS_RECT);
297
298         /* add modal handler */
299         WM_event_add_modal_handler(C, &CTX_wm_window(C)->handlers, op);
300         
301         WM_event_add_notifier(C, WM_NOTE_GESTURE_REDRAW, 0, NULL);
302
303         return OPERATOR_RUNNING_MODAL;
304 }
305
306 int WM_border_select_modal(bContext *C, wmOperator *op, wmEvent *event)
307 {
308         wmGesture *gesture= op->customdata;
309         rcti *rect= gesture->customdata;
310         int sx, sy;
311         
312         switch(event->type) {
313                 case MOUSEMOVE:
314                         
315                         wm_subwindow_getorigin(CTX_wm_window(C), gesture->swinid, &sx, &sy);
316                         
317                         if(gesture->type==WM_GESTURE_CROSS_RECT && gesture->mode==0) {
318                                 rect->xmin= rect->xmax= event->x - sx;
319                                 rect->ymin= rect->ymax= event->y - sy;
320                         }
321                         else {
322                                 rect->xmax= event->x - sx;
323                                 rect->ymax= event->y - sy;
324                         }
325                         
326                         WM_event_add_notifier(C, WM_NOTE_GESTURE_REDRAW, 0, NULL);
327
328                         break;
329                         
330                 case LEFTMOUSE:
331                 case MIDDLEMOUSE:
332                 case RIGHTMOUSE:
333                         if(event->val==1) {
334                                 if(gesture->type==WM_GESTURE_CROSS_RECT && gesture->mode==0) {
335                                         gesture->mode= 1;
336                                         WM_event_add_notifier(C, WM_NOTE_GESTURE_REDRAW, 0, NULL);
337                                 }
338                         }
339                         else {
340                                 border_apply(C, op, event->type);
341                                 border_end(C, op);
342                                 return OPERATOR_FINISHED;
343                         }
344                         break;
345                 case ESCKEY:
346                         border_end(C, op);
347                         return OPERATOR_CANCELLED;
348         }
349         return OPERATOR_RUNNING_MODAL;
350 }
351
352 /* **************** circle gesture *************** */
353 /* works now only for selection or modal paint stuff, calls exec while hold mouse */
354
355 int WM_gesture_circle_invoke(bContext *C, wmOperator *op, wmEvent *event)
356 {
357         op->customdata= WM_gesture_new(C, event, WM_GESTURE_CIRCLE);
358         
359         /* add modal handler */
360         WM_event_add_modal_handler(C, &CTX_wm_window(C)->handlers, op);
361         
362         WM_event_add_notifier(C, WM_NOTE_GESTURE_REDRAW, 0, NULL);
363         
364         return OPERATOR_RUNNING_MODAL;
365 }
366
367 static void gesture_circle_end(bContext *C, wmOperator *op)
368 {
369         wmGesture *gesture= op->customdata;
370         
371         WM_gesture_end(C, gesture);     /* frees gesture itself, and unregisters from window */
372         op->customdata= NULL;
373         
374         ED_area_tag_redraw(CTX_wm_area(C));
375 }
376
377 static void gesture_circle_apply(bContext *C, wmOperator *op, int event_type)
378 {
379         wmGesture *gesture= op->customdata;
380         rcti *rect= gesture->customdata;
381         
382         /* operator arguments and storage. */
383         RNA_int_set(op->ptr, "x", rect->xmin);
384         RNA_int_set(op->ptr, "y", rect->ymin);
385         RNA_int_set(op->ptr, "radius", rect->xmax);
386         if( RNA_struct_find_property(op->ptr, "event_type") )
387                 RNA_int_set(op->ptr, "event_type", event_type);
388         
389         if(op->type->exec)
390                 op->type->exec(C, op);
391 }
392
393 int WM_gesture_circle_modal(bContext *C, wmOperator *op, wmEvent *event)
394 {
395         wmGesture *gesture= op->customdata;
396         rcti *rect= gesture->customdata;
397         int sx, sy;
398         
399         switch(event->type) {
400                 case MOUSEMOVE:
401                         
402                         wm_subwindow_getorigin(CTX_wm_window(C), gesture->swinid, &sx, &sy);
403                         
404                         rect->xmin= event->x - sx;
405                         rect->ymin= event->y - sy;
406                         
407                         WM_event_add_notifier(C, WM_NOTE_GESTURE_REDRAW, 0, NULL);
408                         
409                         if(gesture->mode)
410                                 gesture_circle_apply(C, op, event->type);
411
412                         break;
413                 case WHEELUPMOUSE:
414                         rect->xmax += 2 + rect->xmax/10;
415                         WM_event_add_notifier(C, WM_NOTE_GESTURE_REDRAW, 0, NULL);
416                         break;
417                 case WHEELDOWNMOUSE:
418                         rect->xmax -= 2 + rect->xmax/10;
419                         if(rect->xmax < 1) rect->xmax= 1;
420                         WM_event_add_notifier(C, WM_NOTE_GESTURE_REDRAW, 0, NULL);
421                         break;
422                 case LEFTMOUSE:
423                 case MIDDLEMOUSE:
424                 case RIGHTMOUSE:
425                         if(event->val==0) {     /* key release */
426                                 gesture_circle_end(C, op);
427                                 return OPERATOR_FINISHED;
428                         }
429                         else
430                                 gesture->mode= 1;
431
432                         break;
433                 case ESCKEY:
434                         gesture_circle_end(C, op);
435                         return OPERATOR_CANCELLED;
436         }
437         return OPERATOR_RUNNING_MODAL;
438 }
439
440 #if 0
441 /* template to copy from */
442 void WM_OT_circle_gesture(wmOperatorType *ot)
443 {
444         ot->name= "Circle Gesture";
445         ot->idname= "WM_OT_circle_gesture";
446         
447         ot->invoke= WM_gesture_circle_invoke;
448         ot->modal= WM_gesture_circle_modal;
449         
450         ot->poll= WM_operator_winactive;
451         
452         RNA_def_property(ot->srna, "x", PROP_INT, PROP_NONE);
453         RNA_def_property(ot->srna, "y", PROP_INT, PROP_NONE);
454         RNA_def_property(ot->srna, "radius", PROP_INT, PROP_NONE);
455
456 }
457 #endif
458
459 /* **************** Tweak gesture *************** */
460
461 static int tweak_gesture_invoke(bContext *C, wmOperator *op, wmEvent *event)
462 {
463         op->customdata= WM_gesture_new(C, event, WM_GESTURE_TWEAK);
464         
465         /* add modal handler */
466         WM_event_add_modal_handler(C, &CTX_wm_window(C)->handlers, op);
467         
468         WM_event_add_notifier(C, WM_NOTE_GESTURE_REDRAW, 0, NULL);
469         
470         return OPERATOR_RUNNING_MODAL;
471 }
472
473 static void tweak_gesture_end(bContext *C, wmOperator *op)
474 {
475         wmGesture *gesture= op->customdata;
476         
477         WM_gesture_end(C, gesture);     /* frees gesture itself, and unregisters from window */
478         op->customdata= NULL;
479
480         ED_area_tag_redraw(CTX_wm_area(C));
481         
482 }
483
484 static int tweak_gesture_modal(bContext *C, wmOperator *op, wmEvent *event)
485 {
486         wmWindow *window= CTX_wm_window(C);
487         wmGesture *gesture= op->customdata;
488         rcti *rect= gesture->customdata;
489         int sx, sy, val;
490         
491         switch(event->type) {
492                 case MOUSEMOVE:
493                         
494                         wm_subwindow_getorigin(window, gesture->swinid, &sx, &sy);
495                         
496                         rect->xmax= event->x - sx;
497                         rect->ymax= event->y - sy;
498                         
499                         if((val= wm_gesture_evaluate(C, gesture))) {
500                                 wmEvent event;
501                                         
502                                 event= *(window->eventstate);
503                                 if(gesture->event_type==LEFTMOUSE)
504                                         event.type= EVT_TWEAK_L;
505                                 else if(gesture->event_type==RIGHTMOUSE)
506                                         event.type= EVT_TWEAK_R;
507                                 else
508                                         event.type= EVT_TWEAK_M;
509                                 event.val= val;
510                                 /* mouse coords! */
511                                 wm_event_add(window, &event);
512                                 
513                                 tweak_gesture_end(C, op);
514                                 return OPERATOR_FINISHED;
515                         }
516                         else
517                                 WM_event_add_notifier(C, WM_NOTE_GESTURE_REDRAW, 0, NULL);
518                         
519                         break;
520                         
521                 case LEFTMOUSE:
522                 case RIGHTMOUSE:
523                 case MIDDLEMOUSE:
524                         if(gesture->event_type==event->type) {
525                                 wm_gesture_evaluate(C, gesture);
526                                 tweak_gesture_end(C, op);
527                                 return OPERATOR_FINISHED;
528                         }
529                         break;
530         }
531         return OPERATOR_RUNNING_MODAL;
532 }
533
534 void WM_OT_tweak_gesture(wmOperatorType *ot)
535 {
536         ot->name= "Tweak Gesture";
537         ot->idname= "WM_OT_tweak_gesture";
538         
539         ot->invoke= tweak_gesture_invoke;
540         ot->modal= tweak_gesture_modal;
541
542         ot->poll= WM_operator_winactive;
543 }
544
545
546 /* ******************************************************* */
547  
548 /* called on initialize WM_exit() */
549 void wm_operatortype_free(void)
550 {
551         BLI_freelistN(&global_ops);
552 }
553
554 /* called on initialize WM_init() */
555 void wm_operatortype_init(void)
556 {
557         WM_operatortype_append(WM_OT_window_duplicate);
558         WM_operatortype_append(WM_OT_save_homefile);
559         WM_operatortype_append(WM_OT_window_fullscreen_toggle);
560         WM_operatortype_append(WM_OT_exit_blender);
561         WM_operatortype_append(WM_OT_tweak_gesture);
562         WM_operatortype_append(WM_OT_open_recentfile);
563 }
564
565 /* default keymap for windows and screens, only call once per WM */
566 void wm_window_keymap(wmWindowManager *wm)
567 {
568         ListBase *keymap= WM_keymap_listbase(wm, "Window", 0, 0);
569         
570         /* note, this doesn't replace existing keymap items */
571         WM_keymap_verify_item(keymap, "WM_OT_window_duplicate", AKEY, KM_PRESS, KM_CTRL|KM_ALT, 0);
572         WM_keymap_verify_item(keymap, "WM_OT_save_homefile", UKEY, KM_PRESS, KM_CTRL, 0);
573         WM_keymap_verify_item(keymap, "WM_OT_open_recentfile", OKEY, KM_PRESS, KM_CTRL, 0);
574         WM_keymap_verify_item(keymap, "WM_OT_window_fullscreen_toggle", FKEY, KM_PRESS, 0, 0);
575         WM_keymap_verify_item(keymap, "WM_OT_exit_blender", QKEY, KM_PRESS, KM_CTRL, 0);
576
577 }
578