From 7ca4dcac5a113db514faf7362b95bfe584e252be Mon Sep 17 00:00:00 2001 From: Hans Goudey Date: Fri, 19 Jan 2024 10:45:33 -0500 Subject: [PATCH] Cleanup: Use std::string for UI drop box tooltip return type And use fmt for formatting strings. --- source/blender/editors/include/ED_object.hh | 12 ++-- .../blender/editors/include/UI_interface.hh | 8 +-- .../blender/editors/include/UI_interface_c.hh | 2 +- .../editors/interface/interface_drop.cc | 11 ++-- .../editors/interface/interface_dropboxes.cc | 43 +++++++------- .../editors/interface/interface_style.cc | 6 +- .../blender/editors/io/io_drop_import_file.cc | 12 ++-- source/blender/editors/object/CMakeLists.txt | 1 + .../editors/object/object_relations.cc | 36 ++++++------ .../space_outliner/outliner_dragdrop.cc | 57 +++++++------------ .../editors/space_view3d/space_view3d.cc | 26 ++++----- source/blender/windowmanager/WM_types.hh | 8 +-- .../windowmanager/intern/wm_dragdrop.cc | 24 ++++---- 13 files changed, 118 insertions(+), 128 deletions(-) diff --git a/source/blender/editors/include/ED_object.hh b/source/blender/editors/include/ED_object.hh index 3ef569d8899..369075b1434 100644 --- a/source/blender/editors/include/ED_object.hh +++ b/source/blender/editors/include/ED_object.hh @@ -8,6 +8,8 @@ #pragma once +#include + #include "BLI_compiler_attrs.h" #include "BLI_string_ref.hh" @@ -248,10 +250,12 @@ Base *ED_object_add_duplicate( Main *bmain, Scene *scene, ViewLayer *view_layer, Base *base, eDupli_ID_Flags dupflag); void ED_object_parent(Object *ob, Object *parent, int type, const char *substr); -char *ED_object_ot_drop_named_material_tooltip(bContext *C, const char *name, const int mval[2]); -char *ED_object_ot_drop_geometry_nodes_tooltip(bContext *C, - PointerRNA *properties, - const int mval[2]); +std::string ED_object_ot_drop_named_material_tooltip(bContext *C, + const char *name, + const int mval[2]); +std::string ED_object_ot_drop_geometry_nodes_tooltip(bContext *C, + PointerRNA *properties, + const int mval[2]); /* bitflags for enter/exit editmode */ enum { diff --git a/source/blender/editors/include/UI_interface.hh b/source/blender/editors/include/UI_interface.hh index cc0e4adc83a..2da1889a228 100644 --- a/source/blender/editors/include/UI_interface.hh +++ b/source/blender/editors/include/UI_interface.hh @@ -179,10 +179,10 @@ bool drop_target_apply_drop(bContext &C, * Call #DropTargetInterface::drop_tooltip() and return the result as newly allocated C string * (unless the result is empty, returns null then). Needs freeing with MEM_freeN(). */ -char *drop_target_tooltip(const ARegion ®ion, - const DropTargetInterface &drop_target, - const wmDrag &drag, - const wmEvent &event); +std::string drop_target_tooltip(const ARegion ®ion, + const DropTargetInterface &drop_target, + const wmDrag &drag, + const wmEvent &event); std::unique_ptr view_drop_target(uiViewHandle *view_handle); std::unique_ptr view_item_drop_target(uiViewItemHandle *item_handle); diff --git a/source/blender/editors/include/UI_interface_c.hh b/source/blender/editors/include/UI_interface_c.hh index 238a1adf610..f1bbde61105 100644 --- a/source/blender/editors/include/UI_interface_c.hh +++ b/source/blender/editors/include/UI_interface_c.hh @@ -3171,7 +3171,7 @@ void UI_fontstyle_draw_simple( void UI_fontstyle_draw_simple_backdrop(const uiFontStyle *fs, float x, float y, - const char *str, + blender::StringRef str, const float col_fg[4], const float col_bg[4]); diff --git a/source/blender/editors/interface/interface_drop.cc b/source/blender/editors/interface/interface_drop.cc index bd02644f90d..5b1c828ff08 100644 --- a/source/blender/editors/interface/interface_drop.cc +++ b/source/blender/editors/interface/interface_drop.cc @@ -48,10 +48,10 @@ bool drop_target_apply_drop(bContext &C, return false; } -char *drop_target_tooltip(const ARegion ®ion, - const DropTargetInterface &drop_target, - const wmDrag &drag, - const wmEvent &event) +std::string drop_target_tooltip(const ARegion ®ion, + const DropTargetInterface &drop_target, + const wmDrag &drag, + const wmEvent &event) { const char *disabled_hint_dummy = nullptr; if (!drop_target.can_drop(drag, &disabled_hint_dummy)) { @@ -65,8 +65,7 @@ char *drop_target_tooltip(const ARegion ®ion, } const DragInfo drag_info{drag, event, *drop_location}; - const std::string tooltip = drop_target.drop_tooltip(drag_info); - return tooltip.empty() ? nullptr : BLI_strdup(tooltip.c_str()); + return drop_target.drop_tooltip(drag_info); } } // namespace blender::ui diff --git a/source/blender/editors/interface/interface_dropboxes.cc b/source/blender/editors/interface/interface_dropboxes.cc index 9305def9a36..0bd7ef280ad 100644 --- a/source/blender/editors/interface/interface_dropboxes.cc +++ b/source/blender/editors/interface/interface_dropboxes.cc @@ -6,6 +6,8 @@ * \ingroup edinterface */ +#include + #include "BKE_context.hh" #include "BLI_string.h" @@ -47,7 +49,10 @@ static bool ui_view_drop_poll(bContext *C, wmDrag *drag, const wmEvent *event) return drop_target->can_drop(*drag, &drag->drop_state.disabled_info); } -static char *ui_view_drop_tooltip(bContext *C, wmDrag *drag, const int xy[2], wmDropBox * /*drop*/) +static std::string ui_view_drop_tooltip(bContext *C, + wmDrag *drag, + const int xy[2], + wmDropBox * /*drop*/) { const wmWindow *win = CTX_wm_window(C); const ARegion *region = CTX_wm_region(C); @@ -91,10 +96,10 @@ static void ui_drop_material_copy(bContext *C, wmDrag *drag, wmDropBox *drop) RNA_int_set(drop->ptr, "session_uuid", int(id->session_uuid)); } -static char *ui_drop_material_tooltip(bContext *C, - wmDrag *drag, - const int /*xy*/[2], - wmDropBox * /*drop*/) +static std::string ui_drop_material_tooltip(bContext *C, + wmDrag *drag, + const int /*xy*/[2], + wmDropBox * /*drop*/) { PointerRNA rna_ptr = CTX_data_pointer_get_type(C, "object", &RNA_Object); Object *ob = (Object *)rna_ptr.data; @@ -109,25 +114,21 @@ static char *ui_drop_material_tooltip(bContext *C, Material *prev_mat_in_slot = (Material *)rna_prev_material.data; const char *dragged_material_name = WM_drag_get_item_name(drag); - char *result; if (prev_mat_in_slot) { - const char *tooltip = TIP_("Drop %s on slot %d (replacing %s) of %s"); - result = BLI_sprintfN(tooltip, - dragged_material_name, - target_slot, - prev_mat_in_slot->id.name + 2, - ob->id.name + 2); + return fmt::format(TIP_("Drop {} on slot {} (replacing {}) of {}"), + dragged_material_name, + target_slot, + prev_mat_in_slot->id.name + 2, + ob->id.name + 2); } - else if (target_slot == ob->actcol) { - const char *tooltip = TIP_("Drop %s on slot %d (active slot) of %s"); - result = BLI_sprintfN(tooltip, dragged_material_name, target_slot, ob->id.name + 2); + if (target_slot == ob->actcol) { + return fmt::format(TIP_("Drop {} on slot {} (active slot) of {}"), + dragged_material_name, + target_slot, + ob->id.name + 2); } - else { - const char *tooltip = TIP_("Drop %s on slot %d of %s"); - result = BLI_sprintfN(tooltip, dragged_material_name, target_slot, ob->id.name + 2); - } - - return result; + return fmt::format( + TIP_("Drop {} on slot {} of {}"), dragged_material_name, target_slot, ob->id.name + 2); } /** \} */ diff --git a/source/blender/editors/interface/interface_style.cc b/source/blender/editors/interface/interface_style.cc index a6cbdae2fb5..93523898c47 100644 --- a/source/blender/editors/interface/interface_style.cc +++ b/source/blender/editors/interface/interface_style.cc @@ -279,14 +279,14 @@ void UI_fontstyle_draw_simple( void UI_fontstyle_draw_simple_backdrop(const uiFontStyle *fs, float x, float y, - const char *str, + const blender::StringRef str, const float col_fg[4], const float col_bg[4]) { UI_fontstyle_set(fs); { - const int width = BLF_width(fs->uifont_id, str, BLF_DRAW_STR_DUMMY_MAX); + const int width = BLF_width(fs->uifont_id, str.data(), str.size()); const int height = BLF_height_max(fs->uifont_id); const int decent = BLF_descender(fs->uifont_id); const float margin = height / 4.0f; @@ -302,7 +302,7 @@ void UI_fontstyle_draw_simple_backdrop(const uiFontStyle *fs, BLF_position(fs->uifont_id, x, y, 0.0f); BLF_color4fv(fs->uifont_id, col_fg); - BLF_draw(fs->uifont_id, str, BLF_DRAW_STR_DUMMY_MAX); + BLF_draw(fs->uifont_id, str.data(), str.size()); } /* ************** helpers ************************ */ diff --git a/source/blender/editors/io/io_drop_import_file.cc b/source/blender/editors/io/io_drop_import_file.cc index 28b2da176c7..a9e89109052 100644 --- a/source/blender/editors/io/io_drop_import_file.cc +++ b/source/blender/editors/io/io_drop_import_file.cc @@ -223,19 +223,19 @@ static bool drop_import_file_poll(bContext *C, wmDrag *drag, const wmEvent * /*e return !drop_import_file_poll_file_handlers(C, paths, true).is_empty(); } -static char *drop_import_file_tooltip(bContext *C, - wmDrag *drag, - const int /*xy*/[2], - wmDropBox * /*drop*/) +static std::string drop_import_file_tooltip(bContext *C, + wmDrag *drag, + const int /*xy*/[2], + wmDropBox * /*drop*/) { const auto paths = WM_drag_get_paths(drag); const auto file_handlers = drop_import_file_poll_file_handlers(C, paths, true); if (file_handlers.size() == 1) { wmOperatorType *ot = WM_operatortype_find(file_handlers[0]->import_operator, false); - return BLI_strdup(TIP_(ot->name)); + return TIP_(ot->name); } - return BLI_strdup(TIP_("Multiple file handlers can be used, drop to pick which to use")); + return TIP_("Multiple file handlers can be used, drop to pick which to use"); } void ED_dropbox_drop_import_file() diff --git a/source/blender/editors/object/CMakeLists.txt b/source/blender/editors/object/CMakeLists.txt index 69c63252ae8..39f216fece5 100644 --- a/source/blender/editors/object/CMakeLists.txt +++ b/source/blender/editors/object/CMakeLists.txt @@ -19,6 +19,7 @@ set(INC ../../render ../../shader_fx ../../windowmanager + ../../../../extern/fmtlib/include # RNA_prototypes.h ${CMAKE_BINARY_DIR}/source/blender/makesrna diff --git a/source/blender/editors/object/object_relations.cc b/source/blender/editors/object/object_relations.cc index 9f33fb15087..3dd47500a66 100644 --- a/source/blender/editors/object/object_relations.cc +++ b/source/blender/editors/object/object_relations.cc @@ -10,6 +10,8 @@ #include #include +#include + #include "MEM_guardedalloc.h" #include "DNA_anim_types.h" @@ -2925,27 +2927,27 @@ void OBJECT_OT_make_single_user(wmOperatorType *ot) /** \name Drop Named Material on Object Operator * \{ */ -char *ED_object_ot_drop_named_material_tooltip(bContext *C, const char *name, const int mval[2]) +std::string ED_object_ot_drop_named_material_tooltip(bContext *C, + const char *name, + const int mval[2]) { int mat_slot = 0; Object *ob = ED_view3d_give_material_slot_under_cursor(C, mval, &mat_slot); if (ob == nullptr) { - return BLI_strdup(""); + return {}; } mat_slot = max_ii(mat_slot, 1); Material *prev_mat = BKE_object_material_get(ob, mat_slot); - char *result; if (prev_mat) { - const char *tooltip = TIP_("Drop %s on %s (slot %d, replacing %s)"); - result = BLI_sprintfN(tooltip, name, ob->id.name + 2, mat_slot, prev_mat->id.name + 2); + return fmt::format(TIP_("Drop {} on {} (slot {}, replacing {})"), + name, + ob->id.name + 2, + mat_slot, + prev_mat->id.name + 2); } - else { - const char *tooltip = TIP_("Drop %s on %s (slot %d)"); - result = BLI_sprintfN(tooltip, name, ob->id.name + 2, mat_slot); - } - return result; + return fmt::format(TIP_("Drop {} on {} (slot {})"), name, ob->id.name + 2, mat_slot); } static int drop_named_material_invoke(bContext *C, wmOperator *op, const wmEvent *event) @@ -2996,23 +2998,23 @@ void OBJECT_OT_drop_named_material(wmOperatorType *ot) /** \name Drop Geometry Nodes on Object Operator * \{ */ -char *ED_object_ot_drop_geometry_nodes_tooltip(bContext *C, - PointerRNA *properties, - const int mval[2]) +std::string ED_object_ot_drop_geometry_nodes_tooltip(bContext *C, + PointerRNA *properties, + const int mval[2]) { const Object *ob = ED_view3d_give_object_under_cursor(C, mval); if (ob == nullptr) { - return BLI_strdup(""); + return {}; } const uint32_t session_uuid = RNA_int_get(properties, "session_uuid"); const ID *id = BKE_libblock_find_session_uuid(CTX_data_main(C), ID_NT, session_uuid); if (!id) { - return BLI_strdup(""); + return {}; } - const char *tooltip = TIP_("Add modifier with node group \"%s\" on object \"%s\""); - return BLI_sprintfN(tooltip, id->name, ob->id.name); + return fmt::format( + TIP_("Add modifier with node group \"{}\" on object \"{}\""), id->name, ob->id.name); } static bool check_geometry_node_group_sockets(wmOperator *op, const bNodeTree *tree) diff --git a/source/blender/editors/space_outliner/outliner_dragdrop.cc b/source/blender/editors/space_outliner/outliner_dragdrop.cc index 1fa0727bba9..1fc524521e0 100644 --- a/source/blender/editors/space_outliner/outliner_dragdrop.cc +++ b/source/blender/editors/space_outliner/outliner_dragdrop.cc @@ -892,34 +892,28 @@ static bool datastack_drop_poll(bContext *C, wmDrag *drag, const wmEvent *event) return true; } -static char *datastack_drop_tooltip(bContext * /*C*/, - wmDrag *drag, - const int /*xy*/[2], - wmDropBox * /*drop*/) +static std::string datastack_drop_tooltip(bContext * /*C*/, + wmDrag *drag, + const int /*xy*/[2], + wmDropBox * /*drop*/) { StackDropData *drop_data = static_cast(drag->poin); switch (drop_data->drop_action) { case DATA_STACK_DROP_REORDER: - return BLI_strdup(TIP_("Reorder")); - break; + return TIP_("Reorder"); case DATA_STACK_DROP_COPY: if (drop_data->pchan_parent) { - return BLI_strdup(TIP_("Copy to bone")); + return TIP_("Copy to bone"); } - else { - return BLI_strdup(TIP_("Copy to object")); - } - break; + return TIP_("Copy to object"); + case DATA_STACK_DROP_LINK: if (drop_data->pchan_parent) { - return BLI_strdup(TIP_("Link all to bone")); + return TIP_("Link all to bone"); } - else { - return BLI_strdup(TIP_("Link all to object")); - } - break; + return TIP_("Link all to object"); } - return nullptr; + return {}; } static void datastack_drop_link(bContext *C, StackDropData *drop_data) @@ -1234,10 +1228,10 @@ static bool collection_drop_poll(bContext *C, wmDrag *drag, const wmEvent *event return false; } -static char *collection_drop_tooltip(bContext *C, - wmDrag *drag, - const int xy[2], - wmDropBox * /*drop*/) +static std::string collection_drop_tooltip(bContext *C, + wmDrag *drag, + const int xy[2], + wmDropBox * /*drop*/) { wmWindow *win = CTX_wm_window(C); const wmEvent *event = win ? win->eventstate : nullptr; @@ -1268,23 +1262,17 @@ static char *collection_drop_tooltip(bContext *C, switch (data.insert_type) { case TE_INSERT_BEFORE: if (te->prev && outliner_is_collection_tree_element(te->prev)) { - return BLI_strdup(tooltip_between); + return tooltip_between; } - else { - return BLI_strdup(tooltip_before); - } - break; + return tooltip_before; case TE_INSERT_AFTER: if (te->next && outliner_is_collection_tree_element(te->next)) { - return BLI_strdup(tooltip_between); + return tooltip_between; } - else { - return BLI_strdup(tooltip_after); - } - break; + return tooltip_after; case TE_INSERT_INTO: { if (is_link) { - return BLI_strdup(TIP_("Link inside collection")); + return TIP_("Link inside collection"); } /* Check the type of the drag IDs to avoid the incorrect "Shift to parent" @@ -1293,10 +1281,9 @@ static char *collection_drop_tooltip(bContext *C, wmDragID *drag_id = (wmDragID *)drag->ids.first; const bool is_object = (GS(drag_id->id->name) == ID_OB); if (is_object) { - return BLI_strdup(TIP_("Move inside collection (Ctrl to link, Shift to parent)")); + return TIP_("Move inside collection (Ctrl to link, Shift to parent)"); } - return BLI_strdup(TIP_("Move inside collection (Ctrl to link)")); - break; + return TIP_("Move inside collection (Ctrl to link)"); } } } diff --git a/source/blender/editors/space_view3d/space_view3d.cc b/source/blender/editors/space_view3d/space_view3d.cc index c3909bc11e6..b7c4f67ba69 100644 --- a/source/blender/editors/space_view3d/space_view3d.cc +++ b/source/blender/editors/space_view3d/space_view3d.cc @@ -590,10 +590,10 @@ static bool view3d_mat_drop_poll(bContext *C, wmDrag *drag, const wmEvent *event return view3d_drop_id_in_main_region_poll(C, drag, event, ID_MA); } -static char *view3d_mat_drop_tooltip(bContext *C, - wmDrag *drag, - const int xy[2], - wmDropBox * /*drop*/) +static std::string view3d_mat_drop_tooltip(bContext *C, + wmDrag *drag, + const int xy[2], + wmDropBox * /*drop*/) { const char *name = WM_drag_get_item_name(drag); ARegion *region = CTX_wm_region(C); @@ -618,12 +618,12 @@ static bool view3d_object_data_drop_poll(bContext *C, wmDrag *drag, const wmEven return false; } -static char *view3d_object_data_drop_tooltip(bContext * /*C*/, - wmDrag * /*drag*/, - const int /*xy*/[2], - wmDropBox * /*drop*/) +static std::string view3d_object_data_drop_tooltip(bContext * /*C*/, + wmDrag * /*drag*/, + const int /*xy*/[2], + wmDropBox * /*drop*/) { - return BLI_strdup(TIP_("Create object instance from object-data")); + return TIP_("Create object instance from object-data"); } static bool view3d_ima_drop_poll(bContext *C, wmDrag *drag, const wmEvent *event) @@ -726,10 +726,10 @@ static bool view3d_geometry_nodes_drop_poll(bContext *C, wmDrag *drag, const wmE return true; } -static char *view3d_geometry_nodes_drop_tooltip(bContext *C, - wmDrag * /*drag*/, - const int xy[2], - wmDropBox *drop) +static std::string view3d_geometry_nodes_drop_tooltip(bContext *C, + wmDrag * /*drag*/, + const int xy[2], + wmDropBox *drop) { ARegion *region = CTX_wm_region(C); int mval[2] = {xy[0] - region->winrct.xmin, xy[1] - region->winrct.ymin}; diff --git a/source/blender/windowmanager/WM_types.hh b/source/blender/windowmanager/WM_types.hh index 8a602fbd363..6d485ce5cb4 100644 --- a/source/blender/windowmanager/WM_types.hh +++ b/source/blender/windowmanager/WM_types.hh @@ -1224,10 +1224,10 @@ struct wmDragGreasePencilLayer { GreasePencilLayer *layer; }; -using WMDropboxTooltipFunc = char *(*)(bContext *C, - wmDrag *drag, - const int xy[2], - wmDropBox *drop); +using WMDropboxTooltipFunc = std::string (*)(bContext *C, + wmDrag *drag, + const int xy[2], + wmDropBox *drop); struct wmDragActiveDropState { wmDragActiveDropState(); diff --git a/source/blender/windowmanager/intern/wm_dragdrop.cc b/source/blender/windowmanager/intern/wm_dragdrop.cc index 00df8455b81..be311015904 100644 --- a/source/blender/windowmanager/intern/wm_dragdrop.cc +++ b/source/blender/windowmanager/intern/wm_dragdrop.cc @@ -417,18 +417,15 @@ void WM_drag_free_list(ListBase *lb) } } -static char *dropbox_tooltip(bContext *C, wmDrag *drag, const int xy[2], wmDropBox *drop) +static std::string dropbox_tooltip(bContext *C, wmDrag *drag, const int xy[2], wmDropBox *drop) { - char *tooltip = nullptr; if (drop->tooltip) { - tooltip = drop->tooltip(C, drag, xy, drop); + return drop->tooltip(C, drag, xy, drop); } - if (!tooltip && drop->ot) { - tooltip = BLI_strdup(WM_operatortype_name(drop->ot, drop->ptr).c_str()); + if (drop->ot) { + return WM_operatortype_name(drop->ot, drop->ptr); } - /* XXX Doing translation here might not be ideal, but later we have no more - * access to ot (and hence op context)... */ - return tooltip; + return {}; } static wmDropBox *dropbox_active(bContext *C, @@ -925,7 +922,7 @@ int WM_drag_get_path_file_type(const wmDrag *drag) /* ************** draw ***************** */ -static void wm_drop_operator_draw(const char *name, int x, int y) +static void wm_drop_operator_draw(const blender::StringRef name, int x, int y) { const uiFontStyle *fstyle = UI_FSTYLE_WIDGET; @@ -940,7 +937,7 @@ static void wm_drop_operator_draw(const char *name, int x, int y) UI_fontstyle_draw_simple_backdrop(fstyle, x, y, name, col_fg, col_bg); } -static void wm_drop_redalert_draw(const char *redalert_str, int x, int y) +static void wm_drop_redalert_draw(const blender::StringRef redalert_str, int x, int y) { const uiFontStyle *fstyle = UI_FSTYLE_WIDGET; const bTheme *btheme = UI_GetTheme(); @@ -1058,14 +1055,14 @@ static void wm_drag_draw_tooltip(bContext *C, wmWindow *win, wmDrag *drag, const int iconsize = UI_ICON_SIZE; int padding = 4 * UI_SCALE_FAC; - char *tooltip = nullptr; + std::string tooltip; if (drag->drop_state.active_dropbox) { tooltip = dropbox_tooltip(C, drag, xy, drag->drop_state.active_dropbox); } const bool has_disabled_info = drag->drop_state.disabled_info && drag->drop_state.disabled_info[0]; - if (!tooltip && !has_disabled_info) { + if (tooltip.empty() && !has_disabled_info) { return; } @@ -1095,9 +1092,8 @@ static void wm_drag_draw_tooltip(bContext *C, wmWindow *win, wmDrag *drag, const } } - if (tooltip) { + if (!tooltip.empty()) { wm_drop_operator_draw(tooltip, x, y); - MEM_freeN(tooltip); } else if (has_disabled_info) { wm_drop_redalert_draw(drag->drop_state.disabled_info, x, y);