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