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