2017-04-15 75 views
0

我知道,类似的问题已经在这里问:How to get a current font for GtkTextView?如何找到当前字体为GtkLabel的

gtk_style_context_get_font自3.8版本已被弃用,不应该在新编写的代码中使用。改为使用gtk_style_context_get()作为“字体”或子属性。

在这里,我卡住了。如何使用新推荐的技术来找出小部件的当前字体?

@Edit 一些代码:

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)); 

打印 '随机' 字符,因为pango_font_description_get_family返回NULL指针。

同时打印“不是一个字体”

回答

0

这仅仅是在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); 

见附近的行号:96和384

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

+0

应工作(理论上),但实际上当我打电话pango_font_description_get_family()上与font_desc我得到一个空指针。 – ProNOOB