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