2.5
[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_fileselect.h"
57 #include "ED_screen.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, "DOC_BROKEN"); /* TODO - add a discription to wmOperatorType? */
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);
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(&iter.ptr, 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(op->ptr, 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         } else {
239                 SpaceFile *sfile;
240
241                 ED_screen_full_newspace(C, CTX_wm_area(C), SPACE_FILE);
242                 
243                 /* settings for filebrowser */
244                 sfile= (SpaceFile*)CTX_wm_space_data(C);
245                 sfile->op = op;
246                 ED_fileselect_set_params(sfile, FILE_BLENDER, op->type->name, "", 0, 0, 0);
247
248                 /* screen and area have been reset already in ED_screen_full_newspace */
249                 return OPERATOR_RUNNING_MODAL;
250         }
251 }
252
253 /* op->poll */
254 int WM_operator_winactive(bContext *C)
255 {
256         if(CTX_wm_window(C)==NULL) return 0;
257         return 1;
258 }
259
260 /* ************ window / screen operator definitions ************** */
261
262 static void WM_OT_window_duplicate(wmOperatorType *ot)
263 {
264         ot->name= "Duplicate Window";
265         ot->idname= "WM_OT_window_duplicate";
266         
267         ot->invoke= WM_operator_confirm;
268         ot->exec= wm_window_duplicate_op;
269         ot->poll= WM_operator_winactive;
270 }
271
272 static void WM_OT_save_homefile(wmOperatorType *ot)
273 {
274         ot->name= "Save User Settings";
275         ot->idname= "WM_OT_save_homefile";
276         
277         ot->invoke= WM_operator_confirm;
278         ot->exec= WM_write_homefile;
279         ot->poll= WM_operator_winactive;
280 }
281
282 static void WM_OT_read_homefile(wmOperatorType *ot)
283 {
284         ot->name= "Reload Start-Up File";
285         ot->idname= "WM_OT_read_homefile";
286         
287         ot->invoke= WM_operator_confirm;
288         ot->exec= WM_read_homefile;
289         ot->poll= WM_operator_winactive;
290         
291         RNA_def_boolean(ot->srna, "factory", 0, "Factory Settings", "");
292 }
293
294
295 /* ********* recent file *********** */
296
297 static int recentfile_exec(bContext *C, wmOperator *op)
298 {
299         int event= RNA_enum_get(op->ptr, "nr");
300
301         // XXX wm in context is not set correctly after WM_read_file -> crash
302         // do it before for now, but is this correct with multiple windows?
303
304         if(event>0) {
305                 if (G.sce[0] && (event==1)) {
306                         WM_event_add_notifier(C, NC_WINDOW, NULL);
307                         WM_read_file(C, G.sce, op->reports);
308                 }
309                 else {
310                         struct RecentFile *recent = BLI_findlink(&(G.recent_files), event-2);
311                         if(recent) {
312                                 WM_event_add_notifier(C, NC_WINDOW, NULL);
313                                 WM_read_file(C, recent->filename, op->reports);
314                         }
315                 }
316         }
317         return 0;
318 }
319
320 static int wm_recentfile_invoke(bContext *C, wmOperator *op, wmEvent *event)
321 {
322         struct RecentFile *recent;
323         uiMenuItem *head;
324         int i, ofs= 0;
325
326         head= uiPupMenuBegin("Open Recent", 0);
327
328         if(G.sce[0]) {
329                 uiMenuItemIntO(head, G.sce, 0, op->type->idname, "nr", 1);
330                 ofs = 1;
331         }
332         
333         for(recent = G.recent_files.first, i=0; (i<U.recent_files) && (recent); recent = recent->next, i++)
334                 if(strcmp(recent->filename, G.sce))
335                         uiMenuItemIntO(head, recent->filename, 0, op->type->idname, "nr", i+ofs+1);
336
337         uiPupMenuEnd(C, head);
338         
339         return OPERATOR_CANCELLED;
340 }
341
342 static void WM_OT_open_recentfile(wmOperatorType *ot)
343 {
344         ot->name= "Open Recent File";
345         ot->idname= "WM_OT_open_recentfile";
346         
347         ot->invoke= wm_recentfile_invoke;
348         ot->exec= recentfile_exec;
349         ot->poll= WM_operator_winactive;
350         
351         RNA_def_property(ot->srna, "nr", PROP_ENUM, PROP_NONE);
352 }
353
354 /* ********* main file *********** */
355
356 static int wm_mainfile_invoke(bContext *C, wmOperator *op, wmEvent *event)
357 {
358         SpaceFile *sfile;
359         
360         ED_screen_full_newspace(C, CTX_wm_area(C), SPACE_FILE);
361
362         /* settings for filebrowser */
363         sfile= (SpaceFile*)CTX_wm_space_data(C);
364         sfile->op = op;
365         // XXX replace G.sce
366         ED_fileselect_set_params(sfile, FILE_BLENDER, "Load", G.sce, 0, 0, 0);
367
368         /* screen and area have been reset already in ED_screen_full_newspace */
369
370         return OPERATOR_RUNNING_MODAL;
371 }
372
373 static int wm_mainfile_exec(bContext *C, wmOperator *op)
374 {
375         char filename[FILE_MAX];
376         RNA_string_get(op->ptr, "filename", filename);
377         
378         // XXX wm in context is not set correctly after WM_read_file -> crash
379         // do it before for now, but is this correct with multiple windows?
380         WM_event_add_notifier(C, NC_WINDOW, NULL);
381
382         WM_read_file(C, filename, op->reports);
383         
384         return 0;
385 }
386
387 static void WM_OT_open_mainfile(wmOperatorType *ot)
388 {
389         ot->name= "Open Blender File";
390         ot->idname= "WM_OT_open_mainfile";
391         
392         ot->invoke= wm_mainfile_invoke;
393         ot->exec= wm_mainfile_exec;
394         ot->poll= WM_operator_winactive;
395         
396         ot->flag= 0;
397         
398         RNA_def_property(ot->srna, "filename", PROP_STRING, PROP_FILEPATH);
399
400 }
401
402 static int wm_save_as_mainfile_invoke(bContext *C, wmOperator *op, wmEvent *event)
403 {
404         SpaceFile *sfile;
405         
406         ED_screen_full_newspace(C, CTX_wm_area(C), SPACE_FILE);
407
408         /* settings for filebrowser */
409         sfile= (SpaceFile*)CTX_wm_space_data(C);
410         sfile->op = op;
411         // XXX replace G.sce
412         ED_fileselect_set_params(sfile, FILE_BLENDER, "Save As", G.sce, 0, 0, 0);
413
414         /* screen and area have been reset already in ED_screen_full_newspace */
415
416         return OPERATOR_RUNNING_MODAL;
417 }
418
419 static int wm_save_as_mainfile_exec(bContext *C, wmOperator *op)
420 {
421         char filename[FILE_MAX];
422         RNA_string_get(op->ptr, "filename", filename);
423         
424         WM_write_file(C, filename, op->reports);
425         
426         WM_event_add_notifier(C, NC_WINDOW, NULL);
427
428         return 0;
429 }
430
431 static void WM_OT_save_as_mainfile(wmOperatorType *ot)
432 {
433         ot->name= "Open Blender File";
434         ot->idname= "WM_OT_save_as_mainfile";
435         
436         ot->invoke= wm_save_as_mainfile_invoke;
437         ot->exec= wm_save_as_mainfile_exec;
438         ot->poll= WM_operator_winactive;
439         
440         ot->flag= 0;
441         
442         RNA_def_property(ot->srna, "filename", PROP_STRING, PROP_FILEPATH);
443
444 }
445
446 /* *********************** */
447
448 static void WM_OT_window_fullscreen_toggle(wmOperatorType *ot)
449 {
450     ot->name= "Toggle Fullscreen";
451     ot->idname= "WM_OT_window_fullscreen_toggle";
452
453     ot->invoke= WM_operator_confirm;
454     ot->exec= wm_window_fullscreen_toggle_op;
455     ot->poll= WM_operator_winactive;
456 }
457
458 static int wm_exit_blender_op(bContext *C, wmOperator *op)
459 {
460         WM_operator_free(op);
461         
462         WM_exit(C);     
463         
464         return OPERATOR_FINISHED;
465 }
466
467 static void WM_OT_exit_blender(wmOperatorType *ot)
468 {
469         ot->name= "Exit Blender";
470         ot->idname= "WM_OT_exit_blender";
471
472         ot->invoke= WM_operator_confirm;
473         ot->exec= wm_exit_blender_op;
474         ot->poll= WM_operator_winactive;
475 }
476
477 /* ************ default paint cursors, draw always around cursor *********** */
478 /*
479  - returns handler to free 
480  - poll(bContext): returns 1 if draw should happen
481  - draw(bContext): drawing callback for paint cursor
482 */
483
484 void *WM_paint_cursor_activate(wmWindowManager *wm, int (*poll)(bContext *C),
485                                void (*draw)(bContext *C, int, int, void *customdata), void *customdata)
486 {
487         wmPaintCursor *pc= MEM_callocN(sizeof(wmPaintCursor), "paint cursor");
488         
489         BLI_addtail(&wm->paintcursors, pc);
490         
491         pc->customdata = customdata;
492         pc->poll= poll;
493         pc->draw= draw;
494         
495         return pc;
496 }
497
498 void WM_paint_cursor_end(wmWindowManager *wm, void *handle)
499 {
500         wmPaintCursor *pc;
501         
502         for(pc= wm->paintcursors.first; pc; pc= pc->next) {
503                 if(pc == (wmPaintCursor *)handle) {
504                         BLI_remlink(&wm->paintcursors, pc);
505                         MEM_freeN(pc);
506                         return;
507                 }
508         }
509 }
510
511 /* ************ window gesture operator-callback definitions ************** */
512 /*
513  * These are default callbacks for use in operators requiring gesture input
514  */
515
516 /* **************** Border gesture *************** */
517
518 /* Border gesture has two types:
519    1) WM_GESTURE_CROSS_RECT: starts a cross, on mouse click it changes to border 
520    2) WM_GESTURE_RECT: starts immediate as a border, on mouse click or release it ends
521
522    It stores 4 values (xmin, xmax, ymin, ymax) and event it ended with (event_type)
523 */
524
525 static int border_apply(bContext *C, wmOperator *op, int event_type)
526 {
527         wmGesture *gesture= op->customdata;
528         rcti *rect= gesture->customdata;
529         
530         if(rect->xmin > rect->xmax)
531                 SWAP(int, rect->xmin, rect->xmax);
532         if(rect->ymin > rect->ymax)
533                 SWAP(int, rect->ymin, rect->ymax);
534         
535         if(rect->xmin==rect->xmax || rect->ymin==rect->ymax)
536                 return 0;
537                 
538         /* operator arguments and storage. */
539         RNA_int_set(op->ptr, "xmin", rect->xmin);
540         RNA_int_set(op->ptr, "ymin", rect->ymin);
541         RNA_int_set(op->ptr, "xmax", rect->xmax);
542         RNA_int_set(op->ptr, "ymax", rect->ymax);
543         if( RNA_struct_find_property(op->ptr, "event_type") )
544                 RNA_int_set(op->ptr, "event_type", event_type);
545         
546         op->type->exec(C, op);
547         
548         return 1;
549 }
550
551 static void wm_gesture_end(bContext *C, wmOperator *op)
552 {
553         wmGesture *gesture= op->customdata;
554         
555         WM_gesture_end(C, gesture);     /* frees gesture itself, and unregisters from window */
556         op->customdata= NULL;
557
558         ED_area_tag_redraw(CTX_wm_area(C));
559         
560 }
561
562 int WM_border_select_invoke(bContext *C, wmOperator *op, wmEvent *event)
563 {
564         op->customdata= WM_gesture_new(C, event, WM_GESTURE_CROSS_RECT);
565
566         /* add modal handler */
567         WM_event_add_modal_handler(C, &CTX_wm_window(C)->handlers, op);
568         
569         wm_gesture_tag_redraw(C);
570
571         return OPERATOR_RUNNING_MODAL;
572 }
573
574 int WM_border_select_modal(bContext *C, wmOperator *op, wmEvent *event)
575 {
576         wmGesture *gesture= op->customdata;
577         rcti *rect= gesture->customdata;
578         int sx, sy;
579         
580         switch(event->type) {
581                 case MOUSEMOVE:
582                         
583                         wm_subwindow_getorigin(CTX_wm_window(C), gesture->swinid, &sx, &sy);
584                         
585                         if(gesture->type==WM_GESTURE_CROSS_RECT && gesture->mode==0) {
586                                 rect->xmin= rect->xmax= event->x - sx;
587                                 rect->ymin= rect->ymax= event->y - sy;
588                         }
589                         else {
590                                 rect->xmax= event->x - sx;
591                                 rect->ymax= event->y - sy;
592                         }
593                         
594                         wm_gesture_tag_redraw(C);
595
596                         break;
597                         
598                 case LEFTMOUSE:
599                 case MIDDLEMOUSE:
600                 case RIGHTMOUSE:
601                         if(event->val==1) {
602                                 if(gesture->type==WM_GESTURE_CROSS_RECT && gesture->mode==0) {
603                                         gesture->mode= 1;
604                                         wm_gesture_tag_redraw(C);
605                                 }
606                         }
607                         else {
608                                 if(border_apply(C, op, event->type)) {
609                                         wm_gesture_end(C, op);
610                                         return OPERATOR_FINISHED;
611                                 }
612                                 wm_gesture_end(C, op);
613                                 return OPERATOR_CANCELLED;
614                         }
615                         break;
616                 case ESCKEY:
617                         wm_gesture_end(C, op);
618                         return OPERATOR_CANCELLED;
619         }
620         return OPERATOR_RUNNING_MODAL;
621 }
622
623 /* **************** circle gesture *************** */
624 /* works now only for selection or modal paint stuff, calls exec while hold mouse, exit on release */
625
626 int WM_gesture_circle_invoke(bContext *C, wmOperator *op, wmEvent *event)
627 {
628         op->customdata= WM_gesture_new(C, event, WM_GESTURE_CIRCLE);
629         
630         /* add modal handler */
631         WM_event_add_modal_handler(C, &CTX_wm_window(C)->handlers, op);
632         
633         wm_gesture_tag_redraw(C);
634         
635         return OPERATOR_RUNNING_MODAL;
636 }
637
638 static void gesture_circle_apply(bContext *C, wmOperator *op)
639 {
640         wmGesture *gesture= op->customdata;
641         rcti *rect= gesture->customdata;
642         
643         /* operator arguments and storage. */
644         RNA_int_set(op->ptr, "x", rect->xmin);
645         RNA_int_set(op->ptr, "y", rect->ymin);
646         RNA_int_set(op->ptr, "radius", rect->xmax);
647         
648         if(op->type->exec)
649                 op->type->exec(C, op);
650 }
651
652 int WM_gesture_circle_modal(bContext *C, wmOperator *op, wmEvent *event)
653 {
654         wmGesture *gesture= op->customdata;
655         rcti *rect= gesture->customdata;
656         int sx, sy;
657         
658         switch(event->type) {
659                 case MOUSEMOVE:
660                         
661                         wm_subwindow_getorigin(CTX_wm_window(C), gesture->swinid, &sx, &sy);
662                         
663                         rect->xmin= event->x - sx;
664                         rect->ymin= event->y - sy;
665                         
666                         wm_gesture_tag_redraw(C);
667                         
668                         if(gesture->mode)
669                                 gesture_circle_apply(C, op);
670
671                         break;
672                 case WHEELUPMOUSE:
673                         rect->xmax += 2 + rect->xmax/10;
674                         wm_gesture_tag_redraw(C);
675                         break;
676                 case WHEELDOWNMOUSE:
677                         rect->xmax -= 2 + rect->xmax/10;
678                         if(rect->xmax < 1) rect->xmax= 1;
679                         wm_gesture_tag_redraw(C);
680                         break;
681                 case LEFTMOUSE:
682                 case MIDDLEMOUSE:
683                 case RIGHTMOUSE:
684                         if(event->val==0) {     /* key release */
685                                 wm_gesture_end(C, op);
686                                 return OPERATOR_FINISHED;
687                         }
688                         else {
689                                 if( RNA_struct_find_property(op->ptr, "event_type") )
690                                         RNA_int_set(op->ptr, "event_type", event->type);
691                                 
692                                 /* apply first click */
693                                 gesture_circle_apply(C, op);
694                                 gesture->mode= 1;
695                         }
696                         break;
697                 case ESCKEY:
698                         wm_gesture_end(C, op);
699                         return OPERATOR_CANCELLED;
700         }
701         return OPERATOR_RUNNING_MODAL;
702 }
703
704 #if 0
705 /* template to copy from */
706 void WM_OT_circle_gesture(wmOperatorType *ot)
707 {
708         ot->name= "Circle Gesture";
709         ot->idname= "WM_OT_circle_gesture";
710         
711         ot->invoke= WM_gesture_circle_invoke;
712         ot->modal= WM_gesture_circle_modal;
713         
714         ot->poll= WM_operator_winactive;
715         
716         RNA_def_property(ot->srna, "x", PROP_INT, PROP_NONE);
717         RNA_def_property(ot->srna, "y", PROP_INT, PROP_NONE);
718         RNA_def_property(ot->srna, "radius", PROP_INT, PROP_NONE);
719
720 }
721 #endif
722
723 /* **************** Tweak gesture *************** */
724
725 static void tweak_gesture_modal(bContext *C, wmEvent *event)
726 {
727         wmWindow *window= CTX_wm_window(C);
728         wmGesture *gesture= window->tweak;
729         rcti *rect= gesture->customdata;
730         int sx, sy, val;
731         
732         switch(event->type) {
733                 case MOUSEMOVE:
734                         
735                         wm_subwindow_getorigin(window, gesture->swinid, &sx, &sy);
736                         
737                         rect->xmax= event->x - sx;
738                         rect->ymax= event->y - sy;
739                         
740                         if((val= wm_gesture_evaluate(C, gesture))) {
741                                 wmEvent event;
742
743                                 event= *(window->eventstate);
744                                 if(gesture->event_type==LEFTMOUSE)
745                                         event.type= EVT_TWEAK_L;
746                                 else if(gesture->event_type==RIGHTMOUSE)
747                                         event.type= EVT_TWEAK_R;
748                                 else
749                                         event.type= EVT_TWEAK_M;
750                                 event.val= val;
751                                 /* mouse coords! */
752                                 wm_event_add(window, &event);
753                                 
754                                 WM_gesture_end(C, gesture);     /* frees gesture itself, and unregisters from window */
755                                 window->tweak= NULL;
756                         }
757                         
758                         break;
759                         
760                 case LEFTMOUSE:
761                 case RIGHTMOUSE:
762                 case MIDDLEMOUSE:
763                         if(gesture->event_type==event->type) {
764                                 WM_gesture_end(C, gesture);
765                                 window->tweak= NULL;
766                                 
767                                 /* when tweak fails we should give the other keymap entries a chance */
768                                 event->val= KM_RELEASE;
769                         }
770                         break;
771                 default:
772                         WM_gesture_end(C, gesture);
773                         window->tweak= NULL;
774         }
775 }
776
777 /* standard tweak, called after window handlers passed on event */
778 void wm_tweakevent_test(bContext *C, wmEvent *event, int action)
779 {
780         wmWindow *win= CTX_wm_window(C);
781         
782         if(win->tweak==NULL) {
783                 if(CTX_wm_region(C)) {
784                         if(event->val) { // pressed
785                                 if( ELEM3(event->type, LEFTMOUSE, MIDDLEMOUSE, RIGHTMOUSE) )
786                                         win->tweak= WM_gesture_new(C, event, WM_GESTURE_TWEAK);
787                         }
788                 }
789         }
790         else {
791                 if(action==WM_HANDLER_BREAK) {
792                         WM_gesture_end(C, win->tweak);
793                         win->tweak= NULL;
794                 }
795                 else
796                         tweak_gesture_modal(C, event);
797         }
798 }
799
800 /* *********************** lasso gesture ****************** */
801
802 int WM_gesture_lasso_invoke(bContext *C, wmOperator *op, wmEvent *event)
803 {
804         op->customdata= WM_gesture_new(C, event, WM_GESTURE_LASSO);
805         
806         /* add modal handler */
807         WM_event_add_modal_handler(C, &CTX_wm_window(C)->handlers, op);
808         
809         wm_gesture_tag_redraw(C);
810         
811         return OPERATOR_RUNNING_MODAL;
812 }
813
814
815 static void gesture_lasso_apply(bContext *C, wmOperator *op, int event_type)
816 {
817         wmGesture *gesture= op->customdata;
818         PointerRNA itemptr;
819         float loc[2];
820         int i;
821         short *lasso= gesture->customdata;
822         
823         /* operator storage as path. */
824
825         for(i=0; i<gesture->points; i++, lasso+=2) {
826                 loc[0]= lasso[0];
827                 loc[1]= lasso[1];
828                 RNA_collection_add(op->ptr, "path", &itemptr);
829                 RNA_float_set_array(&itemptr, "loc", loc);
830         }
831         
832         wm_gesture_end(C, op);
833                 
834         if(op->type->exec)
835                 op->type->exec(C, op);
836         
837 }
838
839 int WM_gesture_lasso_modal(bContext *C, wmOperator *op, wmEvent *event)
840 {
841         wmGesture *gesture= op->customdata;
842         int sx, sy;
843         
844         switch(event->type) {
845                 case MOUSEMOVE:
846                         
847                         wm_gesture_tag_redraw(C);
848                         
849                         wm_subwindow_getorigin(CTX_wm_window(C), gesture->swinid, &sx, &sy);
850                         if(gesture->points < WM_LASSO_MAX_POINTS) {
851                                 short *lasso= gesture->customdata;
852                                 lasso += 2 * gesture->points;
853                                 lasso[0] = event->x - sx;
854                                 lasso[1] = event->y - sy;
855                                 gesture->points++;
856                         }
857                         else {
858                                 gesture_lasso_apply(C, op, event->type);
859                                 return OPERATOR_FINISHED;
860                         }
861                         break;
862                         
863                 case LEFTMOUSE:
864                 case MIDDLEMOUSE:
865                 case RIGHTMOUSE:
866                         if(event->val==0) {     /* key release */
867                                 gesture_lasso_apply(C, op, event->type);
868                                 return OPERATOR_FINISHED;
869                         }
870                         break;
871                 case ESCKEY:
872                         wm_gesture_end(C, op);
873                         return OPERATOR_CANCELLED;
874         }
875         return OPERATOR_RUNNING_MODAL;
876 }
877
878 #if 0
879 /* template to copy from */
880
881 static int gesture_lasso_exec(bContext *C, wmOperator *op)
882 {
883         RNA_BEGIN(op->ptr, itemptr, "path") {
884                 float loc[2];
885                 
886                 RNA_float_get_array(&itemptr, "loc", loc);
887                 printf("Location: %f %f\n", loc[0], loc[1]);
888         }
889         RNA_END;
890         
891         return OPERATOR_FINISHED;
892 }
893
894 void WM_OT_lasso_gesture(wmOperatorType *ot)
895 {
896         PropertyRNA *prop;
897         
898         ot->name= "Lasso Gesture";
899         ot->idname= "WM_OT_lasso_gesture";
900         
901         ot->invoke= WM_gesture_lasso_invoke;
902         ot->modal= WM_gesture_lasso_modal;
903         ot->exec= gesture_lasso_exec;
904         
905         ot->poll= WM_operator_winactive;
906         
907         prop= RNA_def_property(ot->srna, "path", PROP_COLLECTION, PROP_NONE);
908         RNA_def_property_struct_runtime(prop, &RNA_OperatorMousePath);
909 }
910 #endif
911
912 /* *********************** radial control ****************** */
913
914 const int WM_RADIAL_CONTROL_DISPLAY_SIZE = 200;
915
916 typedef struct wmRadialControl {
917         int mode;
918         float initial_value, value, max_value;
919         int initial_mouse[2];
920         void *cursor;
921         GLuint tex;
922 } wmRadialControl;
923
924 static void wm_radial_control_paint(bContext *C, int x, int y, void *customdata)
925 {
926         wmRadialControl *rc = (wmRadialControl*)customdata;
927         ARegion *ar = CTX_wm_region(C);
928         float r1=0.0f, r2=0.0f, r3=0.0f, angle=0.0f;
929
930         /* Keep cursor in the original place */
931         x = rc->initial_mouse[0] - ar->winrct.xmin;
932         y = rc->initial_mouse[1] - ar->winrct.ymin;
933
934         glPushMatrix();
935         
936         glTranslatef((float)x, (float)y, 0.0f);
937
938         if(rc->mode == WM_RADIALCONTROL_SIZE) {
939                 r1= rc->value;
940                 r2= rc->initial_value;
941                 r3= r1;
942         } else if(rc->mode == WM_RADIALCONTROL_STRENGTH) {
943                 r1= (1 - rc->value) * WM_RADIAL_CONTROL_DISPLAY_SIZE;
944                 r2= WM_RADIAL_CONTROL_DISPLAY_SIZE;
945                 r3= WM_RADIAL_CONTROL_DISPLAY_SIZE;
946         } else if(rc->mode == WM_RADIALCONTROL_ANGLE) {
947                 r1= r2= WM_RADIAL_CONTROL_DISPLAY_SIZE;
948                 r3= WM_RADIAL_CONTROL_DISPLAY_SIZE;
949                 angle = rc->value;
950         }
951
952         glColor4ub(255, 255, 255, 128);
953         glEnable( GL_LINE_SMOOTH );
954         glEnable(GL_BLEND);
955
956         if(rc->mode == WM_RADIALCONTROL_ANGLE)
957                 fdrawline(0, 0, WM_RADIAL_CONTROL_DISPLAY_SIZE, 0);
958
959         if(rc->tex) {
960                 const float str = rc->mode == WM_RADIALCONTROL_STRENGTH ? (rc->value + 0.5) : 1;
961
962                 if(rc->mode == WM_RADIALCONTROL_ANGLE) {
963                         glRotatef(angle, 0, 0, 1);
964                         fdrawline(0, 0, WM_RADIAL_CONTROL_DISPLAY_SIZE, 0);
965                 }
966
967                 glBindTexture(GL_TEXTURE_2D, rc->tex);
968
969                 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
970                 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
971
972                 glEnable(GL_TEXTURE_2D);
973                 glBegin(GL_QUADS);
974                 glColor4f(0,0,0, str);
975                 glTexCoord2f(0,0);
976                 glVertex2f(-r3, -r3);
977                 glTexCoord2f(1,0);
978                 glVertex2f(r3, -r3);
979                 glTexCoord2f(1,1);
980                 glVertex2f(r3, r3);
981                 glTexCoord2f(0,1);
982                 glVertex2f(-r3, r3);
983                 glEnd();
984                 glDisable(GL_TEXTURE_2D);
985         }
986
987         glColor4ub(255, 255, 255, 128); 
988         glutil_draw_lined_arc(0.0, M_PI*2.0, r1, 40);
989         glutil_draw_lined_arc(0.0, M_PI*2.0, r2, 40);
990         glDisable(GL_BLEND);
991         glDisable( GL_LINE_SMOOTH );
992         
993         glPopMatrix();
994 }
995
996 int WM_radial_control_modal(bContext *C, wmOperator *op, wmEvent *event)
997 {
998         wmRadialControl *rc = (wmRadialControl*)op->customdata;
999         int mode, initial_mouse[2], delta[2];
1000         float dist;
1001         double new_value = RNA_float_get(op->ptr, "new_value");
1002         int ret = OPERATOR_RUNNING_MODAL;
1003
1004         mode = RNA_int_get(op->ptr, "mode");
1005         RNA_int_get_array(op->ptr, "initial_mouse", initial_mouse);
1006
1007         switch(event->type) {
1008         case MOUSEMOVE:
1009                 delta[0]= initial_mouse[0] - event->x;
1010                 delta[1]= initial_mouse[1] - event->y;
1011                 dist= sqrt(delta[0]*delta[0]+delta[1]*delta[1]);
1012
1013                 if(mode == WM_RADIALCONTROL_SIZE)
1014                         new_value = dist;
1015                 else if(mode == WM_RADIALCONTROL_STRENGTH) {
1016                         new_value = 1 - dist / WM_RADIAL_CONTROL_DISPLAY_SIZE;
1017                 } else if(mode == WM_RADIALCONTROL_ANGLE)
1018                         new_value = ((int)(atan2(delta[1], delta[0]) * (180.0 / M_PI)) + 180);
1019                 
1020                 if(event->ctrl) {
1021                         if(mode == WM_RADIALCONTROL_STRENGTH)
1022                                 new_value = ((int)(new_value * 100) / 10*10) / 100.0f;
1023                         else
1024                                 new_value = ((int)new_value + 5) / 10*10;
1025                 }
1026                 
1027                 break;
1028         case ESCKEY:
1029         case RIGHTMOUSE:
1030                 ret = OPERATOR_CANCELLED;
1031                 break;
1032         case LEFTMOUSE:
1033         case PADENTER:
1034                 op->type->exec(C, op);
1035                 ret = OPERATOR_FINISHED;
1036                 break;
1037         }
1038
1039         /* Clamp */
1040         if(new_value > rc->max_value)
1041                 new_value = rc->max_value;
1042         else if(new_value < 0)
1043                 new_value = 0;
1044
1045         /* Update paint data */
1046         rc->value = new_value;
1047
1048         RNA_float_set(op->ptr, "new_value", new_value);
1049
1050         if(ret != OPERATOR_RUNNING_MODAL) {
1051                 WM_paint_cursor_end(CTX_wm_manager(C), rc->cursor);
1052                 MEM_freeN(rc);
1053         }
1054         
1055         ED_region_tag_redraw(CTX_wm_region(C));
1056
1057         return ret;
1058 }
1059
1060 /* Expects the operator customdata to be an ImBuf (or NULL) */
1061 int WM_radial_control_invoke(bContext *C, wmOperator *op, wmEvent *event)
1062 {
1063         wmRadialControl *rc = MEM_callocN(sizeof(wmRadialControl), "radial control");
1064         int mode = RNA_int_get(op->ptr, "mode");
1065         float initial_value = RNA_float_get(op->ptr, "initial_value");
1066         int mouse[2] = {event->x, event->y};
1067
1068         if(mode == WM_RADIALCONTROL_SIZE) {
1069                 rc->max_value = 200;
1070                 mouse[0]-= initial_value;
1071         }
1072         else if(mode == WM_RADIALCONTROL_STRENGTH) {
1073                 rc->max_value = 1;
1074                 mouse[0]-= WM_RADIAL_CONTROL_DISPLAY_SIZE * (1 - initial_value);
1075         }
1076         else if(mode == WM_RADIALCONTROL_ANGLE) {
1077                 rc->max_value = 360;
1078                 mouse[0]-= WM_RADIAL_CONTROL_DISPLAY_SIZE * cos(initial_value);
1079                 mouse[1]-= WM_RADIAL_CONTROL_DISPLAY_SIZE * sin(initial_value);
1080                 initial_value *= 180.0f/M_PI;
1081         }
1082
1083         if(op->customdata) {
1084                 ImBuf *im = (ImBuf*)op->customdata;
1085                 /* Build GL texture */
1086                 glGenTextures(1, &rc->tex);
1087                 glBindTexture(GL_TEXTURE_2D, rc->tex);
1088                 glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, im->x, im->y, 0, GL_ALPHA, GL_FLOAT, im->rect_float);
1089                 MEM_freeN(im->rect_float);
1090                 MEM_freeN(im);
1091         }
1092
1093         RNA_int_set_array(op->ptr, "initial_mouse", mouse);
1094         RNA_float_set(op->ptr, "new_value", initial_value);
1095                 
1096         op->customdata = rc;
1097         rc->mode = mode;
1098         rc->initial_value = initial_value;
1099         rc->initial_mouse[0] = mouse[0];
1100         rc->initial_mouse[1] = mouse[1];
1101         rc->cursor = WM_paint_cursor_activate(CTX_wm_manager(C), op->type->poll,
1102                                               wm_radial_control_paint, op->customdata);
1103
1104         /* add modal handler */
1105         WM_event_add_modal_handler(C, &CTX_wm_window(C)->handlers, op);
1106         
1107         WM_radial_control_modal(C, op, event);
1108         
1109         return OPERATOR_RUNNING_MODAL;
1110 }
1111
1112 /* Gets a descriptive string of the operation */
1113 void WM_radial_control_string(wmOperator *op, char str[], int maxlen)
1114 {
1115         int mode = RNA_int_get(op->ptr, "mode");
1116         float v = RNA_float_get(op->ptr, "new_value");
1117
1118         if(mode == WM_RADIALCONTROL_SIZE)
1119                 sprintf(str, "Size: %d", (int)v);
1120         else if(mode == WM_RADIALCONTROL_STRENGTH)
1121                 sprintf(str, "Strength: %d", (int)v);
1122         else if(mode == WM_RADIALCONTROL_ANGLE)
1123                 sprintf(str, "Angle: %d", (int)(v * 180.0f/M_PI));
1124 }
1125
1126 /** Important: this doesn't define an actual operator, it
1127     just sets up the common parts of the radial control op. **/
1128 void WM_OT_radial_control_partial(wmOperatorType *ot)
1129 {
1130         static EnumPropertyItem prop_mode_items[] = {
1131                 {WM_RADIALCONTROL_SIZE, "SIZE", "Size", ""},
1132                 {WM_RADIALCONTROL_STRENGTH, "STRENGTH", "Strength", ""},
1133                 {WM_RADIALCONTROL_ANGLE, "ANGLE", "Angle", ""},
1134                 {0, NULL, NULL, NULL}};
1135
1136         /* Should be set in custom invoke() */
1137         RNA_def_float(ot->srna, "initial_value", 0, 0, FLT_MAX, "Initial Value", "", 0, FLT_MAX);
1138
1139         /* Set internally, should be used in custom exec() to get final value */
1140         RNA_def_float(ot->srna, "new_value", 0, 0, FLT_MAX, "New Value", "", 0, FLT_MAX);
1141
1142         /* Should be set before calling operator */
1143         RNA_def_enum(ot->srna, "mode", prop_mode_items, 0, "Mode", "");
1144
1145         /* Internal */
1146         RNA_def_int_vector(ot->srna, "initial_mouse", 2, NULL, INT_MIN, INT_MAX, "initial_mouse", "", INT_MIN, INT_MAX);
1147 }
1148
1149 /* ******************************************************* */
1150  
1151 /* called on initialize WM_exit() */
1152 void wm_operatortype_free(void)
1153 {
1154         BLI_freelistN(&global_ops);
1155 }
1156
1157 /* called on initialize WM_init() */
1158 void wm_operatortype_init(void)
1159 {
1160         WM_operatortype_append(WM_OT_window_duplicate);
1161         WM_operatortype_append(WM_OT_read_homefile);
1162         WM_operatortype_append(WM_OT_save_homefile);
1163         WM_operatortype_append(WM_OT_window_fullscreen_toggle);
1164         WM_operatortype_append(WM_OT_exit_blender);
1165         WM_operatortype_append(WM_OT_open_recentfile);
1166         WM_operatortype_append(WM_OT_open_mainfile);
1167         WM_operatortype_append(WM_OT_jobs_timer);
1168         WM_operatortype_append(WM_OT_save_as_mainfile);
1169 }
1170
1171 /* default keymap for windows and screens, only call once per WM */
1172 void wm_window_keymap(wmWindowManager *wm)
1173 {
1174         ListBase *keymap= WM_keymap_listbase(wm, "Window", 0, 0);
1175         
1176         /* items to make WM work */
1177         WM_keymap_verify_item(keymap, "WM_OT_jobs_timer", TIMERJOBS, KM_ANY, KM_ANY, 0);
1178         
1179         /* note, this doesn't replace existing keymap items */
1180         WM_keymap_verify_item(keymap, "WM_OT_window_duplicate", WKEY, KM_PRESS, KM_CTRL|KM_ALT, 0);
1181         WM_keymap_verify_item(keymap, "WM_OT_read_homefile", XKEY, KM_PRESS, KM_CTRL, 0);
1182         WM_keymap_verify_item(keymap, "WM_OT_save_homefile", UKEY, KM_PRESS, KM_CTRL, 0);
1183         WM_keymap_verify_item(keymap, "WM_OT_open_recentfile", OKEY, KM_PRESS, KM_CTRL, 0);
1184         WM_keymap_verify_item(keymap, "WM_OT_open_mainfile", F1KEY, KM_PRESS, 0, 0);
1185         WM_keymap_verify_item(keymap, "WM_OT_save_as_mainfile", F2KEY, KM_PRESS, 0, 0);
1186         WM_keymap_verify_item(keymap, "WM_OT_window_fullscreen_toggle", F11KEY, KM_PRESS, 0, 0);
1187         WM_keymap_verify_item(keymap, "WM_OT_exit_blender", QKEY, KM_PRESS, KM_CTRL, 0);
1188
1189 }
1190