How and when do you actually get theme colours?

There is quite a bit of documentation of the process that I have read but its not exactly clear about what you ask for and when. What I’m using now is this code:

GdkRGBA normal;
GdkRGBA active;
GdkRGBA select;
GtkStyleContext *style = gtk_widget_get_style_context (wgt);
gtk_style_context_get_color (style, GTK_STATE_FLAG_NORMAL, &normal);
gtk_style_context_get_color (style, GTK_STATE_FLAG_ACTIVE, &active);
gtk_style_context_get_color (style, GTK_STATE_FLAG_SELECTED, &select);

This is running inside the realise callback of list box control, so in this case wgt is that listbox. However, the colours returned are all the same shade of near white. I’m using Qogir dark which uses several shades of blue and grey but all I’m getting is a light grey.

I’m trying to draw in a GtkDrawingArea where some things are selected and some are active. I did consider using gtk_style_context_get_background_color but the documentation makes it clear that shouldn’t be used. So I’m getting nowhere for the moment.

That’s not how the theming system in GTK works. Not every UI element has a color as its background—in some cases it’s a gradient, in others it’s an image, in others it’s just transparent with an inset/outset shadow. You cannot “ask” for a color.

You either ignore the theme entirely, or you use the gtk_render_*() family of functions, which use the CSS state appropriately, depending on the style classes associated to a widget.

Unfortunately the things I’m trying to draw don’t really fit any of the gtk_render_ functions. Is there no way I can draw a custom control and fit the user’s theme?

Not unless you use the gtk_render_* API, no. That’s how GTK widgets are drawn, and that’s how the style system is applied.

That’s a bit of a downer but your quick response is appreciated all the same.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.