lgi-devs / lgi

Dynamic Lua binding to GObject libraries using GObject-Introspection
MIT License
440 stars 70 forks source link

Gtk.StyleContext.add_provider_for_screen don't works properly? #252

Closed Miqueas closed 4 years ago

Miqueas commented 4 years ago

I think that I found a problem with this function. I have the following code:

Gui = Builder.objects
Styles = Gtk.CssProvider()
Styles:load_from_path("data/styles.css")

Gui.Window
  :get_style_context()
  :add_provider_for_screen(
    Gui.Window:get_screen(),
    Styles,
    Gtk.StyleProvider.PRIORITY_USER
  )

Then... add_provider_for_screen() takes the following arguments:

  1. GdkScreen
  2. GtkStyleProvider
  3. guint

Right? Well... When I run the code, I have the following error:

lua5.1: ./src/Gui.lua:10: bad argument #1 to 'add_provider_for_screen' (Gdk.Screen expected, got userdata)
stack traceback:
    [C]: in function 'add_provider_for_screen'
    ./src/Gui.lua:10: in main chunk
    [C]: in function 'require'
    main3.lua:2: in main chunk
    [C]: ?

But... Why? Technically, Gui.Window:get_screen() returns a Gdk.Screen (acording to the docs)... So... I think that is a problem of LGI, but I'm not sure. If isn't a problem of LGI, can anyone help me?

psychon commented 4 years ago

Well... do you happen to have a self-contained example I could play with?

However, I am not quite sure what you are doing. Let's assume that Gui.Window is a GtkWindow. Then :get_style_context() is gtk_widget_get_style_context() returning a GtkStyleContext. Next, you call :add_provider_for_screen(), which would be this function:

void
gtk_style_context_add_provider_for_screen
                               (GdkScreen *screen,
                                GtkStyleProvider *provider,
                                guint priority);

This function does not expect a GtkStyleProvider as its first argument, but you are calling with one due to the : in :add_provider_for_screen().

Thus, a random guess would be that the following works:

Gui = Builder.objects
Styles = Gtk.CssProvider()
Styles:load_from_path("data/styles.css")

Gui.Window
  :get_style_context()
  .add_provider_for_screen(
    Gui.Window:get_screen(),
    Styles,
    Gtk.StyleProvider.PRIORITY_USER
  )
Miqueas commented 4 years ago

Sorry for the delay.

Answering the question you asked at the beginning, basically what I want to do is apply CSS to all the widgets that need it. According to the documentation I have in DevHelp, that method works for that.

Everything you assumed is correct, however I hadn't thought about that solution you mentioned. I will try with that

EDIT: IT WORKS!!! Thanks @psychon for your help!