~ubuntu-branches/ubuntu/precise/gtkmm3.0/precise

« back to all changes in this revision

Viewing changes to gtk/src/selectiondata.ccg

  • Committer: Bazaar Package Importer
  • Author(s): Michael Biebl
  • Date: 2011-06-17 00:12:44 UTC
  • Revision ID: james.westby@ubuntu.com-20110617001244-9hl5an15hiaaahi6
Tags: upstream-3.0.1
ImportĀ upstreamĀ versionĀ 3.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// -*- c++ -*-
 
2
/* $Id: selectiondata.ccg,v 1.5 2006/07/05 16:59:28 murrayc Exp $ */
 
3
 
 
4
/* Copyright 2002 The gtkmm Development Team
 
5
 *
 
6
 * This library is free software; you can redistribute it and/or
 
7
 * modify it under the terms of the GNU Lesser General Public
 
8
 * License as published by the Free Software Foundation; either
 
9
 * version 2.1 of the License, or (at your option) any later version.
 
10
 *
 
11
 * This library is distributed in the hope that it will be useful,
 
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
14
 * Lesser General Public License for more details.
 
15
 *
 
16
 * You should have received a copy of the GNU Lesser General Public
 
17
 * License along with this library; if not, write to the Free
 
18
 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
19
 */
 
20
 
 
21
#include <glibmm/vectorutils.h>
 
22
 
 
23
#include <gtkmm/textbuffer.h>
 
24
#include <gdkmm/pixbuf.h>
 
25
#include <glibmm/utility.h> //For ScopedPtr<>.
 
26
#include <gtk/gtk.h>
 
27
 
 
28
 
 
29
 
 
30
namespace Gtk
 
31
{
 
32
 
 
33
void SelectionData::set(const std::string& type, int format, const guint8* data, int length)
 
34
{
 
35
  gtk_selection_data_set(gobj(), gdk_atom_intern(type.c_str(), 0), format, data, length);
 
36
}
 
37
 
 
38
void SelectionData::set(int format, const guint8* data, int length)
 
39
{
 
40
  //The C examples do this - that's why I added this method overload. murrayc.
 
41
  set(get_target(), format, data, length);
 
42
}
 
43
 
 
44
void SelectionData::set(const std::string& type, const std::string& data)
 
45
{
 
46
  gtk_selection_data_set(gobj(), gdk_atom_intern(type.c_str(), 0),
 
47
                         sizeof(char) * 8, // format is 8 bits per unit
 
48
                         reinterpret_cast<const guint8*>(data.data()), data.size());
 
49
}
 
50
 
 
51
bool SelectionData::set_text(const Glib::ustring& data)
 
52
{
 
53
  return gtk_selection_data_set_text(gobj(), data.data(), data.bytes());
 
54
}
 
55
 
 
56
Glib::ustring SelectionData::get_text() const
 
57
{
 
58
  return Glib::convert_return_gchar_ptr_to_ustring(
 
59
      reinterpret_cast<char*>(gtk_selection_data_get_text(const_cast<GtkSelectionData*>(gobj()))));
 
60
}
 
61
 
 
62
std::string SelectionData::get_data_as_string() const
 
63
{
 
64
  const guchar* data = get_data();
 
65
  const int length = get_length();
 
66
  if(data && length > 0)
 
67
    return std::string(reinterpret_cast<const char*>(data), length);
 
68
  else
 
69
    return std::string();
 
70
}
 
71
 
 
72
std::string SelectionData::get_target() const
 
73
{
 
74
  return Glib::convert_return_gchar_ptr_to_stdstring(
 
75
      gdk_atom_name( gtk_selection_data_get_target(const_cast<GtkSelectionData*>(gobj())) ) );
 
76
}
 
77
 
 
78
std::vector<std::string> SelectionData::get_targets() const
 
79
{
 
80
  GdkAtom* targets = 0;
 
81
  int n_targets = 0;
 
82
 
 
83
  if(!gtk_selection_data_get_targets(const_cast<GtkSelectionData*>(gobj()), &targets, &n_targets))
 
84
    n_targets = 0; // it's set to -1 otherwise
 
85
 
 
86
  //Note that we free the GdkAtom* array, but we don't need to free its items:
 
87
  return Glib::ArrayHandler<std::string, Gdk::AtomStringTraits>::array_to_vector(targets, n_targets, Glib::OWNERSHIP_SHALLOW);
 
88
}
 
89
 
 
90
 
 
91
std::string SelectionData::get_data_type() const
 
92
{
 
93
  return Glib::convert_return_gchar_ptr_to_stdstring(
 
94
    gdk_atom_name( gtk_selection_data_get_data_type(const_cast<GtkSelectionData*>(gobj())) ) );
 
95
}
 
96
 
 
97
 
 
98
} // namespace Gtk