]> git.saurik.com Git - wxWidgets.git/blob - src/cocoa/timer.mm
in case of an faulty event this might not get initialized
[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 #ifndef WX_PRECOMP
26 #include "wx/timer.h"
27 #endif
28
29 #include "wx/cocoa/autorelease.h"
30
31 #import <Foundation/NSTimer.h>
32
33 // ============================================================================
34 // implementation
35 // ============================================================================
36
37 IMPLEMENT_CLASS(wxTimer, wxTimerBase)
38
39 // ========================================================================
40 // wxNSTimerData
41 // ========================================================================
42 @interface wxNSTimerData : NSObject
43 {
44 wxTimer* m_timer;
45 }
46
47 - (id)init;
48 - (id)initWithWxTimer:(wxTimer*)theTimer;
49 - (wxTimer*)timer;
50 - (void)onNotify:(NSTimer *)theTimer;
51 @end // interface wxNSTimerData : NSObject
52
53 @implementation wxNSTimerData : NSObject
54 - (id)init
55 {
56 if(!(self = [super init]))
57 return nil;
58 m_timer = NULL;
59 return self;
60 }
61
62 - (id)initWithWxTimer:(wxTimer*)theTimer;
63 {
64 if(!(self = [super init]))
65 return nil;
66 m_timer = theTimer;
67 return self;
68 }
69
70 - (wxTimer*)timer
71 {
72 return m_timer;
73 }
74
75 - (void)onNotify:(NSTimer *)theTimer
76 {
77 m_timer->Notify(); //wxTimerBase method
78 }
79 @end
80
81 // ----------------------------------------------------------------------------
82 // wxTimer
83 // ----------------------------------------------------------------------------
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 Stop();
98
99 wxAutoNSAutoreleasePool thePool;
100
101 wxNSTimerData *timerData = [[wxNSTimerData alloc] initWithWxTimer:this];
102 m_cocoaNSTimer = [[NSTimer
103 scheduledTimerWithTimeInterval: millisecs / 1000.0 //seconds
104 target: timerData
105 selector: @selector(onNotify:)
106 userInfo: nil
107 repeats: oneShot == false] retain];
108 [timerData release];
109
110 return IsRunning();
111 }
112
113 void wxTimer::Stop()
114 {
115 if (m_cocoaNSTimer)
116 {
117 // FIXME: Is this safe to do if !isValid ?
118 [m_cocoaNSTimer invalidate];
119 [m_cocoaNSTimer release];
120 m_cocoaNSTimer = NULL;
121 }
122 }
123
124 bool wxTimer::IsRunning() const
125 {
126 return m_cocoaNSTimer != NULL && [m_cocoaNSTimer isValid];
127 }
128
129 #endif // wxUSE_TIMER
130