From 106d2bb3120b579fdf11336baf4e1cf8669bc53b Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Sun, 17 Sep 2023 09:01:45 +1000 Subject: [PATCH] Cleanup: document ownership for uiTooltipField, add function attributes - Document ownership. - Make some arguments const. - Use function style casts. - Make width/height into a size[2]. --- .../blender/editors/include/UI_interface_c.hh | 15 ++++++-- .../interface/interface_region_tooltip.cc | 38 +++++++++---------- 2 files changed, 30 insertions(+), 23 deletions(-) diff --git a/source/blender/editors/include/UI_interface_c.hh b/source/blender/editors/include/UI_interface_c.hh index 19d7016a9c3..dc85266848e 100644 --- a/source/blender/editors/include/UI_interface_c.hh +++ b/source/blender/editors/include/UI_interface_c.hh @@ -1774,17 +1774,24 @@ void UI_but_func_tooltip_custom_set(uiBut *but, void *arg, uiFreeArgFunc free_arg); +/** + * \param text: Allocated text (transfer ownership to `data`) or null. + * \param suffix: Allocated text (transfer ownership to `data`) or null. + */ void UI_tooltip_text_field_add(struct uiTooltipData *data, char *text, char *suffix, const uiTooltipStyle style, const uiTooltipColorID color_id, - const bool is_pad = false); + const bool is_pad = false) ATTR_NONNULL(1); +/** + * \param image: Image buffer (duplicated, ownership is *not* transferred to `data`). + * \param image_size: Display size for the image (pixels ) + */ void UI_tooltip_image_field_add(struct uiTooltipData *data, - struct ImBuf *image, - short width, - short height); + const struct ImBuf *image, + const short image_size[2]) ATTR_NONNULL(1, 2, 3); /** * Recreate tool-tip (use to update dynamic tips) diff --git a/source/blender/editors/interface/interface_region_tooltip.cc b/source/blender/editors/interface/interface_region_tooltip.cc index aeef3400239..9b7c27d7643 100644 --- a/source/blender/editors/interface/interface_region_tooltip.cc +++ b/source/blender/editors/interface/interface_region_tooltip.cc @@ -80,8 +80,10 @@ struct uiTooltipFormat { }; struct uiTooltipField { - char *text; - char *text_suffix; + /** 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; struct { /** X cursor position at the end of the last line. */ uint x_pos; @@ -90,8 +92,7 @@ struct uiTooltipField { } geom; uiTooltipFormat format; ImBuf *image; - short image_width; - short image_height; + short image_size[2]; }; struct uiTooltipData { @@ -125,21 +126,20 @@ void UI_tooltip_text_field_add(uiTooltipData *data, field->format.style = style; field->format.color_id = color_id; field->format.is_pad = is_pad; - field->text = text ? text : nullptr; - field->text_suffix = suffix ? suffix : nullptr; + field->text = text; + field->text_suffix = suffix; } void UI_tooltip_image_field_add(uiTooltipData *data, - struct ImBuf *image, - short width, - short height) + const struct 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_width = MIN2(width, UI_TIP_MAXIMAGEWIDTH * UI_SCALE_FAC); - field->image_height = MIN2(height, UI_TIP_MAXIMAGEHEIGHT * UI_SCALE_FAC); + 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; } @@ -262,8 +262,8 @@ static void ui_tooltip_region_draw_cb(const bContext * /*C*/, ARegion *region) } else if (field->format.style == UI_TIP_STYLE_IMAGE) { - bbox.ymax -= field->image_height; - bbox.ymin -= field->image_height; + bbox.ymax -= field->image_size[0]; + bbox.ymin -= field->image_size[1]; GPU_blend(GPU_BLEND_ALPHA_PREMULT); IMMDrawPixelsTexState state = immDrawPixelsTexSetup(GPU_SHADER_3D_IMAGE_COLOR); @@ -277,8 +277,8 @@ static void ui_tooltip_region_draw_cb(const bContext * /*C*/, ARegion *region) field->image->byte_buffer.data, 1.0f, 1.0f, - (float)field->image_width / (float)field->image->x, - (float)field->image_height / (float)field->image->y, + float(field->image_size[0]) / float(field->image->x), + float(field->image_size[1]) / float(field->image->y), NULL); GPU_blend(GPU_BLEND_ALPHA); @@ -313,10 +313,10 @@ static void ui_tooltip_region_free_cb(ARegion *region) for (int i = 0; i < data->fields_len; i++) { const uiTooltipField *field = &data->fields[i]; if (field->text) { - MEM_freeN(field->text); + MEM_freeN((void *)field->text); } if (field->text_suffix) { - MEM_freeN(field->text_suffix); + MEM_freeN((void *)field->text_suffix); } if (field->image) { IMB_freeImBuf(field->image); @@ -1245,8 +1245,8 @@ static ARegion *ui_tooltip_create_with_data(bContext *C, } if (field->format.style == UI_TIP_STYLE_IMAGE) { - fonth += field->image_height; - w = field->image_width; + fonth += field->image_size[1]; + w = field->image_size[0]; } fontw = max_ii(fontw, w);