cocoa wxTimer

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@31752 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Ryan Norton
2005-02-04 17:21:28 +00:00
parent 086d7f3177
commit 2fcd7f64a5
5 changed files with 200 additions and 22 deletions

123
src/cocoa/timer.mm Normal file
View File

@@ -0,0 +1,123 @@
/////////////////////////////////////////////////////////////////////////////
// Name: src/cocoa/timer.mm
// Purpose: wxTimer for wxCocoa
// Author: Ryan Norton
// Modified by:
// Created: 2005-02-04
// RCS-ID: $Id$
// Copyright: (c) Ryan Norton
// Licence: wxWindows licence
/////////////////////////////////////////////////////////////////////////////
// ============================================================================
// declarations
// ============================================================================
// ----------------------------------------------------------------------------
// headers
// ----------------------------------------------------------------------------
// For compilers that support precompilation, includes "wx.h".
#include "wx/wxprec.h"
#if wxUSE_TIMER
#ifndef WX_PRECOMP
#include "wx/timer.h"
#endif
#import <Foundation/NSTimer.h>
// ============================================================================
// implementation
// ============================================================================
IMPLEMENT_CLASS(wxTimer, wxTimerBase)
// ========================================================================
// wxNSTimerDelegate
// ========================================================================
@interface wxNSTimerDelegate : NSObject
{
}
- (void)onNotify:(NSTimer *)theTimer;
@end // interface wxNSTimerDelegate : NSObject
// ========================================================================
// wxNSTimerData
// ========================================================================
@interface wxNSTimerData : NSObject
{
wxTimer* m_timer;
}
- (id)setTimer:(wxTimer*)theTimer;
- (wxTimer*)timer;
@end // interface wxNSTimerData : NSObject
@implementation wxNSTimerData : NSObject
- (id)setTimer:(wxTimer*)theTimer;
{
m_timer = theTimer;
return self;
}
- (wxTimer*)timer
{
return m_timer;
}
@end
@implementation wxNSTimerDelegate : NSObject
- (void)onNotify:(NSTimer *)theTimer
{
wxNSTimerData* theData = [theTimer userInfo];
[theData timer]->Notify(); //wxTimerBase method
}
@end
// ----------------------------------------------------------------------------
// wxTimer
// ----------------------------------------------------------------------------
const wxObjcAutoRefFromAlloc<struct objc_object*> wxTimer::sm_cocoaDelegate = [[wxNSTimerDelegate alloc] init];
wxTimer::~wxTimer()
{
Stop();
}
void wxTimer::Init()
{
m_cocoaNSTimer = NULL;
}
bool wxTimer::Start(int millisecs, bool oneShot)
{
m_cocoaNSTimer = [[NSTimer
scheduledTimerWithTimeInterval: millisecs / 1000.0 //seconds
target: wxTimer::sm_cocoaDelegate
selector: @selector(onNotify:)
userInfo: [[wxNSTimerData alloc] setTimer:this]
repeats: oneShot == false] retain];
return IsRunning();
}
void wxTimer::Stop()
{
if (m_cocoaNSTimer)
{
[m_cocoaNSTimer invalidate];
[[m_cocoaNSTimer userInfo] release];
[m_cocoaNSTimer release];
}
}
bool wxTimer::IsRunning() const
{
return m_cocoaNSTimer != NULL && [m_cocoaNSTimer isValid];
}
#endif // wxUSE_TIMER