Branch libreoffice-5-0-4
[LibreOffice.git] / include / tools / multisel.hxx
blobbac6fe8e8ff688721f2fcadf56769055b937f1ba
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 INCLUDED_TOOLS_MULTISEL_HXX
20 #define INCLUDED_TOOLS_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 bool bInverseCur;// inverse cursor
42 bool bCurValid; // are nCurIndex and nCurSubSel valid
43 bool bSelectNew; // auto-select newly inserted indexes
45 TOOLS_DLLPRIVATE void ImplClear();
46 TOOLS_DLLPRIVATE size_t ImplFindSubSelection( long nIndex ) const;
47 TOOLS_DLLPRIVATE 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 bool operator== ( MultiSelection& rOrig );
58 bool operator!= ( MultiSelection& rOrig )
59 { return !operator==( rOrig ); }
60 bool operator !() const
61 { return nSelCount == 0; }
63 void SelectAll( bool bSelect = true );
64 bool Select( long nIndex, bool bSelect = true );
65 void Select( const Range& rIndexRange, bool bSelect = true );
66 bool IsSelected( long nIndex ) const;
67 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 bool IsCurValid() const { return bCurValid; }
77 long GetCurSelected() const { return nCurIndex; }
78 long FirstSelected( bool bInverse = false );
79 long LastSelected();
80 long NextSelected();
82 size_t GetRangeCount() const { return aSels.size(); }
83 const Range& GetRange( size_t nRange ) const { return *aSels[nRange]; }
86 class TOOLS_DLLPUBLIC StringRangeEnumerator
88 struct Range
90 sal_Int32 nFirst;
91 sal_Int32 nLast;
93 Range() : nFirst( -1 ), nLast( -1 ) {}
94 Range( sal_Int32 i_nFirst, sal_Int32 i_nLast ) : nFirst( i_nFirst ), nLast( i_nLast ) {}
96 std::vector< StringRangeEnumerator::Range > maSequence;
97 sal_Int32 mnCount;
98 sal_Int32 mnMin;
99 sal_Int32 mnMax;
100 sal_Int32 mnOffset;
101 bool mbValidInput;
103 bool setRange( const OUString& i_rNewRange, bool i_bStrict = false );
104 bool insertRange( sal_Int32 nFirst, sal_Int32 nLast, bool bSequence, bool bMayAdjust );
105 bool insertJoinedRanges( const std::vector< sal_Int32 >& rNumbers, bool i_bStrict );
106 bool checkValue( sal_Int32, const std::set< sal_Int32 >* i_pPossibleValues = NULL ) const;
107 public:
108 class TOOLS_DLLPUBLIC Iterator
110 const StringRangeEnumerator* pEnumerator;
111 const std::set< sal_Int32 >* pPossibleValues;
112 sal_Int32 nRangeIndex;
113 sal_Int32 nCurrent;
115 friend class StringRangeEnumerator;
116 Iterator( const StringRangeEnumerator* i_pEnum,
117 const std::set< sal_Int32 >* i_pPossibleValues,
118 sal_Int32 i_nRange,
119 sal_Int32 i_nCurrent )
120 : pEnumerator( i_pEnum ), pPossibleValues( i_pPossibleValues )
121 , nRangeIndex( i_nRange ), nCurrent( i_nCurrent ) {}
123 public:
124 Iterator() : pEnumerator( NULL ), pPossibleValues( NULL ), nRangeIndex( -1 ), nCurrent( -1 ) {}
125 Iterator& operator++();
126 sal_Int32 operator*() const { return nCurrent;}
127 bool operator==(const Iterator&) const;
128 bool operator!=(const Iterator& i_rComp) const
129 { return ! (*this == i_rComp); }
132 friend class StringRangeEnumerator::Iterator;
134 StringRangeEnumerator( const OUString& i_rInput,
135 sal_Int32 i_nMinNumber,
136 sal_Int32 i_nMaxNumber,
137 sal_Int32 i_nLogicalOffset = -1
140 sal_Int32 size() const { return mnCount; }
141 Iterator begin( const std::set< sal_Int32 >* i_pPossibleValues = NULL ) const;
142 Iterator end( const std::set< sal_Int32 >* i_pPossibleValues = NULL ) const;
144 bool isValidInput() const { return mbValidInput; }
145 bool hasValue( sal_Int32 nValue, const std::set< sal_Int32 >* i_pPossibleValues = NULL ) const;
148 i_rPageRange: the string to be changed into a sequence of numbers
149 valid format example "5-3,9,9,7-8" ; instead of ',' ';' or ' ' are allowed as well
150 o_rPageVector: the output sequence of numbers
151 i_nLogicalOffset: an offset to be applied to each number in the string before inserting it in the resulting sequence
152 example: a user enters page numbers from 1 to n (since that is logical)
153 of course usable page numbers in code would start from 0 and end at n-1
154 so the logical offset would be -1
155 i_nMinNumber: the minimum allowed number
156 i_nMaxNumber: the maximum allowed number
158 @returns: true if the input string was valid, o_rPageVector will contain the resulting sequence
159 false if the input string was invalid, o_rPageVector will contain
160 the sequence that parser is able to extract
162 behavior:
163 - only non-negative sequence numbers are allowed
164 - only non-negative values in the input string are allowed
165 - the string "-3" means the sequence i_nMinNumber to 3
166 - the string "3-" means the sequence 3 to i_nMaxNumber
167 - the string "-" means the sequence i_nMinNumber to i_nMaxNumber
168 - single number that doesn't fit in [i_nMinNumber,i_nMaxNumber] will be ignored
169 - range that doesn't fit in [i_nMinNumber,i_nMaxNumber] will be adjusted
171 static bool getRangesFromString( const OUString& i_rPageRange,
172 std::vector< sal_Int32 >& o_rPageVector,
173 sal_Int32 i_nMinNumber,
174 sal_Int32 i_nMaxNumber,
175 sal_Int32 i_nLogicalOffset = -1,
176 std::set< sal_Int32 >* i_pPossibleValues = NULL
180 #endif
182 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */