diff --git a/source/blender/editors/interface/interface.cc b/source/blender/editors/interface/interface.cc index b5e4af3d03c..b511980e3d9 100644 --- a/source/blender/editors/interface/interface.cc +++ b/source/blender/editors/interface/interface.cc @@ -13,6 +13,8 @@ #include /* `offsetof()` */ #include +#include + #include "MEM_guardedalloc.h" #include "DNA_object_types.h" @@ -466,7 +468,7 @@ void ui_block_bounds_calc(uiBlock *block) /* hardcoded exception... but that one is annoying with larger safety */ uiBut *bt = static_cast(block->buttons.first); - const int xof = ((bt && STRPREFIX(bt->str, "ERROR")) ? 10 : 40) * UI_SCALE_FAC; + const int xof = ((bt && STRPREFIX(bt->str.c_str(), "ERROR")) ? 10 : 40) * UI_SCALE_FAC; block->safety.xmin = block->rect.xmin - xof; block->safety.ymin = block->rect.ymin - xof; @@ -918,22 +920,7 @@ static void ui_but_update_old_active_from_new(uiBut *oldbut, uiBut *but) /* move/copy string from the new button to the old */ /* needed for alt+mouse wheel over enums */ - if (but->str != but->strdata) { - if (oldbut->str != oldbut->strdata) { - std::swap(but->str, oldbut->str); - } - else { - oldbut->str = but->str; - but->str = but->strdata; - } - } - else { - if (oldbut->str != oldbut->strdata) { - MEM_freeN(oldbut->str); - oldbut->str = oldbut->strdata; - } - STRNCPY(oldbut->strdata, but->strdata); - } + std::swap(but->str, oldbut->str); if (but->dragpoin) { std::swap(but->dragpoin, oldbut->dragpoin); @@ -1161,11 +1148,11 @@ static void ui_menu_block_set_keyaccels(uiBlock *block) continue; } - if (but->str == nullptr || but->str[0] == '\0') { + if (but->str.empty()) { continue; } - const char *str_pt = but->str; + const char *str_pt = but->str.c_str(); uchar menu_key; do { menu_key = tolower(*str_pt); @@ -1214,9 +1201,9 @@ static void ui_menu_block_set_keyaccels(uiBlock *block) void ui_but_add_shortcut(uiBut *but, const char *shortcut_str, const bool do_strip) { if (do_strip && (but->flag & UI_BUT_HAS_SEP_CHAR)) { - char *cpoin = strrchr(but->str, UI_SEP_CHAR); - if (cpoin) { - *cpoin = '\0'; + const size_t sep_index = but->str.find_first_of(UI_SEP_CHAR); + if (sep_index != std::string::npos) { + but->str = but->str.substr(0, sep_index); } but->flag &= ~UI_BUT_HAS_SEP_CHAR; } @@ -1226,16 +1213,7 @@ void ui_but_add_shortcut(uiBut *but, const char *shortcut_str, const bool do_str return; } - char *butstr_orig; - if (but->str != but->strdata) { - butstr_orig = but->str; /* free after using as source buffer */ - } - else { - butstr_orig = BLI_strdup(but->str); - } - SNPRINTF(but->strdata, "%s" UI_SEP_CHAR_S "%s", butstr_orig, shortcut_str); - MEM_freeN(butstr_orig); - but->str = but->strdata; + but->str = fmt::format("{}" UI_SEP_CHAR_S "{}", but->str, shortcut_str); but->flag |= UI_BUT_HAS_SEP_CHAR; ui_but_update(but); } @@ -3138,27 +3116,7 @@ bool ui_but_string_eval_number(bContext *C, const uiBut *but, const char *str, d static void ui_but_string_set_internal(uiBut *but, const char *str, size_t str_len) { BLI_assert(str_len == strlen(str)); - BLI_assert(but->str == nullptr); - str_len += 1; - - if (str_len > UI_MAX_NAME_STR) { - but->str = static_cast(MEM_mallocN(str_len, "ui_def_but str")); - } - else { - but->str = but->strdata; - } - memcpy(but->str, str, str_len); -} - -static void ui_but_string_free_internal(uiBut *but) -{ - if (but->str) { - if (but->str != but->strdata) { - MEM_freeN(but->str); - } - /* must call 'ui_but_string_set_internal' after */ - but->str = nullptr; - } + but->str = std::string(str, str_len); } bool ui_but_string_set(bContext *C, uiBut *but, const char *str) @@ -3509,9 +3467,6 @@ static void ui_but_free(const bContext *C, uiBut *but) } } } - if (but->str && but->str != but->strdata) { - MEM_freeN(but->str); - } if ((but->type == UI_BTYPE_IMAGE) && but->poin) { IMB_freeImBuf((ImBuf *)but->poin); @@ -3736,7 +3691,7 @@ void UI_block_set_search_only(uiBlock *block, bool search_only) static void ui_but_build_drawstr_float(uiBut *but, double value) { size_t slen = 0; - STR_CONCAT(but->drawstr, slen, but->str); + STR_CONCAT(but->drawstr, slen, but->str.c_str()); PropertySubType subtype = PROP_NONE; if (but->rnaprop) { @@ -3784,7 +3739,7 @@ static void ui_but_build_drawstr_float(uiBut *but, double value) static void ui_but_build_drawstr_int(uiBut *but, int value) { size_t slen = 0; - STR_CONCAT(but->drawstr, slen, but->str); + STR_CONCAT(but->drawstr, slen, but->str.c_str()); PropertySubType subtype = PROP_NONE; if (but->rnaprop) { @@ -3878,13 +3833,13 @@ static void ui_but_update_ex(uiBut *but, const bool validate) &item)) { const size_t slen = strlen(item.name); - ui_but_string_free_internal(but); + but->str.clear(); ui_but_string_set_internal(but, item.name, slen); but->icon = item.icon; } } } - STRNCPY(but->drawstr, but->str); + STRNCPY(but->drawstr, but->str.c_str()); } break; @@ -3906,10 +3861,10 @@ static void ui_but_update_ex(uiBut *but, const bool validate) if (ui_but_is_float(but)) { UI_GET_BUT_VALUE_INIT(but, value); const int prec = ui_but_calc_float_precision(but, value); - SNPRINTF(but->drawstr, "%s%.*f", but->str, prec, value); + SNPRINTF(but->drawstr, "%s%.*f", but->str.c_str(), prec, value); } else { - STRNCPY(but->drawstr, but->str); + STRNCPY(but->drawstr, but->str.c_str()); } break; @@ -3920,7 +3875,7 @@ static void ui_but_update_ex(uiBut *but, const bool validate) char str[UI_MAX_DRAW_STR]; ui_but_string_get(but, str, UI_MAX_DRAW_STR); - SNPRINTF(but->drawstr, "%s%s", but->str, str); + SNPRINTF(but->drawstr, "%s%s", but->str.c_str(), str); } break; @@ -3933,7 +3888,7 @@ static void ui_but_update_ex(uiBut *but, const bool validate) UI_GET_BUT_VALUE_INIT(but, value); str = WM_key_event_string(short(value), false); } - SNPRINTF(but->drawstr, "%s%s", but->str, str); + SNPRINTF(but->drawstr, "%s%s", but->str.c_str(), str); break; } case UI_BTYPE_HOTKEY_EVENT: @@ -3954,7 +3909,7 @@ static void ui_but_update_ex(uiBut *but, const bool validate) } } else { - STRNCPY_UTF8(but->drawstr, but->str); + STRNCPY_UTF8(but->drawstr, but->str.c_str()); } break; @@ -3963,7 +3918,7 @@ static void ui_but_update_ex(uiBut *but, const bool validate) case UI_BTYPE_HSVCIRCLE: break; default: - STRNCPY(but->drawstr, but->str); + STRNCPY(but->drawstr, but->str.c_str()); break; } @@ -4082,7 +4037,6 @@ uiBut *ui_but_change_type(uiBut *but, eButType new_type) const uiBut *old_but_ptr = but; /* Button may have pointer to a member within itself, this will have to be updated. */ - const bool has_str_ptr_to_self = but->str == but->strdata; const bool has_poin_ptr_to_self = but->poin == (char *)but; /* Copy construct button with the new type. */ @@ -4090,9 +4044,6 @@ uiBut *ui_but_change_type(uiBut *but, eButType new_type) *but = *old_but_ptr; /* We didn't mean to override this :) */ but->type = new_type; - if (has_str_ptr_to_self) { - but->str = but->strdata; - } if (has_poin_ptr_to_self) { but->poin = (char *)but; } @@ -4201,18 +4152,16 @@ static uiBut *ui_def_but(uiBlock *block, but->pos = -1; /* cursor invisible */ if (ELEM(but->type, UI_BTYPE_NUM, UI_BTYPE_NUM_SLIDER)) { /* add a space to name */ - /* slen remains unchanged from previous assignment, ensure this stays true */ if (slen > 0 && slen < UI_MAX_NAME_STR - 2) { - if (but->str[slen - 1] != ' ') { - but->str[slen] = ' '; - but->str[slen + 1] = 0; + if (but->str[but->str.size() - 1] != ' ') { + but->str += ' '; } } } if (block->flag & UI_BLOCK_RADIAL) { but->drawflag |= UI_BUT_TEXT_LEFT; - if (but->str && but->str[0]) { + if (!but->str.empty()) { but->drawflag |= UI_BUT_ICON_LEFT; } } @@ -4296,7 +4245,7 @@ void ui_def_but_icon(uiBut *but, const int icon, const int flag) but->icon = icon; but->flag |= flag; - if (but->str && but->str[0]) { + if (!but->str.empty()) { but->drawflag |= UI_BUT_ICON_LEFT; } } @@ -6628,28 +6577,24 @@ void UI_but_string_info_get(bContext *C, uiBut *but, ...) va_start(args, but); while ((si = (uiStringInfo *)va_arg(args, void *))) { uiStringInfoType type = si->type; - char *tmp = nullptr; + std::string tmp; if (type == BUT_GET_TIP_LABEL) { if (but->tip_label_func) { - const std::string tooltip_label = but->tip_label_func(but); - tmp = BLI_strdupn(tooltip_label.c_str(), tooltip_label.size()); + tmp = but->tip_label_func(but); } } if (type == BUT_GET_LABEL) { - if (but->str && but->str[0]) { - const char *str_sep; - size_t str_len; - - if ((but->flag & UI_BUT_HAS_SEP_CHAR) && (str_sep = strrchr(but->str, UI_SEP_CHAR))) { - str_len = (str_sep - but->str); + if (!but->str.empty()) { + size_t str_len = but->str.size(); + if (but->flag & UI_BUT_HAS_SEP_CHAR) { + const size_t sep_index = but->str.find_first_of(UI_SEP_CHAR); + if (sep_index != std::string::npos) { + str_len = sep_index; + } } - else { - str_len = strlen(but->str); - } - - tmp = BLI_strdupn(but->str, str_len); + tmp = but->str.substr(0, str_len); } else { type = BUT_GET_RNA_LABEL; /* Fail-safe solution... */ @@ -6734,7 +6679,7 @@ void UI_but_string_info_get(bContext *C, uiBut *but, ...) } } - if (tmp == nullptr) { + if (tmp.empty()) { wmOperatorType *ot = UI_but_operatortype_get_from_enum_menu(but, nullptr); if (ot) { if (type == BUT_GET_RNA_LABEL) { @@ -6746,7 +6691,7 @@ void UI_but_string_info_get(bContext *C, uiBut *but, ...) } } - if (tmp == nullptr) { + if (tmp.empty()) { PanelType *pt = UI_but_paneltype_get(but); if (pt) { if (type == BUT_GET_RNA_LABEL) { @@ -6859,7 +6804,7 @@ void UI_but_string_info_get(bContext *C, uiBut *but, ...) } } - si->strinfo = tmp; + si->strinfo = BLI_strdupn(tmp.c_str(), tmp.size()); } va_end(args); diff --git a/source/blender/editors/interface/interface_intern.hh b/source/blender/editors/interface/interface_intern.hh index 37cd867baff..576c2c51ce1 100644 --- a/source/blender/editors/interface/interface_intern.hh +++ b/source/blender/editors/interface/interface_intern.hh @@ -178,8 +178,8 @@ struct uiBut { short bit = 0, bitnr = 0, retval = 0, strwidth = 0, alignnr = 0; short ofs = 0, pos = 0, selsta = 0, selend = 0; - char *str = nullptr; - char strdata[UI_MAX_NAME_STR] = ""; + std::string str; + char drawstr[UI_MAX_DRAW_STR] = ""; char *placeholder = nullptr; diff --git a/source/blender/editors/interface/interface_layout.cc b/source/blender/editors/interface/interface_layout.cc index 7f8b72e77a9..a1b8fb8f01e 100644 --- a/source/blender/editors/interface/interface_layout.cc +++ b/source/blender/editors/interface/interface_layout.cc @@ -2536,7 +2536,7 @@ void uiItemFullR(uiLayout *layout, /* ensure text isn't added to icon_only buttons */ if (but && icon_only) { - BLI_assert(but->str[0] == '\0'); + BLI_assert(but->str.empty()); } } @@ -2863,7 +2863,7 @@ uiBut *ui_but_add_search(uiBut *but, if (RNA_property_type(prop) == PROP_ENUM) { /* XXX, this will have a menu string, * but in this case we just want the text */ - but->str[0] = 0; + but->str.clear(); } UI_but_func_search_set_results_are_suggestions(but, results_are_suggestions); @@ -3602,7 +3602,7 @@ static int menu_item_enum_opname_menu_active(bContext *C, uiBut *but, MenuItemLe WM_operator_properties_sanitize(&ptr, false); PropertyRNA *prop = RNA_struct_find_property(&ptr, lvl->propname); RNA_property_enum_items_gettexted(C, &ptr, prop, &item_array, &totitem, &free); - int active = RNA_enum_from_name(item_array, but->str); + int active = RNA_enum_from_name(item_array, but->str.c_str()); if (free) { MEM_freeN((void *)item_array); } @@ -5405,7 +5405,7 @@ static bool block_search_panel_label_matches(const uiBlock *block, const char *s static bool button_matches_search_filter(uiBut *but, const char *search_filter) { /* Do the shorter checks first for better performance in case there is a match. */ - if (BLI_strcasestr(but->str, search_filter)) { + if (BLI_strcasestr(but->str.c_str(), search_filter)) { return true; } @@ -5891,7 +5891,7 @@ void ui_layout_add_but(uiLayout *layout, uiBut *but) ui_item_size((uiItem *)bitem, &w, &h); /* XXX uiBut hasn't scaled yet * we can flag the button as not expandable, depending on its size */ - if (w <= 2 * UI_UNIT_X && (!but->str || but->str[0] == '\0')) { + if (w <= 2 * UI_UNIT_X && but->str.empty()) { bitem->item.flag |= UI_ITEM_FIXED_SIZE; } @@ -6166,7 +6166,7 @@ static bool ui_layout_has_panel_label(const uiLayout *layout, const PanelType *p if (subitem->type == ITEM_BUTTON) { uiButtonItem *bitem = (uiButtonItem *)subitem; if (!(bitem->but->flag & UI_HIDDEN) && - STREQ(bitem->but->str, CTX_IFACE_(pt->translation_context, pt->label))) + STREQ(bitem->but->str.c_str(), CTX_IFACE_(pt->translation_context, pt->label))) { return true; } diff --git a/source/blender/editors/interface/interface_region_color_picker.cc b/source/blender/editors/interface/interface_region_color_picker.cc index 3075f05154f..670651adc5a 100644 --- a/source/blender/editors/interface/interface_region_color_picker.cc +++ b/source/blender/editors/interface/interface_region_color_picker.cc @@ -199,7 +199,7 @@ static void ui_update_color_picker_buts_rgb(uiBut *from_but, * push, so disable it on RNA buttons in the color picker block */ UI_but_flag_disable(bt, UI_BUT_UNDO); } - else if (STREQ(bt->str, "Hex:")) { + else if (bt->str == "Hex:") { float rgb_hex[3]; uchar rgb_hex_uchar[3]; char col[16]; diff --git a/source/blender/editors/interface/interface_region_menu_pie.cc b/source/blender/editors/interface/interface_region_menu_pie.cc index 0866cf8f62a..88a59114bc5 100644 --- a/source/blender/editors/interface/interface_region_menu_pie.cc +++ b/source/blender/editors/interface/interface_region_menu_pie.cc @@ -180,7 +180,7 @@ uiPieMenu *UI_pie_menu_begin(bContext *C, const char *title, int icon, const wmE } /* do not align left */ but->drawflag &= ~UI_BUT_TEXT_LEFT; - pie->block_radial->pie_data.title = but->str; + pie->block_radial->pie_data.title = but->str.c_str(); pie->block_radial->pie_data.icon = icon; } diff --git a/source/blender/editors/interface/interface_region_menu_popup.cc b/source/blender/editors/interface/interface_region_menu_popup.cc index 01be642743a..b13cc9ab88a 100644 --- a/source/blender/editors/interface/interface_region_menu_popup.cc +++ b/source/blender/editors/interface/interface_region_menu_popup.cc @@ -71,7 +71,7 @@ int ui_but_menu_step(uiBut *but, int direction) direction); } - printf("%s: cannot cycle button '%s'\n", __func__, but->str); + printf("%s: cannot cycle button '%s'\n", __func__, but->str.c_str()); return 0; } @@ -124,7 +124,7 @@ static uiBut *ui_popup_menu_memory__internal(uiBlock *block, uiBut *but) if (but) { /* set */ - mem[hash_mod] = ui_popup_string_hash(but->str, but->flag & UI_BUT_HAS_SEP_CHAR); + mem[hash_mod] = ui_popup_string_hash(but->str.c_str(), but->flag & UI_BUT_HAS_SEP_CHAR); return nullptr; } @@ -136,7 +136,8 @@ static uiBut *ui_popup_menu_memory__internal(uiBlock *block, uiBut *but) if (ELEM(but_iter->type, UI_BTYPE_LABEL, UI_BTYPE_SEPR, UI_BTYPE_SEPR_LINE)) { continue; } - if (mem[hash_mod] == ui_popup_string_hash(but_iter->str, but_iter->flag & UI_BUT_HAS_SEP_CHAR)) + if (mem[hash_mod] == + ui_popup_string_hash(but_iter->str.c_str(), but_iter->flag & UI_BUT_HAS_SEP_CHAR)) { return but_iter; } diff --git a/source/blender/editors/interface/interface_template_search_menu.cc b/source/blender/editors/interface/interface_template_search_menu.cc index c86af15437a..6569593871e 100644 --- a/source/blender/editors/interface/interface_template_search_menu.cc +++ b/source/blender/editors/interface/interface_template_search_menu.cc @@ -298,7 +298,6 @@ static bool menu_items_to_ui_button(MenuSearch_Item *item, uiBut *but) } but->icon = item->icon; - but->str = but->strdata; } return changed; diff --git a/source/blender/editors/interface/interface_widgets.cc b/source/blender/editors/interface/interface_widgets.cc index 67b09ea2b2c..4b0d22e5c05 100644 --- a/source/blender/editors/interface/interface_widgets.cc +++ b/source/blender/editors/interface/interface_widgets.cc @@ -1342,7 +1342,7 @@ static void widget_draw_icon( if (but->drawflag & UI_BUT_ICON_LEFT) { /* special case - icon_only pie buttons */ if (ui_block_is_pie_menu(but->block) && !ELEM(but->type, UI_BTYPE_MENU, UI_BTYPE_POPOVER) && - but->str && but->str[0] == '\0') + but->str.empty()) { xs = rect->xmin + 2.0f * ofs; } @@ -4960,7 +4960,8 @@ void ui_draw_but(const bContext *C, ARegion *region, uiStyle *style, uiBut *but, /* We could use a flag for this, but for now just check size, * add up/down arrows if there is room. */ - if ((!but->str[0] && but->icon && (BLI_rcti_size_x(rect) < BLI_rcti_size_y(rect) + 2)) || + if ((but->str.empty() && but->icon && + (BLI_rcti_size_x(rect) < BLI_rcti_size_y(rect) + 2)) || /* disable for brushes also */ (but->flag & UI_BUT_ICON_PREVIEW)) {