2.5: Error reporting
[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 /* op->invoke */
199 int WM_operator_confirm(bContext *C, wmOperator *op, wmEvent *event)
200 {
201         char buf[512];
202         
203         sprintf(buf, "OK? %%i%d%%t|%s", ICON_HELP, op->type->name);
204         uiPupmenuOperator(C, 0, op, NULL, buf);
205         
206         return OPERATOR_RUNNING_MODAL;
207 }
208
209 /* op->poll */
210 int WM_operator_winactive(bContext *C)
211 {
212         if(CTX_wm_window(C)==NULL) return 0;
213         return 1;
214 }
215
216 /* ************ window / screen operator definitions ************** */
217
218 static void WM_OT_window_duplicate(wmOperatorType *ot)
219 {
220         ot->name= "Duplicate Window";
221         ot->idname= "WM_OT_window_duplicate";
222         
223         ot->invoke= WM_operator_confirm;
224         ot->exec= wm_window_duplicate_op;
225         ot->poll= WM_operator_winactive;
226 }
227
228 static void WM_OT_save_homefile(wmOperatorType *ot)
229 {
230         ot->name= "Save User Settings";
231         ot->idname= "WM_OT_save_homefile";
232         
233         ot->invoke= WM_operator_confirm;
234         ot->exec= WM_write_homefile;
235         ot->poll= WM_operator_winactive;
236         
237         ot->flag= OPTYPE_REGISTER;
238 }
239
240 /* ********* recent file *********** */
241
242 static void recent_filelist(char *pup)
243 {
244         struct RecentFile *recent;
245         int i, ofs= 0;
246         char *p;
247         
248         p= pup + sprintf(pup, "Open Recent%%t");
249         
250         if (G.sce[0]) {
251                 p+= sprintf(p, "|%s %%x%d", G.sce, 1);
252                 ofs = 1;
253         }
254         
255         for (recent = G.recent_files.first, i=0; (i<U.recent_files) && (recent); recent = recent->next, i++) {
256                 if (strcmp(recent->filename, G.sce)) {
257                         p+= sprintf(p, "|%s %%x%d", recent->filename, i+ofs+1);
258                 }
259         }
260 }
261
262 static int recentfile_exec(bContext *C, wmOperator *op)
263 {
264         int event= RNA_enum_get(op->ptr, "nr");
265         
266         if(event>0) {
267                 if (G.sce[0] && (event==1))
268                         WM_read_file(C, G.sce, op->reports);
269                 else {
270                         struct RecentFile *recent = BLI_findlink(&(G.recent_files), event-2);
271                         if(recent) {
272                                 WM_read_file(C, recent->filename, op->reports);
273                         }
274                 }
275         }
276         return 0;
277 }
278
279 static int wm_recentfile_invoke(bContext *C, wmOperator *op, wmEvent *event)
280 {
281         char pup[2048];
282         
283         recent_filelist(pup);
284         uiPupmenuOperator(C, 0, op, "nr", pup);
285         
286         return OPERATOR_RUNNING_MODAL;
287 }
288
289 static void WM_OT_open_recentfile(wmOperatorType *ot)
290 {
291         ot->name= "Open Recent File";
292         ot->idname= "WM_OT_open_recentfile";
293         
294         ot->invoke= wm_recentfile_invoke;
295         ot->exec= recentfile_exec;
296         ot->poll= WM_operator_winactive;
297         
298         ot->flag= OPTYPE_REGISTER;
299         
300         RNA_def_property(ot->srna, "nr", PROP_ENUM, PROP_NONE);
301
302 }
303
304 /* *********************** */
305
306 static void WM_OT_window_fullscreen_toggle(wmOperatorType *ot)
307 {
308     ot->name= "Toggle Fullscreen";
309     ot->idname= "WM_OT_window_fullscreen_toggle";
310
311     ot->invoke= WM_operator_confirm;
312     ot->exec= wm_window_fullscreen_toggle_op;
313     ot->poll= WM_operator_winactive;
314 }
315
316 static void WM_OT_exit_blender(wmOperatorType *ot)
317 {
318         ot->name= "Exit Blender";
319         ot->idname= "WM_OT_exit_blender";
320
321         ot->invoke= WM_operator_confirm;
322         ot->exec= wm_exit_blender_op;
323         ot->poll= WM_operator_winactive;
324 }
325
326 /* ************ window gesture operator-callback definitions ************** */
327 /*
328  * These are default callbacks for use in operators requiring gesture input
329  */
330
331 /* **************** Border gesture *************** */
332
333 /* Border gesture has two types:
334    1) WM_GESTURE_CROSS_RECT: starts a cross, on mouse click it changes to border 
335    2) WM_GESTURE_RECT: starts immediate as a border, on mouse click or release it ends
336
337    It stores 4 values (xmin, xmax, ymin, ymax) and event it ended with (event_type)
338 */
339
340 static void border_apply(bContext *C, wmOperator *op, int event_type)
341 {
342         wmGesture *gesture= op->customdata;
343         rcti *rect= gesture->customdata;
344         
345         if(rect->xmin > rect->xmax)
346                 SWAP(int, rect->xmin, rect->xmax);
347         if(rect->ymin > rect->ymax)
348                 SWAP(int, rect->ymin, rect->ymax);
349         
350         /* operator arguments and storage. */
351         RNA_int_set(op->ptr, "xmin", rect->xmin);
352         RNA_int_set(op->ptr, "ymin", rect->ymin);
353         RNA_int_set(op->ptr, "xmax", rect->xmax);
354         RNA_int_set(op->ptr, "ymax", rect->ymax);
355         if( RNA_struct_find_property(op->ptr, "event_type") )
356                 RNA_int_set(op->ptr, "event_type", event_type);
357         
358         op->type->exec(C, op);
359 }
360
361 static void border_end(bContext *C, wmOperator *op)
362 {
363         wmGesture *gesture= op->customdata;
364         
365         WM_gesture_end(C, gesture);     /* frees gesture itself, and unregisters from window */
366         op->customdata= NULL;
367
368         ED_area_tag_redraw(CTX_wm_area(C));
369         
370 }
371
372 int WM_border_select_invoke(bContext *C, wmOperator *op, wmEvent *event)
373 {
374         op->customdata= WM_gesture_new(C, event, WM_GESTURE_CROSS_RECT);
375
376         /* add modal handler */
377         WM_event_add_modal_handler(C, &CTX_wm_window(C)->handlers, op);
378         
379         wm_gesture_tag_redraw(C);
380
381         return OPERATOR_RUNNING_MODAL;
382 }
383
384 int WM_border_select_modal(bContext *C, wmOperator *op, wmEvent *event)
385 {
386         wmGesture *gesture= op->customdata;
387         rcti *rect= gesture->customdata;
388         int sx, sy;
389         
390         switch(event->type) {
391                 case MOUSEMOVE:
392                         
393                         wm_subwindow_getorigin(CTX_wm_window(C), gesture->swinid, &sx, &sy);
394                         
395                         if(gesture->type==WM_GESTURE_CROSS_RECT && gesture->mode==0) {
396                                 rect->xmin= rect->xmax= event->x - sx;
397                                 rect->ymin= rect->ymax= event->y - sy;
398                         }
399                         else {
400                                 rect->xmax= event->x - sx;
401                                 rect->ymax= event->y - sy;
402                         }
403                         
404                         wm_gesture_tag_redraw(C);
405
406                         break;
407                         
408                 case LEFTMOUSE:
409                 case MIDDLEMOUSE:
410                 case RIGHTMOUSE:
411                         if(event->val==1) {
412                                 if(gesture->type==WM_GESTURE_CROSS_RECT && gesture->mode==0) {
413                                         gesture->mode= 1;
414                                         wm_gesture_tag_redraw(C);
415                                 }
416                         }
417                         else {
418                                 border_apply(C, op, event->type);
419                                 border_end(C, op);
420                                 return OPERATOR_FINISHED;
421                         }
422                         break;
423                 case ESCKEY:
424                         border_end(C, op);
425                         return OPERATOR_CANCELLED;
426         }
427         return OPERATOR_RUNNING_MODAL;
428 }
429
430 /* **************** circle gesture *************** */
431 /* works now only for selection or modal paint stuff, calls exec while hold mouse */
432
433 int WM_gesture_circle_invoke(bContext *C, wmOperator *op, wmEvent *event)
434 {
435         op->customdata= WM_gesture_new(C, event, WM_GESTURE_CIRCLE);
436         
437         /* add modal handler */
438         WM_event_add_modal_handler(C, &CTX_wm_window(C)->handlers, op);
439         
440         wm_gesture_tag_redraw(C);
441         
442         return OPERATOR_RUNNING_MODAL;
443 }
444
445 static void gesture_circle_end(bContext *C, wmOperator *op)
446 {
447         wmGesture *gesture= op->customdata;
448         
449         WM_gesture_end(C, gesture);     /* frees gesture itself, and unregisters from window */
450         op->customdata= NULL;
451         
452         ED_area_tag_redraw(CTX_wm_area(C));
453 }
454
455 static void gesture_circle_apply(bContext *C, wmOperator *op, int event_type)
456 {
457         wmGesture *gesture= op->customdata;
458         rcti *rect= gesture->customdata;
459         
460         /* operator arguments and storage. */
461         RNA_int_set(op->ptr, "x", rect->xmin);
462         RNA_int_set(op->ptr, "y", rect->ymin);
463         RNA_int_set(op->ptr, "radius", rect->xmax);
464         if( RNA_struct_find_property(op->ptr, "event_type") )
465                 RNA_int_set(op->ptr, "event_type", event_type);
466         
467         if(op->type->exec)
468                 op->type->exec(C, op);
469 }
470
471 int WM_gesture_circle_modal(bContext *C, wmOperator *op, wmEvent *event)
472 {
473         wmGesture *gesture= op->customdata;
474         rcti *rect= gesture->customdata;
475         int sx, sy;
476         
477         switch(event->type) {
478                 case MOUSEMOVE:
479                         
480                         wm_subwindow_getorigin(CTX_wm_window(C), gesture->swinid, &sx, &sy);
481                         
482                         rect->xmin= event->x - sx;
483                         rect->ymin= event->y - sy;
484                         
485                         wm_gesture_tag_redraw(C);
486                         
487                         if(gesture->mode)
488                                 gesture_circle_apply(C, op, event->type);
489
490                         break;
491                 case WHEELUPMOUSE:
492                         rect->xmax += 2 + rect->xmax/10;
493                         wm_gesture_tag_redraw(C);
494                         break;
495                 case WHEELDOWNMOUSE:
496                         rect->xmax -= 2 + rect->xmax/10;
497                         if(rect->xmax < 1) rect->xmax= 1;
498                         wm_gesture_tag_redraw(C);
499                         break;
500                 case LEFTMOUSE:
501                 case MIDDLEMOUSE:
502                 case RIGHTMOUSE:
503                         if(event->val==0) {     /* key release */
504                                 gesture_circle_end(C, op);
505                                 return OPERATOR_FINISHED;
506                         }
507                         else
508                                 gesture->mode= 1;
509
510                         break;
511                 case ESCKEY:
512                         gesture_circle_end(C, op);
513                         return OPERATOR_CANCELLED;
514         }
515         return OPERATOR_RUNNING_MODAL;
516 }
517
518 #if 0
519 /* template to copy from */
520 void WM_OT_circle_gesture(wmOperatorType *ot)
521 {
522         ot->name= "Circle Gesture";
523         ot->idname= "WM_OT_circle_gesture";
524         
525         ot->invoke= WM_gesture_circle_invoke;
526         ot->modal= WM_gesture_circle_modal;
527         
528         ot->poll= WM_operator_winactive;
529         
530         RNA_def_property(ot->srna, "x", PROP_INT, PROP_NONE);
531         RNA_def_property(ot->srna, "y", PROP_INT, PROP_NONE);
532         RNA_def_property(ot->srna, "radius", PROP_INT, PROP_NONE);
533
534 }
535 #endif
536
537 /* **************** Tweak gesture *************** */
538
539 static int tweak_gesture_invoke(bContext *C, wmOperator *op, wmEvent *event)
540 {
541         op->customdata= WM_gesture_new(C, event, WM_GESTURE_TWEAK);
542         
543         /* add modal handler */
544         WM_event_add_modal_handler(C, &CTX_wm_window(C)->handlers, op);
545         
546         wm_gesture_tag_redraw(C);
547         
548         return OPERATOR_RUNNING_MODAL;
549 }
550
551 static void tweak_gesture_end(bContext *C, wmOperator *op)
552 {
553         wmGesture *gesture= op->customdata;
554         
555         WM_gesture_end(C, gesture);     /* frees gesture itself, and unregisters from window */
556         op->customdata= NULL;
557
558         ED_area_tag_redraw(CTX_wm_area(C));
559         
560 }
561
562 static int tweak_gesture_modal(bContext *C, wmOperator *op, wmEvent *event)
563 {
564         wmWindow *window= CTX_wm_window(C);
565         wmGesture *gesture= op->customdata;
566         rcti *rect= gesture->customdata;
567         int sx, sy, val;
568         
569         switch(event->type) {
570                 case MOUSEMOVE:
571                         
572                         wm_subwindow_getorigin(window, gesture->swinid, &sx, &sy);
573                         
574                         rect->xmax= event->x - sx;
575                         rect->ymax= event->y - sy;
576                         
577                         if((val= wm_gesture_evaluate(C, gesture))) {
578                                 wmEvent event;
579                                         
580                                 event= *(window->eventstate);
581                                 if(gesture->event_type==LEFTMOUSE)
582                                         event.type= EVT_TWEAK_L;
583                                 else if(gesture->event_type==RIGHTMOUSE)
584                                         event.type= EVT_TWEAK_R;
585                                 else
586                                         event.type= EVT_TWEAK_M;
587                                 event.val= val;
588                                 /* mouse coords! */
589                                 wm_event_add(window, &event);
590                                 
591                                 tweak_gesture_end(C, op);
592                                 return OPERATOR_FINISHED;
593                         }
594                         else
595                                 wm_gesture_tag_redraw(C);
596                         
597                         break;
598                         
599                 case LEFTMOUSE:
600                 case RIGHTMOUSE:
601                 case MIDDLEMOUSE:
602                         if(gesture->event_type==event->type) {
603                                 wm_gesture_evaluate(C, gesture);
604                                 tweak_gesture_end(C, op);
605                                 return OPERATOR_FINISHED;
606                         }
607                         break;
608         }
609         return OPERATOR_RUNNING_MODAL;
610 }
611
612 void WM_OT_tweak_gesture(wmOperatorType *ot)
613 {
614         ot->name= "Tweak Gesture";
615         ot->idname= "WM_OT_tweak_gesture";
616         
617         ot->invoke= tweak_gesture_invoke;
618         ot->modal= tweak_gesture_modal;
619
620         ot->poll= WM_operator_winactive;
621 }
622
623
624 /* ******************************************************* */
625  
626 /* called on initialize WM_exit() */
627 void wm_operatortype_free(void)
628 {
629         BLI_freelistN(&global_ops);
630 }
631
632 /* called on initialize WM_init() */
633 void wm_operatortype_init(void)
634 {
635         WM_operatortype_append(WM_OT_window_duplicate);
636         WM_operatortype_append(WM_OT_save_homefile);
637         WM_operatortype_append(WM_OT_window_fullscreen_toggle);
638         WM_operatortype_append(WM_OT_exit_blender);
639         WM_operatortype_append(WM_OT_tweak_gesture);
640         WM_operatortype_append(WM_OT_open_recentfile);
641 }
642
643 /* default keymap for windows and screens, only call once per WM */
644 void wm_window_keymap(wmWindowManager *wm)
645 {
646         ListBase *keymap= WM_keymap_listbase(wm, "Window", 0, 0);
647         
648         /* note, this doesn't replace existing keymap items */
649         WM_keymap_verify_item(keymap, "WM_OT_window_duplicate", AKEY, KM_PRESS, KM_CTRL|KM_ALT, 0);
650         WM_keymap_verify_item(keymap, "WM_OT_save_homefile", UKEY, KM_PRESS, KM_CTRL, 0);
651         WM_keymap_verify_item(keymap, "WM_OT_open_recentfile", OKEY, KM_PRESS, KM_CTRL, 0);
652         WM_keymap_verify_item(keymap, "WM_OT_window_fullscreen_toggle", FKEY, KM_PRESS, 0, 0);
653         WM_keymap_verify_item(keymap, "WM_OT_exit_blender", QKEY, KM_PRESS, KM_CTRL, 0);
654
655 }
656