Cleanup: Store UI button drawstr with std::string

Similar to bff51ae66c
This commit is contained in:
Hans Goudey
2024-01-22 14:54:44 -05:00
parent 18af495cfc
commit 089c389b5c
12 changed files with 120 additions and 110 deletions

View File

@@ -378,7 +378,7 @@ static void ui_block_bounds_calc_text(uiBlock *block, float offset)
uiBut *init_col_bt = static_cast<uiBut *>(block->buttons.first);
LISTBASE_FOREACH (uiBut *, bt, &block->buttons) {
if (!ELEM(bt->type, UI_BTYPE_SEPR, UI_BTYPE_SEPR_LINE, UI_BTYPE_SEPR_SPACER)) {
j = BLF_width(style->widget.uifont_id, bt->drawstr, sizeof(bt->drawstr));
j = BLF_width(style->widget.uifont_id, bt->drawstr.c_str(), bt->drawstr.size());
if (j > i) {
i = j;
@@ -2519,7 +2519,7 @@ bool ui_but_is_rna_valid(uiBut *but)
if (but->rnaprop == nullptr || RNA_struct_contains_property(&but->rnapoin, but->rnaprop)) {
return true;
}
printf("property removed %s: %p\n", but->drawstr, but->rnaprop);
printf("property removed %s: %p\n", but->drawstr.c_str(), but->rnaprop);
return false;
}
@@ -3694,9 +3694,6 @@ 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.c_str());
PropertySubType subtype = PROP_NONE;
if (but->rnaprop) {
subtype = RNA_property_subtype(but->rnaprop);
@@ -3706,57 +3703,54 @@ static void ui_but_build_drawstr_float(uiBut *but, double value)
value += +0.0f;
if (value == double(FLT_MAX)) {
STR_CONCAT(but->drawstr, slen, "inf");
but->drawstr = but->str + "inf";
}
else if (value == double(-FLT_MAX)) {
STR_CONCAT(but->drawstr, slen, "-inf");
but->drawstr = but->str + "-inf";
}
else if (subtype == PROP_PERCENTAGE) {
const int prec = ui_but_calc_float_precision(but, value);
STR_CONCATF(but->drawstr, slen, "%.*f%%", prec, value);
but->drawstr = fmt::format("{}{:.{}f}%", but->str, value, prec);
}
else if (subtype == PROP_PIXEL) {
const int prec = ui_but_calc_float_precision(but, value);
STR_CONCATF(but->drawstr, slen, "%.*f px", prec, value);
but->drawstr = fmt::format("{}{:.{}f} px", but->str, value, prec);
}
else if (subtype == PROP_FACTOR) {
const int precision = ui_but_calc_float_precision(but, value);
if (U.factor_display_type == USER_FACTOR_AS_FACTOR) {
STR_CONCATF(but->drawstr, slen, "%.*f", precision, value);
but->drawstr += fmt::format("{}{:.{}f}", but->str, value, precision);
}
else {
STR_CONCATF(but->drawstr, slen, "%.*f%%", std::max(0, precision - 2), value * 100);
but->drawstr += fmt::format("{}{:.{}f}", but->str, value * 100, std::max(0, precision - 2));
}
}
else if (ui_but_is_unit(but)) {
char new_str[sizeof(but->drawstr)];
char new_str[UI_MAX_DRAW_STR];
ui_get_but_string_unit(but, new_str, sizeof(new_str), value, true, -1);
STR_CONCAT(but->drawstr, slen, new_str);
but->drawstr = but->str + new_str;
}
else {
const int prec = ui_but_calc_float_precision(but, value);
STR_CONCATF(but->drawstr, slen, "%.*f", prec, value);
but->drawstr = fmt::format("{}{:.{}f}", but->str, value, prec);
}
}
static void ui_but_build_drawstr_int(uiBut *but, int value)
{
size_t slen = 0;
STR_CONCAT(but->drawstr, slen, but->str.c_str());
PropertySubType subtype = PROP_NONE;
if (but->rnaprop) {
subtype = RNA_property_subtype(but->rnaprop);
}
STR_CONCATF(but->drawstr, slen, "%d", value);
but->drawstr = but->str + std::to_string(value);
if (subtype == PROP_PERCENTAGE) {
STR_CONCAT(but->drawstr, slen, "%");
but->drawstr += "%";
}
else if (subtype == PROP_PIXEL) {
STR_CONCAT(but->drawstr, slen, " px");
but->drawstr += " px";
}
}
@@ -3843,7 +3837,7 @@ static void ui_but_update_ex(uiBut *but, const bool validate)
}
}
}
STRNCPY(but->drawstr, but->str.c_str());
but->drawstr = but->str;
}
break;
@@ -3865,10 +3859,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.c_str(), prec, value);
but->drawstr = fmt::format("{}{:.{}f}", but->str, value, prec);
}
else {
STRNCPY(but->drawstr, but->str.c_str());
but->drawstr = but->str;
}
break;
@@ -3877,9 +3871,8 @@ static void ui_but_update_ex(uiBut *but, const bool validate)
case UI_BTYPE_SEARCH_MENU:
if (!but->editstr) {
char str[UI_MAX_DRAW_STR];
ui_but_string_get(but, str, UI_MAX_DRAW_STR);
SNPRINTF(but->drawstr, "%s%s", but->str.c_str(), str);
but->drawstr = fmt::format("{}{}", but->str, str);
}
break;
@@ -3892,7 +3885,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.c_str(), str);
but->drawstr = fmt::format("{}{}", but->str, str);
break;
}
case UI_BTYPE_HOTKEY_EVENT:
@@ -3906,14 +3899,17 @@ static void ui_but_update_ex(uiBut *but, const bool validate)
kmi_dummy.ctrl = (hotkey_but->modifier_key & KM_CTRL) ? KM_PRESS : KM_NOTHING;
kmi_dummy.alt = (hotkey_but->modifier_key & KM_ALT) ? KM_PRESS : KM_NOTHING;
kmi_dummy.oskey = (hotkey_but->modifier_key & KM_OSKEY) ? KM_PRESS : KM_NOTHING;
WM_keymap_item_to_string(&kmi_dummy, true, but->drawstr, sizeof(but->drawstr));
char kmi_str[128];
WM_keymap_item_to_string(&kmi_dummy, true, kmi_str, sizeof(kmi_str));
but->drawstr = kmi_str;
}
else {
STRNCPY_UTF8(but->drawstr, IFACE_("Press a key"));
but->drawstr = IFACE_("Press a key");
}
}
else {
STRNCPY_UTF8(but->drawstr, but->str.c_str());
but->drawstr = but->str;
}
break;
@@ -3922,13 +3918,13 @@ static void ui_but_update_ex(uiBut *but, const bool validate)
case UI_BTYPE_HSVCIRCLE:
break;
default:
STRNCPY(but->drawstr, but->str.c_str());
but->drawstr = but->str;
break;
}
/* if we are doing text editing, this will override the drawstr */
if (but->editstr) {
but->drawstr[0] = '\0';
but->drawstr.clear();
}
/* text clipping moved to widget drawing code itself */
@@ -6359,7 +6355,7 @@ void UI_but_func_search_set(uiBut *but,
* buttons where any result is valid anyway, since any string will be valid anyway. */
if (0 == (but->block->flag & UI_BLOCK_LOOP) && !search_but->results_are_suggestions) {
/* skip empty buttons, not all buttons need input, we only show invalid */
if (but->drawstr[0]) {
if (!but->drawstr.empty()) {
ui_but_search_refresh(search_but);
}
}

View File

@@ -375,8 +375,7 @@ static void ui_but_user_menu_add(bContext *C, uiBut *but, bUserMenu *um)
{
BLI_assert(ui_but_is_user_menu_compatible(C, but));
char drawstr[sizeof(but->drawstr)];
ui_but_drawstr_without_sep_char(but, drawstr, sizeof(drawstr));
std::string drawstr = ui_but_drawstr_without_sep_char(but);
/* Used for USER_MENU_TYPE_MENU. */
MenuType *mt = nullptr;
@@ -401,12 +400,12 @@ static void ui_but_user_menu_add(bContext *C, uiBut *but, bUserMenu *um)
idname);
char *expr_result = nullptr;
if (BPY_run_string_as_string(C, expr_imports, expr, nullptr, &expr_result)) {
STRNCPY(drawstr, expr_result);
drawstr = expr_result;
MEM_freeN(expr_result);
}
else {
BLI_assert(0);
STRNCPY(drawstr, idname);
drawstr = idname;
}
}
#else
@@ -416,7 +415,7 @@ static void ui_but_user_menu_add(bContext *C, uiBut *but, bUserMenu *um)
}
ED_screen_user_menu_item_add_operator(
&um->items,
drawstr,
drawstr.c_str(),
but->optype,
but->opptr ? static_cast<const IDProperty *>(but->opptr->data) : nullptr,
"",
@@ -434,7 +433,7 @@ static void ui_but_user_menu_add(bContext *C, uiBut *but, bUserMenu *um)
MEM_freeN(member_id_data_path);
}
else if ((mt = UI_but_menutype_get(but))) {
ED_screen_user_menu_item_add_menu(&um->items, drawstr, mt);
ED_screen_user_menu_item_add_menu(&um->items, drawstr.c_str(), mt);
}
else if ((ot = UI_but_operatortype_get_from_enum_menu(but, &prop))) {
ED_screen_user_menu_item_add_operator(&um->items,

View File

@@ -506,7 +506,7 @@ struct uiAfterFunc {
std::optional<bContextStore> context;
char undostr[BKE_UNDO_STR_MAX];
char drawstr[UI_MAX_DRAW_STR];
std::string drawstr;
};
static void button_activate_init(bContext *C,
@@ -794,7 +794,7 @@ static void ui_handle_afterfunc_add_operator_ex(wmOperatorType *ot,
}
if (context_but) {
ui_but_drawstr_without_sep_char(context_but, after->drawstr, sizeof(after->drawstr));
after->drawstr = ui_but_drawstr_without_sep_char(context_but);
}
}
@@ -909,7 +909,7 @@ static void ui_apply_but_func(bContext *C, uiBut *but)
after->context = *but->context;
}
ui_but_drawstr_without_sep_char(but, after->drawstr, sizeof(after->drawstr));
after->drawstr = ui_but_drawstr_without_sep_char(but);
but->optype = nullptr;
but->opcontext = wmOperatorCallContext(0);
@@ -929,11 +929,11 @@ static void ui_apply_but_undo(uiBut *but)
/* define which string to use for undo */
if (but->type == UI_BTYPE_MENU) {
str = but->drawstr;
str = but->drawstr.empty() ? nullptr : but->drawstr.c_str();
str_len_clip = ui_but_drawstr_len_without_sep_char(but);
}
else if (but->drawstr[0]) {
str = but->drawstr;
else if (!but->drawstr.empty()) {
str = but->drawstr.c_str();
str_len_clip = ui_but_drawstr_len_without_sep_char(but);
}
else {
@@ -1042,7 +1042,7 @@ static void ui_apply_but_funcs_after(bContext *C)
after.opcontext,
(after.opptr) ? &opptr : nullptr,
nullptr,
after.drawstr);
after.drawstr.c_str());
}
if (after.opptr) {
@@ -2946,7 +2946,7 @@ void ui_but_clipboard_free()
static int ui_text_position_from_hidden(uiBut *but, int pos)
{
const char *butstr = (but->editstr) ? but->editstr : but->drawstr;
const char *butstr = (but->editstr) ? but->editstr : but->drawstr.c_str();
const char *strpos = butstr;
const char *str_end = butstr + strlen(butstr);
for (int i = 0; i < pos; i++) {
@@ -2958,7 +2958,7 @@ static int ui_text_position_from_hidden(uiBut *but, int pos)
static int ui_text_position_to_hidden(uiBut *but, int pos)
{
const char *butstr = (but->editstr) ? but->editstr : but->drawstr;
const char *butstr = (but->editstr) ? but->editstr : but->drawstr.c_str();
return BLI_strnlen_utf8(butstr, pos);
}
@@ -2970,7 +2970,7 @@ void ui_but_text_password_hide(char password_str[UI_MAX_PASSWORD_STR],
return;
}
char *butstr = (but->editstr) ? but->editstr : but->drawstr;
char *butstr = (but->editstr) ? but->editstr : but->drawstr.data();
if (restore) {
/* restore original string */
@@ -4616,7 +4616,7 @@ static int ui_do_but_HOTKEYEVT(bContext *C,
if (ELEM(event->type, LEFTMOUSE, EVT_PADENTER, EVT_RETKEY, EVT_BUT_OPEN) &&
(event->val == KM_PRESS))
{
but->drawstr[0] = 0;
but->drawstr.clear();
hotkey_but->modifier_key = 0;
button_activate_state(C, but, BUTTON_STATE_WAIT_KEY_EVENT);
return WM_UI_HANDLER_BREAK;

View File

@@ -13,6 +13,7 @@
#include "BLI_compiler_attrs.h"
#include "BLI_math_vector_types.hh"
#include "BLI_rect.h"
#include "BLI_string_ref.hh"
#include "BLI_vector.hh"
#include "DNA_listBase.h"
@@ -180,7 +181,7 @@ struct uiBut {
std::string str;
char drawstr[UI_MAX_DRAW_STR] = "";
std::string drawstr;
char *placeholder = nullptr;
@@ -1254,7 +1255,7 @@ void ui_draw_preview_item(const uiFontStyle *fstyle,
*/
void ui_draw_preview_item_stateless(const uiFontStyle *fstyle,
rcti *rect,
const char *name,
blender::StringRef name,
int iconid,
const uchar text_col[4],
eFontStyle_Align text_align,
@@ -1428,8 +1429,7 @@ uiBut *ui_list_find_mouse_over_ex(const ARegion *region, const int xy[2])
bool ui_but_contains_password(const uiBut *but) ATTR_WARN_UNUSED_RESULT;
size_t ui_but_drawstr_without_sep_char(const uiBut *but, char *str, size_t str_maxncpy)
ATTR_NONNULL(1, 2);
blender::StringRef ui_but_drawstr_without_sep_char(const uiBut *but) ATTR_NONNULL();
size_t ui_but_drawstr_len_without_sep_char(const uiBut *but);
size_t ui_but_tip_len_only_first_line(const uiBut *but);

View File

@@ -1312,7 +1312,7 @@ static void ui_item_menu_hold(bContext *C, ARegion *butregion, uiBut *but)
block->flag |= UI_BLOCK_POPUP_HOLD;
char direction = UI_DIR_DOWN;
if (!but->drawstr[0]) {
if (but->drawstr.empty()) {
switch (RGN_ALIGN_ENUM_FROM_MASK(butregion->alignment)) {
case RGN_ALIGN_LEFT:
direction = UI_DIR_RIGHT;
@@ -6265,7 +6265,7 @@ static void ui_layout_introspect_button(DynStr *ds, uiButtonItem *bitem)
{
uiBut *but = bitem->but;
BLI_dynstr_appendf(ds, "'type':%d, ", int(but->type));
BLI_dynstr_appendf(ds, "'draw_string':'''%s''', ", but->drawstr);
BLI_dynstr_appendf(ds, "'draw_string':'''%s''', ", but->drawstr.c_str());
/* Not exactly needed, rna has this. */
BLI_dynstr_appendf(ds, "'tip':'''%s''', ", but->tip ? but->tip : "");

View File

@@ -1801,8 +1801,7 @@ static bool ui_editsource_uibut_match(uiBut *but_a, uiBut *but_b)
* if this fails it only means edit-source fails - campbell */
if (BLI_rctf_compare(&but_a->rect, &but_b->rect, FLT_EPSILON) && (but_a->type == but_b->type) &&
(but_a->rnaprop == but_b->rnaprop) && (but_a->optype == but_b->optype) &&
(but_a->unit_type == but_b->unit_type) &&
STREQLEN(but_a->drawstr, but_b->drawstr, UI_MAX_DRAW_STR))
(but_a->unit_type == but_b->unit_type) && but_a->drawstr == but_b->drawstr)
{
return true;
}

View File

@@ -590,18 +590,18 @@ bool ui_but_contains_password(const uiBut *but)
size_t ui_but_drawstr_len_without_sep_char(const uiBut *but)
{
if (but->flag & UI_BUT_HAS_SEP_CHAR) {
const char *str_sep = strrchr(but->drawstr, UI_SEP_CHAR);
if (str_sep != nullptr) {
return (str_sep - but->drawstr);
const size_t sep_index = but->drawstr.find(UI_SEP_CHAR);
if (sep_index != std::string::npos) {
return sep_index;
}
}
return strlen(but->drawstr);
return but->drawstr.size();
}
size_t ui_but_drawstr_without_sep_char(const uiBut *but, char *str, size_t str_maxncpy)
blender::StringRef ui_but_drawstr_without_sep_char(const uiBut *but)
{
size_t str_len_clip = ui_but_drawstr_len_without_sep_char(but);
return BLI_strncpy_rlen(str, but->drawstr, min_zz(str_len_clip + 1, str_maxncpy));
return blender::StringRef(but->drawstr).substr(0, str_len_clip);
}
size_t ui_but_tip_len_only_first_line(const uiBut *but)

View File

@@ -247,7 +247,7 @@ static uiBlock *ui_block_func_POPUP(bContext *C, uiPopupBlockHandle *handle, voi
}
else if (pup->but) {
/* Minimum width to enforce. */
if (pup->but->drawstr[0]) {
if (!pup->but->drawstr.empty()) {
minwidth = BLI_rctf_size_x(&pup->but->rect);
}
else {

View File

@@ -1098,7 +1098,7 @@ void ui_but_search_refresh(uiButSearch *but)
items->names[i] = (char *)MEM_callocN(but->hardmax + 1, __func__);
}
ui_searchbox_update_fn((bContext *)but->block->evil_C, but, but->drawstr, items);
ui_searchbox_update_fn((bContext *)but->block->evil_C, but, but->drawstr.c_str(), items);
if (!but->results_are_suggestions) {
/* Only red-alert when we are sure of it, this can miss cases when >10 matches. */
@@ -1106,7 +1106,7 @@ void ui_but_search_refresh(uiButSearch *but)
UI_but_flag_enable(but, UI_BUT_REDALERT);
}
else if (items->more == 0) {
if (UI_search_items_find_index(items, but->drawstr) == -1) {
if (UI_search_items_find_index(items, but->drawstr.c_str()) == -1) {
UI_but_flag_enable(but, UI_BUT_REDALERT);
}
}

View File

@@ -467,7 +467,7 @@ static uiTooltipData *ui_tooltip_data_from_tool(bContext *C, uiBut *but, bool is
* doesn't have access to information about non-active tools. */
/* Title (when icon-only). */
if (but->drawstr[0] == '\0') {
if (but->drawstr.empty()) {
const char *expr_imports[] = {"bpy", "bl_ui", nullptr};
char expr[256];
SNPRINTF(expr,
@@ -866,7 +866,9 @@ static uiTooltipData *ui_tooltip_data_from_button_or_extra_icon(bContext *C,
* prefix instead of comparing because the button may include the shortcut. Buttons with dynamic
* tool-tips also don't get their default label here since they can already provide more accurate
* and specific tool-tip content. */
else if (!but_label.empty() && !STRPREFIX(but->drawstr, but_label.c_str()) && !but->tip_func) {
else if (!but_label.empty() && !blender::StringRef(but->drawstr).startswith(but_label) &&
!but->tip_func)
{
UI_tooltip_text_field_add(data, but_label, {}, UI_TIP_STYLE_HEADER, UI_TIP_LC_NORMAL);
}

View File

@@ -163,14 +163,14 @@ static bool menu_items_from_ui_create_item_from_button(MenuSearch_Data *data,
MenuSearch_Context *wm_context,
MenuSearch_Parent *menu_parent)
{
using namespace blender;
MenuSearch_Item *item = nullptr;
/* Use override if the name is empty, this can happen with popovers. */
std::string drawstr_override;
const char *drawstr_sep = (but->flag & UI_BUT_HAS_SEP_CHAR) ?
strrchr(but->drawstr, UI_SEP_CHAR) :
nullptr;
const bool drawstr_is_empty = (drawstr_sep == but->drawstr) || (but->drawstr[0] == '\0');
const size_t sep_index = (but->flag & UI_BUT_HAS_SEP_CHAR) ? but->drawstr.find(UI_SEP_CHAR) :
std::string::npos;
const bool drawstr_is_empty = sep_index == 0 || but->drawstr.empty();
if (but->optype != nullptr) {
if (drawstr_is_empty) {
@@ -215,7 +215,7 @@ static bool menu_items_from_ui_create_item_from_button(MenuSearch_Data *data,
/* Note that these buttons are not prevented,
* but aren't typically used in menus. */
printf("Button '%s' in menu '%s' is a menu item with unsupported RNA type %d\n",
but->drawstr,
but->drawstr.c_str(),
mt->idname,
prop_type);
}
@@ -236,12 +236,14 @@ static bool menu_items_from_ui_create_item_from_button(MenuSearch_Data *data,
if (item != nullptr) {
/* Handle shared settings. */
if (!drawstr_override.empty()) {
const char *drawstr_suffix = drawstr_sep ? drawstr_sep : "";
const StringRef drawstr_suffix = sep_index == std::string::npos ?
"" :
StringRef(but->drawstr).drop_prefix(sep_index);
std::string drawstr = std::string("(") + drawstr_override + ")" + drawstr_suffix;
item->drawstr = strdup_memarena(memarena, drawstr.c_str());
}
else {
item->drawstr = strdup_memarena(memarena, but->drawstr);
item->drawstr = strdup_memarena(memarena, but->drawstr.c_str());
}
item->icon = ui_but_icon(but);
@@ -290,11 +292,11 @@ static bool menu_items_to_ui_button(MenuSearch_Item *item, uiBut *but)
}
if (changed) {
STRNCPY(but->drawstr, item->drawstr);
char *drawstr_sep = (item->state & UI_BUT_HAS_SEP_CHAR) ? strrchr(but->drawstr, UI_SEP_CHAR) :
nullptr;
if (drawstr_sep) {
*drawstr_sep = '\0';
but->drawstr = item->drawstr;
const size_t sep_index = but->drawstr.find(UI_SEP_CHAR);
if (sep_index != std::string::npos) {
but->drawstr.resize(sep_index);
}
but->icon = item->icon;
@@ -707,7 +709,7 @@ static MenuSearch_Data *menu_items_from_ui_create(bContext *C,
}
if (but_test == nullptr) {
menu_display_name_map.add(mt, strdup_memarena(memarena, but->drawstr));
menu_display_name_map.add(mt, strdup_memarena(memarena, but->drawstr.c_str()));
}
}
else if (menu_items_from_ui_create_item_from_button(
@@ -730,14 +732,14 @@ static MenuSearch_Data *menu_items_from_ui_create(bContext *C,
* we only want to do that for the last menu item, not the path that leads to it.
*/
const char *drawstr_sep = but->flag & UI_BUT_HAS_SEP_CHAR ?
strrchr(but->drawstr, UI_SEP_CHAR) :
strrchr(but->drawstr.c_str(), UI_SEP_CHAR) :
nullptr;
bool drawstr_is_empty = false;
if (drawstr_sep != nullptr) {
BLI_assert(BLI_dynstr_get_len(dyn_str) == 0);
/* Detect empty string, fallback to menu name. */
const char *drawstr = but->drawstr;
int drawstr_len = drawstr_sep - but->drawstr;
const char *drawstr = but->drawstr.c_str();
int drawstr_len = drawstr_sep - but->drawstr.c_str();
if (UNLIKELY(drawstr_len == 0)) {
drawstr = CTX_IFACE_(mt_from_but->translation_context, mt_from_but->label);
drawstr_len = strlen(drawstr);
@@ -751,7 +753,7 @@ static MenuSearch_Data *menu_items_from_ui_create(bContext *C,
BLI_dynstr_clear(dyn_str);
}
else {
const char *drawstr = but->drawstr;
const char *drawstr = but->drawstr.c_str();
if (UNLIKELY(drawstr[0] == '\0')) {
drawstr = CTX_IFACE_(mt_from_but->translation_context, mt_from_but->label);
if (drawstr[0] == '\0') {
@@ -792,7 +794,7 @@ static MenuSearch_Data *menu_items_from_ui_create(bContext *C,
MenuSearch_Parent *menu_parent = (MenuSearch_Parent *)BLI_memarena_calloc(
memarena, sizeof(*menu_parent));
menu_parent->drawstr = strdup_memarena(memarena, but->drawstr);
menu_parent->drawstr = strdup_memarena(memarena, but->drawstr.c_str());
menu_parent->parent = current_menu.self_as_parent;
LISTBASE_FOREACH (uiBut *, sub_but, &sub_block->buttons) {

View File

@@ -1599,11 +1599,14 @@ static void ui_text_clip_middle(const uiFontStyle *fstyle, uiBut *but, const rct
0 :
int(UI_TEXT_CLIP_MARGIN + 0.5f);
const float okwidth = float(max_ii(BLI_rcti_size_x(rect) - border, 0));
const size_t max_len = sizeof(but->drawstr);
const float minwidth = float(UI_ICON_SIZE) / but->block->aspect * 2.0f;
but->ofs = 0;
but->strwidth = UI_text_clip_middle_ex(fstyle, but->drawstr, okwidth, minwidth, max_len, '\0');
char new_drawstr[UI_MAX_DRAW_STR];
STRNCPY(new_drawstr, but->drawstr.c_str());
const size_t max_len = sizeof(new_drawstr);
but->strwidth = UI_text_clip_middle_ex(fstyle, new_drawstr, okwidth, minwidth, max_len, '\0');
but->drawstr = new_drawstr;
}
/**
@@ -1622,11 +1625,14 @@ static void ui_text_clip_middle_protect_right(const uiFontStyle *fstyle,
0 :
int(UI_TEXT_CLIP_MARGIN + 0.5f);
const float okwidth = float(max_ii(BLI_rcti_size_x(rect) - border, 0));
const size_t max_len = sizeof(but->drawstr);
const float minwidth = float(UI_ICON_SIZE) / but->block->aspect * 2.0f;
but->ofs = 0;
but->strwidth = UI_text_clip_middle_ex(fstyle, but->drawstr, okwidth, minwidth, max_len, rsep);
char new_drawstr[UI_MAX_DRAW_STR];
STRNCPY(new_drawstr, but->drawstr.c_str());
const size_t max_len = sizeof(new_drawstr);
but->strwidth = UI_text_clip_middle_ex(fstyle, new_drawstr, okwidth, minwidth, max_len, rsep);
but->drawstr = new_drawstr;
}
/**
@@ -1694,13 +1700,17 @@ static void ui_text_clip_right_label(const uiFontStyle *fstyle, uiBut *but, cons
{
const int border = UI_TEXT_CLIP_MARGIN + 1;
const int okwidth = max_ii(BLI_rcti_size_x(rect) - border, 0);
int drawstr_len = strlen(but->drawstr);
const char *cpend = but->drawstr + drawstr_len;
int drawstr_len = but->drawstr.size();
char new_drawstr[UI_MAX_DRAW_STR];
STRNCPY(new_drawstr, but->drawstr.c_str());
const char *cpend = new_drawstr + drawstr_len;
/* need to set this first */
UI_fontstyle_set(fstyle);
but->strwidth = BLF_width(fstyle->uifont_id, but->drawstr, sizeof(but->drawstr));
but->strwidth = BLF_width(fstyle->uifont_id, new_drawstr, drawstr_len);
/* The string already fits, so do nothing. */
if (but->strwidth <= okwidth) {
@@ -1723,14 +1733,14 @@ static void ui_text_clip_right_label(const uiFontStyle *fstyle, uiBut *but, cons
*/
/* find the space after ':' separator */
char *cpoin = strrchr(but->drawstr, ':');
char *cpoin = strrchr(new_drawstr, ':');
if (cpoin && (cpoin < cpend - 2)) {
char *cp2 = cpoin;
/* chop off the leading text, starting from the right */
while (but->strwidth > okwidth && cp2 > but->drawstr) {
const char *prev_utf8 = BLI_str_find_prev_char_utf8(cp2, but->drawstr);
while (but->strwidth > okwidth && cp2 > new_drawstr) {
const char *prev_utf8 = BLI_str_find_prev_char_utf8(cp2, new_drawstr);
const int bytes = cp2 - prev_utf8;
/* shift the text after and including cp2 back by 1 char,
@@ -1739,11 +1749,10 @@ static void ui_text_clip_right_label(const uiFontStyle *fstyle, uiBut *but, cons
cp2 -= bytes;
drawstr_len -= bytes;
// BLI_assert(strlen(but->drawstr) == drawstr_len);
but->strwidth = BLF_width(fstyle->uifont_id,
but->drawstr + but->ofs,
sizeof(but->drawstr) - but->ofs) +
new_drawstr + but->ofs,
sizeof(new_drawstr) - but->ofs) +
sep_strwidth;
if (but->strwidth < sep_strwidth) {
break;
@@ -1752,9 +1761,9 @@ static void ui_text_clip_right_label(const uiFontStyle *fstyle, uiBut *but, cons
/* after the leading text is gone, chop off the : and following space, with ofs */
while ((but->strwidth > okwidth) && (but->ofs < 2)) {
ui_text_clip_give_next_off(but, but->drawstr, but->drawstr + drawstr_len);
ui_text_clip_give_next_off(but, new_drawstr, new_drawstr + drawstr_len);
but->strwidth = BLF_width(
fstyle->uifont_id, but->drawstr + but->ofs, sizeof(but->drawstr) - but->ofs);
fstyle->uifont_id, new_drawstr + but->ofs, sizeof(new_drawstr) - but->ofs);
if (but->strwidth < 10) {
break;
}
@@ -1766,23 +1775,25 @@ static void ui_text_clip_right_label(const uiFontStyle *fstyle, uiBut *but, cons
if (but->strwidth > okwidth) {
float strwidth;
drawstr_len = BLF_width_to_strlen(fstyle->uifont_id,
but->drawstr + but->ofs,
new_drawstr + but->ofs,
drawstr_len - but->ofs,
okwidth,
&strwidth) +
but->ofs;
but->strwidth = strwidth;
but->drawstr[drawstr_len] = 0;
new_drawstr[drawstr_len] = 0;
}
cpoin = strrchr(but->drawstr, ':');
if (cpoin && (cpoin - but->drawstr > 0) && (drawstr_len < (sizeof(but->drawstr) - sep_len))) {
cpoin = strrchr(new_drawstr, ':');
if (cpoin && (cpoin - new_drawstr > 0) && (drawstr_len < (sizeof(new_drawstr) - sep_len))) {
/* We shortened the string and still have a colon, so insert ellipsis. */
memmove(cpoin + sep_len, cpoin, cpend - cpoin);
memcpy(cpoin, sep, sep_len);
but->strwidth = BLF_width(
fstyle->uifont_id, but->drawstr + but->ofs, sizeof(but->drawstr) - but->ofs);
fstyle->uifont_id, new_drawstr + but->ofs, sizeof(new_drawstr) - but->ofs);
}
but->drawstr = new_drawstr;
}
#ifdef WITH_INPUT_IME
@@ -1846,7 +1857,7 @@ static void widget_draw_text(const uiFontStyle *fstyle,
rcti *rect)
{
int drawstr_left_len = UI_MAX_DRAW_STR;
const char *drawstr = but->drawstr;
const char *drawstr = but->drawstr.c_str();
const char *drawstr_right = nullptr;
bool use_right_only = false;
const char *indeterminate_str = UI_VALUE_INDETERMINATE_CHAR;
@@ -5636,7 +5647,7 @@ void ui_draw_menu_item(const uiFontStyle *fstyle,
void ui_draw_preview_item_stateless(const uiFontStyle *fstyle,
rcti *rect,
const char *name,
const blender::StringRef name,
int iconid,
const uchar text_col[4],
eFontStyle_Align text_align,
@@ -5644,7 +5655,7 @@ void ui_draw_preview_item_stateless(const uiFontStyle *fstyle,
{
rcti trect = *rect;
const float text_size = UI_UNIT_Y;
const bool has_text = name && name[0];
const bool has_text = !name.is_empty();
float alpha = 1.0f;
@@ -5687,7 +5698,8 @@ void ui_draw_preview_item_stateless(const uiFontStyle *fstyle,
const size_t max_len = sizeof(drawstr);
const float minwidth = float(UI_ICON_SIZE);
STRNCPY(drawstr, name);
memcpy(drawstr, name.data(), name.size());
drawstr[name.size()] = '\0';
UI_text_clip_middle_ex(fstyle, drawstr, okwidth, minwidth, max_len, '\0');
uiFontStyleDraw_Params params{};