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