/src/libreoffice/include/editeng/ESelection.hxx
Line | Count | Source |
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 | | |
20 | | #pragma once |
21 | | |
22 | | #include <sal/config.h> |
23 | | |
24 | | #include <editeng/EPaM.hxx> |
25 | | |
26 | | #include <cassert> |
27 | | #include <utility> |
28 | | |
29 | | struct ESelection |
30 | | { |
31 | | // Select all text |
32 | 1.93k | static constexpr ESelection All() { return { 0, 0, EE_PARA_MAX, EE_TEXTPOS_MAX }; } |
33 | | |
34 | | // Set to end without selection |
35 | 0 | static constexpr ESelection AtEnd() { return { EE_PARA_MAX, EE_TEXTPOS_MAX }; } |
36 | | |
37 | | EPaM start; |
38 | | EPaM end; |
39 | | |
40 | 31.4M | constexpr ESelection() = default; |
41 | | |
42 | | constexpr ESelection(sal_Int32 _nStartPara, sal_Int32 _nStartPos, sal_Int32 _nEndPara, |
43 | | sal_Int32 _nEndPos) |
44 | 29.3M | : start(_nStartPara, _nStartPos) |
45 | 29.3M | , end(_nEndPara, _nEndPos) |
46 | 29.3M | { |
47 | 29.3M | } |
48 | | |
49 | | constexpr ESelection(sal_Int32 nPara, sal_Int32 nPos) |
50 | 11.7M | : ESelection(nPara, nPos, nPara, nPos) |
51 | 11.7M | { |
52 | 11.7M | } |
53 | | |
54 | | constexpr explicit ESelection(const EPaM& rPos) |
55 | 8.99M | : ESelection(rPos.nPara, rPos.nIndex) |
56 | 8.99M | { |
57 | 8.99M | } |
58 | | |
59 | 198k | bool IsAdjusted() const { return !(end < start); } |
60 | | |
61 | | void Adjust() |
62 | 198k | { |
63 | 198k | if (!IsAdjusted()) |
64 | 92 | std::swap(start, end); |
65 | 198k | } |
66 | | |
67 | 1.56M | void CollapseToStart() { end = start; } |
68 | 600k | void CollapseToEnd() { start = end; } |
69 | | |
70 | 113k | bool operator==(const ESelection&) const = default; |
71 | | |
72 | | bool operator<(const ESelection& rSelection) const |
73 | 0 | { |
74 | 0 | // The selection must be adjusted. |
75 | 0 | assert(IsAdjusted() && rSelection.IsAdjusted()); |
76 | 0 | // => Only check if end of 'this' < Start of rS |
77 | 0 | return end < rSelection.start; |
78 | 0 | } |
79 | 0 | bool operator>(const ESelection& rSelection) const { return rSelection < *this; } |
80 | | |
81 | 2.03M | bool HasRange() const { return start != end; } |
82 | | }; |
83 | | |
84 | | template <typename charT, typename traits> |
85 | | inline std::basic_ostream<charT, traits>& operator<<(std::basic_ostream<charT, traits>& stream, |
86 | | ESelection const& rSelection) |
87 | | { |
88 | | return stream << "ESelection(" << rSelection.start.nPara << ',' << rSelection.start.nIndex |
89 | | << "," << rSelection.end.nPara << "," << rSelection.end.nIndex << ")"; |
90 | | } |
91 | | |
92 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |