4 * ***** BEGIN GPL LICENSE BLOCK *****
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.
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.
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.
20 * The Original Code is Copyright (C) 2007 Blender Foundation.
21 * All rights reserved.
24 * Contributor(s): Blender Foundation
26 * ***** END GPL LICENSE BLOCK *****
29 /** \file blender/windowmanager/intern/wm_operators.c
41 #include "GHOST_C-api.h"
43 #include "MEM_guardedalloc.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"
56 #include "BLI_blenlib.h"
57 #include "BLI_dynstr.h" /*for WM_operator_pystring */
59 #include "BLI_string.h"
60 #include "BLI_utildefines.h"
62 #include "BLO_readfile.h"
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"
72 #include "BKE_report.h"
73 #include "BKE_scene.h"
74 #include "BKE_screen.h" /* BKE_ST_MAXNAME */
76 #include "BKE_idcode.h"
79 #include "BIF_glutil.h" /* for paint cursor */
81 #include "IMB_imbuf_types.h"
83 #include "ED_screen.h"
86 #include "RNA_access.h"
87 #include "RNA_define.h"
88 #include "RNA_enum_types.h"
90 #include "UI_interface.h"
91 #include "UI_resources.h"
98 #include "wm_event_system.h"
99 #include "wm_event_types.h"
100 #include "wm_subwindow.h"
101 #include "wm_window.h"
103 static ListBase global_ops= {NULL, NULL};
105 /* ************ operator API, exported ********** */
108 wmOperatorType *WM_operatortype_find(const char *idname, int quiet)
112 char idname_bl[OP_MAX_TYPENAME]; // XXX, needed to support python style names without the _OT_ syntax
113 WM_operator_bl_idname(idname_bl, idname);
116 ot= (wmOperatorType *)BLI_findstring_ptr(&global_ops, idname_bl, offsetof(wmOperatorType, idname));
123 printf("search for unknown operator %s, %s\n", idname_bl, idname);
128 wmOperatorType *WM_operatortype_first(void)
130 return global_ops.first;
133 /* all ops in 1 list (for time being... needs evaluation later) */
134 void WM_operatortype_append(void (*opfunc)(wmOperatorType*))
138 ot= MEM_callocN(sizeof(wmOperatorType), "operatortype");
139 ot->srna= RNA_def_struct(&BLENDER_RNA, "", "OperatorProperties");
143 static char dummy_name[] = "Dummy Name";
144 fprintf(stderr, "ERROR: Operator %s has no name property!\n", ot->idname);
145 ot->name= dummy_name;
148 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.
149 RNA_def_struct_identifier(ot->srna, ot->idname);
150 BLI_addtail(&global_ops, ot);
153 void WM_operatortype_append_ptr(void (*opfunc)(wmOperatorType*, void*), void *userdata)
157 ot= MEM_callocN(sizeof(wmOperatorType), "operatortype");
158 ot->srna= RNA_def_struct(&BLENDER_RNA, "", "OperatorProperties");
159 opfunc(ot, userdata);
160 RNA_def_struct_ui_text(ot->srna, ot->name, ot->description ? ot->description:"(undocumented operator)");
161 RNA_def_struct_identifier(ot->srna, ot->idname);
162 BLI_addtail(&global_ops, ot);
165 /* ********************* macro operator ******************** */
171 static void wm_macro_start(wmOperator *op)
173 if (op->customdata == NULL) {
174 op->customdata = MEM_callocN(sizeof(MacroData), "MacroData");
178 static int wm_macro_end(wmOperator *op, int retval)
180 if (retval & OPERATOR_CANCELLED) {
181 MacroData *md = op->customdata;
183 if (md->retval & OPERATOR_FINISHED) {
184 retval |= OPERATOR_FINISHED;
185 retval &= ~OPERATOR_CANCELLED;
189 /* if modal is ending, free custom data */
190 if (retval & (OPERATOR_FINISHED|OPERATOR_CANCELLED)) {
191 if (op->customdata) {
192 MEM_freeN(op->customdata);
193 op->customdata = NULL;
200 /* macro exec only runs exec calls */
201 static int wm_macro_exec(bContext *C, wmOperator *op)
204 int retval= OPERATOR_FINISHED;
208 for(opm= op->macro.first; opm; opm= opm->next) {
210 if(opm->type->exec) {
211 retval= opm->type->exec(C, opm);
213 if (retval & OPERATOR_FINISHED) {
214 MacroData *md = op->customdata;
215 md->retval = OPERATOR_FINISHED; /* keep in mind that at least one operator finished */
217 break; /* operator didn't finish, end macro */
222 return wm_macro_end(op, retval);
225 static int wm_macro_invoke_internal(bContext *C, wmOperator *op, wmEvent *event, wmOperator *opm)
227 int retval= OPERATOR_FINISHED;
229 /* start from operator received as argument */
230 for( ; opm; opm= opm->next) {
231 if(opm->type->invoke)
232 retval= opm->type->invoke(C, opm, event);
233 else if(opm->type->exec)
234 retval= opm->type->exec(C, opm);
236 BLI_movelisttolist(&op->reports->list, &opm->reports->list);
238 if (retval & OPERATOR_FINISHED) {
239 MacroData *md = op->customdata;
240 md->retval = OPERATOR_FINISHED; /* keep in mind that at least one operator finished */
242 break; /* operator didn't finish, end macro */
246 return wm_macro_end(op, retval);
249 static int wm_macro_invoke(bContext *C, wmOperator *op, wmEvent *event)
252 return wm_macro_invoke_internal(C, op, event, op->macro.first);
255 static int wm_macro_modal(bContext *C, wmOperator *op, wmEvent *event)
257 wmOperator *opm = op->opm;
258 int retval= OPERATOR_FINISHED;
261 printf("macro error, calling NULL modal()\n");
263 retval = opm->type->modal(C, opm, event);
265 /* if this one is done but it's not the last operator in the macro */
266 if ((retval & OPERATOR_FINISHED) && opm->next) {
267 MacroData *md = op->customdata;
269 md->retval = OPERATOR_FINISHED; /* keep in mind that at least one operator finished */
271 retval = wm_macro_invoke_internal(C, op, event, opm->next);
273 /* if new operator is modal and also added its own handler */
274 if (retval & OPERATOR_RUNNING_MODAL && op->opm != opm) {
275 wmWindow *win = CTX_wm_window(C);
276 wmEventHandler *handler = NULL;
278 for (handler = win->modalhandlers.first; handler; handler = handler->next) {
279 /* first handler in list is the new one */
280 if (handler->op == op)
285 BLI_remlink(&win->modalhandlers, handler);
286 wm_event_free_handler(handler);
289 /* if operator is blocking, grab cursor
290 * This may end up grabbing twice, but we don't care.
292 if(op->opm->type->flag & OPTYPE_BLOCKING) {
293 int bounds[4] = {-1,-1,-1,-1};
294 int wrap = (U.uiflag & USER_CONTINUOUS_MOUSE) && ((op->opm->flag & OP_GRAB_POINTER) || (op->opm->type->flag & OPTYPE_GRAB_POINTER));
297 ARegion *ar= CTX_wm_region(C);
299 bounds[0]= ar->winrct.xmin;
300 bounds[1]= ar->winrct.ymax;
301 bounds[2]= ar->winrct.xmax;
302 bounds[3]= ar->winrct.ymin;
306 WM_cursor_grab(CTX_wm_window(C), wrap, FALSE, bounds);
312 return wm_macro_end(op, retval);
315 static int wm_macro_cancel(bContext *C, wmOperator *op)
317 /* call cancel on the current modal operator, if any */
318 if (op->opm && op->opm->type->cancel) {
319 op->opm->type->cancel(C, op->opm);
322 return wm_macro_end(op, OPERATOR_CANCELLED);
325 /* Names have to be static for now */
326 wmOperatorType *WM_operatortype_append_macro(const char *idname, const char *name, int flag)
330 if(WM_operatortype_find(idname, TRUE)) {
331 printf("Macro error: operator %s exists\n", idname);
335 ot= MEM_callocN(sizeof(wmOperatorType), "operatortype");
336 ot->srna= RNA_def_struct(&BLENDER_RNA, "", "OperatorProperties");
340 ot->flag= OPTYPE_MACRO|flag;
342 ot->exec= wm_macro_exec;
343 ot->invoke= wm_macro_invoke;
344 ot->modal= wm_macro_modal;
345 ot->cancel= wm_macro_cancel;
349 ot->description= "(undocumented operator)";
351 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.
352 RNA_def_struct_identifier(ot->srna, ot->idname);
354 BLI_addtail(&global_ops, ot);
359 void WM_operatortype_append_macro_ptr(void (*opfunc)(wmOperatorType*, void*), void *userdata)
363 ot= MEM_callocN(sizeof(wmOperatorType), "operatortype");
364 ot->srna= RNA_def_struct(&BLENDER_RNA, "", "OperatorProperties");
366 ot->flag= OPTYPE_MACRO;
367 ot->exec= wm_macro_exec;
368 ot->invoke= wm_macro_invoke;
369 ot->modal= wm_macro_modal;
370 ot->cancel= wm_macro_cancel;
374 ot->description= "(undocumented operator)";
376 opfunc(ot, userdata);
378 RNA_def_struct_ui_text(ot->srna, ot->name, ot->description);
379 RNA_def_struct_identifier(ot->srna, ot->idname);
381 BLI_addtail(&global_ops, ot);
384 wmOperatorTypeMacro *WM_operatortype_macro_define(wmOperatorType *ot, const char *idname)
386 wmOperatorTypeMacro *otmacro= MEM_callocN(sizeof(wmOperatorTypeMacro), "wmOperatorTypeMacro");
388 BLI_strncpy(otmacro->idname, idname, OP_MAX_TYPENAME);
390 /* do this on first use, since operatordefinitions might have been not done yet */
391 WM_operator_properties_alloc(&(otmacro->ptr), &(otmacro->properties), idname);
392 WM_operator_properties_sanitize(otmacro->ptr, 1);
394 BLI_addtail(&ot->macro, otmacro);
397 /* operator should always be found but in the event its not. dont segfault */
398 wmOperatorType *otsub = WM_operatortype_find(idname, 0);
400 RNA_def_pointer_runtime(ot->srna, otsub->idname, otsub->srna,
401 otsub->name, otsub->description);
408 static void wm_operatortype_free_macro(wmOperatorType *ot)
410 wmOperatorTypeMacro *otmacro;
412 for(otmacro= ot->macro.first; otmacro; otmacro= otmacro->next) {
414 WM_operator_properties_free(otmacro->ptr);
415 MEM_freeN(otmacro->ptr);
418 BLI_freelistN(&ot->macro);
422 int WM_operatortype_remove(const char *idname)
424 wmOperatorType *ot = WM_operatortype_find(idname, 0);
429 BLI_remlink(&global_ops, ot);
430 RNA_struct_free(&BLENDER_RNA, ot->srna);
433 wm_operatortype_free_macro(ot);
440 /* SOME_OT_op -> some.op */
441 void WM_operator_py_idname(char *to, const char *from)
443 char *sep= strstr(from, "_OT_");
447 /* note, we use ascii tolower instead of system tolower, because the
448 latter depends on the locale, and can lead to idname mistmatch */
449 memcpy(to, from, sizeof(char)*ofs);
450 BLI_ascii_strtolower(to, ofs);
453 BLI_strncpy(to+(ofs+1), sep+4, OP_MAX_TYPENAME);
456 /* should not happen but support just incase */
457 BLI_strncpy(to, from, OP_MAX_TYPENAME);
461 /* some.op -> SOME_OT_op */
462 void WM_operator_bl_idname(char *to, const char *from)
465 char *sep= strchr(from, '.');
470 memcpy(to, from, sizeof(char)*ofs);
471 BLI_ascii_strtoupper(to, ofs);
473 BLI_strncpy(to+ofs, "_OT_", OP_MAX_TYPENAME);
474 BLI_strncpy(to+(ofs+4), sep+1, OP_MAX_TYPENAME);
477 /* should not happen but support just incase */
478 BLI_strncpy(to, from, OP_MAX_TYPENAME);
485 /* print a string representation of the operator, with the args that it runs
486 * so python can run it again,
488 * When calling from an existing wmOperator do.
489 * WM_operator_pystring(op->type, op->ptr);
491 char *WM_operator_pystring(bContext *C, wmOperatorType *ot, PointerRNA *opptr, int all_args)
493 const char *arg_name= NULL;
494 char idname_py[OP_MAX_TYPENAME];
496 PropertyRNA *prop, *iterprop;
498 /* for building the string */
499 DynStr *dynstr= BLI_dynstr_new();
501 int first_iter=1, ok= 1;
504 /* only to get the orginal props for comparisons */
505 PointerRNA opptr_default;
506 PropertyRNA *prop_default;
508 if(all_args==0 || opptr==NULL) {
509 WM_operator_properties_create_ptr(&opptr_default, ot);
512 opptr = &opptr_default;
516 WM_operator_py_idname(idname_py, ot->idname);
517 BLI_dynstr_appendf(dynstr, "bpy.ops.%s(", idname_py);
519 iterprop= RNA_struct_iterator_property(opptr->type);
521 RNA_PROP_BEGIN(opptr, propptr, iterprop) {
523 arg_name= RNA_property_identifier(prop);
525 if (strcmp(arg_name, "rna_type")==0) continue;
527 buf= RNA_property_as_string(C, opptr, prop);
532 /* not verbose, so only add in attributes that use non-default values
533 * slow but good for tooltips */
534 prop_default= RNA_struct_find_property(&opptr_default, arg_name);
537 buf_default= RNA_property_as_string(C, &opptr_default, prop_default);
539 if(strcmp(buf, buf_default)==0)
540 ok= 0; /* values match, dont bother printing */
542 MEM_freeN(buf_default);
547 BLI_dynstr_appendf(dynstr, first_iter?"%s=%s":", %s=%s", arg_name, buf);
556 if(all_args==0 || opptr==&opptr_default )
557 WM_operator_properties_free(&opptr_default);
559 BLI_dynstr_append(dynstr, ")");
561 cstring = BLI_dynstr_get_cstring(dynstr);
562 BLI_dynstr_free(dynstr);
566 void WM_operator_properties_create_ptr(PointerRNA *ptr, wmOperatorType *ot)
568 RNA_pointer_create(NULL, ot->srna, NULL, ptr);
571 void WM_operator_properties_create(PointerRNA *ptr, const char *opstring)
573 wmOperatorType *ot= WM_operatortype_find(opstring, 0);
576 WM_operator_properties_create_ptr(ptr, ot);
578 RNA_pointer_create(NULL, &RNA_OperatorProperties, NULL, ptr);
581 /* similar to the function above except its uses ID properties
582 * used for keymaps and macros */
583 void WM_operator_properties_alloc(PointerRNA **ptr, IDProperty **properties, const char *opstring)
585 if(*properties==NULL) {
586 IDPropertyTemplate val = {0};
587 *properties= IDP_New(IDP_GROUP, val, "wmOpItemProp");
591 *ptr= MEM_callocN(sizeof(PointerRNA), "wmOpItemPtr");
592 WM_operator_properties_create(*ptr, opstring);
595 (*ptr)->data= *properties;
599 void WM_operator_properties_sanitize(PointerRNA *ptr, const short no_context)
601 RNA_STRUCT_BEGIN(ptr, prop) {
602 switch(RNA_property_type(prop)) {
605 RNA_def_property_flag(prop, PROP_ENUM_NO_CONTEXT);
607 RNA_def_property_clear_flag(prop, PROP_ENUM_NO_CONTEXT);
611 StructRNA *ptype= RNA_property_pointer_type(ptr, prop);
613 /* recurse into operator properties */
614 if (RNA_struct_is_a(ptype, &RNA_OperatorProperties)) {
615 PointerRNA opptr = RNA_property_pointer_get(ptr, prop);
616 WM_operator_properties_sanitize(&opptr, no_context);
627 void WM_operator_properties_free(PointerRNA *ptr)
629 IDProperty *properties= ptr->data;
632 IDP_FreeProperty(properties);
633 MEM_freeN(properties);
634 ptr->data= NULL; /* just incase */
638 /* ************ default op callbacks, exported *********** */
640 /* invoke callback, uses enum property named "type" */
641 int WM_menu_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
643 PropertyRNA *prop= op->type->prop;
648 printf("WM_menu_invoke: %s has no enum property set\n", op->type->idname);
650 else if (RNA_property_type(prop) != PROP_ENUM) {
651 printf("WM_menu_invoke: %s \"%s\" is not an enum property\n", op->type->idname, RNA_property_identifier(prop));
653 else if (RNA_property_is_set(op->ptr, RNA_property_identifier(prop))) {
654 return op->type->exec(C, op);
657 pup= uiPupMenuBegin(C, op->type->name, ICON_NONE);
658 layout= uiPupMenuLayout(pup);
659 uiItemsFullEnumO(layout, op->type->idname, (char*)RNA_property_identifier(prop), op->ptr->data, WM_OP_EXEC_REGION_WIN, 0);
660 uiPupMenuEnd(C, pup);
663 return OPERATOR_CANCELLED;
667 /* generic enum search invoke popup */
668 static void operator_enum_search_cb(const struct bContext *C, void *arg_ot, const char *str, uiSearchItems *items)
670 wmOperatorType *ot = (wmOperatorType *)arg_ot;
671 PropertyRNA *prop= ot->prop;
674 printf("WM_enum_search_invoke: %s has no enum property set\n", ot->idname);
676 else if (RNA_property_type(prop) != PROP_ENUM) {
677 printf("WM_enum_search_invoke: %s \"%s\" is not an enum property\n", ot->idname, RNA_property_identifier(prop));
682 EnumPropertyItem *item, *item_array;
685 RNA_pointer_create(NULL, ot->srna, NULL, &ptr);
686 RNA_property_enum_items((bContext *)C, &ptr, prop, &item_array, NULL, &do_free);
688 for(item= item_array; item->identifier; item++) {
689 /* note: need to give the intex rather then the dientifier because the enum can be freed */
690 if(BLI_strcasestr(item->name, str))
691 if(0==uiSearchItemAdd(items, item->name, SET_INT_IN_POINTER(item->value), 0))
696 MEM_freeN(item_array);
700 static void operator_enum_call_cb(struct bContext *C, void *arg1, void *arg2)
702 wmOperatorType *ot= arg1;
706 PointerRNA props_ptr;
707 WM_operator_properties_create_ptr(&props_ptr, ot);
708 RNA_property_enum_set(&props_ptr, ot->prop, GET_INT_FROM_POINTER(arg2));
709 WM_operator_name_call(C, ot->idname, WM_OP_EXEC_DEFAULT, &props_ptr);
710 WM_operator_properties_free(&props_ptr);
713 printf("operator_enum_call_cb: op->prop for '%s' is NULL\n", ot->idname);
718 static uiBlock *wm_enum_search_menu(bContext *C, ARegion *ar, void *arg_op)
720 static char search[256]= "";
722 wmWindow *win= CTX_wm_window(C);
725 wmOperator *op= (wmOperator *)arg_op;
727 block= uiBeginBlock(C, ar, "_popup", UI_EMBOSS);
728 uiBlockSetFlag(block, UI_BLOCK_LOOP|UI_BLOCK_RET_1|UI_BLOCK_MOVEMOUSE_QUIT);
730 //uiDefBut(block, LABEL, 0, op->type->name, 10, 10, 180, 19, NULL, 0.0, 0.0, 0, 0, ""); // ok, this isnt so easy...
731 but= uiDefSearchBut(block, search, 0, ICON_VIEWZOOM, 256, 10, 10, 180, 19, 0, 0, "");
732 uiButSetSearchFunc(but, operator_enum_search_cb, op->type, operator_enum_call_cb, NULL);
734 /* fake button, it holds space for search items */
735 uiDefBut(block, LABEL, 0, "", 10, 10 - uiSearchBoxhHeight(), 180, uiSearchBoxhHeight(), NULL, 0, 0, 0, 0, NULL);
737 uiPopupBoundsBlock(block, 6, 0, -20); /* move it downwards, mouse over button */
738 uiEndBlock(C, block);
740 event= *(win->eventstate); /* XXX huh huh? make api call */
741 event.type= EVT_BUT_OPEN;
743 event.customdata= but;
744 event.customdatafree= FALSE;
745 wm_event_add(win, &event);
751 int WM_enum_search_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
753 uiPupBlock(C, wm_enum_search_menu, op);
754 return OPERATOR_CANCELLED;
757 /* Can't be used as an invoke directly, needs message arg (can be NULL) */
758 int WM_operator_confirm_message(bContext *C, wmOperator *op, const char *message)
762 IDProperty *properties= op->ptr->data;
764 if(properties && properties->len)
765 properties= IDP_CopyProperty(op->ptr->data);
769 pup= uiPupMenuBegin(C, "OK?", ICON_QUESTION);
770 layout= uiPupMenuLayout(pup);
771 uiItemFullO(layout, op->type->idname, message, ICON_NONE, properties, WM_OP_EXEC_REGION_WIN, 0);
772 uiPupMenuEnd(C, pup);
774 return OPERATOR_CANCELLED;
778 int WM_operator_confirm(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
780 return WM_operator_confirm_message(C, op, NULL);
783 /* op->invoke, opens fileselect if path property not set, otherwise executes */
784 int WM_operator_filesel(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
786 if (RNA_property_is_set(op->ptr, "filepath")) {
787 return WM_operator_call(C, op);
790 WM_event_add_fileselect(C, op);
791 return OPERATOR_RUNNING_MODAL;
795 /* default properties for fileselect */
796 void WM_operator_properties_filesel(wmOperatorType *ot, int filter, short type, short action, short flag)
801 if(flag & WM_FILESEL_FILEPATH)
802 RNA_def_string_file_path(ot->srna, "filepath", "", FILE_MAX, "File Path", "Path to file");
804 if(flag & WM_FILESEL_DIRECTORY)
805 RNA_def_string_dir_path(ot->srna, "directory", "", FILE_MAX, "Directory", "Directory of the file");
807 if(flag & WM_FILESEL_FILENAME)
808 RNA_def_string_file_name(ot->srna, "filename", "", FILE_MAX, "File Name", "Name of the file");
810 if (action == FILE_SAVE) {
811 prop= RNA_def_boolean(ot->srna, "check_existing", 1, "Check Existing", "Check and warn on overwriting existing files");
812 RNA_def_property_flag(prop, PROP_HIDDEN);
815 prop= RNA_def_boolean(ot->srna, "filter_blender", (filter & BLENDERFILE), "Filter .blend files", "");
816 RNA_def_property_flag(prop, PROP_HIDDEN);
817 prop= RNA_def_boolean(ot->srna, "filter_image", (filter & IMAGEFILE), "Filter image files", "");
818 RNA_def_property_flag(prop, PROP_HIDDEN);
819 prop= RNA_def_boolean(ot->srna, "filter_movie", (filter & MOVIEFILE), "Filter movie files", "");
820 RNA_def_property_flag(prop, PROP_HIDDEN);
821 prop= RNA_def_boolean(ot->srna, "filter_python", (filter & PYSCRIPTFILE), "Filter python files", "");
822 RNA_def_property_flag(prop, PROP_HIDDEN);
823 prop= RNA_def_boolean(ot->srna, "filter_font", (filter & FTFONTFILE), "Filter font files", "");
824 RNA_def_property_flag(prop, PROP_HIDDEN);
825 prop= RNA_def_boolean(ot->srna, "filter_sound", (filter & SOUNDFILE), "Filter sound files", "");
826 RNA_def_property_flag(prop, PROP_HIDDEN);
827 prop= RNA_def_boolean(ot->srna, "filter_text", (filter & TEXTFILE), "Filter text files", "");
828 RNA_def_property_flag(prop, PROP_HIDDEN);
829 prop= RNA_def_boolean(ot->srna, "filter_btx", (filter & BTXFILE), "Filter btx files", "");
830 RNA_def_property_flag(prop, PROP_HIDDEN);
831 prop= RNA_def_boolean(ot->srna, "filter_collada", (filter & COLLADAFILE), "Filter COLLADA files", "");
832 RNA_def_property_flag(prop, PROP_HIDDEN);
833 prop= RNA_def_boolean(ot->srna, "filter_folder", (filter & FOLDERFILE), "Filter folders", "");
834 RNA_def_property_flag(prop, PROP_HIDDEN);
836 prop= RNA_def_int(ot->srna, "filemode", type, FILE_LOADLIB, FILE_SPECIAL,
837 "File Browser Mode", "The setting for the file browser mode to load a .blend file, a library or a special file",
838 FILE_LOADLIB, FILE_SPECIAL);
839 RNA_def_property_flag(prop, PROP_HIDDEN);
841 if(flag & WM_FILESEL_RELPATH)
842 RNA_def_boolean(ot->srna, "relative_path", (U.flag & USER_RELPATHS) ? 1:0, "Relative Path", "Select the file relative to the blend file");
845 void WM_operator_properties_select_all(wmOperatorType *ot) {
846 static EnumPropertyItem select_all_actions[] = {
847 {SEL_TOGGLE, "TOGGLE", 0, "Toggle", "Toggle selection for all elements"},
848 {SEL_SELECT, "SELECT", 0, "Select", "Select all elements"},
849 {SEL_DESELECT, "DESELECT", 0, "Deselect", "Deselect all elements"},
850 {SEL_INVERT, "INVERT", 0, "Invert", "Invert selection of all elements"},
851 {0, NULL, 0, NULL, NULL}
854 RNA_def_enum(ot->srna, "action", select_all_actions, SEL_TOGGLE, "Action", "Selection action to execute");
857 void WM_operator_properties_gesture_border(wmOperatorType *ot, int extend)
859 RNA_def_int(ot->srna, "gesture_mode", 0, INT_MIN, INT_MAX, "Gesture Mode", "", INT_MIN, INT_MAX);
860 RNA_def_int(ot->srna, "xmin", 0, INT_MIN, INT_MAX, "X Min", "", INT_MIN, INT_MAX);
861 RNA_def_int(ot->srna, "xmax", 0, INT_MIN, INT_MAX, "X Max", "", INT_MIN, INT_MAX);
862 RNA_def_int(ot->srna, "ymin", 0, INT_MIN, INT_MAX, "Y Min", "", INT_MIN, INT_MAX);
863 RNA_def_int(ot->srna, "ymax", 0, INT_MIN, INT_MAX, "Y Max", "", INT_MIN, INT_MAX);
866 RNA_def_boolean(ot->srna, "extend", 1, "Extend", "Extend selection instead of deselecting everything first");
869 void WM_operator_properties_gesture_straightline(wmOperatorType *ot, int cursor)
871 RNA_def_int(ot->srna, "xstart", 0, INT_MIN, INT_MAX, "X Start", "", INT_MIN, INT_MAX);
872 RNA_def_int(ot->srna, "xend", 0, INT_MIN, INT_MAX, "X End", "", INT_MIN, INT_MAX);
873 RNA_def_int(ot->srna, "ystart", 0, INT_MIN, INT_MAX, "Y Start", "", INT_MIN, INT_MAX);
874 RNA_def_int(ot->srna, "yend", 0, INT_MIN, INT_MAX, "Y End", "", INT_MIN, INT_MAX);
877 RNA_def_int(ot->srna, "cursor", cursor, 0, INT_MAX, "Cursor", "Mouse cursor style to use during the modal operator", 0, INT_MAX);
882 int WM_operator_winactive(bContext *C)
884 if(CTX_wm_window(C)==NULL) return 0;
888 wmOperator *WM_operator_last_redo(const bContext *C)
890 wmWindowManager *wm= CTX_wm_manager(C);
893 /* only for operators that are registered and did an undo push */
894 for(op= wm->operators.last; op; op= op->prev)
895 if((op->type->flag & OPTYPE_REGISTER) && (op->type->flag & OPTYPE_UNDO))
901 static uiBlock *wm_block_create_redo(bContext *C, ARegion *ar, void *arg_op)
903 wmOperator *op= arg_op;
906 uiStyle *style= U.uistyles.first;
910 block= uiBeginBlock(C, ar, "redo_popup", UI_EMBOSS);
911 uiBlockClearFlag(block, UI_BLOCK_LOOP);
912 uiBlockSetFlag(block, UI_BLOCK_KEEP_OPEN|UI_BLOCK_RET_1|UI_BLOCK_MOVEMOUSE_QUIT);
914 /* if register is not enabled, the operator gets freed on OPERATOR_FINISHED
915 * ui_apply_but_funcs_after calls ED_undo_operator_repeate_cb and crashes */
916 assert(op->type->flag & OPTYPE_REGISTER);
918 uiBlockSetHandleFunc(block, ED_undo_operator_repeat_cb_evt, arg_op);
919 layout= uiBlockLayout(block, UI_LAYOUT_VERTICAL, UI_LAYOUT_PANEL, 0, 0, width, 20, style);
921 if(ED_undo_valid(C, op->type->name)==0)
922 uiLayoutSetEnabled(layout, 0);
924 uiLayoutOperatorButs(C, layout, op, NULL, 'H', UI_LAYOUT_OP_SHOW_TITLE);
926 uiPopupBoundsBlock(block, 4, 0, 0);
927 uiEndBlock(C, block);
932 /* Only invoked by OK button in popups created with wm_block_create_dialog() */
933 static void dialog_exec_cb(bContext *C, void *arg1, void *arg2)
935 wmOperator *op= arg1;
936 uiBlock *block= arg2;
938 WM_operator_call(C, op);
940 uiPupBlockClose(C, block);
943 static void dialog_check_cb(bContext *C, void *op_ptr, void *UNUSED(arg))
945 wmOperator *op= op_ptr;
946 if(op->type->check) {
947 if(op->type->check(C, op)) {
953 /* Dialogs are popups that require user verification (click OK) before exec */
954 static uiBlock *wm_block_create_dialog(bContext *C, ARegion *ar, void *userData)
956 struct { wmOperator *op; int width; int height; } * data = userData;
957 wmOperator *op= data->op;
960 uiStyle *style= U.uistyles.first;
962 block = uiBeginBlock(C, ar, "operator dialog", UI_EMBOSS);
963 uiBlockClearFlag(block, UI_BLOCK_LOOP);
964 uiBlockSetFlag(block, UI_BLOCK_KEEP_OPEN|UI_BLOCK_RET_1|UI_BLOCK_MOVEMOUSE_QUIT);
966 layout= uiBlockLayout(block, UI_LAYOUT_VERTICAL, UI_LAYOUT_PANEL, 0, 0, data->width, data->height, style);
968 uiBlockSetFunc(block, dialog_check_cb, op, NULL);
970 uiLayoutOperatorButs(C, layout, op, NULL, 'H', UI_LAYOUT_OP_SHOW_TITLE);
972 /* clear so the OK button is left alone */
973 uiBlockSetFunc(block, NULL, NULL, NULL);
975 /* new column so as not to interfear with custom layouts [#26436] */
981 col= uiLayoutColumn(layout, FALSE);
982 col_block= uiLayoutGetBlock(col);
983 /* Create OK button, the callback of which will execute op */
984 btn= uiDefBut(col_block, BUT, 0, "OK", 0, -30, 0, 20, NULL, 0, 0, 0, 0, "");
985 uiButSetFunc(btn, dialog_exec_cb, op, col_block);
988 /* center around the mouse */
989 uiPopupBoundsBlock(block, 4, data->width/-2, data->height/2);
990 uiEndBlock(C, block);
995 static uiBlock *wm_operator_create_ui(bContext *C, ARegion *ar, void *userData)
997 struct { wmOperator *op; int width; int height; } * data = userData;
998 wmOperator *op= data->op;
1001 uiStyle *style= U.uistyles.first;
1003 block= uiBeginBlock(C, ar, "opui_popup", UI_EMBOSS);
1004 uiBlockClearFlag(block, UI_BLOCK_LOOP);
1005 uiBlockSetFlag(block, UI_BLOCK_KEEP_OPEN|UI_BLOCK_RET_1|UI_BLOCK_MOVEMOUSE_QUIT);
1007 layout= uiBlockLayout(block, UI_LAYOUT_VERTICAL, UI_LAYOUT_PANEL, 0, 0, data->width, data->height, style);
1009 /* since ui is defined the auto-layout args are not used */
1010 uiLayoutOperatorButs(C, layout, op, NULL, 'V', 0);
1012 uiPopupBoundsBlock(block, 4, 0, 0);
1013 uiEndBlock(C, block);
1018 /* operator menu needs undo, for redo callback */
1019 int WM_operator_props_popup(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
1022 if((op->type->flag & OPTYPE_REGISTER)==0) {
1023 BKE_reportf(op->reports, RPT_ERROR, "Operator '%s' does not have register enabled, incorrect invoke function.", op->type->idname);
1024 return OPERATOR_CANCELLED;
1027 ED_undo_push_op(C, op);
1028 wm_operator_register(C, op);
1030 uiPupBlock(C, wm_block_create_redo, op);
1032 return OPERATOR_RUNNING_MODAL;
1035 int WM_operator_props_dialog_popup(bContext *C, wmOperator *op, int width, int height)
1037 struct { wmOperator *op; int width; int height; } data;
1041 data.height= height;
1043 /* op is not executed until popup OK but is clicked */
1044 uiPupBlock(C, wm_block_create_dialog, &data);
1046 return OPERATOR_RUNNING_MODAL;
1049 int WM_operator_ui_popup(bContext *C, wmOperator *op, int width, int height)
1051 struct { wmOperator *op; int width; int height; } data;
1054 data.height = height;
1055 uiPupBlock(C, wm_operator_create_ui, &data);
1056 return OPERATOR_RUNNING_MODAL;
1059 int WM_operator_redo_popup(bContext *C, wmOperator *op)
1061 /* CTX_wm_reports(C) because operator is on stack, not active in event system */
1062 if((op->type->flag & OPTYPE_REGISTER)==0) {
1063 BKE_reportf(CTX_wm_reports(C), RPT_ERROR, "Operator redo '%s' does not have register enabled, incorrect invoke function.", op->type->idname);
1064 return OPERATOR_CANCELLED;
1066 if(op->type->poll && op->type->poll(C)==0) {
1067 BKE_reportf(CTX_wm_reports(C), RPT_ERROR, "Operator redo '%s': wrong context.", op->type->idname);
1068 return OPERATOR_CANCELLED;
1071 uiPupBlock(C, wm_block_create_redo, op);
1073 return OPERATOR_CANCELLED;
1076 /* ***************** Debug menu ************************* */
1078 static int wm_debug_menu_exec(bContext *C, wmOperator *op)
1080 G.rt= RNA_int_get(op->ptr, "debug_value");
1081 ED_screen_refresh(CTX_wm_manager(C), CTX_wm_window(C));
1082 WM_event_add_notifier(C, NC_WINDOW, NULL);
1084 return OPERATOR_FINISHED;
1087 static int wm_debug_menu_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
1089 RNA_int_set(op->ptr, "debug_value", G.rt);
1090 return WM_operator_props_dialog_popup(C, op, 180, 20);
1093 static void WM_OT_debug_menu(wmOperatorType *ot)
1095 ot->name= "Debug Menu";
1096 ot->idname= "WM_OT_debug_menu";
1097 ot->description= "Open a popup to set the debug level";
1099 ot->invoke= wm_debug_menu_invoke;
1100 ot->exec= wm_debug_menu_exec;
1101 ot->poll= WM_operator_winactive;
1103 RNA_def_int(ot->srna, "debug_value", 0, -10000, 10000, "Debug Value", "", INT_MIN, INT_MAX);
1107 /* ***************** Splash Screen ************************* */
1109 static void wm_block_splash_close(bContext *C, void *arg_block, void *UNUSED(arg))
1111 uiPupBlockClose(C, arg_block);
1114 static uiBlock *wm_block_create_splash(bContext *C, ARegion *ar, void *arg_unused);
1116 /* XXX: hack to refresh splash screen with updated prest menu name,
1117 * since popup blocks don't get regenerated like panels do */
1118 static void wm_block_splash_refreshmenu (bContext *UNUSED(C), void *UNUSED(arg_block), void *UNUSED(arg))
1120 /* ugh, causes crashes in other buttons, disabling for now until
1122 uiPupBlockClose(C, arg_block);
1123 uiPupBlock(C, wm_block_create_splash, NULL);
1127 static int wm_resource_check_prev(void)
1130 char *res= BLI_get_folder_version(BLENDER_RESOURCE_PATH_USER, BLENDER_VERSION, TRUE);
1132 // if(res) printf("USER: %s\n", res);
1134 #if 0 /* ignore the local folder */
1136 /* with a local dir, copying old files isnt useful since local dir get priority for config */
1137 res= BLI_get_folder_version(BLENDER_RESOURCE_PATH_LOCAL, BLENDER_VERSION, TRUE);
1141 // if(res) printf("LOCAL: %s\n", res);
1146 return (BLI_get_folder_version(BLENDER_RESOURCE_PATH_USER, BLENDER_VERSION - 1, TRUE) != NULL);
1150 static uiBlock *wm_block_create_splash(bContext *C, ARegion *ar, void *UNUSED(arg))
1154 uiLayout *layout, *split, *col;
1155 uiStyle *style= U.uistyles.first;
1156 struct RecentFile *recent;
1158 MenuType *mt= WM_menutype_find("USERPREF_MT_splash", TRUE);
1161 #ifdef NAN_BUILDINFO
1162 int ver_width, rev_width;
1163 char *version_str = NULL;
1164 char *revision_str = NULL;
1165 char version_buf[128];
1166 char revision_buf[128];
1167 extern char build_rev[];
1169 version_str = &version_buf[0];
1170 revision_str = &revision_buf[0];
1172 sprintf(version_str, "%d.%02d.%d", BLENDER_VERSION/100, BLENDER_VERSION%100, BLENDER_SUBVERSION);
1173 sprintf(revision_str, "r%s", build_rev);
1175 BLF_size(style->widgetlabel.uifont_id, style->widgetlabel.points, U.dpi);
1176 ver_width = (int)BLF_width(style->widgetlabel.uifont_id, version_str) + 5;
1177 rev_width = (int)BLF_width(style->widgetlabel.uifont_id, revision_str) + 5;
1178 #endif //NAN_BUILDINFO
1180 block= uiBeginBlock(C, ar, "_popup", UI_EMBOSS);
1181 uiBlockSetFlag(block, UI_BLOCK_KEEP_OPEN);
1183 but= uiDefBut(block, BUT_IMAGE, 0, "", 0, 10, 501, 282, NULL, 0.0, 0.0, 0, 0, "");
1184 uiButSetFunc(but, wm_block_splash_close, block, NULL);
1185 uiBlockSetFunc(block, wm_block_splash_refreshmenu, block, NULL);
1187 #ifdef NAN_BUILDINFO
1188 uiDefBut(block, LABEL, 0, version_str, 494-ver_width, 282-24, ver_width, 20, NULL, 0, 0, 0, 0, NULL);
1189 uiDefBut(block, LABEL, 0, revision_str, 494-rev_width, 282-36, rev_width, 20, NULL, 0, 0, 0, 0, NULL);
1190 #endif //NAN_BUILDINFO
1192 layout= uiBlockLayout(block, UI_LAYOUT_VERTICAL, UI_LAYOUT_PANEL, 10, 2, 480, 110, style);
1194 uiBlockSetEmboss(block, UI_EMBOSS);
1195 /* show the splash menu (containing interaction presets), using python */
1198 menu.layout= layout;
1202 // wmWindowManager *wm= CTX_wm_manager(C);
1203 // uiItemM(layout, C, "USERPREF_MT_keyconfigs", U.keyconfigstr, ICON_NONE);
1206 uiBlockSetEmboss(block, UI_EMBOSSP);
1207 uiLayoutSetOperatorContext(layout, WM_OP_EXEC_REGION_WIN);
1209 split = uiLayoutSplit(layout, 0, 0);
1210 col = uiLayoutColumn(split, 0);
1211 uiItemL(col, "Links", ICON_NONE);
1212 uiItemStringO(col, "Donations", ICON_URL, "WM_OT_url_open", "url", "http://www.blender.org/blenderorg/blender-foundation/donation-payment/");
1213 uiItemStringO(col, "Release Log", ICON_URL, "WM_OT_url_open", "url", "http://www.blender.org/development/release-logs/blender-257/");
1214 uiItemStringO(col, "Manual", ICON_URL, "WM_OT_url_open", "url", "http://wiki.blender.org/index.php/Doc:2.5/Manual");
1215 uiItemStringO(col, "Blender Website", ICON_URL, "WM_OT_url_open", "url", "http://www.blender.org/");
1216 uiItemStringO(col, "User Community", ICON_URL, "WM_OT_url_open", "url", "http://www.blender.org/community/user-community/"); //
1217 if(strcmp(STRINGIFY(BLENDER_VERSION_CYCLE), "release")==0) {
1218 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);
1221 BLI_snprintf(url, sizeof(url), "http://www.blender.org/documentation/blender_python_api_%d_%d_%d", BLENDER_VERSION/100, BLENDER_VERSION%100, BLENDER_SUBVERSION);
1223 uiItemStringO(col, "Python API Reference", ICON_URL, "WM_OT_url_open", "url", url);
1224 uiItemL(col, "", ICON_NONE);
1226 col = uiLayoutColumn(split, 0);
1228 if(wm_resource_check_prev()) {
1229 uiItemO(col, NULL, ICON_NEW, "WM_OT_copy_prev_settings");
1233 uiItemL(col, "Recent", ICON_NONE);
1234 for(recent = G.recent_files.first, i=0; (i<5) && (recent); recent = recent->next, i++) {
1235 uiItemStringO(col, BLI_path_basename(recent->filepath), ICON_FILE_BLEND, "WM_OT_open_mainfile", "filepath", recent->filepath);
1239 uiItemO(col, NULL, ICON_RECOVER_LAST, "WM_OT_recover_last_session");
1240 uiItemL(col, "", ICON_NONE);
1242 uiCenteredBoundsBlock(block, 0);
1243 uiEndBlock(C, block);
1248 static int wm_splash_invoke(bContext *C, wmOperator *UNUSED(op), wmEvent *UNUSED(event))
1250 uiPupBlock(C, wm_block_create_splash, NULL);
1252 return OPERATOR_FINISHED;
1255 static void WM_OT_splash(wmOperatorType *ot)
1257 ot->name= "Splash Screen";
1258 ot->idname= "WM_OT_splash";
1259 ot->description= "Opens a blocking popup region with release info";
1261 ot->invoke= wm_splash_invoke;
1262 ot->poll= WM_operator_winactive;
1266 /* ***************** Search menu ************************* */
1267 static void operator_call_cb(struct bContext *C, void *UNUSED(arg1), void *arg2)
1269 wmOperatorType *ot= arg2;
1272 WM_operator_name_call(C, ot->idname, WM_OP_INVOKE_DEFAULT, NULL);
1275 static void operator_search_cb(const struct bContext *C, void *UNUSED(arg), const char *str, uiSearchItems *items)
1277 wmOperatorType *ot = WM_operatortype_first();
1279 for(; ot; ot= ot->next) {
1281 if(BLI_strcasestr(ot->name, str)) {
1282 if(WM_operator_poll((bContext*)C, ot)) {
1284 int len= strlen(ot->name);
1286 /* display name for menu, can hold hotkey */
1287 BLI_strncpy(name, ot->name, 256);
1289 /* check for hotkey */
1291 if(WM_key_event_operator_string(C, ot->idname, WM_OP_EXEC_DEFAULT, NULL, &name[len+1], 256-len-1))
1295 if(0==uiSearchItemAdd(items, name, ot, 0))
1302 static uiBlock *wm_block_search_menu(bContext *C, ARegion *ar, void *UNUSED(arg_op))
1304 static char search[256]= "";
1306 wmWindow *win= CTX_wm_window(C);
1310 block= uiBeginBlock(C, ar, "_popup", UI_EMBOSS);
1311 uiBlockSetFlag(block, UI_BLOCK_LOOP|UI_BLOCK_RET_1|UI_BLOCK_MOVEMOUSE_QUIT);
1313 but= uiDefSearchBut(block, search, 0, ICON_VIEWZOOM, 256, 10, 10, 180, 19, 0, 0, "");
1314 uiButSetSearchFunc(but, operator_search_cb, NULL, operator_call_cb, NULL);
1316 /* fake button, it holds space for search items */
1317 uiDefBut(block, LABEL, 0, "", 10, 10 - uiSearchBoxhHeight(), 180, uiSearchBoxhHeight(), NULL, 0, 0, 0, 0, NULL);
1319 uiPopupBoundsBlock(block, 6, 0, -20); /* move it downwards, mouse over button */
1320 uiEndBlock(C, block);
1322 event= *(win->eventstate); /* XXX huh huh? make api call */
1323 event.type= EVT_BUT_OPEN;
1324 event.val= KM_PRESS;
1325 event.customdata= but;
1326 event.customdatafree= FALSE;
1327 wm_event_add(win, &event);
1332 static int wm_search_menu_exec(bContext *UNUSED(C), wmOperator *UNUSED(op))
1334 return OPERATOR_FINISHED;
1337 static int wm_search_menu_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
1339 uiPupBlock(C, wm_block_search_menu, op);
1341 return OPERATOR_CANCELLED;
1345 static int wm_search_menu_poll(bContext *C)
1347 if(CTX_wm_window(C)==NULL) {
1351 ScrArea *sa= CTX_wm_area(C);
1353 if(sa->spacetype==SPACE_CONSOLE) return 0; // XXX - so we can use the shortcut in the console
1354 if(sa->spacetype==SPACE_TEXT) return 0; // XXX - so we can use the spacebar in the text editor
1357 Object *editob= CTX_data_edit_object(C);
1358 if(editob && editob->type==OB_FONT) return 0; // XXX - so we can use the spacebar for entering text
1364 static void WM_OT_search_menu(wmOperatorType *ot)
1366 ot->name= "Search Menu";
1367 ot->idname= "WM_OT_search_menu";
1369 ot->invoke= wm_search_menu_invoke;
1370 ot->exec= wm_search_menu_exec;
1371 ot->poll= wm_search_menu_poll;
1374 static int wm_call_menu_exec(bContext *C, wmOperator *op)
1376 char idname[BKE_ST_MAXNAME];
1377 RNA_string_get(op->ptr, "name", idname);
1379 uiPupMenuInvoke(C, idname);
1381 return OPERATOR_CANCELLED;
1384 static void WM_OT_call_menu(wmOperatorType *ot)
1386 ot->name= "Call Menu";
1387 ot->idname= "WM_OT_call_menu";
1389 ot->exec= wm_call_menu_exec;
1390 ot->poll= WM_operator_winactive;
1392 RNA_def_string(ot->srna, "name", "", BKE_ST_MAXNAME, "Name", "Name of the menu");
1395 /* ************ window / screen operator definitions ************** */
1397 /* this poll functions is needed in place of WM_operator_winactive
1398 * while it crashes on full screen */
1399 static int wm_operator_winactive_normal(bContext *C)
1401 wmWindow *win= CTX_wm_window(C);
1403 if(win==NULL || win->screen==NULL || win->screen->full != SCREENNORMAL)
1409 static void WM_OT_window_duplicate(wmOperatorType *ot)
1411 ot->name= "Duplicate Window";
1412 ot->idname= "WM_OT_window_duplicate";
1413 ot->description="Duplicate the current Blender window";
1415 ot->exec= wm_window_duplicate_exec;
1416 ot->poll= wm_operator_winactive_normal;
1419 static void WM_OT_save_homefile(wmOperatorType *ot)
1421 ot->name= "Save User Settings";
1422 ot->idname= "WM_OT_save_homefile";
1423 ot->description="Make the current file the default .blend file";
1425 ot->invoke= WM_operator_confirm;
1426 ot->exec= WM_write_homefile;
1427 ot->poll= WM_operator_winactive;
1430 static void WM_OT_read_homefile(wmOperatorType *ot)
1432 ot->name= "Reload Start-Up File";
1433 ot->idname= "WM_OT_read_homefile";
1434 ot->description="Open the default file (doesn't save the current file)";
1436 ot->invoke= WM_operator_confirm;
1437 ot->exec= WM_read_homefile_exec;
1438 /* ommit poll to run in background mode */
1441 static void WM_OT_read_factory_settings(wmOperatorType *ot)
1443 ot->name= "Load Factory Settings";
1444 ot->idname= "WM_OT_read_factory_settings";
1445 ot->description="Load default file and user preferences";
1447 ot->invoke= WM_operator_confirm;
1448 ot->exec= WM_read_homefile_exec;
1449 /* ommit poll to run in background mode */
1452 /* *************** open file **************** */
1454 static void open_set_load_ui(wmOperator *op)
1456 if(!RNA_property_is_set(op->ptr, "load_ui"))
1457 RNA_boolean_set(op->ptr, "load_ui", !(U.flag & USER_FILENOUI));
1460 static void open_set_use_scripts(wmOperator *op)
1462 if(!RNA_property_is_set(op->ptr, "use_scripts")) {
1463 /* use G_SCRIPT_AUTOEXEC rather then the userpref because this means if
1464 * the flag has been disabled from the command line, then opening
1465 * from the menu wont enable this setting. */
1466 RNA_boolean_set(op->ptr, "use_scripts", (G.f & G_SCRIPT_AUTOEXEC));
1470 static int wm_open_mainfile_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
1472 const char *openname= G.main->name;
1474 /* if possible, get the name of the most recently used .blend file */
1475 if (G.recent_files.first) {
1476 struct RecentFile *recent = G.recent_files.first;
1477 openname = recent->filepath;
1480 RNA_string_set(op->ptr, "filepath", openname);
1481 open_set_load_ui(op);
1482 open_set_use_scripts(op);
1484 WM_event_add_fileselect(C, op);
1486 return OPERATOR_RUNNING_MODAL;
1489 static int wm_open_mainfile_exec(bContext *C, wmOperator *op)
1491 char path[FILE_MAX];
1493 RNA_string_get(op->ptr, "filepath", path);
1494 open_set_load_ui(op);
1495 open_set_use_scripts(op);
1497 if(RNA_boolean_get(op->ptr, "load_ui"))
1498 G.fileflags &= ~G_FILE_NO_UI;
1500 G.fileflags |= G_FILE_NO_UI;
1502 if(RNA_boolean_get(op->ptr, "use_scripts"))
1503 G.f |= G_SCRIPT_AUTOEXEC;
1505 G.f &= ~G_SCRIPT_AUTOEXEC;
1507 // XXX wm in context is not set correctly after WM_read_file -> crash
1508 // do it before for now, but is this correct with multiple windows?
1509 WM_event_add_notifier(C, NC_WINDOW, NULL);
1511 WM_read_file(C, path, op->reports);
1513 return OPERATOR_FINISHED;
1516 static void WM_OT_open_mainfile(wmOperatorType *ot)
1518 ot->name= "Open Blender File";
1519 ot->idname= "WM_OT_open_mainfile";
1520 ot->description="Open a Blender file";
1522 ot->invoke= wm_open_mainfile_invoke;
1523 ot->exec= wm_open_mainfile_exec;
1524 ot->poll= WM_operator_winactive;
1526 WM_operator_properties_filesel(ot, FOLDERFILE|BLENDERFILE, FILE_BLENDER, FILE_OPENFILE, WM_FILESEL_FILEPATH);
1528 RNA_def_boolean(ot->srna, "load_ui", 1, "Load UI", "Load user interface setup in the .blend file");
1529 RNA_def_boolean(ot->srna, "use_scripts", 1, "Trusted Source", "Allow blend file execute scripts automatically, default available from system preferences");
1532 /* **************** link/append *************** */
1534 static int wm_link_append_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
1536 if(!RNA_property_is_set(op->ptr, "relative_path"))
1537 RNA_boolean_set(op->ptr, "relative_path", U.flag & USER_RELPATHS);
1539 if(RNA_property_is_set(op->ptr, "filepath")) {
1540 return WM_operator_call(C, op);
1543 /* XXX TODO solve where to get last linked library from */
1544 RNA_string_set(op->ptr, "filepath", G.lib);
1545 WM_event_add_fileselect(C, op);
1546 return OPERATOR_RUNNING_MODAL;
1550 static short wm_link_append_flag(wmOperator *op)
1554 if(RNA_boolean_get(op->ptr, "autoselect")) flag |= FILE_AUTOSELECT;
1555 if(RNA_boolean_get(op->ptr, "active_layer")) flag |= FILE_ACTIVELAY;
1556 if(RNA_boolean_get(op->ptr, "relative_path")) flag |= FILE_RELPATH;
1557 if(RNA_boolean_get(op->ptr, "link")) flag |= FILE_LINK;
1558 if(RNA_boolean_get(op->ptr, "instance_groups")) flag |= FILE_GROUP_INSTANCE;
1563 static int wm_link_append_exec(bContext *C, wmOperator *op)
1565 Main *bmain= CTX_data_main(C);
1566 Scene *scene= CTX_data_scene(C);
1570 char name[FILE_MAX], dir[FILE_MAX], libname[FILE_MAX], group[GROUP_MAX];
1571 int idcode, totfiles=0;
1575 RNA_string_get(op->ptr, "filename", name);
1576 RNA_string_get(op->ptr, "directory", dir);
1578 /* test if we have a valid data */
1579 if(BLO_is_a_library(dir, libname, group) == 0) {
1580 BKE_report(op->reports, RPT_ERROR, "Not a library");
1581 return OPERATOR_CANCELLED;
1583 else if(group[0] == 0) {
1584 BKE_report(op->reports, RPT_ERROR, "Nothing indicated");
1585 return OPERATOR_CANCELLED;
1587 else if(BLI_path_cmp(bmain->name, libname) == 0) {
1588 BKE_report(op->reports, RPT_ERROR, "Cannot use current file as library");
1589 return OPERATOR_CANCELLED;
1592 /* check if something is indicated for append/link */
1593 prop = RNA_struct_find_property(op->ptr, "files");
1595 totfiles= RNA_property_collection_length(op->ptr, prop);
1597 if(name[0] == '\0') {
1598 BKE_report(op->reports, RPT_ERROR, "Nothing indicated");
1599 return OPERATOR_CANCELLED;
1603 else if(name[0] == '\0') {
1604 BKE_report(op->reports, RPT_ERROR, "Nothing indicated");
1605 return OPERATOR_CANCELLED;
1608 bh = BLO_blendhandle_from_file(libname, op->reports);
1611 /* unlikely since we just browsed it, but possible
1612 * error reports will have been made by BLO_blendhandle_from_file() */
1613 return OPERATOR_CANCELLED;
1617 /* from here down, no error returns */
1619 idcode = BKE_idcode_from_name(group);
1621 /* now we have or selected, or an indicated file */
1622 if(RNA_boolean_get(op->ptr, "autoselect"))
1623 scene_deselect_all(scene);
1626 flag = wm_link_append_flag(op);
1628 /* sanity checks for flag */
1629 if(scene->id.lib && (flag & FILE_GROUP_INSTANCE)) {
1630 /* TODO, user never gets this message */
1631 BKE_reportf(op->reports, RPT_WARNING, "Scene '%s' is linked, group instance disabled", scene->id.name+2);
1632 flag &= ~FILE_GROUP_INSTANCE;
1636 /* tag everything, all untagged data can be made local
1637 * its also generally useful to know what is new
1639 * take extra care flag_all_listbases_ids(LIB_LINK_TAG, 0) is called after! */
1640 flag_all_listbases_ids(LIB_PRE_EXISTING, 1);
1642 /* here appending/linking starts */
1643 mainl = BLO_library_append_begin(C, &bh, libname);
1645 BLO_library_append_named_part(C, mainl, &bh, name, idcode, flag);
1648 RNA_BEGIN(op->ptr, itemptr, "files") {
1649 RNA_string_get(&itemptr, "name", name);
1650 BLO_library_append_named_part(C, mainl, &bh, name, idcode, flag);
1654 BLO_library_append_end(C, mainl, &bh, idcode, flag);
1656 /* mark all library linked objects to be updated */
1657 recalc_all_library_objects(bmain);
1659 /* append, rather than linking */
1660 if((flag & FILE_LINK)==0) {
1661 Library *lib= BLI_findstring(&bmain->library, libname, offsetof(Library, filepath));
1662 if(lib) all_local(lib, 1);
1663 else BLI_assert(!"cant find name of just added library!");
1666 /* important we unset, otherwise these object wont
1667 * link into other scenes from this blend file */
1668 flag_all_listbases_ids(LIB_PRE_EXISTING, 0);
1670 /* recreate dependency graph to include new objects */
1671 DAG_scene_sort(bmain, scene);
1672 DAG_ids_flush_update(bmain, 0);
1674 BLO_blendhandle_close(bh);
1676 /* XXX TODO: align G.lib with other directory storage (like last opened image etc...) */
1677 BLI_strncpy(G.lib, dir, FILE_MAX);
1679 WM_event_add_notifier(C, NC_WINDOW, NULL);
1681 return OPERATOR_FINISHED;
1684 static void WM_OT_link_append(wmOperatorType *ot)
1686 ot->name= "Link/Append from Library";
1687 ot->idname= "WM_OT_link_append";
1688 ot->description= "Link or Append from a Library .blend file";
1690 ot->invoke= wm_link_append_invoke;
1691 ot->exec= wm_link_append_exec;
1692 ot->poll= WM_operator_winactive;
1694 ot->flag |= OPTYPE_UNDO;
1696 WM_operator_properties_filesel(ot, FOLDERFILE|BLENDERFILE, FILE_LOADLIB, FILE_OPENFILE, WM_FILESEL_FILEPATH|WM_FILESEL_DIRECTORY|WM_FILESEL_FILENAME| WM_FILESEL_RELPATH);
1698 RNA_def_boolean(ot->srna, "link", 1, "Link", "Link the objects or datablocks rather than appending");
1699 RNA_def_boolean(ot->srna, "autoselect", 1, "Select", "Select the linked objects");
1700 RNA_def_boolean(ot->srna, "active_layer", 1, "Active Layer", "Put the linked objects on the active layer");
1701 RNA_def_boolean(ot->srna, "instance_groups", 1, "Instance Groups", "Create instances for each group as a DupliGroup");
1703 RNA_def_collection_runtime(ot->srna, "files", &RNA_OperatorFileListElement, "Files", "");
1706 /* *************** recover last session **************** */
1708 static int wm_recover_last_session_exec(bContext *C, wmOperator *op)
1710 char filename[FILE_MAX];
1712 G.fileflags |= G_FILE_RECOVER;
1714 // XXX wm in context is not set correctly after WM_read_file -> crash
1715 // do it before for now, but is this correct with multiple windows?
1716 WM_event_add_notifier(C, NC_WINDOW, NULL);
1719 BLI_make_file_string("/", filename, btempdir, "quit.blend");
1720 WM_read_file(C, filename, op->reports);
1722 G.fileflags &= ~G_FILE_RECOVER;
1723 return OPERATOR_FINISHED;
1726 static void WM_OT_recover_last_session(wmOperatorType *ot)
1728 ot->name= "Recover Last Session";
1729 ot->idname= "WM_OT_recover_last_session";
1730 ot->description="Open the last closed file (\"quit.blend\")";
1732 ot->exec= wm_recover_last_session_exec;
1733 ot->poll= WM_operator_winactive;
1736 /* *************** recover auto save **************** */
1738 static int wm_recover_auto_save_exec(bContext *C, wmOperator *op)
1740 char path[FILE_MAX];
1742 RNA_string_get(op->ptr, "filepath", path);
1744 G.fileflags |= G_FILE_RECOVER;
1746 // XXX wm in context is not set correctly after WM_read_file -> crash
1747 // do it before for now, but is this correct with multiple windows?
1748 WM_event_add_notifier(C, NC_WINDOW, NULL);
1751 WM_read_file(C, path, op->reports);
1753 G.fileflags &= ~G_FILE_RECOVER;
1755 return OPERATOR_FINISHED;
1758 static int wm_recover_auto_save_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
1760 char filename[FILE_MAX];
1762 wm_autosave_location(filename);
1763 RNA_string_set(op->ptr, "filepath", filename);
1764 WM_event_add_fileselect(C, op);
1766 return OPERATOR_RUNNING_MODAL;
1769 static void WM_OT_recover_auto_save(wmOperatorType *ot)
1771 ot->name= "Recover Auto Save";
1772 ot->idname= "WM_OT_recover_auto_save";
1773 ot->description="Open an automatically saved file to recover it";
1775 ot->exec= wm_recover_auto_save_exec;
1776 ot->invoke= wm_recover_auto_save_invoke;
1777 ot->poll= WM_operator_winactive;
1779 WM_operator_properties_filesel(ot, BLENDERFILE, FILE_BLENDER, FILE_OPENFILE, WM_FILESEL_FILEPATH);
1782 /* *************** save file as **************** */
1784 static void untitled(char *name)
1786 if(G.save_over == 0 && strlen(name) < FILE_MAX-16) {
1787 char *c= BLI_last_slash(name);
1790 strcpy(&c[1], "untitled.blend");
1792 strcpy(name, "untitled.blend");
1796 static void save_set_compress(wmOperator *op)
1798 if(!RNA_property_is_set(op->ptr, "compress")) {
1799 if(G.save_over) /* keep flag for existing file */
1800 RNA_boolean_set(op->ptr, "compress", G.fileflags & G_FILE_COMPRESS);
1801 else /* use userdef for new file */
1802 RNA_boolean_set(op->ptr, "compress", U.flag & USER_FILECOMPRESS);
1806 static int wm_save_as_mainfile_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
1808 char name[FILE_MAX];
1810 save_set_compress(op);
1812 /* if not saved before, get the name of the most recently used .blend file */
1813 if(G.main->name[0]==0 && G.recent_files.first) {
1814 struct RecentFile *recent = G.recent_files.first;
1815 BLI_strncpy(name, recent->filepath, FILE_MAX);
1818 BLI_strncpy(name, G.main->name, FILE_MAX);
1821 RNA_string_set(op->ptr, "filepath", name);
1823 WM_event_add_fileselect(C, op);
1825 return OPERATOR_RUNNING_MODAL;
1828 /* function used for WM_OT_save_mainfile too */
1829 static int wm_save_as_mainfile_exec(bContext *C, wmOperator *op)
1831 char path[FILE_MAX];
1835 save_set_compress(op);
1837 if(RNA_property_is_set(op->ptr, "filepath"))
1838 RNA_string_get(op->ptr, "filepath", path);
1840 BLI_strncpy(path, G.main->name, FILE_MAX);
1844 if(RNA_property_is_set(op->ptr, "copy"))
1845 copy = RNA_boolean_get(op->ptr, "copy");
1847 fileflags= G.fileflags;
1849 /* set compression flag */
1850 if(RNA_boolean_get(op->ptr, "compress")) fileflags |= G_FILE_COMPRESS;
1851 else fileflags &= ~G_FILE_COMPRESS;
1852 if(RNA_boolean_get(op->ptr, "relative_remap")) fileflags |= G_FILE_RELATIVE_REMAP;
1853 else fileflags &= ~G_FILE_RELATIVE_REMAP;
1855 if ( WM_write_file(C, path, fileflags, op->reports, copy) != 0)
1856 return OPERATOR_CANCELLED;
1858 WM_event_add_notifier(C, NC_WM|ND_FILESAVE, NULL);
1860 return OPERATOR_FINISHED;
1863 /* function used for WM_OT_save_mainfile too */
1864 static int blend_save_check(bContext *UNUSED(C), wmOperator *op)
1866 char filepath[FILE_MAX];
1867 RNA_string_get(op->ptr, "filepath", filepath);
1868 if(BLI_replace_extension(filepath, sizeof(filepath), ".blend")) {
1869 RNA_string_set(op->ptr, "filepath", filepath);
1875 static void WM_OT_save_as_mainfile(wmOperatorType *ot)
1877 ot->name= "Save As Blender File";
1878 ot->idname= "WM_OT_save_as_mainfile";
1879 ot->description="Save the current file in the desired location";
1881 ot->invoke= wm_save_as_mainfile_invoke;
1882 ot->exec= wm_save_as_mainfile_exec;
1883 ot->check= blend_save_check;
1884 /* ommit window poll so this can work in background mode */
1886 WM_operator_properties_filesel(ot, FOLDERFILE|BLENDERFILE, FILE_BLENDER, FILE_SAVE, WM_FILESEL_FILEPATH);
1887 RNA_def_boolean(ot->srna, "compress", 0, "Compress", "Write compressed .blend file");
1888 RNA_def_boolean(ot->srna, "relative_remap", 1, "Remap Relative", "Remap relative paths when saving in a different directory");
1889 RNA_def_boolean(ot->srna, "copy", 0, "Save Copy", "Save a copy of the actual working state but does not make saved file active.");
1892 /* *************** save file directly ******** */
1894 static int wm_save_mainfile_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
1896 char name[FILE_MAX];
1897 int check_existing=1;
1899 /* cancel if no active window */
1900 if (CTX_wm_window(C) == NULL)
1901 return OPERATOR_CANCELLED;
1903 save_set_compress(op);
1905 /* if not saved before, get the name of the most recently used .blend file */
1906 if(G.main->name[0]==0 && G.recent_files.first) {
1907 struct RecentFile *recent = G.recent_files.first;
1908 BLI_strncpy(name, recent->filepath, FILE_MAX);
1911 BLI_strncpy(name, G.main->name, FILE_MAX);
1915 RNA_string_set(op->ptr, "filepath", name);
1917 if (RNA_struct_find_property(op->ptr, "check_existing"))
1918 if (RNA_boolean_get(op->ptr, "check_existing")==0)
1923 uiPupMenuSaveOver(C, op, name);
1925 wm_save_as_mainfile_exec(C, op);
1928 WM_event_add_fileselect(C, op);
1931 return OPERATOR_RUNNING_MODAL;
1934 static void WM_OT_save_mainfile(wmOperatorType *ot)
1936 ot->name= "Save Blender File";
1937 ot->idname= "WM_OT_save_mainfile";
1938 ot->description="Save the current Blender file";
1940 ot->invoke= wm_save_mainfile_invoke;
1941 ot->exec= wm_save_as_mainfile_exec;
1942 ot->check= blend_save_check;
1945 WM_operator_properties_filesel(ot, FOLDERFILE|BLENDERFILE, FILE_BLENDER, FILE_SAVE, WM_FILESEL_FILEPATH);
1946 RNA_def_boolean(ot->srna, "compress", 0, "Compress", "Write compressed .blend file");
1947 RNA_def_boolean(ot->srna, "relative_remap", 0, "Remap Relative", "Remap relative paths when saving in a different directory");
1950 /* XXX: move these collada operators to a more appropriate place */
1953 #include "../../collada/collada.h"
1955 static int wm_collada_export_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
1957 if(!RNA_property_is_set(op->ptr, "filepath")) {
1958 char filepath[FILE_MAX];
1959 BLI_strncpy(filepath, G.main->name, sizeof(filepath));
1960 BLI_replace_extension(filepath, sizeof(filepath), ".dae");
1961 RNA_string_set(op->ptr, "filepath", filepath);
1964 WM_event_add_fileselect(C, op);
1966 return OPERATOR_RUNNING_MODAL;
1969 /* function used for WM_OT_save_mainfile too */
1970 static int wm_collada_export_exec(bContext *C, wmOperator *op)
1972 char filename[FILE_MAX];
1974 if(!RNA_property_is_set(op->ptr, "filepath")) {
1975 BKE_report(op->reports, RPT_ERROR, "No filename given");
1976 return OPERATOR_CANCELLED;
1979 RNA_string_get(op->ptr, "filepath", filename);
1980 if(collada_export(CTX_data_scene(C), filename)) {
1981 return OPERATOR_FINISHED;
1984 return OPERATOR_CANCELLED;
1988 static void WM_OT_collada_export(wmOperatorType *ot)
1990 ot->name= "Export COLLADA";
1991 ot->idname= "WM_OT_collada_export";
1993 ot->invoke= wm_collada_export_invoke;
1994 ot->exec= wm_collada_export_exec;
1995 ot->poll= WM_operator_winactive;
1997 WM_operator_properties_filesel(ot, FOLDERFILE|COLLADAFILE, FILE_BLENDER, FILE_SAVE, WM_FILESEL_FILEPATH);
2000 /* function used for WM_OT_save_mainfile too */
2001 static int wm_collada_import_exec(bContext *C, wmOperator *op)
2003 char filename[FILE_MAX];
2005 if(!RNA_property_is_set(op->ptr, "filepath")) {
2006 BKE_report(op->reports, RPT_ERROR, "No filename given");
2007 return OPERATOR_CANCELLED;
2010 RNA_string_get(op->ptr, "filepath", filename);
2011 collada_import(C, filename);
2013 return OPERATOR_FINISHED;
2016 static void WM_OT_collada_import(wmOperatorType *ot)
2018 ot->name= "Import COLLADA";
2019 ot->idname= "WM_OT_collada_import";
2021 ot->invoke= WM_operator_filesel;
2022 ot->exec= wm_collada_import_exec;
2023 ot->poll= WM_operator_winactive;
2025 WM_operator_properties_filesel(ot, FOLDERFILE|COLLADAFILE, FILE_BLENDER, FILE_OPENFILE, WM_FILESEL_FILEPATH);
2031 /* *********************** */
2033 static void WM_OT_window_fullscreen_toggle(wmOperatorType *ot)
2035 ot->name= "Toggle Fullscreen";
2036 ot->idname= "WM_OT_window_fullscreen_toggle";
2037 ot->description="Toggle the current window fullscreen";
2039 ot->exec= wm_window_fullscreen_toggle_exec;
2040 ot->poll= WM_operator_winactive;
2043 static int wm_exit_blender_op(bContext *C, wmOperator *op)
2045 WM_operator_free(op);
2049 return OPERATOR_FINISHED;
2052 static void WM_OT_quit_blender(wmOperatorType *ot)
2054 ot->name= "Quit Blender";
2055 ot->idname= "WM_OT_quit_blender";
2056 ot->description= "Quit Blender";
2058 ot->invoke= WM_operator_confirm;
2059 ot->exec= wm_exit_blender_op;
2060 ot->poll= WM_operator_winactive;
2063 /* *********************** */
2067 static int wm_console_toggle_op(bContext *UNUSED(C), wmOperator *UNUSED(op))
2069 GHOST_toggleConsole(2);
2070 return OPERATOR_FINISHED;
2073 static void WM_OT_console_toggle(wmOperatorType *ot)
2075 ot->name= "Toggle System Console";
2076 ot->idname= "WM_OT_console_toggle";
2077 ot->description= "Toggle System Console";
2079 ot->exec= wm_console_toggle_op;
2080 ot->poll= WM_operator_winactive;
2085 /* ************ default paint cursors, draw always around cursor *********** */
2087 - returns handler to free
2088 - poll(bContext): returns 1 if draw should happen
2089 - draw(bContext): drawing callback for paint cursor
2092 void *WM_paint_cursor_activate(wmWindowManager *wm, int (*poll)(bContext *C),
2093 wmPaintCursorDraw draw, void *customdata)
2095 wmPaintCursor *pc= MEM_callocN(sizeof(wmPaintCursor), "paint cursor");
2097 BLI_addtail(&wm->paintcursors, pc);
2099 pc->customdata = customdata;
2106 void WM_paint_cursor_end(wmWindowManager *wm, void *handle)
2110 for(pc= wm->paintcursors.first; pc; pc= pc->next) {
2111 if(pc == (wmPaintCursor *)handle) {
2112 BLI_remlink(&wm->paintcursors, pc);
2119 /* ************ window gesture operator-callback definitions ************** */
2121 * These are default callbacks for use in operators requiring gesture input
2124 /* **************** Border gesture *************** */
2126 /* Border gesture has two types:
2127 1) WM_GESTURE_CROSS_RECT: starts a cross, on mouse click it changes to border
2128 2) WM_GESTURE_RECT: starts immediate as a border, on mouse click or release it ends
2130 It stores 4 values (xmin, xmax, ymin, ymax) and event it ended with (event_type)
2133 static int border_apply_rect(wmOperator *op)
2135 wmGesture *gesture= op->customdata;
2136 rcti *rect= gesture->customdata;
2138 if(rect->xmin==rect->xmax || rect->ymin==rect->ymax)
2142 /* operator arguments and storage. */
2143 RNA_int_set(op->ptr, "xmin", MIN2(rect->xmin, rect->xmax) );
2144 RNA_int_set(op->ptr, "ymin", MIN2(rect->ymin, rect->ymax) );
2145 RNA_int_set(op->ptr, "xmax", MAX2(rect->xmin, rect->xmax) );
2146 RNA_int_set(op->ptr, "ymax", MAX2(rect->ymin, rect->ymax) );
2151 static int border_apply(bContext *C, wmOperator *op, int gesture_mode)
2153 if (!border_apply_rect(op))
2156 /* XXX weak; border should be configured for this without reading event types */
2157 if( RNA_struct_find_property(op->ptr, "gesture_mode") )
2158 RNA_int_set(op->ptr, "gesture_mode", gesture_mode);
2160 op->type->exec(C, op);
2164 static void wm_gesture_end(bContext *C, wmOperator *op)
2166 wmGesture *gesture= op->customdata;
2168 WM_gesture_end(C, gesture); /* frees gesture itself, and unregisters from window */
2169 op->customdata= NULL;
2171 ED_area_tag_redraw(CTX_wm_area(C));
2173 if( RNA_struct_find_property(op->ptr, "cursor") )
2174 WM_cursor_restore(CTX_wm_window(C));
2177 int WM_border_select_invoke(bContext *C, wmOperator *op, wmEvent *event)
2179 if(ISTWEAK(event->type))
2180 op->customdata= WM_gesture_new(C, event, WM_GESTURE_RECT);
2182 op->customdata= WM_gesture_new(C, event, WM_GESTURE_CROSS_RECT);
2184 /* add modal handler */
2185 WM_event_add_modal_handler(C, op);
2187 wm_gesture_tag_redraw(C);
2189 return OPERATOR_RUNNING_MODAL;
2192 int WM_border_select_modal(bContext *C, wmOperator *op, wmEvent *event)
2194 wmGesture *gesture= op->customdata;
2195 rcti *rect= gesture->customdata;
2198 if(event->type== MOUSEMOVE) {
2199 wm_subwindow_getorigin(CTX_wm_window(C), gesture->swinid, &sx, &sy);
2201 if(gesture->type==WM_GESTURE_CROSS_RECT && gesture->mode==0) {
2202 rect->xmin= rect->xmax= event->x - sx;
2203 rect->ymin= rect->ymax= event->y - sy;
2206 rect->xmax= event->x - sx;
2207 rect->ymax= event->y - sy;
2209 border_apply_rect(op);
2211 wm_gesture_tag_redraw(C);
2213 else if (event->type==EVT_MODAL_MAP) {
2214 switch (event->val) {
2215 case GESTURE_MODAL_BEGIN:
2216 if(gesture->type==WM_GESTURE_CROSS_RECT && gesture->mode==0) {
2218 wm_gesture_tag_redraw(C);
2221 case GESTURE_MODAL_SELECT:
2222 case GESTURE_MODAL_DESELECT:
2223 case GESTURE_MODAL_IN:
2224 case GESTURE_MODAL_OUT:
2225 if(border_apply(C, op, event->val)) {
2226 wm_gesture_end(C, op);
2227 return OPERATOR_FINISHED;
2229 wm_gesture_end(C, op);
2230 return OPERATOR_CANCELLED;
2233 case GESTURE_MODAL_CANCEL:
2234 wm_gesture_end(C, op);
2235 return OPERATOR_CANCELLED;
2239 // // Allow view navigation???
2241 // return OPERATOR_PASS_THROUGH;
2244 return OPERATOR_RUNNING_MODAL;
2247 /* **************** circle gesture *************** */
2248 /* works now only for selection or modal paint stuff, calls exec while hold mouse, exit on release */
2250 #ifdef GESTURE_MEMORY
2251 int circle_select_size= 25; // XXX - need some operator memory thing\!
2254 int WM_gesture_circle_invoke(bContext *C, wmOperator *op, wmEvent *event)
2256 op->customdata= WM_gesture_new(C, event, WM_GESTURE_CIRCLE);
2258 /* add modal handler */
2259 WM_event_add_modal_handler(C, op);
2261 wm_gesture_tag_redraw(C);
2263 return OPERATOR_RUNNING_MODAL;
2266 static void gesture_circle_apply(bContext *C, wmOperator *op)
2268 wmGesture *gesture= op->customdata;
2269 rcti *rect= gesture->customdata;
2271 if(RNA_int_get(op->ptr, "gesture_mode")==GESTURE_MODAL_NOP)
2274 /* operator arguments and storage. */
2275 RNA_int_set(op->ptr, "x", rect->xmin);
2276 RNA_int_set(op->ptr, "y", rect->ymin);
2277 RNA_int_set(op->ptr, "radius", rect->xmax);
2280 op->type->exec(C, op);
2282 #ifdef GESTURE_MEMORY
2283 circle_select_size= rect->xmax;
2287 int WM_gesture_circle_modal(bContext *C, wmOperator *op, wmEvent *event)
2289 wmGesture *gesture= op->customdata;
2290 rcti *rect= gesture->customdata;
2293 if(event->type== MOUSEMOVE) {
2294 wm_subwindow_getorigin(CTX_wm_window(C), gesture->swinid, &sx, &sy);
2296 rect->xmin= event->x - sx;
2297 rect->ymin= event->y - sy;
2299 wm_gesture_tag_redraw(C);
2302 gesture_circle_apply(C, op);
2304 else if (event->type==EVT_MODAL_MAP) {
2305 switch (event->val) {
2306 case GESTURE_MODAL_CIRCLE_ADD:
2307 rect->xmax += 2 + rect->xmax/10;
2308 wm_gesture_tag_redraw(C);
2310 case GESTURE_MODAL_CIRCLE_SUB:
2311 rect->xmax -= 2 + rect->xmax/10;
2312 if(rect->xmax < 1) rect->xmax= 1;
2313 wm_gesture_tag_redraw(C);
2315 case GESTURE_MODAL_SELECT:
2316 case GESTURE_MODAL_DESELECT:
2317 case GESTURE_MODAL_NOP:
2318 if(RNA_struct_find_property(op->ptr, "gesture_mode"))
2319 RNA_int_set(op->ptr, "gesture_mode", event->val);
2321 if(event->val != GESTURE_MODAL_NOP) {
2322 /* apply first click */
2323 gesture_circle_apply(C, op);
2325 wm_gesture_tag_redraw(C);
2329 case GESTURE_MODAL_CANCEL:
2330 case GESTURE_MODAL_CONFIRM:
2331 wm_gesture_end(C, op);
2332 return OPERATOR_FINISHED; /* use finish or we dont get an undo */
2335 // // Allow view navigation???
2337 // return OPERATOR_PASS_THROUGH;
2340 return OPERATOR_RUNNING_MODAL;
2344 /* template to copy from */
2345 void WM_OT_circle_gesture(wmOperatorType *ot)
2347 ot->name= "Circle Gesture";
2348 ot->idname= "WM_OT_circle_gesture";
2349 ot->description="Enter rotate mode with a circular gesture";
2351 ot->invoke= WM_gesture_circle_invoke;
2352 ot->modal= WM_gesture_circle_modal;
2354 ot->poll= WM_operator_winactive;
2356 RNA_def_property(ot->srna, "x", PROP_INT, PROP_NONE);
2357 RNA_def_property(ot->srna, "y", PROP_INT, PROP_NONE);
2358 RNA_def_property(ot->srna, "radius", PROP_INT, PROP_NONE);
2363 /* **************** Tweak gesture *************** */
2365 static void tweak_gesture_modal(bContext *C, wmEvent *event)
2367 wmWindow *window= CTX_wm_window(C);
2368 wmGesture *gesture= window->tweak;
2369 rcti *rect= gesture->customdata;
2372 switch(event->type) {
2374 case INBETWEEN_MOUSEMOVE:
2376 wm_subwindow_getorigin(window, gesture->swinid, &sx, &sy);
2378 rect->xmax= event->x - sx;
2379 rect->ymax= event->y - sy;
2381 if((val= wm_gesture_evaluate(gesture))) {
2384 tevent= *(window->eventstate);
2385 if(gesture->event_type==LEFTMOUSE)
2386 tevent.type= EVT_TWEAK_L;
2387 else if(gesture->event_type==RIGHTMOUSE)
2388 tevent.type= EVT_TWEAK_R;
2390 tevent.type= EVT_TWEAK_M;
2393 wm_event_add(window, &tevent);
2395 WM_gesture_end(C, gesture); /* frees gesture itself, and unregisters from window */
2403 if(gesture->event_type==event->type) {
2404 WM_gesture_end(C, gesture);
2406 /* when tweak fails we should give the other keymap entries a chance */
2407 event->val= KM_RELEASE;
2411 if(!ISTIMER(event->type)) {
2412 WM_gesture_end(C, gesture);
2418 /* standard tweak, called after window handlers passed on event */
2419 void wm_tweakevent_test(bContext *C, wmEvent *event, int action)
2421 wmWindow *win= CTX_wm_window(C);
2423 if(win->tweak==NULL) {
2424 if(CTX_wm_region(C)) {
2425 if(event->val==KM_PRESS) {
2426 if( ELEM3(event->type, LEFTMOUSE, MIDDLEMOUSE, RIGHTMOUSE) )
2427 win->tweak= WM_gesture_new(C, event, WM_GESTURE_TWEAK);
2432 /* no tweaks if event was handled */
2433 if((action & WM_HANDLER_BREAK)) {
2434 WM_gesture_end(C, win->tweak);
2437 tweak_gesture_modal(C, event);
2441 /* *********************** lasso gesture ****************** */
2443 int WM_gesture_lasso_invoke(bContext *C, wmOperator *op, wmEvent *event)
2445 op->customdata= WM_gesture_new(C, event, WM_GESTURE_LASSO);
2447 /* add modal handler */
2448 WM_event_add_modal_handler(C, op);
2450 wm_gesture_tag_redraw(C);
2452 if( RNA_struct_find_property(op->ptr, "cursor") )
2453 WM_cursor_modal(CTX_wm_window(C), RNA_int_get(op->ptr, "cursor"));
2455 return OPERATOR_RUNNING_MODAL;
2458 int WM_gesture_lines_invoke(bContext *C, wmOperator *op, wmEvent *event)
2460 op->customdata= WM_gesture_new(C, event, WM_GESTURE_LINES);
2462 /* add modal handler */
2463 WM_event_add_modal_handler(C, op);
2465 wm_gesture_tag_redraw(C);
2467 if( RNA_struct_find_property(op->ptr, "cursor") )
2468 WM_cursor_modal(CTX_wm_window(C), RNA_int_get(op->ptr, "cursor"));
2470 return OPERATOR_RUNNING_MODAL;
2474 static void gesture_lasso_apply(bContext *C, wmOperator *op)
2476 wmGesture *gesture= op->customdata;
2480 short *lasso= gesture->customdata;
2482 /* operator storage as path. */
2484 for(i=0; i<gesture->points; i++, lasso+=2) {
2487 RNA_collection_add(op->ptr, "path", &itemptr);
2488 RNA_float_set_array(&itemptr, "loc", loc);
2491 wm_gesture_end(C, op);
2494 op->type->exec(C, op);
2498 int WM_gesture_lasso_modal(bContext *C, wmOperator *op, wmEvent *event)
2500 wmGesture *gesture= op->customdata;
2503 switch(event->type) {
2505 case INBETWEEN_MOUSEMOVE:
2507 wm_gesture_tag_redraw(C);
2509 wm_subwindow_getorigin(CTX_wm_window(C), gesture->swinid, &sx, &sy);
2511 if(gesture->points == gesture->size) {
2512 short *old_lasso = gesture->customdata;
2513 gesture->customdata= MEM_callocN(2*sizeof(short)*(gesture->size + WM_LASSO_MIN_POINTS), "lasso points");
2514 memcpy(gesture->customdata, old_lasso, 2*sizeof(short)*gesture->size);
2515 gesture->size = gesture->size + WM_LASSO_MIN_POINTS;
2516 MEM_freeN(old_lasso);
2517 printf("realloc\n");
2522 short *lasso= gesture->customdata;
2524 lasso += (2 * gesture->points - 2);
2525 x = (event->x - sx - lasso[0]);
2526 y = (event->y - sy - lasso[1]);
2528 /* make a simple distance check to get a smoother lasso
2529 add only when at least 2 pixels between this and previous location */
2532 lasso[0] = event->x - sx;
2533 lasso[1] = event->y - sy;
2542 if(event->val==KM_RELEASE) { /* key release */
2543 gesture_lasso_apply(C, op);
2544 return OPERATOR_FINISHED;
2548 wm_gesture_end(C, op);
2549 return OPERATOR_CANCELLED;
2551 return OPERATOR_RUNNING_MODAL;
2554 int WM_gesture_lines_modal(bContext *C, wmOperator *op, wmEvent *event)
2556 return WM_gesture_lasso_modal(C, op, event);
2560 /* template to copy from */
2562 static int gesture_lasso_exec(bContext *C, wmOperator *op)
2564 RNA_BEGIN(op->ptr, itemptr, "path") {
2567 RNA_float_get_array(&itemptr, "loc", loc);
2568 printf("Location: %f %f\n", loc[0], loc[1]);
2572 return OPERATOR_FINISHED;
2575 void WM_OT_lasso_gesture(wmOperatorType *ot)
2579 ot->name= "Lasso Gesture";
2580 ot->idname= "WM_OT_lasso_gesture";
2581 ot->description="Select objects within the lasso as you move the pointer";
2583 ot->invoke= WM_gesture_lasso_invoke;
2584 ot->modal= WM_gesture_lasso_modal;
2585 ot->exec= gesture_lasso_exec;
2587 ot->poll= WM_operator_winactive;
2589 prop= RNA_def_property(ot->srna, "path", PROP_COLLECTION, PROP_NONE);
2590 RNA_def_property_struct_runtime(prop, &RNA_OperatorMousePath);
2594 /* *********************** straight line gesture ****************** */
2596 static int straightline_apply(bContext *C, wmOperator *op)
2598 wmGesture *gesture= op->customdata;
2599 rcti *rect= gesture->customdata;
2601 if(rect->xmin==rect->xmax && rect->ymin==rect->ymax)
2604 /* operator arguments and storage. */
2605 RNA_int_set(op->ptr, "xstart", rect->xmin);
2606 RNA_int_set(op->ptr, "ystart", rect->ymin);
2607 RNA_int_set(op->ptr, "xend", rect->xmax);
2608 RNA_int_set(op->ptr, "yend", rect->ymax);
2611 op->type->exec(C, op);
2617 int WM_gesture_straightline_invoke(bContext *C, wmOperator *op, wmEvent *event)
2619 op->customdata= WM_gesture_new(C, event, WM_GESTURE_STRAIGHTLINE);
2621 /* add modal handler */
2622 WM_event_add_modal_handler(C, op);
2624 wm_gesture_tag_redraw(C);
2626 if( RNA_struct_find_property(op->ptr, "cursor") )
2627 WM_cursor_modal(CTX_wm_window(C), RNA_int_get(op->ptr, "cursor"));
2629 return OPERATOR_RUNNING_MODAL;
2632 int WM_gesture_straightline_modal(bContext *C, wmOperator *op, wmEvent *event)
2634 wmGesture *gesture= op->customdata;
2635 rcti *rect= gesture->customdata;
2638 if(event->type== MOUSEMOVE) {
2639 wm_subwindow_getorigin(CTX_wm_window(C), gesture->swinid, &sx, &sy);
2641 if(gesture->mode==0) {
2642 rect->xmin= rect->xmax= event->x - sx;
2643 rect->ymin= rect->ymax= event->y - sy;
2646 rect->xmax= event->x - sx;
2647 rect->ymax= event->y - sy;
2648 straightline_apply(C, op);
2651 wm_gesture_tag_redraw(C);
2653 else if (event->type==EVT_MODAL_MAP) {
2654 switch (event->val) {
2655 case GESTURE_MODAL_BEGIN:
2656 if(gesture->mode==0) {
2658 wm_gesture_tag_redraw(C);
2661 case GESTURE_MODAL_SELECT:
2662 if(straightline_apply(C, op)) {
2663 wm_gesture_end(C, op);
2664 return OPERATOR_FINISHED;
2666 wm_gesture_end(C, op);
2667 return OPERATOR_CANCELLED;
2670 case GESTURE_MODAL_CANCEL:
2671 wm_gesture_end(C, op);
2672 return OPERATOR_CANCELLED;
2677 return OPERATOR_RUNNING_MODAL;
2681 /* template to copy from */
2682 void WM_OT_straightline_gesture(wmOperatorType *ot)
2686 ot->name= "Straight Line Gesture";
2687 ot->idname= "WM_OT_straightline_gesture";
2688 ot->description="Draw a straight line as you move the pointer";
2690 ot->invoke= WM_gesture_straightline_invoke;
2691 ot->modal= WM_gesture_straightline_modal;
2692 ot->exec= gesture_straightline_exec;
2694 ot->poll= WM_operator_winactive;
2696 WM_operator_properties_gesture_straightline(ot, 0);
2700 /* *********************** radial control ****************** */
2702 static const int WM_RADIAL_CONTROL_DISPLAY_SIZE = 200;
2706 PropertySubType subtype;
2707 PointerRNA ptr, col_ptr, fill_col_ptr, rot_ptr, zoom_ptr, image_id_ptr;
2708 PropertyRNA *prop, *col_prop, *fill_col_prop, *rot_prop, *zoom_prop;
2709 StructRNA *image_id_srna;
2710 float initial_value, current_value, min_value, max_value;
2711 int initial_mouse[2];
2713 ListBase orig_paintcursors;
2717 static void radial_control_set_initial_mouse(RadialControl *rc, wmEvent *event)
2719 float d[2] = {0, 0};
2720 float zoom[2] = {1, 1};
2722 rc->initial_mouse[0]= event->x;
2723 rc->initial_mouse[1]= event->y;
2725 switch(rc->subtype) {
2727 d[0] = rc->initial_value;
2730 d[0] = WM_RADIAL_CONTROL_DISPLAY_SIZE * (1 - rc->initial_value);
2733 d[0] = WM_RADIAL_CONTROL_DISPLAY_SIZE * cos(rc->initial_value);
2734 d[1] = WM_RADIAL_CONTROL_DISPLAY_SIZE * sin(rc->initial_value);
2741 RNA_property_float_get_array(&rc->zoom_ptr, rc->zoom_prop, zoom);
2746 rc->initial_mouse[0]-= d[0];
2747 rc->initial_mouse[1]-= d[1];
2750 static void radial_control_set_tex(RadialControl *rc)
2754 switch(RNA_type_to_ID_code(rc->image_id_ptr.type)) {
2756 if((ibuf = brush_gen_radial_control_imbuf(rc->image_id_ptr.data))) {
2757 glGenTextures(1, &rc->gltex);
2758 glBindTexture(GL_TEXTURE_2D, rc->gltex);
2759 glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, ibuf->x, ibuf->y, 0,
2760 GL_ALPHA, GL_FLOAT, ibuf->rect_float);
2761 MEM_freeN(ibuf->rect_float);
2770 static void radial_control_paint_tex(RadialControl *rc, float radius, float alpha)
2772 float col[3] = {0, 0, 0};
2775 /* set fill color */
2776 if(rc->fill_col_prop)
2777 RNA_property_float_get_array(&rc->fill_col_ptr, rc->fill_col_prop, col);
2778 glColor4f(col[0], col[1], col[2], alpha);
2781 glBindTexture(GL_TEXTURE_2D, rc->gltex);
2783 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
2784 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
2786 /* set up rotation if available */
2788 rot = RNA_property_float_get(&rc->rot_ptr, rc->rot_prop);
2790 glRotatef(RAD2DEGF(rot), 0, 0, 1);
2793 /* draw textured quad */
2794 glEnable(GL_TEXTURE_2D);
2797 glVertex2f(-radius, -radius);
2799 glVertex2f(radius, -radius);
2801 glVertex2f(radius, radius);
2803 glVertex2f(-radius, radius);
2805 glDisable(GL_TEXTURE_2D);
2812 /* flat color if no texture available */
2813 glutil_draw_filled_arc(0, M_PI * 2, radius, 40);
2817 static void radial_control_paint_cursor(bContext *C, int x, int y, void *customdata)
2819 RadialControl *rc = customdata;
2820 ARegion *ar = CTX_wm_region(C);
2821 float r1=0.0f, r2=0.0f, tex_radius, alpha;
2822 float zoom[2], col[3] = {1, 1, 1};
2824 switch(rc->subtype) {
2826 r1= rc->current_value;
2827 r2= rc->initial_value;
2832 r1= (1 - rc->current_value) * WM_RADIAL_CONTROL_DISPLAY_SIZE;
2833 r2= tex_radius= WM_RADIAL_CONTROL_DISPLAY_SIZE;
2834 alpha = rc->current_value / 2 + 0.5;
2837 r1= r2= tex_radius= WM_RADIAL_CONTROL_DISPLAY_SIZE;
2841 tex_radius= WM_RADIAL_CONTROL_DISPLAY_SIZE; /* note, this is a dummy value */
2846 /* Keep cursor in the original place */
2847 x = rc->initial_mouse[0] - ar->winrct.xmin;
2848 y = rc->initial_mouse[1] - ar->winrct.ymin;
2849 glTranslatef((float)x, (float)y, 0.0f);
2852 glEnable(GL_LINE_SMOOTH);
2854 /* apply zoom if available */
2856 RNA_property_float_get_array(&rc->zoom_ptr, rc->zoom_prop, zoom);
2857 glScalef(zoom[0], zoom[1], 1);
2860 /* draw rotated texture */
2861 radial_control_paint_tex(rc, tex_radius, alpha);
2863 /* set line color */
2865 RNA_property_float_get_array(&rc->col_ptr, rc->col_prop, col);
2866 glColor4f(col[0], col[1], col[2], 0.5);
2868 if(rc->subtype == PROP_ANGLE) {
2870 /* draw original angle line */
2871 glRotatef(RAD2DEGF(rc->initial_value), 0, 0, 1);
2872 fdrawline(0.0f, 0.0f, (float)WM_RADIAL_CONTROL_DISPLAY_SIZE, 0.0f);
2873 /* draw new angle line */
2874 glRotatef(RAD2DEGF(rc->current_value - rc->initial_value), 0, 0, 1);
2875 fdrawline(0.0f, 0.0f, (float)WM_RADIAL_CONTROL_DISPLAY_SIZE, 0.0f);
2879 /* draw circles on top */
2880 glutil_draw_lined_arc(0.0, (float)(M_PI*2.0), r1, 40);
2881 glutil_draw_lined_arc(0.0, (float)(M_PI*2.0), r2, 40);
2883 glDisable(GL_BLEND);
2884 glDisable(GL_LINE_SMOOTH);
2887 /* attempt to retrieve the rna pointer/property from an rna path;
2888 returns 0 for failure, 1 for success, and also 1 if property is not
2890 static int radial_control_get_path(PointerRNA *ctx_ptr, wmOperator *op,
2891 const char *name, PointerRNA *r_ptr,
2892 PropertyRNA **r_prop, int req_float,
2893 int req_length, int allow_missing)
2895 PropertyRNA *unused_prop;
2899 /* get an rna string path from the operator's properties */
2900 if(!(str = RNA_string_get_alloc(op->ptr, name, NULL, 0)))
2903 if(str[0] == '\0') {
2909 r_prop = &unused_prop;
2911 /* get rna from path */
2912 if(!RNA_path_resolve(ctx_ptr, str, r_ptr, r_prop)) {
2917 BKE_reportf(op->reports, RPT_ERROR, "Couldn't resolve path %s", name);
2922 /* if property is expected to be a float, check its type */
2924 if(!(*r_prop) || (RNA_property_type(*r_prop) != PROP_FLOAT)) {
2926 BKE_reportf(op->reports, RPT_ERROR,
2927 "Property from path %s is not a float", name);
2932 /* check property's array length */
2933 if(*r_prop && (len = RNA_property_array_length(r_ptr, *r_prop)) != req_length) {
2935 BKE_reportf(op->reports, RPT_ERROR,
2936 "Property from path %s has length %d instead of %d",
2937 name, len, req_length);
2946 /* initialize the rna pointers and properties using rna paths */
2947 static int radial_control_get_properties(bContext *C, wmOperator *op)
2949 RadialControl *rc = op->customdata;
2952 RNA_pointer_create(NULL, &RNA_Context, C, &ctx_ptr);
2954 if(!radial_control_get_path(&ctx_ptr, op, "data_path", &rc->ptr, &rc->prop, 0, 0, 0))
2957 /* data path is required */
2961 if(!radial_control_get_path(&ctx_ptr, op, "rotation_path", &rc->rot_ptr, &rc->rot_prop, 1, 0, 0))
2963 if(!radial_control_get_path(&ctx_ptr, op, "color_path", &rc->col_ptr, &rc->col_prop, 1, 3, 0))
2965 if(!radial_control_get_path(&ctx_ptr, op, "fill_color_path", &rc->fill_col_ptr, &rc->fill_col_prop, 1, 3, 0))
2968 /* slightly ugly; allow this property to not resolve
2969 correctly. needed because 3d texture paint shares the same
2970 keymap as 2d image paint */
2971 if(!radial_control_get_path(&ctx_ptr, op, "zoom_path", &rc->zoom_ptr, &rc->zoom_prop, 1, 2, 1))
2974 if(!radial_control_get_path(&ctx_ptr, op, "image_id", &rc->image_id_ptr, NULL, 0, 0, 0))
2976 else if(rc->image_id_ptr.data) {
2977 /* extra check, pointer must be to an ID */
2978 if(!RNA_struct_is_ID(rc->image_id_ptr.type)) {
2979 BKE_report(op->reports, RPT_ERROR,
2980 "Pointer from path image_id is not an ID");
2988 static int radial_control_invoke(bContext *C, wmOperator *op, wmEvent *event)
2990 wmWindowManager *wm;
2992 int min_value_int, max_value_int, step_int;
2993 float step_float, precision;
2995 if(!(op->customdata = rc = MEM_callocN(sizeof(RadialControl), "RadialControl")))
2996 return OPERATOR_CANCELLED;
2998 if(!radial_control_get_properties(C, op)) {
3000 return OPERATOR_CANCELLED;
3003 /* get type, initial, min, and max values of the property */
3004 switch((rc->type = RNA_property_type(rc->prop))) {
3006 rc->initial_value = RNA_property_int_get(&rc->ptr, rc->prop);
3007 RNA_property_int_ui_range(&rc->ptr, rc->prop, &min_value_int,
3008 &max_value_int, &step_int);
3009 rc->min_value = min_value_int;
3010 rc->max_value = max_value_int;
3013 rc->initial_value = RNA_property_float_get(&rc->ptr, rc->prop);
3014 RNA_property_float_ui_range(&rc->ptr, rc->prop, &rc->min_value,
3015 &rc->max_value, &step_float, &precision);
3018 BKE_report(op->reports, RPT_ERROR, "Property must be an integer or a float");
3020 return OPERATOR_CANCELLED;
3023 /* get subtype of property */
3024 rc->subtype = RNA_property_subtype(rc->prop);
3025 if(!ELEM3(rc->subtype, PROP_DISTANCE, PROP_FACTOR, PROP_ANGLE)) {
3026 BKE_report(op->reports, RPT_ERROR, "Property must be a distance, a factor, or an angle");
3028 return OPERATOR_CANCELLED;
3031 rc->current_value = rc->initial_value;
3032 radial_control_set_initial_mouse(rc, event);
3033 radial_control_set_tex(rc);
3035 /* temporarily disable other paint cursors */
3036 wm = CTX_wm_manager(C);
3037 rc->orig_paintcursors = wm->paintcursors;
3038 wm->paintcursors.first = wm->paintcursors.last = NULL;
3040 /* add radial control paint cursor */
3041 rc->cursor = WM_paint_cursor_activate(wm, op->type->poll,
3042 radial_control_paint_cursor, rc);
3044 WM_event_add_modal_handler(C, op);
3046 return OPERATOR_RUNNING_MODAL;
3049 static void radial_control_set_value(RadialControl *rc, float val)
3053 RNA_property_int_set(&rc->ptr, rc->prop, val);
3056 RNA_property_float_set(&rc->ptr, rc->prop, val);
3063 static int radial_control_modal(bContext *C, wmOperator *op, wmEvent *event)
3065 RadialControl *rc = op->customdata;
3066 wmWindowManager *wm;
3067 float new_value, dist, zoom[2];
3068 float delta[2], snap, ret = OPERATOR_RUNNING_MODAL;
3070 /* TODO: fix hardcoded events */
3074 switch(event->type) {
3076 delta[0]= rc->initial_mouse[0] - event->x;
3077 delta[1]= rc->initial_mouse[1] - event->y;
3080 RNA_property_float_get_array(&rc->zoom_ptr, rc->zoom_prop, zoom);
3081 delta[0] /= zoom[0];
3082 delta[1] /= zoom[1];
3085 dist= sqrt(delta[0]*delta[0]+delta[1]*delta[1]);
3087 /* calculate new value and apply snapping */
3088 switch(rc->subtype) {
3091 if(snap) new_value = ((int)new_value + 5) / 10*10;
3094 new_value = 1 - dist / WM_RADIAL_CONTROL_DISPLAY_SIZE;
3095 if(snap) new_value = ((int)ceil(new_value * 10.f) * 10.0f) / 100.f;
3098 new_value = atan2(delta[1], delta[0]) + M_PI;
3099 if(snap) new_value = DEG2RADF(((int)RAD2DEGF(new_value) + 5) / 10*10);
3102 new_value = dist; /* dummy value, should this ever happen? - campbell */
3106 /* clamp and update */
3107 CLAMP(new_value, rc->min_value, rc->max_value);
3108 radial_control_set_value(rc, new_value);
3109 rc->current_value = new_value;
3114 /* cancelled; restore original value */
3115 radial_control_set_value(rc, rc->initial_value);
3116 ret = OPERATOR_CANCELLED;
3121 /* done; value already set */
3122 ret = OPERATOR_FINISHED;
3126 ED_region_tag_redraw(CTX_wm_region(C));
3128 if(ret != OPERATOR_RUNNING_MODAL) {
3129 wm = CTX_wm_manager(C);
3131 WM_paint_cursor_end(wm, rc->cursor);
3133 /* restore original paint cursors */
3134 wm->paintcursors = rc->orig_paintcursors;
3136 /* not sure if this is a good notifier to use;
3137 intended purpose is to update the UI so that the
3138 new value is displayed in sliders/numfields */
3139 WM_event_add_notifier(C, NC_WINDOW, NULL);
3141 glDeleteTextures(1, &rc->gltex);
3149 static void WM_OT_radial_control(wmOperatorType *ot)
3151 ot->name= "Radial Control";
3152 ot->idname= "WM_OT_radial_control";
3154 ot->invoke= radial_control_invoke;
3155 ot->modal= radial_control_modal;
3157 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO|OPTYPE_BLOCKING;
3159 /* all paths relative to the context */
3160 RNA_def_string(ot->srna, "data_path", "", 0, "Data Path", "Path of property to be set by the radial control.");
3161 RNA_def_string(ot->srna, "rotation_path", "", 0, "Rotation Path", "Path of property used to rotate the texture display.");
3162 RNA_def_string(ot->srna, "color_path", "", 0, "Color Path", "Path of property used to set the color of the control.");
3163 RNA_def_string(ot->srna, "fill_color_path", "", 0, "Fill Color Path", "Path of property used to set the fill color of the control.");
3164 RNA_def_string(ot->srna, "zoom_path", "", 0, "Zoom Path", "Path of property used to set the zoom level for the control.");
3165 RNA_def_string(ot->srna, "image_id", "", 0, "Image ID", "Path of ID that is used to generate an image for the control.");
3168 /* ************************** timer for testing ***************** */
3170 /* uses no type defines, fully local testing function anyway... ;) */
3172 static void redraw_timer_window_swap(bContext *C)
3174 wmWindow *win= CTX_wm_window(C);
3177 for(sa= CTX_wm_screen(C)->areabase.first; sa; sa= sa->next)
3178 ED_area_tag_redraw(sa);
3181 CTX_wm_window_set(C, win); /* XXX context manipulation warning! */
3184 static EnumPropertyItem redraw_timer_type_items[] = {
3185 {0, "DRAW", 0, "Draw Region", "Draw Region"},
3186 {1, "DRAW_SWAP", 0, "Draw Region + Swap", "Draw Region and Swap"},
3187 {2, "DRAW_WIN", 0, "Draw Window", "Draw Window"},
3188 {3, "DRAW_WIN_SWAP", 0, "Draw Window + Swap", "Draw Window and Swap"},
3189 {4, "ANIM_STEP", 0, "Anim Step", "Animation Steps"},
3190 {5, "ANIM_PLAY", 0, "Anim Play", "Animation Playback"},
3191 {6, "UNDO", 0, "Undo/Redo", "Undo/Redo"},
3192 {0, NULL, 0, NULL, NULL}};
3194 static int redraw_timer_exec(bContext *C, wmOperator *op)
3196 ARegion *ar= CTX_wm_region(C);
3197 double stime= PIL_check_seconds_timer();
3198 int type = RNA_enum_get(op->ptr, "type");
3199 int iter = RNA_int_get(op->ptr, "iterations");
3202 const char *infostr= "";
3206 for(a=0; a<iter; a++) {
3209 ED_region_do_draw(C, ar);