]> git.saurik.com Git - wxWidgets.git/blob - src/cocoa/timer.mm
Minor corrections to XRC format description.
[wxWidgets.git] / src / cocoa / timer.mm
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/cocoa/timer.mm
3 // Purpose: wxTimer for wxCocoa
4 // Author: Ryan Norton
5 // Modified by: David Elliott
6 // Created: 2005-02-04
7 // Copyright: (c) Ryan Norton
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 // ============================================================================
12 // declarations
13 // ============================================================================
14
15 // ----------------------------------------------------------------------------
16 // headers
17 // ----------------------------------------------------------------------------
18
19 // For compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
21
22 #if wxUSE_TIMER
23
24 #include "wx/cocoa/private/timer.h"
25 #include "wx/cocoa/autorelease.h"
26
27 #include "wx/cocoa/objc/objc_uniquifying.h"
28
29 #import <Foundation/NSTimer.h>
30
31 // ========================================================================
32 // wxNSTimerData
33 // ========================================================================
34 @interface wxNSTimerData : NSObject
35 {
36 wxCocoaTimerImpl* m_timer;
37 }
38
39 - (id)init;
40 - (id)initWithWxTimer:(wxCocoaTimerImpl*)theTimer;
41 - (wxCocoaTimerImpl*)timer;
42 - (void)onNotify:(NSTimer *)theTimer;
43 @end // interface wxNSTimerData : NSObject
44 WX_DECLARE_GET_OBJC_CLASS(wxNSTimerData,NSObject)
45
46 @implementation wxNSTimerData : NSObject
47 - (id)init
48 {
49 if(!(self = [super init]))
50 return nil;
51 m_timer = NULL;
52 return self;
53 }
54
55 - (id)initWithWxTimer:(wxCocoaTimerImpl*)theTimer;
56 {
57 if(!(self = [super init]))
58 return nil;
59 m_timer = theTimer;
60 return self;
61 }
62
63 - (wxCocoaTimerImpl*)timer
64 {
65 return m_timer;
66 }
67
68 - (void)onNotify:(NSTimer *)theTimer
69 {
70 m_timer->Notify();
71 }
72 @end
73 WX_IMPLEMENT_GET_OBJC_CLASS(wxNSTimerData,NSObject)
74
75 // ----------------------------------------------------------------------------
76 // wxCocoaTimerImpl
77 // ----------------------------------------------------------------------------
78
79 wxCocoaTimerImpl::~wxCocoaTimerImpl()
80 {
81 Stop();
82 }
83
84 void wxCocoaTimerImpl::Init()
85 {
86 m_cocoaNSTimer = NULL;
87 }
88
89 bool wxCocoaTimerImpl::Start(int millisecs, bool oneShot)
90 {
91 Stop();
92
93 wxAutoNSAutoreleasePool thePool;
94
95 wxNSTimerData *timerData = [[WX_GET_OBJC_CLASS(wxNSTimerData) alloc] initWithWxTimer:this];
96 m_cocoaNSTimer = [[NSTimer
97 scheduledTimerWithTimeInterval: millisecs / 1000.0 //seconds
98 target: timerData
99 selector: @selector(onNotify:)
100 userInfo: nil
101 repeats: oneShot == false] retain];
102 [timerData release];
103
104 return IsRunning();
105 }
106
107 void wxCocoaTimerImpl::Stop()
108 {
109 if (m_cocoaNSTimer)
110 {
111 // FIXME: Is this safe to do if !isValid ?
112 [m_cocoaNSTimer invalidate];
113 [m_cocoaNSTimer release];
114 m_cocoaNSTimer = NULL;
115 }
116 }
117
118 bool wxCocoaTimerImpl::IsRunning() const
119 {
120 return m_cocoaNSTimer != NULL && [m_cocoaNSTimer isValid];
121 }
122
123 #endif // wxUSE_TIMER
124