]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: src/cocoa/timer.mm | |
3 | // Purpose: wxTimer for wxCocoa | |
4 | // Author: Ryan Norton | |
5 | // Modified by: | |
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)setTimer:(wxTimer*)theTimer; | |
58 | - (wxTimer*)timer; | |
59 | @end // interface wxNSTimerData : NSObject | |
60 | ||
61 | @implementation wxNSTimerData : NSObject | |
62 | - (id)setTimer:(wxTimer*)theTimer; | |
63 | { | |
64 | m_timer = theTimer; | |
65 | return self; | |
66 | } | |
67 | - (wxTimer*)timer | |
68 | { | |
69 | return m_timer; | |
70 | } | |
71 | @end | |
72 | ||
73 | @implementation wxNSTimerDelegate : NSObject | |
74 | - (void)onNotify:(NSTimer *)theTimer | |
75 | { | |
76 | wxNSTimerData* theData = [theTimer userInfo]; | |
77 | [theData timer]->Notify(); //wxTimerBase method | |
78 | } | |
79 | @end | |
80 | ||
81 | // ---------------------------------------------------------------------------- | |
82 | // wxTimer | |
83 | // ---------------------------------------------------------------------------- | |
84 | ||
85 | const wxObjcAutoRefFromAlloc<struct objc_object*> wxTimer::sm_cocoaDelegate = [[wxNSTimerDelegate alloc] init]; | |
86 | ||
87 | wxTimer::~wxTimer() | |
88 | { | |
89 | Stop(); | |
90 | } | |
91 | ||
92 | void wxTimer::Init() | |
93 | { | |
94 | m_cocoaNSTimer = NULL; | |
95 | } | |
96 | ||
97 | bool wxTimer::Start(int millisecs, bool oneShot) | |
98 | { | |
99 | Stop(); | |
100 | ||
101 | wxAutoNSAutoreleasePool thePool; | |
102 | ||
103 | m_cocoaNSTimer = [[NSTimer | |
104 | scheduledTimerWithTimeInterval: millisecs / 1000.0 //seconds | |
105 | target: wxTimer::sm_cocoaDelegate | |
106 | selector: @selector(onNotify:) | |
107 | userInfo: [[wxNSTimerData alloc] setTimer:this] | |
108 | repeats: oneShot == false] retain]; | |
109 | ||
110 | return IsRunning(); | |
111 | } | |
112 | ||
113 | void wxTimer::Stop() | |
114 | { | |
115 | if (m_cocoaNSTimer) | |
116 | { | |
117 | NSObject* theUserInfo = [m_cocoaNSTimer userInfo]; | |
118 | [m_cocoaNSTimer invalidate]; | |
119 | [theUserInfo release]; | |
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 |