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 .
23 #include "autostyl.hxx"
27 struct ScAutoStyleInitData
34 ScAutoStyleInitData( const ScRange
& rR
, const OUString
& rSt1
, sal_uLong nT
, const OUString
& rSt2
) :
35 aRange(rR
), aStyle1(rSt1
), nTimeout(nT
), aStyle2(rSt2
) {}
38 struct ScAutoStyleData
44 ScAutoStyleData( sal_uLong nT
, const ScRange
& rR
, const OUString
& rT
) :
45 nTimeout(nT
), aRange(rR
), aStyle(rT
) {}
48 inline sal_uLong
TimeNow() // Sekunden
50 return (sal_uLong
) time(0);
55 class FindByRange
: public ::std::unary_function
<ScAutoStyleData
, bool>
59 FindByRange(const ScRange
& r
) : maRange(r
) {}
60 bool operator() (const ScAutoStyleData
& rData
) const { return rData
.aRange
== maRange
; }
63 class FindByTimeout
: public ::std::unary_function
<ScAutoStyleData
, bool>
67 FindByTimeout(sal_uLong n
) : mnTimeout(n
) {}
68 bool operator() (const ScAutoStyleData
& rData
) const { return rData
.nTimeout
>= mnTimeout
; }
71 struct FindNonZeroTimeout
: public ::std::unary_function
<ScAutoStyleData
, bool>
73 bool operator() (const ScAutoStyleData
& rData
) const
75 return rData
.nTimeout
!= 0;
81 ScAutoStyleList::ScAutoStyleList(ScDocShell
* pShell
)
85 aTimer
.SetTimeoutHdl( LINK( this, ScAutoStyleList
, TimerHdl
) );
86 aInitIdle
.SetIdleHdl( LINK( this, ScAutoStyleList
, InitHdl
) );
87 aInitIdle
.SetPriority( SchedulerPriority::HIGHEST
);
90 ScAutoStyleList::~ScAutoStyleList()
94 // initial short delay (asynchronous call)
96 void ScAutoStyleList::AddInitial( const ScRange
& rRange
, const OUString
& rStyle1
,
97 sal_uLong nTimeout
, const OUString
& rStyle2
)
99 aInitials
.push_back(new ScAutoStyleInitData( rRange
, rStyle1
, nTimeout
, rStyle2
));
103 IMPL_LINK_NOARG_TYPED(ScAutoStyleList
, InitHdl
, Idle
*, void)
105 boost::ptr_vector
<ScAutoStyleInitData
>::iterator iter
;
106 for (iter
= aInitials
.begin(); iter
!= aInitials
.end(); ++iter
)
108 // apply first style immediately
109 pDocSh
->DoAutoStyle(iter
->aRange
,iter
->aStyle1
);
111 // add second style to list
113 AddEntry(iter
->nTimeout
,iter
->aRange
,iter
->aStyle2
);
119 void ScAutoStyleList::AddEntry( sal_uLong nTimeout
, const ScRange
& rRange
, const OUString
& rStyle
)
122 sal_uLong nNow
= TimeNow();
124 // Remove the first item with the same range.
125 ::boost::ptr_vector
<ScAutoStyleData
>::iterator itr
=
126 ::std::find_if(aEntries
.begin(), aEntries
.end(), FindByRange(rRange
));
128 if (itr
!= aEntries
.end())
131 // Timeouts von allen Eintraegen anpassen
133 if (!aEntries
.empty() && nNow
!= nTimerStart
)
135 OSL_ENSURE(nNow
>nTimerStart
, "Zeit laeuft rueckwaerts?");
136 AdjustEntries((nNow
-nTimerStart
)*1000);
139 // Einfuege-Position suchen
140 boost::ptr_vector
<ScAutoStyleData
>::iterator iter
=
141 ::std::find_if(aEntries
.begin(), aEntries
.end(), FindByTimeout(nTimeout
));
143 aEntries
.insert(iter
,new ScAutoStyleData(nTimeout
,rRange
,rStyle
));
145 // abgelaufene ausfuehren, Timer neu starten
151 void ScAutoStyleList::AdjustEntries( sal_uLong nDiff
) // Millisekunden
153 boost::ptr_vector
<ScAutoStyleData
>::iterator iter
;
154 for (iter
= aEntries
.begin(); iter
!= aEntries
.end(); ++iter
)
156 if (iter
->nTimeout
<= nDiff
)
157 iter
->nTimeout
= 0; // abgelaufen
159 iter
->nTimeout
-= nDiff
; // weiterzaehlen
163 void ScAutoStyleList::ExecuteEntries()
165 // Execute and remove all items with timeout == 0 from the begin position
166 // until the first item with non-zero timeout value.
167 ::boost::ptr_vector
<ScAutoStyleData
>::iterator itr
= aEntries
.begin(), itrEnd
= aEntries
.end();
168 for (; itr
!= itrEnd
; ++itr
)
173 pDocSh
->DoAutoStyle(itr
->aRange
, itr
->aStyle
);
175 // At this point itr should be on the first item with non-zero timeout, or
176 // the end position in case all items have timeout == 0.
177 aEntries
.erase(aEntries
.begin(), itr
);
180 void ScAutoStyleList::ExecuteAllNow()
184 boost::ptr_vector
<ScAutoStyleData
>::iterator iter
;
185 for (iter
= aEntries
.begin(); iter
!= aEntries
.end(); ++iter
)
186 pDocSh
->DoAutoStyle(iter
->aRange
,iter
->aStyle
);
191 void ScAutoStyleList::StartTimer( sal_uLong nNow
) // Sekunden
193 // ersten Eintrag mit Timeout != 0 suchen
194 boost::ptr_vector
<ScAutoStyleData
>::iterator iter
=
195 ::std::find_if(aEntries
.begin(),aEntries
.end(), FindNonZeroTimeout());
197 if (iter
!= aEntries
.end())
199 aTimer
.SetTimeout(iter
->nTimeout
);
206 IMPL_LINK_NOARG_TYPED(ScAutoStyleList
, TimerHdl
, Timer
*, void)
208 sal_uLong nNow
= TimeNow();
209 AdjustEntries(aTimer
.GetTimeout()); // eingestellte Wartezeit
214 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */