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