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