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