sqram
sqram

Reputation: 7201

Repainting cairo windows?

I have ran into a problem that i suspect has to do painting/drawing elements in cairo.

I have a borderless window in pygtk, but i draw two rectangles with cairo.a black rectangle, and a grey rectangle inside. When the window is resized, there seems to be parts of the inner rectangle doesn't get drawn/painted. I have included 3 screenshots to show this issue.

enter image description here enter image description here enter image description here

As you can see in the second and third picture, some pieces of the window do not get painted grey. One way to fix this, is to call pygtk's window's present() method..but this makes my program extrmely slow, as the height of the window changes with pretty much each keystroke. So i was wondering what alternatives i have to fix this.

below is the relevant cairo code i use

def expose(self, widget, e):
    cr = widget.window.cairo_create()

    # Draw the background
    cr.set_operator(cairo.OPERATOR_SOURCE)

    # Create black rectangle with 60% opacity (serves as border)
    (width, height) = widget.get_size()
    cr.set_source_rgba(0, 0, 0, 0.6)
    cr.rectangle(0, 0, width, height)
    cr.fill()

    # Inside the black rectangle, put a lighter one (will hold widgets)
    (width, height) = widget.get_size()
    cr.set_source_rgb(205/255, 205/255, 193/255)
    cr.rectangle(10, 10, width-20, height-20)
    cr.fill()   

    return False

def screen_changed(self, widget, old_screen = None):
    screen = widget.get_screen()
    colormap = screen.get_rgba_colormap()
    widget.set_colormap(colormap)

Upvotes: 3

Views: 575

Answers (2)

Søren Sandmann
Søren Sandmann

Reputation: 539

It's basically a GTK+ bug, I believe. When a window is resized, GTK+ doesn't always queue the entire window for repainting. As a workaround, you can call window.queue_draw() at the place where you cause the window to be resized.

Upvotes: 2

Tucif
Tucif

Reputation: 11

Try using the following right after you create your cairo widget:

cr.set_source_rgb(0,0,0)
cr.paint()

This will ensure you are always having a clean canvas.

Upvotes: 1

Related Questions