|
| 1 | +{ |
| 2 | + This file is part of: |
| 3 | +
|
| 4 | + SDL3 for Pascal |
| 5 | + (https://github.com/PascalGameDevelopment/SDL3-for-Pascal) |
| 6 | + SPDX-License-Identifier: Zlib |
| 7 | +} |
| 8 | + |
| 9 | +{* |
| 10 | + * # CategoryClipboard |
| 11 | + * |
| 12 | + * SDL provides access to the system clipboard, both for reading information |
| 13 | + * from other processes and publishing information of its own. |
| 14 | + * |
| 15 | + * This is not just text! SDL apps can access and publish data by mimetype. |
| 16 | + * |
| 17 | + * ## Basic use (text) |
| 18 | + * |
| 19 | + * Obtaining and publishing simple text to the system clipboard is as easy as |
| 20 | + * calling SDL_GetClipboardText() and SDL_SetClipboardText(), respectively. |
| 21 | + * These deal with C strings in UTF-8 encoding. Data transmission and encoding |
| 22 | + * conversion is completely managed by SDL. |
| 23 | + * |
| 24 | + * ## Clipboard callbacks (data other than text) |
| 25 | + * |
| 26 | + * Things get more complicated when the clipboard contains something other |
| 27 | + * than text. Not only can the system clipboard contain data of any type, in |
| 28 | + * some cases it can contain the same data in different formats! For example, |
| 29 | + * an image painting app might let the user copy a graphic to the clipboard, |
| 30 | + * and offers it in .BMP, .JPG, or .PNG format for other apps to consume. |
| 31 | + * |
| 32 | + * Obtaining clipboard data ("pasting") like this is a matter of calling |
| 33 | + * SDL_GetClipboardData() and telling it the mimetype of the data you want. |
| 34 | + * But how does one know if that format is available? SDL_HasClipboardData() |
| 35 | + * can report if a specific mimetype is offered, and |
| 36 | + * SDL_GetClipboardMimeTypes() can provide the entire list of mimetypes |
| 37 | + * available, so the app can decide what to do with the data and what formats |
| 38 | + * it can support. |
| 39 | + * |
| 40 | + * Setting the clipboard ("copying") to arbitrary data is done with |
| 41 | + * SDL_SetClipboardData. The app does not provide the data in this call, but |
| 42 | + * rather the mimetypes it is willing to provide and a callback function. |
| 43 | + * During the callback, the app will generate the data. This allows massive |
| 44 | + * data sets to be provided to the clipboard, without any data being copied |
| 45 | + * before it is explicitly requested. More specifically, it allows an app to |
| 46 | + * offer data in multiple formats without providing a copy of all of them |
| 47 | + * upfront. If the app has an image that it could provide in PNG or JPG |
| 48 | + * format, it doesn't have to encode it to either of those unless and until |
| 49 | + * something tries to paste it. |
| 50 | + * |
| 51 | + * ## Primary Selection |
| 52 | + * |
| 53 | + * The X11 and Wayland video targets have a concept of the "primary selection" |
| 54 | + * in addition to the usual clipboard. This is generally highlighted (but not |
| 55 | + * explicitly copied) text from various apps. SDL offers APIs for this through |
| 56 | + * SDL_GetPrimarySelectionText() and SDL_SetPrimarySelectionText(). SDL offers |
| 57 | + * these APIs on platforms without this concept, too, but only so far that it |
| 58 | + * will keep a copy of a string that the app sets for later retrieval; the |
| 59 | + * operating system will not ever attempt to change the string externally if |
| 60 | + * it doesn't support a primary selection. |
| 61 | + } |
| 62 | + |
| 63 | +{ Function prototypes } |
| 64 | + |
| 65 | +{* |
| 66 | + * Put UTF-8 text into the clipboard. |
| 67 | + * |
| 68 | + * \param text the text to store in the clipboard. |
| 69 | + * \returns true on success or false on failure; call SDL_GetError() for more |
| 70 | + * information. |
| 71 | + * |
| 72 | + * \threadsafety You may only call this function from the main thread. |
| 73 | + * |
| 74 | + * \since This function is available since SDL 3.1.3. |
| 75 | + * |
| 76 | + * \sa SDL_GetClipboardText |
| 77 | + * \sa SDL_HasClipboardText |
| 78 | + } |
| 79 | +function SDL_SetClipboardText(text: PAnsiChar): cbool; cdecl; |
| 80 | + external SDL_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_SetClipboardText' {$ENDIF} {$ENDIF}; |
| 81 | + |
| 82 | +{* |
| 83 | + * Get UTF-8 text from the clipboard. |
| 84 | + * |
| 85 | + * This functions returns an empty string if there was not enough memory left |
| 86 | + * for a copy of the clipboard's content. |
| 87 | + * |
| 88 | + * \returns the clipboard text on success or an empty string on failure; call |
| 89 | + * SDL_GetError() for more information. This should be freed with |
| 90 | + * SDL_free() when it is no longer needed. |
| 91 | + * |
| 92 | + * \threadsafety You may only call this function from the main thread. |
| 93 | + * |
| 94 | + * \since This function is available since SDL 3.1.3. |
| 95 | + * |
| 96 | + * \sa SDL_HasClipboardText |
| 97 | + * \sa SDL_SetClipboardText |
| 98 | + } |
| 99 | +function SDL_GetClipboardText: PAnsiChar; cdecl; |
| 100 | + external SDL_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_GetClipboardText' {$ENDIF} {$ENDIF}; |
| 101 | + |
| 102 | +{* |
| 103 | + * Query whether the clipboard exists and contains a non-empty text string. |
| 104 | + * |
| 105 | + * \returns true if the clipboard has text, or false if it does not. |
| 106 | + * |
| 107 | + * \threadsafety You may only call this function from the main thread. |
| 108 | + * |
| 109 | + * \since This function is available since SDL 3.1.3. |
| 110 | + * |
| 111 | + * \sa SDL_GetClipboardText |
| 112 | + * \sa SDL_SetClipboardText |
| 113 | + } |
| 114 | +function SDL_HasClipboardText: cbool; cdecl; |
| 115 | + external SDL_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_HasClipboardText' {$ENDIF} {$ENDIF}; |
| 116 | + |
| 117 | +{* |
| 118 | + * Put UTF-8 text into the primary selection. |
| 119 | + * |
| 120 | + * \param text the text to store in the primary selection. |
| 121 | + * \returns true on success or false on failure; call SDL_GetError() for more |
| 122 | + * information. |
| 123 | + * |
| 124 | + * \threadsafety You may only call this function from the main thread. |
| 125 | + * |
| 126 | + * \since This function is available since SDL 3.1.3. |
| 127 | + * |
| 128 | + * \sa SDL_GetPrimarySelectionText |
| 129 | + * \sa SDL_HasPrimarySelectionText |
| 130 | + } |
| 131 | +function SDL_SetPrimarySelectionText(text: PAnsiChar): cbool; cdecl; |
| 132 | + external SDL_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_SetPrimarySelectionText' {$ENDIF} {$ENDIF}; |
| 133 | + |
| 134 | +{* |
| 135 | + * Get UTF-8 text from the primary selection. |
| 136 | + * |
| 137 | + * This functions returns an empty string if there was not enough memory left |
| 138 | + * for a copy of the primary selection's content. |
| 139 | + * |
| 140 | + * \returns the primary selection text on success or an empty string on |
| 141 | + * failure; call SDL_GetError() for more information. This should be |
| 142 | + * freed with SDL_free() when it is no longer needed. |
| 143 | + * |
| 144 | + * \threadsafety You may only call this function from the main thread. |
| 145 | + * |
| 146 | + * \since This function is available since SDL 3.1.3. |
| 147 | + * |
| 148 | + * \sa SDL_HasPrimarySelectionText |
| 149 | + * \sa SDL_SetPrimarySelectionText |
| 150 | + } |
| 151 | +function SDL_GetPrimarySelectionText: PAnsiChar; cdecl; |
| 152 | + external SDL_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_GetPrimarySelectionText' {$ENDIF} {$ENDIF}; |
| 153 | + |
| 154 | +{* |
| 155 | + * Query whether the primary selection exists and contains a non-empty text |
| 156 | + * string. |
| 157 | + * |
| 158 | + * \returns true if the primary selection has text, or false if it does not. |
| 159 | + * |
| 160 | + * \threadsafety You may only call this function from the main thread. |
| 161 | + * |
| 162 | + * \since This function is available since SDL 3.1.3. |
| 163 | + * |
| 164 | + * \sa SDL_GetPrimarySelectionText |
| 165 | + * \sa SDL_SetPrimarySelectionText |
| 166 | + } |
| 167 | +function SDL_HasPrimarySelectionText: cbool; cdecl; |
| 168 | + external SDL_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_HasPrimarySelectionText' {$ENDIF} {$ENDIF}; |
| 169 | + |
| 170 | +{* |
| 171 | + * Callback function that will be called when data for the specified mime-type |
| 172 | + * is requested by the OS. |
| 173 | + * |
| 174 | + * The callback function is called with nil as the mime_type when the |
| 175 | + * clipboard is cleared or new data is set. The clipboard is automatically |
| 176 | + * cleared in SDL_Quit(). |
| 177 | + * |
| 178 | + * \param userdata a Pointer to provided user data. |
| 179 | + * \param mime_type the requested mime-type. |
| 180 | + * \param size a Pointer filled in with the length of the returned data. |
| 181 | + * \returns a Pointer to the data for the provided mime-type. Returning nil |
| 182 | + * or setting length to 0 will cause no data to be sent to the |
| 183 | + * "receiver". It is up to the receiver to handle this. Essentially |
| 184 | + * returning no data is more or less undefined behavior and may cause |
| 185 | + * breakage in receiving applications. The returned data will not be |
| 186 | + * freed so it needs to be retained and dealt with internally. |
| 187 | + * |
| 188 | + * \since This function is available since SDL 3.1.3. |
| 189 | + * |
| 190 | + * \sa SDL_SetClipboardData |
| 191 | + } |
| 192 | +type |
| 193 | + TSDL_ClipboardDataCallback = function(userdata: Pointer; mime_type: PAnsiChar; size: pcsize_t): Pointer; cdecl; |
| 194 | + |
| 195 | +{* |
| 196 | + * Callback function that will be called when the clipboard is cleared, or new |
| 197 | + * data is set. |
| 198 | + * |
| 199 | + * \param userdata a Pointer to provided user data. |
| 200 | + * |
| 201 | + * \since This function is available since SDL 3.1.3. |
| 202 | + * |
| 203 | + * \sa SDL_SetClipboardData |
| 204 | + } |
| 205 | +type |
| 206 | + TSDL_ClipboardCleanupCallback = procedure(userdata: Pointer); cdecl; |
| 207 | + |
| 208 | +{* |
| 209 | + * Offer clipboard data to the OS. |
| 210 | + * |
| 211 | + * Tell the operating system that the application is offering clipboard data |
| 212 | + * for each of the proivded mime-types. Once another application requests the |
| 213 | + * data the callback function will be called allowing it to generate and |
| 214 | + * respond with the data for the requested mime-type. |
| 215 | + * |
| 216 | + * The size of text data does not include any terminator, and the text does |
| 217 | + * not need to be null terminated (e.g. you can directly copy a portion of a |
| 218 | + * document) |
| 219 | + * |
| 220 | + * \param callback a function Pointer to the function that provides the |
| 221 | + * clipboard data. |
| 222 | + * \param cleanup a function Pointer to the function that cleans up the |
| 223 | + * clipboard data. |
| 224 | + * \param userdata an opaque Pointer that will be forwarded to the callbacks. |
| 225 | + * \param mime_types a list of mime-types that are being offered. |
| 226 | + * \param num_mime_types the number of mime-types in the mime_types list. |
| 227 | + * \returns true on success or false on failure; call SDL_GetError() for more |
| 228 | + * information. |
| 229 | + * |
| 230 | + * \threadsafety You may only call this function from the main thread. |
| 231 | + * |
| 232 | + * \since This function is available since SDL 3.1.3. |
| 233 | + * |
| 234 | + * \sa SDL_ClearClipboardData |
| 235 | + * \sa SDL_GetClipboardData |
| 236 | + * \sa SDL_HasClipboardData |
| 237 | + } |
| 238 | +function SDL_SetClipboardData(callback: TSDL_ClipboardDataCallback; cleanup: TSDL_ClipboardCleanupCallback; userdata: Pointer; mime_types: PPAnsiChar; num_mime_types: csize_t): cbool; cdecl; |
| 239 | + external SDL_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_SetClipboardData' {$ENDIF} {$ENDIF}; |
| 240 | + |
| 241 | +{* |
| 242 | + * Clear the clipboard data. |
| 243 | + * |
| 244 | + * \returns true on success or false on failure; call SDL_GetError() for more |
| 245 | + * information. |
| 246 | + * |
| 247 | + * \threadsafety You may only call this function from the main thread. |
| 248 | + * |
| 249 | + * \since This function is available since SDL 3.1.3. |
| 250 | + * |
| 251 | + * \sa SDL_SetClipboardData |
| 252 | + } |
| 253 | +function SDL_ClearClipboardData: cbool; cdecl; |
| 254 | + external SDL_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_ClearClipboardData' {$ENDIF} {$ENDIF}; |
| 255 | + |
| 256 | +{* |
| 257 | + * Get the data from clipboard for a given mime type. |
| 258 | + * |
| 259 | + * The size of text data does not include the terminator, but the text is |
| 260 | + * guaranteed to be null terminated. |
| 261 | + * |
| 262 | + * \param mime_type the mime type to read from the clipboard. |
| 263 | + * \param size a Pointer filled in with the length of the returned data. |
| 264 | + * \returns the retrieved data buffer or nil on failure; call SDL_GetError() |
| 265 | + * for more information. This should be freed with SDL_free() when it |
| 266 | + * is no longer needed. |
| 267 | + * |
| 268 | + * \threadsafety You may only call this function from the main thread. |
| 269 | + * |
| 270 | + * \since This function is available since SDL 3.1.3. |
| 271 | + * |
| 272 | + * \sa SDL_HasClipboardData |
| 273 | + * \sa SDL_SetClipboardData |
| 274 | + } |
| 275 | +function SDL_GetClipboardData(mime_type: PAnsiChar; size: pcsize_t): Pointer; cdecl; |
| 276 | + external SDL_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_GetClipboardData' {$ENDIF} {$ENDIF}; |
| 277 | + |
| 278 | +{* |
| 279 | + * Query whether there is data in the clipboard for the provided mime type. |
| 280 | + * |
| 281 | + * \param mime_type the mime type to check for data for. |
| 282 | + * \returns true if there exists data in clipboard for the provided mime type, |
| 283 | + * false if it does not. |
| 284 | + * |
| 285 | + * \threadsafety You may only call this function from the main thread. |
| 286 | + * |
| 287 | + * \since This function is available since SDL 3.1.3. |
| 288 | + * |
| 289 | + * \sa SDL_SetClipboardData |
| 290 | + * \sa SDL_GetClipboardData |
| 291 | + } |
| 292 | +function SDL_HasClipboardData(mime_type: PAnsiChar): cbool; cdecl; |
| 293 | + external SDL_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_HasClipboardData' {$ENDIF} {$ENDIF}; |
| 294 | + |
| 295 | +{* |
| 296 | + * Retrieve the list of mime types available in the clipboard. |
| 297 | + * |
| 298 | + * \param num_mime_types a Pointer filled with the number of mime types, may |
| 299 | + * be nil. |
| 300 | + * \returns a null terminated array of strings with mime types, or nil on |
| 301 | + * failure; call SDL_GetError() for more information. This should be |
| 302 | + * freed with SDL_free() when it is no longer needed. |
| 303 | + * |
| 304 | + * \threadsafety You may only call this function from the main thread. |
| 305 | + * |
| 306 | + * \since This function is available since SDL 3.1.3. |
| 307 | + * |
| 308 | + * \sa SDL_SetClipboardData |
| 309 | + } |
| 310 | +function SDL_GetClipboardMimeTypes(num_mime_types: pcsize_t):PPAnsiChar; cdecl; |
| 311 | + external SDL_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_GetClipboardMimeTypes' {$ENDIF} {$ENDIF}; |
| 312 | + |
0 commit comments