| 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 <glibmm/vectorutils.h> |
| 18 |
|
| 19 |
#include <gdkmm/types.h> |
| 20 |
|
| 21 |
#include <gtkmm/textbuffer.h> |
| 22 |
#include <gtkmm/selectiondata_private.h> |
| 23 |
#include <glibmm/utility.h> //For make_unique_ptr_gfree<>. |
| 24 |
#include <gtk/gtk.h> |
| 25 |
|
| 26 |
namespace |
| 27 |
{ |
| 28 |
|
| 29 |
// SignalProxy_GetClear: |
| 30 |
// This Signal Proxy allows the C++ coder to specify a sigc::slot instead of a static function. |
| 31 |
class SignalProxy_GetClear |
| 32 |
{ |
| 33 |
public: |
| 34 |
SignalProxy_GetClear(const Gtk::Clipboard::SlotGet& slot_get, |
| 35 |
const Gtk::Clipboard::SlotClear& slot_clear); |
| 36 |
|
| 37 |
static void gtk_callback_get(GtkClipboard* clipboard, GtkSelectionData* selection_data, |
| 38 |
unsigned int info, void* data); |
| 39 |
static void gtk_callback_clear(GtkClipboard* clipboard, void* data); |
| 40 |
|
| 41 |
Gtk::Clipboard::SlotGet slot_get_; |
| 42 |
Gtk::Clipboard::SlotClear slot_clear_; |
| 43 |
}; |
| 44 |
|
| 45 |
SignalProxy_GetClear::SignalProxy_GetClear(const Gtk::Clipboard::SlotGet& slot_get, |
| 46 |
const Gtk::Clipboard::SlotClear& slot_clear) |
| 47 |
: |
| 48 |
slot_get_ (slot_get), |
| 49 |
slot_clear_ (slot_clear) |
| 50 |
{} |
| 51 |
|
| 52 |
static void SignalProxy_GetClear_gtk_callback_get(GtkClipboard*, GtkSelectionData* selection_data, |
| 53 |
unsigned int info, void* data) |
| 54 |
{ |
| 55 |
const auto self = static_cast<SignalProxy_GetClear*>(data); |
| 56 |
|
| 57 |
try |
| 58 |
{ |
| 59 |
Gtk::SelectionData_WithoutOwnership cppSelectionData(selection_data); |
| 60 |
(self->slot_get_)(cppSelectionData, info); |
| 61 |
} |
| 62 |
catch(...) |
| 63 |
{ |
| 64 |
Glib::exception_handlers_invoke(); |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
static void SignalProxy_GetClear_gtk_callback_clear(GtkClipboard*, void* data) |
| 69 |
{ |
| 70 |
const auto self = static_cast<SignalProxy_GetClear*>(data); |
| 71 |
|
| 72 |
try |
| 73 |
{ |
| 74 |
(self->slot_clear_)(); |
| 75 |
} |
| 76 |
catch(...) |
| 77 |
{ |
| 78 |
Glib::exception_handlers_invoke(); |
| 79 |
} |
| 80 |
|
| 81 |
delete self; // After this callback has been called, none of the 2 callbacks will be called again. |
| 82 |
|
| 83 |
//This might leak the last SignalProxy_GetClear(), but only the last one, |
| 84 |
//because clear() is called when set() is called again. |
| 85 |
} |
| 86 |
|
| 87 |
} //anonymous namespace |
| 88 |
|
| 89 |
|
| 90 |
static void SignalProxy_Received_gtk_callback(GtkClipboard*, GtkSelectionData* selection_data, void* data) |
| 91 |
{ |
| 92 |
auto the_slot = static_cast<Gtk::Clipboard::SlotReceived*>(data); |
| 93 |
|
| 94 |
try |
| 95 |
{ |
| 96 |
Gtk::SelectionData cppSelectionData(selection_data, true /*take_copy=true*/); |
| 97 |
(*the_slot)(cppSelectionData); |
| 98 |
} |
| 99 |
catch(...) |
| 100 |
{ |
| 101 |
Glib::exception_handlers_invoke(); |
| 102 |
} |
| 103 |
|
| 104 |
delete the_slot; |
| 105 |
} |
| 106 |
|
| 107 |
static void SignalProxy_TargetsReceived_gtk_callback(GtkClipboard*, GdkAtom* atoms, |
| 108 |
gint n_atoms, gpointer data) |
| 109 |
{ |
| 110 |
Gtk::Clipboard::SlotTargetsReceived *const |
| 111 |
slot = static_cast<Gtk::Clipboard::SlotTargetsReceived*>(data); |
| 112 |
|
| 113 |
try |
| 114 |
{ |
| 115 |
// TODO: check if the ownership is good. |
| 116 |
(*slot) (Glib::ArrayHandler<Glib::ustring, Gdk::AtomUstringTraits>::array_to_vector(atoms, n_atoms > 0 ? n_atoms : 0, Glib::OWNERSHIP_NONE)); |
| 117 |
} |
| 118 |
catch (...) |
| 119 |
{ |
| 120 |
Glib::exception_handlers_invoke(); |
| 121 |
} |
| 122 |
delete slot; // the callback is only used once |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
static void SignalProxy_TextReceived_gtk_callback(GtkClipboard*, const char* text, void* data) |
| 127 |
{ |
| 128 |
auto the_slot = static_cast<Gtk::Clipboard::SlotTextReceived*>(data); |
| 129 |
|
| 130 |
try |
| 131 |
{ |
| 132 |
(*the_slot)((text) ? Glib::ustring(text) : Glib::ustring()); |
| 133 |
} |
| 134 |
catch(...) |
| 135 |
{ |
| 136 |
Glib::exception_handlers_invoke(); |
| 137 |
} |
| 138 |
|
| 139 |
delete the_slot; // the callback is only used once |
| 140 |
} |
| 141 |
|
| 142 |
static void SignalProxy_RichTextReceived_gtk_callback(GtkClipboard*, GdkAtom format, |
| 143 |
const guint8* text, gsize length, void* data) |
| 144 |
{ |
| 145 |
Gtk::Clipboard::SlotRichTextReceived *const |
| 146 |
slot = static_cast<Gtk::Clipboard::SlotRichTextReceived*>(data); |
| 147 |
|
| 148 |
try |
| 149 |
{ |
| 150 |
(*slot)(Glib::convert_return_gchar_ptr_to_ustring(gdk_atom_name(format)), |
| 151 |
(text) ? std::string(reinterpret_cast<const char*>(text), length) : std::string()); |
| 152 |
} |
| 153 |
catch(...) |
| 154 |
{ |
| 155 |
Glib::exception_handlers_invoke(); |
| 156 |
} |
| 157 |
delete slot; // the callback is only used once |
| 158 |
} |
| 159 |
|
| 160 |
static void SignalProxy_UrisReceived_gtk_callback(GtkClipboard*, gchar** uris, void* data) |
| 161 |
{ |
| 162 |
auto the_slot = static_cast<Gtk::Clipboard::SlotUrisReceived*>(data); |
| 163 |
|
| 164 |
try |
| 165 |
{ |
| 166 |
// TODO: check if the ownetship is good. krnowak. |
| 167 |
//Handle: Does this take ownership? It should probalby copy. murrayc. |
| 168 |
(*the_slot)( Glib::ArrayHandler<Glib::ustring>::array_to_vector(uris, Glib::OWNERSHIP_NONE) ); |
| 169 |
} |
| 170 |
catch(...) |
| 171 |
{ |
| 172 |
Glib::exception_handlers_invoke(); |
| 173 |
} |
| 174 |
|
| 175 |
delete the_slot; // the callback is only used once |
| 176 |
} |
| 177 |
|
| 178 |
static void SignalProxy_ImageReceived_gtk_callback(GtkClipboard*, GdkPixbuf* image, void* data) |
| 179 |
{ |
| 180 |
auto the_slot = static_cast<Gtk::Clipboard::SlotImageReceived*>(data); |
| 181 |
|
| 182 |
try |
| 183 |
{ |
| 184 |
(*the_slot)(Glib::wrap(image, true /* take_ref */)); |
| 185 |
} |
| 186 |
catch(...) |
| 187 |
{ |
| 188 |
Glib::exception_handlers_invoke(); |
| 189 |
} |
| 190 |
|
| 191 |
delete the_slot; // the callback is only used once |
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
namespace Gtk |
| 196 |
{ |
| 197 |
|
| 198 |
bool Clipboard::set(const std::vector<TargetEntry>& targets, |
| 199 |
const SlotGet& slot_get, const SlotClear& slot_clear) |
| 200 |
{ |
| 201 |
// Create a signal proxy. A pointer to this will be passed through the callback's data parameter. |
| 202 |
const auto pSignalProxy = new SignalProxy_GetClear(slot_get, slot_clear); |
| 203 |
|
| 204 |
return gtk_clipboard_set_with_data( |
| 205 |
gobj(), |
| 206 |
Glib::ArrayHandler<TargetEntry, TargetEntryTraits>::vector_to_array(targets).data (), |
| 207 |
targets.size(), |
| 208 |
&SignalProxy_GetClear_gtk_callback_get, |
| 209 |
&SignalProxy_GetClear_gtk_callback_clear, |
| 210 |
pSignalProxy); |
| 211 |
} |
| 212 |
|
| 213 |
void Clipboard::set_text(const Glib::ustring& text) |
| 214 |
{ |
| 215 |
gtk_clipboard_set_text(gobj(), text.c_str(), text.bytes()); |
| 216 |
} |
| 217 |
|
| 218 |
|
| 219 |
void Clipboard::request_contents(const Glib::ustring& target, const SlotReceived& slot) |
| 220 |
{ |
| 221 |
// Create a copy of the slot. A pointer to this will be passed through the callback's data parameter. |
| 222 |
auto slot_copy = new SlotReceived(slot); |
| 223 |
|
| 224 |
gtk_clipboard_request_contents(gobj(), gdk_atom_intern(target.c_str(), FALSE), |
| 225 |
&SignalProxy_Received_gtk_callback, slot_copy); |
| 226 |
} |
| 227 |
|
| 228 |
void Clipboard::request_text(const SlotTextReceived& slot) |
| 229 |
{ |
| 230 |
// Create a copy of the slot. A pointer to this will be passed through the callback's data parameter. |
| 231 |
auto slot_copy = new SlotTextReceived(slot); |
| 232 |
|
| 233 |
gtk_clipboard_request_text(gobj(), |
| 234 |
&SignalProxy_TextReceived_gtk_callback, slot_copy); |
| 235 |
} |
| 236 |
|
| 237 |
void Clipboard::request_rich_text(const Glib::RefPtr<TextBuffer>& buffer, const SlotRichTextReceived& slot) |
| 238 |
{ |
| 239 |
// Create a copy of the slot. A pointer to this will be passed through the callback's data parameter. |
| 240 |
auto slot_copy = new SlotRichTextReceived(slot); |
| 241 |
|
| 242 |
gtk_clipboard_request_rich_text(gobj(), buffer->gobj(), |
| 243 |
&SignalProxy_RichTextReceived_gtk_callback, slot_copy); |
| 244 |
} |
| 245 |
|
| 246 |
void Clipboard::request_uris(const SlotUrisReceived& slot) |
| 247 |
{ |
| 248 |
// Create a copy of the slot. A pointer to this will be passed through the callback's data parameter. |
| 249 |
auto slot_copy = new SlotUrisReceived(slot); |
| 250 |
|
| 251 |
gtk_clipboard_request_uris(gobj(), &SignalProxy_UrisReceived_gtk_callback, slot_copy); |
| 252 |
} |
| 253 |
|
| 254 |
void Clipboard::request_image(const SlotImageReceived& slot) |
| 255 |
{ |
| 256 |
// Create a copy of the slot. A pointer to this will be passed through the callback's data parameter. |
| 257 |
auto slot_copy = new SlotImageReceived(slot); |
| 258 |
|
| 259 |
gtk_clipboard_request_image(gobj(), |
| 260 |
&SignalProxy_ImageReceived_gtk_callback, slot_copy); |
| 261 |
} |
| 262 |
|
| 263 |
void Clipboard::request_targets(const SlotTargetsReceived& slot) |
| 264 |
{ |
| 265 |
// Create a copy of the slot. A pointer to this will be passed through the callback's data parameter. |
| 266 |
auto slot_copy = new SlotTargetsReceived(slot); |
| 267 |
|
| 268 |
gtk_clipboard_request_targets(gobj(), &SignalProxy_TargetsReceived_gtk_callback, slot_copy); |
| 269 |
} |
| 270 |
|
| 271 |
SelectionData Clipboard::wait_for_contents(const Glib::ustring& target) const |
| 272 |
{ |
| 273 |
//gtk_clipboard_wait_for_contents returns a newly-allocated GtkSelectionData, or NULL. |
| 274 |
auto cData = gtk_clipboard_wait_for_contents( const_cast<GtkClipboard*>(gobj()), gdk_atom_intern(target.c_str(), FALSE) ); |
| 275 |
return SelectionData(cData, false /* take_copy */); |
| 276 |
} |
| 277 |
|
| 278 |
std::vector<Glib::ustring> Clipboard::wait_for_targets() const |
| 279 |
{ |
| 280 |
GdkAtom* atoms (nullptr); |
| 281 |
int n_targets (0); |
| 282 |
|
| 283 |
if (!gtk_clipboard_wait_for_targets(const_cast<GtkClipboard*>(gobj()), &atoms, &n_targets)) |
| 284 |
{ |
| 285 |
n_targets = 0; |
| 286 |
} |
| 287 |
|
| 288 |
return Glib::ArrayHandler<Glib::ustring, Gdk::AtomUstringTraits>::array_to_vector(atoms, n_targets, Glib::OWNERSHIP_SHALLOW); |
| 289 |
} |
| 290 |
|
| 291 |
void Clipboard::set_can_store(const std::vector<TargetEntry>& targets) |
| 292 |
{ |
| 293 |
gtk_clipboard_set_can_store( gobj(), Glib::ArrayHandler<TargetEntry, TargetEntryTraits>::vector_to_array(targets).data (), targets.size() ); |
| 294 |
} |
| 295 |
|
| 296 |
void Clipboard::set_can_store() |
| 297 |
{ |
| 298 |
gtk_clipboard_set_can_store( gobj(), nullptr, 0 /* See C docs */ ); |
| 299 |
} |
| 300 |
|
| 301 |
std::string Clipboard::wait_for_rich_text(const Glib::RefPtr<TextBuffer>& buffer, std::string& format) |
| 302 |
{ |
| 303 |
std::string result; |
| 304 |
|
| 305 |
GdkAtom format_atom = GDK_NONE; |
| 306 |
gsize length = 0; |
| 307 |
guint8* text = gtk_clipboard_wait_for_rich_text(const_cast<GtkClipboard*>(gobj()), buffer->gobj(), &format_atom, &length); |
| 308 |
if(text && length) |
| 309 |
{ |
| 310 |
gchar* format_atom_name = gdk_atom_name(format_atom); |
| 311 |
if(format_atom_name) |
| 312 |
format = Glib::make_unique_ptr_gfree(format_atom_name).get(); //This frees the buffer. |
| 313 |
|
| 314 |
result = std::string((char*)text, length); |
| 315 |
g_free(text); |
| 316 |
} |
| 317 |
|
| 318 |
return result; |
| 319 |
} |
| 320 |
|
| 321 |
} //namespace Gtk |