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