]> git.saurik.com Git - wxWidgets.git/blob - src/msw/evtloopconsole.cpp
f1538af277462c2fce50ae54f13dc97015501d43
[wxWidgets.git] / src / msw / evtloopconsole.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/evtloopconsole.cpp
3 // Purpose: wxConsoleEventLoop class for Windows
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 01.06.01
7 // RCS-ID: $Id$
8 // Copyright: (c) 2001 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #include "wx/evtloop.h"
28
29 // ============================================================================
30 // wxMSWEventLoopBase implementation
31 // ============================================================================
32
33 // ----------------------------------------------------------------------------
34 // ctor/dtor
35 // ----------------------------------------------------------------------------
36
37 wxMSWEventLoopBase::wxMSWEventLoopBase()
38 {
39 m_shouldExit = false;
40 m_exitcode = 0;
41 }
42
43 // ----------------------------------------------------------------------------
44 // wxEventLoop message processing dispatching
45 // ----------------------------------------------------------------------------
46
47 bool wxMSWEventLoopBase::Pending() const
48 {
49 MSG msg;
50 return ::PeekMessage(&msg, 0, 0, 0, PM_NOREMOVE) != 0;
51 }
52
53 bool wxMSWEventLoopBase::GetNextMessage(WXMSG* msg)
54 {
55 const BOOL rc = ::GetMessage(msg, NULL, 0, 0);
56
57 if ( rc == 0 )
58 {
59 // got WM_QUIT
60 return false;
61 }
62
63 if ( rc == -1 )
64 {
65 // should never happen, but let's test for it nevertheless
66 wxLogLastError(wxT("GetMessage"));
67
68 // still break from the loop
69 return false;
70 }
71
72 return true;
73 }
74
75 int wxMSWEventLoopBase::GetNextMessageTimeout(WXMSG *msg, unsigned long timeout)
76 {
77 // MsgWaitForMultipleObjects() won't notice any input which was already
78 // examined (e.g. using PeekMessage()) but not yet removed from the queue
79 // so we need to remove any immediately messages manually
80 //
81 // NB: using MsgWaitForMultipleObjectsEx() could simplify the code here but
82 // it is not available in very old Windows versions
83 if ( !::PeekMessage(msg, 0, 0, 0, PM_REMOVE) )
84 {
85 // we use this function just in order to not block longer than the
86 // given timeout, so we don't pass any handles to it at all
87 DWORD rc = ::MsgWaitForMultipleObjects
88 (
89 0, NULL,
90 FALSE,
91 timeout,
92 QS_ALLINPUT
93 );
94
95 switch ( rc )
96 {
97 default:
98 wxLogDebug("unexpected MsgWaitForMultipleObjects() return "
99 "value %lu", rc);
100 // fall through
101
102 case WAIT_TIMEOUT:
103 return -1;
104
105 case WAIT_OBJECT_0:
106 if ( !::PeekMessage(msg, 0, 0, 0, PM_REMOVE) )
107 {
108 // somehow it may happen that MsgWaitForMultipleObjects()
109 // returns true but there are no messages -- just treat it
110 // the same as timeout then
111 return -1;
112 }
113 break;
114 }
115 }
116
117 return msg->message != WM_QUIT;
118 }
119
120 // ============================================================================
121 // wxConsoleEventLoop implementation
122 // ============================================================================
123
124 #if wxUSE_CONSOLE_EVENTLOOP
125
126 void wxConsoleEventLoop::WakeUp()
127 {
128 #if wxUSE_THREADS
129 wxWakeUpMainThread();
130 #endif
131 }
132
133 void wxConsoleEventLoop::ProcessMessage(WXMSG *msg)
134 {
135 ::DispatchMessage(msg);
136 }
137
138 bool wxConsoleEventLoop::Dispatch()
139 {
140 MSG msg;
141 if ( !GetNextMessage(&msg) )
142 return false;
143
144 ProcessMessage(&msg);
145
146 return !m_shouldExit;
147 }
148
149 int wxConsoleEventLoop::DispatchTimeout(unsigned long timeout)
150 {
151 MSG msg;
152 int rc = GetNextMessageTimeout(&msg, timeout);
153 if ( rc != 1 )
154 return rc;
155
156 ProcessMessage(&msg);
157
158 return !m_shouldExit;
159 }
160
161 #endif // wxUSE_CONSOLE_EVENTLOOP