1 /*
2  * Copyright © 2020 Christian Persch
3  *
4  * This library is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as published
6  * by the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with this library.  If not, see <https://www.gnu.org/licenses/>.
16  */
17 
18 #pragma once
19 
20 #include <memory>
21 #include <optional>
22 #include <string>
23 
24 #include <gtk/gtk.h>
25 
26 #include "glib-glue.hh"
27 #include "refptr.hh"
28 #include "fwd.hh"
29 
30 namespace vte::platform {
31 
32 enum class ClipboardFormat {
33         TEXT,
34         HTML
35 };
36 
37 enum class ClipboardType {
38         CLIPBOARD = 0,
39         PRIMARY   = 1
40 };
41 
42 class Clipboard : public std::enable_shared_from_this<Clipboard> {
43 public:
44         Clipboard(Widget& delegate,
45                   ClipboardType type) /* throws */;
46         ~Clipboard() = default;
47 
48         Clipboard(Clipboard const&) = delete;
49         Clipboard(Clipboard&&) = delete;
50 
51         Clipboard& operator=(Clipboard const&) = delete;
52         Clipboard& operator=(Clipboard&&) = delete;
53 
type() const54         constexpr auto type() const noexcept { return m_type; }
55 
disown()56         void disown() noexcept
57         {
58                 m_delegate.reset();
59         }
60 
61         using OfferGetCallback = std::optional<std::string_view>(Widget::*)(Clipboard const&,
62                                                                             ClipboardFormat format);
63         using OfferClearCallback = void (Widget::*)(Clipboard const&);
64         using RequestDoneCallback = void (Widget::*)(Clipboard const&,
65                                                      std::string_view const&);
66         using RequestFailedCallback = void (Widget::*)(Clipboard const&);
67 
68         void offer_data(ClipboardFormat format,
69                         OfferGetCallback get_callback,
70                         OfferClearCallback clear_callback) /* throws */;
71 
72         void set_text(std::string_view const& text) noexcept;
73 
74         void request_text(RequestDoneCallback done_callback,
75                           RequestFailedCallback failed_callback) /* throws */;
76 
77 private:
78         vte::glib::RefPtr<GtkClipboard> m_clipboard;
79         std::weak_ptr<Widget> m_delegate;
80         ClipboardType m_type;
81 
platform() const82         auto platform() const noexcept { return m_clipboard.get(); }
83 
84         class Offer;
85         class Request;
86 
87 }; // class Clipboard
88 
89 } // namespace vte::platform
90