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