///////////////////////////////////////////////////////////////////////////////
-// Name: gtk/evtloop.cpp
+// Name: src/gtk/evtloop.cpp
// Purpose: implements wxEventLoop for GTK+
// Author: Vadim Zeitlin
// Modified by:
// Created: 10.07.01
// RCS-ID: $Id$
// Copyright: (c) 2001 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
-// License: wxWindows license
+// License: wxWindows licence
///////////////////////////////////////////////////////////////////////////////
// ============================================================================
// headers
// ----------------------------------------------------------------------------
-#ifdef __GNUG__
- #pragma implementation "evtloop.h"
-#endif
-
// For compilers that support precompilation, includes "wx.h".
#include "wx/wxprec.h"
#pragma hdrstop
#endif
-#ifndef WX_PRECOMP
-#endif //WX_PRECOMP
-
#include "wx/evtloop.h"
-#include <gtk/gtk.h>
-
-// ----------------------------------------------------------------------------
-// wxEventLoopImpl
-// ----------------------------------------------------------------------------
-
-class WXDLLEXPORT wxEventLoopImpl
-{
-public:
- // ctor
- wxEventLoopImpl() { SetExitCode(0); }
-
- // set/get the exit code
- void SetExitCode(int exitcode) { m_exitcode = exitcode; }
- int GetExitCode() const { return m_exitcode; }
+#ifndef WX_PRECOMP
+ #include "wx/app.h"
+#endif // WX_PRECOMP
-private:
- // the exit code of the event loop
- int m_exitcode;
-};
+#include <gtk/gtk.h>
// ============================================================================
// wxEventLoop implementation
// wxEventLoop running and exiting
// ----------------------------------------------------------------------------
-wxEventLoop::~wxEventLoop()
-{
- wxASSERT_MSG( !m_impl, _T("should have been deleted in Run()") );
-}
-
-bool wxEventLoop::IsRunning() const
+wxGUIEventLoop::wxGUIEventLoop()
{
- return m_impl != NULL;
+ m_exitcode = 0;
}
-int wxEventLoop::Run()
+int wxGUIEventLoop::Run()
{
// event loops are not recursive, you need to create another loop!
- wxCHECK_MSG( !IsRunning(), -1, _T("can't reenter a message loop") );
+ wxCHECK_MSG( !IsRunning(), -1, "can't reenter a message loop" );
- m_impl = new wxEventLoopImpl;
+ wxEventLoopActivator activate(this);
gtk_main();
- int exitcode = m_impl->GetExitCode();
- delete m_impl;
- m_impl = NULL;
+ OnExit();
- return exitcode;
+ return m_exitcode;
}
-void wxEventLoop::Exit(int rc)
+void wxGUIEventLoop::Exit(int rc)
{
- wxCHECK_RET( IsRunning(), _T("can't call Exit() if not running") );
+ wxCHECK_RET( IsRunning(), "can't call Exit() if not running" );
- m_impl->SetExitCode(rc);
+ m_exitcode = rc;
gtk_main_quit();
}
+void wxGUIEventLoop::WakeUp()
+{
+ // TODO: idle events handling should really be done by wxEventLoop itself
+ // but for now it's completely in gtk/app.cpp so just call there when
+ // we have wxTheApp and hope that it doesn't matter that we do
+ // nothing when we don't...
+ if ( wxTheApp )
+ wxTheApp->WakeUpIdle();
+}
+
// ----------------------------------------------------------------------------
// wxEventLoop message processing dispatching
// ----------------------------------------------------------------------------
-bool wxEventLoop::Pending() const
+bool wxGUIEventLoop::Pending() const
{
- return gtk_events_pending() > 0;
+ if ( wxTheApp )
+ {
+ // this avoids false positives from our idle source
+ return wxTheApp->EventsPending();
+ }
+
+ return gtk_events_pending() != 0;
}
-bool wxEventLoop::Dispatch()
+bool wxGUIEventLoop::Dispatch()
{
- wxCHECK_MSG( IsRunning(), FALSE, _T("can't call Dispatch() if not running") );
+ wxCHECK_MSG( IsRunning(), false, _T("can't call Dispatch() if not running") );
- gtk_main_iteration();
+ // gtk_main_iteration() returns TRUE only if gtk_main_quit() was called
+ return !gtk_main_iteration();
+}
+
+extern "C" {
+static gboolean wx_event_loop_timeout(void* data)
+{
+ bool* expired = static_cast<bool*>(data);
+ *expired = true;
- return TRUE;
+ // return FALSE to remove this timeout
+ return FALSE;
+}
}
+int wxGUIEventLoop::DispatchTimeout(unsigned long timeout)
+{
+ bool expired = false;
+ const unsigned id = g_timeout_add(timeout, wx_event_loop_timeout, &expired);
+ bool quit = gtk_main_iteration() != 0;
+
+ if ( expired )
+ return -1;
+
+ g_source_remove(id);
+
+ return !quit;
+}
+
+