diff -r fcaabd817774 -r 7175ed629a76 tuikit/widget.py --- a/tuikit/widget.py Wed Apr 13 13:07:26 2011 +0200 +++ b/tuikit/widget.py Sun Jul 31 13:04:39 2011 +0200 @@ -2,9 +2,16 @@ import curses +from .eventsource import EventSource -class Widget: + +class Widget(EventSource): + + """Base class for all widgets.""" + def __init__(self, width = 10, height = 10): + EventSource.__init__(self) + self.parent = None self.top = None # placing - set by parent widget's layout manager @@ -24,17 +31,16 @@ self._redraw = True self.hidden = False # event handlers - self.event = { - 'resize' : [], - 'draw' : [], - 'focus' : [], - 'unfocus' : [], - 'keypress' : [], - 'mousedown' : [], - 'mouseup' : [], - 'mousemove' : [], - 'mousewheel' : [], - } + self.addevents( + 'resize', + 'draw', + 'focus', + 'unfocus', + 'keypress', + 'mousedown', + 'mouseup', + 'mousemove', + 'mousewheel') @property @@ -60,37 +66,7 @@ self.top = top - ### event management - - - def newevent(self, event): - self.event[event] = [] - - - def connect(self, event, handler): - if event in list(self.event.keys()): - self.event[event] += [handler] - - - def disconnect(self, event, handler=None): - if event in list(self.event.keys()): - if handler: - i = self.event[event].index(handler) - del self.event[event][i] - else: - self.event[event] = [] - - - def handle(self, event, *args, **kwargs): - for handler in self.event[event]: - handler(*args, **kwargs) - - - def emit(self, event, *args, **kwargs): - getattr(self, event)(*args, **kwargs) - - - ### + ### events def resize(self): @@ -124,26 +100,26 @@ return bool(self.event['keypress']) + def hasfocus(self): + return self.top.focuswidget == self + + def setfocus(self): - if self.hasfocus(): + if self.hasfocus() or not self.canfocus(): return if self.top.focuswidget: - self.top.focuswidget.unfocus(self) + self.top.focuswidget.resetfocus() self.top.focuswidget = self self.emit('focus') - def unfocus(self): + def resetfocus(self): if self.top.focuswidget != self: return self.top.focuswidget = None self.emit('unfocus') - def hasfocus(self): - return self.top.focuswidget == self - - def focus(self): '''handle focus event''' self.handle('focus') @@ -154,11 +130,13 @@ self.handle('unfocus', newfocus) - ### + ### events def keypress(self, keyname, char): - self.handle('keypress', keyname, char) + handled = self.handle('keypress', keyname, char) + if not handled and self.parent and self.parent != self.top: + self.parent.emit('keypress', keyname, char) def mousedown(self, ev): @@ -178,6 +156,9 @@ self.handle('mousewheel', ev) + ### + + def enclose(self, x, y): if self.hidden: return False