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