Add setup script. Add Checkbox widget + demo. Updates.
# -*- coding: utf-8 -*-
from tuikit.widget import Widget
class Button(Widget):
'''Clickable button.'''
def __init__(self, label=''):
'''Create button with given label, size according to label.'''
w = len(label) + 4
h = 1
Widget.__init__(self, w, h)
#: 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
# size
self.sizereq.w = w
self.sizereq.h = h
self.connect('draw', self.on_draw)
self.connect('mousedown', self.on_mousedown)
self.connect('mouseup', self.on_mouseup)
self.connect('keypress', self.on_keypress)
self.addevents('click')
def on_draw(self, screen, x, y):
pad = self.width - len(self.label) - len(self.prefix) - len(self.suffix)
lpad, rpad = self._divide_padding(pad)
screen.pushcolor(self.getcolor())
# 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()
def on_mousedown(self, ev):
self.highlight = True
self.redraw()
def on_mouseup(self, ev):
self.highlight = False
self.redraw()
if self.enclose(ev.px, ev.py):
self.handle('click')
def on_keypress(self, keyname, char):
if keyname == 'enter':
self.handle('click')
def getcolor(self):
if self.highlight or self.hasfocus():
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