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