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