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