]>
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 | ||
c2ca375c | 25 | #include "wx/cocoa/private/timer.h" |
0b6d4714 RN |
26 | #include "wx/cocoa/autorelease.h" |
27 | ||
2fcd7f64 RN |
28 | #import <Foundation/NSTimer.h> |
29 | ||
2fcd7f64 RN |
30 | // ======================================================================== |
31 | // wxNSTimerData | |
32 | // ======================================================================== | |
33 | @interface wxNSTimerData : NSObject | |
34 | { | |
c2ca375c | 35 | wxCocoaTimerImpl* m_timer; |
2fcd7f64 RN |
36 | } |
37 | ||
06c2bab0 | 38 | - (id)init; |
c2ca375c VZ |
39 | - (id)initWithWxTimer:(wxCocoaTimerImpl*)theTimer; |
40 | - (wxCocoaTimerImpl*)timer; | |
ae999b11 | 41 | - (void)onNotify:(NSTimer *)theTimer; |
2fcd7f64 RN |
42 | @end // interface wxNSTimerData : NSObject |
43 | ||
44 | @implementation wxNSTimerData : NSObject | |
06c2bab0 | 45 | - (id)init |
2fcd7f64 | 46 | { |
06c2bab0 DE |
47 | if(!(self = [super init])) |
48 | return nil; | |
49 | m_timer = NULL; | |
50 | return self; | |
51 | } | |
52 | ||
c2ca375c | 53 | - (id)initWithWxTimer:(wxCocoaTimerImpl*)theTimer; |
06c2bab0 DE |
54 | { |
55 | if(!(self = [super init])) | |
56 | return nil; | |
2fcd7f64 RN |
57 | m_timer = theTimer; |
58 | return self; | |
59 | } | |
06c2bab0 | 60 | |
c2ca375c | 61 | - (wxCocoaTimerImpl*)timer |
2fcd7f64 RN |
62 | { |
63 | return m_timer; | |
64 | } | |
2fcd7f64 | 65 | |
2fcd7f64 RN |
66 | - (void)onNotify:(NSTimer *)theTimer |
67 | { | |
c2ca375c | 68 | m_timer->Notify(); |
2fcd7f64 | 69 | } |
c0badb70 | 70 | @end |
2fcd7f64 RN |
71 | |
72 | // ---------------------------------------------------------------------------- | |
c2ca375c | 73 | // wxCocoaTimerImpl |
2fcd7f64 RN |
74 | // ---------------------------------------------------------------------------- |
75 | ||
c2ca375c | 76 | wxCocoaTimerImpl::~wxCocoaTimerImpl() |
2fcd7f64 RN |
77 | { |
78 | Stop(); | |
79 | } | |
80 | ||
c2ca375c | 81 | void wxCocoaTimerImpl::Init() |
2fcd7f64 RN |
82 | { |
83 | m_cocoaNSTimer = NULL; | |
84 | } | |
85 | ||
c2ca375c | 86 | bool wxCocoaTimerImpl::Start(int millisecs, bool oneShot) |
2fcd7f64 | 87 | { |
0b6d4714 | 88 | Stop(); |
c0badb70 | 89 | |
0b6d4714 RN |
90 | wxAutoNSAutoreleasePool thePool; |
91 | ||
ae999b11 | 92 | wxNSTimerData *timerData = [[wxNSTimerData alloc] initWithWxTimer:this]; |
c0badb70 | 93 | m_cocoaNSTimer = [[NSTimer |
2fcd7f64 | 94 | scheduledTimerWithTimeInterval: millisecs / 1000.0 //seconds |
c0badb70 WS |
95 | target: timerData |
96 | selector: @selector(onNotify:) | |
97 | userInfo: nil | |
98 | repeats: oneShot == false] retain]; | |
ae999b11 | 99 | [timerData release]; |
c0badb70 | 100 | |
2fcd7f64 RN |
101 | return IsRunning(); |
102 | } | |
103 | ||
c2ca375c | 104 | void wxCocoaTimerImpl::Stop() |
2fcd7f64 RN |
105 | { |
106 | if (m_cocoaNSTimer) | |
107 | { | |
06c2bab0 | 108 | // FIXME: Is this safe to do if !isValid ? |
2fcd7f64 | 109 | [m_cocoaNSTimer invalidate]; |
2fcd7f64 | 110 | [m_cocoaNSTimer release]; |
0b6d4714 | 111 | m_cocoaNSTimer = NULL; |
2fcd7f64 RN |
112 | } |
113 | } | |
114 | ||
c2ca375c | 115 | bool wxCocoaTimerImpl::IsRunning() const |
2fcd7f64 RN |
116 | { |
117 | return m_cocoaNSTimer != NULL && [m_cocoaNSTimer isValid]; | |
118 | } | |
119 | ||
120 | #endif // wxUSE_TIMER | |
121 |