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