- player building again
[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;
938
939 #ifdef NAN_BUILDINFO
940         int ver_width, rev_width;
941         char *version_str = NULL;
942         char *revision_str = NULL;
943         char version_buf[128];
944         char revision_buf[128];
945         extern char * build_rev;
946         char *cp;
947         
948         version_str = &version_buf[0];
949         revision_str = &revision_buf[0];
950         
951         sprintf(version_str, "%d.%02d.%d", BLENDER_VERSION/100, BLENDER_VERSION%100, BLENDER_SUBVERSION);
952         sprintf(revision_str, "r%s", build_rev);
953         
954         /* here on my system I get ugly double quotes around the revision number.
955          * if so, clip it off: */
956         cp = strchr(revision_str, '"');
957         if (cp) {
958                 memmove(cp, cp+1, strlen(cp+1));
959                 cp = strchr(revision_str, '"');
960                 if (cp)
961                         *cp = 0;
962         }
963         
964         BLF_size(style->widgetlabel.points, U.dpi);
965         ver_width = BLF_width(version_str)+5;
966         rev_width = BLF_width(revision_str)+5;
967 #endif //NAN_BUILDINFO
968
969         block= uiBeginBlock(C, ar, "_popup", UI_EMBOSS);
970         uiBlockSetFlag(block, UI_BLOCK_KEEP_OPEN|UI_BLOCK_RET_1);
971         
972         but= uiDefBut(block, BUT_IMAGE, 0, "", 0, 10, 501, 282, NULL, 0.0, 0.0, 0, 0, "");
973         uiButSetFunc(but, wm_block_splash_close, block, NULL);
974         
975 #ifdef NAN_BUILDINFO    
976         uiDefBut(block, LABEL, 0, version_str, 500-ver_width, 282-24, ver_width, 20, NULL, 0, 0, 0, 0, NULL);
977         uiDefBut(block, LABEL, 0, revision_str, 500-rev_width, 282-36, rev_width, 20, NULL, 0, 0, 0, 0, NULL);
978 #endif //NAN_BUILDINFO
979         
980         
981         uiBlockSetEmboss(block, UI_EMBOSSP);
982         
983         layout= uiBlockLayout(block, UI_LAYOUT_VERTICAL, UI_LAYOUT_MENU, 10, 10, 480, 110, style);
984
985         uiLayoutSetOperatorContext(layout, WM_OP_EXEC_REGION_WIN);
986         
987         split = uiLayoutSplit(layout, 0, 0);
988         col = uiLayoutColumn(split, 0);
989         uiItemL(col, "Links", 0);
990         uiItemO(col, NULL, ICON_URL, "HELP_OT_release_logs");
991         uiItemO(col, NULL, ICON_URL, "HELP_OT_manual");
992         uiItemO(col, NULL, ICON_URL, "HELP_OT_blender_website");
993         uiItemO(col, NULL, ICON_URL, "HELP_OT_user_community");
994         uiItemO(col, NULL, ICON_URL, "HELP_OT_python_api");
995         uiItemL(col, "", 0);
996         
997         col = uiLayoutColumn(split, 0);
998         uiItemL(col, "Recent", 0);
999         for(recent = G.recent_files.first, i=0; (i<6) && (recent); recent = recent->next, i++) {
1000                 char *display_name= BLI_last_slash(recent->filename);
1001                 if(display_name)        display_name++; /* skip the slash */
1002                 else                            display_name= recent->filename;
1003                 uiItemStringO(col, display_name, ICON_FILE_BLEND, "WM_OT_open_mainfile", "path", recent->filename);
1004         }
1005
1006         uiItemL(col, "", 0);
1007
1008         uiCenteredBoundsBlock(block, 0.0f);
1009         uiEndBlock(C, block);
1010         
1011         return block;
1012 }
1013
1014 static int wm_splash_invoke(bContext *C, wmOperator *op, wmEvent *event)
1015 {
1016         uiPupBlock(C, wm_block_create_splash, NULL);
1017         
1018         return OPERATOR_FINISHED;
1019 }
1020
1021 static void WM_OT_splash(wmOperatorType *ot)
1022 {
1023         ot->name= "Splash Screen";
1024         ot->idname= "WM_OT_splash";
1025         ot->description= "Opens a blocking popup region with release info";
1026         
1027         ot->invoke= wm_splash_invoke;
1028         ot->poll= WM_operator_winactive;
1029 }
1030
1031
1032 /* ***************** Search menu ************************* */
1033 static void operator_call_cb(struct bContext *C, void *arg1, void *arg2)
1034 {
1035         wmOperatorType *ot= arg2;
1036         
1037         if(ot)
1038                 WM_operator_name_call(C, ot->idname, WM_OP_INVOKE_DEFAULT, NULL);
1039 }
1040
1041 static void operator_search_cb(const struct bContext *C, void *arg, char *str, uiSearchItems *items)
1042 {
1043         wmOperatorType *ot = WM_operatortype_first();
1044         
1045         for(; ot; ot= ot->next) {
1046                 
1047                 if(BLI_strcasestr(ot->name, str)) {
1048                         if(WM_operator_poll((bContext*)C, ot)) {
1049                                 char name[256];
1050                                 int len= strlen(ot->name);
1051                                 
1052                                 /* display name for menu, can hold hotkey */
1053                                 BLI_strncpy(name, ot->name, 256);
1054                                 
1055                                 /* check for hotkey */
1056                                 if(len < 256-6) {
1057                                         if(WM_key_event_operator_string(C, ot->idname, WM_OP_EXEC_DEFAULT, NULL, &name[len+1], 256-len-1))
1058                                                 name[len]= '|';
1059                                 }
1060                                 
1061                                 if(0==uiSearchItemAdd(items, name, ot, 0))
1062                                         break;
1063                         }
1064                 }
1065         }
1066 }
1067
1068 static uiBlock *wm_block_search_menu(bContext *C, ARegion *ar, void *arg_op)
1069 {
1070         static char search[256]= "";
1071         wmEvent event;
1072         wmWindow *win= CTX_wm_window(C);
1073         uiBlock *block;
1074         uiBut *but;
1075         
1076         block= uiBeginBlock(C, ar, "_popup", UI_EMBOSS);
1077         uiBlockSetFlag(block, UI_BLOCK_LOOP|UI_BLOCK_RET_1|UI_BLOCK_MOVEMOUSE_QUIT);
1078         
1079         but= uiDefSearchBut(block, search, 0, ICON_VIEWZOOM, 256, 10, 10, 180, 19, 0, 0, "");
1080         uiButSetSearchFunc(but, operator_search_cb, NULL, operator_call_cb, NULL);
1081         
1082         /* fake button, it holds space for search items */
1083         uiDefBut(block, LABEL, 0, "", 10, 10 - uiSearchBoxhHeight(), 180, uiSearchBoxhHeight(), NULL, 0, 0, 0, 0, NULL);
1084         
1085         uiPopupBoundsBlock(block, 6.0f, 0, -20); /* move it downwards, mouse over button */
1086         uiEndBlock(C, block);
1087         
1088         event= *(win->eventstate);      /* XXX huh huh? make api call */
1089         event.type= EVT_BUT_OPEN;
1090         event.val= KM_PRESS;
1091         event.customdata= but;
1092         event.customdatafree= FALSE;
1093         wm_event_add(win, &event);
1094         
1095         return block;
1096 }
1097
1098 static int wm_search_menu_exec(bContext *C, wmOperator *op)
1099 {
1100         
1101         return OPERATOR_FINISHED;       
1102 }
1103
1104 static int wm_search_menu_invoke(bContext *C, wmOperator *op, wmEvent *event)
1105 {
1106         
1107         uiPupBlock(C, wm_block_search_menu, op);
1108         
1109         return OPERATOR_CANCELLED;
1110 }
1111
1112 /* op->poll */
1113 static int wm_search_menu_poll(bContext *C)
1114 {
1115         if(CTX_wm_window(C)==NULL) return 0;
1116         if(CTX_wm_area(C) && CTX_wm_area(C)->spacetype==SPACE_CONSOLE) return 0;  // XXX - so we can use the shortcut in the console
1117         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
1118         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
1119         return 1;
1120 }
1121
1122 static void WM_OT_search_menu(wmOperatorType *ot)
1123 {
1124         ot->name= "Search Menu";
1125         ot->idname= "WM_OT_search_menu";
1126         
1127         ot->invoke= wm_search_menu_invoke;
1128         ot->exec= wm_search_menu_exec;
1129         ot->poll= wm_search_menu_poll;
1130 }
1131
1132 static int wm_call_menu_invoke(bContext *C, wmOperator *op, wmEvent *event)
1133 {
1134         char idname[BKE_ST_MAXNAME];
1135         RNA_string_get(op->ptr, "name", idname);
1136
1137         uiPupMenuInvoke(C, idname);
1138
1139         return OPERATOR_CANCELLED;
1140 }
1141
1142 static void WM_OT_call_menu(wmOperatorType *ot)
1143 {
1144         ot->name= "Call Menu";
1145         ot->idname= "WM_OT_call_menu";
1146
1147         ot->invoke= wm_call_menu_invoke;
1148
1149         RNA_def_string(ot->srna, "name", "", BKE_ST_MAXNAME, "Name", "Name of the menu");
1150 }
1151
1152 /* ************ window / screen operator definitions ************** */
1153
1154 static void WM_OT_window_duplicate(wmOperatorType *ot)
1155 {
1156         ot->name= "Duplicate Window";
1157         ot->idname= "WM_OT_window_duplicate";
1158         ot->description="Duplicate the current Blender window.";
1159                 
1160         ot->exec= wm_window_duplicate_op;
1161         ot->poll= WM_operator_winactive;
1162 }
1163
1164 static void WM_OT_save_homefile(wmOperatorType *ot)
1165 {
1166         ot->name= "Save User Settings";
1167         ot->idname= "WM_OT_save_homefile";
1168         ot->description="Make the current file the default .blend file.";
1169                 
1170         ot->invoke= WM_operator_confirm;
1171         ot->exec= WM_write_homefile;
1172         ot->poll= WM_operator_winactive;
1173 }
1174
1175 static void WM_OT_read_homefile(wmOperatorType *ot)
1176 {
1177         ot->name= "Reload Start-Up File";
1178         ot->idname= "WM_OT_read_homefile";
1179         ot->description="Open the default file (doesn't save the current file).";
1180         
1181         ot->invoke= WM_operator_confirm;
1182         ot->exec= WM_read_homefile;
1183         ot->poll= WM_operator_winactive;
1184         
1185         RNA_def_boolean(ot->srna, "factory", 0, "Factory Settings", "");
1186 }
1187
1188 /* *************** open file **************** */
1189
1190 static void open_set_load_ui(wmOperator *op)
1191 {
1192         if(!RNA_property_is_set(op->ptr, "load_ui"))
1193                 RNA_boolean_set(op->ptr, "load_ui", !(U.flag & USER_FILENOUI));
1194 }
1195
1196 static int wm_open_mainfile_invoke(bContext *C, wmOperator *op, wmEvent *event)
1197 {
1198         RNA_string_set(op->ptr, "path", G.sce);
1199         open_set_load_ui(op);
1200
1201         WM_event_add_fileselect(C, op);
1202
1203         return OPERATOR_RUNNING_MODAL;
1204 }
1205
1206 static int wm_open_mainfile_exec(bContext *C, wmOperator *op)
1207 {
1208         char path[FILE_MAX];
1209
1210         RNA_string_get(op->ptr, "path", path);
1211         open_set_load_ui(op);
1212
1213         if(RNA_boolean_get(op->ptr, "load_ui"))
1214                 G.fileflags &= ~G_FILE_NO_UI;
1215         else
1216                 G.fileflags |= G_FILE_NO_UI;
1217         
1218         // XXX wm in context is not set correctly after WM_read_file -> crash
1219         // do it before for now, but is this correct with multiple windows?
1220         WM_event_add_notifier(C, NC_WINDOW, NULL);
1221
1222         WM_read_file(C, path, op->reports);
1223         
1224         return OPERATOR_FINISHED;
1225 }
1226
1227 static void WM_OT_open_mainfile(wmOperatorType *ot)
1228 {
1229         ot->name= "Open Blender File";
1230         ot->idname= "WM_OT_open_mainfile";
1231         ot->description="Open a Blender file.";
1232         
1233         ot->invoke= wm_open_mainfile_invoke;
1234         ot->exec= wm_open_mainfile_exec;
1235         ot->poll= WM_operator_winactive;
1236         
1237         WM_operator_properties_filesel(ot, FOLDERFILE|BLENDERFILE, FILE_BLENDER);
1238
1239         RNA_def_boolean(ot->srna, "load_ui", 1, "Load UI", "Load user interface setup in the .blend file.");
1240 }
1241
1242 /* **************** link/append *************** */
1243
1244 static int wm_link_append_invoke(bContext *C, wmOperator *op, wmEvent *event)
1245 {
1246         if(RNA_property_is_set(op->ptr, "path")) {
1247                 return WM_operator_call(C, op);
1248         } 
1249         else {
1250                 /* XXX TODO solve where to get last linked library from */
1251                 RNA_string_set(op->ptr, "path", G.lib);
1252                 WM_event_add_fileselect(C, op);
1253                 return OPERATOR_RUNNING_MODAL;
1254         }
1255 }
1256
1257 static short wm_link_append_flag(wmOperator *op)
1258 {
1259         short flag= 0;
1260
1261         if(RNA_boolean_get(op->ptr, "autoselect")) flag |= FILE_AUTOSELECT;
1262         if(RNA_boolean_get(op->ptr, "active_layer")) flag |= FILE_ACTIVELAY;
1263         if(RNA_boolean_get(op->ptr, "relative_paths")) flag |= FILE_STRINGCODE;
1264         if(RNA_boolean_get(op->ptr, "link")) flag |= FILE_LINK;
1265         if(RNA_boolean_get(op->ptr, "instance_groups")) flag |= FILE_GROUP_INSTANCE;
1266         return flag;
1267 }
1268
1269 static void wm_link_make_library_local(Main *main, const char *libname)
1270 {
1271         Library *lib;
1272
1273         /* and now find the latest append lib file */
1274         for(lib= main->library.first; lib; lib=lib->id.next)
1275                 if(BLI_streq(libname, lib->filename))
1276                         break;
1277         
1278         /* make local */
1279         if(lib) {
1280                 all_local(lib, 1);
1281         }
1282 }
1283
1284 static int wm_link_append_exec(bContext *C, wmOperator *op)
1285 {
1286         Main *bmain= CTX_data_main(C);
1287         Scene *scene= CTX_data_scene(C);
1288         Main *mainl= 0;
1289         BlendHandle *bh;
1290         PropertyRNA *prop;
1291         char name[FILE_MAX], dir[FILE_MAX], libname[FILE_MAX], group[GROUP_MAX];
1292         int idcode, totfiles=0;
1293         short flag;
1294
1295         name[0] = '\0';
1296         RNA_string_get(op->ptr, "filename", name);
1297         RNA_string_get(op->ptr, "directory", dir);
1298
1299         /* test if we have a valid data */
1300         if(BLO_is_a_library(dir, libname, group) == 0) {
1301                 BKE_report(op->reports, RPT_ERROR, "Not a library");
1302                 return OPERATOR_CANCELLED;
1303         }
1304         else if(group[0] == 0) {
1305                 BKE_report(op->reports, RPT_ERROR, "Nothing indicated");
1306                 return OPERATOR_CANCELLED;
1307         }
1308         else if(BLI_streq(bmain->name, libname)) {
1309                 BKE_report(op->reports, RPT_ERROR, "Cannot use current file as library");
1310                 return OPERATOR_CANCELLED;
1311         }
1312
1313         /* check if something is indicated for append/link */
1314         prop = RNA_struct_find_property(op->ptr, "files");
1315         if(prop) {
1316                 totfiles= RNA_property_collection_length(op->ptr, prop);
1317                 if(totfiles == 0) {
1318                         if(name[0] == '\0') {
1319                                 BKE_report(op->reports, RPT_ERROR, "Nothing indicated");
1320                                 return OPERATOR_CANCELLED;
1321                         }
1322                 }
1323         }
1324         else if(name[0] == '\0') {
1325                 BKE_report(op->reports, RPT_ERROR, "Nothing indicated");
1326                 return OPERATOR_CANCELLED;
1327         }
1328
1329         /* now we have or selected, or an indicated file */
1330         if(RNA_boolean_get(op->ptr, "autoselect"))
1331                 scene_deselect_all(scene);
1332
1333         bh = BLO_blendhandle_from_file(libname);
1334         idcode = BLO_idcode_from_name(group);
1335         
1336         flag = wm_link_append_flag(op);
1337
1338         /* tag everything, all untagged data can be made local
1339          * its also generally useful to know what is new
1340          *
1341          * take extra care flag_all_listbases_ids(LIB_LINK_TAG, 0) is called after! */
1342         flag_all_listbases_ids(LIB_PRE_EXISTING, 1);
1343
1344         /* here appending/linking starts */
1345         mainl = BLO_library_append_begin(C, &bh, libname);
1346         if(totfiles == 0) {
1347                 BLO_library_append_named_part(C, mainl, &bh, name, idcode, flag);
1348         }
1349         else {
1350                 RNA_BEGIN(op->ptr, itemptr, "files") {
1351                         RNA_string_get(&itemptr, "name", name);
1352                         BLO_library_append_named_part(C, mainl, &bh, name, idcode, flag);
1353                 }
1354                 RNA_END;
1355         }
1356         BLO_library_append_end(C, mainl, &bh, idcode, flag);
1357         
1358         /* mark all library linked objects to be updated */
1359         recalc_all_library_objects(bmain);
1360
1361         /* append, rather than linking */
1362         if((flag & FILE_LINK)==0)
1363                 wm_link_make_library_local(bmain, libname);
1364
1365         /* important we unset, otherwise these object wont
1366          * link into other scenes from this blend file */
1367         flag_all_listbases_ids(LIB_PRE_EXISTING, 0);
1368
1369         /* recreate dependency graph to include new objects */
1370         DAG_scene_sort(scene);
1371         DAG_ids_flush_update(0);
1372
1373         BLO_blendhandle_close(bh);
1374
1375         /* XXX TODO: align G.lib with other directory storage (like last opened image etc...) */
1376         BLI_strncpy(G.lib, dir, FILE_MAX);
1377
1378         WM_event_add_notifier(C, NC_WINDOW, NULL);
1379
1380         return OPERATOR_FINISHED;
1381 }
1382
1383 static void WM_OT_link_append(wmOperatorType *ot)
1384 {
1385         ot->name= "Link/Append from Library";
1386         ot->idname= "WM_OT_link_append";
1387         ot->description= "Link or Append from a Library .blend file";
1388         
1389         ot->invoke= wm_link_append_invoke;
1390         ot->exec= wm_link_append_exec;
1391         ot->poll= WM_operator_winactive;
1392         
1393         ot->flag |= OPTYPE_UNDO;
1394
1395         WM_operator_properties_filesel(ot, FOLDERFILE|BLENDERFILE, FILE_LOADLIB);
1396         
1397         RNA_def_boolean(ot->srna, "link", 1, "Link", "Link the objects or datablocks rather than appending.");
1398         RNA_def_boolean(ot->srna, "autoselect", 1, "Select", "Select the linked objects.");
1399         RNA_def_boolean(ot->srna, "active_layer", 1, "Active Layer", "Put the linked objects on the active layer.");
1400         RNA_def_boolean(ot->srna, "instance_groups", 1, "Instance Groups", "Create instances for each group as a DupliGroup.");
1401         RNA_def_boolean(ot->srna, "relative_paths", 1, "Relative Paths", "Store the library path as a relative path to current .blend file.");
1402
1403         RNA_def_collection_runtime(ot->srna, "files", &RNA_OperatorFileListElement, "Files", "");
1404 }       
1405
1406 /* *************** recover last session **************** */
1407
1408 static int wm_recover_last_session_exec(bContext *C, wmOperator *op)
1409 {
1410         char filename[FILE_MAX];
1411
1412         G.fileflags |= G_FILE_RECOVER;
1413
1414         // XXX wm in context is not set correctly after WM_read_file -> crash
1415         // do it before for now, but is this correct with multiple windows?
1416         WM_event_add_notifier(C, NC_WINDOW, NULL);
1417
1418         /* load file */
1419         BLI_make_file_string("/", filename, btempdir, "quit.blend");
1420         WM_read_file(C, filename, op->reports);
1421
1422         G.fileflags &= ~G_FILE_RECOVER;
1423
1424         return OPERATOR_FINISHED;
1425 }
1426
1427 static void WM_OT_recover_last_session(wmOperatorType *ot)
1428 {
1429         ot->name= "Recover Last Session";
1430         ot->idname= "WM_OT_recover_last_session";
1431         ot->description="Open the last closed file (\"quit.blend\").";
1432         
1433         ot->exec= wm_recover_last_session_exec;
1434         ot->poll= WM_operator_winactive;
1435 }
1436
1437 /* *************** recover auto save **************** */
1438
1439 static int wm_recover_auto_save_exec(bContext *C, wmOperator *op)
1440 {
1441         char path[FILE_MAX];
1442
1443         RNA_string_get(op->ptr, "path", path);
1444
1445         G.fileflags |= G_FILE_RECOVER;
1446
1447         // XXX wm in context is not set correctly after WM_read_file -> crash
1448         // do it before for now, but is this correct with multiple windows?
1449         WM_event_add_notifier(C, NC_WINDOW, NULL);
1450
1451         /* load file */
1452         WM_read_file(C, path, op->reports);
1453
1454         G.fileflags &= ~G_FILE_RECOVER;
1455
1456         return OPERATOR_FINISHED;
1457 }
1458
1459 static int wm_recover_auto_save_invoke(bContext *C, wmOperator *op, wmEvent *event)
1460 {
1461         char filename[FILE_MAX];
1462
1463         wm_autosave_location(filename);
1464         RNA_string_set(op->ptr, "path", filename);
1465         WM_event_add_fileselect(C, op);
1466
1467         return OPERATOR_RUNNING_MODAL;
1468 }
1469
1470 static void WM_OT_recover_auto_save(wmOperatorType *ot)
1471 {
1472         ot->name= "Recover Auto Save";
1473         ot->idname= "WM_OT_recover_auto_save";
1474         ot->description="Open an automatically saved file to recover it.";
1475         
1476         ot->exec= wm_recover_auto_save_exec;
1477         ot->invoke= wm_recover_auto_save_invoke;
1478         ot->poll= WM_operator_winactive;
1479
1480         WM_operator_properties_filesel(ot, BLENDERFILE, FILE_BLENDER);
1481 }
1482
1483 /* *************** save file as **************** */
1484
1485 static void untitled(char *name)
1486 {
1487         if(G.save_over == 0 && strlen(name) < FILE_MAX-16) {
1488                 char *c= BLI_last_slash(name);
1489                 
1490                 if(c)
1491                         strcpy(&c[1], "untitled.blend");
1492                 else
1493                         strcpy(name, "untitled.blend");
1494         }
1495 }
1496
1497 static void save_set_compress(wmOperator *op)
1498 {
1499         if(!RNA_property_is_set(op->ptr, "compress")) {
1500                 if(G.save_over) /* keep flag for existing file */
1501                         RNA_boolean_set(op->ptr, "compress", G.fileflags & G_FILE_COMPRESS);
1502                 else /* use userdef for new file */
1503                         RNA_boolean_set(op->ptr, "compress", U.flag & USER_FILECOMPRESS);
1504         }
1505 }
1506
1507 static int wm_save_as_mainfile_invoke(bContext *C, wmOperator *op, wmEvent *event)
1508 {
1509         char name[FILE_MAX];
1510
1511         save_set_compress(op);
1512         
1513         BLI_strncpy(name, G.sce, FILE_MAX);
1514         untitled(name);
1515         RNA_string_set(op->ptr, "path", name);
1516         
1517         WM_event_add_fileselect(C, op);
1518
1519         return OPERATOR_RUNNING_MODAL;
1520 }
1521
1522 /* function used for WM_OT_save_mainfile too */
1523 static int wm_save_as_mainfile_exec(bContext *C, wmOperator *op)
1524 {
1525         char path[FILE_MAX];
1526         int fileflags;
1527
1528         save_set_compress(op);
1529         
1530         if(RNA_property_is_set(op->ptr, "path"))
1531                 RNA_string_get(op->ptr, "path", path);
1532         else {
1533                 BLI_strncpy(path, G.sce, FILE_MAX);
1534                 untitled(path);
1535         }
1536
1537         fileflags= G.fileflags;
1538
1539         /* set compression flag */
1540         if(RNA_boolean_get(op->ptr, "compress"))                fileflags |=  G_FILE_COMPRESS;
1541         else                                                                                    fileflags &= ~G_FILE_COMPRESS;
1542         if(RNA_boolean_get(op->ptr, "relative_remap"))  fileflags |=  G_FILE_RELATIVE_REMAP;
1543         else                                                                                    fileflags &= ~G_FILE_RELATIVE_REMAP;
1544
1545         WM_write_file(C, path, fileflags, op->reports);
1546         
1547         WM_event_add_notifier(C, NC_WM|ND_FILESAVE, NULL);
1548
1549         return 0;
1550 }
1551
1552 static void WM_OT_save_as_mainfile(wmOperatorType *ot)
1553 {
1554         ot->name= "Save As Blender File";
1555         ot->idname= "WM_OT_save_as_mainfile";
1556         ot->description="Save the current file in the desired location.";
1557         
1558         ot->invoke= wm_save_as_mainfile_invoke;
1559         ot->exec= wm_save_as_mainfile_exec;
1560         ot->poll= WM_operator_winactive;
1561         
1562         WM_operator_properties_filesel(ot, FOLDERFILE|BLENDERFILE, FILE_BLENDER);
1563         RNA_def_boolean(ot->srna, "compress", 0, "Compress", "Write compressed .blend file.");
1564         RNA_def_boolean(ot->srna, "relative_remap", 0, "Remap Relative", "Remap relative paths when saving in a different directory.");
1565 }
1566
1567 /* *************** save file directly ******** */
1568
1569 static int wm_save_mainfile_invoke(bContext *C, wmOperator *op, wmEvent *event)
1570 {
1571         char name[FILE_MAX];
1572
1573         /* cancel if no active window */
1574         if (CTX_wm_window(C) == NULL)
1575                 return OPERATOR_CANCELLED;
1576
1577         save_set_compress(op);
1578         
1579         BLI_strncpy(name, G.sce, FILE_MAX);
1580         untitled(name);
1581         RNA_string_set(op->ptr, "path", name);
1582         
1583         if (G.save_over)
1584                 uiPupMenuSaveOver(C, op, name);
1585         else
1586                 WM_event_add_fileselect(C, op);
1587         
1588         return OPERATOR_RUNNING_MODAL;
1589 }
1590
1591 static void WM_OT_save_mainfile(wmOperatorType *ot)
1592 {
1593         ot->name= "Save Blender File";
1594         ot->idname= "WM_OT_save_mainfile";
1595         ot->description="Save the current Blender file.";
1596         
1597         ot->invoke= wm_save_mainfile_invoke;
1598         ot->exec= wm_save_as_mainfile_exec;
1599         ot->poll= NULL;
1600         
1601         WM_operator_properties_filesel(ot, FOLDERFILE|BLENDERFILE, FILE_BLENDER);
1602         RNA_def_boolean(ot->srna, "compress", 0, "Compress", "Write compressed .blend file.");
1603         RNA_def_boolean(ot->srna, "relative_remap", 0, "Remap Relative", "Remap relative paths when saving in a different directory.");
1604 }
1605
1606
1607 /* XXX: move these collada operators to a more appropriate place */
1608 #ifdef WITH_COLLADA
1609
1610 #include "../../collada/collada.h"
1611
1612 static int wm_collada_export_invoke(bContext *C, wmOperator *op, wmEvent *event)
1613 {
1614         //char name[FILE_MAX];
1615         //BLI_strncpy(name, G.sce, FILE_MAX);
1616         //untitled(name);
1617
1618         /* RNA_string_set(op->ptr, "path", "/tmp/test.dae"); */
1619         
1620         WM_event_add_fileselect(C, op);
1621
1622         return OPERATOR_RUNNING_MODAL;
1623 }
1624
1625 /* function used for WM_OT_save_mainfile too */
1626 static int wm_collada_export_exec(bContext *C, wmOperator *op)
1627 {
1628         char filename[FILE_MAX];
1629         
1630         if(RNA_property_is_set(op->ptr, "path"))
1631                 RNA_string_get(op->ptr, "path", filename);
1632         else {
1633                 BLI_strncpy(filename, G.sce, FILE_MAX);
1634                 untitled(filename);
1635         }
1636         
1637         //WM_write_file(C, filename, op->reports);
1638         collada_export(CTX_data_scene(C), filename);
1639         
1640         /* WM_event_add_notifier(C, NC_WM|ND_FILESAVE, NULL); */
1641
1642         return OPERATOR_FINISHED;
1643 }
1644
1645 static void WM_OT_collada_export(wmOperatorType *ot)
1646 {
1647         ot->name= "Export COLLADA";
1648         ot->idname= "WM_OT_collada_export";
1649         
1650         ot->invoke= wm_collada_export_invoke;
1651         ot->exec= wm_collada_export_exec;
1652         ot->poll= WM_operator_winactive;
1653         
1654         ot->flag= 0;
1655         
1656         RNA_def_property(ot->srna, "path", PROP_STRING, PROP_FILEPATH);
1657 }
1658
1659 static int wm_collada_import_invoke(bContext *C, wmOperator *op, wmEvent *event)
1660 {
1661         /* RNA_string_set(op->ptr, "path", "/tmp/test.dae"); */
1662         
1663         WM_event_add_fileselect(C, op);
1664
1665         return OPERATOR_RUNNING_MODAL;
1666 }
1667
1668 /* function used for WM_OT_save_mainfile too */
1669 static int wm_collada_import_exec(bContext *C, wmOperator *op)
1670 {
1671         char filename[FILE_MAX];
1672         
1673         if(RNA_property_is_set(op->ptr, "path"))
1674                 RNA_string_get(op->ptr, "path", filename);
1675         else {
1676                 BLI_strncpy(filename, G.sce, FILE_MAX);
1677                 untitled(filename);
1678         }
1679         
1680         //WM_write_file(C, filename, op->reports);
1681         collada_import(C, filename);
1682         
1683         /* WM_event_add_notifier(C, NC_WM|ND_FILESAVE, NULL); */
1684
1685         return OPERATOR_FINISHED;
1686 }
1687
1688 static void WM_OT_collada_import(wmOperatorType *ot)
1689 {
1690         ot->name= "Import COLLADA";
1691         ot->idname= "WM_OT_collada_import";
1692         
1693         ot->invoke= wm_collada_import_invoke;
1694         ot->exec= wm_collada_import_exec;
1695         ot->poll= WM_operator_winactive;
1696         
1697         ot->flag= 0;
1698         
1699         RNA_def_property(ot->srna, "path", PROP_STRING, PROP_FILEPATH);
1700 }
1701
1702 #endif
1703
1704
1705
1706 /* *********************** */
1707
1708 static void WM_OT_window_fullscreen_toggle(wmOperatorType *ot)
1709 {
1710         ot->name= "Toggle Fullscreen";
1711         ot->idname= "WM_OT_window_fullscreen_toggle";
1712         ot->description="Toggle the current window fullscreen.";
1713
1714         ot->exec= wm_window_fullscreen_toggle_op;
1715         ot->poll= WM_operator_winactive;
1716 }
1717
1718 static int wm_exit_blender_op(bContext *C, wmOperator *op)
1719 {
1720         WM_operator_free(op);
1721         
1722         WM_exit(C);     
1723         
1724         return OPERATOR_FINISHED;
1725 }
1726
1727 static void WM_OT_exit_blender(wmOperatorType *ot)
1728 {
1729         ot->name= "Exit Blender";
1730         ot->idname= "WM_OT_exit_blender";
1731         ot->description= "Quit Blender.";
1732
1733         ot->invoke= WM_operator_confirm;
1734         ot->exec= wm_exit_blender_op;
1735         ot->poll= WM_operator_winactive;
1736 }
1737
1738 /* ************ default paint cursors, draw always around cursor *********** */
1739 /*
1740  - returns handler to free 
1741  - poll(bContext): returns 1 if draw should happen
1742  - draw(bContext): drawing callback for paint cursor
1743 */
1744
1745 void *WM_paint_cursor_activate(wmWindowManager *wm, int (*poll)(bContext *C),
1746                                wmPaintCursorDraw draw, void *customdata)
1747 {
1748         wmPaintCursor *pc= MEM_callocN(sizeof(wmPaintCursor), "paint cursor");
1749         
1750         BLI_addtail(&wm->paintcursors, pc);
1751         
1752         pc->customdata = customdata;
1753         pc->poll= poll;
1754         pc->draw= draw;
1755         
1756         return pc;
1757 }
1758
1759 void WM_paint_cursor_end(wmWindowManager *wm, void *handle)
1760 {
1761         wmPaintCursor *pc;
1762         
1763         for(pc= wm->paintcursors.first; pc; pc= pc->next) {
1764                 if(pc == (wmPaintCursor *)handle) {
1765                         BLI_remlink(&wm->paintcursors, pc);
1766                         MEM_freeN(pc);
1767                         return;
1768                 }
1769         }
1770 }
1771
1772 /* ************ window gesture operator-callback definitions ************** */
1773 /*
1774  * These are default callbacks for use in operators requiring gesture input
1775  */
1776
1777 /* **************** Border gesture *************** */
1778
1779 /* Border gesture has two types:
1780    1) WM_GESTURE_CROSS_RECT: starts a cross, on mouse click it changes to border 
1781    2) WM_GESTURE_RECT: starts immediate as a border, on mouse click or release it ends
1782
1783    It stores 4 values (xmin, xmax, ymin, ymax) and event it ended with (event_type)
1784 */
1785
1786 static int border_apply(bContext *C, wmOperator *op, int gesture_mode)
1787 {
1788         wmGesture *gesture= op->customdata;
1789         rcti *rect= gesture->customdata;
1790         
1791         if(rect->xmin > rect->xmax)
1792                 SWAP(int, rect->xmin, rect->xmax);
1793         if(rect->ymin > rect->ymax)
1794                 SWAP(int, rect->ymin, rect->ymax);
1795         
1796         if(rect->xmin==rect->xmax || rect->ymin==rect->ymax)
1797                 return 0;
1798                 
1799         /* operator arguments and storage. */
1800         RNA_int_set(op->ptr, "xmin", rect->xmin);
1801         RNA_int_set(op->ptr, "ymin", rect->ymin);
1802         RNA_int_set(op->ptr, "xmax", rect->xmax);
1803         RNA_int_set(op->ptr, "ymax", rect->ymax);
1804         
1805         /* XXX weak; border should be configured for this without reading event types */
1806         if( RNA_struct_find_property(op->ptr, "gesture_mode") )
1807                 RNA_int_set(op->ptr, "gesture_mode", gesture_mode);
1808
1809         op->type->exec(C, op);
1810         
1811         return 1;
1812 }
1813
1814 static void wm_gesture_end(bContext *C, wmOperator *op)
1815 {
1816         wmGesture *gesture= op->customdata;
1817         
1818         WM_gesture_end(C, gesture);     /* frees gesture itself, and unregisters from window */
1819         op->customdata= NULL;
1820
1821         ED_area_tag_redraw(CTX_wm_area(C));
1822         
1823         if( RNA_struct_find_property(op->ptr, "cursor") )
1824                 WM_cursor_restore(CTX_wm_window(C));
1825 }
1826
1827 int WM_border_select_invoke(bContext *C, wmOperator *op, wmEvent *event)
1828 {
1829         if(ISTWEAK(event->type))
1830                 op->customdata= WM_gesture_new(C, event, WM_GESTURE_RECT);
1831         else
1832                 op->customdata= WM_gesture_new(C, event, WM_GESTURE_CROSS_RECT);
1833
1834         /* add modal handler */
1835         WM_event_add_modal_handler(C, op);
1836         
1837         wm_gesture_tag_redraw(C);
1838
1839         return OPERATOR_RUNNING_MODAL;
1840 }
1841
1842 int WM_border_select_modal(bContext *C, wmOperator *op, wmEvent *event)
1843 {
1844         wmGesture *gesture= op->customdata;
1845         rcti *rect= gesture->customdata;
1846         int sx, sy;
1847         
1848         if(event->type== MOUSEMOVE) {
1849                 wm_subwindow_getorigin(CTX_wm_window(C), gesture->swinid, &sx, &sy);
1850
1851                 if(gesture->type==WM_GESTURE_CROSS_RECT && gesture->mode==0) {
1852                         rect->xmin= rect->xmax= event->x - sx;
1853                         rect->ymin= rect->ymax= event->y - sy;
1854                 }
1855                 else {
1856                         rect->xmax= event->x - sx;
1857                         rect->ymax= event->y - sy;
1858                 }
1859
1860                 wm_gesture_tag_redraw(C);
1861         }
1862         else if (event->type==EVT_MODAL_MAP) {
1863                 switch (event->val) {
1864                 case GESTURE_MODAL_BORDER_BEGIN:
1865                         if(gesture->type==WM_GESTURE_CROSS_RECT && gesture->mode==0) {
1866                                 gesture->mode= 1;
1867                                 wm_gesture_tag_redraw(C);
1868                         }
1869                         break;
1870                 case GESTURE_MODAL_SELECT:
1871                 case GESTURE_MODAL_DESELECT:
1872                         if(border_apply(C, op, event->val)) {
1873                                 wm_gesture_end(C, op);
1874                                 return OPERATOR_FINISHED;
1875                         }
1876                         wm_gesture_end(C, op);
1877                         return OPERATOR_CANCELLED;
1878                         break;
1879
1880                 case GESTURE_MODAL_CANCEL:
1881                         wm_gesture_end(C, op);
1882                         return OPERATOR_CANCELLED;
1883                 }
1884
1885         }
1886 //      // Allow view navigation???
1887 //      else {
1888 //              return OPERATOR_PASS_THROUGH;
1889 //      }
1890
1891         return OPERATOR_RUNNING_MODAL;
1892 }
1893
1894 /* **************** circle gesture *************** */
1895 /* works now only for selection or modal paint stuff, calls exec while hold mouse, exit on release */
1896
1897 #ifdef GESTURE_MEMORY
1898 int circle_select_size= 25; // XXX - need some operator memory thing\!
1899 #endif
1900
1901 int WM_gesture_circle_invoke(bContext *C, wmOperator *op, wmEvent *event)
1902 {
1903         op->customdata= WM_gesture_new(C, event, WM_GESTURE_CIRCLE);
1904         
1905         /* add modal handler */
1906         WM_event_add_modal_handler(C, op);
1907         
1908         wm_gesture_tag_redraw(C);
1909         
1910         return OPERATOR_RUNNING_MODAL;
1911 }
1912
1913 static void gesture_circle_apply(bContext *C, wmOperator *op)
1914 {
1915         wmGesture *gesture= op->customdata;
1916         rcti *rect= gesture->customdata;
1917         
1918     if(RNA_int_get(op->ptr, "gesture_mode")==GESTURE_MODAL_NOP)
1919         return;
1920
1921         /* operator arguments and storage. */
1922         RNA_int_set(op->ptr, "x", rect->xmin);
1923         RNA_int_set(op->ptr, "y", rect->ymin);
1924         RNA_int_set(op->ptr, "radius", rect->xmax);
1925         
1926         if(op->type->exec)
1927                 op->type->exec(C, op);
1928
1929 #ifdef GESTURE_MEMORY
1930         circle_select_size= rect->xmax;
1931 #endif
1932 }
1933
1934 int WM_gesture_circle_modal(bContext *C, wmOperator *op, wmEvent *event)
1935 {
1936         wmGesture *gesture= op->customdata;
1937         rcti *rect= gesture->customdata;
1938         int sx, sy;
1939
1940         if(event->type== MOUSEMOVE) {
1941                 wm_subwindow_getorigin(CTX_wm_window(C), gesture->swinid, &sx, &sy);
1942
1943                 rect->xmin= event->x - sx;
1944                 rect->ymin= event->y - sy;
1945
1946                 wm_gesture_tag_redraw(C);
1947
1948                 if(gesture->mode)
1949                         gesture_circle_apply(C, op);
1950         }
1951         else if (event->type==EVT_MODAL_MAP) {
1952                 switch (event->val) {
1953                 case GESTURE_MODAL_CIRCLE_ADD:
1954                         rect->xmax += 2 + rect->xmax/10;
1955                         wm_gesture_tag_redraw(C);
1956                         break;
1957                 case GESTURE_MODAL_CIRCLE_SUB:
1958                         rect->xmax -= 2 + rect->xmax/10;
1959                         if(rect->xmax < 1) rect->xmax= 1;
1960                         wm_gesture_tag_redraw(C);
1961                         break;
1962                 case GESTURE_MODAL_SELECT:
1963                 case GESTURE_MODAL_DESELECT:
1964                 case GESTURE_MODAL_NOP:
1965                         if(RNA_struct_find_property(op->ptr, "gesture_mode"))
1966                                 RNA_int_set(op->ptr, "gesture_mode", event->val);
1967
1968                         if(event->val != GESTURE_MODAL_NOP) {
1969                                 /* apply first click */
1970                                 gesture_circle_apply(C, op);
1971                                 gesture->mode= 1;
1972                         }
1973                         break;
1974
1975                 case GESTURE_MODAL_CANCEL:
1976                 case GESTURE_MODAL_CONFIRM:
1977                         wm_gesture_end(C, op);
1978                         return OPERATOR_FINISHED; /* use finish or we dont get an undo */
1979                 }
1980         }
1981 //      // Allow view navigation???
1982 //      else {
1983 //              return OPERATOR_PASS_THROUGH;
1984 //      }
1985
1986         return OPERATOR_RUNNING_MODAL;
1987 }
1988
1989 #if 0
1990 /* template to copy from */
1991 void WM_OT_circle_gesture(wmOperatorType *ot)
1992 {
1993         ot->name= "Circle Gesture";
1994         ot->idname= "WM_OT_circle_gesture";
1995         ot->description="Enter rotate mode with a circular gesture.";
1996         
1997         ot->invoke= WM_gesture_circle_invoke;
1998         ot->modal= WM_gesture_circle_modal;
1999         
2000         ot->poll= WM_operator_winactive;
2001         
2002         RNA_def_property(ot->srna, "x", PROP_INT, PROP_NONE);
2003         RNA_def_property(ot->srna, "y", PROP_INT, PROP_NONE);
2004         RNA_def_property(ot->srna, "radius", PROP_INT, PROP_NONE);
2005
2006 }
2007 #endif
2008
2009 /* **************** Tweak gesture *************** */
2010
2011 static void tweak_gesture_modal(bContext *C, wmEvent *event)
2012 {
2013         wmWindow *window= CTX_wm_window(C);
2014         wmGesture *gesture= window->tweak;
2015         rcti *rect= gesture->customdata;
2016         int sx, sy, val;
2017         
2018         switch(event->type) {
2019                 case MOUSEMOVE:
2020                         
2021                         wm_subwindow_getorigin(window, gesture->swinid, &sx, &sy);
2022                         
2023                         rect->xmax= event->x - sx;
2024                         rect->ymax= event->y - sy;
2025                         
2026                         if((val= wm_gesture_evaluate(C, gesture))) {
2027                                 wmEvent event;
2028
2029                                 event= *(window->eventstate);
2030                                 if(gesture->event_type==LEFTMOUSE)
2031                                         event.type= EVT_TWEAK_L;
2032                                 else if(gesture->event_type==RIGHTMOUSE)
2033                                         event.type= EVT_TWEAK_R;
2034                                 else
2035                                         event.type= EVT_TWEAK_M;
2036                                 event.val= val;
2037                                 /* mouse coords! */
2038                                 wm_event_add(window, &event);
2039                                 
2040                                 WM_gesture_end(C, gesture);     /* frees gesture itself, and unregisters from window */
2041                                 window->tweak= NULL;
2042                         }
2043                         
2044                         break;
2045                         
2046                 case LEFTMOUSE:
2047                 case RIGHTMOUSE:
2048                 case MIDDLEMOUSE:
2049                         if(gesture->event_type==event->type) {
2050                                 WM_gesture_end(C, gesture);
2051                                 window->tweak= NULL;
2052
2053                                 /* when tweak fails we should give the other keymap entries a chance */
2054                                 event->val= KM_RELEASE;
2055                         }
2056                         break;
2057                 default:
2058                         if(!ISTIMER(event->type)) {
2059                                 WM_gesture_end(C, gesture);
2060                                 window->tweak= NULL;
2061                         }
2062                         break;
2063         }
2064 }
2065
2066 /* standard tweak, called after window handlers passed on event */
2067 void wm_tweakevent_test(bContext *C, wmEvent *event, int action)
2068 {
2069         wmWindow *win= CTX_wm_window(C);
2070         
2071         if(win->tweak==NULL) {
2072                 if(CTX_wm_region(C)) {
2073                         if(event->val==KM_PRESS) { // pressed
2074                                 if( ELEM3(event->type, LEFTMOUSE, MIDDLEMOUSE, RIGHTMOUSE) )
2075                                         win->tweak= WM_gesture_new(C, event, WM_GESTURE_TWEAK);
2076                         }
2077                 }
2078         }
2079         else {
2080                 if(action & WM_HANDLER_BREAK) {
2081                         WM_gesture_end(C, win->tweak);
2082                         win->tweak= NULL;
2083                 }
2084                 else
2085                         tweak_gesture_modal(C, event);
2086         }
2087 }
2088
2089 /* *********************** lasso gesture ****************** */
2090
2091 int WM_gesture_lasso_invoke(bContext *C, wmOperator *op, wmEvent *event)
2092 {
2093         op->customdata= WM_gesture_new(C, event, WM_GESTURE_LASSO);
2094         
2095         /* add modal handler */
2096         WM_event_add_modal_handler(C, op);
2097         
2098         wm_gesture_tag_redraw(C);
2099         
2100         if( RNA_struct_find_property(op->ptr, "cursor") )
2101                 WM_cursor_modal(CTX_wm_window(C), RNA_int_get(op->ptr, "cursor"));
2102         
2103         return OPERATOR_RUNNING_MODAL;
2104 }
2105
2106 int WM_gesture_lines_invoke(bContext *C, wmOperator *op, wmEvent *event)
2107 {
2108         op->customdata= WM_gesture_new(C, event, WM_GESTURE_LINES);
2109         
2110         /* add modal handler */
2111         WM_event_add_modal_handler(C, op);
2112         
2113         wm_gesture_tag_redraw(C);
2114         
2115         if( RNA_struct_find_property(op->ptr, "cursor") )
2116                 WM_cursor_modal(CTX_wm_window(C), RNA_int_get(op->ptr, "cursor"));
2117         
2118         return OPERATOR_RUNNING_MODAL;
2119 }
2120
2121
2122 static void gesture_lasso_apply(bContext *C, wmOperator *op, int event_type)
2123 {
2124         wmGesture *gesture= op->customdata;
2125         PointerRNA itemptr;
2126         float loc[2];
2127         int i;
2128         short *lasso= gesture->customdata;
2129         
2130         /* operator storage as path. */
2131
2132         for(i=0; i<gesture->points; i++, lasso+=2) {
2133                 loc[0]= lasso[0];
2134                 loc[1]= lasso[1];
2135                 RNA_collection_add(op->ptr, "path", &itemptr);
2136                 RNA_float_set_array(&itemptr, "loc", loc);
2137         }
2138         
2139         wm_gesture_end(C, op);
2140                 
2141         if(op->type->exec)
2142                 op->type->exec(C, op);
2143         
2144 }
2145
2146 int WM_gesture_lasso_modal(bContext *C, wmOperator *op, wmEvent *event)
2147 {
2148         wmGesture *gesture= op->customdata;
2149         int sx, sy;
2150         
2151         switch(event->type) {
2152                 case MOUSEMOVE:
2153                         
2154                         wm_gesture_tag_redraw(C);
2155                         
2156                         wm_subwindow_getorigin(CTX_wm_window(C), gesture->swinid, &sx, &sy);
2157
2158                         if(gesture->points == gesture->size) {
2159                                 short *old_lasso = gesture->customdata;
2160                                 gesture->customdata= MEM_callocN(2*sizeof(short)*(gesture->size + WM_LASSO_MIN_POINTS), "lasso points");
2161                                 memcpy(gesture->customdata, old_lasso, 2*sizeof(short)*gesture->size);
2162                                 gesture->size = gesture->size + WM_LASSO_MIN_POINTS;
2163                                 MEM_freeN(old_lasso);
2164                                 printf("realloc\n");
2165                         }
2166
2167                         {
2168                                 short *lasso= gesture->customdata;
2169                                 lasso += 2 * gesture->points;
2170                                 lasso[0] = event->x - sx;
2171                                 lasso[1] = event->y - sy;
2172                                 gesture->points++;
2173                         }
2174                         break;
2175                         
2176                 case LEFTMOUSE:
2177                 case MIDDLEMOUSE:
2178                 case RIGHTMOUSE:
2179                         if(event->val==KM_RELEASE) {    /* key release */
2180                                 gesture_lasso_apply(C, op, event->type);
2181                                 return OPERATOR_FINISHED;
2182                         }
2183                         break;
2184                 case ESCKEY:
2185                         wm_gesture_end(C, op);
2186                         return OPERATOR_CANCELLED;
2187         }
2188         return OPERATOR_RUNNING_MODAL;
2189 }
2190
2191 int WM_gesture_lines_modal(bContext *C, wmOperator *op, wmEvent *event)
2192 {
2193         return WM_gesture_lasso_modal(C, op, event);
2194 }
2195
2196 #if 0
2197 /* template to copy from */
2198
2199 static int gesture_lasso_exec(bContext *C, wmOperator *op)
2200 {
2201         RNA_BEGIN(op->ptr, itemptr, "path") {
2202                 float loc[2];
2203                 
2204                 RNA_float_get_array(&itemptr, "loc", loc);
2205                 printf("Location: %f %f\n", loc[0], loc[1]);
2206         }
2207         RNA_END;
2208         
2209         return OPERATOR_FINISHED;
2210 }
2211
2212 void WM_OT_lasso_gesture(wmOperatorType *ot)
2213 {
2214         PropertyRNA *prop;
2215         
2216         ot->name= "Lasso Gesture";
2217         ot->idname= "WM_OT_lasso_gesture";
2218         ot->description="Select objects within the lasso as you move the pointer.";
2219         
2220         ot->invoke= WM_gesture_lasso_invoke;
2221         ot->modal= WM_gesture_lasso_modal;
2222         ot->exec= gesture_lasso_exec;
2223         
2224         ot->poll= WM_operator_winactive;
2225         
2226         prop= RNA_def_property(ot->srna, "path", PROP_COLLECTION, PROP_NONE);
2227         RNA_def_property_struct_runtime(prop, &RNA_OperatorMousePath);
2228 }
2229 #endif
2230
2231 /* *********************** radial control ****************** */
2232
2233 const int WM_RADIAL_CONTROL_DISPLAY_SIZE = 200;
2234
2235 typedef struct wmRadialControl {
2236         int mode;
2237         float initial_value, value, max_value;
2238         int initial_mouse[2];
2239         void *cursor;
2240         GLuint tex;
2241 } wmRadialControl;
2242
2243 static void wm_radial_control_paint(bContext *C, int x, int y, void *customdata)
2244 {
2245         wmRadialControl *rc = (wmRadialControl*)customdata;
2246         ARegion *ar = CTX_wm_region(C);
2247         float r1=0.0f, r2=0.0f, r3=0.0f, angle=0.0f;
2248
2249         /* Keep cursor in the original place */
2250         x = rc->initial_mouse[0] - ar->winrct.xmin;
2251         y = rc->initial_mouse[1] - ar->winrct.ymin;
2252
2253         glPushMatrix();
2254         
2255         glTranslatef((float)x, (float)y, 0.0f);
2256
2257         if(rc->mode == WM_RADIALCONTROL_SIZE) {
2258                 r1= rc->value;
2259                 r2= rc->initial_value;
2260                 r3= r1;
2261         } else if(rc->mode == WM_RADIALCONTROL_STRENGTH) {
2262                 r1= (1 - rc->value) * WM_RADIAL_CONTROL_DISPLAY_SIZE;
2263                 r2= WM_RADIAL_CONTROL_DISPLAY_SIZE;
2264                 r3= WM_RADIAL_CONTROL_DISPLAY_SIZE;
2265         } else if(rc->mode == WM_RADIALCONTROL_ANGLE) {
2266                 r1= r2= WM_RADIAL_CONTROL_DISPLAY_SIZE;
2267                 r3= WM_RADIAL_CONTROL_DISPLAY_SIZE;
2268                 angle = rc->value;
2269         }
2270
2271         glColor4ub(255, 255, 255, 128);
2272         glEnable( GL_LINE_SMOOTH );
2273         glEnable(GL_BLEND);
2274
2275         if(rc->mode == WM_RADIALCONTROL_ANGLE)
2276                 fdrawline(0, 0, WM_RADIAL_CONTROL_DISPLAY_SIZE, 0);
2277
2278         if(rc->tex) {
2279                 const float str = rc->mode == WM_RADIALCONTROL_STRENGTH ? (rc->value + 0.5) : 1;
2280
2281                 if(rc->mode == WM_RADIALCONTROL_ANGLE) {
2282                         glRotatef(angle, 0, 0, 1);
2283                         fdrawline(0, 0, WM_RADIAL_CONTROL_DISPLAY_SIZE, 0);
2284                 }
2285
2286                 glBindTexture(GL_TEXTURE_2D, rc->tex);
2287
2288                 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
2289                 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
2290
2291                 glEnable(GL_TEXTURE_2D);
2292                 glBegin(GL_QUADS);
2293                 glColor4f(0,0,0, str);
2294                 glTexCoord2f(0,0);
2295                 glVertex2f(-r3, -r3);
2296                 glTexCoord2f(1,0);
2297                 glVertex2f(r3, -r3);
2298                 glTexCoord2f(1,1);
2299                 glVertex2f(r3, r3);
2300                 glTexCoord2f(0,1);
2301                 glVertex2f(-r3, r3);
2302                 glEnd();
2303                 glDisable(GL_TEXTURE_2D);
2304         }
2305
2306         glColor4ub(255, 255, 255, 128); 
2307         glutil_draw_lined_arc(0.0, M_PI*2.0, r1, 40);
2308         glutil_draw_lined_arc(0.0, M_PI*2.0, r2, 40);
2309         glDisable(GL_BLEND);
2310         glDisable( GL_LINE_SMOOTH );
2311         
2312         glPopMatrix();
2313 }
2314
2315 int WM_radial_control_modal(bContext *C, wmOperator *op, wmEvent *event)
2316 {
2317         wmRadialControl *rc = (wmRadialControl*)op->customdata;
2318         int mode, initial_mouse[2], delta[2];
2319         float dist;
2320         double new_value = RNA_float_get(op->ptr, "new_value");
2321         int ret = OPERATOR_RUNNING_MODAL;
2322
2323         mode = RNA_int_get(op->ptr, "mode");
2324         RNA_int_get_array(op->ptr, "initial_mouse", initial_mouse);
2325
2326         switch(event->type) {
2327         case MOUSEMOVE:
2328                 delta[0]= initial_mouse[0] - event->x;
2329                 delta[1]= initial_mouse[1] - event->y;
2330                 dist= sqrt(delta[0]*delta[0]+delta[1]*delta[1]);
2331
2332                 if(mode == WM_RADIALCONTROL_SIZE)
2333                         new_value = dist;
2334                 else if(mode == WM_RADIALCONTROL_STRENGTH) {
2335                         new_value = 1 - dist / WM_RADIAL_CONTROL_DISPLAY_SIZE;
2336                 } else if(mode == WM_RADIALCONTROL_ANGLE)
2337                         new_value = ((int)(atan2(delta[1], delta[0]) * (180.0 / M_PI)) + 180);
2338                 
2339                 if(event->ctrl) {
2340                         if(mode == WM_RADIALCONTROL_STRENGTH)
2341                                 new_value = ((int)ceil(new_value * 10.f) * 10.0f) / 100.f;
2342                         else
2343                                 new_value = ((int)new_value + 5) / 10*10;
2344                 }
2345                 
2346                 break;
2347         case ESCKEY:
2348         case RIGHTMOUSE:
2349                 ret = OPERATOR_CANCELLED;
2350                 break;
2351         case LEFTMOUSE:
2352         case PADENTER:
2353                 op->type->exec(C, op);
2354                 ret = OPERATOR_FINISHED;
2355                 break;
2356         }
2357
2358         /* Clamp */
2359         if(new_value > rc->max_value)
2360                 new_value = rc->max_value;
2361         else if(new_value < 0)
2362                 new_value = 0;
2363
2364         /* Update paint data */
2365         rc->value = new_value;
2366
2367         RNA_float_set(op->ptr, "new_value", new_value);
2368
2369         if(ret != OPERATOR_RUNNING_MODAL) {
2370                 WM_paint_cursor_end(CTX_wm_manager(C), rc->cursor);
2371                 MEM_freeN(rc);
2372         }
2373         
2374         ED_region_tag_redraw(CTX_wm_region(C));
2375
2376         return ret;
2377 }
2378
2379 /* Expects the operator customdata to be an ImBuf (or NULL) */
2380 int WM_radial_control_invoke(bContext *C, wmOperator *op, wmEvent *event)
2381 {
2382         wmRadialControl *rc = MEM_callocN(sizeof(wmRadialControl), "radial control");
2383         int mode = RNA_int_get(op->ptr, "mode");
2384         float initial_value = RNA_float_get(op->ptr, "initial_value");
2385         int mouse[2] = {event->x, event->y};
2386
2387         if(mode == WM_RADIALCONTROL_SIZE) {
2388                 rc->max_value = 200;
2389                 mouse[0]-= initial_value;
2390         }
2391         else if(mode == WM_RADIALCONTROL_STRENGTH) {
2392                 rc->max_value = 1;
2393                 mouse[0]-= WM_RADIAL_CONTROL_DISPLAY_SIZE * (1 - initial_value);
2394         }
2395         else if(mode == WM_RADIALCONTROL_ANGLE) {
2396                 rc->max_value = 360;
2397                 mouse[0]-= WM_RADIAL_CONTROL_DISPLAY_SIZE * cos(initial_value);
2398                 mouse[1]-= WM_RADIAL_CONTROL_DISPLAY_SIZE * sin(initial_value);
2399                 initial_value *= 180.0f/M_PI;
2400         }
2401
2402         if(op->customdata) {
2403                 ImBuf *im = (ImBuf*)op->customdata;
2404                 /* Build GL texture */
2405                 glGenTextures(1, &rc->tex);
2406                 glBindTexture(GL_TEXTURE_2D, rc->tex);
2407                 glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, im->x, im->y, 0, GL_ALPHA, GL_FLOAT, im->rect_float);
2408                 MEM_freeN(im->rect_float);
2409                 MEM_freeN(im);
2410         }
2411
2412         RNA_int_set_array(op->ptr, "initial_mouse", mouse);
2413         RNA_float_set(op->ptr, "new_value", initial_value);
2414                 
2415         op->customdata = rc;
2416         rc->mode = mode;
2417         rc->initial_value = initial_value;
2418         rc->initial_mouse[0] = mouse[0];
2419         rc->initial_mouse[1] = mouse[1];
2420         rc->cursor = WM_paint_cursor_activate(CTX_wm_manager(C), op->type->poll,
2421                                               wm_radial_control_paint, op->customdata);
2422
2423         /* add modal handler */
2424         WM_event_add_modal_handler(C, op);
2425         
2426         WM_radial_control_modal(C, op, event);
2427         
2428         return OPERATOR_RUNNING_MODAL;
2429 }
2430
2431 /* Gets a descriptive string of the operation */
2432 void WM_radial_control_string(wmOperator *op, char str[], int maxlen)
2433 {
2434         int mode = RNA_int_get(op->ptr, "mode");
2435         float v = RNA_float_get(op->ptr, "new_value");
2436
2437         if(mode == WM_RADIALCONTROL_SIZE)
2438                 sprintf(str, "Size: %d", (int)v);
2439         else if(mode == WM_RADIALCONTROL_STRENGTH)
2440                 sprintf(str, "Strength: %d", (int)v);
2441         else if(mode == WM_RADIALCONTROL_ANGLE)
2442                 sprintf(str, "Angle: %d", (int)(v * 180.0f/M_PI));
2443 }
2444
2445 /** Important: this doesn't define an actual operator, it
2446     just sets up the common parts of the radial control op. **/
2447 void WM_OT_radial_control_partial(wmOperatorType *ot)
2448 {
2449         static EnumPropertyItem radial_mode_items[] = {
2450                 {WM_RADIALCONTROL_SIZE, "SIZE", 0, "Size", ""},
2451                 {WM_RADIALCONTROL_STRENGTH, "STRENGTH", 0, "Strength", ""},
2452                 {WM_RADIALCONTROL_ANGLE, "ANGLE", 0, "Angle", ""},
2453                 {0, NULL, 0, NULL, NULL}};
2454
2455         /* Should be set in custom invoke() */
2456         RNA_def_float(ot->srna, "initial_value", 0, 0, FLT_MAX, "Initial Value", "", 0, FLT_MAX);
2457
2458         /* Set internally, should be used in custom exec() to get final value */
2459         RNA_def_float(ot->srna, "new_value", 0, 0, FLT_MAX, "New Value", "", 0, FLT_MAX);
2460
2461         /* Should be set before calling operator */
2462         RNA_def_enum(ot->srna, "mode", radial_mode_items, 0, "Mode", "");
2463
2464         /* Internal */
2465         RNA_def_int_vector(ot->srna, "initial_mouse", 2, NULL, INT_MIN, INT_MAX, "initial_mouse", "", INT_MIN, INT_MAX);
2466 }
2467
2468 /* ************************** timer for testing ***************** */
2469
2470 /* uses no type defines, fully local testing function anyway... ;) */
2471
2472 static int redraw_timer_exec(bContext *C, wmOperator *op)
2473 {
2474         ARegion *ar= CTX_wm_region(C);
2475         double stime= PIL_check_seconds_timer();
2476         int type = RNA_int_get(op->ptr, "type");
2477         int iter = RNA_int_get(op->ptr, "iterations");
2478         int a;
2479         float time;
2480         char *infostr= "";
2481         
2482         WM_cursor_wait(1);
2483
2484         for(a=0; a<iter; a++) {
2485                 if (type==0) {
2486                         ED_region_do_draw(C, ar);
2487                 } 
2488                 else if (type==1) {
2489                         wmWindow *win= CTX_wm_window(C);
2490                         
2491                         ED_region_tag_redraw(ar);
2492                         wm_draw_update(C);
2493                         
2494                         CTX_wm_window_set(C, win);      /* XXX context manipulation warning! */
2495                 }
2496                 else if (type==2) {
2497                         wmWindow *win= CTX_wm_window(C);
2498                         ScrArea *sa;
2499                         
2500                         ScrArea *sa_back= CTX_wm_area(C);
2501                         ARegion *ar_back= CTX_wm_region(C);
2502
2503                         for(sa= CTX_wm_screen(C)->areabase.first; sa; sa= sa->next) {
2504                                 ARegion *ar_iter;
2505                                 CTX_wm_area_set(C, sa);
2506
2507                                 for(ar_iter= sa->regionbase.first; ar_iter; ar_iter= ar_iter->next) {
2508                                         if(ar_iter->swinid) {
2509                                                 CTX_wm_region_set(C, ar_iter);
2510                                                 ED_region_do_draw(C, ar_iter);
2511                                         }
2512                                 }
2513                         }
2514
2515                         CTX_wm_window_set(C, win);      /* XXX context manipulation warning! */
2516
2517                         CTX_wm_area_set(C, sa_back);
2518                         CTX_wm_region_set(C, ar_back);
2519                 }
2520                 else if (type==3) {
2521                         wmWindow *win= CTX_wm_window(C);
2522                         ScrArea *sa;
2523
2524                         for(sa= CTX_wm_screen(C)->areabase.first; sa; sa= sa->next)
2525                                 ED_area_tag_redraw(sa);
2526                         wm_draw_update(C);
2527                         
2528                         CTX_wm_window_set(C, win);      /* XXX context manipulation warning! */
2529                 }
2530                 else if (type==4) {
2531                         Scene *scene= CTX_data_scene(C);
2532                         
2533                         if(a & 1) scene->r.cfra--;
2534                         else scene->r.cfra++;
2535                         scene_update_for_newframe(scene, scene->lay);
2536                 }
2537                 else {
2538                         ED_undo_pop(C);
2539                         ED_undo_redo(C);
2540                 }
2541         }
2542         
2543         time= ((PIL_check_seconds_timer()-stime)*1000);
2544         
2545         if(type==0) infostr= "Draw Region";
2546         if(type==1) infostr= "Draw Region and Swap";
2547         if(type==2) infostr= "Draw Window";
2548         if(type==3) infostr= "Draw Window and Swap";
2549         if(type==4) infostr= "Animation Steps";
2550         if(type==5) infostr= "Undo/Redo";
2551         
2552         WM_cursor_wait(0);
2553         
2554         BKE_reportf(op->reports, RPT_INFO, "%d x %s: %.2f ms,  average: %.4f", iter, infostr, time, time/iter);
2555         
2556         return OPERATOR_FINISHED;
2557 }
2558
2559 static void WM_OT_redraw_timer(wmOperatorType *ot)
2560 {
2561         static EnumPropertyItem prop_type_items[] = {
2562         {0, "DRAW", 0, "Draw Region", ""},
2563         {1, "DRAW_SWAP", 0, "Draw Region + Swap", ""},
2564         {2, "DRAW_WIN", 0, "Draw Window", ""},
2565         {3, "DRAW_WIN_SWAP", 0, "Draw Window + Swap", ""},
2566         {4, "ANIM_STEP", 0, "Anim Step", ""},
2567         {5, "UNDO", 0, "Undo/Redo", ""},
2568         {0, NULL, 0, NULL, NULL}};
2569         
2570         ot->name= "Redraw Timer";
2571         ot->idname= "WM_OT_redraw_timer";
2572         ot->description="Simple redraw timer to test the speed of updating the interface.";
2573         
2574         ot->invoke= WM_menu_invoke;
2575         ot->exec= redraw_timer_exec;
2576         ot->poll= WM_operator_winactive;
2577         
2578         RNA_def_enum(ot->srna, "type", prop_type_items, 0, "Type", "");
2579         RNA_def_int(ot->srna, "iterations", 10, 1,INT_MAX, "Iterations", "Number of times to redraw", 1,1000);
2580
2581 }
2582
2583 /* ************************** memory statistics for testing ***************** */
2584
2585 static int memory_statistics_exec(bContext *C, wmOperator *op)
2586 {
2587         MEM_printmemlist_stats();
2588         return OPERATOR_FINISHED;
2589 }
2590
2591 static void WM_OT_memory_statistics(wmOperatorType *ot)
2592 {
2593         ot->name= "Memory Statistics";
2594         ot->idname= "WM_OT_memory_statistics";
2595         ot->description= "Print memory statistics to the console.";
2596         
2597         ot->exec= memory_statistics_exec;
2598 }
2599
2600 /* ******************************************************* */
2601  
2602 /* called on initialize WM_exit() */
2603 void wm_operatortype_free(void)
2604 {
2605         wmOperatorType *ot;
2606         
2607         for(ot= global_ops.first; ot; ot= ot->next) {
2608                 if(ot->macro.first)
2609                         wm_operatortype_free_macro(ot);
2610
2611                 if(ot->ext.srna) /* python operator, allocs own string */
2612                         MEM_freeN(ot->idname);
2613         }
2614         
2615         BLI_freelistN(&global_ops);
2616 }
2617
2618 /* called on initialize WM_init() */
2619 void wm_operatortype_init(void)
2620 {
2621         WM_operatortype_append(WM_OT_window_duplicate);
2622         WM_operatortype_append(WM_OT_read_homefile);
2623         WM_operatortype_append(WM_OT_save_homefile);
2624         WM_operatortype_append(WM_OT_window_fullscreen_toggle);
2625         WM_operatortype_append(WM_OT_exit_blender);
2626         WM_operatortype_append(WM_OT_open_mainfile);
2627         WM_operatortype_append(WM_OT_link_append);
2628         WM_operatortype_append(WM_OT_recover_last_session);
2629         WM_operatortype_append(WM_OT_recover_auto_save);
2630         WM_operatortype_append(WM_OT_save_as_mainfile);
2631         WM_operatortype_append(WM_OT_save_mainfile);
2632         WM_operatortype_append(WM_OT_redraw_timer);
2633         WM_operatortype_append(WM_OT_memory_statistics);
2634         WM_operatortype_append(WM_OT_debug_menu);
2635         WM_operatortype_append(WM_OT_splash);
2636         WM_operatortype_append(WM_OT_search_menu);
2637         WM_operatortype_append(WM_OT_call_menu);
2638
2639 #ifdef WITH_COLLADA
2640         /* XXX: move these */
2641         WM_operatortype_append(WM_OT_collada_export);
2642         WM_operatortype_append(WM_OT_collada_import);
2643 #endif
2644
2645 }
2646
2647 /* called in transform_ops.c, on each regeneration of keymaps  */
2648 static void gesture_circle_modal_keymap(wmKeyConfig *keyconf)
2649 {
2650         static EnumPropertyItem modal_items[] = {
2651         {GESTURE_MODAL_CANCEL,  "CANCEL", 0, "Cancel", ""},
2652         {GESTURE_MODAL_CONFIRM, "CONFIRM", 0, "Confirm", ""},
2653         {GESTURE_MODAL_CIRCLE_ADD, "ADD", 0, "Add", ""},
2654         {GESTURE_MODAL_CIRCLE_SUB, "SUBTRACT", 0, "Subtract", ""},
2655
2656         {GESTURE_MODAL_SELECT,  "SELECT", 0, "Select", ""},
2657         {GESTURE_MODAL_DESELECT,"DESELECT", 0, "DeSelect", ""},
2658         {GESTURE_MODAL_NOP,"NOP", 0, "No Operation", ""},
2659
2660
2661         {0, NULL, 0, NULL, NULL}};
2662
2663         wmKeyMap *keymap= WM_modalkeymap_get(keyconf, "View3D Gesture Circle");
2664
2665         /* this function is called for each spacetype, only needs to add map once */
2666         if(keymap) return;
2667
2668         keymap= WM_modalkeymap_add(keyconf, "View3D Gesture Circle", modal_items);
2669
2670         /* items for modal map */
2671         WM_modalkeymap_add_item(keymap, ESCKEY,    KM_PRESS, KM_ANY, 0, GESTURE_MODAL_CANCEL);
2672         WM_modalkeymap_add_item(keymap, RIGHTMOUSE, KM_ANY, KM_ANY, 0, GESTURE_MODAL_CANCEL);
2673
2674         WM_modalkeymap_add_item(keymap, RETKEY, KM_PRESS, KM_ANY, 0, GESTURE_MODAL_CONFIRM);
2675         WM_modalkeymap_add_item(keymap, PADENTER, KM_PRESS, 0, 0, GESTURE_MODAL_CONFIRM);
2676
2677         WM_modalkeymap_add_item(keymap, LEFTMOUSE, KM_PRESS, 0, 0, GESTURE_MODAL_SELECT);
2678
2679 #if 0 // Durien guys like this :S
2680         WM_modalkeymap_add_item(keymap, LEFTMOUSE, KM_PRESS, KM_SHIFT, 0, GESTURE_MODAL_DESELECT);
2681         WM_modalkeymap_add_item(keymap, LEFTMOUSE, KM_RELEASE, KM_SHIFT, 0, GESTURE_MODAL_NOP);
2682 #else
2683         WM_modalkeymap_add_item(keymap, MIDDLEMOUSE, KM_PRESS, 0, 0, GESTURE_MODAL_DESELECT); //  defailt 2.4x
2684         WM_modalkeymap_add_item(keymap, MIDDLEMOUSE, KM_RELEASE, 0, 0, GESTURE_MODAL_NOP); //  defailt 2.4x
2685 #endif
2686
2687         WM_modalkeymap_add_item(keymap, LEFTMOUSE, KM_RELEASE, 0, 0, GESTURE_MODAL_NOP);
2688
2689         WM_modalkeymap_add_item(keymap, WHEELUPMOUSE, KM_PRESS, 0, 0, GESTURE_MODAL_CIRCLE_SUB);
2690         WM_modalkeymap_add_item(keymap, PADMINUS, KM_PRESS, 0, 0, GESTURE_MODAL_CIRCLE_SUB);
2691         WM_modalkeymap_add_item(keymap, WHEELDOWNMOUSE, KM_PRESS, 0, 0, GESTURE_MODAL_CIRCLE_ADD);
2692         WM_modalkeymap_add_item(keymap, PADPLUSKEY, KM_PRESS, 0, 0, GESTURE_MODAL_CIRCLE_ADD);
2693
2694         /* assign map to operators */
2695         WM_modalkeymap_assign(keymap, "VIEW3D_OT_select_circle");
2696         WM_modalkeymap_assign(keymap, "UV_OT_circle_select");
2697
2698 }
2699
2700 /* called in transform_ops.c, on each regeneration of keymaps  */
2701 static void gesture_border_modal_keymap(wmKeyConfig *keyconf)
2702 {
2703         static EnumPropertyItem modal_items[] = {
2704         {GESTURE_MODAL_CANCEL,  "CANCEL", 0, "Cancel", ""},
2705         {GESTURE_MODAL_SELECT,  "SELECT", 0, "Select", ""},
2706         {GESTURE_MODAL_DESELECT,"DESELECT", 0, "DeSelect", ""},
2707         {GESTURE_MODAL_BORDER_BEGIN,    "BEGIN", 0, "Begin", ""},
2708         {0, NULL, 0, NULL, NULL}};
2709
2710         wmKeyMap *keymap= WM_modalkeymap_get(keyconf, "Gesture Border");
2711
2712         /* this function is called for each spacetype, only needs to add map once */
2713         if(keymap) return;
2714
2715         keymap= WM_modalkeymap_add(keyconf, "Gesture Border", modal_items);
2716
2717         /* items for modal map */
2718         WM_modalkeymap_add_item(keymap, ESCKEY,    KM_PRESS, KM_ANY, 0, GESTURE_MODAL_CANCEL);
2719         WM_modalkeymap_add_item(keymap, RIGHTMOUSE, KM_ANY, KM_ANY, 0, GESTURE_MODAL_CANCEL);
2720
2721         WM_modalkeymap_add_item(keymap, LEFTMOUSE, KM_PRESS, 0, 0, GESTURE_MODAL_BORDER_BEGIN);
2722         WM_modalkeymap_add_item(keymap, LEFTMOUSE, KM_RELEASE, 0, 0, GESTURE_MODAL_SELECT);
2723
2724 #if 0 // Durian guys like this
2725         WM_modalkeymap_add_item(keymap, LEFTMOUSE, KM_PRESS, KM_SHIFT, 0, GESTURE_MODAL_BORDER_BEGIN);
2726         WM_modalkeymap_add_item(keymap, LEFTMOUSE, KM_RELEASE, KM_SHIFT, 0, GESTURE_MODAL_DESELECT);
2727 #else
2728         WM_modalkeymap_add_item(keymap, MIDDLEMOUSE, KM_PRESS, 0, 0, GESTURE_MODAL_BORDER_BEGIN);
2729         WM_modalkeymap_add_item(keymap, MIDDLEMOUSE, KM_RELEASE, 0, 0, GESTURE_MODAL_DESELECT);
2730 #endif
2731
2732         /* assign map to operators */
2733         WM_modalkeymap_assign(keymap, "ACTION_OT_select_border");
2734         WM_modalkeymap_assign(keymap, "ANIM_OT_channels_select_border");
2735         WM_modalkeymap_assign(keymap, "ANIM_OT_previewrange_set");
2736         WM_modalkeymap_assign(keymap, "CONSOLE_OT_select_border");
2737         WM_modalkeymap_assign(keymap, "FILE_OT_select_border");
2738         WM_modalkeymap_assign(keymap, "GRAPH_OT_select_border");
2739         WM_modalkeymap_assign(keymap, "MARKER_OT_select_border");
2740         WM_modalkeymap_assign(keymap, "NLA_OT_select_border");
2741         WM_modalkeymap_assign(keymap, "NODE_OT_select_border");
2742 //      WM_modalkeymap_assign(keymap, "SCREEN_OT_border_select"); // template
2743         WM_modalkeymap_assign(keymap, "SEQUENCER_OT_select_border");
2744         WM_modalkeymap_assign(keymap, "UV_OT_select_border");
2745         WM_modalkeymap_assign(keymap, "VIEW2D_OT_zoom_border");
2746         WM_modalkeymap_assign(keymap, "VIEW3D_OT_clip_border");
2747         WM_modalkeymap_assign(keymap, "VIEW3D_OT_render_border");
2748         WM_modalkeymap_assign(keymap, "VIEW3D_OT_select_border");
2749         WM_modalkeymap_assign(keymap, "VIEW3D_OT_zoom_border"); // XXX TODO: zoom border should perhaps map rightmouse to zoom out instead of in+cancel
2750 }
2751
2752 /* default keymap for windows and screens, only call once per WM */
2753 void wm_window_keymap(wmKeyConfig *keyconf)
2754 {
2755         wmKeyMap *keymap= WM_keymap_find(keyconf, "Window", 0, 0);
2756         wmKeyMapItem *kmi;
2757         
2758         /* note, this doesn't replace existing keymap items */
2759         WM_keymap_verify_item(keymap, "WM_OT_window_duplicate", WKEY, KM_PRESS, KM_CTRL|KM_ALT, 0);
2760         #ifdef __APPLE__
2761         WM_keymap_add_item(keymap, "WM_OT_read_homefile", NKEY, KM_PRESS, KM_OSKEY, 0);
2762         WM_keymap_add_menu(keymap, "INFO_MT_file_open_recent", OKEY, KM_PRESS, KM_SHIFT|KM_OSKEY, 0);
2763         WM_keymap_add_item(keymap, "WM_OT_open_mainfile", OKEY, KM_PRESS, KM_OSKEY, 0);
2764         WM_keymap_add_item(keymap, "WM_OT_save_mainfile", SKEY, KM_PRESS, KM_OSKEY, 0);
2765         WM_keymap_add_item(keymap, "WM_OT_save_as_mainfile", SKEY, KM_PRESS, KM_SHIFT|KM_OSKEY, 0);
2766         WM_keymap_add_item(keymap, "WM_OT_exit_blender", QKEY, KM_PRESS, KM_OSKEY, 0);
2767         #endif
2768         WM_keymap_add_item(keymap, "WM_OT_read_homefile", NKEY, KM_PRESS, KM_CTRL, 0);
2769         WM_keymap_add_item(keymap, "WM_OT_save_homefile", UKEY, KM_PRESS, KM_CTRL, 0); 
2770         WM_keymap_add_menu(keymap, "INFO_MT_file_open_recent", OKEY, KM_PRESS, KM_SHIFT|KM_CTRL, 0);
2771         WM_keymap_add_item(keymap, "WM_OT_open_mainfile", OKEY, KM_PRESS, KM_CTRL, 0);
2772         WM_keymap_add_item(keymap, "WM_OT_open_mainfile", F1KEY, KM_PRESS, 0, 0);
2773         WM_keymap_add_item(keymap, "WM_OT_link_append", OKEY, KM_PRESS, KM_CTRL|KM_ALT, 0);
2774         kmi= WM_keymap_add_item(keymap, "WM_OT_link_append", F1KEY, KM_PRESS, KM_SHIFT, 0);
2775         RNA_boolean_set(kmi->ptr, "link", FALSE);
2776
2777         WM_keymap_add_item(keymap, "WM_OT_save_mainfile", SKEY, KM_PRESS, KM_CTRL, 0);
2778         WM_keymap_add_item(keymap, "WM_OT_save_mainfile", WKEY, KM_PRESS, KM_CTRL, 0);
2779         WM_keymap_add_item(keymap, "WM_OT_save_as_mainfile", SKEY, KM_PRESS, KM_SHIFT|KM_CTRL, 0);
2780         WM_keymap_add_item(keymap, "WM_OT_save_as_mainfile", F2KEY, KM_PRESS, 0, 0);
2781
2782         WM_keymap_verify_item(keymap, "WM_OT_window_fullscreen_toggle", F11KEY, KM_PRESS, KM_ALT, 0);
2783         WM_keymap_add_item(keymap, "WM_OT_exit_blender", QKEY, KM_PRESS, KM_CTRL, 0);
2784
2785         /* debug/testing */
2786         WM_keymap_verify_item(keymap, "WM_OT_redraw_timer", TKEY, KM_PRESS, KM_ALT|KM_CTRL, 0);
2787         WM_keymap_verify_item(keymap, "WM_OT_debug_menu", DKEY, KM_PRESS, KM_ALT|KM_CTRL, 0);
2788         WM_keymap_verify_item(keymap, "WM_OT_search_menu", SPACEKEY, KM_PRESS, 0, 0);
2789         
2790         /* Space switching */
2791
2792
2793         kmi = WM_keymap_add_item(keymap, "WM_OT_context_set_enum", F2KEY, KM_PRESS, KM_SHIFT, 0); /* new in 2.5x, was DXF export */
2794         RNA_string_set(kmi->ptr, "path", "area.type");
2795         RNA_string_set(kmi->ptr, "value", "LOGIC_EDITOR");
2796
2797         kmi = WM_keymap_add_item(keymap, "WM_OT_context_set_enum", F3KEY, KM_PRESS, KM_SHIFT, 0);
2798         RNA_string_set(kmi->ptr, "path", "area.type");
2799         RNA_string_set(kmi->ptr, "value", "NODE_EDITOR");
2800
2801         kmi = WM_keymap_add_item(keymap, "WM_OT_context_set_enum", F4KEY, KM_PRESS, KM_SHIFT, 0); /* new in 2.5x, was data browser */
2802         RNA_string_set(kmi->ptr, "path", "area.type");
2803         RNA_string_set(kmi->ptr, "value", "CONSOLE");
2804
2805         kmi = WM_keymap_add_item(keymap, "WM_OT_context_set_enum", F5KEY, KM_PRESS, KM_SHIFT, 0);
2806         RNA_string_set(kmi->ptr, "path", "area.type");
2807         RNA_string_set(kmi->ptr, "value", "VIEW_3D");
2808
2809         kmi = WM_keymap_add_item(keymap, "WM_OT_context_set_enum", F6KEY, KM_PRESS, KM_SHIFT, 0);
2810         RNA_string_set(kmi->ptr, "path", "area.type");
2811         RNA_string_set(kmi->ptr, "value", "GRAPH_EDITOR");
2812
2813         kmi = WM_keymap_add_item(keymap, "WM_OT_context_set_enum", F7KEY, KM_PRESS, KM_SHIFT, 0);
2814         RNA_string_set(kmi->ptr, "path", "area.type");
2815         RNA_string_set(kmi->ptr, "value", "PROPERTIES");
2816
2817         kmi = WM_keymap_add_item(keymap, "WM_OT_context_set_enum", F8KEY, KM_PRESS, KM_SHIFT, 0);
2818         RNA_string_set(kmi->ptr, "path", "area.type");
2819         RNA_string_set(kmi->ptr, "value", "SEQUENCE_EDITOR");
2820
2821         kmi = WM_keymap_add_item(keymap, "WM_OT_context_set_enum", F9KEY, KM_PRESS, KM_SHIFT, 0);
2822         RNA_string_set(kmi->ptr, "path", "area.type");
2823         RNA_string_set(kmi->ptr, "value", "OUTLINER");
2824
2825         kmi = WM_keymap_add_item(keymap, "WM_OT_context_set_enum", F10KEY, KM_PRESS, KM_SHIFT, 0);
2826         RNA_string_set(kmi->ptr, "path", "area.type");
2827         RNA_string_set(kmi->ptr, "value", "IMAGE_EDITOR");
2828
2829         kmi = WM_keymap_add_item(keymap, "WM_OT_context_set_enum", F11KEY, KM_PRESS, KM_SHIFT, 0);
2830         RNA_string_set(kmi->ptr, "path", "area.type");
2831         RNA_string_set(kmi->ptr, "value", "TEXT_EDITOR");
2832
2833         kmi = WM_keymap_add_item(keymap, "WM_OT_context_set_enum", F12KEY, KM_PRESS, KM_SHIFT, 0);
2834         RNA_string_set(kmi->ptr, "path", "area.type");
2835         RNA_string_set(kmi->ptr, "value", "DOPESHEET_EDITOR");
2836
2837         gesture_circle_modal_keymap(keyconf);
2838         gesture_border_modal_keymap(keyconf);
2839 }
2840
2841 /* Generic itemf's for operators that take library args */
2842 static EnumPropertyItem *rna_id_itemf(bContext *C, PointerRNA *ptr, int *free, ID *id)
2843 {
2844         EnumPropertyItem *item= NULL, item_tmp;
2845         int totitem= 0;
2846         int i= 0;
2847
2848         memset(&item_tmp, 0, sizeof(item_tmp));
2849
2850         for( ; id; id= id->next) {
2851                 item_tmp.identifier= item_tmp.name= id->name+2;
2852                 item_tmp.value= i++;
2853                 RNA_enum_item_add(&item, &totitem, &item_tmp);
2854         }
2855
2856         RNA_enum_item_end(&item, &totitem);
2857         *free= 1;
2858
2859         return item;
2860 }
2861
2862 /* can add more */
2863 EnumPropertyItem *RNA_group_itemf(bContext *C, PointerRNA *ptr, int *free)
2864 {
2865         return rna_id_itemf(C, ptr, free, (ID *)CTX_data_main(C)->group.first);
2866 }
2867 EnumPropertyItem *RNA_scene_itemf(bContext *C, PointerRNA *ptr, int *free)
2868 {
2869         return rna_id_itemf(C, ptr, free, (ID *)CTX_data_main(C)->scene.first);
2870 }