Update ooo320-m1
[ooovba.git] / vcl / aqua / source / app / saltimer.cxx
blob9c0e6e071e15cde93af15c6fd24e07cb6df5f3ce
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: saltimer.cxx,v $
10 * $Revision: 1.20 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_vcl.hxx"
34 #include "saltimer.h"
35 #include "salnstimer.h"
36 #include "saldata.hxx"
37 #include "salframe.h"
38 #include "salinst.h"
40 // =======================================================================
42 NSTimer* AquaSalTimer::pRunningTimer = nil;
43 bool AquaSalTimer::bDispatchTimer = false;
46 void ImplSalStartTimer( ULONG nMS )
48 SalData* pSalData = GetSalData();
49 if( pSalData->mpFirstInstance->isNSAppThread() )
51 AquaSalTimer::bDispatchTimer = true;
52 NSTimeInterval aTI = double(nMS)/1000.0;
53 if( AquaSalTimer::pRunningTimer != nil )
55 if( [AquaSalTimer::pRunningTimer timeInterval] == aTI )
56 // set new fire date
57 [AquaSalTimer::pRunningTimer setFireDate: [NSDate dateWithTimeIntervalSinceNow: aTI]];
58 else
60 [AquaSalTimer::pRunningTimer invalidate];
61 AquaSalTimer::pRunningTimer = nil;
64 if( AquaSalTimer::pRunningTimer == nil )
66 AquaSalTimer::pRunningTimer = [NSTimer scheduledTimerWithTimeInterval: aTI
67 target: [[[TimerCallbackCaller alloc] init] autorelease]
68 selector: @selector(timerElapsed:)
69 userInfo: nil
70 repeats: YES];
71 /* #i84055# add timer to tracking run loop mode,
72 so they also elapse while e.g. life resize
74 [[NSRunLoop currentRunLoop] addTimer: AquaSalTimer::pRunningTimer forMode: NSEventTrackingRunLoopMode];
77 else
79 SalData::ensureThreadAutoreleasePool();
80 // post an event so we can get into the main thread
81 NSPoint aPt = { 0, 0 };
82 NSEvent* pEvent = [NSEvent otherEventWithType: NSApplicationDefined
83 location: aPt
84 modifierFlags: 0
85 timestamp: [NSDate timeIntervalSinceReferenceDate]
86 windowNumber: 0
87 context: nil
88 subtype: AquaSalInstance::AppStartTimerEvent
89 data1: (int)nMS
90 data2: 0 ];
91 if( pEvent )
92 [NSApp postEvent: pEvent atStart: YES];
96 void ImplSalStopTimer()
98 AquaSalTimer::bDispatchTimer = false;
101 void AquaSalTimer::handleStartTimerEvent( NSEvent* pEvent )
103 ImplSVData* pSVData = ImplGetSVData();
104 if( pSVData->mpSalTimer )
106 NSTimeInterval posted = [pEvent timestamp] + NSTimeInterval([pEvent data1])/1000.0;
107 NSTimeInterval current = [NSDate timeIntervalSinceReferenceDate];
108 if( (posted - current) <= 0.0 )
110 YIELD_GUARD;
111 // timer already elapsed since event posted
112 pSVData->mpSalTimer->CallCallback();
114 ImplSalStartTimer( ULONG( [pEvent data1] ) );
119 AquaSalTimer::AquaSalTimer( )
123 AquaSalTimer::~AquaSalTimer()
125 ImplSalStopTimer();
128 void AquaSalTimer::Start( ULONG nMS )
130 ImplSalStartTimer( nMS );
133 void AquaSalTimer::Stop()
135 ImplSalStopTimer();