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