Debian Codegraph @

protolib/src/wx/wxProtoApp.cpp

raw
1 #include "wxProtoApp.h"
2
3 #define WXPROTO_USE_THREADING 1
4 #ifndef WXPROTO_USE_THREADING
5 #include "wx/apptrait.h"
6 #include "wx/cmdline.h"
7 #include "wx/evtloop.h"
8 #include "wx/msgout.h"
9 #include "wx/thread.h"
10 #include "wx/utils.h"
11 #include "wx/ptr_scpd.h"
12 #endif // !WXPROTO_USE_THREADING
13
14 /*BEGIN_DECLARE_EVENT_TYPES()
15 DECLARE_EVENT_TYPE(wxPROTO_DISPATCH_EVENT, -1)
16 END_DECLARE_EVENT_TYPES()
17
18 DEFINE_EVENT_TYPE(wxPROTO_DISPATCH_EVENT)*/
19
20 const wxEventType wxProtoApp::wxPROTO_DISPATCH_EVENT = wxNewEventType();
21
22 // it may also be convenient to define an event table macro for this event type
23 #define EVT_PROTO_DISPATCH_COMMAND(id, fn) \
24 DECLARE_EVENT_TABLE_ENTRY( \
25 wxProtoApp::wxPROTO_DISPATCH_EVENT, id, -1, \
26 (wxObjectEventFunction)(wxEventFunction)(wxCommandEventFunction)&fn, \
27 (wxObject *) NULL \
28 ),
29
30 DECLARE_APP(wxProtoApp)
31
32 BEGIN_EVENT_TABLE(wxProtoApp, wxApp)
33 EVT_PROTO_DISPATCH_COMMAND(-1, wxProtoApp::OnDispatchEvent)
34 #ifndef WXPROTO_USE_THREADING
35 EVT_IDLE(wxProtoApp::OnIdleEvent)
36 #endif // WXPROTO_USE_THREADING
37 END_EVENT_TABLE()
38
39
40 wxProtoApp::wxProtoApp(bool appDispatch)
41 : ProtoDispatcher::Controller(dispatcher), app_dispatch(appDispatch)
42 {
43
44 }
45
46 wxProtoApp::~wxProtoApp()
47 {
48 dispatcher.Stop();
49 }
50
51 int wxProtoApp::MainLoop()
52 {
53 #if WXPROTO_USE_THREADING
54 // 1) Start ProtoDispatcher thread
55 if (app_dispatch)
56 dispatcher.StartThread(true, this);
57 else
58 dispatcher.StartThread(true);
59
60 // 2) Enter standard wxWidgets event loop
61 int result = wxApp::MainLoop();
62
63 // Stop ProtoDispatcher thread
64 dispatcher.Stop();
65
66 return result;
67 #else
68
69 TRACE("Using unstable, non-threaded wxProtoApp main loop ...\n");
70
71 #ifndef WIN32
72 // The "guiTimer" makes sure we check for
73 // wxWindows GUI events regularly
74 // (TBD) asynchronously monitor for GUI events instead
75 // (this already works on Win32)
76 ProtoTimer guiTimer;
77 guiTimer.SetRepeat(-1);
78 guiTimer.SetInterval(0.5);
79 dispatcher.ActivateTimer(guiTimer);
80 #endif // !WIN32
81 return wxApp::MainLoop();
82 /*
83 while (1)
84 {
85 TRACE("guiTimer.IsActive(): %d\n", guiTimer.IsActive());
86 if (wxApp::Pending())
87 {
88 TRACE("dispatching ...\n");
89 wxApp::Dispatch();
90 }
91 else
92 {
93 TRACE("wx not pending ...\n");
94 if (dispatcher.IsPending())
95 {
96 dispatcher.Run(true); // one-shot wait/dispatch
97 }
98 #ifdef WIN32
99 else
100 {
101 // (Win32 ProtoDispatcher breaks on messages)
102 TRACE(" dispatcher.Wait() ...\n");
103 dispatcher.Wait();
104 TRACE(" dispatcher.Dispatch() ...\n");
105 dispatcher.Dispatch();
106 }
107 #endif // WIN32
108 }
109 if (NULL == GetTopWindow())
110 {
111 #ifndef WIN32
112 if (guiTimer.IsActive()) guiTimer.Deactivate();
113 #endif // !WIN32
114 if (!dispatcher.IsPending())
115 {
116 dispatcher.Stop();
117 break;
118 }
119 }
120 #ifndef WIN32
121 else if (!guiTimer.IsActive())
122 {
123 ActivateTimer(guiTimer);
124 }
125 #endif // !WIN32
126 }
127 return wxApp::MainLoop();
128 */
129 #endif
130 } // end wxProtoApp::MainLoop()
131
132 bool wxProtoApp::SignalDispatchReady()
133 {
134 wxCommandEvent dispatchEvent(wxPROTO_DISPATCH_EVENT);
135 //AddPendingEvent(dispatchEvent);
136 wxPostEvent(this, dispatchEvent);
137 return true;
138 } // end wxProtoApp::SignalDispatchReady()
139
140
141 void wxProtoApp::OnDispatchEvent(wxCommandEvent& /*event*/)
142 {
143 OnDispatch();
144 }
145
146 void wxProtoApp::OnIdleEvent(wxIdleEvent& event)
147 {
148 TRACE("wxProtoApp::OnIdleEvent() ...\n");
149 if (dispatcher.IsPending())
150 {
151 dispatcher.Run(true);
152 }
153 event.RequestMore();
154 }