// -*- c++ -*-
/* $Id: display.ccg,v 1.5 2005/01/05 17:30:16 murrayc Exp $ */

/*
 *
 * Copyright 1998-2002 The gtkmm Development Team
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the Free
 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <gdkmm/window.h>
#include <gdk/gdkdisplay.h>

namespace Gdk
{

bool Display::set_selection_owner(const Glib::RefPtr<Window>& owner, Glib::ustring& selection, guint32 time_, bool send_event)
{
  return gdk_selection_owner_set_for_display(gobj(), owner->gobj(), Gdk::AtomString::to_c_type(selection), time_, (gboolean)send_event);
}

Glib::RefPtr<Window> Display::get_selection_owner(const Glib::ustring& selection)
{
  return Glib::wrap( (GdkWindowObject*)gdk_selection_owner_get_for_display(gobj(), Gdk::AtomString::to_c_type(selection)) , true);
}

void Display::selection_send_notify(guint32 requestor, Glib::ustring& selection, Glib::ustring& target, Glib::ustring& property, guint32 time_)
{
  gdk_selection_send_notify_for_display(gobj(), requestor, Gdk::AtomString::to_c_type(selection), Gdk::AtomString::to_c_type(target), Gdk::AtomString::to_c_type(property), time_);
}

void Display::get_pointer(Glib::RefPtr<Screen>& screen, int& x, int& y, ModifierType& mask)
{
  GdkScreen* cScreen = 0;
  GdkModifierType cMask = (GdkModifierType)mask;
  gdk_display_get_pointer(gobj(), &cScreen, &x, &y, &cMask);
  screen = Glib::wrap(cScreen);
  mask = (ModifierType)cMask;
}

void Display::get_pointer(int& x, int& y, ModifierType& mask)
{
  GdkModifierType cMask = (GdkModifierType)mask;
  gdk_display_get_pointer(gobj(), 0, &x, &y, &cMask);
  mask = (ModifierType)cMask;
}


Glib::RefPtr<Window> Display::get_window_at_pointer()
{

  Glib::RefPtr<Window> retvalue = Glib::wrap((GdkWindowObject*)(gdk_display_get_window_at_pointer(gobj(), 0, 0)));

  if(retvalue)
    retvalue->reference(); //The function does not do a ref for us.
  return retvalue;
}

Glib::RefPtr<const Window> Display::get_window_at_pointer() const
{

  Glib::RefPtr<const Window> retvalue = Glib::wrap((GdkWindowObject*)(gdk_display_get_window_at_pointer(const_cast<GdkDisplay*>(gobj()), 0, 0)));

  if(retvalue)
    retvalue->reference(); //The function does not do a ref for us.
  return retvalue;
}

GdkDisplayPointerHooks*  Display::unset_pointer_hooks()
{
  return gdk_display_set_pointer_hooks(gobj(), 0 /* See GDK docs */);  
}

 void Display::store_clipboard(const Glib::RefPtr<Gdk::Window>& clipboard_window, guint32 time_)
 {
   gdk_display_store_clipboard(gobj(), clipboard_window->gobj(), time_, 0 /* see the C docs */, 0);
 }
  
 void Display::store_clipboard(const Glib::RefPtr<Gdk::Window>& clipboard_window, guint32 time_, const Glib::StringArrayHandle& targets)
 {
   //Put it into a real container that we can use:
   std::vector<Glib::ustring> targets_copy = targets;
   
   //Create array of target GdkAtoms from target strings:
   if(!targets_copy.empty())
   {
     GdkAtom* pAtoms = new GdkAtom[targets_copy.size()];
     
     for(guint i = 0; i < targets_copy.size(); ++i)
     {
       *pAtoms = Gdk::AtomString::to_c_type(targets_copy[i]);
     }
 
     gdk_display_store_clipboard(gobj(), clipboard_window->gobj(), time_, 0 /* see the C docs */, 0);
     
     delete[] pAtoms;
   }
 }

} //Gdk


