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