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
) :
85 aTimer
.SetTimeoutHdl( LINK( this, ScAutoStyleList
, TimerHdl
) );
86 aInitTimer
.SetTimeoutHdl( LINK( this, ScAutoStyleList
, InitHdl
) );
87 aInitTimer
.SetTimeout( 0 );
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(ScAutoStyleList
, InitHdl
)
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
);
121 void ScAutoStyleList::AddEntry( sal_uLong nTimeout
, const ScRange
& rRange
, const OUString
& rStyle
)
124 sal_uLong nNow
= TimeNow();
126 // Remove the first item with the same range.
127 ::boost::ptr_vector
<ScAutoStyleData
>::iterator itr
=
128 ::std::find_if(aEntries
.begin(), aEntries
.end(), FindByRange(rRange
));
130 if (itr
!= aEntries
.end())
133 // Timeouts von allen Eintraegen anpassen
135 if (!aEntries
.empty() && nNow
!= nTimerStart
)
137 OSL_ENSURE(nNow
>nTimerStart
, "Zeit laeuft rueckwaerts?");
138 AdjustEntries((nNow
-nTimerStart
)*1000);
141 // Einfuege-Position suchen
142 boost::ptr_vector
<ScAutoStyleData
>::iterator iter
=
143 ::std::find_if(aEntries
.begin(), aEntries
.end(), FindByTimeout(nTimeout
));
145 aEntries
.insert(iter
,new ScAutoStyleData(nTimeout
,rRange
,rStyle
));
147 // abgelaufene ausfuehren, Timer neu starten
153 void ScAutoStyleList::AdjustEntries( sal_uLong nDiff
) // Millisekunden
155 boost::ptr_vector
<ScAutoStyleData
>::iterator iter
;
156 for (iter
= aEntries
.begin(); iter
!= aEntries
.end(); ++iter
)
158 if (iter
->nTimeout
<= nDiff
)
159 iter
->nTimeout
= 0; // abgelaufen
161 iter
->nTimeout
-= nDiff
; // weiterzaehlen
165 void ScAutoStyleList::ExecuteEntries()
167 // Execute and remove all items with timeout == 0 from the begin position
168 // until the first item with non-zero timeout value.
169 ::boost::ptr_vector
<ScAutoStyleData
>::iterator itr
= aEntries
.begin(), itrEnd
= aEntries
.end();
170 for (; itr
!= itrEnd
; ++itr
)
175 pDocSh
->DoAutoStyle(itr
->aRange
, itr
->aStyle
);
177 // At this point itr should be on the first item with non-zero timeout, or
178 // the end position in case all items have timeout == 0.
179 aEntries
.erase(aEntries
.begin(), itr
);
182 void ScAutoStyleList::ExecuteAllNow()
186 boost::ptr_vector
<ScAutoStyleData
>::iterator iter
;
187 for (iter
= aEntries
.begin(); iter
!= aEntries
.end(); ++iter
)
188 pDocSh
->DoAutoStyle(iter
->aRange
,iter
->aStyle
);
193 void ScAutoStyleList::StartTimer( sal_uLong nNow
) // Sekunden
195 // ersten Eintrag mit Timeout != 0 suchen
196 boost::ptr_vector
<ScAutoStyleData
>::iterator iter
=
197 ::std::find_if(aEntries
.begin(),aEntries
.end(), FindNonZeroTimeout());
199 if (iter
!= aEntries
.end())
201 aTimer
.SetTimeout(iter
->nTimeout
);
208 IMPL_LINK_NOARG(ScAutoStyleList
, TimerHdl
)
210 sal_uLong nNow
= TimeNow();
211 AdjustEntries(aTimer
.GetTimeout()); // eingestellte Wartezeit
221 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */