1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk1/evtloop.cpp
3 // Purpose: implements wxEventLoop for GTK+
4 // Author: Vadim Zeitlin
7 // Copyright: (c) 2001 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 // For compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
26 #include "wx/evtloop.h"
27 #include "wx/private/eventloopsourcesmanager.h"
36 // ----------------------------------------------------------------------------
38 // ----------------------------------------------------------------------------
40 class WXDLLEXPORT wxEventLoopImpl
44 wxEventLoopImpl() { SetExitCode(0); }
46 // set/get the exit code
47 void SetExitCode(int exitcode
) { m_exitcode
= exitcode
; }
48 int GetExitCode() const { return m_exitcode
; }
51 // the exit code of the event loop
55 // ============================================================================
56 // wxGUIEventLoop implementation
57 // ============================================================================
59 // ----------------------------------------------------------------------------
60 // wxGUIEventLoop running and exiting
61 // ----------------------------------------------------------------------------
63 wxGUIEventLoop::~wxGUIEventLoop()
65 wxASSERT_MSG( !m_impl
, wxT("should have been deleted in Run()") );
68 int wxGUIEventLoop::DoRun()
70 m_impl
= new wxEventLoopImpl
;
72 guint loopLevel
= gtk_main_level();
74 // This is placed inside of a loop to take into account nested
75 // event loops. For example, inside this event loop, we may recieve
76 // Exit() for a different event loop (which we are currently inside of)
77 // That Exit() will cause this gtk_main() to exit so we need to re-enter it.
80 // this code was intended to support nested event loops. However,
81 // exiting a dialog will result in a application hang (because
82 // gtk_main_quit is called when closing the dialog????)
83 // So for the moment this code is disabled and nested event loops
84 // probably fail for wxGTK1
85 while ( !m_shouldExit
)
92 // Force the enclosing event loop to also exit to see if it is done
93 // in case that event loop ended inside of this one. If it is not time
94 // yet for that event loop to exit, it will be executed again due to
95 // the while() loop on m_shouldExit().
97 // This is unnecessary if we are the top level loop, i.e. loop of level 0.
106 int exitcode
= m_impl
->GetExitCode();
112 void wxGUIEventLoop::ScheduleExit(int rc
)
114 wxCHECK_RET( IsInsideRun(), wxT("can't call ScheduleExit() if not started") );
116 m_impl
->SetExitCode(rc
);
123 // ----------------------------------------------------------------------------
124 // wxEventLoop message processing dispatching
125 // ----------------------------------------------------------------------------
127 bool wxGUIEventLoop::Pending() const
131 // We need to remove idle callbacks or gtk_events_pending will
132 // never return false.
133 wxTheApp
->RemoveIdleTag();
136 return gtk_events_pending();
139 bool wxGUIEventLoop::Dispatch()
141 wxCHECK_MSG( IsRunning(), false, wxT("can't call Dispatch() if not running") );
143 gtk_main_iteration();
148 //-----------------------------------------------------------------------------
150 //-----------------------------------------------------------------------------
152 bool wxGUIEventLoop::YieldFor(long eventsToProcess
)
155 if ( !wxThread::IsMain() )
157 // can't call gtk_main_iteration() from other threads like this
160 #endif // wxUSE_THREADS
162 m_isInsideYield
= true;
163 m_eventsToProcessInsideYield
= eventsToProcess
;
165 // We need to remove idle callbacks or the loop will
167 wxTheApp
->RemoveIdleTag();
170 // disable log flushing from here because a call to wxYield() shouldn't
171 // normally result in message boxes popping up &c
175 // TODO: implement event filtering using the eventsToProcess mask
176 while (gtk_events_pending())
177 gtk_main_iteration();
179 // It's necessary to call ProcessIdle() to update the frames sizes which
180 // might have been changed (it also will update other things set from
181 // OnUpdateUI() which is a nice (and desired) side effect). But we
182 // call ProcessIdle() only once since this is not meant for longish
183 // background jobs (controlled by wxIdleEvent::RequestMore() and the
184 // return value of Processidle().
188 // let the logs be flashed again
192 m_isInsideYield
= false;
197 class wxGUIEventLoopSourcesManager
: public wxEventLoopSourcesManagerBase
201 AddSourceForFD(int WXUNUSED(fd
),
202 wxEventLoopSourceHandler
* WXUNUSED(handler
),
205 wxFAIL_MSG("Monitoring FDs in the main loop is not implemented in wxGTK1");
211 wxEventLoopSourcesManagerBase
* wxGUIAppTraits::GetEventLoopSourcesManager()
213 static wxGUIEventLoopSourcesManager s_eventLoopSourcesManager
;
215 return &s_eventLoopSourcesManager
;