Importer base for Chingiz.
[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_scene_types.h"
37 #include "DNA_userdef_types.h"
38 #include "DNA_windowmanager_types.h"
39
40 #include "MEM_guardedalloc.h"
41
42 #include "PIL_time.h"
43
44 #include "BLI_blenlib.h"
45 #include "BLI_dynstr.h" /*for WM_operator_pystring */
46
47 #include "BKE_blender.h"
48 #include "BKE_context.h"
49 #include "BKE_idprop.h"
50 #include "BKE_library.h"
51 #include "BKE_global.h"
52 #include "BKE_main.h"
53 #include "BKE_scene.h"
54 #include "BKE_utildefines.h"
55
56 #include "BIF_gl.h"
57 #include "BIF_glutil.h" /* for paint cursor */
58
59 #include "IMB_imbuf_types.h"
60
61 #include "ED_screen.h"
62 #include "ED_util.h"
63
64 #include "RNA_access.h"
65 #include "RNA_define.h"
66
67 #include "UI_interface.h"
68 #include "UI_resources.h"
69
70 #include "WM_api.h"
71 #include "WM_types.h"
72
73 #include "wm.h"
74 #include "wm_draw.h"
75 #include "wm_event_system.h"
76 #include "wm_subwindow.h"
77 #include "wm_window.h"
78
79
80
81 static ListBase global_ops= {NULL, NULL};
82
83 /* ************ operator API, exported ********** */
84
85 wmOperatorType *WM_operatortype_find(const char *idname)
86 {
87         wmOperatorType *ot;
88         
89         for(ot= global_ops.first; ot; ot= ot->next) {
90                 if(strncmp(ot->idname, idname, OP_MAX_TYPENAME)==0)
91                    return ot;
92         }
93         printf("search for unknown operator %s\n", idname);
94         return NULL;
95 }
96
97 wmOperatorType *WM_operatortype_first(void)
98 {
99         return global_ops.first;
100 }
101
102 /* all ops in 1 list (for time being... needs evaluation later) */
103 void WM_operatortype_append(void (*opfunc)(wmOperatorType*))
104 {
105         wmOperatorType *ot;
106         
107         ot= MEM_callocN(sizeof(wmOperatorType), "operatortype");
108         ot->srna= RNA_def_struct(&BLENDER_RNA, "", "OperatorProperties");
109         opfunc(ot);
110         RNA_def_struct_ui_text(ot->srna, ot->name, ot->description ? ot->description:""); // XXX All ops should have a description but for now allow them not to.
111         RNA_def_struct_identifier(ot->srna, ot->idname);
112         BLI_addtail(&global_ops, ot);
113 }
114
115 void WM_operatortype_append_ptr(void (*opfunc)(wmOperatorType*, void*), void *userdata)
116 {
117         wmOperatorType *ot;
118
119         ot= MEM_callocN(sizeof(wmOperatorType), "operatortype");
120         ot->srna= RNA_def_struct(&BLENDER_RNA, "", "OperatorProperties");
121         opfunc(ot, userdata);
122         RNA_def_struct_ui_text(ot->srna, ot->name, "DOC_BROKEN"); /* TODO - add a discription to wmOperatorType? */
123         RNA_def_struct_identifier(ot->srna, ot->idname);
124         BLI_addtail(&global_ops, ot);
125 }
126
127 int WM_operatortype_remove(const char *idname)
128 {
129         wmOperatorType *ot = WM_operatortype_find(idname);
130
131         if (ot==NULL)
132                 return 0;
133         
134         BLI_remlink(&global_ops, ot);
135         RNA_struct_free(&BLENDER_RNA, ot->srna);
136         MEM_freeN(ot);
137
138         return 1;
139 }
140
141 /* print a string representation of the operator, with the args that it runs 
142  * so python can run it again */
143 char *WM_operator_pystring(wmOperator *op)
144 {
145         const char *arg_name= NULL;
146
147         PropertyRNA *prop, *iterprop;
148         CollectionPropertyIterator iter;
149
150         /* for building the string */
151         DynStr *dynstr= BLI_dynstr_new();
152         char *cstring, *buf;
153         int first_iter=1;
154
155         BLI_dynstr_appendf(dynstr, "%s(", op->idname);
156
157         iterprop= RNA_struct_iterator_property(op->ptr->type);
158         RNA_property_collection_begin(op->ptr, iterprop, &iter);
159
160         for(; iter.valid; RNA_property_collection_next(&iter)) {
161                 prop= iter.ptr.data;
162                 arg_name= RNA_property_identifier(prop);
163
164                 if (strcmp(arg_name, "rna_type")==0) continue;
165
166                 buf= RNA_property_as_string(op->ptr, prop);
167                 
168                 BLI_dynstr_appendf(dynstr, first_iter?"%s=%s":", %s=%s", arg_name, buf);
169
170                 MEM_freeN(buf);
171                 first_iter = 0;
172         }
173
174         RNA_property_collection_end(&iter);
175
176         BLI_dynstr_append(dynstr, ")");
177
178         cstring = BLI_dynstr_get_cstring(dynstr);
179         BLI_dynstr_free(dynstr);
180         return cstring;
181 }
182
183 void WM_operator_properties_create(PointerRNA *ptr, const char *opstring)
184 {
185         wmOperatorType *ot= WM_operatortype_find(opstring);
186
187         if(ot)
188                 RNA_pointer_create(NULL, ot->srna, NULL, ptr);
189         else
190                 RNA_pointer_create(NULL, &RNA_OperatorProperties, NULL, ptr);
191 }
192
193 void WM_operator_properties_free(PointerRNA *ptr)
194 {
195         IDProperty *properties= ptr->data;
196
197         if(properties) {
198                 IDP_FreeProperty(properties);
199                 MEM_freeN(properties);
200         }
201 }
202
203 /* ************ default op callbacks, exported *********** */
204
205 /* invoke callback, uses enum property named "type" */
206 /* only weak thing is the fixed property name... */
207 int WM_menu_invoke(bContext *C, wmOperator *op, wmEvent *event)
208 {
209         PropertyRNA *prop= RNA_struct_find_property(op->ptr, "type");
210         uiPopupMenu *pup;
211         uiLayout *layout;
212
213         if(prop==NULL) {
214                 printf("WM_menu_invoke: %s has no \"type\" enum property\n", op->type->idname);
215         }
216         else if (RNA_property_type(prop) != PROP_ENUM) {
217                 printf("WM_menu_invoke: %s \"type\" is not an enum property\n", op->type->idname);
218         }
219         else {
220                 pup= uiPupMenuBegin(C, op->type->name, 0);
221                 layout= uiPupMenuLayout(pup);
222                 uiItemsEnumO(layout, op->type->idname, "type");
223                 uiPupMenuEnd(C, pup);
224         }
225
226         return OPERATOR_CANCELLED;
227 }
228
229 /* op->invoke */
230 int WM_operator_confirm(bContext *C, wmOperator *op, wmEvent *event)
231 {
232         uiPopupMenu *pup;
233         uiLayout *layout;
234
235         pup= uiPupMenuBegin(C, "OK?", ICON_QUESTION);
236         layout= uiPupMenuLayout(pup);
237         uiItemO(layout, NULL, 0, op->type->idname);
238         uiPupMenuEnd(C, pup);
239         
240         return OPERATOR_CANCELLED;
241 }
242
243 /* op->invoke, opens fileselect if filename property not set, otherwise executes */
244 int WM_operator_filesel(bContext *C, wmOperator *op, wmEvent *event)
245 {
246         if (RNA_property_is_set(op->ptr, "filename")) {
247                 return WM_operator_call(C, op);
248         } 
249         else {
250                 WM_event_add_fileselect(C, op);
251                 return OPERATOR_RUNNING_MODAL;
252         }
253 }
254
255 /* op->poll */
256 int WM_operator_winactive(bContext *C)
257 {
258         if(CTX_wm_window(C)==NULL) return 0;
259         return 1;
260 }
261
262 /* op->invoke */
263 static void redo_cb(bContext *C, void *arg_op, void *arg2)
264 {
265         wmOperator *lastop= arg_op;
266         
267         if(lastop) {
268                 ED_undo_pop(C);
269                 WM_operator_repeat(C, lastop);
270         }
271 }
272
273 static uiBlock *wm_block_create_redo(bContext *C, ARegion *ar, void *arg_op)
274 {
275         wmWindowManager *wm= CTX_wm_manager(C);
276         wmOperator *op= arg_op;
277         PointerRNA ptr;
278         uiBlock *block;
279         uiLayout *layout;
280         uiStyle *style= U.uistyles.first;
281         
282         block= uiBeginBlock(C, ar, "redo_popup", UI_EMBOSS);
283         uiBlockClearFlag(block, UI_BLOCK_LOOP);
284         uiBlockSetFlag(block, UI_BLOCK_KEEP_OPEN|UI_BLOCK_RET_1);
285         uiBlockSetFunc(block, redo_cb, arg_op, NULL);
286
287         if(!op->properties) {
288                 IDPropertyTemplate val = {0};
289                 op->properties= IDP_New(IDP_GROUP, val, "wmOperatorProperties");
290         }
291
292         RNA_pointer_create(&wm->id, op->type->srna, op->properties, &ptr);
293         layout= uiBlockLayout(block, UI_LAYOUT_VERTICAL, UI_LAYOUT_PANEL, 0, 0, 300, 20, style);
294         uiDefAutoButsRNA(C, layout, &ptr);
295
296         uiPopupBoundsBlock(block, 4.0f, 0, 0);
297         uiEndBlock(C, block);
298
299         return block;
300 }
301
302 int WM_operator_redo(bContext *C, wmOperator *op, wmEvent *event)
303 {
304         int retval= OPERATOR_CANCELLED;
305         
306         if(op->type->exec)
307                 retval= op->type->exec(C, op);
308
309         if(retval != OPERATOR_CANCELLED)
310                 uiPupBlock(C, wm_block_create_redo, op);
311
312         return retval;
313 }
314
315 int WM_operator_redo_popup(bContext *C, wmOperator *op)
316 {
317         uiPupBlock(C, wm_block_create_redo, op);
318
319         return OPERATOR_CANCELLED;
320 }
321
322 /* ***************** Debug menu ************************* */
323
324 static uiBlock *wm_block_create_menu(bContext *C, ARegion *ar, void *arg_op)
325 {
326         wmOperator *op= arg_op;
327         uiBlock *block;
328         uiLayout *layout;
329         uiStyle *style= U.uistyles.first;
330         
331         block= uiBeginBlock(C, ar, "_popup", UI_EMBOSS);
332         uiBlockClearFlag(block, UI_BLOCK_LOOP);
333         uiBlockSetFlag(block, UI_BLOCK_KEEP_OPEN|UI_BLOCK_RET_1);
334         
335         layout= uiBlockLayout(block, UI_LAYOUT_VERTICAL, UI_LAYOUT_PANEL, 0, 0, 300, 20, style);
336         uiDefAutoButsRNA(C, layout, op->ptr);
337         
338         uiPopupBoundsBlock(block, 4.0f, 0, 0);
339         uiEndBlock(C, block);
340         
341         return block;
342 }
343
344 static int wm_debug_menu_exec(bContext *C, wmOperator *op)
345 {
346         G.rt= RNA_int_get(op->ptr, "debugval");
347         ED_screen_refresh(CTX_wm_manager(C), CTX_wm_window(C));
348         WM_event_add_notifier(C, NC_WINDOW, NULL);
349         
350         return OPERATOR_FINISHED;       
351 }
352
353 static int wm_debug_menu_invoke(bContext *C, wmOperator *op, wmEvent *event)
354 {
355         
356         RNA_int_set(op->ptr, "debugval", G.rt);
357         
358         /* pass on operator, so return modal */
359         uiPupBlockOperator(C, wm_block_create_menu, op, WM_OP_EXEC_DEFAULT);
360         
361         return OPERATOR_RUNNING_MODAL;
362 }
363
364 static void WM_OT_debug_menu(wmOperatorType *ot)
365 {
366         ot->name= "Debug Menu";
367         ot->idname= "WM_OT_debug_menu";
368         
369         ot->invoke= wm_debug_menu_invoke;
370         ot->exec= wm_debug_menu_exec;
371         ot->poll= WM_operator_winactive;
372         
373         RNA_def_int(ot->srna, "debugval", 0, -10000, 10000, "Debug Value", "", INT_MIN, INT_MAX);
374 }
375
376
377 /* ************ window / screen operator definitions ************** */
378
379 static void WM_OT_window_duplicate(wmOperatorType *ot)
380 {
381         ot->name= "Duplicate Window";
382         ot->idname= "WM_OT_window_duplicate";
383         
384         ot->invoke= WM_operator_confirm;
385         ot->exec= wm_window_duplicate_op;
386         ot->poll= WM_operator_winactive;
387 }
388
389 static void WM_OT_save_homefile(wmOperatorType *ot)
390 {
391         ot->name= "Save User Settings";
392         ot->idname= "WM_OT_save_homefile";
393         
394         ot->invoke= WM_operator_confirm;
395         ot->exec= WM_write_homefile;
396         ot->poll= WM_operator_winactive;
397 }
398
399 static void WM_OT_read_homefile(wmOperatorType *ot)
400 {
401         ot->name= "Reload Start-Up File";
402         ot->idname= "WM_OT_read_homefile";
403         
404         ot->invoke= WM_operator_confirm;
405         ot->exec= WM_read_homefile;
406         ot->poll= WM_operator_winactive;
407         
408         RNA_def_boolean(ot->srna, "factory", 0, "Factory Settings", "");
409 }
410
411
412 /* ********* recent file *********** */
413
414 static int recentfile_exec(bContext *C, wmOperator *op)
415 {
416         int event= RNA_enum_get(op->ptr, "nr");
417
418         // XXX wm in context is not set correctly after WM_read_file -> crash
419         // do it before for now, but is this correct with multiple windows?
420
421         if(event>0) {
422                 if (G.sce[0] && (event==1)) {
423                         WM_event_add_notifier(C, NC_WINDOW, NULL);
424                         WM_read_file(C, G.sce, op->reports);
425                 }
426                 else {
427                         struct RecentFile *recent = BLI_findlink(&(G.recent_files), event-2);
428                         if(recent) {
429                                 WM_event_add_notifier(C, NC_WINDOW, NULL);
430                                 WM_read_file(C, recent->filename, op->reports);
431                         }
432                 }
433         }
434         return 0;
435 }
436
437 static int wm_recentfile_invoke(bContext *C, wmOperator *op, wmEvent *event)
438 {
439         struct RecentFile *recent;
440         uiPopupMenu *pup;
441         uiLayout *layout;
442         int i, ofs= 0;
443
444         pup= uiPupMenuBegin(C, "Open Recent", 0);
445         layout= uiPupMenuLayout(pup);
446
447         if(G.sce[0]) {
448                 uiItemIntO(layout, G.sce, 0, op->type->idname, "nr", 1);
449                 ofs = 1;
450         }
451         
452         for(recent = G.recent_files.first, i=0; (i<U.recent_files) && (recent); recent = recent->next, i++)
453                 if(strcmp(recent->filename, G.sce))
454                         uiItemIntO(layout, recent->filename, 0, op->type->idname, "nr", i+ofs+1);
455
456         uiPupMenuEnd(C, pup);
457         
458         return OPERATOR_CANCELLED;
459 }
460
461 static void WM_OT_open_recentfile(wmOperatorType *ot)
462 {
463         ot->name= "Open Recent File";
464         ot->idname= "WM_OT_open_recentfile";
465         
466         ot->invoke= wm_recentfile_invoke;
467         ot->exec= recentfile_exec;
468         ot->poll= WM_operator_winactive;
469         
470         RNA_def_property(ot->srna, "nr", PROP_ENUM, PROP_NONE);
471 }
472
473 /* ********* main file *********** */
474
475 static void untitled(char *name)
476 {
477         if (G.save_over == 0 && strlen(name) < FILE_MAX-16) {
478                 char *c= BLI_last_slash(name);
479                 
480                 if (c)
481                         strcpy(&c[1], "untitled.blend");
482                 else
483                         strcpy(name, "untitled.blend");
484         }
485 }
486
487
488 static int wm_open_mainfile_invoke(bContext *C, wmOperator *op, wmEvent *event)
489 {
490
491         RNA_string_set(op->ptr, "filename", G.sce);
492         WM_event_add_fileselect(C, op);
493
494         return OPERATOR_RUNNING_MODAL;
495 }
496
497 static int wm_open_mainfile_exec(bContext *C, wmOperator *op)
498 {
499         char filename[FILE_MAX];
500         RNA_string_get(op->ptr, "filename", filename);
501         
502         // XXX wm in context is not set correctly after WM_read_file -> crash
503         // do it before for now, but is this correct with multiple windows?
504         WM_event_add_notifier(C, NC_WINDOW, NULL);
505
506         WM_read_file(C, filename, op->reports);
507         
508         return 0;
509 }
510
511 static void WM_OT_open_mainfile(wmOperatorType *ot)
512 {
513         ot->name= "Open Blender File";
514         ot->idname= "WM_OT_open_mainfile";
515         
516         ot->invoke= wm_open_mainfile_invoke;
517         ot->exec= wm_open_mainfile_exec;
518         ot->poll= WM_operator_winactive;
519         
520         ot->flag= 0;
521         
522         RNA_def_property(ot->srna, "filename", PROP_STRING, PROP_FILEPATH);
523
524 }
525
526 static int wm_save_as_mainfile_invoke(bContext *C, wmOperator *op, wmEvent *event)
527 {
528         char name[FILE_MAX];
529         
530         BLI_strncpy(name, G.sce, FILE_MAX);
531         untitled(name);
532         RNA_string_set(op->ptr, "filename", name);
533         
534         WM_event_add_fileselect(C, op);
535
536         return OPERATOR_RUNNING_MODAL;
537 }
538
539 /* function used for WM_OT_save_mainfile too */
540 static int wm_save_as_mainfile_exec(bContext *C, wmOperator *op)
541 {
542         char filename[FILE_MAX];
543         
544         if(RNA_property_is_set(op->ptr, "filename"))
545                 RNA_string_get(op->ptr, "filename", filename);
546         else {
547                 BLI_strncpy(filename, G.sce, FILE_MAX);
548                 untitled(filename);
549         }
550         WM_write_file(C, filename, op->reports);
551         
552         WM_event_add_notifier(C, NC_WM|ND_FILESAVE, NULL);
553
554         return 0;
555 }
556
557 static void WM_OT_save_as_mainfile(wmOperatorType *ot)
558 {
559         ot->name= "Save As Blender File";
560         ot->idname= "WM_OT_save_as_mainfile";
561         
562         ot->invoke= wm_save_as_mainfile_invoke;
563         ot->exec= wm_save_as_mainfile_exec;
564         ot->poll= WM_operator_winactive;
565         
566         ot->flag= 0;
567         
568         RNA_def_property(ot->srna, "filename", PROP_STRING, PROP_FILEPATH);
569
570 }
571
572 /* *************** Save file directly ******** */
573
574 static int wm_save_mainfile_invoke(bContext *C, wmOperator *op, wmEvent *event)
575 {
576         char name[FILE_MAX];
577         
578         BLI_strncpy(name, G.sce, FILE_MAX);
579         untitled(name);
580         RNA_string_set(op->ptr, "filename", name);
581         uiPupMenuSaveOver(C, op, name);
582
583         return OPERATOR_RUNNING_MODAL;
584 }
585
586 static void WM_OT_save_mainfile(wmOperatorType *ot)
587 {
588         ot->name= "Save Blender File";
589         ot->idname= "WM_OT_save_mainfile";
590         
591         ot->invoke= wm_save_mainfile_invoke;
592         ot->exec= wm_save_as_mainfile_exec;
593         ot->poll= WM_operator_winactive;
594         
595         ot->flag= 0;
596         
597         RNA_def_property(ot->srna, "filename", PROP_STRING, PROP_FILEPATH);
598         
599 }
600
601
602
603
604
605 /* XXX: move these to a more appropriate place */
606 #include "../../collada/collada.h"
607
608 static int wm_collada_export_invoke(bContext *C, wmOperator *op, wmEvent *event)
609 {
610         //char name[FILE_MAX];
611         //BLI_strncpy(name, G.sce, FILE_MAX);
612         //untitled(name);
613
614         // XXX: temporary
615         RNA_string_set(op->ptr, "filename", "/tmp/test.dae");
616         
617         WM_event_add_fileselect(C, op);
618
619         return OPERATOR_RUNNING_MODAL;
620 }
621
622 /* function used for WM_OT_save_mainfile too */
623 static int wm_collada_export_exec(bContext *C, wmOperator *op)
624 {
625         char filename[FILE_MAX];
626         
627         if(RNA_property_is_set(op->ptr, "filename"))
628                 RNA_string_get(op->ptr, "filename", filename);
629         else {
630                 BLI_strncpy(filename, G.sce, FILE_MAX);
631                 untitled(filename);
632         }
633         
634         //WM_write_file(C, filename, op->reports);
635         collada_export(CTX_data_scene(C), filename);
636         
637         /* WM_event_add_notifier(C, NC_WM|ND_FILESAVE, NULL); */
638
639         return OPERATOR_FINISHED;
640 }
641
642 static void WM_OT_collada_export(wmOperatorType *ot)
643 {
644         ot->name= "Collada Export";
645         ot->idname= "WM_OT_collada_export";
646         
647         ot->invoke= wm_collada_export_invoke;
648         ot->exec= wm_collada_export_exec;
649         ot->poll= WM_operator_winactive;
650         
651         ot->flag= 0;
652         
653         RNA_def_property(ot->srna, "filename", PROP_STRING, PROP_FILEPATH);
654 }
655
656 static int wm_collada_import_invoke(bContext *C, wmOperator *op, wmEvent *event)
657 {
658         // XXX: temporary
659         RNA_string_set(op->ptr, "filename", "/tmp/test.dae");
660         
661         WM_event_add_fileselect(C, op);
662
663         return OPERATOR_RUNNING_MODAL;
664 }
665
666 /* function used for WM_OT_save_mainfile too */
667 static int wm_collada_import_exec(bContext *C, wmOperator *op)
668 {
669         char filename[FILE_MAX];
670         
671         if(RNA_property_is_set(op->ptr, "filename"))
672                 RNA_string_get(op->ptr, "filename", filename);
673         else {
674                 BLI_strncpy(filename, G.sce, FILE_MAX);
675                 untitled(filename);
676         }
677         
678         //WM_write_file(C, filename, op->reports);
679         collada_import(CTX_data_scene(C), filename);
680         
681         /* WM_event_add_notifier(C, NC_WM|ND_FILESAVE, NULL); */
682
683         return OPERATOR_FINISHED;
684 }
685
686 static void WM_OT_collada_import(wmOperatorType *ot)
687 {
688         ot->name= "Collada Import";
689         ot->idname= "WM_OT_collada_import";
690         
691         ot->invoke= wm_collada_import_invoke;
692         ot->exec= wm_collada_import_exec;
693         ot->poll= WM_operator_winactive;
694         
695         ot->flag= 0;
696         
697         RNA_def_property(ot->srna, "filename", PROP_STRING, PROP_FILEPATH);
698 }
699
700
701
702
703
704 /* *********************** */
705
706 static void WM_OT_window_fullscreen_toggle(wmOperatorType *ot)
707 {
708     ot->name= "Toggle Fullscreen";
709     ot->idname= "WM_OT_window_fullscreen_toggle";
710
711     ot->invoke= WM_operator_confirm;
712     ot->exec= wm_window_fullscreen_toggle_op;
713     ot->poll= WM_operator_winactive;
714 }
715
716 static int wm_exit_blender_op(bContext *C, wmOperator *op)
717 {
718         WM_operator_free(op);
719         
720         WM_exit(C);     
721         
722         return OPERATOR_FINISHED;
723 }
724
725 static void WM_OT_exit_blender(wmOperatorType *ot)
726 {
727         ot->name= "Exit Blender";
728         ot->idname= "WM_OT_exit_blender";
729
730         ot->invoke= WM_operator_confirm;
731         ot->exec= wm_exit_blender_op;
732         ot->poll= WM_operator_winactive;
733 }
734
735 /* ************ default paint cursors, draw always around cursor *********** */
736 /*
737  - returns handler to free 
738  - poll(bContext): returns 1 if draw should happen
739  - draw(bContext): drawing callback for paint cursor
740 */
741
742 void *WM_paint_cursor_activate(wmWindowManager *wm, int (*poll)(bContext *C),
743                                void (*draw)(bContext *C, int, int, void *customdata), void *customdata)
744 {
745         wmPaintCursor *pc= MEM_callocN(sizeof(wmPaintCursor), "paint cursor");
746         
747         BLI_addtail(&wm->paintcursors, pc);
748         
749         pc->customdata = customdata;
750         pc->poll= poll;
751         pc->draw= draw;
752         
753         return pc;
754 }
755
756 void WM_paint_cursor_end(wmWindowManager *wm, void *handle)
757 {
758         wmPaintCursor *pc;
759         
760         for(pc= wm->paintcursors.first; pc; pc= pc->next) {
761                 if(pc == (wmPaintCursor *)handle) {
762                         BLI_remlink(&wm->paintcursors, pc);
763                         MEM_freeN(pc);
764                         return;
765                 }
766         }
767 }
768
769 /* ************ window gesture operator-callback definitions ************** */
770 /*
771  * These are default callbacks for use in operators requiring gesture input
772  */
773
774 /* **************** Border gesture *************** */
775
776 /* Border gesture has two types:
777    1) WM_GESTURE_CROSS_RECT: starts a cross, on mouse click it changes to border 
778    2) WM_GESTURE_RECT: starts immediate as a border, on mouse click or release it ends
779
780    It stores 4 values (xmin, xmax, ymin, ymax) and event it ended with (event_type)
781 */
782
783 static int border_apply(bContext *C, wmOperator *op, int event_type)
784 {
785         wmGesture *gesture= op->customdata;
786         rcti *rect= gesture->customdata;
787         
788         if(rect->xmin > rect->xmax)
789                 SWAP(int, rect->xmin, rect->xmax);
790         if(rect->ymin > rect->ymax)
791                 SWAP(int, rect->ymin, rect->ymax);
792         
793         if(rect->xmin==rect->xmax || rect->ymin==rect->ymax)
794                 return 0;
795                 
796         /* operator arguments and storage. */
797         RNA_int_set(op->ptr, "xmin", rect->xmin);
798         RNA_int_set(op->ptr, "ymin", rect->ymin);
799         RNA_int_set(op->ptr, "xmax", rect->xmax);
800         RNA_int_set(op->ptr, "ymax", rect->ymax);
801         if( RNA_struct_find_property(op->ptr, "event_type") )
802                 RNA_int_set(op->ptr, "event_type", event_type);
803         
804         op->type->exec(C, op);
805         
806         return 1;
807 }
808
809 static void wm_gesture_end(bContext *C, wmOperator *op)
810 {
811         wmGesture *gesture= op->customdata;
812         
813         WM_gesture_end(C, gesture);     /* frees gesture itself, and unregisters from window */
814         op->customdata= NULL;
815
816         ED_area_tag_redraw(CTX_wm_area(C));
817         
818         if( RNA_struct_find_property(op->ptr, "cursor") )
819                 WM_cursor_restore(CTX_wm_window(C));
820 }
821
822 int WM_border_select_invoke(bContext *C, wmOperator *op, wmEvent *event)
823 {
824         op->customdata= WM_gesture_new(C, event, WM_GESTURE_CROSS_RECT);
825
826         /* add modal handler */
827         WM_event_add_modal_handler(C, &CTX_wm_window(C)->handlers, op);
828         
829         wm_gesture_tag_redraw(C);
830
831         return OPERATOR_RUNNING_MODAL;
832 }
833
834 int WM_border_select_modal(bContext *C, wmOperator *op, wmEvent *event)
835 {
836         wmGesture *gesture= op->customdata;
837         rcti *rect= gesture->customdata;
838         int sx, sy;
839         
840         switch(event->type) {
841                 case MOUSEMOVE:
842                         
843                         wm_subwindow_getorigin(CTX_wm_window(C), gesture->swinid, &sx, &sy);
844                         
845                         if(gesture->type==WM_GESTURE_CROSS_RECT && gesture->mode==0) {
846                                 rect->xmin= rect->xmax= event->x - sx;
847                                 rect->ymin= rect->ymax= event->y - sy;
848                         }
849                         else {
850                                 rect->xmax= event->x - sx;
851                                 rect->ymax= event->y - sy;
852                         }
853                         
854                         wm_gesture_tag_redraw(C);
855
856                         break;
857                         
858                 case LEFTMOUSE:
859                 case MIDDLEMOUSE:
860                 case RIGHTMOUSE:
861                         if(event->val==1) {
862                                 if(gesture->type==WM_GESTURE_CROSS_RECT && gesture->mode==0) {
863                                         gesture->mode= 1;
864                                         wm_gesture_tag_redraw(C);
865                                 }
866                         }
867                         else {
868                                 if(border_apply(C, op, event->type)) {
869                                         wm_gesture_end(C, op);
870                                         return OPERATOR_FINISHED;
871                                 }
872                                 wm_gesture_end(C, op);
873                                 return OPERATOR_CANCELLED;
874                         }
875                         break;
876                 case ESCKEY:
877                         wm_gesture_end(C, op);
878                         return OPERATOR_CANCELLED;
879         }
880         return OPERATOR_RUNNING_MODAL;
881 }
882
883 /* **************** circle gesture *************** */
884 /* works now only for selection or modal paint stuff, calls exec while hold mouse, exit on release */
885
886 int WM_gesture_circle_invoke(bContext *C, wmOperator *op, wmEvent *event)
887 {
888         op->customdata= WM_gesture_new(C, event, WM_GESTURE_CIRCLE);
889         
890         /* add modal handler */
891         WM_event_add_modal_handler(C, &CTX_wm_window(C)->handlers, op);
892         
893         wm_gesture_tag_redraw(C);
894         
895         return OPERATOR_RUNNING_MODAL;
896 }
897
898 static void gesture_circle_apply(bContext *C, wmOperator *op)
899 {
900         wmGesture *gesture= op->customdata;
901         rcti *rect= gesture->customdata;
902         
903         /* operator arguments and storage. */
904         RNA_int_set(op->ptr, "x", rect->xmin);
905         RNA_int_set(op->ptr, "y", rect->ymin);
906         RNA_int_set(op->ptr, "radius", rect->xmax);
907         
908         if(op->type->exec)
909                 op->type->exec(C, op);
910 }
911
912 int WM_gesture_circle_modal(bContext *C, wmOperator *op, wmEvent *event)
913 {
914         wmGesture *gesture= op->customdata;
915         rcti *rect= gesture->customdata;
916         int sx, sy;
917         
918         switch(event->type) {
919                 case MOUSEMOVE:
920                         
921                         wm_subwindow_getorigin(CTX_wm_window(C), gesture->swinid, &sx, &sy);
922                         
923                         rect->xmin= event->x - sx;
924                         rect->ymin= event->y - sy;
925                         
926                         wm_gesture_tag_redraw(C);
927                         
928                         if(gesture->mode)
929                                 gesture_circle_apply(C, op);
930
931                         break;
932                 case WHEELUPMOUSE:
933                         rect->xmax += 2 + rect->xmax/10;
934                         wm_gesture_tag_redraw(C);
935                         break;
936                 case WHEELDOWNMOUSE:
937                         rect->xmax -= 2 + rect->xmax/10;
938                         if(rect->xmax < 1) rect->xmax= 1;
939                         wm_gesture_tag_redraw(C);
940                         break;
941                 case LEFTMOUSE:
942                 case MIDDLEMOUSE:
943                 case RIGHTMOUSE:
944                         if(event->val==0) {     /* key release */
945                                 wm_gesture_end(C, op);
946                                 return OPERATOR_FINISHED;
947                         }
948                         else {
949                                 if( RNA_struct_find_property(op->ptr, "event_type") )
950                                         RNA_int_set(op->ptr, "event_type", event->type);
951                                 
952                                 /* apply first click */
953                                 gesture_circle_apply(C, op);
954                                 gesture->mode= 1;
955                         }
956                         break;
957                 case ESCKEY:
958                         wm_gesture_end(C, op);
959                         return OPERATOR_CANCELLED;
960         }
961         return OPERATOR_RUNNING_MODAL;
962 }
963
964 #if 0
965 /* template to copy from */
966 void WM_OT_circle_gesture(wmOperatorType *ot)
967 {
968         ot->name= "Circle Gesture";
969         ot->idname= "WM_OT_circle_gesture";
970         
971         ot->invoke= WM_gesture_circle_invoke;
972         ot->modal= WM_gesture_circle_modal;
973         
974         ot->poll= WM_operator_winactive;
975         
976         RNA_def_property(ot->srna, "x", PROP_INT, PROP_NONE);
977         RNA_def_property(ot->srna, "y", PROP_INT, PROP_NONE);
978         RNA_def_property(ot->srna, "radius", PROP_INT, PROP_NONE);
979
980 }
981 #endif
982
983 /* **************** Tweak gesture *************** */
984
985 static void tweak_gesture_modal(bContext *C, wmEvent *event)
986 {
987         wmWindow *window= CTX_wm_window(C);
988         wmGesture *gesture= window->tweak;
989         rcti *rect= gesture->customdata;
990         int sx, sy, val;
991         
992         switch(event->type) {
993                 case MOUSEMOVE:
994                         
995                         wm_subwindow_getorigin(window, gesture->swinid, &sx, &sy);
996                         
997                         rect->xmax= event->x - sx;
998                         rect->ymax= event->y - sy;
999                         
1000                         if((val= wm_gesture_evaluate(C, gesture))) {
1001                                 wmEvent event;
1002
1003                                 event= *(window->eventstate);
1004                                 if(gesture->event_type==LEFTMOUSE)
1005                                         event.type= EVT_TWEAK_L;
1006                                 else if(gesture->event_type==RIGHTMOUSE)
1007                                         event.type= EVT_TWEAK_R;
1008                                 else
1009                                         event.type= EVT_TWEAK_M;
1010                                 event.val= val;
1011                                 /* mouse coords! */
1012                                 wm_event_add(window, &event);
1013                                 
1014                                 WM_gesture_end(C, gesture);     /* frees gesture itself, and unregisters from window */
1015                                 window->tweak= NULL;
1016                         }
1017                         
1018                         break;
1019                         
1020                 case LEFTMOUSE:
1021                 case RIGHTMOUSE:
1022                 case MIDDLEMOUSE:
1023                         if(gesture->event_type==event->type) {
1024                                 WM_gesture_end(C, gesture);
1025                                 window->tweak= NULL;
1026                                 
1027                                 /* when tweak fails we should give the other keymap entries a chance */
1028                                 event->val= KM_RELEASE;
1029                         }
1030                         break;
1031                 default:
1032                         WM_gesture_end(C, gesture);
1033                         window->tweak= NULL;
1034         }
1035 }
1036
1037 /* standard tweak, called after window handlers passed on event */
1038 void wm_tweakevent_test(bContext *C, wmEvent *event, int action)
1039 {
1040         wmWindow *win= CTX_wm_window(C);
1041         
1042         if(win->tweak==NULL) {
1043                 if(CTX_wm_region(C)) {
1044                         if(event->val) { // pressed
1045                                 if( ELEM3(event->type, LEFTMOUSE, MIDDLEMOUSE, RIGHTMOUSE) )
1046                                         win->tweak= WM_gesture_new(C, event, WM_GESTURE_TWEAK);
1047                         }
1048                 }
1049         }
1050         else {
1051                 if(action==WM_HANDLER_BREAK) {
1052                         WM_gesture_end(C, win->tweak);
1053                         win->tweak= NULL;
1054                 }
1055                 else
1056                         tweak_gesture_modal(C, event);
1057         }
1058 }
1059
1060 /* *********************** lasso gesture ****************** */
1061
1062 int WM_gesture_lasso_invoke(bContext *C, wmOperator *op, wmEvent *event)
1063 {
1064         op->customdata= WM_gesture_new(C, event, WM_GESTURE_LASSO);
1065         
1066         /* add modal handler */
1067         WM_event_add_modal_handler(C, &CTX_wm_window(C)->handlers, op);
1068         
1069         wm_gesture_tag_redraw(C);
1070         
1071         if( RNA_struct_find_property(op->ptr, "cursor") )
1072                 WM_cursor_modal(CTX_wm_window(C), RNA_int_get(op->ptr, "cursor"));
1073         
1074         return OPERATOR_RUNNING_MODAL;
1075 }
1076
1077 int WM_gesture_lines_invoke(bContext *C, wmOperator *op, wmEvent *event)
1078 {
1079         op->customdata= WM_gesture_new(C, event, WM_GESTURE_LINES);
1080         
1081         /* add modal handler */
1082         WM_event_add_modal_handler(C, &CTX_wm_window(C)->handlers, op);
1083         
1084         wm_gesture_tag_redraw(C);
1085         
1086         if( RNA_struct_find_property(op->ptr, "cursor") )
1087                 WM_cursor_modal(CTX_wm_window(C), RNA_int_get(op->ptr, "cursor"));
1088         
1089         return OPERATOR_RUNNING_MODAL;
1090 }
1091
1092
1093 static void gesture_lasso_apply(bContext *C, wmOperator *op, int event_type)
1094 {
1095         wmGesture *gesture= op->customdata;
1096         PointerRNA itemptr;
1097         float loc[2];
1098         int i;
1099         short *lasso= gesture->customdata;
1100         
1101         /* operator storage as path. */
1102
1103         for(i=0; i<gesture->points; i++, lasso+=2) {
1104                 loc[0]= lasso[0];
1105                 loc[1]= lasso[1];
1106                 RNA_collection_add(op->ptr, "path", &itemptr);
1107                 RNA_float_set_array(&itemptr, "loc", loc);
1108         }
1109         
1110         wm_gesture_end(C, op);
1111                 
1112         if(op->type->exec)
1113                 op->type->exec(C, op);
1114         
1115 }
1116
1117 int WM_gesture_lasso_modal(bContext *C, wmOperator *op, wmEvent *event)
1118 {
1119         wmGesture *gesture= op->customdata;
1120         int sx, sy;
1121         
1122         switch(event->type) {
1123                 case MOUSEMOVE:
1124                         
1125                         wm_gesture_tag_redraw(C);
1126                         
1127                         wm_subwindow_getorigin(CTX_wm_window(C), gesture->swinid, &sx, &sy);
1128                         if(gesture->points < WM_LASSO_MAX_POINTS) {
1129                                 short *lasso= gesture->customdata;
1130                                 lasso += 2 * gesture->points;
1131                                 lasso[0] = event->x - sx;
1132                                 lasso[1] = event->y - sy;
1133                                 gesture->points++;
1134                         }
1135                         else {
1136                                 gesture_lasso_apply(C, op, event->type);
1137                                 return OPERATOR_FINISHED;
1138                         }
1139                         break;
1140                         
1141                 case LEFTMOUSE:
1142                 case MIDDLEMOUSE:
1143                 case RIGHTMOUSE:
1144                         if(event->val==0) {     /* key release */
1145                                 gesture_lasso_apply(C, op, event->type);
1146                                 return OPERATOR_FINISHED;
1147                         }
1148                         break;
1149                 case ESCKEY:
1150                         wm_gesture_end(C, op);
1151                         return OPERATOR_CANCELLED;
1152         }
1153         return OPERATOR_RUNNING_MODAL;
1154 }
1155
1156 int WM_gesture_lines_modal(bContext *C, wmOperator *op, wmEvent *event)
1157 {
1158         return WM_gesture_lasso_modal(C, op, event);
1159 }
1160
1161 #if 0
1162 /* template to copy from */
1163
1164 static int gesture_lasso_exec(bContext *C, wmOperator *op)
1165 {
1166         RNA_BEGIN(op->ptr, itemptr, "path") {
1167                 float loc[2];
1168                 
1169                 RNA_float_get_array(&itemptr, "loc", loc);
1170                 printf("Location: %f %f\n", loc[0], loc[1]);
1171         }
1172         RNA_END;
1173         
1174         return OPERATOR_FINISHED;
1175 }
1176
1177 void WM_OT_lasso_gesture(wmOperatorType *ot)
1178 {
1179         PropertyRNA *prop;
1180         
1181         ot->name= "Lasso Gesture";
1182         ot->idname= "WM_OT_lasso_gesture";
1183         
1184         ot->invoke= WM_gesture_lasso_invoke;
1185         ot->modal= WM_gesture_lasso_modal;
1186         ot->exec= gesture_lasso_exec;
1187         
1188         ot->poll= WM_operator_winactive;
1189         
1190         prop= RNA_def_property(ot->srna, "path", PROP_COLLECTION, PROP_NONE);
1191         RNA_def_property_struct_runtime(prop, &RNA_OperatorMousePath);
1192 }
1193 #endif
1194
1195 /* *********************** radial control ****************** */
1196
1197 const int WM_RADIAL_CONTROL_DISPLAY_SIZE = 200;
1198
1199 typedef struct wmRadialControl {
1200         int mode;
1201         float initial_value, value, max_value;
1202         int initial_mouse[2];
1203         void *cursor;
1204         GLuint tex;
1205 } wmRadialControl;
1206
1207 static void wm_radial_control_paint(bContext *C, int x, int y, void *customdata)
1208 {
1209         wmRadialControl *rc = (wmRadialControl*)customdata;
1210         ARegion *ar = CTX_wm_region(C);
1211         float r1=0.0f, r2=0.0f, r3=0.0f, angle=0.0f;
1212
1213         /* Keep cursor in the original place */
1214         x = rc->initial_mouse[0] - ar->winrct.xmin;
1215         y = rc->initial_mouse[1] - ar->winrct.ymin;
1216
1217         glPushMatrix();
1218         
1219         glTranslatef((float)x, (float)y, 0.0f);
1220
1221         if(rc->mode == WM_RADIALCONTROL_SIZE) {
1222                 r1= rc->value;
1223                 r2= rc->initial_value;
1224                 r3= r1;
1225         } else if(rc->mode == WM_RADIALCONTROL_STRENGTH) {
1226                 r1= (1 - rc->value) * WM_RADIAL_CONTROL_DISPLAY_SIZE;
1227                 r2= WM_RADIAL_CONTROL_DISPLAY_SIZE;
1228                 r3= WM_RADIAL_CONTROL_DISPLAY_SIZE;
1229         } else if(rc->mode == WM_RADIALCONTROL_ANGLE) {
1230                 r1= r2= WM_RADIAL_CONTROL_DISPLAY_SIZE;
1231                 r3= WM_RADIAL_CONTROL_DISPLAY_SIZE;
1232                 angle = rc->value;
1233         }
1234
1235         glColor4ub(255, 255, 255, 128);
1236         glEnable( GL_LINE_SMOOTH );
1237         glEnable(GL_BLEND);
1238
1239         if(rc->mode == WM_RADIALCONTROL_ANGLE)
1240                 fdrawline(0, 0, WM_RADIAL_CONTROL_DISPLAY_SIZE, 0);
1241
1242         if(rc->tex) {
1243                 const float str = rc->mode == WM_RADIALCONTROL_STRENGTH ? (rc->value + 0.5) : 1;
1244
1245                 if(rc->mode == WM_RADIALCONTROL_ANGLE) {
1246                         glRotatef(angle, 0, 0, 1);
1247                         fdrawline(0, 0, WM_RADIAL_CONTROL_DISPLAY_SIZE, 0);
1248                 }
1249
1250                 glBindTexture(GL_TEXTURE_2D, rc->tex);
1251
1252                 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1253                 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1254
1255                 glEnable(GL_TEXTURE_2D);
1256                 glBegin(GL_QUADS);
1257                 glColor4f(0,0,0, str);
1258                 glTexCoord2f(0,0);
1259                 glVertex2f(-r3, -r3);
1260                 glTexCoord2f(1,0);
1261                 glVertex2f(r3, -r3);
1262                 glTexCoord2f(1,1);
1263                 glVertex2f(r3, r3);
1264                 glTexCoord2f(0,1);
1265                 glVertex2f(-r3, r3);
1266                 glEnd();
1267                 glDisable(GL_TEXTURE_2D);
1268         }
1269
1270         glColor4ub(255, 255, 255, 128); 
1271         glutil_draw_lined_arc(0.0, M_PI*2.0, r1, 40);
1272         glutil_draw_lined_arc(0.0, M_PI*2.0, r2, 40);
1273         glDisable(GL_BLEND);
1274         glDisable( GL_LINE_SMOOTH );
1275         
1276         glPopMatrix();
1277 }
1278
1279 int WM_radial_control_modal(bContext *C, wmOperator *op, wmEvent *event)
1280 {
1281         wmRadialControl *rc = (wmRadialControl*)op->customdata;
1282         int mode, initial_mouse[2], delta[2];
1283         float dist;
1284         double new_value = RNA_float_get(op->ptr, "new_value");
1285         int ret = OPERATOR_RUNNING_MODAL;
1286
1287         mode = RNA_int_get(op->ptr, "mode");
1288         RNA_int_get_array(op->ptr, "initial_mouse", initial_mouse);
1289
1290         switch(event->type) {
1291         case MOUSEMOVE:
1292                 delta[0]= initial_mouse[0] - event->x;
1293                 delta[1]= initial_mouse[1] - event->y;
1294                 dist= sqrt(delta[0]*delta[0]+delta[1]*delta[1]);
1295
1296                 if(mode == WM_RADIALCONTROL_SIZE)
1297                         new_value = dist;
1298                 else if(mode == WM_RADIALCONTROL_STRENGTH) {
1299                         new_value = 1 - dist / WM_RADIAL_CONTROL_DISPLAY_SIZE;
1300                 } else if(mode == WM_RADIALCONTROL_ANGLE)
1301                         new_value = ((int)(atan2(delta[1], delta[0]) * (180.0 / M_PI)) + 180);
1302                 
1303                 if(event->ctrl) {
1304                         if(mode == WM_RADIALCONTROL_STRENGTH)
1305                                 new_value = ((int)(new_value * 100) / 10*10) / 100.0f;
1306                         else
1307                                 new_value = ((int)new_value + 5) / 10*10;
1308                 }
1309                 
1310                 break;
1311         case ESCKEY:
1312         case RIGHTMOUSE:
1313                 ret = OPERATOR_CANCELLED;
1314                 break;
1315         case LEFTMOUSE:
1316         case PADENTER:
1317                 op->type->exec(C, op);
1318                 ret = OPERATOR_FINISHED;
1319                 break;
1320         }
1321
1322         /* Clamp */
1323         if(new_value > rc->max_value)
1324                 new_value = rc->max_value;
1325         else if(new_value < 0)
1326                 new_value = 0;
1327
1328         /* Update paint data */
1329         rc->value = new_value;
1330
1331         RNA_float_set(op->ptr, "new_value", new_value);
1332
1333         if(ret != OPERATOR_RUNNING_MODAL) {
1334                 WM_paint_cursor_end(CTX_wm_manager(C), rc->cursor);
1335                 MEM_freeN(rc);
1336         }
1337         
1338         ED_region_tag_redraw(CTX_wm_region(C));
1339
1340         return ret;
1341 }
1342
1343 /* Expects the operator customdata to be an ImBuf (or NULL) */
1344 int WM_radial_control_invoke(bContext *C, wmOperator *op, wmEvent *event)
1345 {
1346         wmRadialControl *rc = MEM_callocN(sizeof(wmRadialControl), "radial control");
1347         int mode = RNA_int_get(op->ptr, "mode");
1348         float initial_value = RNA_float_get(op->ptr, "initial_value");
1349         int mouse[2] = {event->x, event->y};
1350
1351         if(mode == WM_RADIALCONTROL_SIZE) {
1352                 rc->max_value = 200;
1353                 mouse[0]-= initial_value;
1354         }
1355         else if(mode == WM_RADIALCONTROL_STRENGTH) {
1356                 rc->max_value = 1;
1357                 mouse[0]-= WM_RADIAL_CONTROL_DISPLAY_SIZE * (1 - initial_value);
1358         }
1359         else if(mode == WM_RADIALCONTROL_ANGLE) {
1360                 rc->max_value = 360;
1361                 mouse[0]-= WM_RADIAL_CONTROL_DISPLAY_SIZE * cos(initial_value);
1362                 mouse[1]-= WM_RADIAL_CONTROL_DISPLAY_SIZE * sin(initial_value);
1363                 initial_value *= 180.0f/M_PI;
1364         }
1365
1366         if(op->customdata) {
1367                 ImBuf *im = (ImBuf*)op->customdata;
1368                 /* Build GL texture */
1369                 glGenTextures(1, &rc->tex);
1370                 glBindTexture(GL_TEXTURE_2D, rc->tex);
1371                 glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, im->x, im->y, 0, GL_ALPHA, GL_FLOAT, im->rect_float);
1372                 MEM_freeN(im->rect_float);
1373                 MEM_freeN(im);
1374         }
1375
1376         RNA_int_set_array(op->ptr, "initial_mouse", mouse);
1377         RNA_float_set(op->ptr, "new_value", initial_value);
1378                 
1379         op->customdata = rc;
1380         rc->mode = mode;
1381         rc->initial_value = initial_value;
1382         rc->initial_mouse[0] = mouse[0];
1383         rc->initial_mouse[1] = mouse[1];
1384         rc->cursor = WM_paint_cursor_activate(CTX_wm_manager(C), op->type->poll,
1385                                               wm_radial_control_paint, op->customdata);
1386
1387         /* add modal handler */
1388         WM_event_add_modal_handler(C, &CTX_wm_window(C)->handlers, op);
1389         
1390         WM_radial_control_modal(C, op, event);
1391         
1392         return OPERATOR_RUNNING_MODAL;
1393 }
1394
1395 /* Gets a descriptive string of the operation */
1396 void WM_radial_control_string(wmOperator *op, char str[], int maxlen)
1397 {
1398         int mode = RNA_int_get(op->ptr, "mode");
1399         float v = RNA_float_get(op->ptr, "new_value");
1400
1401         if(mode == WM_RADIALCONTROL_SIZE)
1402                 sprintf(str, "Size: %d", (int)v);
1403         else if(mode == WM_RADIALCONTROL_STRENGTH)
1404                 sprintf(str, "Strength: %d", (int)v);
1405         else if(mode == WM_RADIALCONTROL_ANGLE)
1406                 sprintf(str, "Angle: %d", (int)(v * 180.0f/M_PI));
1407 }
1408
1409 /** Important: this doesn't define an actual operator, it
1410     just sets up the common parts of the radial control op. **/
1411 void WM_OT_radial_control_partial(wmOperatorType *ot)
1412 {
1413         static EnumPropertyItem prop_mode_items[] = {
1414                 {WM_RADIALCONTROL_SIZE, "SIZE", "Size", ""},
1415                 {WM_RADIALCONTROL_STRENGTH, "STRENGTH", "Strength", ""},
1416                 {WM_RADIALCONTROL_ANGLE, "ANGLE", "Angle", ""},
1417                 {0, NULL, NULL, NULL}};
1418
1419         /* Should be set in custom invoke() */
1420         RNA_def_float(ot->srna, "initial_value", 0, 0, FLT_MAX, "Initial Value", "", 0, FLT_MAX);
1421
1422         /* Set internally, should be used in custom exec() to get final value */
1423         RNA_def_float(ot->srna, "new_value", 0, 0, FLT_MAX, "New Value", "", 0, FLT_MAX);
1424
1425         /* Should be set before calling operator */
1426         RNA_def_enum(ot->srna, "mode", prop_mode_items, 0, "Mode", "");
1427
1428         /* Internal */
1429         RNA_def_int_vector(ot->srna, "initial_mouse", 2, NULL, INT_MIN, INT_MAX, "initial_mouse", "", INT_MIN, INT_MAX);
1430 }
1431
1432 /* ************************** timer for testing ***************** */
1433
1434 /* uses no type defines, fully local testing function anyway... ;) */
1435
1436 static int ten_timer_exec(bContext *C, wmOperator *op)
1437 {
1438         ARegion *ar= CTX_wm_region(C);
1439         double stime= PIL_check_seconds_timer();
1440         int type = RNA_int_get(op->ptr, "type");
1441         int a, time;
1442         char tmpstr[128];
1443         
1444         WM_cursor_wait(1);
1445         
1446         for(a=0; a<10; a++) {
1447                 if (type==0) {
1448                         ED_region_do_draw(C, ar);
1449                 } 
1450                 else if (type==1) {
1451                         wmWindow *win= CTX_wm_window(C);
1452                         
1453                         ED_region_tag_redraw(ar);
1454                         wm_draw_update(C);
1455                         
1456                         CTX_wm_window_set(C, win);      /* XXX context manipulation warning! */
1457                 }
1458                 else if (type==2) {
1459                         wmWindow *win= CTX_wm_window(C);
1460                         ScrArea *sa;
1461                         
1462                         for(sa= CTX_wm_screen(C)->areabase.first; sa; sa= sa->next)
1463                                 ED_area_tag_redraw(sa);
1464                         wm_draw_update(C);
1465                         
1466                         CTX_wm_window_set(C, win);      /* XXX context manipulation warning! */
1467                 }
1468                 else if (type==3) {
1469                         Scene *scene= CTX_data_scene(C);
1470                         
1471                         if(a & 1) scene->r.cfra--;
1472                         else scene->r.cfra++;
1473                         scene_update_for_newframe(scene, scene->lay);
1474                 }
1475                 else {
1476                         ED_undo_pop(C);
1477                         ED_undo_redo(C);
1478                 }
1479         }
1480         
1481         time= (int) ((PIL_check_seconds_timer()-stime)*1000);
1482         
1483         if(type==0) sprintf(tmpstr, "10 x Draw Region: %d ms", time);
1484         if(type==1) sprintf(tmpstr, "10 x Draw Region and Swap: %d ms", time);
1485         if(type==2) sprintf(tmpstr, "10 x Draw Window and Swap: %d ms", time);
1486         if(type==3) sprintf(tmpstr, "Anim Step: %d ms", time);
1487         if(type==4) sprintf(tmpstr, "10 x Undo/Redo: %d ms", time);
1488         
1489         WM_cursor_wait(0);
1490         
1491         uiPupMenuNotice(C, tmpstr);
1492         
1493         return OPERATOR_FINISHED;
1494 }
1495
1496 static void WM_OT_ten_timer(wmOperatorType *ot)
1497 {
1498         static EnumPropertyItem prop_type_items[] = {
1499         {0, "DRAW", "Draw Region", ""},
1500         {1, "DRAWSWAP", "Draw Region + Swap", ""},
1501         {2, "DRAWWINSWAP", "Draw Window + Swap", ""},
1502         {3, "ANIMSTEP", "Anim Step", ""},
1503         {4, "UNDO", "Undo/Redo", ""},
1504         {0, NULL, NULL, NULL}};
1505         
1506         ot->name= "Ten Timer";
1507         ot->idname= "WM_OT_ten_timer";
1508         
1509         ot->invoke= WM_menu_invoke;
1510         ot->exec= ten_timer_exec;
1511         ot->poll= WM_operator_winactive;
1512         
1513         RNA_def_enum(ot->srna, "type", prop_type_items, 0, "Type", "");
1514
1515 }
1516
1517
1518
1519 /* ******************************************************* */
1520  
1521 /* called on initialize WM_exit() */
1522 void wm_operatortype_free(void)
1523 {
1524         BLI_freelistN(&global_ops);
1525 }
1526
1527 /* called on initialize WM_init() */
1528 void wm_operatortype_init(void)
1529 {
1530         WM_operatortype_append(WM_OT_window_duplicate);
1531         WM_operatortype_append(WM_OT_read_homefile);
1532         WM_operatortype_append(WM_OT_save_homefile);
1533         WM_operatortype_append(WM_OT_window_fullscreen_toggle);
1534         WM_operatortype_append(WM_OT_exit_blender);
1535         WM_operatortype_append(WM_OT_open_recentfile);
1536         WM_operatortype_append(WM_OT_open_mainfile);
1537         WM_operatortype_append(WM_OT_jobs_timer);
1538         WM_operatortype_append(WM_OT_save_as_mainfile);
1539         WM_operatortype_append(WM_OT_save_mainfile);
1540         WM_operatortype_append(WM_OT_ten_timer);
1541         WM_operatortype_append(WM_OT_debug_menu);
1542
1543         /* XXX: move these */
1544         WM_operatortype_append(WM_OT_collada_export);
1545         WM_operatortype_append(WM_OT_collada_import);
1546 }
1547
1548 /* default keymap for windows and screens, only call once per WM */
1549 void wm_window_keymap(wmWindowManager *wm)
1550 {
1551         ListBase *keymap= WM_keymap_listbase(wm, "Window", 0, 0);
1552         
1553         /* items to make WM work */
1554         WM_keymap_verify_item(keymap, "WM_OT_jobs_timer", TIMERJOBS, KM_ANY, KM_ANY, 0);
1555         
1556         /* note, this doesn't replace existing keymap items */
1557         WM_keymap_verify_item(keymap, "WM_OT_window_duplicate", WKEY, KM_PRESS, KM_CTRL|KM_ALT, 0);
1558         WM_keymap_verify_item(keymap, "WM_OT_read_homefile", XKEY, KM_PRESS, KM_CTRL, 0);
1559         WM_keymap_verify_item(keymap, "WM_OT_save_homefile", UKEY, KM_PRESS, KM_CTRL, 0);
1560         WM_keymap_verify_item(keymap, "WM_OT_open_recentfile", OKEY, KM_PRESS, KM_CTRL, 0);
1561         WM_keymap_verify_item(keymap, "WM_OT_open_mainfile", F1KEY, KM_PRESS, 0, 0);
1562         WM_keymap_verify_item(keymap, "WM_OT_save_mainfile", WKEY, KM_PRESS, KM_CTRL, 0);
1563         WM_keymap_verify_item(keymap, "WM_OT_save_as_mainfile", F2KEY, KM_PRESS, 0, 0);
1564         WM_keymap_verify_item(keymap, "WM_OT_window_fullscreen_toggle", F11KEY, KM_PRESS, 0, 0);
1565         WM_keymap_verify_item(keymap, "WM_OT_exit_blender", QKEY, KM_PRESS, KM_CTRL, 0);
1566
1567         /* debug/testing */
1568         WM_keymap_verify_item(keymap, "WM_OT_ten_timer", TKEY, KM_PRESS, KM_ALT|KM_CTRL, 0);
1569         WM_keymap_verify_item(keymap, "WM_OT_debug_menu", DKEY, KM_PRESS, KM_ALT|KM_CTRL, 0);
1570         
1571 }
1572