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