GTK4: gtk_style_context_get replacement

For rendering a custom widget I’ve used the above in GTK3 to retrieve e.g. the font to use from CSS. This allowed to keep the styling information out of the code. How would I do the same in GTK4? (I’ve also used gtk_style_context_set_{path,parent} but I think I can emulate that with gtk_widget_add_css_class() sufficiently well).

The purpose here is not to support external themes (I only care about Adwaita and HC) but rather to not have those things hardcoded in C.

There are quite a number of examples on Stackoverflow.

Just look for something.

Thanks for the answer but how does that apply to custom widgets where I want to retrieve the style properties to do custom rendering in the snapshot vfunc and therefore e.g. get the font via

  gtk_style_context_get (context, state, "font", &font, NULL);

The documentation says:

"Do not use widget style properties

Style properties do not exist in GTK 4. You should stop using them in your custom CSS and in your code."

So I conclude, that it is no longer possible to query the StyleContext of a widget hierarchy.

If necessary, you can manually create a corresponding resource, as shown in the examples given above.

"Do not use widget style properties

I’ve read the documentation and I think I have some idea how to apply CSS to widgets in general. My question is how I can query e.g. the effective pango font description that applies to a widget in a hierarchy for custom rendering. Can you point me where the examples you refer to do this?

GtkInspector can do it but uses GtkCssNode which is private.

“My question is how I can query e.g. the effective pango font description that applies to a widget…”

My proposal is to:

#include"pango-widget.h"


void activate (GtkApplication *app, gpointer data)
{
  GtkWidget *window;

  window =gtk_application_window_new(app);
  gtk_widget_set_size_request(window,50,50);

  PangoContext* pcontext = gtk_widget_get_pango_context (window);

  PangoFontDescription* description = pango_context_get_font_description (pcontext);

  const char* family = pango_font_description_get_family (description);

  g_print("family: %s \n",family);

 gtk_widget_set_visible(window,TRUE);
}

Further necessary information should also be extracted.

You don’t: you’re supposed to use GtkLabel (or GtkInscription).

“Custom rendering” and “styling” are orthogonal: either you render custom content, or you get CSS integration by using widgets.

…ahh missed that one. Thanks!

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