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