1

I know that similar question was already asked here: How to get a current font for GtkTextView?

But

gtk_style_context_get_font has been deprecated since version 3.8 and should not be used in newly-written code. Use gtk_style_context_get() for "font" or subproperties instead.

And here I am stuck. How to use the new recommended technique to find out the current font for the widget?

@Edit Some code:

PangoFontDescription *font_desc;
GtkStyleContext *style;
GdkRGBA fore_color;

font_desc = pango_font_description_new ();
style = gtk_widget_get_style_context (base);
gtk_style_context_get_color (style, GTK_STATE_FLAG_NORMAL, &fore_color);

gtk_style_context_save (style);
gtk_style_context_set_state (style, 0);
gtk_style_context_get (style, GTK_STATE_FLAG_NORMAL, "font", font_desc, NULL);
gtk_style_context_restore (style);

if (G_TYPE_CHECK_INSTANCE_TYPE ((font_desc), PANGO_TYPE_FONT_DESCRIPTION)) {
  printf("%s\n", "Is a font");
} else {
  printf("%s\n", "Not a font");
}

printf("%s\n", pango_font_description_get_family (font_desc));

prints 'random' characters because pango_font_description_get_family returns a NULL pointer.

Also prints 'Not a font'

Community
  • 1
  • 1
ProNOOB
  • 504
  • 2
  • 14

1 Answers1

1

This is just a quick search on Github.

PangoFontDescription *font_desc;
GtkStyleContext *style_context;
style_context = gtk_widget_get_style_context (widget);
gtk_style_context_save (style_context);
gtk_style_context_set_state (style_context, 0);
gtk_style_context_get (style_context, 
     gtk_style_context_get_state(style_context), "font", &font_desc, NULL);
gtk_style_context_restore (style_context);

See near line numbers: 96 and 384

at https://github.com/jessevdk/libgd/blob/master/libgd/gd-two-lines-renderer.c

  • Should work (In theory) but in fact when I call pango_font_description_get_family () on &font_desc I get a NULL pointer. – ProNOOB Apr 15 '17 at 19:28