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