Re: ‘gtk_widget_override_background_color’ is deprecated



Hi;

On 18 August 2016 at 21:21, rastersoft <raster rastersoft com> wrote:

Gtk.Widget.override_background is deprecated. How should I replace it? I
read something about CSS, but not sure how to do it...

Yes, the function is deprecated — though GTK tries to make it limp along.

The quick answer is: widgets do not have a "background color" any
more; that was a GTK+ 2.x concept that, for porting purposes, was left
in the 3.0 API.

Widgets use CSS to describe their background; CSS has fairly complex
rules to determine what a background is and how it's drawn. A CSS
background may contain a color — even though it's considered an image
with a solid color throughout. That's also the reason why you cannot
query the background color any more.

If you want to have widgets with different backgrounds depending on
some semantic or programmatic condition, you should decide what those
conditions are and create a CSS fragment that defines those colors as
CSS classes, e.g.

    .red-background { background-image: none; background-color: red; }
    .yellow-background { background-image: none; background-color:
rgb(255, 255, 0); }
    …

Then use a GtkCssProvider and associate it to the screen so that it is
shared among all widgets in your window; I'm going to use Python for
brevity, but the API should be immediately translatable to other
languages:

    provider = Gtk.CssProvider()
    provider.load_from_file(your_css_fragment)
    Gtk.StyleContext.add_provider_for_screen(Gdk.Screen.get_default(),
provider, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION)

You should do this only once, when your application starts up; if
you're using Gtk.Application, the "startup" virtual function is the
most appropriate.

Now, every time you want to set the color of a widget that supports
backgrounds, you need to use Gtk.Widget.get_style_context() to
retrieve the Gtk.StyleContext instance associated to the widget, and
call Gtk.StyleContext.add_class() to add one of the classes that you
defined in your CSS fragment, e.g.:

  your_widget.get_style_context().add_class('red-background')

The same code is used to define additional, application-specific
styling, not just for background colors. If you want to change fonts,
or borders, or margins, you can load them in an application-specific
CSS fragment.

It gets a bit more complicated if you want to do things like letting
the user select a color and draw widgets with that color. You can
generate a CSS fragment programmatically, in that case, and instead of
attaching the Gtk.CssProvider to the global list of providers you can
attach it directly to the Gtk.StyleContext of a widget, e.g.:

  provider = Gtk.CssProvider()
  provider.load_from_data(some_css_fragment)
  widget.get_style_context().add_provider(provider,
Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION)

and then:

  widget.get_style_context().add_class(some_class)

Once you want to reset the widget's style, you call:

  widget.get_style_context().remove_provider(provider)

The main difference with the example above is that the CSS defined by
the style provider will *only* apply to the widget and not cascade
into its children.

Ciao,
 Emmanuele.

-- 
https://www.bassi.io
[@] ebassi [@gmail.com]


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]