]> git.saurik.com Git - wxWidgets.git/blame - src/cocoa/evtloop.mm
removed src/gtk/eggtrayicon.h
[wxWidgets.git] / src / cocoa / evtloop.mm
CommitLineData
f5e10026
DE
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>
065e208e 9// License: wxWidgets licence
f5e10026
DE
10///////////////////////////////////////////////////////////////////////////////
11
12#include "wx/wxprec.h"
13#ifndef WX_PRECOMP
14 #include "wx/log.h"
eb537cfb 15 #include "wx/app.h"
f5e10026
DE
16#endif //WX_PRECOMP
17
18#include "wx/evtloop.h"
19
20#import <AppKit/NSApplication.h>
72ae85dd
DE
21#import <AppKit/NSEvent.h>
22#import <Foundation/NSRunLoop.h>
f5e10026
DE
23
24// ========================================================================
25// wxEventLoopImpl
26// ========================================================================
27
28class WXDLLEXPORT wxEventLoopImpl
29{
30public:
31 // ctor
32 wxEventLoopImpl() { SetExitCode(0); }
33
34 // set/get the exit code
35 void SetExitCode(int exitcode) { m_exitcode = exitcode; }
36 int GetExitCode() const { return m_exitcode; }
37
38private:
39 // the exit code of the event loop
40 int m_exitcode;
41};
42
43// ========================================================================
44// wxEventLoop
45// ========================================================================
46
47// ----------------------------------------------------------------------------
48// wxEventLoop running and exiting
49// ----------------------------------------------------------------------------
50
3754265e 51wxEventLoop *wxEventLoopBase::ms_activeLoop = NULL;
f5e10026
DE
52
53wxEventLoop::~wxEventLoop()
54{
55 wxASSERT_MSG( !m_impl, _T("should have been deleted in Run()") );
56}
57
f5e10026
DE
58int wxEventLoop::Run()
59{
60 // event loops are not recursive, you need to create another loop!
61 wxCHECK_MSG( !IsRunning(), -1, _T("can't reenter a message loop") );
62
63 wxEventLoop *oldLoop = ms_activeLoop;
64 ms_activeLoop = this;
65
66 m_impl = new wxEventLoopImpl;
67
68 [[NSApplication sharedApplication] run];
69
70 int exitcode = m_impl->GetExitCode();
71 delete m_impl;
72 m_impl = NULL;
73
74 ms_activeLoop = oldLoop;
75
76 return exitcode;
77}
78
79void wxEventLoop::Exit(int rc)
80{
81 wxCHECK_RET( IsRunning(), _T("can't call Exit() if not running") );
82
83 m_impl->SetExitCode(rc);
84
85 NSApplication *cocoaApp = [NSApplication sharedApplication];
48580976 86 wxLogTrace(wxTRACE_COCOA,wxT("wxEventLoop::Exit isRunning=%d"), (int)[cocoaApp isRunning]);
eb537cfb 87 wxTheApp->WakeUpIdle();
9670dfc4 88 /* Notes:
eb537cfb
DE
89 If we're being called from idle time (which occurs while checking the
90 queue for new events) there may or may not be any events in the queue.
91 In order to successfully stop the event loop, at least one event must
92 be processed. To ensure this always happens, WakeUpIdle is called.
9670dfc4
DE
93
94 If the application was active when closed then this is unnecessary
95 because it would receive a deactivate event anyway. However, if the
96 application was not active when closed, then no events would be
97 added to the queue by Cocoa and thus the application would wait
98 indefinitely for the next event.
99 */
f5e10026
DE
100 [cocoaApp stop: cocoaApp];
101}
102
103// ----------------------------------------------------------------------------
104// wxEventLoop message processing dispatching
105// ----------------------------------------------------------------------------
106
107bool wxEventLoop::Pending() const
108{
72ae85dd
DE
109 // a pointer to the event is returned if there is one, or nil if not
110 return [[NSApplication sharedApplication]
111 nextEventMatchingMask: NSAnyEventMask
112 untilDate: nil /* Equivalent to [NSDate distantPast] */
113 inMode: NSDefaultRunLoopMode
114 dequeue: NO];
f5e10026
DE
115}
116
117bool wxEventLoop::Dispatch()
118{
72ae85dd
DE
119 // This check is required by wxGTK but probably not really for wxCocoa
120 // Keep it here to encourage developers to write cross-platform code
f5e10026 121 wxCHECK_MSG( IsRunning(), false, _T("can't call Dispatch() if not running") );
72ae85dd
DE
122 NSApplication *cocoaApp = [NSApplication sharedApplication];
123 // Block to retrieve an event then send it
124 if(NSEvent *event = [cocoaApp
125 nextEventMatchingMask:NSAnyEventMask
126 untilDate:[NSDate distantFuture]
127 inMode:NSDefaultRunLoopMode
128 dequeue: YES])
129 {
130 [cocoaApp sendEvent: event];
131 return true;
132 }
f5e10026
DE
133 return false;
134}
135