// -*- c++ -*-
/* $Id: selectiondata.ccg,v 1.5 2006/07/05 16:59:28 murrayc Exp $ */

/* Copyright 2002 The gtkmm Development Team
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include <glibmm/vectorutils.h>

#include <gtkmm/textbuffer.h>
#include <gdkmm/pixbuf.h>
#include <glibmm/utility.h> //For ScopedPtr<>.
#include <gtk/gtk.h>



namespace Gtk
{

void SelectionData::set(const std::string& type, int format, const guint8* data, int length)
{
  gtk_selection_data_set(gobj(), gdk_atom_intern(type.c_str(), 0), format, data, length);
}

void SelectionData::set(int format, const guint8* data, int length)
{
  //The C examples do this - that's why I added this method overload. murrayc.
  set(get_target(), format, data, length);
}

void SelectionData::set(const std::string& type, const std::string& data)
{
  gtk_selection_data_set(gobj(), gdk_atom_intern(type.c_str(), 0),
                         sizeof(char) * 8, // format is 8 bits per unit
                         reinterpret_cast<const guint8*>(data.data()), data.size());
}

bool SelectionData::set_text(const Glib::ustring& data)
{
  return gtk_selection_data_set_text(gobj(), data.data(), data.bytes());
}

Glib::ustring SelectionData::get_text() const
{
  return Glib::convert_return_gchar_ptr_to_ustring(
      reinterpret_cast<char*>(gtk_selection_data_get_text(const_cast<GtkSelectionData*>(gobj()))));
}

std::string SelectionData::get_data_as_string() const
{
  const guchar* data = get_data();
  const int length = get_length();
  if(data && length > 0)
    return std::string(reinterpret_cast<const char*>(data), length);
  else
    return std::string();
}

std::string SelectionData::get_target() const
{
  return Glib::convert_return_gchar_ptr_to_stdstring(
      gdk_atom_name( gtk_selection_data_get_target(const_cast<GtkSelectionData*>(gobj())) ) );
}

std::vector<std::string> SelectionData::get_targets() const
{
  GdkAtom* targets = 0;
  int n_targets = 0;

  if(!gtk_selection_data_get_targets(const_cast<GtkSelectionData*>(gobj()), &targets, &n_targets))
    n_targets = 0; // it's set to -1 otherwise

  //Note that we free the GdkAtom* array, but we don't need to free its items:
  return Glib::ArrayHandler<std::string, Gdk::AtomStringTraits>::array_to_vector(targets, n_targets, Glib::OWNERSHIP_SHALLOW);
}


std::string SelectionData::get_data_type() const
{
  return Glib::convert_return_gchar_ptr_to_stdstring(
    gdk_atom_name( gtk_selection_data_get_data_type(const_cast<GtkSelectionData*>(gobj())) ) );
}


} // namespace Gtk
