After an evaluation, GNOME has moved from Bugzilla to GitLab. Learn more about GitLab.
No new issues can be reported in GNOME Bugzilla anymore.
To report an issue in a GNOME project, go to GNOME GitLab.
Do not go to GNOME Gitlab for: Bluefish, Doxygen, GnuCash, GStreamer, java-gnome, LDTP, NetworkManager, Tomboy.
Bug 747136 - Add convenience function for adding CSS style to a widget
Add convenience function for adding CSS style to a widget
Product: gtk+
Classification: Platform
Component: .General
Other All
: Normal enhancement
: ---
Assigned To: gtk-bugs
Depends on:
Reported: 2015-03-31 19:03 UTC by Emmanuele Bassi (:ebassi)
Modified: 2018-05-02 16:28 UTC
See Also:
GNOME target: ---
GNOME version: ---

widget: Convenience function for adding CSS style (4.01 KB, patch)
2015-03-31 19:03 UTC, Emmanuele Bassi (:ebassi)
none Details | Review

Description Emmanuele Bassi (:ebassi) 2015-03-31 19:03:44 UTC
See attached patch for a full description.
Comment 1 Emmanuele Bassi (:ebassi) 2015-03-31 19:03:49 UTC
Created attachment 300703 [details] [review]
widget: Convenience function for adding CSS style

It seems that one of the most requested functions for GtkWidget is being
able to add new CSS snippets on top of the existing style. This is
usually easily done just by using GtkCssProvider, but since people are
reimplementing this kind of API left and right, having it inside GTK+
may make sense.

The newly added gtk_widget_add_style() takes a CSS string, parses it
through GtkCssProvider, and adds the CSS provider instance to the
widget's style context, which then owns the GtkCssProvider instance.

Since users may want to remove the style later on, it seems easy to
return a pointer to the GtkCssProvider created by this function, than
figuring out a way to return a unique identifier for later removal, as
well as a way to map unique identifiers to GtkStyleProvider instances.

It is conceivable that a gtk_widget_replace_style() function may be
added in the future, which removes all the GtkStyleProviders associated
with a widget's style context, and replaces them with a new
GtkCssProvider that defines the whole of the CSS.
Comment 2 Emmanuele Bassi (:ebassi) 2015-03-31 19:16:27 UTC
An argument could be made that the function should be called `gtk_widget_add_style_from_string()`, and maybe have a `gtk_widget_add_style_from_resource()` as well, since application developers also ship with additional CSS in the form of files embedded in GResources. I do like the idea of a small, string-oriented function, though. You can still load the data in a GResource, get the bytes buffer, and then pass it to this function.

We could also use a `GError**` out argument, but I went with the `g_critical()` because of the programmatic nature of the function; you're not supposed to feed it random CSS, so there's no point in being recoverable. If you want to feed random CSS, then you should be using GtkCssProvider in the first place.
Comment 3 Emmanuele Bassi (:ebassi) 2016-05-26 11:42:13 UTC
Side note: this function could be used as the basis for re-implementing the various gtk_widget_modify_* deprecated API, because adding a new style provider to a style context does not change the style contexts of the children of the widget — though this may be not desirable in its own right.
Comment 4 Matthias Clasen 2016-05-26 12:58:13 UTC
Before we add more convenience functions like this, I think we should sort out the scope question - can we have style providers that apply to a widget and all its descendents ? Because I think that is what many people would expect from this api
Comment 5 GNOME Infrastructure Team 2018-05-02 16:28:28 UTC
-- GitLab Migration Automatic Message --

This bug has been migrated to GNOME's GitLab instance and has been closed from further activity.

You can subscribe and participate further through the new bug through this link to our GitLab instance: