2.50: svn merge https://svn.blender.org/svnroot/bf-blender/trunk/blender -r18677...
[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 <float.h>
30 #define _USE_MATH_DEFINES
31 #include <math.h>
32 #include <string.h>
33
34 #include "DNA_ID.h"
35 #include "DNA_screen_types.h"
36 #include "DNA_userdef_types.h"
37 #include "DNA_windowmanager_types.h"
38
39 #include "MEM_guardedalloc.h"
40
41 #include "BLI_blenlib.h"
42 #include "BLI_dynstr.h" /*for WM_operator_pystring */
43
44 #include "BKE_blender.h"
45 #include "BKE_context.h"
46 #include "BKE_idprop.h"
47 #include "BKE_library.h"
48 #include "BKE_global.h"
49 #include "BKE_main.h"
50 #include "BKE_utildefines.h"
51
52 #include "BIF_gl.h"
53 #include "BIF_glutil.h" /* for paint cursor */
54 #include "IMB_imbuf_types.h"
55
56 #include "ED_screen.h"
57
58 #include "RNA_access.h"
59 #include "RNA_define.h"
60
61 #include "UI_interface.h"
62 #include "UI_resources.h"
63
64 #include "WM_api.h"
65 #include "WM_types.h"
66
67 #include "wm.h"
68 #include "wm_window.h"
69 #include "wm_subwindow.h"
70 #include "wm_event_system.h"
71
72
73
74 static ListBase global_ops= {NULL, NULL};
75
76 /* ************ operator API, exported ********** */
77
78 wmOperatorType *WM_operatortype_find(const char *idname)
79 {
80         wmOperatorType *ot;
81         
82         for(ot= global_ops.first; ot; ot= ot->next) {
83                 if(strncmp(ot->idname, idname, OP_MAX_TYPENAME)==0)
84                    return ot;
85         }
86         printf("search for unknown operator %s\n", idname);
87         return NULL;
88 }
89
90 wmOperatorType *WM_operatortype_first(void)
91 {
92         return global_ops.first;
93 }
94
95 /* all ops in 1 list (for time being... needs evaluation later) */
96 void WM_operatortype_append(void (*opfunc)(wmOperatorType*))
97 {
98         wmOperatorType *ot;
99         
100         ot= MEM_callocN(sizeof(wmOperatorType), "operatortype");
101         ot->srna= RNA_def_struct(&BLENDER_RNA, "", "OperatorProperties");
102         opfunc(ot);
103         RNA_def_struct_ui_text(ot->srna, ot->name, ot->description ? ot->description:""); // XXX All ops should have a description but for now allow them not to.
104         RNA_def_struct_identifier(ot->srna, ot->idname);
105         BLI_addtail(&global_ops, ot);
106 }
107
108 void WM_operatortype_append_ptr(void (*opfunc)(wmOperatorType*, void*), void *userdata)
109 {
110         wmOperatorType *ot;
111
112         ot= MEM_callocN(sizeof(wmOperatorType), "operatortype");
113         ot->srna= RNA_def_struct(&BLENDER_RNA, "", "OperatorProperties");
114         opfunc(ot, userdata);
115         RNA_def_struct_ui_text(ot->srna, ot->name, "DOC_BROKEN"); /* TODO - add a discription to wmOperatorType? */
116         RNA_def_struct_identifier(ot->srna, ot->idname);
117         BLI_addtail(&global_ops, ot);
118 }
119
120 int WM_operatortype_remove(const char *idname)
121 {
122         wmOperatorType *ot = WM_operatortype_find(idname);
123
124         if (ot==NULL)
125                 return 0;
126         
127         BLI_remlink(&global_ops, ot);
128         RNA_struct_free(&BLENDER_RNA, ot->srna);
129         MEM_freeN(ot);
130
131         return 1;
132 }
133
134 /* print a string representation of the operator, with the args that it runs 
135  * so python can run it again */
136 char *WM_operator_pystring(wmOperator *op)
137 {
138         const char *arg_name= NULL;
139
140         PropertyRNA *prop, *iterprop;
141         CollectionPropertyIterator iter;
142
143         /* for building the string */
144         DynStr *dynstr= BLI_dynstr_new();
145         char *cstring, *buf;
146         int first_iter=1;
147
148         BLI_dynstr_appendf(dynstr, "%s(", op->idname);
149
150         iterprop= RNA_struct_iterator_property(op->ptr);
151         RNA_property_collection_begin(op->ptr, iterprop, &iter);
152
153         for(; iter.valid; RNA_property_collection_next(&iter)) {
154                 prop= iter.ptr.data;
155                 arg_name= RNA_property_identifier(&iter.ptr, prop);
156
157                 if (strcmp(arg_name, "rna_type")==0) continue;
158
159                 buf= RNA_property_as_string(op->ptr, prop);
160                 
161                 BLI_dynstr_appendf(dynstr, first_iter?"%s=%s":", %s=%s", arg_name, buf);
162
163                 MEM_freeN(buf);
164                 first_iter = 0;
165         }
166
167         RNA_property_collection_end(&iter);
168
169         BLI_dynstr_append(dynstr, ")");
170
171         cstring = BLI_dynstr_get_cstring(dynstr);
172         BLI_dynstr_free(dynstr);
173         return cstring;
174 }
175
176 void WM_operator_properties_create(PointerRNA *ptr, const char *opstring)
177 {
178         wmOperatorType *ot= WM_operatortype_find(opstring);
179
180         if(ot)
181                 RNA_pointer_create(NULL, ot->srna, NULL, ptr);
182         else
183                 RNA_pointer_create(NULL, &RNA_OperatorProperties, NULL, ptr);
184 }
185
186 void WM_operator_properties_free(PointerRNA *ptr)
187 {
188         IDProperty *properties= ptr->data;
189
190         if(properties) {
191                 IDP_FreeProperty(properties);
192                 MEM_freeN(properties);
193         }
194 }
195
196 /* ************ default op callbacks, exported *********** */
197
198 /* invoke callback, uses enum property named "type" */
199 /* only weak thing is the fixed property name... */
200 int WM_menu_invoke(bContext *C, wmOperator *op, wmEvent *event)
201 {
202         PropertyRNA *prop= RNA_struct_find_property(op->ptr, "type");
203         uiMenuItem *head;
204
205         if(prop==NULL) {
206                 printf("WM_menu_invoke: %s has no \"type\" enum property\n", op->type->idname);
207         }
208         else if (RNA_property_type(op->ptr, prop) != PROP_ENUM) {
209                 printf("WM_menu_invoke: %s \"type\" is not an enum property\n", op->type->idname);
210         }
211         else {
212                 head= uiPupMenuBegin(op->type->name, 0);
213                 uiMenuItemsEnumO(head, op->type->idname, "type");
214                 uiPupMenuEnd(C, head);
215         }
216
217         return OPERATOR_CANCELLED;
218 }
219
220 /* op->invoke */
221 int WM_operator_confirm(bContext *C, wmOperator *op, wmEvent *event)
222 {
223         uiMenuItem *head;
224
225         head= uiPupMenuBegin("OK?", ICON_HELP);
226         uiMenuItemO(head, 0, op->type->idname);
227         uiPupMenuEnd(C, head);
228         
229         return OPERATOR_CANCELLED;
230 }
231
232 /* op->invoke, opens fileselect if filename property not set, otherwise executes */
233 int WM_operator_filesel(bContext *C, wmOperator *op, wmEvent *event)
234 {
235         if (RNA_property_is_set(op->ptr, "filename")) {
236                 return WM_operator_call(C, op);
237         } 
238         else {
239                 WM_event_add_fileselect(C, op);
240                 return OPERATOR_RUNNING_MODAL;
241         }
242 }
243
244 /* op->poll */
245 int WM_operator_winactive(bContext *C)
246 {
247         if(CTX_wm_window(C)==NULL) return 0;
248         return 1;
249 }
250
251 /* ************ window / screen operator definitions ************** */
252
253 static void WM_OT_window_duplicate(wmOperatorType *ot)
254 {
255         ot->name= "Duplicate Window";
256         ot->idname= "WM_OT_window_duplicate";
257         
258         ot->invoke= WM_operator_confirm;
259         ot->exec= wm_window_duplicate_op;
260         ot->poll= WM_operator_winactive;
261 }
262
263 static void WM_OT_save_homefile(wmOperatorType *ot)
264 {
265         ot->name= "Save User Settings";
266         ot->idname= "WM_OT_save_homefile";
267         
268         ot->invoke= WM_operator_confirm;
269         ot->exec= WM_write_homefile;
270         ot->poll= WM_operator_winactive;
271 }
272
273 static void WM_OT_read_homefile(wmOperatorType *ot)
274 {
275         ot->name= "Reload Start-Up File";
276         ot->idname= "WM_OT_read_homefile";
277         
278         ot->invoke= WM_operator_confirm;
279         ot->exec= WM_read_homefile;
280         ot->poll= WM_operator_winactive;
281         
282         RNA_def_boolean(ot->srna, "factory", 0, "Factory Settings", "");
283 }
284
285
286 /* ********* recent file *********** */
287
288 static int recentfile_exec(bContext *C, wmOperator *op)
289 {
290         int event= RNA_enum_get(op->ptr, "nr");
291
292         // XXX wm in context is not set correctly after WM_read_file -> crash
293         // do it before for now, but is this correct with multiple windows?
294
295         if(event>0) {
296                 if (G.sce[0] && (event==1)) {
297                         WM_event_add_notifier(C, NC_WINDOW, NULL);
298                         WM_read_file(C, G.sce, op->reports);
299                 }
300                 else {
301                         struct RecentFile *recent = BLI_findlink(&(G.recent_files), event-2);
302                         if(recent) {
303                                 WM_event_add_notifier(C, NC_WINDOW, NULL);
304                                 WM_read_file(C, recent->filename, op->reports);
305                         }
306                 }
307         }
308         return 0;
309 }
310
311 static int wm_recentfile_invoke(bContext *C, wmOperator *op, wmEvent *event)
312 {
313         struct RecentFile *recent;
314         uiMenuItem *head;
315         int i, ofs= 0;
316
317         head= uiPupMenuBegin("Open Recent", 0);
318
319         if(G.sce[0]) {
320                 uiMenuItemIntO(head, G.sce, 0, op->type->idname, "nr", 1);
321                 ofs = 1;
322         }
323         
324         for(recent = G.recent_files.first, i=0; (i<U.recent_files) && (recent); recent = recent->next, i++)
325                 if(strcmp(recent->filename, G.sce))
326                         uiMenuItemIntO(head, recent->filename, 0, op->type->idname, "nr", i+ofs+1);
327
328         uiPupMenuEnd(C, head);
329         
330         return OPERATOR_CANCELLED;
331 }
332
333 static void WM_OT_open_recentfile(wmOperatorType *ot)
334 {
335         ot->name= "Open Recent File";
336         ot->idname= "WM_OT_open_recentfile";
337         
338         ot->invoke= wm_recentfile_invoke;
339         ot->exec= recentfile_exec;
340         ot->poll= WM_operator_winactive;
341         
342         RNA_def_property(ot->srna, "nr", PROP_ENUM, PROP_NONE);
343 }
344
345 /* ********* main file *********** */
346
347 static void untitled(char *name)
348 {
349         if (G.save_over == 0 && strlen(name) < FILE_MAX-16) {
350                 char *c= BLI_last_slash(name);
351                 
352                 if (c)
353                         strcpy(&c[1], "untitled.blend");
354                 else
355                         strcpy(name, "untitled.blend");
356         }
357 }
358
359
360 static int wm_open_mainfile_invoke(bContext *C, wmOperator *op, wmEvent *event)
361 {
362
363         RNA_string_set(op->ptr, "filename", G.sce);
364         WM_event_add_fileselect(C, op);
365
366         return OPERATOR_RUNNING_MODAL;
367 }
368
369 static int wm_open_mainfile_exec(bContext *C, wmOperator *op)
370 {
371         char filename[FILE_MAX];
372         RNA_string_get(op->ptr, "filename", filename);
373         
374         // XXX wm in context is not set correctly after WM_read_file -> crash
375         // do it before for now, but is this correct with multiple windows?
376         WM_event_add_notifier(C, NC_WINDOW, NULL);
377
378         WM_read_file(C, filename, op->reports);
379         
380         return 0;
381 }
382
383 static void WM_OT_open_mainfile(wmOperatorType *ot)
384 {
385         ot->name= "Open Blender File";
386         ot->idname= "WM_OT_open_mainfile";
387         
388         ot->invoke= wm_open_mainfile_invoke;
389         ot->exec= wm_open_mainfile_exec;
390         ot->poll= WM_operator_winactive;
391         
392         ot->flag= 0;
393         
394         RNA_def_property(ot->srna, "filename", PROP_STRING, PROP_FILEPATH);
395
396 }
397
398 static int wm_save_as_mainfile_invoke(bContext *C, wmOperator *op, wmEvent *event)
399 {
400         char name[FILE_MAX];
401         
402         BLI_strncpy(name, G.sce, FILE_MAX);
403         untitled(name);
404         RNA_string_set(op->ptr, "filename", name);
405         
406         WM_event_add_fileselect(C, op);
407
408         return OPERATOR_RUNNING_MODAL;
409 }
410
411 /* function used for WM_OT_save_mainfile too */
412 static int wm_save_as_mainfile_exec(bContext *C, wmOperator *op)
413 {
414         char filename[FILE_MAX];
415         
416         if(RNA_property_is_set(op->ptr, "filename"))
417                 RNA_string_get(op->ptr, "filename", filename);
418         else {
419                 BLI_strncpy(filename, G.sce, FILE_MAX);
420                 untitled(filename);
421         }
422         WM_write_file(C, filename, op->reports);
423         
424         WM_event_add_notifier(C, NC_WM|ND_FILESAVE, NULL);
425
426         return 0;
427 }
428
429 static void WM_OT_save_as_mainfile(wmOperatorType *ot)
430 {
431         ot->name= "Save As Blender File";
432         ot->idname= "WM_OT_save_as_mainfile";
433         
434         ot->invoke= wm_save_as_mainfile_invoke;
435         ot->exec= wm_save_as_mainfile_exec;
436         ot->poll= WM_operator_winactive;
437         
438         ot->flag= 0;
439         
440         RNA_def_property(ot->srna, "filename", PROP_STRING, PROP_FILEPATH);
441
442 }
443
444 /* *************** Save file directly ******** */
445
446 static int wm_save_mainfile_invoke(bContext *C, wmOperator *op, wmEvent *event)
447 {
448         char name[FILE_MAX];
449         
450         BLI_strncpy(name, G.sce, FILE_MAX);
451         untitled(name);
452         RNA_string_set(op->ptr, "filename", name);
453         uiPupMenuSaveOver(C, op, name);
454
455         return OPERATOR_RUNNING_MODAL;
456 }
457
458 static void WM_OT_save_mainfile(wmOperatorType *ot)
459 {
460         ot->name= "Save Blender File";
461         ot->idname= "WM_OT_save_mainfile";
462         
463         ot->invoke= wm_save_mainfile_invoke;
464         ot->exec= wm_save_as_mainfile_exec;
465         ot->poll= WM_operator_winactive;
466         
467         ot->flag= 0;
468         
469         RNA_def_property(ot->srna, "filename", PROP_STRING, PROP_FILEPATH);
470         
471 }
472
473
474 /* *********************** */
475
476 static void WM_OT_window_fullscreen_toggle(wmOperatorType *ot)
477 {
478     ot->name= "Toggle Fullscreen";
479     ot->idname= "WM_OT_window_fullscreen_toggle";
480
481     ot->invoke= WM_operator_confirm;
482     ot->exec= wm_window_fullscreen_toggle_op;
483     ot->poll= WM_operator_winactive;
484 }
485
486 static int wm_exit_blender_op(bContext *C, wmOperator *op)
487 {
488         WM_operator_free(op);
489         
490         WM_exit(C);     
491         
492         return OPERATOR_FINISHED;
493 }
494
495 static void WM_OT_exit_blender(wmOperatorType *ot)
496 {
497         ot->name= "Exit Blender";
498         ot->idname= "WM_OT_exit_blender";
499
500         ot->invoke= WM_operator_confirm;
501         ot->exec= wm_exit_blender_op;
502         ot->poll= WM_operator_winactive;
503 }
504
505 /* ************ default paint cursors, draw always around cursor *********** */
506 /*
507  - returns handler to free 
508  - poll(bContext): returns 1 if draw should happen
509  - draw(bContext): drawing callback for paint cursor
510 */
511
512 void *WM_paint_cursor_activate(wmWindowManager *wm, int (*poll)(bContext *C),
513                                void (*draw)(bContext *C, int, int, void *customdata), void *customdata)
514 {
515         wmPaintCursor *pc= MEM_callocN(sizeof(wmPaintCursor), "paint cursor");
516         
517         BLI_addtail(&wm->paintcursors, pc);
518         
519         pc->customdata = customdata;
520         pc->poll= poll;
521         pc->draw= draw;
522         
523         return pc;
524 }
525
526 void WM_paint_cursor_end(wmWindowManager *wm, void *handle)
527 {
528         wmPaintCursor *pc;
529         
530         for(pc= wm->paintcursors.first; pc; pc= pc->next) {
531                 if(pc == (wmPaintCursor *)handle) {
532                         BLI_remlink(&wm->paintcursors, pc);
533                         MEM_freeN(pc);
534                         return;
535                 }
536         }
537 }
538
539 /* ************ window gesture operator-callback definitions ************** */
540 /*
541  * These are default callbacks for use in operators requiring gesture input
542  */
543
544 /* **************** Border gesture *************** */
545
546 /* Border gesture has two types:
547    1) WM_GESTURE_CROSS_RECT: starts a cross, on mouse click it changes to border 
548    2) WM_GESTURE_RECT: starts immediate as a border, on mouse click or release it ends
549
550    It stores 4 values (xmin, xmax, ymin, ymax) and event it ended with (event_type)
551 */
552
553 static int border_apply(bContext *C, wmOperator *op, int event_type)
554 {
555         wmGesture *gesture= op->customdata;
556         rcti *rect= gesture->customdata;
557         
558         if(rect->xmin > rect->xmax)
559                 SWAP(int, rect->xmin, rect->xmax);
560         if(rect->ymin > rect->ymax)
561                 SWAP(int, rect->ymin, rect->ymax);
562         
563         if(rect->xmin==rect->xmax || rect->ymin==rect->ymax)
564                 return 0;
565                 
566         /* operator arguments and storage. */
567         RNA_int_set(op->ptr, "xmin", rect->xmin);
568         RNA_int_set(op->ptr, "ymin", rect->ymin);
569         RNA_int_set(op->ptr, "xmax", rect->xmax);
570         RNA_int_set(op->ptr, "ymax", rect->ymax);
571         if( RNA_struct_find_property(op->ptr, "event_type") )
572                 RNA_int_set(op->ptr, "event_type", event_type);
573         
574         op->type->exec(C, op);
575         
576         return 1;
577 }
578
579 static void wm_gesture_end(bContext *C, wmOperator *op)
580 {
581         wmGesture *gesture= op->customdata;
582         
583         WM_gesture_end(C, gesture);     /* frees gesture itself, and unregisters from window */
584         op->customdata= NULL;
585
586         ED_area_tag_redraw(CTX_wm_area(C));
587         
588         if( RNA_struct_find_property(op->ptr, "cursor") )
589                 WM_cursor_restore(CTX_wm_window(C));
590 }
591
592 int WM_border_select_invoke(bContext *C, wmOperator *op, wmEvent *event)
593 {
594         op->customdata= WM_gesture_new(C, event, WM_GESTURE_CROSS_RECT);
595
596         /* add modal handler */
597         WM_event_add_modal_handler(C, &CTX_wm_window(C)->handlers, op);
598         
599         wm_gesture_tag_redraw(C);
600
601         return OPERATOR_RUNNING_MODAL;
602 }
603
604 int WM_border_select_modal(bContext *C, wmOperator *op, wmEvent *event)
605 {
606         wmGesture *gesture= op->customdata;
607         rcti *rect= gesture->customdata;
608         int sx, sy;
609         
610         switch(event->type) {
611                 case MOUSEMOVE:
612                         
613                         wm_subwindow_getorigin(CTX_wm_window(C), gesture->swinid, &sx, &sy);
614                         
615                         if(gesture->type==WM_GESTURE_CROSS_RECT && gesture->mode==0) {
616                                 rect->xmin= rect->xmax= event->x - sx;
617                                 rect->ymin= rect->ymax= event->y - sy;
618                         }
619                         else {
620                                 rect->xmax= event->x - sx;
621                                 rect->ymax= event->y - sy;
622                         }
623                         
624                         wm_gesture_tag_redraw(C);
625
626                         break;
627                         
628                 case LEFTMOUSE:
629                 case MIDDLEMOUSE:
630                 case RIGHTMOUSE:
631                         if(event->val==1) {
632                                 if(gesture->type==WM_GESTURE_CROSS_RECT && gesture->mode==0) {
633                                         gesture->mode= 1;
634                                         wm_gesture_tag_redraw(C);
635                                 }
636                         }
637                         else {
638                                 if(border_apply(C, op, event->type)) {
639                                         wm_gesture_end(C, op);
640                                         return OPERATOR_FINISHED;
641                                 }
642                                 wm_gesture_end(C, op);
643                                 return OPERATOR_CANCELLED;
644                         }
645                         break;
646                 case ESCKEY:
647                         wm_gesture_end(C, op);
648                         return OPERATOR_CANCELLED;
649         }
650         return OPERATOR_RUNNING_MODAL;
651 }
652
653 /* **************** circle gesture *************** */
654 /* works now only for selection or modal paint stuff, calls exec while hold mouse, exit on release */
655
656 int WM_gesture_circle_invoke(bContext *C, wmOperator *op, wmEvent *event)
657 {
658         op->customdata= WM_gesture_new(C, event, WM_GESTURE_CIRCLE);
659         
660         /* add modal handler */
661         WM_event_add_modal_handler(C, &CTX_wm_window(C)->handlers, op);
662         
663         wm_gesture_tag_redraw(C);
664         
665         return OPERATOR_RUNNING_MODAL;
666 }
667
668 static void gesture_circle_apply(bContext *C, wmOperator *op)
669 {
670         wmGesture *gesture= op->customdata;
671         rcti *rect= gesture->customdata;
672         
673         /* operator arguments and storage. */
674         RNA_int_set(op->ptr, "x", rect->xmin);
675         RNA_int_set(op->ptr, "y", rect->ymin);
676         RNA_int_set(op->ptr, "radius", rect->xmax);
677         
678         if(op->type->exec)
679                 op->type->exec(C, op);
680 }
681
682 int WM_gesture_circle_modal(bContext *C, wmOperator *op, wmEvent *event)
683 {
684         wmGesture *gesture= op->customdata;
685         rcti *rect= gesture->customdata;
686         int sx, sy;
687         
688         switch(event->type) {
689                 case MOUSEMOVE:
690                         
691                         wm_subwindow_getorigin(CTX_wm_window(C), gesture->swinid, &sx, &sy);
692                         
693                         rect->xmin= event->x - sx;
694                         rect->ymin= event->y - sy;
695                         
696                         wm_gesture_tag_redraw(C);
697                         
698                         if(gesture->mode)
699                                 gesture_circle_apply(C, op);
700
701                         break;
702                 case WHEELUPMOUSE:
703                         rect->xmax += 2 + rect->xmax/10;
704                         wm_gesture_tag_redraw(C);
705                         break;
706                 case WHEELDOWNMOUSE:
707                         rect->xmax -= 2 + rect->xmax/10;
708                         if(rect->xmax < 1) rect->xmax= 1;
709                         wm_gesture_tag_redraw(C);
710                         break;
711                 case LEFTMOUSE:
712                 case MIDDLEMOUSE:
713                 case RIGHTMOUSE:
714                         if(event->val==0) {     /* key release */
715                                 wm_gesture_end(C, op);
716                                 return OPERATOR_FINISHED;
717                         }
718                         else {
719                                 if( RNA_struct_find_property(op->ptr, "event_type") )
720                                         RNA_int_set(op->ptr, "event_type", event->type);
721                                 
722                                 /* apply first click */
723                                 gesture_circle_apply(C, op);
724                                 gesture->mode= 1;
725                         }
726                         break;
727                 case ESCKEY:
728                         wm_gesture_end(C, op);
729                         return OPERATOR_CANCELLED;
730         }
731         return OPERATOR_RUNNING_MODAL;
732 }
733
734 #if 0
735 /* template to copy from */
736 void WM_OT_circle_gesture(wmOperatorType *ot)
737 {
738         ot->name= "Circle Gesture";
739         ot->idname= "WM_OT_circle_gesture";
740         
741         ot->invoke= WM_gesture_circle_invoke;
742         ot->modal= WM_gesture_circle_modal;
743         
744         ot->poll= WM_operator_winactive;
745         
746         RNA_def_property(ot->srna, "x", PROP_INT, PROP_NONE);
747         RNA_def_property(ot->srna, "y", PROP_INT, PROP_NONE);
748         RNA_def_property(ot->srna, "radius", PROP_INT, PROP_NONE);
749
750 }
751 #endif
752
753 /* **************** Tweak gesture *************** */
754
755 static void tweak_gesture_modal(bContext *C, wmEvent *event)
756 {
757         wmWindow *window= CTX_wm_window(C);
758         wmGesture *gesture= window->tweak;
759         rcti *rect= gesture->customdata;
760         int sx, sy, val;
761         
762         switch(event->type) {
763                 case MOUSEMOVE:
764                         
765                         wm_subwindow_getorigin(window, gesture->swinid, &sx, &sy);
766                         
767                         rect->xmax= event->x - sx;
768                         rect->ymax= event->y - sy;
769                         
770                         if((val= wm_gesture_evaluate(C, gesture))) {
771                                 wmEvent event;
772
773                                 event= *(window->eventstate);
774                                 if(gesture->event_type==LEFTMOUSE)
775                                         event.type= EVT_TWEAK_L;
776                                 else if(gesture->event_type==RIGHTMOUSE)
777                                         event.type= EVT_TWEAK_R;
778                                 else
779                                         event.type= EVT_TWEAK_M;
780                                 event.val= val;
781                                 /* mouse coords! */
782                                 wm_event_add(window, &event);
783                                 
784                                 WM_gesture_end(C, gesture);     /* frees gesture itself, and unregisters from window */
785                                 window->tweak= NULL;
786                         }
787                         
788                         break;
789                         
790                 case LEFTMOUSE:
791                 case RIGHTMOUSE:
792                 case MIDDLEMOUSE:
793                         if(gesture->event_type==event->type) {
794                                 WM_gesture_end(C, gesture);
795                                 window->tweak= NULL;
796                                 
797                                 /* when tweak fails we should give the other keymap entries a chance */
798                                 event->val= KM_RELEASE;
799                         }
800                         break;
801                 default:
802                         WM_gesture_end(C, gesture);
803                         window->tweak= NULL;
804         }
805 }
806
807 /* standard tweak, called after window handlers passed on event */
808 void wm_tweakevent_test(bContext *C, wmEvent *event, int action)
809 {
810         wmWindow *win= CTX_wm_window(C);
811         
812         if(win->tweak==NULL) {
813                 if(CTX_wm_region(C)) {
814                         if(event->val) { // pressed
815                                 if( ELEM3(event->type, LEFTMOUSE, MIDDLEMOUSE, RIGHTMOUSE) )
816                                         win->tweak= WM_gesture_new(C, event, WM_GESTURE_TWEAK);
817                         }
818                 }
819         }
820         else {
821                 if(action==WM_HANDLER_BREAK) {
822                         WM_gesture_end(C, win->tweak);
823                         win->tweak= NULL;
824                 }
825                 else
826                         tweak_gesture_modal(C, event);
827         }
828 }
829
830 /* *********************** lasso gesture ****************** */
831
832 int WM_gesture_lasso_invoke(bContext *C, wmOperator *op, wmEvent *event)
833 {
834         op->customdata= WM_gesture_new(C, event, WM_GESTURE_LASSO);
835         
836         /* add modal handler */
837         WM_event_add_modal_handler(C, &CTX_wm_window(C)->handlers, op);
838         
839         wm_gesture_tag_redraw(C);
840         
841         if( RNA_struct_find_property(op->ptr, "cursor") )
842                 WM_cursor_modal(CTX_wm_window(C), RNA_int_get(op->ptr, "cursor"));
843         
844         return OPERATOR_RUNNING_MODAL;
845 }
846
847 int WM_gesture_lines_invoke(bContext *C, wmOperator *op, wmEvent *event)
848 {
849         op->customdata= WM_gesture_new(C, event, WM_GESTURE_LINES);
850         
851         /* add modal handler */
852         WM_event_add_modal_handler(C, &CTX_wm_window(C)->handlers, op);
853         
854         wm_gesture_tag_redraw(C);
855         
856         if( RNA_struct_find_property(op->ptr, "cursor") )
857                 WM_cursor_modal(CTX_wm_window(C), RNA_int_get(op->ptr, "cursor"));
858         
859         return OPERATOR_RUNNING_MODAL;
860 }
861
862
863 static void gesture_lasso_apply(bContext *C, wmOperator *op, int event_type)
864 {
865         wmGesture *gesture= op->customdata;
866         PointerRNA itemptr;
867         float loc[2];
868         int i;
869         short *lasso= gesture->customdata;
870         
871         /* operator storage as path. */
872
873         for(i=0; i<gesture->points; i++, lasso+=2) {
874                 loc[0]= lasso[0];
875                 loc[1]= lasso[1];
876                 RNA_collection_add(op->ptr, "path", &itemptr);
877                 RNA_float_set_array(&itemptr, "loc", loc);
878         }
879         
880         wm_gesture_end(C, op);
881                 
882         if(op->type->exec)
883                 op->type->exec(C, op);
884         
885 }
886
887 int WM_gesture_lasso_modal(bContext *C, wmOperator *op, wmEvent *event)
888 {
889         wmGesture *gesture= op->customdata;
890         int sx, sy;
891         
892         switch(event->type) {
893                 case MOUSEMOVE:
894                         
895                         wm_gesture_tag_redraw(C);
896                         
897                         wm_subwindow_getorigin(CTX_wm_window(C), gesture->swinid, &sx, &sy);
898                         if(gesture->points < WM_LASSO_MAX_POINTS) {
899                                 short *lasso= gesture->customdata;
900                                 lasso += 2 * gesture->points;
901                                 lasso[0] = event->x - sx;
902                                 lasso[1] = event->y - sy;
903                                 gesture->points++;
904                         }
905                         else {
906                                 gesture_lasso_apply(C, op, event->type);
907                                 return OPERATOR_FINISHED;
908                         }
909                         break;
910                         
911                 case LEFTMOUSE:
912                 case MIDDLEMOUSE:
913                 case RIGHTMOUSE:
914                         if(event->val==0) {     /* key release */
915                                 gesture_lasso_apply(C, op, event->type);
916                                 return OPERATOR_FINISHED;
917                         }
918                         break;
919                 case ESCKEY:
920                         wm_gesture_end(C, op);
921                         return OPERATOR_CANCELLED;
922         }
923         return OPERATOR_RUNNING_MODAL;
924 }
925
926 int WM_gesture_lines_modal(bContext *C, wmOperator *op, wmEvent *event)
927 {
928         return WM_gesture_lasso_modal(C, op, event);
929 }
930
931 #if 0
932 /* template to copy from */
933
934 static int gesture_lasso_exec(bContext *C, wmOperator *op)
935 {
936         RNA_BEGIN(op->ptr, itemptr, "path") {
937                 float loc[2];
938                 
939                 RNA_float_get_array(&itemptr, "loc", loc);
940                 printf("Location: %f %f\n", loc[0], loc[1]);
941         }
942         RNA_END;
943         
944         return OPERATOR_FINISHED;
945 }
946
947 void WM_OT_lasso_gesture(wmOperatorType *ot)
948 {
949         PropertyRNA *prop;
950         
951         ot->name= "Lasso Gesture";
952         ot->idname= "WM_OT_lasso_gesture";
953         
954         ot->invoke= WM_gesture_lasso_invoke;
955         ot->modal= WM_gesture_lasso_modal;
956         ot->exec= gesture_lasso_exec;
957         
958         ot->poll= WM_operator_winactive;
959         
960         prop= RNA_def_property(ot->srna, "path", PROP_COLLECTION, PROP_NONE);
961         RNA_def_property_struct_runtime(prop, &RNA_OperatorMousePath);
962 }
963 #endif
964
965 /* *********************** radial control ****************** */
966
967 const int WM_RADIAL_CONTROL_DISPLAY_SIZE = 200;
968
969 typedef struct wmRadialControl {
970         int mode;
971         float initial_value, value, max_value;
972         int initial_mouse[2];
973         void *cursor;
974         GLuint tex;
975 } wmRadialControl;
976
977 static void wm_radial_control_paint(bContext *C, int x, int y, void *customdata)
978 {
979         wmRadialControl *rc = (wmRadialControl*)customdata;
980         ARegion *ar = CTX_wm_region(C);
981         float r1=0.0f, r2=0.0f, r3=0.0f, angle=0.0f;
982
983         /* Keep cursor in the original place */
984         x = rc->initial_mouse[0] - ar->winrct.xmin;
985         y = rc->initial_mouse[1] - ar->winrct.ymin;
986
987         glPushMatrix();
988         
989         glTranslatef((float)x, (float)y, 0.0f);
990
991         if(rc->mode == WM_RADIALCONTROL_SIZE) {
992                 r1= rc->value;
993                 r2= rc->initial_value;
994                 r3= r1;
995         } else if(rc->mode == WM_RADIALCONTROL_STRENGTH) {
996                 r1= (1 - rc->value) * WM_RADIAL_CONTROL_DISPLAY_SIZE;
997                 r2= WM_RADIAL_CONTROL_DISPLAY_SIZE;
998                 r3= WM_RADIAL_CONTROL_DISPLAY_SIZE;
999         } else if(rc->mode == WM_RADIALCONTROL_ANGLE) {
1000                 r1= r2= WM_RADIAL_CONTROL_DISPLAY_SIZE;
1001                 r3= WM_RADIAL_CONTROL_DISPLAY_SIZE;
1002                 angle = rc->value;
1003         }
1004
1005         glColor4ub(255, 255, 255, 128);
1006         glEnable( GL_LINE_SMOOTH );
1007         glEnable(GL_BLEND);
1008
1009         if(rc->mode == WM_RADIALCONTROL_ANGLE)
1010                 fdrawline(0, 0, WM_RADIAL_CONTROL_DISPLAY_SIZE, 0);
1011
1012         if(rc->tex) {
1013                 const float str = rc->mode == WM_RADIALCONTROL_STRENGTH ? (rc->value + 0.5) : 1;
1014
1015                 if(rc->mode == WM_RADIALCONTROL_ANGLE) {
1016                         glRotatef(angle, 0, 0, 1);
1017                         fdrawline(0, 0, WM_RADIAL_CONTROL_DISPLAY_SIZE, 0);
1018                 }
1019
1020                 glBindTexture(GL_TEXTURE_2D, rc->tex);
1021
1022                 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1023                 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1024
1025                 glEnable(GL_TEXTURE_2D);
1026                 glBegin(GL_QUADS);
1027                 glColor4f(0,0,0, str);
1028                 glTexCoord2f(0,0);
1029                 glVertex2f(-r3, -r3);
1030                 glTexCoord2f(1,0);
1031                 glVertex2f(r3, -r3);
1032                 glTexCoord2f(1,1);
1033                 glVertex2f(r3, r3);
1034                 glTexCoord2f(0,1);
1035                 glVertex2f(-r3, r3);
1036                 glEnd();
1037                 glDisable(GL_TEXTURE_2D);
1038         }
1039
1040         glColor4ub(255, 255, 255, 128); 
1041         glutil_draw_lined_arc(0.0, M_PI*2.0, r1, 40);
1042         glutil_draw_lined_arc(0.0, M_PI*2.0, r2, 40);
1043         glDisable(GL_BLEND);
1044         glDisable( GL_LINE_SMOOTH );
1045         
1046         glPopMatrix();
1047 }
1048
1049 int WM_radial_control_modal(bContext *C, wmOperator *op, wmEvent *event)
1050 {
1051         wmRadialControl *rc = (wmRadialControl*)op->customdata;
1052         int mode, initial_mouse[2], delta[2];
1053         float dist;
1054         double new_value = RNA_float_get(op->ptr, "new_value");
1055         int ret = OPERATOR_RUNNING_MODAL;
1056
1057         mode = RNA_int_get(op->ptr, "mode");
1058         RNA_int_get_array(op->ptr, "initial_mouse", initial_mouse);
1059
1060         switch(event->type) {
1061         case MOUSEMOVE:
1062                 delta[0]= initial_mouse[0] - event->x;
1063                 delta[1]= initial_mouse[1] - event->y;
1064                 dist= sqrt(delta[0]*delta[0]+delta[1]*delta[1]);
1065
1066                 if(mode == WM_RADIALCONTROL_SIZE)
1067                         new_value = dist;
1068                 else if(mode == WM_RADIALCONTROL_STRENGTH) {
1069                         new_value = 1 - dist / WM_RADIAL_CONTROL_DISPLAY_SIZE;
1070                 } else if(mode == WM_RADIALCONTROL_ANGLE)
1071                         new_value = ((int)(atan2(delta[1], delta[0]) * (180.0 / M_PI)) + 180);
1072                 
1073                 if(event->ctrl) {
1074                         if(mode == WM_RADIALCONTROL_STRENGTH)
1075                                 new_value = ((int)(new_value * 100) / 10*10) / 100.0f;
1076                         else
1077                                 new_value = ((int)new_value + 5) / 10*10;
1078                 }
1079                 
1080                 break;
1081         case ESCKEY:
1082         case RIGHTMOUSE:
1083                 ret = OPERATOR_CANCELLED;
1084                 break;
1085         case LEFTMOUSE:
1086         case PADENTER:
1087                 op->type->exec(C, op);
1088                 ret = OPERATOR_FINISHED;
1089                 break;
1090         }
1091
1092         /* Clamp */
1093         if(new_value > rc->max_value)
1094                 new_value = rc->max_value;
1095         else if(new_value < 0)
1096                 new_value = 0;
1097
1098         /* Update paint data */
1099         rc->value = new_value;
1100
1101         RNA_float_set(op->ptr, "new_value", new_value);
1102
1103         if(ret != OPERATOR_RUNNING_MODAL) {
1104                 WM_paint_cursor_end(CTX_wm_manager(C), rc->cursor);
1105                 MEM_freeN(rc);
1106         }
1107         
1108         ED_region_tag_redraw(CTX_wm_region(C));
1109
1110         return ret;
1111 }
1112
1113 /* Expects the operator customdata to be an ImBuf (or NULL) */
1114 int WM_radial_control_invoke(bContext *C, wmOperator *op, wmEvent *event)
1115 {
1116         wmRadialControl *rc = MEM_callocN(sizeof(wmRadialControl), "radial control");
1117         int mode = RNA_int_get(op->ptr, "mode");
1118         float initial_value = RNA_float_get(op->ptr, "initial_value");
1119         int mouse[2] = {event->x, event->y};
1120
1121         if(mode == WM_RADIALCONTROL_SIZE) {
1122                 rc->max_value = 200;
1123                 mouse[0]-= initial_value;
1124         }
1125         else if(mode == WM_RADIALCONTROL_STRENGTH) {
1126                 rc->max_value = 1;
1127                 mouse[0]-= WM_RADIAL_CONTROL_DISPLAY_SIZE * (1 - initial_value);
1128         }
1129         else if(mode == WM_RADIALCONTROL_ANGLE) {
1130                 rc->max_value = 360;
1131                 mouse[0]-= WM_RADIAL_CONTROL_DISPLAY_SIZE * cos(initial_value);
1132                 mouse[1]-= WM_RADIAL_CONTROL_DISPLAY_SIZE * sin(initial_value);
1133                 initial_value *= 180.0f/M_PI;
1134         }
1135
1136         if(op->customdata) {
1137                 ImBuf *im = (ImBuf*)op->customdata;
1138                 /* Build GL texture */
1139                 glGenTextures(1, &rc->tex);
1140                 glBindTexture(GL_TEXTURE_2D, rc->tex);
1141                 glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, im->x, im->y, 0, GL_ALPHA, GL_FLOAT, im->rect_float);
1142                 MEM_freeN(im->rect_float);
1143                 MEM_freeN(im);
1144         }
1145
1146         RNA_int_set_array(op->ptr, "initial_mouse", mouse);
1147         RNA_float_set(op->ptr, "new_value", initial_value);
1148                 
1149         op->customdata = rc;
1150         rc->mode = mode;
1151         rc->initial_value = initial_value;
1152         rc->initial_mouse[0] = mouse[0];
1153         rc->initial_mouse[1] = mouse[1];
1154         rc->cursor = WM_paint_cursor_activate(CTX_wm_manager(C), op->type->poll,
1155                                               wm_radial_control_paint, op->customdata);
1156
1157         /* add modal handler */
1158         WM_event_add_modal_handler(C, &CTX_wm_window(C)->handlers, op);
1159         
1160         WM_radial_control_modal(C, op, event);
1161         
1162         return OPERATOR_RUNNING_MODAL;
1163 }
1164
1165 /* Gets a descriptive string of the operation */
1166 void WM_radial_control_string(wmOperator *op, char str[], int maxlen)
1167 {
1168         int mode = RNA_int_get(op->ptr, "mode");
1169         float v = RNA_float_get(op->ptr, "new_value");
1170
1171         if(mode == WM_RADIALCONTROL_SIZE)
1172                 sprintf(str, "Size: %d", (int)v);
1173         else if(mode == WM_RADIALCONTROL_STRENGTH)
1174                 sprintf(str, "Strength: %d", (int)v);
1175         else if(mode == WM_RADIALCONTROL_ANGLE)
1176                 sprintf(str, "Angle: %d", (int)(v * 180.0f/M_PI));
1177 }
1178
1179 /** Important: this doesn't define an actual operator, it
1180     just sets up the common parts of the radial control op. **/
1181 void WM_OT_radial_control_partial(wmOperatorType *ot)
1182 {
1183         static EnumPropertyItem prop_mode_items[] = {
1184                 {WM_RADIALCONTROL_SIZE, "SIZE", "Size", ""},
1185                 {WM_RADIALCONTROL_STRENGTH, "STRENGTH", "Strength", ""},
1186                 {WM_RADIALCONTROL_ANGLE, "ANGLE", "Angle", ""},
1187                 {0, NULL, NULL, NULL}};
1188
1189         /* Should be set in custom invoke() */
1190         RNA_def_float(ot->srna, "initial_value", 0, 0, FLT_MAX, "Initial Value", "", 0, FLT_MAX);
1191
1192         /* Set internally, should be used in custom exec() to get final value */
1193         RNA_def_float(ot->srna, "new_value", 0, 0, FLT_MAX, "New Value", "", 0, FLT_MAX);
1194
1195         /* Should be set before calling operator */
1196         RNA_def_enum(ot->srna, "mode", prop_mode_items, 0, "Mode", "");
1197
1198         /* Internal */
1199         RNA_def_int_vector(ot->srna, "initial_mouse", 2, NULL, INT_MIN, INT_MAX, "initial_mouse", "", INT_MIN, INT_MAX);
1200 }
1201
1202 /* ******************************************************* */
1203  
1204 /* called on initialize WM_exit() */
1205 void wm_operatortype_free(void)
1206 {
1207         BLI_freelistN(&global_ops);
1208 }
1209
1210 /* called on initialize WM_init() */
1211 void wm_operatortype_init(void)
1212 {
1213         WM_operatortype_append(WM_OT_window_duplicate);
1214         WM_operatortype_append(WM_OT_read_homefile);
1215         WM_operatortype_append(WM_OT_save_homefile);
1216         WM_operatortype_append(WM_OT_window_fullscreen_toggle);
1217         WM_operatortype_append(WM_OT_exit_blender);
1218         WM_operatortype_append(WM_OT_open_recentfile);
1219         WM_operatortype_append(WM_OT_open_mainfile);
1220         WM_operatortype_append(WM_OT_jobs_timer);
1221         WM_operatortype_append(WM_OT_save_as_mainfile);
1222         WM_operatortype_append(WM_OT_save_mainfile);
1223 }
1224
1225 /* default keymap for windows and screens, only call once per WM */
1226 void wm_window_keymap(wmWindowManager *wm)
1227 {
1228         ListBase *keymap= WM_keymap_listbase(wm, "Window", 0, 0);
1229         
1230         /* items to make WM work */
1231         WM_keymap_verify_item(keymap, "WM_OT_jobs_timer", TIMERJOBS, KM_ANY, KM_ANY, 0);
1232         
1233         /* note, this doesn't replace existing keymap items */
1234         WM_keymap_verify_item(keymap, "WM_OT_window_duplicate", WKEY, KM_PRESS, KM_CTRL|KM_ALT, 0);
1235         WM_keymap_verify_item(keymap, "WM_OT_read_homefile", XKEY, KM_PRESS, KM_CTRL, 0);
1236         WM_keymap_verify_item(keymap, "WM_OT_save_homefile", UKEY, KM_PRESS, KM_CTRL, 0);
1237         WM_keymap_verify_item(keymap, "WM_OT_open_recentfile", OKEY, KM_PRESS, KM_CTRL, 0);
1238         WM_keymap_verify_item(keymap, "WM_OT_open_mainfile", F1KEY, KM_PRESS, 0, 0);
1239         WM_keymap_verify_item(keymap, "WM_OT_save_mainfile", WKEY, KM_PRESS, KM_CTRL, 0);
1240         WM_keymap_verify_item(keymap, "WM_OT_save_as_mainfile", F2KEY, KM_PRESS, 0, 0);
1241         WM_keymap_verify_item(keymap, "WM_OT_window_fullscreen_toggle", F11KEY, KM_PRESS, 0, 0);
1242         WM_keymap_verify_item(keymap, "WM_OT_exit_blender", QKEY, KM_PRESS, KM_CTRL, 0);
1243
1244 }
1245