e048601a4d710d0ac1868cefa94b36d566b6ee41
[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 #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:"(undocumented operator)"); // 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, ot->description ? ot->description:"(undocumented operator)");
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 /* op->poll */
525 int wm_search_menu_poll(bContext *C)
526 {
527         if(CTX_wm_window(C)==NULL) return 0;
528         if(CTX_wm_area(C) && CTX_wm_area(C)->spacetype==SPACE_CONSOLE) return 0;  // XXX - so we can use the shortcut in the console
529         return 1;
530 }
531
532 static void WM_OT_search_menu(wmOperatorType *ot)
533 {
534         ot->name= "Search Menu";
535         ot->idname= "WM_OT_search_menu";
536         
537         ot->invoke= wm_search_menu_invoke;
538         ot->exec= wm_search_menu_exec;
539         ot->poll= wm_search_menu_poll;
540 }
541
542
543 /* ************ window / screen operator definitions ************** */
544
545 static void WM_OT_window_duplicate(wmOperatorType *ot)
546 {
547         ot->name= "Duplicate Window";
548         ot->idname= "WM_OT_window_duplicate";
549         
550         ot->invoke= WM_operator_confirm;
551         ot->exec= wm_window_duplicate_op;
552         ot->poll= WM_operator_winactive;
553 }
554
555 static void WM_OT_save_homefile(wmOperatorType *ot)
556 {
557         ot->name= "Save User Settings";
558         ot->idname= "WM_OT_save_homefile";
559         
560         ot->invoke= WM_operator_confirm;
561         ot->exec= WM_write_homefile;
562         ot->poll= WM_operator_winactive;
563 }
564
565 static void WM_OT_read_homefile(wmOperatorType *ot)
566 {
567         ot->name= "Reload Start-Up File";
568         ot->idname= "WM_OT_read_homefile";
569         
570         ot->invoke= WM_operator_confirm;
571         ot->exec= WM_read_homefile;
572         ot->poll= WM_operator_winactive;
573         
574         RNA_def_boolean(ot->srna, "factory", 0, "Factory Settings", "");
575 }
576
577
578 /* ********* recent file *********** */
579
580 static int recentfile_exec(bContext *C, wmOperator *op)
581 {
582         int event= RNA_enum_get(op->ptr, "file");
583
584         // XXX wm in context is not set correctly after WM_read_file -> crash
585         // do it before for now, but is this correct with multiple windows?
586
587         if(event>0) {
588                 if (G.sce[0] && (event==1)) {
589                         WM_event_add_notifier(C, NC_WINDOW, NULL);
590                         WM_read_file(C, G.sce, op->reports);
591                 }
592                 else {
593                         struct RecentFile *recent = BLI_findlink(&(G.recent_files), event-2);
594                         if(recent) {
595                                 WM_event_add_notifier(C, NC_WINDOW, NULL);
596                                 WM_read_file(C, recent->filename, op->reports);
597                         }
598                 }
599         }
600         return 0;
601 }
602
603 static int wm_recentfile_invoke(bContext *C, wmOperator *op, wmEvent *event)
604 {
605         uiPopupMenu *pup;
606         uiLayout *layout;
607
608         pup= uiPupMenuBegin(C, "Open Recent", 0);
609         layout= uiPupMenuLayout(pup);
610         uiItemsEnumO(layout, op->type->idname, "file");
611         uiPupMenuEnd(C, pup);
612         
613         return OPERATOR_CANCELLED;
614 }
615
616 static EnumPropertyItem *open_recentfile_itemf(bContext *C, PointerRNA *ptr, int *free)
617 {
618         EnumPropertyItem tmp = {0, "", 0, "", ""};
619         EnumPropertyItem *item= NULL;
620         struct RecentFile *recent;
621         int totitem= 0, i, ofs= 0;
622
623         if(G.sce[0]) {
624                 tmp.value= 1;
625                 tmp.identifier= G.sce;
626                 tmp.name= G.sce;
627                 RNA_enum_item_add(&item, &totitem, &tmp);
628                 ofs = 1;
629         }
630
631         /* dynamically construct enum */
632         for(recent = G.recent_files.first, i=0; (i<U.recent_files) && (recent); recent = recent->next, i++) {
633                 if(strcmp(recent->filename, G.sce)) {
634                         tmp.value= i+ofs+1;
635                         tmp.identifier= recent->filename;
636                         tmp.name= recent->filename;
637                         RNA_enum_item_add(&item, &totitem, &tmp);
638                 }
639         }
640
641         RNA_enum_item_end(&item, &totitem);
642         *free= 1;
643
644         return item;
645 }
646
647 static void WM_OT_open_recentfile(wmOperatorType *ot)
648 {
649         PropertyRNA *prop;
650         static EnumPropertyItem file_items[]= {
651                 {0, NULL, 0, NULL, NULL}};
652
653         ot->name= "Open Recent File";
654         ot->idname= "WM_OT_open_recentfile";
655         
656         ot->invoke= wm_recentfile_invoke;
657         ot->exec= recentfile_exec;
658         ot->poll= WM_operator_winactive;
659         
660         prop= RNA_def_enum(ot->srna, "file", file_items, 1, "File", "");
661         RNA_def_enum_funcs(prop, open_recentfile_itemf);
662 }
663
664 /* ********* main file *********** */
665
666 static void untitled(char *name)
667 {
668         if (G.save_over == 0 && strlen(name) < FILE_MAX-16) {
669                 char *c= BLI_last_slash(name);
670                 
671                 if (c)
672                         strcpy(&c[1], "untitled.blend");
673                 else
674                         strcpy(name, "untitled.blend");
675         }
676 }
677
678
679 static int wm_open_mainfile_invoke(bContext *C, wmOperator *op, wmEvent *event)
680 {
681
682         RNA_string_set(op->ptr, "filename", G.sce);
683         RNA_boolean_set(op->ptr, "filter_blender", 1);
684         RNA_boolean_set(op->ptr, "filter_folder", 1);
685         WM_event_add_fileselect(C, op);
686
687         return OPERATOR_RUNNING_MODAL;
688 }
689
690 static int wm_open_mainfile_exec(bContext *C, wmOperator *op)
691 {
692         char filename[FILE_MAX];
693         RNA_string_get(op->ptr, "filename", filename);
694         
695         // XXX wm in context is not set correctly after WM_read_file -> crash
696         // do it before for now, but is this correct with multiple windows?
697         WM_event_add_notifier(C, NC_WINDOW, NULL);
698
699         WM_read_file(C, filename, op->reports);
700         
701         return 0;
702 }
703
704 static void WM_OT_open_mainfile(wmOperatorType *ot)
705 {
706         PropertyRNA *prop;
707         ot->name= "Open Blender File";
708         ot->idname= "WM_OT_open_mainfile";
709         
710         ot->invoke= wm_open_mainfile_invoke;
711         ot->exec= wm_open_mainfile_exec;
712         ot->poll= WM_operator_winactive;
713         
714         RNA_def_string_file_path(ot->srna, "filename", "", FILE_MAX, "Filename", "File path of blendfile to open.");
715
716         prop= RNA_def_boolean(ot->srna, "filter_blender", 0, "Filter Blendfiles", "");
717         RNA_def_property_boolean_sdna(prop, NULL, "filter", BLENDERFILE);
718         prop= RNA_def_boolean(ot->srna, "filter_folder", 0, "Filter Blendfiles", "");
719         RNA_def_property_boolean_sdna(prop, NULL, "filter", FOLDERFILE);
720         
721 }
722
723 static int wm_recover_last_session_exec(bContext *C, wmOperator *op)
724 {
725         char scestr[FILE_MAX], filename[FILE_MAX];
726         int save_over;
727
728         /* back up some values */
729         BLI_strncpy(scestr, G.sce, sizeof(scestr));
730         save_over = G.save_over;
731
732         // XXX wm in context is not set correctly after WM_read_file -> crash
733         // do it before for now, but is this correct with multiple windows?
734         WM_event_add_notifier(C, NC_WINDOW, NULL);
735
736         /* load file */
737         BLI_make_file_string("/", filename, btempdir, "quit.blend");
738         WM_read_file(C, filename, op->reports);
739
740         /* restore */
741         G.save_over = save_over;
742         BLI_strncpy(G.sce, scestr, sizeof(G.sce));
743
744         return 0;
745 }
746
747 static void WM_OT_recover_last_session(wmOperatorType *ot)
748 {
749         ot->name= "Recover Last Session";
750         ot->idname= "WM_OT_recover_last_session";
751         
752         ot->exec= wm_recover_last_session_exec;
753         ot->poll= WM_operator_winactive;
754 }
755
756 static void save_set_compress(wmOperator *op)
757 {
758         if(!RNA_property_is_set(op->ptr, "compress")) {
759                 if(G.save_over) /* keep flag for existing file */
760                         RNA_boolean_set(op->ptr, "compress", G.fileflags & G_FILE_COMPRESS);
761                 else /* use userdef for new file */
762                         RNA_boolean_set(op->ptr, "compress", U.flag & USER_FILECOMPRESS);
763         }
764 }
765
766 static int wm_save_as_mainfile_invoke(bContext *C, wmOperator *op, wmEvent *event)
767 {
768         char name[FILE_MAX];
769
770         save_set_compress(op);
771         
772         BLI_strncpy(name, G.sce, FILE_MAX);
773         untitled(name);
774         RNA_string_set(op->ptr, "filename", name);
775         
776         WM_event_add_fileselect(C, op);
777
778         return OPERATOR_RUNNING_MODAL;
779 }
780
781 /* function used for WM_OT_save_mainfile too */
782 static int wm_save_as_mainfile_exec(bContext *C, wmOperator *op)
783 {
784         char filename[FILE_MAX];
785         int compress;
786
787         save_set_compress(op);
788         compress= RNA_boolean_get(op->ptr, "compress");
789         
790         if(RNA_property_is_set(op->ptr, "filename"))
791                 RNA_string_get(op->ptr, "filename", filename);
792         else {
793                 BLI_strncpy(filename, G.sce, FILE_MAX);
794                 untitled(filename);
795         }
796
797         WM_write_file(C, filename, compress, op->reports);
798         
799         WM_event_add_notifier(C, NC_WM|ND_FILESAVE, NULL);
800
801         return 0;
802 }
803
804 static void WM_OT_save_as_mainfile(wmOperatorType *ot)
805 {
806         ot->name= "Save As Blender File";
807         ot->idname= "WM_OT_save_as_mainfile";
808         
809         ot->invoke= wm_save_as_mainfile_invoke;
810         ot->exec= wm_save_as_mainfile_exec;
811         ot->poll= WM_operator_winactive;
812         
813         RNA_def_string_file_path(ot->srna, "filename", "", 0, "Filename", "");
814         RNA_def_boolean(ot->srna, "compress", 0, "Compress", "Write compressed .blend file.");
815 }
816
817 /* *************** Save file directly ******** */
818
819 static int wm_save_mainfile_invoke(bContext *C, wmOperator *op, wmEvent *event)
820 {
821         char name[FILE_MAX];
822
823         save_set_compress(op);
824         
825         BLI_strncpy(name, G.sce, FILE_MAX);
826         untitled(name);
827         RNA_string_set(op->ptr, "filename", name);
828         uiPupMenuSaveOver(C, op, name);
829
830         return OPERATOR_RUNNING_MODAL;
831 }
832
833 static void WM_OT_save_mainfile(wmOperatorType *ot)
834 {
835         ot->name= "Save Blender File";
836         ot->idname= "WM_OT_save_mainfile";
837         
838         ot->invoke= wm_save_mainfile_invoke;
839         ot->exec= wm_save_as_mainfile_exec;
840         ot->poll= WM_operator_winactive;
841         
842         RNA_def_string_file_path(ot->srna, "filename", "", 0, "Filename", "");
843         RNA_def_boolean(ot->srna, "compress", 0, "Compress", "Write compressed .blend file.");
844 }
845
846
847 /* *********************** */
848
849 static void WM_OT_window_fullscreen_toggle(wmOperatorType *ot)
850 {
851     ot->name= "Toggle Fullscreen";
852     ot->idname= "WM_OT_window_fullscreen_toggle";
853
854     ot->invoke= WM_operator_confirm;
855     ot->exec= wm_window_fullscreen_toggle_op;
856     ot->poll= WM_operator_winactive;
857 }
858
859 static int wm_exit_blender_op(bContext *C, wmOperator *op)
860 {
861         WM_operator_free(op);
862         
863         WM_exit(C);     
864         
865         return OPERATOR_FINISHED;
866 }
867
868 static void WM_OT_exit_blender(wmOperatorType *ot)
869 {
870         ot->name= "Exit Blender";
871         ot->idname= "WM_OT_exit_blender";
872
873         ot->invoke= WM_operator_confirm;
874         ot->exec= wm_exit_blender_op;
875         ot->poll= WM_operator_winactive;
876 }
877
878 /* ************ default paint cursors, draw always around cursor *********** */
879 /*
880  - returns handler to free 
881  - poll(bContext): returns 1 if draw should happen
882  - draw(bContext): drawing callback for paint cursor
883 */
884
885 void *WM_paint_cursor_activate(wmWindowManager *wm, int (*poll)(bContext *C),
886                                void (*draw)(bContext *C, int, int, void *customdata), void *customdata)
887 {
888         wmPaintCursor *pc= MEM_callocN(sizeof(wmPaintCursor), "paint cursor");
889         
890         BLI_addtail(&wm->paintcursors, pc);
891         
892         pc->customdata = customdata;
893         pc->poll= poll;
894         pc->draw= draw;
895         
896         return pc;
897 }
898
899 void WM_paint_cursor_end(wmWindowManager *wm, void *handle)
900 {
901         wmPaintCursor *pc;
902         
903         for(pc= wm->paintcursors.first; pc; pc= pc->next) {
904                 if(pc == (wmPaintCursor *)handle) {
905                         BLI_remlink(&wm->paintcursors, pc);
906                         MEM_freeN(pc);
907                         return;
908                 }
909         }
910 }
911
912 /* ************ window gesture operator-callback definitions ************** */
913 /*
914  * These are default callbacks for use in operators requiring gesture input
915  */
916
917 /* **************** Border gesture *************** */
918
919 /* Border gesture has two types:
920    1) WM_GESTURE_CROSS_RECT: starts a cross, on mouse click it changes to border 
921    2) WM_GESTURE_RECT: starts immediate as a border, on mouse click or release it ends
922
923    It stores 4 values (xmin, xmax, ymin, ymax) and event it ended with (event_type)
924 */
925
926 static int border_apply(bContext *C, wmOperator *op, int event_type, int event_orig)
927 {
928         wmGesture *gesture= op->customdata;
929         rcti *rect= gesture->customdata;
930         
931         if(rect->xmin > rect->xmax)
932                 SWAP(int, rect->xmin, rect->xmax);
933         if(rect->ymin > rect->ymax)
934                 SWAP(int, rect->ymin, rect->ymax);
935         
936         if(rect->xmin==rect->xmax || rect->ymin==rect->ymax)
937                 return 0;
938                 
939         /* operator arguments and storage. */
940         RNA_int_set(op->ptr, "xmin", rect->xmin);
941         RNA_int_set(op->ptr, "ymin", rect->ymin);
942         RNA_int_set(op->ptr, "xmax", rect->xmax);
943         RNA_int_set(op->ptr, "ymax", rect->ymax);
944         
945         /* XXX weak; border should be configured for this without reading event types */
946         if( RNA_struct_find_property(op->ptr, "event_type") ) {
947                 if(ELEM4(event_orig, EVT_TWEAK_L, EVT_TWEAK_R, EVT_TWEAK_A, EVT_TWEAK_S))
948                         event_type= LEFTMOUSE;
949                 
950                 RNA_int_set(op->ptr, "event_type", event_type);
951         }
952         op->type->exec(C, op);
953         
954         return 1;
955 }
956
957 static void wm_gesture_end(bContext *C, wmOperator *op)
958 {
959         wmGesture *gesture= op->customdata;
960         
961         WM_gesture_end(C, gesture);     /* frees gesture itself, and unregisters from window */
962         op->customdata= NULL;
963
964         ED_area_tag_redraw(CTX_wm_area(C));
965         
966         if( RNA_struct_find_property(op->ptr, "cursor") )
967                 WM_cursor_restore(CTX_wm_window(C));
968 }
969
970 int WM_border_select_invoke(bContext *C, wmOperator *op, wmEvent *event)
971 {
972         if(WM_key_event_is_tweak(event->type))
973                 op->customdata= WM_gesture_new(C, event, WM_GESTURE_RECT);
974         else
975                 op->customdata= WM_gesture_new(C, event, WM_GESTURE_CROSS_RECT);
976
977         /* add modal handler */
978         WM_event_add_modal_handler(C, &CTX_wm_window(C)->handlers, op);
979         
980         wm_gesture_tag_redraw(C);
981
982         return OPERATOR_RUNNING_MODAL;
983 }
984
985 int WM_border_select_modal(bContext *C, wmOperator *op, wmEvent *event)
986 {
987         wmGesture *gesture= op->customdata;
988         rcti *rect= gesture->customdata;
989         int sx, sy;
990         
991         switch(event->type) {
992                 case MOUSEMOVE:
993                         
994                         wm_subwindow_getorigin(CTX_wm_window(C), gesture->swinid, &sx, &sy);
995                         
996                         if(gesture->type==WM_GESTURE_CROSS_RECT && gesture->mode==0) {
997                                 rect->xmin= rect->xmax= event->x - sx;
998                                 rect->ymin= rect->ymax= event->y - sy;
999                         }
1000                         else {
1001                                 rect->xmax= event->x - sx;
1002                                 rect->ymax= event->y - sy;
1003                         }
1004                         
1005                         wm_gesture_tag_redraw(C);
1006
1007                         break;
1008                         
1009                 case LEFTMOUSE:
1010                 case MIDDLEMOUSE:
1011                 case RIGHTMOUSE:
1012                         if(event->val==KM_PRESS) {
1013                                 if(gesture->type==WM_GESTURE_CROSS_RECT && gesture->mode==0) {
1014                                         gesture->mode= 1;
1015                                         wm_gesture_tag_redraw(C);
1016                                 }
1017                         }
1018                         else {
1019                                 if(border_apply(C, op, event->type, gesture->event_type)) {
1020                                         wm_gesture_end(C, op);
1021                                         return OPERATOR_FINISHED;
1022                                 }
1023                                 wm_gesture_end(C, op);
1024                                 return OPERATOR_CANCELLED;
1025                         }
1026                         break;
1027                 case ESCKEY:
1028                         wm_gesture_end(C, op);
1029                         return OPERATOR_CANCELLED;
1030         }
1031         return OPERATOR_RUNNING_MODAL;
1032 }
1033
1034 /* **************** circle gesture *************** */
1035 /* works now only for selection or modal paint stuff, calls exec while hold mouse, exit on release */
1036
1037 int WM_gesture_circle_invoke(bContext *C, wmOperator *op, wmEvent *event)
1038 {
1039         op->customdata= WM_gesture_new(C, event, WM_GESTURE_CIRCLE);
1040         
1041         /* add modal handler */
1042         WM_event_add_modal_handler(C, &CTX_wm_window(C)->handlers, op);
1043         
1044         wm_gesture_tag_redraw(C);
1045         
1046         return OPERATOR_RUNNING_MODAL;
1047 }
1048
1049 static void gesture_circle_apply(bContext *C, wmOperator *op)
1050 {
1051         wmGesture *gesture= op->customdata;
1052         rcti *rect= gesture->customdata;
1053         
1054         /* operator arguments and storage. */
1055         RNA_int_set(op->ptr, "x", rect->xmin);
1056         RNA_int_set(op->ptr, "y", rect->ymin);
1057         RNA_int_set(op->ptr, "radius", rect->xmax);
1058         
1059         if(op->type->exec)
1060                 op->type->exec(C, op);
1061 }
1062
1063 int WM_gesture_circle_modal(bContext *C, wmOperator *op, wmEvent *event)
1064 {
1065         wmGesture *gesture= op->customdata;
1066         rcti *rect= gesture->customdata;
1067         int sx, sy;
1068         
1069         switch(event->type) {
1070                 case MOUSEMOVE:
1071                         
1072                         wm_subwindow_getorigin(CTX_wm_window(C), gesture->swinid, &sx, &sy);
1073                         
1074                         rect->xmin= event->x - sx;
1075                         rect->ymin= event->y - sy;
1076                         
1077                         wm_gesture_tag_redraw(C);
1078                         
1079                         if(gesture->mode)
1080                                 gesture_circle_apply(C, op);
1081
1082                         break;
1083                 case WHEELUPMOUSE:
1084                         rect->xmax += 2 + rect->xmax/10;
1085                         wm_gesture_tag_redraw(C);
1086                         break;
1087                 case WHEELDOWNMOUSE:
1088                         rect->xmax -= 2 + rect->xmax/10;
1089                         if(rect->xmax < 1) rect->xmax= 1;
1090                         wm_gesture_tag_redraw(C);
1091                         break;
1092                 case LEFTMOUSE:
1093                 case MIDDLEMOUSE:
1094                 case RIGHTMOUSE:
1095                         if(event->val==0) {     /* key release */
1096                                 wm_gesture_end(C, op);
1097                                 return OPERATOR_FINISHED;
1098                         }
1099                         else {
1100                                 if( RNA_struct_find_property(op->ptr, "event_type") )
1101                                         RNA_int_set(op->ptr, "event_type", event->type);
1102                                 
1103                                 /* apply first click */
1104                                 gesture_circle_apply(C, op);
1105                                 gesture->mode= 1;
1106                         }
1107                         break;
1108                 case ESCKEY:
1109                         wm_gesture_end(C, op);
1110                         return OPERATOR_CANCELLED;
1111         }
1112         return OPERATOR_RUNNING_MODAL;
1113 }
1114
1115 #if 0
1116 /* template to copy from */
1117 void WM_OT_circle_gesture(wmOperatorType *ot)
1118 {
1119         ot->name= "Circle Gesture";
1120         ot->idname= "WM_OT_circle_gesture";
1121         
1122         ot->invoke= WM_gesture_circle_invoke;
1123         ot->modal= WM_gesture_circle_modal;
1124         
1125         ot->poll= WM_operator_winactive;
1126         
1127         RNA_def_property(ot->srna, "x", PROP_INT, PROP_NONE);
1128         RNA_def_property(ot->srna, "y", PROP_INT, PROP_NONE);
1129         RNA_def_property(ot->srna, "radius", PROP_INT, PROP_NONE);
1130
1131 }
1132 #endif
1133
1134 /* **************** Tweak gesture *************** */
1135
1136 static void tweak_gesture_modal(bContext *C, wmEvent *event)
1137 {
1138         wmWindow *window= CTX_wm_window(C);
1139         wmGesture *gesture= window->tweak;
1140         rcti *rect= gesture->customdata;
1141         int sx, sy, val;
1142         
1143         switch(event->type) {
1144                 case MOUSEMOVE:
1145                         
1146                         wm_subwindow_getorigin(window, gesture->swinid, &sx, &sy);
1147                         
1148                         rect->xmax= event->x - sx;
1149                         rect->ymax= event->y - sy;
1150                         
1151                         if((val= wm_gesture_evaluate(C, gesture))) {
1152                                 wmEvent event;
1153
1154                                 event= *(window->eventstate);
1155                                 if(gesture->event_type==LEFTMOUSE)
1156                                         event.type= EVT_TWEAK_L;
1157                                 else if(gesture->event_type==RIGHTMOUSE)
1158                                         event.type= EVT_TWEAK_R;
1159                                 else
1160                                         event.type= EVT_TWEAK_M;
1161                                 event.val= val;
1162                                 /* mouse coords! */
1163                                 wm_event_add(window, &event);
1164                                 
1165                                 WM_gesture_end(C, gesture);     /* frees gesture itself, and unregisters from window */
1166                                 window->tweak= NULL;
1167                         }
1168                         
1169                         break;
1170                         
1171                 case LEFTMOUSE:
1172                 case RIGHTMOUSE:
1173                 case MIDDLEMOUSE:
1174                         if(gesture->event_type==event->type) {
1175                                 WM_gesture_end(C, gesture);
1176                                 window->tweak= NULL;
1177                                 
1178                                 /* when tweak fails we should give the other keymap entries a chance */
1179                                 event->val= KM_RELEASE;
1180                         }
1181                         break;
1182                 default:
1183                         WM_gesture_end(C, gesture);
1184                         window->tweak= NULL;
1185         }
1186 }
1187
1188 /* standard tweak, called after window handlers passed on event */
1189 void wm_tweakevent_test(bContext *C, wmEvent *event, int action)
1190 {
1191         wmWindow *win= CTX_wm_window(C);
1192         
1193         if(win->tweak==NULL) {
1194                 if(CTX_wm_region(C)) {
1195                         if(event->val) { // pressed
1196                                 if( ELEM3(event->type, LEFTMOUSE, MIDDLEMOUSE, RIGHTMOUSE) )
1197                                         win->tweak= WM_gesture_new(C, event, WM_GESTURE_TWEAK);
1198                         }
1199                 }
1200         }
1201         else {
1202                 if(action==WM_HANDLER_BREAK) {
1203                         WM_gesture_end(C, win->tweak);
1204                         win->tweak= NULL;
1205                 }
1206                 else
1207                         tweak_gesture_modal(C, event);
1208         }
1209 }
1210
1211 /* *********************** lasso gesture ****************** */
1212
1213 int WM_gesture_lasso_invoke(bContext *C, wmOperator *op, wmEvent *event)
1214 {
1215         op->customdata= WM_gesture_new(C, event, WM_GESTURE_LASSO);
1216         
1217         /* add modal handler */
1218         WM_event_add_modal_handler(C, &CTX_wm_window(C)->handlers, op);
1219         
1220         wm_gesture_tag_redraw(C);
1221         
1222         if( RNA_struct_find_property(op->ptr, "cursor") )
1223                 WM_cursor_modal(CTX_wm_window(C), RNA_int_get(op->ptr, "cursor"));
1224         
1225         return OPERATOR_RUNNING_MODAL;
1226 }
1227
1228 int WM_gesture_lines_invoke(bContext *C, wmOperator *op, wmEvent *event)
1229 {
1230         op->customdata= WM_gesture_new(C, event, WM_GESTURE_LINES);
1231         
1232         /* add modal handler */
1233         WM_event_add_modal_handler(C, &CTX_wm_window(C)->handlers, op);
1234         
1235         wm_gesture_tag_redraw(C);
1236         
1237         if( RNA_struct_find_property(op->ptr, "cursor") )
1238                 WM_cursor_modal(CTX_wm_window(C), RNA_int_get(op->ptr, "cursor"));
1239         
1240         return OPERATOR_RUNNING_MODAL;
1241 }
1242
1243
1244 static void gesture_lasso_apply(bContext *C, wmOperator *op, int event_type)
1245 {
1246         wmGesture *gesture= op->customdata;
1247         PointerRNA itemptr;
1248         float loc[2];
1249         int i;
1250         short *lasso= gesture->customdata;
1251         
1252         /* operator storage as path. */
1253
1254         for(i=0; i<gesture->points; i++, lasso+=2) {
1255                 loc[0]= lasso[0];
1256                 loc[1]= lasso[1];
1257                 RNA_collection_add(op->ptr, "path", &itemptr);
1258                 RNA_float_set_array(&itemptr, "loc", loc);
1259         }
1260         
1261         wm_gesture_end(C, op);
1262                 
1263         if(op->type->exec)
1264                 op->type->exec(C, op);
1265         
1266 }
1267
1268 int WM_gesture_lasso_modal(bContext *C, wmOperator *op, wmEvent *event)
1269 {
1270         wmGesture *gesture= op->customdata;
1271         int sx, sy;
1272         
1273         switch(event->type) {
1274                 case MOUSEMOVE:
1275                         
1276                         wm_gesture_tag_redraw(C);
1277                         
1278                         wm_subwindow_getorigin(CTX_wm_window(C), gesture->swinid, &sx, &sy);
1279                         if(gesture->points < WM_LASSO_MAX_POINTS) {
1280                                 short *lasso= gesture->customdata;
1281                                 lasso += 2 * gesture->points;
1282                                 lasso[0] = event->x - sx;
1283                                 lasso[1] = event->y - sy;
1284                                 gesture->points++;
1285                         }
1286                         else {
1287                                 gesture_lasso_apply(C, op, event->type);
1288                                 return OPERATOR_FINISHED;
1289                         }
1290                         break;
1291                         
1292                 case LEFTMOUSE:
1293                 case MIDDLEMOUSE:
1294                 case RIGHTMOUSE:
1295                         if(event->val==0) {     /* key release */
1296                                 gesture_lasso_apply(C, op, event->type);
1297                                 return OPERATOR_FINISHED;
1298                         }
1299                         break;
1300                 case ESCKEY:
1301                         wm_gesture_end(C, op);
1302                         return OPERATOR_CANCELLED;
1303         }
1304         return OPERATOR_RUNNING_MODAL;
1305 }
1306
1307 int WM_gesture_lines_modal(bContext *C, wmOperator *op, wmEvent *event)
1308 {
1309         return WM_gesture_lasso_modal(C, op, event);
1310 }
1311
1312 #if 0
1313 /* template to copy from */
1314
1315 static int gesture_lasso_exec(bContext *C, wmOperator *op)
1316 {
1317         RNA_BEGIN(op->ptr, itemptr, "path") {
1318                 float loc[2];
1319                 
1320                 RNA_float_get_array(&itemptr, "loc", loc);
1321                 printf("Location: %f %f\n", loc[0], loc[1]);
1322         }
1323         RNA_END;
1324         
1325         return OPERATOR_FINISHED;
1326 }
1327
1328 void WM_OT_lasso_gesture(wmOperatorType *ot)
1329 {
1330         PropertyRNA *prop;
1331         
1332         ot->name= "Lasso Gesture";
1333         ot->idname= "WM_OT_lasso_gesture";
1334         
1335         ot->invoke= WM_gesture_lasso_invoke;
1336         ot->modal= WM_gesture_lasso_modal;
1337         ot->exec= gesture_lasso_exec;
1338         
1339         ot->poll= WM_operator_winactive;
1340         
1341         prop= RNA_def_property(ot->srna, "path", PROP_COLLECTION, PROP_NONE);
1342         RNA_def_property_struct_runtime(prop, &RNA_OperatorMousePath);
1343 }
1344 #endif
1345
1346 /* *********************** radial control ****************** */
1347
1348 const int WM_RADIAL_CONTROL_DISPLAY_SIZE = 200;
1349
1350 typedef struct wmRadialControl {
1351         int mode;
1352         float initial_value, value, max_value;
1353         int initial_mouse[2];
1354         void *cursor;
1355         GLuint tex;
1356 } wmRadialControl;
1357
1358 static void wm_radial_control_paint(bContext *C, int x, int y, void *customdata)
1359 {
1360         wmRadialControl *rc = (wmRadialControl*)customdata;
1361         ARegion *ar = CTX_wm_region(C);
1362         float r1=0.0f, r2=0.0f, r3=0.0f, angle=0.0f;
1363
1364         /* Keep cursor in the original place */
1365         x = rc->initial_mouse[0] - ar->winrct.xmin;
1366         y = rc->initial_mouse[1] - ar->winrct.ymin;
1367
1368         glPushMatrix();
1369         
1370         glTranslatef((float)x, (float)y, 0.0f);
1371
1372         if(rc->mode == WM_RADIALCONTROL_SIZE) {
1373                 r1= rc->value;
1374                 r2= rc->initial_value;
1375                 r3= r1;
1376         } else if(rc->mode == WM_RADIALCONTROL_STRENGTH) {
1377                 r1= (1 - rc->value) * WM_RADIAL_CONTROL_DISPLAY_SIZE;
1378                 r2= WM_RADIAL_CONTROL_DISPLAY_SIZE;
1379                 r3= WM_RADIAL_CONTROL_DISPLAY_SIZE;
1380         } else if(rc->mode == WM_RADIALCONTROL_ANGLE) {
1381                 r1= r2= WM_RADIAL_CONTROL_DISPLAY_SIZE;
1382                 r3= WM_RADIAL_CONTROL_DISPLAY_SIZE;
1383                 angle = rc->value;
1384         }
1385
1386         glColor4ub(255, 255, 255, 128);
1387         glEnable( GL_LINE_SMOOTH );
1388         glEnable(GL_BLEND);
1389
1390         if(rc->mode == WM_RADIALCONTROL_ANGLE)
1391                 fdrawline(0, 0, WM_RADIAL_CONTROL_DISPLAY_SIZE, 0);
1392
1393         if(rc->tex) {
1394                 const float str = rc->mode == WM_RADIALCONTROL_STRENGTH ? (rc->value + 0.5) : 1;
1395
1396                 if(rc->mode == WM_RADIALCONTROL_ANGLE) {
1397                         glRotatef(angle, 0, 0, 1);
1398                         fdrawline(0, 0, WM_RADIAL_CONTROL_DISPLAY_SIZE, 0);
1399                 }
1400
1401                 glBindTexture(GL_TEXTURE_2D, rc->tex);
1402
1403                 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1404                 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1405
1406                 glEnable(GL_TEXTURE_2D);
1407                 glBegin(GL_QUADS);
1408                 glColor4f(0,0,0, str);
1409                 glTexCoord2f(0,0);
1410                 glVertex2f(-r3, -r3);
1411                 glTexCoord2f(1,0);
1412                 glVertex2f(r3, -r3);
1413                 glTexCoord2f(1,1);
1414                 glVertex2f(r3, r3);
1415                 glTexCoord2f(0,1);
1416                 glVertex2f(-r3, r3);
1417                 glEnd();
1418                 glDisable(GL_TEXTURE_2D);
1419         }
1420
1421         glColor4ub(255, 255, 255, 128); 
1422         glutil_draw_lined_arc(0.0, M_PI*2.0, r1, 40);
1423         glutil_draw_lined_arc(0.0, M_PI*2.0, r2, 40);
1424         glDisable(GL_BLEND);
1425         glDisable( GL_LINE_SMOOTH );
1426         
1427         glPopMatrix();
1428 }
1429
1430 int WM_radial_control_modal(bContext *C, wmOperator *op, wmEvent *event)
1431 {
1432         wmRadialControl *rc = (wmRadialControl*)op->customdata;
1433         int mode, initial_mouse[2], delta[2];
1434         float dist;
1435         double new_value = RNA_float_get(op->ptr, "new_value");
1436         int ret = OPERATOR_RUNNING_MODAL;
1437
1438         mode = RNA_int_get(op->ptr, "mode");
1439         RNA_int_get_array(op->ptr, "initial_mouse", initial_mouse);
1440
1441         switch(event->type) {
1442         case MOUSEMOVE:
1443                 delta[0]= initial_mouse[0] - event->x;
1444                 delta[1]= initial_mouse[1] - event->y;
1445                 dist= sqrt(delta[0]*delta[0]+delta[1]*delta[1]);
1446
1447                 if(mode == WM_RADIALCONTROL_SIZE)
1448                         new_value = dist;
1449                 else if(mode == WM_RADIALCONTROL_STRENGTH) {
1450                         new_value = 1 - dist / WM_RADIAL_CONTROL_DISPLAY_SIZE;
1451                 } else if(mode == WM_RADIALCONTROL_ANGLE)
1452                         new_value = ((int)(atan2(delta[1], delta[0]) * (180.0 / M_PI)) + 180);
1453                 
1454                 if(event->ctrl) {
1455                         if(mode == WM_RADIALCONTROL_STRENGTH)
1456                                 new_value = ((int)(new_value * 100) / 10*10) / 100.0f;
1457                         else
1458                                 new_value = ((int)new_value + 5) / 10*10;
1459                 }
1460                 
1461                 break;
1462         case ESCKEY:
1463         case RIGHTMOUSE:
1464                 ret = OPERATOR_CANCELLED;
1465                 break;
1466         case LEFTMOUSE:
1467         case PADENTER:
1468                 op->type->exec(C, op);
1469                 ret = OPERATOR_FINISHED;
1470                 break;
1471         }
1472
1473         /* Clamp */
1474         if(new_value > rc->max_value)
1475                 new_value = rc->max_value;
1476         else if(new_value < 0)
1477                 new_value = 0;
1478
1479         /* Update paint data */
1480         rc->value = new_value;
1481
1482         RNA_float_set(op->ptr, "new_value", new_value);
1483
1484         if(ret != OPERATOR_RUNNING_MODAL) {
1485                 WM_paint_cursor_end(CTX_wm_manager(C), rc->cursor);
1486                 MEM_freeN(rc);
1487         }
1488         
1489         ED_region_tag_redraw(CTX_wm_region(C));
1490
1491         return ret;
1492 }
1493
1494 /* Expects the operator customdata to be an ImBuf (or NULL) */
1495 int WM_radial_control_invoke(bContext *C, wmOperator *op, wmEvent *event)
1496 {
1497         wmRadialControl *rc = MEM_callocN(sizeof(wmRadialControl), "radial control");
1498         int mode = RNA_int_get(op->ptr, "mode");
1499         float initial_value = RNA_float_get(op->ptr, "initial_value");
1500         int mouse[2] = {event->x, event->y};
1501
1502         if(mode == WM_RADIALCONTROL_SIZE) {
1503                 rc->max_value = 200;
1504                 mouse[0]-= initial_value;
1505         }
1506         else if(mode == WM_RADIALCONTROL_STRENGTH) {
1507                 rc->max_value = 1;
1508                 mouse[0]-= WM_RADIAL_CONTROL_DISPLAY_SIZE * (1 - initial_value);
1509         }
1510         else if(mode == WM_RADIALCONTROL_ANGLE) {
1511                 rc->max_value = 360;
1512                 mouse[0]-= WM_RADIAL_CONTROL_DISPLAY_SIZE * cos(initial_value);
1513                 mouse[1]-= WM_RADIAL_CONTROL_DISPLAY_SIZE * sin(initial_value);
1514                 initial_value *= 180.0f/M_PI;
1515         }
1516
1517         if(op->customdata) {
1518                 ImBuf *im = (ImBuf*)op->customdata;
1519                 /* Build GL texture */
1520                 glGenTextures(1, &rc->tex);
1521                 glBindTexture(GL_TEXTURE_2D, rc->tex);
1522                 glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, im->x, im->y, 0, GL_ALPHA, GL_FLOAT, im->rect_float);
1523                 MEM_freeN(im->rect_float);
1524                 MEM_freeN(im);
1525         }
1526
1527         RNA_int_set_array(op->ptr, "initial_mouse", mouse);
1528         RNA_float_set(op->ptr, "new_value", initial_value);
1529                 
1530         op->customdata = rc;
1531         rc->mode = mode;
1532         rc->initial_value = initial_value;
1533         rc->initial_mouse[0] = mouse[0];
1534         rc->initial_mouse[1] = mouse[1];
1535         rc->cursor = WM_paint_cursor_activate(CTX_wm_manager(C), op->type->poll,
1536                                               wm_radial_control_paint, op->customdata);
1537
1538         /* add modal handler */
1539         WM_event_add_modal_handler(C, &CTX_wm_window(C)->handlers, op);
1540         
1541         WM_radial_control_modal(C, op, event);
1542         
1543         return OPERATOR_RUNNING_MODAL;
1544 }
1545
1546 /* Gets a descriptive string of the operation */
1547 void WM_radial_control_string(wmOperator *op, char str[], int maxlen)
1548 {
1549         int mode = RNA_int_get(op->ptr, "mode");
1550         float v = RNA_float_get(op->ptr, "new_value");
1551
1552         if(mode == WM_RADIALCONTROL_SIZE)
1553                 sprintf(str, "Size: %d", (int)v);
1554         else if(mode == WM_RADIALCONTROL_STRENGTH)
1555                 sprintf(str, "Strength: %d", (int)v);
1556         else if(mode == WM_RADIALCONTROL_ANGLE)
1557                 sprintf(str, "Angle: %d", (int)(v * 180.0f/M_PI));
1558 }
1559
1560 /** Important: this doesn't define an actual operator, it
1561     just sets up the common parts of the radial control op. **/
1562 void WM_OT_radial_control_partial(wmOperatorType *ot)
1563 {
1564         static EnumPropertyItem prop_mode_items[] = {
1565                 {WM_RADIALCONTROL_SIZE, "SIZE", 0, "Size", ""},
1566                 {WM_RADIALCONTROL_STRENGTH, "STRENGTH", 0, "Strength", ""},
1567                 {WM_RADIALCONTROL_ANGLE, "ANGLE", 0, "Angle", ""},
1568                 {0, NULL, 0, NULL, NULL}};
1569
1570         /* Should be set in custom invoke() */
1571         RNA_def_float(ot->srna, "initial_value", 0, 0, FLT_MAX, "Initial Value", "", 0, FLT_MAX);
1572
1573         /* Set internally, should be used in custom exec() to get final value */
1574         RNA_def_float(ot->srna, "new_value", 0, 0, FLT_MAX, "New Value", "", 0, FLT_MAX);
1575
1576         /* Should be set before calling operator */
1577         RNA_def_enum(ot->srna, "mode", prop_mode_items, 0, "Mode", "");
1578
1579         /* Internal */
1580         RNA_def_int_vector(ot->srna, "initial_mouse", 2, NULL, INT_MIN, INT_MAX, "initial_mouse", "", INT_MIN, INT_MAX);
1581 }
1582
1583 /* ************************** timer for testing ***************** */
1584
1585 /* uses no type defines, fully local testing function anyway... ;) */
1586
1587 static int ten_timer_exec(bContext *C, wmOperator *op)
1588 {
1589         ARegion *ar= CTX_wm_region(C);
1590         double stime= PIL_check_seconds_timer();
1591         int type = RNA_int_get(op->ptr, "type");
1592         int a, time;
1593         char tmpstr[128];
1594         
1595         WM_cursor_wait(1);
1596         
1597         for(a=0; a<10; a++) {
1598                 if (type==0) {
1599                         ED_region_do_draw(C, ar);
1600                 } 
1601                 else if (type==1) {
1602                         wmWindow *win= CTX_wm_window(C);
1603                         
1604                         ED_region_tag_redraw(ar);
1605                         wm_draw_update(C);
1606                         
1607                         CTX_wm_window_set(C, win);      /* XXX context manipulation warning! */
1608                 }
1609                 else if (type==2) {
1610                         wmWindow *win= CTX_wm_window(C);
1611                         ScrArea *sa;
1612                         
1613                         for(sa= CTX_wm_screen(C)->areabase.first; sa; sa= sa->next)
1614                                 ED_area_tag_redraw(sa);
1615                         wm_draw_update(C);
1616                         
1617                         CTX_wm_window_set(C, win);      /* XXX context manipulation warning! */
1618                 }
1619                 else if (type==3) {
1620                         Scene *scene= CTX_data_scene(C);
1621                         
1622                         if(a & 1) scene->r.cfra--;
1623                         else scene->r.cfra++;
1624                         scene_update_for_newframe(scene, scene->lay);
1625                 }
1626                 else {
1627                         ED_undo_pop(C);
1628                         ED_undo_redo(C);
1629                 }
1630         }
1631         
1632         time= (int) ((PIL_check_seconds_timer()-stime)*1000);
1633         
1634         if(type==0) sprintf(tmpstr, "10 x Draw Region: %d ms", time);
1635         if(type==1) sprintf(tmpstr, "10 x Draw Region and Swap: %d ms", time);
1636         if(type==2) sprintf(tmpstr, "10 x Draw Window and Swap: %d ms", time);
1637         if(type==3) sprintf(tmpstr, "Anim Step: %d ms", time);
1638         if(type==4) sprintf(tmpstr, "10 x Undo/Redo: %d ms", time);
1639         
1640         WM_cursor_wait(0);
1641         
1642         uiPupMenuNotice(C, tmpstr);
1643         
1644         return OPERATOR_FINISHED;
1645 }
1646
1647 static void WM_OT_ten_timer(wmOperatorType *ot)
1648 {
1649         static EnumPropertyItem prop_type_items[] = {
1650         {0, "DRAW", 0, "Draw Region", ""},
1651         {1, "DRAWSWAP", 0, "Draw Region + Swap", ""},
1652         {2, "DRAWWINSWAP", 0, "Draw Window + Swap", ""},
1653         {3, "ANIMSTEP", 0, "Anim Step", ""},
1654         {4, "UNDO", 0, "Undo/Redo", ""},
1655         {0, NULL, 0, NULL, NULL}};
1656         
1657         ot->name= "Ten Timer";
1658         ot->idname= "WM_OT_ten_timer";
1659         
1660         ot->invoke= WM_menu_invoke;
1661         ot->exec= ten_timer_exec;
1662         ot->poll= WM_operator_winactive;
1663         
1664         RNA_def_enum(ot->srna, "type", prop_type_items, 0, "Type", "");
1665
1666 }
1667
1668
1669
1670 /* ******************************************************* */
1671  
1672 /* called on initialize WM_exit() */
1673 void wm_operatortype_free(void)
1674 {
1675         BLI_freelistN(&global_ops);
1676 }
1677
1678 /* called on initialize WM_init() */
1679 void wm_operatortype_init(void)
1680 {
1681         WM_operatortype_append(WM_OT_window_duplicate);
1682         WM_operatortype_append(WM_OT_read_homefile);
1683         WM_operatortype_append(WM_OT_save_homefile);
1684         WM_operatortype_append(WM_OT_window_fullscreen_toggle);
1685         WM_operatortype_append(WM_OT_exit_blender);
1686         WM_operatortype_append(WM_OT_open_recentfile);
1687         WM_operatortype_append(WM_OT_open_mainfile);
1688         WM_operatortype_append(WM_OT_recover_last_session);
1689         WM_operatortype_append(WM_OT_jobs_timer);
1690         WM_operatortype_append(WM_OT_save_as_mainfile);
1691         WM_operatortype_append(WM_OT_save_mainfile);
1692         WM_operatortype_append(WM_OT_ten_timer);
1693         WM_operatortype_append(WM_OT_debug_menu);
1694         WM_operatortype_append(WM_OT_search_menu);
1695 }
1696
1697 /* default keymap for windows and screens, only call once per WM */
1698 void wm_window_keymap(wmWindowManager *wm)
1699 {
1700         ListBase *keymap= WM_keymap_listbase(wm, "Window", 0, 0);
1701         
1702         /* items to make WM work */
1703         WM_keymap_verify_item(keymap, "WM_OT_jobs_timer", TIMERJOBS, KM_ANY, KM_ANY, 0);
1704         
1705         /* note, this doesn't replace existing keymap items */
1706         WM_keymap_verify_item(keymap, "WM_OT_window_duplicate", WKEY, KM_PRESS, KM_CTRL|KM_ALT, 0);
1707         WM_keymap_verify_item(keymap, "WM_OT_read_homefile", XKEY, KM_PRESS, KM_CTRL, 0);
1708         WM_keymap_verify_item(keymap, "WM_OT_save_homefile", UKEY, KM_PRESS, KM_CTRL, 0);
1709         WM_keymap_verify_item(keymap, "WM_OT_open_recentfile", OKEY, KM_PRESS, KM_CTRL, 0);
1710         WM_keymap_verify_item(keymap, "WM_OT_open_mainfile", F1KEY, KM_PRESS, 0, 0);
1711         WM_keymap_verify_item(keymap, "WM_OT_save_mainfile", WKEY, KM_PRESS, KM_CTRL, 0);
1712         WM_keymap_verify_item(keymap, "WM_OT_save_as_mainfile", F2KEY, KM_PRESS, 0, 0);
1713         WM_keymap_verify_item(keymap, "WM_OT_window_fullscreen_toggle", F11KEY, KM_PRESS, KM_SHIFT, 0);
1714         WM_keymap_verify_item(keymap, "WM_OT_exit_blender", QKEY, KM_PRESS, KM_CTRL, 0);
1715
1716         /* debug/testing */
1717         WM_keymap_verify_item(keymap, "WM_OT_ten_timer", TKEY, KM_PRESS, KM_ALT|KM_CTRL, 0);
1718         WM_keymap_verify_item(keymap, "WM_OT_debug_menu", DKEY, KM_PRESS, KM_ALT|KM_CTRL, 0);
1719         WM_keymap_verify_item(keymap, "WM_OT_search_menu", SPACEKEY, KM_PRESS, KM_CTRL, 0);
1720         
1721 }
1722