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