]> git.saurik.com Git - wxWidgets.git/blob - src/common/evtloopcmn.cpp
improved wxMac event loop implementation and refactored the code to share it with...
[wxWidgets.git] / src / common / evtloopcmn.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/evtloopcmn.cpp
3 // Purpose: common wxEventLoop-related stuff
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 2006-01-12
7 // RCS-ID: $Id$
8 // Copyright: (c) 2006 Vadim Zeitlin <vadim@wxwindows.org>
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 // globals
31 // ----------------------------------------------------------------------------
32
33 wxEventLoop *wxEventLoopBase::ms_activeLoop = NULL;
34
35 // ============================================================================
36 // wxEventLoopManual implementation
37 // ============================================================================
38
39 wxEventLoopManual::wxEventLoopManual()
40 {
41 m_exitcode = 0;
42 m_shouldExit = false;
43 }
44
45 int wxEventLoopManual::Run()
46 {
47 // event loops are not recursive, you need to create another loop!
48 wxCHECK_MSG( !IsRunning(), -1, _T("can't reenter a message loop") );
49
50 // ProcessIdle() and Dispatch() below may throw so the code here should
51 // be exception-safe, hence we must use local objects for all actions we
52 // should undo
53 wxEventLoopActivator activate(wx_static_cast(wxEventLoop *, this));
54
55 // we must ensure that OnExit() is called even if an exception is thrown
56 // from inside Dispatch() but we must call it from Exit() in normal
57 // situations because it is supposed to be called synchronously,
58 // wxModalEventLoop depends on this (so we can't just use ON_BLOCK_EXIT or
59 // something similar here)
60 #if wxUSE_EXCEPTIONS
61 for ( ;; )
62 {
63 try
64 {
65 #endif // wxUSE_EXCEPTIONS
66
67 // this is the event loop itself
68 for ( ;; )
69 {
70 // give them the possibility to do whatever they want
71 OnNextIteration();
72
73 // generate and process idle events for as long as we don't
74 // have anything else to do
75 while ( !Pending() && (wxTheApp && wxTheApp->ProcessIdle()) )
76 ;
77
78 // if the "should exit" flag is set, the loop should terminate
79 // but not before processing any remaining messages so while
80 // Pending() returns true, do process them
81 if ( m_shouldExit )
82 {
83 while ( Pending() )
84 Dispatch();
85
86 break;
87 }
88
89 // a message came or no more idle processing to do, sit in
90 // Dispatch() waiting for the next message
91 if ( !Dispatch() )
92 {
93 // we got WM_QUIT
94 break;
95 }
96 }
97
98 #if wxUSE_EXCEPTIONS
99 // exit the outer loop as well
100 break;
101 }
102 catch ( ... )
103 {
104 try
105 {
106 if ( !wxTheApp || !wxTheApp->OnExceptionInMainLoop() )
107 {
108 OnExit();
109 break;
110 }
111 //else: continue running the event loop
112 }
113 catch ( ... )
114 {
115 // OnException() throwed, possibly rethrowing the same
116 // exception again: very good, but we still need OnExit() to
117 // be called
118 OnExit();
119 throw;
120 }
121 }
122 }
123 #endif // wxUSE_EXCEPTIONS
124
125 return m_exitcode;
126 }
127
128 void wxEventLoopManual::Exit(int rc)
129 {
130 wxCHECK_RET( IsRunning(), _T("can't call Exit() if not running") );
131
132 m_exitcode = rc;
133 m_shouldExit = true;
134
135 OnExit();
136
137 // all we have to do to exit from the loop is to (maybe) wake it up so that
138 // it can notice that Exit() had been called
139 //
140 // in particular, do *not* use here calls such as PostQuitMessage() (under
141 // MSW) which terminate the current event loop here because we're not sure
142 // that it is going to be processed by the correct event loop: it would be
143 // possible that another one is started and terminated by mistake if we do
144 // this
145 WakeUp();
146 }
147