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 .
21 #include <osl/diagnose.h>
23 #include <address.hxx>
24 #include <autostyl.hxx>
27 static sal_uLong
TimeNow() // seconds
29 return static_cast<sal_uLong
>(time(nullptr));
38 explicit FindByRange(const ScRange
& r
) : maRange(r
) {}
39 bool operator() (const ScAutoStyleData
& rData
) const { return rData
.aRange
== maRange
; }
46 explicit FindByTimeout(sal_uLong n
) : mnTimeout(n
) {}
47 bool operator() (const ScAutoStyleData
& rData
) const { return rData
.nTimeout
>= mnTimeout
; }
50 struct FindNonZeroTimeout
52 bool operator() (const ScAutoStyleData
& rData
) const
54 return rData
.nTimeout
!= 0;
60 ScAutoStyleList::ScAutoStyleList(ScDocShell
* pShell
)
62 , aTimer("ScAutoStyleList Timer")
63 , aInitIdle("ScAutoStyleList InitIdle")
66 aTimer
.SetInvokeHandler( LINK( this, ScAutoStyleList
, TimerHdl
) );
67 aInitIdle
.SetInvokeHandler( LINK( this, ScAutoStyleList
, InitHdl
) );
68 aInitIdle
.SetPriority( TaskPriority::HIGHEST
);
71 ScAutoStyleList::~ScAutoStyleList()
75 // initial short delay (asynchronous call)
77 void ScAutoStyleList::AddInitial( const ScRange
& rRange
, const OUString
& rStyle1
,
78 sal_uLong nTimeout
, const OUString
& rStyle2
)
80 aInitials
.emplace_back( rRange
, rStyle1
, nTimeout
, rStyle2
);
84 IMPL_LINK_NOARG(ScAutoStyleList
, InitHdl
, Timer
*, void)
86 std::vector
<ScAutoStyleInitData
> aLocalInitials(std::move(aInitials
));
87 for (const auto& rInitial
: aLocalInitials
)
89 // apply first style immediately
90 pDocSh
->DoAutoStyle(rInitial
.aRange
, rInitial
.aStyle1
);
92 // add second style to list
93 if (rInitial
.nTimeout
)
94 AddEntry(rInitial
.nTimeout
, rInitial
.aRange
, rInitial
.aStyle2
);
98 void ScAutoStyleList::AddEntry( sal_uLong nTimeout
, const ScRange
& rRange
, const OUString
& rStyle
)
101 sal_uLong nNow
= TimeNow();
103 // Remove the first item with the same range.
104 std::vector
<ScAutoStyleData
>::iterator itr
=
105 ::std::find_if(aEntries
.begin(), aEntries
.end(), FindByRange(rRange
));
107 if (itr
!= aEntries
.end())
110 // adjust timeouts of all entries
112 if (!aEntries
.empty() && nNow
!= nTimerStart
)
114 OSL_ENSURE(nNow
>nTimerStart
, "Time is running backwards?");
115 AdjustEntries((nNow
-nTimerStart
)*1000);
118 // find insert position
119 std::vector
<ScAutoStyleData
>::iterator iter
=
120 ::std::find_if(aEntries
.begin(), aEntries
.end(), FindByTimeout(nTimeout
));
122 aEntries
.insert(iter
, ScAutoStyleData(nTimeout
,rRange
,rStyle
));
124 // execute expired, restart timer
130 void ScAutoStyleList::AdjustEntries( sal_uLong nDiff
) // milliseconds
132 for (auto& rEntry
: aEntries
)
134 if (rEntry
.nTimeout
<= nDiff
)
135 rEntry
.nTimeout
= 0; // expired
137 rEntry
.nTimeout
-= nDiff
; // continue counting
141 void ScAutoStyleList::ExecuteEntries()
143 // Execute and remove all items with timeout == 0 from the begin position
144 // until the first item with non-zero timeout value.
145 std::vector
<ScAutoStyleData
>::iterator itr
= aEntries
.begin(), itrEnd
= aEntries
.end();
146 for (; itr
!= itrEnd
; ++itr
)
151 pDocSh
->DoAutoStyle(itr
->aRange
, itr
->aStyle
);
153 // At this point itr should be on the first item with non-zero timeout, or
154 // the end position in case all items have timeout == 0.
155 aEntries
.erase(aEntries
.begin(), itr
);
158 void ScAutoStyleList::ExecuteAllNow()
162 for (const auto& rEntry
: aEntries
)
163 pDocSh
->DoAutoStyle(rEntry
.aRange
, rEntry
.aStyle
);
168 void ScAutoStyleList::StartTimer( sal_uLong nNow
) // seconds
170 // find first entry with Timeout != 0
171 std::vector
<ScAutoStyleData
>::iterator iter
=
172 ::std::find_if(aEntries
.begin(),aEntries
.end(), FindNonZeroTimeout());
174 if (iter
!= aEntries
.end())
176 aTimer
.SetTimeout(iter
->nTimeout
);
183 IMPL_LINK_NOARG(ScAutoStyleList
, TimerHdl
, Timer
*, void)
185 sal_uLong nNow
= TimeNow();
186 AdjustEntries(aTimer
.GetTimeout()); // the set waiting time
191 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */