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