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