I’m making a custom widget, which is a switch that can display icons:
I’m drawing it using the snapshot() of the widget. Since it’s all custom painting, I need foreground and background colors to paint it with. I’m currently using StyleContext lookup_color, which is deprecated. I’m aware of GtkWidget.color and it would suit my needs perfectly, however it only covers the foreground color. I’d still need a way to get a background color.
There used to be render_background for that purpose, but it was also deprecated.
I saw this paragraph in the gtk source:
The best way to render parts of your widget using CSS styling
is to use subwidgets. For example, to show a piece of text with
fonts, effects and shadows according to the current CSS style,
use a GtkLabel.
But I’m not sure how could I do that in my current context. I need a background color, even if I use a subwidget, I only know how to get its foreground color?
thank you for the answer! However it’s clear that I didn’t properly formulate my question and so this doesn’t help me. I included a screenshot of the widget I’ve build in my original post, and you can see it has four “layers”:
the background
the blue background of the slider
the white knob of the slider
the black icon on top of the knob
When I mentioned the background I was actually aiming at #2. Or maybe #3… Yeah, my question was not formulated in a great manner.
So what my code is currently doing is… I’m not drawing #1, as you say. For #2, I’m using “accent_bg_color”, which I’m fetching through the style context (deprecated API!). For #3 I’m using “accent_fg_color”, which I’m again fetching through the deprecated style context API. And for #4 I currently hardcoded black. Maybe I should change #4 to the widget foreground color…
But without the style context API I don’t see any other way than to hardcode colors in the widget, because it seems to me I can only get one color from the CSS, the foreground color. I could also get the background color as you point out, but I need more colors than that…
So I’m not sure how to do that maybe even now, but it’s even worse when style context is deprecated
I had actually seen mention of that pattern in the documentation, but I don’t exactly picture how could it work. Is there an example of the use of this pattern (including in the gtk source code) anywhere so that I could use that as inspiration?
alright thank you. I had already peeked but I’m often afraid that such builtin widgets use APIs that are not exported outside of gtk. I’ll review this and thanks to @gwillems infos I’m much more optimistic now!
The only private widget used by GtkSwitch is GtkGizmo, which is a plain GtkWidget subclass that doesn’t do anything except provide an empty area that gets styled by CSS. You can achieve the same result with an empty GtkLabel or a GtkImage.