xref: /aoo42x/main/sc/source/ui/docshell/autostyl.cxx (revision cdf0e10c)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_sc.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir 
32*cdf0e10cSrcweir 
33*cdf0e10cSrcweir // INCLUDE ---------------------------------------------------------------
34*cdf0e10cSrcweir 
35*cdf0e10cSrcweir #include <time.h>
36*cdf0e10cSrcweir #include "autostyl.hxx"
37*cdf0e10cSrcweir 
38*cdf0e10cSrcweir #include "docsh.hxx"
39*cdf0e10cSrcweir #include "attrib.hxx"
40*cdf0e10cSrcweir #include "sc.hrc"
41*cdf0e10cSrcweir 
42*cdf0e10cSrcweir //==================================================================
43*cdf0e10cSrcweir 
44*cdf0e10cSrcweir struct ScAutoStyleInitData
45*cdf0e10cSrcweir {
46*cdf0e10cSrcweir 	ScRange	aRange;
47*cdf0e10cSrcweir 	String	aStyle1;
48*cdf0e10cSrcweir 	sal_uLong	nTimeout;
49*cdf0e10cSrcweir 	String	aStyle2;
50*cdf0e10cSrcweir 
51*cdf0e10cSrcweir 	ScAutoStyleInitData( const ScRange& rR, const String& rSt1, sal_uLong nT, const String& rSt2 ) :
52*cdf0e10cSrcweir 		aRange(rR), aStyle1(rSt1), nTimeout(nT), aStyle2(rSt2) {}
53*cdf0e10cSrcweir };
54*cdf0e10cSrcweir 
55*cdf0e10cSrcweir struct ScAutoStyleData
56*cdf0e10cSrcweir {
57*cdf0e10cSrcweir 	sal_uLong	nTimeout;
58*cdf0e10cSrcweir 	ScRange	aRange;
59*cdf0e10cSrcweir 	String	aStyle;
60*cdf0e10cSrcweir 
61*cdf0e10cSrcweir 	ScAutoStyleData( sal_uLong nT, const ScRange& rR, const String& rT ) :
62*cdf0e10cSrcweir 		nTimeout(nT), aRange(rR), aStyle(rT) {}
63*cdf0e10cSrcweir };
64*cdf0e10cSrcweir 
65*cdf0e10cSrcweir //==================================================================
66*cdf0e10cSrcweir 
67*cdf0e10cSrcweir inline sal_uLong TimeNow()			// Sekunden
68*cdf0e10cSrcweir {
69*cdf0e10cSrcweir 	return (sal_uLong) time(0);
70*cdf0e10cSrcweir }
71*cdf0e10cSrcweir 
72*cdf0e10cSrcweir //==================================================================
73*cdf0e10cSrcweir 
74*cdf0e10cSrcweir ScAutoStyleList::ScAutoStyleList(ScDocShell* pShell) :
75*cdf0e10cSrcweir 	pDocSh( pShell )
76*cdf0e10cSrcweir {
77*cdf0e10cSrcweir 	aTimer.SetTimeoutHdl( LINK( this, ScAutoStyleList, TimerHdl ) );
78*cdf0e10cSrcweir 	aInitTimer.SetTimeoutHdl( LINK( this, ScAutoStyleList, InitHdl ) );
79*cdf0e10cSrcweir 	aInitTimer.SetTimeout( 0 );
80*cdf0e10cSrcweir }
81*cdf0e10cSrcweir 
82*cdf0e10cSrcweir ScAutoStyleList::~ScAutoStyleList()
83*cdf0e10cSrcweir {
84*cdf0e10cSrcweir 	sal_uLong i;
85*cdf0e10cSrcweir 	sal_uLong nCount = aEntries.Count();
86*cdf0e10cSrcweir 	for (i=0; i<nCount; i++)
87*cdf0e10cSrcweir 		delete (ScAutoStyleData*) aEntries.GetObject(i);
88*cdf0e10cSrcweir 	nCount = aInitials.Count();
89*cdf0e10cSrcweir 	for (i=0; i<nCount; i++)
90*cdf0e10cSrcweir 		delete (ScAutoStyleInitData*) aInitials.GetObject(i);
91*cdf0e10cSrcweir }
92*cdf0e10cSrcweir 
93*cdf0e10cSrcweir //==================================================================
94*cdf0e10cSrcweir 
95*cdf0e10cSrcweir //	initial short delay (asynchronous call)
96*cdf0e10cSrcweir 
97*cdf0e10cSrcweir void ScAutoStyleList::AddInitial( const ScRange& rRange, const String& rStyle1,
98*cdf0e10cSrcweir 									sal_uLong nTimeout, const String& rStyle2 )
99*cdf0e10cSrcweir {
100*cdf0e10cSrcweir 	ScAutoStyleInitData* pNew =
101*cdf0e10cSrcweir 		new ScAutoStyleInitData( rRange, rStyle1, nTimeout, rStyle2 );
102*cdf0e10cSrcweir 	aInitials.Insert( pNew, aInitials.Count() );
103*cdf0e10cSrcweir 	aInitTimer.Start();
104*cdf0e10cSrcweir }
105*cdf0e10cSrcweir 
106*cdf0e10cSrcweir IMPL_LINK( ScAutoStyleList, InitHdl, Timer*, EMPTYARG )
107*cdf0e10cSrcweir {
108*cdf0e10cSrcweir 	sal_uLong nCount = aInitials.Count();
109*cdf0e10cSrcweir 	for (sal_uLong i=0; i<nCount; i++)
110*cdf0e10cSrcweir 	{
111*cdf0e10cSrcweir 		ScAutoStyleInitData* pData = (ScAutoStyleInitData*) aInitials.GetObject(i);
112*cdf0e10cSrcweir 
113*cdf0e10cSrcweir 		//	apply first style immediately
114*cdf0e10cSrcweir 		pDocSh->DoAutoStyle( pData->aRange, pData->aStyle1 );
115*cdf0e10cSrcweir 
116*cdf0e10cSrcweir 		//	add second style to list
117*cdf0e10cSrcweir 		if ( pData->nTimeout )
118*cdf0e10cSrcweir 			AddEntry( pData->nTimeout, pData->aRange, pData->aStyle2 );
119*cdf0e10cSrcweir 
120*cdf0e10cSrcweir 		delete pData;
121*cdf0e10cSrcweir 	}
122*cdf0e10cSrcweir 	aInitials.Clear();
123*cdf0e10cSrcweir 
124*cdf0e10cSrcweir 	return 0;
125*cdf0e10cSrcweir }
126*cdf0e10cSrcweir 
127*cdf0e10cSrcweir //==================================================================
128*cdf0e10cSrcweir 
129*cdf0e10cSrcweir void ScAutoStyleList::AddEntry( sal_uLong nTimeout, const ScRange& rRange, const String& rStyle )
130*cdf0e10cSrcweir {
131*cdf0e10cSrcweir 	aTimer.Stop();
132*cdf0e10cSrcweir 	sal_uLong nNow = TimeNow();
133*cdf0e10cSrcweir 
134*cdf0e10cSrcweir 	//	alten Eintrag loeschen
135*cdf0e10cSrcweir 
136*cdf0e10cSrcweir 	sal_uLong nCount = aEntries.Count();
137*cdf0e10cSrcweir 	sal_uLong i;
138*cdf0e10cSrcweir 	for (i=0; i<nCount; i++)
139*cdf0e10cSrcweir 	{
140*cdf0e10cSrcweir 		ScAutoStyleData* pData = (ScAutoStyleData*) aEntries.GetObject(i);
141*cdf0e10cSrcweir 		if (pData->aRange == rRange)
142*cdf0e10cSrcweir 		{
143*cdf0e10cSrcweir 			delete pData;
144*cdf0e10cSrcweir 			aEntries.Remove(i);
145*cdf0e10cSrcweir 			--nCount;
146*cdf0e10cSrcweir 			break;						// nicht weitersuchen - es kann nur einen geben
147*cdf0e10cSrcweir 		}
148*cdf0e10cSrcweir 	}
149*cdf0e10cSrcweir 
150*cdf0e10cSrcweir 	//	Timeouts von allen Eintraegen anpassen
151*cdf0e10cSrcweir 
152*cdf0e10cSrcweir 	if (nCount && nNow != nTimerStart)
153*cdf0e10cSrcweir 	{
154*cdf0e10cSrcweir 		DBG_ASSERT(nNow>nTimerStart, "Zeit laeuft rueckwaerts?");
155*cdf0e10cSrcweir 		AdjustEntries((nNow-nTimerStart)*1000);
156*cdf0e10cSrcweir 	}
157*cdf0e10cSrcweir 
158*cdf0e10cSrcweir 	//	Einfuege-Position suchen
159*cdf0e10cSrcweir 
160*cdf0e10cSrcweir 	sal_uLong nPos = LIST_APPEND;
161*cdf0e10cSrcweir 	for (i=0; i<nCount && nPos == LIST_APPEND; i++)
162*cdf0e10cSrcweir 		if (nTimeout <= ((ScAutoStyleData*) aEntries.GetObject(i))->nTimeout)
163*cdf0e10cSrcweir 			nPos = i;
164*cdf0e10cSrcweir 
165*cdf0e10cSrcweir 	ScAutoStyleData* pNew = new ScAutoStyleData( nTimeout, rRange, rStyle );
166*cdf0e10cSrcweir 	aEntries.Insert( pNew, nPos );
167*cdf0e10cSrcweir 
168*cdf0e10cSrcweir 	//	abgelaufene ausfuehren, Timer neu starten
169*cdf0e10cSrcweir 
170*cdf0e10cSrcweir 	ExecuteEntries();
171*cdf0e10cSrcweir 	StartTimer(nNow);
172*cdf0e10cSrcweir }
173*cdf0e10cSrcweir 
174*cdf0e10cSrcweir void ScAutoStyleList::AdjustEntries( sal_uLong nDiff )	// Millisekunden
175*cdf0e10cSrcweir {
176*cdf0e10cSrcweir 	sal_uLong nCount = aEntries.Count();
177*cdf0e10cSrcweir 	for (sal_uLong i=0; i<nCount; i++)
178*cdf0e10cSrcweir 	{
179*cdf0e10cSrcweir 		ScAutoStyleData* pData = (ScAutoStyleData*) aEntries.GetObject(i);
180*cdf0e10cSrcweir 		if ( pData->nTimeout <= nDiff )
181*cdf0e10cSrcweir 			pData->nTimeout = 0;					// abgelaufen
182*cdf0e10cSrcweir 		else
183*cdf0e10cSrcweir 			pData->nTimeout -= nDiff;				// weiterzaehlen
184*cdf0e10cSrcweir 	}
185*cdf0e10cSrcweir }
186*cdf0e10cSrcweir 
187*cdf0e10cSrcweir void ScAutoStyleList::ExecuteEntries()
188*cdf0e10cSrcweir {
189*cdf0e10cSrcweir 	ScAutoStyleData* pData;
190*cdf0e10cSrcweir     while ((pData = (ScAutoStyleData*) aEntries.GetObject(0)) != NULL && pData->nTimeout == 0)
191*cdf0e10cSrcweir 	{
192*cdf0e10cSrcweir 		pDocSh->DoAutoStyle( pData->aRange, pData->aStyle );	//! oder Request ???
193*cdf0e10cSrcweir 
194*cdf0e10cSrcweir 		delete pData;
195*cdf0e10cSrcweir 		aEntries.Remove((sal_uLong)0);
196*cdf0e10cSrcweir 	}
197*cdf0e10cSrcweir }
198*cdf0e10cSrcweir 
199*cdf0e10cSrcweir void ScAutoStyleList::ExecuteAllNow()
200*cdf0e10cSrcweir {
201*cdf0e10cSrcweir 	aTimer.Stop();
202*cdf0e10cSrcweir 
203*cdf0e10cSrcweir 	sal_uLong nCount = aEntries.Count();
204*cdf0e10cSrcweir 	for (sal_uLong i=0; i<nCount; i++)
205*cdf0e10cSrcweir 	{
206*cdf0e10cSrcweir 		ScAutoStyleData* pData = (ScAutoStyleData*) aEntries.GetObject(i);
207*cdf0e10cSrcweir 
208*cdf0e10cSrcweir 		pDocSh->DoAutoStyle( pData->aRange, pData->aStyle );	//! oder Request ???
209*cdf0e10cSrcweir 
210*cdf0e10cSrcweir 		delete pData;
211*cdf0e10cSrcweir 	}
212*cdf0e10cSrcweir 	aEntries.Clear();
213*cdf0e10cSrcweir }
214*cdf0e10cSrcweir 
215*cdf0e10cSrcweir void ScAutoStyleList::StartTimer( sal_uLong nNow )		// Sekunden
216*cdf0e10cSrcweir {
217*cdf0e10cSrcweir 	// ersten Eintrag mit Timeout != 0 suchen
218*cdf0e10cSrcweir 
219*cdf0e10cSrcweir 	sal_uLong nPos = 0;
220*cdf0e10cSrcweir 	ScAutoStyleData* pData;
221*cdf0e10cSrcweir     while ( (pData = (ScAutoStyleData*) aEntries.GetObject(nPos)) != NULL && pData->nTimeout == 0 )
222*cdf0e10cSrcweir 		++nPos;
223*cdf0e10cSrcweir 
224*cdf0e10cSrcweir 	if (pData)
225*cdf0e10cSrcweir 	{
226*cdf0e10cSrcweir 		aTimer.SetTimeout( pData->nTimeout );
227*cdf0e10cSrcweir 		aTimer.Start();
228*cdf0e10cSrcweir 	}
229*cdf0e10cSrcweir 	nTimerStart = nNow;
230*cdf0e10cSrcweir }
231*cdf0e10cSrcweir 
232*cdf0e10cSrcweir IMPL_LINK( ScAutoStyleList, TimerHdl, Timer*, EMPTYARG )
233*cdf0e10cSrcweir {
234*cdf0e10cSrcweir 	sal_uLong nNow = TimeNow();
235*cdf0e10cSrcweir 	AdjustEntries(aTimer.GetTimeout());				// eingestellte Wartezeit
236*cdf0e10cSrcweir 	ExecuteEntries();
237*cdf0e10cSrcweir 	StartTimer(nNow);
238*cdf0e10cSrcweir 
239*cdf0e10cSrcweir 	return 0;
240*cdf0e10cSrcweir }
241*cdf0e10cSrcweir 
242*cdf0e10cSrcweir 
243*cdf0e10cSrcweir 
244*cdf0e10cSrcweir 
245