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