Merge from trunk -r 23968:24181.
[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_object_types.h"
38 #include "DNA_screen_types.h"
39 #include "DNA_scene_types.h"
40 #include "DNA_userdef_types.h"
41 #include "DNA_windowmanager_types.h"
42
43 #include "MEM_guardedalloc.h"
44
45 #include "PIL_time.h"
46
47 #include "BLI_blenlib.h"
48 #include "BLI_dynstr.h" /*for WM_operator_pystring */
49
50 #include "BLO_readfile.h"
51
52 #include "BKE_blender.h"
53 #include "BKE_context.h"
54 #include "BKE_depsgraph.h"
55 #include "BKE_idprop.h"
56 #include "BKE_library.h"
57 #include "BKE_global.h"
58 #include "BKE_main.h"
59 #include "BKE_report.h"
60 #include "BKE_scene.h"
61 #include "BKE_screen.h" /* BKE_ST_MAXNAME */
62 #include "BKE_utildefines.h"
63
64 #include "BIF_gl.h"
65 #include "BIF_glutil.h" /* for paint cursor */
66
67 #include "IMB_imbuf_types.h"
68
69 #include "ED_screen.h"
70 #include "ED_util.h"
71
72 #include "RNA_access.h"
73 #include "RNA_define.h"
74
75 #include "UI_interface.h"
76 #include "UI_resources.h"
77
78 #include "WM_api.h"
79 #include "WM_types.h"
80
81 #include "wm.h"
82 #include "wm_draw.h"
83 #include "wm_event_system.h"
84 #include "wm_subwindow.h"
85 #include "wm_window.h"
86
87
88
89 static ListBase global_ops= {NULL, NULL};
90
91 /* ************ operator API, exported ********** */
92
93
94 wmOperatorType *WM_operatortype_find(const char *idname, int quiet)
95 {
96         wmOperatorType *ot;
97         
98         char idname_bl[OP_MAX_TYPENAME]; // XXX, needed to support python style names without the _OT_ syntax
99         WM_operator_bl_idname(idname_bl, idname);
100         
101         if (idname_bl[0]) {
102                 for(ot= global_ops.first; ot; ot= ot->next) {
103                         if(strncmp(ot->idname, idname_bl, OP_MAX_TYPENAME)==0)
104                            return ot;
105                 }
106         }
107         
108         if(!quiet)
109                 printf("search for unknown operator %s, %s\n", idname_bl, idname);
110         
111         return NULL;
112 }
113
114 wmOperatorType *WM_operatortype_exists(const char *idname)
115 {
116         wmOperatorType *ot;
117         
118         char idname_bl[OP_MAX_TYPENAME]; // XXX, needed to support python style names without the _OT_ syntax
119         WM_operator_bl_idname(idname_bl, idname);
120         
121         if(idname_bl[0]) {
122                 for(ot= global_ops.first; ot; ot= ot->next) {
123                         if(strncmp(ot->idname, idname_bl, OP_MAX_TYPENAME)==0)
124                            return ot;
125                 }
126         }
127         return NULL;
128 }
129
130 wmOperatorType *WM_operatortype_first(void)
131 {
132         return global_ops.first;
133 }
134
135 /* all ops in 1 list (for time being... needs evaluation later) */
136 void WM_operatortype_append(void (*opfunc)(wmOperatorType*))
137 {
138         wmOperatorType *ot;
139         
140         ot= MEM_callocN(sizeof(wmOperatorType), "operatortype");
141         ot->srna= RNA_def_struct(&BLENDER_RNA, "", "OperatorProperties");
142         opfunc(ot);
143
144         if(ot->name==NULL) {
145                 static char dummy_name[] = "Dummy Name";
146                 fprintf(stderr, "ERROR: Operator %s has no name property!\n", ot->idname);
147                 ot->name= dummy_name;
148         }
149
150         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.
151         RNA_def_struct_identifier(ot->srna, ot->idname);
152         BLI_addtail(&global_ops, ot);
153 }
154
155 void WM_operatortype_append_ptr(void (*opfunc)(wmOperatorType*, void*), void *userdata)
156 {
157         wmOperatorType *ot;
158
159         ot= MEM_callocN(sizeof(wmOperatorType), "operatortype");
160         ot->srna= RNA_def_struct(&BLENDER_RNA, "", "OperatorProperties");
161         opfunc(ot, userdata);
162         RNA_def_struct_ui_text(ot->srna, ot->name, ot->description ? ot->description:"(undocumented operator)");
163         RNA_def_struct_identifier(ot->srna, ot->idname);
164         BLI_addtail(&global_ops, ot);
165 }
166
167 /* ********************* macro operator ******************** */
168
169 /* macro exec only runs exec calls */
170 static int wm_macro_exec(bContext *C, wmOperator *op)
171 {
172         wmOperator *opm;
173         int retval= OPERATOR_FINISHED;
174         
175 //      printf("macro exec %s\n", op->type->idname);
176         
177         for(opm= op->macro.first; opm; opm= opm->next) {
178                 
179                 if(opm->type->exec) {
180 //                      printf("macro exec %s\n", opm->type->idname);
181                         retval= opm->type->exec(C, opm);
182                 
183                         if(!(retval & OPERATOR_FINISHED))
184                                 break;
185                 }
186         }
187 //      if(opm)
188 //              printf("macro ended not finished\n");
189 //      else
190 //              printf("macro end\n");
191         
192         return retval;
193 }
194
195 static int wm_macro_invoke(bContext *C, wmOperator *op, wmEvent *event)
196 {
197         wmOperator *opm;
198         int retval= OPERATOR_FINISHED;
199         
200 //      printf("macro invoke %s\n", op->type->idname);
201         
202         for(opm= op->macro.first; opm; opm= opm->next) {
203                 
204                 if(opm->type->invoke)
205                         retval= opm->type->invoke(C, opm, event);
206                 else if(opm->type->exec)
207                         retval= opm->type->exec(C, opm);
208                 
209                 if(!(retval & OPERATOR_FINISHED))
210                         break;
211         }
212         
213 //      if(opm)
214 //              printf("macro ended not finished\n");
215 //      else
216 //              printf("macro end\n");
217         
218         
219         return retval;
220 }
221
222 static int wm_macro_modal(bContext *C, wmOperator *op, wmEvent *event)
223 {
224 //      printf("macro modal %s\n", op->type->idname);
225         
226         if(op->opm==NULL)
227                 printf("macro error, calling NULL modal()\n");
228         else {
229 //              printf("macro modal %s\n", op->opm->type->idname);
230                 return op->opm->type->modal(C, op->opm, event);
231         }       
232         
233         return OPERATOR_FINISHED;
234 }
235
236 /* Names have to be static for now */
237 wmOperatorType *WM_operatortype_append_macro(char *idname, char *name, int flag)
238 {
239         wmOperatorType *ot;
240         
241         if(WM_operatortype_exists(idname)) {
242                 printf("Macro error: operator %s exists\n", idname);
243                 return NULL;
244         }
245         
246         ot= MEM_callocN(sizeof(wmOperatorType), "operatortype");
247         ot->srna= RNA_def_struct(&BLENDER_RNA, "", "OperatorProperties");
248         
249         ot->idname= idname;
250         ot->name= name;
251         ot->flag= OPTYPE_MACRO|flag;
252         
253         ot->exec= wm_macro_exec;
254         ot->invoke= wm_macro_invoke;
255         ot->modal= wm_macro_modal;
256         ot->poll= NULL;
257         
258         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.
259         RNA_def_struct_identifier(ot->srna, ot->idname);
260
261         BLI_addtail(&global_ops, ot);
262
263         return ot;
264 }
265
266 wmOperatorTypeMacro *WM_operatortype_macro_define(wmOperatorType *ot, const char *idname)
267 {
268         wmOperatorTypeMacro *otmacro= MEM_callocN(sizeof(wmOperatorTypeMacro), "wmOperatorTypeMacro");
269         
270         BLI_strncpy(otmacro->idname, idname, OP_MAX_TYPENAME);
271
272         /* do this on first use, since operatordefinitions might have been not done yet */
273         WM_operator_properties_alloc(&(otmacro->ptr), &(otmacro->properties), idname);
274         
275         BLI_addtail(&ot->macro, otmacro);
276         
277         return otmacro;
278 }
279
280 static void wm_operatortype_free_macro(wmOperatorType *ot)
281 {
282         wmOperatorTypeMacro *otmacro;
283         
284         for(otmacro= ot->macro.first; otmacro; otmacro= otmacro->next) {
285                 if(otmacro->ptr) {
286                         WM_operator_properties_free(otmacro->ptr);
287                         MEM_freeN(otmacro->ptr);
288                 }
289         }
290         BLI_freelistN(&ot->macro);
291 }
292
293
294 int WM_operatortype_remove(const char *idname)
295 {
296         wmOperatorType *ot = WM_operatortype_find(idname, 0);
297
298         if (ot==NULL)
299                 return 0;
300         
301         BLI_remlink(&global_ops, ot);
302         RNA_struct_free(&BLENDER_RNA, ot->srna);
303         
304         if(ot->macro.first)
305                 wm_operatortype_free_macro(ot);
306         
307         MEM_freeN(ot);
308
309         return 1;
310 }
311
312 /* SOME_OT_op -> some.op */
313 void WM_operator_py_idname(char *to, const char *from)
314 {
315         char *sep= strstr(from, "_OT_");
316         if(sep) {
317                 int i, ofs= (sep-from);
318
319                 for(i=0; i<ofs; i++)
320                         to[i]= tolower(from[i]);
321
322                 to[ofs] = '.';
323                 BLI_strncpy(to+(ofs+1), sep+4, OP_MAX_TYPENAME);
324         }
325         else {
326                 /* should not happen but support just incase */
327                 BLI_strncpy(to, from, OP_MAX_TYPENAME);
328         }
329 }
330
331 /* some.op -> SOME_OT_op */
332 void WM_operator_bl_idname(char *to, const char *from)
333 {
334         if (from) {
335                 char *sep= strchr(from, '.');
336
337                 if(sep) {
338                         int i, ofs= (sep-from);
339
340                         for(i=0; i<ofs; i++)
341                                 to[i]= toupper(from[i]);
342
343                         BLI_strncpy(to+ofs, "_OT_", OP_MAX_TYPENAME);
344                         BLI_strncpy(to+(ofs+4), sep+1, OP_MAX_TYPENAME);
345                 }
346                 else {
347                         /* should not happen but support just incase */
348                         BLI_strncpy(to, from, OP_MAX_TYPENAME);
349                 }
350         }
351         else
352                 to[0]= 0;
353 }
354
355 /* print a string representation of the operator, with the args that it runs 
356  * so python can run it again,
357  *
358  * When calling from an existing wmOperator do.
359  * WM_operator_pystring(op->type, op->ptr);
360  */
361 char *WM_operator_pystring(bContext *C, wmOperatorType *ot, PointerRNA *opptr, int all_args)
362 {
363         const char *arg_name= NULL;
364         char idname_py[OP_MAX_TYPENAME];
365
366         PropertyRNA *prop, *iterprop;
367
368         /* for building the string */
369         DynStr *dynstr= BLI_dynstr_new();
370         char *cstring, *buf;
371         int first_iter=1, ok= 1;
372
373
374         /* only to get the orginal props for comparisons */
375         PointerRNA opptr_default;
376         PropertyRNA *prop_default;
377         char *buf_default;
378         if(!all_args) {
379                 WM_operator_properties_create(&opptr_default, ot->idname);
380         }
381
382
383         WM_operator_py_idname(idname_py, ot->idname);
384         BLI_dynstr_appendf(dynstr, "bpy.ops.%s(", idname_py);
385
386         iterprop= RNA_struct_iterator_property(opptr->type);
387
388         RNA_PROP_BEGIN(opptr, propptr, iterprop) {
389                 prop= propptr.data;
390                 arg_name= RNA_property_identifier(prop);
391
392                 if (strcmp(arg_name, "rna_type")==0) continue;
393
394                 buf= RNA_property_as_string(C, opptr, prop);
395                 
396                 ok= 1;
397
398                 if(!all_args) {
399                         /* not verbose, so only add in attributes that use non-default values
400                          * slow but good for tooltips */
401                         prop_default= RNA_struct_find_property(&opptr_default, arg_name);
402
403                         if(prop_default) {
404                                 buf_default= RNA_property_as_string(C, &opptr_default, prop_default);
405
406                                 if(strcmp(buf, buf_default)==0)
407                                         ok= 0; /* values match, dont bother printing */
408
409                                 MEM_freeN(buf_default);
410                         }
411
412                 }
413                 if(ok) {
414                         BLI_dynstr_appendf(dynstr, first_iter?"%s=%s":", %s=%s", arg_name, buf);
415                         first_iter = 0;
416                 }
417
418                 MEM_freeN(buf);
419
420         }
421         RNA_PROP_END;
422
423         if(all_args==0)
424                 WM_operator_properties_free(&opptr_default);
425
426         BLI_dynstr_append(dynstr, ")");
427
428         cstring = BLI_dynstr_get_cstring(dynstr);
429         BLI_dynstr_free(dynstr);
430         return cstring;
431 }
432
433 void WM_operator_properties_create(PointerRNA *ptr, const char *opstring)
434 {
435         wmOperatorType *ot= WM_operatortype_find(opstring, 0);
436
437         if(ot)
438                 RNA_pointer_create(NULL, ot->srna, NULL, ptr);
439         else
440                 RNA_pointer_create(NULL, &RNA_OperatorProperties, NULL, ptr);
441 }
442
443 /* similar to the function above except its uses ID properties
444  * used for keymaps and macros */
445 void WM_operator_properties_alloc(PointerRNA **ptr, IDProperty **properties, const char *opstring)
446 {
447         if(*properties==NULL) {
448                 IDPropertyTemplate val = {0};
449                 *properties= IDP_New(IDP_GROUP, val, "wmOpItemProp");
450         }
451
452         if(*ptr==NULL) {
453                 *ptr= MEM_callocN(sizeof(PointerRNA), "wmOpItemPtr");
454                 WM_operator_properties_create(*ptr, opstring);
455         }
456
457         (*ptr)->data= *properties;
458
459 }
460
461
462 void WM_operator_properties_free(PointerRNA *ptr)
463 {
464         IDProperty *properties= ptr->data;
465
466         if(properties) {
467                 IDP_FreeProperty(properties);
468                 MEM_freeN(properties);
469         }
470 }
471
472 /* ************ default op callbacks, exported *********** */
473
474 /* invoke callback, uses enum property named "type" */
475 int WM_menu_invoke(bContext *C, wmOperator *op, wmEvent *event)
476 {
477         PropertyRNA *prop;
478         uiPopupMenu *pup;
479         uiLayout *layout;
480
481         prop= RNA_struct_find_property(op->ptr, "type");
482
483         if(!prop) {
484                 RNA_STRUCT_BEGIN(op->ptr, findprop) {
485                         if(RNA_property_type(findprop) == PROP_ENUM) {
486                                 prop= findprop;
487                                 break;
488                         }
489                 }
490                 RNA_STRUCT_END;
491         }
492
493         if(prop==NULL) {
494                 printf("WM_menu_invoke: %s has no \"type\" enum property\n", op->type->idname);
495         }
496         else if (RNA_property_type(prop) != PROP_ENUM) {
497                 printf("WM_menu_invoke: %s \"type\" is not an enum property\n", op->type->idname);
498         }
499         else {
500                 pup= uiPupMenuBegin(C, op->type->name, 0);
501                 layout= uiPupMenuLayout(pup);
502                 uiItemsEnumO(layout, op->type->idname, (char*)RNA_property_identifier(prop));
503                 uiPupMenuEnd(C, pup);
504         }
505
506         return OPERATOR_CANCELLED;
507 }
508
509 /* Can't be used as an invoke directly, needs message arg (can be NULL) */
510 int WM_operator_confirm_message(bContext *C, wmOperator *op, char *message)
511 {
512         uiPopupMenu *pup;
513         uiLayout *layout;
514
515         pup= uiPupMenuBegin(C, "OK?", ICON_QUESTION);
516         layout= uiPupMenuLayout(pup);
517         uiItemO(layout, message, 0, op->type->idname);
518         uiPupMenuEnd(C, pup);
519         
520         return OPERATOR_CANCELLED;
521 }
522
523
524 int WM_operator_confirm(bContext *C, wmOperator *op, wmEvent *event)
525 {
526         return WM_operator_confirm_message(C, op, NULL);
527 }
528
529 /* op->invoke, opens fileselect if path property not set, otherwise executes */
530 int WM_operator_filesel(bContext *C, wmOperator *op, wmEvent *event)
531 {
532         if (RNA_property_is_set(op->ptr, "path")) {
533                 return WM_operator_call(C, op);
534         } 
535         else {
536                 WM_event_add_fileselect(C, op);
537                 return OPERATOR_RUNNING_MODAL;
538         }
539 }
540
541 /* default properties for fileselect */
542 void WM_operator_properties_filesel(wmOperatorType *ot, int filter, short type)
543 {
544         PropertyRNA *prop;
545
546         RNA_def_string_file_path(ot->srna, "path", "", FILE_MAX, "File Path", "Path to file.");
547         RNA_def_string_file_name(ot->srna, "filename", "", FILE_MAX, "File Name", "Name of the file.");
548         RNA_def_string_dir_path(ot->srna, "directory", "", FILE_MAX, "Directory", "Directory of the file.");
549
550         prop= RNA_def_boolean(ot->srna, "filter_blender", (filter & BLENDERFILE), "Filter .blend files", "");
551         RNA_def_property_flag(prop, PROP_HIDDEN);
552         prop= RNA_def_boolean(ot->srna, "filter_image", (filter & IMAGEFILE), "Filter image files", "");
553         RNA_def_property_flag(prop, PROP_HIDDEN);
554         prop= RNA_def_boolean(ot->srna, "filter_movie", (filter & MOVIEFILE), "Filter movie files", "");
555         RNA_def_property_flag(prop, PROP_HIDDEN);
556         prop= RNA_def_boolean(ot->srna, "filter_python", (filter & PYSCRIPTFILE), "Filter python files", "");
557         RNA_def_property_flag(prop, PROP_HIDDEN);
558         prop= RNA_def_boolean(ot->srna, "filter_font", (filter & FTFONTFILE), "Filter font files", "");
559         RNA_def_property_flag(prop, PROP_HIDDEN);
560         prop= RNA_def_boolean(ot->srna, "filter_sound", (filter & SOUNDFILE), "Filter sound files", "");
561         RNA_def_property_flag(prop, PROP_HIDDEN);
562         prop= RNA_def_boolean(ot->srna, "filter_text", (filter & TEXTFILE), "Filter text files", "");
563         RNA_def_property_flag(prop, PROP_HIDDEN);
564         prop= RNA_def_boolean(ot->srna, "filter_folder", (filter & FOLDERFILE), "Filter folders", "");
565         RNA_def_property_flag(prop, PROP_HIDDEN);
566
567         prop= RNA_def_int(ot->srna, "filemode", type, FILE_LOADLIB, FILE_SPECIAL, 
568                 "File Browser Mode", "The setting for the file browser mode to load a .blend file, a library or a special file.",
569                 FILE_LOADLIB, FILE_SPECIAL);
570         RNA_def_property_flag(prop, PROP_HIDDEN);
571 }
572
573 /* op->poll */
574 int WM_operator_winactive(bContext *C)
575 {
576         if(CTX_wm_window(C)==NULL) return 0;
577         return 1;
578 }
579
580 /* op->invoke */
581 static void redo_cb(bContext *C, void *arg_op, int event)
582 {
583         wmOperator *lastop= arg_op;
584         
585         if(lastop) {
586                 ED_undo_pop_op(C, lastop);
587                 WM_operator_repeat(C, lastop);
588         }
589 }
590
591 static uiBlock *wm_block_create_redo(bContext *C, ARegion *ar, void *arg_op)
592 {
593         wmWindowManager *wm= CTX_wm_manager(C);
594         wmOperator *op= arg_op;
595         PointerRNA ptr;
596         uiBlock *block;
597         uiLayout *layout;
598         uiStyle *style= U.uistyles.first;
599         
600         block= uiBeginBlock(C, ar, "redo_popup", UI_EMBOSS);
601         uiBlockClearFlag(block, UI_BLOCK_LOOP);
602         uiBlockSetFlag(block, UI_BLOCK_KEEP_OPEN|UI_BLOCK_RET_1);
603         uiBlockSetHandleFunc(block, redo_cb, arg_op);
604
605         if(!op->properties) {
606                 IDPropertyTemplate val = {0};
607                 op->properties= IDP_New(IDP_GROUP, val, "wmOperatorProperties");
608         }
609
610         RNA_pointer_create(&wm->id, op->type->srna, op->properties, &ptr);
611         layout= uiBlockLayout(block, UI_LAYOUT_VERTICAL, UI_LAYOUT_PANEL, 0, 0, 300, 20, style);
612         uiItemL(layout, op->type->name, 0);
613
614         if(op->type->ui)
615                 op->type->ui((bContext*)C, &ptr, layout);
616         else
617                 uiDefAutoButsRNA(C, layout, &ptr, 2);
618
619         uiPopupBoundsBlock(block, 4.0f, 0, 0);
620         uiEndBlock(C, block);
621
622         return block;
623 }
624
625 int WM_operator_props_popup(bContext *C, wmOperator *op, wmEvent *event)
626 {
627         int retval= OPERATOR_CANCELLED;
628         
629         if(op->type->exec)
630                 retval= op->type->exec(C, op);
631
632         if(retval != OPERATOR_CANCELLED)
633                 uiPupBlock(C, wm_block_create_redo, op);
634
635         return retval;
636 }
637
638 int WM_operator_redo_popup(bContext *C, wmOperator *op)
639 {
640         uiPupBlock(C, wm_block_create_redo, op);
641
642         return OPERATOR_CANCELLED;
643 }
644
645
646 /* ***************** Debug menu ************************* */
647
648 static uiBlock *wm_block_create_menu(bContext *C, ARegion *ar, void *arg_op)
649 {
650         wmOperator *op= arg_op;
651         uiBlock *block;
652         uiLayout *layout;
653         uiStyle *style= U.uistyles.first;
654         
655         block= uiBeginBlock(C, ar, "_popup", UI_EMBOSS);
656         uiBlockClearFlag(block, UI_BLOCK_LOOP);
657         uiBlockSetFlag(block, UI_BLOCK_KEEP_OPEN|UI_BLOCK_RET_1);
658         
659         layout= uiBlockLayout(block, UI_LAYOUT_VERTICAL, UI_LAYOUT_PANEL, 0, 0, 300, 20, style);
660         uiItemL(layout, op->type->name, 0);
661
662         if(op->type->ui)
663                 op->type->ui(C, op->ptr, layout);
664         else
665                 uiDefAutoButsRNA(C, layout, op->ptr, 2);
666         
667         uiPopupBoundsBlock(block, 4.0f, 0, 0);
668         uiEndBlock(C, block);
669         
670         return block;
671 }
672
673 static int wm_debug_menu_exec(bContext *C, wmOperator *op)
674 {
675         G.rt= RNA_int_get(op->ptr, "debugval");
676         ED_screen_refresh(CTX_wm_manager(C), CTX_wm_window(C));
677         WM_event_add_notifier(C, NC_WINDOW, NULL);
678         
679         return OPERATOR_FINISHED;       
680 }
681
682 static int wm_debug_menu_invoke(bContext *C, wmOperator *op, wmEvent *event)
683 {
684         
685         RNA_int_set(op->ptr, "debugval", G.rt);
686         
687         /* pass on operator, so return modal */
688         uiPupBlockOperator(C, wm_block_create_menu, op, WM_OP_EXEC_DEFAULT);
689         
690         return OPERATOR_RUNNING_MODAL;
691 }
692
693 static void WM_OT_debug_menu(wmOperatorType *ot)
694 {
695         ot->name= "Debug Menu";
696         ot->idname= "WM_OT_debug_menu";
697         ot->description= "Open a popup to set the debug level.";
698         
699         ot->invoke= wm_debug_menu_invoke;
700         ot->exec= wm_debug_menu_exec;
701         ot->poll= WM_operator_winactive;
702         
703         RNA_def_int(ot->srna, "debugval", 0, -10000, 10000, "Debug Value", "", INT_MIN, INT_MAX);
704 }
705
706 /* ***************** Search menu ************************* */
707 static void operator_call_cb(struct bContext *C, void *arg1, void *arg2)
708 {
709         wmOperatorType *ot= arg2;
710         
711         if(ot)
712                 WM_operator_name_call(C, ot->idname, WM_OP_INVOKE_DEFAULT, NULL);
713 }
714
715 static void operator_search_cb(const struct bContext *C, void *arg, char *str, uiSearchItems *items)
716 {
717         wmOperatorType *ot = WM_operatortype_first();
718         
719         for(; ot; ot= ot->next) {
720                 
721                 if(BLI_strcasestr(ot->name, str)) {
722                         if(WM_operator_poll((bContext*)C, ot)) {
723                                 char name[256];
724                                 int len= strlen(ot->name);
725                                 
726                                 /* display name for menu, can hold hotkey */
727                                 BLI_strncpy(name, ot->name, 256);
728                                 
729                                 /* check for hotkey */
730                                 if(len < 256-6) {
731                                         if(WM_key_event_operator_string(C, ot->idname, WM_OP_EXEC_DEFAULT, NULL, &name[len+1], 256-len-1))
732                                                 name[len]= '|';
733                                 }
734                                 
735                                 if(0==uiSearchItemAdd(items, name, ot, 0))
736                                         break;
737                         }
738                 }
739         }
740 }
741
742 static uiBlock *wm_block_search_menu(bContext *C, ARegion *ar, void *arg_op)
743 {
744         static char search[256]= "";
745         wmEvent event;
746         wmWindow *win= CTX_wm_window(C);
747         uiBlock *block;
748         uiBut *but;
749         
750         block= uiBeginBlock(C, ar, "_popup", UI_EMBOSS);
751         uiBlockSetFlag(block, UI_BLOCK_LOOP|UI_BLOCK_RET_1);
752         
753         but= uiDefSearchBut(block, search, 0, ICON_VIEWZOOM, 256, 10, 10, 180, 19, "");
754         uiButSetSearchFunc(but, operator_search_cb, NULL, operator_call_cb, NULL);
755         
756         /* fake button, it holds space for search items */
757         uiDefBut(block, LABEL, 0, "", 10, 10 - uiSearchBoxhHeight(), 180, uiSearchBoxhHeight(), NULL, 0, 0, 0, 0, NULL);
758         
759         uiPopupBoundsBlock(block, 6.0f, 0, -20); /* move it downwards, mouse over button */
760         uiEndBlock(C, block);
761         
762         event= *(win->eventstate);      /* XXX huh huh? make api call */
763         event.type= EVT_BUT_OPEN;
764         event.val= KM_PRESS;
765         event.customdata= but;
766         event.customdatafree= FALSE;
767         wm_event_add(win, &event);
768         
769         return block;
770 }
771
772 static int wm_search_menu_exec(bContext *C, wmOperator *op)
773 {
774         
775         return OPERATOR_FINISHED;       
776 }
777
778 static int wm_search_menu_invoke(bContext *C, wmOperator *op, wmEvent *event)
779 {
780         
781         uiPupBlock(C, wm_block_search_menu, op);
782         
783         return OPERATOR_CANCELLED;
784 }
785
786 /* op->poll */
787 static int wm_search_menu_poll(bContext *C)
788 {
789         if(CTX_wm_window(C)==NULL) return 0;
790         if(CTX_wm_area(C) && CTX_wm_area(C)->spacetype==SPACE_CONSOLE) return 0;  // XXX - so we can use the shortcut in the console
791         if(CTX_wm_area(C) && CTX_wm_area(C)->spacetype==SPACE_TEXT) return 0;  // XXX - so we can use the spacebar in the text editor
792         if(CTX_data_edit_object(C) && CTX_data_edit_object(C)->type==OB_CURVE) return 0; // XXX - so we can use the spacebar for entering text
793         return 1;
794 }
795
796 static void WM_OT_search_menu(wmOperatorType *ot)
797 {
798         ot->name= "Search Menu";
799         ot->idname= "WM_OT_search_menu";
800         
801         ot->invoke= wm_search_menu_invoke;
802         ot->exec= wm_search_menu_exec;
803         ot->poll= wm_search_menu_poll;
804 }
805
806 static int wm_call_menu_invoke(bContext *C, wmOperator *op, wmEvent *event)
807 {
808         char idname[BKE_ST_MAXNAME];
809         RNA_string_get(op->ptr, "name", idname);
810
811         uiPupMenuInvoke(C, idname);
812
813         return OPERATOR_CANCELLED;
814 }
815
816 static void WM_OT_call_menu(wmOperatorType *ot)
817 {
818         ot->name= "Call Menu";
819         ot->idname= "WM_OT_call_menu";
820
821         ot->invoke= wm_call_menu_invoke;
822
823         RNA_def_string(ot->srna, "name", "", BKE_ST_MAXNAME, "Name", "Name of the new sequence strip");
824 }
825
826 /* ************ window / screen operator definitions ************** */
827
828 static void WM_OT_window_duplicate(wmOperatorType *ot)
829 {
830         ot->name= "Duplicate Window";
831         ot->idname= "WM_OT_window_duplicate";
832         ot->description="Duplicate the current Blender window.";
833                 
834         ot->exec= wm_window_duplicate_op;
835         ot->poll= WM_operator_winactive;
836 }
837
838 static void WM_OT_save_homefile(wmOperatorType *ot)
839 {
840         ot->name= "Save User Settings";
841         ot->idname= "WM_OT_save_homefile";
842         ot->description="Make the current file the default .blend file.";
843                 
844         ot->invoke= WM_operator_confirm;
845         ot->exec= WM_write_homefile;
846         ot->poll= WM_operator_winactive;
847 }
848
849 static void WM_OT_read_homefile(wmOperatorType *ot)
850 {
851         ot->name= "Reload Start-Up File";
852         ot->idname= "WM_OT_read_homefile";
853         ot->description="Open the default file (doesn't save the current file).";
854         
855         ot->invoke= WM_operator_confirm;
856         ot->exec= WM_read_homefile;
857         ot->poll= WM_operator_winactive;
858         
859         RNA_def_boolean(ot->srna, "factory", 0, "Factory Settings", "");
860 }
861
862
863 /* ********* recent file *********** */
864
865 static int recentfile_exec(bContext *C, wmOperator *op)
866 {
867         int event= RNA_enum_get(op->ptr, "file");
868
869         // XXX wm in context is not set correctly after WM_read_file -> crash
870         // do it before for now, but is this correct with multiple windows?
871
872         if(event>0) {
873                 if (G.sce[0] && (event==1)) {
874                         WM_event_add_notifier(C, NC_WINDOW, NULL);
875                         WM_read_file(C, G.sce, op->reports);
876                 }
877                 else {
878                         struct RecentFile *recent = BLI_findlink(&(G.recent_files), event-1);
879                         if(recent) {
880                                 WM_event_add_notifier(C, NC_WINDOW, NULL);
881                                 WM_read_file(C, recent->filename, op->reports);
882                         }
883                 }
884         }
885         return 0;
886 }
887
888 static int wm_recentfile_invoke(bContext *C, wmOperator *op, wmEvent *event)
889 {
890         uiPopupMenu *pup;
891         uiLayout *layout;
892
893         pup= uiPupMenuBegin(C, "Open Recent", 0);
894         layout= uiPupMenuLayout(pup);
895         uiItemsEnumO(layout, op->type->idname, "file");
896         uiPupMenuEnd(C, pup);
897         
898         return OPERATOR_CANCELLED;
899 }
900
901 static EnumPropertyItem *open_recentfile_itemf(bContext *C, PointerRNA *ptr, int *free)
902 {
903         EnumPropertyItem tmp = {0, "", 0, "", ""};
904         EnumPropertyItem *item= NULL;
905         struct RecentFile *recent;
906         int totitem= 0, i;
907
908         /* dynamically construct enum */
909         for(recent = G.recent_files.first, i=0; (i<U.recent_files) && (recent); recent = recent->next, i++) {
910                 tmp.value= i+1;
911                 tmp.identifier= recent->filename;
912                 tmp.name= BLI_short_filename(recent->filename);
913                 RNA_enum_item_add(&item, &totitem, &tmp);
914         }
915
916         RNA_enum_item_end(&item, &totitem);
917         *free= 1;
918
919         return item;
920 }
921
922 static void WM_OT_open_recentfile(wmOperatorType *ot)
923 {
924         PropertyRNA *prop;
925         static EnumPropertyItem file_items[]= {
926                 {0, NULL, 0, NULL, NULL}};
927
928         ot->name= "Open Recent File";
929         ot->idname= "WM_OT_open_recentfile";
930         ot->description="Open recent files list.";
931         
932         ot->invoke= wm_recentfile_invoke;
933         ot->exec= recentfile_exec;
934         ot->poll= WM_operator_winactive;
935         
936         prop= RNA_def_enum(ot->srna, "file", file_items, 1, "File", "");
937         RNA_def_enum_funcs(prop, open_recentfile_itemf);
938 }
939
940 /* *************** open file **************** */
941
942 static void open_set_load_ui(wmOperator *op)
943 {
944         if(!RNA_property_is_set(op->ptr, "load_ui"))
945                 RNA_boolean_set(op->ptr, "load_ui", !(U.flag & USER_FILENOUI));
946 }
947
948 static int wm_open_mainfile_invoke(bContext *C, wmOperator *op, wmEvent *event)
949 {
950         RNA_string_set(op->ptr, "path", G.sce);
951         open_set_load_ui(op);
952
953         WM_event_add_fileselect(C, op);
954
955         return OPERATOR_RUNNING_MODAL;
956 }
957
958 static int wm_open_mainfile_exec(bContext *C, wmOperator *op)
959 {
960         char path[FILE_MAX];
961
962         RNA_string_get(op->ptr, "path", path);
963         open_set_load_ui(op);
964
965         if(RNA_boolean_get(op->ptr, "load_ui"))
966                 G.fileflags &= ~G_FILE_NO_UI;
967         else
968                 G.fileflags |= G_FILE_NO_UI;
969         
970         // XXX wm in context is not set correctly after WM_read_file -> crash
971         // do it before for now, but is this correct with multiple windows?
972         WM_event_add_notifier(C, NC_WINDOW, NULL);
973
974         WM_read_file(C, path, op->reports);
975         
976         return OPERATOR_FINISHED;
977 }
978
979 static void WM_OT_open_mainfile(wmOperatorType *ot)
980 {
981         ot->name= "Open Blender File";
982         ot->idname= "WM_OT_open_mainfile";
983         ot->description="Open a Blender file.";
984         
985         ot->invoke= wm_open_mainfile_invoke;
986         ot->exec= wm_open_mainfile_exec;
987         ot->poll= WM_operator_winactive;
988         
989         WM_operator_properties_filesel(ot, FOLDERFILE|BLENDERFILE, FILE_BLENDER);
990
991         RNA_def_boolean(ot->srna, "load_ui", 1, "Load UI", "Load user interface setup in the .blend file.");
992 }
993
994 /* **************** link/append *************** */
995
996 static int wm_link_append_invoke(bContext *C, wmOperator *op, wmEvent *event)
997 {
998         if(RNA_property_is_set(op->ptr, "path")) {
999                 return WM_operator_call(C, op);
1000         } 
1001         else {
1002                 /* XXX TODO solve where to get last linked library from */
1003                 RNA_string_set(op->ptr, "path", G.lib);
1004                 WM_event_add_fileselect(C, op);
1005                 return OPERATOR_RUNNING_MODAL;
1006         }
1007 }
1008
1009 static short wm_link_append_flag(wmOperator *op)
1010 {
1011         short flag= 0;
1012
1013         if(RNA_boolean_get(op->ptr, "autoselect")) flag |= FILE_AUTOSELECT;
1014         if(RNA_boolean_get(op->ptr, "active_layer")) flag |= FILE_ACTIVELAY;
1015         if(RNA_boolean_get(op->ptr, "relative_paths")) flag |= FILE_STRINGCODE;
1016         if(RNA_boolean_get(op->ptr, "link")) flag |= FILE_LINK;
1017
1018         return flag;
1019 }
1020
1021 static void wm_link_make_library_local(Main *main, const char *libname)
1022 {
1023         Library *lib;
1024
1025         /* and now find the latest append lib file */
1026         for(lib= main->library.first; lib; lib=lib->id.next)
1027                 if(BLI_streq(libname, lib->filename))
1028                         break;
1029         
1030         /* make local */
1031         if(lib) {
1032                 all_local(lib, 1);
1033                 /* important we unset, otherwise these object wont
1034                  * link into other scenes from this blend file */
1035                 flag_all_listbases_ids(LIB_APPEND_TAG, 0);
1036         }
1037 }
1038
1039 static int wm_link_append_exec(bContext *C, wmOperator *op)
1040 {
1041         Main *bmain= CTX_data_main(C);
1042         Scene *scene= CTX_data_scene(C);
1043         Main *mainl= 0;
1044         BlendHandle *bh;
1045         PropertyRNA *prop;
1046         char name[FILE_MAX], dir[FILE_MAX], libname[FILE_MAX], group[GROUP_MAX];
1047         int idcode, totfiles=0;
1048         short flag;
1049
1050         name[0] = '\0';
1051         RNA_string_get(op->ptr, "filename", name);
1052         RNA_string_get(op->ptr, "directory", dir);
1053
1054         /* test if we have a valid data */
1055         if(BLO_is_a_library(dir, libname, group) == 0) {
1056                 BKE_report(op->reports, RPT_ERROR, "Not a library");
1057                 return OPERATOR_CANCELLED;
1058         }
1059         else if(group[0] == 0) {
1060                 BKE_report(op->reports, RPT_ERROR, "Nothing indicated");
1061                 return OPERATOR_CANCELLED;
1062         }
1063         else if(BLI_streq(bmain->name, libname)) {
1064                 BKE_report(op->reports, RPT_ERROR, "Cannot use current file as library");
1065                 return OPERATOR_CANCELLED;
1066         }
1067
1068         /* check if something is indicated for append/link */
1069         prop = RNA_struct_find_property(op->ptr, "files");
1070         if(prop) {
1071                 totfiles= RNA_property_collection_length(op->ptr, prop);
1072                 if(totfiles == 0) {
1073                         if(name[0] == '\0') {
1074                                 BKE_report(op->reports, RPT_ERROR, "Nothing indicated");
1075                                 return OPERATOR_CANCELLED;
1076                         }
1077                 }
1078         }
1079         else if(name[0] == '\0') {
1080                 BKE_report(op->reports, RPT_ERROR, "Nothing indicated");
1081                 return OPERATOR_CANCELLED;
1082         }
1083
1084         /* now we have or selected, or an indicated file */
1085         if(RNA_boolean_get(op->ptr, "autoselect"))
1086                 scene_deselect_all(scene);
1087
1088         bh = BLO_blendhandle_from_file(libname);
1089         idcode = BLO_idcode_from_name(group);
1090         
1091         flag = wm_link_append_flag(op);
1092
1093         /* tag everything, all untagged data can be made local */
1094         if((flag & FILE_LINK)==0)
1095                 flag_all_listbases_ids(LIB_APPEND_TAG, 1);
1096
1097         /* here appending/linking starts */
1098         mainl = BLO_library_append_begin(C, &bh, libname);
1099         if(totfiles == 0) {
1100                 BLO_library_append_named_part(C, mainl, &bh, name, idcode, flag);
1101         }
1102         else {
1103                 RNA_BEGIN(op->ptr, itemptr, "files") {
1104                         RNA_string_get(&itemptr, "name", name);
1105                         BLO_library_append_named_part(C, mainl, &bh, name, idcode, flag);
1106                 }
1107                 RNA_END;
1108         }
1109         BLO_library_append_end(C, mainl, &bh, idcode, flag);
1110         
1111         /* mark all library linked objects to be updated */
1112         recalc_all_library_objects(bmain);
1113
1114         /* append, rather than linking */
1115         if((flag & FILE_LINK)==0)
1116                 wm_link_make_library_local(bmain, libname);
1117
1118         /* recreate dependency graph to include new objects */
1119         DAG_scene_sort(scene);
1120         DAG_ids_flush_update(0);
1121
1122         BLO_blendhandle_close(bh);
1123
1124         /* XXX TODO: align G.lib with other directory storage (like last opened image etc...) */
1125         BLI_strncpy(G.lib, dir, FILE_MAX);
1126
1127         WM_event_add_notifier(C, NC_WINDOW, NULL);
1128
1129         return OPERATOR_FINISHED;
1130 }
1131
1132 static void WM_OT_link_append(wmOperatorType *ot)
1133 {
1134         ot->name= "Link/Append from Library";
1135         ot->idname= "WM_OT_link_append";
1136         ot->description= "Link or Append from a Library .blend file";
1137         
1138         ot->invoke= wm_link_append_invoke;
1139         ot->exec= wm_link_append_exec;
1140         ot->poll= WM_operator_winactive;
1141         
1142         ot->flag |= OPTYPE_UNDO;
1143
1144         WM_operator_properties_filesel(ot, FOLDERFILE|BLENDERFILE, FILE_LOADLIB);
1145         
1146         RNA_def_boolean(ot->srna, "link", 1, "Link", "Link the objects or datablocks rather than appending.");
1147         RNA_def_boolean(ot->srna, "autoselect", 1, "Select", "Select the linked objects.");
1148         RNA_def_boolean(ot->srna, "active_layer", 1, "Active Layer", "Put the linked objects on the active layer.");
1149         RNA_def_boolean(ot->srna, "relative_paths", 1, "Relative Paths", "Store the library path as a relative path to current .blend file.");
1150
1151         RNA_def_collection_runtime(ot->srna, "files", &RNA_OperatorFileListElement, "Files", "");
1152 }       
1153
1154 /* *************** recover last session **************** */
1155
1156 static int wm_recover_last_session_exec(bContext *C, wmOperator *op)
1157 {
1158         char filename[FILE_MAX];
1159
1160         G.fileflags |= G_FILE_RECOVER;
1161
1162         // XXX wm in context is not set correctly after WM_read_file -> crash
1163         // do it before for now, but is this correct with multiple windows?
1164         WM_event_add_notifier(C, NC_WINDOW, NULL);
1165
1166         /* load file */
1167         BLI_make_file_string("/", filename, btempdir, "quit.blend");
1168         WM_read_file(C, filename, op->reports);
1169
1170         G.fileflags &= ~G_FILE_RECOVER;
1171
1172         return OPERATOR_FINISHED;
1173 }
1174
1175 static void WM_OT_recover_last_session(wmOperatorType *ot)
1176 {
1177         ot->name= "Recover Last Session";
1178         ot->idname= "WM_OT_recover_last_session";
1179         ot->description="Open the last closed file (\"quit.blend\").";
1180         
1181         ot->exec= wm_recover_last_session_exec;
1182         ot->poll= WM_operator_winactive;
1183 }
1184
1185 /* *************** recover auto save **************** */
1186
1187 static int wm_recover_auto_save_exec(bContext *C, wmOperator *op)
1188 {
1189         char path[FILE_MAX];
1190
1191         RNA_string_get(op->ptr, "path", path);
1192
1193         G.fileflags |= G_FILE_RECOVER;
1194
1195         // XXX wm in context is not set correctly after WM_read_file -> crash
1196         // do it before for now, but is this correct with multiple windows?
1197         WM_event_add_notifier(C, NC_WINDOW, NULL);
1198
1199         /* load file */
1200         WM_read_file(C, path, op->reports);
1201
1202         G.fileflags &= ~G_FILE_RECOVER;
1203
1204         return OPERATOR_FINISHED;
1205 }
1206
1207 static int wm_recover_auto_save_invoke(bContext *C, wmOperator *op, wmEvent *event)
1208 {
1209         char filename[FILE_MAX];
1210
1211         wm_autosave_location(filename);
1212         RNA_string_set(op->ptr, "path", filename);
1213         WM_event_add_fileselect(C, op);
1214
1215         return OPERATOR_RUNNING_MODAL;
1216 }
1217
1218 static void WM_OT_recover_auto_save(wmOperatorType *ot)
1219 {
1220         ot->name= "Recover Auto Save";
1221         ot->idname= "WM_OT_recover_auto_save";
1222         ot->description="Open an automatically saved file to recover it.";
1223         
1224         ot->exec= wm_recover_auto_save_exec;
1225         ot->invoke= wm_recover_auto_save_invoke;
1226         ot->poll= WM_operator_winactive;
1227
1228         WM_operator_properties_filesel(ot, BLENDERFILE, FILE_BLENDER);
1229 }
1230
1231 /* *************** save file as **************** */
1232
1233 static void untitled(char *name)
1234 {
1235         if(G.save_over == 0 && strlen(name) < FILE_MAX-16) {
1236                 char *c= BLI_last_slash(name);
1237                 
1238                 if(c)
1239                         strcpy(&c[1], "untitled.blend");
1240                 else
1241                         strcpy(name, "untitled.blend");
1242         }
1243 }
1244
1245 static void save_set_compress(wmOperator *op)
1246 {
1247         if(!RNA_property_is_set(op->ptr, "compress")) {
1248                 if(G.save_over) /* keep flag for existing file */
1249                         RNA_boolean_set(op->ptr, "compress", G.fileflags & G_FILE_COMPRESS);
1250                 else /* use userdef for new file */
1251                         RNA_boolean_set(op->ptr, "compress", U.flag & USER_FILECOMPRESS);
1252         }
1253 }
1254
1255 static int wm_save_as_mainfile_invoke(bContext *C, wmOperator *op, wmEvent *event)
1256 {
1257         char name[FILE_MAX];
1258
1259         save_set_compress(op);
1260         
1261         BLI_strncpy(name, G.sce, FILE_MAX);
1262         untitled(name);
1263         RNA_string_set(op->ptr, "path", name);
1264         
1265         WM_event_add_fileselect(C, op);
1266
1267         return OPERATOR_RUNNING_MODAL;
1268 }
1269
1270 /* function used for WM_OT_save_mainfile too */
1271 static int wm_save_as_mainfile_exec(bContext *C, wmOperator *op)
1272 {
1273         char path[FILE_MAX];
1274         int fileflags;
1275
1276         save_set_compress(op);
1277         
1278         if(RNA_property_is_set(op->ptr, "path"))
1279                 RNA_string_get(op->ptr, "path", path);
1280         else {
1281                 BLI_strncpy(path, G.sce, FILE_MAX);
1282                 untitled(path);
1283         }
1284
1285         fileflags= G.fileflags;
1286
1287         /* set compression flag */
1288         if(RNA_boolean_get(op->ptr, "compress"))
1289                 fileflags |= G_FILE_COMPRESS;
1290         else
1291                 fileflags &= ~G_FILE_COMPRESS;
1292
1293         WM_write_file(C, path, fileflags, op->reports);
1294         
1295         WM_event_add_notifier(C, NC_WM|ND_FILESAVE, NULL);
1296
1297         return 0;
1298 }
1299
1300 static void WM_OT_save_as_mainfile(wmOperatorType *ot)
1301 {
1302         ot->name= "Save As Blender File";
1303         ot->idname= "WM_OT_save_as_mainfile";
1304         ot->description="Save the current file in the desired location.";
1305         
1306         ot->invoke= wm_save_as_mainfile_invoke;
1307         ot->exec= wm_save_as_mainfile_exec;
1308         ot->poll= WM_operator_winactive;
1309         
1310         WM_operator_properties_filesel(ot, FOLDERFILE|BLENDERFILE, FILE_BLENDER);
1311         RNA_def_boolean(ot->srna, "compress", 0, "Compress", "Write compressed .blend file.");
1312 }
1313
1314 /* *************** save file directly ******** */
1315
1316 static int wm_save_mainfile_invoke(bContext *C, wmOperator *op, wmEvent *event)
1317 {
1318         char name[FILE_MAX];
1319
1320         save_set_compress(op);
1321         
1322         BLI_strncpy(name, G.sce, FILE_MAX);
1323         untitled(name);
1324         RNA_string_set(op->ptr, "path", name);
1325         
1326         if (G.save_over)
1327                 uiPupMenuSaveOver(C, op, name);
1328         else
1329                 WM_event_add_fileselect(C, op);
1330         
1331         return OPERATOR_RUNNING_MODAL;
1332 }
1333
1334 static void WM_OT_save_mainfile(wmOperatorType *ot)
1335 {
1336         ot->name= "Save Blender File";
1337         ot->idname= "WM_OT_save_mainfile";
1338         ot->description="Save the current Blender file.";
1339         
1340         ot->invoke= wm_save_mainfile_invoke;
1341         ot->exec= wm_save_as_mainfile_exec;
1342         ot->poll= WM_operator_winactive;
1343         
1344         WM_operator_properties_filesel(ot, FOLDERFILE|BLENDERFILE, FILE_BLENDER);
1345         RNA_def_boolean(ot->srna, "compress", 0, "Compress", "Write compressed .blend file.");
1346 }
1347
1348
1349 /* XXX: move these collada operators to a more appropriate place */
1350 #ifdef WITH_COLLADA
1351
1352 #include "../../collada/collada.h"
1353
1354 static int wm_collada_export_invoke(bContext *C, wmOperator *op, wmEvent *event)
1355 {
1356         //char name[FILE_MAX];
1357         //BLI_strncpy(name, G.sce, FILE_MAX);
1358         //untitled(name);
1359
1360         /* RNA_string_set(op->ptr, "path", "/tmp/test.dae"); */
1361         
1362         WM_event_add_fileselect(C, op);
1363
1364         return OPERATOR_RUNNING_MODAL;
1365 }
1366
1367 /* function used for WM_OT_save_mainfile too */
1368 static int wm_collada_export_exec(bContext *C, wmOperator *op)
1369 {
1370         char filename[FILE_MAX];
1371         
1372         if(RNA_property_is_set(op->ptr, "path"))
1373                 RNA_string_get(op->ptr, "path", filename);
1374         else {
1375                 BLI_strncpy(filename, G.sce, FILE_MAX);
1376                 untitled(filename);
1377         }
1378         
1379         //WM_write_file(C, filename, op->reports);
1380         collada_export(CTX_data_scene(C), filename);
1381         
1382         /* WM_event_add_notifier(C, NC_WM|ND_FILESAVE, NULL); */
1383
1384         return OPERATOR_FINISHED;
1385 }
1386
1387 static void WM_OT_collada_export(wmOperatorType *ot)
1388 {
1389         ot->name= "Export COLLADA";
1390         ot->idname= "WM_OT_collada_export";
1391         
1392         ot->invoke= wm_collada_export_invoke;
1393         ot->exec= wm_collada_export_exec;
1394         ot->poll= WM_operator_winactive;
1395         
1396         ot->flag= 0;
1397         
1398         RNA_def_property(ot->srna, "path", PROP_STRING, PROP_FILEPATH);
1399 }
1400
1401 static int wm_collada_import_invoke(bContext *C, wmOperator *op, wmEvent *event)
1402 {
1403         /* RNA_string_set(op->ptr, "path", "/tmp/test.dae"); */
1404         
1405         WM_event_add_fileselect(C, op);
1406
1407         return OPERATOR_RUNNING_MODAL;
1408 }
1409
1410 /* function used for WM_OT_save_mainfile too */
1411 static int wm_collada_import_exec(bContext *C, wmOperator *op)
1412 {
1413         char filename[FILE_MAX];
1414         
1415         if(RNA_property_is_set(op->ptr, "path"))
1416                 RNA_string_get(op->ptr, "path", filename);
1417         else {
1418                 BLI_strncpy(filename, G.sce, FILE_MAX);
1419                 untitled(filename);
1420         }
1421         
1422         //WM_write_file(C, filename, op->reports);
1423         collada_import(C, filename);
1424         
1425         /* WM_event_add_notifier(C, NC_WM|ND_FILESAVE, NULL); */
1426
1427         return OPERATOR_FINISHED;
1428 }
1429
1430 static void WM_OT_collada_import(wmOperatorType *ot)
1431 {
1432         ot->name= "Import COLLADA";
1433         ot->idname= "WM_OT_collada_import";
1434         
1435         ot->invoke= wm_collada_import_invoke;
1436         ot->exec= wm_collada_import_exec;
1437         ot->poll= WM_operator_winactive;
1438         
1439         ot->flag= 0;
1440         
1441         RNA_def_property(ot->srna, "path", PROP_STRING, PROP_FILEPATH);
1442 }
1443
1444 #endif
1445
1446
1447
1448 /* *********************** */
1449
1450 static void WM_OT_window_fullscreen_toggle(wmOperatorType *ot)
1451 {
1452         ot->name= "Toggle Fullscreen";
1453         ot->idname= "WM_OT_window_fullscreen_toggle";
1454         ot->description="Toggle the current window fullscreen.";
1455
1456         ot->exec= wm_window_fullscreen_toggle_op;
1457         ot->poll= WM_operator_winactive;
1458 }
1459
1460 static int wm_exit_blender_op(bContext *C, wmOperator *op)
1461 {
1462         WM_operator_free(op);
1463         
1464         WM_exit(C);     
1465         
1466         return OPERATOR_FINISHED;
1467 }
1468
1469 static void WM_OT_exit_blender(wmOperatorType *ot)
1470 {
1471         ot->name= "Exit Blender";
1472         ot->idname= "WM_OT_exit_blender";
1473         ot->description= "Quit Blender.";
1474
1475         ot->invoke= WM_operator_confirm;
1476         ot->exec= wm_exit_blender_op;
1477         ot->poll= WM_operator_winactive;
1478 }
1479
1480 /* ************ default paint cursors, draw always around cursor *********** */
1481 /*
1482  - returns handler to free 
1483  - poll(bContext): returns 1 if draw should happen
1484  - draw(bContext): drawing callback for paint cursor
1485 */
1486
1487 void *WM_paint_cursor_activate(wmWindowManager *wm, int (*poll)(bContext *C),
1488                                wmPaintCursorDraw draw, void *customdata)
1489 {
1490         wmPaintCursor *pc= MEM_callocN(sizeof(wmPaintCursor), "paint cursor");
1491         
1492         BLI_addtail(&wm->paintcursors, pc);
1493         
1494         pc->customdata = customdata;
1495         pc->poll= poll;
1496         pc->draw= draw;
1497         
1498         return pc;
1499 }
1500
1501 void WM_paint_cursor_end(wmWindowManager *wm, void *handle)
1502 {
1503         wmPaintCursor *pc;
1504         
1505         for(pc= wm->paintcursors.first; pc; pc= pc->next) {
1506                 if(pc == (wmPaintCursor *)handle) {
1507                         BLI_remlink(&wm->paintcursors, pc);
1508                         MEM_freeN(pc);
1509                         return;
1510                 }
1511         }
1512 }
1513
1514 /* ************ window gesture operator-callback definitions ************** */
1515 /*
1516  * These are default callbacks for use in operators requiring gesture input
1517  */
1518
1519 /* **************** Border gesture *************** */
1520
1521 /* Border gesture has two types:
1522    1) WM_GESTURE_CROSS_RECT: starts a cross, on mouse click it changes to border 
1523    2) WM_GESTURE_RECT: starts immediate as a border, on mouse click or release it ends
1524
1525    It stores 4 values (xmin, xmax, ymin, ymax) and event it ended with (event_type)
1526 */
1527
1528 static int border_apply(bContext *C, wmOperator *op, int event_type, int event_orig)
1529 {
1530         wmGesture *gesture= op->customdata;
1531         rcti *rect= gesture->customdata;
1532         
1533         if(rect->xmin > rect->xmax)
1534                 SWAP(int, rect->xmin, rect->xmax);
1535         if(rect->ymin > rect->ymax)
1536                 SWAP(int, rect->ymin, rect->ymax);
1537         
1538         if(rect->xmin==rect->xmax || rect->ymin==rect->ymax)
1539                 return 0;
1540                 
1541         /* operator arguments and storage. */
1542         RNA_int_set(op->ptr, "xmin", rect->xmin);
1543         RNA_int_set(op->ptr, "ymin", rect->ymin);
1544         RNA_int_set(op->ptr, "xmax", rect->xmax);
1545         RNA_int_set(op->ptr, "ymax", rect->ymax);
1546         
1547         /* XXX weak; border should be configured for this without reading event types */
1548         if( RNA_struct_find_property(op->ptr, "event_type") ) {
1549                 if(ELEM4(event_orig, EVT_TWEAK_L, EVT_TWEAK_R, EVT_TWEAK_A, EVT_TWEAK_S))
1550                         event_type= LEFTMOUSE;
1551                 
1552                 RNA_int_set(op->ptr, "event_type", event_type);
1553         }
1554         op->type->exec(C, op);
1555         
1556         return 1;
1557 }
1558
1559 static void wm_gesture_end(bContext *C, wmOperator *op)
1560 {
1561         wmGesture *gesture= op->customdata;
1562         
1563         WM_gesture_end(C, gesture);     /* frees gesture itself, and unregisters from window */
1564         op->customdata= NULL;
1565
1566         ED_area_tag_redraw(CTX_wm_area(C));
1567         
1568         if( RNA_struct_find_property(op->ptr, "cursor") )
1569                 WM_cursor_restore(CTX_wm_window(C));
1570 }
1571
1572 int WM_border_select_invoke(bContext *C, wmOperator *op, wmEvent *event)
1573 {
1574         if(ISTWEAK(event->type))
1575                 op->customdata= WM_gesture_new(C, event, WM_GESTURE_RECT);
1576         else
1577                 op->customdata= WM_gesture_new(C, event, WM_GESTURE_CROSS_RECT);
1578
1579         /* add modal handler */
1580         WM_event_add_modal_handler(C, op);
1581         
1582         wm_gesture_tag_redraw(C);
1583
1584         return OPERATOR_RUNNING_MODAL;
1585 }
1586
1587 int WM_border_select_modal(bContext *C, wmOperator *op, wmEvent *event)
1588 {
1589         wmGesture *gesture= op->customdata;
1590         rcti *rect= gesture->customdata;
1591         int sx, sy;
1592         
1593         switch(event->type) {
1594                 case MOUSEMOVE:
1595                         
1596                         wm_subwindow_getorigin(CTX_wm_window(C), gesture->swinid, &sx, &sy);
1597                         
1598                         if(gesture->type==WM_GESTURE_CROSS_RECT && gesture->mode==0) {
1599                                 rect->xmin= rect->xmax= event->x - sx;
1600                                 rect->ymin= rect->ymax= event->y - sy;
1601                         }
1602                         else {
1603                                 rect->xmax= event->x - sx;
1604                                 rect->ymax= event->y - sy;
1605                         }
1606                         
1607                         wm_gesture_tag_redraw(C);
1608
1609                         break;
1610                         
1611                 case LEFTMOUSE:
1612                 case MIDDLEMOUSE:
1613                 case RIGHTMOUSE:
1614                         if(event->val==KM_PRESS) {
1615                                 if(gesture->type==WM_GESTURE_CROSS_RECT && gesture->mode==0) {
1616                                         gesture->mode= 1;
1617                                         wm_gesture_tag_redraw(C);
1618                                 }
1619                         }
1620                         else {
1621                                 if(border_apply(C, op, event->type, gesture->event_type)) {
1622                                         wm_gesture_end(C, op);
1623                                         return OPERATOR_FINISHED;
1624                                 }
1625                                 wm_gesture_end(C, op);
1626                                 return OPERATOR_CANCELLED;
1627                         }
1628                         break;
1629                 case ESCKEY:
1630                         wm_gesture_end(C, op);
1631                         return OPERATOR_CANCELLED;
1632         }
1633         return OPERATOR_RUNNING_MODAL;
1634 }
1635
1636 /* **************** circle gesture *************** */
1637 /* works now only for selection or modal paint stuff, calls exec while hold mouse, exit on release */
1638
1639 int WM_gesture_circle_invoke(bContext *C, wmOperator *op, wmEvent *event)
1640 {
1641         op->customdata= WM_gesture_new(C, event, WM_GESTURE_CIRCLE);
1642         
1643         /* add modal handler */
1644         WM_event_add_modal_handler(C, op);
1645         
1646         wm_gesture_tag_redraw(C);
1647         
1648         return OPERATOR_RUNNING_MODAL;
1649 }
1650
1651 static void gesture_circle_apply(bContext *C, wmOperator *op)
1652 {
1653         wmGesture *gesture= op->customdata;
1654         rcti *rect= gesture->customdata;
1655         
1656         /* operator arguments and storage. */
1657         RNA_int_set(op->ptr, "x", rect->xmin);
1658         RNA_int_set(op->ptr, "y", rect->ymin);
1659         RNA_int_set(op->ptr, "radius", rect->xmax);
1660         
1661         if(op->type->exec)
1662                 op->type->exec(C, op);
1663 }
1664
1665 int WM_gesture_circle_modal(bContext *C, wmOperator *op, wmEvent *event)
1666 {
1667         wmGesture *gesture= op->customdata;
1668         rcti *rect= gesture->customdata;
1669         int sx, sy;
1670         
1671         switch(event->type) {
1672                 case MOUSEMOVE:
1673                         
1674                         wm_subwindow_getorigin(CTX_wm_window(C), gesture->swinid, &sx, &sy);
1675                         
1676                         rect->xmin= event->x - sx;
1677                         rect->ymin= event->y - sy;
1678                         
1679                         wm_gesture_tag_redraw(C);
1680                         
1681                         if(gesture->mode)
1682                                 gesture_circle_apply(C, op);
1683
1684                         break;
1685                 case WHEELUPMOUSE:
1686                         rect->xmax += 2 + rect->xmax/10;
1687                         wm_gesture_tag_redraw(C);
1688                         break;
1689                 case WHEELDOWNMOUSE:
1690                         rect->xmax -= 2 + rect->xmax/10;
1691                         if(rect->xmax < 1) rect->xmax= 1;
1692                         wm_gesture_tag_redraw(C);
1693                         break;
1694                 case LEFTMOUSE:
1695                 case MIDDLEMOUSE:
1696                 case RIGHTMOUSE:
1697                         if(event->val==KM_RELEASE) {    /* key release */
1698                                 wm_gesture_end(C, op);
1699                                 return OPERATOR_FINISHED;
1700                         }
1701                         else {
1702                                 if( RNA_struct_find_property(op->ptr, "event_type") )
1703                                         RNA_int_set(op->ptr, "event_type", event->type);
1704                                 
1705                                 /* apply first click */
1706                                 gesture_circle_apply(C, op);
1707                                 gesture->mode= 1;
1708                         }
1709                         break;
1710                 case ESCKEY:
1711                         wm_gesture_end(C, op);
1712                         return OPERATOR_CANCELLED;
1713         }
1714         return OPERATOR_RUNNING_MODAL;
1715 }
1716
1717 #if 0
1718 /* template to copy from */
1719 void WM_OT_circle_gesture(wmOperatorType *ot)
1720 {
1721         ot->name= "Circle Gesture";
1722         ot->idname= "WM_OT_circle_gesture";
1723         ot->description="Enter rotate mode with a circular gesture.";
1724         
1725         ot->invoke= WM_gesture_circle_invoke;
1726         ot->modal= WM_gesture_circle_modal;
1727         
1728         ot->poll= WM_operator_winactive;
1729         
1730         RNA_def_property(ot->srna, "x", PROP_INT, PROP_NONE);
1731         RNA_def_property(ot->srna, "y", PROP_INT, PROP_NONE);
1732         RNA_def_property(ot->srna, "radius", PROP_INT, PROP_NONE);
1733
1734 }
1735 #endif
1736
1737 /* **************** Tweak gesture *************** */
1738
1739 static void tweak_gesture_modal(bContext *C, wmEvent *event)
1740 {
1741         wmWindow *window= CTX_wm_window(C);
1742         wmGesture *gesture= window->tweak;
1743         rcti *rect= gesture->customdata;
1744         int sx, sy, val;
1745         
1746         switch(event->type) {
1747                 case MOUSEMOVE:
1748                         
1749                         wm_subwindow_getorigin(window, gesture->swinid, &sx, &sy);
1750                         
1751                         rect->xmax= event->x - sx;
1752                         rect->ymax= event->y - sy;
1753                         
1754                         if((val= wm_gesture_evaluate(C, gesture))) {
1755                                 wmEvent event;
1756
1757                                 event= *(window->eventstate);
1758                                 if(gesture->event_type==LEFTMOUSE)
1759                                         event.type= EVT_TWEAK_L;
1760                                 else if(gesture->event_type==RIGHTMOUSE)
1761                                         event.type= EVT_TWEAK_R;
1762                                 else
1763                                         event.type= EVT_TWEAK_M;
1764                                 event.val= val;
1765                                 /* mouse coords! */
1766                                 wm_event_add(window, &event);
1767                                 
1768                                 WM_gesture_end(C, gesture);     /* frees gesture itself, and unregisters from window */
1769                                 window->tweak= NULL;
1770                         }
1771                         
1772                         break;
1773                         
1774                 case LEFTMOUSE:
1775                 case RIGHTMOUSE:
1776                 case MIDDLEMOUSE:
1777                         if(gesture->event_type==event->type) {
1778                                 WM_gesture_end(C, gesture);
1779                                 window->tweak= NULL;
1780
1781                                 /* when tweak fails we should give the other keymap entries a chance */
1782                                 event->val= KM_RELEASE;
1783                         }
1784                         break;
1785                 default:
1786                         WM_gesture_end(C, gesture);
1787                         window->tweak= NULL;
1788         }
1789 }
1790
1791 /* standard tweak, called after window handlers passed on event */
1792 void wm_tweakevent_test(bContext *C, wmEvent *event, int action)
1793 {
1794         wmWindow *win= CTX_wm_window(C);
1795         
1796         if(win->tweak==NULL) {
1797                 if(CTX_wm_region(C)) {
1798                         if(event->val==KM_PRESS) { // pressed
1799                                 if( ELEM3(event->type, LEFTMOUSE, MIDDLEMOUSE, RIGHTMOUSE) )
1800                                         win->tweak= WM_gesture_new(C, event, WM_GESTURE_TWEAK);
1801                         }
1802                 }
1803         }
1804         else {
1805                 if(action==WM_HANDLER_BREAK) {
1806                         WM_gesture_end(C, win->tweak);
1807                         win->tweak= NULL;
1808                 }
1809                 else
1810                         tweak_gesture_modal(C, event);
1811         }
1812 }
1813
1814 /* *********************** lasso gesture ****************** */
1815
1816 int WM_gesture_lasso_invoke(bContext *C, wmOperator *op, wmEvent *event)
1817 {
1818         op->customdata= WM_gesture_new(C, event, WM_GESTURE_LASSO);
1819         
1820         /* add modal handler */
1821         WM_event_add_modal_handler(C, op);
1822         
1823         wm_gesture_tag_redraw(C);
1824         
1825         if( RNA_struct_find_property(op->ptr, "cursor") )
1826                 WM_cursor_modal(CTX_wm_window(C), RNA_int_get(op->ptr, "cursor"));
1827         
1828         return OPERATOR_RUNNING_MODAL;
1829 }
1830
1831 int WM_gesture_lines_invoke(bContext *C, wmOperator *op, wmEvent *event)
1832 {
1833         op->customdata= WM_gesture_new(C, event, WM_GESTURE_LINES);
1834         
1835         /* add modal handler */
1836         WM_event_add_modal_handler(C, op);
1837         
1838         wm_gesture_tag_redraw(C);
1839         
1840         if( RNA_struct_find_property(op->ptr, "cursor") )
1841                 WM_cursor_modal(CTX_wm_window(C), RNA_int_get(op->ptr, "cursor"));
1842         
1843         return OPERATOR_RUNNING_MODAL;
1844 }
1845
1846
1847 static void gesture_lasso_apply(bContext *C, wmOperator *op, int event_type)
1848 {
1849         wmGesture *gesture= op->customdata;
1850         PointerRNA itemptr;
1851         float loc[2];
1852         int i;
1853         short *lasso= gesture->customdata;
1854         
1855         /* operator storage as path. */
1856
1857         for(i=0; i<gesture->points; i++, lasso+=2) {
1858                 loc[0]= lasso[0];
1859                 loc[1]= lasso[1];
1860                 RNA_collection_add(op->ptr, "path", &itemptr);
1861                 RNA_float_set_array(&itemptr, "loc", loc);
1862         }
1863         
1864         wm_gesture_end(C, op);
1865                 
1866         if(op->type->exec)
1867                 op->type->exec(C, op);
1868         
1869 }
1870
1871 int WM_gesture_lasso_modal(bContext *C, wmOperator *op, wmEvent *event)
1872 {
1873         wmGesture *gesture= op->customdata;
1874         int sx, sy;
1875         
1876         switch(event->type) {
1877                 case MOUSEMOVE:
1878                         
1879                         wm_gesture_tag_redraw(C);
1880                         
1881                         wm_subwindow_getorigin(CTX_wm_window(C), gesture->swinid, &sx, &sy);
1882                         if(gesture->points < WM_LASSO_MAX_POINTS) {
1883                                 short *lasso= gesture->customdata;
1884                                 lasso += 2 * gesture->points;
1885                                 lasso[0] = event->x - sx;
1886                                 lasso[1] = event->y - sy;
1887                                 gesture->points++;
1888                         }
1889                         else {
1890                                 gesture_lasso_apply(C, op, event->type);
1891                                 return OPERATOR_FINISHED;
1892                         }
1893                         break;
1894                         
1895                 case LEFTMOUSE:
1896                 case MIDDLEMOUSE:
1897                 case RIGHTMOUSE:
1898                         if(event->val==KM_RELEASE) {    /* key release */
1899                                 gesture_lasso_apply(C, op, event->type);
1900                                 return OPERATOR_FINISHED;
1901                         }
1902                         break;
1903                 case ESCKEY:
1904                         wm_gesture_end(C, op);
1905                         return OPERATOR_CANCELLED;
1906         }
1907         return OPERATOR_RUNNING_MODAL;
1908 }
1909
1910 int WM_gesture_lines_modal(bContext *C, wmOperator *op, wmEvent *event)
1911 {
1912         return WM_gesture_lasso_modal(C, op, event);
1913 }
1914
1915 #if 0
1916 /* template to copy from */
1917
1918 static int gesture_lasso_exec(bContext *C, wmOperator *op)
1919 {
1920         RNA_BEGIN(op->ptr, itemptr, "path") {
1921                 float loc[2];
1922                 
1923                 RNA_float_get_array(&itemptr, "loc", loc);
1924                 printf("Location: %f %f\n", loc[0], loc[1]);
1925         }
1926         RNA_END;
1927         
1928         return OPERATOR_FINISHED;
1929 }
1930
1931 void WM_OT_lasso_gesture(wmOperatorType *ot)
1932 {
1933         PropertyRNA *prop;
1934         
1935         ot->name= "Lasso Gesture";
1936         ot->idname= "WM_OT_lasso_gesture";
1937         ot->description="Select objects within the lasso as you move the pointer.";
1938         
1939         ot->invoke= WM_gesture_lasso_invoke;
1940         ot->modal= WM_gesture_lasso_modal;
1941         ot->exec= gesture_lasso_exec;
1942         
1943         ot->poll= WM_operator_winactive;
1944         
1945         prop= RNA_def_property(ot->srna, "path", PROP_COLLECTION, PROP_NONE);
1946         RNA_def_property_struct_runtime(prop, &RNA_OperatorMousePath);
1947 }
1948 #endif
1949
1950 /* *********************** radial control ****************** */
1951
1952 const int WM_RADIAL_CONTROL_DISPLAY_SIZE = 200;
1953
1954 typedef struct wmRadialControl {
1955         int mode;
1956         float initial_value, value, max_value;
1957         int initial_mouse[2];
1958         void *cursor;
1959         GLuint tex;
1960 } wmRadialControl;
1961
1962 static void wm_radial_control_paint(bContext *C, int x, int y, void *customdata)
1963 {
1964         wmRadialControl *rc = (wmRadialControl*)customdata;
1965         ARegion *ar = CTX_wm_region(C);
1966         float r1=0.0f, r2=0.0f, r3=0.0f, angle=0.0f;
1967
1968         /* Keep cursor in the original place */
1969         x = rc->initial_mouse[0] - ar->winrct.xmin;
1970         y = rc->initial_mouse[1] - ar->winrct.ymin;
1971
1972         glPushMatrix();
1973         
1974         glTranslatef((float)x, (float)y, 0.0f);
1975
1976         if(rc->mode == WM_RADIALCONTROL_SIZE) {
1977                 r1= rc->value;
1978                 r2= rc->initial_value;
1979                 r3= r1;
1980         } else if(rc->mode == WM_RADIALCONTROL_STRENGTH) {
1981                 r1= (1 - rc->value) * WM_RADIAL_CONTROL_DISPLAY_SIZE;
1982                 r2= WM_RADIAL_CONTROL_DISPLAY_SIZE;
1983                 r3= WM_RADIAL_CONTROL_DISPLAY_SIZE;
1984         } else if(rc->mode == WM_RADIALCONTROL_ANGLE) {
1985                 r1= r2= WM_RADIAL_CONTROL_DISPLAY_SIZE;
1986                 r3= WM_RADIAL_CONTROL_DISPLAY_SIZE;
1987                 angle = rc->value;
1988         }
1989
1990         glColor4ub(255, 255, 255, 128);
1991         glEnable( GL_LINE_SMOOTH );
1992         glEnable(GL_BLEND);
1993
1994         if(rc->mode == WM_RADIALCONTROL_ANGLE)
1995                 fdrawline(0, 0, WM_RADIAL_CONTROL_DISPLAY_SIZE, 0);
1996
1997         if(rc->tex) {
1998                 const float str = rc->mode == WM_RADIALCONTROL_STRENGTH ? (rc->value + 0.5) : 1;
1999
2000                 if(rc->mode == WM_RADIALCONTROL_ANGLE) {
2001                         glRotatef(angle, 0, 0, 1);
2002                         fdrawline(0, 0, WM_RADIAL_CONTROL_DISPLAY_SIZE, 0);
2003                 }
2004
2005                 glBindTexture(GL_TEXTURE_2D, rc->tex);
2006
2007                 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
2008                 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
2009
2010                 glEnable(GL_TEXTURE_2D);
2011                 glBegin(GL_QUADS);
2012                 glColor4f(0,0,0, str);
2013                 glTexCoord2f(0,0);
2014                 glVertex2f(-r3, -r3);
2015                 glTexCoord2f(1,0);
2016                 glVertex2f(r3, -r3);
2017                 glTexCoord2f(1,1);
2018                 glVertex2f(r3, r3);
2019                 glTexCoord2f(0,1);
2020                 glVertex2f(-r3, r3);
2021                 glEnd();
2022                 glDisable(GL_TEXTURE_2D);
2023         }
2024
2025         glColor4ub(255, 255, 255, 128); 
2026         glutil_draw_lined_arc(0.0, M_PI*2.0, r1, 40);
2027         glutil_draw_lined_arc(0.0, M_PI*2.0, r2, 40);
2028         glDisable(GL_BLEND);
2029         glDisable( GL_LINE_SMOOTH );
2030         
2031         glPopMatrix();
2032 }
2033
2034 int WM_radial_control_modal(bContext *C, wmOperator *op, wmEvent *event)
2035 {
2036         wmRadialControl *rc = (wmRadialControl*)op->customdata;
2037         int mode, initial_mouse[2], delta[2];
2038         float dist;
2039         double new_value = RNA_float_get(op->ptr, "new_value");
2040         int ret = OPERATOR_RUNNING_MODAL;
2041
2042         mode = RNA_int_get(op->ptr, "mode");
2043         RNA_int_get_array(op->ptr, "initial_mouse", initial_mouse);
2044
2045         switch(event->type) {
2046         case MOUSEMOVE:
2047                 delta[0]= initial_mouse[0] - event->x;
2048                 delta[1]= initial_mouse[1] - event->y;
2049                 dist= sqrt(delta[0]*delta[0]+delta[1]*delta[1]);
2050
2051                 if(mode == WM_RADIALCONTROL_SIZE)
2052                         new_value = dist;
2053                 else if(mode == WM_RADIALCONTROL_STRENGTH) {
2054                         new_value = 1 - dist / WM_RADIAL_CONTROL_DISPLAY_SIZE;
2055                 } else if(mode == WM_RADIALCONTROL_ANGLE)
2056                         new_value = ((int)(atan2(delta[1], delta[0]) * (180.0 / M_PI)) + 180);
2057                 
2058                 if(event->ctrl) {
2059                         if(mode == WM_RADIALCONTROL_STRENGTH)
2060                                 new_value = ((int)(new_value * 100) / 10*10) / 100.0f;
2061                         else
2062                                 new_value = ((int)new_value + 5) / 10*10;
2063                 }
2064                 
2065                 break;
2066         case ESCKEY:
2067         case RIGHTMOUSE:
2068                 ret = OPERATOR_CANCELLED;
2069                 break;
2070         case LEFTMOUSE:
2071         case PADENTER:
2072                 op->type->exec(C, op);
2073                 ret = OPERATOR_FINISHED;
2074                 break;
2075         }
2076
2077         /* Clamp */
2078         if(new_value > rc->max_value)
2079                 new_value = rc->max_value;
2080         else if(new_value < 0)
2081                 new_value = 0;
2082
2083         /* Update paint data */
2084         rc->value = new_value;
2085
2086         RNA_float_set(op->ptr, "new_value", new_value);
2087
2088         if(ret != OPERATOR_RUNNING_MODAL) {
2089                 WM_paint_cursor_end(CTX_wm_manager(C), rc->cursor);
2090                 MEM_freeN(rc);
2091         }
2092         
2093         ED_region_tag_redraw(CTX_wm_region(C));
2094
2095         return ret;
2096 }
2097
2098 /* Expects the operator customdata to be an ImBuf (or NULL) */
2099 int WM_radial_control_invoke(bContext *C, wmOperator *op, wmEvent *event)
2100 {
2101         wmRadialControl *rc = MEM_callocN(sizeof(wmRadialControl), "radial control");
2102         int mode = RNA_int_get(op->ptr, "mode");
2103         float initial_value = RNA_float_get(op->ptr, "initial_value");
2104         int mouse[2] = {event->x, event->y};
2105
2106         if(mode == WM_RADIALCONTROL_SIZE) {
2107                 rc->max_value = 200;
2108                 mouse[0]-= initial_value;
2109         }
2110         else if(mode == WM_RADIALCONTROL_STRENGTH) {
2111                 rc->max_value = 1;
2112                 mouse[0]-= WM_RADIAL_CONTROL_DISPLAY_SIZE * (1 - initial_value);
2113         }
2114         else if(mode == WM_RADIALCONTROL_ANGLE) {
2115                 rc->max_value = 360;
2116                 mouse[0]-= WM_RADIAL_CONTROL_DISPLAY_SIZE * cos(initial_value);
2117                 mouse[1]-= WM_RADIAL_CONTROL_DISPLAY_SIZE * sin(initial_value);
2118                 initial_value *= 180.0f/M_PI;
2119         }
2120
2121         if(op->customdata) {
2122                 ImBuf *im = (ImBuf*)op->customdata;
2123                 /* Build GL texture */
2124                 glGenTextures(1, &rc->tex);
2125                 glBindTexture(GL_TEXTURE_2D, rc->tex);
2126                 glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, im->x, im->y, 0, GL_ALPHA, GL_FLOAT, im->rect_float);
2127                 MEM_freeN(im->rect_float);
2128                 MEM_freeN(im);
2129         }
2130
2131         RNA_int_set_array(op->ptr, "initial_mouse", mouse);
2132         RNA_float_set(op->ptr, "new_value", initial_value);
2133                 
2134         op->customdata = rc;
2135         rc->mode = mode;
2136         rc->initial_value = initial_value;
2137         rc->initial_mouse[0] = mouse[0];
2138         rc->initial_mouse[1] = mouse[1];
2139         rc->cursor = WM_paint_cursor_activate(CTX_wm_manager(C), op->type->poll,
2140                                               wm_radial_control_paint, op->customdata);
2141
2142         /* add modal handler */
2143         WM_event_add_modal_handler(C, op);
2144         
2145         WM_radial_control_modal(C, op, event);
2146         
2147         return OPERATOR_RUNNING_MODAL;
2148 }
2149
2150 /* Gets a descriptive string of the operation */
2151 void WM_radial_control_string(wmOperator *op, char str[], int maxlen)
2152 {
2153         int mode = RNA_int_get(op->ptr, "mode");
2154         float v = RNA_float_get(op->ptr, "new_value");
2155
2156         if(mode == WM_RADIALCONTROL_SIZE)
2157                 sprintf(str, "Size: %d", (int)v);
2158         else if(mode == WM_RADIALCONTROL_STRENGTH)
2159                 sprintf(str, "Strength: %d", (int)v);
2160         else if(mode == WM_RADIALCONTROL_ANGLE)
2161                 sprintf(str, "Angle: %d", (int)(v * 180.0f/M_PI));
2162 }
2163
2164 /** Important: this doesn't define an actual operator, it
2165     just sets up the common parts of the radial control op. **/
2166 void WM_OT_radial_control_partial(wmOperatorType *ot)
2167 {
2168         static EnumPropertyItem radial_mode_items[] = {
2169                 {WM_RADIALCONTROL_SIZE, "SIZE", 0, "Size", ""},
2170                 {WM_RADIALCONTROL_STRENGTH, "STRENGTH", 0, "Strength", ""},
2171                 {WM_RADIALCONTROL_ANGLE, "ANGLE", 0, "Angle", ""},
2172                 {0, NULL, 0, NULL, NULL}};
2173
2174         /* Should be set in custom invoke() */
2175         RNA_def_float(ot->srna, "initial_value", 0, 0, FLT_MAX, "Initial Value", "", 0, FLT_MAX);
2176
2177         /* Set internally, should be used in custom exec() to get final value */
2178         RNA_def_float(ot->srna, "new_value", 0, 0, FLT_MAX, "New Value", "", 0, FLT_MAX);
2179
2180         /* Should be set before calling operator */
2181         RNA_def_enum(ot->srna, "mode", radial_mode_items, 0, "Mode", "");
2182
2183         /* Internal */
2184         RNA_def_int_vector(ot->srna, "initial_mouse", 2, NULL, INT_MIN, INT_MAX, "initial_mouse", "", INT_MIN, INT_MAX);
2185 }
2186
2187
2188 /* ************************** timer for testing ***************** */
2189
2190 /* uses no type defines, fully local testing function anyway... ;) */
2191
2192 static int redraw_timer_exec(bContext *C, wmOperator *op)
2193 {
2194         ARegion *ar= CTX_wm_region(C);
2195         double stime= PIL_check_seconds_timer();
2196         int type = RNA_int_get(op->ptr, "type");
2197         int iter = RNA_int_get(op->ptr, "iterations");
2198         int a;
2199         float time;
2200         char *infostr= "";
2201         
2202         WM_cursor_wait(1);
2203
2204         for(a=0; a<iter; a++) {
2205                 if (type==0) {
2206                         ED_region_do_draw(C, ar);
2207                 } 
2208                 else if (type==1) {
2209                         wmWindow *win= CTX_wm_window(C);
2210                         
2211                         ED_region_tag_redraw(ar);
2212                         wm_draw_update(C);
2213                         
2214                         CTX_wm_window_set(C, win);      /* XXX context manipulation warning! */
2215                 }
2216                 else if (type==2) {
2217                         wmWindow *win= CTX_wm_window(C);
2218                         ScrArea *sa;
2219                         
2220                         ScrArea *sa_back= CTX_wm_area(C);
2221                         ARegion *ar_back= CTX_wm_region(C);
2222
2223                         for(sa= CTX_wm_screen(C)->areabase.first; sa; sa= sa->next) {
2224                                 ARegion *ar_iter;
2225                                 CTX_wm_area_set(C, sa);
2226
2227                                 for(ar_iter= sa->regionbase.first; ar_iter; ar_iter= ar_iter->next) {
2228                                         CTX_wm_region_set(C, ar_iter);
2229                                         ED_region_do_draw(C, ar_iter);
2230                                 }
2231                         }
2232
2233                         CTX_wm_window_set(C, win);      /* XXX context manipulation warning! */
2234
2235                         CTX_wm_area_set(C, sa_back);
2236                         CTX_wm_region_set(C, ar_back);
2237                 }
2238                 else if (type==3) {
2239                         wmWindow *win= CTX_wm_window(C);
2240                         ScrArea *sa;
2241
2242                         for(sa= CTX_wm_screen(C)->areabase.first; sa; sa= sa->next)
2243                                 ED_area_tag_redraw(sa);
2244                         wm_draw_update(C);
2245                         
2246                         CTX_wm_window_set(C, win);      /* XXX context manipulation warning! */
2247                 }
2248                 else if (type==4) {
2249                         Scene *scene= CTX_data_scene(C);
2250                         
2251                         if(a & 1) scene->r.cfra--;
2252                         else scene->r.cfra++;
2253                         scene_update_for_newframe(scene, scene->lay);
2254                 }
2255                 else {
2256                         ED_undo_pop(C);
2257                         ED_undo_redo(C);
2258                 }
2259         }
2260         
2261         time= ((PIL_check_seconds_timer()-stime)*1000);
2262         
2263         if(type==0) infostr= "Draw Region";
2264         if(type==1) infostr= "Draw Region and Swap";
2265         if(type==2) infostr= "Draw Window";
2266         if(type==3) infostr= "Draw Window and Swap";
2267         if(type==4) infostr= "Animation Steps";
2268         if(type==5) infostr= "Undo/Redo";
2269         
2270         WM_cursor_wait(0);
2271         
2272         BKE_reportf(op->reports, RPT_INFO, "%d x %s: %.2f ms,  average: %.4f", iter, infostr, time, time/iter);
2273         
2274         return OPERATOR_FINISHED;
2275 }
2276
2277 static void WM_OT_redraw_timer(wmOperatorType *ot)
2278 {
2279         static EnumPropertyItem prop_type_items[] = {
2280         {0, "DRAW", 0, "Draw Region", ""},
2281         {1, "DRAW_SWAP", 0, "Draw Region + Swap", ""},
2282         {2, "DRAW_WIN", 0, "Draw Window", ""},
2283         {3, "DRAW_WIN_SWAP", 0, "Draw Window + Swap", ""},
2284         {4, "ANIM_STEP", 0, "Anim Step", ""},
2285         {5, "UNDO", 0, "Undo/Redo", ""},
2286         {0, NULL, 0, NULL, NULL}};
2287         
2288         ot->name= "Redraw Timer";
2289         ot->idname= "WM_OT_redraw_timer";
2290         ot->description="Simple redraw timer to test the speed of updating the interface.";
2291         
2292         ot->invoke= WM_menu_invoke;
2293         ot->exec= redraw_timer_exec;
2294         ot->poll= WM_operator_winactive;
2295         
2296         RNA_def_enum(ot->srna, "type", prop_type_items, 0, "Type", "");
2297         RNA_def_int(ot->srna, "iterations", 10, 1,INT_MAX, "Iterations", "Number of times to redraw", 1,1000);
2298
2299 }
2300
2301 /* ************************** memory statistics for testing ***************** */
2302
2303 static int memory_statistics_exec(bContext *C, wmOperator *op)
2304 {
2305         MEM_printmemlist_stats();
2306         return OPERATOR_FINISHED;
2307 }
2308
2309 static void WM_OT_memory_statistics(wmOperatorType *ot)
2310 {
2311         ot->name= "Memory Statistics";
2312         ot->idname= "WM_OT_memory_statistics";
2313         ot->description= "Print memory statistics to the console.";
2314         
2315         ot->exec= memory_statistics_exec;
2316 }
2317
2318 /* ******************************************************* */
2319  
2320 /* called on initialize WM_exit() */
2321 void wm_operatortype_free(void)
2322 {
2323         wmOperatorType *ot;
2324         
2325         for(ot= global_ops.first; ot; ot= ot->next)
2326                 if(ot->macro.first)
2327                         wm_operatortype_free_macro(ot);
2328         
2329         BLI_freelistN(&global_ops);
2330 }
2331
2332 /* called on initialize WM_init() */
2333 void wm_operatortype_init(void)
2334 {
2335         WM_operatortype_append(WM_OT_window_duplicate);
2336         WM_operatortype_append(WM_OT_read_homefile);
2337         WM_operatortype_append(WM_OT_save_homefile);
2338         WM_operatortype_append(WM_OT_window_fullscreen_toggle);
2339         WM_operatortype_append(WM_OT_exit_blender);
2340         WM_operatortype_append(WM_OT_open_recentfile);
2341         WM_operatortype_append(WM_OT_open_mainfile);
2342         WM_operatortype_append(WM_OT_link_append);
2343         WM_operatortype_append(WM_OT_recover_last_session);
2344         WM_operatortype_append(WM_OT_recover_auto_save);
2345         WM_operatortype_append(WM_OT_save_as_mainfile);
2346         WM_operatortype_append(WM_OT_save_mainfile);
2347         WM_operatortype_append(WM_OT_redraw_timer);
2348         WM_operatortype_append(WM_OT_memory_statistics);
2349         WM_operatortype_append(WM_OT_debug_menu);
2350         WM_operatortype_append(WM_OT_search_menu);
2351
2352 #ifdef WITH_COLLADA
2353         /* XXX: move these */
2354         WM_operatortype_append(WM_OT_collada_export);
2355         WM_operatortype_append(WM_OT_collada_import);
2356 #endif
2357
2358         WM_operatortype_append(WM_OT_call_menu);
2359 }
2360
2361 /* default keymap for windows and screens, only call once per WM */
2362 void wm_window_keymap(wmKeyConfig *keyconf)
2363 {
2364         wmKeyMap *keymap= WM_keymap_find(keyconf, "Window", 0, 0);
2365         wmKeyMapItem *km;
2366         
2367         /* note, this doesn't replace existing keymap items */
2368         WM_keymap_verify_item(keymap, "WM_OT_window_duplicate", WKEY, KM_PRESS, KM_CTRL|KM_ALT, 0);
2369         #ifdef __APPLE__
2370         WM_keymap_add_item(keymap, "WM_OT_read_homefile", NKEY, KM_PRESS, KM_OSKEY, 0);
2371         WM_keymap_add_item(keymap, "WM_OT_open_recentfile", OKEY, KM_PRESS, KM_SHIFT|KM_OSKEY, 0);
2372         WM_keymap_add_item(keymap, "WM_OT_open_mainfile", OKEY, KM_PRESS, KM_OSKEY, 0);
2373         WM_keymap_add_item(keymap, "WM_OT_save_mainfile", SKEY, KM_PRESS, KM_OSKEY, 0);
2374         WM_keymap_add_item(keymap, "WM_OT_save_as_mainfile", SKEY, KM_PRESS, KM_SHIFT|KM_OSKEY, 0);
2375         WM_keymap_add_item(keymap, "WM_OT_exit_blender", QKEY, KM_PRESS, KM_OSKEY, 0);
2376         #endif
2377         WM_keymap_add_item(keymap, "WM_OT_read_homefile", NKEY, KM_PRESS, KM_CTRL, 0);
2378         WM_keymap_add_item(keymap, "WM_OT_save_homefile", UKEY, KM_PRESS, KM_CTRL, 0); 
2379         WM_keymap_add_item(keymap, "WM_OT_open_recentfile", OKEY, KM_PRESS, KM_SHIFT|KM_CTRL, 0);
2380         WM_keymap_add_item(keymap, "WM_OT_open_mainfile", OKEY, KM_PRESS, KM_CTRL, 0);
2381         WM_keymap_add_item(keymap, "WM_OT_open_mainfile", F1KEY, KM_PRESS, 0, 0);
2382         WM_keymap_add_item(keymap, "WM_OT_link_append", OKEY, KM_PRESS, KM_CTRL|KM_ALT, 0);
2383         WM_keymap_add_item(keymap, "WM_OT_link_append", F1KEY, KM_PRESS, KM_SHIFT, 0);
2384         WM_keymap_add_item(keymap, "WM_OT_save_mainfile", SKEY, KM_PRESS, KM_CTRL, 0);
2385         WM_keymap_add_item(keymap, "WM_OT_save_mainfile", WKEY, KM_PRESS, KM_CTRL, 0);
2386         WM_keymap_add_item(keymap, "WM_OT_save_as_mainfile", SKEY, KM_PRESS, KM_SHIFT|KM_CTRL, 0);
2387         WM_keymap_add_item(keymap, "WM_OT_save_as_mainfile", F2KEY, KM_PRESS, 0, 0);
2388
2389         WM_keymap_verify_item(keymap, "WM_OT_window_fullscreen_toggle", F11KEY, KM_PRESS, KM_ALT, 0);
2390         WM_keymap_add_item(keymap, "WM_OT_exit_blender", QKEY, KM_PRESS, KM_CTRL, 0);
2391
2392         /* debug/testing */
2393         WM_keymap_verify_item(keymap, "WM_OT_redraw_timer", TKEY, KM_PRESS, KM_ALT|KM_CTRL, 0);
2394         WM_keymap_verify_item(keymap, "WM_OT_debug_menu", DKEY, KM_PRESS, KM_ALT|KM_CTRL, 0);
2395         WM_keymap_verify_item(keymap, "WM_OT_search_menu", SPACEKEY, KM_PRESS, 0, 0);
2396         
2397         /* Space switching */
2398
2399
2400         km = WM_keymap_add_item(keymap, "WM_OT_context_set_enum", F2KEY, KM_PRESS, KM_SHIFT, 0); /* new in 2.5x, was DXF export */
2401         RNA_string_set(km->ptr, "path", "area.type");
2402         RNA_string_set(km->ptr, "value", "LOGIC_EDITOR");
2403
2404         km = WM_keymap_add_item(keymap, "WM_OT_context_set_enum", F3KEY, KM_PRESS, KM_SHIFT, 0);
2405         RNA_string_set(km->ptr, "path", "area.type");
2406         RNA_string_set(km->ptr, "value", "NODE_EDITOR");
2407
2408         km = WM_keymap_add_item(keymap, "WM_OT_context_set_enum", F4KEY, KM_PRESS, KM_SHIFT, 0); /* new in 2.5x, was data browser */
2409         RNA_string_set(km->ptr, "path", "area.type");
2410         RNA_string_set(km->ptr, "value", "CONSOLE");
2411
2412         km = WM_keymap_add_item(keymap, "WM_OT_context_set_enum", F5KEY, KM_PRESS, KM_SHIFT, 0);
2413         RNA_string_set(km->ptr, "path", "area.type");
2414         RNA_string_set(km->ptr, "value", "VIEW_3D");
2415
2416         km = WM_keymap_add_item(keymap, "WM_OT_context_set_enum", F6KEY, KM_PRESS, KM_SHIFT, 0);
2417         RNA_string_set(km->ptr, "path", "area.type");
2418         RNA_string_set(km->ptr, "value", "GRAPH_EDITOR");
2419
2420         km = WM_keymap_add_item(keymap, "WM_OT_context_set_enum", F7KEY, KM_PRESS, KM_SHIFT, 0);
2421         RNA_string_set(km->ptr, "path", "area.type");
2422         RNA_string_set(km->ptr, "value", "PROPERTIES");
2423
2424         km = WM_keymap_add_item(keymap, "WM_OT_context_set_enum", F8KEY, KM_PRESS, KM_SHIFT, 0);
2425         RNA_string_set(km->ptr, "path", "area.type");
2426         RNA_string_set(km->ptr, "value", "SEQUENCE_EDITOR");
2427
2428         km = WM_keymap_add_item(keymap, "WM_OT_context_set_enum", F9KEY, KM_PRESS, KM_SHIFT, 0);
2429         RNA_string_set(km->ptr, "path", "area.type");
2430         RNA_string_set(km->ptr, "value", "OUTLINER");
2431
2432         km = WM_keymap_add_item(keymap, "WM_OT_context_set_enum", F9KEY, KM_PRESS, KM_SHIFT, 0);
2433         RNA_string_set(km->ptr, "path", "area.type");
2434         RNA_string_set(km->ptr, "value", "OUTLINER");
2435
2436         km = WM_keymap_add_item(keymap, "WM_OT_context_set_enum", F10KEY, KM_PRESS, KM_SHIFT, 0);
2437         RNA_string_set(km->ptr, "path", "area.type");
2438         RNA_string_set(km->ptr, "value", "IMAGE_EDITOR");
2439
2440         km = WM_keymap_add_item(keymap, "WM_OT_context_set_enum", F11KEY, KM_PRESS, KM_SHIFT, 0);
2441         RNA_string_set(km->ptr, "path", "area.type");
2442         RNA_string_set(km->ptr, "value", "TEXT_EDITOR");
2443
2444         km = WM_keymap_add_item(keymap, "WM_OT_context_set_enum", F12KEY, KM_PRESS, KM_SHIFT, 0);
2445         RNA_string_set(km->ptr, "path", "area.type");
2446         RNA_string_set(km->ptr, "value", "DOPESHEET_EDITOR");
2447 }
2448