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