docs
Loading...
Searching...
No Matches
ClipboardData.h
Go to the documentation of this file.
1///
2/// Copyright (C) 2026 Ultralight, Inc. All rights reserved.
3/// A license is required for commercial use. https://ultralig.ht
4///
5#pragma once
6#include <Ultralight/Buffer.h>
8#include <Ultralight/RefPtr.h>
9#include <Ultralight/String.h>
10#include <cstddef>
11
12namespace ultralight {
13
14///
15/// A single clipboard value: empty, UTF-8 text, or raw bytes.
16///
17/// This is the variant value type of a ClipboardData entry. It converts between strings and
18/// buffers so you can pass either directly to ClipboardData::Set().
19///
21 public:
22 ///
23 /// Create an empty value.
24 ///
26
27 ///
28 /// Create an empty value (lets you pass `nullptr` where a value is expected).
29 ///
30 ClipboardValue(std::nullptr_t);
31
32 ///
33 /// Create a text value.
34 ///
36
37 ///
38 /// Create a text value from a null-terminated UTF-8 string.
39 ///
40 ClipboardValue(const char* text);
41
42 ///
43 /// Create a bytes value (a null buffer creates an empty value).
44 ///
46
47 ///
48 /// Copy constructor.
49 ///
51
52 ///
53 /// Copy assignment.
54 ///
56
57 ///
58 /// Move constructor (`other` becomes empty).
59 ///
60 ClipboardValue(ClipboardValue&& other) noexcept;
61
62 ///
63 /// Move assignment (`other` becomes empty).
64 ///
66
67 ///
68 /// Destructor.
69 ///
71
72 ///
73 /// Whether or not this value is empty (neither text nor bytes).
74 ///
75 [[nodiscard]] bool is_empty() const;
76
77 ///
78 /// Whether or not this value holds text.
79 ///
80 [[nodiscard]] bool is_text() const;
81
82 ///
83 /// Whether or not this value holds raw bytes.
84 ///
85 [[nodiscard]] bool is_bytes() const;
86
87 ///
88 /// Get the text (an empty string when this value doesn't hold text).
89 ///
90 [[nodiscard]] const String& text() const;
91
92 ///
93 /// Get the bytes (a null pointer when this value doesn't hold bytes).
94 ///
95 [[nodiscard]] RefPtr<Buffer> bytes() const;
96
97 private:
98 enum class Kind : uint8_t { Empty, Text, Bytes };
99 Kind kind_;
100 String text_;
101 RefPtr<Buffer> bytes_;
102};
103
104///
105/// Clipboard contents, stored as multiple type representations of the same data.
106///
107/// Clipboard data is stored in multiple formats, keyed by MIME type (eg, `text/plain`, `text/html`)
108/// or an application-defined type string.
109///
110/// ## Building a Payload
111///
112/// Use ClipboardData::Create() to create a payload, then call Set() to add entries.
113/// Set() is chainable, so you can build a multi-format payload in one expression:
114///
115/// ```
116/// // Create a payload with a single text/plain entry.
117/// auto data = ClipboardData::Create("hello world");
118///
119/// // Add a text/html entry to the same payload.
120/// data->Set("text/html", "<b>hello world</b>");
121/// ```
122///
123/// Entry order is preserved: when you build a payload yourself you should add the richest
124/// format first (the order the OS clipboard will advertise them in).
125///
126/// @note Use bare MIME types (`text/plain`, not `text/plain;charset=utf-8`)-- the library
127/// normalizes charset-suffixes at its own boundaries.
128///
130 public:
131 ///
132 /// Create an empty payload.
133 ///
134 /// @return Returns a ref-pointer to a new ClipboardData instance.
135 ///
137
138 ///
139 /// Create a payload holding a single `text/plain` entry.
140 ///
141 /// @return Returns a ref-pointer to a new ClipboardData instance.
142 ///
144
145 ///
146 /// Set the value for a type (replaces any existing entry with the same type).
147 ///
148 /// @param type The entry's type (a bare MIME type or an application-defined string).
149 ///
150 /// @param value The value to store (text or bytes; converts implicitly from String,
151 /// C-string, and RefPtr<Buffer>).
152 ///
153 /// @return Returns this ClipboardData so you can chain further calls.
154 ///
155 virtual ClipboardData& Set(const String& type, const ClipboardValue& value) = 0;
156
157 ///
158 /// Remove the entry for a type (does nothing if no such entry exists).
159 ///
160 /// @return Returns this ClipboardData so you can chain further calls.
161 ///
162 virtual ClipboardData& Remove(const String& type) = 0;
163
164 ///
165 /// Whether or not an entry exists for a type.
166 ///
167 [[nodiscard]] virtual bool Has(const String& type) const = 0;
168
169 ///
170 /// Get the value for a type (an empty value when no such entry exists).
171 ///
172 [[nodiscard]] virtual ClipboardValue Get(const String& type) const = 0;
173
174 ///
175 /// Get the `text/plain` entry's text (an empty string when there is none, or when that entry
176 /// holds bytes).
177 ///
178 [[nodiscard]] virtual String AsText() const = 0;
179
180 ///
181 /// Get the `text/html` entry's text (an empty string when there is none, or when that entry
182 /// holds bytes).
183 ///
184 [[nodiscard]] virtual String AsHTML() const = 0;
185
186 ///
187 /// Get the number of entries.
188 ///
189 [[nodiscard]] virtual size_t size() const = 0;
190
191 ///
192 /// Get the type of the entry at a certain index, in insertion order (an empty string when
193 /// `index` is out of range).
194 ///
195 [[nodiscard]] virtual String type_at(size_t index) const = 0;
196
197 ///
198 /// Get the URL of the document this payload came from (can be empty).
199 ///
200 [[nodiscard]] virtual String source_url() const = 0;
201
202 ///
203 /// Set the URL of the document this payload came from.
204 ///
205 /// The library sets this when copying page content; on Windows it becomes the HTML clipboard
206 /// format's SourceURL.
207 ///
208 virtual void set_source_url(const String& url) = 0;
209
210 protected:
212 virtual ~ClipboardData();
215};
216
217} // namespace ultralight
#define UExport
Definition Exports.h:22
virtual ClipboardData & Remove(const String &type)=0
Remove the entry for a type (does nothing if no such entry exists).
virtual String AsText() const =0
Get the text/plain entry's text (an empty string when there is none, or when that entry holds bytes).
virtual bool Has(const String &type) const =0
Whether or not an entry exists for a type.
void operator=(const ClipboardData &)
virtual void set_source_url(const String &url)=0
Set the URL of the document this payload came from.
virtual ClipboardData & Set(const String &type, const ClipboardValue &value)=0
Set the value for a type (replaces any existing entry with the same type).
virtual String AsHTML() const =0
Get the text/html entry's text (an empty string when there is none, or when that entry holds bytes).
static RefPtr< ClipboardData > Create(const String &text)
Create a payload holding a single text/plain entry.
ClipboardData(const ClipboardData &)
virtual String type_at(size_t index) const =0
Get the type of the entry at a certain index, in insertion order (an empty string when index is out o...
virtual size_t size() const =0
Get the number of entries.
virtual ClipboardValue Get(const String &type) const =0
Get the value for a type (an empty value when no such entry exists).
virtual String source_url() const =0
Get the URL of the document this payload came from (can be empty).
static RefPtr< ClipboardData > Create()
Create an empty payload.
A single clipboard value: empty, UTF-8 text, or raw bytes.
Definition ClipboardData.h:20
ClipboardValue(RefPtr< Buffer > bytes)
Create a bytes value (a null buffer creates an empty value).
ClipboardValue(const String &text)
Create a text value.
ClipboardValue(ClipboardValue &&other) noexcept
Move constructor (other becomes empty).
ClipboardValue(std::nullptr_t)
Create an empty value (lets you pass nullptr where a value is expected).
ClipboardValue & operator=(ClipboardValue &&other) noexcept
Move assignment (other becomes empty).
bool is_text() const
Whether or not this value holds text.
bool is_bytes() const
Whether or not this value holds raw bytes.
ClipboardValue(const char *text)
Create a text value from a null-terminated UTF-8 string.
bool is_empty() const
Whether or not this value is empty (neither text nor bytes).
ClipboardValue & operator=(const ClipboardValue &other)
Copy assignment.
const String & text() const
Get the text (an empty string when this value doesn't hold text).
RefPtr< Buffer > bytes() const
Get the bytes (a null pointer when this value doesn't hold bytes).
ClipboardValue(const ClipboardValue &other)
Copy constructor.
ClipboardValue()
Create an empty value.
Interface for all ref-counted objects that will be managed using the RefPtr<> smart pointer.
Definition RefPtr.h:49
A nullable smart pointer.
Definition RefPtr.h:126
Unicode string container with conversions for UTF-8, UTF-16, and UTF-32.
Definition String.h:31
Root namespace for every public Ultralight type, function, and enumeration.