1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <vcl/svapp.hxx>
22 #include <idlemgr.hxx>
24 // =======================================================================
29 sal_uInt16 mnPriority
;
33 #define IMPL_IDLETIMEOUT 350
35 // =======================================================================
37 ImplIdleMgr::ImplIdleMgr()
39 mpIdleList
= new ImplIdleList();
41 maTimer
.SetTimeout( IMPL_IDLETIMEOUT
);
42 maTimer
.SetTimeoutHdl( LINK( this, ImplIdleMgr
, TimeoutHdl
) );
45 // -----------------------------------------------------------------------
47 ImplIdleMgr::~ImplIdleMgr()
50 for ( size_t i
= 0, n
= mpIdleList
->size(); i
< n
; ++i
) {
51 delete (*mpIdleList
)[ i
];
57 // -----------------------------------------------------------------------
59 sal_Bool
ImplIdleMgr::InsertIdleHdl( const Link
& rLink
, sal_uInt16 nPriority
)
61 size_t nPos
= (size_t)-1;
62 size_t n
= mpIdleList
->size();
63 for ( size_t i
= 0; i
< n
; ++i
) {
64 // we need to check each element to verify that rLink isn't in the array
65 if ( (*mpIdleList
)[ i
]->maIdleHdl
== rLink
) {
68 if ( nPriority
<= (*mpIdleList
)[ i
]->mnPriority
) {
73 ImplIdleData
* pIdleData
= new ImplIdleData
;
74 pIdleData
->maIdleHdl
= rLink
;
75 pIdleData
->mnPriority
= nPriority
;
76 pIdleData
->mbTimeout
= false;
78 if ( nPos
< mpIdleList
->size() ) {
79 ImplIdleList::iterator it
= mpIdleList
->begin();
80 ::std::advance( it
, nPos
);
81 mpIdleList
->insert( it
, pIdleData
);
83 mpIdleList
->push_back( pIdleData
);
86 // if Timer was not started already then start it now
87 if ( !maTimer
.IsActive() )
93 // -----------------------------------------------------------------------
95 void ImplIdleMgr::RemoveIdleHdl( const Link
& rLink
)
97 for ( ImplIdleList::iterator it
= mpIdleList
->begin(); it
!= mpIdleList
->end(); ++it
) {
98 if ( (*it
)->maIdleHdl
== rLink
) {
100 mpIdleList
->erase( it
);
105 // there are no more handlers...
106 if ( mpIdleList
->empty() )
110 // -----------------------------------------------------------------------
112 IMPL_LINK_NOARG(ImplIdleMgr
, TimeoutHdl
)
114 for ( size_t i
= 0; i
< mpIdleList
->size(); ++i
) {
115 ImplIdleData
* pIdleData
= (*mpIdleList
)[ i
];
116 if ( !pIdleData
->mbTimeout
) {
117 pIdleData
->mbTimeout
= true;
118 pIdleData
->maIdleHdl
.Call( GetpApp() );
119 // May have been removed in the handler
120 for ( size_t j
= 0; j
< mpIdleList
->size(); ++j
) {
121 if ( (*mpIdleList
)[ j
] == pIdleData
) {
122 pIdleData
->mbTimeout
= false;
132 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */