update dev300-m58
[ooovba.git] / sc / source / ui / docshell / autostyl.cxx
blob55666628195b7545ef604888c3d555b23ce02e1f
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: autostyl.cxx,v $
10 * $Revision: 1.5 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_sc.hxx"
36 // INCLUDE ---------------------------------------------------------------
38 #include <time.h>
39 #include "autostyl.hxx"
41 #include "docsh.hxx"
42 #include "attrib.hxx"
43 #include "sc.hrc"
45 //==================================================================
47 struct ScAutoStyleInitData
49 ScRange aRange;
50 String aStyle1;
51 ULONG nTimeout;
52 String aStyle2;
54 ScAutoStyleInitData( const ScRange& rR, const String& rSt1, ULONG nT, const String& rSt2 ) :
55 aRange(rR), aStyle1(rSt1), nTimeout(nT), aStyle2(rSt2) {}
58 struct ScAutoStyleData
60 ULONG nTimeout;
61 ScRange aRange;
62 String aStyle;
64 ScAutoStyleData( ULONG nT, const ScRange& rR, const String& rT ) :
65 nTimeout(nT), aRange(rR), aStyle(rT) {}
68 //==================================================================
70 inline ULONG TimeNow() // Sekunden
72 return (ULONG) time(0);
75 //==================================================================
77 ScAutoStyleList::ScAutoStyleList(ScDocShell* pShell) :
78 pDocSh( pShell )
80 aTimer.SetTimeoutHdl( LINK( this, ScAutoStyleList, TimerHdl ) );
81 aInitTimer.SetTimeoutHdl( LINK( this, ScAutoStyleList, InitHdl ) );
82 aInitTimer.SetTimeout( 0 );
85 ScAutoStyleList::~ScAutoStyleList()
87 ULONG i;
88 ULONG nCount = aEntries.Count();
89 for (i=0; i<nCount; i++)
90 delete (ScAutoStyleData*) aEntries.GetObject(i);
91 nCount = aInitials.Count();
92 for (i=0; i<nCount; i++)
93 delete (ScAutoStyleInitData*) aInitials.GetObject(i);
96 //==================================================================
98 // initial short delay (asynchronous call)
100 void ScAutoStyleList::AddInitial( const ScRange& rRange, const String& rStyle1,
101 ULONG nTimeout, const String& rStyle2 )
103 ScAutoStyleInitData* pNew =
104 new ScAutoStyleInitData( rRange, rStyle1, nTimeout, rStyle2 );
105 aInitials.Insert( pNew, aInitials.Count() );
106 aInitTimer.Start();
109 IMPL_LINK( ScAutoStyleList, InitHdl, Timer*, EMPTYARG )
111 ULONG nCount = aInitials.Count();
112 for (ULONG i=0; i<nCount; i++)
114 ScAutoStyleInitData* pData = (ScAutoStyleInitData*) aInitials.GetObject(i);
116 // apply first style immediately
117 pDocSh->DoAutoStyle( pData->aRange, pData->aStyle1 );
119 // add second style to list
120 if ( pData->nTimeout )
121 AddEntry( pData->nTimeout, pData->aRange, pData->aStyle2 );
123 delete pData;
125 aInitials.Clear();
127 return 0;
130 //==================================================================
132 void ScAutoStyleList::AddEntry( ULONG nTimeout, const ScRange& rRange, const String& rStyle )
134 aTimer.Stop();
135 ULONG nNow = TimeNow();
137 // alten Eintrag loeschen
139 ULONG nCount = aEntries.Count();
140 ULONG i;
141 for (i=0; i<nCount; i++)
143 ScAutoStyleData* pData = (ScAutoStyleData*) aEntries.GetObject(i);
144 if (pData->aRange == rRange)
146 delete pData;
147 aEntries.Remove(i);
148 --nCount;
149 break; // nicht weitersuchen - es kann nur einen geben
153 // Timeouts von allen Eintraegen anpassen
155 if (nCount && nNow != nTimerStart)
157 DBG_ASSERT(nNow>nTimerStart, "Zeit laeuft rueckwaerts?");
158 AdjustEntries((nNow-nTimerStart)*1000);
161 // Einfuege-Position suchen
163 ULONG nPos = LIST_APPEND;
164 for (i=0; i<nCount && nPos == LIST_APPEND; i++)
165 if (nTimeout <= ((ScAutoStyleData*) aEntries.GetObject(i))->nTimeout)
166 nPos = i;
168 ScAutoStyleData* pNew = new ScAutoStyleData( nTimeout, rRange, rStyle );
169 aEntries.Insert( pNew, nPos );
171 // abgelaufene ausfuehren, Timer neu starten
173 ExecuteEntries();
174 StartTimer(nNow);
177 void ScAutoStyleList::AdjustEntries( ULONG nDiff ) // Millisekunden
179 ULONG nCount = aEntries.Count();
180 for (ULONG i=0; i<nCount; i++)
182 ScAutoStyleData* pData = (ScAutoStyleData*) aEntries.GetObject(i);
183 if ( pData->nTimeout <= nDiff )
184 pData->nTimeout = 0; // abgelaufen
185 else
186 pData->nTimeout -= nDiff; // weiterzaehlen
190 void ScAutoStyleList::ExecuteEntries()
192 ScAutoStyleData* pData;
193 while ((pData = (ScAutoStyleData*) aEntries.GetObject(0)) != NULL && pData->nTimeout == 0)
195 pDocSh->DoAutoStyle( pData->aRange, pData->aStyle ); //! oder Request ???
197 delete pData;
198 aEntries.Remove((ULONG)0);
202 void ScAutoStyleList::ExecuteAllNow()
204 aTimer.Stop();
206 ULONG nCount = aEntries.Count();
207 for (ULONG i=0; i<nCount; i++)
209 ScAutoStyleData* pData = (ScAutoStyleData*) aEntries.GetObject(i);
211 pDocSh->DoAutoStyle( pData->aRange, pData->aStyle ); //! oder Request ???
213 delete pData;
215 aEntries.Clear();
218 void ScAutoStyleList::StartTimer( ULONG nNow ) // Sekunden
220 // ersten Eintrag mit Timeout != 0 suchen
222 ULONG nPos = 0;
223 ScAutoStyleData* pData;
224 while ( (pData = (ScAutoStyleData*) aEntries.GetObject(nPos)) != NULL && pData->nTimeout == 0 )
225 ++nPos;
227 if (pData)
229 aTimer.SetTimeout( pData->nTimeout );
230 aTimer.Start();
232 nTimerStart = nNow;
235 IMPL_LINK( ScAutoStyleList, TimerHdl, Timer*, EMPTYARG )
237 ULONG nNow = TimeNow();
238 AdjustEntries(aTimer.GetTimeout()); // eingestellte Wartezeit
239 ExecuteEntries();
240 StartTimer(nNow);
242 return 0;