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