tuikit/textedit.py
author Radek Brich <radek.brich@devl.cz>
Sun, 22 Feb 2015 09:53:13 +0100
changeset 119 dd91747504dd
parent 74 23767a33a781
permissions -rw-r--r--
Redraw widgets on request. Add scrollbar demo.

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

from tuikit.layout import AnchorLayout
from tuikit.editbox import EditBox
from tuikit.scrollbar import VScrollbar, HScrollbar
from tuikit.common import Borders


class TextEdit(AnchorLayout):
    def __init__(self, text=''):
        AnchorLayout.__init__(self)
        self._default_size.update(20, 20)

        self.editbox = EditBox(text)
        self.editbox.add_handler('scroll', self.on_editbox_scroll)
        self.editbox.add_handler('areasize', self.on_editbox_areasize)
        self.add(self.editbox, halign='fill', valign='fill', margin=Borders(full=1))

        self.vscroll = VScrollbar()
        self.vscroll.add_handler('change', self.on_vscroll_change)
        self.add(self.vscroll, halign='right', valign='fill', margin=Borders(t=1, b=1))

        self.hscroll = HScrollbar()
        self.hscroll.add_handler('change', self.on_hscroll_change)
        self.add(self.hscroll, halign='fill', valign='bottom', margin=Borders(l=1, r=1))

        self.on_editbox_areasize(None)

    def settext(self, text):
        self.editbox.set_text(text)

    def scrolltoend(self):
        self.editbox.move_bottom()

    def on_draw(self, ev):
        ev.driver.frame(ev.x, ev.y, self.width, self.height)

    def on_editbox_scroll(self, ev):
        self.vscroll.scroll_pos = self.editbox.yofs

    def on_editbox_areasize(self, ev):
        smax = len(self.editbox.lines) - self.editbox.height
        if smax < 0:
            smax = 0
        self.vscroll.scroll_max = smax

    def on_vscroll_change(self, ev):
        self.editbox.set_yofs(self.vscroll.scroll_pos)
        self.editbox.redraw()

    def on_hscroll_change(self, ev):
        self.editbox.xofs = self.hscroll.scroll_pos
        self.editbox.redraw()