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