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