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 .
24 #include "autostyl.hxx"
28 struct ScAutoStyleInitData
35 ScAutoStyleInitData( const ScRange
& rR
, const OUString
& rSt1
, sal_uLong nT
, const OUString
& rSt2
) :
36 aRange(rR
), aStyle1(rSt1
), nTimeout(nT
), aStyle2(rSt2
) {}
39 struct ScAutoStyleData
45 ScAutoStyleData( sal_uLong nT
, const ScRange
& rR
, const OUString
& rT
) :
46 nTimeout(nT
), aRange(rR
), aStyle(rT
) {}
49 inline sal_uLong
TimeNow() // Sekunden
51 return (sal_uLong
) time(0);
56 class FindByRange
: public ::std::unary_function
<ScAutoStyleData
, bool>
60 FindByRange(const ScRange
& r
) : maRange(r
) {}
61 bool operator() (const ScAutoStyleData
& rData
) const { return rData
.aRange
== maRange
; }
64 class FindByTimeout
: public ::std::unary_function
<ScAutoStyleData
, bool>
68 FindByTimeout(sal_uLong n
) : mnTimeout(n
) {}
69 bool operator() (const ScAutoStyleData
& rData
) const { return rData
.nTimeout
>= mnTimeout
; }
72 struct FindNonZeroTimeout
: public ::std::unary_function
<ScAutoStyleData
, bool>
74 bool operator() (const ScAutoStyleData
& rData
) const
76 return rData
.nTimeout
!= 0;
82 ScAutoStyleList::ScAutoStyleList(ScDocShell
* pShell
)
86 aTimer
.SetTimeoutHdl( LINK( this, ScAutoStyleList
, TimerHdl
) );
87 aInitTimer
.SetTimeoutHdl( LINK( this, ScAutoStyleList
, InitHdl
) );
88 aInitTimer
.SetTimeout( 0 );
91 ScAutoStyleList::~ScAutoStyleList()
95 // initial short delay (asynchronous call)
97 void ScAutoStyleList::AddInitial( const ScRange
& rRange
, const OUString
& rStyle1
,
98 sal_uLong nTimeout
, const OUString
& rStyle2
)
100 aInitials
.push_back(new ScAutoStyleInitData( rRange
, rStyle1
, nTimeout
, rStyle2
));
104 IMPL_LINK_NOARG(ScAutoStyleList
, InitHdl
)
106 boost::ptr_vector
<ScAutoStyleInitData
>::iterator iter
;
107 for (iter
= aInitials
.begin(); iter
!= aInitials
.end(); ++iter
)
109 // apply first style immediately
110 pDocSh
->DoAutoStyle(iter
->aRange
,iter
->aStyle1
);
112 // add second style to list
114 AddEntry(iter
->nTimeout
,iter
->aRange
,iter
->aStyle2
);
122 void ScAutoStyleList::AddEntry( sal_uLong nTimeout
, const ScRange
& rRange
, const OUString
& rStyle
)
125 sal_uLong nNow
= TimeNow();
127 // Remove the first item with the same range.
128 ::boost::ptr_vector
<ScAutoStyleData
>::iterator itr
=
129 ::std::find_if(aEntries
.begin(), aEntries
.end(), FindByRange(rRange
));
131 if (itr
!= aEntries
.end())
134 // Timeouts von allen Eintraegen anpassen
136 if (!aEntries
.empty() && nNow
!= nTimerStart
)
138 OSL_ENSURE(nNow
>nTimerStart
, "Zeit laeuft rueckwaerts?");
139 AdjustEntries((nNow
-nTimerStart
)*1000);
142 // Einfuege-Position suchen
143 boost::ptr_vector
<ScAutoStyleData
>::iterator iter
=
144 ::std::find_if(aEntries
.begin(), aEntries
.end(), FindByTimeout(nTimeout
));
146 aEntries
.insert(iter
,new ScAutoStyleData(nTimeout
,rRange
,rStyle
));
148 // abgelaufene ausfuehren, Timer neu starten
154 void ScAutoStyleList::AdjustEntries( sal_uLong nDiff
) // Millisekunden
156 boost::ptr_vector
<ScAutoStyleData
>::iterator iter
;
157 for (iter
= aEntries
.begin(); iter
!= aEntries
.end(); ++iter
)
159 if (iter
->nTimeout
<= nDiff
)
160 iter
->nTimeout
= 0; // abgelaufen
162 iter
->nTimeout
-= nDiff
; // weiterzaehlen
166 void ScAutoStyleList::ExecuteEntries()
168 // Execute and remove all items with timeout == 0 from the begin position
169 // until the first item with non-zero timeout value.
170 ::boost::ptr_vector
<ScAutoStyleData
>::iterator itr
= aEntries
.begin(), itrEnd
= aEntries
.end();
171 for (; itr
!= itrEnd
; ++itr
)
176 pDocSh
->DoAutoStyle(itr
->aRange
, itr
->aStyle
);
178 // At this point itr should be on the first item with non-zero timeout, or
179 // the end position in case all items have timeout == 0.
180 aEntries
.erase(aEntries
.begin(), itr
);
183 void ScAutoStyleList::ExecuteAllNow()
187 boost::ptr_vector
<ScAutoStyleData
>::iterator iter
;
188 for (iter
= aEntries
.begin(); iter
!= aEntries
.end(); ++iter
)
189 pDocSh
->DoAutoStyle(iter
->aRange
,iter
->aStyle
);
194 void ScAutoStyleList::StartTimer( sal_uLong nNow
) // Sekunden
196 // ersten Eintrag mit Timeout != 0 suchen
197 boost::ptr_vector
<ScAutoStyleData
>::iterator iter
=
198 ::std::find_if(aEntries
.begin(),aEntries
.end(), FindNonZeroTimeout());
200 if (iter
!= aEntries
.end())
202 aTimer
.SetTimeout(iter
->nTimeout
);
209 IMPL_LINK_NOARG(ScAutoStyleList
, TimerHdl
)
211 sal_uLong nNow
= TimeNow();
212 AdjustEntries(aTimer
.GetTimeout()); // eingestellte Wartezeit
220 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */