# HG changeset patch # User Radek Brich # Date 1355389292 -3600 # Node ID 05500124d7fba40dd002ddb0e10c0c077d72d98d # Parent c0cdef06fd1619647af89ed9d863b24b387845eb Add setup script. Add Checkbox widget + demo. Updates. diff -r c0cdef06fd16 -r 05500124d7fb .hgignore --- a/.hgignore Mon Feb 20 18:15:13 2012 +0100 +++ b/.hgignore Thu Dec 13 10:01:32 2012 +0100 @@ -1,8 +1,8 @@ .*~ -tuikit/.*\.pyc -docs/_build -tuikit\.log -old +^tuikit/.*\.pyc +^docs/_build +^tuikit\.log +^build \.project \.pydevproject \.settings diff -r c0cdef06fd16 -r 05500124d7fb demo_checkbox.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/demo_checkbox.py Thu Dec 13 10:01:32 2012 +0100 @@ -0,0 +1,33 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + +import locale +locale.setlocale(locale.LC_ALL, '') + +from tuikit import * + + +class MyApplication(Application): + def __init__(self): + Application.__init__(self) + self.top.connect('keypress', self.globalkeypress) + + vert = VerticalLayout(homogeneous=False) + self.top.layout(vert) + + combo = ComboBox(items=['abc', 'xyz']) + self.top.add(combo) + + for i in range(10): + cbox = Checkbox('checkbox ' + str(i)) + self.top.add(cbox) + + def globalkeypress(self, keyname, char): + if keyname == 'escape' or char == 'q': + self.terminate() + + +if __name__ == '__main__': + app = MyApplication() + app.start() + diff -r c0cdef06fd16 -r 05500124d7fb setup.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/setup.py Thu Dec 13 10:01:32 2012 +0100 @@ -0,0 +1,15 @@ +#!/usr/bin/env python + +from distutils.core import setup + +setup( + name='tuikit', + version='0.1', + description='Curses UI toolkit', + author='Radek Brich', + author_email='radek.brich@devl.cz', + url='http://hg.devl.cz/tuikit/', + keywords=['curses', 'tui', 'toolkit'], + packages=['tuikit'], + ) + diff -r c0cdef06fd16 -r 05500124d7fb tuikit/__init__.py --- a/tuikit/__init__.py Mon Feb 20 18:15:13 2012 +0100 +++ b/tuikit/__init__.py Thu Dec 13 10:01:32 2012 +0100 @@ -2,6 +2,7 @@ from tuikit.application import Application from tuikit.button import Button +from tuikit.checkbox import Checkbox from tuikit.combobox import ComboBox from tuikit.common import Rect, Size, Coords from tuikit.container import Container diff -r c0cdef06fd16 -r 05500124d7fb tuikit/application.py --- a/tuikit/application.py Mon Feb 20 18:15:13 2012 +0100 +++ b/tuikit/application.py Thu Dec 13 10:01:32 2012 +0100 @@ -142,6 +142,7 @@ driver.setcolor('button-active', 'black on cyan') driver.setcolor('menu', 'black on cyan') driver.setcolor('menu-active', 'white on cyan, bold') + driver.setcolor('combo:normal', 'black on green') def get_driver_instance(self, name): diff -r c0cdef06fd16 -r 05500124d7fb tuikit/button.py --- a/tuikit/button.py Mon Feb 20 18:15:13 2012 +0100 +++ b/tuikit/button.py Thu Dec 13 10:01:32 2012 +0100 @@ -15,6 +15,13 @@ #: Button label. self.label = label + #: Text or graphics to be added before label + self.prefix = '[' + #: Text or graphics to be added after label + self.suffix = ']' + #: How should label be aligned if button has excess space - center | left | right + self.align = 'center' + self.bg = 'button' self.bghi = 'button-active' self.highlight = False @@ -23,11 +30,6 @@ self.sizereq.w = w self.sizereq.h = h - #: Left bracket graphic character. - self.lbracket = '[' - #: Right bracket graphic character. - self.rbracket = ']' - self.connect('draw', self.on_draw) self.connect('mousedown', self.on_mousedown) self.connect('mouseup', self.on_mouseup) @@ -37,15 +39,23 @@ def on_draw(self, screen, x, y): - l = (self.width - len(self.label)) // 2 + pad = self.width - len(self.label) - len(self.prefix) - len(self.suffix) + lpad, rpad = self._divide_padding(pad) screen.pushcolor(self.getcolor()) - screen.putch(x, y, self.lbracket) - for i in range(x+1, x+l): - screen.putch(i, y, ' ') - screen.puts(x + l, y, self.label) - for i in range(x+l+len(self.label), x+self.width-1): - screen.putch(i, y, ' ') - screen.putch(x + self.width - 1, y, self.rbracket) + # prefix + screen.puts(x, y, self.prefix) + pos = len(self.prefix) + # left pad + screen.puts(x + pos, y, ' ' * lpad) + pos += lpad + # label + screen.puts(x + pos, y, self.label) + pos += len(self.label) + # right pad + screen.puts(x + pos, y, ' ' * rpad) + pos += rpad + # suffix + screen.puts(x + pos, y, self.suffix) screen.popcolor() @@ -72,3 +82,13 @@ return self.bghi return self.bg + def _divide_padding(self, pad): + # default is 'left' + lpad, rpad = 0, pad + if self.align == 'center': + lpad = pad // 2 + rpad = pad - lpad + elif self.align == 'right': + lpad, rpad = pad, 0 + return lpad, rpad + diff -r c0cdef06fd16 -r 05500124d7fb tuikit/checkbox.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tuikit/checkbox.py Thu Dec 13 10:01:32 2012 +0100 @@ -0,0 +1,30 @@ +# -*- coding: utf-8 -*- + +from tuikit.button import Button + + +class Checkbox(Button): + + '''Checkbox - button.''' + + def __init__(self, label=''): + Button.__init__(self, label) + + self.checked = False + + self.prefix = '[ ] ' + self.suffix = '' + self.align = 'left' + self.bg = 'normal' + self.bghi = 'active' + + self.connect('click', self.on_click) + + def on_click(self): + if self.checked: + self.checked = False + self.prefix = '[ ] ' + else: + self.checked = True + self.prefix = '[x] ' + diff -r c0cdef06fd16 -r 05500124d7fb tuikit/combobox.py --- a/tuikit/combobox.py Mon Feb 20 18:15:13 2012 +0100 +++ b/tuikit/combobox.py Thu Dec 13 10:01:32 2012 +0100 @@ -10,22 +10,24 @@ def __init__(self, width=15, value='', items=[]): Container.__init__(self, width, 1) + self.colorprefix = 'combo:' + self._edit = EditField(width - 3, value) self.add(self._edit) self._btn = Button('v') + self._btn.prefix = '' + self._btn.suffix = '' self._btn.x = width - 3 self._btn.width = 3 self._btn.connect('click', self._on_btn_click) - self.add(self._edit) + self.add(self._btn) self._menu = Menu(items) self._menu.hide() self._menu.allowlayout = False -# self.top.add(self._menu) - + #self.top.add(self._menu) def _on_btn_click(self): pass - - + diff -r c0cdef06fd16 -r 05500124d7fb tuikit/editfield.py --- a/tuikit/editfield.py Mon Feb 20 18:15:13 2012 +0100 +++ b/tuikit/editfield.py Thu Dec 13 10:01:32 2012 +0100 @@ -30,6 +30,7 @@ def on_draw(self, screen, x, y): + screen.pushcolor('normal') # draw value val = self.value + ' ' * self.tw # add spaces to fill rest of field val = val[self.ofs : self.ofs + self.tw] # cut value - begin from ofs, limit to tw chars @@ -47,7 +48,7 @@ screen.putch(x + self.width-1, y, c) self.cursor = (1 + self.pos - self.ofs, 0) - + screen.popcolor() def on_keypress(self, keyname, char): handled = False diff -r c0cdef06fd16 -r 05500124d7fb tuikit/pager.py --- a/tuikit/pager.py Mon Feb 20 18:15:13 2012 +0100 +++ b/tuikit/pager.py Thu Dec 13 10:01:32 2012 +0100 @@ -8,7 +8,15 @@ class Pager(Container): - '''Only one of children is visible at the time.''' + '''Only one of children is visible at the time. + + Pages are switched using buttons at top (tabs). + + page_one = Container() + pager.add(page_one, title="page one") + pager.select(page_one) + + ''' def __init__(self, width=20, height=20): Container.__init__(self, width, height) @@ -23,7 +31,7 @@ Container.add(self, self.buttons) horz = HorizontalLayout(homogeneous=True, spacing=1) self.buttons.layout(horz) - + def add(self, widget, **kw): Container.add(self, widget, expand=True, fill=True) diff -r c0cdef06fd16 -r 05500124d7fb tuikit/treeview.py --- a/tuikit/treeview.py Mon Feb 20 18:15:13 2012 +0100 +++ b/tuikit/treeview.py Thu Dec 13 10:01:32 2012 +0100 @@ -122,16 +122,24 @@ def setmodel(self, value): if self._model: - self._model.disconnect('change', self.redraw) + self._model.disconnect('change', self.model_change) self._model = value if self._model: - self._model.connect('change', self.redraw) + self._model.connect('change', self.model_change) try: self.cnode = self._model.root[0] except IndexError: pass + + model = property(getmodel, setmodel) - model = property(getmodel, setmodel) + def model_change(self): + if self.cnode is None: + try: + self.cnode = self._model.root[0] + except IndexError: + pass + self.redraw() def collapse(self, path, collapse=True): node = self._model.find(path)