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