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