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"
35 // ----------------------------------------------------------------------------
37 // ----------------------------------------------------------------------------
39 class WXDLLEXPORT wxEventLoopImpl
43 wxEventLoopImpl() { SetExitCode(0); }
45 // set/get the exit code
46 void SetExitCode(int exitcode
) { m_exitcode
= exitcode
; }
47 int GetExitCode() const { return m_exitcode
; }
50 // the exit code of the event loop
54 // ============================================================================
55 // wxGUIEventLoop implementation
56 // ============================================================================
58 // ----------------------------------------------------------------------------
59 // wxGUIEventLoop running and exiting
60 // ----------------------------------------------------------------------------
62 wxGUIEventLoop::~wxGUIEventLoop()
64 wxASSERT_MSG( !m_impl
, wxT("should have been deleted in Run()") );
67 int wxGUIEventLoop::DoRun()
69 m_impl
= new wxEventLoopImpl
;
71 guint loopLevel
= gtk_main_level();
73 // This is placed inside of a loop to take into account nested
74 // event loops. For example, inside this event loop, we may recieve
75 // Exit() for a different event loop (which we are currently inside of)
76 // That Exit() will cause this gtk_main() to exit so we need to re-enter it.
79 // this code was intended to support nested event loops. However,
80 // exiting a dialog will result in a application hang (because
81 // gtk_main_quit is called when closing the dialog????)
82 // So for the moment this code is disabled and nested event loops
83 // probably fail for wxGTK1
84 while ( !m_shouldExit
)
91 // Force the enclosing event loop to also exit to see if it is done
92 // in case that event loop ended inside of this one. If it is not time
93 // yet for that event loop to exit, it will be executed again due to
94 // the while() loop on m_shouldExit().
96 // This is unnecessary if we are the top level loop, i.e. loop of level 0.
105 int exitcode
= m_impl
->GetExitCode();
111 void wxGUIEventLoop::ScheduleExit(int rc
)
113 wxCHECK_RET( IsInsideRun(), wxT("can't call ScheduleExit() if not started") );
115 m_impl
->SetExitCode(rc
);
122 // ----------------------------------------------------------------------------
123 // wxEventLoop message processing dispatching
124 // ----------------------------------------------------------------------------
126 bool wxGUIEventLoop::Pending() const
130 // We need to remove idle callbacks or gtk_events_pending will
131 // never return false.
132 wxTheApp
->RemoveIdleTag();
135 return gtk_events_pending();
138 bool wxGUIEventLoop::Dispatch()
140 wxCHECK_MSG( IsRunning(), false, wxT("can't call Dispatch() if not running") );
142 gtk_main_iteration();
147 //-----------------------------------------------------------------------------
149 //-----------------------------------------------------------------------------
151 bool wxGUIEventLoop::YieldFor(long eventsToProcess
)
154 if ( !wxThread::IsMain() )
156 // can't call gtk_main_iteration() from other threads like this
159 #endif // wxUSE_THREADS
161 m_isInsideYield
= true;
162 m_eventsToProcessInsideYield
= eventsToProcess
;
164 // We need to remove idle callbacks or the loop will
166 wxTheApp
->RemoveIdleTag();
169 // disable log flushing from here because a call to wxYield() shouldn't
170 // normally result in message boxes popping up &c
174 // TODO: implement event filtering using the eventsToProcess mask
175 while (gtk_events_pending())
176 gtk_main_iteration();
178 // It's necessary to call ProcessIdle() to update the frames sizes which
179 // might have been changed (it also will update other things set from
180 // OnUpdateUI() which is a nice (and desired) side effect). But we
181 // call ProcessIdle() only once since this is not meant for longish
182 // background jobs (controlled by wxIdleEvent::RequestMore() and the
183 // return value of Processidle().
187 // let the logs be flashed again
191 m_isInsideYield
= false;
196 class wxGUIEventLoopSourcesManager
: public wxEventLoopSourcesManagerBase
200 AddSourceForFD(int WXUNUSED(fd
),
201 wxEventLoopSourceHandler
* WXUNUSED(handler
),
204 wxFAIL_MSG("Monitoring FDs in the main loop is not implemented in wxGTK1");
210 wxEventLoopSourcesManagerBase
* wxGUIAppTraits::GetEventLoopSourcesManager()
212 static wxGUIEventLoopSourcesManager s_eventLoopSourcesManager
;
214 return &s_eventLoopSourcesManager
;