2 * ***** BEGIN GPL LICENSE BLOCK *****
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * The Original Code is Copyright (C) 2007 Blender Foundation.
19 * All rights reserved.
22 * Contributor(s): Blender Foundation
24 * ***** END GPL LICENSE BLOCK *****
27 /** \file blender/windowmanager/intern/wm_operators.c
30 * Functions for dealing with wmOperator, adding, removing, calling
31 * as well as some generic operators and shared operator properties.
44 # include "GHOST_C-api.h"
47 #include "MEM_guardedalloc.h"
52 #include "DNA_object_types.h"
53 #include "DNA_screen_types.h"
54 #include "DNA_scene_types.h"
55 #include "DNA_userdef_types.h"
56 #include "DNA_windowmanager_types.h"
57 #include "DNA_workspace_types.h"
59 #include "BLT_translation.h"
63 #include "BLI_blenlib.h"
64 #include "BLI_dial_2d.h"
65 #include "BLI_dynstr.h" /*for WM_operator_pystring */
67 #include "BLI_utildefines.h"
68 #include "BLI_ghash.h"
70 #include "BLO_readfile.h"
72 #include "BKE_appdir.h"
73 #include "BKE_blender_version.h"
74 #include "BKE_brush.h"
75 #include "BKE_context.h"
76 #include "BKE_global.h"
77 #include "BKE_icons.h"
78 #include "BKE_idprop.h"
79 #include "BKE_image.h"
80 #include "BKE_library.h"
81 #include "BKE_library_query.h"
83 #include "BKE_material.h"
84 #include "BKE_report.h"
85 #include "BKE_scene.h"
86 #include "BKE_screen.h" /* BKE_ST_MAXNAME */
89 #include "BKE_idcode.h"
93 #include "GPU_immediate.h"
94 #include "GPU_immediate_util.h"
95 #include "GPU_matrix.h"
97 #include "IMB_imbuf_types.h"
98 #include "IMB_imbuf.h"
100 #include "ED_numinput.h"
101 #include "ED_screen.h"
103 #include "ED_view3d.h"
105 #include "RNA_access.h"
106 #include "RNA_define.h"
107 #include "RNA_enum_types.h"
109 #include "UI_interface.h"
110 #include "UI_interface_icons.h"
111 #include "UI_resources.h"
114 #include "WM_types.h"
118 #include "wm_event_system.h"
119 #include "wm_event_types.h"
120 #include "wm_files.h"
121 #include "wm_window.h"
123 #define UNDOCUMENTED_OPERATOR_TIP N_("(undocumented operator)")
125 /* ************ operator API, exported ********** */
127 /* SOME_OT_op -> some.op */
128 void WM_operator_py_idname(char *to, const char *from)
130 const char *sep = strstr(from, "_OT_");
132 int ofs = (sep - from);
134 /* note, we use ascii tolower instead of system tolower, because the
135 * latter depends on the locale, and can lead to idname mismatch */
136 memcpy(to, from, sizeof(char) * ofs);
137 BLI_str_tolower_ascii(to, ofs);
140 BLI_strncpy(to + (ofs + 1), sep + 4, OP_MAX_TYPENAME - (ofs + 1));
143 /* should not happen but support just in case */
144 BLI_strncpy(to, from, OP_MAX_TYPENAME);
148 /* some.op -> SOME_OT_op */
149 void WM_operator_bl_idname(char *to, const char *from)
152 const char *sep = strchr(from, '.');
155 int ofs = (sep - from);
157 memcpy(to, from, sizeof(char) * ofs);
158 BLI_str_toupper_ascii(to, ofs);
159 strcpy(to + ofs, "_OT_");
160 strcpy(to + (ofs + 4), sep + 1);
163 /* should not happen but support just in case */
164 BLI_strncpy(to, from, OP_MAX_TYPENAME);
172 * Sanity check to ensure #WM_operator_bl_idname won't fail.
173 * \returns true when there are no problems with \a idname, otherwise report an error.
175 bool WM_operator_py_idname_ok_or_report(ReportList *reports, const char *classname, const char *idname)
177 const char *ch = idname;
180 for (i = 0; *ch; i++, ch++) {
181 if ((*ch >= 'a' && *ch <= 'z') || (*ch >= '0' && *ch <= '9') || *ch == '_') {
184 else if (*ch == '.') {
188 BKE_reportf(reports, RPT_ERROR,
189 "Registering operator class: '%s', invalid bl_idname '%s', at position %d",
190 classname, idname, i);
195 if (i > (MAX_NAME - 3)) {
196 BKE_reportf(reports, RPT_ERROR, "Registering operator class: '%s', invalid bl_idname '%s', "
197 "is too long, maximum length is %d", classname, idname,
203 BKE_reportf(reports, RPT_ERROR,
204 "Registering operator class: '%s', invalid bl_idname '%s', must contain 1 '.' character",
212 * Print a string representation of the operator, with the args that it runs so python can run it again.
214 * When calling from an existing wmOperator, better to use simple version:
215 * `WM_operator_pystring(C, op);`
217 * \note Both \a op and \a opptr may be `NULL` (\a op is only used for macro operators).
219 char *WM_operator_pystring_ex(bContext *C, wmOperator *op, const bool all_args, const bool macro_args,
220 wmOperatorType *ot, PointerRNA *opptr)
222 char idname_py[OP_MAX_TYPENAME];
224 /* for building the string */
225 DynStr *dynstr = BLI_dynstr_new();
229 /* arbitrary, but can get huge string with stroke painting otherwise */
230 int max_prop_length = 10;
232 WM_operator_py_idname(idname_py, ot->idname);
233 BLI_dynstr_appendf(dynstr, "bpy.ops.%s(", idname_py);
235 if (op && op->macro.first) {
236 /* Special handling for macros, else we only get default values in this case... */
238 bool first_op = true;
240 opm = macro_args ? op->macro.first : NULL;
242 for (; opm; opm = opm->next) {
243 PointerRNA *opmptr = opm->ptr;
244 PointerRNA opmptr_default;
245 if (opmptr == NULL) {
246 WM_operator_properties_create_ptr(&opmptr_default, opm->type);
247 opmptr = &opmptr_default;
250 cstring_args = RNA_pointer_as_string_id(C, opmptr);
252 BLI_dynstr_appendf(dynstr, "%s=%s", opm->type->idname, cstring_args);
256 BLI_dynstr_appendf(dynstr, ", %s=%s", opm->type->idname, cstring_args);
258 MEM_freeN(cstring_args);
260 if (opmptr == &opmptr_default) {
261 WM_operator_properties_free(&opmptr_default);
266 /* only to get the original props for comparisons */
267 PointerRNA opptr_default;
268 const bool macro_args_test = ot->macro.first ? macro_args : true;
271 WM_operator_properties_create_ptr(&opptr_default, ot);
272 opptr = &opptr_default;
275 cstring_args = RNA_pointer_as_string_keywords(C, opptr, false, all_args, macro_args_test, max_prop_length);
276 BLI_dynstr_append(dynstr, cstring_args);
277 MEM_freeN(cstring_args);
279 if (opptr == &opptr_default) {
280 WM_operator_properties_free(&opptr_default);
284 BLI_dynstr_append(dynstr, ")");
286 cstring = BLI_dynstr_get_cstring(dynstr);
287 BLI_dynstr_free(dynstr);
291 char *WM_operator_pystring(bContext *C, wmOperator *op,
292 const bool all_args, const bool macro_args)
294 return WM_operator_pystring_ex(C, op, all_args, macro_args, op->type, op->ptr);
299 * \return true if the string was shortened
301 bool WM_operator_pystring_abbreviate(char *str, int str_len_max)
303 const int str_len = strlen(str);
304 const char *parens_start = strchr(str, '(');
307 const int parens_start_pos = parens_start - str;
308 const char *parens_end = strrchr(parens_start + 1, ')');
311 const int parens_len = parens_end - parens_start;
313 if (parens_len > str_len_max) {
314 const char *comma_first = strchr(parens_start, ',');
316 /* truncate after the first comma */
318 const char end_str[] = " ... )";
319 const int end_str_len = sizeof(end_str) - 1;
321 /* leave a place for the first argument*/
322 const int new_str_len = (comma_first - parens_start) + 1;
324 if (str_len >= new_str_len + parens_start_pos + end_str_len + 1) {
325 /* append " ... )" to the string after the comma */
326 memcpy(str + new_str_len + parens_start_pos, end_str, end_str_len + 1);
338 /* return NULL if no match is found */
340 static const char *wm_context_member_from_ptr(bContext *C, const PointerRNA *ptr)
342 /* loop over all context items and do 2 checks
344 * - see if the pointer is in the context.
345 * - see if the pointers ID is in the context.
348 /* don't get from the context store since this is normally set only for the UI and not usable elsewhere */
349 ListBase lb = CTX_data_dir_get_ex(C, false, true, true);
352 const char *member_found = NULL;
353 const char *member_id = NULL;
355 for (link = lb.first; link; link = link->next) {
356 const char *identifier = link->data;
357 PointerRNA ctx_item_ptr = {{0}}; // CTX_data_pointer_get(C, identifier); // XXX, this isnt working
359 if (ctx_item_ptr.type == NULL) {
363 if (ptr->id.data == ctx_item_ptr.id.data) {
364 if ((ptr->data == ctx_item_ptr.data) &&
365 (ptr->type == ctx_item_ptr.type))
368 member_found = identifier;
371 else if (RNA_struct_is_ID(ctx_item_ptr.type)) {
372 /* we found a reference to this ID,
373 * so fallback to it if there is no direct reference */
374 member_id = identifier;
383 else if (member_id) {
393 /* use hard coded checks for now */
395 static const char *wm_context_member_from_ptr(bContext *C, const PointerRNA *ptr)
397 const char *member_id = NULL;
401 #define CTX_TEST_PTR_ID(C, member, idptr) \
403 const char *ctx_member = member; \
404 PointerRNA ctx_item_ptr = CTX_data_pointer_get(C, ctx_member); \
405 if (ctx_item_ptr.id.data == idptr) { \
406 member_id = ctx_member; \
411 #define CTX_TEST_PTR_ID_CAST(C, member, member_full, cast, idptr) \
413 const char *ctx_member = member; \
414 const char *ctx_member_full = member_full; \
415 PointerRNA ctx_item_ptr = CTX_data_pointer_get(C, ctx_member); \
416 if (ctx_item_ptr.id.data && cast(ctx_item_ptr.id.data) == idptr) { \
417 member_id = ctx_member_full; \
422 #define CTX_TEST_PTR_DATA_TYPE(C, member, rna_type, rna_ptr, dataptr_cmp) \
424 const char *ctx_member = member; \
425 if (RNA_struct_is_a((ptr)->type, &(rna_type)) && (ptr)->data == (dataptr_cmp)) { \
426 member_id = ctx_member; \
431 #define CTX_TEST_SPACE_TYPE(space_data_type, member_full, dataptr_cmp) \
433 const char *ctx_member_full = member_full; \
434 if (space_data->spacetype == space_data_type && ptr->data == dataptr_cmp) { \
435 member_id = ctx_member_full; \
440 switch (GS(((ID *)ptr->id.data)->name)) {
443 CTX_TEST_PTR_ID(C, "scene", ptr->id.data);
448 CTX_TEST_PTR_ID(C, "object", ptr->id.data);
451 /* from rna_Main_objects_new */
452 case OB_DATA_SUPPORT_ID_CASE:
454 #define ID_CAST_OBDATA(id_pt) (((Object *)(id_pt))->data)
455 CTX_TEST_PTR_ID_CAST(C, "object", "object.data", ID_CAST_OBDATA, ptr->id.data);
457 #undef ID_CAST_OBDATA
461 #define ID_CAST_OBMATACT(id_pt) (give_current_material(((Object *)id_pt), ((Object *)id_pt)->actcol))
462 CTX_TEST_PTR_ID_CAST(C, "object", "object.active_material", ID_CAST_OBMATACT, ptr->id.data);
464 #undef ID_CAST_OBMATACT
468 #define ID_CAST_SCENEWORLD(id_pt) (((Scene *)(id_pt))->world)
469 CTX_TEST_PTR_ID_CAST(C, "scene", "scene.world", ID_CAST_SCENEWORLD, ptr->id.data);
471 #undef ID_CAST_SCENEWORLD
475 CTX_TEST_PTR_ID(C, "screen", ptr->id.data);
477 SpaceLink *space_data = CTX_wm_space_data(C);
479 CTX_TEST_PTR_DATA_TYPE(C, "space_data", RNA_Space, ptr, space_data);
480 CTX_TEST_PTR_DATA_TYPE(C, "space_data", RNA_View3DOverlay, ptr, space_data);
481 CTX_TEST_PTR_DATA_TYPE(C, "space_data", RNA_View3DShading, ptr, space_data);
482 CTX_TEST_PTR_DATA_TYPE(C, "area", RNA_Area, ptr, CTX_wm_area(C));
483 CTX_TEST_PTR_DATA_TYPE(C, "region", RNA_Region, ptr, CTX_wm_region(C));
485 CTX_TEST_SPACE_TYPE(SPACE_IMAGE, "space_data.uv_editor", space_data);
486 CTX_TEST_SPACE_TYPE(SPACE_VIEW3D, "space_data.fx_settings", &(CTX_wm_view3d(C)->fx_settings));
487 CTX_TEST_SPACE_TYPE(SPACE_NLA, "space_data.dopesheet", CTX_wm_space_nla(C)->ads);
488 CTX_TEST_SPACE_TYPE(SPACE_IPO, "space_data.dopesheet", CTX_wm_space_graph(C)->ads);
489 CTX_TEST_SPACE_TYPE(SPACE_ACTION, "space_data.dopesheet", &(CTX_wm_space_action(C)->ads));
490 CTX_TEST_SPACE_TYPE(SPACE_FILE, "space_data.params", CTX_wm_space_file(C)->params);
496 #undef CTX_TEST_PTR_ID
497 #undef CTX_TEST_PTR_ID_CAST
498 #undef CTX_TEST_SPACE_TYPE
505 static char *wm_prop_pystring_from_context(bContext *C, PointerRNA *ptr, PropertyRNA *prop, int index)
507 const char *member_id = wm_context_member_from_ptr(C, ptr);
509 if (member_id != NULL) {
510 char *prop_str = RNA_path_struct_property_py(ptr, prop, index);
512 ret = BLI_sprintfN("bpy.context.%s.%s", member_id, prop_str);
519 const char *WM_context_member_from_ptr(bContext *C, const PointerRNA *ptr)
521 return wm_context_member_from_ptr(C, ptr);
524 char *WM_prop_pystring_assign(bContext *C, PointerRNA *ptr, PropertyRNA *prop, int index)
526 char *lhs, *rhs, *ret;
528 lhs = C ? wm_prop_pystring_from_context(C, ptr, prop, index) : NULL;
531 /* fallback to bpy.data.foo[id] if we dont find in the context */
532 lhs = RNA_path_full_property_py(ptr, prop, index);
539 rhs = RNA_property_as_string(C, ptr, prop, index, INT_MAX);
545 ret = BLI_sprintfN("%s = %s", lhs, rhs);
551 void WM_operator_properties_create_ptr(PointerRNA *ptr, wmOperatorType *ot)
553 RNA_pointer_create(NULL, ot->srna, NULL, ptr);
556 void WM_operator_properties_create(PointerRNA *ptr, const char *opstring)
558 wmOperatorType *ot = WM_operatortype_find(opstring, false);
561 WM_operator_properties_create_ptr(ptr, ot);
563 RNA_pointer_create(NULL, &RNA_OperatorProperties, NULL, ptr);
566 /* similar to the function above except its uses ID properties
567 * used for keymaps and macros */
568 void WM_operator_properties_alloc(PointerRNA **ptr, IDProperty **properties, const char *opstring)
570 if (*properties == NULL) {
571 IDPropertyTemplate val = {0};
572 *properties = IDP_New(IDP_GROUP, &val, "wmOpItemProp");
576 *ptr = MEM_callocN(sizeof(PointerRNA), "wmOpItemPtr");
577 WM_operator_properties_create(*ptr, opstring);
580 (*ptr)->data = *properties;
584 void WM_operator_properties_sanitize(PointerRNA *ptr, const bool no_context)
586 RNA_STRUCT_BEGIN (ptr, prop)
588 switch (RNA_property_type(prop)) {
591 RNA_def_property_flag(prop, PROP_ENUM_NO_CONTEXT);
593 RNA_def_property_clear_flag(prop, PROP_ENUM_NO_CONTEXT);
597 StructRNA *ptype = RNA_property_pointer_type(ptr, prop);
599 /* recurse into operator properties */
600 if (RNA_struct_is_a(ptype, &RNA_OperatorProperties)) {
601 PointerRNA opptr = RNA_property_pointer_get(ptr, prop);
602 WM_operator_properties_sanitize(&opptr, no_context);
614 /** set all props to their default,
615 * \param do_update: Only update un-initialized props.
617 * \note, there's nothing specific to operators here.
618 * this could be made a general function.
620 bool WM_operator_properties_default(PointerRNA *ptr, const bool do_update)
622 bool changed = false;
623 RNA_STRUCT_BEGIN (ptr, prop)
625 switch (RNA_property_type(prop)) {
628 StructRNA *ptype = RNA_property_pointer_type(ptr, prop);
629 if (ptype != &RNA_Struct) {
630 PointerRNA opptr = RNA_property_pointer_get(ptr, prop);
631 changed |= WM_operator_properties_default(&opptr, do_update);
636 if ((do_update == false) || (RNA_property_is_set(ptr, prop) == false)) {
637 if (RNA_property_reset(ptr, prop, -1)) {
649 /* remove all props without PROP_SKIP_SAVE */
650 void WM_operator_properties_reset(wmOperator *op)
653 PropertyRNA *iterprop;
654 iterprop = RNA_struct_iterator_property(op->type->srna);
656 RNA_PROP_BEGIN (op->ptr, itemptr, iterprop)
658 PropertyRNA *prop = itemptr.data;
660 if ((RNA_property_flag(prop) & PROP_SKIP_SAVE) == 0) {
661 const char *identifier = RNA_property_identifier(prop);
662 RNA_struct_idprops_unset(op->ptr, identifier);
669 void WM_operator_properties_clear(PointerRNA *ptr)
671 IDProperty *properties = ptr->data;
674 IDP_ClearProperty(properties);
678 void WM_operator_properties_free(PointerRNA *ptr)
680 IDProperty *properties = ptr->data;
683 IDP_FreeProperty(properties);
684 MEM_freeN(properties);
685 ptr->data = NULL; /* just in case */
689 /* ************ default op callbacks, exported *********** */
691 void WM_operator_view3d_unit_defaults(struct bContext *C, struct wmOperator *op)
693 if (op->flag & OP_IS_INVOKE) {
694 Scene *scene = CTX_data_scene(C);
695 View3D *v3d = CTX_wm_view3d(C);
697 const float dia = v3d ? ED_view3d_grid_scale(scene, v3d, NULL) : ED_scene_grid_scale(scene, NULL);
699 /* always run, so the values are initialized,
700 * otherwise we may get differ behavior when (dia != 1.0) */
701 RNA_STRUCT_BEGIN (op->ptr, prop)
703 if (RNA_property_type(prop) == PROP_FLOAT) {
704 PropertySubType pstype = RNA_property_subtype(prop);
705 if (pstype == PROP_DISTANCE) {
706 /* we don't support arrays yet */
707 BLI_assert(RNA_property_array_check(prop) == false);
709 if (!RNA_property_is_set_ex(op->ptr, prop, false)) {
710 const float value = RNA_property_float_get_default(op->ptr, prop) * dia;
711 RNA_property_float_set(op->ptr, prop, value);
720 int WM_operator_smooth_viewtx_get(const wmOperator *op)
722 return (op->flag & OP_IS_INVOKE) ? U.smooth_viewtx : 0;
725 /* invoke callback, uses enum property named "type" */
726 int WM_menu_invoke_ex(bContext *C, wmOperator *op, int opcontext)
728 PropertyRNA *prop = op->type->prop;
733 CLOG_ERROR(WM_LOG_OPERATORS,
734 "'%s' has no enum property set",
737 else if (RNA_property_type(prop) != PROP_ENUM) {
738 CLOG_ERROR(WM_LOG_OPERATORS,
739 "'%s', '%s' is not an enum property",
740 op->type->idname, RNA_property_identifier(prop));
742 else if (RNA_property_is_set(op->ptr, prop)) {
743 const int retval = op->type->exec(C, op);
744 OPERATOR_RETVAL_CHECK(retval);
748 pup = UI_popup_menu_begin(C, RNA_struct_ui_name(op->type->srna), ICON_NONE);
749 layout = UI_popup_menu_layout(pup);
750 /* set this so the default execution context is the same as submenus */
751 uiLayoutSetOperatorContext(layout, opcontext);
752 uiItemsFullEnumO(layout, op->type->idname, RNA_property_identifier(prop), op->ptr->data, opcontext, 0);
753 UI_popup_menu_end(C, pup);
754 return OPERATOR_INTERFACE;
757 return OPERATOR_CANCELLED;
760 int WM_menu_invoke(bContext *C, wmOperator *op, const wmEvent *UNUSED(event))
762 return WM_menu_invoke_ex(C, op, WM_OP_INVOKE_REGION_WIN);
765 struct EnumSearchMenu {
766 wmOperator *op; /* the operator that will be executed when selecting an item */
769 short prv_cols, prv_rows;
772 /* generic enum search invoke popup */
773 static uiBlock *wm_enum_search_menu(bContext *C, ARegion *ar, void *arg)
775 struct EnumSearchMenu *search_menu = arg;
776 wmWindow *win = CTX_wm_window(C);
777 wmOperator *op = search_menu->op;
778 /* template_ID uses 4 * widget_unit for width, we use a bit more, some items may have a suffix to show */
779 const int width = search_menu->use_previews ? 5 * U.widget_unit * search_menu->prv_cols : UI_searchbox_size_x();
780 const int height = search_menu->use_previews ? 5 * U.widget_unit * search_menu->prv_rows : UI_searchbox_size_y();
781 static char search[256] = "";
785 block = UI_block_begin(C, ar, "_popup", UI_EMBOSS);
786 UI_block_flag_enable(block, UI_BLOCK_LOOP | UI_BLOCK_MOVEMOUSE_QUIT | UI_BLOCK_SEARCH_MENU);
787 UI_block_theme_style_set(block, UI_BLOCK_THEME_STYLE_POPUP);
790 BLI_assert(search_menu->use_previews || (search_menu->prv_cols == 0 && search_menu->prv_rows == 0));
791 #if 0 /* ok, this isn't so easy... */
792 uiDefBut(block, UI_BTYPE_LABEL, 0, RNA_struct_ui_name(op->type->srna), 10, 10, UI_searchbox_size_x(), UI_UNIT_Y, NULL, 0.0, 0.0, 0, 0, "");
794 but = uiDefSearchButO_ptr(block, op->type, op->ptr->data, search, 0, ICON_VIEWZOOM, sizeof(search),
795 10, 10, width, UI_UNIT_Y, search_menu->prv_rows, search_menu->prv_cols, "");
797 /* fake button, it holds space for search items */
798 uiDefBut(block, UI_BTYPE_LABEL, 0, "", 10, 10 - UI_searchbox_size_y(), width, height, NULL, 0, 0, 0, 0, NULL);
800 UI_block_bounds_set_popup(block, 6, 0, -UI_UNIT_Y); /* move it downwards, mouse over button */
801 UI_but_focus_on_enter_event(win, but);
807 * Similar to #WM_enum_search_invoke, but draws previews. Also, this can't
808 * be used as invoke callback directly since it needs additional info.
810 int WM_enum_search_invoke_previews(
811 bContext *C, wmOperator *op, short prv_cols, short prv_rows)
813 static struct EnumSearchMenu search_menu;
816 search_menu.use_previews = true;
817 search_menu.prv_cols = prv_cols;
818 search_menu.prv_rows = prv_rows;
820 UI_popup_block_invoke(C, wm_enum_search_menu, &search_menu);
822 return OPERATOR_INTERFACE;
825 int WM_enum_search_invoke(bContext *C, wmOperator *op, const wmEvent *UNUSED(event))
827 static struct EnumSearchMenu search_menu;
829 UI_popup_block_invoke(C, wm_enum_search_menu, &search_menu);
830 return OPERATOR_INTERFACE;
833 /* Can't be used as an invoke directly, needs message arg (can be NULL) */
834 int WM_operator_confirm_message_ex(bContext *C, wmOperator *op,
835 const char *title, const int icon,
840 IDProperty *properties = op->ptr->data;
842 if (properties && properties->len)
843 properties = IDP_CopyProperty(op->ptr->data);
847 pup = UI_popup_menu_begin(C, title, icon);
848 layout = UI_popup_menu_layout(pup);
849 uiItemFullO_ptr(layout, op->type, message, ICON_NONE, properties, WM_OP_EXEC_REGION_WIN, 0, NULL);
850 UI_popup_menu_end(C, pup);
852 return OPERATOR_INTERFACE;
855 int WM_operator_confirm_message(bContext *C, wmOperator *op, const char *message)
857 return WM_operator_confirm_message_ex(C, op, IFACE_("OK?"), ICON_QUESTION, message);
860 int WM_operator_confirm(bContext *C, wmOperator *op, const wmEvent *UNUSED(event))
862 return WM_operator_confirm_message(C, op, NULL);
865 int WM_operator_confirm_or_exec(bContext *C, wmOperator *op, const wmEvent *UNUSED(event))
867 const bool confirm = RNA_boolean_get(op->ptr, "confirm");
869 return WM_operator_confirm_message(C, op, NULL);
872 return op->type->exec(C, op);
877 /* op->invoke, opens fileselect if path property not set, otherwise executes */
878 int WM_operator_filesel(bContext *C, wmOperator *op, const wmEvent *UNUSED(event))
880 if (RNA_struct_property_is_set(op->ptr, "filepath")) {
881 return WM_operator_call_notest(C, op); /* call exec direct */
884 WM_event_add_fileselect(C, op);
885 return OPERATOR_RUNNING_MODAL;
889 bool WM_operator_filesel_ensure_ext_imtype(wmOperator *op, const struct ImageFormatData *im_format)
892 char filepath[FILE_MAX];
893 /* dont NULL check prop, this can only run on ops with a 'filepath' */
894 prop = RNA_struct_find_property(op->ptr, "filepath");
895 RNA_property_string_get(op->ptr, prop, filepath);
896 if (BKE_image_path_ensure_ext_from_imformat(filepath, im_format)) {
897 RNA_property_string_set(op->ptr, prop, filepath);
898 /* note, we could check for and update 'filename' here,
899 * but so far nothing needs this. */
906 bool WM_operator_winactive(bContext *C)
908 if (CTX_wm_window(C) == NULL) return 0;
912 /* return false, if the UI should be disabled */
913 bool WM_operator_check_ui_enabled(const bContext *C, const char *idname)
915 wmWindowManager *wm = CTX_wm_manager(C);
916 Scene *scene = CTX_data_scene(C);
918 return !((ED_undo_is_valid(C, idname) == false) || WM_jobs_test(wm, scene, WM_JOB_TYPE_ANY));
921 wmOperator *WM_operator_last_redo(const bContext *C)
923 wmWindowManager *wm = CTX_wm_manager(C);
926 /* only for operators that are registered and did an undo push */
927 for (op = wm->operators.last; op; op = op->prev)
928 if ((op->type->flag & OPTYPE_REGISTER) && (op->type->flag & OPTYPE_UNDO))
934 IDProperty *WM_operator_last_properties_ensure_idprops(wmOperatorType *ot)
936 if (ot->last_properties == NULL) {
937 IDPropertyTemplate val = {0};
938 ot->last_properties = IDP_New(IDP_GROUP, &val, "wmOperatorProperties");
940 return ot->last_properties;
943 void WM_operator_last_properties_ensure(wmOperatorType *ot, PointerRNA *ptr)
945 IDProperty *props = WM_operator_last_properties_ensure_idprops(ot);
946 RNA_pointer_create(NULL, ot->srna, props, ptr);
950 * Use for drag & drop a path or name with operators invoke() function.
952 ID *WM_operator_drop_load_path(struct bContext *C, wmOperator *op, const short idcode)
954 Main *bmain = CTX_data_main(C);
956 /* check input variables */
957 if (RNA_struct_property_is_set(op->ptr, "filepath")) {
958 const bool is_relative_path = RNA_boolean_get(op->ptr, "relative_path");
962 RNA_string_get(op->ptr, "filepath", path);
966 if (idcode == ID_IM) {
967 id = (ID *)BKE_image_load_exists_ex(bmain, path, &exists);
974 BKE_reportf(op->reports, RPT_ERROR, "Cannot read %s '%s': %s",
975 BKE_idcode_to_name(idcode), path,
976 errno ? strerror(errno) : TIP_("unsupported format"));
980 if (is_relative_path ) {
981 if (exists == false) {
982 if (idcode == ID_IM) {
983 BLI_path_rel(((Image *)id)->name, BKE_main_blendfile_path(bmain));
991 else if (RNA_struct_property_is_set(op->ptr, "name")) {
992 char name[MAX_ID_NAME - 2];
993 RNA_string_get(op->ptr, "name", name);
994 id = BKE_libblock_find_name(bmain, idcode, name);
996 BKE_reportf(op->reports, RPT_ERROR, "%s '%s' not found",
997 BKE_idcode_to_name(idcode), name);
1006 static void wm_block_redo_cb(bContext *C, void *arg_op, int UNUSED(arg_event))
1008 wmOperator *op = arg_op;
1010 if (op == WM_operator_last_redo(C)) {
1011 /* operator was already executed once? undo & repeat */
1012 ED_undo_operator_repeat(C, op);
1015 /* operator not executed yet, call it */
1016 ED_undo_push_op(C, op);
1017 wm_operator_register(C, op);
1019 WM_operator_repeat(C, op);
1023 static void wm_block_redo_cancel_cb(bContext *C, void *arg_op)
1025 wmOperator *op = arg_op;
1027 /* if operator never got executed, free it */
1028 if (op != WM_operator_last_redo(C))
1029 WM_operator_free(op);
1032 static uiBlock *wm_block_create_redo(bContext *C, ARegion *ar, void *arg_op)
1034 wmOperator *op = arg_op;
1037 uiStyle *style = UI_style_get();
1038 int width = 15 * UI_UNIT_X;
1040 block = UI_block_begin(C, ar, __func__, UI_EMBOSS);
1041 UI_block_flag_disable(block, UI_BLOCK_LOOP);
1042 UI_block_theme_style_set(block, UI_BLOCK_THEME_STYLE_REGULAR);
1044 /* UI_BLOCK_NUMSELECT for layer buttons */
1045 UI_block_flag_enable(block, UI_BLOCK_NUMSELECT | UI_BLOCK_KEEP_OPEN | UI_BLOCK_MOVEMOUSE_QUIT);
1047 /* if register is not enabled, the operator gets freed on OPERATOR_FINISHED
1048 * ui_apply_but_funcs_after calls ED_undo_operator_repeate_cb and crashes */
1049 assert(op->type->flag & OPTYPE_REGISTER);
1051 UI_block_func_handle_set(block, wm_block_redo_cb, arg_op);
1052 layout = UI_block_layout(block, UI_LAYOUT_VERTICAL, UI_LAYOUT_PANEL, 0, 0, width, UI_UNIT_Y, 0, style);
1054 if (op == WM_operator_last_redo(C))
1055 if (!WM_operator_check_ui_enabled(C, op->type->name))
1056 uiLayoutSetEnabled(layout, false);
1058 if (op->type->flag & OPTYPE_MACRO) {
1059 for (op = op->macro.first; op; op = op->next) {
1060 uiTemplateOperatorPropertyButs(
1061 C, layout, op, UI_BUT_LABEL_ALIGN_SPLIT_COLUMN,
1062 UI_TEMPLATE_OP_PROPS_SHOW_TITLE);
1068 uiTemplateOperatorPropertyButs(
1069 C, layout, op, UI_BUT_LABEL_ALIGN_SPLIT_COLUMN,
1070 UI_TEMPLATE_OP_PROPS_SHOW_TITLE);
1073 UI_block_bounds_set_popup(block, 4, 0, 0);
1078 typedef struct wmOpPopUp {
1085 /* Only invoked by OK button in popups created with wm_block_dialog_create() */
1086 static void dialog_exec_cb(bContext *C, void *arg1, void *arg2)
1088 wmOpPopUp *data = arg1;
1089 uiBlock *block = arg2;
1091 /* Explicitly set UI_RETURN_OK flag, otherwise the menu might be canceled
1092 * in case WM_operator_call_ex exits/reloads the current file (T49199). */
1093 UI_popup_menu_retval_set(block, UI_RETURN_OK, true);
1095 WM_operator_call_ex(C, data->op, true);
1097 /* let execute handle freeing it */
1098 //data->free_op = false;
1101 /* in this case, wm_operator_ui_popup_cancel wont run */
1104 /* get context data *after* WM_operator_call_ex which might have closed the current file and changed context */
1105 wmWindowManager *wm = CTX_wm_manager(C);
1106 wmWindow *win = CTX_wm_window(C);
1108 /* check window before 'block->handle' incase the
1109 * popup execution closed the window and freed the block. see T44688.
1111 /* Post 2.78 TODO: Check if this fix and others related to T44688 are still
1112 * needed or can be improved now that requesting context data has been corrected
1113 * (see above). We're close to release so not a good time for experiments.
1116 if (BLI_findindex(&wm->windows, win) != -1) {
1117 UI_popup_block_close(C, win, block);
1121 /* Dialogs are popups that require user verification (click OK) before exec */
1122 static uiBlock *wm_block_dialog_create(bContext *C, ARegion *ar, void *userData)
1124 wmOpPopUp *data = userData;
1125 wmOperator *op = data->op;
1128 uiStyle *style = UI_style_get();
1130 block = UI_block_begin(C, ar, __func__, UI_EMBOSS);
1131 UI_block_flag_disable(block, UI_BLOCK_LOOP);
1132 UI_block_theme_style_set(block, UI_BLOCK_THEME_STYLE_REGULAR);
1134 /* intentionally don't use 'UI_BLOCK_MOVEMOUSE_QUIT', some dialogues have many items
1135 * where quitting by accident is very annoying */
1136 UI_block_flag_enable(block, UI_BLOCK_KEEP_OPEN | UI_BLOCK_NUMSELECT);
1138 layout = UI_block_layout(block, UI_LAYOUT_VERTICAL, UI_LAYOUT_PANEL, 0, 0, data->width, data->height, 0, style);
1140 uiTemplateOperatorPropertyButs(
1141 C, layout, op, UI_BUT_LABEL_ALIGN_SPLIT_COLUMN,
1142 UI_TEMPLATE_OP_PROPS_SHOW_TITLE);
1144 /* clear so the OK button is left alone */
1145 UI_block_func_set(block, NULL, NULL, NULL);
1147 /* new column so as not to interfere with custom layouts [#26436] */
1153 col = uiLayoutColumn(layout, false);
1154 col_block = uiLayoutGetBlock(col);
1155 /* Create OK button, the callback of which will execute op */
1156 btn = uiDefBut(col_block, UI_BTYPE_BUT, 0, IFACE_("OK"), 0, -30, 0, UI_UNIT_Y, NULL, 0, 0, 0, 0, "");
1157 UI_but_func_set(btn, dialog_exec_cb, data, col_block);
1160 /* center around the mouse */
1161 UI_block_bounds_set_popup(block, 4, data->width / -2, data->height / 2);
1166 static uiBlock *wm_operator_ui_create(bContext *C, ARegion *ar, void *userData)
1168 wmOpPopUp *data = userData;
1169 wmOperator *op = data->op;
1172 uiStyle *style = UI_style_get();
1174 block = UI_block_begin(C, ar, __func__, UI_EMBOSS);
1175 UI_block_flag_disable(block, UI_BLOCK_LOOP);
1176 UI_block_flag_enable(block, UI_BLOCK_KEEP_OPEN | UI_BLOCK_MOVEMOUSE_QUIT);
1177 UI_block_theme_style_set(block, UI_BLOCK_THEME_STYLE_REGULAR);
1179 layout = UI_block_layout(block, UI_LAYOUT_VERTICAL, UI_LAYOUT_PANEL, 0, 0, data->width, data->height, 0, style);
1181 /* since ui is defined the auto-layout args are not used */
1182 uiTemplateOperatorPropertyButs(C, layout, op, UI_BUT_LABEL_ALIGN_COLUMN, 0);
1184 UI_block_func_set(block, NULL, NULL, NULL);
1186 UI_block_bounds_set_popup(block, 4, 0, 0);
1191 static void wm_operator_ui_popup_cancel(struct bContext *C, void *userData)
1193 wmOpPopUp *data = userData;
1194 wmOperator *op = data->op;
1197 if (op->type->cancel) {
1198 op->type->cancel(C, op);
1201 if (data->free_op) {
1202 WM_operator_free(op);
1209 static void wm_operator_ui_popup_ok(struct bContext *C, void *arg, int retval)
1211 wmOpPopUp *data = arg;
1212 wmOperator *op = data->op;
1214 if (op && retval > 0)
1215 WM_operator_call_ex(C, op, true);
1220 int WM_operator_ui_popup(bContext *C, wmOperator *op, int width, int height)
1222 wmOpPopUp *data = MEM_callocN(sizeof(wmOpPopUp), "WM_operator_ui_popup");
1224 data->width = width;
1225 data->height = height;
1226 data->free_op = true; /* if this runs and gets registered we may want not to free it */
1227 UI_popup_block_ex(C, wm_operator_ui_create, NULL, wm_operator_ui_popup_cancel, data, op);
1228 return OPERATOR_RUNNING_MODAL;
1232 * For use by #WM_operator_props_popup_call, #WM_operator_props_popup only.
1234 * \note operator menu needs undo flag enabled, for redo callback */
1235 static int wm_operator_props_popup_ex(bContext *C, wmOperator *op,
1236 const bool do_call, const bool do_redo)
1238 if ((op->type->flag & OPTYPE_REGISTER) == 0) {
1239 BKE_reportf(op->reports, RPT_ERROR,
1240 "Operator '%s' does not have register enabled, incorrect invoke function", op->type->idname);
1241 return OPERATOR_CANCELLED;
1245 if ((op->type->flag & OPTYPE_UNDO) == 0) {
1246 BKE_reportf(op->reports, RPT_ERROR,
1247 "Operator '%s' does not have undo enabled, incorrect invoke function", op->type->idname);
1248 return OPERATOR_CANCELLED;
1252 /* if we don't have global undo, we can't do undo push for automatic redo,
1253 * so we require manual OK clicking in this popup */
1254 if (!do_redo || !(U.uiflag & USER_GLOBALUNDO))
1255 return WM_operator_props_dialog_popup(C, op, 300, 20);
1257 UI_popup_block_ex(C, wm_block_create_redo, NULL, wm_block_redo_cancel_cb, op, op);
1260 wm_block_redo_cb(C, op, 0);
1262 return OPERATOR_RUNNING_MODAL;
1266 * Same as #WM_operator_props_popup but don't use operator redo.
1267 * just wraps #WM_operator_props_dialog_popup.
1269 int WM_operator_props_popup_confirm(bContext *C, wmOperator *op, const wmEvent *UNUSED(event))
1271 return wm_operator_props_popup_ex(C, op, false, false);
1275 * Same as #WM_operator_props_popup but call the operator first,
1276 * This way - the button values correspond to the result of the operator.
1277 * Without this, first access to a button will make the result jump, see T32452.
1279 int WM_operator_props_popup_call(bContext *C, wmOperator *op, const wmEvent *UNUSED(event))
1281 return wm_operator_props_popup_ex(C, op, true, true);
1284 int WM_operator_props_popup(bContext *C, wmOperator *op, const wmEvent *UNUSED(event))
1286 return wm_operator_props_popup_ex(C, op, false, true);
1289 int WM_operator_props_dialog_popup(bContext *C, wmOperator *op, int width, int height)
1291 wmOpPopUp *data = MEM_callocN(sizeof(wmOpPopUp), "WM_operator_props_dialog_popup");
1294 data->width = width * U.dpi_fac;
1295 data->height = height * U.dpi_fac;
1296 data->free_op = true; /* if this runs and gets registered we may want not to free it */
1298 /* op is not executed until popup OK but is clicked */
1299 UI_popup_block_ex(C, wm_block_dialog_create, wm_operator_ui_popup_ok, wm_operator_ui_popup_cancel, data, op);
1301 return OPERATOR_RUNNING_MODAL;
1304 int WM_operator_redo_popup(bContext *C, wmOperator *op)
1306 /* CTX_wm_reports(C) because operator is on stack, not active in event system */
1307 if ((op->type->flag & OPTYPE_REGISTER) == 0) {
1308 BKE_reportf(CTX_wm_reports(C), RPT_ERROR,
1309 "Operator redo '%s' does not have register enabled, incorrect invoke function", op->type->idname);
1310 return OPERATOR_CANCELLED;
1312 if (op->type->poll && op->type->poll(C) == 0) {
1313 BKE_reportf(CTX_wm_reports(C), RPT_ERROR, "Operator redo '%s': wrong context", op->type->idname);
1314 return OPERATOR_CANCELLED;
1317 UI_popup_block_invoke(C, wm_block_create_redo, op);
1319 return OPERATOR_CANCELLED;
1322 /* ***************** Debug menu ************************* */
1324 static int wm_debug_menu_exec(bContext *C, wmOperator *op)
1326 G.debug_value = RNA_int_get(op->ptr, "debug_value");
1327 ED_screen_refresh(CTX_wm_manager(C), CTX_wm_window(C));
1328 WM_event_add_notifier(C, NC_WINDOW, NULL);
1330 return OPERATOR_FINISHED;
1333 static int wm_debug_menu_invoke(bContext *C, wmOperator *op, const wmEvent *UNUSED(event))
1335 RNA_int_set(op->ptr, "debug_value", G.debug_value);
1336 return WM_operator_props_dialog_popup(C, op, 180, 20);
1339 static void WM_OT_debug_menu(wmOperatorType *ot)
1341 ot->name = "Debug Menu";
1342 ot->idname = "WM_OT_debug_menu";
1343 ot->description = "Open a popup to set the debug level";
1345 ot->invoke = wm_debug_menu_invoke;
1346 ot->exec = wm_debug_menu_exec;
1347 ot->poll = WM_operator_winactive;
1349 RNA_def_int(ot->srna, "debug_value", 0, SHRT_MIN, SHRT_MAX, "Debug Value", "", -10000, 10000);
1352 /* ***************** Operator defaults ************************* */
1353 static int wm_operator_defaults_exec(bContext *C, wmOperator *op)
1355 PointerRNA ptr = CTX_data_pointer_get_type(C, "active_operator", &RNA_Operator);
1358 BKE_report(op->reports, RPT_ERROR, "No operator in context");
1359 return OPERATOR_CANCELLED;
1362 WM_operator_properties_reset((wmOperator *)ptr.data);
1363 return OPERATOR_FINISHED;
1366 /* used by operator preset menu. pre-2.65 this was a 'Reset' button */
1367 static void WM_OT_operator_defaults(wmOperatorType *ot)
1369 ot->name = "Restore Defaults";
1370 ot->idname = "WM_OT_operator_defaults";
1371 ot->description = "Set the active operator to its default values";
1373 ot->exec = wm_operator_defaults_exec;
1375 ot->flag = OPTYPE_INTERNAL;
1378 /* ***************** Splash Screen ************************* */
1380 static void wm_block_splash_close(bContext *C, void *arg_block, void *UNUSED(arg))
1382 wmWindow *win = CTX_wm_window(C);
1383 UI_popup_block_close(C, win, arg_block);
1386 static uiBlock *wm_block_create_splash(bContext *C, ARegion *ar, void *arg_unused);
1388 static void wm_block_splash_refreshmenu(bContext *C, void *UNUSED(arg_block), void *UNUSED(arg))
1390 ARegion *ar_menu = CTX_wm_menu(C);
1391 ED_region_tag_refresh_ui(ar_menu);
1394 static uiBlock *wm_block_create_splash(bContext *C, ARegion *ar, void *UNUSED(arg))
1398 uiStyle *style = UI_style_get();
1400 #ifndef WITH_HEADLESS
1401 extern char datatoc_splash_png[];
1402 extern int datatoc_splash_png_size;
1404 extern char datatoc_splash_2x_png[];
1405 extern int datatoc_splash_2x_png_size;
1411 #ifdef WITH_BUILDINFO
1412 int label_delta = 0;
1413 int hash_width, date_width;
1414 char date_buf[128] = "\0";
1415 char hash_buf[128] = "\0";
1416 extern unsigned long build_commit_timestamp;
1417 extern char build_hash[], build_commit_date[], build_commit_time[], build_branch[];
1419 /* Builds made from tag only shows tag sha */
1420 BLI_snprintf(hash_buf, sizeof(hash_buf), "Hash: %s", build_hash);
1421 BLI_snprintf(date_buf, sizeof(date_buf), "Date: %s %s", build_commit_date, build_commit_time);
1423 BLF_size(style->widgetlabel.uifont_id, style->widgetlabel.points, U.pixelsize * U.dpi);
1424 hash_width = (int)BLF_width(style->widgetlabel.uifont_id, hash_buf, sizeof(hash_buf)) + U.widget_unit;
1425 date_width = (int)BLF_width(style->widgetlabel.uifont_id, date_buf, sizeof(date_buf)) + U.widget_unit;
1426 #endif /* WITH_BUILDINFO */
1428 #ifndef WITH_HEADLESS
1429 if (U.dpi_fac > 1.0) {
1430 ibuf = IMB_ibImageFromMemory((unsigned char *)datatoc_splash_2x_png,
1431 datatoc_splash_2x_png_size, IB_rect, NULL, "<splash screen>");
1434 ibuf = IMB_ibImageFromMemory((unsigned char *)datatoc_splash_png,
1435 datatoc_splash_png_size, IB_rect, NULL, "<splash screen>");
1438 /* overwrite splash with template image */
1439 if (U.app_template[0] != '\0') {
1440 ImBuf *ibuf_template = NULL;
1441 char splash_filepath[FILE_MAX];
1442 char template_directory[FILE_MAX];
1444 if (BKE_appdir_app_template_id_search(
1446 template_directory, sizeof(template_directory)))
1449 splash_filepath, sizeof(splash_filepath), template_directory,
1450 (U.pixelsize == 2) ? "splash_2x.png" : "splash.png");
1451 ibuf_template = IMB_loadiffname(splash_filepath, IB_rect, NULL);
1452 if (ibuf_template) {
1453 const int x_expect = ibuf->x;
1454 const int y_expect = 250 * (int)U.dpi_fac;
1455 /* don't cover the header text */
1456 if (ibuf_template->x == x_expect && ibuf_template->y == y_expect) {
1457 memcpy(ibuf->rect, ibuf_template->rect, ibuf_template->x * ibuf_template->y * sizeof(char[4]));
1460 CLOG_ERROR(WM_LOG_OPERATORS,
1461 "Splash expected %dx%d found %dx%d, ignoring: %s\n",
1462 x_expect, y_expect, ibuf_template->x, ibuf_template->y, splash_filepath);
1464 IMB_freeImBuf(ibuf_template);
1470 block = UI_block_begin(C, ar, "splash", UI_EMBOSS);
1472 /* note on UI_BLOCK_NO_WIN_CLIP, the window size is not always synchronized
1473 * with the OS when the splash shows, window clipping in this case gives
1474 * ugly results and clipping the splash isn't useful anyway, just disable it [#32938] */
1475 UI_block_flag_enable(block, UI_BLOCK_LOOP | UI_BLOCK_KEEP_OPEN | UI_BLOCK_NO_WIN_CLIP);
1476 UI_block_theme_style_set(block, UI_BLOCK_THEME_STYLE_POPUP);
1478 but = uiDefBut(block, UI_BTYPE_IMAGE, 0, "", 0, 0.5f * U.widget_unit, U.dpi_fac * 501, U.dpi_fac * 250, ibuf, 0.0, 0.0, 0, 0, ""); /* button owns the imbuf now */
1479 UI_but_func_set(but, wm_block_splash_close, block, NULL);
1480 UI_block_func_set(block, wm_block_splash_refreshmenu, block, NULL);
1482 /* label for 'a' bugfix releases, or 'Release Candidate 1'...
1483 * avoids recreating splash for version updates */
1484 const char *version_suffix = NULL;
1486 if (STREQ(STRINGIFY(BLENDER_VERSION_CYCLE), "alpha")) {
1487 version_suffix = " Alpha";
1489 else if (STREQ(STRINGIFY(BLENDER_VERSION_CYCLE), "beta")) {
1490 version_suffix = " Beta";
1492 else if (STREQ(STRINGIFY(BLENDER_VERSION_CYCLE), "rc")) {
1493 version_suffix = " Release Candidate";
1495 else if (STREQ(STRINGIFY(BLENDER_VERSION_CYCLE), "release")) {
1496 version_suffix = STRINGIFY(BLENDER_VERSION_CHAR);
1499 char *version = BLI_sprintfN("Version %d.%d%s", BLENDER_VERSION / 100, BLENDER_VERSION % 100, version_suffix);
1501 if (version != NULL && version[0]) {
1502 /* placed after the version number in the image,
1503 * placing y is tricky to match baseline */
1504 /* hack to have text draw 'text_sel' */
1505 UI_block_emboss_set(block, UI_EMBOSS_NONE);
1506 int x = 202 * U.dpi_fac;
1507 int y = 130 * U.dpi_fac;
1508 int w = 240 * U.dpi_fac;
1511 but = uiDefBut(block, UI_BTYPE_LABEL, 0, version, x, y, w, UI_UNIT_Y, NULL, 0, 0, 0, 0, NULL);
1512 /* XXX, set internal flag - UI_SELECT */
1513 UI_but_flag_enable(but, 1);
1514 UI_block_emboss_set(block, UI_EMBOSS);
1519 #ifdef WITH_BUILDINFO
1520 if (build_commit_timestamp != 0) {
1522 block, UI_BTYPE_LABEL, 0, date_buf,
1523 U.dpi_fac * 502 - date_width, U.dpi_fac * 237,
1524 date_width, UI_UNIT_Y, NULL, 0, 0, 0, 0, NULL);
1525 /* XXX, set internal flag - UI_SELECT */
1526 UI_but_flag_enable(but, 0);
1530 block, UI_BTYPE_LABEL, 0, hash_buf,
1531 U.dpi_fac * 502 - hash_width, U.dpi_fac * (237 - label_delta),
1532 hash_width, UI_UNIT_Y, NULL, 0, 0, 0, 0, NULL);
1533 /* XXX, set internal flag - UI_SELECT */
1534 UI_but_flag_enable(but, 0);
1536 if (!STREQ(build_branch, "master")) {
1537 char branch_buf[128] = "\0";
1539 BLI_snprintf(branch_buf, sizeof(branch_buf), "Branch: %s", build_branch);
1540 branch_width = (int)BLF_width(style->widgetlabel.uifont_id, branch_buf, sizeof(branch_buf)) + U.widget_unit;
1542 block, UI_BTYPE_LABEL, 0, branch_buf,
1543 U.dpi_fac * 502 - branch_width, U.dpi_fac * (225 - label_delta),
1544 branch_width, UI_UNIT_Y, NULL, 0, 0, 0, 0, NULL);
1545 /* XXX, set internal flag - UI_SELECT */
1546 UI_but_flag_enable(but, 0);
1548 #endif /* WITH_BUILDINFO */
1550 uiLayout *layout = UI_block_layout(
1551 block, UI_LAYOUT_VERTICAL, UI_LAYOUT_PANEL, U.dpi_fac * 40, 0,
1552 U.dpi_fac * 450, U.dpi_fac * 110, 0, style);
1554 MenuType *mt = WM_menutype_find("WM_MT_splash", true);
1556 UI_menutype_draw(C, mt, layout);
1559 UI_block_bounds_set_centered(block, 0);
1564 static int wm_splash_invoke(bContext *C, wmOperator *UNUSED(op), const wmEvent *UNUSED(event))
1566 UI_popup_block_invoke(C, wm_block_create_splash, NULL);
1568 return OPERATOR_FINISHED;
1571 static void WM_OT_splash(wmOperatorType *ot)
1573 ot->name = "Splash Screen";
1574 ot->idname = "WM_OT_splash";
1575 ot->description = "Open the splash screen with release info";
1577 ot->invoke = wm_splash_invoke;
1578 ot->poll = WM_operator_winactive;
1582 /* ***************** Search menu ************************* */
1584 struct SearchPopupInit_Data {
1588 static uiBlock *wm_block_search_menu(bContext *C, ARegion *ar, void *userdata)
1590 const struct SearchPopupInit_Data *init_data = userdata;
1591 static char search[256] = "";
1593 wmWindow *win = CTX_wm_window(C);
1597 block = UI_block_begin(C, ar, "_popup", UI_EMBOSS);
1598 UI_block_flag_enable(block, UI_BLOCK_LOOP | UI_BLOCK_MOVEMOUSE_QUIT | UI_BLOCK_SEARCH_MENU);
1599 UI_block_theme_style_set(block, UI_BLOCK_THEME_STYLE_POPUP);
1601 but = uiDefSearchBut(block, search, 0, ICON_VIEWZOOM, sizeof(search), 10, 10, init_data->size[0], UI_UNIT_Y, 0, 0, "");
1602 UI_but_func_operator_search(but);
1604 /* fake button, it holds space for search items */
1605 uiDefBut(block, UI_BTYPE_LABEL, 0, "", 10, 10 - init_data->size[1],
1606 init_data->size[0], init_data->size[1], NULL, 0, 0, 0, 0, NULL);
1608 UI_block_bounds_set_popup(block, 6, 0, -UI_UNIT_Y); /* move it downwards, mouse over button */
1610 wm_event_init_from_window(win, &event);
1611 event.type = EVT_BUT_OPEN;
1612 event.val = KM_PRESS;
1613 event.customdata = but;
1614 event.customdatafree = false;
1615 wm_event_add(win, &event);
1620 static int wm_search_menu_exec(bContext *UNUSED(C), wmOperator *UNUSED(op))
1622 return OPERATOR_FINISHED;
1625 static int wm_search_menu_invoke(bContext *C, wmOperator *UNUSED(op), const wmEvent *event)
1627 /* Exception for launching via spacebar */
1628 if (event->type == SPACEKEY) {
1630 ScrArea *sa = CTX_wm_area(C);
1632 if (sa->spacetype == SPACE_CONSOLE) {
1633 /* So we can use the shortcut in the console. */
1636 else if (sa->spacetype == SPACE_TEXT) {
1637 /* So we can use the spacebar in the text editor. */
1642 Object *editob = CTX_data_edit_object(C);
1643 if (editob && editob->type == OB_FONT) {
1644 /* So we can use the spacebar for entering text. */
1649 return OPERATOR_PASS_THROUGH;
1654 static struct SearchPopupInit_Data data;
1655 data.size[0] = UI_searchbox_size_x() * 2;
1656 data.size[1] = UI_searchbox_size_y();
1658 UI_popup_block_invoke(C, wm_block_search_menu, &data);
1660 return OPERATOR_INTERFACE;
1663 static void WM_OT_search_menu(wmOperatorType *ot)
1665 ot->name = "Search Menu";
1666 ot->idname = "WM_OT_search_menu";
1667 ot->description = "Pop-up a search menu over all available operators in current context";
1669 ot->invoke = wm_search_menu_invoke;
1670 ot->exec = wm_search_menu_exec;
1671 ot->poll = WM_operator_winactive;
1674 static int wm_call_menu_exec(bContext *C, wmOperator *op)
1676 char idname[BKE_ST_MAXNAME];
1677 RNA_string_get(op->ptr, "name", idname);
1679 return UI_popup_menu_invoke(C, idname, op->reports);
1682 static void WM_OT_call_menu(wmOperatorType *ot)
1684 ot->name = "Call Menu";
1685 ot->idname = "WM_OT_call_menu";
1686 ot->description = "Call (draw) a pre-defined menu";
1688 ot->exec = wm_call_menu_exec;
1689 ot->poll = WM_operator_winactive;
1691 ot->flag = OPTYPE_INTERNAL;
1693 RNA_def_string(ot->srna, "name", NULL, BKE_ST_MAXNAME, "Name", "Name of the menu");
1696 static int wm_call_pie_menu_invoke(bContext *C, wmOperator *op, const wmEvent *event)
1698 char idname[BKE_ST_MAXNAME];
1699 RNA_string_get(op->ptr, "name", idname);
1701 return UI_pie_menu_invoke(C, idname, event);
1704 static int wm_call_pie_menu_exec(bContext *C, wmOperator *op)
1706 char idname[BKE_ST_MAXNAME];
1707 RNA_string_get(op->ptr, "name", idname);
1709 return UI_pie_menu_invoke(C, idname, CTX_wm_window(C)->eventstate);
1712 static void WM_OT_call_menu_pie(wmOperatorType *ot)
1714 ot->name = "Call Pie Menu";
1715 ot->idname = "WM_OT_call_menu_pie";
1716 ot->description = "Call (draw) a pre-defined pie menu";
1718 ot->invoke = wm_call_pie_menu_invoke;
1719 ot->exec = wm_call_pie_menu_exec;
1720 ot->poll = WM_operator_winactive;
1722 ot->flag = OPTYPE_INTERNAL;
1724 RNA_def_string(ot->srna, "name", NULL, BKE_ST_MAXNAME, "Name", "Name of the pie menu");
1727 static int wm_call_panel_exec(bContext *C, wmOperator *op)
1729 char idname[BKE_ST_MAXNAME];
1730 RNA_string_get(op->ptr, "name", idname);
1731 const bool keep_open = RNA_boolean_get(op->ptr, "keep_open");
1733 return UI_popover_panel_invoke(C, idname, keep_open, op->reports);
1736 static void WM_OT_call_panel(wmOperatorType *ot)
1738 ot->name = "Call Panel";
1739 ot->idname = "WM_OT_call_panel";
1740 ot->description = "Call (draw) a pre-defined panel";
1742 ot->exec = wm_call_panel_exec;
1743 ot->poll = WM_operator_winactive;
1745 ot->flag = OPTYPE_INTERNAL;
1749 prop = RNA_def_string(ot->srna, "name", NULL, BKE_ST_MAXNAME, "Name", "Name of the menu");
1750 RNA_def_property_flag(prop, PROP_SKIP_SAVE);
1751 prop = RNA_def_boolean(ot->srna, "keep_open", true, "Keep Open", "");
1752 RNA_def_property_flag(prop, PROP_SKIP_SAVE);
1755 /* ************ window / screen operator definitions ************** */
1757 /* this poll functions is needed in place of WM_operator_winactive
1758 * while it crashes on full screen */
1759 static bool wm_operator_winactive_normal(bContext *C)
1761 wmWindow *win = CTX_wm_window(C);
1766 if (!((screen = WM_window_get_active_screen(win)) && (screen->state == SCREENNORMAL)))
1772 /* included for script-access */
1773 static void WM_OT_window_close(wmOperatorType *ot)
1775 ot->name = "Close Window";
1776 ot->idname = "WM_OT_window_close";
1777 ot->description = "Close the current window";
1779 ot->exec = wm_window_close_exec;
1780 ot->poll = WM_operator_winactive;
1783 static void WM_OT_window_new(wmOperatorType *ot)
1785 ot->name = "New Window";
1786 ot->idname = "WM_OT_window_new";
1787 ot->description = "Create a new window";
1789 ot->exec = wm_window_new_exec;
1790 ot->poll = wm_operator_winactive_normal;
1793 static void WM_OT_window_new_main(wmOperatorType *ot)
1795 ot->name = "New Main Window";
1796 ot->idname = "WM_OT_window_new_main";
1797 ot->description = "Create a new main window with its own workspace and scene selection";
1799 ot->exec = wm_window_new_main_exec;
1800 ot->poll = wm_operator_winactive_normal;
1803 static void WM_OT_window_fullscreen_toggle(wmOperatorType *ot)
1805 ot->name = "Toggle Window Fullscreen";
1806 ot->idname = "WM_OT_window_fullscreen_toggle";
1807 ot->description = "Toggle the current window fullscreen";
1809 ot->exec = wm_window_fullscreen_toggle_exec;
1810 ot->poll = WM_operator_winactive;
1813 static int wm_exit_blender_exec(bContext *C, wmOperator *UNUSED(op))
1815 wm_quit_with_optional_confirmation_prompt(C, CTX_wm_window(C));
1816 return OPERATOR_FINISHED;
1819 static int wm_exit_blender_invoke(bContext *C, wmOperator *op, const wmEvent *event)
1821 if (U.uiflag & USER_QUIT_PROMPT) {
1822 return wm_exit_blender_exec(C, op);
1825 return WM_operator_confirm(C, op, event);
1829 static void WM_OT_quit_blender(wmOperatorType *ot)
1831 ot->name = "Quit Blender";
1832 ot->idname = "WM_OT_quit_blender";
1833 ot->description = "Quit Blender";
1835 ot->invoke = wm_exit_blender_invoke;
1836 ot->exec = wm_exit_blender_exec;
1839 /* *********************** */
1843 static int wm_console_toggle_exec(bContext *UNUSED(C), wmOperator *UNUSED(op))
1845 GHOST_toggleConsole(2);
1846 return OPERATOR_FINISHED;
1849 static void WM_OT_console_toggle(wmOperatorType *ot)
1851 /* XXX Have to mark these for xgettext, as under linux they do not exists... */
1852 ot->name = CTX_N_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Toggle System Console");
1853 ot->idname = "WM_OT_console_toggle";
1854 ot->description = N_("Toggle System Console");
1856 ot->exec = wm_console_toggle_exec;
1857 ot->poll = WM_operator_winactive;
1862 /* ************ default paint cursors, draw always around cursor *********** */
1864 * - returns handler to free
1865 * - poll(bContext): returns 1 if draw should happen
1866 * - draw(bContext): drawing callback for paint cursor
1869 wmPaintCursor *WM_paint_cursor_activate(
1870 wmWindowManager *wm,
1871 short space_type, short region_type,
1872 bool (*poll)(bContext *C),
1873 wmPaintCursorDraw draw, void *customdata)
1875 wmPaintCursor *pc = MEM_callocN(sizeof(wmPaintCursor), "paint cursor");
1877 BLI_addtail(&wm->paintcursors, pc);
1879 pc->customdata = customdata;
1883 pc->space_type = space_type;
1884 pc->region_type = region_type;
1889 bool WM_paint_cursor_end(wmWindowManager *wm, wmPaintCursor *handle)
1893 for (pc = wm->paintcursors.first; pc; pc = pc->next) {
1894 if (pc == (wmPaintCursor *)handle) {
1895 BLI_remlink(&wm->paintcursors, pc);
1903 /* *********************** radial control ****************** */
1905 #define WM_RADIAL_CONTROL_DISPLAY_SIZE (200 * UI_DPI_FAC)
1906 #define WM_RADIAL_CONTROL_DISPLAY_MIN_SIZE (35 * UI_DPI_FAC)
1907 #define WM_RADIAL_CONTROL_DISPLAY_WIDTH (WM_RADIAL_CONTROL_DISPLAY_SIZE - WM_RADIAL_CONTROL_DISPLAY_MIN_SIZE)
1908 #define WM_RADIAL_MAX_STR 10
1912 PropertySubType subtype;
1913 PointerRNA ptr, col_ptr, fill_col_ptr, rot_ptr, zoom_ptr, image_id_ptr;
1914 PointerRNA fill_col_override_ptr, fill_col_override_test_ptr;
1915 PropertyRNA *prop, *col_prop, *fill_col_prop, *rot_prop, *zoom_prop;
1916 PropertyRNA *fill_col_override_prop, *fill_col_override_test_prop;
1917 StructRNA *image_id_srna;
1918 float initial_value, current_value, min_value, max_value;
1919 int initial_mouse[2];
1924 ListBase orig_paintcursors;
1925 bool use_secondary_tex;
1930 static void radial_control_update_header(wmOperator *op, bContext *C)
1932 RadialControl *rc = op->customdata;
1933 char msg[UI_MAX_DRAW_STR];
1934 ScrArea *sa = CTX_wm_area(C);
1935 Scene *scene = CTX_data_scene(C);
1937 if (hasNumInput(&rc->num_input)) {
1938 char num_str[NUM_STR_REP_LEN];
1939 outputNumInput(&rc->num_input, num_str, &scene->unit);
1940 BLI_snprintf(msg, sizeof(msg), "%s: %s", RNA_property_ui_name(rc->prop), num_str);
1943 const char *ui_name = RNA_property_ui_name(rc->prop);
1944 switch (rc->subtype) {
1947 BLI_snprintf(msg, sizeof(msg), "%s: %0.4f", ui_name, rc->current_value);
1950 BLI_snprintf(msg, sizeof(msg), "%s: %d", ui_name, (int)rc->current_value); /* XXX: round to nearest? */
1952 case PROP_PERCENTAGE:
1953 BLI_snprintf(msg, sizeof(msg), "%s: %3.1f%%", ui_name, rc->current_value);
1956 BLI_snprintf(msg, sizeof(msg), "%s: %1.3f", ui_name, rc->current_value);
1959 BLI_snprintf(msg, sizeof(msg), "%s: %3.2f", ui_name, RAD2DEGF(rc->current_value));
1962 BLI_snprintf(msg, sizeof(msg), "%s", ui_name); /* XXX: No value? */
1967 ED_area_status_text(sa, msg);
1970 static void radial_control_set_initial_mouse(RadialControl *rc, const wmEvent *event)
1972 float d[2] = {0, 0};
1973 float zoom[2] = {1, 1};
1975 rc->initial_mouse[0] = event->x;
1976 rc->initial_mouse[1] = event->y;
1978 switch (rc->subtype) {
1982 d[0] = rc->initial_value;
1984 case PROP_PERCENTAGE:
1985 d[0] = (rc->initial_value) / 100.0f * WM_RADIAL_CONTROL_DISPLAY_WIDTH + WM_RADIAL_CONTROL_DISPLAY_MIN_SIZE;
1988 d[0] = (1 - rc->initial_value) * WM_RADIAL_CONTROL_DISPLAY_WIDTH + WM_RADIAL_CONTROL_DISPLAY_MIN_SIZE;
1991 d[0] = WM_RADIAL_CONTROL_DISPLAY_SIZE * cosf(rc->initial_value);
1992 d[1] = WM_RADIAL_CONTROL_DISPLAY_SIZE * sinf(rc->initial_value);
1998 if (rc->zoom_prop) {
1999 RNA_property_float_get_array(&rc->zoom_ptr, rc->zoom_prop, zoom);
2004 rc->initial_mouse[0] -= d[0];
2005 rc->initial_mouse[1] -= d[1];
2008 static void radial_control_set_tex(RadialControl *rc)
2012 switch (RNA_type_to_ID_code(rc->image_id_ptr.type)) {
2014 if ((ibuf = BKE_brush_gen_radial_control_imbuf(rc->image_id_ptr.data, rc->use_secondary_tex))) {
2015 glGenTextures(1, &rc->gltex);
2016 glBindTexture(GL_TEXTURE_2D, rc->gltex);
2017 glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, ibuf->x, ibuf->y, 0,
2018 GL_RED, GL_FLOAT, ibuf->rect_float);
2019 glBindTexture(GL_TEXTURE_2D, 0);
2020 MEM_freeN(ibuf->rect_float);
2029 static void radial_control_paint_tex(RadialControl *rc, float radius, float alpha)
2031 float col[3] = {0, 0, 0};
2034 /* set fill color */
2035 if (rc->fill_col_prop) {
2036 PointerRNA *fill_ptr;
2037 PropertyRNA *fill_prop;
2039 if (rc->fill_col_override_prop &&
2040 RNA_property_boolean_get(&rc->fill_col_override_test_ptr, rc->fill_col_override_test_prop))
2042 fill_ptr = &rc->fill_col_override_ptr;
2043 fill_prop = rc->fill_col_override_prop;
2046 fill_ptr = &rc->fill_col_ptr;
2047 fill_prop = rc->fill_col_prop;
2050 RNA_property_float_get_array(fill_ptr, fill_prop, col);
2053 GPUVertFormat *format = immVertexFormat();
2054 uint pos = GPU_vertformat_attr_add(format, "pos", GPU_COMP_F32, 2, GPU_FETCH_FLOAT);
2058 uint texCoord = GPU_vertformat_attr_add(format, "texCoord", GPU_COMP_F32, 2, GPU_FETCH_FLOAT);
2060 glActiveTexture(GL_TEXTURE0);
2061 glBindTexture(GL_TEXTURE_2D, rc->gltex);
2063 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
2064 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
2066 GLint swizzleMask[] = {GL_ZERO, GL_ZERO, GL_ZERO, GL_RED};
2067 glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_RGBA, swizzleMask);
2069 immBindBuiltinProgram(GPU_SHADER_2D_IMAGE_MASK_UNIFORM_COLOR);
2071 immUniformColor3fvAlpha(col, alpha);
2072 immUniform1i("image", 0);
2074 /* set up rotation if available */
2076 rot = RNA_property_float_get(&rc->rot_ptr, rc->rot_prop);
2078 GPU_matrix_rotate_2d(RAD2DEGF(rot));
2081 /* draw textured quad */
2082 immBegin(GPU_PRIM_TRI_FAN, 4);
2084 immAttr2f(texCoord, 0, 0);
2085 immVertex2f(pos, -radius, -radius);
2087 immAttr2f(texCoord, 1, 0);
2088 immVertex2f(pos, radius, -radius);
2090 immAttr2f(texCoord, 1, 1);
2091 immVertex2f(pos, radius, radius);
2093 immAttr2f(texCoord, 0, 1);
2094 immVertex2f(pos, -radius, radius);
2103 /* flat color if no texture available */
2104 immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
2105 immUniformColor3fvAlpha(col, alpha);
2106 imm_draw_circle_fill_2d(pos, 0.0f, 0.0f, radius, 40);
2112 static void radial_control_paint_cursor(bContext *UNUSED(C), int x, int y, void *customdata)
2114 RadialControl *rc = customdata;
2115 uiStyle *style = UI_style_get();
2116 const uiFontStyle *fstyle = &style->widget;
2117 const int fontid = fstyle->uifont_id;
2118 short fstyle_points = fstyle->points;
2119 char str[WM_RADIAL_MAX_STR];
2120 short strdrawlen = 0;
2121 float strwidth, strheight;
2122 float r1 = 0.0f, r2 = 0.0f, rmin = 0.0, tex_radius, alpha;
2123 float zoom[2], col[3] = {1, 1, 1};
2125 switch (rc->subtype) {
2129 r1 = rc->current_value;
2130 r2 = rc->initial_value;
2134 case PROP_PERCENTAGE:
2135 r1 = rc->current_value / 100.0f * WM_RADIAL_CONTROL_DISPLAY_WIDTH + WM_RADIAL_CONTROL_DISPLAY_MIN_SIZE;
2136 r2 = tex_radius = WM_RADIAL_CONTROL_DISPLAY_SIZE;
2137 rmin = WM_RADIAL_CONTROL_DISPLAY_MIN_SIZE;
2138 BLI_snprintf(str, WM_RADIAL_MAX_STR, "%3.1f%%", rc->current_value);
2139 strdrawlen = BLI_strlen_utf8(str);
2144 r1 = (1 - rc->current_value) * WM_RADIAL_CONTROL_DISPLAY_WIDTH + WM_RADIAL_CONTROL_DISPLAY_MIN_SIZE;
2145 r2 = tex_radius = WM_RADIAL_CONTROL_DISPLAY_SIZE;
2146 rmin = WM_RADIAL_CONTROL_DISPLAY_MIN_SIZE;
2147 alpha = rc->current_value / 2.0f + 0.5f;
2148 BLI_snprintf(str, WM_RADIAL_MAX_STR, "%1.3f", rc->current_value);
2149 strdrawlen = BLI_strlen_utf8(str);
2152 r1 = r2 = tex_radius = WM_RADIAL_CONTROL_DISPLAY_SIZE;
2154 rmin = WM_RADIAL_CONTROL_DISPLAY_MIN_SIZE;
2155 BLI_snprintf(str, WM_RADIAL_MAX_STR, "%3.2f", RAD2DEGF(rc->current_value));
2156 strdrawlen = BLI_strlen_utf8(str);
2159 tex_radius = WM_RADIAL_CONTROL_DISPLAY_SIZE; /* note, this is a dummy value */
2164 /* Keep cursor in the original place */
2165 x = rc->initial_mouse[0];
2166 y = rc->initial_mouse[1];
2167 GPU_matrix_translate_2f((float)x, (float)y);
2170 glEnable(GL_LINE_SMOOTH);
2172 /* apply zoom if available */
2173 if (rc->zoom_prop) {
2174 RNA_property_float_get_array(&rc->zoom_ptr, rc->zoom_prop, zoom);
2175 GPU_matrix_scale_2fv(zoom);
2178 /* draw rotated texture */
2179 radial_control_paint_tex(rc, tex_radius, alpha);
2181 /* set line color */
2183 RNA_property_float_get_array(&rc->col_ptr, rc->col_prop, col);
2185 GPUVertFormat *format = immVertexFormat();
2186 uint pos = GPU_vertformat_attr_add(format, "pos", GPU_COMP_F32, 2, GPU_FETCH_FLOAT);
2188 immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
2189 immUniformColor3fvAlpha(col, 0.5f);
2191 if (rc->subtype == PROP_ANGLE) {
2194 /* draw original angle line */
2195 GPU_matrix_rotate_2d(RAD2DEGF(rc->initial_value));
2196 immBegin(GPU_PRIM_LINES, 2);
2197 immVertex2f(pos, (float)WM_RADIAL_CONTROL_DISPLAY_MIN_SIZE, 0.0f);
2198 immVertex2f(pos, (float)WM_RADIAL_CONTROL_DISPLAY_SIZE, 0.0f);
2201 /* draw new angle line */
2202 GPU_matrix_rotate_2d(RAD2DEGF(rc->current_value - rc->initial_value));
2203 immBegin(GPU_PRIM_LINES, 2);
2204 immVertex2f(pos, (float)WM_RADIAL_CONTROL_DISPLAY_MIN_SIZE, 0.0f);
2205 immVertex2f(pos, (float)WM_RADIAL_CONTROL_DISPLAY_SIZE, 0.0f);
2211 /* draw circles on top */
2212 imm_draw_circle_wire_2d(pos, 0.0f, 0.0f, r1, 40);
2213 imm_draw_circle_wire_2d(pos, 0.0f, 0.0f, r2, 40);
2215 imm_draw_circle_wire_2d(pos, 0.0, 0.0f, rmin, 40);
2218 BLF_size(fontid, 1.5 * fstyle_points * U.pixelsize, U.dpi);
2219 BLF_enable(fontid, BLF_SHADOW);
2220 BLF_shadow(fontid, 3, (const float[4]){0.0f, 0.0f, 0.0f, 0.5f});
2221 BLF_shadow_offset(fontid, 1, -1);
2224 BLF_width_and_height(fontid, str, strdrawlen, &strwidth, &strheight);
2225 BLF_position(fontid, -0.5f * strwidth, -0.5f * strheight, 0.0f);
2226 BLF_draw(fontid, str, strdrawlen);
2228 BLF_disable(fontid, BLF_SHADOW);
2230 glDisable(GL_BLEND);
2231 glDisable(GL_LINE_SMOOTH);
2236 RC_PROP_ALLOW_MISSING = 1,
2237 RC_PROP_REQUIRE_FLOAT = 2,
2238 RC_PROP_REQUIRE_BOOL = 4,
2242 * Attempt to retrieve the rna pointer/property from an rna path.
2244 * \return 0 for failure, 1 for success, and also 1 if property is not set.
2246 static int radial_control_get_path(
2247 PointerRNA *ctx_ptr, wmOperator *op,
2248 const char *name, PointerRNA *r_ptr,
2249 PropertyRNA **r_prop, int req_length, RCPropFlags flags)
2251 PropertyRNA *unused_prop;
2256 if ((flags & RC_PROP_REQUIRE_BOOL) && (flags & RC_PROP_REQUIRE_FLOAT)) {
2257 BKE_report(op->reports, RPT_ERROR, "Property cannot be both boolean and float");
2261 /* get an rna string path from the operator's properties */
2262 if (!(str = RNA_string_get_alloc(op->ptr, name, NULL, 0)))
2265 if (str[0] == '\0') {
2266 if (r_prop) *r_prop = NULL;
2272 r_prop = &unused_prop;
2274 /* get rna from path */
2275 if (!RNA_path_resolve(ctx_ptr, str, r_ptr, r_prop)) {
2277 if (flags & RC_PROP_ALLOW_MISSING)
2280 BKE_reportf(op->reports, RPT_ERROR, "Could not resolve path '%s'", name);
2285 /* check property type */
2286 if (flags & (RC_PROP_REQUIRE_BOOL | RC_PROP_REQUIRE_FLOAT)) {
2287 PropertyType prop_type = RNA_property_type(*r_prop);
2289 if (((flags & RC_PROP_REQUIRE_BOOL) && (prop_type != PROP_BOOLEAN)) ||
2290 ((flags & RC_PROP_REQUIRE_FLOAT) && (prop_type != PROP_FLOAT)))
2293 BKE_reportf(op->reports, RPT_ERROR, "Property from path '%s' is not a float", name);
2298 /* check property's array length */
2299 if (*r_prop && (len = RNA_property_array_length(r_ptr, *r_prop)) != req_length) {
2301 BKE_reportf(op->reports, RPT_ERROR, "Property from path '%s' has length %d instead of %d",
2302 name, len, req_length);
2311 /* initialize the rna pointers and properties using rna paths */
2312 static int radial_control_get_properties(bContext *C, wmOperator *op)
2314 RadialControl *rc = op->customdata;
2315 PointerRNA ctx_ptr, use_secondary_ptr;
2316 PropertyRNA *use_secondary_prop = NULL;
2317 const char *data_path;
2319 RNA_pointer_create(NULL, &RNA_Context, C, &ctx_ptr);
2321 /* check if we use primary or secondary path */
2322 if (!radial_control_get_path(&ctx_ptr, op, "use_secondary",
2323 &use_secondary_ptr, &use_secondary_prop,
2324 0, (RC_PROP_ALLOW_MISSING |
2325 RC_PROP_REQUIRE_BOOL)))
2330 if (use_secondary_prop &&
2331 RNA_property_boolean_get(&use_secondary_ptr, use_secondary_prop))
2333 data_path = "data_path_secondary";
2336 data_path = "data_path_primary";
2340 if (!radial_control_get_path(&ctx_ptr, op, data_path, &rc->ptr, &rc->prop, 0, 0))
2343 /* data path is required */
2347 if (!radial_control_get_path(&ctx_ptr, op, "rotation_path", &rc->rot_ptr, &rc->rot_prop, 0, RC_PROP_REQUIRE_FLOAT))
2349 if (!radial_control_get_path(&ctx_ptr, op, "color_path", &rc->col_ptr, &rc->col_prop, 3, RC_PROP_REQUIRE_FLOAT))
2353 if (!radial_control_get_path(
2354 &ctx_ptr, op, "fill_color_path", &rc->fill_col_ptr, &rc->fill_col_prop, 3, RC_PROP_REQUIRE_FLOAT))
2359 if (!radial_control_get_path(
2360 &ctx_ptr, op, "fill_color_override_path",
2361 &rc->fill_col_override_ptr, &rc->fill_col_override_prop, 3, RC_PROP_REQUIRE_FLOAT))
2365 if (!radial_control_get_path(
2366 &ctx_ptr, op, "fill_color_override_test_path",
2367 &rc->fill_col_override_test_ptr, &rc->fill_col_override_test_prop, 0, RC_PROP_REQUIRE_BOOL))
2372 /* slightly ugly; allow this property to not resolve
2373 * correctly. needed because 3d texture paint shares the same
2374 * keymap as 2d image paint */
2375 if (!radial_control_get_path(&ctx_ptr, op, "zoom_path",
2376 &rc->zoom_ptr, &rc->zoom_prop, 2,
2377 RC_PROP_REQUIRE_FLOAT | RC_PROP_ALLOW_MISSING))
2382 if (!radial_control_get_path(&ctx_ptr, op, "image_id", &rc->image_id_ptr, NULL, 0, 0))
2384 else if (rc->image_id_ptr.data) {
2385 /* extra check, pointer must be to an ID */
2386 if (!RNA_struct_is_ID(rc->image_id_ptr.type)) {
2387 BKE_report(op->reports, RPT_ERROR, "Pointer from path image_id is not an ID");
2392 rc->use_secondary_tex = RNA_boolean_get(op->ptr, "secondary_tex");
2397 static int radial_control_invoke(bContext *C, wmOperator *op, const wmEvent *event)
2399 wmWindowManager *wm;
2403 if (!(op->customdata = rc = MEM_callocN(sizeof(RadialControl), "RadialControl")))
2404 return OPERATOR_CANCELLED;
2406 if (!radial_control_get_properties(C, op)) {
2408 return OPERATOR_CANCELLED;
2411 /* get type, initial, min, and max values of the property */
2412 switch ((rc->type = RNA_property_type(rc->prop))) {
2415 int value, min, max, step;
2417 value = RNA_property_int_get(&rc->ptr, rc->prop);
2418 RNA_property_int_ui_range(&rc->ptr, rc->prop, &min, &max, &step);
2420 rc->initial_value = value;
2421 rc->min_value = min_ii(value, min);
2422 rc->max_value = max_ii(value, max);
2427 float value, min, max, step, precision;
2429 value = RNA_property_float_get(&rc->ptr, rc->prop);
2430 RNA_property_float_ui_range(&rc->ptr, rc->prop, &min, &max, &step, &precision);
2432 rc->initial_value = value;
2433 rc->min_value = min_ff(value, min);
2434 rc->max_value = max_ff(value, max);
2438 BKE_report(op->reports, RPT_ERROR, "Property must be an integer or a float");
2440 return OPERATOR_CANCELLED;
2443 /* initialize numerical input */
2444 initNumInput(&rc->num_input);
2445 rc->num_input.idx_max = 0;
2446 rc->num_input.val_flag[0] |= NUM_NO_NEGATIVE;
2447 rc->num_input.unit_sys = USER_UNIT_NONE;
2448 rc->num_input.unit_type[0] = B_UNIT_LENGTH;
2450 /* get subtype of property */
2451 rc->subtype = RNA_property_subtype(rc->prop);
2452 if (!ELEM(rc->subtype, PROP_NONE, PROP_DISTANCE, PROP_FACTOR, PROP_PERCENTAGE, PROP_ANGLE, PROP_PIXEL)) {
2453 BKE_report(op->reports, RPT_ERROR, "Property must be a none, distance, factor, percentage, angle, or pixel");
2455 return OPERATOR_CANCELLED;
2458 rc->current_value = rc->initial_value;
2459 radial_control_set_initial_mouse(rc, event);
2460 radial_control_set_tex(rc);
2462 /* temporarily disable other paint cursors */
2463 wm = CTX_wm_manager(C);
2464 rc->orig_paintcursors = wm->paintcursors;
2465 BLI_listbase_clear(&wm->paintcursors);
2467 /* add radial control paint cursor */
2468 rc->cursor = WM_paint_cursor_activate(
2470 SPACE_TYPE_ANY, RGN_TYPE_ANY,
2472 radial_control_paint_cursor, rc);
2474 WM_event_add_modal_handler(C, op);
2476 return OPERATOR_RUNNING_MODAL;
2479 static void radial_control_set_value(RadialControl *rc, float val)
2483 RNA_property_int_set(&rc->ptr, rc->prop, val);
2486 RNA_property_float_set(&rc->ptr, rc->prop, val);
2493 static void radial_control_cancel(bContext *C, wmOperator *op)
2495 RadialControl *rc = op->customdata;
2496 wmWindowManager *wm = CTX_wm_manager(C);
2497 ScrArea *sa = CTX_wm_area(C);
2500 MEM_freeN(rc->dial);
2504 ED_area_status_text(sa, NULL);
2506 WM_paint_cursor_end(wm, rc->cursor);
2508 /* restore original paint cursors */
2509 wm->paintcursors = rc->orig_paintcursors;
2511 /* not sure if this is a good notifier to use;
2512 * intended purpose is to update the UI so that the
2513 * new value is displayed in sliders/numfields */
2514 WM_event_add_notifier(C, NC_WINDOW, NULL);
2516 glDeleteTextures(1, &rc->gltex);
2521 static int radial_control_modal(bContext *C, wmOperator *op, const wmEvent *event)
2523 RadialControl *rc = op->customdata;
2524 float new_value, dist = 0.0f, zoom[2];
2525 float delta[2], ret = OPERATOR_RUNNING_MODAL;
2527 float angle_precision = 0.0f;
2528 const bool has_numInput = hasNumInput(&rc->num_input);
2529 bool handled = false;
2531 /* TODO: fix hardcoded events */
2533 snap = event->ctrl != 0;
2535 /* Modal numinput active, try to handle numeric inputs first... */
2536 if (event->val == KM_PRESS && has_numInput && handleNumInput(C, &rc->num_input, event)) {
2538 applyNumInput(&rc->num_input, &numValue);
2540 if (rc->subtype == PROP_ANGLE) {
2541 numValue = DEG2RADF(numValue);
2542 numValue = fmod(numValue, 2.0f * (float)M_PI);
2543 if (numValue < 0.0f)
2544 numValue += 2.0f * (float)M_PI;
2547 CLAMP(numValue, rc->min_value, rc->max_value);
2548 new_value = numValue;
2550 radial_control_set_value(rc, new_value);
2551 rc->current_value = new_value;
2552 radial_control_update_header(op, C);
2553 return OPERATOR_RUNNING_MODAL;
2557 switch (event->type) {
2560 /* canceled; restore original value */
2561 radial_control_set_value(rc, rc->initial_value);
2562 ret = OPERATOR_CANCELLED;
2568 /* done; value already set */
2569 RNA_property_update(C, &rc->ptr, rc->prop);
2570 ret = OPERATOR_FINISHED;
2574 if (!has_numInput) {
2575 if (rc->slow_mode) {
2576 if (rc->subtype == PROP_ANGLE) {
2577 float position[2] = {event->x, event->y};
2579 /* calculate the initial angle here first */
2580 delta[0] = rc->initial_mouse[0] - rc->slow_mouse[0];
2581 delta[1] = rc->initial_mouse[1] - rc->slow_mouse[1];
2583 /* precision angle gets calculated from dial and gets added later */
2584 angle_precision = -0.1f * BLI_dial_angle(rc->dial, position);
2587 delta[0] = rc->initial_mouse[0] - rc->slow_mouse[0];
2588 delta[1] = rc->initial_mouse[1] - rc->slow_mouse[1];
2590 if (rc->zoom_prop) {
2591 RNA_property_float_get_array(&rc->zoom_ptr, rc->zoom_prop, zoom);
2592 delta[0] /= zoom[0];
2593 delta[1] /= zoom[1];
2596 dist = len_v2(delta);
2598 delta[0] = event->x - rc->slow_mouse[0];
2599 delta[1] = event->y - rc->slow_mouse[1];
2601 if (rc->zoom_prop) {
2602 delta[0] /= zoom[0];
2603 delta[1] /= zoom[1];
2606 dist = dist + 0.1f * (delta[0] + delta[1]);
2610 delta[0] = rc->initial_mouse[0] - event->x;
2611 delta[1] = rc->initial_mouse[1] - event->y;
2613 if (rc->zoom_prop) {
2614 RNA_property_float_get_array(&rc->zoom_ptr, rc->zoom_prop, zoom);
2615 delta[0] /= zoom[0];
2616 delta[1] /= zoom[1];
2619 dist = len_v2(delta);
2622 /* calculate new value and apply snapping */
2623 switch (rc->subtype) {
2628 if (snap) new_value = ((int)new_value + 5) / 10 * 10;
2630 case PROP_PERCENTAGE:
2631 new_value = ((dist - WM_RADIAL_CONTROL_DISPLAY_MIN_SIZE) / WM_RADIAL_CONTROL_DISPLAY_WIDTH) * 100.0f;
2632 if (snap) new_value = ((int)(new_value + 2.5f)) / 5 * 5;
2635 new_value = (WM_RADIAL_CONTROL_DISPLAY_SIZE - dist) / WM_RADIAL_CONTROL_DISPLAY_WIDTH;
2636 if (snap) new_value = ((int)ceil(new_value * 10.f) * 10.0f) / 100.f;
2639 new_value = atan2f(delta[1], delta[0]) + (float)M_PI + angle_precision;
2640 new_value = fmod(new_value, 2.0f * (float)M_PI);
2641 if (new_value < 0.0f)
2642 new_value += 2.0f * (float)M_PI;
2643 if (snap) new_value = DEG2RADF(((int)RAD2DEGF(new_value) + 5) / 10 * 10);
2646 new_value = dist; /* dummy value, should this ever happen? - campbell */
2650 /* clamp and update */
2651 CLAMP(new_value, rc->min_value, rc->max_value);
2652 radial_control_set_value(rc, new_value);
2653 rc->current_value = new_value;
2662 if (event->val == KM_PRESS) {
2663 rc->slow_mouse[0] = event->x;
2664 rc->slow_mouse[1] = event->y;
2665 rc->slow_mode = true;
2666 if (rc->subtype == PROP_ANGLE) {
2667 float initial_position[2] = {UNPACK2(rc->initial_mouse)};
2668 float current_position[2] = {UNPACK2(rc->slow_mouse)};
2669 rc->dial = BLI_dial_initialize(initial_position, 0.0f);
2670 /* immediately set the position to get a an initial direction */
2671 BLI_dial_angle(rc->dial, current_position);
2675 if (event->val == KM_RELEASE) {
2676 rc->slow_mode = false;
2679 MEM_freeN(rc->dial);
2687 /* Modal numinput inactive, try to handle numeric inputs last... */
2688 if (!handled && event->val == KM_PRESS && handleNumInput(C, &rc->num_input, event)) {
2689 applyNumInput(&rc->num_input, &numValue);
2691 if (rc->subtype == PROP_ANGLE) {
2692 numValue = DEG2RADF(numValue);
2693 numValue = fmod(numValue, 2.0f * (float)M_PI);
2694 if (numValue < 0.0f)
2695 numValue += 2.0f * (float)M_PI;
2698 CLAMP(numValue, rc->min_value, rc->max_value);
2699 new_value = numValue;
2701 radial_control_set_value(rc, new_value);
2703 rc->current_value = new_value;
2704 radial_control_update_header(op, C);
2705 return OPERATOR_RUNNING_MODAL;
2709 ED_region_tag_redraw(CTX_wm_region(C));
2710 radial_control_update_header(op, C);
2712 if (ret != OPERATOR_RUNNING_MODAL)
2713 radial_control_cancel(C, op);
2718 static void WM_OT_radial_control(wmOperatorType *ot)
2720 ot->name = "Radial Control";
2721 ot->idname = "WM_OT_radial_control";
2722 ot->description = "Set some size property (like e.g. brush size) with mouse wheel";
2724 ot->invoke = radial_control_invoke;
2725 ot->modal = radial_control_modal;
2726 ot->cancel = radial_control_cancel;
2728 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO | OPTYPE_BLOCKING;
2730 /* all paths relative to the context */
2732 prop = RNA_def_string(ot->srna, "data_path_primary", NULL, 0, "Primary Data Path", "Primary path of property to be set by the radial control");
2733 RNA_def_property_flag(prop, PROP_SKIP_SAVE);
2735 prop = RNA_def_string(ot->srna, "data_path_secondary", NULL, 0, "Secondary Data Path", "Secondary path of property to be set by the radial control");
2736 RNA_def_property_flag(prop, PROP_SKIP_SAVE);
2738 prop = RNA_def_string(ot->srna, "use_secondary", NULL, 0, "Use Secondary", "Path of property to select between the primary and secondary data paths");
2739 RNA_def_property_flag(prop, PROP_SKIP_SAVE);
2741 prop = RNA_def_string(ot->srna, "rotation_path", NULL, 0, "Rotation Path", "Path of property used to rotate the texture display");
2742 RNA_def_property_flag(prop, PROP_SKIP_SAVE);
2744 prop = RNA_def_string(ot->srna, "color_path", NULL, 0, "Color Path", "Path of property used to set the color of the control");
2745 RNA_def_property_flag(prop, PROP_SKIP_SAVE);
2747 prop = RNA_def_string(ot->srna, "fill_color_path", NULL, 0, "Fill Color Path", "Path of property used to set the fill color of the control");
2748 RNA_def_property_flag(prop, PROP_SKIP_SAVE);
2750 prop = RNA_def_string(ot->srna, "fill_color_override_path", NULL, 0, "Fill Color Override Path", "");
2751 RNA_def_property_flag(prop, PROP_SKIP_SAVE);
2752 prop = RNA_def_string(ot->srna, "fill_color_override_test_path", NULL, 0, "Fill Color Override Test", "");
2753 RNA_def_property_flag(prop, PROP_SKIP_SAVE);
2755 prop = RNA_def_string(ot->srna, "zoom_path", NULL, 0, "Zoom Path", "Path of property used to set the zoom level for the control");
2756 RNA_def_property_flag(prop, PROP_SKIP_SAVE);
2758 prop = RNA_def_string(ot->srna, "image_id", NULL, 0, "Image ID", "Path of ID that is used to generate an image for the control");
2759 RNA_def_property_flag(prop, PROP_SKIP_SAVE);
2761 prop = RNA_def_boolean(ot->srna, "secondary_tex", false, "Secondary Texture", "Tweak brush secondary/mask texture");
2762 RNA_def_property_flag(prop, PROP_SKIP_SAVE);
2765 /* ************************** timer for testing ***************** */
2767 /* uses no type defines, fully local testing function anyway... ;) */
2769 static void redraw_timer_window_swap(bContext *C)
2771 wmWindow *win = CTX_wm_window(C);
2773 CTX_wm_menu_set(C, NULL);
2775 for (sa = CTX_wm_screen(C)->areabase.first; sa; sa = sa->next)
2776 ED_area_tag_redraw(sa);
2779 CTX_wm_window_set(C, win); /* XXX context manipulation warning! */
2784 eRTDrawRegionSwap = 1,
2786 eRTDrawWindowSwap = 3,
2787 eRTAnimationStep = 4,
2788 eRTAnimationPlay = 5,
2792 static const EnumPropertyItem redraw_timer_type_items[] = {
2793 {eRTDrawRegion, "DRAW", 0, "Draw Region", "Draw Region"},
2794 {eRTDrawRegionSwap, "DRAW_SWAP", 0, "Draw Region + Swap", "Draw Region and Swap"},
2795 {eRTDrawWindow, "DRAW_WIN", 0, "Draw Window", "Draw Window"},
2796 {eRTDrawWindowSwap, "DRAW_WIN_SWAP", 0, "Draw Window + Swap", "Draw Window and Swap"},
2797 {eRTAnimationStep, "ANIM_STEP", 0, "Anim Step", "Animation Steps"},
2798 {eRTAnimationPlay, "ANIM_PLAY", 0, "Anim Play", "Animation Playback"},
2799 {eRTUndo, "UNDO", 0, "Undo/Redo", "Undo/Redo"},
2800 {0, NULL, 0, NULL, NULL},
2804 static void redraw_timer_step(
2805 bContext *C, Main *bmain, Scene *scene,
2806 struct Depsgraph *depsgraph,
2807 wmWindow *win, ScrArea *sa, ARegion *ar,
2808 const int type, const int cfra)
2810 if (type == eRTDrawRegion) {
2812 ED_region_do_draw(C, ar);
2813 ar->do_draw = false;
2816 else if (type == eRTDrawRegionSwap) {
2817 CTX_wm_menu_set(C, NULL);
2819 ED_region_tag_redraw(ar);
2822 CTX_wm_window_set(C, win); /* XXX context manipulation warning! */
2824 else if (type == eRTDrawWindow) {
2825 bScreen *screen = WM_window_get_active_screen(win);
2828 CTX_wm_menu_set(C, NULL);
2830 for (sa_iter = screen->areabase.first; sa_iter; sa_iter = sa_iter->next) {
2832 CTX_wm_area_set(C, sa_iter);
2834 for (ar_iter = sa_iter->regionbase.first; ar_iter; ar_iter = ar_iter->next) {
2835 if (ar_iter->visible) {
2836 CTX_wm_region_set(C, ar_iter);
2837 ED_region_do_draw(C, ar_iter);
2838 ar_iter->do_draw = false;
2843 CTX_wm_window_set(C, win); /* XXX context manipulation warning! */
2845 CTX_wm_area_set(C, sa);
2846 CTX_wm_region_set(C, ar);
2848 else if (type == eRTDrawWindowSwap) {
2849 redraw_timer_window_swap(C);
2851 else if (type == eRTAnimationStep) {
2852 scene->r.cfra += (cfra == scene->r.cfra) ? 1 : -1;
2853 BKE_scene_graph_update_for_newframe(depsgraph, bmain);
2855 else if (type == eRTAnimationPlay) {
2856 /* play anim, return on same frame as started with */
2857 int tot = (scene->r.efra - scene->r.sfra) + 1;
2860 /* todo, ability to escape! */
2862 if (scene->r.cfra > scene->r.efra)
2863 scene->r.cfra = scene->r.sfra;
2865 BKE_scene_graph_update_for_newframe(depsgraph, bmain);
2866 redraw_timer_window_swap(C);
2869 else { /* eRTUndo */
2875 static int redraw_timer_exec(bContext *C, wmOperator *op)
2877 Main *bmain = CTX_data_main(C);
2878 Scene *scene = CTX_data_scene(C);
2879 wmWindow *win = CTX_wm_window(C);
2880 ScrArea *sa = CTX_wm_area(C);
2881 ARegion *ar = CTX_wm_region(C);
2882 double time_start, time_delta;
2883 const int type = RNA_enum_get(op->ptr, "type");
2884 const int iter = RNA_int_get(op->ptr, "iterations");
2885 const double time_limit = (double)RNA_float_get(op->ptr, "time_limit");
2886 const int cfra = scene->r.cfra;
2887 int a, iter_steps = 0;
2888 const char *infostr = "";
2889 struct Depsgraph *depsgraph = CTX_data_depsgraph(C);
2893 time_start = PIL_check_seconds_timer();
2895 for (a = 0; a < iter; a++) {
2896 redraw_timer_step(C, bmain, scene, depsgraph, win, sa, ar, type, cfra);
2899 if (time_limit != 0.0) {
2900 if ((PIL_check_seconds_timer() - time_start) > time_limit) {
2907 time_delta = (PIL_check_seconds_timer() - time_start) * 1000;
2909 RNA_enum_description(redraw_timer_type_items, type, &infostr);
2913 BKE_reportf(op->reports, RPT_WARNING,
2914 "%d x %s: %.4f ms, average: %.8f ms",
2915 iter_steps, infostr, time_delta, time_delta / iter_steps);
2917 return OPERATOR_FINISHED;
2920 static void WM_OT_redraw_timer(wmOperatorType *ot)
2922 ot->name = "Redraw Timer";
2923 ot->idname = "WM_OT_redraw_timer";
2924 ot->description = "Simple redraw timer to test the speed of updating the interface";
2926 ot->invoke = WM_menu_invoke;
2927 ot->exec = redraw_timer_exec;
2928 ot->poll = WM_operator_winactive;
2930 ot->prop = RNA_def_enum(ot->srna, "type", redraw_timer_type_items, eRTDrawRegion, "Type", "");
2931 RNA_def_int(ot->srna, "iterations", 10, 1, INT_MAX, "Iterations", "Number of times to redraw", 1, 1000);
2932 RNA_def_float(ot->srna, "time_limit", 0.0, 0.0, FLT_MAX,
2933 "Time Limit", "Seconds to run the test for (override iterations)", 0.0, 60.0);
2937 /* ************************** memory statistics for testing ***************** */
2939 static int memory_statistics_exec(bContext *UNUSED(C), wmOperator *UNUSED(op))
2941 MEM_printmemlist_stats();
2942 return OPERATOR_FINISHED;
2945 static void WM_OT_memory_statistics(wmOperatorType *ot)
2947 ot->name = "Memory Statistics";
2948 ot->idname = "WM_OT_memory_statistics";
2949 ot->description = "Print memory statistics to the console";
2951 ot->exec = memory_statistics_exec;
2954 /* *************************** Mat/tex/etc. previews generation ************* */
2956 typedef struct PreviewsIDEnsureData {
2959 } PreviewsIDEnsureData;
2961 static void previews_id_ensure(bContext *C, Scene *scene, ID *id)
2963 BLI_assert(ELEM(GS(id->name), ID_MA, ID_TE, ID_IM, ID_WO, ID_LA));
2965 /* Only preview non-library datablocks, lib ones do not pertain to this .blend file!
2966 * Same goes for ID with no user. */
2967 if (!ID_IS_LINKED(id) && (id->us != 0)) {
2968 UI_id_icon_render(C, scene, id, false, false);
2969 UI_id_icon_render(C, scene, id, true, false);
2973 static int previews_id_ensure_callback(void *userdata, ID *UNUSED(self_id), ID **idptr, int cb_flag)
2975 if (cb_flag & IDWALK_CB_PRIVATE) {
2976 return IDWALK_RET_NOP;
2979 PreviewsIDEnsureData *data = userdata;
2982 if (id && (id->tag & LIB_TAG_DOIT)) {
2983 BLI_assert(ELEM(GS(id->name), ID_MA, ID_TE, ID_IM, ID_WO, ID_LA));
2984 previews_id_ensure(data->C, data->scene, id);
2985 id->tag &= ~LIB_TAG_DOIT;
2988 return IDWALK_RET_NOP;
2991 static int previews_ensure_exec(bContext *C, wmOperator *UNUSED(op))
2993 Main *bmain = CTX_data_main(C);
2994 ListBase *lb[] = {&bmain->mat, &bmain->tex, &bmain->image, &bmain->world, &bmain->lamp, NULL};
2995 PreviewsIDEnsureData preview_id_data;
3000 /* We use LIB_TAG_DOIT to check whether we have already handled a given ID or not. */
3001 BKE_main_id_tag_all(bmain, LIB_TAG_DOIT, false);
3002 for (i = 0; lb[i]; i++) {
3003 BKE_main_id_tag_listbase(lb[i], LIB_TAG_DOIT, true);
3006 preview_id_data.C = C;
3007 for (scene = bmain->scene.first; scene; scene = scene->id.next) {
3008 preview_id_data.scene = scene;
3011 BKE_library_foreach_ID_link(NULL, id, previews_id_ensure_callback, &preview_id_data, IDWALK_RECURSE);
3014 /* Check a last time for ID not used (fake users only, in theory), and
3015 * do our best for those, using current scene... */
3016 for (i = 0; lb[i]; i++) {
3017 for (id = lb[i]->first; id; id = id->next) {
3018 if (id->tag & LIB_TAG_DOIT) {
3019 previews_id_ensure(C, NULL, id);
3020 id->tag &= ~LIB_TAG_DOIT;
3025 return OPERATOR_FINISHED;
3028 static void WM_OT_previews_ensure(wmOperatorType *ot)
3030 ot->name = "Refresh Data-Block Previews";
3031 ot->idname = "WM_OT_previews_ensure";
3032 ot->description = "Ensure data-block previews are available and up-to-date "
3033 "(to be saved in .blend file, only for some types like materials, textures, etc.)";
3035 ot->exec = previews_ensure_exec;
3038 /* *************************** Datablocks previews clear ************* */
3040 /* Only types supporting previews currently. */
3041 static const EnumPropertyItem preview_id_type_items[] = {
3042 {FILTER_ID_SCE, "SCENE", 0, "Scenes", ""},
3043 {FILTER_ID_GR, "GROUP", 0, "Groups", ""},
3044 {FILTER_ID_OB, "OBJECT", 0, "Objects", ""},
3045 {FILTER_ID_MA, "MATERIAL", 0, "Materials", ""},
3046 {FILTER_ID_LA, "LIGHT", 0, "Lights", ""},
3047 {FILTER_ID_WO, "WORLD", 0, "Worlds", ""},
3048 {FILTER_ID_TE, "TEXTURE", 0, "Textures", ""},
3049 {FILTER_ID_IM, "IMAGE", 0, "Images", ""},
3050 #if 0 /* XXX TODO */
3051 {FILTER_ID_BR, "BRUSH", 0, "Brushes", ""},
3053 {0, NULL, 0, NULL, NULL},
3056 static int previews_clear_exec(bContext *C, wmOperator *op)
3058 Main *bmain = CTX_data_main(C);
3059 ListBase *lb[] = {&bmain->object, &bmain->collection,
3060 &bmain->mat, &bmain->world, &bmain->lamp, &bmain->tex, &bmain->image, NULL};
3063 const int id_filters = RNA_enum_get(op->ptr, "id_type");
3065 for (i = 0; lb[i]; i++) {
3066 ID *id = lb[i]->first;
3070 // printf("%s: %d, %d, %d -> %d\n", id->name, GS(id->name), BKE_idcode_to_idfilter(GS(id->name)),
3071 // id_filters, BKE_idcode_to_idfilter(GS(id->name)) & id_filters);
3073 if (!id || !(BKE_idcode_to_idfilter(GS(id->name)) & id_filters)) {
3077 for (; id; id = id->next) {
3078 PreviewImage *prv_img = BKE_previewimg_id_ensure(id);
3080 BKE_previewimg_clear(prv_img);
3084 return OPERATOR_FINISHED;
3087 static void WM_OT_previews_clear(wmOperatorType *ot)
3089 ot->name = "Clear Data-Block Previews";
3090 ot->idname = "WM_OT_previews_clear";
3091 ot->description = "Clear data-block previews (only for some types like objects, materials, textures, etc.)";
3093 ot->exec = previews_clear_exec;
3094 ot->invoke = WM_menu_invoke;
3096 ot->prop = RNA_def_enum_flag(ot->srna, "id_type", preview_id_type_items,
3097 FILTER_ID_SCE | FILTER_ID_OB | FILTER_ID_GR |
3098 FILTER_ID_MA | FILTER_ID_LA | FILTER_ID_WO | FILTER_ID_TE | FILTER_ID_IM,
3099 "Data-Block Type", "Which data-block previews to clear");
3102 /* *************************** Doc from UI ************* */
3104 static int doc_view_manual_ui_context_exec(bContext *C, wmOperator *UNUSED(op))
3106 PointerRNA ptr_props;
3108 short retval = OPERATOR_CANCELLED;
3110 if (UI_but_online_manual_id_from_active(C, buf, sizeof(buf))) {
3111 WM_operator_properties_create(&ptr_props, "WM_OT_doc_view_manual");
3112 RNA_string_set(&ptr_props, "doc_id", buf);
3114 retval = WM_operator_name_call_ptr(
3115 C, WM_operatortype_find("WM_OT_doc_view_manual", false),
3116 WM_OP_EXEC_DEFAULT, &ptr_props);
3118 WM_operator_properties_free(&ptr_props);
3124 static void WM_OT_doc_view_manual_ui_context(wmOperatorType *ot)
3127 ot->name = "View Online Manual";
3128 ot->idname = "WM_OT_doc_view_manual_ui_context";
3129 ot->description = "View a context based online manual in a web browser";
3132 ot->poll = ED_operator_regionactive;
3133 ot->exec = doc_view_manual_ui_context_exec;
3136 /* ******************************************************* */
3137 /* toggle 3D for current window, turning it fullscreen if needed */
3138 static void WM_OT_stereo3d_set(wmOperatorType *ot)
3142 ot->name = "Set Stereo 3D";
3143 ot->idname = "WM_OT_set_stereo_3d";
3144 ot->description = "Toggle 3D stereo support for current window (or change the display mode)";
3146 ot->exec = wm_stereo3d_set_exec;
3147 ot->invoke = wm_stereo3d_set_invoke;
3148 ot->poll = WM_operator_winactive;
3149 ot->ui = wm_stereo3d_set_draw;
3150 ot->check = wm_stereo3d_set_check;
3151 ot->cancel = wm_stereo3d_set_cancel;
3153 prop = RNA_def_enum(ot->srna, "display_mode", rna_enum_stereo3d_display_items, S3D_DISPLAY_ANAGLYPH, "Display Mode", "");
3154 RNA_def_property_flag(prop, PROP_SKIP_SAVE);
3155 prop = RNA_def_enum(ot->srna, "anaglyph_type", rna_enum_stereo3d_anaglyph_type_items, S3D_ANAGLYPH_REDCYAN, "Anaglyph Type", "");
3156 RNA_def_property_flag(prop, PROP_SKIP_SAVE);
3157 prop = RNA_def_enum(ot->srna, "interlace_type", rna_enum_stereo3d_interlace_type_items, S3D_INTERLACE_ROW, "Interlace Type", "");
3158 RNA_def_property_flag(prop, PROP_SKIP_SAVE);
3159 prop = RNA_def_boolean(ot->srna, "use_interlace_swap", false, "Swap Left/Right",
3160 "Swap left and right stereo channels");