1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: saltimer.cxx,v $
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"
35 #include "salnstimer.h"
36 #include "saldata.hxx"
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
)
57 [AquaSalTimer::pRunningTimer setFireDate
: [NSDate dateWithTimeIntervalSinceNow
: aTI
]];
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
:)
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
];
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
85 timestamp
: [NSDate timeIntervalSinceReferenceDate
]
88 subtype
: AquaSalInstance::AppStartTimerEvent
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 )
111 // timer already elapsed since event posted
112 pSVData
->mpSalTimer
->CallCallback();
114 ImplSalStartTimer( ULONG( [pEvent data1
] ) );
119 AquaSalTimer::AquaSalTimer( )
123 AquaSalTimer::~AquaSalTimer()
128 void AquaSalTimer::Start( ULONG nMS
)
130 ImplSalStartTimer( nMS
);
133 void AquaSalTimer::Stop()