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