# $Id$ # test script for miniwck import _wck import aggdraw class Window: _timers = {} def __init__(self, title, width=None, height=None, layered=None): if not isinstance(title, unicode): title = unicode(title) self.hwnd = _wck.createwindow( self.__dispatcher, title=title, width=width or 0, height=height or 0, layered=layered or 0 ) def __dispatcher(self, action, *args): try: handler = getattr(self, "ui_handle_" + action) except AttributeError, v: print "MISSING", v else: return apply(handler, args) def ui_damage(self, x0=None, y0=None, x1=None, y1=None): if x0 is None: _wck.invalidate_rect(self.hwnd) else: _wck.invalidate_rect(self.hwnd, x0, y0, x1, y1) def ui_handle_clear(self, dc, x0, y0, x1, y1): pass def ui_handle_damage(self, x0, y0, x1, y1): pass def ui_handle_destroy(self): pass def ui_handle_repair(self, dc, x0, y0, x1, y1): pass def ui_handle_resize(self, width, height): pass def ui_handle_event(self, event): # print "%04x %04x %08x" % (event._message, event._wParam, event._lParam) if event.type == "KeyPress" and event.char: import sys, unicodedata print repr(event.char), if isinstance(event.char, unicode): print unicodedata.name(event.char), print # print dict([(k, getattr(event, k)) for k in dir(event)]) # # timer support def ui_handle_timer(self, timer): try: self._timers[timer]() finally: try: del self._timers[timer] except KeyError: pass _wck.timer_kill(self.hwnd, timer) def after(self, delay, function, *args): class Timer: def __init__(self, function, args): self.function = function self.args = args def __call__(self): self.function(*args) timer = Timer(function, args) self._timers[id(timer)] = timer _wck.timer_set(self.hwnd, id(timer), delay) return id(timer) def after_cancel(self, timer): try: del self._timers[timer] except KeyError: pass _wck.timer_kill(self.hwnd, timer) def mainloop(self): _wck.eventloop() class AggView(Window): def ui_handle_resize(self, width, height): self.size = width, height self.image = None def ui_handle_clear(self, draw, x0, y0, x1, y1): pass # ignore clear events def ui_handle_repair(self, draw, x0, y0, x1, y1): if self.image is None: # create agg drawing context # FIXME: fetch background color self.image = aggdraw.Dib("RGB", self.size, "#ffffff") self.agg_draw(self.image, 0, 0, self.size[0], self.size[1]) self.image.expose(hdc=draw) ## # Forces redraw. def agg_damage(self, x0=None, y0=None, x1=None, y1=None): self.image = None self.ui_damage(x0, y0, x1, y1) ## # (Hook) Called to draw the AGG view contents. This is # called whenever the view needs to be redrawn. def agg_draw(self, draw, x0, y0, x1, y1): p = aggdraw.Pen("red", 5) draw.line((x0, y0, x1, y1), p) draw.line((x0, y1, x1, y0), p) try: im = _wck.image_open("/images/lenna.jpg") except IOError: im = None class GdiWindow(Window): def __init__(self, *args, **kw): if 0: kw["layered"] = 1 Window.__init__(self, *args, **kw) im = _wck.image_open("fan.png") _wck.alpha_update(self.hwnd, im.size, im.tostring()) else: Window.__init__(self, *args, **kw) def callback(args): print args timer = self.after(1000, callback, "hello") # self.after_cancel(timer) def ui_handle_resize(self, width, height): self.size = width, height def ui_handle_clear(self, draw, x0, y0, x1, y1): draw = _wck.Graphics(draw) draw.rectangle((x0, y0, x1, y1), self.ui_brush("white")) def ui_handle_repair(self, draw, x0, y0, x1, y1): draw = _wck.Graphics(draw) if im: draw.image(im, (x0, y0, x1, y1)) p = self.ui_pen("red", 5, opacity=128) draw.line((x0, y0, x1, y1), p) draw.line((x0, y1, x1, y0), p) def ui_pen(self, color="black", width=1, opacity=255): return _wck.Pen(color, width, opacity) def ui_brush(self, color="black", opacity=255): return _wck.Brush(color, opacity) if 0: win = AggView("Hello AGG", 600, 400) win.mainloop() else: win = GdiWindow("Hello GDI+", 600, 400) win.mainloop()