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