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