| 1 |
/* Copyright (C) 2002 The gtkmm Development Team |
| 2 |
* |
| 3 |
* This library is free software; you can redistribute it and/or |
| 4 |
* modify it under the terms of the GNU Lesser General Public |
| 5 |
* License as published by the Free Software Foundation; either |
| 6 |
* version 2.1 of the License, or (at your option) any later version. |
| 7 |
* |
| 8 |
* This library is distributed in the hope that it will be useful, |
| 9 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 11 |
* Lesser General Public License for more details. |
| 12 |
* |
| 13 |
* You should have received a copy of the GNU Lesser General Public |
| 14 |
* License along with this library; if not, see <https://www.gnu.org/licenses/>. |
| 15 |
*/ |
| 16 |
|
| 17 |
#include <vector> |
| 18 |
|
| 19 |
#include <gdkmm/display.h> |
| 20 |
#include <gdkmm/pixbuf.h> |
| 21 |
#include <gtkmm/targetentry.h> |
| 22 |
#include <gtkmm/selectiondata.h> |
| 23 |
#include <glibmm/object.h> |
| 24 |
|
| 25 |
_DEFS(gtkmm,gtk) |
| 26 |
_PINCLUDE(glibmm/private/object_p.h) |
| 27 |
|
| 28 |
|
| 29 |
namespace Gtk |
| 30 |
{ |
| 31 |
|
| 32 |
#ifndef DOXYGEN_SHOULD_SKIP_THIS |
| 33 |
class GTKMM_API TextBuffer; |
| 34 |
#endif //DOXYGEN_SHOULD_SKIP_THIS |
| 35 |
|
| 36 |
/** The Clipboard object represents a clipboard of data shared between different processes or between |
| 37 |
* different widgets in the same process. Each clipboard is identified by a name encoded as a GdkAtom. * |
| 38 |
* (Conversion to and from strings can be done with gdk_atom_intern() and gdk_atom_name().) The default |
| 39 |
* clipboard corresponds to the "CLIPBOARD" atom; another commonly used clipboard is the "PRIMARY" clipboard, |
| 40 |
* which, in X, traditionally contains the currently selected text. |
| 41 |
* |
| 42 |
* To support having a number of different formats on the clipboard at the same time, the clipboard mechanism |
| 43 |
* allows providing callbacks instead of the actual data. When you set the contents of the clipboard, you can |
| 44 |
* either supply the data directly (via functions like set_text()), or you can supply a callback |
| 45 |
* to be called at a later time when the data is needed (via set().) Providing a callback also avoids having to |
| 46 |
* make copies of the data when it is not needed. |
| 47 |
* |
| 48 |
* Requesting the data from the clipboard is essentially asynchronous. If the contents of the clipboard are |
| 49 |
* provided within the same process, then a direct function call will be made to retrieve the data, but if they |
| 50 |
* are provided by another process, then the data needs to be retrieved from the other process, which may take |
| 51 |
* some time. To avoid blocking the user interface, the call to request the selection, request_contents() takes |
| 52 |
* a callback that will be called when the contents are received (or when the request fails.) If you don't want |
| 53 |
* to deal with providing a separate callback, you can also use wait_for_contents(). This runs the |
| 54 |
* GLib main loop recursively waiting for the contents. This can simplify the code flow, but you still have to |
| 55 |
* be aware that other callbacks in your program can be called while this recursive mainloop is running. |
| 56 |
* |
| 57 |
* Along with the functions to get the clipboard contents as an arbitrary data chunk, there are also functions |
| 58 |
* to retrieve it as text, request_text() and wait_for_text(). These functions take |
| 59 |
* care of determining which formats are advertised by the clipboard provider, asking for the clipboard in the |
| 60 |
* best available format and converting the results into the UTF-8 encoding. |
| 61 |
*/ |
| 62 |
class GTKMM_API Clipboard : public Glib::Object |
| 63 |
{ |
| 64 |
_CLASS_GOBJECT(Clipboard, GtkClipboard, GTK_CLIPBOARD, Glib::Object, GObject, , , GTKMM_API) |
| 65 |
public: |
| 66 |
|
| 67 |
_WRAP_METHOD(static Glib::RefPtr<Clipboard> get(GdkAtom selection = GDK_SELECTION_CLIPBOARD), gtk_clipboard_get, refreturn) |
| 68 |
_WRAP_METHOD(static Glib::RefPtr<Clipboard> get_for_display(const Glib::RefPtr<Gdk::Display>& display, GdkAtom selection = GDK_SELECTION_CLIPBOARD), gtk_clipboard_get_for_display, refreturn) |
| 69 |
|
| 70 |
_WRAP_METHOD(Glib::RefPtr<Gdk::Display> get_display(), gtk_clipboard_get_display, refreturn) |
| 71 |
_WRAP_METHOD(Glib::RefPtr<const Gdk::Display> get_display() const, gtk_clipboard_get_display, refreturn, constversion) |
| 72 |
|
| 73 |
/// For instance: void on_get(Gtk::SelectionData& selection_data, guint info); |
| 74 |
typedef sigc::slot<void, SelectionData&, guint> SlotGet; |
| 75 |
|
| 76 |
/// For instance: void on_clear(); |
| 77 |
typedef sigc::slot<void> SlotClear; |
| 78 |
|
| 79 |
/** Virtually sets the contents of the specified clipboard by providing |
| 80 |
* a list of supported formats for the clipboard data and a function |
| 81 |
* to call to get the actual data when it is requested. |
| 82 |
* |
| 83 |
* @param targets Information about the available forms for the clipboard data. |
| 84 |
* @param slot_get method to call to get the actual clipboard data. |
| 85 |
* @param slot_clear When the clipboard contents are set again, this method will |
| 86 |
* be called, and slot_get will not be subsequently called. |
| 87 |
* |
| 88 |
* @return true if setting the clipboard data succeeded. If setting |
| 89 |
* the clipboard data failed then the provided callback methods |
| 90 |
* will be ignored. |
| 91 |
*/ |
| 92 |
bool set(const std::vector<TargetEntry>& targets, const SlotGet& slot_get, const SlotClear& slot_clear); |
| 93 |
_IGNORE(gtk_clipboard_set_with_owner, gtk_clipboard_set_with_data) |
| 94 |
|
| 95 |
_WRAP_METHOD(Glib::RefPtr<Glib::Object> get_owner(), gtk_clipboard_get_owner, refreturn) |
| 96 |
_WRAP_METHOD(Glib::RefPtr<const Glib::Object> get_owner() const, gtk_clipboard_get_owner, refreturn, constversion) |
| 97 |
|
| 98 |
/** |
| 99 |
* Clears the contents of the clipboard. Generally this should only |
| 100 |
* be called between the time you call set() |
| 101 |
* and when the slot_clear you supplied is called. Otherwise, the |
| 102 |
* clipboard may be owned by someone else. |
| 103 |
*/ |
| 104 |
_WRAP_METHOD(void clear(), gtk_clipboard_clear) |
| 105 |
|
| 106 |
/** |
| 107 |
* Sets the contents of the clipboard to the given UTF-8 string. GTK+ will |
| 108 |
* make a copy of the text and take responsibility for responding |
| 109 |
* for requests for the text, and for converting the text into |
| 110 |
* the requested format. |
| 111 |
* |
| 112 |
* @param text A UTF-8 string. |
| 113 |
* |
| 114 |
*/ |
| 115 |
void set_text(const Glib::ustring& text); |
| 116 |
_IGNORE(gtk_clipboard_set_text) |
| 117 |
|
| 118 |
_WRAP_METHOD(void set_image(const Glib::RefPtr<Gdk::Pixbuf>& pixbuf), gtk_clipboard_set_image) |
| 119 |
|
| 120 |
/// For instance: void on_received(const SelectionData& selection_data); |
| 121 |
typedef sigc::slot<void, const SelectionData&> SlotReceived; |
| 122 |
|
| 123 |
/** Requests the contents of clipboard as the given target. |
| 124 |
* When the results of the result are later received the supplied callback |
| 125 |
* will be called. |
| 126 |
* |
| 127 |
* @param target The form into which the clipboard |
| 128 |
* owner should convert the selection. |
| 129 |
* @param slot A function to call when the results are received |
| 130 |
* (or the retrieval fails). If the retrieval fails |
| 131 |
* the length field of selection_data will be |
| 132 |
* negative. |
| 133 |
**/ |
| 134 |
void request_contents(const Glib::ustring& target, const SlotReceived& slot); |
| 135 |
_IGNORE(gtk_clipboard_request_contents) |
| 136 |
|
| 137 |
/// For instance: void on_text_received(const Glib::ustring& text); |
| 138 |
typedef sigc::slot<void, const Glib::ustring&> SlotTextReceived; |
| 139 |
|
| 140 |
/** Requests the contents of the clipboard as text. When the text is |
| 141 |
* later received, it will be converted to UTF-8 if necessary, and |
| 142 |
* slot will be called. |
| 143 |
* |
| 144 |
* The text parameter to slot will contain the resulting text if |
| 145 |
* the request succeeded, or will be empty if it failed. This could happen for |
| 146 |
* various reasons, in particular if the clipboard was empty or if the |
| 147 |
* contents of the clipboard could not be converted into text form. |
| 148 |
* |
| 149 |
* @param slot: a function to call when the text is received, |
| 150 |
* or the retrieval fails. (It will always be called |
| 151 |
* one way or the other.) |
| 152 |
*/ |
| 153 |
void request_text(const SlotTextReceived& slot); |
| 154 |
_IGNORE(gtk_clipboard_request_text) |
| 155 |
|
| 156 |
/// For instance: void on_rich_text_received(const Glib::ustring& format, const std::string& text); |
| 157 |
typedef sigc::slot<void, const Glib::ustring&, const std::string&> SlotRichTextReceived; |
| 158 |
|
| 159 |
/** Requests the contents of the clipboard as rich text. When the rich text is later received, |
| 160 |
* @a slot will be called. |
| 161 |
* |
| 162 |
* The text parameter to the slot will contain the resulting rich text if the request succeeded, or |
| 163 |
* an empty string if it failed. This function can fail for various reasons, in particular if the |
| 164 |
* clipboard was empty or if the contents of the clipboard could not be converted into rich text form. |
| 165 |
* |
| 166 |
* @param buffer A Gtk::TextBuffer. |
| 167 |
* @param slot A function to call when the text is received, |
| 168 |
* or the retrieval fails. (It will always be called |
| 169 |
* one way or the other.) |
| 170 |
*/ |
| 171 |
void request_rich_text(const Glib::RefPtr<TextBuffer>& buffer, const SlotRichTextReceived& slot); |
| 172 |
_IGNORE(gtk_clipboard_request_rich_text) |
| 173 |
|
| 174 |
|
| 175 |
/// For instance: void on_uris_received(const std::vector<Glib::ustring>& uris); |
| 176 |
typedef sigc::slot<void, const std::vector<Glib::ustring>&> SlotUrisReceived; |
| 177 |
|
| 178 |
/** Requests the contents of the clipboard as URIs. When the URIs are |
| 179 |
* later received @a slot will be called. |
| 180 |
* |
| 181 |
* The uris parameter to @a slot will contain the resulting array of |
| 182 |
* URIs if the request succeeded, or an empty list if it failed. This could happen |
| 183 |
* for various reasons, in particular if the clipboard was empty or if the |
| 184 |
* contents of the clipboard could not be converted into URI form. |
| 185 |
* |
| 186 |
* @param slot A slot to call when the URIs are received, or the retrieval fails. (It will always be called one way or the other.) |
| 187 |
* |
| 188 |
* @newin{2,14} |
| 189 |
*/ |
| 190 |
void request_uris(const SlotUrisReceived& slot); |
| 191 |
_IGNORE(gtk_clipboard_request_uris) |
| 192 |
|
| 193 |
|
| 194 |
/// For instance: void on_image_received(const Glib::RefPtr<Gdk::Pixbuf>& text); |
| 195 |
typedef sigc::slot<void, const Glib::RefPtr<Gdk::Pixbuf>&> SlotImageReceived; |
| 196 |
|
| 197 |
/** Requests the contents of the clipboard as image. When the image is |
| 198 |
* later received, it will be converted to a Gdk::Pixbuf. |
| 199 |
* This function waits for |
| 200 |
* the data to be received using the main loop, so events, |
| 201 |
* timeouts, etc, may be dispatched during the wait. |
| 202 |
* |
| 203 |
* The pixbuf parameter to slot will contain the resulting pixbuf if |
| 204 |
* the request succeeded, or will be empty if it failed. This could happen for |
| 205 |
* various reasons, in particular if the clipboard was empty or if the |
| 206 |
* contents of the clipboard could not be converted into image form. |
| 207 |
* |
| 208 |
* @param slot: a function to call when the text is received, |
| 209 |
* or the retrieval fails. (It will always be called |
| 210 |
* one way or the other.) |
| 211 |
*/ |
| 212 |
void request_image(const SlotImageReceived& slot); |
| 213 |
_IGNORE(gtk_clipboard_request_image) |
| 214 |
|
| 215 |
/// For instance: void on_targets_received(const std::vector<Glib::ustring>& targets); |
| 216 |
typedef sigc::slot<void, const std::vector<Glib::ustring>&> SlotTargetsReceived; |
| 217 |
|
| 218 |
/** Requests the contents of the clipboard as list of supported targets. |
| 219 |
* When the list is later received, callback will be called. |
| 220 |
* |
| 221 |
* The targets parameter to slot will contain the resulting targets if |
| 222 |
* the request succeeded. |
| 223 |
* |
| 224 |
* @param slot a function to call when the targets are received, |
| 225 |
* or the retrieval fails. (It will always be called |
| 226 |
* one way or the other.) |
| 227 |
* |
| 228 |
* @newin{2,4} |
| 229 |
*/ |
| 230 |
void request_targets(const SlotTargetsReceived& slot); |
| 231 |
_IGNORE(gtk_clipboard_request_targets) |
| 232 |
|
| 233 |
/** |
| 234 |
* Requests the contents of the clipboard using the given target. |
| 235 |
* This function waits for the data to be received using the main |
| 236 |
* loop, so events, timeouts, etc, may be dispatched during the wait. |
| 237 |
* |
| 238 |
* @param target The form into which the clipboard owner should convert the selection. |
| 239 |
* |
| 240 |
* @return A SelectionData object, which will be invalid if retrieving the given target failed. |
| 241 |
*/ |
| 242 |
SelectionData wait_for_contents(const Glib::ustring& target) const; |
| 243 |
_IGNORE(gtk_clipboard_wait_for_contents) |
| 244 |
|
| 245 |
_WRAP_METHOD(Glib::ustring wait_for_text() const, gtk_clipboard_wait_for_text) |
| 246 |
|
| 247 |
std::string wait_for_rich_text(const Glib::RefPtr<TextBuffer>& buffer, std::string& format); |
| 248 |
_IGNORE(gtk_clipboard_wait_for_rich_text) |
| 249 |
|
| 250 |
//Maybe the result should be const, but constness is not so clear-cut here. murrayc |
| 251 |
_WRAP_METHOD(Glib::RefPtr<Gdk::Pixbuf> wait_for_image() const, gtk_clipboard_wait_for_image) |
| 252 |
|
| 253 |
_WRAP_METHOD(bool wait_is_text_available() const, gtk_clipboard_wait_is_text_available) |
| 254 |
_WRAP_METHOD(bool wait_is_rich_text_available(const Glib::RefPtr<TextBuffer>& buffer) const, gtk_clipboard_wait_is_rich_text_available) |
| 255 |
_WRAP_METHOD(bool wait_is_image_available() const, gtk_clipboard_wait_is_image_available) |
| 256 |
_WRAP_METHOD(bool wait_is_uris_available() const, gtk_clipboard_wait_is_uris_available) |
| 257 |
_WRAP_METHOD(bool wait_is_target_available(const Glib::ustring& target), gtk_clipboard_wait_is_target_available) |
| 258 |
|
| 259 |
/** Returns a list of targets that are present on the clipboard. |
| 260 |
* This function waits for the data to be received using the main |
| 261 |
* loop, so events, timeouts, etc, may be dispatched during the wait. |
| 262 |
* |
| 263 |
* @result targets: The targets. |
| 264 |
* |
| 265 |
* @newin{2,4} |
| 266 |
*/ |
| 267 |
std::vector<Glib::ustring> wait_for_targets() const; |
| 268 |
_IGNORE(gtk_clipboard_wait_for_targets) |
| 269 |
|
| 270 |
#m4 _CONVERSION(`gchar**',`std::vector<Glib::ustring>',`Glib::ArrayHandler<Glib::ustring>::array_to_vector($3, Glib::OWNERSHIP_DEEP)') |
| 271 |
/** Requests the contents of the clipboard as URIs. This function waits |
| 272 |
* for the data to be received using the main loop, so events, |
| 273 |
* timeouts, etc, may be dispatched during the wait. |
| 274 |
* |
| 275 |
* @newin{2,14} |
| 276 |
* |
| 277 |
* @return A vector of strings, which is empty if retrieving the |
| 278 |
* selection data failed. (This could happen for various reasons, |
| 279 |
* in particular if the clipboard was empty or if the contents of |
| 280 |
* the clipboard could not be converted into URI form.). |
| 281 |
*/ |
| 282 |
_WRAP_METHOD(std::vector<Glib::ustring> wait_for_uris() const, gtk_clipboard_wait_for_uris) |
| 283 |
|
| 284 |
/** Hints that the clipboard data should be stored somewhere when the application exits or when store() |
| 285 |
* is called. |
| 286 |
* |
| 287 |
* This value is reset when the clipboard owner changes. Where the clipboard data is stored is platform |
| 288 |
* dependent, see Gdk::Display::store_clipboard() for more information. |
| 289 |
* |
| 290 |
* @param targets Array containing information about which forms should be stored. |
| 291 |
*/ |
| 292 |
void set_can_store(const std::vector<TargetEntry>& targets); |
| 293 |
|
| 294 |
/** Hints that all forms of clipboard data should be stored somewhere when the application exits or when store() |
| 295 |
* is called. |
| 296 |
* |
| 297 |
* This value is reset when the clipboard owner changes. Where the clipboard data is stored is platform |
| 298 |
* dependent, see Gdk::Display::store_clipboard() for more information. |
| 299 |
*/ |
| 300 |
void set_can_store(); |
| 301 |
_IGNORE(gtk_clipboard_set_can_store) |
| 302 |
|
| 303 |
_WRAP_METHOD(void store(), gtk_clipboard_store) |
| 304 |
_WRAP_METHOD(GdkAtom get_selection() const, gtk_clipboard_get_selection) |
| 305 |
|
| 306 |
_WRAP_SIGNAL(void owner_change(GdkEventOwnerChange* event), "owner_change", no_default_handler) |
| 307 |
|
| 308 |
}; |
| 309 |
|
| 310 |
} //namespace Gtk |