]> git.saurik.com Git - wxWidgets.git/commitdiff
newevent helpers
authorRobin Dunn <robin@alldunn.com>
Wed, 1 Oct 2003 23:30:43 +0000 (23:30 +0000)
committerRobin Dunn <robin@alldunn.com>
Wed, 1 Oct 2003 23:30:43 +0000 (23:30 +0000)
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@24049 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775

wxPython/wxPython/lib/newevent.py [new file with mode: 0644]

diff --git a/wxPython/wxPython/lib/newevent.py b/wxPython/wxPython/lib/newevent.py
new file mode 100644 (file)
index 0000000..a6c828d
--- /dev/null
@@ -0,0 +1,122 @@
+#!/usr/bin/env python
+"""Easy generation of new events classes and binder functions"""
+
+__author__ = "Miki Tebeka <tebeka@cs.bgu.ac.il>"
+
+import wx
+
+#---------------------------------------------------------------------------
+
+def NewEvent():
+    """Generate new (Event, Binder) tuple
+        e.g. MooEvent, EVT_MOO = NewEvent()
+    """
+    evttype = wx.NewEventType()
+    
+    class _Event(wx.PyEvent):
+        def __init__(self, **kw):
+            wx.PyEvent.__init__(self)
+            self.SetEventType(evttype)
+            self.__dict__.update(kw)
+
+    def Binder(win, func):
+        win.Connect(-1, -1, evttype, func)
+
+    return _Event, Binder
+
+
+
+def NewCommandEvent():
+    """Generate new (CmdEvent, Binder) tuple
+        e.g. MooCmdEvent, EVT_MOO = NewCommandEvent()
+    """
+    evttype = wx.NewEventType()
+
+    class _Event(wx.PyCommandEvent):
+        def __init__(self, id, **kw):
+            wx.PyCommandEvent.__init__(self, evttype, id)
+            self.__dict__.update(kw)
+    
+    def Binder(win, id, func):
+        win.Connect(id, -1, evttype, func)
+
+    return _Event, Binder
+
+
+#---------------------------------------------------------------------------
+
+def _test():
+    """A little smoke test"""
+    from threading import Thread
+    from time import sleep
+
+    MooEvent, EVT_MOO = NewEvent()
+    GooEvent, EVT_GOO = NewCommandEvent()
+
+    DELAY = 0.7
+
+    def evt_thr(win):
+        sleep(DELAY)
+        wx.PostEvent(win, MooEvent(moo=1))
+
+    def cmd_thr(win, id):
+        sleep(DELAY)
+        wx.PostEvent(win, GooEvent(id, goo=id))
+
+    ID_CMD1 = wx.NewId()
+    ID_CMD2 = wx.NewId()
+
+    class Frame(wx.Frame):
+        def __init__(self):
+            wx.Frame.__init__(self, None, -1, "MOO")
+            sizer = wx.BoxSizer(wx.VERTICAL)
+            EVT_MOO(self, self.on_moo)
+            b = wx.Button(self, -1, "Generate MOO")
+            sizer.Add(b, 1, wx.EXPAND)
+            wx.EVT_BUTTON(self, b.GetId(), self.on_evt_click)
+            b = wx.Button(self, ID_CMD1, "Generate GOO with %d" % ID_CMD1)
+            sizer.Add(b, 1, wx.EXPAND)
+            wx.EVT_BUTTON(self, ID_CMD1, self.on_cmd_click)
+            b = wx.Button(self, ID_CMD2, "Generate GOO with %d" % ID_CMD2)
+            sizer.Add(b, 1, wx.EXPAND)
+            wx.EVT_BUTTON(self, ID_CMD2, self.on_cmd_click)
+
+            EVT_GOO(self, ID_CMD1, self.on_cmd1)
+            EVT_GOO(self, ID_CMD2, self.on_cmd2)
+
+            self.SetSizer(sizer)
+            self.SetAutoLayout(True)
+            sizer.Fit(self)
+
+        def on_evt_click(self, e):
+            t = Thread(target=evt_thr, args=(self, ))
+            t.setDaemon(True)
+            t.start()
+
+        def on_cmd_click(self, e):
+            t = Thread(target=cmd_thr, args=(self, e.GetId()))
+            t.setDaemon(True)
+            t.start()
+
+        def show(self, msg, title):
+            dlg = wx.MessageDialog(self, msg, title, wx.OK)
+            dlg.ShowModal()
+            dlg.Destroy()
+
+        def on_moo(self, e):
+            self.show("MOO = %s" % e.moo, "Got Moo")
+
+        def on_cmd1(self, e):
+            self.show("goo = %s" % e.goo, "Got Goo (cmd1)")
+
+        def on_cmd2(self, e):
+            self.show("goo = %s" % e.goo, "Got Goo (cmd2)")
+            
+
+    app = wx.PySimpleApp()
+    f = Frame()
+    f.Show(True)
+    app.MainLoop()
+
+if __name__ == "__main__":
+    _test()