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