Search code examples
backgroundgtkvala

How to draw a texture as background in gtk?


I want to add a texture as background in a gtk container, is it possible?

What I want is similar to the repeat-x repeat-y properties in css, but it's not supported in gtk yet, so, how to do it without any ugly hacks?. Another example is what nautilus have, where you can change the background.

thanks :)

pd:sorry 4 ma english


Solution

  • I did it this way:

        private bool draw_background (Cairo.Context cr) {
    
        int width = this.get_allocated_width ();
        int height = this.get_allocated_height ();
    
        cr.set_operator (Cairo.Operator.CLEAR);
        cr.paint ();
        cr.set_operator (Cairo.Operator.OVER);
    
        var background_style = this.get_style_context ();
        background_style.render_background (cr, 0, 0, width, height);
        background_style.render_frame (cr, 0, 0, width, height);
    
        var pat = new Cairo.Pattern.for_surface (new Cairo.ImageSurface.from_png (Build.PKGDATADIR + "/files/texture.png"));
        pat.set_extend (Cairo.Extend.REPEAT);
        cr.set_source (pat);
        cr.paint_with_alpha (0.6);
    
        return false;
    }