1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: autostyl.cxx,v $
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 ---------------------------------------------------------------
39 #include "autostyl.hxx"
45 //==================================================================
47 struct ScAutoStyleInitData
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
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
) :
80 aTimer
.SetTimeoutHdl( LINK( this, ScAutoStyleList
, TimerHdl
) );
81 aInitTimer
.SetTimeoutHdl( LINK( this, ScAutoStyleList
, InitHdl
) );
82 aInitTimer
.SetTimeout( 0 );
85 ScAutoStyleList::~ScAutoStyleList()
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() );
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
);
130 //==================================================================
132 void ScAutoStyleList::AddEntry( ULONG nTimeout
, const ScRange
& rRange
, const String
& rStyle
)
135 ULONG nNow
= TimeNow();
137 // alten Eintrag loeschen
139 ULONG nCount
= aEntries
.Count();
141 for (i
=0; i
<nCount
; i
++)
143 ScAutoStyleData
* pData
= (ScAutoStyleData
*) aEntries
.GetObject(i
);
144 if (pData
->aRange
== rRange
)
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
)
168 ScAutoStyleData
* pNew
= new ScAutoStyleData( nTimeout
, rRange
, rStyle
);
169 aEntries
.Insert( pNew
, nPos
);
171 // abgelaufene ausfuehren, Timer neu starten
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
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 ???
198 aEntries
.Remove((ULONG
)0);
202 void ScAutoStyleList::ExecuteAllNow()
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 ???
218 void ScAutoStyleList::StartTimer( ULONG nNow
) // Sekunden
220 // ersten Eintrag mit Timeout != 0 suchen
223 ScAutoStyleData
* pData
;
224 while ( (pData
= (ScAutoStyleData
*) aEntries
.GetObject(nPos
)) != NULL
&& pData
->nTimeout
== 0 )
229 aTimer
.SetTimeout( pData
->nTimeout
);
235 IMPL_LINK( ScAutoStyleList
, TimerHdl
, Timer
*, EMPTYARG
)
237 ULONG nNow
= TimeNow();
238 AdjustEntries(aTimer
.GetTimeout()); // eingestellte Wartezeit