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