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