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