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         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 void tweak_gesture_modal(bContext *C, wmEvent *event)
733 {
734         wmWindow *window= CTX_wm_window(C);
735         wmGesture *gesture= window->tweak;
736         rcti *rect= gesture->customdata;
737         int sx, sy, val;
738         
739         switch(event->type) {
740                 case MOUSEMOVE:
741                         
742                         wm_subwindow_getorigin(window, gesture->swinid, &sx, &sy);
743                         
744                         rect->xmax= event->x - sx;
745                         rect->ymax= event->y - sy;
746                         
747                         if((val= wm_gesture_evaluate(C, gesture))) {
748                                 wmEvent event;
749
750                                 event= *(window->eventstate);
751                                 if(gesture->event_type==LEFTMOUSE)
752                                         event.type= EVT_TWEAK_L;
753                                 else if(gesture->event_type==RIGHTMOUSE)
754                                         event.type= EVT_TWEAK_R;
755                                 else
756                                         event.type= EVT_TWEAK_M;
757                                 event.val= val;
758                                 /* mouse coords! */
759                                 wm_event_add(window, &event);
760                                 
761                                 WM_gesture_end(C, gesture);     /* frees gesture itself, and unregisters from window */
762                                 window->tweak= NULL;
763                         }
764                         
765                         break;
766                         
767                 case LEFTMOUSE:
768                 case RIGHTMOUSE:
769                 case MIDDLEMOUSE:
770                         if(gesture->event_type==event->type) {
771                                 WM_gesture_end(C, gesture);
772                                 window->tweak= NULL;
773                                 
774                                 /* when tweak fails we should give the other keymap entries a chance */
775                                 event->val= KM_RELEASE;
776                         }
777                         break;
778                 default:
779                         WM_gesture_end(C, gesture);
780                         window->tweak= NULL;
781         }
782 }
783
784 /* standard tweak, called after window handlers passed on event */
785 void wm_tweakevent_test(bContext *C, wmEvent *event, int action)
786 {
787         wmWindow *win= CTX_wm_window(C);
788         
789         if(win->tweak==NULL) {
790                 if(CTX_wm_region(C)) {
791                         if(event->val) { // pressed
792                                 if( ELEM3(event->type, LEFTMOUSE, MIDDLEMOUSE, RIGHTMOUSE) )
793                                         win->tweak= WM_gesture_new(C, event, WM_GESTURE_TWEAK);
794                         }
795                 }
796         }
797         else {
798                 if(action==WM_HANDLER_BREAK) {
799                         WM_gesture_end(C, win->tweak);
800                         win->tweak= NULL;
801                 }
802                 else
803                         tweak_gesture_modal(C, event);
804         }
805 }
806
807 /* *********************** lasso gesture ****************** */
808
809 int WM_gesture_lasso_invoke(bContext *C, wmOperator *op, wmEvent *event)
810 {
811         op->customdata= WM_gesture_new(C, event, WM_GESTURE_LASSO);
812         
813         /* add modal handler */
814         WM_event_add_modal_handler(C, &CTX_wm_window(C)->handlers, op);
815         
816         wm_gesture_tag_redraw(C);
817         
818         return OPERATOR_RUNNING_MODAL;
819 }
820
821
822 static void gesture_lasso_apply(bContext *C, wmOperator *op, int event_type)
823 {
824         wmGesture *gesture= op->customdata;
825         PointerRNA itemptr;
826         float loc[2];
827         int i;
828         short *lasso= gesture->customdata;
829         
830         /* operator storage as path. */
831
832         for(i=0; i<gesture->points; i++, lasso+=2) {
833                 loc[0]= lasso[0];
834                 loc[1]= lasso[1];
835                 RNA_collection_add(op->ptr, "path", &itemptr);
836                 RNA_float_set_array(&itemptr, "loc", loc);
837         }
838         
839         wm_gesture_end(C, op);
840                 
841         if(op->type->exec)
842                 op->type->exec(C, op);
843         
844 }
845
846 int WM_gesture_lasso_modal(bContext *C, wmOperator *op, wmEvent *event)
847 {
848         wmGesture *gesture= op->customdata;
849         int sx, sy;
850         
851         switch(event->type) {
852                 case MOUSEMOVE:
853                         
854                         wm_gesture_tag_redraw(C);
855                         
856                         wm_subwindow_getorigin(CTX_wm_window(C), gesture->swinid, &sx, &sy);
857                         if(gesture->points < WM_LASSO_MAX_POINTS) {
858                                 short *lasso= gesture->customdata;
859                                 lasso += 2 * gesture->points;
860                                 lasso[0] = event->x - sx;
861                                 lasso[1] = event->y - sy;
862                                 gesture->points++;
863                         }
864                         else {
865                                 gesture_lasso_apply(C, op, event->type);
866                                 return OPERATOR_FINISHED;
867                         }
868                         break;
869                         
870                 case LEFTMOUSE:
871                 case MIDDLEMOUSE:
872                 case RIGHTMOUSE:
873                         if(event->val==0) {     /* key release */
874                                 gesture_lasso_apply(C, op, event->type);
875                                 return OPERATOR_FINISHED;
876                         }
877                         break;
878                 case ESCKEY:
879                         wm_gesture_end(C, op);
880                         return OPERATOR_CANCELLED;
881         }
882         return OPERATOR_RUNNING_MODAL;
883 }
884
885 #if 0
886 /* template to copy from */
887
888 static int gesture_lasso_exec(bContext *C, wmOperator *op)
889 {
890         RNA_BEGIN(op->ptr, itemptr, "path") {
891                 float loc[2];
892                 
893                 RNA_float_get_array(&itemptr, "loc", loc);
894                 printf("Location: %f %f\n", loc[0], loc[1]);
895         }
896         RNA_END;
897         
898         return OPERATOR_FINISHED;
899 }
900
901 void WM_OT_lasso_gesture(wmOperatorType *ot)
902 {
903         PropertyRNA *prop;
904         
905         ot->name= "Lasso Gesture";
906         ot->idname= "WM_OT_lasso_gesture";
907         
908         ot->invoke= WM_gesture_lasso_invoke;
909         ot->modal= WM_gesture_lasso_modal;
910         ot->exec= gesture_lasso_exec;
911         
912         ot->poll= WM_operator_winactive;
913         
914         prop= RNA_def_property(ot->srna, "path", PROP_COLLECTION, PROP_NONE);
915         RNA_def_property_struct_runtime(prop, &RNA_OperatorMousePath);
916 }
917 #endif
918
919 /* *********************** radial control ****************** */
920
921 const int WM_RADIAL_CONTROL_DISPLAY_SIZE = 200;
922
923 typedef struct wmRadialControl {
924         int mode;
925         float initial_value, value, max_value;
926         int initial_mouse[2];
927         void *cursor;
928         GLuint tex;
929 } wmRadialControl;
930
931 static void wm_radial_control_paint(bContext *C, int x, int y, void *customdata)
932 {
933         wmRadialControl *rc = (wmRadialControl*)customdata;
934         ARegion *ar = CTX_wm_region(C);
935         float r1, r2, r3, angle;
936
937         /* Keep cursor in the original place */
938         x = rc->initial_mouse[0] - ar->winrct.xmin;
939         y = rc->initial_mouse[1] - ar->winrct.ymin;
940
941         glPushMatrix();
942         
943         glTranslatef((float)x, (float)y, 0.0f);
944
945         if(rc->mode == WM_RADIALCONTROL_SIZE) {
946                 r1= rc->value;
947                 r2= rc->initial_value;
948                 r3= r1;
949         } else if(rc->mode == WM_RADIALCONTROL_STRENGTH) {
950                 r1= (1 - rc->value) * WM_RADIAL_CONTROL_DISPLAY_SIZE;
951                 r2= WM_RADIAL_CONTROL_DISPLAY_SIZE;
952                 r3= WM_RADIAL_CONTROL_DISPLAY_SIZE;
953         } else if(rc->mode == WM_RADIALCONTROL_ANGLE) {
954                 r1= r2= WM_RADIAL_CONTROL_DISPLAY_SIZE;
955                 r3= WM_RADIAL_CONTROL_DISPLAY_SIZE;
956                 angle = rc->value;
957         }
958
959         glColor4ub(255, 255, 255, 128);
960         glEnable( GL_LINE_SMOOTH );
961         glEnable(GL_BLEND);
962
963         if(rc->mode == WM_RADIALCONTROL_ANGLE)
964                 fdrawline(0, 0, WM_RADIAL_CONTROL_DISPLAY_SIZE, 0);
965
966         if(rc->tex) {
967                 const float str = rc->mode == WM_RADIALCONTROL_STRENGTH ? (rc->value + 0.5) : 1;
968
969                 if(rc->mode == WM_RADIALCONTROL_ANGLE) {
970                         glRotatef(angle, 0, 0, 1);
971                         fdrawline(0, 0, WM_RADIAL_CONTROL_DISPLAY_SIZE, 0);
972                 }
973
974                 glBindTexture(GL_TEXTURE_2D, rc->tex);
975
976                 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
977                 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
978
979                 glEnable(GL_TEXTURE_2D);
980                 glBegin(GL_QUADS);
981                 glColor4f(0,0,0, str);
982                 glTexCoord2f(0,0);
983                 glVertex2f(-r3, -r3);
984                 glTexCoord2f(1,0);
985                 glVertex2f(r3, -r3);
986                 glTexCoord2f(1,1);
987                 glVertex2f(r3, r3);
988                 glTexCoord2f(0,1);
989                 glVertex2f(-r3, r3);
990                 glEnd();
991                 glDisable(GL_TEXTURE_2D);
992         }
993
994         glColor4ub(255, 255, 255, 128); 
995         glutil_draw_lined_arc(0.0, M_PI*2.0, r1, 40);
996         glutil_draw_lined_arc(0.0, M_PI*2.0, r2, 40);
997         glDisable(GL_BLEND);
998         glDisable( GL_LINE_SMOOTH );
999         
1000         glPopMatrix();
1001 }
1002
1003 int WM_radial_control_modal(bContext *C, wmOperator *op, wmEvent *event)
1004 {
1005         wmRadialControl *rc = (wmRadialControl*)op->customdata;
1006         int mode, initial_mouse[2], delta[2];
1007         float dist;
1008         double new_value = RNA_float_get(op->ptr, "new_value");
1009         int ret = OPERATOR_RUNNING_MODAL;
1010
1011         mode = RNA_int_get(op->ptr, "mode");
1012         RNA_int_get_array(op->ptr, "initial_mouse", initial_mouse);
1013
1014         switch(event->type) {
1015         case MOUSEMOVE:
1016                 delta[0]= initial_mouse[0] - event->x;
1017                 delta[1]= initial_mouse[1] - event->y;
1018                 dist= sqrt(delta[0]*delta[0]+delta[1]*delta[1]);
1019
1020                 if(mode == WM_RADIALCONTROL_SIZE)
1021                         new_value = dist;
1022                 else if(mode == WM_RADIALCONTROL_STRENGTH) {
1023                         new_value = 1 - dist / WM_RADIAL_CONTROL_DISPLAY_SIZE;
1024                 } else if(mode == WM_RADIALCONTROL_ANGLE)
1025                         new_value = ((int)(atan2(delta[1], delta[0]) * (180.0 / M_PI)) + 180);
1026                 
1027                 if(event->ctrl) {
1028                         if(mode == WM_RADIALCONTROL_STRENGTH)
1029                                 new_value = ((int)(new_value * 100) / 10*10) / 100.0f;
1030                         else
1031                                 new_value = ((int)new_value + 5) / 10*10;
1032                 }
1033                 
1034                 break;
1035         case ESCKEY:
1036         case RIGHTMOUSE:
1037                 ret = OPERATOR_CANCELLED;
1038                 break;
1039         case LEFTMOUSE:
1040         case PADENTER:
1041                 op->type->exec(C, op);
1042                 ret = OPERATOR_FINISHED;
1043                 break;
1044         }
1045
1046         /* Clamp */
1047         if(new_value > rc->max_value)
1048                 new_value = rc->max_value;
1049         else if(new_value < 0)
1050                 new_value = 0;
1051
1052         /* Update paint data */
1053         rc->value = new_value;
1054
1055         RNA_float_set(op->ptr, "new_value", new_value);
1056
1057         if(ret != OPERATOR_RUNNING_MODAL) {
1058                 WM_paint_cursor_end(CTX_wm_manager(C), rc->cursor);
1059                 MEM_freeN(rc);
1060         }
1061         
1062         ED_region_tag_redraw(CTX_wm_region(C));
1063
1064         return ret;
1065 }
1066
1067 /* Expects the operator customdata to be an ImBuf (or NULL) */
1068 int WM_radial_control_invoke(bContext *C, wmOperator *op, wmEvent *event)
1069 {
1070         wmRadialControl *rc = MEM_callocN(sizeof(wmRadialControl), "radial control");
1071         int mode = RNA_int_get(op->ptr, "mode");
1072         float initial_value = RNA_float_get(op->ptr, "initial_value");
1073         int mouse[2] = {event->x, event->y};
1074
1075         if(mode == WM_RADIALCONTROL_SIZE) {
1076                 rc->max_value = 200;
1077                 mouse[0]-= initial_value;
1078         }
1079         else if(mode == WM_RADIALCONTROL_STRENGTH) {
1080                 rc->max_value = 1;
1081                 mouse[0]-= WM_RADIAL_CONTROL_DISPLAY_SIZE * (1 - initial_value);
1082         }
1083         else if(mode == WM_RADIALCONTROL_ANGLE) {
1084                 rc->max_value = 360;
1085                 mouse[0]-= WM_RADIAL_CONTROL_DISPLAY_SIZE * cos(initial_value);
1086                 mouse[1]-= WM_RADIAL_CONTROL_DISPLAY_SIZE * sin(initial_value);
1087                 initial_value *= 180.0f/M_PI;
1088         }
1089
1090         if(op->customdata) {
1091                 ImBuf *im = (ImBuf*)op->customdata;
1092                 /* Build GL texture */
1093                 glGenTextures(1, &rc->tex);
1094                 glBindTexture(GL_TEXTURE_2D, rc->tex);
1095                 glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, im->x, im->y, 0, GL_ALPHA, GL_FLOAT, im->rect_float);
1096                 MEM_freeN(im->rect_float);
1097                 MEM_freeN(im);
1098         }
1099
1100         RNA_int_set_array(op->ptr, "initial_mouse", mouse);
1101         RNA_float_set(op->ptr, "new_value", initial_value);
1102                 
1103         op->customdata = rc;
1104         rc->mode = mode;
1105         rc->initial_value = initial_value;
1106         rc->initial_mouse[0] = mouse[0];
1107         rc->initial_mouse[1] = mouse[1];
1108         rc->cursor = WM_paint_cursor_activate(CTX_wm_manager(C), op->type->poll,
1109                                               wm_radial_control_paint, op->customdata);
1110
1111         /* add modal handler */
1112         WM_event_add_modal_handler(C, &CTX_wm_window(C)->handlers, op);
1113         
1114         WM_radial_control_modal(C, op, event);
1115         
1116         return OPERATOR_RUNNING_MODAL;
1117 }
1118
1119 /* Gets a descriptive string of the operation */
1120 void WM_radial_control_string(wmOperator *op, char str[], int maxlen)
1121 {
1122         int mode = RNA_int_get(op->ptr, "mode");
1123         float v = RNA_float_get(op->ptr, "new_value");
1124
1125         if(mode == WM_RADIALCONTROL_SIZE)
1126                 sprintf(str, "Size: %d", (int)v);
1127         else if(mode == WM_RADIALCONTROL_STRENGTH)
1128                 sprintf(str, "Strength: %d", (int)v);
1129         else if(mode == WM_RADIALCONTROL_ANGLE)
1130                 sprintf(str, "Angle: %d", (int)(v * 180.0f/M_PI));
1131 }
1132
1133 /** Important: this doesn't define an actual operator, it
1134     just sets up the common parts of the radial control op. **/
1135 void WM_OT_radial_control_partial(wmOperatorType *ot)
1136 {
1137         static EnumPropertyItem prop_mode_items[] = {
1138                 {WM_RADIALCONTROL_SIZE, "SIZE", "Size", ""},
1139                 {WM_RADIALCONTROL_STRENGTH, "STRENGTH", "Strength", ""},
1140                 {WM_RADIALCONTROL_ANGLE, "ANGLE", "Angle", ""},
1141                 {0, NULL, NULL, NULL}};
1142
1143         /* Should be set in custom invoke() */
1144         RNA_def_float(ot->srna, "initial_value", 0, 0, FLT_MAX, "Initial Value", "", 0, FLT_MAX);
1145
1146         /* Set internally, should be used in custom exec() to get final value */
1147         RNA_def_float(ot->srna, "new_value", 0, 0, FLT_MAX, "New Value", "", 0, FLT_MAX);
1148
1149         /* Should be set before calling operator */
1150         RNA_def_enum(ot->srna, "mode", prop_mode_items, 0, "Mode", "");
1151
1152         /* Internal */
1153         RNA_def_int_vector(ot->srna, "initial_mouse", 2, NULL, INT_MIN, INT_MAX, "initial_mouse", "", INT_MIN, INT_MAX);
1154 }
1155
1156 /* ******************************************************* */
1157  
1158 /* called on initialize WM_exit() */
1159 void wm_operatortype_free(void)
1160 {
1161         BLI_freelistN(&global_ops);
1162 }
1163
1164 /* called on initialize WM_init() */
1165 void wm_operatortype_init(void)
1166 {
1167         WM_operatortype_append(WM_OT_window_duplicate);
1168         WM_operatortype_append(WM_OT_save_homefile);
1169         WM_operatortype_append(WM_OT_window_fullscreen_toggle);
1170         WM_operatortype_append(WM_OT_exit_blender);
1171         WM_operatortype_append(WM_OT_open_recentfile);
1172         WM_operatortype_append(WM_OT_open_mainfile);
1173         WM_operatortype_append(WM_OT_jobs_timer);
1174         WM_operatortype_append(WM_OT_save_as_mainfile);
1175 }
1176
1177 /* default keymap for windows and screens, only call once per WM */
1178 void wm_window_keymap(wmWindowManager *wm)
1179 {
1180         ListBase *keymap= WM_keymap_listbase(wm, "Window", 0, 0);
1181         
1182         /* items to make WM work */
1183         WM_keymap_verify_item(keymap, "WM_OT_jobs_timer", TIMERJOBS, KM_ANY, KM_ANY, 0);
1184         
1185         /* note, this doesn't replace existing keymap items */
1186         WM_keymap_verify_item(keymap, "WM_OT_window_duplicate", AKEY, KM_PRESS, KM_CTRL|KM_ALT, 0);
1187         WM_keymap_verify_item(keymap, "WM_OT_save_homefile", UKEY, KM_PRESS, KM_CTRL, 0);
1188         WM_keymap_verify_item(keymap, "WM_OT_open_recentfile", OKEY, KM_PRESS, KM_CTRL, 0);
1189         WM_keymap_verify_item(keymap, "WM_OT_open_mainfile", F1KEY, KM_PRESS, 0, 0);
1190         WM_keymap_verify_item(keymap, "WM_OT_save_as_mainfile", F2KEY, KM_PRESS, 0, 0);
1191         WM_keymap_verify_item(keymap, "WM_OT_window_fullscreen_toggle", F11KEY, KM_PRESS, 0, 0);
1192         WM_keymap_verify_item(keymap, "WM_OT_exit_blender", QKEY, KM_PRESS, KM_CTRL, 0);
1193
1194 }
1195