Extremely simple implementation of wxEventLoop using wxGTK as a skeleton
[wxWidgets.git] / src / cocoa / evtloop.mm
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name:        cocoa/evtloop.mm
3 // Purpose:     implements wxEventLoop for Cocoa
4 // Author:      David Elliott
5 // Modified by:
6 // Created:     2003/10/02
7 // RCS-ID:      $Id$
8 // Copyright:   (c) 2003 David Elliott <dfe@cox.net>
9 // License:     wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 #include "wx/wxprec.h"
13 #ifndef WX_PRECOMP
14     #include "wx/log.h"
15 #endif //WX_PRECOMP
16
17 #include "wx/evtloop.h"
18
19 #import <AppKit/NSApplication.h>
20
21 // ========================================================================
22 // wxEventLoopImpl
23 // ========================================================================
24
25 class WXDLLEXPORT wxEventLoopImpl
26 {
27 public:
28     // ctor
29     wxEventLoopImpl() { SetExitCode(0); }
30
31     // set/get the exit code
32     void SetExitCode(int exitcode) { m_exitcode = exitcode; }
33     int GetExitCode() const { return m_exitcode; }
34
35 private:
36     // the exit code of the event loop
37     int m_exitcode;
38 };
39
40 // ========================================================================
41 // wxEventLoop
42 // ========================================================================
43
44 // ----------------------------------------------------------------------------
45 // wxEventLoop running and exiting
46 // ----------------------------------------------------------------------------
47
48 wxEventLoop *wxEventLoop::ms_activeLoop = NULL;
49
50 wxEventLoop::~wxEventLoop()
51 {
52     wxASSERT_MSG( !m_impl, _T("should have been deleted in Run()") );
53 }
54
55 bool wxEventLoop::IsRunning() const
56 {
57     return m_impl;
58 }
59
60 int wxEventLoop::Run()
61 {
62     // event loops are not recursive, you need to create another loop!
63     wxCHECK_MSG( !IsRunning(), -1, _T("can't reenter a message loop") );
64
65     wxEventLoop *oldLoop = ms_activeLoop;
66     ms_activeLoop = this;
67
68     m_impl = new wxEventLoopImpl;
69
70     [[NSApplication sharedApplication] run];
71
72     int exitcode = m_impl->GetExitCode();
73     delete m_impl;
74     m_impl = NULL;
75
76     ms_activeLoop = oldLoop;
77
78     return exitcode;
79 }
80
81 void wxEventLoop::Exit(int rc)
82 {
83     wxCHECK_RET( IsRunning(), _T("can't call Exit() if not running") );
84
85     m_impl->SetExitCode(rc);
86
87     NSApplication *cocoaApp = [NSApplication sharedApplication];
88     wxLogDebug("wxEventLoop::Exit isRunning=%d", (int)[cocoaApp isRunning]);
89     [cocoaApp stop: cocoaApp];
90 }
91
92 // ----------------------------------------------------------------------------
93 // wxEventLoop message processing dispatching
94 // ----------------------------------------------------------------------------
95
96 bool wxEventLoop::Pending() const
97 {
98     return 0;
99 }
100
101 bool wxEventLoop::Dispatch()
102 {
103     wxCHECK_MSG( IsRunning(), false, _T("can't call Dispatch() if not running") );
104     return false;
105 }
106