Initial import to Tizen
[profile/ivi/python-twisted.git] / doc / core / examples / wxacceptance.py
1 # Copyright (c) Twisted Matrix Laboratories.
2 # See LICENSE for details.
3
4 """
5 Acceptance tests for wxreactor.
6
7 Please test on Linux, Win32 and OS X:
8 1. Startup event is called at startup.
9 2. Scheduled event is called after 2 seconds.
10 3. Shutdown takes 3 seconds, both when quiting from menu and when closing
11    window (e.g. Alt-F4 in metacity). This tests reactor.stop() and
12    wxApp.ExitEventLoop().
13 4. 'hello, world' continues to be printed even when modal dialog is open
14    (use dialog menu item), when menus are held down, when window is being
15    dragged.
16 """
17
18 import sys, time
19
20 try:
21     from wx import Frame as wxFrame, DefaultPosition as wxDefaultPosition, \
22          Size as wxSize, Menu as wxMenu, MenuBar as wxMenuBar, \
23          EVT_MENU, MessageDialog as wxMessageDialog, App as wxApp
24 except ImportError, e:
25     from wxPython.wx import *
26
27 from twisted.python import log
28 from twisted.internet import wxreactor
29 wxreactor.install()
30 from twisted.internet import reactor, defer
31
32
33 # set up so that "hello, world" is printed continously
34 dc = None
35 def helloWorld():
36     global dc
37     print "hello, world", time.time()
38     dc = reactor.callLater(0.1, helloWorld)
39 dc = reactor.callLater(0.1, helloWorld)
40
41 def twoSecondsPassed():
42     print "two seconds passed"
43
44 def printer(s):
45     print s
46
47 def shutdown():
48     print "shutting down in 3 seconds"
49     if dc.active():
50         dc.cancel()
51     reactor.callLater(1, printer, "2...")
52     reactor.callLater(2, printer, "1...")
53     reactor.callLater(3, printer, "0...")
54     d = defer.Deferred()
55     reactor.callLater(3, d.callback, 1)
56     return d
57
58 def startup():
59     print "Start up event!"
60
61 reactor.callLater(2, twoSecondsPassed)
62 reactor.addSystemEventTrigger("after", "startup", startup)
63 reactor.addSystemEventTrigger("before", "shutdown", shutdown)
64
65
66 ID_EXIT  = 101
67 ID_DIALOG = 102
68
69 class MyFrame(wxFrame):
70     def __init__(self, parent, ID, title):
71         wxFrame.__init__(self, parent, ID, title, wxDefaultPosition, wxSize(300, 200))
72         menu = wxMenu()
73         menu.Append(ID_DIALOG, "D&ialog", "Show dialog")
74         menu.Append(ID_EXIT, "E&xit", "Terminate the program")
75         menuBar = wxMenuBar()
76         menuBar.Append(menu, "&File")
77         self.SetMenuBar(menuBar)
78         EVT_MENU(self, ID_EXIT,  self.DoExit)
79         EVT_MENU(self, ID_DIALOG,  self.DoDialog)
80         # you really ought to do this instead of reactor.stop() in
81         # DoExit, but for the sake of testing we'll let closing the
82         # window shutdown wx without reactor.stop(), to make sure that
83         # still does the right thing.
84         #EVT_CLOSE(self, lambda evt: reactor.stop())
85
86     def DoDialog(self, event):
87         dl = wxMessageDialog(self, "Check terminal to see if messages are still being "
88                              "printed by Twisted.")
89         dl.ShowModal()
90         dl.Destroy()
91
92     def DoExit(self, event):
93         reactor.stop()
94
95
96 class MyApp(wxApp):
97
98     def OnInit(self):
99         frame = MyFrame(None, -1, "Hello, world")
100         frame.Show(True)
101         self.SetTopWindow(frame)
102         return True
103
104
105 def demo():
106     log.startLogging(sys.stdout)
107     app = MyApp(0)
108     reactor.registerWxApp(app)
109     reactor.run()
110
111
112 if __name__ == '__main__':
113     demo()