Debian Codegraph @

gtk/src/selectiondata.ccg

raw
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 <gtkmm/textbuffer.h>
20 #include <gdkmm/pixbuf.h>
21 #include <glibmm/utility.h> //For make_unique_ptr_gfree<>.
22 #include <gtk/gtk.h>
23
24
25 namespace Gtk
26 {
27
28 void SelectionData::set(const std::string& type, int format, const guint8* data, int length)
29 {
30 gtk_selection_data_set(gobj(), gdk_atom_intern(type.c_str(), 0), format, data, length);
31 }
32
33 void SelectionData::set(int format, const guint8* data, int length)
34 {
35 //The C examples do this - that's why I added this method overload. murrayc.
36 set(get_target(), format, data, length);
37 }
38
39 void SelectionData::set(const std::string& type, const std::string& data)
40 {
41 gtk_selection_data_set(gobj(), gdk_atom_intern(type.c_str(), 0),
42 sizeof(char) * 8, // format is 8 bits per unit
43 reinterpret_cast<const guint8*>(data.data()), data.size());
44 }
45
46 bool SelectionData::set_text(const Glib::ustring& data)
47 {
48 return gtk_selection_data_set_text(gobj(), data.data(), data.bytes());
49 }
50
51 Glib::ustring SelectionData::get_text() const
52 {
53 return Glib::convert_return_gchar_ptr_to_ustring(
54 reinterpret_cast<char*>(gtk_selection_data_get_text(const_cast<GtkSelectionData*>(gobj()))));
55 }
56
57 std::string SelectionData::get_data_as_string() const
58 {
59 const guchar* data = get_data();
60 const int length = get_length();
61 if(data && length > 0)
62 return std::string(reinterpret_cast<const char*>(data), length);
63 else
64 return std::string();
65 }
66
67 std::string SelectionData::get_target() const
68 {
69 return Glib::convert_return_gchar_ptr_to_stdstring(
70 gdk_atom_name( gtk_selection_data_get_target(const_cast<GtkSelectionData*>(gobj())) ) );
71 }
72
73 std::vector<std::string> SelectionData::get_targets() const
74 {
75 GdkAtom* targets = nullptr;
76 int n_targets = 0;
77
78 if(!gtk_selection_data_get_targets(const_cast<GtkSelectionData*>(gobj()), &targets, &n_targets))
79 n_targets = 0; // it's set to -1 otherwise
80
81 //Note that we free the GdkAtom* array, but we don't need to free its items:
82 return Glib::ArrayHandler<std::string, Gdk::AtomStringTraits>::array_to_vector(targets, n_targets, Glib::OWNERSHIP_SHALLOW);
83 }
84
85 std::string SelectionData::get_data_type() const
86 {
87 return Glib::convert_return_gchar_ptr_to_stdstring(
88 gdk_atom_name( gtk_selection_data_get_data_type(const_cast<GtkSelectionData*>(gobj())) ) );
89 }
90
91 } // namespace Gtk