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