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_utildefines.h"
61 #include "BLO_readfile.h"
63 #include "BKE_blender.h"
64 #include "BKE_brush.h"
65 #include "BKE_context.h"
66 #include "BKE_depsgraph.h"
67 #include "BKE_idprop.h"
68 #include "BKE_library.h"
69 #include "BKE_global.h"
71 #include "BKE_report.h"
72 #include "BKE_scene.h"
73 #include "BKE_screen.h" /* BKE_ST_MAXNAME */
75 #include "BKE_idcode.h"
78 #include "BIF_glutil.h" /* for paint cursor */
80 #include "IMB_imbuf_types.h"
82 #include "ED_screen.h"
85 #include "RNA_access.h"
86 #include "RNA_define.h"
87 #include "RNA_enum_types.h"
89 #include "UI_interface.h"
90 #include "UI_resources.h"
97 #include "wm_event_system.h"
98 #include "wm_event_types.h"
99 #include "wm_subwindow.h"
100 #include "wm_window.h"
102 static ListBase global_ops= {NULL, NULL};
104 /* ************ operator API, exported ********** */
107 wmOperatorType *WM_operatortype_find(const char *idname, int quiet)
111 char idname_bl[OP_MAX_TYPENAME]; // XXX, needed to support python style names without the _OT_ syntax
112 WM_operator_bl_idname(idname_bl, idname);
115 ot= (wmOperatorType *)BLI_findstring_ptr(&global_ops, idname_bl, offsetof(wmOperatorType, idname));
122 printf("search for unknown operator %s, %s\n", idname_bl, idname);
127 wmOperatorType *WM_operatortype_first(void)
129 return global_ops.first;
132 /* all ops in 1 list (for time being... needs evaluation later) */
133 void WM_operatortype_append(void (*opfunc)(wmOperatorType*))
137 ot= MEM_callocN(sizeof(wmOperatorType), "operatortype");
138 ot->srna= RNA_def_struct(&BLENDER_RNA, "", "OperatorProperties");
142 static char dummy_name[] = "Dummy Name";
143 fprintf(stderr, "ERROR: Operator %s has no name property!\n", ot->idname);
144 ot->name= dummy_name;
147 RNA_def_struct_ui_text(ot->srna, ot->name, ot->description ? ot->description:"(undocumented operator)"); // XXX All ops should have a description but for now allow them not to.
148 RNA_def_struct_identifier(ot->srna, ot->idname);
149 BLI_addtail(&global_ops, ot);
152 void WM_operatortype_append_ptr(void (*opfunc)(wmOperatorType*, void*), void *userdata)
156 ot= MEM_callocN(sizeof(wmOperatorType), "operatortype");
157 ot->srna= RNA_def_struct(&BLENDER_RNA, "", "OperatorProperties");
158 opfunc(ot, userdata);
159 RNA_def_struct_ui_text(ot->srna, ot->name, ot->description ? ot->description:"(undocumented operator)");
160 RNA_def_struct_identifier(ot->srna, ot->idname);
161 BLI_addtail(&global_ops, ot);
164 /* ********************* macro operator ******************** */
170 static void wm_macro_start(wmOperator *op)
172 if (op->customdata == NULL) {
173 op->customdata = MEM_callocN(sizeof(MacroData), "MacroData");
177 static int wm_macro_end(wmOperator *op, int retval)
179 if (retval & OPERATOR_CANCELLED) {
180 MacroData *md = op->customdata;
182 if (md->retval & OPERATOR_FINISHED) {
183 retval |= OPERATOR_FINISHED;
184 retval &= ~OPERATOR_CANCELLED;
188 /* if modal is ending, free custom data */
189 if (retval & (OPERATOR_FINISHED|OPERATOR_CANCELLED)) {
190 if (op->customdata) {
191 MEM_freeN(op->customdata);
192 op->customdata = NULL;
199 /* macro exec only runs exec calls */
200 static int wm_macro_exec(bContext *C, wmOperator *op)
203 int retval= OPERATOR_FINISHED;
207 for(opm= op->macro.first; opm; opm= opm->next) {
209 if(opm->type->exec) {
210 retval= opm->type->exec(C, opm);
212 if (retval & OPERATOR_FINISHED) {
213 MacroData *md = op->customdata;
214 md->retval = OPERATOR_FINISHED; /* keep in mind that at least one operator finished */
216 break; /* operator didn't finish, end macro */
221 return wm_macro_end(op, retval);
224 static int wm_macro_invoke_internal(bContext *C, wmOperator *op, wmEvent *event, wmOperator *opm)
226 int retval= OPERATOR_FINISHED;
228 /* start from operator received as argument */
229 for( ; opm; opm= opm->next) {
230 if(opm->type->invoke)
231 retval= opm->type->invoke(C, opm, event);
232 else if(opm->type->exec)
233 retval= opm->type->exec(C, opm);
235 BLI_movelisttolist(&op->reports->list, &opm->reports->list);
237 if (retval & OPERATOR_FINISHED) {
238 MacroData *md = op->customdata;
239 md->retval = OPERATOR_FINISHED; /* keep in mind that at least one operator finished */
241 break; /* operator didn't finish, end macro */
245 return wm_macro_end(op, retval);
248 static int wm_macro_invoke(bContext *C, wmOperator *op, wmEvent *event)
251 return wm_macro_invoke_internal(C, op, event, op->macro.first);
254 static int wm_macro_modal(bContext *C, wmOperator *op, wmEvent *event)
256 wmOperator *opm = op->opm;
257 int retval= OPERATOR_FINISHED;
260 printf("macro error, calling NULL modal()\n");
262 retval = opm->type->modal(C, opm, event);
264 /* if this one is done but it's not the last operator in the macro */
265 if ((retval & OPERATOR_FINISHED) && opm->next) {
266 MacroData *md = op->customdata;
268 md->retval = OPERATOR_FINISHED; /* keep in mind that at least one operator finished */
270 retval = wm_macro_invoke_internal(C, op, event, opm->next);
272 /* if new operator is modal and also added its own handler */
273 if (retval & OPERATOR_RUNNING_MODAL && op->opm != opm) {
274 wmWindow *win = CTX_wm_window(C);
275 wmEventHandler *handler = NULL;
277 for (handler = win->modalhandlers.first; handler; handler = handler->next) {
278 /* first handler in list is the new one */
279 if (handler->op == op)
284 BLI_remlink(&win->modalhandlers, handler);
285 wm_event_free_handler(handler);
288 /* if operator is blocking, grab cursor
289 * This may end up grabbing twice, but we don't care.
291 if(op->opm->type->flag & OPTYPE_BLOCKING) {
292 int bounds[4] = {-1,-1,-1,-1};
293 int wrap = (U.uiflag & USER_CONTINUOUS_MOUSE) && ((op->opm->flag & OP_GRAB_POINTER) || (op->opm->type->flag & OPTYPE_GRAB_POINTER));
296 ARegion *ar= CTX_wm_region(C);
298 bounds[0]= ar->winrct.xmin;
299 bounds[1]= ar->winrct.ymax;
300 bounds[2]= ar->winrct.xmax;
301 bounds[3]= ar->winrct.ymin;
305 WM_cursor_grab(CTX_wm_window(C), wrap, FALSE, bounds);
311 return wm_macro_end(op, retval);
314 static int wm_macro_cancel(bContext *C, wmOperator *op)
316 /* call cancel on the current modal operator, if any */
317 if (op->opm && op->opm->type->cancel) {
318 op->opm->type->cancel(C, op->opm);
321 return wm_macro_end(op, OPERATOR_CANCELLED);
324 /* Names have to be static for now */
325 wmOperatorType *WM_operatortype_append_macro(const char *idname, const char *name, int flag)
329 if(WM_operatortype_find(idname, TRUE)) {
330 printf("Macro error: operator %s exists\n", idname);
334 ot= MEM_callocN(sizeof(wmOperatorType), "operatortype");
335 ot->srna= RNA_def_struct(&BLENDER_RNA, "", "OperatorProperties");
339 ot->flag= OPTYPE_MACRO|flag;
341 ot->exec= wm_macro_exec;
342 ot->invoke= wm_macro_invoke;
343 ot->modal= wm_macro_modal;
344 ot->cancel= wm_macro_cancel;
348 ot->description= "(undocumented operator)";
350 RNA_def_struct_ui_text(ot->srna, ot->name, ot->description); // XXX All ops should have a description but for now allow them not to.
351 RNA_def_struct_identifier(ot->srna, ot->idname);
353 BLI_addtail(&global_ops, ot);
358 void WM_operatortype_append_macro_ptr(void (*opfunc)(wmOperatorType*, void*), void *userdata)
362 ot= MEM_callocN(sizeof(wmOperatorType), "operatortype");
363 ot->srna= RNA_def_struct(&BLENDER_RNA, "", "OperatorProperties");
365 ot->flag= OPTYPE_MACRO;
366 ot->exec= wm_macro_exec;
367 ot->invoke= wm_macro_invoke;
368 ot->modal= wm_macro_modal;
369 ot->cancel= wm_macro_cancel;
373 ot->description= "(undocumented operator)";
375 opfunc(ot, userdata);
377 RNA_def_struct_ui_text(ot->srna, ot->name, ot->description);
378 RNA_def_struct_identifier(ot->srna, ot->idname);
380 BLI_addtail(&global_ops, ot);
383 wmOperatorTypeMacro *WM_operatortype_macro_define(wmOperatorType *ot, const char *idname)
385 wmOperatorTypeMacro *otmacro= MEM_callocN(sizeof(wmOperatorTypeMacro), "wmOperatorTypeMacro");
387 BLI_strncpy(otmacro->idname, idname, OP_MAX_TYPENAME);
389 /* do this on first use, since operatordefinitions might have been not done yet */
390 WM_operator_properties_alloc(&(otmacro->ptr), &(otmacro->properties), idname);
391 WM_operator_properties_sanitize(otmacro->ptr, 1);
393 BLI_addtail(&ot->macro, otmacro);
396 /* operator should always be found but in the event its not. dont segfault */
397 wmOperatorType *otsub = WM_operatortype_find(idname, 0);
399 RNA_def_pointer_runtime(ot->srna, otsub->idname, otsub->srna,
400 otsub->name, otsub->description);
407 static void wm_operatortype_free_macro(wmOperatorType *ot)
409 wmOperatorTypeMacro *otmacro;
411 for(otmacro= ot->macro.first; otmacro; otmacro= otmacro->next) {
413 WM_operator_properties_free(otmacro->ptr);
414 MEM_freeN(otmacro->ptr);
417 BLI_freelistN(&ot->macro);
421 int WM_operatortype_remove(const char *idname)
423 wmOperatorType *ot = WM_operatortype_find(idname, 0);
428 BLI_remlink(&global_ops, ot);
429 RNA_struct_free(&BLENDER_RNA, ot->srna);
432 wm_operatortype_free_macro(ot);
439 /* SOME_OT_op -> some.op */
440 void WM_operator_py_idname(char *to, const char *from)
442 char *sep= strstr(from, "_OT_");
444 int i, ofs= (sep-from);
447 to[i]= tolower(from[i]);
450 BLI_strncpy(to+(ofs+1), sep+4, OP_MAX_TYPENAME);
453 /* should not happen but support just incase */
454 BLI_strncpy(to, from, OP_MAX_TYPENAME);
458 /* some.op -> SOME_OT_op */
459 void WM_operator_bl_idname(char *to, const char *from)
462 char *sep= strchr(from, '.');
465 int i, ofs= (sep-from);
468 to[i]= toupper(from[i]);
470 BLI_strncpy(to+ofs, "_OT_", OP_MAX_TYPENAME);
471 BLI_strncpy(to+(ofs+4), sep+1, OP_MAX_TYPENAME);
474 /* should not happen but support just incase */
475 BLI_strncpy(to, from, OP_MAX_TYPENAME);
482 /* print a string representation of the operator, with the args that it runs
483 * so python can run it again,
485 * When calling from an existing wmOperator do.
486 * WM_operator_pystring(op->type, op->ptr);
488 char *WM_operator_pystring(bContext *C, wmOperatorType *ot, PointerRNA *opptr, int all_args)
490 const char *arg_name= NULL;
491 char idname_py[OP_MAX_TYPENAME];
493 PropertyRNA *prop, *iterprop;
495 /* for building the string */
496 DynStr *dynstr= BLI_dynstr_new();
498 int first_iter=1, ok= 1;
501 /* only to get the orginal props for comparisons */
502 PointerRNA opptr_default;
503 PropertyRNA *prop_default;
505 if(all_args==0 || opptr==NULL) {
506 WM_operator_properties_create_ptr(&opptr_default, ot);
509 opptr = &opptr_default;
513 WM_operator_py_idname(idname_py, ot->idname);
514 BLI_dynstr_appendf(dynstr, "bpy.ops.%s(", idname_py);
516 iterprop= RNA_struct_iterator_property(opptr->type);
518 RNA_PROP_BEGIN(opptr, propptr, iterprop) {
520 arg_name= RNA_property_identifier(prop);
522 if (strcmp(arg_name, "rna_type")==0) continue;
524 buf= RNA_property_as_string(C, opptr, prop);
529 /* not verbose, so only add in attributes that use non-default values
530 * slow but good for tooltips */
531 prop_default= RNA_struct_find_property(&opptr_default, arg_name);
534 buf_default= RNA_property_as_string(C, &opptr_default, prop_default);
536 if(strcmp(buf, buf_default)==0)
537 ok= 0; /* values match, dont bother printing */
539 MEM_freeN(buf_default);
544 BLI_dynstr_appendf(dynstr, first_iter?"%s=%s":", %s=%s", arg_name, buf);
553 if(all_args==0 || opptr==&opptr_default )
554 WM_operator_properties_free(&opptr_default);
556 BLI_dynstr_append(dynstr, ")");
558 cstring = BLI_dynstr_get_cstring(dynstr);
559 BLI_dynstr_free(dynstr);
563 void WM_operator_properties_create_ptr(PointerRNA *ptr, wmOperatorType *ot)
565 RNA_pointer_create(NULL, ot->srna, NULL, ptr);
568 void WM_operator_properties_create(PointerRNA *ptr, const char *opstring)
570 wmOperatorType *ot= WM_operatortype_find(opstring, 0);
573 WM_operator_properties_create_ptr(ptr, ot);
575 RNA_pointer_create(NULL, &RNA_OperatorProperties, NULL, ptr);
578 /* similar to the function above except its uses ID properties
579 * used for keymaps and macros */
580 void WM_operator_properties_alloc(PointerRNA **ptr, IDProperty **properties, const char *opstring)
582 if(*properties==NULL) {
583 IDPropertyTemplate val = {0};
584 *properties= IDP_New(IDP_GROUP, val, "wmOpItemProp");
588 *ptr= MEM_callocN(sizeof(PointerRNA), "wmOpItemPtr");
589 WM_operator_properties_create(*ptr, opstring);
592 (*ptr)->data= *properties;
596 void WM_operator_properties_sanitize(PointerRNA *ptr, const short no_context)
598 RNA_STRUCT_BEGIN(ptr, prop) {
599 switch(RNA_property_type(prop)) {
602 RNA_def_property_flag(prop, PROP_ENUM_NO_CONTEXT);
604 RNA_def_property_clear_flag(prop, PROP_ENUM_NO_CONTEXT);
608 StructRNA *ptype= RNA_property_pointer_type(ptr, prop);
610 /* recurse into operator properties */
611 if (RNA_struct_is_a(ptype, &RNA_OperatorProperties)) {
612 PointerRNA opptr = RNA_property_pointer_get(ptr, prop);
613 WM_operator_properties_sanitize(&opptr, no_context);
624 void WM_operator_properties_free(PointerRNA *ptr)
626 IDProperty *properties= ptr->data;
629 IDP_FreeProperty(properties);
630 MEM_freeN(properties);
631 ptr->data= NULL; /* just incase */
635 /* ************ default op callbacks, exported *********** */
637 /* invoke callback, uses enum property named "type" */
638 int WM_menu_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
640 PropertyRNA *prop= op->type->prop;
645 printf("WM_menu_invoke: %s has no enum property set\n", op->type->idname);
647 else if (RNA_property_type(prop) != PROP_ENUM) {
648 printf("WM_menu_invoke: %s \"%s\" is not an enum property\n", op->type->idname, RNA_property_identifier(prop));
650 else if (RNA_property_is_set(op->ptr, RNA_property_identifier(prop))) {
651 return op->type->exec(C, op);
654 pup= uiPupMenuBegin(C, op->type->name, ICON_NONE);
655 layout= uiPupMenuLayout(pup);
656 uiItemsFullEnumO(layout, op->type->idname, (char*)RNA_property_identifier(prop), op->ptr->data, WM_OP_EXEC_REGION_WIN, 0);
657 uiPupMenuEnd(C, pup);
660 return OPERATOR_CANCELLED;
664 /* generic enum search invoke popup */
665 static void operator_enum_search_cb(const struct bContext *C, void *arg_ot, const char *str, uiSearchItems *items)
667 wmOperatorType *ot = (wmOperatorType *)arg_ot;
668 PropertyRNA *prop= ot->prop;
671 printf("WM_enum_search_invoke: %s has no enum property set\n", ot->idname);
673 else if (RNA_property_type(prop) != PROP_ENUM) {
674 printf("WM_enum_search_invoke: %s \"%s\" is not an enum property\n", ot->idname, RNA_property_identifier(prop));
679 EnumPropertyItem *item, *item_array;
682 RNA_pointer_create(NULL, ot->srna, NULL, &ptr);
683 RNA_property_enum_items((bContext *)C, &ptr, prop, &item_array, NULL, &do_free);
685 for(item= item_array; item->identifier; item++) {
686 /* note: need to give the intex rather then the dientifier because the enum can be freed */
687 if(BLI_strcasestr(item->name, str))
688 if(0==uiSearchItemAdd(items, item->name, SET_INT_IN_POINTER(item->value), 0))
693 MEM_freeN(item_array);
697 static void operator_enum_call_cb(struct bContext *C, void *arg1, void *arg2)
699 wmOperatorType *ot= arg1;
703 PointerRNA props_ptr;
704 WM_operator_properties_create_ptr(&props_ptr, ot);
705 RNA_property_enum_set(&props_ptr, ot->prop, GET_INT_FROM_POINTER(arg2));
706 WM_operator_name_call(C, ot->idname, WM_OP_EXEC_DEFAULT, &props_ptr);
707 WM_operator_properties_free(&props_ptr);
710 printf("operator_enum_call_cb: op->prop for '%s' is NULL\n", ot->idname);
715 static uiBlock *wm_enum_search_menu(bContext *C, ARegion *ar, void *arg_op)
717 static char search[256]= "";
719 wmWindow *win= CTX_wm_window(C);
722 wmOperator *op= (wmOperator *)arg_op;
724 block= uiBeginBlock(C, ar, "_popup", UI_EMBOSS);
725 uiBlockSetFlag(block, UI_BLOCK_LOOP|UI_BLOCK_RET_1|UI_BLOCK_MOVEMOUSE_QUIT);
727 //uiDefBut(block, LABEL, 0, op->type->name, 10, 10, 180, 19, NULL, 0.0, 0.0, 0, 0, ""); // ok, this isnt so easy...
728 but= uiDefSearchBut(block, search, 0, ICON_VIEWZOOM, 256, 10, 10, 180, 19, 0, 0, "");
729 uiButSetSearchFunc(but, operator_enum_search_cb, op->type, operator_enum_call_cb, NULL);
731 /* fake button, it holds space for search items */
732 uiDefBut(block, LABEL, 0, "", 10, 10 - uiSearchBoxhHeight(), 180, uiSearchBoxhHeight(), NULL, 0, 0, 0, 0, NULL);
734 uiPopupBoundsBlock(block, 6, 0, -20); /* move it downwards, mouse over button */
735 uiEndBlock(C, block);
737 event= *(win->eventstate); /* XXX huh huh? make api call */
738 event.type= EVT_BUT_OPEN;
740 event.customdata= but;
741 event.customdatafree= FALSE;
742 wm_event_add(win, &event);
748 int WM_enum_search_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
750 uiPupBlock(C, wm_enum_search_menu, op);
751 return OPERATOR_CANCELLED;
754 /* Can't be used as an invoke directly, needs message arg (can be NULL) */
755 int WM_operator_confirm_message(bContext *C, wmOperator *op, const char *message)
759 IDProperty *properties= op->ptr->data;
761 if(properties && properties->len)
762 properties= IDP_CopyProperty(op->ptr->data);
766 pup= uiPupMenuBegin(C, "OK?", ICON_QUESTION);
767 layout= uiPupMenuLayout(pup);
768 uiItemFullO(layout, op->type->idname, message, ICON_NONE, properties, WM_OP_EXEC_REGION_WIN, 0);
769 uiPupMenuEnd(C, pup);
771 return OPERATOR_CANCELLED;
775 int WM_operator_confirm(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
777 return WM_operator_confirm_message(C, op, NULL);
780 /* op->invoke, opens fileselect if path property not set, otherwise executes */
781 int WM_operator_filesel(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
783 if (RNA_property_is_set(op->ptr, "filepath")) {
784 return WM_operator_call(C, op);
787 WM_event_add_fileselect(C, op);
788 return OPERATOR_RUNNING_MODAL;
792 /* default properties for fileselect */
793 void WM_operator_properties_filesel(wmOperatorType *ot, int filter, short type, short action, short flag)
798 if(flag & WM_FILESEL_FILEPATH)
799 RNA_def_string_file_path(ot->srna, "filepath", "", FILE_MAX, "File Path", "Path to file");
801 if(flag & WM_FILESEL_DIRECTORY)
802 RNA_def_string_dir_path(ot->srna, "directory", "", FILE_MAX, "Directory", "Directory of the file");
804 if(flag & WM_FILESEL_FILENAME)
805 RNA_def_string_file_name(ot->srna, "filename", "", FILE_MAX, "File Name", "Name of the file");
807 if (action == FILE_SAVE) {
808 prop= RNA_def_boolean(ot->srna, "check_existing", 1, "Check Existing", "Check and warn on overwriting existing files");
809 RNA_def_property_flag(prop, PROP_HIDDEN);
812 prop= RNA_def_boolean(ot->srna, "filter_blender", (filter & BLENDERFILE), "Filter .blend files", "");
813 RNA_def_property_flag(prop, PROP_HIDDEN);
814 prop= RNA_def_boolean(ot->srna, "filter_image", (filter & IMAGEFILE), "Filter image files", "");
815 RNA_def_property_flag(prop, PROP_HIDDEN);
816 prop= RNA_def_boolean(ot->srna, "filter_movie", (filter & MOVIEFILE), "Filter movie files", "");
817 RNA_def_property_flag(prop, PROP_HIDDEN);
818 prop= RNA_def_boolean(ot->srna, "filter_python", (filter & PYSCRIPTFILE), "Filter python files", "");
819 RNA_def_property_flag(prop, PROP_HIDDEN);
820 prop= RNA_def_boolean(ot->srna, "filter_font", (filter & FTFONTFILE), "Filter font files", "");
821 RNA_def_property_flag(prop, PROP_HIDDEN);
822 prop= RNA_def_boolean(ot->srna, "filter_sound", (filter & SOUNDFILE), "Filter sound files", "");
823 RNA_def_property_flag(prop, PROP_HIDDEN);
824 prop= RNA_def_boolean(ot->srna, "filter_text", (filter & TEXTFILE), "Filter text files", "");
825 RNA_def_property_flag(prop, PROP_HIDDEN);
826 prop= RNA_def_boolean(ot->srna, "filter_btx", (filter & BTXFILE), "Filter btx files", "");
827 RNA_def_property_flag(prop, PROP_HIDDEN);
828 prop= RNA_def_boolean(ot->srna, "filter_collada", (filter & COLLADAFILE), "Filter COLLADA files", "");
829 RNA_def_property_flag(prop, PROP_HIDDEN);
830 prop= RNA_def_boolean(ot->srna, "filter_folder", (filter & FOLDERFILE), "Filter folders", "");
831 RNA_def_property_flag(prop, PROP_HIDDEN);
833 prop= RNA_def_int(ot->srna, "filemode", type, FILE_LOADLIB, FILE_SPECIAL,
834 "File Browser Mode", "The setting for the file browser mode to load a .blend file, a library or a special file",
835 FILE_LOADLIB, FILE_SPECIAL);
836 RNA_def_property_flag(prop, PROP_HIDDEN);
838 if(flag & WM_FILESEL_RELPATH)
839 RNA_def_boolean(ot->srna, "relative_path", (U.flag & USER_RELPATHS) ? 1:0, "Relative Path", "Select the file relative to the blend file");
842 void WM_operator_properties_select_all(wmOperatorType *ot) {
843 static EnumPropertyItem select_all_actions[] = {
844 {SEL_TOGGLE, "TOGGLE", 0, "Toggle", "Toggle selection for all elements"},
845 {SEL_SELECT, "SELECT", 0, "Select", "Select all elements"},
846 {SEL_DESELECT, "DESELECT", 0, "Deselect", "Deselect all elements"},
847 {SEL_INVERT, "INVERT", 0, "Invert", "Invert selection of all elements"},
848 {0, NULL, 0, NULL, NULL}
851 RNA_def_enum(ot->srna, "action", select_all_actions, SEL_TOGGLE, "Action", "Selection action to execute");
854 void WM_operator_properties_gesture_border(wmOperatorType *ot, int extend)
856 RNA_def_int(ot->srna, "gesture_mode", 0, INT_MIN, INT_MAX, "Gesture Mode", "", INT_MIN, INT_MAX);
857 RNA_def_int(ot->srna, "xmin", 0, INT_MIN, INT_MAX, "X Min", "", INT_MIN, INT_MAX);
858 RNA_def_int(ot->srna, "xmax", 0, INT_MIN, INT_MAX, "X Max", "", INT_MIN, INT_MAX);
859 RNA_def_int(ot->srna, "ymin", 0, INT_MIN, INT_MAX, "Y Min", "", INT_MIN, INT_MAX);
860 RNA_def_int(ot->srna, "ymax", 0, INT_MIN, INT_MAX, "Y Max", "", INT_MIN, INT_MAX);
863 RNA_def_boolean(ot->srna, "extend", 1, "Extend", "Extend selection instead of deselecting everything first");
866 void WM_operator_properties_gesture_straightline(wmOperatorType *ot, int cursor)
868 RNA_def_int(ot->srna, "xstart", 0, INT_MIN, INT_MAX, "X Start", "", INT_MIN, INT_MAX);
869 RNA_def_int(ot->srna, "xend", 0, INT_MIN, INT_MAX, "X End", "", INT_MIN, INT_MAX);
870 RNA_def_int(ot->srna, "ystart", 0, INT_MIN, INT_MAX, "Y Start", "", INT_MIN, INT_MAX);
871 RNA_def_int(ot->srna, "yend", 0, INT_MIN, INT_MAX, "Y End", "", INT_MIN, INT_MAX);
874 RNA_def_int(ot->srna, "cursor", cursor, 0, INT_MAX, "Cursor", "Mouse cursor style to use during the modal operator", 0, INT_MAX);
879 int WM_operator_winactive(bContext *C)
881 if(CTX_wm_window(C)==NULL) return 0;
885 wmOperator *WM_operator_last_redo(const bContext *C)
887 wmWindowManager *wm= CTX_wm_manager(C);
890 /* only for operators that are registered and did an undo push */
891 for(op= wm->operators.last; op; op= op->prev)
892 if((op->type->flag & OPTYPE_REGISTER) && (op->type->flag & OPTYPE_UNDO))
898 static uiBlock *wm_block_create_redo(bContext *C, ARegion *ar, void *arg_op)
900 wmOperator *op= arg_op;
903 uiStyle *style= U.uistyles.first;
907 block= uiBeginBlock(C, ar, "redo_popup", UI_EMBOSS);
908 uiBlockClearFlag(block, UI_BLOCK_LOOP);
909 uiBlockSetFlag(block, UI_BLOCK_KEEP_OPEN|UI_BLOCK_RET_1|UI_BLOCK_MOVEMOUSE_QUIT);
911 /* if register is not enabled, the operator gets freed on OPERATOR_FINISHED
912 * ui_apply_but_funcs_after calls ED_undo_operator_repeate_cb and crashes */
913 assert(op->type->flag & OPTYPE_REGISTER);
915 uiBlockSetHandleFunc(block, ED_undo_operator_repeat_cb_evt, arg_op);
916 layout= uiBlockLayout(block, UI_LAYOUT_VERTICAL, UI_LAYOUT_PANEL, 0, 0, width, 20, style);
918 if(ED_undo_valid(C, op->type->name)==0)
919 uiLayoutSetEnabled(layout, 0);
921 uiLayoutOperatorButs(C, layout, op, NULL, 'H', UI_LAYOUT_OP_SHOW_TITLE);
923 uiPopupBoundsBlock(block, 4, 0, 0);
924 uiEndBlock(C, block);
929 /* Only invoked by OK button in popups created with wm_block_create_dialog() */
930 static void dialog_exec_cb(bContext *C, void *arg1, void *arg2)
932 wmOperator *op= arg1;
933 uiBlock *block= arg2;
935 WM_operator_call(C, op);
937 uiPupBlockClose(C, block);
940 static void dialog_check_cb(bContext *C, void *op_ptr, void *UNUSED(arg))
942 wmOperator *op= op_ptr;
943 if(op->type->check) {
944 if(op->type->check(C, op)) {
950 /* Dialogs are popups that require user verification (click OK) before exec */
951 static uiBlock *wm_block_create_dialog(bContext *C, ARegion *ar, void *userData)
953 struct { wmOperator *op; int width; int height; } * data = userData;
954 wmOperator *op= data->op;
957 uiStyle *style= U.uistyles.first;
959 block = uiBeginBlock(C, ar, "operator dialog", UI_EMBOSS);
960 uiBlockClearFlag(block, UI_BLOCK_LOOP);
961 uiBlockSetFlag(block, UI_BLOCK_KEEP_OPEN|UI_BLOCK_RET_1|UI_BLOCK_MOVEMOUSE_QUIT);
963 layout= uiBlockLayout(block, UI_LAYOUT_VERTICAL, UI_LAYOUT_PANEL, 0, 0, data->width, data->height, style);
965 uiBlockSetFunc(block, dialog_check_cb, op, NULL);
967 uiLayoutOperatorButs(C, layout, op, NULL, 'H', UI_LAYOUT_OP_SHOW_TITLE);
969 /* clear so the OK button is left alone */
970 uiBlockSetFunc(block, NULL, NULL, NULL);
972 /* new column so as not to interfear with custom layouts [#26436] */
978 col= uiLayoutColumn(layout, FALSE);
979 col_block= uiLayoutGetBlock(col);
980 /* Create OK button, the callback of which will execute op */
981 btn= uiDefBut(col_block, BUT, 0, "OK", 0, -30, 0, 20, NULL, 0, 0, 0, 0, "");
982 uiButSetFunc(btn, dialog_exec_cb, op, col_block);
985 /* center around the mouse */
986 uiPopupBoundsBlock(block, 4, data->width/-2, data->height/2);
987 uiEndBlock(C, block);
992 static uiBlock *wm_operator_create_ui(bContext *C, ARegion *ar, void *userData)
994 struct { wmOperator *op; int width; int height; } * data = userData;
995 wmOperator *op= data->op;
998 uiStyle *style= U.uistyles.first;
1000 block= uiBeginBlock(C, ar, "opui_popup", UI_EMBOSS);
1001 uiBlockClearFlag(block, UI_BLOCK_LOOP);
1002 uiBlockSetFlag(block, UI_BLOCK_KEEP_OPEN|UI_BLOCK_RET_1|UI_BLOCK_MOVEMOUSE_QUIT);
1004 layout= uiBlockLayout(block, UI_LAYOUT_VERTICAL, UI_LAYOUT_PANEL, 0, 0, data->width, data->height, style);
1006 /* since ui is defined the auto-layout args are not used */
1007 uiLayoutOperatorButs(C, layout, op, NULL, 'V', 0);
1009 uiPopupBoundsBlock(block, 4, 0, 0);
1010 uiEndBlock(C, block);
1015 /* operator menu needs undo, for redo callback */
1016 int WM_operator_props_popup(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
1019 if((op->type->flag & OPTYPE_REGISTER)==0) {
1020 BKE_reportf(op->reports, RPT_ERROR, "Operator '%s' does not have register enabled, incorrect invoke function.", op->type->idname);
1021 return OPERATOR_CANCELLED;
1024 ED_undo_push_op(C, op);
1025 wm_operator_register(C, op);
1027 uiPupBlock(C, wm_block_create_redo, op);
1029 return OPERATOR_RUNNING_MODAL;
1032 int WM_operator_props_dialog_popup(bContext *C, wmOperator *op, int width, int height)
1034 struct { wmOperator *op; int width; int height; } data;
1038 data.height= height;
1040 /* op is not executed until popup OK but is clicked */
1041 uiPupBlock(C, wm_block_create_dialog, &data);
1043 return OPERATOR_RUNNING_MODAL;
1046 int WM_operator_ui_popup(bContext *C, wmOperator *op, int width, int height)
1048 struct { wmOperator *op; int width; int height; } data;
1051 data.height = height;
1052 uiPupBlock(C, wm_operator_create_ui, &data);
1053 return OPERATOR_RUNNING_MODAL;
1056 int WM_operator_redo_popup(bContext *C, wmOperator *op)
1058 /* CTX_wm_reports(C) because operator is on stack, not active in event system */
1059 if((op->type->flag & OPTYPE_REGISTER)==0) {
1060 BKE_reportf(CTX_wm_reports(C), RPT_ERROR, "Operator redo '%s' does not have register enabled, incorrect invoke function.", op->type->idname);
1061 return OPERATOR_CANCELLED;
1063 if(op->type->poll && op->type->poll(C)==0) {
1064 BKE_reportf(CTX_wm_reports(C), RPT_ERROR, "Operator redo '%s': wrong context.", op->type->idname);
1065 return OPERATOR_CANCELLED;
1068 uiPupBlock(C, wm_block_create_redo, op);
1070 return OPERATOR_CANCELLED;
1073 /* ***************** Debug menu ************************* */
1075 static int wm_debug_menu_exec(bContext *C, wmOperator *op)
1077 G.rt= RNA_int_get(op->ptr, "debug_value");
1078 ED_screen_refresh(CTX_wm_manager(C), CTX_wm_window(C));
1079 WM_event_add_notifier(C, NC_WINDOW, NULL);
1081 return OPERATOR_FINISHED;
1084 static int wm_debug_menu_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
1086 RNA_int_set(op->ptr, "debug_value", G.rt);
1087 return WM_operator_props_dialog_popup(C, op, 180, 20);
1090 static void WM_OT_debug_menu(wmOperatorType *ot)
1092 ot->name= "Debug Menu";
1093 ot->idname= "WM_OT_debug_menu";
1094 ot->description= "Open a popup to set the debug level";
1096 ot->invoke= wm_debug_menu_invoke;
1097 ot->exec= wm_debug_menu_exec;
1098 ot->poll= WM_operator_winactive;
1100 RNA_def_int(ot->srna, "debug_value", 0, -10000, 10000, "Debug Value", "", INT_MIN, INT_MAX);
1104 /* ***************** Splash Screen ************************* */
1106 static void wm_block_splash_close(bContext *C, void *arg_block, void *UNUSED(arg))
1108 uiPupBlockClose(C, arg_block);
1111 static uiBlock *wm_block_create_splash(bContext *C, ARegion *ar, void *arg_unused);
1113 /* XXX: hack to refresh splash screen with updated prest menu name,
1114 * since popup blocks don't get regenerated like panels do */
1115 static void wm_block_splash_refreshmenu (bContext *UNUSED(C), void *UNUSED(arg_block), void *UNUSED(arg))
1117 /* ugh, causes crashes in other buttons, disabling for now until
1119 uiPupBlockClose(C, arg_block);
1120 uiPupBlock(C, wm_block_create_splash, NULL);
1124 static int wm_resource_check_prev(void)
1127 char *res= BLI_get_folder_version(BLENDER_RESOURCE_PATH_USER, BLENDER_VERSION, TRUE);
1129 // if(res) printf("USER: %s\n", res);
1131 #if 0 /* ignore the local folder */
1133 /* with a local dir, copying old files isnt useful since local dir get priority for config */
1134 res= BLI_get_folder_version(BLENDER_RESOURCE_PATH_LOCAL, BLENDER_VERSION, TRUE);
1138 // if(res) printf("LOCAL: %s\n", res);
1143 return (BLI_get_folder_version(BLENDER_RESOURCE_PATH_USER, BLENDER_VERSION - 1, TRUE) != NULL);
1147 static uiBlock *wm_block_create_splash(bContext *C, ARegion *ar, void *UNUSED(arg))
1151 uiLayout *layout, *split, *col;
1152 uiStyle *style= U.uistyles.first;
1153 struct RecentFile *recent;
1155 MenuType *mt= WM_menutype_find("USERPREF_MT_splash", TRUE);
1158 #ifdef NAN_BUILDINFO
1159 int ver_width, rev_width;
1160 char *version_str = NULL;
1161 char *revision_str = NULL;
1162 char version_buf[128];
1163 char revision_buf[128];
1164 extern char build_rev[];
1166 version_str = &version_buf[0];
1167 revision_str = &revision_buf[0];
1169 sprintf(version_str, "%d.%02d.%d", BLENDER_VERSION/100, BLENDER_VERSION%100, BLENDER_SUBVERSION);
1170 sprintf(revision_str, "r%s", build_rev);
1172 BLF_size(style->widgetlabel.uifont_id, style->widgetlabel.points, U.dpi);
1173 ver_width = (int)BLF_width(style->widgetlabel.uifont_id, version_str) + 5;
1174 rev_width = (int)BLF_width(style->widgetlabel.uifont_id, revision_str) + 5;
1175 #endif //NAN_BUILDINFO
1177 block= uiBeginBlock(C, ar, "_popup", UI_EMBOSS);
1178 uiBlockSetFlag(block, UI_BLOCK_KEEP_OPEN);
1180 but= uiDefBut(block, BUT_IMAGE, 0, "", 0, 10, 501, 282, NULL, 0.0, 0.0, 0, 0, "");
1181 uiButSetFunc(but, wm_block_splash_close, block, NULL);
1182 uiBlockSetFunc(block, wm_block_splash_refreshmenu, block, NULL);
1184 #ifdef NAN_BUILDINFO
1185 uiDefBut(block, LABEL, 0, version_str, 494-ver_width, 282-24, ver_width, 20, NULL, 0, 0, 0, 0, NULL);
1186 uiDefBut(block, LABEL, 0, revision_str, 494-rev_width, 282-36, rev_width, 20, NULL, 0, 0, 0, 0, NULL);
1187 #endif //NAN_BUILDINFO
1189 layout= uiBlockLayout(block, UI_LAYOUT_VERTICAL, UI_LAYOUT_PANEL, 10, 2, 480, 110, style);
1191 uiBlockSetEmboss(block, UI_EMBOSS);
1192 /* show the splash menu (containing interaction presets), using python */
1195 menu.layout= layout;
1199 // wmWindowManager *wm= CTX_wm_manager(C);
1200 // uiItemM(layout, C, "USERPREF_MT_keyconfigs", U.keyconfigstr, ICON_NONE);
1203 uiBlockSetEmboss(block, UI_EMBOSSP);
1204 uiLayoutSetOperatorContext(layout, WM_OP_EXEC_REGION_WIN);
1206 split = uiLayoutSplit(layout, 0, 0);
1207 col = uiLayoutColumn(split, 0);
1208 uiItemL(col, "Links", ICON_NONE);
1209 uiItemStringO(col, "Donations", ICON_URL, "WM_OT_url_open", "url", "http://www.blender.org/blenderorg/blender-foundation/donation-payment/");
1210 uiItemStringO(col, "Release Log", ICON_URL, "WM_OT_url_open", "url", "http://www.blender.org/development/release-logs/blender-257/");
1211 uiItemStringO(col, "Manual", ICON_URL, "WM_OT_url_open", "url", "http://wiki.blender.org/index.php/Doc:2.5/Manual");
1212 uiItemStringO(col, "Blender Website", ICON_URL, "WM_OT_url_open", "url", "http://www.blender.org/");
1213 uiItemStringO(col, "User Community", ICON_URL, "WM_OT_url_open", "url", "http://www.blender.org/community/user-community/"); //
1214 if(strcmp(STRINGIFY(BLENDER_VERSION_CYCLE), "release")==0) {
1215 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);
1218 BLI_snprintf(url, sizeof(url), "http://www.blender.org/documentation/blender_python_api_%d_%d_%d", BLENDER_VERSION/100, BLENDER_VERSION%100, BLENDER_SUBVERSION);
1220 uiItemStringO(col, "Python API Reference", ICON_URL, "WM_OT_url_open", "url", url);
1221 uiItemL(col, "", ICON_NONE);
1223 col = uiLayoutColumn(split, 0);
1225 if(wm_resource_check_prev()) {
1226 uiItemO(col, NULL, ICON_NEW, "WM_OT_copy_prev_settings");
1230 uiItemL(col, "Recent", ICON_NONE);
1231 for(recent = G.recent_files.first, i=0; (i<5) && (recent); recent = recent->next, i++) {
1232 uiItemStringO(col, BLI_path_basename(recent->filepath), ICON_FILE_BLEND, "WM_OT_open_mainfile", "filepath", recent->filepath);
1236 uiItemO(col, NULL, ICON_RECOVER_LAST, "WM_OT_recover_last_session");
1237 uiItemL(col, "", ICON_NONE);
1239 uiCenteredBoundsBlock(block, 0);
1240 uiEndBlock(C, block);
1245 static int wm_splash_invoke(bContext *C, wmOperator *UNUSED(op), wmEvent *UNUSED(event))
1247 uiPupBlock(C, wm_block_create_splash, NULL);
1249 return OPERATOR_FINISHED;
1252 static void WM_OT_splash(wmOperatorType *ot)
1254 ot->name= "Splash Screen";
1255 ot->idname= "WM_OT_splash";
1256 ot->description= "Opens a blocking popup region with release info";
1258 ot->invoke= wm_splash_invoke;
1259 ot->poll= WM_operator_winactive;
1263 /* ***************** Search menu ************************* */
1264 static void operator_call_cb(struct bContext *C, void *UNUSED(arg1), void *arg2)
1266 wmOperatorType *ot= arg2;
1269 WM_operator_name_call(C, ot->idname, WM_OP_INVOKE_DEFAULT, NULL);
1272 static void operator_search_cb(const struct bContext *C, void *UNUSED(arg), const char *str, uiSearchItems *items)
1274 wmOperatorType *ot = WM_operatortype_first();
1276 for(; ot; ot= ot->next) {
1278 if(BLI_strcasestr(ot->name, str)) {
1279 if(WM_operator_poll((bContext*)C, ot)) {
1281 int len= strlen(ot->name);
1283 /* display name for menu, can hold hotkey */
1284 BLI_strncpy(name, ot->name, 256);
1286 /* check for hotkey */
1288 if(WM_key_event_operator_string(C, ot->idname, WM_OP_EXEC_DEFAULT, NULL, &name[len+1], 256-len-1))
1292 if(0==uiSearchItemAdd(items, name, ot, 0))
1299 static uiBlock *wm_block_search_menu(bContext *C, ARegion *ar, void *UNUSED(arg_op))
1301 static char search[256]= "";
1303 wmWindow *win= CTX_wm_window(C);
1307 block= uiBeginBlock(C, ar, "_popup", UI_EMBOSS);
1308 uiBlockSetFlag(block, UI_BLOCK_LOOP|UI_BLOCK_RET_1|UI_BLOCK_MOVEMOUSE_QUIT);
1310 but= uiDefSearchBut(block, search, 0, ICON_VIEWZOOM, 256, 10, 10, 180, 19, 0, 0, "");
1311 uiButSetSearchFunc(but, operator_search_cb, NULL, operator_call_cb, NULL);
1313 /* fake button, it holds space for search items */
1314 uiDefBut(block, LABEL, 0, "", 10, 10 - uiSearchBoxhHeight(), 180, uiSearchBoxhHeight(), NULL, 0, 0, 0, 0, NULL);
1316 uiPopupBoundsBlock(block, 6, 0, -20); /* move it downwards, mouse over button */
1317 uiEndBlock(C, block);
1319 event= *(win->eventstate); /* XXX huh huh? make api call */
1320 event.type= EVT_BUT_OPEN;
1321 event.val= KM_PRESS;
1322 event.customdata= but;
1323 event.customdatafree= FALSE;
1324 wm_event_add(win, &event);
1329 static int wm_search_menu_exec(bContext *UNUSED(C), wmOperator *UNUSED(op))
1331 return OPERATOR_FINISHED;
1334 static int wm_search_menu_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
1336 uiPupBlock(C, wm_block_search_menu, op);
1338 return OPERATOR_CANCELLED;
1342 static int wm_search_menu_poll(bContext *C)
1344 if(CTX_wm_window(C)==NULL) {
1348 ScrArea *sa= CTX_wm_area(C);
1350 if(sa->spacetype==SPACE_CONSOLE) return 0; // XXX - so we can use the shortcut in the console
1351 if(sa->spacetype==SPACE_TEXT) return 0; // XXX - so we can use the spacebar in the text editor
1354 Object *editob= CTX_data_edit_object(C);
1355 if(editob && editob->type==OB_FONT) return 0; // XXX - so we can use the spacebar for entering text
1361 static void WM_OT_search_menu(wmOperatorType *ot)
1363 ot->name= "Search Menu";
1364 ot->idname= "WM_OT_search_menu";
1366 ot->invoke= wm_search_menu_invoke;
1367 ot->exec= wm_search_menu_exec;
1368 ot->poll= wm_search_menu_poll;
1371 static int wm_call_menu_exec(bContext *C, wmOperator *op)
1373 char idname[BKE_ST_MAXNAME];
1374 RNA_string_get(op->ptr, "name", idname);
1376 uiPupMenuInvoke(C, idname);
1378 return OPERATOR_CANCELLED;
1381 static void WM_OT_call_menu(wmOperatorType *ot)
1383 ot->name= "Call Menu";
1384 ot->idname= "WM_OT_call_menu";
1386 ot->exec= wm_call_menu_exec;
1387 ot->poll= WM_operator_winactive;
1389 RNA_def_string(ot->srna, "name", "", BKE_ST_MAXNAME, "Name", "Name of the menu");
1392 /* ************ window / screen operator definitions ************** */
1394 /* this poll functions is needed in place of WM_operator_winactive
1395 * while it crashes on full screen */
1396 static int wm_operator_winactive_normal(bContext *C)
1398 wmWindow *win= CTX_wm_window(C);
1400 if(win==NULL || win->screen==NULL || win->screen->full != SCREENNORMAL)
1406 static void WM_OT_window_duplicate(wmOperatorType *ot)
1408 ot->name= "Duplicate Window";
1409 ot->idname= "WM_OT_window_duplicate";
1410 ot->description="Duplicate the current Blender window";
1412 ot->exec= wm_window_duplicate_exec;
1413 ot->poll= wm_operator_winactive_normal;
1416 static void WM_OT_save_homefile(wmOperatorType *ot)
1418 ot->name= "Save User Settings";
1419 ot->idname= "WM_OT_save_homefile";
1420 ot->description="Make the current file the default .blend file";
1422 ot->invoke= WM_operator_confirm;
1423 ot->exec= WM_write_homefile;
1424 ot->poll= WM_operator_winactive;
1427 static void WM_OT_read_homefile(wmOperatorType *ot)
1429 ot->name= "Reload Start-Up File";
1430 ot->idname= "WM_OT_read_homefile";
1431 ot->description="Open the default file (doesn't save the current file)";
1433 ot->invoke= WM_operator_confirm;
1434 ot->exec= WM_read_homefile_exec;
1435 /* ommit poll to run in background mode */
1438 static void WM_OT_read_factory_settings(wmOperatorType *ot)
1440 ot->name= "Load Factory Settings";
1441 ot->idname= "WM_OT_read_factory_settings";
1442 ot->description="Load default file and user preferences";
1444 ot->invoke= WM_operator_confirm;
1445 ot->exec= WM_read_homefile_exec;
1446 /* ommit poll to run in background mode */
1449 /* *************** open file **************** */
1451 static void open_set_load_ui(wmOperator *op)
1453 if(!RNA_property_is_set(op->ptr, "load_ui"))
1454 RNA_boolean_set(op->ptr, "load_ui", !(U.flag & USER_FILENOUI));
1457 static void open_set_use_scripts(wmOperator *op)
1459 if(!RNA_property_is_set(op->ptr, "use_scripts")) {
1460 /* use G_SCRIPT_AUTOEXEC rather then the userpref because this means if
1461 * the flag has been disabled from the command line, then opening
1462 * from the menu wont enable this setting. */
1463 RNA_boolean_set(op->ptr, "use_scripts", (G.f & G_SCRIPT_AUTOEXEC));
1467 static int wm_open_mainfile_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
1469 const char *openname= G.main->name;
1471 /* if possible, get the name of the most recently used .blend file */
1472 if (G.recent_files.first) {
1473 struct RecentFile *recent = G.recent_files.first;
1474 openname = recent->filepath;
1477 RNA_string_set(op->ptr, "filepath", openname);
1478 open_set_load_ui(op);
1479 open_set_use_scripts(op);
1481 WM_event_add_fileselect(C, op);
1483 return OPERATOR_RUNNING_MODAL;
1486 static int wm_open_mainfile_exec(bContext *C, wmOperator *op)
1488 char path[FILE_MAX];
1490 RNA_string_get(op->ptr, "filepath", path);
1491 open_set_load_ui(op);
1492 open_set_use_scripts(op);
1494 if(RNA_boolean_get(op->ptr, "load_ui"))
1495 G.fileflags &= ~G_FILE_NO_UI;
1497 G.fileflags |= G_FILE_NO_UI;
1499 if(RNA_boolean_get(op->ptr, "use_scripts"))
1500 G.f |= G_SCRIPT_AUTOEXEC;
1502 G.f &= ~G_SCRIPT_AUTOEXEC;
1504 // XXX wm in context is not set correctly after WM_read_file -> crash
1505 // do it before for now, but is this correct with multiple windows?
1506 WM_event_add_notifier(C, NC_WINDOW, NULL);
1508 WM_read_file(C, path, op->reports);
1510 return OPERATOR_FINISHED;
1513 static void WM_OT_open_mainfile(wmOperatorType *ot)
1515 ot->name= "Open Blender File";
1516 ot->idname= "WM_OT_open_mainfile";
1517 ot->description="Open a Blender file";
1519 ot->invoke= wm_open_mainfile_invoke;
1520 ot->exec= wm_open_mainfile_exec;
1521 ot->poll= WM_operator_winactive;
1523 WM_operator_properties_filesel(ot, FOLDERFILE|BLENDERFILE, FILE_BLENDER, FILE_OPENFILE, WM_FILESEL_FILEPATH);
1525 RNA_def_boolean(ot->srna, "load_ui", 1, "Load UI", "Load user interface setup in the .blend file");
1526 RNA_def_boolean(ot->srna, "use_scripts", 1, "Trusted Source", "Allow blend file execute scripts automatically, default available from system preferences");
1529 /* **************** link/append *************** */
1531 static int wm_link_append_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
1533 if(!RNA_property_is_set(op->ptr, "relative_path"))
1534 RNA_boolean_set(op->ptr, "relative_path", U.flag & USER_RELPATHS);
1536 if(RNA_property_is_set(op->ptr, "filepath")) {
1537 return WM_operator_call(C, op);
1540 /* XXX TODO solve where to get last linked library from */
1541 RNA_string_set(op->ptr, "filepath", G.lib);
1542 WM_event_add_fileselect(C, op);
1543 return OPERATOR_RUNNING_MODAL;
1547 static short wm_link_append_flag(wmOperator *op)
1551 if(RNA_boolean_get(op->ptr, "autoselect")) flag |= FILE_AUTOSELECT;
1552 if(RNA_boolean_get(op->ptr, "active_layer")) flag |= FILE_ACTIVELAY;
1553 if(RNA_boolean_get(op->ptr, "relative_path")) flag |= FILE_RELPATH;
1554 if(RNA_boolean_get(op->ptr, "link")) flag |= FILE_LINK;
1555 if(RNA_boolean_get(op->ptr, "instance_groups")) flag |= FILE_GROUP_INSTANCE;
1560 static int wm_link_append_exec(bContext *C, wmOperator *op)
1562 Main *bmain= CTX_data_main(C);
1563 Scene *scene= CTX_data_scene(C);
1567 char name[FILE_MAX], dir[FILE_MAX], libname[FILE_MAX], group[GROUP_MAX];
1568 int idcode, totfiles=0;
1572 RNA_string_get(op->ptr, "filename", name);
1573 RNA_string_get(op->ptr, "directory", dir);
1575 /* test if we have a valid data */
1576 if(BLO_is_a_library(dir, libname, group) == 0) {
1577 BKE_report(op->reports, RPT_ERROR, "Not a library");
1578 return OPERATOR_CANCELLED;
1580 else if(group[0] == 0) {
1581 BKE_report(op->reports, RPT_ERROR, "Nothing indicated");
1582 return OPERATOR_CANCELLED;
1584 else if(BLI_path_cmp(bmain->name, libname) == 0) {
1585 BKE_report(op->reports, RPT_ERROR, "Cannot use current file as library");
1586 return OPERATOR_CANCELLED;
1589 /* check if something is indicated for append/link */
1590 prop = RNA_struct_find_property(op->ptr, "files");
1592 totfiles= RNA_property_collection_length(op->ptr, prop);
1594 if(name[0] == '\0') {
1595 BKE_report(op->reports, RPT_ERROR, "Nothing indicated");
1596 return OPERATOR_CANCELLED;
1600 else if(name[0] == '\0') {
1601 BKE_report(op->reports, RPT_ERROR, "Nothing indicated");
1602 return OPERATOR_CANCELLED;
1605 bh = BLO_blendhandle_from_file(libname, op->reports);
1608 /* unlikely since we just browsed it, but possible
1609 * error reports will have been made by BLO_blendhandle_from_file() */
1610 return OPERATOR_CANCELLED;
1614 /* from here down, no error returns */
1616 idcode = BKE_idcode_from_name(group);
1618 /* now we have or selected, or an indicated file */
1619 if(RNA_boolean_get(op->ptr, "autoselect"))
1620 scene_deselect_all(scene);
1623 flag = wm_link_append_flag(op);
1625 /* sanity checks for flag */
1626 if(scene->id.lib && (flag & FILE_GROUP_INSTANCE)) {
1627 /* TODO, user never gets this message */
1628 BKE_reportf(op->reports, RPT_WARNING, "Scene '%s' is linked, group instance disabled", scene->id.name+2);
1629 flag &= ~FILE_GROUP_INSTANCE;
1633 /* tag everything, all untagged data can be made local
1634 * its also generally useful to know what is new
1636 * take extra care flag_all_listbases_ids(LIB_LINK_TAG, 0) is called after! */
1637 flag_all_listbases_ids(LIB_PRE_EXISTING, 1);
1639 /* here appending/linking starts */
1640 mainl = BLO_library_append_begin(C, &bh, libname);
1642 BLO_library_append_named_part(C, mainl, &bh, name, idcode, flag);
1645 RNA_BEGIN(op->ptr, itemptr, "files") {
1646 RNA_string_get(&itemptr, "name", name);
1647 BLO_library_append_named_part(C, mainl, &bh, name, idcode, flag);
1651 BLO_library_append_end(C, mainl, &bh, idcode, flag);
1653 /* mark all library linked objects to be updated */
1654 recalc_all_library_objects(bmain);
1656 /* append, rather than linking */
1657 if((flag & FILE_LINK)==0) {
1658 Library *lib= BLI_findstring(&bmain->library, libname, offsetof(Library, filepath));
1659 if(lib) all_local(lib, 1);
1660 else BLI_assert(!"cant find name of just added library!");
1663 /* important we unset, otherwise these object wont
1664 * link into other scenes from this blend file */
1665 flag_all_listbases_ids(LIB_PRE_EXISTING, 0);
1667 /* recreate dependency graph to include new objects */
1668 DAG_scene_sort(bmain, scene);
1669 DAG_ids_flush_update(bmain, 0);
1671 BLO_blendhandle_close(bh);
1673 /* XXX TODO: align G.lib with other directory storage (like last opened image etc...) */
1674 BLI_strncpy(G.lib, dir, FILE_MAX);
1676 WM_event_add_notifier(C, NC_WINDOW, NULL);
1678 return OPERATOR_FINISHED;
1681 static void WM_OT_link_append(wmOperatorType *ot)
1683 ot->name= "Link/Append from Library";
1684 ot->idname= "WM_OT_link_append";
1685 ot->description= "Link or Append from a Library .blend file";
1687 ot->invoke= wm_link_append_invoke;
1688 ot->exec= wm_link_append_exec;
1689 ot->poll= WM_operator_winactive;
1691 ot->flag |= OPTYPE_UNDO;
1693 WM_operator_properties_filesel(ot, FOLDERFILE|BLENDERFILE, FILE_LOADLIB, FILE_OPENFILE, WM_FILESEL_FILEPATH|WM_FILESEL_DIRECTORY|WM_FILESEL_FILENAME| WM_FILESEL_RELPATH);
1695 RNA_def_boolean(ot->srna, "link", 1, "Link", "Link the objects or datablocks rather than appending");
1696 RNA_def_boolean(ot->srna, "autoselect", 1, "Select", "Select the linked objects");
1697 RNA_def_boolean(ot->srna, "active_layer", 1, "Active Layer", "Put the linked objects on the active layer");
1698 RNA_def_boolean(ot->srna, "instance_groups", 1, "Instance Groups", "Create instances for each group as a DupliGroup");
1700 RNA_def_collection_runtime(ot->srna, "files", &RNA_OperatorFileListElement, "Files", "");
1703 /* *************** recover last session **************** */
1705 static int wm_recover_last_session_exec(bContext *C, wmOperator *op)
1707 char filename[FILE_MAX];
1709 G.fileflags |= G_FILE_RECOVER;
1711 // XXX wm in context is not set correctly after WM_read_file -> crash
1712 // do it before for now, but is this correct with multiple windows?
1713 WM_event_add_notifier(C, NC_WINDOW, NULL);
1716 BLI_make_file_string("/", filename, btempdir, "quit.blend");
1717 WM_read_file(C, filename, op->reports);
1719 G.fileflags &= ~G_FILE_RECOVER;
1720 return OPERATOR_FINISHED;
1723 static void WM_OT_recover_last_session(wmOperatorType *ot)
1725 ot->name= "Recover Last Session";
1726 ot->idname= "WM_OT_recover_last_session";
1727 ot->description="Open the last closed file (\"quit.blend\")";
1729 ot->exec= wm_recover_last_session_exec;
1730 ot->poll= WM_operator_winactive;
1733 /* *************** recover auto save **************** */
1735 static int wm_recover_auto_save_exec(bContext *C, wmOperator *op)
1737 char path[FILE_MAX];
1739 RNA_string_get(op->ptr, "filepath", path);
1741 G.fileflags |= G_FILE_RECOVER;
1743 // XXX wm in context is not set correctly after WM_read_file -> crash
1744 // do it before for now, but is this correct with multiple windows?
1745 WM_event_add_notifier(C, NC_WINDOW, NULL);
1748 WM_read_file(C, path, op->reports);
1750 G.fileflags &= ~G_FILE_RECOVER;
1752 return OPERATOR_FINISHED;
1755 static int wm_recover_auto_save_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
1757 char filename[FILE_MAX];
1759 wm_autosave_location(filename);
1760 RNA_string_set(op->ptr, "filepath", filename);
1761 WM_event_add_fileselect(C, op);
1763 return OPERATOR_RUNNING_MODAL;
1766 static void WM_OT_recover_auto_save(wmOperatorType *ot)
1768 ot->name= "Recover Auto Save";
1769 ot->idname= "WM_OT_recover_auto_save";
1770 ot->description="Open an automatically saved file to recover it";
1772 ot->exec= wm_recover_auto_save_exec;
1773 ot->invoke= wm_recover_auto_save_invoke;
1774 ot->poll= WM_operator_winactive;
1776 WM_operator_properties_filesel(ot, BLENDERFILE, FILE_BLENDER, FILE_OPENFILE, WM_FILESEL_FILEPATH);
1779 /* *************** save file as **************** */
1781 static void untitled(char *name)
1783 if(G.save_over == 0 && strlen(name) < FILE_MAX-16) {
1784 char *c= BLI_last_slash(name);
1787 strcpy(&c[1], "untitled.blend");
1789 strcpy(name, "untitled.blend");
1793 static void save_set_compress(wmOperator *op)
1795 if(!RNA_property_is_set(op->ptr, "compress")) {
1796 if(G.save_over) /* keep flag for existing file */
1797 RNA_boolean_set(op->ptr, "compress", G.fileflags & G_FILE_COMPRESS);
1798 else /* use userdef for new file */
1799 RNA_boolean_set(op->ptr, "compress", U.flag & USER_FILECOMPRESS);
1803 static int wm_save_as_mainfile_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
1805 char name[FILE_MAX];
1807 save_set_compress(op);
1809 /* if not saved before, get the name of the most recently used .blend file */
1810 if(G.main->name[0]==0 && G.recent_files.first) {
1811 struct RecentFile *recent = G.recent_files.first;
1812 BLI_strncpy(name, recent->filepath, FILE_MAX);
1815 BLI_strncpy(name, G.main->name, FILE_MAX);
1818 RNA_string_set(op->ptr, "filepath", name);
1820 WM_event_add_fileselect(C, op);
1822 return OPERATOR_RUNNING_MODAL;
1825 /* function used for WM_OT_save_mainfile too */
1826 static int wm_save_as_mainfile_exec(bContext *C, wmOperator *op)
1828 char path[FILE_MAX];
1832 save_set_compress(op);
1834 if(RNA_property_is_set(op->ptr, "filepath"))
1835 RNA_string_get(op->ptr, "filepath", path);
1837 BLI_strncpy(path, G.main->name, FILE_MAX);
1841 if(RNA_property_is_set(op->ptr, "copy"))
1842 copy = RNA_boolean_get(op->ptr, "copy");
1844 fileflags= G.fileflags;
1846 /* set compression flag */
1847 if(RNA_boolean_get(op->ptr, "compress")) fileflags |= G_FILE_COMPRESS;
1848 else fileflags &= ~G_FILE_COMPRESS;
1849 if(RNA_boolean_get(op->ptr, "relative_remap")) fileflags |= G_FILE_RELATIVE_REMAP;
1850 else fileflags &= ~G_FILE_RELATIVE_REMAP;
1852 if ( WM_write_file(C, path, fileflags, op->reports, copy) != 0)
1853 return OPERATOR_CANCELLED;
1855 WM_event_add_notifier(C, NC_WM|ND_FILESAVE, NULL);
1857 return OPERATOR_FINISHED;
1860 /* function used for WM_OT_save_mainfile too */
1861 static int blend_save_check(bContext *UNUSED(C), wmOperator *op)
1863 char filepath[FILE_MAX];
1864 RNA_string_get(op->ptr, "filepath", filepath);
1865 if(BLI_replace_extension(filepath, sizeof(filepath), ".blend")) {
1866 RNA_string_set(op->ptr, "filepath", filepath);
1872 static void WM_OT_save_as_mainfile(wmOperatorType *ot)
1874 ot->name= "Save As Blender File";
1875 ot->idname= "WM_OT_save_as_mainfile";
1876 ot->description="Save the current file in the desired location";
1878 ot->invoke= wm_save_as_mainfile_invoke;
1879 ot->exec= wm_save_as_mainfile_exec;
1880 ot->check= blend_save_check;
1881 /* ommit window poll so this can work in background mode */
1883 WM_operator_properties_filesel(ot, FOLDERFILE|BLENDERFILE, FILE_BLENDER, FILE_SAVE, WM_FILESEL_FILEPATH);
1884 RNA_def_boolean(ot->srna, "compress", 0, "Compress", "Write compressed .blend file");
1885 RNA_def_boolean(ot->srna, "relative_remap", 1, "Remap Relative", "Remap relative paths when saving in a different directory");
1886 RNA_def_boolean(ot->srna, "copy", 0, "Save Copy", "Save a copy of the actual working state but does not make saved file active.");
1889 /* *************** save file directly ******** */
1891 static int wm_save_mainfile_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
1893 char name[FILE_MAX];
1894 int check_existing=1;
1896 /* cancel if no active window */
1897 if (CTX_wm_window(C) == NULL)
1898 return OPERATOR_CANCELLED;
1900 save_set_compress(op);
1902 /* if not saved before, get the name of the most recently used .blend file */
1903 if(G.main->name[0]==0 && G.recent_files.first) {
1904 struct RecentFile *recent = G.recent_files.first;
1905 BLI_strncpy(name, recent->filepath, FILE_MAX);
1908 BLI_strncpy(name, G.main->name, FILE_MAX);
1912 RNA_string_set(op->ptr, "filepath", name);
1914 if (RNA_struct_find_property(op->ptr, "check_existing"))
1915 if (RNA_boolean_get(op->ptr, "check_existing")==0)
1920 uiPupMenuSaveOver(C, op, name);
1922 wm_save_as_mainfile_exec(C, op);
1925 WM_event_add_fileselect(C, op);
1928 return OPERATOR_RUNNING_MODAL;
1931 static void WM_OT_save_mainfile(wmOperatorType *ot)
1933 ot->name= "Save Blender File";
1934 ot->idname= "WM_OT_save_mainfile";
1935 ot->description="Save the current Blender file";
1937 ot->invoke= wm_save_mainfile_invoke;
1938 ot->exec= wm_save_as_mainfile_exec;
1939 ot->check= blend_save_check;
1942 WM_operator_properties_filesel(ot, FOLDERFILE|BLENDERFILE, FILE_BLENDER, FILE_SAVE, WM_FILESEL_FILEPATH);
1943 RNA_def_boolean(ot->srna, "compress", 0, "Compress", "Write compressed .blend file");
1944 RNA_def_boolean(ot->srna, "relative_remap", 0, "Remap Relative", "Remap relative paths when saving in a different directory");
1947 /* XXX: move these collada operators to a more appropriate place */
1950 #include "../../collada/collada.h"
1952 static int wm_collada_export_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
1954 if(!RNA_property_is_set(op->ptr, "filepath")) {
1955 char filepath[FILE_MAX];
1956 BLI_strncpy(filepath, G.main->name, sizeof(filepath));
1957 BLI_replace_extension(filepath, sizeof(filepath), ".dae");
1958 RNA_string_set(op->ptr, "filepath", filepath);
1961 WM_event_add_fileselect(C, op);
1963 return OPERATOR_RUNNING_MODAL;
1966 /* function used for WM_OT_save_mainfile too */
1967 static int wm_collada_export_exec(bContext *C, wmOperator *op)
1969 char filename[FILE_MAX];
1971 if(!RNA_property_is_set(op->ptr, "filepath")) {
1972 BKE_report(op->reports, RPT_ERROR, "No filename given");
1973 return OPERATOR_CANCELLED;
1976 RNA_string_get(op->ptr, "filepath", filename);
1977 if(collada_export(CTX_data_scene(C), filename)) {
1978 return OPERATOR_FINISHED;
1981 return OPERATOR_CANCELLED;
1985 static void WM_OT_collada_export(wmOperatorType *ot)
1987 ot->name= "Export COLLADA";
1988 ot->idname= "WM_OT_collada_export";
1990 ot->invoke= wm_collada_export_invoke;
1991 ot->exec= wm_collada_export_exec;
1992 ot->poll= WM_operator_winactive;
1994 WM_operator_properties_filesel(ot, FOLDERFILE|COLLADAFILE, FILE_BLENDER, FILE_SAVE, WM_FILESEL_FILEPATH);
1997 /* function used for WM_OT_save_mainfile too */
1998 static int wm_collada_import_exec(bContext *C, wmOperator *op)
2000 char filename[FILE_MAX];
2002 if(!RNA_property_is_set(op->ptr, "filepath")) {
2003 BKE_report(op->reports, RPT_ERROR, "No filename given");
2004 return OPERATOR_CANCELLED;
2007 RNA_string_get(op->ptr, "filepath", filename);
2008 collada_import(C, filename);
2010 return OPERATOR_FINISHED;
2013 static void WM_OT_collada_import(wmOperatorType *ot)
2015 ot->name= "Import COLLADA";
2016 ot->idname= "WM_OT_collada_import";
2018 ot->invoke= WM_operator_filesel;
2019 ot->exec= wm_collada_import_exec;
2020 ot->poll= WM_operator_winactive;
2022 WM_operator_properties_filesel(ot, FOLDERFILE|COLLADAFILE, FILE_BLENDER, FILE_OPENFILE, WM_FILESEL_FILEPATH);
2028 /* *********************** */
2030 static void WM_OT_window_fullscreen_toggle(wmOperatorType *ot)
2032 ot->name= "Toggle Fullscreen";
2033 ot->idname= "WM_OT_window_fullscreen_toggle";
2034 ot->description="Toggle the current window fullscreen";
2036 ot->exec= wm_window_fullscreen_toggle_exec;
2037 ot->poll= WM_operator_winactive;
2040 static int wm_exit_blender_op(bContext *C, wmOperator *op)
2042 WM_operator_free(op);
2046 return OPERATOR_FINISHED;
2049 static void WM_OT_quit_blender(wmOperatorType *ot)
2051 ot->name= "Quit Blender";
2052 ot->idname= "WM_OT_quit_blender";
2053 ot->description= "Quit Blender";
2055 ot->invoke= WM_operator_confirm;
2056 ot->exec= wm_exit_blender_op;
2057 ot->poll= WM_operator_winactive;
2060 /* *********************** */
2064 static int wm_console_toggle_op(bContext *UNUSED(C), wmOperator *UNUSED(op))
2066 GHOST_toggleConsole(2);
2067 return OPERATOR_FINISHED;
2070 static void WM_OT_console_toggle(wmOperatorType *ot)
2072 ot->name= "Toggle System Console";
2073 ot->idname= "WM_OT_console_toggle";
2074 ot->description= "Toggle System Console";
2076 ot->exec= wm_console_toggle_op;
2077 ot->poll= WM_operator_winactive;
2082 /* ************ default paint cursors, draw always around cursor *********** */
2084 - returns handler to free
2085 - poll(bContext): returns 1 if draw should happen
2086 - draw(bContext): drawing callback for paint cursor
2089 void *WM_paint_cursor_activate(wmWindowManager *wm, int (*poll)(bContext *C),
2090 wmPaintCursorDraw draw, void *customdata)
2092 wmPaintCursor *pc= MEM_callocN(sizeof(wmPaintCursor), "paint cursor");
2094 BLI_addtail(&wm->paintcursors, pc);
2096 pc->customdata = customdata;
2103 void WM_paint_cursor_end(wmWindowManager *wm, void *handle)
2107 for(pc= wm->paintcursors.first; pc; pc= pc->next) {
2108 if(pc == (wmPaintCursor *)handle) {
2109 BLI_remlink(&wm->paintcursors, pc);
2116 /* ************ window gesture operator-callback definitions ************** */
2118 * These are default callbacks for use in operators requiring gesture input
2121 /* **************** Border gesture *************** */
2123 /* Border gesture has two types:
2124 1) WM_GESTURE_CROSS_RECT: starts a cross, on mouse click it changes to border
2125 2) WM_GESTURE_RECT: starts immediate as a border, on mouse click or release it ends
2127 It stores 4 values (xmin, xmax, ymin, ymax) and event it ended with (event_type)
2130 static int border_apply_rect(wmOperator *op)
2132 wmGesture *gesture= op->customdata;
2133 rcti *rect= gesture->customdata;
2135 if(rect->xmin==rect->xmax || rect->ymin==rect->ymax)
2139 /* operator arguments and storage. */
2140 RNA_int_set(op->ptr, "xmin", MIN2(rect->xmin, rect->xmax) );
2141 RNA_int_set(op->ptr, "ymin", MIN2(rect->ymin, rect->ymax) );
2142 RNA_int_set(op->ptr, "xmax", MAX2(rect->xmin, rect->xmax) );
2143 RNA_int_set(op->ptr, "ymax", MAX2(rect->ymin, rect->ymax) );
2148 static int border_apply(bContext *C, wmOperator *op, int gesture_mode)
2150 if (!border_apply_rect(op))
2153 /* XXX weak; border should be configured for this without reading event types */
2154 if( RNA_struct_find_property(op->ptr, "gesture_mode") )
2155 RNA_int_set(op->ptr, "gesture_mode", gesture_mode);
2157 op->type->exec(C, op);
2161 static void wm_gesture_end(bContext *C, wmOperator *op)
2163 wmGesture *gesture= op->customdata;
2165 WM_gesture_end(C, gesture); /* frees gesture itself, and unregisters from window */
2166 op->customdata= NULL;
2168 ED_area_tag_redraw(CTX_wm_area(C));
2170 if( RNA_struct_find_property(op->ptr, "cursor") )
2171 WM_cursor_restore(CTX_wm_window(C));
2174 int WM_border_select_invoke(bContext *C, wmOperator *op, wmEvent *event)
2176 if(ISTWEAK(event->type))
2177 op->customdata= WM_gesture_new(C, event, WM_GESTURE_RECT);
2179 op->customdata= WM_gesture_new(C, event, WM_GESTURE_CROSS_RECT);
2181 /* add modal handler */
2182 WM_event_add_modal_handler(C, op);
2184 wm_gesture_tag_redraw(C);
2186 return OPERATOR_RUNNING_MODAL;
2189 int WM_border_select_modal(bContext *C, wmOperator *op, wmEvent *event)
2191 wmGesture *gesture= op->customdata;
2192 rcti *rect= gesture->customdata;
2195 if(event->type== MOUSEMOVE) {
2196 wm_subwindow_getorigin(CTX_wm_window(C), gesture->swinid, &sx, &sy);
2198 if(gesture->type==WM_GESTURE_CROSS_RECT && gesture->mode==0) {
2199 rect->xmin= rect->xmax= event->x - sx;
2200 rect->ymin= rect->ymax= event->y - sy;
2203 rect->xmax= event->x - sx;
2204 rect->ymax= event->y - sy;
2206 border_apply_rect(op);
2208 wm_gesture_tag_redraw(C);
2210 else if (event->type==EVT_MODAL_MAP) {
2211 switch (event->val) {
2212 case GESTURE_MODAL_BEGIN:
2213 if(gesture->type==WM_GESTURE_CROSS_RECT && gesture->mode==0) {
2215 wm_gesture_tag_redraw(C);
2218 case GESTURE_MODAL_SELECT:
2219 case GESTURE_MODAL_DESELECT:
2220 case GESTURE_MODAL_IN:
2221 case GESTURE_MODAL_OUT:
2222 if(border_apply(C, op, event->val)) {
2223 wm_gesture_end(C, op);
2224 return OPERATOR_FINISHED;
2226 wm_gesture_end(C, op);
2227 return OPERATOR_CANCELLED;
2230 case GESTURE_MODAL_CANCEL:
2231 wm_gesture_end(C, op);
2232 return OPERATOR_CANCELLED;
2236 // // Allow view navigation???
2238 // return OPERATOR_PASS_THROUGH;
2241 return OPERATOR_RUNNING_MODAL;
2244 /* **************** circle gesture *************** */
2245 /* works now only for selection or modal paint stuff, calls exec while hold mouse, exit on release */
2247 #ifdef GESTURE_MEMORY
2248 int circle_select_size= 25; // XXX - need some operator memory thing\!
2251 int WM_gesture_circle_invoke(bContext *C, wmOperator *op, wmEvent *event)
2253 op->customdata= WM_gesture_new(C, event, WM_GESTURE_CIRCLE);
2255 /* add modal handler */
2256 WM_event_add_modal_handler(C, op);
2258 wm_gesture_tag_redraw(C);
2260 return OPERATOR_RUNNING_MODAL;
2263 static void gesture_circle_apply(bContext *C, wmOperator *op)
2265 wmGesture *gesture= op->customdata;
2266 rcti *rect= gesture->customdata;
2268 if(RNA_int_get(op->ptr, "gesture_mode")==GESTURE_MODAL_NOP)
2271 /* operator arguments and storage. */
2272 RNA_int_set(op->ptr, "x", rect->xmin);
2273 RNA_int_set(op->ptr, "y", rect->ymin);
2274 RNA_int_set(op->ptr, "radius", rect->xmax);
2277 op->type->exec(C, op);
2279 #ifdef GESTURE_MEMORY
2280 circle_select_size= rect->xmax;
2284 int WM_gesture_circle_modal(bContext *C, wmOperator *op, wmEvent *event)
2286 wmGesture *gesture= op->customdata;
2287 rcti *rect= gesture->customdata;
2290 if(event->type== MOUSEMOVE) {
2291 wm_subwindow_getorigin(CTX_wm_window(C), gesture->swinid, &sx, &sy);
2293 rect->xmin= event->x - sx;
2294 rect->ymin= event->y - sy;
2296 wm_gesture_tag_redraw(C);
2299 gesture_circle_apply(C, op);
2301 else if (event->type==EVT_MODAL_MAP) {
2302 switch (event->val) {
2303 case GESTURE_MODAL_CIRCLE_ADD:
2304 rect->xmax += 2 + rect->xmax/10;
2305 wm_gesture_tag_redraw(C);
2307 case GESTURE_MODAL_CIRCLE_SUB:
2308 rect->xmax -= 2 + rect->xmax/10;
2309 if(rect->xmax < 1) rect->xmax= 1;
2310 wm_gesture_tag_redraw(C);
2312 case GESTURE_MODAL_SELECT:
2313 case GESTURE_MODAL_DESELECT:
2314 case GESTURE_MODAL_NOP:
2315 if(RNA_struct_find_property(op->ptr, "gesture_mode"))
2316 RNA_int_set(op->ptr, "gesture_mode", event->val);
2318 if(event->val != GESTURE_MODAL_NOP) {
2319 /* apply first click */
2320 gesture_circle_apply(C, op);
2322 wm_gesture_tag_redraw(C);
2326 case GESTURE_MODAL_CANCEL:
2327 case GESTURE_MODAL_CONFIRM:
2328 wm_gesture_end(C, op);
2329 return OPERATOR_FINISHED; /* use finish or we dont get an undo */
2332 // // Allow view navigation???
2334 // return OPERATOR_PASS_THROUGH;
2337 return OPERATOR_RUNNING_MODAL;
2341 /* template to copy from */
2342 void WM_OT_circle_gesture(wmOperatorType *ot)
2344 ot->name= "Circle Gesture";
2345 ot->idname= "WM_OT_circle_gesture";
2346 ot->description="Enter rotate mode with a circular gesture";
2348 ot->invoke= WM_gesture_circle_invoke;
2349 ot->modal= WM_gesture_circle_modal;
2351 ot->poll= WM_operator_winactive;
2353 RNA_def_property(ot->srna, "x", PROP_INT, PROP_NONE);
2354 RNA_def_property(ot->srna, "y", PROP_INT, PROP_NONE);
2355 RNA_def_property(ot->srna, "radius", PROP_INT, PROP_NONE);
2360 /* **************** Tweak gesture *************** */
2362 static void tweak_gesture_modal(bContext *C, wmEvent *event)
2364 wmWindow *window= CTX_wm_window(C);
2365 wmGesture *gesture= window->tweak;
2366 rcti *rect= gesture->customdata;
2369 switch(event->type) {
2371 case INBETWEEN_MOUSEMOVE:
2373 wm_subwindow_getorigin(window, gesture->swinid, &sx, &sy);
2375 rect->xmax= event->x - sx;
2376 rect->ymax= event->y - sy;
2378 if((val= wm_gesture_evaluate(gesture))) {
2381 tevent= *(window->eventstate);
2382 if(gesture->event_type==LEFTMOUSE)
2383 tevent.type= EVT_TWEAK_L;
2384 else if(gesture->event_type==RIGHTMOUSE)
2385 tevent.type= EVT_TWEAK_R;
2387 tevent.type= EVT_TWEAK_M;
2390 wm_event_add(window, &tevent);
2392 WM_gesture_end(C, gesture); /* frees gesture itself, and unregisters from window */
2400 if(gesture->event_type==event->type) {
2401 WM_gesture_end(C, gesture);
2403 /* when tweak fails we should give the other keymap entries a chance */
2404 event->val= KM_RELEASE;
2408 if(!ISTIMER(event->type)) {
2409 WM_gesture_end(C, gesture);
2415 /* standard tweak, called after window handlers passed on event */
2416 void wm_tweakevent_test(bContext *C, wmEvent *event, int action)
2418 wmWindow *win= CTX_wm_window(C);
2420 if(win->tweak==NULL) {
2421 if(CTX_wm_region(C)) {
2422 if(event->val==KM_PRESS) {
2423 if( ELEM3(event->type, LEFTMOUSE, MIDDLEMOUSE, RIGHTMOUSE) )
2424 win->tweak= WM_gesture_new(C, event, WM_GESTURE_TWEAK);
2429 /* no tweaks if event was handled */
2430 if((action & WM_HANDLER_BREAK)) {
2431 WM_gesture_end(C, win->tweak);
2434 tweak_gesture_modal(C, event);
2438 /* *********************** lasso gesture ****************** */
2440 int WM_gesture_lasso_invoke(bContext *C, wmOperator *op, wmEvent *event)
2442 op->customdata= WM_gesture_new(C, event, WM_GESTURE_LASSO);
2444 /* add modal handler */
2445 WM_event_add_modal_handler(C, op);
2447 wm_gesture_tag_redraw(C);
2449 if( RNA_struct_find_property(op->ptr, "cursor") )
2450 WM_cursor_modal(CTX_wm_window(C), RNA_int_get(op->ptr, "cursor"));
2452 return OPERATOR_RUNNING_MODAL;
2455 int WM_gesture_lines_invoke(bContext *C, wmOperator *op, wmEvent *event)
2457 op->customdata= WM_gesture_new(C, event, WM_GESTURE_LINES);
2459 /* add modal handler */
2460 WM_event_add_modal_handler(C, op);
2462 wm_gesture_tag_redraw(C);
2464 if( RNA_struct_find_property(op->ptr, "cursor") )
2465 WM_cursor_modal(CTX_wm_window(C), RNA_int_get(op->ptr, "cursor"));
2467 return OPERATOR_RUNNING_MODAL;
2471 static void gesture_lasso_apply(bContext *C, wmOperator *op)
2473 wmGesture *gesture= op->customdata;
2477 short *lasso= gesture->customdata;
2479 /* operator storage as path. */
2481 for(i=0; i<gesture->points; i++, lasso+=2) {
2484 RNA_collection_add(op->ptr, "path", &itemptr);
2485 RNA_float_set_array(&itemptr, "loc", loc);
2488 wm_gesture_end(C, op);
2491 op->type->exec(C, op);
2495 int WM_gesture_lasso_modal(bContext *C, wmOperator *op, wmEvent *event)
2497 wmGesture *gesture= op->customdata;
2500 switch(event->type) {
2502 case INBETWEEN_MOUSEMOVE:
2504 wm_gesture_tag_redraw(C);
2506 wm_subwindow_getorigin(CTX_wm_window(C), gesture->swinid, &sx, &sy);
2508 if(gesture->points == gesture->size) {
2509 short *old_lasso = gesture->customdata;
2510 gesture->customdata= MEM_callocN(2*sizeof(short)*(gesture->size + WM_LASSO_MIN_POINTS), "lasso points");
2511 memcpy(gesture->customdata, old_lasso, 2*sizeof(short)*gesture->size);
2512 gesture->size = gesture->size + WM_LASSO_MIN_POINTS;
2513 MEM_freeN(old_lasso);
2514 printf("realloc\n");
2519 short *lasso= gesture->customdata;
2521 lasso += (2 * gesture->points - 2);
2522 x = (event->x - sx - lasso[0]);
2523 y = (event->y - sy - lasso[1]);
2525 /* make a simple distance check to get a smoother lasso
2526 add only when at least 2 pixels between this and previous location */
2529 lasso[0] = event->x - sx;
2530 lasso[1] = event->y - sy;
2539 if(event->val==KM_RELEASE) { /* key release */
2540 gesture_lasso_apply(C, op);
2541 return OPERATOR_FINISHED;
2545 wm_gesture_end(C, op);
2546 return OPERATOR_CANCELLED;
2548 return OPERATOR_RUNNING_MODAL;
2551 int WM_gesture_lines_modal(bContext *C, wmOperator *op, wmEvent *event)
2553 return WM_gesture_lasso_modal(C, op, event);
2557 /* template to copy from */
2559 static int gesture_lasso_exec(bContext *C, wmOperator *op)
2561 RNA_BEGIN(op->ptr, itemptr, "path") {
2564 RNA_float_get_array(&itemptr, "loc", loc);
2565 printf("Location: %f %f\n", loc[0], loc[1]);
2569 return OPERATOR_FINISHED;
2572 void WM_OT_lasso_gesture(wmOperatorType *ot)
2576 ot->name= "Lasso Gesture";
2577 ot->idname= "WM_OT_lasso_gesture";
2578 ot->description="Select objects within the lasso as you move the pointer";
2580 ot->invoke= WM_gesture_lasso_invoke;
2581 ot->modal= WM_gesture_lasso_modal;
2582 ot->exec= gesture_lasso_exec;
2584 ot->poll= WM_operator_winactive;
2586 prop= RNA_def_property(ot->srna, "path", PROP_COLLECTION, PROP_NONE);
2587 RNA_def_property_struct_runtime(prop, &RNA_OperatorMousePath);
2591 /* *********************** straight line gesture ****************** */
2593 static int straightline_apply(bContext *C, wmOperator *op)
2595 wmGesture *gesture= op->customdata;
2596 rcti *rect= gesture->customdata;
2598 if(rect->xmin==rect->xmax && rect->ymin==rect->ymax)
2601 /* operator arguments and storage. */
2602 RNA_int_set(op->ptr, "xstart", rect->xmin);
2603 RNA_int_set(op->ptr, "ystart", rect->ymin);
2604 RNA_int_set(op->ptr, "xend", rect->xmax);
2605 RNA_int_set(op->ptr, "yend", rect->ymax);
2608 op->type->exec(C, op);
2614 int WM_gesture_straightline_invoke(bContext *C, wmOperator *op, wmEvent *event)
2616 op->customdata= WM_gesture_new(C, event, WM_GESTURE_STRAIGHTLINE);
2618 /* add modal handler */
2619 WM_event_add_modal_handler(C, op);
2621 wm_gesture_tag_redraw(C);
2623 if( RNA_struct_find_property(op->ptr, "cursor") )
2624 WM_cursor_modal(CTX_wm_window(C), RNA_int_get(op->ptr, "cursor"));
2626 return OPERATOR_RUNNING_MODAL;
2629 int WM_gesture_straightline_modal(bContext *C, wmOperator *op, wmEvent *event)
2631 wmGesture *gesture= op->customdata;
2632 rcti *rect= gesture->customdata;
2635 if(event->type== MOUSEMOVE) {
2636 wm_subwindow_getorigin(CTX_wm_window(C), gesture->swinid, &sx, &sy);
2638 if(gesture->mode==0) {
2639 rect->xmin= rect->xmax= event->x - sx;
2640 rect->ymin= rect->ymax= event->y - sy;
2643 rect->xmax= event->x - sx;
2644 rect->ymax= event->y - sy;
2645 straightline_apply(C, op);
2648 wm_gesture_tag_redraw(C);
2650 else if (event->type==EVT_MODAL_MAP) {
2651 switch (event->val) {
2652 case GESTURE_MODAL_BEGIN:
2653 if(gesture->mode==0) {
2655 wm_gesture_tag_redraw(C);
2658 case GESTURE_MODAL_SELECT:
2659 if(straightline_apply(C, op)) {
2660 wm_gesture_end(C, op);
2661 return OPERATOR_FINISHED;
2663 wm_gesture_end(C, op);
2664 return OPERATOR_CANCELLED;
2667 case GESTURE_MODAL_CANCEL:
2668 wm_gesture_end(C, op);
2669 return OPERATOR_CANCELLED;
2674 return OPERATOR_RUNNING_MODAL;
2678 /* template to copy from */
2679 void WM_OT_straightline_gesture(wmOperatorType *ot)
2683 ot->name= "Straight Line Gesture";
2684 ot->idname= "WM_OT_straightline_gesture";
2685 ot->description="Draw a straight line as you move the pointer";
2687 ot->invoke= WM_gesture_straightline_invoke;
2688 ot->modal= WM_gesture_straightline_modal;
2689 ot->exec= gesture_straightline_exec;
2691 ot->poll= WM_operator_winactive;
2693 WM_operator_properties_gesture_straightline(ot, 0);
2697 /* *********************** radial control ****************** */
2699 static const int WM_RADIAL_CONTROL_DISPLAY_SIZE = 200;
2703 PropertySubType subtype;
2704 PointerRNA ptr, col_ptr, fill_col_ptr, rot_ptr, zoom_ptr, image_id_ptr;
2705 PropertyRNA *prop, *col_prop, *fill_col_prop, *rot_prop, *zoom_prop;
2706 StructRNA *image_id_srna;
2707 float initial_value, current_value, min_value, max_value;
2708 int initial_mouse[2];
2710 ListBase orig_paintcursors;
2714 static void radial_control_set_initial_mouse(RadialControl *rc, wmEvent *event)
2716 float d[2] = {0, 0};
2717 float zoom[2] = {1, 1};
2719 rc->initial_mouse[0]= event->x;
2720 rc->initial_mouse[1]= event->y;
2722 switch(rc->subtype) {
2724 d[0] = rc->initial_value;
2727 d[0] = WM_RADIAL_CONTROL_DISPLAY_SIZE * (1 - rc->initial_value);
2730 d[0] = WM_RADIAL_CONTROL_DISPLAY_SIZE * cos(rc->initial_value);
2731 d[1] = WM_RADIAL_CONTROL_DISPLAY_SIZE * sin(rc->initial_value);
2738 RNA_property_float_get_array(&rc->zoom_ptr, rc->zoom_prop, zoom);
2743 rc->initial_mouse[0]-= d[0];
2744 rc->initial_mouse[1]-= d[1];
2747 static void radial_control_set_tex(RadialControl *rc)
2751 switch(RNA_type_to_ID_code(rc->image_id_ptr.type)) {
2753 if((ibuf = brush_gen_radial_control_imbuf(rc->image_id_ptr.data))) {
2754 glGenTextures(1, &rc->gltex);
2755 glBindTexture(GL_TEXTURE_2D, rc->gltex);
2756 glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, ibuf->x, ibuf->y, 0,
2757 GL_ALPHA, GL_FLOAT, ibuf->rect_float);
2758 MEM_freeN(ibuf->rect_float);
2767 static void radial_control_paint_tex(RadialControl *rc, float radius, float alpha)
2769 float col[3] = {0, 0, 0};
2772 /* set fill color */
2773 if(rc->fill_col_prop)
2774 RNA_property_float_get_array(&rc->fill_col_ptr, rc->fill_col_prop, col);
2775 glColor4f(col[0], col[1], col[2], alpha);
2778 glBindTexture(GL_TEXTURE_2D, rc->gltex);
2780 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
2781 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
2783 /* set up rotation if available */
2785 rot = RNA_property_float_get(&rc->rot_ptr, rc->rot_prop);
2787 glRotatef(RAD2DEGF(rot), 0, 0, 1);
2790 /* draw textured quad */
2791 glEnable(GL_TEXTURE_2D);
2794 glVertex2f(-radius, -radius);
2796 glVertex2f(radius, -radius);
2798 glVertex2f(radius, radius);
2800 glVertex2f(-radius, radius);
2802 glDisable(GL_TEXTURE_2D);
2809 /* flat color if no texture available */
2810 glutil_draw_filled_arc(0, M_PI * 2, radius, 40);
2814 static void radial_control_paint_cursor(bContext *C, int x, int y, void *customdata)
2816 RadialControl *rc = customdata;
2817 ARegion *ar = CTX_wm_region(C);
2818 float r1=0.0f, r2=0.0f, tex_radius, alpha;
2819 float zoom[2], col[3] = {1, 1, 1};
2821 switch(rc->subtype) {
2823 r1= rc->current_value;
2824 r2= rc->initial_value;
2829 r1= (1 - rc->current_value) * WM_RADIAL_CONTROL_DISPLAY_SIZE;
2830 r2= tex_radius= WM_RADIAL_CONTROL_DISPLAY_SIZE;
2831 alpha = rc->current_value / 2 + 0.5;
2834 r1= r2= tex_radius= WM_RADIAL_CONTROL_DISPLAY_SIZE;
2841 /* Keep cursor in the original place */
2842 x = rc->initial_mouse[0] - ar->winrct.xmin;
2843 y = rc->initial_mouse[1] - ar->winrct.ymin;
2844 glTranslatef((float)x, (float)y, 0.0f);
2847 glEnable(GL_LINE_SMOOTH);
2849 /* apply zoom if available */
2851 RNA_property_float_get_array(&rc->zoom_ptr, rc->zoom_prop, zoom);
2852 glScalef(zoom[0], zoom[1], 1);
2855 /* draw rotated texture */
2856 radial_control_paint_tex(rc, tex_radius, alpha);
2858 /* set line color */
2860 RNA_property_float_get_array(&rc->col_ptr, rc->col_prop, col);
2861 glColor4f(col[0], col[1], col[2], 0.5);
2863 if(rc->subtype == PROP_ANGLE) {
2865 /* draw original angle line */
2866 glRotatef(RAD2DEGF(rc->initial_value), 0, 0, 1);
2867 fdrawline(0.0f, 0.0f, (float)WM_RADIAL_CONTROL_DISPLAY_SIZE, 0.0f);
2868 /* draw new angle line */
2869 glRotatef(RAD2DEGF(rc->current_value - rc->initial_value), 0, 0, 1);
2870 fdrawline(0.0f, 0.0f, (float)WM_RADIAL_CONTROL_DISPLAY_SIZE, 0.0f);
2874 /* draw circles on top */
2875 glutil_draw_lined_arc(0.0, (float)(M_PI*2.0), r1, 40);
2876 glutil_draw_lined_arc(0.0, (float)(M_PI*2.0), r2, 40);
2878 glDisable(GL_BLEND);
2879 glDisable(GL_LINE_SMOOTH);
2882 /* attempt to retrieve the rna pointer/property from an rna path;
2883 returns 0 for failure, 1 for success, and also 1 if property is not
2885 static int radial_control_get_path(PointerRNA *ctx_ptr, wmOperator *op,
2886 const char *name, PointerRNA *r_ptr,
2887 PropertyRNA **r_prop, int req_float,
2888 int req_length, int allow_missing)
2890 PropertyRNA *unused_prop;
2894 /* get an rna string path from the operator's properties */
2895 if(!(str = RNA_string_get_alloc(op->ptr, name, NULL, 0)))
2898 if(str[0] == '\0') {
2904 r_prop = &unused_prop;
2906 /* get rna from path */
2907 if(!RNA_path_resolve(ctx_ptr, str, r_ptr, r_prop)) {
2912 BKE_reportf(op->reports, RPT_ERROR, "Couldn't resolve path %s", name);
2917 /* if property is expected to be a float, check its type */
2919 if(!(*r_prop) || (RNA_property_type(*r_prop) != PROP_FLOAT)) {
2921 BKE_reportf(op->reports, RPT_ERROR,
2922 "Property from path %s is not a float", name);
2927 /* check property's array length */
2928 if(*r_prop && (len = RNA_property_array_length(r_ptr, *r_prop)) != req_length) {
2930 BKE_reportf(op->reports, RPT_ERROR,
2931 "Property from path %s has length %d instead of %d",
2932 name, len, req_length);
2941 /* initialize the rna pointers and properties using rna paths */
2942 static int radial_control_get_properties(bContext *C, wmOperator *op)
2944 RadialControl *rc = op->customdata;
2947 RNA_pointer_create(NULL, &RNA_Context, C, &ctx_ptr);
2949 if(!radial_control_get_path(&ctx_ptr, op, "data_path", &rc->ptr, &rc->prop, 0, 0, 0))
2952 /* data path is required */
2956 if(!radial_control_get_path(&ctx_ptr, op, "rotation_path", &rc->rot_ptr, &rc->rot_prop, 1, 0, 0))
2958 if(!radial_control_get_path(&ctx_ptr, op, "color_path", &rc->col_ptr, &rc->col_prop, 1, 3, 0))
2960 if(!radial_control_get_path(&ctx_ptr, op, "fill_color_path", &rc->fill_col_ptr, &rc->fill_col_prop, 1, 3, 0))
2963 /* slightly ugly; allow this property to not resolve
2964 correctly. needed because 3d texture paint shares the same
2965 keymap as 2d image paint */
2966 if(!radial_control_get_path(&ctx_ptr, op, "zoom_path", &rc->zoom_ptr, &rc->zoom_prop, 1, 2, 1))
2969 if(!radial_control_get_path(&ctx_ptr, op, "image_id", &rc->image_id_ptr, NULL, 0, 0, 0))
2971 else if(rc->image_id_ptr.data) {
2972 /* extra check, pointer must be to an ID */
2973 if(!RNA_struct_is_ID(rc->image_id_ptr.type)) {
2974 BKE_report(op->reports, RPT_ERROR,
2975 "Pointer from path image_id is not an ID");
2983 static int radial_control_invoke(bContext *C, wmOperator *op, wmEvent *event)
2985 wmWindowManager *wm;
2987 int min_value_int, max_value_int, step_int;
2988 float step_float, precision;
2990 if(!(op->customdata = rc = MEM_callocN(sizeof(RadialControl), "RadialControl")))
2991 return OPERATOR_CANCELLED;
2993 if(!radial_control_get_properties(C, op)) {
2995 return OPERATOR_CANCELLED;
2998 /* get type, initial, min, and max values of the property */
2999 switch((rc->type = RNA_property_type(rc->prop))) {
3001 rc->initial_value = RNA_property_int_get(&rc->ptr, rc->prop);
3002 RNA_property_int_ui_range(&rc->ptr, rc->prop, &min_value_int,
3003 &max_value_int, &step_int);
3004 rc->min_value = min_value_int;
3005 rc->max_value = max_value_int;
3008 rc->initial_value = RNA_property_float_get(&rc->ptr, rc->prop);
3009 RNA_property_float_ui_range(&rc->ptr, rc->prop, &rc->min_value,
3010 &rc->max_value, &step_float, &precision);
3013 BKE_report(op->reports, RPT_ERROR, "Property must be an integer or a float");
3015 return OPERATOR_CANCELLED;
3018 /* get subtype of property */
3019 rc->subtype = RNA_property_subtype(rc->prop);
3020 if(!ELEM3(rc->subtype, PROP_DISTANCE, PROP_FACTOR, PROP_ANGLE)) {
3021 BKE_report(op->reports, RPT_ERROR, "Property must be a distance, a factor, or an angle");
3023 return OPERATOR_CANCELLED;
3026 rc->current_value = rc->initial_value;
3027 radial_control_set_initial_mouse(rc, event);
3028 radial_control_set_tex(rc);
3030 /* temporarily disable other paint cursors */
3031 wm = CTX_wm_manager(C);
3032 rc->orig_paintcursors = wm->paintcursors;
3033 wm->paintcursors.first = wm->paintcursors.last = NULL;
3035 /* add radial control paint cursor */
3036 rc->cursor = WM_paint_cursor_activate(wm, op->type->poll,
3037 radial_control_paint_cursor, rc);
3039 WM_event_add_modal_handler(C, op);
3041 return OPERATOR_RUNNING_MODAL;
3044 static void radial_control_set_value(RadialControl *rc, float val)
3048 RNA_property_int_set(&rc->ptr, rc->prop, val);
3051 RNA_property_float_set(&rc->ptr, rc->prop, val);
3058 static int radial_control_modal(bContext *C, wmOperator *op, wmEvent *event)
3060 RadialControl *rc = op->customdata;
3061 wmWindowManager *wm;
3062 float new_value, dist, zoom[2];
3063 float delta[2], snap, ret = OPERATOR_RUNNING_MODAL;
3065 /* TODO: fix hardcoded events */
3069 switch(event->type) {
3071 delta[0]= rc->initial_mouse[0] - event->x;
3072 delta[1]= rc->initial_mouse[1] - event->y;
3075 RNA_property_float_get_array(&rc->zoom_ptr, rc->zoom_prop, zoom);
3076 delta[0] /= zoom[0];
3077 delta[1] /= zoom[1];
3080 dist= sqrt(delta[0]*delta[0]+delta[1]*delta[1]);
3082 /* calculate new value and apply snapping */
3083 switch(rc->subtype) {
3086 if(snap) new_value = ((int)new_value + 5) / 10*10;
3089 new_value = 1 - dist / WM_RADIAL_CONTROL_DISPLAY_SIZE;
3090 if(snap) new_value = ((int)ceil(new_value * 10.f) * 10.0f) / 100.f;
3093 new_value = atan2(delta[1], delta[0]) + M_PI;
3094 if(snap) new_value = DEG2RADF(((int)RAD2DEGF(new_value) + 5) / 10*10);
3100 /* clamp and update */
3101 CLAMP(new_value, rc->min_value, rc->max_value);
3102 radial_control_set_value(rc, new_value);
3103 rc->current_value = new_value;
3108 /* cancelled; restore original value */
3109 radial_control_set_value(rc, rc->initial_value);
3110 ret = OPERATOR_CANCELLED;
3115 /* done; value already set */
3116 ret = OPERATOR_FINISHED;
3120 ED_region_tag_redraw(CTX_wm_region(C));
3122 if(ret != OPERATOR_RUNNING_MODAL) {
3123 wm = CTX_wm_manager(C);
3125 WM_paint_cursor_end(wm, rc->cursor);
3127 /* restore original paint cursors */
3128 wm->paintcursors = rc->orig_paintcursors;
3130 /* not sure if this is a good notifier to use;
3131 intended purpose is to update the UI so that the
3132 new value is displayed in sliders/numfields */
3133 WM_event_add_notifier(C, NC_WINDOW, NULL);
3135 glDeleteTextures(1, &rc->gltex);
3143 static void WM_OT_radial_control(wmOperatorType *ot)
3145 ot->name= "Radial Control";
3146 ot->idname= "WM_OT_radial_control";
3148 ot->invoke= radial_control_invoke;
3149 ot->modal= radial_control_modal;
3151 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO|OPTYPE_BLOCKING;
3153 /* all paths relative to the context */
3154 RNA_def_string(ot->srna, "data_path", "", 0, "Data Path", "Path of property to be set by the radial control.");
3155 RNA_def_string(ot->srna, "rotation_path", "", 0, "Rotation Path", "Path of property used to rotate the texture display.");
3156 RNA_def_string(ot->srna, "color_path", "", 0, "Color Path", "Path of property used to set the color of the control.");
3157 RNA_def_string(ot->srna, "fill_color_path", "", 0, "Fill Color Path", "Path of property used to set the fill color of the control.");
3158 RNA_def_string(ot->srna, "zoom_path", "", 0, "Zoom Path", "Path of property used to set the zoom level for the control.");
3159 RNA_def_string(ot->srna, "image_id", "", 0, "Image ID", "Path of ID that is used to generate an image for the control.");
3162 /* ************************** timer for testing ***************** */
3164 /* uses no type defines, fully local testing function anyway... ;) */
3166 static void redraw_timer_window_swap(bContext *C)
3168 wmWindow *win= CTX_wm_window(C);
3171 for(sa= CTX_wm_screen(C)->areabase.first; sa; sa= sa->next)
3172 ED_area_tag_redraw(sa);
3175 CTX_wm_window_set(C, win); /* XXX context manipulation warning! */
3178 static EnumPropertyItem redraw_timer_type_items[] = {
3179 {0, "DRAW", 0, "Draw Region", "Draw Region"},
3180 {1, "DRAW_SWAP", 0, "Draw Region + Swap", "Draw Region and Swap"},
3181 {2, "DRAW_WIN", 0, "Draw Window", "Draw Window"},
3182 {3, "DRAW_WIN_SWAP", 0, "Draw Window + Swap", "Draw Window and Swap"},
3183 {4, "ANIM_STEP", 0, "Anim Step", "Animation Steps"},
3184 {5, "ANIM_PLAY", 0, "Anim Play", "Animation Playback"},
3185 {6, "UNDO", 0, "Undo/Redo", "Undo/Redo"},
3186 {0, NULL, 0, NULL, NULL}};
3188 static int redraw_timer_exec(bContext *C, wmOperator *op)
3190 ARegion *ar= CTX_wm_region(C);
3191 double stime= PIL_check_seconds_timer();
3192 int type = RNA_enum_get(op->ptr, "type");
3193 int iter = RNA_int_get(op->ptr, "iterations");
3196 const char *infostr= "";
3200 for(a=0; a<iter; a++) {
3203 ED_region_do_draw(C, ar);
3206 wmWindow *win= CTX_wm_window(C);
3208 ED_region_tag_redraw(ar);
3211 CTX_wm_window_set(C, win); /* XXX context