Script to test import operators, so a single command can execute an operator on all...
[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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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 #include <string.h>
31 #include <ctype.h>
32 #include <stdio.h>
33 #include <stddef.h>
34 #include <assert.h>
35
36 #ifdef WIN32
37 #include "BLI_winstuff.h"
38 #include <windows.h>  
39
40 #include <io.h>
41 #endif
42
43 #include "MEM_guardedalloc.h"
44
45 #include "DNA_ID.h"
46 #include "DNA_object_types.h"
47 #include "DNA_screen_types.h"
48 #include "DNA_scene_types.h"
49 #include "DNA_userdef_types.h"
50 #include "DNA_windowmanager_types.h"
51
52 #include "BLF_api.h"
53
54 #include "PIL_time.h"
55
56 #include "BLI_blenlib.h"
57 #include "BLI_dynstr.h" /*for WM_operator_pystring */
58 #include "BLI_math.h"
59 #include "BLI_utildefines.h"
60
61 #include "BLO_readfile.h"
62
63 #include "BKE_blender.h"
64 #include "BKE_context.h"
65 #include "BKE_depsgraph.h"
66 #include "BKE_idprop.h"
67 #include "BKE_library.h"
68 #include "BKE_global.h"
69 #include "BKE_main.h"
70 #include "BKE_report.h"
71 #include "BKE_scene.h"
72 #include "BKE_screen.h" /* BKE_ST_MAXNAME */
73
74 #include "BKE_idcode.h"
75
76 #include "BIF_gl.h"
77 #include "BIF_glutil.h" /* for paint cursor */
78
79 #include "IMB_imbuf_types.h"
80
81 #include "ED_screen.h"
82 #include "ED_util.h"
83
84 #include "RNA_access.h"
85 #include "RNA_define.h"
86
87 #include "UI_interface.h"
88 #include "UI_resources.h"
89
90 #include "WM_api.h"
91 #include "WM_types.h"
92
93 #include "wm.h"
94 #include "wm_draw.h"
95 #include "wm_event_system.h"
96 #include "wm_event_types.h"
97 #include "wm_subwindow.h"
98 #include "wm_window.h"
99
100 static ListBase global_ops= {NULL, NULL};
101
102 /* ************ operator API, exported ********** */
103
104
105 wmOperatorType *WM_operatortype_find(const char *idname, int quiet)
106 {
107         wmOperatorType *ot;
108         
109         char idname_bl[OP_MAX_TYPENAME]; // XXX, needed to support python style names without the _OT_ syntax
110         WM_operator_bl_idname(idname_bl, idname);
111
112         if (idname_bl[0]) {
113                 ot= (wmOperatorType *)BLI_findstring_ptr(&global_ops, idname_bl, offsetof(wmOperatorType, idname));
114                 if(ot) {
115                         return ot;
116                 }
117         }
118         
119         if(!quiet)
120                 printf("search for unknown operator %s, %s\n", idname_bl, idname);
121         
122         return NULL;
123 }
124
125 wmOperatorType *WM_operatortype_first(void)
126 {
127         return global_ops.first;
128 }
129
130 /* all ops in 1 list (for time being... needs evaluation later) */
131 void WM_operatortype_append(void (*opfunc)(wmOperatorType*))
132 {
133         wmOperatorType *ot;
134         
135         ot= MEM_callocN(sizeof(wmOperatorType), "operatortype");
136         ot->srna= RNA_def_struct(&BLENDER_RNA, "", "OperatorProperties");
137         opfunc(ot);
138
139         if(ot->name==NULL) {
140                 static char dummy_name[] = "Dummy Name";
141                 fprintf(stderr, "ERROR: Operator %s has no name property!\n", ot->idname);
142                 ot->name= dummy_name;
143         }
144
145         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.
146         RNA_def_struct_identifier(ot->srna, ot->idname);
147         BLI_addtail(&global_ops, ot);
148 }
149
150 void WM_operatortype_append_ptr(void (*opfunc)(wmOperatorType*, void*), void *userdata)
151 {
152         wmOperatorType *ot;
153
154         ot= MEM_callocN(sizeof(wmOperatorType), "operatortype");
155         ot->srna= RNA_def_struct(&BLENDER_RNA, "", "OperatorProperties");
156         opfunc(ot, userdata);
157         RNA_def_struct_ui_text(ot->srna, ot->name, ot->description ? ot->description:"(undocumented operator)");
158         RNA_def_struct_identifier(ot->srna, ot->idname);
159         BLI_addtail(&global_ops, ot);
160 }
161
162 /* ********************* macro operator ******************** */
163
164 typedef struct {
165         int retval;
166 } MacroData;
167
168 static void wm_macro_start(wmOperator *op)
169 {
170         if (op->customdata == NULL) {
171                 op->customdata = MEM_callocN(sizeof(MacroData), "MacroData");
172         }
173 }
174
175 static int wm_macro_end(wmOperator *op, int retval)
176 {
177         if (retval & OPERATOR_CANCELLED) {
178                 MacroData *md = op->customdata;
179
180                 if (md->retval & OPERATOR_FINISHED) {
181                         retval |= OPERATOR_FINISHED;
182                         retval &= ~OPERATOR_CANCELLED;
183                 }
184         }
185
186         /* if modal is ending, free custom data */
187         if (retval & (OPERATOR_FINISHED|OPERATOR_CANCELLED)) {
188                 if (op->customdata) {
189                         MEM_freeN(op->customdata);
190                         op->customdata = NULL;
191                 }
192         }
193
194         return retval;
195 }
196
197 /* macro exec only runs exec calls */
198 static int wm_macro_exec(bContext *C, wmOperator *op)
199 {
200         wmOperator *opm;
201         int retval= OPERATOR_FINISHED;
202         
203         wm_macro_start(op);
204
205         for(opm= op->macro.first; opm; opm= opm->next) {
206                 
207                 if(opm->type->exec) {
208                         retval= opm->type->exec(C, opm);
209                 
210                         if (retval & OPERATOR_FINISHED) {
211                                 MacroData *md = op->customdata;
212                                 md->retval = OPERATOR_FINISHED; /* keep in mind that at least one operator finished */
213                         } else {
214                                 break; /* operator didn't finish, end macro */
215                         }
216                 }
217         }
218         
219         return wm_macro_end(op, retval);
220 }
221
222 int wm_macro_invoke_internal(bContext *C, wmOperator *op, wmEvent *event, wmOperator *opm)
223 {
224         int retval= OPERATOR_FINISHED;
225
226         /* start from operator received as argument */
227         for( ; opm; opm= opm->next) {
228                 if(opm->type->invoke)
229                         retval= opm->type->invoke(C, opm, event);
230                 else if(opm->type->exec)
231                         retval= opm->type->exec(C, opm);
232
233                 BLI_movelisttolist(&op->reports->list, &opm->reports->list);
234                 
235                 if (retval & OPERATOR_FINISHED) {
236                         MacroData *md = op->customdata;
237                         md->retval = OPERATOR_FINISHED; /* keep in mind that at least one operator finished */
238                 } else {
239                         break; /* operator didn't finish, end macro */
240                 }
241         }
242
243         return wm_macro_end(op, retval);
244 }
245
246 static int wm_macro_invoke(bContext *C, wmOperator *op, wmEvent *event)
247 {
248         wm_macro_start(op);
249         return wm_macro_invoke_internal(C, op, event, op->macro.first);
250 }
251
252 static int wm_macro_modal(bContext *C, wmOperator *op, wmEvent *event)
253 {
254         wmOperator *opm = op->opm;
255         int retval= OPERATOR_FINISHED;
256         
257         if(opm==NULL)
258                 printf("macro error, calling NULL modal()\n");
259         else {
260                 retval = opm->type->modal(C, opm, event);
261
262                 /* if this one is done but it's not the last operator in the macro */
263                 if ((retval & OPERATOR_FINISHED) && opm->next) {
264                         MacroData *md = op->customdata;
265
266                         md->retval = OPERATOR_FINISHED; /* keep in mind that at least one operator finished */
267
268                         retval = wm_macro_invoke_internal(C, op, event, opm->next);
269
270                         /* if new operator is modal and also added its own handler */
271                         if (retval & OPERATOR_RUNNING_MODAL && op->opm != opm) {
272                                 wmWindow *win = CTX_wm_window(C);
273                                 wmEventHandler *handler = NULL;
274
275                                 for (handler = win->modalhandlers.first; handler; handler = handler->next) {
276                                         /* first handler in list is the new one */
277                                         if (handler->op == op)
278                                                 break;
279                                 }
280
281                                 if (handler) {
282                                         BLI_remlink(&win->modalhandlers, handler);
283                                         wm_event_free_handler(handler);
284                                 }
285
286                                 /* if operator is blocking, grab cursor
287                                  * This may end up grabbing twice, but we don't care.
288                                  * */
289                                 if(op->opm->type->flag & OPTYPE_BLOCKING) {
290                                         int bounds[4] = {-1,-1,-1,-1};
291                                         int wrap = (U.uiflag & USER_CONTINUOUS_MOUSE) && ((op->opm->flag & OP_GRAB_POINTER) || (op->opm->type->flag & OPTYPE_GRAB_POINTER));
292
293                                         if(wrap) {
294                                                 ARegion *ar= CTX_wm_region(C);
295                                                 if(ar) {
296                                                         bounds[0]= ar->winrct.xmin;
297                                                         bounds[1]= ar->winrct.ymax;
298                                                         bounds[2]= ar->winrct.xmax;
299                                                         bounds[3]= ar->winrct.ymin;
300                                                 }
301                                         }
302
303                                         WM_cursor_grab(CTX_wm_window(C), wrap, FALSE, bounds);
304                                 }
305                         }
306                 }
307         }
308
309         return wm_macro_end(op, retval);
310 }
311
312 static int wm_macro_cancel(bContext *C, wmOperator *op)
313 {
314         /* call cancel on the current modal operator, if any */
315         if (op->opm && op->opm->type->cancel) {
316                 op->opm->type->cancel(C, op->opm);
317         }
318
319         return wm_macro_end(op, OPERATOR_CANCELLED);
320 }
321
322 /* Names have to be static for now */
323 wmOperatorType *WM_operatortype_append_macro(const char *idname, const char *name, int flag)
324 {
325         wmOperatorType *ot;
326         
327         if(WM_operatortype_find(idname, TRUE)) {
328                 printf("Macro error: operator %s exists\n", idname);
329                 return NULL;
330         }
331         
332         ot= MEM_callocN(sizeof(wmOperatorType), "operatortype");
333         ot->srna= RNA_def_struct(&BLENDER_RNA, "", "OperatorProperties");
334         
335         ot->idname= idname;
336         ot->name= name;
337         ot->flag= OPTYPE_MACRO|flag;
338         
339         ot->exec= wm_macro_exec;
340         ot->invoke= wm_macro_invoke;
341         ot->modal= wm_macro_modal;
342         ot->cancel= wm_macro_cancel;
343         ot->poll= NULL;
344
345         if(!ot->description)
346                 ot->description= "(undocumented operator)";
347         
348         RNA_def_struct_ui_text(ot->srna, ot->name, ot->description); // XXX All ops should have a description but for now allow them not to.
349         RNA_def_struct_identifier(ot->srna, ot->idname);
350
351         BLI_addtail(&global_ops, ot);
352
353         return ot;
354 }
355
356 void WM_operatortype_append_macro_ptr(void (*opfunc)(wmOperatorType*, void*), void *userdata)
357 {
358         wmOperatorType *ot;
359
360         ot= MEM_callocN(sizeof(wmOperatorType), "operatortype");
361         ot->srna= RNA_def_struct(&BLENDER_RNA, "", "OperatorProperties");
362
363         ot->flag= OPTYPE_MACRO;
364         ot->exec= wm_macro_exec;
365         ot->invoke= wm_macro_invoke;
366         ot->modal= wm_macro_modal;
367         ot->cancel= wm_macro_cancel;
368         ot->poll= NULL;
369
370         if(!ot->description)
371                 ot->description= "(undocumented operator)";
372
373         opfunc(ot, userdata);
374
375         RNA_def_struct_ui_text(ot->srna, ot->name, ot->description);
376         RNA_def_struct_identifier(ot->srna, ot->idname);
377
378         BLI_addtail(&global_ops, ot);
379 }
380
381 wmOperatorTypeMacro *WM_operatortype_macro_define(wmOperatorType *ot, const char *idname)
382 {
383         wmOperatorTypeMacro *otmacro= MEM_callocN(sizeof(wmOperatorTypeMacro), "wmOperatorTypeMacro");
384         
385         BLI_strncpy(otmacro->idname, idname, OP_MAX_TYPENAME);
386
387         /* do this on first use, since operatordefinitions might have been not done yet */
388         WM_operator_properties_alloc(&(otmacro->ptr), &(otmacro->properties), idname);
389         WM_operator_properties_sanitize(otmacro->ptr, 1);
390         
391         BLI_addtail(&ot->macro, otmacro);
392
393         {
394                 wmOperatorType *otsub = WM_operatortype_find(idname, 0);
395                 RNA_def_pointer_runtime(ot->srna, otsub->idname, otsub->srna,
396                 otsub->name, otsub->description);
397         }
398         
399         return otmacro;
400 }
401
402 static void wm_operatortype_free_macro(wmOperatorType *ot)
403 {
404         wmOperatorTypeMacro *otmacro;
405         
406         for(otmacro= ot->macro.first; otmacro; otmacro= otmacro->next) {
407                 if(otmacro->ptr) {
408                         WM_operator_properties_free(otmacro->ptr);
409                         MEM_freeN(otmacro->ptr);
410                 }
411         }
412         BLI_freelistN(&ot->macro);
413 }
414
415
416 int WM_operatortype_remove(const char *idname)
417 {
418         wmOperatorType *ot = WM_operatortype_find(idname, 0);
419
420         if (ot==NULL)
421                 return 0;
422         
423         BLI_remlink(&global_ops, ot);
424         RNA_struct_free(&BLENDER_RNA, ot->srna);
425         
426         if(ot->macro.first)
427                 wm_operatortype_free_macro(ot);
428         
429         MEM_freeN(ot);
430
431         return 1;
432 }
433
434 /* SOME_OT_op -> some.op */
435 void WM_operator_py_idname(char *to, const char *from)
436 {
437         char *sep= strstr(from, "_OT_");
438         if(sep) {
439                 int i, ofs= (sep-from);
440
441                 for(i=0; i<ofs; i++)
442                         to[i]= tolower(from[i]);
443
444                 to[ofs] = '.';
445                 BLI_strncpy(to+(ofs+1), sep+4, OP_MAX_TYPENAME);
446         }
447         else {
448                 /* should not happen but support just incase */
449                 BLI_strncpy(to, from, OP_MAX_TYPENAME);
450         }
451 }
452
453 /* some.op -> SOME_OT_op */
454 void WM_operator_bl_idname(char *to, const char *from)
455 {
456         if (from) {
457                 char *sep= strchr(from, '.');
458
459                 if(sep) {
460                         int i, ofs= (sep-from);
461
462                         for(i=0; i<ofs; i++)
463                                 to[i]= toupper(from[i]);
464
465                         BLI_strncpy(to+ofs, "_OT_", OP_MAX_TYPENAME);
466                         BLI_strncpy(to+(ofs+4), sep+1, OP_MAX_TYPENAME);
467                 }
468                 else {
469                         /* should not happen but support just incase */
470                         BLI_strncpy(to, from, OP_MAX_TYPENAME);
471                 }
472         }
473         else
474                 to[0]= 0;
475 }
476
477 /* print a string representation of the operator, with the args that it runs 
478  * so python can run it again,
479  *
480  * When calling from an existing wmOperator do.
481  * WM_operator_pystring(op->type, op->ptr);
482  */
483 char *WM_operator_pystring(bContext *C, wmOperatorType *ot, PointerRNA *opptr, int all_args)
484 {
485         const char *arg_name= NULL;
486         char idname_py[OP_MAX_TYPENAME];
487
488         PropertyRNA *prop, *iterprop;
489
490         /* for building the string */
491         DynStr *dynstr= BLI_dynstr_new();
492         char *cstring, *buf;
493         int first_iter=1, ok= 1;
494
495
496         /* only to get the orginal props for comparisons */
497         PointerRNA opptr_default;
498         PropertyRNA *prop_default;
499         char *buf_default;
500         if(all_args==0 || opptr==NULL) {
501                 WM_operator_properties_create_ptr(&opptr_default, ot);
502
503                 if(opptr==NULL)
504                         opptr = &opptr_default;
505         }
506
507
508         WM_operator_py_idname(idname_py, ot->idname);
509         BLI_dynstr_appendf(dynstr, "bpy.ops.%s(", idname_py);
510
511         iterprop= RNA_struct_iterator_property(opptr->type);
512
513         RNA_PROP_BEGIN(opptr, propptr, iterprop) {
514                 prop= propptr.data;
515                 arg_name= RNA_property_identifier(prop);
516
517                 if (strcmp(arg_name, "rna_type")==0) continue;
518
519                 buf= RNA_property_as_string(C, opptr, prop);
520                 
521                 ok= 1;
522
523                 if(!all_args) {
524                         /* not verbose, so only add in attributes that use non-default values
525                          * slow but good for tooltips */
526                         prop_default= RNA_struct_find_property(&opptr_default, arg_name);
527
528                         if(prop_default) {
529                                 buf_default= RNA_property_as_string(C, &opptr_default, prop_default);
530
531                                 if(strcmp(buf, buf_default)==0)
532                                         ok= 0; /* values match, dont bother printing */
533
534                                 MEM_freeN(buf_default);
535                         }
536
537                 }
538                 if(ok) {
539                         BLI_dynstr_appendf(dynstr, first_iter?"%s=%s":", %s=%s", arg_name, buf);
540                         first_iter = 0;
541                 }
542
543                 MEM_freeN(buf);
544
545         }
546         RNA_PROP_END;
547
548         if(all_args==0 || opptr==&opptr_default )
549                 WM_operator_properties_free(&opptr_default);
550
551         BLI_dynstr_append(dynstr, ")");
552
553         cstring = BLI_dynstr_get_cstring(dynstr);
554         BLI_dynstr_free(dynstr);
555         return cstring;
556 }
557
558 void WM_operator_properties_create_ptr(PointerRNA *ptr, wmOperatorType *ot)
559 {
560         RNA_pointer_create(NULL, ot->srna, NULL, ptr);
561 }
562
563 void WM_operator_properties_create(PointerRNA *ptr, const char *opstring)
564 {
565         wmOperatorType *ot= WM_operatortype_find(opstring, 0);
566
567         if(ot)
568                 WM_operator_properties_create_ptr(ptr, ot);
569         else
570                 RNA_pointer_create(NULL, &RNA_OperatorProperties, NULL, ptr);
571 }
572
573 /* similar to the function above except its uses ID properties
574  * used for keymaps and macros */
575 void WM_operator_properties_alloc(PointerRNA **ptr, IDProperty **properties, const char *opstring)
576 {
577         if(*properties==NULL) {
578                 IDPropertyTemplate val = {0};
579                 *properties= IDP_New(IDP_GROUP, val, "wmOpItemProp");
580         }
581
582         if(*ptr==NULL) {
583                 *ptr= MEM_callocN(sizeof(PointerRNA), "wmOpItemPtr");
584                 WM_operator_properties_create(*ptr, opstring);
585         }
586
587         (*ptr)->data= *properties;
588
589 }
590
591 void WM_operator_properties_sanitize(PointerRNA *ptr, const short no_context)
592 {
593         RNA_STRUCT_BEGIN(ptr, prop) {
594                 switch(RNA_property_type(prop)) {
595                 case PROP_ENUM:
596                         if (no_context)
597                                 RNA_def_property_flag(prop, PROP_ENUM_NO_CONTEXT);
598                         else
599                                 RNA_def_property_clear_flag(prop, PROP_ENUM_NO_CONTEXT);
600                         break;
601                 case PROP_POINTER:
602                         {
603                                 StructRNA *ptype= RNA_property_pointer_type(ptr, prop);
604
605                                 /* recurse into operator properties */
606                                 if (RNA_struct_is_a(ptype, &RNA_OperatorProperties)) {
607                                         PointerRNA opptr = RNA_property_pointer_get(ptr, prop);
608                                         WM_operator_properties_sanitize(&opptr, no_context);
609                                 }
610                                 break;
611                         }
612                 default:
613                         break;
614                 }
615         }
616         RNA_STRUCT_END;
617 }
618
619 void WM_operator_properties_free(PointerRNA *ptr)
620 {
621         IDProperty *properties= ptr->data;
622
623         if(properties) {
624                 IDP_FreeProperty(properties);
625                 MEM_freeN(properties);
626                 ptr->data= NULL; /* just incase */
627         }
628 }
629
630 /* ************ default op callbacks, exported *********** */
631
632 /* invoke callback, uses enum property named "type" */
633 int WM_menu_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
634 {
635         PropertyRNA *prop= op->type->prop;
636         uiPopupMenu *pup;
637         uiLayout *layout;
638
639         if(prop==NULL) {
640                 printf("WM_menu_invoke: %s has no enum property set\n", op->type->idname);
641         }
642         else if (RNA_property_type(prop) != PROP_ENUM) {
643                 printf("WM_menu_invoke: %s \"%s\" is not an enum property\n", op->type->idname, RNA_property_identifier(prop));
644         }
645         else if (RNA_property_is_set(op->ptr, RNA_property_identifier(prop))) {
646                 return op->type->exec(C, op);
647         }
648         else {
649                 pup= uiPupMenuBegin(C, op->type->name, ICON_NULL);
650                 layout= uiPupMenuLayout(pup);
651                 uiItemsFullEnumO(layout, op->type->idname, (char*)RNA_property_identifier(prop), op->ptr->data, WM_OP_EXEC_REGION_WIN, 0);
652                 uiPupMenuEnd(C, pup);
653         }
654
655         return OPERATOR_CANCELLED;
656 }
657
658
659 /* generic enum search invoke popup */
660 static void operator_enum_search_cb(const struct bContext *C, void *arg_ot, const char *str, uiSearchItems *items)
661 {
662         wmOperatorType *ot = (wmOperatorType *)arg_ot;
663         PropertyRNA *prop= ot->prop;
664
665         if(prop==NULL) {
666                 printf("WM_enum_search_invoke: %s has no enum property set\n", ot->idname);
667         }
668         else if (RNA_property_type(prop) != PROP_ENUM) {
669                 printf("WM_enum_search_invoke: %s \"%s\" is not an enum property\n", ot->idname, RNA_property_identifier(prop));
670         }
671         else {
672                 PointerRNA ptr;
673
674                 EnumPropertyItem *item, *item_array;
675                 int do_free;
676
677                 RNA_pointer_create(NULL, ot->srna, NULL, &ptr);
678                 RNA_property_enum_items((bContext *)C, &ptr, prop, &item_array, NULL, &do_free);
679
680                 for(item= item_array; item->identifier; item++) {
681                         /* note: need to give the intex rather then the dientifier because the enum can be freed */
682                         if(BLI_strcasestr(item->name, str))
683                                 if(0==uiSearchItemAdd(items, item->name, SET_INT_IN_POINTER(item->value), 0))
684                                         break;
685                 }
686
687                 if(do_free)
688                         MEM_freeN(item_array);
689         }
690 }
691
692 static void operator_enum_call_cb(struct bContext *C, void *arg1, void *arg2)
693 {
694         wmOperatorType *ot= arg1;
695
696         if(ot) {
697                 PointerRNA props_ptr;
698                 WM_operator_properties_create_ptr(&props_ptr, ot);
699                 RNA_property_enum_set(&props_ptr, ot->prop, GET_INT_FROM_POINTER(arg2));
700                 WM_operator_name_call(C, ot->idname, WM_OP_EXEC_DEFAULT, &props_ptr);
701                 WM_operator_properties_free(&props_ptr);
702         }
703 }
704
705 static uiBlock *wm_enum_search_menu(bContext *C, ARegion *ar, void *arg_op)
706 {
707         static char search[256]= "";
708         wmEvent event;
709         wmWindow *win= CTX_wm_window(C);
710         uiBlock *block;
711         uiBut *but;
712         wmOperator *op= (wmOperator *)arg_op;
713
714         block= uiBeginBlock(C, ar, "_popup", UI_EMBOSS);
715         uiBlockSetFlag(block, UI_BLOCK_LOOP|UI_BLOCK_RET_1|UI_BLOCK_MOVEMOUSE_QUIT);
716
717         //uiDefBut(block, LABEL, 0, op->type->name, 10, 10, 180, 19, NULL, 0.0, 0.0, 0, 0, ""); // ok, this isnt so easy...
718         but= uiDefSearchBut(block, search, 0, ICON_VIEWZOOM, 256, 10, 10, 180, 19, 0, 0, "");
719         uiButSetSearchFunc(but, operator_enum_search_cb, op->type, operator_enum_call_cb, NULL);
720
721         /* fake button, it holds space for search items */
722         uiDefBut(block, LABEL, 0, "", 10, 10 - uiSearchBoxhHeight(), 180, uiSearchBoxhHeight(), NULL, 0, 0, 0, 0, NULL);
723
724         uiPopupBoundsBlock(block, 6.0f, 0, -20); /* move it downwards, mouse over button */
725         uiEndBlock(C, block);
726
727         event= *(win->eventstate);      /* XXX huh huh? make api call */
728         event.type= EVT_BUT_OPEN;
729         event.val= KM_PRESS;
730         event.customdata= but;
731         event.customdatafree= FALSE;
732         wm_event_add(win, &event);
733
734         return block;
735 }
736
737
738 int WM_enum_search_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
739 {
740         uiPupBlock(C, wm_enum_search_menu, op);
741         return OPERATOR_CANCELLED;
742 }
743
744 /* Can't be used as an invoke directly, needs message arg (can be NULL) */
745 int WM_operator_confirm_message(bContext *C, wmOperator *op, const char *message)
746 {
747         uiPopupMenu *pup;
748         uiLayout *layout;
749         IDProperty *properties= op->ptr->data;
750
751         if(properties && properties->len)
752                 properties= IDP_CopyProperty(op->ptr->data);
753         else
754                 properties= NULL;
755
756         pup= uiPupMenuBegin(C, "OK?", ICON_QUESTION);
757         layout= uiPupMenuLayout(pup);
758         uiItemFullO(layout, op->type->idname, message, ICON_NULL, properties, WM_OP_EXEC_REGION_WIN, 0);
759         uiPupMenuEnd(C, pup);
760         
761         return OPERATOR_CANCELLED;
762 }
763
764
765 int WM_operator_confirm(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
766 {
767         return WM_operator_confirm_message(C, op, NULL);
768 }
769
770 /* op->invoke, opens fileselect if path property not set, otherwise executes */
771 int WM_operator_filesel(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
772 {
773         if (RNA_property_is_set(op->ptr, "filepath")) {
774                 return WM_operator_call(C, op);
775         } 
776         else {
777                 WM_event_add_fileselect(C, op);
778                 return OPERATOR_RUNNING_MODAL;
779         }
780 }
781
782 /* default properties for fileselect */
783 void WM_operator_properties_filesel(wmOperatorType *ot, int filter, short type, short action, short flag)
784 {
785         PropertyRNA *prop;
786
787
788         if(flag & WM_FILESEL_FILEPATH)
789                 RNA_def_string_file_path(ot->srna, "filepath", "", FILE_MAX, "File Path", "Path to file");
790
791         if(flag & WM_FILESEL_DIRECTORY)
792                 RNA_def_string_dir_path(ot->srna, "directory", "", FILE_MAX, "Directory", "Directory of the file");
793
794         if(flag & WM_FILESEL_FILENAME)
795                 RNA_def_string_file_name(ot->srna, "filename", "", FILE_MAX, "File Name", "Name of the file");
796
797         if (action == FILE_SAVE) {
798                 prop= RNA_def_boolean(ot->srna, "check_existing", 1, "Check Existing", "Check and warn on overwriting existing files");
799                 RNA_def_property_flag(prop, PROP_HIDDEN);
800         }
801         
802         prop= RNA_def_boolean(ot->srna, "filter_blender", (filter & BLENDERFILE), "Filter .blend files", "");
803         RNA_def_property_flag(prop, PROP_HIDDEN);
804         prop= RNA_def_boolean(ot->srna, "filter_image", (filter & IMAGEFILE), "Filter image files", "");
805         RNA_def_property_flag(prop, PROP_HIDDEN);
806         prop= RNA_def_boolean(ot->srna, "filter_movie", (filter & MOVIEFILE), "Filter movie files", "");
807         RNA_def_property_flag(prop, PROP_HIDDEN);
808         prop= RNA_def_boolean(ot->srna, "filter_python", (filter & PYSCRIPTFILE), "Filter python files", "");
809         RNA_def_property_flag(prop, PROP_HIDDEN);
810         prop= RNA_def_boolean(ot->srna, "filter_font", (filter & FTFONTFILE), "Filter font files", "");
811         RNA_def_property_flag(prop, PROP_HIDDEN);
812         prop= RNA_def_boolean(ot->srna, "filter_sound", (filter & SOUNDFILE), "Filter sound files", "");
813         RNA_def_property_flag(prop, PROP_HIDDEN);
814         prop= RNA_def_boolean(ot->srna, "filter_text", (filter & TEXTFILE), "Filter text files", "");
815         RNA_def_property_flag(prop, PROP_HIDDEN);
816         prop= RNA_def_boolean(ot->srna, "filter_btx", (filter & BTXFILE), "Filter btx files", "");
817         RNA_def_property_flag(prop, PROP_HIDDEN);
818         prop= RNA_def_boolean(ot->srna, "filter_collada", (filter & COLLADAFILE), "Filter COLLADA files", "");
819         RNA_def_property_flag(prop, PROP_HIDDEN);
820         prop= RNA_def_boolean(ot->srna, "filter_folder", (filter & FOLDERFILE), "Filter folders", "");
821         RNA_def_property_flag(prop, PROP_HIDDEN);
822
823         prop= RNA_def_int(ot->srna, "filemode", type, FILE_LOADLIB, FILE_SPECIAL, 
824                 "File Browser Mode", "The setting for the file browser mode to load a .blend file, a library or a special file",
825                 FILE_LOADLIB, FILE_SPECIAL);
826         RNA_def_property_flag(prop, PROP_HIDDEN);
827
828         if(flag & WM_FILESEL_RELPATH)
829                 RNA_def_boolean(ot->srna, "relative_path", (U.flag & USER_RELPATHS) ? 1:0, "Relative Path", "Select the file relative to the blend file");
830 }
831
832 void WM_operator_properties_select_all(wmOperatorType *ot) {
833         static EnumPropertyItem select_all_actions[] = {
834                         {SEL_TOGGLE, "TOGGLE", 0, "Toggle", "Toggle selection for all elements"},
835                         {SEL_SELECT, "SELECT", 0, "Select", "Select all elements"},
836                         {SEL_DESELECT, "DESELECT", 0, "Deselect", "Deselect all elements"},
837                         {SEL_INVERT, "INVERT", 0, "Invert", "Invert selection of all elements"},
838                         {0, NULL, 0, NULL, NULL}
839         };
840
841         RNA_def_enum(ot->srna, "action", select_all_actions, SEL_TOGGLE, "Action", "Selection action to execute");
842 }
843
844 void WM_operator_properties_gesture_border(wmOperatorType *ot, int extend)
845 {
846         RNA_def_int(ot->srna, "gesture_mode", 0, INT_MIN, INT_MAX, "Gesture Mode", "", INT_MIN, INT_MAX);
847         RNA_def_int(ot->srna, "xmin", 0, INT_MIN, INT_MAX, "X Min", "", INT_MIN, INT_MAX);
848         RNA_def_int(ot->srna, "xmax", 0, INT_MIN, INT_MAX, "X Max", "", INT_MIN, INT_MAX);
849         RNA_def_int(ot->srna, "ymin", 0, INT_MIN, INT_MAX, "Y Min", "", INT_MIN, INT_MAX);
850         RNA_def_int(ot->srna, "ymax", 0, INT_MIN, INT_MAX, "Y Max", "", INT_MIN, INT_MAX);
851
852         if(extend)
853                 RNA_def_boolean(ot->srna, "extend", 1, "Extend", "Extend selection instead of deselecting everything first");
854 }
855
856 void WM_operator_properties_gesture_straightline(wmOperatorType *ot, int cursor)
857 {
858         RNA_def_int(ot->srna, "xstart", 0, INT_MIN, INT_MAX, "X Start", "", INT_MIN, INT_MAX);
859         RNA_def_int(ot->srna, "xend", 0, INT_MIN, INT_MAX, "X End", "", INT_MIN, INT_MAX);
860         RNA_def_int(ot->srna, "ystart", 0, INT_MIN, INT_MAX, "Y Start", "", INT_MIN, INT_MAX);
861         RNA_def_int(ot->srna, "yend", 0, INT_MIN, INT_MAX, "Y End", "", INT_MIN, INT_MAX);
862         
863         if(cursor)
864                 RNA_def_int(ot->srna, "cursor", cursor, 0, INT_MAX, "Cursor", "Mouse cursor style to use during the modal operator", 0, INT_MAX);
865 }
866
867
868 /* op->poll */
869 int WM_operator_winactive(bContext *C)
870 {
871         if(CTX_wm_window(C)==NULL) return 0;
872         return 1;
873 }
874
875 static uiBlock *wm_block_create_redo(bContext *C, ARegion *ar, void *arg_op)
876 {
877         wmOperator *op= arg_op;
878         uiBlock *block;
879         uiLayout *layout;
880         uiStyle *style= U.uistyles.first;
881         int width= 300;
882         
883
884         block= uiBeginBlock(C, ar, "redo_popup", UI_EMBOSS);
885         uiBlockClearFlag(block, UI_BLOCK_LOOP);
886         uiBlockSetFlag(block, UI_BLOCK_KEEP_OPEN|UI_BLOCK_RET_1|UI_BLOCK_MOVEMOUSE_QUIT);
887
888         /* if register is not enabled, the operator gets freed on OPERATOR_FINISHED
889          * ui_apply_but_funcs_after calls ED_undo_operator_repeate_cb and crashes */
890         assert(op->type->flag & OPTYPE_REGISTER);
891
892         uiBlockSetHandleFunc(block, ED_undo_operator_repeat_cb_evt, arg_op);
893         layout= uiBlockLayout(block, UI_LAYOUT_VERTICAL, UI_LAYOUT_PANEL, 0, 0, width, 20, style);
894
895         if(ED_undo_valid(C, op->type->name)==0)
896                 uiLayoutSetEnabled(layout, 0);
897
898         uiLayoutOperatorButs(C, layout, op, NULL, 'H', UI_LAYOUT_OP_SHOW_TITLE);
899
900         uiPopupBoundsBlock(block, 4.0f, 0, 0);
901         uiEndBlock(C, block);
902
903         return block;
904 }
905
906 /* Only invoked by OK button in popups created with wm_block_create_dialog() */
907 static void dialog_exec_cb(bContext *C, void *arg1, void *arg2)
908 {
909         wmOperator *op= arg1;
910         uiBlock *block= arg2;
911
912         WM_operator_call(C, op);
913
914         uiPupBlockClose(C, block);
915 }
916
917 void dialog_check_cb(bContext *C, void *op_ptr, void *UNUSED(arg))
918 {
919         wmOperator *op= op_ptr;
920         if(op->type->check) {
921                 if(op->type->check(C, op)) {
922                         /* refresh */
923                 }
924         }
925 }
926
927 /* Dialogs are popups that require user verification (click OK) before exec */
928 static uiBlock *wm_block_create_dialog(bContext *C, ARegion *ar, void *userData)
929 {
930         struct { wmOperator *op; int width; int height; } * data = userData;
931         wmOperator *op= data->op;
932         uiBlock *block;
933         uiLayout *layout;
934         uiBut *btn;
935         uiStyle *style= U.uistyles.first;
936
937         block = uiBeginBlock(C, ar, "operator dialog", UI_EMBOSS);
938         uiBlockClearFlag(block, UI_BLOCK_LOOP);
939         uiBlockSetFlag(block, UI_BLOCK_KEEP_OPEN|UI_BLOCK_RET_1|UI_BLOCK_MOVEMOUSE_QUIT);
940
941         layout= uiBlockLayout(block, UI_LAYOUT_VERTICAL, UI_LAYOUT_PANEL, 0, 0, data->width, data->height, style);
942         
943         uiBlockSetFunc(block, dialog_check_cb, op, NULL);
944
945         uiLayoutOperatorButs(C, layout, op, NULL, 'H', UI_LAYOUT_OP_SHOW_TITLE);
946         
947         /* clear so the OK button is left alone */
948         uiBlockSetFunc(block, NULL, NULL, NULL);
949
950         /* Create OK button, the callback of which will execute op */
951         btn= uiDefBut(block, BUT, 0, "OK", 0, 0, 0, 20, NULL, 0, 0, 0, 0, "");
952         uiButSetFunc(btn, dialog_exec_cb, op, block);
953
954         /* center around the mouse */
955         uiPopupBoundsBlock(block, 4.0f, data->width/-2, data->height/2);
956         uiEndBlock(C, block);
957
958         return block;
959 }
960
961 static uiBlock *wm_operator_create_ui(bContext *C, ARegion *ar, void *userData)
962 {
963         struct { wmOperator *op; int width; int height; } * data = userData;
964         wmOperator *op= data->op;
965         uiBlock *block;
966         uiLayout *layout;
967         uiStyle *style= U.uistyles.first;
968
969         block= uiBeginBlock(C, ar, "opui_popup", UI_EMBOSS);
970         uiBlockClearFlag(block, UI_BLOCK_LOOP);
971         uiBlockSetFlag(block, UI_BLOCK_KEEP_OPEN|UI_BLOCK_RET_1|UI_BLOCK_MOVEMOUSE_QUIT);
972
973         layout= uiBlockLayout(block, UI_LAYOUT_VERTICAL, UI_LAYOUT_PANEL, 0, 0, data->width, data->height, style);
974
975         /* since ui is defined the auto-layout args are not used */
976         uiLayoutOperatorButs(C, layout, op, NULL, 'V', 0);
977
978         uiPopupBoundsBlock(block, 4.0f, 0, 0);
979         uiEndBlock(C, block);
980
981         return block;
982 }
983
984 /* operator menu needs undo, for redo callback */
985 int WM_operator_props_popup(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
986 {
987         
988         if((op->type->flag & OPTYPE_REGISTER)==0) {
989                 BKE_reportf(op->reports, RPT_ERROR, "Operator '%s' does not have register enabled, incorrect invoke function.", op->type->idname);
990                 return OPERATOR_CANCELLED;
991         }
992         
993         ED_undo_push_op(C, op);
994         wm_operator_register(C, op);
995
996         uiPupBlock(C, wm_block_create_redo, op);
997
998         return OPERATOR_RUNNING_MODAL;
999 }
1000
1001 int WM_operator_props_dialog_popup(bContext *C, wmOperator *op, int width, int height)
1002 {
1003         struct { wmOperator *op; int width; int height; } data;
1004         
1005         data.op= op;
1006         data.width= width;
1007         data.height= height;
1008
1009         /* op is not executed until popup OK but is clicked */
1010         uiPupBlock(C, wm_block_create_dialog, &data);
1011
1012         return OPERATOR_RUNNING_MODAL;
1013 }
1014
1015 int WM_operator_ui_popup(bContext *C, wmOperator *op, int width, int height)
1016 {
1017         struct { wmOperator *op; int width; int height; } data;
1018         data.op = op;
1019         data.width = width;
1020         data.height = height;
1021         uiPupBlock(C, wm_operator_create_ui, &data);
1022         return OPERATOR_RUNNING_MODAL;
1023 }
1024
1025 int WM_operator_redo_popup(bContext *C, wmOperator *op)
1026 {
1027         if((op->type->flag & OPTYPE_REGISTER)==0) {
1028                 BKE_reportf(op->reports, RPT_ERROR, "Operator '%s' does not have register enabled, incorrect invoke function.", op->type->idname);
1029                 return OPERATOR_CANCELLED;
1030         }
1031         
1032         uiPupBlock(C, wm_block_create_redo, op);
1033
1034         return OPERATOR_CANCELLED;
1035 }
1036
1037 /* ***************** Debug menu ************************* */
1038
1039 static int wm_debug_menu_exec(bContext *C, wmOperator *op)
1040 {
1041         G.rt= RNA_int_get(op->ptr, "debug_value");
1042         ED_screen_refresh(CTX_wm_manager(C), CTX_wm_window(C));
1043         WM_event_add_notifier(C, NC_WINDOW, NULL);
1044
1045         return OPERATOR_FINISHED;       
1046 }
1047
1048 static int wm_debug_menu_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
1049 {
1050         RNA_int_set(op->ptr, "debug_value", G.rt);
1051         return WM_operator_props_dialog_popup(C, op, 180, 20);
1052 }
1053
1054 static void WM_OT_debug_menu(wmOperatorType *ot)
1055 {
1056         ot->name= "Debug Menu";
1057         ot->idname= "WM_OT_debug_menu";
1058         ot->description= "Open a popup to set the debug level";
1059         
1060         ot->invoke= wm_debug_menu_invoke;
1061         ot->exec= wm_debug_menu_exec;
1062         ot->poll= WM_operator_winactive;
1063         
1064         RNA_def_int(ot->srna, "debug_value", 0, -10000, 10000, "Debug Value", "", INT_MIN, INT_MAX);
1065 }
1066
1067
1068 /* ***************** Splash Screen ************************* */
1069
1070 static void wm_block_splash_close(bContext *C, void *arg_block, void *UNUSED(arg))
1071 {
1072         uiPupBlockClose(C, arg_block);
1073 }
1074
1075 static uiBlock *wm_block_create_splash(bContext *C, ARegion *ar, void *arg_unused);
1076
1077 /* XXX: hack to refresh splash screen with updated prest menu name,
1078  * since popup blocks don't get regenerated like panels do */
1079 void wm_block_splash_refreshmenu (bContext *UNUSED(C), void *UNUSED(arg_block), void *UNUSED(arg))
1080 {
1081         /* ugh, causes crashes in other buttons, disabling for now until 
1082          * a better fix
1083         uiPupBlockClose(C, arg_block);
1084         uiPupBlock(C, wm_block_create_splash, NULL);
1085           */
1086 }
1087
1088 static uiBlock *wm_block_create_splash(bContext *C, ARegion *ar, void *UNUSED(arg))
1089 {
1090         uiBlock *block;
1091         uiBut *but;
1092         uiLayout *layout, *split, *col;
1093         uiStyle *style= U.uistyles.first;
1094         struct RecentFile *recent;
1095         int i;
1096         MenuType *mt= WM_menutype_find("USERPREF_MT_splash", TRUE);
1097         char url[64];
1098         
1099 #ifdef NAN_BUILDINFO
1100         int ver_width, rev_width;
1101         char *version_str = NULL;
1102         char *revision_str = NULL;
1103         char version_buf[128];
1104         char revision_buf[128];
1105         extern char build_rev[];
1106         
1107         version_str = &version_buf[0];
1108         revision_str = &revision_buf[0];
1109         
1110         sprintf(version_str, "%d.%02d.%d", BLENDER_VERSION/100, BLENDER_VERSION%100, BLENDER_SUBVERSION);
1111         sprintf(revision_str, "r%s", build_rev);
1112         
1113         BLF_size(style->widgetlabel.uifont_id, style->widgetlabel.points, U.dpi);
1114         ver_width = BLF_width(style->widgetlabel.uifont_id, version_str)+5;
1115         rev_width = BLF_width(style->widgetlabel.uifont_id, revision_str)+5;
1116 #endif //NAN_BUILDINFO
1117
1118         block= uiBeginBlock(C, ar, "_popup", UI_EMBOSS);
1119         uiBlockSetFlag(block, UI_BLOCK_KEEP_OPEN);
1120         
1121         but= uiDefBut(block, BUT_IMAGE, 0, "", 0, 10, 501, 282, NULL, 0.0, 0.0, 0, 0, "");
1122         uiButSetFunc(but, wm_block_splash_close, block, NULL);
1123         uiBlockSetFunc(block, wm_block_splash_refreshmenu, block, NULL);
1124         
1125 #ifdef NAN_BUILDINFO    
1126         uiDefBut(block, LABEL, 0, version_str, 494-ver_width, 282-24, ver_width, 20, NULL, 0, 0, 0, 0, NULL);
1127         uiDefBut(block, LABEL, 0, revision_str, 494-rev_width, 282-36, rev_width, 20, NULL, 0, 0, 0, 0, NULL);
1128 #endif //NAN_BUILDINFO
1129         
1130         layout= uiBlockLayout(block, UI_LAYOUT_VERTICAL, UI_LAYOUT_PANEL, 10, 2, 480, 110, style);
1131         
1132         uiBlockSetEmboss(block, UI_EMBOSS);
1133         /* show the splash menu (containing interaction presets), using python */
1134         if (mt) {
1135                 Menu menu= {0};
1136                 menu.layout= layout;
1137                 menu.type= mt;
1138                 mt->draw(C, &menu);
1139
1140 //              wmWindowManager *wm= CTX_wm_manager(C);
1141 //              uiItemM(layout, C, "USERPREF_MT_keyconfigs", U.keyconfigstr, ICON_NULL);
1142         }
1143         
1144         uiBlockSetEmboss(block, UI_EMBOSSP);
1145         uiLayoutSetOperatorContext(layout, WM_OP_EXEC_REGION_WIN);
1146         
1147         split = uiLayoutSplit(layout, 0, 0);
1148         col = uiLayoutColumn(split, 0);
1149         uiItemL(col, "Links", ICON_NULL);
1150         uiItemStringO(col, "Donations", ICON_URL, "WM_OT_url_open", "url", "http://www.blender.org/blenderorg/blender-foundation/donation-payment/");
1151         uiItemStringO(col, "Release Log", ICON_URL, "WM_OT_url_open", "url", "http://www.blender.org/development/release-logs/blender-256-beta/");
1152         uiItemStringO(col, "Manual", ICON_URL, "WM_OT_url_open", "url", "http://wiki.blender.org/index.php/Doc:Manual");
1153         uiItemStringO(col, "Blender Website", ICON_URL, "WM_OT_url_open", "url", "http://www.blender.org/");
1154         uiItemStringO(col, "User Community", ICON_URL, "WM_OT_url_open", "url", "http://www.blender.org/community/user-community/"); // 
1155         BLI_snprintf(url, sizeof(url), "http://www.blender.org/documentation/blender_python_api_%d_%d_%d", BLENDER_VERSION/100, BLENDER_VERSION%100, BLENDER_SUBVERSION);
1156         uiItemStringO(col, "Python API Reference", ICON_URL, "WM_OT_url_open", "url", url);
1157         uiItemL(col, "", ICON_NULL);
1158
1159         col = uiLayoutColumn(split, 0);
1160         uiItemL(col, "Recent", ICON_NULL);
1161         for(recent = G.recent_files.first, i=0; (i<5) && (recent); recent = recent->next, i++) {
1162                 uiItemStringO(col, BLI_path_basename(recent->filepath), ICON_FILE_BLEND, "WM_OT_open_mainfile", "filepath", recent->filepath);
1163         }
1164         uiItemS(col);
1165         uiItemO(col, NULL, ICON_RECOVER_LAST, "WM_OT_recover_last_session");
1166         uiItemL(col, "", ICON_NULL);
1167         
1168         uiCenteredBoundsBlock(block, 0.0f);
1169         uiEndBlock(C, block);
1170         
1171         return block;
1172 }
1173
1174 static int wm_splash_invoke(bContext *C, wmOperator *UNUSED(op), wmEvent *UNUSED(event))
1175 {
1176         uiPupBlock(C, wm_block_create_splash, NULL);
1177         
1178         return OPERATOR_FINISHED;
1179 }
1180
1181 static void WM_OT_splash(wmOperatorType *ot)
1182 {
1183         ot->name= "Splash Screen";
1184         ot->idname= "WM_OT_splash";
1185         ot->description= "Opens a blocking popup region with release info";
1186         
1187         ot->invoke= wm_splash_invoke;
1188         ot->poll= WM_operator_winactive;
1189 }
1190
1191
1192 /* ***************** Search menu ************************* */
1193 static void operator_call_cb(struct bContext *C, void *UNUSED(arg1), void *arg2)
1194 {
1195         wmOperatorType *ot= arg2;
1196         
1197         if(ot)
1198                 WM_operator_name_call(C, ot->idname, WM_OP_INVOKE_DEFAULT, NULL);
1199 }
1200
1201 static void operator_search_cb(const struct bContext *C, void *UNUSED(arg), const char *str, uiSearchItems *items)
1202 {
1203         wmOperatorType *ot = WM_operatortype_first();
1204         
1205         for(; ot; ot= ot->next) {
1206                 
1207                 if(BLI_strcasestr(ot->name, str)) {
1208                         if(WM_operator_poll((bContext*)C, ot)) {
1209                                 char name[256];
1210                                 int len= strlen(ot->name);
1211                                 
1212                                 /* display name for menu, can hold hotkey */
1213                                 BLI_strncpy(name, ot->name, 256);
1214                                 
1215                                 /* check for hotkey */
1216                                 if(len < 256-6) {
1217                                         if(WM_key_event_operator_string(C, ot->idname, WM_OP_EXEC_DEFAULT, NULL, &name[len+1], 256-len-1))
1218                                                 name[len]= '|';
1219                                 }
1220                                 
1221                                 if(0==uiSearchItemAdd(items, name, ot, 0))
1222                                         break;
1223                         }
1224                 }
1225         }
1226 }
1227
1228 static uiBlock *wm_block_search_menu(bContext *C, ARegion *ar, void *UNUSED(arg_op))
1229 {
1230         static char search[256]= "";
1231         wmEvent event;
1232         wmWindow *win= CTX_wm_window(C);
1233         uiBlock *block;
1234         uiBut *but;
1235         
1236         block= uiBeginBlock(C, ar, "_popup", UI_EMBOSS);
1237         uiBlockSetFlag(block, UI_BLOCK_LOOP|UI_BLOCK_RET_1|UI_BLOCK_MOVEMOUSE_QUIT);
1238         
1239         but= uiDefSearchBut(block, search, 0, ICON_VIEWZOOM, 256, 10, 10, 180, 19, 0, 0, "");
1240         uiButSetSearchFunc(but, operator_search_cb, NULL, operator_call_cb, NULL);
1241         
1242         /* fake button, it holds space for search items */
1243         uiDefBut(block, LABEL, 0, "", 10, 10 - uiSearchBoxhHeight(), 180, uiSearchBoxhHeight(), NULL, 0, 0, 0, 0, NULL);
1244         
1245         uiPopupBoundsBlock(block, 6.0f, 0, -20); /* move it downwards, mouse over button */
1246         uiEndBlock(C, block);
1247         
1248         event= *(win->eventstate);      /* XXX huh huh? make api call */
1249         event.type= EVT_BUT_OPEN;
1250         event.val= KM_PRESS;
1251         event.customdata= but;
1252         event.customdatafree= FALSE;
1253         wm_event_add(win, &event);
1254         
1255         return block;
1256 }
1257
1258 static int wm_search_menu_exec(bContext *UNUSED(C), wmOperator *UNUSED(op))
1259 {
1260         return OPERATOR_FINISHED;       
1261 }
1262
1263 static int wm_search_menu_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
1264 {
1265         uiPupBlock(C, wm_block_search_menu, op);
1266         
1267         return OPERATOR_CANCELLED;
1268 }
1269
1270 /* op->poll */
1271 static int wm_search_menu_poll(bContext *C)
1272 {
1273         if(CTX_wm_window(C)==NULL) return 0;
1274         if(CTX_wm_area(C) && CTX_wm_area(C)->spacetype==SPACE_CONSOLE) return 0;  // XXX - so we can use the shortcut in the console
1275         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
1276         if(CTX_data_edit_object(C) && CTX_data_edit_object(C)->type==OB_FONT) return 0; // XXX - so we can use the spacebar for entering text
1277         return 1;
1278 }
1279
1280 static void WM_OT_search_menu(wmOperatorType *ot)
1281 {
1282         ot->name= "Search Menu";
1283         ot->idname= "WM_OT_search_menu";
1284         
1285         ot->invoke= wm_search_menu_invoke;
1286         ot->exec= wm_search_menu_exec;
1287         ot->poll= wm_search_menu_poll;
1288 }
1289
1290 static int wm_call_menu_exec(bContext *C, wmOperator *op)
1291 {
1292         char idname[BKE_ST_MAXNAME];
1293         RNA_string_get(op->ptr, "name", idname);
1294
1295         uiPupMenuInvoke(C, idname);
1296
1297         return OPERATOR_CANCELLED;
1298 }
1299
1300 static void WM_OT_call_menu(wmOperatorType *ot)
1301 {
1302         ot->name= "Call Menu";
1303         ot->idname= "WM_OT_call_menu";
1304
1305         ot->exec= wm_call_menu_exec;
1306
1307         RNA_def_string(ot->srna, "name", "", BKE_ST_MAXNAME, "Name", "Name of the menu");
1308 }
1309
1310 /* ************ window / screen operator definitions ************** */
1311
1312 /* this poll functions is needed in place of WM_operator_winactive
1313  * while it crashes on full screen */
1314 static int wm_operator_winactive_normal(bContext *C)
1315 {
1316         wmWindow *win= CTX_wm_window(C);
1317
1318         if(win==NULL || win->screen==NULL || win->screen->full != SCREENNORMAL)
1319                 return 0;
1320
1321         return 1;
1322 }
1323
1324 static void WM_OT_window_duplicate(wmOperatorType *ot)
1325 {
1326         ot->name= "Duplicate Window";
1327         ot->idname= "WM_OT_window_duplicate";
1328         ot->description="Duplicate the current Blender window";
1329                 
1330         ot->exec= wm_window_duplicate_exec;
1331         ot->poll= wm_operator_winactive_normal;
1332 }
1333
1334 static void WM_OT_save_homefile(wmOperatorType *ot)
1335 {
1336         ot->name= "Save User Settings";
1337         ot->idname= "WM_OT_save_homefile";
1338         ot->description="Make the current file the default .blend file";
1339                 
1340         ot->invoke= WM_operator_confirm;
1341         ot->exec= WM_write_homefile;
1342         ot->poll= WM_operator_winactive;
1343 }
1344
1345 static void WM_OT_read_homefile(wmOperatorType *ot)
1346 {
1347         ot->name= "Reload Start-Up File";
1348         ot->idname= "WM_OT_read_homefile";
1349         ot->description="Open the default file (doesn't save the current file)";
1350         
1351         ot->invoke= WM_operator_confirm;
1352         ot->exec= WM_read_homefile_exec;
1353         /* ommit poll to run in background mode */
1354 }
1355
1356 static void WM_OT_read_factory_settings(wmOperatorType *ot)
1357 {
1358         ot->name= "Load Factory Settings";
1359         ot->idname= "WM_OT_read_factory_settings";
1360         ot->description="Load default file and user preferences";
1361         
1362         ot->invoke= WM_operator_confirm;
1363         ot->exec= WM_read_homefile_exec;
1364         /* ommit poll to run in background mode */
1365 }
1366
1367 /* *************** open file **************** */
1368
1369 static void open_set_load_ui(wmOperator *op)
1370 {
1371         if(!RNA_property_is_set(op->ptr, "load_ui"))
1372                 RNA_boolean_set(op->ptr, "load_ui", !(U.flag & USER_FILENOUI));
1373 }
1374
1375 static void open_set_use_scripts(wmOperator *op)
1376 {
1377         if(!RNA_property_is_set(op->ptr, "use_scripts"))
1378                 RNA_boolean_set(op->ptr, "use_scripts", !(U.flag & USER_SCRIPT_AUTOEXEC_DISABLE));
1379 }
1380
1381 static int wm_open_mainfile_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
1382 {
1383         const char *openname= G.main->name;
1384
1385         /* if possible, get the name of the most recently used .blend file */
1386         if (G.recent_files.first) {
1387                 struct RecentFile *recent = G.recent_files.first;
1388                 openname = recent->filepath;
1389         }
1390
1391         RNA_string_set(op->ptr, "filepath", openname);
1392         open_set_load_ui(op);
1393         open_set_use_scripts(op);
1394
1395         WM_event_add_fileselect(C, op);
1396
1397         return OPERATOR_RUNNING_MODAL;
1398 }
1399
1400 static int wm_open_mainfile_exec(bContext *C, wmOperator *op)
1401 {
1402         char path[FILE_MAX];
1403
1404         RNA_string_get(op->ptr, "filepath", path);
1405         open_set_load_ui(op);
1406         open_set_use_scripts(op);
1407
1408         if(RNA_boolean_get(op->ptr, "load_ui"))
1409                 G.fileflags &= ~G_FILE_NO_UI;
1410         else
1411                 G.fileflags |= G_FILE_NO_UI;
1412                 
1413         if(RNA_boolean_get(op->ptr, "use_scripts"))
1414                 G.f |= G_SCRIPT_AUTOEXEC;
1415         else
1416                 G.f &= ~G_SCRIPT_AUTOEXEC;
1417         
1418         // XXX wm in context is not set correctly after WM_read_file -> crash
1419         // do it before for now, but is this correct with multiple windows?
1420         WM_event_add_notifier(C, NC_WINDOW, NULL);
1421
1422         WM_read_file(C, path, op->reports);
1423         
1424         return OPERATOR_FINISHED;
1425 }
1426
1427 static void WM_OT_open_mainfile(wmOperatorType *ot)
1428 {
1429         ot->name= "Open Blender File";
1430         ot->idname= "WM_OT_open_mainfile";
1431         ot->description="Open a Blender file";
1432         
1433         ot->invoke= wm_open_mainfile_invoke;
1434         ot->exec= wm_open_mainfile_exec;
1435         ot->poll= WM_operator_winactive;
1436         
1437         WM_operator_properties_filesel(ot, FOLDERFILE|BLENDERFILE, FILE_BLENDER, FILE_OPENFILE, WM_FILESEL_FILEPATH);
1438
1439         RNA_def_boolean(ot->srna, "load_ui", 1, "Load UI", "Load user interface setup in the .blend file");
1440         RNA_def_boolean(ot->srna, "use_scripts", 1, "Trusted Source", "Allow blend file execute scripts automatically, default available from system preferences");
1441 }
1442
1443 /* **************** link/append *************** */
1444
1445 static int wm_link_append_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
1446 {
1447         if(!RNA_property_is_set(op->ptr, "relative_path"))
1448                 RNA_boolean_set(op->ptr, "relative_path", U.flag & USER_RELPATHS);
1449
1450         if(RNA_property_is_set(op->ptr, "filepath")) {
1451                 return WM_operator_call(C, op);
1452         } 
1453         else {
1454                 /* XXX TODO solve where to get last linked library from */
1455                 RNA_string_set(op->ptr, "filepath", G.lib);
1456                 WM_event_add_fileselect(C, op);
1457                 return OPERATOR_RUNNING_MODAL;
1458         }
1459 }
1460
1461 static short wm_link_append_flag(wmOperator *op)
1462 {
1463         short flag= 0;
1464
1465         if(RNA_boolean_get(op->ptr, "autoselect")) flag |= FILE_AUTOSELECT;
1466         if(RNA_boolean_get(op->ptr, "active_layer")) flag |= FILE_ACTIVELAY;
1467         if(RNA_boolean_get(op->ptr, "relative_path")) flag |= FILE_RELPATH;
1468         if(RNA_boolean_get(op->ptr, "link")) flag |= FILE_LINK;
1469         if(RNA_boolean_get(op->ptr, "instance_groups")) flag |= FILE_GROUP_INSTANCE;
1470
1471         return flag;
1472 }
1473
1474 static void wm_link_make_library_local(Main *main, const char *libname)
1475 {
1476         Library *lib;
1477
1478         /* and now find the latest append lib file */
1479         for(lib= main->library.first; lib; lib=lib->id.next)
1480                 if(BLI_streq(libname, lib->filepath))
1481                         break;
1482         
1483         /* make local */
1484         if(lib) {
1485                 all_local(lib, 1);
1486         }
1487 }
1488
1489 static int wm_link_append_exec(bContext *C, wmOperator *op)
1490 {
1491         Main *bmain= CTX_data_main(C);
1492         Scene *scene= CTX_data_scene(C);
1493         Main *mainl= 0;
1494         BlendHandle *bh;
1495         PropertyRNA *prop;
1496         char name[FILE_MAX], dir[FILE_MAX], libname[FILE_MAX], group[GROUP_MAX];
1497         int idcode, totfiles=0;
1498         short flag;
1499
1500         name[0] = '\0';
1501         RNA_string_get(op->ptr, "filename", name);
1502         RNA_string_get(op->ptr, "directory", dir);
1503
1504         /* test if we have a valid data */
1505         if(BLO_is_a_library(dir, libname, group) == 0) {
1506                 BKE_report(op->reports, RPT_ERROR, "Not a library");
1507                 return OPERATOR_CANCELLED;
1508         }
1509         else if(group[0] == 0) {
1510                 BKE_report(op->reports, RPT_ERROR, "Nothing indicated");
1511                 return OPERATOR_CANCELLED;
1512         }
1513         else if(BLI_streq(bmain->name, libname)) {
1514                 BKE_report(op->reports, RPT_ERROR, "Cannot use current file as library");
1515                 return OPERATOR_CANCELLED;
1516         }
1517
1518         /* check if something is indicated for append/link */
1519         prop = RNA_struct_find_property(op->ptr, "files");
1520         if(prop) {
1521                 totfiles= RNA_property_collection_length(op->ptr, prop);
1522                 if(totfiles == 0) {
1523                         if(name[0] == '\0') {
1524                                 BKE_report(op->reports, RPT_ERROR, "Nothing indicated");
1525                                 return OPERATOR_CANCELLED;
1526                         }
1527                 }
1528         }
1529         else if(name[0] == '\0') {
1530                 BKE_report(op->reports, RPT_ERROR, "Nothing indicated");
1531                 return OPERATOR_CANCELLED;
1532         }
1533
1534         /* now we have or selected, or an indicated file */
1535         if(RNA_boolean_get(op->ptr, "autoselect"))
1536                 scene_deselect_all(scene);
1537
1538         bh = BLO_blendhandle_from_file(libname);
1539         idcode = BKE_idcode_from_name(group);
1540         
1541         flag = wm_link_append_flag(op);
1542
1543         /* sanity checks for flag */
1544         if(scene->id.lib && (flag & FILE_GROUP_INSTANCE)) {
1545                 /* TODO, user never gets this message */
1546                 BKE_reportf(op->reports, RPT_WARNING, "Scene '%s' is linked, group instance disabled", scene->id.name+2);
1547                 flag &= ~FILE_GROUP_INSTANCE;
1548         }
1549
1550
1551         /* tag everything, all untagged data can be made local
1552          * its also generally useful to know what is new
1553          *
1554          * take extra care flag_all_listbases_ids(LIB_LINK_TAG, 0) is called after! */
1555         flag_all_listbases_ids(LIB_PRE_EXISTING, 1);
1556
1557         /* here appending/linking starts */
1558         mainl = BLO_library_append_begin(C, &bh, libname);
1559         if(totfiles == 0) {
1560                 BLO_library_append_named_part(C, mainl, &bh, name, idcode, flag);
1561         }
1562         else {
1563                 RNA_BEGIN(op->ptr, itemptr, "files") {
1564                         RNA_string_get(&itemptr, "name", name);
1565                         BLO_library_append_named_part(C, mainl, &bh, name, idcode, flag);
1566                 }
1567                 RNA_END;
1568         }
1569         BLO_library_append_end(C, mainl, &bh, idcode, flag);
1570         
1571         /* mark all library linked objects to be updated */
1572         recalc_all_library_objects(bmain);
1573
1574         /* append, rather than linking */
1575         if((flag & FILE_LINK)==0)
1576                 wm_link_make_library_local(bmain, libname);
1577
1578         /* important we unset, otherwise these object wont
1579          * link into other scenes from this blend file */
1580         flag_all_listbases_ids(LIB_PRE_EXISTING, 0);
1581
1582         /* recreate dependency graph to include new objects */
1583         DAG_scene_sort(bmain, scene);
1584         DAG_ids_flush_update(bmain, 0);
1585
1586         BLO_blendhandle_close(bh);
1587
1588         /* XXX TODO: align G.lib with other directory storage (like last opened image etc...) */
1589         BLI_strncpy(G.lib, dir, FILE_MAX);
1590
1591         WM_event_add_notifier(C, NC_WINDOW, NULL);
1592
1593         return OPERATOR_FINISHED;
1594 }
1595
1596 static void WM_OT_link_append(wmOperatorType *ot)
1597 {
1598         ot->name= "Link/Append from Library";
1599         ot->idname= "WM_OT_link_append";
1600         ot->description= "Link or Append from a Library .blend file";
1601         
1602         ot->invoke= wm_link_append_invoke;
1603         ot->exec= wm_link_append_exec;
1604         ot->poll= WM_operator_winactive;
1605         
1606         ot->flag |= OPTYPE_UNDO;
1607
1608         WM_operator_properties_filesel(ot, FOLDERFILE|BLENDERFILE, FILE_LOADLIB, FILE_OPENFILE, WM_FILESEL_FILEPATH|WM_FILESEL_DIRECTORY|WM_FILESEL_FILENAME| WM_FILESEL_RELPATH);
1609         
1610         RNA_def_boolean(ot->srna, "link", 1, "Link", "Link the objects or datablocks rather than appending");
1611         RNA_def_boolean(ot->srna, "autoselect", 1, "Select", "Select the linked objects");
1612         RNA_def_boolean(ot->srna, "active_layer", 1, "Active Layer", "Put the linked objects on the active layer");
1613         RNA_def_boolean(ot->srna, "instance_groups", 1, "Instance Groups", "Create instances for each group as a DupliGroup");
1614
1615         RNA_def_collection_runtime(ot->srna, "files", &RNA_OperatorFileListElement, "Files", "");
1616 }       
1617
1618 /* *************** recover last session **************** */
1619
1620 static int wm_recover_last_session_exec(bContext *C, wmOperator *op)
1621 {
1622         char filename[FILE_MAX];
1623
1624         G.fileflags |= G_FILE_RECOVER;
1625
1626         // XXX wm in context is not set correctly after WM_read_file -> crash
1627         // do it before for now, but is this correct with multiple windows?
1628         WM_event_add_notifier(C, NC_WINDOW, NULL);
1629
1630         /* load file */
1631         BLI_make_file_string("/", filename, btempdir, "quit.blend");
1632         WM_read_file(C, filename, op->reports);
1633
1634         G.fileflags &= ~G_FILE_RECOVER;
1635         return OPERATOR_FINISHED;
1636 }
1637
1638 static void WM_OT_recover_last_session(wmOperatorType *ot)
1639 {
1640         ot->name= "Recover Last Session";
1641         ot->idname= "WM_OT_recover_last_session";
1642         ot->description="Open the last closed file (\"quit.blend\")";
1643         
1644         ot->exec= wm_recover_last_session_exec;
1645         ot->poll= WM_operator_winactive;
1646 }
1647
1648 /* *************** recover auto save **************** */
1649
1650 static int wm_recover_auto_save_exec(bContext *C, wmOperator *op)
1651 {
1652         char path[FILE_MAX];
1653
1654         RNA_string_get(op->ptr, "filepath", path);
1655
1656         G.fileflags |= G_FILE_RECOVER;
1657
1658         // XXX wm in context is not set correctly after WM_read_file -> crash
1659         // do it before for now, but is this correct with multiple windows?
1660         WM_event_add_notifier(C, NC_WINDOW, NULL);
1661
1662         /* load file */
1663         WM_read_file(C, path, op->reports);
1664
1665         G.fileflags &= ~G_FILE_RECOVER;
1666
1667         return OPERATOR_FINISHED;
1668 }
1669
1670 static int wm_recover_auto_save_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
1671 {
1672         char filename[FILE_MAX];
1673
1674         wm_autosave_location(filename);
1675         RNA_string_set(op->ptr, "filepath", filename);
1676         WM_event_add_fileselect(C, op);
1677
1678         return OPERATOR_RUNNING_MODAL;
1679 }
1680
1681 static void WM_OT_recover_auto_save(wmOperatorType *ot)
1682 {
1683         ot->name= "Recover Auto Save";
1684         ot->idname= "WM_OT_recover_auto_save";
1685         ot->description="Open an automatically saved file to recover it";
1686         
1687         ot->exec= wm_recover_auto_save_exec;
1688         ot->invoke= wm_recover_auto_save_invoke;
1689         ot->poll= WM_operator_winactive;
1690
1691         WM_operator_properties_filesel(ot, BLENDERFILE, FILE_BLENDER, FILE_OPENFILE, WM_FILESEL_FILEPATH);
1692 }
1693
1694 /* *************** save file as **************** */
1695
1696 static void untitled(char *name)
1697 {
1698         if(G.save_over == 0 && strlen(name) < FILE_MAX-16) {
1699                 char *c= BLI_last_slash(name);
1700                 
1701                 if(c)
1702                         strcpy(&c[1], "untitled.blend");
1703                 else
1704                         strcpy(name, "untitled.blend");
1705         }
1706 }
1707
1708 static void save_set_compress(wmOperator *op)
1709 {
1710         if(!RNA_property_is_set(op->ptr, "compress")) {
1711                 if(G.save_over) /* keep flag for existing file */
1712                         RNA_boolean_set(op->ptr, "compress", G.fileflags & G_FILE_COMPRESS);
1713                 else /* use userdef for new file */
1714                         RNA_boolean_set(op->ptr, "compress", U.flag & USER_FILECOMPRESS);
1715         }
1716 }
1717
1718 static int wm_save_as_mainfile_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
1719 {
1720         char name[FILE_MAX];
1721
1722         save_set_compress(op);
1723         
1724         BLI_strncpy(name, G.main->name, FILE_MAX);
1725         untitled(name);
1726         RNA_string_set(op->ptr, "filepath", name);
1727         
1728         WM_event_add_fileselect(C, op);
1729
1730         return OPERATOR_RUNNING_MODAL;
1731 }
1732
1733 /* function used for WM_OT_save_mainfile too */
1734 static int wm_save_as_mainfile_exec(bContext *C, wmOperator *op)
1735 {
1736         char path[FILE_MAX];
1737         int fileflags;
1738         int copy=0;
1739
1740         save_set_compress(op);
1741         
1742         if(RNA_property_is_set(op->ptr, "filepath"))
1743                 RNA_string_get(op->ptr, "filepath", path);
1744         else {
1745                 BLI_strncpy(path, G.main->name, FILE_MAX);
1746                 untitled(path);
1747         }
1748
1749         if(RNA_property_is_set(op->ptr, "copy"))
1750                 copy = RNA_boolean_get(op->ptr, "copy");
1751         
1752         fileflags= G.fileflags;
1753
1754         /* set compression flag */
1755         if(RNA_boolean_get(op->ptr, "compress"))                fileflags |=  G_FILE_COMPRESS;
1756         else                                                                                    fileflags &= ~G_FILE_COMPRESS;
1757         if(RNA_boolean_get(op->ptr, "relative_remap"))  fileflags |=  G_FILE_RELATIVE_REMAP;
1758         else                                                                                    fileflags &= ~G_FILE_RELATIVE_REMAP;
1759
1760         if ( WM_write_file(C, path, fileflags, op->reports, copy) != 0)
1761                 return OPERATOR_CANCELLED;
1762
1763         WM_event_add_notifier(C, NC_WM|ND_FILESAVE, NULL);
1764
1765         return OPERATOR_FINISHED;
1766 }
1767
1768 /* function used for WM_OT_save_mainfile too */
1769 static int blend_save_check(bContext *UNUSED(C), wmOperator *op)
1770 {
1771         char filepath[FILE_MAX];
1772         RNA_string_get(op->ptr, "filepath", filepath);
1773         if(BLI_replace_extension(filepath, sizeof(filepath), ".blend")) {
1774                 RNA_string_set(op->ptr, "filepath", filepath);
1775                 return TRUE;
1776         }
1777         return FALSE;
1778 }
1779
1780 static void WM_OT_save_as_mainfile(wmOperatorType *ot)
1781 {
1782         ot->name= "Save As Blender File";
1783         ot->idname= "WM_OT_save_as_mainfile";
1784         ot->description="Save the current file in the desired location";
1785         
1786         ot->invoke= wm_save_as_mainfile_invoke;
1787         ot->exec= wm_save_as_mainfile_exec;
1788         ot->check= blend_save_check;
1789         /* ommit window poll so this can work in background mode */
1790
1791         WM_operator_properties_filesel(ot, FOLDERFILE|BLENDERFILE, FILE_BLENDER, FILE_SAVE, WM_FILESEL_FILEPATH);
1792         RNA_def_boolean(ot->srna, "compress", 0, "Compress", "Write compressed .blend file");
1793         RNA_def_boolean(ot->srna, "relative_remap", 1, "Remap Relative", "Remap relative paths when saving in a different directory");
1794         RNA_def_boolean(ot->srna, "copy", 0, "Save Copy", "Save a copy of the actual working state but does not make saved file active.");
1795 }
1796
1797 /* *************** save file directly ******** */
1798
1799 static int wm_save_mainfile_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
1800 {
1801         char name[FILE_MAX];
1802         int check_existing=1;
1803         
1804         /* cancel if no active window */
1805         if (CTX_wm_window(C) == NULL)
1806                 return OPERATOR_CANCELLED;
1807
1808         save_set_compress(op);
1809         
1810         BLI_strncpy(name, G.main->name, FILE_MAX);
1811         untitled(name);
1812         RNA_string_set(op->ptr, "filepath", name);
1813         
1814         if (RNA_struct_find_property(op->ptr, "check_existing"))
1815                 if (RNA_boolean_get(op->ptr, "check_existing")==0)
1816                         check_existing = 0;
1817         
1818         if (G.save_over) {
1819                 if (check_existing)
1820                         uiPupMenuSaveOver(C, op, name);
1821                 else {
1822                         wm_save_as_mainfile_exec(C, op);
1823                 }
1824         } else {
1825                 WM_event_add_fileselect(C, op);
1826         }
1827         
1828         return OPERATOR_RUNNING_MODAL;
1829 }
1830
1831 static void WM_OT_save_mainfile(wmOperatorType *ot)
1832 {
1833         ot->name= "Save Blender File";
1834         ot->idname= "WM_OT_save_mainfile";
1835         ot->description="Save the current Blender file";
1836         
1837         ot->invoke= wm_save_mainfile_invoke;
1838         ot->exec= wm_save_as_mainfile_exec;
1839         ot->check= blend_save_check;
1840         ot->poll= NULL;
1841         
1842         WM_operator_properties_filesel(ot, FOLDERFILE|BLENDERFILE, FILE_BLENDER, FILE_SAVE, WM_FILESEL_FILEPATH);
1843         RNA_def_boolean(ot->srna, "compress", 0, "Compress", "Write compressed .blend file");
1844         RNA_def_boolean(ot->srna, "relative_remap", 0, "Remap Relative", "Remap relative paths when saving in a different directory");
1845 }
1846
1847 /* XXX: move these collada operators to a more appropriate place */
1848 #ifdef WITH_COLLADA
1849
1850 #include "../../collada/collada.h"
1851
1852 static int wm_collada_export_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
1853 {       
1854         if(!RNA_property_is_set(op->ptr, "filepath")) {
1855                 char filepath[FILE_MAX];
1856                 BLI_strncpy(filepath, G.main->name, sizeof(filepath));
1857                 BLI_replace_extension(filepath, sizeof(filepath), ".dae");
1858                 RNA_string_set(op->ptr, "filepath", filepath);
1859         }
1860
1861         WM_event_add_fileselect(C, op);
1862
1863         return OPERATOR_RUNNING_MODAL;
1864 }
1865
1866 /* function used for WM_OT_save_mainfile too */
1867 static int wm_collada_export_exec(bContext *C, wmOperator *op)
1868 {
1869         char filename[FILE_MAX];
1870         
1871         if(!RNA_property_is_set(op->ptr, "filepath")) {
1872                 BKE_report(op->reports, RPT_ERROR, "No filename given");
1873                 return OPERATOR_CANCELLED;
1874         }
1875         
1876         RNA_string_get(op->ptr, "filepath", filename);
1877         collada_export(CTX_data_scene(C), filename);
1878         
1879         return OPERATOR_FINISHED;
1880 }
1881
1882 static void WM_OT_collada_export(wmOperatorType *ot)
1883 {
1884         ot->name= "Export COLLADA";
1885         ot->idname= "WM_OT_collada_export";
1886         
1887         ot->invoke= wm_collada_export_invoke;
1888         ot->exec= wm_collada_export_exec;
1889         ot->poll= WM_operator_winactive;
1890         
1891         WM_operator_properties_filesel(ot, FOLDERFILE|COLLADAFILE, FILE_BLENDER, FILE_SAVE, WM_FILESEL_FILEPATH);
1892 }
1893
1894 /* function used for WM_OT_save_mainfile too */
1895 static int wm_collada_import_exec(bContext *C, wmOperator *op)
1896 {
1897         char filename[FILE_MAX];
1898         
1899         if(!RNA_property_is_set(op->ptr, "filepath")) {
1900                 BKE_report(op->reports, RPT_ERROR, "No filename given");
1901                 return OPERATOR_CANCELLED;
1902         }
1903
1904         RNA_string_get(op->ptr, "filepath", filename);
1905         collada_import(C, filename);
1906         
1907         return OPERATOR_FINISHED;
1908 }
1909
1910 static void WM_OT_collada_import(wmOperatorType *ot)
1911 {
1912         ot->name= "Import COLLADA";
1913         ot->idname= "WM_OT_collada_import";
1914         
1915         ot->invoke= WM_operator_filesel;
1916         ot->exec= wm_collada_import_exec;
1917         ot->poll= WM_operator_winactive;
1918         
1919         WM_operator_properties_filesel(ot, FOLDERFILE|COLLADAFILE, FILE_BLENDER, FILE_OPENFILE, WM_FILESEL_FILEPATH);
1920 }
1921
1922 #endif
1923
1924
1925
1926 /* *********************** */
1927
1928 static void WM_OT_window_fullscreen_toggle(wmOperatorType *ot)
1929 {
1930         ot->name= "Toggle Fullscreen";
1931         ot->idname= "WM_OT_window_fullscreen_toggle";
1932         ot->description="Toggle the current window fullscreen";
1933
1934         ot->exec= wm_window_fullscreen_toggle_exec;
1935         ot->poll= WM_operator_winactive;
1936 }
1937
1938 static int wm_exit_blender_op(bContext *C, wmOperator *op)
1939 {
1940         WM_operator_free(op);
1941         
1942         WM_exit(C);     
1943         
1944         return OPERATOR_FINISHED;
1945 }
1946
1947 static void WM_OT_quit_blender(wmOperatorType *ot)
1948 {
1949         ot->name= "Quit Blender";
1950         ot->idname= "WM_OT_quit_blender";
1951         ot->description= "Quit Blender";
1952
1953         ot->invoke= WM_operator_confirm;
1954         ot->exec= wm_exit_blender_op;
1955         ot->poll= WM_operator_winactive;
1956 }
1957
1958 /* *********************** */
1959 #if defined(WIN32)
1960 static int console= 1;
1961 void WM_toggle_console(bContext *UNUSED(C), short show)
1962 {
1963         if(show) {
1964                 ShowWindow(GetConsoleWindow(),SW_SHOW);
1965                 console= 1;
1966         }
1967         else {
1968                 ShowWindow(GetConsoleWindow(),SW_HIDE);
1969                 console= 0;
1970         }
1971 }
1972
1973 static int wm_toggle_console_op(bContext *C, wmOperator *UNUSED(op))
1974 {
1975         if(console) {
1976                 WM_toggle_console(C, 0);
1977         }
1978         else {
1979                 WM_toggle_console(C, 1);
1980         }
1981         return OPERATOR_FINISHED;
1982 }
1983
1984 static void WM_OT_toggle_console(wmOperatorType *ot)
1985 {
1986         ot->name= "Toggle System Console";
1987         ot->idname= "WM_OT_toggle_console";
1988         ot->description= "Toggle System Console";
1989         
1990         ot->exec= wm_toggle_console_op;
1991         ot->poll= WM_operator_winactive;
1992 }
1993 #endif
1994
1995 /* ************ default paint cursors, draw always around cursor *********** */
1996 /*
1997  - returns handler to free 
1998  - poll(bContext): returns 1 if draw should happen
1999  - draw(bContext): drawing callback for paint cursor
2000 */
2001
2002 void *WM_paint_cursor_activate(wmWindowManager *wm, int (*poll)(bContext *C),
2003                                    wmPaintCursorDraw draw, void *customdata)
2004 {
2005         wmPaintCursor *pc= MEM_callocN(sizeof(wmPaintCursor), "paint cursor");
2006         
2007         BLI_addtail(&wm->paintcursors, pc);
2008         
2009         pc->customdata = customdata;
2010         pc->poll= poll;
2011         pc->draw= draw;
2012         
2013         return pc;
2014 }
2015
2016 void WM_paint_cursor_end(wmWindowManager *wm, void *handle)
2017 {
2018         wmPaintCursor *pc;
2019         
2020         for(pc= wm->paintcursors.first; pc; pc= pc->next) {
2021                 if(pc == (wmPaintCursor *)handle) {
2022                         BLI_remlink(&wm->paintcursors, pc);
2023                         MEM_freeN(pc);
2024                         return;
2025                 }
2026         }
2027 }
2028
2029 /* ************ window gesture operator-callback definitions ************** */
2030 /*
2031  * These are default callbacks for use in operators requiring gesture input
2032  */
2033
2034 /* **************** Border gesture *************** */
2035
2036 /* Border gesture has two types:
2037    1) WM_GESTURE_CROSS_RECT: starts a cross, on mouse click it changes to border 
2038    2) WM_GESTURE_RECT: starts immediate as a border, on mouse click or release it ends
2039
2040    It stores 4 values (xmin, xmax, ymin, ymax) and event it ended with (event_type)
2041 */
2042
2043 static int border_apply(bContext *C, wmOperator *op, int gesture_mode)
2044 {
2045         wmGesture *gesture= op->customdata;
2046         rcti *rect= gesture->customdata;
2047         
2048         if(rect->xmin > rect->xmax)
2049                 SWAP(int, rect->xmin, rect->xmax);
2050         if(rect->ymin > rect->ymax)
2051                 SWAP(int, rect->ymin, rect->ymax);
2052         
2053         if(rect->xmin==rect->xmax || rect->ymin==rect->ymax)
2054                 return 0;
2055                 
2056         /* operator arguments and storage. */
2057         RNA_int_set(op->ptr, "xmin", rect->xmin);
2058         RNA_int_set(op->ptr, "ymin", rect->ymin);
2059         RNA_int_set(op->ptr, "xmax", rect->xmax);
2060         RNA_int_set(op->ptr, "ymax", rect->ymax);
2061         
2062         /* XXX weak; border should be configured for this without reading event types */
2063         if( RNA_struct_find_property(op->ptr, "gesture_mode") )
2064                 RNA_int_set(op->ptr, "gesture_mode", gesture_mode);
2065
2066         op->type->exec(C, op);
2067         
2068         return 1;
2069 }
2070
2071 static void wm_gesture_end(bContext *C, wmOperator *op)
2072 {
2073         wmGesture *gesture= op->customdata;
2074         
2075         WM_gesture_end(C, gesture);     /* frees gesture itself, and unregisters from window */
2076         op->customdata= NULL;
2077
2078         ED_area_tag_redraw(CTX_wm_area(C));
2079         
2080         if( RNA_struct_find_property(op->ptr, "cursor") )
2081                 WM_cursor_restore(CTX_wm_window(C));
2082 }
2083
2084 int WM_border_select_invoke(bContext *C, wmOperator *op, wmEvent *event)
2085 {
2086         if(ISTWEAK(event->type))
2087                 op->customdata= WM_gesture_new(C, event, WM_GESTURE_RECT);
2088         else
2089                 op->customdata= WM_gesture_new(C, event, WM_GESTURE_CROSS_RECT);
2090
2091         /* add modal handler */
2092         WM_event_add_modal_handler(C, op);
2093         
2094         wm_gesture_tag_redraw(C);
2095
2096         return OPERATOR_RUNNING_MODAL;
2097 }
2098
2099 int WM_border_select_modal(bContext *C, wmOperator *op, wmEvent *event)
2100 {
2101         wmGesture *gesture= op->customdata;
2102         rcti *rect= gesture->customdata;
2103         int sx, sy;
2104         
2105         if(event->type== MOUSEMOVE) {
2106                 wm_subwindow_getorigin(CTX_wm_window(C), gesture->swinid, &sx, &sy);
2107
2108                 if(gesture->type==WM_GESTURE_CROSS_RECT && gesture->mode==0) {
2109                         rect->xmin= rect->xmax= event->x - sx;
2110                         rect->ymin= rect->ymax= event->y - sy;
2111                 }
2112                 else {
2113                         rect->xmax= event->x - sx;
2114                         rect->ymax= event->y - sy;
2115                 }
2116
2117                 wm_gesture_tag_redraw(C);
2118         }
2119         else if (event->type==EVT_MODAL_MAP) {
2120                 switch (event->val) {
2121                 case GESTURE_MODAL_BEGIN:
2122                         if(gesture->type==WM_GESTURE_CROSS_RECT && gesture->mode==0) {
2123                                 gesture->mode= 1;
2124                                 wm_gesture_tag_redraw(C);
2125                         }
2126                         break;
2127                 case GESTURE_MODAL_SELECT:
2128                 case GESTURE_MODAL_DESELECT:
2129                 case GESTURE_MODAL_IN:
2130                 case GESTURE_MODAL_OUT:
2131                         if(border_apply(C, op, event->val)) {
2132                                 wm_gesture_end(C, op);
2133                                 return OPERATOR_FINISHED;
2134                         }
2135                         wm_gesture_end(C, op);
2136                         return OPERATOR_CANCELLED;
2137                         break;
2138
2139                 case GESTURE_MODAL_CANCEL:
2140                         wm_gesture_end(C, op);
2141                         return OPERATOR_CANCELLED;
2142                 }
2143
2144         }
2145 //      // Allow view navigation???
2146 //      else {
2147 //              return OPERATOR_PASS_THROUGH;
2148 //      }
2149
2150         return OPERATOR_RUNNING_MODAL;
2151 }
2152
2153 /* **************** circle gesture *************** */
2154 /* works now only for selection or modal paint stuff, calls exec while hold mouse, exit on release */
2155
2156 #ifdef GESTURE_MEMORY
2157 int circle_select_size= 25; // XXX - need some operator memory thing\!
2158 #endif
2159
2160 int WM_gesture_circle_invoke(bContext *C, wmOperator *op, wmEvent *event)
2161 {
2162         op->customdata= WM_gesture_new(C, event, WM_GESTURE_CIRCLE);
2163         
2164         /* add modal handler */
2165         WM_event_add_modal_handler(C, op);
2166         
2167         wm_gesture_tag_redraw(C);
2168         
2169         return OPERATOR_RUNNING_MODAL;
2170 }
2171
2172 static void gesture_circle_apply(bContext *C, wmOperator *op)
2173 {
2174         wmGesture *gesture= op->customdata;
2175         rcti *rect= gesture->customdata;
2176         
2177         if(RNA_int_get(op->ptr, "gesture_mode")==GESTURE_MODAL_NOP)
2178                 return;
2179
2180         /* operator arguments and storage. */
2181         RNA_int_set(op->ptr, "x", rect->xmin);
2182         RNA_int_set(op->ptr, "y", rect->ymin);
2183         RNA_int_set(op->ptr, "radius", rect->xmax);
2184         
2185         if(op->type->exec)
2186                 op->type->exec(C, op);
2187
2188 #ifdef GESTURE_MEMORY
2189         circle_select_size= rect->xmax;
2190 #endif
2191 }
2192
2193 int WM_gesture_circle_modal(bContext *C, wmOperator *op, wmEvent *event)
2194 {
2195         wmGesture *gesture= op->customdata;
2196         rcti *rect= gesture->customdata;
2197         int sx, sy;
2198
2199         if(event->type== MOUSEMOVE) {
2200                 wm_subwindow_getorigin(CTX_wm_window(C), gesture->swinid, &sx, &sy);
2201
2202                 rect->xmin= event->x - sx;
2203                 rect->ymin= event->y - sy;
2204
2205                 wm_gesture_tag_redraw(C);
2206
2207                 if(gesture->mode)
2208                         gesture_circle_apply(C, op);
2209         }
2210         else if (event->type==EVT_MODAL_MAP) {
2211                 switch (event->val) {
2212                 case GESTURE_MODAL_CIRCLE_ADD:
2213                         rect->xmax += 2 + rect->xmax/10;
2214                         wm_gesture_tag_redraw(C);
2215                         break;
2216                 case GESTURE_MODAL_CIRCLE_SUB:
2217                         rect->xmax -= 2 + rect->xmax/10;
2218                         if(rect->xmax < 1) rect->xmax= 1;
2219                         wm_gesture_tag_redraw(C);
2220                         break;
2221                 case GESTURE_MODAL_SELECT:
2222                 case GESTURE_MODAL_DESELECT:
2223                 case GESTURE_MODAL_NOP:
2224                         if(RNA_struct_find_property(op->ptr, "gesture_mode"))
2225                                 RNA_int_set(op->ptr, "gesture_mode", event->val);
2226
2227                         if(event->val != GESTURE_MODAL_NOP) {
2228                                 /* apply first click */
2229                                 gesture_circle_apply(C, op);
2230                                 gesture->mode= 1;
2231                                 wm_gesture_tag_redraw(C);
2232                         }
2233                         break;
2234
2235                 case GESTURE_MODAL_CANCEL:
2236                 case GESTURE_MODAL_CONFIRM:
2237                         wm_gesture_end(C, op);
2238                         return OPERATOR_FINISHED; /* use finish or we dont get an undo */
2239                 }
2240         }
2241 //      // Allow view navigation???
2242 //      else {
2243 //              return OPERATOR_PASS_THROUGH;
2244 //      }
2245
2246         return OPERATOR_RUNNING_MODAL;
2247 }
2248
2249 #if 0
2250 /* template to copy from */
2251 void WM_OT_circle_gesture(wmOperatorType *ot)
2252 {
2253         ot->name= "Circle Gesture";
2254         ot->idname= "WM_OT_circle_gesture";
2255         ot->description="Enter rotate mode with a circular gesture";
2256         
2257         ot->invoke= WM_gesture_circle_invoke;
2258         ot->modal= WM_gesture_circle_modal;
2259         
2260         ot->poll= WM_operator_winactive;
2261         
2262         RNA_def_property(ot->srna, "x", PROP_INT, PROP_NONE);
2263         RNA_def_property(ot->srna, "y", PROP_INT, PROP_NONE);
2264         RNA_def_property(ot->srna, "radius", PROP_INT, PROP_NONE);
2265
2266 }
2267 #endif
2268
2269 /* **************** Tweak gesture *************** */
2270
2271 static void tweak_gesture_modal(bContext *C, wmEvent *event)
2272 {
2273         wmWindow *window= CTX_wm_window(C);
2274         wmGesture *gesture= window->tweak;
2275         rcti *rect= gesture->customdata;
2276         int sx, sy, val;
2277         
2278         switch(event->type) {
2279                 case MOUSEMOVE:
2280                 case INBETWEEN_MOUSEMOVE:
2281                         
2282                         wm_subwindow_getorigin(window, gesture->swinid, &sx, &sy);
2283                         
2284                         rect->xmax= event->x - sx;
2285                         rect->ymax= event->y - sy;
2286                         
2287                         if((val= wm_gesture_evaluate(gesture))) {
2288                                 wmEvent tevent;
2289
2290                                 tevent= *(window->eventstate);
2291                                 if(gesture->event_type==LEFTMOUSE)
2292                                         tevent.type= EVT_TWEAK_L;
2293                                 else if(gesture->event_type==RIGHTMOUSE)
2294                                         tevent.type= EVT_TWEAK_R;
2295                                 else
2296                                         tevent.type= EVT_TWEAK_M;
2297                                 tevent.val= val;
2298                                 /* mouse coords! */
2299                                 wm_event_add(window, &tevent);
2300                                 
2301                                 WM_gesture_end(C, gesture);     /* frees gesture itself, and unregisters from window */
2302                         }
2303                         
2304                         break;
2305                         
2306                 case LEFTMOUSE:
2307                 case RIGHTMOUSE:
2308                 case MIDDLEMOUSE:
2309                         if(gesture->event_type==event->type) {
2310                                 WM_gesture_end(C, gesture);
2311
2312                                 /* when tweak fails we should give the other keymap entries a chance */
2313                                 event->val= KM_RELEASE;
2314                         }
2315                         break;
2316                 default:
2317                         if(!ISTIMER(event->type)) {
2318                                 WM_gesture_end(C, gesture);
2319                         }
2320                         break;
2321         }
2322 }
2323
2324 /* standard tweak, called after window handlers passed on event */
2325 void wm_tweakevent_test(bContext *C, wmEvent *event, int action)
2326 {
2327         wmWindow *win= CTX_wm_window(C);
2328         
2329         if(win->tweak==NULL) {
2330                 if(CTX_wm_region(C)) {
2331                         if(event->val==KM_PRESS) { 
2332                                 if( ELEM3(event->type, LEFTMOUSE, MIDDLEMOUSE, RIGHTMOUSE) )
2333                                         win->tweak= WM_gesture_new(C, event, WM_GESTURE_TWEAK);
2334                         }
2335                 }
2336         }
2337         else {
2338                 /* no tweaks if event was handled */
2339                 if((action & WM_HANDLER_BREAK)) {
2340                         WM_gesture_end(C, win->tweak);
2341                 }
2342                 else
2343                         tweak_gesture_modal(C, event);
2344         }
2345 }
2346
2347 /* *********************** lasso gesture ****************** */
2348
2349 int WM_gesture_lasso_invoke(bContext *C, wmOperator *op, wmEvent *event)
2350 {
2351         op->customdata= WM_gesture_new(C, event, WM_GESTURE_LASSO);
2352         
2353         /* add modal handler */
2354         WM_event_add_modal_handler(C, op);
2355         
2356         wm_gesture_tag_redraw(C);
2357         
2358         if( RNA_struct_find_property(op->ptr, "cursor") )
2359                 WM_cursor_modal(CTX_wm_window(C), RNA_int_get(op->ptr, "cursor"));
2360         
2361         return OPERATOR_RUNNING_MODAL;
2362 }
2363
2364 int WM_gesture_lines_invoke(bContext *C, wmOperator *op, wmEvent *event)
2365 {
2366         op->customdata= WM_gesture_new(C, event, WM_GESTURE_LINES);
2367         
2368         /* add modal handler */
2369         WM_event_add_modal_handler(C, op);
2370         
2371         wm_gesture_tag_redraw(C);
2372         
2373         if( RNA_struct_find_property(op->ptr, "cursor") )
2374                 WM_cursor_modal(CTX_wm_window(C), RNA_int_get(op->ptr, "cursor"));
2375         
2376         return OPERATOR_RUNNING_MODAL;
2377 }
2378
2379
2380 static void gesture_lasso_apply(bContext *C, wmOperator *op)
2381 {
2382         wmGesture *gesture= op->customdata;
2383         PointerRNA itemptr;
2384         float loc[2];
2385         int i;
2386         short *lasso= gesture->customdata;
2387         
2388         /* operator storage as path. */
2389
2390         for(i=0; i<gesture->points; i++, lasso+=2) {
2391                 loc[0]= lasso[0];
2392                 loc[1]= lasso[1];
2393                 RNA_collection_add(op->ptr, "path", &itemptr);
2394                 RNA_float_set_array(&itemptr, "loc", loc);
2395         }
2396         
2397         wm_gesture_end(C, op);
2398                 
2399         if(op->type->exec)
2400                 op->type->exec(C, op);
2401         
2402 }
2403
2404 int WM_gesture_lasso_modal(bContext *C, wmOperator *op, wmEvent *event)
2405 {
2406         wmGesture *gesture= op->customdata;
2407         int sx, sy;
2408         
2409         switch(event->type) {
2410                 case MOUSEMOVE:
2411                 case INBETWEEN_MOUSEMOVE:
2412                         
2413                         wm_gesture_tag_redraw(C);
2414                         
2415                         wm_subwindow_getorigin(CTX_wm_window(C), gesture->swinid, &sx, &sy);
2416
2417                         if(gesture->points == gesture->size) {
2418                                 short *old_lasso = gesture->customdata;
2419                                 gesture->customdata= MEM_callocN(2*sizeof(short)*(gesture->size + WM_LASSO_MIN_POINTS), "lasso points");
2420                                 memcpy(gesture->customdata, old_lasso, 2*sizeof(short)*gesture->size);
2421                                 gesture->size = gesture->size + WM_LASSO_MIN_POINTS;
2422                                 MEM_freeN(old_lasso);
2423                                 printf("realloc\n");
2424                         }
2425
2426                         {
2427                                 short x, y;
2428                                 short *lasso= gesture->customdata;
2429                                 
2430                                 lasso += (2 * gesture->points - 2);
2431                                 x = (event->x - sx - lasso[0]);
2432                                 y = (event->y - sy - lasso[1]);
2433                                 
2434                                 /* make a simple distance check to get a smoother lasso
2435                                    add only when at least 2 pixels between this and previous location */
2436                                 if((x*x+y*y) > 4) {
2437                                         lasso += 2;
2438                                         lasso[0] = event->x - sx;
2439                                         lasso[1] = event->y - sy;
2440                                         gesture->points++;
2441                                 }
2442                         }
2443                         break;
2444                         
2445                 case LEFTMOUSE:
2446                 case MIDDLEMOUSE:
2447                 case RIGHTMOUSE:
2448                         if(event->val==KM_RELEASE) {    /* key release */
2449                                 gesture_lasso_apply(C, op);
2450                                 return OPERATOR_FINISHED;
2451                         }
2452                         break;
2453                 case ESCKEY:
2454                         wm_gesture_end(C, op);
2455                         return OPERATOR_CANCELLED;
2456         }
2457         return OPERATOR_RUNNING_MODAL;
2458 }
2459
2460 int WM_gesture_lines_modal(bContext *C, wmOperator *op, wmEvent *event)
2461 {
2462         return WM_gesture_lasso_modal(C, op, event);
2463 }
2464
2465 #if 0
2466 /* template to copy from */
2467
2468 static int gesture_lasso_exec(bContext *C, wmOperator *op)
2469 {
2470         RNA_BEGIN(op->ptr, itemptr, "path") {
2471                 float loc[2];
2472                 
2473                 RNA_float_get_array(&itemptr, "loc", loc);
2474                 printf("Location: %f %f\n", loc[0], loc[1]);
2475         }
2476         RNA_END;
2477         
2478         return OPERATOR_FINISHED;
2479 }
2480
2481 void WM_OT_lasso_gesture(wmOperatorType *ot)
2482 {
2483         PropertyRNA *prop;
2484         
2485         ot->name= "Lasso Gesture";
2486         ot->idname= "WM_OT_lasso_gesture";
2487         ot->description="Select objects within the lasso as you move the pointer";
2488         
2489         ot->invoke= WM_gesture_lasso_invoke;
2490         ot->modal= WM_gesture_lasso_modal;
2491         ot->exec= gesture_lasso_exec;
2492         
2493         ot->poll= WM_operator_winactive;
2494         
2495         prop= RNA_def_property(ot->srna, "path", PROP_COLLECTION, PROP_NONE);
2496         RNA_def_property_struct_runtime(prop, &RNA_OperatorMousePath);
2497 }
2498 #endif
2499
2500 /* *********************** straight line gesture ****************** */
2501
2502 static int straightline_apply(bContext *C, wmOperator *op)
2503 {
2504         wmGesture *gesture= op->customdata;
2505         rcti *rect= gesture->customdata;
2506         
2507         if(rect->xmin==rect->xmax && rect->ymin==rect->ymax)
2508                 return 0;
2509         
2510         /* operator arguments and storage. */
2511         RNA_int_set(op->ptr, "xstart", rect->xmin);
2512         RNA_int_set(op->ptr, "ystart", rect->ymin);
2513         RNA_int_set(op->ptr, "xend", rect->xmax);
2514         RNA_int_set(op->ptr, "yend", rect->ymax);
2515
2516         if(op->type->exec)
2517                 op->type->exec(C, op);
2518         
2519         return 1;
2520 }
2521
2522
2523 int WM_gesture_straightline_invoke(bContext *C, wmOperator *op, wmEvent *event)
2524 {
2525         op->customdata= WM_gesture_new(C, event, WM_GESTURE_STRAIGHTLINE);
2526         
2527         /* add modal handler */
2528         WM_event_add_modal_handler(C, op);
2529         
2530         wm_gesture_tag_redraw(C);
2531         
2532         if( RNA_struct_find_property(op->ptr, "cursor") )
2533                 WM_cursor_modal(CTX_wm_window(C), RNA_int_get(op->ptr, "cursor"));
2534                 
2535         return OPERATOR_RUNNING_MODAL;
2536 }
2537
2538 int WM_gesture_straightline_modal(bContext *C, wmOperator *op, wmEvent *event)
2539 {
2540         wmGesture *gesture= op->customdata;
2541         rcti *rect= gesture->customdata;
2542         int sx, sy;
2543         
2544         if(event->type== MOUSEMOVE) {
2545                 wm_subwindow_getorigin(CTX_wm_window(C), gesture->swinid, &sx, &sy);
2546                 
2547                 if(gesture->mode==0) {
2548                         rect->xmin= rect->xmax= event->x - sx;
2549                         rect->ymin= rect->ymax= event->y - sy;
2550                 }
2551                 else {
2552                         rect->xmax= event->x - sx;
2553                         rect->ymax= event->y - sy;
2554                         straightline_apply(C, op);
2555                 }
2556                 
2557                 wm_gesture_tag_redraw(C);
2558         }
2559         else if (event->type==EVT_MODAL_MAP) {
2560                 switch (event->val) {
2561                         case GESTURE_MODAL_BEGIN:
2562                                 if(gesture->mode==0) {
2563                                         gesture->mode= 1;
2564                                         wm_gesture_tag_redraw(C);
2565                                 }
2566                                 break;
2567                         case GESTURE_MODAL_SELECT:
2568                                 if(straightline_apply(C, op)) {
2569                                         wm_gesture_end(C, op);
2570                                         return OPERATOR_FINISHED;
2571                                 }
2572                                 wm_gesture_end(C, op);
2573                                 return OPERATOR_CANCELLED;
2574                                 break;
2575                                 
2576                         case GESTURE_MODAL_CANCEL:
2577                                 wm_gesture_end(C, op);
2578                                 return OPERATOR_CANCELLED;
2579                 }
2580                 
2581         }
2582
2583         return OPERATOR_RUNNING_MODAL;
2584 }
2585
2586 #if 0
2587 /* template to copy from */
2588 void WM_OT_straightline_gesture(wmOperatorType *ot)
2589 {
2590         PropertyRNA *prop;
2591         
2592         ot->name= "Straight Line Gesture";
2593         ot->idname= "WM_OT_straightline_gesture";
2594         ot->description="Draw a straight line as you move the pointer";
2595         
2596         ot->invoke= WM_gesture_straightline_invoke;
2597         ot->modal= WM_gesture_straightline_modal;
2598         ot->exec= gesture_straightline_exec;
2599         
2600         ot->poll= WM_operator_winactive;
2601         
2602         WM_operator_properties_gesture_straightline(ot, 0);
2603 }
2604 #endif
2605
2606 /* *********************** radial control ****************** */
2607
2608 const int WM_RADIAL_CONTROL_DISPLAY_SIZE = 200;
2609
2610 typedef struct wmRadialControl {
2611         int mode;
2612         float initial_value, value, max_value;
2613         float col[4], tex_col[4];
2614         int initial_mouse[2];
2615         void *cursor;
2616         GLuint tex;
2617 } wmRadialControl;
2618
2619 static void wm_radial_control_paint(bContext *C, int x, int y, void *customdata)
2620 {
2621         wmRadialControl *rc = (wmRadialControl*)customdata;
2622         ARegion *ar = CTX_wm_region(C);
2623         float r1=0.0f, r2=0.0f, r3=0.0f, angle=0.0f;
2624
2625         // int hit = 0;
2626         
2627         if(rc->mode == WM_RADIALCONTROL_STRENGTH)
2628                 rc->tex_col[3]= (rc->value + 0.5);
2629
2630         if(rc->mode == WM_RADIALCONTROL_SIZE) {
2631                 r1= rc->value;
2632                 r2= rc->initial_value;
2633                 r3= r1;
2634         } else if(rc->mode == WM_RADIALCONTROL_STRENGTH) {
2635                 r1= (1 - rc->value) * WM_RADIAL_CONTROL_DISPLAY_SIZE;
2636                 r2= r3= WM_RADIAL_CONTROL_DISPLAY_SIZE;
2637         } else if(rc->mode == WM_RADIALCONTROL_ANGLE) {
2638                 r1= r2= r3= WM_RADIAL_CONTROL_DISPLAY_SIZE;
2639                 angle = rc->value;
2640         }
2641
2642         /* Keep cursor in the original place */
2643         x = rc->initial_mouse[0] - ar->winrct.xmin;
2644         y = rc->initial_mouse[1] - ar->winrct.ymin;
2645
2646         glTranslatef((float)x, (float)y, 0.0f);
2647
2648         glEnable(GL_BLEND);
2649
2650         if(rc->mode == WM_RADIALCONTROL_ANGLE) {
2651                 glRotatef(angle, 0, 0, 1);
2652         }
2653
2654         if (rc->tex) {
2655                 glBindTexture(GL_TEXTURE_2D, rc->tex);
2656
2657                 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
2658                 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
2659
2660                 glEnable(GL_TEXTURE_2D);
2661                 glBegin(GL_QUADS);
2662                 glColor4f(rc->tex_col[0], rc->tex_col[1], rc->tex_col[2], rc->tex_col[3]);
2663                 glTexCoord2f(0,0);
2664                 glVertex2f(-r3, -r3);
2665                 glTexCoord2f(1,0);
2666                 glVertex2f(r3, -r3);
2667                 glTexCoord2f(1,1);
2668                 glVertex2f(r3, r3);
2669                 glTexCoord2f(0,1);
2670                 glVertex2f(-r3, r3);
2671                 glEnd();
2672                 glDisable(GL_TEXTURE_2D);
2673         }
2674
2675         if(rc->mode == WM_RADIALCONTROL_ANGLE) {
2676                 glColor4f(rc->col[0], rc->col[1], rc->col[2], rc->col[3]);
2677                 glEnable(GL_LINE_SMOOTH);
2678                 glRotatef(-angle, 0, 0, 1);
2679                 fdrawline(0, 0, WM_RADIAL_CONTROL_DISPLAY_SIZE, 0);
2680                 glRotatef(angle, 0, 0, 1);
2681                 fdrawline(0, 0, WM_RADIAL_CONTROL_DISPLAY_SIZE, 0);
2682                 glDisable(GL_LINE_SMOOTH);
2683         }
2684
2685         glColor4f(rc->col[0], rc->col[1], rc->col[2], rc->col[3]);
2686         glutil_draw_lined_arc(0.0, M_PI*2.0, r1, 40);
2687         glutil_draw_lined_arc(0.0, M_PI*2.0, r2, 40);
2688         glDisable(GL_BLEND);
2689 }
2690
2691 int WM_radial_control_modal(bContext *C, wmOperator *op, wmEvent *event)
2692 {
2693         wmRadialControl *rc = (wmRadialControl*)op->customdata;
2694         int mode, initial_mouse[2], delta[2];
2695         float dist;
2696         double new_value = RNA_float_get(op->ptr, "new_value");
2697         int ret = OPERATOR_RUNNING_MODAL;
2698         // float initial_value = RNA_float_get(op->ptr, "initial_value");
2699
2700         mode = RNA_int_get(op->ptr, "mode");
2701         RNA_int_get_array(op->ptr, "initial_mouse", initial_mouse);
2702
2703         switch(event->type) {
2704         case MOUSEMOVE:
2705                 delta[0]= initial_mouse[0] - event->x;
2706                 delta[1]= initial_mouse[1] - event->y;
2707
2708                 //if (mode == WM_RADIALCONTROL_SIZE) 
2709                 //      delta[0]+= initial_value;
2710                 //else if(mode == WM_RADIALCONTROL_STRENGTH)
2711                 //      delta[0]+= WM_RADIAL_CONTROL_DISPLAY_SIZE * (1 - initial_value);
2712                 //else if(mode == WM_RADIALCONTROL_ANGLE) {
2713                 //      delta[0]+= WM_RADIAL_CONTROL_DISPLAY_SIZE * cos(initial_value*M_PI/180.0f);
2714                 //      delta[1]+= WM_RADIAL_CONTROL_DISPLAY_SIZE * sin(initial_value*M_PI/180.0f);
2715                 //}
2716
2717                 dist= sqrt(delta[0]*delta[0]+delta[1]*delta[1]);
2718
2719                 if(mode == WM_RADIALCONTROL_SIZE)
2720                         new_value = dist;
2721                 else if(mode == WM_RADIALCONTROL_STRENGTH) {
2722                         new_value = 1 - dist / WM_RADIAL_CONTROL_DISPLAY_SIZE;
2723                 } else if(mode == WM_RADIALCONTROL_ANGLE)
2724                         new_value = ((int)(atan2(delta[1], delta[0]) * (180.0 / M_PI)) + 180);
2725                 
2726                 if(event->ctrl) {
2727                         if(mode == WM_RADIALCONTROL_STRENGTH)
2728                                 new_value = ((int)ceil(new_value * 10.f) * 10.0f) / 100.f;
2729                         else
2730                                 new_value = ((int)new_value + 5) / 10*10;
2731                 }
2732                 
2733                 break;
2734         case ESCKEY:
2735         case RIGHTMOUSE:
2736                 ret = OPERATOR_CANCELLED;
2737                 break;
2738         case LEFTMOUSE:
2739         case PADENTER:
2740                 op->type->exec(C, op);
2741                 ret = OPERATOR_FINISHED;
2742                 break;
2743         }
2744
2745         /* Clamp */
2746         if(new_value > rc->max_value)
2747                 new_value = rc->max_value;
2748         else if(new_value < 0)
2749                 new_value = 0;
2750
2751         /* Update paint data */
2752         rc->value = new_value;
2753
2754         RNA_float_set(op->ptr, "new_value", new_value);
2755
2756         if(ret != OPERATOR_RUNNING_MODAL) {
2757                 WM_paint_cursor_end(CTX_wm_manager(C), rc->cursor);
2758                 MEM_freeN(rc);
2759         }
2760         
2761         ED_region_tag_redraw(CTX_wm_region(C));
2762
2763         //if (ret != OPERATOR_RUNNING_MODAL) {
2764         //      wmWindow *win = CTX_wm_window(C);
2765         //      WM_cursor_restore(win);
2766         //}
2767
2768         return ret;
2769 }
2770
2771 /* Expects the operator customdata to be an ImBuf (or NULL) */
2772 int WM_radial_control_invoke(bContext *C, wmOperator *op, wmEvent *event)
2773 {
2774         wmRadialControl *rc = MEM_callocN(sizeof(wmRadialControl), "radial control");
2775         // wmWindow *win = CTX_wm_window(C);
2776         int mode = RNA_int_get(op->ptr, "mode");
2777         float initial_value = RNA_float_get(op->ptr, "initial_value");
2778         //float initial_size = RNA_float_get(op->ptr, "initial_size");
2779         int mouse[2];
2780
2781         mouse[0]= event->x;
2782         mouse[1]= event->y;
2783
2784         //if (initial_size == 0)
2785         //      initial_size = WM_RADIAL_CONTROL_DISPLAY_SIZE;
2786
2787         if(mode == WM_RADIALCONTROL_SIZE) {
2788                 rc->max_value = 200;
2789                 mouse[0]-= initial_value;
2790         }
2791         else if(mode == WM_RADIALCONTROL_STRENGTH) {
2792                 rc->max_value = 1;
2793                 mouse[0]-= WM_RADIAL_CONTROL_DISPLAY_SIZE * (1 - initial_value);
2794         }
2795         else if(mode == WM_RADIALCONTROL_ANGLE) {
2796                 rc->max_value = 360;
2797                 mouse[0]-= WM_RADIAL_CONTROL_DISPLAY_SIZE * cos(initial_value);
2798                 mouse[1]-= WM_RADIAL_CONTROL_DISPLAY_SIZE * sin(initial_value);
2799                 initial_value *= 180.0f/M_PI;
2800         }
2801
2802         if(op->customdata) {
2803                 ImBuf *im = (ImBuf*)op->customdata;
2804                 /* Build GL texture */
2805                 glGenTextures(1, &rc->tex);
2806                 glBindTexture(GL_TEXTURE_2D, rc->tex);
2807                 glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, im->x, im->y, 0, GL_ALPHA, GL_FLOAT, im->rect_float);
2808                 MEM_freeN(im->rect_float);
2809                 MEM_freeN(im);
2810         }
2811
2812         RNA_float_get_array(op->ptr, "color", rc->col);
2813         RNA_float_get_array(op->ptr, "texture_color", rc->tex_col);
2814
2815         RNA_int_set_array(op->ptr, "initial_mouse", mouse);
2816         RNA_float_set(op->ptr, "new_value", initial_value);
2817                 
2818         op->customdata = rc;
2819         rc->mode = mode;
2820         rc->initial_value = initial_value;
2821         rc->initial_mouse[0] = mouse[0];
2822         rc->initial_mouse[1] = mouse[1];
2823         rc->cursor = WM_paint_cursor_activate(CTX_wm_manager(C), op->type->poll,
2824                                                   wm_radial_control_paint, op->customdata);
2825
2826         //WM_cursor_modal(win, CURSOR_NONE);
2827
2828         /* add modal handler */
2829         WM_event_add_modal_handler(C, op);
2830         
2831         WM_radial_control_modal(C, op, event);
2832         
2833         return OPERATOR_RUNNING_MODAL;
2834 }
2835
2836 /* Gets a descriptive string of the operation */
2837 void WM_radial_control_string(wmOperator *op, char str[], int maxlen)
2838 {
2839         int mode = RNA_int_get(op->ptr, "mode");
2840         float v = RNA_float_get(op->ptr, "new_value");
2841
2842         if(mode == WM_RADIALCONTROL_SIZE)
2843                 BLI_snprintf(str, maxlen, "Size: %d", (int)v);
2844         else if(mode == WM_RADIALCONTROL_STRENGTH)
2845                 BLI_snprintf(str, maxlen, "Strength: %d", (int)v);
2846         else if(mode == WM_RADIALCONTROL_ANGLE)
2847                 BLI_snprintf(str, maxlen, "Angle: %d", (int)(v * 180.0f/M_PI));
2848 }
2849
2850 /** Important: this doesn't define an actual operator, it
2851         just sets up the common parts of the radial control op. **/
2852 void WM_OT_radial_control_partial(wmOperatorType *ot)
2853 {
2854         static EnumPropertyItem radial_mode_items[] = {
2855                 {WM_RADIALCONTROL_SIZE, "SIZE", 0, "Size", ""},
2856                 {WM_RADIALCONTROL_STRENGTH, "STRENGTH", 0, "Strength", ""},
2857                 {WM_RADIALCONTROL_ANGLE, "ANGLE", 0, "Angle", ""},
2858                 {0, NULL, 0, NULL, NULL}};
2859         static float color[4] = {1.0f, 1.0f, 1.0f, 0.5f};
2860         static float tex_color[4] = {1.0f, 1.0f, 1.0f, 1.0f};
2861
2862         /* Should be set in custom invoke() */
2863         RNA_def_float(ot->srna, "initial_value", 0, 0, FLT_MAX, "Initial Value", "", 0, FLT_MAX);
2864
2865         /* Set internally, should be used in custom exec() to get final value */
2866         RNA_def_float(ot->srna, "new_value", 0, 0, FLT_MAX, "New Value", "", 0, FLT_MAX);
2867
2868         /* Should be set before calling operator */
2869         RNA_def_enum(ot->srna, "mode", radial_mode_items, 0, "Mode", "");
2870
2871         /* Internal */
2872         RNA_def_int_vector(ot->srna, "initial_mouse", 2, NULL, INT_MIN, INT_MAX, "Initial Mouse", "", INT_MIN, INT_MAX);
2873
2874         RNA_def_float_color(ot->srna, "color", 4, color, 0.0f, FLT_MAX, "Color", "Radial control color", 0.0f, 1.0f);
2875         RNA_def_float_color(ot->srna, "texture_color", 4, tex_color, 0.0f, FLT_MAX, "Texture Color", "Radial control texture color", 0.0f, 1.0f);
2876 }
2877
2878 /* ************************** timer for testing ***************** */
2879
2880 /* uses no type defines, fully local testing function anyway... ;) */
2881
2882 static void redraw_timer_window_swap(bContext *C)
2883 {
2884         wmWindow *win= CTX_wm_window(C);
2885         ScrArea *sa;
2886
2887         for(sa= CTX_wm_screen(C)->areabase.first; sa; sa= sa->next)
2888                 ED_area_tag_redraw(sa);
2889         wm_draw_update(C);
2890
2891         CTX_wm_window_set(C, win);      /* XXX context manipulation warning! */
2892 }
2893
2894 static EnumPropertyItem redraw_timer_type_items[] = {
2895         {0, "DRAW", 0, "Draw Region", "Draw Region"},
2896         {1, "DRAW_SWAP", 0, "Draw Region + Swap", "Draw Region and Swap"},
2897         {2, "DRAW_WIN", 0, "Draw Window", "Draw Window"},
2898         {3, "DRAW_WIN_SWAP", 0, "Draw Window + Swap", "Draw Window and Swap"},
2899         {4, "ANIM_STEP", 0, "Anim Step", "Animation Steps"},
2900         {5, "ANIM_PLAY", 0, "Anim Play", "Animation Playback"},
2901         {6, "UNDO", 0, "Undo/Redo", "Undo/Redo"},
2902         {0, NULL, 0, NULL, NULL}};
2903
2904 static int redraw_timer_exec(bContext *C, wmOperator *op)
2905 {
2906         ARegion *ar= CTX_wm_region(C);
2907         double stime= PIL_check_seconds_timer();
2908         int type = RNA_int_get(op->ptr, "type");
2909         int iter = RNA_int_get(op->ptr, "iterations");
2910         int a;
2911         float time;
2912         const char *infostr= "";
2913         
2914         WM_cursor_wait(1);
2915
2916         for(a=0; a<iter; a++) {
2917                 if (type==0) {
2918                         if(ar)
2919                                 ED_region_do_draw(C, ar);
2920                 } 
2921                 else if (type==1) {
2922                         wmWindow *win= CTX_wm_window(C);
2923                         
2924                         ED_region_tag_redraw(ar);
2925                         wm_draw_update(C);
2926                         
2927                         CTX_wm_window_set(C, win);      /* XXX context manipulation warning! */
2928                 }
2929                 else if (type==2) {
2930                         wmWindow *win= CTX_wm_window(C);
2931                         ScrArea *sa;
2932                         
2933                         ScrArea *sa_back= CTX_wm_area(C);
2934                         ARegion *ar_back= CTX_wm_region(C);
2935
2936                         for(sa= CTX_wm_screen(C)->areabase.first; sa; sa= sa->next) {
2937                                 ARegion *ar_iter;
2938                                 CTX_wm_area_set(C, sa);
2939
2940                                 for(ar_iter= sa->regionbase.first; ar_iter; ar_iter= ar_iter->next) {
2941                                         if(ar_iter->swinid) {
2942                                                 CTX_wm_region_set(C, ar_iter);
2943                                                 ED_region_do_draw(C, ar_iter);
2944                                         }
2945                                 }
2946                         }
2947
2948                         CTX_wm_window_set(C, win);      /* XXX context manipulation warning! */
2949
2950                         CTX_wm_area_set(C, sa_back);
2951                         CTX_wm_region_set(C, ar_back);
2952                 }
2953                 else if (type==3) {
2954                         redraw_timer_window_swap(C);
2955                 }
2956                 else if (type==4) {
2957                         Main *bmain= CTX_data_main(C);
2958                         Scene *scene= CTX_data_scene(C);
2959                         
2960                         if(a & 1) scene->r.cfra--;
2961                         else scene->r.cfra++;
2962                         scene_update_for_newframe(bmain, scene, scene->lay);
2963                 }
2964                 else if (type==5) {
2965
2966                         /* play anim, return on same frame as started with */
2967                         Main *bmain= CTX_data_main(C);
2968                         Scene *scene= CTX_data_scene(C);
2969                         int tot= (scene->r.efra - scene->r.sfra) + 1;
2970
2971                         while(tot--) {
2972                                 /* todo, ability to escape! */
2973                                 scene->r.cfra++;
2974                                 if(scene->r.cfra > scene->r.efra)
2975                                         scene->r.cfra= scene->r.sfra;
2976
2977                                 scene_update_for_newframe(bmain, scene, scene->lay);
2978                                 redraw_timer_window_swap(C);
2979                         }
2980                 }
2981                 else { /* 6 */
2982                         ED_undo_pop(C);
2983                         ED_undo_redo(C);
2984                 }
2985         }
2986         
2987         time= ((PIL_check_seconds_timer()-stime)*1000);
2988
2989         RNA_enum_description(redraw_timer_type_items, type, &infostr);
2990
2991         WM_cursor_wait(0);
2992
2993         BKE_reportf(op->reports, RPT_WARNING, "%d x %s: %.2f ms,  average: %.4f", iter, infostr, time, time/iter);
2994         
2995         return OPERATOR_FINISHED;
2996 }
2997
2998 static void WM_OT_redraw_timer(wmOperatorType *ot)
2999 {
3000         ot->name= "Redraw Timer";
3001         ot->idname= "WM_OT_redraw_timer";
3002         ot->description="Simple redraw timer to test the speed of updating the interface";
3003
3004         ot->invoke= WM_menu_invoke;
3005         ot->exec= redraw_timer_exec;
3006         ot->poll= WM_operator_winactive;
3007
3008         ot->prop= RNA_def_enum(ot->srna, "type", redraw_timer_type_items, 0, "Type", "");
3009         RNA_def_int(ot->srna, "iterations", 10, 1,INT_MAX, "Iterations", "Number of times to redraw", 1,1000);
3010
3011 }
3012
3013 /* ************************** memory statistics for testing ***************** */
3014
3015 static int memory_statistics_exec(bContext *UNUSED(C), wmOperator *UNUSED(op))
3016 {
3017         MEM_printmemlist_stats();
3018         return OPERATOR_FINISHED;
3019 }
3020
3021 static void WM_OT_memory_statistics(wmOperatorType *ot)
3022 {
3023         ot->name= "Memory Statistics";
3024         ot->idname= "WM_OT_memory_statistics";
3025         ot->description= "Print memory statistics to the console";
3026         
3027         ot->exec= memory_statistics_exec;
3028 }
3029
3030 /* ******************************************************* */
3031  
3032 /* called on initialize WM_exit() */
3033 void wm_operatortype_free(void)
3034 {
3035         wmOperatorType *ot;
3036         
3037         for(ot= global_ops.first; ot; ot= ot->next) {
3038                 if(ot->macro.first)
3039                         wm_operatortype_free_macro(ot);
3040
3041                 if(ot->ext.srna) /* python operator, allocs own string */
3042                         MEM_freeN((void *)ot->idname);
3043         }
3044         
3045         BLI_freelistN(&global_ops);
3046 }
3047
3048 /* called on initialize WM_init() */
3049 void wm_operatortype_init(void)
3050 {
3051         WM_operatortype_append(WM_OT_window_duplicate);
3052         WM_operatortype_append(WM_OT_read_homefile);
3053         WM_operatortype_append(WM_OT_read_factory_settings);
3054         WM_operatortype_append(WM_OT_save_homefile);
3055         WM_operatortype_append(WM_OT_window_fullscreen_toggle);
3056         WM_operatortype_append(WM_OT_quit_blender);
3057         WM_operatortype_append(WM_OT_open_mainfile);
3058         WM_operatortype_append(WM_OT_link_append);
3059         WM_operatortype_append(WM_OT_recover_last_session);
3060         WM_operatortype_append(WM_OT_recover_auto_save);
3061         WM_operatortype_append(WM_OT_save_as_mainfile);
3062         WM_operatortype_append(WM_OT_save_mainfile);
3063         WM_operatortype_append(WM_OT_redraw_timer);
3064         WM_operatortype_append(WM_OT_memory_statistics);
3065         WM_operatortype_append(WM_OT_debug_menu);
3066         WM_operatortype_append(WM_OT_splash);
3067         WM_operatortype_append(WM_OT_search_menu);
3068         WM_operatortype_append(WM_OT_call_menu);
3069 #if defined(WIN32)
3070         WM_operatortype_append(WM_OT_toggle_console);
3071 #endif
3072
3073 #ifdef WITH_COLLADA
3074         /* XXX: move these */
3075         WM_operatortype_append(WM_OT_collada_export);
3076         WM_operatortype_append(WM_OT_collada_import);
3077 #endif
3078
3079 }
3080
3081 /* circleselect-like modal operators */
3082 static void gesture_circle_modal_keymap(wmKeyConfig *keyconf)
3083 {
3084         static EnumPropertyItem modal_items[] = {
3085         {GESTURE_MODAL_CANCEL,  "CANCEL", 0, "Cancel", ""},
3086         {GESTURE_MODAL_CONFIRM, "CONFIRM", 0, "Confirm", ""},
3087         {GESTURE_MODAL_CIRCLE_ADD, "ADD", 0, "Add", ""},
3088         {GESTURE_MODAL_CIRCLE_SUB, "SUBTRACT", 0, "Subtract", ""},
3089
3090         {GESTURE_MODAL_SELECT,  "SELECT", 0, "Select", ""},
3091         {GESTURE_MODAL_DESELECT,"DESELECT", 0, "DeSelect", ""},
3092         {GESTURE_MODAL_NOP,"NOP", 0, "No Operation", ""},
3093
3094
3095         {0, NULL, 0, NULL, NULL}};
3096
3097         wmKeyMap *keymap= WM_modalkeymap_get(keyconf, "View3D Gesture Circle");
3098
3099         /* this function is called for each spacetype, only needs to add map once */
3100         if(keymap) return;
3101
3102         keymap= WM_modalkeymap_add(keyconf, "View3D Gesture Circle", modal_items);
3103
3104         /* items for modal map */
3105         WM_modalkeymap_add_item(keymap, ESCKEY,    KM_PRESS, KM_ANY, 0, GESTURE_MODAL_CANCEL);
3106         WM_modalkeymap_add_item(keymap, RIGHTMOUSE, KM_ANY, KM_ANY, 0, GESTURE_MODAL_CANCEL);
3107
3108         WM_modalkeymap_add_item(keymap, RETKEY, KM_PRESS, KM_ANY, 0, GESTURE_MODAL_CONFIRM);
3109         WM_modalkeymap_add_item(keymap, PADENTER, KM_PRESS, 0, 0, GESTURE_MODAL_CONFIRM);
3110
3111         WM_modalkeymap_add_item(keymap, LEFTMOUSE, KM_PRESS, 0, 0, GESTURE_MODAL_SELECT);
3112
3113 #if 0 // Durien guys like this :S
3114         WM_modalkeymap_add_item(keymap, LEFTMOUSE, KM_PRESS, KM_SHIFT, 0, GESTURE_MODAL_DESELECT);
3115         WM_modalkeymap_add_item(keymap, LEFTMOUSE, KM_RELEASE, KM_SHIFT, 0, GESTURE_MODAL_NOP);
3116 #else
3117         WM_modalkeymap_add_item(keymap, MIDDLEMOUSE, KM_PRESS, 0, 0, GESTURE_MODAL_DESELECT); //  defailt 2.4x
3118         WM_modalkeymap_add_item(keymap, MIDDLEMOUSE, KM_RELEASE, 0, 0, GESTURE_MODAL_NOP); //  defailt 2.4x
3119 #endif
3120
3121         WM_modalkeymap_add_item(keymap, LEFTMOUSE, KM_RELEASE, 0, 0, GESTURE_MODAL_NOP);
3122
3123         WM_modalkeymap_add_item(keymap, WHEELUPMOUSE, KM_PRESS, 0, 0, GESTURE_MODAL_CIRCLE_SUB);
3124         WM_modalkeymap_add_item(keymap, PADMINUS, KM_PRESS, 0, 0, GESTURE_MODAL_CIRCLE_SUB);
3125         WM_modalkeymap_add_item(keymap, WHEELDOWNMOUSE, KM_PRESS, 0, 0, GESTURE_MODAL_CIRCLE_ADD);
3126         WM_modalkeymap_add_item(keymap, PADPLUSKEY, KM_PRESS, 0, 0, GESTURE_MODAL_CIRCLE_ADD);
3127
3128         /* assign map to operators */
3129         WM_modalkeymap_assign(keymap, "VIEW3D_OT_select_circle");
3130         WM_modalkeymap_assign(keymap, "UV_OT_circle_select");
3131
3132 }
3133
3134 /* straight line modal operators */
3135 static void gesture_straightline_modal_keymap(wmKeyConfig *keyconf)
3136 {
3137         static EnumPropertyItem modal_items[] = {
3138                 {GESTURE_MODAL_CANCEL,  "CANCEL", 0, "Cancel", ""},
3139                 {GESTURE_MODAL_SELECT,  "SELECT", 0, "Select", ""},
3140                 {GESTURE_MODAL_BEGIN,   "BEGIN", 0, "Begin", ""},
3141                 {0, NULL, 0, NULL, NULL}};
3142         
3143         wmKeyMap *keymap= WM_modalkeymap_get(keyconf, "Gesture Straight Line");
3144         
3145         /* this function is called for each spacetype, only needs to add map once */
3146         if(keymap) return;
3147         
3148         keymap= WM_modalkeymap_add(keyconf, "Gesture Straight Line", modal_items);
3149         
3150         /* items for modal map */
3151         WM_modalkeymap_add_item(keymap, ESCKEY,    KM_PRESS, KM_ANY, 0, GESTURE_MODAL_CANCEL);
3152         WM_modalkeymap_add_item(keymap, RIGHTMOUSE, KM_ANY, KM_ANY, 0, GESTURE_MODAL_CANCEL);
3153         
3154         WM_modalkeymap_add_item(keymap, LEFTMOUSE, KM_PRESS, 0, 0, GESTURE_MODAL_BEGIN);
3155         WM_modalkeymap_add_item(keymap, LEFTMOUSE, KM_RELEASE, 0, 0, GESTURE_MODAL_SELECT);
3156         
3157         /* assign map to operators */
3158         WM_modalkeymap_assign(keymap, "IMAGE_OT_sample_line");
3159 }
3160
3161
3162 /* borderselect-like modal operators */
3163 static void gesture_border_modal_keymap(wmKeyConfig *keyconf)
3164 {
3165         static EnumPropertyItem modal_items[] = {
3166         {GESTURE_MODAL_CANCEL,  "CANCEL", 0, "Cancel", ""},
3167         {GESTURE_MODAL_SELECT,  "SELECT", 0, "Select", ""},
3168         {GESTURE_MODAL_DESELECT,"DESELECT", 0, "DeSelect", ""},
3169         {GESTURE_MODAL_BEGIN,   "BEGIN", 0, "Begin", ""},
3170         {0, NULL, 0, NULL, NULL}};
3171
3172         wmKeyMap *keymap= WM_modalkeymap_get(keyconf, "Gesture Border");
3173
3174         /* this function is called for each spacetype, only needs to add map once */
3175         if(keymap) return;
3176
3177         keymap= WM_modalkeymap_add(keyconf, "Gesture Border", modal_items);
3178
3179         /* items for modal map */
3180         WM_modalkeymap_add_item(keymap, ESCKEY,    KM_PRESS, KM_ANY, 0, GESTURE_MODAL_CANCEL);
3181         WM_modalkeymap_add_item(keymap, RIGHTMOUSE, KM_ANY, KM_ANY, 0, GESTURE_MODAL_CANCEL);
3182
3183         WM_modalkeymap_add_item(keymap, LEFTMOUSE, KM_PRESS, 0, 0, GESTURE_MODAL_BEGIN);
3184         WM_modalkeymap_add_item(keymap, LEFTMOUSE, KM_RELEASE, KM_ANY, 0, GESTURE_MODAL_SELECT);
3185
3186 #if 0 // Durian guys like this
3187         WM_modalkeymap_add_item(keymap, LEFTMOUSE, KM_PRESS, KM_SHIFT, 0, GESTURE_MODAL_BEGIN);
3188         WM_modalkeymap_add_item(keymap, LEFTMOUSE, KM_RELEASE, KM_SHIFT, 0, GESTURE_MODAL_DESELECT);
3189 #else
3190         WM_modalkeymap_add_item(keymap, MIDDLEMOUSE, KM_PRESS, 0, 0, GESTURE_MODAL_BEGIN);
3191         WM_modalkeymap_add_item(keymap, MIDDLEMOUSE, KM_RELEASE, 0, 0, GESTURE_MODAL_DESELECT);
3192 #endif
3193
3194         /* assign map to operators */
3195         WM_modalkeymap_assign(keymap, "ACTION_OT_select_border");
3196         WM_modalkeymap_assign(keymap, "ANIM_OT_channels_select_border");
3197         WM_modalkeymap_assign(keymap, "ANIM_OT_previewrange_set");
3198         WM_modalkeymap_assign(keymap, "INFO_OT_select_border");
3199         WM_modalkeymap_assign(keymap, "FILE_OT_select_border");
3200         WM_modalkeymap_assign(keymap, "GRAPH_OT_select_border");
3201         WM_modalkeymap_assign(keymap, "MARKER_OT_select_border");
3202         WM_modalkeymap_assign(keymap, "NLA_OT_select_border");
3203         WM_modalkeymap_assign(keymap, "NODE_OT_select_border");
3204 //      WM_modalkeymap_assign(keymap, "SCREEN_OT_border_select"); // template
3205         WM_modalkeymap_assign(keymap, "SEQUENCER_OT_select_border");
3206         WM_modalkeymap_assign(keymap, "SEQUENCER_OT_view_ghost_border");
3207         WM_modalkeymap_assign(keymap, "UV_OT_select_border");
3208         WM_modalkeymap_assign(keymap, "VIEW2D_OT_zoom_border");
3209         WM_modalkeymap_assign(keymap, "VIEW3D_OT_clip_border");
3210         WM_modalkeymap_assign(keymap, "VIEW3D_OT_render_border");
3211         WM_modalkeymap_assign(keymap, "VIEW3D_OT_select_border");
3212         WM_modalkeymap_assign(keymap, "VIEW3D_OT_zoom_border"); // XXX TODO: zoom border should perhaps map rightmouse to zoom out instead of in+cancel
3213 }
3214
3215 /* zoom to border modal operators */
3216 static void gesture_zoom_border_modal_keymap(wmKeyConfig *keyconf)
3217 {
3218         static EnumPropertyItem modal_items[] = {
3219         {GESTURE_MODAL_CANCEL, "CANCEL", 0, "Cancel", ""},
3220         {GESTURE_MODAL_IN,      "IN", 0, "In", ""},
3221         {GESTURE_MODAL_OUT, "OUT", 0, "Out", ""},
3222         {GESTURE_MODAL_BEGIN, "BEGIN", 0, "Begin", ""},
3223         {0, NULL, 0, NULL, NULL}};
3224
3225         wmKeyMap *keymap= WM_modalkeymap_get(keyconf, "Gesture Zoom Border");
3226
3227         /* this function is called for each spacetype, only needs to add map once */
3228         if(keymap) return;
3229
3230         keymap= WM_modalkeymap_add(keyconf, "Gesture Zoom Border", modal_items);
3231
3232         /* items for modal map */
3233         WM_modalkeymap_add_item(keymap, ESCKEY,    KM_PRESS, KM_ANY, 0, GESTURE_MODAL_CANCEL);
3234         WM_modalkeymap_add_item(keymap, RIGHTMOUSE, KM_ANY, KM_ANY, 0, GESTURE_MODAL_CANCEL);
3235
3236         WM_modalkeymap_add_item(keymap, LEFTMOUSE, KM_PRESS, 0, 0, GESTURE_MODAL_BEGIN);
3237         WM_modalkeymap_add_item(keymap, LEFTMOUSE, KM_RELEASE, 0, 0, GESTURE_MODAL_IN); 
3238
3239         WM_modalkeymap_add_item(keymap, MIDDLEMOUSE, KM_PRESS, 0, 0, GESTURE_MODAL_BEGIN);
3240         WM_modalkeymap_add_item(keymap, MIDDLEMOUSE, KM_RELEASE, 0, 0, GESTURE_MODAL_OUT);
3241
3242         /* assign map to operators */
3243         WM_modalkeymap_assign(keymap, "VIEW2D_OT_zoom_border");
3244         WM_modalkeymap_assign(keymap, "VIEW3D_OT_zoom_border");
3245 }
3246
3247 /* default keymap for windows and screens, only call once per WM */
3248 void wm_window_keymap(wmKeyConfig *keyconf)
3249 {
3250         wmKeyMap *keymap= WM_keymap_find(keyconf, "Window", 0, 0);
3251         wmKeyMapItem *kmi;
3252         
3253         /* note, this doesn't replace existing keymap items */
3254         WM_keymap_verify_item(keymap, "WM_OT_window_duplicate", WKEY, KM_PRESS, KM_CTRL|KM_ALT, 0);
3255         #ifdef __APPLE__
3256         WM_keymap_add_item(keymap, "WM_OT_read_homefile", NKEY, KM_PRESS, KM_OSKEY, 0);
3257         WM_keymap_add_menu(keymap, "INFO_MT_file_open_recent", OKEY, KM_PRESS, KM_SHIFT|KM_OSKEY, 0);
3258         WM_keymap_add_item(keymap, "WM_OT_open_mainfile", OKEY, KM_PRESS, KM_OSKEY, 0);
3259         WM_keymap_add_item(keymap, "WM_OT_save_mainfile", SKEY, KM_PRESS, KM_OSKEY, 0);
3260         WM_keymap_add_item(keymap, "WM_OT_save_as_mainfile", SKEY, KM_PRESS, KM_SHIFT|KM_OSKEY, 0);
3261         WM_keymap_add_item(keymap, "WM_OT_quit_blender", QKEY, KM_PRESS, KM_OSKEY, 0);
3262         #endif
3263         WM_keymap_add_item(keymap, "WM_OT_read_homefile", NKEY, KM_PRESS, KM_CTRL, 0);
3264         WM_keymap_add_item(keymap, "WM_OT_save_homefile", UKEY, KM_PRESS, KM_CTRL, 0); 
3265         WM_keymap_add_menu(keymap, "INFO_MT_file_open_recent", OKEY, KM_PRESS, KM_SHIFT|KM_CTRL, 0);
3266         WM_keymap_add_item(keymap, "WM_OT_open_mainfile", OKEY, KM_PRESS, KM_CTRL, 0);
3267         WM_keymap_add_item(keymap, "WM_OT_open_mainfile", F1KEY, KM_PRESS, 0, 0);
3268         WM_keymap_add_item(keymap, "WM_OT_link_append", OKEY, KM_PRESS, KM_CTRL|KM_ALT, 0);
3269         kmi= WM_keymap_add_item(keymap, "WM_OT_link_append", F1KEY, KM_PRESS, KM_SHIFT, 0);
3270         RNA_boolean_set(kmi->ptr, "link", FALSE);
3271         RNA_boolean_set(kmi->ptr, "instance_groups", FALSE);
3272
3273         WM_keymap_add_item(keymap, "WM_OT_save_mainfile", SKEY, KM_PRESS, KM_CTRL, 0);
3274         WM_keymap_add_item(keymap, "WM_OT_save_mainfile", WKEY, KM_PRESS, KM_CTRL, 0);
3275         WM_keymap_add_item(keymap, "WM_OT_save_as_mainfile", SKEY, KM_PRESS, KM_SHIFT|KM_CTRL, 0);
3276         WM_keymap_add_item(keymap, "WM_OT_save_as_mainfile", F2KEY, KM_PRESS, 0, 0);
3277         kmi= WM_keymap_add_item(keymap, "WM_OT_save_as_mainfile", SKEY, KM_PRESS, KM_ALT|KM_CTRL, 0);
3278         RNA_boolean_set(kmi->ptr, "copy", 1);
3279
3280         WM_keymap_verify_item(keymap, "WM_OT_window_fullscreen_toggle", F11KEY, KM_PRESS, KM_ALT, 0);
3281         WM_keymap_add_item(keymap, "WM_OT_quit_blender", QKEY, KM_PRESS, KM_CTRL, 0);
3282
3283         /* debug/testing */
3284         WM_keymap_verify_item(keymap, "WM_OT_redraw_timer", TKEY, KM_PRESS, KM_ALT|KM_CTRL, 0);
3285         WM_keymap_verify_item(keymap, "WM_OT_debug_menu", DKEY, KM_PRESS, KM_ALT|KM_CTRL, 0);
3286         WM_keymap_verify_item(keymap, "WM_OT_search_menu", SPACEKEY, KM_PRESS, 0, 0);
3287         
3288         /* Space switching */
3289
3290
3291         kmi = WM_keymap_add_item(keymap, "WM_OT_context_set_enum", F2KEY, KM_PRESS, KM_SHIFT, 0); /* new in 2.5x, was DXF export */
3292         RNA_string_set(kmi->