Resolves tdf#133662 - Copy version info reworked
[LibreOffice.git] / include / tools / multisel.hxx
blob3e06a3dce23939d24a101b0d29a3526282527a49
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 #define SFX_ENDOFSELECTION (-1)
31 class SAL_WARN_UNUSED TOOLS_DLLPUBLIC MultiSelection
33 private:
34 std::vector< Range >
35 aSels; // array of SV-selections
36 Range aTotRange; // total range of indexes
37 sal_Int32 nCurSubSel; // index in aSels of current selected index
38 sal_Int32 nCurIndex; // current selected entry
39 sal_Int32 nSelCount; // number of selected indexes
40 bool bCurValid; // are nCurIndex and nCurSubSel valid
42 TOOLS_DLLPRIVATE void ImplClear();
43 TOOLS_DLLPRIVATE sal_Int32 ImplFindSubSelection( sal_Int32 nIndex ) const;
44 TOOLS_DLLPRIVATE void ImplMergeSubSelections( sal_Int32 nPos1, sal_Int32 nPos2 );
46 public:
47 MultiSelection();
48 MultiSelection( const MultiSelection& rOrig );
49 MultiSelection( const Range& rRange );
50 ~MultiSelection();
52 MultiSelection& operator= ( const MultiSelection& rOrig );
54 void SelectAll( bool bSelect = true );
55 bool Select( sal_Int32 nIndex, bool bSelect = true );
56 void Select( const Range& rIndexRange, bool bSelect = true );
57 bool IsSelected( sal_Int32 nIndex ) const;
58 bool IsAllSelected() const
59 { return nSelCount == aTotRange.Len(); }
60 sal_Int32 GetSelectCount() const { return nSelCount; }
62 void SetTotalRange( const Range& rTotRange );
63 void Insert( sal_Int32 nIndex, sal_Int32 nCount = 1 );
64 void Remove( sal_Int32 nIndex );
65 void Reset();
67 const Range& GetTotalRange() const { return aTotRange; }
68 sal_Int32 FirstSelected();
69 sal_Int32 LastSelected();
70 sal_Int32 NextSelected();
72 sal_Int32 GetRangeCount() const { return aSels.size(); }
73 const Range& GetRange( sal_Int32 nRange ) const { return aSels[nRange]; }
76 class SAL_WARN_UNUSED TOOLS_DLLPUBLIC StringRangeEnumerator
78 struct Range
80 sal_Int32 nFirst;
81 sal_Int32 nLast;
83 Range( sal_Int32 i_nFirst, sal_Int32 i_nLast ) : nFirst( i_nFirst ), nLast( i_nLast ) {}
85 std::vector< StringRangeEnumerator::Range > maSequence;
86 sal_Int32 mnCount;
87 sal_Int32 mnMin;
88 sal_Int32 mnMax;
89 sal_Int32 mnOffset;
90 bool mbValidInput;
92 bool setRange( const OUString& i_rNewRange );
93 bool insertRange( sal_Int32 nFirst, sal_Int32 nLast, bool bSequence );
94 void insertJoinedRanges( const std::vector< sal_Int32 >& rNumbers );
95 bool checkValue( sal_Int32, const std::set< sal_Int32 >* i_pPossibleValues = nullptr ) const;
96 public:
97 class TOOLS_DLLPUBLIC Iterator
99 const StringRangeEnumerator* pEnumerator;
100 const std::set< sal_Int32 >* pPossibleValues;
101 sal_Int32 nRangeIndex;
102 sal_Int32 nCurrent;
104 friend class StringRangeEnumerator;
105 Iterator( const StringRangeEnumerator* i_pEnum,
106 const std::set< sal_Int32 >* i_pPossibleValues,
107 sal_Int32 i_nRange,
108 sal_Int32 i_nCurrent )
109 : pEnumerator( i_pEnum ), pPossibleValues( i_pPossibleValues )
110 , nRangeIndex( i_nRange ), nCurrent( i_nCurrent ) {}
112 public:
113 Iterator& operator++();
114 sal_Int32 operator*() const { return nCurrent;}
115 bool operator==(const Iterator&) const;
116 bool operator!=(const Iterator& i_rComp) const
117 { return ! (*this == i_rComp); }
120 friend class StringRangeEnumerator::Iterator;
122 StringRangeEnumerator( const OUString& i_rInput,
123 sal_Int32 i_nMinNumber,
124 sal_Int32 i_nMaxNumber,
125 sal_Int32 i_nLogicalOffset = -1
128 sal_Int32 size() const { return mnCount; }
129 Iterator begin( const std::set< sal_Int32 >* i_pPossibleValues = nullptr ) const;
130 Iterator end( const std::set< sal_Int32 >* i_pPossibleValues = nullptr ) const;
132 bool hasValue( sal_Int32 nValue, const std::set< sal_Int32 >* i_pPossibleValues = nullptr ) const;
135 i_rPageRange: the string to be changed into a sequence of numbers
136 valid format example "5-3,9,9,7-8" ; instead of ',' ';' or ' ' are allowed as well
137 o_rPageVector: the output sequence of numbers
138 i_nLogicalOffset: an offset to be applied to each number in the string before inserting it in the resulting sequence
139 example: a user enters page numbers from 1 to n (since that is logical)
140 of course usable page numbers in code would start from 0 and end at n-1
141 so the logical offset would be -1
142 i_nMinNumber: the minimum allowed number
143 i_nMaxNumber: the maximum allowed number
145 @returns: true if the input string was valid, o_rPageVector will contain the resulting sequence
146 false if the input string was invalid, o_rPageVector will contain
147 the sequence that parser is able to extract
149 behavior:
150 - only non-negative sequence numbers are allowed
151 - only non-negative values in the input string are allowed
152 - the string "-3" means the sequence i_nMinNumber to 3
153 - the string "3-" means the sequence 3 to i_nMaxNumber
154 - the string "-" means the sequence i_nMinNumber to i_nMaxNumber
155 - single number that doesn't fit in [i_nMinNumber,i_nMaxNumber] will be ignored
156 - range that doesn't fit in [i_nMinNumber,i_nMaxNumber] will be adjusted
158 static bool getRangesFromString( const OUString& i_rPageRange,
159 std::vector< sal_Int32 >& o_rPageVector,
160 sal_Int32 i_nMinNumber,
161 sal_Int32 i_nMaxNumber,
162 sal_Int32 i_nLogicalOffset = -1,
163 std::set< sal_Int32 > const * i_pPossibleValues = nullptr
167 #endif
169 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */