tuikit/container.py
author Radek Brich <radek.brich@devl.cz>
Sun, 31 Jul 2011 13:04:39 +0200
changeset 9 7175ed629a76
parent 5 ae128c885d0f
child 13 19ebde2fd594
permissions -rw-r--r--
Added ComboBox, HorizontalLayout, TreeNode, TreeModel, TreeView. Widget is now descendant of EventSource. Improved color management (color prefixes).

# -*- coding: utf-8 -*-

from .widget import Widget
import logging

class Container(Widget):
    def __init__(self, width = 10, height = 10):
        Widget.__init__(self, width, height)

        self.children = []
        self.mousechild = None

        self.borders = (0, 0, 0, 0) # left, top, right, bottom
        self.widthrequest = (None, None)
        self.heightrequest = (None, None)

        self.colorprefix = None

        self.trapfocus = False  # if True, tab cycles inside container


    def add(self, widget, **kw):
        self.children.append(widget)
        widget.parent = self
        widget.settop(self.top)
        widget.layouthints.update(kw)


    def layout(self, layout):
        self.layout = layout
        layout.container = self
        self.connect('resize', layout.resize)


    def settop(self, top):
        self.top = top
        for child in self.children:
            child.settop(top)


    def focusnext(self):
        i = self.children.index(self.top.focuswidget)
        while True:
            i += 1
            if i >= len(self.children):
                i = 0
            if self.children[i].canfocus():
                self.children[i].setfocus()
                break
        log = logging.getLogger('tuikit')
        log.debug(str(self.top.focuswidget.__class__))


    def keypress(self, keyname, char):
        if keyname == 'tab':
            self.focusnext()
            return
        Widget.keypress(self, keyname, char)


    def resize(self):
        Widget.resize(self)
        for child in self.children:
            child.emit('resize')


    def draw(self, screen, x=0, y=0):
        if self.hidden:
            return

        screen.pushclip(x, y, self.width, self.height)
        if self.colorprefix:
            screen.pushcolorprefix(self.colorprefix)

        Widget.draw(self, screen, x, y)

        for child in [x for x in self.children if not x.allowlayout]:
            child.draw(screen, x + child.x, y + child.y)

        l, t, r, b = self.borders
        screen.pushclip(x+l, y+t, self.width-l-r, self.height-t-b)

        for child in [x for x in self.children if x.allowlayout]:
            child.draw(screen, x + child.x, y + child.y)

        screen.popclip()

        if self.colorprefix:
            screen.popcolorprefix()
        screen.popclip()


    def mousedown(self, ev):
        handled = False
        for child in reversed(self.children):
            if child.enclose(ev.wx, ev.wy):
                childev = ev.childevent(child)
                child.mousedown(childev)
                self.mousechild = child
                handled = True
                break
        if not handled:
            self.setfocus()
            self.handle('mousedown', ev)


    def mouseup(self, ev):
        if self.mousechild:
            childev = ev.childevent(self.mousechild)
            self.mousechild.mouseup(childev)
            self.mousechild = None
        else:
            self.handle('mouseup', ev)
        #handled = False
        #for child in self.children:
            #if child.enclose(ev.wx, ev.wy):
                #childev = ev.childevent(child)
                #child.mouseup(childev)
                #self.mousechild = child
                #handled = True
        #if not handled:
            #self.handle('mouseup', ev)


    def mousemove(self, ev):
        if self.mousechild:
            childev = ev.childevent(self.mousechild)
            self.mousechild.mousemove(childev)
        else:
            self.handle('mousemove', ev)


    def mousewheel(self, ev):
        handled = False
        for child in reversed(self.children):
            if child.enclose(ev.wx, ev.wy):
                childev = ev.childevent(child)
                child.mousewheel(childev)
                handled = True
                break
        if not handled:
            self.handle('mousewheel', ev)