]> git.saurik.com Git - wxWidgets.git/blob - src/cocoa/timer.mm
cleanup
[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 // RCS-ID: $Id$
8 // Copyright: (c) Ryan Norton
9 // Licence: 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 #if wxUSE_TIMER
24
25 #include "wx/cocoa/private/timer.h"
26 #include "wx/cocoa/autorelease.h"
27
28 #include "wx/cocoa/objc/objc_uniquifying.h"
29
30 #import <Foundation/NSTimer.h>
31
32 // ========================================================================
33 // wxNSTimerData
34 // ========================================================================
35 @interface wxNSTimerData : NSObject
36 {
37 wxCocoaTimerImpl* m_timer;
38 }
39
40 - (id)init;
41 - (id)initWithWxTimer:(wxCocoaTimerImpl*)theTimer;
42 - (wxCocoaTimerImpl*)timer;
43 - (void)onNotify:(NSTimer *)theTimer;
44 @end // interface wxNSTimerData : NSObject
45 WX_DECLARE_GET_OBJC_CLASS(wxNSTimerData,NSObject)
46
47 @implementation wxNSTimerData : NSObject
48 - (id)init
49 {
50 if(!(self = [super init]))
51 return nil;
52 m_timer = NULL;
53 return self;
54 }
55
56 - (id)initWithWxTimer:(wxCocoaTimerImpl*)theTimer;
57 {
58 if(!(self = [super init]))
59 return nil;
60 m_timer = theTimer;
61 return self;
62 }
63
64 - (wxCocoaTimerImpl*)timer
65 {
66 return m_timer;
67 }
68
69 - (void)onNotify:(NSTimer *)theTimer
70 {
71 m_timer->Notify();
72 }
73 @end
74 WX_IMPLEMENT_GET_OBJC_CLASS(wxNSTimerData,NSObject)
75
76 // ----------------------------------------------------------------------------
77 // wxCocoaTimerImpl
78 // ----------------------------------------------------------------------------
79
80 wxCocoaTimerImpl::~wxCocoaTimerImpl()
81 {
82 Stop();
83 }
84
85 void wxCocoaTimerImpl::Init()
86 {
87 m_cocoaNSTimer = NULL;
88 }
89
90 bool wxCocoaTimerImpl::Start(int millisecs, bool oneShot)
91 {
92 Stop();
93
94 wxAutoNSAutoreleasePool thePool;
95
96 wxNSTimerData *timerData = [[WX_GET_OBJC_CLASS(wxNSTimerData) alloc] initWithWxTimer:this];
97 m_cocoaNSTimer = [[NSTimer
98 scheduledTimerWithTimeInterval: millisecs / 1000.0 //seconds
99 target: timerData
100 selector: @selector(onNotify:)
101 userInfo: nil
102 repeats: oneShot == false] retain];
103 [timerData release];
104
105 return IsRunning();
106 }
107
108 void wxCocoaTimerImpl::Stop()
109 {
110 if (m_cocoaNSTimer)
111 {
112 // FIXME: Is this safe to do if !isValid ?
113 [m_cocoaNSTimer invalidate];
114 [m_cocoaNSTimer release];
115 m_cocoaNSTimer = NULL;
116 }
117 }
118
119 bool wxCocoaTimerImpl::IsRunning() const
120 {
121 return m_cocoaNSTimer != NULL && [m_cocoaNSTimer isValid];
122 }
123
124 #endif // wxUSE_TIMER
125