HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
text_selection.hpp
1// Copyright Take Vos 2022.
2// Distributed under the Boost Software License, Version 1.0.
3// (See accompanying file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt)
4
5#pragma once
6
7#include "text_cursor.hpp"
8#include "../utility/utility.hpp"
9#include "../macros.hpp"
10#include <tuple>
11#include <cstdlib>
12#include <algorithm>
13
14hi_export_module(hikogui.text.text_selection);
15
16
17hi_export namespace hi::inline v1 {
18
20public:
21 constexpr text_selection() noexcept = default;
22 constexpr text_selection(text_selection const&) noexcept = default;
23 constexpr text_selection(text_selection&&) noexcept = default;
24 constexpr text_selection& operator=(text_selection const&) noexcept = default;
25 constexpr text_selection& operator=(text_selection&&) noexcept = default;
26
27 constexpr text_selection& resize(size_t size) noexcept
28 {
29 _cursor.resize(size);
30 _start_first.resize(size);
31 _start_last.resize(size);
32 _finish_first.resize(size);
33 _finish_last.resize(size);
34 return *this;
35 }
36
37 constexpr text_cursor cursor() const noexcept
38 {
39 return _cursor;
40 }
41
47 constexpr std::pair<text_cursor, text_cursor> selection(size_t size) const noexcept
48 {
49 auto first = std::min(_start_first, _finish_first);
50 auto last = std::max(_start_last, _finish_last);
51
52 first = first.before_neighbor(size);
53 last = last.after_neighbor(size);
54 return {first, last};
55 }
56
61 {
62 auto first = std::min(_start_first, _finish_first);
63 auto last = std::max(_start_last, _finish_last);
64
65 auto first_index = first.after() ? first.index() + 1 : first.index();
66 auto last_index = last.after() ? last.index() + 1 : last.index();
67 return {first_index, last_index};
68 }
69
70 [[nodiscard]] constexpr bool empty() const noexcept
71 {
72 auto const[first_index, last_index] = selection_indices();
73 return first_index >= last_index;
74 }
75
76 constexpr explicit operator bool() const noexcept
77 {
78 return not empty();
79 }
80
81 constexpr text_selection& clear_selection(size_t size) noexcept
82 {
83 return set_cursor(_cursor.resize(size));
84 }
85
86 constexpr text_selection& set_cursor(text_cursor new_cursor) noexcept
87 {
88 _cursor = _start_first = _start_last = _finish_first = _finish_last = new_cursor;
89 hi_axiom(holds_invariant());
90 return *this;
91 }
92
93 constexpr text_selection& operator=(text_cursor const& rhs) noexcept
94 {
95 return set_cursor(rhs);
96 }
97
98 constexpr void start_selection(text_cursor new_cursor, text_cursor first, text_cursor last) noexcept
99 {
100 _start_first = _finish_first = first;
101 _start_last = _finish_last = last;
102 _cursor = new_cursor == first ? first : last;
103 hi_axiom(holds_invariant());
104 }
105
106 constexpr void start_selection(text_cursor new_cursor, std::pair<text_cursor, text_cursor> selection) noexcept
107 {
108 return start_selection(new_cursor, selection.first, selection.second);
109 }
110
111 constexpr void drag_selection(text_cursor drag_cursor) noexcept
112 {
113 _finish_first = _finish_last = drag_cursor;
114 _cursor = drag_cursor;
115 }
116
117 constexpr void drag_selection(text_cursor drag_cursor, text_cursor first, text_cursor last) noexcept
118 {
119 _finish_first = first;
120 _finish_last = last;
121 _cursor = first < _start_first ? first : last > _start_last ? last : drag_cursor == first ? first : last;
122 hi_axiom(holds_invariant());
123 }
124
125 constexpr void drag_selection(text_cursor drag_cursor, std::pair<text_cursor, text_cursor> selection) noexcept
126 {
127 return drag_selection(drag_cursor, selection.first, selection.second);
128 }
129
130 [[nodiscard]] constexpr friend bool operator==(text_selection const&, text_selection const&) noexcept = default;
131
132private:
133 [[nodiscard]] constexpr bool holds_invariant() const noexcept
134 {
135 return _start_first <= _start_last and _finish_first <= _finish_last and
136 (_cursor == _start_first or _cursor == _start_last or _cursor == _finish_first or _cursor == _finish_last);
137 }
138
144 text_cursor _cursor;
145
148 text_cursor _start_first;
149
152 text_cursor _start_last;
153
156 text_cursor _finish_first;
157
160 text_cursor _finish_last;
161};
162
163} // namespace hi::inline v1
DOXYGEN BUG.
Definition algorithm_misc.hpp:20
A cursor-position in text.
Definition text_cursor.hpp:26
Definition text_selection.hpp:19
constexpr std::pair< size_t, size_t > selection_indices() const noexcept
Get the text indices for the selection.
Definition text_selection.hpp:60
constexpr std::pair< text_cursor, text_cursor > selection(size_t size) const noexcept
Return the selection of characters.
Definition text_selection.hpp:47
T max(T... args)
T min(T... args)