Stop leaking all ScPostIt instances.
[LibreOffice.git] / sc / source / ui / docshell / autostyl.cxx
blob78dbf5cd3e0275cbc182130785e575136eb65633
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 .
21 #include <time.h>
23 #include "attrib.hxx"
24 #include "autostyl.hxx"
25 #include "docsh.hxx"
26 #include "sc.hrc"
28 struct ScAutoStyleInitData
30 ScRange aRange;
31 OUString aStyle1;
32 sal_uLong nTimeout;
33 OUString aStyle2;
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
41 sal_uLong nTimeout;
42 ScRange aRange;
43 OUString aStyle;
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);
54 namespace {
56 class FindByRange : public ::std::unary_function<ScAutoStyleData, bool>
58 ScRange maRange;
59 public:
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>
66 sal_uLong mnTimeout;
67 public:
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) :
83 pDocSh( 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 ));
100 aInitTimer.Start();
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
112 if (iter->nTimeout)
113 AddEntry(iter->nTimeout,iter->aRange,iter->aStyle2 );
116 aInitials.clear();
118 return 0;
121 void ScAutoStyleList::AddEntry( sal_uLong nTimeout, const ScRange& rRange, const OUString& rStyle )
123 aTimer.Stop();
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())
131 aEntries.erase(itr);
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
149 ExecuteEntries();
150 StartTimer(nNow);
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
160 else
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)
172 if (itr->nTimeout)
173 break;
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()
184 aTimer.Stop();
186 boost::ptr_vector<ScAutoStyleData>::iterator iter;
187 for (iter = aEntries.begin(); iter != aEntries.end(); ++iter)
188 pDocSh->DoAutoStyle(iter->aRange,iter->aStyle);
190 aEntries.clear();
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);
202 aTimer.Start();
205 nTimerStart = nNow;
208 IMPL_LINK_NOARG(ScAutoStyleList, TimerHdl)
210 sal_uLong nNow = TimeNow();
211 AdjustEntries(aTimer.GetTimeout()); // eingestellte Wartezeit
212 ExecuteEntries();
213 StartTimer(nNow);
215 return 0;
221 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */