What good is GtkCssProvider without GtkStyleContext?

GtkStyleContext has been deprecated in gtk4. GtkCssProvider is not deprecated.

shows how to use the C++ equivalents of gtk_css_provider_load_from_file() and
gtk_style_context_add_provider() to add a CSS file to an application.
This application is quite useless in itself. It just aims at showing how
GtkCssProvider and GtkStyleContext can be used.

When GtkStyleContext is deprecated, can an application still use GtkCssProvider
in a meaningful way without using deprecated API? Is the use of GtkCssProvider and
GtkStyleContext in the example program in gtkmm-documentation a bad example that should
be removed? It actually was more useful in gtk3/gtkmm3 than it is in gtk4/gtkmm4.

Deprecation does not imply removal within the same major API; it’s mainly an hint as to what will happen with the next major API.

In this particular case, though, you never should have used gtk_style_context_add_provider() to add custom CSS styling; the appropriate function is gtk_style_context_add_provider_for_display(), which is not deprecated and does not use the GtkStyleContext type.

The main difference is that, unlike gtk_style_context_add_provider_for_display(), gtk_style_context_add_provider() will add a style provider for the given style context only; there will be no cascading into the children of the style context. This is not really expected, as it will not allow you to use styling as you’d do it on the web. Your application should define all the style classes it uses, load them all into a single CSS style sheet, and then apply it globally.

The other thing that was deprecated was the API to query the CSS machinery; this API was incompatible with how CSS works, and was mainly meant as a way to help porting GTK2 applications poking at the style information directly to GTK3. Applications should not query the style information: you should either use GTK widgets to draw stylable UI elements, or you should draw things that should not be styled at all.

1 Like

Thanks! I hadn’t noticed that gtk_style_context_add_provider_for_display() and
gtk_style_context_remove_provider_for_display() have been moved from gtkstylecontext.h
to gtkstyleprovider.h.

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