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