1 /++
2  + Authors: Avaxar <avaxar@nekkl.org>
3  + Copyright: Copyright © 2023, Avaxar
4  + License: $(LINK2 https://mit-license.org, MIT License)
5  +/
6 
7 module dsdl2.clipboard;
8 @safe:
9 
10 import bindbc.sdl;
11 import dsdl2.sdl;
12 
13 import std.conv : to;
14 import std.string : toStringz;
15 
16 /++
17  + Wraps `SDL_GetClipboardText` which gets the text stored in the clipboard
18  +
19  + Returns: clipboard text content
20  + Throws: `dsdl2.SDLException` if the clipboard text failed to allocate on SDL's side
21  +/
22 string getClipboard() @trusted {
23     char* clipboard = SDL_GetClipboardText();
24     scope (exit)
25         SDL_free(clipboard);
26 
27     if (clipboard !is null) {
28         return clipboard.to!string;
29     }
30     else {
31         throw new SDLException;
32     }
33 }
34 
35 /++
36  + Wraps `SDL_HasClipboardText` which checks whether the clipboard exists and contains a non-empty text string
37  +
38  + Returns: `true` if it exists and contains a non-empty string, otherwise `false`
39  +/
40 bool hasClipboard() @trusted {
41     return SDL_HasClipboardText() == SDL_TRUE;
42 }
43 
44 /++
45  + Wraps `SDL_SetClipboardText` which puts a string of text into the clipboard
46  +
47  + Params:
48  +   text = `string` to put into the clipboard
49  + Throws: `dsdl2.SDLException` on fail when putting the string into the clipboard
50  +/
51 void setClipboard(string text) @trusted {
52     if (SDL_SetClipboardText(text.toStringz()) != 0) {
53         throw new SDLException;
54     }
55 }
56 
57 static if (sdlSupport >= SDLSupport.v2_26) {
58     /++
59      + Wraps `SDL_GetPrimarySelectionText` (from SDL 2.26) which gets the text stored in the primary selection
60      +
61      + Returns: primary selection text content
62      + Throws: `dsdl2.SDLException` if the primary selection text failed to allocate on SDL's side
63      +/
64     string getPrimarySelection() @trusted
65     in {
66         assert(getVersion() >= Version(2, 26));
67     }
68     do {
69         char* primarySelection = SDL_GetPrimarySelectionText();
70         scope (exit)
71             SDL_free(primarySelection);
72 
73         if (primarySelection !is null) {
74             return primarySelection.to!string;
75         }
76         else {
77             throw new SDLException;
78         }
79     }
80 
81     /++
82      + Wraps `SDL_HasPrimarySelectionText` (from SDL 2.26) which checks whether the primary selection exists
83      + and contains a non-empty text string
84      +
85      + Returns: `true` if it exists and contains a non-empty string, otherwise `false`
86      +/
87     bool hasPrimarySelection() @trusted
88     in {
89         assert(getVersion() >= Version(2, 26));
90     }
91     do {
92         return SDL_HasPrimarySelectionText() == SDL_TRUE;
93     }
94 
95     /++
96      + Wraps `SDL_SetPrimarySelectionText` (from SDL 2.26) which puts a string of text into the primary
97      + selection
98      +
99      + Params:
100      +   text = `string` to put into the primary selection
101      + Throws: `dsdl2.SDLException` on fail when putting the string into the primary selection
102      +/
103     void setPrimarySelection(string text) @trusted
104     in {
105         assert(getVersion() >= Version(2, 26));
106     }
107     do {
108         if (SDL_SetPrimarySelectionText(text.toStringz()) != 0) {
109             throw new SDLException;
110         }
111     }
112 }