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