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