Get the color of the current theme for a custom widget

There is a really nice circular progressbar here.

It defaults to a blue outer circle, which does not match with everybody’s user theme (think about distributing this widget to other end users). Is there a theme agnostic way of selecting the current theme color?

I’ve searched a lot, and I can’t find how to get the current color of the theme. There’s all kinds of answers on how to set and edit the colors of your theme, but I can’t find a working, non-deprecated way to get the color of the users theme.

“The color of the current theme” does not make sense in a CSS context. A widget can have a completely different style depending on the parent widget or a sibling widget or on state, etc.

Basically, the theme needs to support the widget for the widget to look good.

Then let us say that this is a normal progressbar. How would one go about getting the colors and states for this widget?

You don’t. There is no “color” in CSS, only texture layers—colors are textures with solid fills—so you cannot ask for something that does not exist.

The way the style system works is that you ask GTK to draw according to the current style state, using the appropriate gtk_render_*() function. You don’t query the state and then draw directly with some rendering API, like you did in GTK1 or GTK2.

All GTK widgets work the same way. If you’re drawing something like a progress bar, you’ll likely want to use gtk_render_background() with the whole allocation of the widget to draw the trough, then gtk_render_frame() using the filled portion of the allocation to render the progress. You will need to select the appropriate CSS classes and states so that you can create a progress bar styled like GTK’s own—or you could use GtkProgressbar instead of your own custom widget.

Thanks, @ebassi. This was not the answer I was looking for, but definitely more upbeat than a “You can’t do that, you idiot.”

I’m going to push my luck here a bit, and ask what the possibility is that a circular progress bar patch would be accepted by the Gtk team? Let’s say this would be implemented in C…

GTK3 is feature frozen, so no new widgets. In general, though, we only add widgets after three or more separate users have experimented with a shared class and ironed out the kinks in the API.

Nautilus has a circular progress button that you can probably look at; it does not use the same colour as the stock progress bar, though.

As a rule of thumb: if you can do it in CSS on a web page, you should be able to do it in GTK, and if you can’t, we can consider adding functionality to let you do that.

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