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