New function width_of_m to get the width of M rendered in a widget.
authorJohn Darrington <john@darrington.wattle.id.au>
Sun, 20 Dec 2020 19:11:10 +0000 (20:11 +0100)
committerJohn Darrington <john@darrington.wattle.id.au>
Sun, 20 Dec 2020 21:55:40 +0000 (22:55 +0100)
helper.c (width_of_m): New function.

src/ui/gui/helper.c
src/ui/gui/helper.h
src/ui/gui/psppire-data-sheet.c

index cde9d92b8f186a29525a00b24fb55e09b3707771..0737d2fb7b08a8401b46a167a866eccac6afa818 100644 (file)
@@ -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;
+}
+
index 8d18fcf4b44e4b318d5bb29eb4686f389d77afa7..69841a5c491d95b42a504e02d37d6ee54851012b 100644 (file)
@@ -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
 
index 728604d5138db9afdc2ac14d87a09cb1a515fd47..c3e1fc2b2465f0e60c4cfd95239212d9ef8da894 100644 (file)
@@ -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);