fix baseline build (old cairo) - 'cairo_rectangle_int_t' does not name a type
[LibreOffice.git] / sc / source / ui / docshell / autostyl.cxx
blobed81680c54f2f5f33025e187fbcaa57cab8a4aca
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 <time.h>
22 #include "attrib.hxx"
23 #include "autostyl.hxx"
24 #include "docsh.hxx"
25 #include "sc.hrc"
27 struct ScAutoStyleInitData
29 ScRange aRange;
30 OUString aStyle1;
31 sal_uLong nTimeout;
32 OUString aStyle2;
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
40 sal_uLong nTimeout;
41 ScRange aRange;
42 OUString aStyle;
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);
53 namespace {
55 class FindByRange : public ::std::unary_function<ScAutoStyleData, bool>
57 ScRange maRange;
58 public:
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>
65 sal_uLong mnTimeout;
66 public:
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)
82 : pDocSh(pShell)
83 , nTimerStart(0)
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 ));
100 aInitIdle.Start();
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
112 if (iter->nTimeout)
113 AddEntry(iter->nTimeout,iter->aRange,iter->aStyle2 );
116 aInitials.clear();
119 void ScAutoStyleList::AddEntry( sal_uLong nTimeout, const ScRange& rRange, const OUString& rStyle )
121 aTimer.Stop();
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())
129 aEntries.erase(itr);
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
147 ExecuteEntries();
148 StartTimer(nNow);
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
158 else
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)
170 if (itr->nTimeout)
171 break;
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()
182 aTimer.Stop();
184 boost::ptr_vector<ScAutoStyleData>::iterator iter;
185 for (iter = aEntries.begin(); iter != aEntries.end(); ++iter)
186 pDocSh->DoAutoStyle(iter->aRange,iter->aStyle);
188 aEntries.clear();
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);
200 aTimer.Start();
203 nTimerStart = nNow;
206 IMPL_LINK_NOARG_TYPED(ScAutoStyleList, TimerHdl, Timer *, void)
208 sal_uLong nNow = TimeNow();
209 AdjustEntries(aTimer.GetTimeout()); // eingestellte Wartezeit
210 ExecuteEntries();
211 StartTimer(nNow);
214 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */