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