Fabian Henze
Fabian Henze

Reputation: 997

Dynamically sizing a GtkLayout in PyGobject

I am looking for a way to automatically "zoom to fit" the content of a GtkLayout depending on the amount of space it is allowed to occupy.

This is my solution:

class MyLayout(Gtk.Layout):
    def __init__(self, document, **args):
        Gtk.Layout.__init__(self, **args)
        doc_width = 0
        doc_height = 0
        self.document = document

        # Determine the size of the document, I want to display
        for page in self.document.pages:
            doc_width = max(doc_width, page.width)
            doc_height += page.height
        self.aspect_ratio = doc_height / doc_width

        self.connect("draw", self.draw)

    def draw(self, widget, context):
        w = self.get_allocated_width()
        h = self.get_allocated_width()*self.aspect_ratio
        self.set_size(w, h) # sets the _content_ size, not the actual widget size
        print("draw")

Unfortunately this causes draw() to be called twice each time the window is resized, which is slow, once I actually need to draw something in the layout.

What is the correct solution for this problem?

Best regards,
Fabian Henze

SOLUTION: I found a solution some time ago. It's here and here in my project. I don't have the time to extract a minimal example from it. If anyone cares to do it, I would be happy to accept it as an answer.

Upvotes: 2

Views: 498

Answers (1)

shakaran
shakaran

Reputation: 11082

My guess is that this could be a problem with parent repainting the childs multiple times and triggered by hierarchy object.

I extend your example for show more verbose on draw flow.

from gi.repository import Gtk

class MyLayout(Gtk.Layout):
    def __init__(self, document, **args):
        Gtk.Layout.__init__(self, **args)
        doc_width = 0
        doc_height = 0
        self.document = document

        # Determine the size of the document, I want to display
        for page in self.document.pages:
            doc_width = max(doc_width, page.width)
            doc_height += page.height
        self.aspect_ratio = doc_height / doc_width

        self.connect("draw", self.draw)

    def do_realize(self):
        Gtk.Layout.do_realize(self)
        print ('do_realize')

    def do_show(self):
        Gtk.Layout.do_show(self)
        print ('do_show')

    def do_draw(self, widget):
        Gtk.Layout.do_draw(self, widget)
        print ('do_draw'), widget

    def draw(self, widget, context):
        print widget, context
        w = self.get_allocated_width()
        h = self.get_allocated_width() * self.aspect_ratio
        self.set_size(w, h) # sets the _content_ size, not the actual widget size
        print("draw")

class page(object):
    def __init__(self, width, height):
        self.width = width
        self.height = height

class document(object):
    def __init__(self):
        self.pages = [page(20, 10), page(40, 30)]

layout = MyLayout(document())
win = Gtk.Window()
layout.show()
win.add(layout)
win.show()
Gtk.main()

For me the ouput is:

do_show
do_realize
<MyLayout object at 0xb6b0ef54 (__main__+MyLayout at 0x898e950)> <cairo.Context object at 0xb73237f0>
draw
do_draw <cairo.Context object at 0xb73237f0>
<MyLayout object at 0xb6b0ef54 (__main__+MyLayout at 0x898e950)> <cairo.Context object at 0xb73237f0>
draw
do_draw <cairo.Context object at 0xb73237f0>
<MyLayout object at 0xb6b0ef54 (__main__+MyLayout at 0x898e950)> <cairo.Context object at 0xb73237f0>
draw
do_draw <cairo.Context object at 0xb73237f0>
<MyLayout object at 0xb6b0ef54 (__main__+MyLayout at 0x898e950)> <cairo.Context object at 0xb73237f0>
draw
do_draw <cairo.Context object at 0xb73237f0>
<MyLayout object at 0xb6b0ef54 (__main__+MyLayout at 0x898e950)> <cairo.Context object at 0xb73237f0>
draw
do_draw <cairo.Context object at 0xb73237f0>

I can see that draw signal is emitted before than do_draw internal widget function and it is triggered multiple times.

Upvotes: 1

Related Questions