Replace wxNSTimerData -setTimer method with -initWithWxTimer: initializer
[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 // wxNSTimerDelegate
41 // ========================================================================
42 @interface wxNSTimerDelegate : NSObject
43 {
44 }
45
46 - (void)onNotify:(NSTimer *)theTimer;
47 @end // interface wxNSTimerDelegate : NSObject
48
49 // ========================================================================
50 // wxNSTimerData
51 // ========================================================================
52 @interface wxNSTimerData : NSObject
53 {
54     wxTimer* m_timer;
55 }
56
57 - (id)init;
58 - (id)initWithWxTimer:(wxTimer*)theTimer;
59 - (wxTimer*)timer;
60 @end // interface wxNSTimerData : NSObject
61
62 @implementation wxNSTimerData : NSObject
63 - (id)init
64 {
65     if(!(self = [super init]))
66         return nil;
67     m_timer = NULL;
68     return self;
69 }
70
71 - (id)initWithWxTimer:(wxTimer*)theTimer;
72 {
73     if(!(self = [super init]))
74         return nil;
75     m_timer = theTimer;
76     return self;
77 }
78
79 - (wxTimer*)timer
80 {
81     return m_timer;
82 }
83 @end 
84
85 @implementation wxNSTimerDelegate : NSObject
86 - (void)onNotify:(NSTimer *)theTimer
87 {
88     wxNSTimerData* theData = [theTimer userInfo];
89     [theData timer]->Notify(); //wxTimerBase method
90 }
91 @end 
92
93 // ----------------------------------------------------------------------------
94 // wxTimer
95 // ----------------------------------------------------------------------------
96
97 const wxObjcAutoRefFromAlloc<struct objc_object*> wxTimer::sm_cocoaDelegate = [[wxNSTimerDelegate alloc] init];
98
99 wxTimer::~wxTimer()
100 {
101     Stop();
102 }
103
104 void wxTimer::Init()
105 {
106     m_cocoaNSTimer = NULL;
107 }
108
109 bool wxTimer::Start(int millisecs, bool oneShot)
110 {
111     Stop();
112     
113     wxAutoNSAutoreleasePool thePool;
114
115     wxNSTimerData *userInfo = [[wxNSTimerData alloc] initWithWxTimer:this];
116     m_cocoaNSTimer =     [[NSTimer 
117             scheduledTimerWithTimeInterval: millisecs / 1000.0 //seconds
118             target:             wxTimer::sm_cocoaDelegate
119             selector:   @selector(onNotify:) 
120             userInfo:   userInfo
121             repeats:    oneShot == false] retain];
122     [userInfo release];
123                        
124     return IsRunning();
125 }
126
127 void wxTimer::Stop()
128 {
129     if (m_cocoaNSTimer)
130     {
131         // FIXME: Is this safe to do if !isValid ?
132         [m_cocoaNSTimer invalidate];
133         [m_cocoaNSTimer release];
134         m_cocoaNSTimer = NULL;
135     }
136 }
137
138 bool wxTimer::IsRunning() const
139 {
140     return m_cocoaNSTimer != NULL && [m_cocoaNSTimer isValid];
141 }
142
143 #endif // wxUSE_TIMER
144