diff --git a/source/blender/editors/include/UI_interface_c.hh b/source/blender/editors/include/UI_interface_c.hh index 523ba4d58ce..238a1adf610 100644 --- a/source/blender/editors/include/UI_interface_c.hh +++ b/source/blender/editors/include/UI_interface_c.hh @@ -1809,11 +1809,11 @@ void UI_but_func_tooltip_custom_set(uiBut *but, * \param suffix: Allocated text (transfer ownership to `data`) or null. */ void UI_tooltip_text_field_add(uiTooltipData *data, - char *text, - char *suffix, + std::string text, + std::string suffix, const uiTooltipStyle style, const uiTooltipColorID color_id, - const bool is_pad = false) ATTR_NONNULL(1); + const bool is_pad = false); /** * \param image: Image buffer (duplicated, ownership is *not* transferred to `data`). diff --git a/source/blender/editors/interface/interface_region_tooltip.cc b/source/blender/editors/interface/interface_region_tooltip.cc index 24f9f61e1b2..78d4abc31be 100644 --- a/source/blender/editors/interface/interface_region_tooltip.cc +++ b/source/blender/editors/interface/interface_region_tooltip.cc @@ -21,6 +21,8 @@ #include #include +#include + #include "MEM_guardedalloc.h" #include "DNA_brush_types.h" @@ -79,10 +81,8 @@ struct uiTooltipFormat { }; struct uiTooltipField { - /** Allocated text (owned by this structure), may be null. */ - const char *text; - /** Allocated text (owned by this structure), may be null. */ - const char *text_suffix; + std::string text; + std::string text_suffix; struct { /** X cursor position at the end of the last line. */ uint x_pos; @@ -96,8 +96,7 @@ struct uiTooltipField { struct uiTooltipData { rcti bbox; - uiTooltipField *fields; - uint fields_len; + blender::Vector fields; uiFontStyle fstyle; int wrap_width; int toth, lineh; @@ -105,44 +104,34 @@ struct uiTooltipData { BLI_STATIC_ASSERT(int(UI_TIP_LC_MAX) == int(UI_TIP_LC_ALERT) + 1, "invalid lc-max"); -static uiTooltipField *text_field_add_only(uiTooltipData *data) -{ - data->fields_len += 1; - data->fields = static_cast( - MEM_recallocN(data->fields, sizeof(*data->fields) * data->fields_len)); - return &data->fields[data->fields_len - 1]; -} - void UI_tooltip_text_field_add(uiTooltipData *data, - char *text, - char *suffix, + std::string text, + std::string suffix, const uiTooltipStyle style, const uiTooltipColorID color_id, const bool is_pad) { if (is_pad) { /* Add a spacer field before this one. */ - UI_tooltip_text_field_add( - data, nullptr, nullptr, UI_TIP_STYLE_SPACER, UI_TIP_LC_NORMAL, false); + UI_tooltip_text_field_add(data, {}, {}, UI_TIP_STYLE_SPACER, UI_TIP_LC_NORMAL, false); } - uiTooltipField *field = text_field_add_only(data); - field->format = {}; - field->format.style = style; - field->format.color_id = color_id; - field->text = text; - field->text_suffix = suffix; + uiTooltipField field{}; + field.format.style = style; + field.format.color_id = color_id; + field.text = std::move(text); + field.text_suffix = std::move(suffix); + data->fields.append(std::move(field)); } void UI_tooltip_image_field_add(uiTooltipData *data, const ImBuf *image, const short image_size[2]) { - uiTooltipField *field = text_field_add_only(data); - field->format = {}; - field->format.style = UI_TIP_STYLE_IMAGE; - field->image = IMB_dupImBuf(image); - field->image_size[0] = MIN2(image_size[0], UI_TIP_MAXIMAGEWIDTH * UI_SCALE_FAC); - field->image_size[1] = MIN2(image_size[1], UI_TIP_MAXIMAGEHEIGHT * UI_SCALE_FAC); - field->text = nullptr; + uiTooltipField field{}; + field.format.style = UI_TIP_STYLE_IMAGE; + field.image = IMB_dupImBuf(image); + field.image_size[0] = std::min(image_size[0], short(UI_TIP_MAXIMAGEWIDTH * UI_SCALE_FAC)); + field.image_size[1] = std::min(image_size[1], short(UI_TIP_MAXIMAGEHEIGHT * UI_SCALE_FAC)); + data->fields.append(std::move(field)); } /* -------------------------------------------------------------------- */ @@ -217,7 +206,7 @@ static void ui_tooltip_region_draw_cb(const bContext * /*C*/, ARegion *region) bbox.xmin += 0.5f * pad_px; /* add padding to the text */ bbox.ymax -= 0.25f * pad_px; - for (int i = 0; i < data->fields_len; i++) { + for (int i = 0; i < data->fields.size(); i++) { const uiTooltipField *field = &data->fields[i]; bbox.ymin = bbox.ymax - (data->lineh * field->geom.lines); @@ -229,18 +218,23 @@ static void ui_tooltip_region_draw_cb(const bContext * /*C*/, ARegion *region) /* Draw header and active data (is done here to be able to change color). */ rgb_float_to_uchar(drawcol, tip_colors[UI_TIP_LC_MAIN]); UI_fontstyle_set(&data->fstyle); - UI_fontstyle_draw(&data->fstyle, &bbox, field->text, UI_TIP_STR_MAX, drawcol, &fs_params); + UI_fontstyle_draw( + &data->fstyle, &bbox, field->text.c_str(), field->text.size(), drawcol, &fs_params); /* Offset to the end of the last line. */ - if (field->text_suffix) { + if (!field->text_suffix.empty()) { const float xofs = field->geom.x_pos; const float yofs = data->lineh * (field->geom.lines - 1); bbox.xmin += xofs; bbox.ymax -= yofs; rgb_float_to_uchar(drawcol, tip_colors[UI_TIP_LC_ACTIVE]); - UI_fontstyle_draw( - &data->fstyle, &bbox, field->text_suffix, UI_TIP_STR_MAX, drawcol, &fs_params); + UI_fontstyle_draw(&data->fstyle, + &bbox, + field->text_suffix.c_str(), + field->text_suffix.size(), + drawcol, + &fs_params); /* Undo offset. */ bbox.xmin -= xofs; @@ -258,7 +252,8 @@ static void ui_tooltip_region_draw_cb(const bContext * /*C*/, ARegion *region) /* XXX: needed because we don't have mono in 'U.uifonts'. */ BLF_size(fstyle_mono.uifont_id, fstyle_mono.points * UI_SCALE_FAC); rgb_float_to_uchar(drawcol, tip_colors[int(field->format.color_id)]); - UI_fontstyle_draw(&fstyle_mono, &bbox, field->text, UI_TIP_STR_MAX, drawcol, &fs_params); + UI_fontstyle_draw( + &fstyle_mono, &bbox, field->text.c_str(), field->text.size(), drawcol, &fs_params); } else if (field->format.style == UI_TIP_STYLE_IMAGE) { @@ -322,7 +317,8 @@ static void ui_tooltip_region_draw_cb(const bContext * /*C*/, ARegion *region) /* Draw remaining data. */ rgb_float_to_uchar(drawcol, tip_colors[int(field->format.color_id)]); UI_fontstyle_set(&data->fstyle); - UI_fontstyle_draw(&data->fstyle, &bbox, field->text, UI_TIP_STR_MAX, drawcol, &fs_params); + UI_fontstyle_draw( + &data->fstyle, &bbox, field->text.c_str(), field->text.size(), drawcol, &fs_params); } bbox.ymax -= data->lineh * field->geom.lines; @@ -335,21 +331,12 @@ static void ui_tooltip_region_draw_cb(const bContext * /*C*/, ARegion *region) static void ui_tooltip_region_free_cb(ARegion *region) { uiTooltipData *data = static_cast(region->regiondata); - - for (int i = 0; i < data->fields_len; i++) { - const uiTooltipField *field = &data->fields[i]; - if (field->text) { - MEM_freeN((void *)field->text); - } - if (field->text_suffix) { - MEM_freeN((void *)field->text_suffix); - } - if (field->image) { - IMB_freeImBuf(field->image); + for (uiTooltipField &field : data->fields) { + if (field.image) { + IMB_freeImBuf(field.image); } } - MEM_freeN(data->fields); - MEM_freeN(data); + MEM_delete(data); region->regiondata = nullptr; } @@ -379,7 +366,7 @@ static char *ui_tooltip_text_python_from_op(bContext *C, wmOperatorType *ot, Poi static bool ui_tooltip_data_append_from_keymap(bContext *C, uiTooltipData *data, wmKeyMap *keymap) { - const int fields_len_init = data->fields_len; + const int fields_len_init = data->fields.size(); char buf[512]; LISTBASE_FOREACH (wmKeyMapItem *, kmi, &keymap->items) { @@ -388,8 +375,8 @@ static bool ui_tooltip_data_append_from_keymap(bContext *C, uiTooltipData *data, /* Tip. */ { UI_tooltip_text_field_add(data, - BLI_strdup(ot->description ? ot->description : ot->name), - nullptr, + ot->description ? ot->description : ot->name, + {}, UI_TIP_STYLE_NORMAL, UI_TIP_LC_MAIN, true); @@ -401,8 +388,8 @@ static bool ui_tooltip_data_append_from_keymap(bContext *C, uiTooltipData *data, found = true; } UI_tooltip_text_field_add(data, - BLI_sprintfN(TIP_("Shortcut: %s"), found ? buf : "None"), - nullptr, + fmt::format(TIP_("Shortcut: {}"), found ? buf : "None"), + {}, UI_TIP_STYLE_NORMAL, UI_TIP_LC_NORMAL); } @@ -410,17 +397,14 @@ static bool ui_tooltip_data_append_from_keymap(bContext *C, uiTooltipData *data, /* Python. */ if (U.flag & USER_TOOLTIPS_PYTHON) { char *str = ui_tooltip_text_python_from_op(C, ot, kmi->ptr); - UI_tooltip_text_field_add(data, - BLI_sprintfN(TIP_("Python: %s"), str), - nullptr, - UI_TIP_STYLE_NORMAL, - UI_TIP_LC_PYTHON); + UI_tooltip_text_field_add( + data, fmt::format(TIP_("Python: {}"), str), {}, UI_TIP_STYLE_NORMAL, UI_TIP_LC_PYTHON); MEM_freeN(str); } } } - return (fields_len_init != data->fields_len); + return (fields_len_init != data->fields.size()); } #endif /* WITH_PYTHON */ @@ -475,7 +459,7 @@ static uiTooltipData *ui_tooltip_data_from_tool(bContext *C, uiBut *but, bool is } /* We have a tool, now extract the info. */ - uiTooltipData *data = MEM_cnew(__func__); + uiTooltipData *data = MEM_new(__func__); #ifdef WITH_PYTHON /* It turns out to be most simple to do this via Python since C @@ -525,10 +509,11 @@ static uiTooltipData *ui_tooltip_data_from_tool(bContext *C, uiBut *but, bool is UI_tooltip_text_field_add(data, expr_result, - nullptr, + {}, UI_TIP_STYLE_NORMAL, (is_error) ? UI_TIP_LC_ALERT : UI_TIP_LC_MAIN, true); + MEM_freeN(expr_result); } } @@ -565,10 +550,11 @@ static uiTooltipData *ui_tooltip_data_from_tool(bContext *C, uiBut *but, bool is if (expr_result != nullptr) { UI_tooltip_text_field_add(data, expr_result, - nullptr, + {}, UI_TIP_STYLE_NORMAL, (is_error) ? UI_TIP_LC_ALERT : UI_TIP_LC_MAIN, true); + MEM_freeN(expr_result); } } @@ -645,7 +631,7 @@ static uiTooltipData *ui_tooltip_data_from_tool(bContext *C, uiBut *but, bool is intptr_t expr_result = 0; if (has_valid_context == false) { - shortcut = BLI_strdup(has_valid_context_error); + shortcut = has_valid_context_error; } else if (BPY_run_string_as_intptr(C, expr_imports, expr, nullptr, &expr_result)) { if (expr_result != 0) { @@ -657,7 +643,7 @@ static uiTooltipData *ui_tooltip_data_from_tool(bContext *C, uiBut *but, bool is if (STREQ(tool_id, tool_id_test)) { char buf[128]; WM_keymap_item_to_string(kmi, false, buf, sizeof(buf)); - shortcut = BLI_sprintfN("%s, %s", shortcut_toolbar, buf); + shortcut = fmt::format("{}, {}", shortcut_toolbar, buf); break; } } @@ -672,8 +658,8 @@ static uiTooltipData *ui_tooltip_data_from_tool(bContext *C, uiBut *but, bool is if (!shortcut.empty()) { UI_tooltip_text_field_add(data, - BLI_sprintfN(TIP_("Shortcut: %s"), shortcut.c_str()), - nullptr, + fmt::format(TIP_("Shortcut: {}"), shortcut), + {}, UI_TIP_STYLE_NORMAL, UI_TIP_LC_VALUE, true); @@ -746,8 +732,8 @@ static uiTooltipData *ui_tooltip_data_from_tool(bContext *C, uiBut *but, bool is if (shortcut[0] != '\0') { UI_tooltip_text_field_add(data, - BLI_sprintfN(TIP_("Shortcut Cycle: %s"), shortcut), - nullptr, + fmt::format(TIP_("Shortcut Cycle: {}"), shortcut), + {}, UI_TIP_STYLE_NORMAL, UI_TIP_LC_VALUE, true); @@ -759,8 +745,8 @@ static uiTooltipData *ui_tooltip_data_from_tool(bContext *C, uiBut *but, bool is if ((is_label == false) && (U.flag & USER_TOOLTIPS_PYTHON)) { char *str = ui_tooltip_text_python_from_op(C, but->optype, but->opptr); UI_tooltip_text_field_add(data, - BLI_sprintfN(TIP_("Python: %s"), str), - nullptr, + fmt::format(TIP_("Python: {}"), str), + {}, UI_TIP_STYLE_NORMAL, UI_TIP_LC_PYTHON, true); @@ -789,12 +775,8 @@ static uiTooltipData *ui_tooltip_data_from_tool(bContext *C, uiBut *but, bool is } else if (BPY_run_string_as_intptr(C, expr_imports, expr, nullptr, &expr_result)) { if (expr_result != 0) { - UI_tooltip_text_field_add(data, - BLI_strdup("Tool Keymap:"), - nullptr, - UI_TIP_STYLE_NORMAL, - UI_TIP_LC_NORMAL, - true); + UI_tooltip_text_field_add( + data, TIP_("Tool Keymap:"), {}, UI_TIP_STYLE_NORMAL, UI_TIP_LC_NORMAL, true); wmKeyMap *keymap = (wmKeyMap *)expr_result; ui_tooltip_data_append_from_keymap(C, data, keymap); } @@ -807,8 +789,8 @@ static uiTooltipData *ui_tooltip_data_from_tool(bContext *C, uiBut *but, bool is UNUSED_VARS(is_label, has_valid_context, has_valid_context_error); #endif /* WITH_PYTHON */ - if (data->fields_len == 0) { - MEM_freeN(data); + if (data->fields.is_empty()) { + MEM_delete(data); return nullptr; } return data; @@ -825,7 +807,7 @@ static uiTooltipData *ui_tooltip_data_from_button_or_extra_icon(bContext *C, but->optype; PropertyRNA *rnaprop = extra_icon ? nullptr : but->rnaprop; - uiTooltipData *data = MEM_cnew(__func__); + uiTooltipData *data = MEM_new(__func__); /* Menus already show shortcuts, don't show them in the tool-tips. */ const bool is_menu = ui_block_is_menu(but->block) && !ui_block_is_pie_menu(but->block); @@ -877,40 +859,32 @@ static uiTooltipData *ui_tooltip_data_from_button_or_extra_icon(bContext *C, /* Label: If there is a custom tooltip label, use that to override the label to display. * Otherwise fallback to the regular label. */ if (!but_tip_label.empty()) { - UI_tooltip_text_field_add( - data, BLI_strdup(but_tip_label.c_str()), nullptr, UI_TIP_STYLE_HEADER, UI_TIP_LC_NORMAL); + UI_tooltip_text_field_add(data, but_tip_label, {}, UI_TIP_STYLE_HEADER, UI_TIP_LC_NORMAL); } /* Regular (non-custom) label. Only show when the button doesn't already show the label. Check * 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) { - UI_tooltip_text_field_add( - data, BLI_strdup(but_label.c_str()), nullptr, UI_TIP_STYLE_HEADER, UI_TIP_LC_NORMAL); + UI_tooltip_text_field_add(data, but_label, {}, UI_TIP_STYLE_HEADER, UI_TIP_LC_NORMAL); } /* Tip */ if (!but_tip.empty()) { if (!enum_label.empty()) { - UI_tooltip_text_field_add(data, - BLI_sprintfN("%s: ", but_tip.c_str()), - BLI_strdup(enum_label.c_str()), - UI_TIP_STYLE_HEADER, - UI_TIP_LC_NORMAL); + UI_tooltip_text_field_add( + data, fmt::format("{}: ", but_tip), enum_label, UI_TIP_STYLE_HEADER, UI_TIP_LC_NORMAL); } else { - UI_tooltip_text_field_add(data, - BLI_sprintfN("%s.", but_tip.c_str()), - nullptr, - UI_TIP_STYLE_HEADER, - UI_TIP_LC_NORMAL); + UI_tooltip_text_field_add( + data, fmt::format("{}.", but_tip), {}, UI_TIP_STYLE_HEADER, UI_TIP_LC_NORMAL); } /* special case enum rna buttons */ if ((but->type & UI_BTYPE_ROW) && rnaprop && RNA_property_flag(rnaprop) & PROP_ENUM_FLAG) { UI_tooltip_text_field_add(data, - BLI_strdup(TIP_("(Shift-Click/Drag to select multiple)")), - nullptr, + TIP_("(Shift-Click/Drag to select multiple)"), + {}, UI_TIP_STYLE_NORMAL, UI_TIP_LC_NORMAL); } @@ -918,20 +892,19 @@ static uiTooltipData *ui_tooltip_data_from_button_or_extra_icon(bContext *C, /* When there is only an enum label (no button label or tip), draw that as header. */ else if (!enum_label.empty() && but_label.empty()) { UI_tooltip_text_field_add( - data, BLI_strdup(enum_label.c_str()), nullptr, UI_TIP_STYLE_HEADER, UI_TIP_LC_NORMAL); + data, std::move(enum_label), {}, UI_TIP_STYLE_HEADER, UI_TIP_LC_NORMAL); } /* Enum field label & tip. */ if (!enum_tip.empty()) { - UI_tooltip_text_field_add( - data, BLI_strdup(enum_tip.c_str()), nullptr, UI_TIP_STYLE_NORMAL, UI_TIP_LC_VALUE); + UI_tooltip_text_field_add(data, std::move(enum_tip), {}, UI_TIP_STYLE_NORMAL, UI_TIP_LC_VALUE); } /* Operator shortcut. */ if (!op_keymap.empty()) { UI_tooltip_text_field_add(data, - BLI_sprintfN(TIP_("Shortcut: %s"), op_keymap.c_str()), - nullptr, + fmt::format(TIP_("Shortcut: {}"), op_keymap), + {}, UI_TIP_STYLE_NORMAL, UI_TIP_LC_VALUE, true); @@ -940,8 +913,8 @@ static uiTooltipData *ui_tooltip_data_from_button_or_extra_icon(bContext *C, /* Property context-toggle shortcut. */ if (!prop_keymap.empty()) { UI_tooltip_text_field_add(data, - BLI_sprintfN(TIP_("Shortcut: %s"), prop_keymap.c_str()), - nullptr, + fmt::format(TIP_("Shortcut: {}"), prop_keymap), + {}, UI_TIP_STYLE_NORMAL, UI_TIP_LC_VALUE, true); @@ -954,8 +927,8 @@ static uiTooltipData *ui_tooltip_data_from_button_or_extra_icon(bContext *C, ui_but_string_get(but, buf, sizeof(buf)); if (buf[0]) { UI_tooltip_text_field_add(data, - BLI_sprintfN(TIP_("Value: %s"), buf), - nullptr, + fmt::format(TIP_("Value: {}"), buf), + {}, UI_TIP_STYLE_NORMAL, UI_TIP_LC_VALUE, true); @@ -972,8 +945,8 @@ static uiTooltipData *ui_tooltip_data_from_button_or_extra_icon(bContext *C, RNA_property_float_get_index(&but->rnapoin, rnaprop, but->rnaindex) : RNA_property_float_get(&but->rnapoin, rnaprop); UI_tooltip_text_field_add(data, - BLI_sprintfN(TIP_("Radians: %f"), value), - nullptr, + fmt::format(TIP_("Radians: {}"), value), + {}, UI_TIP_STYLE_NORMAL, UI_TIP_LC_VALUE); } @@ -982,8 +955,8 @@ static uiTooltipData *ui_tooltip_data_from_button_or_extra_icon(bContext *C, if (but->flag & UI_BUT_DRIVEN) { if (ui_but_anim_expression_get(but, buf, sizeof(buf))) { UI_tooltip_text_field_add(data, - BLI_sprintfN(TIP_("Expression: %s"), buf), - nullptr, + fmt::format(TIP_("Expression: {}"), buf), + {}, UI_TIP_STYLE_NORMAL, UI_TIP_LC_NORMAL); } @@ -993,8 +966,8 @@ static uiTooltipData *ui_tooltip_data_from_button_or_extra_icon(bContext *C, const ID *id = but->rnapoin.owner_id; if (ID_IS_LINKED(id)) { UI_tooltip_text_field_add(data, - BLI_sprintfN(TIP_("Library: %s"), id->lib->filepath), - nullptr, + fmt::format(TIP_("Library: {}"), id->lib->filepath), + {}, UI_TIP_STYLE_NORMAL, UI_TIP_LC_NORMAL); } @@ -1013,8 +986,8 @@ static uiTooltipData *ui_tooltip_data_from_button_or_extra_icon(bContext *C, /* Operator info. */ if (U.flag & USER_TOOLTIPS_PYTHON) { UI_tooltip_text_field_add(data, - BLI_sprintfN(TIP_("Python: %s"), str), - nullptr, + fmt::format(TIP_("Python: {}"), str), + {}, UI_TIP_STYLE_MONO, UI_TIP_LC_PYTHON, true); @@ -1046,8 +1019,8 @@ static uiTooltipData *ui_tooltip_data_from_button_or_extra_icon(bContext *C, if (disabled_msg && disabled_msg[0]) { UI_tooltip_text_field_add(data, - BLI_sprintfN(TIP_("Disabled: %s"), disabled_msg), - nullptr, + fmt::format(TIP_("Disabled: {}"), disabled_msg), + {}, UI_TIP_STYLE_NORMAL, UI_TIP_LC_ALERT); } @@ -1058,30 +1031,27 @@ static uiTooltipData *ui_tooltip_data_from_button_or_extra_icon(bContext *C, if ((U.flag & USER_TOOLTIPS_PYTHON) && !optype && !rna_struct.empty()) { { - UI_tooltip_text_field_add( - data, - rna_prop.empty() ? - BLI_sprintfN(TIP_("Python: %s"), rna_struct.c_str()) : - BLI_sprintfN(TIP_("Python: %s.%s"), rna_struct.c_str(), rna_prop.c_str()), - nullptr, - UI_TIP_STYLE_MONO, - UI_TIP_LC_PYTHON, - true); + UI_tooltip_text_field_add(data, + rna_prop.empty() ? + fmt::format(TIP_("Python: {}"), rna_struct) : + fmt::format(TIP_("Python: {}.{}"), rna_struct, rna_prop), + {}, + UI_TIP_STYLE_MONO, + UI_TIP_LC_PYTHON, + true); } if (but->rnapoin.owner_id) { - UI_tooltip_text_field_add( - data, - (rnaprop) ? RNA_path_full_property_py_ex(&but->rnapoin, rnaprop, but->rnaindex, true) : - RNA_path_full_struct_py(&but->rnapoin), - nullptr, - UI_TIP_STYLE_MONO, - UI_TIP_LC_PYTHON); + char *str = rnaprop ? + RNA_path_full_property_py_ex(&but->rnapoin, rnaprop, but->rnaindex, true) : + RNA_path_full_struct_py(&but->rnapoin); + UI_tooltip_text_field_add(data, str, {}, UI_TIP_STYLE_MONO, UI_TIP_LC_PYTHON); + MEM_freeN(str); } } - if (data->fields_len == 0) { - MEM_freeN(data); + if (data->fields.is_empty()) { + MEM_delete(data); return nullptr; } return data; @@ -1089,7 +1059,7 @@ static uiTooltipData *ui_tooltip_data_from_button_or_extra_icon(bContext *C, static uiTooltipData *ui_tooltip_data_from_gizmo(bContext *C, wmGizmo *gz) { - uiTooltipData *data = MEM_cnew(__func__); + uiTooltipData *data = MEM_new(__func__); /* TODO(@ideasman42): a way for gizmos to have their own descriptions (low priority). */ @@ -1122,10 +1092,8 @@ static uiTooltipData *ui_tooltip_data_from_gizmo(bContext *C, wmGizmo *gz) if (!info.empty()) { UI_tooltip_text_field_add( data, - gzop_actions[i].prefix ? - BLI_sprintfN("%s: %s", gzop_actions[i].prefix, info.c_str()) : - BLI_strdup(info.c_str()), - nullptr, + gzop_actions[i].prefix ? fmt::format("{}: {}", gzop_actions[i].prefix, info) : info, + {}, UI_TIP_STYLE_HEADER, UI_TIP_LC_VALUE, true); @@ -1139,8 +1107,8 @@ static uiTooltipData *ui_tooltip_data_from_gizmo(bContext *C, wmGizmo *gz) C, gzop->type->idname, WM_OP_INVOKE_DEFAULT, prop, true, buf, ARRAY_SIZE(buf))) { UI_tooltip_text_field_add(data, - BLI_sprintfN(TIP_("Shortcut: %s"), buf), - nullptr, + fmt::format(TIP_("Shortcut: {}"), buf), + {}, UI_TIP_STYLE_NORMAL, UI_TIP_LC_VALUE, true); @@ -1159,15 +1127,14 @@ static uiTooltipData *ui_tooltip_data_from_gizmo(bContext *C, wmGizmo *gz) if (gz_prop->prop != nullptr) { const char *info = RNA_property_ui_description(gz_prop->prop); if (info && info[0]) { - UI_tooltip_text_field_add( - data, BLI_strdup(info), nullptr, UI_TIP_STYLE_NORMAL, UI_TIP_LC_VALUE, true); + UI_tooltip_text_field_add(data, info, {}, UI_TIP_STYLE_NORMAL, UI_TIP_LC_VALUE, true); } } } } - if (data->fields_len == 0) { - MEM_freeN(data); + if (data->fields.is_empty()) { + MEM_delete(data); return nullptr; } return data; @@ -1176,13 +1143,13 @@ static uiTooltipData *ui_tooltip_data_from_gizmo(bContext *C, wmGizmo *gz) static uiTooltipData *ui_tooltip_data_from_custom_func(bContext *C, uiBut *but) { /* Create tooltip data. */ - uiTooltipData *data = MEM_cnew(__func__); + uiTooltipData *data = MEM_new(__func__); /* Create fields from custom callback. */ but->tip_custom_func(C, data, but->tip_arg); - if (data->fields_len == 0) { - MEM_freeN(data); + if (data->fields.is_empty()) { + MEM_delete(data); return nullptr; } return data; @@ -1233,7 +1200,7 @@ static ARegion *ui_tooltip_create_with_data(bContext *C, int h = BLF_height_max(data->fstyle.uifont_id); int i, fonth, fontw; - for (i = 0, fontw = 0, fonth = 0; i < data->fields_len; i++) { + for (i = 0, fontw = 0, fonth = 0; i < data->fields.size(); i++) { uiTooltipField *field = &data->fields[i]; ResultBLF info = {0}; int w = 0; @@ -1248,14 +1215,14 @@ static ARegion *ui_tooltip_create_with_data(bContext *C, font_id = data->fstyle.uifont_id; } - if (field->text && field->text[0]) { - w = BLF_width_ex(font_id, field->text, UI_TIP_STR_MAX, &info); + if (!field->text.empty()) { + w = BLF_width_ex(font_id, field->text.c_str(), field->text.size(), &info); } /* check for suffix (enum label) */ - if (field->text_suffix && field->text_suffix[0]) { + if (!field->text_suffix.empty()) { x_pos = info.width; - w = max_ii(w, x_pos + BLF_width(font_id, field->text_suffix, UI_TIP_STR_MAX)); + w = max_ii(w, x_pos + BLF_width(font_id, field->text_suffix.c_str(), field->text.size())); } fonth += h * info.lines; @@ -1541,36 +1508,24 @@ ARegion *UI_tooltip_create_from_gizmo(bContext *C, wmGizmo *gz) static uiTooltipData *ui_tooltip_data_from_search_item_tooltip_data( const uiSearchItemTooltipData *item_tooltip_data) { - uiTooltipData *data = MEM_cnew(__func__); + uiTooltipData *data = MEM_new(__func__); if (item_tooltip_data->description[0]) { - UI_tooltip_text_field_add(data, - BLI_strdup(item_tooltip_data->description), - nullptr, - UI_TIP_STYLE_HEADER, - UI_TIP_LC_NORMAL, - true); + UI_tooltip_text_field_add( + data, item_tooltip_data->description, {}, UI_TIP_STYLE_HEADER, UI_TIP_LC_NORMAL, true); } if (item_tooltip_data->name && item_tooltip_data->name[0]) { - UI_tooltip_text_field_add(data, - BLI_strdup(item_tooltip_data->name), - nullptr, - UI_TIP_STYLE_NORMAL, - UI_TIP_LC_VALUE, - true); + UI_tooltip_text_field_add( + data, item_tooltip_data->name, {}, UI_TIP_STYLE_NORMAL, UI_TIP_LC_VALUE, true); } if (item_tooltip_data->hint[0]) { - UI_tooltip_text_field_add(data, - BLI_strdup(item_tooltip_data->hint), - nullptr, - UI_TIP_STYLE_NORMAL, - UI_TIP_LC_NORMAL, - true); + UI_tooltip_text_field_add( + data, item_tooltip_data->hint, {}, UI_TIP_STYLE_NORMAL, UI_TIP_LC_NORMAL, true); } - if (data->fields_len == 0) { - MEM_freeN(data); + if (data->fields.is_empty()) { + MEM_delete(data); return nullptr; } return data; diff --git a/source/blender/editors/interface/interface_templates.cc b/source/blender/editors/interface/interface_templates.cc index b6f88f89dda..019165c5f1b 100644 --- a/source/blender/editors/interface/interface_templates.cc +++ b/source/blender/editors/interface/interface_templates.cc @@ -11,6 +11,8 @@ #include #include +#include + #include "MEM_guardedalloc.h" #include "DNA_brush_types.h" @@ -7199,12 +7201,11 @@ static void uiTemplateRecentFiles_tooltip_func(bContext * /*C*/, uiTooltipData * /* File path. */ char root[FILE_MAX]; BLI_path_split_dir_part(path, root, FILE_MAX); - UI_tooltip_text_field_add(tip, BLI_strdup(root), nullptr, UI_TIP_STYLE_HEADER, UI_TIP_LC_NORMAL); - UI_tooltip_text_field_add(tip, nullptr, nullptr, UI_TIP_STYLE_SPACER, UI_TIP_LC_NORMAL); + UI_tooltip_text_field_add(tip, root, {}, UI_TIP_STYLE_HEADER, UI_TIP_LC_NORMAL); + UI_tooltip_text_field_add(tip, {}, {}, UI_TIP_STYLE_SPACER, UI_TIP_LC_NORMAL); if (!BLI_exists(path)) { - UI_tooltip_text_field_add( - tip, BLI_strdup(N_("File Not Found")), nullptr, UI_TIP_STYLE_NORMAL, UI_TIP_LC_ALERT); + UI_tooltip_text_field_add(tip, N_("File Not Found"), {}, UI_TIP_STYLE_NORMAL, UI_TIP_LC_ALERT); return; } @@ -7228,12 +7229,9 @@ static void uiTemplateRecentFiles_tooltip_func(bContext * /*C*/, uiTooltipData * } if (version_st[0]) { - UI_tooltip_text_field_add(tip, - BLI_sprintfN("Blender %s", version_st), - nullptr, - UI_TIP_STYLE_NORMAL, - UI_TIP_LC_NORMAL); - UI_tooltip_text_field_add(tip, nullptr, nullptr, UI_TIP_STYLE_SPACER, UI_TIP_LC_NORMAL); + UI_tooltip_text_field_add( + tip, fmt::format("Blender {}", version_st), {}, UI_TIP_STYLE_NORMAL, UI_TIP_LC_NORMAL); + UI_tooltip_text_field_add(tip, {}, {}, UI_TIP_STYLE_SPACER, UI_TIP_LC_NORMAL); } BLI_stat_t status; @@ -7247,23 +7245,20 @@ static void uiTemplateRecentFiles_tooltip_func(bContext * /*C*/, uiTooltipData * day_string = (is_today ? N_("Today") : N_("Yesterday")) + std::string(" "); } UI_tooltip_text_field_add(tip, - BLI_sprintfN("%s: %s%s%s", - N_("Modified"), - day_string.c_str(), - (is_today || is_yesterday) ? "" : date_st, - (is_today || is_yesterday) ? time_st : ""), - nullptr, + fmt::format("{}: {}{}{}", + N_("Modified"), + day_string, + (is_today || is_yesterday) ? "" : date_st, + (is_today || is_yesterday) ? time_st : ""), + {}, UI_TIP_STYLE_NORMAL, UI_TIP_LC_NORMAL); if (status.st_size > 0) { char size[16]; BLI_filelist_entry_size_to_string(nullptr, status.st_size, false, size); - UI_tooltip_text_field_add(tip, - BLI_sprintfN("%s: %s", N_("Size"), size), - nullptr, - UI_TIP_STYLE_NORMAL, - UI_TIP_LC_NORMAL); + UI_tooltip_text_field_add( + tip, fmt::format("{}: {}", N_("Size"), size), {}, UI_TIP_STYLE_NORMAL, UI_TIP_LC_NORMAL); } } @@ -7279,8 +7274,8 @@ static void uiTemplateRecentFiles_tooltip_func(bContext * /*C*/, uiTooltipData * if (thumb) { float scale = (72.0f * UI_SCALE_FAC) / float(MAX2(thumb->x, thumb->y)); short size[2] = {short(float(thumb->x) * scale), short(float(thumb->y) * scale)}; - UI_tooltip_text_field_add(tip, nullptr, nullptr, UI_TIP_STYLE_SPACER, UI_TIP_LC_NORMAL); - UI_tooltip_text_field_add(tip, nullptr, nullptr, UI_TIP_STYLE_SPACER, UI_TIP_LC_NORMAL); + UI_tooltip_text_field_add(tip, {}, {}, UI_TIP_STYLE_SPACER, UI_TIP_LC_NORMAL); + UI_tooltip_text_field_add(tip, {}, {}, UI_TIP_STYLE_SPACER, UI_TIP_LC_NORMAL); UI_tooltip_image_field_add(tip, thumb, size); IMB_freeImBuf(thumb); } diff --git a/source/blender/editors/space_file/file_draw.cc b/source/blender/editors/space_file/file_draw.cc index d8394d12fe7..5f1282243ed 100644 --- a/source/blender/editors/space_file/file_draw.cc +++ b/source/blender/editors/space_file/file_draw.cc @@ -11,6 +11,8 @@ #include #include +#include + #include "MEM_guardedalloc.h" #include "AS_asset_representation.hh" @@ -145,9 +147,8 @@ static void file_draw_tooltip_custom_func(bContext * /*C*/, uiTooltipData *tip, /* Only free if it is loaded later. */ bool free_imbuf = (thumb == nullptr); - UI_tooltip_text_field_add( - tip, BLI_strdup(file->name), nullptr, UI_TIP_STYLE_HEADER, UI_TIP_LC_MAIN); - UI_tooltip_text_field_add(tip, nullptr, nullptr, UI_TIP_STYLE_SPACER, UI_TIP_LC_NORMAL); + UI_tooltip_text_field_add(tip, file->name, {}, UI_TIP_STYLE_HEADER, UI_TIP_LC_MAIN); + UI_tooltip_text_field_add(tip, {}, {}, UI_TIP_STYLE_SPACER, UI_TIP_LC_NORMAL); if (!(file->typeflag & FILE_TYPE_BLENDERLIB)) { @@ -157,37 +158,27 @@ static void file_draw_tooltip_custom_func(bContext * /*C*/, uiTooltipData *tip, if (params->recursion_level > 0) { char root[FILE_MAX]; BLI_path_split_dir_part(full_path, root, FILE_MAX); - UI_tooltip_text_field_add( - tip, BLI_strdup(root), nullptr, UI_TIP_STYLE_NORMAL, UI_TIP_LC_NORMAL); + UI_tooltip_text_field_add(tip, root, {}, UI_TIP_STYLE_NORMAL, UI_TIP_LC_NORMAL); } if (file->redirection_path) { UI_tooltip_text_field_add(tip, - BLI_sprintfN("%s: %s", N_("Link target"), file->redirection_path), - nullptr, + fmt::format("{}: {}", N_("Link target"), file->redirection_path), + {}, UI_TIP_STYLE_NORMAL, UI_TIP_LC_NORMAL); } if (file->attributes & FILE_ATTR_OFFLINE) { - UI_tooltip_text_field_add(tip, - BLI_strdup(N_("This file is offline")), - nullptr, - UI_TIP_STYLE_NORMAL, - UI_TIP_LC_ALERT); + UI_tooltip_text_field_add( + tip, N_("This file is offline"), {}, UI_TIP_STYLE_NORMAL, UI_TIP_LC_ALERT); } if (file->attributes & FILE_ATTR_READONLY) { - UI_tooltip_text_field_add(tip, - BLI_strdup(N_("This file is read-only")), - nullptr, - UI_TIP_STYLE_NORMAL, - UI_TIP_LC_ALERT); + UI_tooltip_text_field_add( + tip, N_("This file is read-only"), {}, UI_TIP_STYLE_NORMAL, UI_TIP_LC_ALERT); } if (file->attributes & (FILE_ATTR_SYSTEM | FILE_ATTR_RESTRICTED)) { - UI_tooltip_text_field_add(tip, - BLI_strdup(N_("This is a restricted system file")), - nullptr, - UI_TIP_STYLE_NORMAL, - UI_TIP_LC_ALERT); + UI_tooltip_text_field_add( + tip, N_("This is a restricted system file"), {}, UI_TIP_STYLE_NORMAL, UI_TIP_LC_ALERT); } if (file->typeflag & (FILE_TYPE_BLENDER | FILE_TYPE_BLENDER_BACKUP)) { @@ -211,12 +202,9 @@ static void file_draw_tooltip_custom_func(bContext * /*C*/, uiTooltipData *tip, } if (version_st[0]) { - UI_tooltip_text_field_add(tip, - BLI_sprintfN("Blender %s", version_st), - nullptr, - UI_TIP_STYLE_NORMAL, - UI_TIP_LC_NORMAL); - UI_tooltip_text_field_add(tip, nullptr, nullptr, UI_TIP_STYLE_SPACER, UI_TIP_LC_NORMAL); + UI_tooltip_text_field_add( + tip, fmt::format("Blender {}", version_st), {}, UI_TIP_STYLE_NORMAL, UI_TIP_LC_NORMAL); + UI_tooltip_text_field_add(tip, {}, {}, UI_TIP_STYLE_SPACER, UI_TIP_LC_NORMAL); } } else if (file->typeflag & FILE_TYPE_IMAGE) { @@ -233,11 +221,11 @@ static void file_draw_tooltip_custom_func(bContext * /*C*/, uiTooltipData *tip, thumb->metadata, "Thumb::Image::Height", value2, sizeof(value2))) { UI_tooltip_text_field_add(tip, - BLI_sprintfN("%s \u00D7 %s", value1, value2), - nullptr, + fmt::format("{} \u00D7 {}", value1, value2), + {}, UI_TIP_STYLE_NORMAL, UI_TIP_LC_NORMAL); - UI_tooltip_text_field_add(tip, nullptr, nullptr, UI_TIP_STYLE_SPACER, UI_TIP_LC_NORMAL); + UI_tooltip_text_field_add(tip, {}, {}, UI_TIP_STYLE_SPACER, UI_TIP_LC_NORMAL); } } } @@ -256,8 +244,8 @@ static void file_draw_tooltip_custom_func(bContext * /*C*/, uiTooltipData *tip, thumb->metadata, "Thumb::Video::Height", value2, sizeof(value2))) { UI_tooltip_text_field_add(tip, - BLI_sprintfN("%s \u00D7 %s", value1, value2), - nullptr, + fmt::format("{} \u00D7 {}", value1, value2), + {}, UI_TIP_STYLE_NORMAL, UI_TIP_LC_NORMAL); } @@ -269,16 +257,16 @@ static void file_draw_tooltip_custom_func(bContext * /*C*/, uiTooltipData *tip, { UI_tooltip_text_field_add( tip, - BLI_sprintfN("%s %s @ %s %s", value1, N_("Frames"), value2, N_("FPS")), - nullptr, + fmt::format("{} {} @ {} {}", value1, N_("Frames"), value2, N_("FPS")), + {}, UI_TIP_STYLE_NORMAL, UI_TIP_LC_NORMAL); UI_tooltip_text_field_add(tip, - BLI_sprintfN("%s %s", value3, N_("seconds")), - nullptr, + fmt::format("{} {}", value3, N_("seconds")), + {}, UI_TIP_STYLE_NORMAL, UI_TIP_LC_NORMAL); - UI_tooltip_text_field_add(tip, nullptr, nullptr, UI_TIP_STYLE_SPACER, UI_TIP_LC_NORMAL); + UI_tooltip_text_field_add(tip, {}, {}, UI_TIP_STYLE_SPACER, UI_TIP_LC_NORMAL); } } } @@ -292,11 +280,11 @@ static void file_draw_tooltip_custom_func(bContext * /*C*/, uiTooltipData *tip, day_string = (is_today ? N_("Today") : N_("Yesterday")) + std::string(" "); } UI_tooltip_text_field_add(tip, - BLI_sprintfN("%s: %s%s%s", - N_("Modified"), - day_string.c_str(), - (is_today || is_yesterday) ? "" : date_st, - (is_today || is_yesterday) ? time_st : ""), + fmt::format("{}: {}{}{}", + N_("Modified"), + day_string, + (is_today || is_yesterday) ? "" : date_st, + (is_today || is_yesterday) ? time_st : ""), nullptr, UI_TIP_STYLE_NORMAL, UI_TIP_LC_NORMAL); @@ -309,15 +297,15 @@ static void file_draw_tooltip_custom_func(bContext * /*C*/, uiTooltipData *tip, BLI_str_format_uint64_grouped(size_full, file->size); UI_tooltip_text_field_add( tip, - BLI_sprintfN("%s: %s (%s %s)", N_("Size"), size, size_full, N_("bytes")), - nullptr, + fmt::format("{}: {} ({} {})", N_("Size"), size, size_full, N_("bytes")), + {}, UI_TIP_STYLE_NORMAL, UI_TIP_LC_NORMAL); } else { UI_tooltip_text_field_add(tip, - BLI_sprintfN("%s: %s", N_("Size"), size), - nullptr, + fmt::format("{}: {}", N_("Size"), size), + {}, UI_TIP_STYLE_NORMAL, UI_TIP_LC_NORMAL); } @@ -327,8 +315,8 @@ static void file_draw_tooltip_custom_func(bContext * /*C*/, uiTooltipData *tip, if (thumb && params->display != FILE_IMGDISPLAY) { float scale = (96.0f * UI_SCALE_FAC) / float(MAX2(thumb->x, thumb->y)); short size[2] = {short(float(thumb->x) * scale), short(float(thumb->y) * scale)}; - UI_tooltip_text_field_add(tip, nullptr, nullptr, UI_TIP_STYLE_SPACER, UI_TIP_LC_NORMAL); - UI_tooltip_text_field_add(tip, nullptr, nullptr, UI_TIP_STYLE_SPACER, UI_TIP_LC_NORMAL); + UI_tooltip_text_field_add(tip, {}, {}, UI_TIP_STYLE_SPACER, UI_TIP_LC_NORMAL); + UI_tooltip_text_field_add(tip, {}, {}, UI_TIP_STYLE_SPACER, UI_TIP_LC_NORMAL); UI_tooltip_image_field_add(tip, thumb, size); }