from GUI import Application, Window, Document, View, rgb, application

class TestDoc(Document):

    color = rgb(0.5, 0.5, 0.5)

    def set_color(self, c):
        self.color = c
        self.changed()
        self.notify_views()


class TestDocView(View):

    def draw(self, c):
        r = self.viewed_rect()
        #print "TestDocView.draw: viewed_rect =", r
        m = self.model
        c.forecolor = m.color
        c.fill_rect(self.viewed_rect())

    def mouse_down(self, e):
        l, t, r, b = self.viewed_rect()
        x, y = e.position
        red = float(x - l) / float(r - l)
        green = float(r - x) / float(r - l)
        blue = float(y - b) / float(t - b)
        self.model.set_color(rgb(red, green, blue))


doc = TestDoc()
view = TestDocView(model = doc, size = (200, 200))
win = Window(title = "Document", resizable = 0, size = (200, 200))
win.add(view)
win.document = doc
#print view.x, view.y, view.width, view.height
win.show()
application().run()
