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