root/stuff/sandbox/miniwck/MiniWCK.py

Revision 336 (by effbot, 05/04/06 15:33:20)

added testclock test script

# $Id$
# test script for miniwck

import _wck

class BaseWindow:

    ui_option_title = None

    ui_option_width = 0 # default
    ui_option_height = 0 # default

    ui_option_background = None

    _ui_timers = {}

    def __init__(self, master, **options):
        self.ui_init(master, options)

    def ui_init(self, master, options):
        for key, value in options.items():
            key = "ui_option_" + key
            getattr(self, key)
            setattr(self, key, value)
        def fixtitle(title):
            if not title:
                title = u""
            elif not isinstance(title, unicode):
                title = unicode(title)
            return title
        self.hwnd = _wck.createwindow(
            self._dispatch,
            title=fixtitle(self.ui_option_title),
            width=self.ui_option_width,
            height=self.ui_option_height,
            )

    def _dispatch(self, action, *args):
        return apply(getattr(self, "_dispatch_" + action), args)

    def _dispatch_clear(self, dc, x0, y0, x1, y1):
        try:
            handler = self.ui_handle_clear
        except AttributeError:
            handler = _wck.clear
        return handler(dc, x0, y0, x1, y1)

    def _dispatch_damage(self, x0, y0, x1, y1):
        try:
            handler = self.ui_handle_damage
        except AttributeError:
            return
        return handler(x0, y0, x1, y1)

    def _dispatch_destroy(self):
        try:
            handler = self.ui_handle_destroy
        except AttributeError:
            return
        return handler()

    def _dispatch_event(self, event):
        try:
            handler = self.ui_handle_event # FIXME: add proper bind support
        except AttributeError:
            return
        return handler(event)

    def _dispatch_repair(self, dc, x0, y0, x1, y1):
        try:
            handler = self.ui_handle_repair
        except AttributeError:
            return
        return handler(dc, x0, y0, x1, y1)

    def _dispatch_resize(self, width, height):
        try:
            handler = self.ui_handle_resize
        except AttributeError:
            return
        return handler(width, height)

    def _dispatch_timer(self, timer):
        try:
            self._ui_timers[timer]()
        finally:
            # FIXME: add special handling for repeated timers?
            self.after_cancel(timer)

    #
    # WCK api

    def ui_damage(self, x0=None, y0=None, x1=None, y1=None):
        if x0 is None:
            _wck.damage(self.hwnd)
        else:
            _wck.damage(self.hwnd, x0, y0, x1, y1)

    #
    # Tk emulation

    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._ui_timers[id(timer)] = timer
        _wck.timer_set(self.hwnd, id(timer), delay)
        return id(timer)

    def after_cancel(self, timer):
        try:
            del self._ui_timers[timer]
        except KeyError:
            pass
        _wck.timer_kill(self.hwnd, timer)

    def mainloop(self):
        _wck.eventloop()


class GdiPlusWindow(BaseWindow):

    def _dispatch_clear(self, draw, x0, y0, x1, y1):
        try:
            return self.ui_handle_clear(_wck.Graphics(draw), x0, y0, x1, y1)
        except:
            BaseWindow._dispatch_clear(self, draw, x0, y0, x1, y1)

    def _dispatch_repair(self, draw, x0, y0, x1, y1):
        return self.ui_handle_repair(_wck.Graphics(draw), x0, y0, x1, y1)

    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)

    def ui_font(self, color="black", family="Times New Roman"):
        return _wck.Font(color, family)

class Window(GdiPlusWindow):
    pass

class Widget(Window):
    pass
Note: See TracBrowser for help on using the browser.