android: Update app-specific/MIME type icons
[LibreOffice.git] / include / tools / multisel.hxx
blobea7a1343feeb0373f87bd941be3a205e5b7b34f9
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 <cstddef>
27 #include <vector>
28 #include <o3tl/sorted_vector.hxx>
30 #define SFX_ENDOFSELECTION (-1)
32 class SAL_WARN_UNUSED TOOLS_DLLPUBLIC MultiSelection
34 private:
35 std::vector< Range >
36 aSels; // array of SV-selections
37 Range aTotRange; // total range of indexes
38 std::size_t nCurSubSel; // index in aSels of current selected index
39 sal_Int32 nCurIndex; // current selected entry
40 sal_Int32 nSelCount; // number of selected indexes
41 bool bCurValid; // are nCurIndex and nCurSubSel valid
43 TOOLS_DLLPRIVATE void ImplClear();
44 TOOLS_DLLPRIVATE std::size_t ImplFindSubSelection( sal_Int32 nIndex ) const;
45 TOOLS_DLLPRIVATE void ImplMergeSubSelections( sal_Int32 nPos1, std::size_t nPos2 );
47 public:
48 MultiSelection();
49 MultiSelection( const MultiSelection& rOrig );
50 MultiSelection( const Range& rRange );
51 ~MultiSelection();
53 MultiSelection& operator= ( const MultiSelection& rOrig );
55 void SelectAll( bool bSelect = true );
56 bool Select( sal_Int32 nIndex, bool bSelect = true );
57 void Select( const Range& rIndexRange, bool bSelect = true );
58 bool IsSelected( sal_Int32 nIndex ) const;
59 bool IsAllSelected() const
60 { return nSelCount == aTotRange.Len(); }
61 sal_Int32 GetSelectCount() const { return nSelCount; }
63 void SetTotalRange( const Range& rTotRange );
64 void Insert( sal_Int32 nIndex, sal_Int32 nCount = 1 );
65 void Remove( sal_Int32 nIndex );
66 void Reset();
68 const Range& GetTotalRange() const { return aTotRange; }
69 sal_Int32 FirstSelected();
70 sal_Int32 LastSelected();
71 sal_Int32 NextSelected();
73 sal_Int32 GetRangeCount() const { return aSels.size(); }
74 const Range& GetRange( sal_Int32 nRange ) const { return aSels[nRange]; }
77 class SAL_WARN_UNUSED TOOLS_DLLPUBLIC StringRangeEnumerator
79 struct Range
81 sal_Int32 nFirst;
82 sal_Int32 nLast;
84 Range( sal_Int32 i_nFirst, sal_Int32 i_nLast ) : nFirst( i_nFirst ), nLast( i_nLast ) {}
86 std::vector< StringRangeEnumerator::Range > maSequence;
87 sal_Int32 mnCount;
88 sal_Int32 mnMin;
89 sal_Int32 mnMax;
90 sal_Int32 mnOffset;
91 bool mbValidInput;
93 bool setRange( std::u16string_view i_rNewRange );
94 bool insertRange( sal_Int32 nFirst, sal_Int32 nLast, bool bSequence );
95 void insertJoinedRanges( const std::vector< sal_Int32 >& rNumbers );
96 bool checkValue( sal_Int32, const o3tl::sorted_vector< sal_Int32 >* i_pPossibleValues = nullptr ) const;
97 public:
98 class TOOLS_DLLPUBLIC Iterator
100 const StringRangeEnumerator* pEnumerator;
101 const o3tl::sorted_vector< sal_Int32 >* pPossibleValues;
102 sal_Int32 nRangeIndex;
103 sal_Int32 nCurrent;
105 friend class StringRangeEnumerator;
106 Iterator( const StringRangeEnumerator* i_pEnum,
107 const o3tl::sorted_vector< sal_Int32 >* i_pPossibleValues,
108 sal_Int32 i_nRange,
109 sal_Int32 i_nCurrent )
110 : pEnumerator( i_pEnum ), pPossibleValues( i_pPossibleValues )
111 , nRangeIndex( i_nRange ), nCurrent( i_nCurrent ) {}
113 public:
114 Iterator& operator++();
115 sal_Int32 operator*() const { return nCurrent;}
116 bool operator==(const Iterator&) const;
117 bool operator!=(const Iterator& i_rComp) const
118 { return ! (*this == i_rComp); }
121 friend class StringRangeEnumerator::Iterator;
123 StringRangeEnumerator( std::u16string_view i_rInput,
124 sal_Int32 i_nMinNumber,
125 sal_Int32 i_nMaxNumber,
126 sal_Int32 i_nLogicalOffset = -1
129 sal_Int32 size() const { return mnCount; }
130 Iterator begin( const o3tl::sorted_vector< sal_Int32 >* i_pPossibleValues = nullptr ) const;
131 Iterator end( const o3tl::sorted_vector< sal_Int32 >* i_pPossibleValues = nullptr ) const;
133 bool hasValue( sal_Int32 nValue, const o3tl::sorted_vector< sal_Int32 >* i_pPossibleValues = nullptr ) const;
136 i_rPageRange: the string to be changed into a sequence of numbers
137 valid format example "5-3,9,9,7-8" ; instead of ',' ';' or ' ' are allowed as well
138 o_rPageVector: the output sequence of numbers
139 i_nLogicalOffset: an offset to be applied to each number in the string before inserting it in the resulting sequence
140 example: a user enters page numbers from 1 to n (since that is logical)
141 of course usable page numbers in code would start from 0 and end at n-1
142 so the logical offset would be -1
143 i_nMinNumber: the minimum allowed number
144 i_nMaxNumber: the maximum allowed number
146 @returns: true if the input string was valid, o_rPageVector will contain the resulting sequence
147 false if the input string was invalid, o_rPageVector will contain
148 the sequence that parser is able to extract
150 behavior:
151 - only non-negative sequence numbers are allowed
152 - only non-negative values in the input string are allowed
153 - the string "-3" means the sequence i_nMinNumber to 3
154 - the string "3-" means the sequence 3 to i_nMaxNumber
155 - the string "-" means the sequence i_nMinNumber to i_nMaxNumber
156 - single number that doesn't fit in [i_nMinNumber,i_nMaxNumber] will be ignored
157 - range that doesn't fit in [i_nMinNumber,i_nMaxNumber] will be adjusted
159 static bool getRangesFromString( std::u16string_view i_rPageRange,
160 std::vector< sal_Int32 >& o_rPageVector,
161 sal_Int32 i_nMinNumber,
162 sal_Int32 i_nMaxNumber,
163 sal_Int32 i_nLogicalOffset = -1,
164 o3tl::sorted_vector< sal_Int32 > const * i_pPossibleValues = nullptr
168 #endif
170 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */