]> git.saurik.com Git - wxWidgets.git/blob - src/cocoa/timer.mm
cocoa wxTimer
[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:
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 #ifndef WX_PRECOMP
26 #include "wx/timer.h"
27 #endif
28
29 #import <Foundation/NSTimer.h>
30
31 // ============================================================================
32 // implementation
33 // ============================================================================
34
35 IMPLEMENT_CLASS(wxTimer, wxTimerBase)
36
37 // ========================================================================
38 // wxNSTimerDelegate
39 // ========================================================================
40 @interface wxNSTimerDelegate : NSObject
41 {
42 }
43
44 - (void)onNotify:(NSTimer *)theTimer;
45 @end // interface wxNSTimerDelegate : NSObject
46
47 // ========================================================================
48 // wxNSTimerData
49 // ========================================================================
50 @interface wxNSTimerData : NSObject
51 {
52 wxTimer* m_timer;
53 }
54
55 - (id)setTimer:(wxTimer*)theTimer;
56 - (wxTimer*)timer;
57 @end // interface wxNSTimerData : NSObject
58
59 @implementation wxNSTimerData : NSObject
60 - (id)setTimer:(wxTimer*)theTimer;
61 {
62 m_timer = theTimer;
63 return self;
64 }
65 - (wxTimer*)timer
66 {
67 return m_timer;
68 }
69 @end
70
71 @implementation wxNSTimerDelegate : NSObject
72 - (void)onNotify:(NSTimer *)theTimer
73 {
74 wxNSTimerData* theData = [theTimer userInfo];
75 [theData timer]->Notify(); //wxTimerBase method
76 }
77 @end
78
79 // ----------------------------------------------------------------------------
80 // wxTimer
81 // ----------------------------------------------------------------------------
82
83 const wxObjcAutoRefFromAlloc<struct objc_object*> wxTimer::sm_cocoaDelegate = [[wxNSTimerDelegate alloc] init];
84
85 wxTimer::~wxTimer()
86 {
87 Stop();
88 }
89
90 void wxTimer::Init()
91 {
92 m_cocoaNSTimer = NULL;
93 }
94
95 bool wxTimer::Start(int millisecs, bool oneShot)
96 {
97 m_cocoaNSTimer = [[NSTimer
98 scheduledTimerWithTimeInterval: millisecs / 1000.0 //seconds
99 target: wxTimer::sm_cocoaDelegate
100 selector: @selector(onNotify:)
101 userInfo: [[wxNSTimerData alloc] setTimer:this]
102 repeats: oneShot == false] retain];
103
104 return IsRunning();
105 }
106
107 void wxTimer::Stop()
108 {
109 if (m_cocoaNSTimer)
110 {
111 [m_cocoaNSTimer invalidate];
112 [[m_cocoaNSTimer userInfo] release];
113 [m_cocoaNSTimer release];
114 }
115 }
116
117 bool wxTimer::IsRunning() const
118 {
119 return m_cocoaNSTimer != NULL && [m_cocoaNSTimer isValid];
120 }
121
122 #endif // wxUSE_TIMER
123