share wxEventLoop::IsRunning() implementation between all ports; moved wxEventLoopAct...
[wxWidgets.git] / src / gtk1 / evtloop.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: gtk/evtloop.cpp
3 // Purpose: implements wxEventLoop for GTK+
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 10.07.01
7 // RCS-ID: $Id$
8 // Copyright: (c) 2001 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // License: 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 #include "wx/app.h"
29
30 #include <gtk/gtk.h>
31
32 // ----------------------------------------------------------------------------
33 // wxEventLoopImpl
34 // ----------------------------------------------------------------------------
35
36 class WXDLLEXPORT wxEventLoopImpl
37 {
38 public:
39 // ctor
40 wxEventLoopImpl() { SetExitCode(0); }
41
42 // set/get the exit code
43 void SetExitCode(int exitcode) { m_exitcode = exitcode; }
44 int GetExitCode() const { return m_exitcode; }
45
46 private:
47 // the exit code of the event loop
48 int m_exitcode;
49 };
50
51 // ============================================================================
52 // wxEventLoop implementation
53 // ============================================================================
54
55 // ----------------------------------------------------------------------------
56 // wxEventLoop running and exiting
57 // ----------------------------------------------------------------------------
58
59 wxEventLoop *wxEventLoopBase::ms_activeLoop = NULL;
60
61 wxEventLoop::~wxEventLoop()
62 {
63 wxASSERT_MSG( !m_impl, _T("should have been deleted in Run()") );
64 }
65
66 int wxEventLoop::Run()
67 {
68 // event loops are not recursive, you need to create another loop!
69 wxCHECK_MSG( !IsRunning(), -1, _T("can't reenter a message loop") );
70
71 wxEventLoopActivator activate(this);
72
73 m_impl = new wxEventLoopImpl;
74
75 gtk_main();
76
77 int exitcode = m_impl->GetExitCode();
78 delete m_impl;
79 m_impl = NULL;
80
81 return exitcode;
82 }
83
84 void wxEventLoop::Exit(int rc)
85 {
86 wxCHECK_RET( IsRunning(), _T("can't call Exit() if not running") );
87
88 m_impl->SetExitCode(rc);
89
90 gtk_main_quit();
91 }
92
93 // ----------------------------------------------------------------------------
94 // wxEventLoop message processing dispatching
95 // ----------------------------------------------------------------------------
96
97 bool wxEventLoop::Pending() const
98 {
99 if (wxTheApp)
100 {
101 // We need to remove idle callbacks or gtk_events_pending will
102 // never return false.
103 wxTheApp->RemoveIdleTag();
104 }
105
106 return gtk_events_pending();
107 }
108
109 bool wxEventLoop::Dispatch()
110 {
111 wxCHECK_MSG( IsRunning(), FALSE, _T("can't call Dispatch() if not running") );
112
113 gtk_main_iteration();
114
115 return TRUE;
116 }
117