From f5c57ff8218daa9d4a7513d2190a4a7c32eba3aa Mon Sep 17 00:00:00 2001 From: Dave Davenport Date: Tue, 23 May 2017 09:12:04 +0200 Subject: [PATCH] [TextBox] Add get_desired_width function. --- include/widgets/textbox.h | 1 + source/widgets/textbox.c | 14 ++++++++++++++ 2 files changed, 15 insertions(+) diff --git a/include/widgets/textbox.h b/include/widgets/textbox.h index b25b4c02..fc75baa7 100644 --- a/include/widgets/textbox.h +++ b/include/widgets/textbox.h @@ -290,5 +290,6 @@ PangoAttrList *textbox_get_pango_attributes ( textbox *tb ); * @returns the visible text. */ const char *textbox_get_visible_text ( const textbox *tb ); +int textbox_get_desired_width ( widget *wid ); /*@}*/ #endif //ROFI_TEXTBOX_H diff --git a/source/widgets/textbox.c b/source/widgets/textbox.c index 48c03084..c0761f11 100644 --- a/source/widgets/textbox.c +++ b/source/widgets/textbox.c @@ -797,3 +797,17 @@ int textbox_get_estimated_height ( const textbox *tb, int eh ) int height = pango_font_metrics_get_ascent ( tb->metrics ) + pango_font_metrics_get_descent ( tb->metrics ); return ( eh * height ) / PANGO_SCALE + widget_padding_get_padding_height ( WIDGET ( tb ) ); } +int textbox_get_desired_width ( widget *wid ) +{ + textbox *tb = (textbox *) wid; + unsigned int offset = ( tb->flags & TB_INDICATOR ) ? DOT_OFFSET : 0; + if ( tb->flags & TB_AUTOWIDTH ) { + return textbox_get_font_width ( tb ) + widget_padding_get_padding_width ( wid ) + offset; + } + int width = 0; + pango_layout_set_width ( tb->layout, -1); + width = textbox_get_font_width ( tb ); + // Restore. + pango_layout_set_width ( tb->layout, PANGO_SCALE * ( tb->widget.w - widget_padding_get_padding_width ( WIDGET ( tb ) ) - offset ) ); + return width + widget_padding_get_padding_width ( wid ) + offset; +}