update credits
[LibreOffice.git] / include / tools / multisel.hxx
blob8b5d434130fa588304ecd8ed0de9883c716b8fd4
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 .
19 #ifndef _SV_MULTISEL_HXX
20 #define _SV_MULTISEL_HXX
22 #include "tools/toolsdllapi.h"
23 #include <tools/gen.hxx>
24 #include <rtl/ustring.hxx>
26 #include <vector>
27 #include <set>
29 typedef ::std::vector< Range* > ImpSelList;
31 #define SFX_ENDOFSELECTION ULONG_MAX
33 class TOOLS_DLLPUBLIC MultiSelection
35 private:
36 ImpSelList aSels; // array of SV-selections
37 Range aTotRange; // total range of indexes
38 sal_uIntPtr nCurSubSel; // index in aSels of current selected index
39 long nCurIndex; // current selected entry
40 sal_uIntPtr nSelCount; // number of selected indexes
41 sal_Bool bInverseCur;// inverse cursor
42 sal_Bool bCurValid; // are nCurIndex and nCurSubSel valid
43 sal_Bool bSelectNew; // auto-select newly inserted indexes
45 TOOLS_DLLPRIVATE void ImplClear();
46 TOOLS_DLLPRIVATE size_t ImplFindSubSelection( long nIndex ) const;
47 TOOLS_DLLPRIVATE sal_Bool ImplMergeSubSelections( size_t nPos1, size_t nPos2 );
48 TOOLS_DLLPRIVATE long ImplFwdUnselected();
50 public:
51 MultiSelection();
52 MultiSelection( const MultiSelection& rOrig );
53 MultiSelection( const Range& rRange );
54 ~MultiSelection();
56 MultiSelection& operator= ( const MultiSelection& rOrig );
57 sal_Bool operator== ( MultiSelection& rOrig );
58 sal_Bool operator!= ( MultiSelection& rOrig )
59 { return !operator==( rOrig ); }
60 sal_Bool operator !() const
61 { return nSelCount == 0; }
63 void SelectAll( sal_Bool bSelect = sal_True );
64 sal_Bool Select( long nIndex, sal_Bool bSelect = sal_True );
65 void Select( const Range& rIndexRange, sal_Bool bSelect = sal_True );
66 sal_Bool IsSelected( long nIndex ) const;
67 sal_Bool IsAllSelected() const
68 { return nSelCount == sal_uIntPtr(aTotRange.Len()); }
69 long GetSelectCount() const { return nSelCount; }
71 void SetTotalRange( const Range& rTotRange );
72 void Insert( long nIndex, long nCount = 1 );
73 void Remove( long nIndex );
75 const Range& GetTotalRange() const { return aTotRange; }
76 sal_Bool IsCurValid() const { return bCurValid; }
77 long GetCurSelected() const { return nCurIndex; }
78 long FirstSelected( sal_Bool bInverse = sal_False );
79 long LastSelected();
80 long NextSelected();
82 size_t GetRangeCount() const { return aSels.size(); }
83 const Range& GetRange( size_t nRange ) const {
84 return *(const Range*)aSels[nRange];
88 class TOOLS_DLLPUBLIC StringRangeEnumerator
90 struct Range
92 sal_Int32 nFirst;
93 sal_Int32 nLast;
95 Range() : nFirst( -1 ), nLast( -1 ) {}
96 Range( sal_Int32 i_nFirst, sal_Int32 i_nLast ) : nFirst( i_nFirst ), nLast( i_nLast ) {}
98 std::vector< StringRangeEnumerator::Range > maSequence;
99 sal_Int32 mnCount;
100 sal_Int32 mnMin;
101 sal_Int32 mnMax;
102 sal_Int32 mnOffset;
103 bool mbValidInput;
105 bool setRange( const OUString& i_rNewRange, bool i_bStrict = false );
106 bool insertRange( sal_Int32 nFirst, sal_Int32 nLast, bool bSequence, bool bMayAdjust );
107 bool insertJoinedRanges( const std::vector< sal_Int32 >& rNumbers, bool i_bStrict );
108 bool checkValue( sal_Int32, const std::set< sal_Int32 >* i_pPossibleValues = NULL ) const;
109 public:
110 class TOOLS_DLLPUBLIC Iterator
112 const StringRangeEnumerator* pEnumerator;
113 const std::set< sal_Int32 >* pPossibleValues;
114 sal_Int32 nRangeIndex;
115 sal_Int32 nCurrent;
117 friend class StringRangeEnumerator;
118 Iterator( const StringRangeEnumerator* i_pEnum,
119 const std::set< sal_Int32 >* i_pPossibleValues,
120 sal_Int32 i_nRange,
121 sal_Int32 i_nCurrent )
122 : pEnumerator( i_pEnum ), pPossibleValues( i_pPossibleValues )
123 , nRangeIndex( i_nRange ), nCurrent( i_nCurrent ) {}
125 public:
126 Iterator() : pEnumerator( NULL ), pPossibleValues( NULL ), nRangeIndex( -1 ), nCurrent( -1 ) {}
127 Iterator& operator++();
128 sal_Int32 operator*() const;
129 bool operator==(const Iterator&) const;
130 bool operator!=(const Iterator& i_rComp) const
131 { return ! (*this == i_rComp); }
134 friend class StringRangeEnumerator::Iterator;
136 StringRangeEnumerator( const OUString& i_rInput,
137 sal_Int32 i_nMinNumber,
138 sal_Int32 i_nMaxNumber,
139 sal_Int32 i_nLogicalOffset = -1
142 sal_Int32 size() const { return mnCount; }
143 Iterator begin( const std::set< sal_Int32 >* i_pPossibleValues = NULL ) const;
144 Iterator end( const std::set< sal_Int32 >* i_pPossibleValues = NULL ) const;
146 bool isValidInput() const { return mbValidInput; }
147 bool hasValue( sal_Int32 nValue, const std::set< sal_Int32 >* i_pPossibleValues = NULL ) const;
150 i_rPageRange: the string to be changed into a sequence of numbers
151 valid format example "5-3,9,9,7-8" ; instead of ',' ';' or ' ' are allowed as well
152 o_rPageVector: the output sequence of numbers
153 i_nLogicalOffset: an offset to be applied to each number in the string before inserting it in the resulting sequence
154 example: a user enters page numbers from 1 to n (since that is logical)
155 of course usable page numbers in code would start from 0 and end at n-1
156 so the logical offset would be -1
157 i_nMinNumber: the minimum allowed number
158 i_nMaxNumber: the maximum allowed number
160 @returns: true if the input string was valid, o_rPageVector will contain the resulting sequence
161 false if the input string was invalid, o_rPageVector will contain
162 the sequence that parser is able to extract
164 behavior:
165 - only non-negative sequence numbers are allowed
166 - only non-negative values in the input string are allowed
167 - the string "-3" means the sequence i_nMinNumber to 3
168 - the string "3-" means the sequence 3 to i_nMaxNumber
169 - the string "-" means the sequence i_nMinNumber to i_nMaxNumber
170 - single number that doesn't fit in [i_nMinNumber,i_nMaxNumber] will be ignored
171 - range that doesn't fit in [i_nMinNumber,i_nMaxNumber] will be adjusted
173 static bool getRangesFromString( const OUString& i_rPageRange,
174 std::vector< sal_Int32 >& o_rPageVector,
175 sal_Int32 i_nMinNumber,
176 sal_Int32 i_nMaxNumber,
177 sal_Int32 i_nLogicalOffset = -1,
178 std::set< sal_Int32 >* i_pPossibleValues = NULL
182 #endif
184 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */