1#---------------------------------------------------------------------------
2# Name:        newevent.py
3# Purpose:     Easy generation of new events classes and binder objects.
4#
5# Author:      Miki Tebeka <miki.tebeka@gmail.com>
6#
7# Created:     18-Sept-2006
8# Copyright:   (c) 2006-2018 by Total Control Software
9# Licence:     wxWindows license
10#
11# Tags:        phoenix-port, documented
12#---------------------------------------------------------------------------
13
14"""
15Easy generation of new events classes and binder objects.
16
17
18Description
19===========
20
21This module contains two functions which makes the generation of custom wxPython events
22particularly easy.
23
24
25Usage
26=====
27
28Sample usage::
29
30    import wx
31    import time
32    import threading
33
34    import wx.lib.newevent as NE
35
36    MooEvent, EVT_MOO = NE.NewEvent()
37    GooEvent, EVT_GOO = NE.NewCommandEvent()
38
39    DELAY = 0.7
40
41    def evt_thr(win):
42        time.sleep(DELAY)
43        wx.PostEvent(win, MooEvent(moo=1))
44
45    def cmd_thr(win, id):
46        time.sleep(DELAY)
47        wx.PostEvent(win, GooEvent(id, goo=id))
48
49    ID_CMD1 = wx.NewIdRef()
50    ID_CMD2 = wx.NewIdRef()
51
52    class Frame(wx.Frame):
53        def __init__(self):
54            wx.Frame.__init__(self, None, -1, "MOO")
55            sizer = wx.BoxSizer(wx.VERTICAL)
56            self.Bind(EVT_MOO, self.on_moo)
57            b = wx.Button(self, -1, "Generate MOO")
58            sizer.Add(b, 1, wx.EXPAND)
59            b.Bind(wx.EVT_BUTTON, self.on_evt_click)
60            b = wx.Button(self, ID_CMD1, "Generate GOO with %d" % ID_CMD1)
61            sizer.Add(b, 1, wx.EXPAND)
62            b.Bind(wx.EVT_BUTTON, self.on_cmd_click)
63            b = wx.Button(self, ID_CMD2, "Generate GOO with %d" % ID_CMD2)
64            sizer.Add(b, 1, wx.EXPAND)
65            b.Bind(wx.EVT_BUTTON, self.on_cmd_click)
66
67            self.Bind(EVT_GOO, self.on_cmd1, id=ID_CMD1)
68            self.Bind(EVT_GOO, self.on_cmd2, id=ID_CMD2)
69
70            self.SetSizer(sizer)
71            self.SetAutoLayout(True)
72            sizer.Fit(self)
73
74        def on_evt_click(self, e):
75            t = threading.Thread(target=evt_thr, args=(self, ))
76            t.setDaemon(True)
77            t.start()
78
79        def on_cmd_click(self, e):
80            t = threading.Thread(target=cmd_thr, args=(self, e.GetId()))
81            t.setDaemon(True)
82            t.start()
83
84        def show(self, msg, title):
85            dlg = wx.MessageDialog(self, msg, title, wx.OK)
86            dlg.ShowModal()
87            dlg.Destroy()
88
89        def on_moo(self, e):
90            self.show("MOO = %s" % e.moo, "Got Moo")
91
92        def on_cmd1(self, e):
93            self.show("goo = %s" % e.goo, "Got Goo (cmd1)")
94
95        def on_cmd2(self, e):
96            self.show("goo = %s" % e.goo, "Got Goo (cmd2)")
97
98
99    app = wx.App(0)
100    f = Frame()
101    f.Show(True)
102    app.MainLoop()
103
104"""
105
106__author__ = "Miki Tebeka <miki.tebeka@gmail.com>"
107
108import wx
109
110#---------------------------------------------------------------------------
111
112def NewEvent():
113    """
114    Generates a new `(event, binder)` tuple.
115
116    ::
117
118        MooEvent, EVT_MOO = NewEvent()
119
120    """
121
122    evttype = wx.NewEventType()
123
124    class _Event(wx.PyEvent):
125        def __init__(self, **kw):
126            wx.PyEvent.__init__(self)
127            self.SetEventType(evttype)
128            self._getAttrDict().update(kw)
129
130    return _Event, wx.PyEventBinder(evttype)
131
132
133def NewCommandEvent():
134    """
135    Generates a new `(command_event, binder)` tuple.
136
137    ::
138
139        MooCmdEvent, EVT_MOO = NewCommandEvent()
140
141    """
142
143    evttype = wx.NewEventType()
144
145    class _Event(wx.PyCommandEvent):
146        def __init__(self, id, **kw):
147            wx.PyCommandEvent.__init__(self, evttype, id)
148            self._getAttrDict().update(kw)
149
150    return _Event, wx.PyEventBinder(evttype, 1)
151
152
153#---------------------------------------------------------------------------
154
155def _test():
156    """A little smoke test"""
157    import time
158    import threading
159
160    MooEvent, EVT_MOO = NewEvent()
161    GooEvent, EVT_GOO = NewCommandEvent()
162
163    DELAY = 0.7
164
165    def evt_thr(win):
166        time.sleep(DELAY)
167        wx.PostEvent(win, MooEvent(moo=1))
168
169    def cmd_thr(win, id):
170        time.sleep(DELAY)
171        wx.PostEvent(win, GooEvent(id, goo=id))
172
173    ID_CMD1 = wx.NewIdRef()
174    ID_CMD2 = wx.NewIdRef()
175
176    class Frame(wx.Frame):
177        def __init__(self):
178            wx.Frame.__init__(self, None, -1, "MOO")
179            sizer = wx.BoxSizer(wx.VERTICAL)
180            self.Bind(EVT_MOO, self.on_moo)
181            b = wx.Button(self, -1, "Generate MOO")
182            sizer.Add(b, 1, wx.EXPAND)
183            b.Bind(wx.EVT_BUTTON, self.on_evt_click)
184            b = wx.Button(self, ID_CMD1, "Generate GOO with %d" % ID_CMD1)
185            sizer.Add(b, 1, wx.EXPAND)
186            b.Bind(wx.EVT_BUTTON, self.on_cmd_click)
187            b = wx.Button(self, ID_CMD2, "Generate GOO with %d" % ID_CMD2)
188            sizer.Add(b, 1, wx.EXPAND)
189            b.Bind(wx.EVT_BUTTON, self.on_cmd_click)
190
191            self.Bind(EVT_GOO, self.on_cmd1, id=ID_CMD1)
192            self.Bind(EVT_GOO, self.on_cmd2, id=ID_CMD2)
193
194            self.SetSizer(sizer)
195            self.SetAutoLayout(True)
196            sizer.Fit(self)
197
198        def on_evt_click(self, e):
199            t = threading.Thread(target=evt_thr, args=(self, ))
200            t.setDaemon(True)
201            t.start()
202
203        def on_cmd_click(self, e):
204            t = threading.Thread(target=cmd_thr, args=(self, e.GetId()))
205            t.setDaemon(True)
206            t.start()
207
208        def show(self, msg, title):
209            dlg = wx.MessageDialog(self, msg, title, wx.OK)
210            dlg.ShowModal()
211            dlg.Destroy()
212
213        def on_moo(self, e):
214            self.show("MOO = %s" % e.moo, "Got Moo")
215
216        def on_cmd1(self, e):
217            self.show("goo = %s" % e.goo, "Got Goo (cmd1)")
218
219        def on_cmd2(self, e):
220            self.show("goo = %s" % e.goo, "Got Goo (cmd2)")
221
222
223    app = wx.App(0)
224    f = Frame()
225    f.Show(True)
226    app.MainLoop()
227
228if __name__ == "__main__":
229    _test()
230