]>
Commit | Line | Data |
---|---|---|
2fcd7f64 RN |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: src/cocoa/timer.mm | |
3 | // Purpose: wxTimer for wxCocoa | |
4 | // Author: Ryan Norton | |
06c2bab0 | 5 | // Modified by: David Elliott |
2fcd7f64 RN |
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 | ||
c0badb70 WS |
25 | #include "wx/timer.h" |
26 | ||
2fcd7f64 | 27 | #ifndef WX_PRECOMP |
2fcd7f64 RN |
28 | #endif |
29 | ||
0b6d4714 RN |
30 | #include "wx/cocoa/autorelease.h" |
31 | ||
2fcd7f64 RN |
32 | #import <Foundation/NSTimer.h> |
33 | ||
34 | // ============================================================================ | |
35 | // implementation | |
36 | // ============================================================================ | |
37 | ||
38 | IMPLEMENT_CLASS(wxTimer, wxTimerBase) | |
39 | ||
2fcd7f64 RN |
40 | // ======================================================================== |
41 | // wxNSTimerData | |
42 | // ======================================================================== | |
43 | @interface wxNSTimerData : NSObject | |
44 | { | |
45 | wxTimer* m_timer; | |
46 | } | |
47 | ||
06c2bab0 DE |
48 | - (id)init; |
49 | - (id)initWithWxTimer:(wxTimer*)theTimer; | |
2fcd7f64 | 50 | - (wxTimer*)timer; |
ae999b11 | 51 | - (void)onNotify:(NSTimer *)theTimer; |
2fcd7f64 RN |
52 | @end // interface wxNSTimerData : NSObject |
53 | ||
54 | @implementation wxNSTimerData : NSObject | |
06c2bab0 | 55 | - (id)init |
2fcd7f64 | 56 | { |
06c2bab0 DE |
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; | |
2fcd7f64 RN |
67 | m_timer = theTimer; |
68 | return self; | |
69 | } | |
06c2bab0 | 70 | |
2fcd7f64 RN |
71 | - (wxTimer*)timer |
72 | { | |
73 | return m_timer; | |
74 | } | |
2fcd7f64 | 75 | |
2fcd7f64 RN |
76 | - (void)onNotify:(NSTimer *)theTimer |
77 | { | |
ae999b11 | 78 | m_timer->Notify(); //wxTimerBase method |
2fcd7f64 | 79 | } |
c0badb70 | 80 | @end |
2fcd7f64 RN |
81 | |
82 | // ---------------------------------------------------------------------------- | |
83 | // wxTimer | |
84 | // ---------------------------------------------------------------------------- | |
85 | ||
2fcd7f64 RN |
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 | { | |
0b6d4714 | 98 | Stop(); |
c0badb70 | 99 | |
0b6d4714 RN |
100 | wxAutoNSAutoreleasePool thePool; |
101 | ||
ae999b11 | 102 | wxNSTimerData *timerData = [[wxNSTimerData alloc] initWithWxTimer:this]; |
c0badb70 | 103 | m_cocoaNSTimer = [[NSTimer |
2fcd7f64 | 104 | scheduledTimerWithTimeInterval: millisecs / 1000.0 //seconds |
c0badb70 WS |
105 | target: timerData |
106 | selector: @selector(onNotify:) | |
107 | userInfo: nil | |
108 | repeats: oneShot == false] retain]; | |
ae999b11 | 109 | [timerData release]; |
c0badb70 | 110 | |
2fcd7f64 RN |
111 | return IsRunning(); |
112 | } | |
113 | ||
114 | void wxTimer::Stop() | |
115 | { | |
116 | if (m_cocoaNSTimer) | |
117 | { | |
06c2bab0 | 118 | // FIXME: Is this safe to do if !isValid ? |
2fcd7f64 | 119 | [m_cocoaNSTimer invalidate]; |
2fcd7f64 | 120 | [m_cocoaNSTimer release]; |
0b6d4714 | 121 | m_cocoaNSTimer = NULL; |
2fcd7f64 RN |
122 | } |
123 | } | |
124 | ||
125 | bool wxTimer::IsRunning() const | |
126 | { | |
127 | return m_cocoaNSTimer != NULL && [m_cocoaNSTimer isValid]; | |
128 | } | |
129 | ||
130 | #endif // wxUSE_TIMER | |
131 |