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