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