帮朋友实现GTK2.X开发linux应用软件,发现Gtk的样式自定义官方文档说明不准确,就做了一下验证和摸索:
GtkWidget * button = gtk_button_new_with_label(g_locale_to_utf8("", -1, NULL, NULL, NULL));
gtk_widget_set_name(button, "rightGtkButton");
const char scroll_style[] =
"style \"view-scroll\" { bg[NORMAL] = \"#00FFFF\" }"
"class \"GtkButton\" style \"view-scroll\"" //标准类定义
"widget_class \"*GtkButton*\" style \"view-scroll\"" //支持标准类的通配模式
"widget \"*rightGtkButton*\" style \"view-scroll\"" //支持自定义widget名称(如rightGtkButton)
"widget \"*.rightGtkButton\" style \"view-scroll\""; //支持自定义widget名称(如rightGtkButton)
gtk_rc_parse_string(scroll_style);
samples:
void set_widget_style_bgcolor(GtkWidget* widget, const gchar* name, const gchar* bgcolor/*="#FF0000"*/)
{
if (name && strlen(name))
{
gchar* style = g_strdup_printf("style \"%s-style\" { bg[NORMAL] = \"%s\" }"
"widget \"*.%s\" style \"%s-style\"",
name, bgcolor, name, name);
if (style)
{
gtk_widget_set_name(widget, name);
gtk_rc_parse_string(style);
g_free(style);
}
}
}