Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 : #ifndef INCLUDED_TOOLS_MULTISEL_HXX
20 : #define INCLUDED_TOOLS_MULTISEL_HXX
21 :
22 : #include <tools/toolsdllapi.h>
23 : #include <tools/gen.hxx>
24 : #include <rtl/ustring.hxx>
25 :
26 : #include <vector>
27 : #include <set>
28 :
29 : typedef ::std::vector< Range* > ImpSelList;
30 :
31 : #define SFX_ENDOFSELECTION ULONG_MAX
32 :
33 : class TOOLS_DLLPUBLIC MultiSelection
34 : {
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
44 :
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();
49 :
50 : public:
51 : MultiSelection();
52 : MultiSelection( const MultiSelection& rOrig );
53 : MultiSelection( const Range& rRange );
54 : ~MultiSelection();
55 :
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; }
62 :
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 0 : bool IsAllSelected() const
68 0 : { return nSelCount == sal_uIntPtr(aTotRange.Len()); }
69 0 : long GetSelectCount() const { return nSelCount; }
70 :
71 : void SetTotalRange( const Range& rTotRange );
72 : void Insert( long nIndex, long nCount = 1 );
73 : void Remove( long nIndex );
74 :
75 0 : 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();
81 :
82 0 : size_t GetRangeCount() const { return aSels.size(); }
83 0 : const Range& GetRange( size_t nRange ) const {
84 0 : return *(const Range*)aSels[nRange];
85 : }
86 : };
87 :
88 0 : class TOOLS_DLLPUBLIC StringRangeEnumerator
89 : {
90 : struct Range
91 : {
92 : sal_Int32 nFirst;
93 : sal_Int32 nLast;
94 :
95 : Range() : nFirst( -1 ), nLast( -1 ) {}
96 0 : Range( sal_Int32 i_nFirst, sal_Int32 i_nLast ) : nFirst( i_nFirst ), nLast( i_nLast ) {}
97 : };
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;
104 :
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
111 : {
112 : const StringRangeEnumerator* pEnumerator;
113 : const std::set< sal_Int32 >* pPossibleValues;
114 : sal_Int32 nRangeIndex;
115 : sal_Int32 nCurrent;
116 :
117 : friend class StringRangeEnumerator;
118 0 : 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 0 : , nRangeIndex( i_nRange ), nCurrent( i_nCurrent ) {}
124 :
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 0 : bool operator!=(const Iterator& i_rComp) const
131 0 : { return ! (*this == i_rComp); }
132 : };
133 :
134 : friend class StringRangeEnumerator::Iterator;
135 :
136 : StringRangeEnumerator( const OUString& i_rInput,
137 : sal_Int32 i_nMinNumber,
138 : sal_Int32 i_nMaxNumber,
139 : sal_Int32 i_nLogicalOffset = -1
140 : );
141 :
142 0 : 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;
145 :
146 0 : bool isValidInput() const { return mbValidInput; }
147 : bool hasValue( sal_Int32 nValue, const std::set< sal_Int32 >* i_pPossibleValues = NULL ) const;
148 :
149 : /**
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
159 :
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
163 :
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
172 : */
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
179 : );
180 : };
181 :
182 : #endif
183 :
184 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|