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