Exemplo n.º 1
0
    def setSize(self, y, x, h, w):
        """
        calculate children's sizes, then call setSize on each of them
        """
        super(RowLayout, self).setSize(y, x, h, w)

        # this is the case when we're being resized before
        # the Engine is initialized
        if y == 0 and x == 0 and h == 0 and w == 0:
            return

        self.log.debug('setSize(%d, %d, %d, %d)' % (y, x, h, w))

        # use the values from parent's setSize()
        y = self.y
        x = self.x
        h = self.h
        w = self.w

        # start from available height
        available = h

        # adjust for borders, if they're to be drawn
        if self.outerborder:
            available -= 2

            # pushes the starting y down by 1
            y += 1
            
            # outer border will also shrink our available horizontal area
            x += 1
            w -= 2

        if self.innerborder:
            available -= (len(self.cells) - 1)


        items = [(item._meta['fixedsize'], item._meta['weight']) for item in self.cells]

        sizes = util.flexSize(items, available)

        for (child, size) in zip(self.cells, sizes):
            child._meta['primary_dim'] = size

            if isinstance(child, Drawable):
                self.log.debug('setting size of "%s" to (%d, %d, %d, %d)', child.name, y, x, child._meta['primary_dim'], w)
                child.setSize(y, x, child._meta['primary_dim'], w)

            y += child._meta['primary_dim']
            if self.innerborder:
                y += 1
Exemplo n.º 2
0
    def render(self):
        """
        if this gets called, then one of our functions has indicated
        that it's time for a redraw (through self.touch()), so we're
        going to update and return the window object for the Engine to
        draw.
        """

        # figure out column widths
        if self.format is None:
            self.format = ''

            availwidth = self.w - ((len(self.cols) - 1) * self.spacing)

            sizeitems = [(c.fixedsize, c.weight) for c in self.cols]
            sizes = util.flexSize(sizeitems, availwidth)

            for (col, size) in zip(self.cols, sizes):
                col.width = size

                self.format += '%'
                if col.alignment == 'left':
                    self.format += '-'
                self.format += ('%d' % col.width) + ('.%d' % col.width) + 's'
                self.format += ' ' * self.spacing

            # trim off trailing space
            if self.spacing:
                self.format = self.format[:len(self.format) - self.spacing]

        offset = 0
        
        # max([None, None, ...]) will evaluate False
        if max(self.colnames):
            # then we must render the header
            self.draw(self.format % tuple(map(lambda x: x or '', self.colnames)), 0, 0, bold = True)
            self.line(1, 0, self.w)

            offset = 2
        
        # the effective height, less the offset (if there's a header)
        height = self.h - offset

        # update firstVisible to so that currently highligted item
        # is visible
        if self.highlighted >= self.firstVisible + height:
            self.firstVisible = self.highlighted - height + 1
        elif self.highlighted < self.firstVisible:
            self.firstVisible = self.highlighted

        for i in range(self.firstVisible, min(len(self.items), self.firstVisible + height)):
            item = self.items[i]

            if len(item) < len(self.cols):
                item = list(item)
                item.extend([''] * (len(self.cols) - len(item)))

            elif len(item) > len(self.cols):
                item = item[:len(self.cols)]

            attr = {}
            if i in self.selected:
                attr['bold'] = True
            if self.focused and i == self.highlighted:
                attr['highlight'] = True

            formatted = self.format % tuple(item)

            self.draw(formatted, i - self.firstVisible + offset, 0, **attr);