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