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