Loading...
Searching...
No Matches
RichTextBuffer.h
Go to the documentation of this file.
1//
2// RichTextBuffer.h
3//
4
5#ifndef MALENA_RICHTEXTBUFFER_H
6#define MALENA_RICHTEXTBUFFER_H
7
8#pragma once
9
14#include <optional>
15#include <string>
16#include <vector>
17
18namespace ml
19{
20 // ── TextAttribute ─────────────────────────────────────────────────────────
21
32 {
33 std::size_t start = 0;
34 std::size_t end = 0;
35
36 const sf::Font* font;
37 std::optional<unsigned int> charSize;
38 std::optional<sf::Color> color;
39 std::optional<bool> bold;
40 std::optional<bool> italic;
41 std::optional<bool> underline;
42
43 TextAttribute() : font(nullptr) {}
44 };
45
46 // ── RichTextBuffer ────────────────────────────────────────────────────────
47
69 {
70 public:
71 // ── Construction ──────────────────────────────────────────────────────
72
73 RichTextBuffer() = default;
74
75 // ── Buffer access ─────────────────────────────────────────────────────
76
78 [[nodiscard]] const std::string& getText() const;
79
81 [[nodiscard]] std::size_t size() const;
82
84 [[nodiscard]] bool empty() const;
85
87 [[nodiscard]] const std::vector<TextAttribute>& getAttributes() const;
88
89 // ── Cursor ────────────────────────────────────────────────────────────
90
92 [[nodiscard]] std::size_t getCursor() const;
93
100 void setCursor(std::size_t pos);
101
108 void moveCursor(int delta, bool extendSelection = false);
109
116 void moveCursorByWord(int direction, bool extendSelection = false);
117
122 void moveCursorToLineStart(bool extendSelection = false);
123
128 void moveCursorToLineEnd(bool extendSelection = false);
129
130 // ── Selection ─────────────────────────────────────────────────────────
131
133 [[nodiscard]] bool hasSelection() const;
134
136 [[nodiscard]] std::size_t getSelectionStart() const;
137
139 [[nodiscard]] std::size_t getSelectionEnd() const;
140
147 void setSelection(std::size_t start, std::size_t end);
148
150 void selectAll();
151
154
156 [[nodiscard]] std::string getSelectedText() const;
157
158 // ── Editing ───────────────────────────────────────────────────────────
159
168 void insert(const std::string& text);
169
174 void insertChar(char32_t unicode);
175
180 void backspace();
181
187
191 void clear();
192
199 void setText(const std::string& text);
200
201 // ── Attributes ────────────────────────────────────────────────────────
202
214
227 std::size_t index,
228 const sf::Font* defaultFont,
229 unsigned int defaultSize,
230 sf::Color defaultColor) const;
231
232 // ── Clipboard helpers ─────────────────────────────────────────────────
233
238 void copyToClipboard() const;
239
245
250
251 private:
252 std::string _text;
253 std::vector<TextAttribute> _attributes;
254
255 std::size_t _cursor = 0;
256 std::size_t _selStart = 0;
257 std::size_t _selEnd = 0;
258 bool _hasSelection = false;
259
260 // Pending attribute for next insert (applied when no selection active)
261 TextAttribute _pendingAttr;
262 bool _hasPending = false;
263
264 void deleteRange(std::size_t start, std::size_t end);
265 void shiftAttributesAfterInsert(std::size_t pos, std::size_t count);
266 void shiftAttributesAfterDelete(std::size_t start, std::size_t count);
267 void splitAttributeAt(std::size_t pos);
268 void mergeAdjacentAttributes();
269 void normalizeAttributes();
270
271 std::size_t findWordBoundary(std::size_t pos, int direction) const;
272 std::size_t findLineStart(std::size_t pos) const;
273 std::size_t findLineEnd(std::size_t pos) const;
274 };
275
276} // namespace ml
277
278#endif // MALENA_RICHTEXTBUFFER_H
void moveCursorToLineStart(bool extendSelection=false)
Move the cursor to the start of the current line.
void setCursor(std::size_t pos)
Set the cursor position clamped to [0, size()]. Clears the selection.
void backspace()
Delete the character immediately before the cursor (backspace). If a selection is active,...
const std::vector< TextAttribute > & getAttributes() const
Return all attribute ranges.
void setText(const std::string &text)
Replace the entire buffer content. Cursor moves to end. Clears all attributes.
std::size_t size() const
Return the number of characters in the buffer.
void moveCursor(int delta, bool extendSelection=false)
Move the cursor by delta characters, clamped to buffer bounds.
void moveCursorToLineEnd(bool extendSelection=false)
Move the cursor to the end of the current line.
std::size_t getCursor() const
Return the current cursor position (char index).
void setSelection(std::size_t start, std::size_t end)
Set an explicit selection range. Cursor moves to end.
void insert(const std::string &text)
Insert text at the cursor position.
void pasteFromClipboard()
Insert the system clipboard contents at the cursor.
void insertChar(char32_t unicode)
Insert a single unicode character at the cursor position.
void clearSelection()
Clear the selection without moving the cursor.
bool empty() const
Return true if the buffer contains no characters.
void selectAll()
Select all text.
void deleteForward()
Delete the character immediately after the cursor (delete key). If a selection is active,...
std::size_t getSelectionStart() const
Return the selection start (always <= selectionEnd).
void copyToClipboard() const
Copy the current selection to the system clipboard. Has no effect if nothing is selected.
bool hasSelection() const
Return true if a selection is active.
std::string getSelectedText() const
Return the currently selected text. Empty if no selection.
void applyAttribute(TextAttribute attr)
Apply an attribute to the current selection.
void moveCursorByWord(int direction, bool extendSelection=false)
Move the cursor to the start of the previous or next word.
TextAttribute getAttributeAt(std::size_t index, const sf::Font *defaultFont, unsigned int defaultSize, sf::Color defaultColor) const
Return the effective TextAttribute at a given character index.
const std::string & getText() const
Return the full text content as a UTF-8 string.
std::size_t getSelectionEnd() const
Return the selection end (always >= selectionStart).
void cutToClipboard()
Cut the current selection to the system clipboard. Has no effect if nothing is selected.
void clear()
Delete all text in the buffer.
RichTextBuffer()=default
#define MALENA_API
Definition Component.h:22
A styled attribute range applied to a span of characters.
std::optional< bool > bold
nullopt = inherit
std::size_t end
Last character index (exclusive).
std::optional< sf::Color > color
nullopt = inherit default color
std::size_t start
First character index (inclusive).
std::optional< unsigned int > charSize
nullopt = inherit default size
const sf::Font * font
nullptr = inherit default font
std::optional< bool > italic
nullopt = inherit
std::optional< bool > underline
nullopt = inherit