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