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