How to access the theming Fonts and Colors on GTK/

2020-07-26 11:51发布

问题:

Lets say i want to write a special text editor widget.

How can i get the default themed colors for texts, selected text and background and which are the users default fonts?

I see that GNOME does define 5 special system fonts and default sizes for this purpose in the GNOME Appearance Configuration dialog, but i haven't found a single word in the GTK documentation how to access them (and the GTK mailing list is a joke:-( ).

Windows and Cocoa both give me dozens of system values.

I found the GtkStyle class but this doesn't seem to be what i need.

回答1:

For the default colors, use something like this:

GdkColor color;
/* Look up the default text color in the theme, use a default 
if it's not defined */
GtkStyle *style = gtk_rc_get_style(my_widget);
if(!gtk_style_lookup_color(style, "text_color", &color))
    gdk_color_parse("black", &color);

There are several names defined for gtk_style_lookup_color(). It's a bit unclear where exactly they are defined, but these are the ones you can define in the GNOME dialog:

  • fg_color
  • bg_color
  • base_color
  • text_color
  • selected_bg_color
  • selected_fg_color
  • tooltip_bg_color
  • tooltip_fg_color

As for the fonts and other system settings, you need to use the GConf library to get these defaults. GTK knows nothing about them, because they're part of the GNOME desktop, not GTK. The default font can be found at the key /desktop/gnome/interface/font_name, for example. If you install the GConf configuration editor, you can browse these keys to see which ones are available; they're all under /desktop/gnome.

PS. What GTK mailing list did you ask on? The one I read doesn't seem to be a joke...