093899954ff1e3a373971038022e2bfe16758b5b
[blender.git] / source / blender / windowmanager / intern / wm_operators.c
1 /*
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version. 
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *
20  * The Original Code is Copyright (C) 2007 Blender Foundation.
21  * All rights reserved.
22  *
23  * 
24  * Contributor(s): Blender Foundation
25  *
26  * ***** END GPL LICENSE BLOCK *****
27  */
28
29 /** \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 uiBlock *wm_block_create_splash(bContext *C, ARegion *ar, void *UNUSED(arg))
1111 {
1112         uiBlock *block;
1113         uiBut *but;
1114         uiLayout *layout, *split, *col;
1115         uiStyle *style= U.uistyles.first;
1116         struct RecentFile *recent;
1117         int i;
1118         MenuType *mt= WM_menutype_find("USERPREF_MT_splash", TRUE);
1119         char url[64];
1120         
1121 #ifdef NAN_BUILDINFO
1122         int ver_width, rev_width;
1123         char *version_str = NULL;
1124         char *revision_str = NULL;
1125         char version_buf[128];
1126         char revision_buf[128];
1127         extern char build_rev[];
1128         
1129         version_str = &version_buf[0];
1130         revision_str = &revision_buf[0];
1131         
1132         sprintf(version_str, "%d.%02d.%d", BLENDER_VERSION/100, BLENDER_VERSION%100, BLENDER_SUBVERSION);
1133         sprintf(revision_str, "r%s", build_rev);
1134         
1135         BLF_size(style->widgetlabel.uifont_id, style->widgetlabel.points, U.dpi);
1136         ver_width = BLF_width(style->widgetlabel.uifont_id, version_str)+5;
1137         rev_width = BLF_width(style->widgetlabel.uifont_id, revision_str)+5;
1138 #endif //NAN_BUILDINFO
1139
1140         block= uiBeginBlock(C, ar, "_popup", UI_EMBOSS);
1141         uiBlockSetFlag(block, UI_BLOCK_KEEP_OPEN);
1142         
1143         but= uiDefBut(block, BUT_IMAGE, 0, "", 0, 10, 501, 282, NULL, 0.0, 0.0, 0, 0, "");
1144         uiButSetFunc(but, wm_block_splash_close, block, NULL);
1145         uiBlockSetFunc(block, wm_block_splash_refreshmenu, block, NULL);
1146         
1147 #ifdef NAN_BUILDINFO    
1148         uiDefBut(block, LABEL, 0, version_str, 494-ver_width, 282-24, ver_width, 20, NULL, 0, 0, 0, 0, NULL);
1149         uiDefBut(block, LABEL, 0, revision_str, 494-rev_width, 282-36, rev_width, 20, NULL, 0, 0, 0, 0, NULL);
1150 #endif //NAN_BUILDINFO
1151         
1152         layout= uiBlockLayout(block, UI_LAYOUT_VERTICAL, UI_LAYOUT_PANEL, 10, 2, 480, 110, style);
1153         
1154         uiBlockSetEmboss(block, UI_EMBOSS);
1155         /* show the splash menu (containing interaction presets), using python */
1156         if (mt) {
1157                 Menu menu= {NULL};
1158                 menu.layout= layout;
1159                 menu.type= mt;
1160                 mt->draw(C, &menu);
1161
1162 //              wmWindowManager *wm= CTX_wm_manager(C);
1163 //              uiItemM(layout, C, "USERPREF_MT_keyconfigs", U.keyconfigstr, ICON_NONE);
1164         }
1165         
1166         uiBlockSetEmboss(block, UI_EMBOSSP);
1167         uiLayoutSetOperatorContext(layout, WM_OP_EXEC_REGION_WIN);
1168         
1169         split = uiLayoutSplit(layout, 0, 0);
1170         col = uiLayoutColumn(split, 0);
1171         uiItemL(col, "Links", ICON_NONE);
1172         uiItemStringO(col, "Donations", ICON_URL, "WM_OT_url_open", "url", "http://www.blender.org/blenderorg/blender-foundation/donation-payment/");
1173         uiItemStringO(col, "Release Log", ICON_URL, "WM_OT_url_open", "url", "http://www.blender.org/development/release-logs/blender-256-beta/");
1174         uiItemStringO(col, "Manual", ICON_URL, "WM_OT_url_open", "url", "http://wiki.blender.org/index.php/Doc:Manual");
1175         uiItemStringO(col, "Blender Website", ICON_URL, "WM_OT_url_open", "url", "http://www.blender.org/");
1176         uiItemStringO(col, "User Community", ICON_URL, "WM_OT_url_open", "url", "http://www.blender.org/community/user-community/"); // 
1177         BLI_snprintf(url, sizeof(url), "http://www.blender.org/documentation/blender_python_api_%d_%d_%d", BLENDER_VERSION/100, BLENDER_VERSION%100, BLENDER_SUBVERSION);
1178         uiItemStringO(col, "Python API Reference", ICON_URL, "WM_OT_url_open", "url", url);
1179         uiItemL(col, "", ICON_NONE);
1180
1181         col = uiLayoutColumn(split, 0);
1182         uiItemL(col, "Recent", ICON_NONE);
1183         for(recent = G.recent_files.first, i=0; (i<5) && (recent); recent = recent->next, i++) {
1184                 uiItemStringO(col, BLI_path_basename(recent->filepath), ICON_FILE_BLEND, "WM_OT_open_mainfile", "filepath", recent->filepath);
1185         }
1186         uiItemS(col);
1187         uiItemO(col, NULL, ICON_RECOVER_LAST, "WM_OT_recover_last_session");
1188         uiItemL(col, "", ICON_NONE);
1189         
1190         uiCenteredBoundsBlock(block, 0.0f);
1191         uiEndBlock(C, block);
1192         
1193         return block;
1194 }
1195
1196 static int wm_splash_invoke(bContext *C, wmOperator *UNUSED(op), wmEvent *UNUSED(event))
1197 {
1198         uiPupBlock(C, wm_block_create_splash, NULL);
1199         
1200         return OPERATOR_FINISHED;
1201 }
1202
1203 static void WM_OT_splash(wmOperatorType *ot)
1204 {
1205         ot->name= "Splash Screen";
1206         ot->idname= "WM_OT_splash";
1207         ot->description= "Opens a blocking popup region with release info";
1208         
1209         ot->invoke= wm_splash_invoke;
1210         ot->poll= WM_operator_winactive;
1211 }
1212
1213
1214 /* ***************** Search menu ************************* */
1215 static void operator_call_cb(struct bContext *C, void *UNUSED(arg1), void *arg2)
1216 {
1217         wmOperatorType *ot= arg2;
1218         
1219         if(ot)
1220                 WM_operator_name_call(C, ot->idname, WM_OP_INVOKE_DEFAULT, NULL);
1221 }
1222
1223 static void operator_search_cb(const struct bContext *C, void *UNUSED(arg), const char *str, uiSearchItems *items)
1224 {
1225         wmOperatorType *ot = WM_operatortype_first();
1226         
1227         for(; ot; ot= ot->next) {
1228                 
1229                 if(BLI_strcasestr(ot->name, str)) {
1230                         if(WM_operator_poll((bContext*)C, ot)) {
1231                                 char name[256];
1232                                 int len= strlen(ot->name);
1233                                 
1234                                 /* display name for menu, can hold hotkey */
1235                                 BLI_strncpy(name, ot->name, 256);
1236                                 
1237                                 /* check for hotkey */
1238                                 if(len < 256-6) {
1239                                         if(WM_key_event_operator_string(C, ot->idname, WM_OP_EXEC_DEFAULT, NULL, &name[len+1], 256-len-1))
1240                                                 name[len]= '|';
1241                                 }
1242                                 
1243                                 if(0==uiSearchItemAdd(items, name, ot, 0))
1244                                         break;
1245                         }
1246                 }
1247         }
1248 }
1249
1250 static uiBlock *wm_block_search_menu(bContext *C, ARegion *ar, void *UNUSED(arg_op))
1251 {
1252         static char search[256]= "";
1253         wmEvent event;
1254         wmWindow *win= CTX_wm_window(C);
1255         uiBlock *block;
1256         uiBut *but;
1257         
1258         block= uiBeginBlock(C, ar, "_popup", UI_EMBOSS);
1259         uiBlockSetFlag(block, UI_BLOCK_LOOP|UI_BLOCK_RET_1|UI_BLOCK_MOVEMOUSE_QUIT);
1260         
1261         but= uiDefSearchBut(block, search, 0, ICON_VIEWZOOM, 256, 10, 10, 180, 19, 0, 0, "");
1262         uiButSetSearchFunc(but, operator_search_cb, NULL, operator_call_cb, NULL);
1263         
1264         /* fake button, it holds space for search items */
1265         uiDefBut(block, LABEL, 0, "", 10, 10 - uiSearchBoxhHeight(), 180, uiSearchBoxhHeight(), NULL, 0, 0, 0, 0, NULL);
1266         
1267         uiPopupBoundsBlock(block, 6, 0, -20); /* move it downwards, mouse over button */
1268         uiEndBlock(C, block);
1269         
1270         event= *(win->eventstate);      /* XXX huh huh? make api call */
1271         event.type= EVT_BUT_OPEN;
1272         event.val= KM_PRESS;
1273         event.customdata= but;
1274         event.customdatafree= FALSE;
1275         wm_event_add(win, &event);
1276         
1277         return block;
1278 }
1279
1280 static int wm_search_menu_exec(bContext *UNUSED(C), wmOperator *UNUSED(op))
1281 {
1282         return OPERATOR_FINISHED;       
1283 }
1284
1285 static int wm_search_menu_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
1286 {
1287         uiPupBlock(C, wm_block_search_menu, op);
1288         
1289         return OPERATOR_CANCELLED;
1290 }
1291
1292 /* op->poll */
1293 static int wm_search_menu_poll(bContext *C)
1294 {
1295         if(CTX_wm_window(C)==NULL) return 0;
1296         if(CTX_wm_area(C) && CTX_wm_area(C)->spacetype==SPACE_CONSOLE) return 0;  // XXX - so we can use the shortcut in the console
1297         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
1298         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
1299         return 1;
1300 }
1301
1302 static void WM_OT_search_menu(wmOperatorType *ot)
1303 {
1304         ot->name= "Search Menu";
1305         ot->idname= "WM_OT_search_menu";
1306         
1307         ot->invoke= wm_search_menu_invoke;
1308         ot->exec= wm_search_menu_exec;
1309         ot->poll= wm_search_menu_poll;
1310 }
1311
1312 static int wm_call_menu_exec(bContext *C, wmOperator *op)
1313 {
1314         char idname[BKE_ST_MAXNAME];
1315         RNA_string_get(op->ptr, "name", idname);
1316
1317         uiPupMenuInvoke(C, idname);
1318
1319         return OPERATOR_CANCELLED;
1320 }
1321
1322 static void WM_OT_call_menu(wmOperatorType *ot)
1323 {
1324         ot->name= "Call Menu";
1325         ot->idname= "WM_OT_call_menu";
1326
1327         ot->exec= wm_call_menu_exec;
1328         ot->poll= WM_operator_winactive;
1329
1330         RNA_def_string(ot->srna, "name", "", BKE_ST_MAXNAME, "Name", "Name of the menu");
1331 }
1332
1333 /* ************ window / screen operator definitions ************** */
1334
1335 /* this poll functions is needed in place of WM_operator_winactive
1336  * while it crashes on full screen */
1337 static int wm_operator_winactive_normal(bContext *C)
1338 {
1339         wmWindow *win= CTX_wm_window(C);
1340
1341         if(win==NULL || win->screen==NULL || win->screen->full != SCREENNORMAL)
1342                 return 0;
1343
1344         return 1;
1345 }
1346
1347 static void WM_OT_window_duplicate(wmOperatorType *ot)
1348 {
1349         ot->name= "Duplicate Window";
1350         ot->idname= "WM_OT_window_duplicate";
1351         ot->description="Duplicate the current Blender window";
1352                 
1353         ot->exec= wm_window_duplicate_exec;
1354         ot->poll= wm_operator_winactive_normal;
1355 }
1356
1357 static void WM_OT_save_homefile(wmOperatorType *ot)
1358 {
1359         ot->name= "Save User Settings";
1360         ot->idname= "WM_OT_save_homefile";
1361         ot->description="Make the current file the default .blend file";
1362                 
1363         ot->invoke= WM_operator_confirm;
1364         ot->exec= WM_write_homefile;
1365         ot->poll= WM_operator_winactive;
1366 }
1367
1368 static void WM_OT_read_homefile(wmOperatorType *ot)
1369 {
1370         ot->name= "Reload Start-Up File";
1371         ot->idname= "WM_OT_read_homefile";
1372         ot->description="Open the default file (doesn't save the current file)";
1373         
1374         ot->invoke= WM_operator_confirm;
1375         ot->exec= WM_read_homefile_exec;
1376         /* ommit poll to run in background mode */
1377 }
1378
1379 static void WM_OT_read_factory_settings(wmOperatorType *ot)
1380 {
1381         ot->name= "Load Factory Settings";
1382         ot->idname= "WM_OT_read_factory_settings";
1383         ot->description="Load default file and user preferences";
1384         
1385         ot->invoke= WM_operator_confirm;
1386         ot->exec= WM_read_homefile_exec;
1387         /* ommit poll to run in background mode */
1388 }
1389
1390 /* *************** open file **************** */
1391
1392 static void open_set_load_ui(wmOperator *op)
1393 {
1394         if(!RNA_property_is_set(op->ptr, "load_ui"))
1395                 RNA_boolean_set(op->ptr, "load_ui", !(U.flag & USER_FILENOUI));
1396 }
1397
1398 static void open_set_use_scripts(wmOperator *op)
1399 {
1400         if(!RNA_property_is_set(op->ptr, "use_scripts")) {
1401                 /* use G_SCRIPT_AUTOEXEC rather then the userpref because this means if
1402                  * the flag has been disabled from the command line, then opening
1403                  * from the menu wont enable this setting. */
1404                 RNA_boolean_set(op->ptr, "use_scripts", (G.f & G_SCRIPT_AUTOEXEC));
1405         }
1406 }
1407
1408 static int wm_open_mainfile_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
1409 {
1410         const char *openname= G.main->name;
1411
1412         /* if possible, get the name of the most recently used .blend file */
1413         if (G.recent_files.first) {
1414                 struct RecentFile *recent = G.recent_files.first;
1415                 openname = recent->filepath;
1416         }
1417
1418         RNA_string_set(op->ptr, "filepath", openname);
1419         open_set_load_ui(op);
1420         open_set_use_scripts(op);
1421
1422         WM_event_add_fileselect(C, op);
1423
1424         return OPERATOR_RUNNING_MODAL;
1425 }
1426
1427 static int wm_open_mainfile_exec(bContext *C, wmOperator *op)
1428 {
1429         char path[FILE_MAX];
1430
1431         RNA_string_get(op->ptr, "filepath", path);
1432         open_set_load_ui(op);
1433         open_set_use_scripts(op);
1434
1435         if(RNA_boolean_get(op->ptr, "load_ui"))
1436                 G.fileflags &= ~G_FILE_NO_UI;
1437         else
1438                 G.fileflags |= G_FILE_NO_UI;
1439                 
1440         if(RNA_boolean_get(op->ptr, "use_scripts"))
1441                 G.f |= G_SCRIPT_AUTOEXEC;
1442         else
1443                 G.f &= ~G_SCRIPT_AUTOEXEC;
1444         
1445         // XXX wm in context is not set correctly after WM_read_file -> crash
1446         // do it before for now, but is this correct with multiple windows?
1447         WM_event_add_notifier(C, NC_WINDOW, NULL);
1448
1449         WM_read_file(C, path, op->reports);
1450         
1451         return OPERATOR_FINISHED;
1452 }
1453
1454 static void WM_OT_open_mainfile(wmOperatorType *ot)
1455 {
1456         ot->name= "Open Blender File";
1457         ot->idname= "WM_OT_open_mainfile";
1458         ot->description="Open a Blender file";
1459         
1460         ot->invoke= wm_open_mainfile_invoke;
1461         ot->exec= wm_open_mainfile_exec;
1462         ot->poll= WM_operator_winactive;
1463         
1464         WM_operator_properties_filesel(ot, FOLDERFILE|BLENDERFILE, FILE_BLENDER, FILE_OPENFILE, WM_FILESEL_FILEPATH);
1465
1466         RNA_def_boolean(ot->srna, "load_ui", 1, "Load UI", "Load user interface setup in the .blend file");
1467         RNA_def_boolean(ot->srna, "use_scripts", 1, "Trusted Source", "Allow blend file execute scripts automatically, default available from system preferences");
1468 }
1469
1470 /* **************** link/append *************** */
1471
1472 static int wm_link_append_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
1473 {
1474         if(!RNA_property_is_set(op->ptr, "relative_path"))
1475                 RNA_boolean_set(op->ptr, "relative_path", U.flag & USER_RELPATHS);
1476
1477         if(RNA_property_is_set(op->ptr, "filepath")) {
1478                 return WM_operator_call(C, op);
1479         } 
1480         else {
1481                 /* XXX TODO solve where to get last linked library from */
1482                 RNA_string_set(op->ptr, "filepath", G.lib);
1483                 WM_event_add_fileselect(C, op);
1484                 return OPERATOR_RUNNING_MODAL;
1485         }
1486 }
1487
1488 static short wm_link_append_flag(wmOperator *op)
1489 {
1490         short flag= 0;
1491
1492         if(RNA_boolean_get(op->ptr, "autoselect")) flag |= FILE_AUTOSELECT;
1493         if(RNA_boolean_get(op->ptr, "active_layer")) flag |= FILE_ACTIVELAY;
1494         if(RNA_boolean_get(op->ptr, "relative_path")) flag |= FILE_RELPATH;
1495         if(RNA_boolean_get(op->ptr, "link")) flag |= FILE_LINK;
1496         if(RNA_boolean_get(op->ptr, "instance_groups")) flag |= FILE_GROUP_INSTANCE;
1497
1498         return flag;
1499 }
1500
1501 static int wm_link_append_exec(bContext *C, wmOperator *op)
1502 {
1503         Main *bmain= CTX_data_main(C);
1504         Scene *scene= CTX_data_scene(C);
1505         Main *mainl= NULL;
1506         BlendHandle *bh;
1507         PropertyRNA *prop;
1508         char name[FILE_MAX], dir[FILE_MAX], libname[FILE_MAX], group[GROUP_MAX];
1509         int idcode, totfiles=0;
1510         short flag;
1511
1512         name[0] = '\0';
1513         RNA_string_get(op->ptr, "filename", name);
1514         RNA_string_get(op->ptr, "directory", dir);
1515
1516         /* test if we have a valid data */
1517         if(BLO_is_a_library(dir, libname, group) == 0) {
1518                 BKE_report(op->reports, RPT_ERROR, "Not a library");
1519                 return OPERATOR_CANCELLED;
1520         }
1521         else if(group[0] == 0) {
1522                 BKE_report(op->reports, RPT_ERROR, "Nothing indicated");
1523                 return OPERATOR_CANCELLED;
1524         }
1525         else if(BLI_streq(bmain->name, libname)) {
1526                 BKE_report(op->reports, RPT_ERROR, "Cannot use current file as library");
1527                 return OPERATOR_CANCELLED;
1528         }
1529
1530         /* check if something is indicated for append/link */
1531         prop = RNA_struct_find_property(op->ptr, "files");
1532         if(prop) {
1533                 totfiles= RNA_property_collection_length(op->ptr, prop);
1534                 if(totfiles == 0) {
1535                         if(name[0] == '\0') {
1536                                 BKE_report(op->reports, RPT_ERROR, "Nothing indicated");
1537                                 return OPERATOR_CANCELLED;
1538                         }
1539                 }
1540         }
1541         else if(name[0] == '\0') {
1542                 BKE_report(op->reports, RPT_ERROR, "Nothing indicated");
1543                 return OPERATOR_CANCELLED;
1544         }
1545
1546         bh = BLO_blendhandle_from_file(libname, op->reports);
1547
1548         if(bh == NULL) {
1549                 /* unlikely since we just browsed it, but possible
1550                  * error reports will have been made by BLO_blendhandle_from_file() */
1551                 return OPERATOR_CANCELLED;
1552         }
1553
1554
1555         /* from here down, no error returns */
1556
1557         idcode = BKE_idcode_from_name(group);
1558
1559         /* now we have or selected, or an indicated file */
1560         if(RNA_boolean_get(op->ptr, "autoselect"))
1561                 scene_deselect_all(scene);
1562
1563         
1564         flag = wm_link_append_flag(op);
1565
1566         /* sanity checks for flag */
1567         if(scene->id.lib && (flag & FILE_GROUP_INSTANCE)) {
1568                 /* TODO, user never gets this message */
1569                 BKE_reportf(op->reports, RPT_WARNING, "Scene '%s' is linked, group instance disabled", scene->id.name+2);
1570                 flag &= ~FILE_GROUP_INSTANCE;
1571         }
1572
1573
1574         /* tag everything, all untagged data can be made local
1575          * its also generally useful to know what is new
1576          *
1577          * take extra care flag_all_listbases_ids(LIB_LINK_TAG, 0) is called after! */
1578         flag_all_listbases_ids(LIB_PRE_EXISTING, 1);
1579
1580         /* here appending/linking starts */
1581         mainl = BLO_library_append_begin(C, &bh, libname);
1582         if(totfiles == 0) {
1583                 BLO_library_append_named_part(C, mainl, &bh, name, idcode, flag);
1584         }
1585         else {
1586                 RNA_BEGIN(op->ptr, itemptr, "files") {
1587                         RNA_string_get(&itemptr, "name", name);
1588                         BLO_library_append_named_part(C, mainl, &bh, name, idcode, flag);
1589                 }
1590                 RNA_END;
1591         }
1592         BLO_library_append_end(C, mainl, &bh, idcode, flag);
1593         
1594         /* mark all library linked objects to be updated */
1595         recalc_all_library_objects(bmain);
1596
1597         /* append, rather than linking */
1598         if((flag & FILE_LINK)==0) {
1599                 Library *lib= BLI_findstring(&bmain->library, libname, offsetof(Library, filepath));
1600                 if(lib) all_local(lib, 1);
1601                 else    BLI_assert(!"cant find name of just added library!");
1602         }
1603
1604         /* important we unset, otherwise these object wont
1605          * link into other scenes from this blend file */
1606         flag_all_listbases_ids(LIB_PRE_EXISTING, 0);
1607
1608         /* recreate dependency graph to include new objects */
1609         DAG_scene_sort(bmain, scene);
1610         DAG_ids_flush_update(bmain, 0);
1611
1612         BLO_blendhandle_close(bh);
1613
1614         /* XXX TODO: align G.lib with other directory storage (like last opened image etc...) */
1615         BLI_strncpy(G.lib, dir, FILE_MAX);
1616
1617         WM_event_add_notifier(C, NC_WINDOW, NULL);
1618
1619         return OPERATOR_FINISHED;
1620 }
1621
1622 static void WM_OT_link_append(wmOperatorType *ot)
1623 {
1624         ot->name= "Link/Append from Library";
1625         ot->idname= "WM_OT_link_append";
1626         ot->description= "Link or Append from a Library .blend file";
1627         
1628         ot->invoke= wm_link_append_invoke;
1629         ot->exec= wm_link_append_exec;
1630         ot->poll= WM_operator_winactive;
1631         
1632         ot->flag |= OPTYPE_UNDO;
1633
1634         WM_operator_properties_filesel(ot, FOLDERFILE|BLENDERFILE, FILE_LOADLIB, FILE_OPENFILE, WM_FILESEL_FILEPATH|WM_FILESEL_DIRECTORY|WM_FILESEL_FILENAME| WM_FILESEL_RELPATH);
1635         
1636         RNA_def_boolean(ot->srna, "link", 1, "Link", "Link the objects or datablocks rather than appending");
1637         RNA_def_boolean(ot->srna, "autoselect", 1, "Select", "Select the linked objects");
1638         RNA_def_boolean(ot->srna, "active_layer", 1, "Active Layer", "Put the linked objects on the active layer");
1639         RNA_def_boolean(ot->srna, "instance_groups", 1, "Instance Groups", "Create instances for each group as a DupliGroup");
1640
1641         RNA_def_collection_runtime(ot->srna, "files", &RNA_OperatorFileListElement, "Files", "");
1642 }       
1643
1644 /* *************** recover last session **************** */
1645
1646 static int wm_recover_last_session_exec(bContext *C, wmOperator *op)
1647 {
1648         char filename[FILE_MAX];
1649
1650         G.fileflags |= G_FILE_RECOVER;
1651
1652         // XXX wm in context is not set correctly after WM_read_file -> crash
1653         // do it before for now, but is this correct with multiple windows?
1654         WM_event_add_notifier(C, NC_WINDOW, NULL);
1655
1656         /* load file */
1657         BLI_make_file_string("/", filename, btempdir, "quit.blend");
1658         WM_read_file(C, filename, op->reports);
1659
1660         G.fileflags &= ~G_FILE_RECOVER;
1661         return OPERATOR_FINISHED;
1662 }
1663
1664 static void WM_OT_recover_last_session(wmOperatorType *ot)
1665 {
1666         ot->name= "Recover Last Session";
1667         ot->idname= "WM_OT_recover_last_session";
1668         ot->description="Open the last closed file (\"quit.blend\")";
1669         
1670         ot->exec= wm_recover_last_session_exec;
1671         ot->poll= WM_operator_winactive;
1672 }
1673
1674 /* *************** recover auto save **************** */
1675
1676 static int wm_recover_auto_save_exec(bContext *C, wmOperator *op)
1677 {
1678         char path[FILE_MAX];
1679
1680         RNA_string_get(op->ptr, "filepath", path);
1681
1682         G.fileflags |= G_FILE_RECOVER;
1683
1684         // XXX wm in context is not set correctly after WM_read_file -> crash
1685         // do it before for now, but is this correct with multiple windows?
1686         WM_event_add_notifier(C, NC_WINDOW, NULL);
1687
1688         /* load file */
1689         WM_read_file(C, path, op->reports);
1690
1691         G.fileflags &= ~G_FILE_RECOVER;
1692
1693         return OPERATOR_FINISHED;
1694 }
1695
1696 static int wm_recover_auto_save_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
1697 {
1698         char filename[FILE_MAX];
1699
1700         wm_autosave_location(filename);
1701         RNA_string_set(op->ptr, "filepath", filename);
1702         WM_event_add_fileselect(C, op);
1703
1704         return OPERATOR_RUNNING_MODAL;
1705 }
1706
1707 static void WM_OT_recover_auto_save(wmOperatorType *ot)
1708 {
1709         ot->name= "Recover Auto Save";
1710         ot->idname= "WM_OT_recover_auto_save";
1711         ot->description="Open an automatically saved file to recover it";
1712         
1713         ot->exec= wm_recover_auto_save_exec;
1714         ot->invoke= wm_recover_auto_save_invoke;
1715         ot->poll= WM_operator_winactive;
1716
1717         WM_operator_properties_filesel(ot, BLENDERFILE, FILE_BLENDER, FILE_OPENFILE, WM_FILESEL_FILEPATH);
1718 }
1719
1720 /* *************** save file as **************** */
1721
1722 static void untitled(char *name)
1723 {
1724         if(G.save_over == 0 && strlen(name) < FILE_MAX-16) {
1725                 char *c= BLI_last_slash(name);
1726                 
1727                 if(c)
1728                         strcpy(&c[1], "untitled.blend");
1729                 else
1730                         strcpy(name, "untitled.blend");
1731         }
1732 }
1733
1734 static void save_set_compress(wmOperator *op)
1735 {
1736         if(!RNA_property_is_set(op->ptr, "compress")) {
1737                 if(G.save_over) /* keep flag for existing file */
1738                         RNA_boolean_set(op->ptr, "compress", G.fileflags & G_FILE_COMPRESS);
1739                 else /* use userdef for new file */
1740                         RNA_boolean_set(op->ptr, "compress", U.flag & USER_FILECOMPRESS);
1741         }
1742 }
1743
1744 static int wm_save_as_mainfile_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
1745 {
1746         char name[FILE_MAX];
1747
1748         save_set_compress(op);
1749         
1750         /* if not saved before, get the name of the most recently used .blend file */
1751         if(G.main->name[0]==0 && G.recent_files.first) {
1752                 struct RecentFile *recent = G.recent_files.first;
1753                 BLI_strncpy(name, recent->filepath, FILE_MAX);
1754         }
1755         else
1756                 BLI_strncpy(name, G.main->name, FILE_MAX);
1757         
1758         untitled(name);
1759         RNA_string_set(op->ptr, "filepath", name);
1760         
1761         WM_event_add_fileselect(C, op);
1762
1763         return OPERATOR_RUNNING_MODAL;
1764 }
1765
1766 /* function used for WM_OT_save_mainfile too */
1767 static int wm_save_as_mainfile_exec(bContext *C, wmOperator *op)
1768 {
1769         char path[FILE_MAX];
1770         int fileflags;
1771         int copy=0;
1772
1773         save_set_compress(op);
1774         
1775         if(RNA_property_is_set(op->ptr, "filepath"))
1776                 RNA_string_get(op->ptr, "filepath", path);
1777         else {
1778                 BLI_strncpy(path, G.main->name, FILE_MAX);
1779                 untitled(path);
1780         }
1781
1782         if(RNA_property_is_set(op->ptr, "copy"))
1783                 copy = RNA_boolean_get(op->ptr, "copy");
1784         
1785         fileflags= G.fileflags;
1786
1787         /* set compression flag */
1788         if(RNA_boolean_get(op->ptr, "compress"))                fileflags |=  G_FILE_COMPRESS;
1789         else                                                                                    fileflags &= ~G_FILE_COMPRESS;
1790         if(RNA_boolean_get(op->ptr, "relative_remap"))  fileflags |=  G_FILE_RELATIVE_REMAP;
1791         else                                                                                    fileflags &= ~G_FILE_RELATIVE_REMAP;
1792
1793         if ( WM_write_file(C, path, fileflags, op->reports, copy) != 0)
1794                 return OPERATOR_CANCELLED;
1795
1796         WM_event_add_notifier(C, NC_WM|ND_FILESAVE, NULL);
1797
1798         return OPERATOR_FINISHED;
1799 }
1800
1801 /* function used for WM_OT_save_mainfile too */
1802 static int blend_save_check(bContext *UNUSED(C), wmOperator *op)
1803 {
1804         char filepath[FILE_MAX];
1805         RNA_string_get(op->ptr, "filepath", filepath);
1806         if(BLI_replace_extension(filepath, sizeof(filepath), ".blend")) {
1807                 RNA_string_set(op->ptr, "filepath", filepath);
1808                 return TRUE;
1809         }
1810         return FALSE;
1811 }
1812
1813 static void WM_OT_save_as_mainfile(wmOperatorType *ot)
1814 {
1815         ot->name= "Save As Blender File";
1816         ot->idname= "WM_OT_save_as_mainfile";
1817         ot->description="Save the current file in the desired location";
1818         
1819         ot->invoke= wm_save_as_mainfile_invoke;
1820         ot->exec= wm_save_as_mainfile_exec;
1821         ot->check= blend_save_check;
1822         /* ommit window poll so this can work in background mode */
1823
1824         WM_operator_properties_filesel(ot, FOLDERFILE|BLENDERFILE, FILE_BLENDER, FILE_SAVE, WM_FILESEL_FILEPATH);
1825         RNA_def_boolean(ot->srna, "compress", 0, "Compress", "Write compressed .blend file");
1826         RNA_def_boolean(ot->srna, "relative_remap", 1, "Remap Relative", "Remap relative paths when saving in a different directory");
1827         RNA_def_boolean(ot->srna, "copy", 0, "Save Copy", "Save a copy of the actual working state but does not make saved file active.");
1828 }
1829
1830 /* *************** save file directly ******** */
1831
1832 static int wm_save_mainfile_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
1833 {
1834         char name[FILE_MAX];
1835         int check_existing=1;
1836         
1837         /* cancel if no active window */
1838         if (CTX_wm_window(C) == NULL)
1839                 return OPERATOR_CANCELLED;
1840
1841         save_set_compress(op);
1842
1843         /* if not saved before, get the name of the most recently used .blend file */
1844         if(G.main->name[0]==0 && G.recent_files.first) {
1845                 struct RecentFile *recent = G.recent_files.first;
1846                 BLI_strncpy(name, recent->filepath, FILE_MAX);
1847         }
1848         else
1849                 BLI_strncpy(name, G.main->name, FILE_MAX);
1850
1851         untitled(name);
1852         
1853         RNA_string_set(op->ptr, "filepath", name);
1854         
1855         if (RNA_struct_find_property(op->ptr, "check_existing"))
1856                 if (RNA_boolean_get(op->ptr, "check_existing")==0)
1857                         check_existing = 0;
1858         
1859         if (G.save_over) {
1860                 if (check_existing)
1861                         uiPupMenuSaveOver(C, op, name);
1862                 else {
1863                         wm_save_as_mainfile_exec(C, op);
1864                 }
1865         } else {
1866                 WM_event_add_fileselect(C, op);
1867         }
1868         
1869         return OPERATOR_RUNNING_MODAL;
1870 }
1871
1872 static void WM_OT_save_mainfile(wmOperatorType *ot)
1873 {
1874         ot->name= "Save Blender File";
1875         ot->idname= "WM_OT_save_mainfile";
1876         ot->description="Save the current Blender file";
1877         
1878         ot->invoke= wm_save_mainfile_invoke;
1879         ot->exec= wm_save_as_mainfile_exec;
1880         ot->check= blend_save_check;
1881         ot->poll= NULL;
1882         
1883         WM_operator_properties_filesel(ot, FOLDERFILE|BLENDERFILE, FILE_BLENDER, FILE_SAVE, WM_FILESEL_FILEPATH);
1884         RNA_def_boolean(ot->srna, "compress", 0, "Compress", "Write compressed .blend file");
1885         RNA_def_boolean(ot->srna, "relative_remap", 0, "Remap Relative", "Remap relative paths when saving in a different directory");
1886 }
1887
1888 /* XXX: move these collada operators to a more appropriate place */
1889 #ifdef WITH_COLLADA
1890
1891 #include "../../collada/collada.h"
1892
1893 static int wm_collada_export_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
1894 {       
1895         if(!RNA_property_is_set(op->ptr, "filepath")) {
1896                 char filepath[FILE_MAX];
1897                 BLI_strncpy(filepath, G.main->name, sizeof(filepath));
1898                 BLI_replace_extension(filepath, sizeof(filepath), ".dae");
1899                 RNA_string_set(op->ptr, "filepath", filepath);
1900         }
1901
1902         WM_event_add_fileselect(C, op);
1903
1904         return OPERATOR_RUNNING_MODAL;
1905 }
1906
1907 /* function used for WM_OT_save_mainfile too */
1908 static int wm_collada_export_exec(bContext *C, wmOperator *op)
1909 {
1910         char filename[FILE_MAX];
1911         
1912         if(!RNA_property_is_set(op->ptr, "filepath")) {
1913                 BKE_report(op->reports, RPT_ERROR, "No filename given");
1914                 return OPERATOR_CANCELLED;
1915         }
1916         
1917         RNA_string_get(op->ptr, "filepath", filename);
1918         collada_export(CTX_data_scene(C), filename);
1919         
1920         return OPERATOR_FINISHED;
1921 }
1922
1923 static void WM_OT_collada_export(wmOperatorType *ot)
1924 {
1925         ot->name= "Export COLLADA";
1926         ot->idname= "WM_OT_collada_export";
1927         
1928         ot->invoke= wm_collada_export_invoke;
1929         ot->exec= wm_collada_export_exec;
1930         ot->poll= WM_operator_winactive;
1931         
1932         WM_operator_properties_filesel(ot, FOLDERFILE|COLLADAFILE, FILE_BLENDER, FILE_SAVE, WM_FILESEL_FILEPATH);
1933 }
1934
1935 /* function used for WM_OT_save_mainfile too */
1936 static int wm_collada_import_exec(bContext *C, wmOperator *op)
1937 {
1938         char filename[FILE_MAX];
1939         
1940         if(!RNA_property_is_set(op->ptr, "filepath")) {
1941                 BKE_report(op->reports, RPT_ERROR, "No filename given");
1942                 return OPERATOR_CANCELLED;
1943         }
1944
1945         RNA_string_get(op->ptr, "filepath", filename);
1946         collada_import(C, filename);
1947         
1948         return OPERATOR_FINISHED;
1949 }
1950
1951 static void WM_OT_collada_import(wmOperatorType *ot)
1952 {
1953         ot->name= "Import COLLADA";
1954         ot->idname= "WM_OT_collada_import";
1955         
1956         ot->invoke= WM_operator_filesel;
1957         ot->exec= wm_collada_import_exec;
1958         ot->poll= WM_operator_winactive;
1959         
1960         WM_operator_properties_filesel(ot, FOLDERFILE|COLLADAFILE, FILE_BLENDER, FILE_OPENFILE, WM_FILESEL_FILEPATH);
1961 }
1962
1963 #endif
1964
1965
1966
1967 /* *********************** */
1968
1969 static void WM_OT_window_fullscreen_toggle(wmOperatorType *ot)
1970 {
1971         ot->name= "Toggle Fullscreen";
1972         ot->idname= "WM_OT_window_fullscreen_toggle";
1973         ot->description="Toggle the current window fullscreen";
1974
1975         ot->exec= wm_window_fullscreen_toggle_exec;
1976         ot->poll= WM_operator_winactive;
1977 }
1978
1979 static int wm_exit_blender_op(bContext *C, wmOperator *op)
1980 {
1981         WM_operator_free(op);
1982         
1983         WM_exit(C);     
1984         
1985         return OPERATOR_FINISHED;
1986 }
1987
1988 static void WM_OT_quit_blender(wmOperatorType *ot)
1989 {
1990         ot->name= "Quit Blender";
1991         ot->idname= "WM_OT_quit_blender";
1992         ot->description= "Quit Blender";
1993
1994         ot->invoke= WM_operator_confirm;
1995         ot->exec= wm_exit_blender_op;
1996         ot->poll= WM_operator_winactive;
1997 }
1998
1999 /* *********************** */
2000 #if defined(WIN32)
2001 static int console= 1;
2002 void WM_toggle_console(bContext *UNUSED(C), short show)
2003 {
2004         if(show) {
2005                 ShowWindow(GetConsoleWindow(),SW_SHOW);
2006                 console= 1;
2007         }
2008         else {
2009                 ShowWindow(GetConsoleWindow(),SW_HIDE);
2010                 console= 0;
2011         }
2012 }
2013
2014 static int wm_toggle_console_op(bContext *C, wmOperator *UNUSED(op))
2015 {
2016         if(console) {
2017                 WM_toggle_console(C, 0);
2018         }
2019         else {
2020                 WM_toggle_console(C, 1);
2021         }
2022         return OPERATOR_FINISHED;
2023 }
2024
2025 static void WM_OT_toggle_console(wmOperatorType *ot)
2026 {
2027         ot->name= "Toggle System Console";
2028         ot->idname= "WM_OT_toggle_console";
2029         ot->description= "Toggle System Console";
2030         
2031         ot->exec= wm_toggle_console_op;
2032         ot->poll= WM_operator_winactive;
2033 }
2034 #endif
2035
2036 /* ************ default paint cursors, draw always around cursor *********** */
2037 /*
2038  - returns handler to free 
2039  - poll(bContext): returns 1 if draw should happen
2040  - draw(bContext): drawing callback for paint cursor
2041 */
2042
2043 void *WM_paint_cursor_activate(wmWindowManager *wm, int (*poll)(bContext *C),
2044                                    wmPaintCursorDraw draw, void *customdata)
2045 {
2046         wmPaintCursor *pc= MEM_callocN(sizeof(wmPaintCursor), "paint cursor");
2047         
2048         BLI_addtail(&wm->paintcursors, pc);
2049         
2050         pc->customdata = customdata;
2051         pc->poll= poll;
2052         pc->draw= draw;
2053         
2054         return pc;
2055 }
2056
2057 void WM_paint_cursor_end(wmWindowManager *wm, void *handle)
2058 {
2059         wmPaintCursor *pc;
2060         
2061         for(pc= wm->paintcursors.first; pc; pc= pc->next) {
2062                 if(pc == (wmPaintCursor *)handle) {
2063                         BLI_remlink(&wm->paintcursors, pc);
2064                         MEM_freeN(pc);
2065                         return;
2066                 }
2067         }
2068 }
2069
2070 /* ************ window gesture operator-callback definitions ************** */
2071 /*
2072  * These are default callbacks for use in operators requiring gesture input
2073  */
2074
2075 /* **************** Border gesture *************** */
2076
2077 /* Border gesture has two types:
2078    1) WM_GESTURE_CROSS_RECT: starts a cross, on mouse click it changes to border 
2079    2) WM_GESTURE_RECT: starts immediate as a border, on mouse click or release it ends
2080
2081    It stores 4 values (xmin, xmax, ymin, ymax) and event it ended with (event_type)
2082 */
2083
2084 static int border_apply_rect(wmOperator *op)
2085 {
2086         wmGesture *gesture= op->customdata;
2087         rcti *rect= gesture->customdata;
2088         
2089         if(rect->xmin==rect->xmax || rect->ymin==rect->ymax)
2090                 return 0;
2091
2092         
2093         /* operator arguments and storage. */
2094         RNA_int_set(op->ptr, "xmin", MIN2(rect->xmin, rect->xmax) );
2095         RNA_int_set(op->ptr, "ymin", MIN2(rect->ymin, rect->ymax) );
2096         RNA_int_set(op->ptr, "xmax", MAX2(rect->xmin, rect->xmax) );
2097         RNA_int_set(op->ptr, "ymax", MAX2(rect->ymin, rect->ymax) );
2098
2099         return 1;
2100 }
2101
2102 static int border_apply(bContext *C, wmOperator *op, int gesture_mode)
2103 {
2104         if (!border_apply_rect(op))
2105                 return 0;
2106         
2107         /* XXX weak; border should be configured for this without reading event types */
2108         if( RNA_struct_find_property(op->ptr, "gesture_mode") )
2109                 RNA_int_set(op->ptr, "gesture_mode", gesture_mode);
2110
2111         op->type->exec(C, op);
2112         return 1;
2113 }
2114
2115 static void wm_gesture_end(bContext *C, wmOperator *op)
2116 {
2117         wmGesture *gesture= op->customdata;
2118         
2119         WM_gesture_end(C, gesture);     /* frees gesture itself, and unregisters from window */
2120         op->customdata= NULL;
2121
2122         ED_area_tag_redraw(CTX_wm_area(C));
2123         
2124         if( RNA_struct_find_property(op->ptr, "cursor") )
2125                 WM_cursor_restore(CTX_wm_window(C));
2126 }
2127
2128 int WM_border_select_invoke(bContext *C, wmOperator *op, wmEvent *event)
2129 {
2130         if(ISTWEAK(event->type))
2131                 op->customdata= WM_gesture_new(C, event, WM_GESTURE_RECT);
2132         else
2133                 op->customdata= WM_gesture_new(C, event, WM_GESTURE_CROSS_RECT);
2134
2135         /* add modal handler */
2136         WM_event_add_modal_handler(C, op);
2137         
2138         wm_gesture_tag_redraw(C);
2139
2140         return OPERATOR_RUNNING_MODAL;
2141 }
2142
2143 int WM_border_select_modal(bContext *C, wmOperator *op, wmEvent *event)
2144 {
2145         wmGesture *gesture= op->customdata;
2146         rcti *rect= gesture->customdata;
2147         int sx, sy;
2148         
2149         if(event->type== MOUSEMOVE) {
2150                 wm_subwindow_getorigin(CTX_wm_window(C), gesture->swinid, &sx, &sy);
2151
2152                 if(gesture->type==WM_GESTURE_CROSS_RECT && gesture->mode==0) {
2153                         rect->xmin= rect->xmax= event->x - sx;
2154                         rect->ymin= rect->ymax= event->y - sy;
2155                 }
2156                 else {
2157                         rect->xmax= event->x - sx;
2158                         rect->ymax= event->y - sy;
2159                 }
2160                 border_apply_rect(op);
2161
2162                 wm_gesture_tag_redraw(C);
2163         }
2164         else if (event->type==EVT_MODAL_MAP) {
2165                 switch (event->val) {
2166                 case GESTURE_MODAL_BEGIN:
2167                         if(gesture->type==WM_GESTURE_CROSS_RECT && gesture->mode==0) {
2168                                 gesture->mode= 1;
2169                                 wm_gesture_tag_redraw(C);
2170                         }
2171                         break;
2172                 case GESTURE_MODAL_SELECT:
2173                 case GESTURE_MODAL_DESELECT:
2174                 case GESTURE_MODAL_IN:
2175                 case GESTURE_MODAL_OUT:
2176                         if(border_apply(C, op, event->val)) {
2177                                 wm_gesture_end(C, op);
2178                                 return OPERATOR_FINISHED;
2179                         }
2180                         wm_gesture_end(C, op);
2181                         return OPERATOR_CANCELLED;
2182                         break;
2183
2184                 case GESTURE_MODAL_CANCEL:
2185                         wm_gesture_end(C, op);
2186                         return OPERATOR_CANCELLED;
2187                 }
2188
2189         }
2190 //      // Allow view navigation???
2191 //      else {
2192 //              return OPERATOR_PASS_THROUGH;
2193 //      }
2194
2195         return OPERATOR_RUNNING_MODAL;
2196 }
2197
2198 /* **************** circle gesture *************** */
2199 /* works now only for selection or modal paint stuff, calls exec while hold mouse, exit on release */
2200
2201 #ifdef GESTURE_MEMORY
2202 int circle_select_size= 25; // XXX - need some operator memory thing\!
2203 #endif
2204
2205 int WM_gesture_circle_invoke(bContext *C, wmOperator *op, wmEvent *event)
2206 {
2207         op->customdata= WM_gesture_new(C, event, WM_GESTURE_CIRCLE);
2208         
2209         /* add modal handler */
2210         WM_event_add_modal_handler(C, op);
2211         
2212         wm_gesture_tag_redraw(C);
2213         
2214         return OPERATOR_RUNNING_MODAL;
2215 }
2216
2217 static void gesture_circle_apply(bContext *C, wmOperator *op)
2218 {
2219         wmGesture *gesture= op->customdata;
2220         rcti *rect= gesture->customdata;
2221         
2222         if(RNA_int_get(op->ptr, "gesture_mode")==GESTURE_MODAL_NOP)
2223                 return;
2224
2225         /* operator arguments and storage. */
2226         RNA_int_set(op->ptr, "x", rect->xmin);
2227         RNA_int_set(op->ptr, "y", rect->ymin);
2228         RNA_int_set(op->ptr, "radius", rect->xmax);
2229         
2230         if(op->type->exec)
2231                 op->type->exec(C, op);
2232
2233 #ifdef GESTURE_MEMORY
2234         circle_select_size= rect->xmax;
2235 #endif
2236 }
2237
2238 int WM_gesture_circle_modal(bContext *C, wmOperator *op, wmEvent *event)
2239 {
2240         wmGesture *gesture= op->customdata;
2241         rcti *rect= gesture->customdata;
2242         int sx, sy;
2243
2244         if(event->type== MOUSEMOVE) {
2245                 wm_subwindow_getorigin(CTX_wm_window(C), gesture->swinid, &sx, &sy);
2246
2247                 rect->xmin= event->x - sx;
2248                 rect->ymin= event->y - sy;
2249
2250                 wm_gesture_tag_redraw(C);
2251
2252                 if(gesture->mode)
2253                         gesture_circle_apply(C, op);
2254         }
2255         else if (event->type==EVT_MODAL_MAP) {
2256                 switch (event->val) {
2257                 case GESTURE_MODAL_CIRCLE_ADD:
2258                         rect->xmax += 2 + rect->xmax/10;
2259                         wm_gesture_tag_redraw(C);
2260                         break;
2261                 case GESTURE_MODAL_CIRCLE_SUB:
2262                         rect->xmax -= 2 + rect->xmax/10;
2263                         if(rect->xmax < 1) rect->xmax= 1;
2264                         wm_gesture_tag_redraw(C);
2265                         break;
2266                 case GESTURE_MODAL_SELECT:
2267                 case GESTURE_MODAL_DESELECT:
2268                 case GESTURE_MODAL_NOP:
2269                         if(RNA_struct_find_property(op->ptr, "gesture_mode"))
2270                                 RNA_int_set(op->ptr, "gesture_mode", event->val);
2271
2272                         if(event->val != GESTURE_MODAL_NOP) {
2273                                 /* apply first click */
2274                                 gesture_circle_apply(C, op);
2275                                 gesture->mode= 1;
2276                                 wm_gesture_tag_redraw(C);
2277                         }
2278                         break;
2279
2280                 case GESTURE_MODAL_CANCEL:
2281                 case GESTURE_MODAL_CONFIRM:
2282                         wm_gesture_end(C, op);
2283                         return OPERATOR_FINISHED; /* use finish or we dont get an undo */
2284                 }
2285         }
2286 //      // Allow view navigation???
2287 //      else {
2288 //              return OPERATOR_PASS_THROUGH;
2289 //      }
2290
2291         return OPERATOR_RUNNING_MODAL;
2292 }
2293
2294 #if 0
2295 /* template to copy from */
2296 void WM_OT_circle_gesture(wmOperatorType *ot)
2297 {
2298         ot->name= "Circle Gesture";
2299         ot->idname= "WM_OT_circle_gesture";
2300         ot->description="Enter rotate mode with a circular gesture";
2301         
2302         ot->invoke= WM_gesture_circle_invoke;
2303         ot->modal= WM_gesture_circle_modal;
2304         
2305         ot->poll= WM_operator_winactive;
2306         
2307         RNA_def_property(ot->srna, "x", PROP_INT, PROP_NONE);
2308         RNA_def_property(ot->srna, "y", PROP_INT, PROP_NONE);
2309         RNA_def_property(ot->srna, "radius", PROP_INT, PROP_NONE);
2310
2311 }
2312 #endif
2313
2314 /* **************** Tweak gesture *************** */
2315
2316 static void tweak_gesture_modal(bContext *C, wmEvent *event)
2317 {
2318         wmWindow *window= CTX_wm_window(C);
2319         wmGesture *gesture= window->tweak;
2320         rcti *rect= gesture->customdata;
2321         int sx, sy, val;
2322         
2323         switch(event->type) {
2324                 case MOUSEMOVE:
2325                 case INBETWEEN_MOUSEMOVE:
2326                         
2327                         wm_subwindow_getorigin(window, gesture->swinid, &sx, &sy);
2328                         
2329                         rect->xmax= event->x - sx;
2330                         rect->ymax= event->y - sy;
2331                         
2332                         if((val= wm_gesture_evaluate(gesture))) {
2333                                 wmEvent tevent;
2334
2335                                 tevent= *(window->eventstate);
2336                                 if(gesture->event_type==LEFTMOUSE)
2337                                         tevent.type= EVT_TWEAK_L;
2338                                 else if(gesture->event_type==RIGHTMOUSE)
2339                                         tevent.type= EVT_TWEAK_R;
2340                                 else
2341                                         tevent.type= EVT_TWEAK_M;
2342                                 tevent.val= val;
2343                                 /* mouse coords! */
2344                                 wm_event_add(window, &tevent);
2345                                 
2346                                 WM_gesture_end(C, gesture);     /* frees gesture itself, and unregisters from window */
2347                         }
2348                         
2349                         break;
2350                         
2351                 case LEFTMOUSE:
2352                 case RIGHTMOUSE:
2353                 case MIDDLEMOUSE:
2354                         if(gesture->event_type==event->type) {
2355                                 WM_gesture_end(C, gesture);
2356
2357                                 /* when tweak fails we should give the other keymap entries a chance */
2358                                 event->val= KM_RELEASE;
2359                         }
2360                         break;
2361                 default:
2362                         if(!ISTIMER(event->type)) {
2363                                 WM_gesture_end(C, gesture);
2364                         }
2365                         break;
2366         }
2367 }
2368
2369 /* standard tweak, called after window handlers passed on event */
2370 void wm_tweakevent_test(bContext *C, wmEvent *event, int action)
2371 {
2372         wmWindow *win= CTX_wm_window(C);
2373         
2374         if(win->tweak==NULL) {
2375                 if(CTX_wm_region(C)) {
2376                         if(event->val==KM_PRESS) { 
2377                                 if( ELEM3(event->type, LEFTMOUSE, MIDDLEMOUSE, RIGHTMOUSE) )
2378                                         win->tweak= WM_gesture_new(C, event, WM_GESTURE_TWEAK);
2379                         }
2380                 }
2381         }
2382         else {
2383                 /* no tweaks if event was handled */
2384                 if((action & WM_HANDLER_BREAK)) {
2385                         WM_gesture_end(C, win->tweak);
2386                 }
2387                 else
2388                         tweak_gesture_modal(C, event);
2389         }
2390 }
2391
2392 /* *********************** lasso gesture ****************** */
2393
2394 int WM_gesture_lasso_invoke(bContext *C, wmOperator *op, wmEvent *event)
2395 {
2396         op->customdata= WM_gesture_new(C, event, WM_GESTURE_LASSO);
2397         
2398         /* add modal handler */
2399         WM_event_add_modal_handler(C, op);
2400         
2401         wm_gesture_tag_redraw(C);
2402         
2403         if( RNA_struct_find_property(op->ptr, "cursor") )
2404                 WM_cursor_modal(CTX_wm_window(C), RNA_int_get(op->ptr, "cursor"));
2405         
2406         return OPERATOR_RUNNING_MODAL;
2407 }
2408
2409 int WM_gesture_lines_invoke(bContext *C, wmOperator *op, wmEvent *event)
2410 {
2411         op->customdata= WM_gesture_new(C, event, WM_GESTURE_LINES);
2412         
2413         /* add modal handler */
2414         WM_event_add_modal_handler(C, op);
2415         
2416         wm_gesture_tag_redraw(C);
2417         
2418         if( RNA_struct_find_property(op->ptr, "cursor") )
2419                 WM_cursor_modal(CTX_wm_window(C), RNA_int_get(op->ptr, "cursor"));
2420         
2421         return OPERATOR_RUNNING_MODAL;
2422 }
2423
2424
2425 static void gesture_lasso_apply(bContext *C, wmOperator *op)
2426 {
2427         wmGesture *gesture= op->customdata;
2428         PointerRNA itemptr;
2429         float loc[2];
2430         int i;
2431         short *lasso= gesture->customdata;
2432         
2433         /* operator storage as path. */
2434
2435         for(i=0; i<gesture->points; i++, lasso+=2) {
2436                 loc[0]= lasso[0];
2437                 loc[1]= lasso[1];
2438                 RNA_collection_add(op->ptr, "path", &itemptr);
2439                 RNA_float_set_array(&itemptr, "loc", loc);
2440         }
2441         
2442         wm_gesture_end(C, op);
2443                 
2444         if(op->type->exec)
2445                 op->type->exec(C, op);
2446         
2447 }
2448
2449 int WM_gesture_lasso_modal(bContext *C, wmOperator *op, wmEvent *event)
2450 {
2451         wmGesture *gesture= op->customdata;
2452         int sx, sy;
2453         
2454         switch(event->type) {
2455                 case MOUSEMOVE:
2456                 case INBETWEEN_MOUSEMOVE:
2457                         
2458                         wm_gesture_tag_redraw(C);
2459                         
2460                         wm_subwindow_getorigin(CTX_wm_window(C), gesture->swinid, &sx, &sy);
2461
2462                         if(gesture->points == gesture->size) {
2463                                 short *old_lasso = gesture->customdata;
2464                                 gesture->customdata= MEM_callocN(2*sizeof(short)*(gesture->size + WM_LASSO_MIN_POINTS), "lasso points");
2465                                 memcpy(gesture->customdata, old_lasso, 2*sizeof(short)*gesture->size);
2466                                 gesture->size = gesture->size + WM_LASSO_MIN_POINTS;
2467                                 MEM_freeN(old_lasso);
2468                                 printf("realloc\n");
2469                         }
2470
2471                         {
2472                                 short x, y;
2473                                 short *lasso= gesture->customdata;
2474                                 
2475                                 lasso += (2 * gesture->points - 2);
2476                                 x = (event->x - sx - lasso[0]);
2477                                 y = (event->y - sy - lasso[1]);
2478                                 
2479                                 /* make a simple distance check to get a smoother lasso
2480                                    add only when at least 2 pixels between this and previous location */
2481                                 if((x*x+y*y) > 4) {
2482                                         lasso += 2;
2483                                         lasso[0] = event->x - sx;
2484                                         lasso[1] = event->y - sy;
2485                                         gesture->points++;
2486                                 }
2487                         }
2488                         break;
2489                         
2490                 case LEFTMOUSE:
2491                 case MIDDLEMOUSE:
2492                 case RIGHTMOUSE:
2493                         if(event->val==KM_RELEASE) {    /* key release */
2494                                 gesture_lasso_apply(C, op);
2495                                 return OPERATOR_FINISHED;
2496                         }
2497                         break;
2498                 case ESCKEY:
2499                         wm_gesture_end(C, op);
2500                         return OPERATOR_CANCELLED;
2501         }
2502         return OPERATOR_RUNNING_MODAL;
2503 }
2504
2505 int WM_gesture_lines_modal(bContext *C, wmOperator *op, wmEvent *event)
2506 {
2507         return WM_gesture_lasso_modal(C, op, event);
2508 }
2509
2510 #if 0
2511 /* template to copy from */
2512
2513 static int gesture_lasso_exec(bContext *C, wmOperator *op)
2514 {
2515         RNA_BEGIN(op->ptr, itemptr, "path") {
2516                 float loc[2];
2517                 
2518                 RNA_float_get_array(&itemptr, "loc", loc);
2519                 printf("Location: %f %f\n", loc[0], loc[1]);
2520         }
2521         RNA_END;
2522         
2523         return OPERATOR_FINISHED;
2524 }
2525
2526 void WM_OT_lasso_gesture(wmOperatorType *ot)
2527 {
2528         PropertyRNA *prop;
2529         
2530         ot->name= "Lasso Gesture";
2531         ot->idname= "WM_OT_lasso_gesture";
2532         ot->description="Select objects within the lasso as you move the pointer";
2533         
2534         ot->invoke= WM_gesture_lasso_invoke;
2535         ot->modal= WM_gesture_lasso_modal;
2536         ot->exec= gesture_lasso_exec;
2537         
2538         ot->poll= WM_operator_winactive;
2539         
2540         prop= RNA_def_property(ot->srna, "path", PROP_COLLECTION, PROP_NONE);
2541         RNA_def_property_struct_runtime(prop, &RNA_OperatorMousePath);
2542 }
2543 #endif
2544
2545 /* *********************** straight line gesture ****************** */
2546
2547 static int straightline_apply(bContext *C, wmOperator *op)
2548 {
2549         wmGesture *gesture= op->customdata;
2550         rcti *rect= gesture->customdata;
2551         
2552         if(rect->xmin==rect->xmax && rect->ymin==rect->ymax)
2553                 return 0;
2554         
2555         /* operator arguments and storage. */
2556         RNA_int_set(op->ptr, "xstart", rect->xmin);
2557         RNA_int_set(op->ptr, "ystart", rect->ymin);
2558         RNA_int_set(op->ptr, "xend", rect->xmax);
2559         RNA_int_set(op->ptr, "yend", rect->ymax);
2560
2561         if(op->type->exec)
2562                 op->type->exec(C, op);
2563         
2564         return 1;
2565 }
2566
2567
2568 int WM_gesture_straightline_invoke(bContext *C, wmOperator *op, wmEvent *event)
2569 {
2570         op->customdata= WM_gesture_new(C, event, WM_GESTURE_STRAIGHTLINE);
2571         
2572         /* add modal handler */
2573         WM_event_add_modal_handler(C, op);
2574         
2575         wm_gesture_tag_redraw(C);
2576         
2577         if( RNA_struct_find_property(op->ptr, "cursor") )
2578                 WM_cursor_modal(CTX_wm_window(C), RNA_int_get(op->ptr, "cursor"));
2579                 
2580         return OPERATOR_RUNNING_MODAL;
2581 }
2582
2583 int WM_gesture_straightline_modal(bContext *C, wmOperator *op, wmEvent *event)
2584 {
2585         wmGesture *gesture= op->customdata;
2586         rcti *rect= gesture->customdata;
2587         int sx, sy;
2588         
2589         if(event->type== MOUSEMOVE) {
2590                 wm_subwindow_getorigin(CTX_wm_window(C), gesture->swinid, &sx, &sy);
2591                 
2592                 if(gesture->mode==0) {
2593                         rect->xmin= rect->xmax= event->x - sx;
2594                         rect->ymin= rect->ymax= event->y - sy;
2595                 }
2596                 else {
2597                         rect->xmax= event->x - sx;
2598                         rect->ymax= event->y - sy;
2599                         straightline_apply(C, op);
2600                 }
2601                 
2602                 wm_gesture_tag_redraw(C);
2603         }
2604         else if (event->type==EVT_MODAL_MAP) {
2605                 switch (event->val) {
2606                         case GESTURE_MODAL_BEGIN:
2607                                 if(gesture->mode==0) {
2608                                         gesture->mode= 1;
2609                                         wm_gesture_tag_redraw(C);
2610                                 }
2611                                 break;
2612                         case GESTURE_MODAL_SELECT:
2613                                 if(straightline_apply(C, op)) {
2614                                         wm_gesture_end(C, op);
2615                                         return OPERATOR_FINISHED;
2616                                 }
2617                                 wm_gesture_end(C, op);
2618                                 return OPERATOR_CANCELLED;
2619                                 break;
2620                                 
2621                         case GESTURE_MODAL_CANCEL:
2622                                 wm_gesture_end(C, op);
2623                                 return OPERATOR_CANCELLED;
2624                 }
2625                 
2626         }
2627
2628         return OPERATOR_RUNNING_MODAL;
2629 }
2630
2631 #if 0
2632 /* template to copy from */
2633 void WM_OT_straightline_gesture(wmOperatorType *ot)
2634 {
2635         PropertyRNA *prop;
2636         
2637         ot->name= "Straight Line Gesture";
2638         ot->idname= "WM_OT_straightline_gesture";
2639         ot->description="Draw a straight line as you move the pointer";
2640         
2641         ot->invoke= WM_gesture_straightline_invoke;
2642         ot->modal= WM_gesture_straightline_modal;
2643         ot->exec= gesture_straightline_exec;
2644         
2645         ot->poll= WM_operator_winactive;
2646         
2647         WM_operator_properties_gesture_straightline(ot, 0);
2648 }
2649 #endif
2650
2651 /* *********************** radial control ****************** */
2652
2653 static const int WM_RADIAL_CONTROL_DISPLAY_SIZE = 200;
2654
2655 typedef struct wmRadialControl {
2656         int mode;
2657         float initial_value, value, max_value;
2658         float col[4], tex_col[4];
2659         int initial_mouse[2];
2660         void *cursor;
2661         GLuint tex;
2662 } wmRadialControl;
2663
2664 static void wm_radial_control_paint(bContext *C, int x, int y, void *customdata)
2665 {
2666         wmRadialControl *rc = (wmRadialControl*)customdata;
2667         ARegion *ar = CTX_wm_region(C);
2668         float r1=0.0f, r2=0.0f, r3=0.0f, angle=0.0f;
2669
2670         // int hit = 0;
2671         
2672         if(rc->mode == WM_RADIALCONTROL_STRENGTH)
2673                 rc->tex_col[3]= (rc->value + 0.5f);
2674
2675         if(rc->mode == WM_RADIALCONTROL_SIZE) {
2676                 r1= rc->value;
2677                 r2= rc->initial_value;
2678                 r3= r1;
2679         } else if(rc->mode == WM_RADIALCONTROL_STRENGTH) {
2680                 r1= (1 - rc->value) * WM_RADIAL_CONTROL_DISPLAY_SIZE;
2681                 r2= r3= WM_RADIAL_CONTROL_DISPLAY_SIZE;
2682         } else if(rc->mode == WM_RADIALCONTROL_ANGLE) {
2683                 r1= r2= r3= WM_RADIAL_CONTROL_DISPLAY_SIZE;
2684                 angle = rc->value;
2685         }
2686
2687         /* Keep cursor in the original place */
2688         x = rc->initial_mouse[0] - ar->winrct.xmin;
2689         y = rc->initial_mouse[1] - ar->winrct.ymin;
2690
2691         glTranslatef((float)x, (float)y, 0.0f);
2692
2693         glEnable(GL_BLEND);
2694
2695         if(rc->mode == WM_RADIALCONTROL_ANGLE) {
2696                 glRotatef(angle, 0, 0, 1);
2697         }
2698
2699         if (rc->tex) {
2700                 glBindTexture(GL_TEXTURE_2D, rc->tex);
2701
2702                 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
2703                 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
2704
2705                 glEnable(GL_TEXTURE_2D);
2706                 glBegin(GL_QUADS);
2707                 glColor4f(rc->tex_col[0], rc->tex_col[1], rc->tex_col[2], rc->tex_col[3]);
2708                 glTexCoord2f(0,0);
2709                 glVertex2f(-r3, -r3);
2710                 glTexCoord2f(1,0);
2711                 glVertex2f(r3, -r3);
2712                 glTexCoord2f(1,1);
2713                 glVertex2f(r3, r3);
2714                 glTexCoord2f(0,1);
2715                 glVertex2f(-r3, r3);
2716                 glEnd();
2717                 glDisable(GL_TEXTURE_2D);
2718         }
2719
2720         if(rc->mode == WM_RADIALCONTROL_ANGLE) {
2721                 glColor4f(rc->col[0], rc->col[1], rc->col[2], rc->col[3]);
2722                 glEnable(GL_LINE_SMOOTH);
2723                 glRotatef(-angle, 0, 0, 1);
2724                 fdrawline(0, 0, WM_RADIAL_CONTROL_DISPLAY_SIZE, 0);
2725                 glRotatef(angle, 0, 0, 1);
2726                 fdrawline(0, 0, WM_RADIAL_CONTROL_DISPLAY_SIZE, 0);
2727                 glDisable(GL_LINE_SMOOTH);
2728         }
2729
2730         glColor4f(rc->col[0], rc->col[1], rc->col[2], rc->col[3]);
2731         glutil_draw_lined_arc(0.0, (float)(M_PI*2.0), r1, 40);
2732         glutil_draw_lined_arc(0.0, (float)(M_PI*2.0), r2, 40);
2733         glDisable(GL_BLEND);
2734 }
2735
2736 int WM_radial_control_modal(bContext *C, wmOperator *op, wmEvent *event)
2737 {
2738         wmRadialControl *rc = (wmRadialControl*)op->customdata;
2739         int mode, initial_mouse[2], delta[2];
2740         float dist;
2741         double new_value = RNA_float_get(op->ptr, "new_value");
2742         int ret = OPERATOR_RUNNING_MODAL;
2743         // float initial_value = RNA_float_get(op->ptr, "initial_value");
2744
2745         mode = RNA_enum_get(op->ptr, "mode");
2746         RNA_int_get_array(op->ptr, "initial_mouse", initial_mouse);
2747
2748         switch(event->type) {
2749         case MOUSEMOVE:
2750                 delta[0]= initial_mouse[0] - event->x;
2751                 delta[1]= initial_mouse[1] - event->y;
2752
2753                 //if (mode == WM_RADIALCONTROL_SIZE) 
2754                 //      delta[0]+= initial_value;
2755                 //else if(mode == WM_RADIALCONTROL_STRENGTH)
2756                 //      delta[0]+= WM_RADIAL_CONTROL_DISPLAY_SIZE * (1 - initial_value);
2757                 //else if(mode == WM_RADIALCONTROL_ANGLE) {
2758                 //      delta[0]+= WM_RADIAL_CONTROL_DISPLAY_SIZE * cos(initial_value*M_PI/180.0f);
2759                 //      delta[1]+= WM_RADIAL_CONTROL_DISPLAY_SIZE * sin(initial_value*M_PI/180.0f);
2760                 //}
2761
2762                 dist= sqrt(delta[0]*delta[0]+delta[1]*delta[1]);
2763
2764                 if(mode == WM_RADIALCONTROL_SIZE)
2765                         new_value = dist;
2766                 else if(mode == WM_RADIALCONTROL_STRENGTH) {
2767                         new_value = 1 - dist / WM_RADIAL_CONTROL_DISPLAY_SIZE;
2768                 } else if(mode == WM_RADIALCONTROL_ANGLE)
2769                         new_value = ((int)(atan2f(delta[1], delta[0]) * (float)(180.0 / M_PI)) + 180);
2770                 
2771                 if(event->ctrl) {
2772                         if(mode == WM_RADIALCONTROL_STRENGTH)
2773                                 new_value = ((int)ceilf(new_value * 10.f) * 10.0f) / 100.f;
2774                         else
2775                                 new_value = ((int)new_value + 5) / 10*10;
2776                 }
2777                 
2778                 break;
2779         case ESCKEY:
2780         case RIGHTMOUSE:
2781                 ret = OPERATOR_CANCELLED;
2782                 break;
2783         case LEFTMOUSE:
2784         case PADENTER:
2785                 op->type->exec(C, op);
2786                 ret = OPERATOR_FINISHED;
2787                 break;
2788         }
2789
2790         /* Clamp */
2791         if(new_value > rc->max_value)
2792                 new_value = rc->max_value;
2793         else if(new_value < 0)
2794                 new_value = 0;
2795
2796         /* Update paint data */
2797         rc->value = new_value;
2798
2799         RNA_float_set(op->ptr, "new_value", new_value);
2800
2801         if(ret != OPERATOR_RUNNING_MODAL) {
2802                 WM_paint_cursor_end(CTX_wm_manager(C), rc->cursor);
2803                 MEM_freeN(rc);
2804         }
2805         
2806         ED_region_tag_redraw(CTX_wm_region(C));
2807
2808         //if (ret != OPERATOR_RUNNING_MODAL) {
2809         //      wmWindow *win = CTX_wm_window(C);
2810         //      WM_cursor_restore(win);
2811         //}
2812
2813         return ret;
2814 }
2815
2816 /* Expects the operator customdata to be an ImBuf (or NULL) */
2817 int WM_radial_control_invoke(bContext *C, wmOperator *op, wmEvent *event)
2818 {
2819         wmRadialControl *rc = MEM_callocN(sizeof(wmRadialControl), "radial control");
2820         // wmWindow *win = CTX_wm_window(C);
2821         int mode = RNA_enum_get(op->ptr, "mode");
2822         float initial_value = RNA_float_get(op->ptr, "initial_value");
2823         //float initial_size = RNA_float_get(op->ptr, "initial_size");
2824         int mouse[2];
2825
2826         mouse[0]= event->x;
2827         mouse[1]= event->y;
2828
2829         //if (initial_size == 0)
2830         //      initial_size = WM_RADIAL_CONTROL_DISPLAY_SIZE;
2831
2832         if(mode == WM_RADIALCONTROL_SIZE) {
2833                 rc->max_value = 200;
2834                 mouse[0]-= initial_value;
2835         }
2836         else if(mode == WM_RADIALCONTROL_STRENGTH) {
2837                 rc->max_value = 1;
2838                 mouse[0]-= WM_RADIAL_CONTROL_DISPLAY_SIZE * (1 - initial_value);
2839         }
2840         else if(mode == WM_RADIALCONTROL_ANGLE) {
2841                 rc->max_value = 360;
2842                 mouse[0]-= WM_RADIAL_CONTROL_DISPLAY_SIZE * cos(initial_value);
2843                 mouse[1]-= WM_RADIAL_CONTROL_DISPLAY_SIZE * sin(initial_value);
2844                 initial_value *= 180.0f/(float)M_PI;
2845         }
2846
2847         if(op->customdata) {
2848                 ImBuf *im = (ImBuf*)op->customdata;
2849                 /* Build GL texture */
2850                 glGenTextures(1, &rc->tex);
2851                 glBindTexture(GL_TEXTURE_2D, rc->tex);
2852                 glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, im->x, im->y, 0, GL_ALPHA, GL_FLOAT, im->rect_float);
2853                 MEM_freeN(im->rect_float);
2854                 MEM_freeN(im);
2855         }
2856
2857         RNA_float_get_array(op->ptr, "color", rc->col);
2858         RNA_float_get_array(op->ptr, "texture_color", rc->tex_col);
2859
2860         RNA_int_set_array(op->ptr, "initial_mouse", mouse);
2861         RNA_float_set(op->ptr, "new_value", initial_value);
2862                 
2863         op->customdata = rc;
2864         rc->mode = mode;
2865         rc->initial_value = initial_value;
2866         rc->initial_mouse[0] = mouse[0];
2867         rc->initial_mouse[1] = mouse[1];
2868         rc->cursor = WM_paint_cursor_activate(CTX_wm_manager(C), op->type->poll,
2869                                                   wm_radial_control_paint, op->customdata);
2870
2871         //WM_cursor_modal(win, CURSOR_NONE);
2872
2873         /* add modal handler */
2874         WM_event_add_modal_handler(C, op);
2875         
2876         WM_radial_control_modal(C, op, event);
2877         
2878         return OPERATOR_RUNNING_MODAL;
2879 }
2880
2881 /* Gets a descriptive string of the operation */
2882 void WM_radial_control_string(wmOperator *op, char str[], int maxlen)
2883 {
2884         int mode = RNA_enum_get(op->ptr, "mode");
2885         float v = RNA_float_get(op->ptr, "new_value");
2886
2887         if(mode == WM_RADIALCONTROL_SIZE)
2888                 BLI_snprintf(str, maxlen, "Size: %d", (int)v);
2889         else if(mode == WM_RADIALCONTROL_STRENGTH)
2890                 BLI_snprintf(str, maxlen, "Strength: %d", (int)v);
2891         else if(mode == WM_RADIALCONTROL_ANGLE)
2892                 BLI_snprintf(str, maxlen, "Angle: %d", (int)(v * 180.0f/(float)M_PI));
2893 }
2894
2895 /** Important: this doesn't define an actual operator, it
2896         just sets up the common parts of the radial control op. **/
2897 void WM_OT_radial_control_partial(wmOperatorType *ot)
2898 {
2899         static EnumPropertyItem radial_mode_items[] = {
2900                 {WM_RADIALCONTROL_SIZE, "SIZE", 0, "Size", ""},
2901                 {WM_RADIALCONTROL_STRENGTH, "STRENGTH", 0, "Strength", ""},
2902                 {WM_RADIALCONTROL_ANGLE, "ANGLE", 0, "Angle", ""},
2903                 {0, NULL, 0, NULL, NULL}};
2904         static float color[4] = {1.0f, 1.0f, 1.0f, 0.5f};
2905         static float tex_color[4] = {1.0f, 1.0f, 1.0f, 1.0f};
2906
2907         /* Should be set in custom invoke() */
2908         RNA_def_float(ot->srna, "initial_value", 0, 0, FLT_MAX, "Initial Value", "", 0, FLT_MAX);
2909
2910         /* Set internally, should be used in custom exec() to get final value */
2911         RNA_def_float(ot->srna, "new_value", 0, 0, FLT_MAX, "New Value", "", 0, FLT_MAX);
2912
2913         /* Should be set before calling operator */
2914         RNA_def_enum(ot->srna, "mode", radial_mode_items, 0, "Mode", "");
2915
2916         /* Internal */
2917         RNA_def_int_vector(ot->srna, "initial_mouse", 2, NULL, INT_MIN, INT_MAX, "Initial Mouse", "", INT_MIN, INT_MAX);
2918
2919         RNA_def_float_color(ot->srna, "color", 4, color, 0.0f, FLT_MAX, "Color", "Radial control color", 0.0f, 1.0f);
2920         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);
2921 }
2922
2923 /* ************************** timer for testing ***************** */
2924
2925 /* uses no type defines, fully local testing function anyway... ;) */
2926
2927 static void redraw_timer_window_swap(bContext *C)
2928 {
2929         wmWindow *win= CTX_wm_window(C);
2930         ScrArea *sa;
2931
2932         for(sa= CTX_wm_screen(C)->areabase.first; sa; sa= sa->next)
2933                 ED_area_tag_redraw(sa);
2934         wm_draw_update(C);
2935
2936         CTX_wm_window_set(C, win);      /* XXX context manipulation warning! */
2937 }
2938
2939 static EnumPropertyItem redraw_timer_type_items[] = {
2940         {0, "DRAW", 0, "Draw Region", "Draw Region"},
2941         {1, "DRAW_SWAP", 0, "Draw Region + Swap", "Draw Region and Swap"},
2942         {2, "DRAW_WIN", 0, "Draw Window", "Draw Window"},
2943         {3, "DRAW_WIN_SWAP", 0, "Draw Window + Swap", "Draw Window and Swap"},
2944         {4, "ANIM_STEP", 0, "Anim Step", "Animation Steps"},
2945         {5, "ANIM_PLAY", 0, "Anim Play", "Animation Playback"},
2946         {6, "UNDO", 0, "Undo/Redo", "Undo/Redo"},
2947         {0, NULL, 0, NULL, NULL}};
2948
2949 static int redraw_timer_exec(bContext *C, wmOperator *op)
2950 {
2951         ARegion *ar= CTX_wm_region(C);
2952         double stime= PIL_check_seconds_timer();
2953         int type = RNA_enum_get(op->ptr, "type");
2954         int iter = RNA_int_get(op->ptr, "iterations");
2955         int a;
2956         float time;
2957         const char *infostr= "";
2958         
2959         WM_cursor_wait(1);
2960
2961         for(a=0; a<iter; a++) {
2962                 if (type==0) {
2963                         if(ar)
2964                                 ED_region_do_draw(C, ar);
2965                 } 
2966                 else if (type==1) {
2967                         wmWindow *win= CTX_wm_window(C);
2968                         
2969                         ED_region_tag_redraw(ar);
2970                         wm_draw_update(C);
2971                         
2972                         CTX_wm_window_set(C, win);      /* XXX context manipulation warning! */
2973                 }
2974                 else if (type==2) {
2975                         wmWindow *win= CTX_wm_window(C);
2976                         ScrArea *sa;
2977                         
2978                         ScrArea *sa_back= CTX_wm_area(C);
2979                         ARegion *ar_back= CTX_wm_region(C);
2980
2981                         for(sa= CTX_wm_screen(C)->areabase.first; sa; sa= sa->next) {
2982                                 ARegion *ar_iter;
2983                                 CTX_wm_area_set(C, sa);
2984
2985                                 for(ar_iter= sa->regionbase.first; ar_iter; ar_iter= ar_iter->next) {
2986                                         if(ar_iter->swinid) {
2987                                                 CTX_wm_region_set(C, ar_iter);
2988                                                 ED_region_do_draw(C, ar_iter);
2989                                         }
2990                                 }
2991                         }
2992
2993                         CTX_wm_window_set(C, win);      /* XXX context manipulation warning! */
2994
2995                         CTX_wm_area_set(C, sa_back);
2996                         CTX_wm_region_set(C, ar_back);
2997                 }
2998                 else if (type==3) {
2999                         redraw_timer_window_swap(C);
3000                 }
3001                 else if (type==4) {
3002                         Main *bmain= CTX_data_main(C);
3003                         Scene *scene= CTX_data_scene(C);
3004                         
3005                         if(a & 1) scene->r.cfra--;
3006                         else scene->r.cfra++;
3007                         scene_update_for_newframe(bmain, scene, scene->lay);
3008                 }
3009                 else if (type==5) {
3010
3011                         /* play anim, return on same frame as started with */
3012                         Main *bmain= CTX_data_main(C);
3013                         Scene *scene= CTX_data_scene(C);
3014                         int tot= (scene->r.efra - scene->r.sfra) + 1;
3015
3016                         while(tot--) {
3017                                 /* todo, ability to escape! */
3018                                 scene->r.cfra++;
3019                                 if(scene->r.cfra > scene->r.efra)
3020                                         scene->r.cfra= scene->r.sfra;
3021
3022                                 scene_update_for_newframe(bmain, scene, scene->lay);
3023                                 redraw_timer_window_swap(C);
3024                         }
3025                 }
3026                 else { /* 6 */
3027                         ED_undo_pop(C);
3028                         ED_undo_redo(C);
3029                 }
3030         }
3031         
3032         time= ((PIL_check_seconds_timer()-stime)*1000);
3033
3034         RNA_enum_description(redraw_timer_type_items, type, &infostr);
3035
3036         WM_cursor_wait(0);
3037
3038         BKE_reportf(op->reports, RPT_WARNING, "%d x %s: %.2f ms,  average: %.4f", iter, infostr, time, time/iter);
3039         
3040         return OPERATOR_FINISHED;
3041 }
3042
3043 static void WM_OT_redraw_timer(wmOperatorType *ot)
3044 {
3045         ot->name= "Redraw Timer";
3046         ot->idname= "WM_OT_redraw_timer";
3047         ot->description="Simple redraw timer to test the speed of updating the interface";
3048
3049         ot->invoke= WM_menu_invoke;
3050         ot->exec= redraw_timer_exec;
3051         ot->poll= WM_operator_winactive;
3052
3053         ot->prop= RNA_def_enum(ot->srna, "type", redraw_timer_type_items, 0, "Type", "");
3054         RNA_def_int(ot->srna, "iterations", 10, 1,INT_MAX, "Iterations", "Number of times to redraw", 1,1000);
3055
3056 }
3057
3058 /* ************************** memory statistics for testing ***************** */
3059
3060 static int memory_statistics_exec(bContext *UNUSED(C), wmOperator *UNUSED(op))
3061 {
3062         MEM_printmemlist_stats();
3063         return OPERATOR_FINISHED;
3064 }
3065
3066 static void WM_OT_memory_statistics(wmOperatorType *ot)
3067 {
3068         ot->name= "Memory Statistics";
3069         ot->idname= "WM_OT_memory_statistics";
3070         ot->description= "Print memory statistics to the console";
3071         
3072         ot->exec= memory_statistics_exec;
3073 }
3074
3075 /* ******************************************************* */
3076  
3077 /* called on initialize WM_exit() */
3078 void wm_operatortype_free(void)
3079 {
3080         wmOperatorType *ot;
3081         
3082         for(ot= global_ops.first; ot; ot= ot->next) {
3083                 if(ot->macro.first)
3084                         wm_operatortype_free_macro(ot);
3085
3086                 if(ot->ext.srna) /* python operator, allocs own string */
3087                         MEM_freeN((void *)ot->idname);
3088         }
3089         
3090         BLI_freelistN(&global_ops);
3091 }
3092
3093 /* called on initialize WM_init() */
3094 void wm_operatortype_init(void)
3095 {
3096         WM_operatortype_append(WM_OT_window_duplicate);
3097         WM_operatortype_append(WM_OT_read_homefile);
3098         WM_operatortype_append(WM_OT_read_factory_settings);
3099         WM_operatortype_append(WM_OT_save_homefile);
3100         WM_operatortype_append(WM_OT_window_fullscreen_toggle);
3101         WM_operatortype_append(WM_OT_quit_blender);
3102         WM_operatortype_append(WM_OT_open_mainfile);
3103         WM_operatortype_append(WM_OT_link_append);
3104         WM_operatortype_append(WM_OT_recover_last_session);
3105         WM_operatortype_append(WM_OT_recover_auto_save);
3106         WM_operatortype_append(WM_OT_save_as_mainfile);
3107         WM_operatortype_append(WM_OT_save_mainfile);
3108         WM_operatortype_append(WM_OT_redraw_timer);
3109         WM_operatortype_append(WM_OT_memory_statistics);
3110         WM_operatortype_append(WM_OT_debug_menu);
3111         WM_operatortype_append(WM_OT_splash);
3112         WM_operatortype_append(WM_OT_search_menu);
3113         WM_operatortype_append(WM_OT_call_menu);
3114 #if defined(WIN32)
3115         WM_operatortype_append(WM_OT_toggle_console);
3116 #endif
3117
3118 #ifdef WITH_COLLADA
3119         /* XXX: move these */
3120         WM_operatortype_append(WM_OT_collada_export);
3121         WM_operatortype_append(WM_OT_collada_import);
3122 #endif
3123
3124 }
3125
3126 /* circleselect-like modal operators */
3127 static void gesture_circle_modal_keymap(wmKeyConfig *keyconf)
3128 {
3129         static EnumPropertyItem modal_items[] = {
3130         {GESTURE_MODAL_CANCEL,  "CANCEL", 0, "Cancel", ""},
3131         {GESTURE_MODAL_CONFIRM, "CONFIRM", 0, "Confirm", ""},
3132         {GESTURE_MODAL_CIRCLE_ADD, "ADD", 0, "Add", ""},
3133         {GESTURE_MODAL_CIRCLE_SUB, "SUBTRACT", 0, "Subtract", ""},
3134
3135         {GESTURE_MODAL_SELECT,  "SELECT", 0, "Select", ""},
3136         {GESTURE_MODAL_DESELECT,"DESELECT", 0, "DeSelect", ""},
3137         {GESTURE_MODAL_NOP,"NOP", 0, "No Operation", ""},
3138
3139
3140         {0, NULL, 0, NULL, NULL}};
3141
3142         wmKeyMap *keymap= WM_modalkeymap_get(keyconf, "View3D Gesture Circle");
3143
3144         /* this function is called for each spacetype, only needs to add map once */
3145         if(keymap) return;
3146
3147         keymap= WM_modalkeymap_add(keyconf, "View3D Gesture Circle", modal_items);
3148
3149         /* items for modal map */
3150         WM_modalkeymap_add_item(keymap, ESCKEY,    KM_PRESS, KM_ANY, 0, GESTURE_MODAL_CANCEL);
3151         WM_modalkeymap_add_item(keymap, RIGHTMOUSE, KM_ANY, KM_ANY, 0, GESTURE_MODAL_CANCEL);
3152
3153         WM_modalkeymap_add_item(keymap, RETKEY, KM_PRESS, KM_ANY, 0, GESTURE_MODAL_CONFIRM);
3154         WM_modalkeymap_add_item(keymap, PADENTER, KM_PRESS, 0, 0, GESTURE_MODAL_CONFIRM);
3155
3156         WM_modalkeymap_add_item(keymap, LEFTMOUSE, KM_PRESS, 0, 0, GESTURE_MODAL_SELECT);
3157
3158 #if 0 // Durien guys like this :S
3159         WM_modalkeymap_add_item(keymap, LEFTMOUSE, KM_PRESS, KM_SHIFT, 0, GESTURE_MODAL_DESELECT);
3160         WM_modalkeymap_add_item(keymap, LEFTMOUSE, KM_RELEASE, KM_SHIFT, 0, GESTURE_MODAL_NOP);
3161 #else
3162         WM_modalkeymap_add_item(keymap, MIDDLEMOUSE, KM_PRESS, 0, 0, GESTURE_MODAL_DESELECT); //  defailt 2.4x
3163         WM_modalkeymap_add_item(keymap, MIDDLEMOUSE, KM_RELEASE, 0, 0, GESTURE_MODAL_NOP); //  defailt 2.4x
3164 #endif
3165
3166         WM_modalkeymap_add_item(keymap, LEFTMOUSE, KM_RELEASE, 0, 0, GESTURE_MODAL_NOP);
3167
3168         WM_modalkeymap_add_item(keymap, WHEELUPMOUSE, KM_PRESS, 0, 0, GESTURE_MODAL_CIRCLE_SUB);
3169         WM_modalkeymap_add_item(keymap, PADMINUS, KM_PRESS, 0, 0, GESTURE_MODAL_CIRCLE_SUB);
3170         WM_modalkeymap_add_item(keymap, WHEELDOWNMOUSE, KM_PRESS, 0, 0, GESTURE_MODAL_CIRCLE_ADD);
3171         WM_modalkeymap_add_item(keymap, PADPLUSKEY, KM_PRESS, 0, 0, GESTURE_MODAL_CIRCLE_ADD);
3172
3173         /* assign map to operators */
3174         WM_modalkeymap_assign(keymap, "VIEW3D_OT_select_circle");
3175         WM_modalkeymap_assign(keymap, "UV_OT_circle_select");
3176
3177 }
3178
3179 /* straight line modal operators */
3180 static void gesture_straightline_modal_keymap(wmKeyConfig *keyconf)
3181 {
3182         static EnumPropertyItem modal_items[] = {
3183                 {GESTURE_MODAL_CANCEL,  "CANCEL", 0, "Cancel", ""},
3184                 {GESTURE_MODAL_SELECT,  "SELECT", 0, "Select", ""},
3185                 {GESTURE_MODAL_BEGIN,   "BEGIN", 0, "Begin", ""},
3186                 {0, NULL, 0, NULL, NULL}};
3187         
3188         wmKeyMap *keymap= WM_modalkeymap_get(keyconf, "Gesture Straight Line");
3189         
3190         /* this function is called for each spacetype, only needs to add map once */
3191         if(keymap) return;
3192         
3193         keymap= WM_modalkeymap_add(keyconf, "Gesture Straight Line", modal_items);
3194         
3195         /* items for modal map */
3196         WM_modalkeymap_add_item(keymap, ESCKEY,    KM_PRESS, KM_ANY, 0, GESTURE_MODAL_CANCEL);
3197         WM_modalkeymap_add_item(keymap, RIGHTMOUSE, KM_ANY, KM_ANY, 0, GESTURE_MODAL_CANCEL);
3198         
3199         WM_modalkeymap_add_item(keymap, LEFTMOUSE, KM_PRESS, 0, 0, GESTURE_MODAL_BEGIN);
3200         WM_modalkeymap_add_item(keymap, LEFTMOUSE, KM_RELEASE, 0, 0, GESTURE_MODAL_SELECT);
3201         
3202         /* assign map to operators */
3203         WM_modalkeymap_assign(keymap, "IMAGE_OT_sample_line");
3204 }
3205
3206
3207 /* borderselect-like modal operators */
3208 static void gesture_border_modal_keymap(wmKeyConfig *keyconf)
3209 {
3210         static EnumPropertyItem modal_items[] = {
3211         {GESTURE_MODAL_CANCEL,  "CANCEL", 0, "Cancel", ""},
3212         {GESTURE_MODAL_SELECT,  "SELECT", 0, "Select", ""},
3213         {GESTURE_MODAL_DESELECT,"DESELECT", 0, "DeSelect", ""},
3214         {GESTURE_MODAL_BEGIN,   "BEGIN", 0, "Begin", ""},
3215         {0, NULL, 0, NULL, NULL}};
3216
3217         wmKeyMap *keymap= WM_modalkeymap_get(keyconf, "Gesture Border");
3218
3219         /* this function is called for each spacetype, only needs to add map once */
3220         if(keymap) return;
3221
3222         keymap= WM_modalkeymap_add(keyconf, "Gesture Border", modal_items);
3223
3224         /* items for modal map */
3225         WM_modalkeymap_add_item(keymap, ESCKEY,    KM_PRESS, KM_ANY, 0, GESTURE_MODAL_CANCEL);
3226         WM_modalkeymap_add_item(keymap, RIGHTMOUSE, KM_ANY, KM_ANY, 0, GESTURE_MODAL_CANCEL);
3227
3228         WM_modalkeymap_add_item(keymap, LEFTMOUSE, KM_PRESS, 0, 0, GESTURE_MODAL_BEGIN);
3229         WM_modalkeymap_add_item(keymap, LEFTMOUSE, KM_RELEASE, KM_ANY, 0, GESTURE_MODAL_SELECT);
3230
3231 #if 0 // Durian guys like this
3232         WM_modalkeymap_add_item(keymap, LEFTMOUSE, KM_PRESS, KM_SHIFT, 0, GESTURE_MODAL_BEGIN);
3233         WM_modalkeymap_add_item(keymap, LEFTMOUSE, KM_RELEASE, KM_SHIFT, 0, GESTURE_MODAL_DESELECT);
3234 #else
3235         WM_modalkeymap_add_item(keymap, MIDDLEMOUSE, KM_PRESS, 0, 0, GESTURE_MODAL_BEGIN);
3236         WM_modalkeymap_add_item(keymap, MIDDLEMOUSE, KM_RELEASE, 0, 0, GESTURE_MODAL_DESELECT);
3237 #endif
3238
3239         /* assign map to operators */
3240         WM_modalkeymap_assign(keymap, "ACTION_OT_select_border");
3241         WM_modalkeymap_assign(keymap, "ANIM_OT_channels_select_border");
3242         WM_modalkeymap_assign(keymap, "ANIM_OT_previewrange_set");
3243         WM_modalkeymap_assign(keymap, "INFO_OT_select_border");
3244         WM_modalkeymap_assign(keymap, "FILE_OT_select_border");
3245         WM_modalkeymap_assign(keymap, "GRAPH_OT_select_border");
3246         WM_modalkeymap_assign(keymap, "MARKER_OT_select_border");
3247         WM_modalkeymap_assign(keymap, "NLA_OT_select_border");
3248         WM_modalkeymap_assign(keymap, "NODE_OT_select_border");
3249 //      WM_modalkeymap_assign(keymap, "SCREEN_OT_border_select"); // template
3250         WM_modalkeymap_assign(keymap, "SEQUENCER_OT_select_border");
3251         WM_modalkeymap_assign(keymap, "SEQUENCER_OT_view_ghost_border");
3252         WM_modalkeymap_assign(keymap, "UV_OT_select_border");
3253         WM_modalkeymap_assign(keymap, "VIEW2D_OT_zoom_border");
3254         WM_modalkeymap_assign(keymap, "VIEW3D_OT_clip_border");
3255         WM_modalkeymap_assign(keymap, "VIEW3D_OT_render_border");
3256         WM_modalkeymap_assign(keymap, "VIEW3D_OT_select_border");
3257         WM_modalkeymap_assign(keymap, "VIEW3D_OT_zoom_border"); // XXX TODO: zoom border should perhaps map rightmouse to zoom out instead of in+cancel
3258 }
3259
3260 /* zoom to border modal operators */
3261 static void gesture_zoom_border_modal_keymap(wmKeyConfig *keyconf)
3262 {
3263         static EnumPropertyItem modal_items[] = {
3264         {GESTURE_MODAL_CANCEL, "CANCEL", 0, "Cancel", ""},
3265         {GESTURE_MODAL_IN,      "IN", 0, "In", ""},
3266         {GESTURE_MODAL_OUT, "OUT", 0, "Out", ""},
3267         {GESTURE_MODAL_BEGIN, "BEGIN", 0, "Begin", ""},
3268         {0, NULL, 0, NULL, NULL}};
3269
3270         wmKeyMap *keymap= WM_modalkeymap_get(keyconf, "Gesture Zoom Border");
3271
3272         /* this function is called for each spacetype, only needs to add map once */
3273         if(keymap) return;
3274
3275         keymap= WM_modalkeymap_add(keyconf, "Gesture Zoom Border", modal_items);
3276
3277         /* items for modal map */
3278         WM_modalkeymap_add_item(keymap, ESCKEY,    KM_PRESS, KM_ANY, 0, GESTURE_MODAL_CANCEL);
3279         WM_modalkeymap_add_item(keymap, RIGHTMOUSE, KM_ANY, KM_ANY, 0, GESTURE_MODAL_CANCEL);
3280
3281         WM_modalkeymap_add_item(keymap, LEFTMOUSE, KM_PRESS, 0, 0, GESTURE_MODAL_BEGIN);
3282         WM_modalkeymap_add_item(keymap, LEFTMOUSE, KM_RELEASE, 0, 0, GESTURE_MODAL_IN); 
3283
3284         WM_modalkeymap_add_item(keymap, MIDDLEMOUSE, KM_PRESS, 0, 0, GESTURE_MODAL_BEGIN);
3285         WM_modalkeymap_add_item(keymap, MIDDLEMOUSE, KM_RELEASE, 0, 0, GESTURE_MODAL_OUT);
3286
3287         /* assign map to operators */
3288         WM_modalkeymap_assign(keymap, &