xref: /aoo41x/main/svl/source/items/whiter.cxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_svl.hxx"
30 // INCLUDE ---------------------------------------------------------------
31 
32 #include <svl/whiter.hxx>
33 #include <svl/itemset.hxx>
34 
35 DBG_NAME(SfxWhichIter)
36 
37 // -----------------------------------------------------------------------
38 
39 SfxWhichIter::SfxWhichIter( const SfxItemSet& rSet, sal_uInt16 nFromWh, sal_uInt16 nToWh ):
40 	pRanges(rSet.GetRanges()),
41 	pStart(rSet.GetRanges()),
42 	nOfst(0), nFrom(nFromWh), nTo(nToWh)
43 {
44 	DBG_CTOR(SfxWhichIter, 0);
45 	if ( nFrom > 0 )
46 		FirstWhich();
47 }
48 
49 // -----------------------------------------------------------------------
50 
51 SfxWhichIter::~SfxWhichIter()
52 {
53 	DBG_DTOR(SfxWhichIter, 0);
54 }
55 
56 // -----------------------------------------------------------------------
57 
58 sal_uInt16 SfxWhichIter::NextWhich()
59 {
60 	DBG_CHKTHIS(SfxWhichIter, 0);
61 	while ( 0 != *pRanges )
62 	{
63 		const sal_uInt16 nLastWhich = *pRanges + nOfst;
64 		++nOfst;
65 		if (*(pRanges+1) == nLastWhich)
66 		{
67 			pRanges += 2;
68 			nOfst = 0;
69 		}
70 		sal_uInt16 nWhich = *pRanges + nOfst;
71 		if ( 0 == nWhich || ( nWhich >= nFrom && nWhich <= nTo ) )
72 			return nWhich;
73 	}
74 	return 0;
75 }
76 
77 // -----------------------------------------------------------------------
78 
79 sal_uInt16  SfxWhichIter::PrevWhich()
80 {
81 	DBG_CHKTHIS(SfxWhichIter, 0);
82 	while ( pRanges != pStart || 0 != nOfst )
83 	{
84 		if(nOfst)
85 			--nOfst;
86 		else {
87 			pRanges -= 2;
88 			nOfst = *(pRanges+1) - (*pRanges);
89 		}
90 		sal_uInt16 nWhich = *pRanges + nOfst;
91 		if ( nWhich >= nFrom && nWhich <= nTo )
92 			return nWhich;
93 	}
94 	return 0;
95 }
96 
97 // -----------------------------------------------------------------------
98 
99 sal_uInt16 SfxWhichIter::FirstWhich()
100 {
101 	DBG_CHKTHIS(SfxWhichIter, 0);
102 	pRanges = pStart;
103 	nOfst = 0;
104 	if ( *pRanges >= nFrom && *pRanges <= nTo )
105 		return *pRanges;
106 	return NextWhich();
107 }
108 
109 // -----------------------------------------------------------------------
110 
111 sal_uInt16 SfxWhichIter::LastWhich()
112 {
113 	DBG_CHKTHIS(SfxWhichIter, 0);
114 	while(*pRanges)
115 		++pRanges;
116 	nOfst = 0;
117 	sal_uInt16 nWhich = *(pRanges-1);
118 	if ( nWhich >= nFrom && nWhich <= nTo )
119 		return nWhich;
120 	return PrevWhich();
121 }
122 
123