From edfa7450c198af7cf6a69760e082d1f36f88cc58 Mon Sep 17 00:00:00 2001 From: John Darrington Date: Sun, 20 Dec 2020 20:11:10 +0100 Subject: [PATCH] New function width_of_m to get the width of M rendered in a widget. helper.c (width_of_m): New function. --- src/ui/gui/helper.c | 20 ++++++++++++++++++++ src/ui/gui/helper.h | 5 +++++ src/ui/gui/psppire-data-sheet.c | 16 +++------------- 3 files changed, 28 insertions(+), 13 deletions(-) diff --git a/src/ui/gui/helper.c b/src/ui/gui/helper.c index cde9d92b8f..0737d2fb7b 100644 --- a/src/ui/gui/helper.c +++ b/src/ui/gui/helper.c @@ -230,3 +230,23 @@ paste_syntax_to_window (gchar *syntax) return syntax; } + + +/* Return the width of an upper case M (in pixels) when rendered onto + WIDGET with its current style. */ +gdouble +width_of_m (GtkWidget *widget) +{ + PangoContext *context = gtk_widget_create_pango_context (widget); + PangoLayout *layout = pango_layout_new (context); + PangoRectangle rect; + + pango_layout_set_text (layout, "M", 1); + pango_layout_get_extents (layout, NULL, &rect); + + g_object_unref (G_OBJECT (layout)); + g_object_unref (G_OBJECT (context)); + + return rect.width / (gdouble) PANGO_SCALE; +} + diff --git a/src/ui/gui/helper.h b/src/ui/gui/helper.h index 8d18fcf4b4..69841a5c49 100644 --- a/src/ui/gui/helper.h +++ b/src/ui/gui/helper.h @@ -73,5 +73,10 @@ psppire_box_pack_start_defaults (GtkBox *box, GtkWidget *widget) implementations rely on this. */ #define GFUNC_COMPAT_CAST(x) ((GFunc) (void (*)(void)) (x)) + +/* Return the width of an upper case M (in pixels) when rendered onto + WIDGET with its current style. */ +gdouble width_of_m (GtkWidget *widget); + #endif diff --git a/src/ui/gui/psppire-data-sheet.c b/src/ui/gui/psppire-data-sheet.c index 728604d513..c3e1fc2b24 100644 --- a/src/ui/gui/psppire-data-sheet.c +++ b/src/ui/gui/psppire-data-sheet.c @@ -28,6 +28,7 @@ #include "ui/gui/executor.h" #include "psppire-data-window.h" #include "ssw-axis-model.h" +#include "helper.h" static void do_sort (PsppireDataSheet *sheet, GtkSortType order) @@ -434,7 +435,6 @@ button_post_create (GtkWidget *button, guint i, gpointer user_data) g_signal_connect_after (button, "draw", G_CALLBACK (indicate_filtered_case), data_store); } - static gboolean resize_display_width (PsppireDict *dict, gint pos, gint size, gpointer user_data) { @@ -442,19 +442,9 @@ resize_display_width (PsppireDict *dict, gint pos, gint size, gpointer user_data return FALSE; PsppireDataSheet *sheet = PSPPIRE_DATA_SHEET (user_data); - PangoContext *context = gtk_widget_create_pango_context (GTK_WIDGET (sheet)); - PangoLayout *layout = pango_layout_new (context); - PangoRectangle rect; - - pango_layout_set_text (layout, "M", 1); - pango_layout_get_extents (layout, NULL, &rect); - - gdouble width_of_M = rect.width / (gdouble) PANGO_SCALE; - - g_object_unref (G_OBJECT (layout)); - g_object_unref (G_OBJECT (context)); + gdouble wm = width_of_m (GTK_WIDGET (sheet)); - gint Ms = round ((size / width_of_M) - 0.25); + gint Ms = round ((size / wm) - 0.25); struct variable *var = psppire_dict_get_variable (dict, pos); g_return_val_if_fail (var, TRUE); var_set_display_width (var, Ms); -- 2.30.2