1 //$Id: dndwindow.cc 5 2003-10-10 04:31:35Z luca $ -*- c++ -*-
3 /* gtkmm example Copyright (C) 2002 gtkmm development team
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2
7 * as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #include "dndwindow.h"
22 Glib::RefPtr<Gdk::Pixbuf> ico_canio, ico_y, ico_codo;
24 DnDWindow::DnDWindow()
25 : m_Button_Canio("Drag Canio\n"),
26 m_Button_Y("Drop Y\n"),
27 m_Button_Codo("Drag Codo\n")
31 ico_canio = Gdk::Pixbuf::create_from_file("canio.png");
32 ico_y = Gdk::Pixbuf::create_from_file("y.png");
33 ico_codo = Gdk::Pixbuf::create_from_file("codo.png");
37 m_HBox.pack_start(m_VBox);
38 m_HBox.pack_start(m_WorkPlace);
39 m_WorkPlace.set_size_request(300, 300);
42 std::list<Gtk::TargetEntry> listTargets;
43 listTargets.push_back( Gtk::TargetEntry("STRING") );
44 listTargets.push_back( Gtk::TargetEntry("text/plain") );
48 //Make m_Button_Drag a DnD drag source:
49 m_Button_Canio.drag_source_set(listTargets);
50 m_Button_Y.drag_source_set(listTargets);
51 m_Button_Codo.drag_source_set(listTargets);
54 m_Button_Canio.signal_drag_data_get().connect( SigC::slot(*this, &DnDWindow::on_canio_drag_data_get));
55 m_Button_Y.signal_drag_data_get().connect( SigC::slot(*this, &DnDWindow::on_y_drag_data_get));
56 m_Button_Codo.signal_drag_data_get().connect( SigC::slot(*this, &DnDWindow::on_codo_drag_data_get));
57 // Señales para cambiar el icono cuando empieza el drag.
58 m_Button_Canio.signal_drag_begin().connect( SigC::slot(*this, &DnDWindow::on_canio_drag_begin));
59 m_Button_Y.signal_drag_begin().connect( SigC::slot(*this, &DnDWindow::on_y_drag_begin));
60 m_Button_Codo.signal_drag_begin().connect( SigC::slot(*this, &DnDWindow::on_codo_drag_begin));
62 m_VBox.pack_start(m_Button_Canio);
63 m_VBox.pack_start(m_Button_Y);
64 m_VBox.pack_start(m_Button_Codo);
69 //Make m_Label_Drop a DnD drop destination:
70 m_WorkPlace.drag_dest_set(listTargets);
73 m_WorkPlace.signal_drag_data_received().connect( SigC::slot(*this, &DnDWindow::on_label_drop_drag_data_received) );
79 DnDWindow::~DnDWindow()
83 // Boton presionado en el codo
84 void DnDWindow::on_item_button_down()
86 std::cout << "boton abajo" << std::endl;
89 void DnDWindow::on_canio_drag_begin(const Glib::RefPtr<Gdk::DragContext> &context)
91 context->set_icon(ico_canio, 0, 0); //ico_canio->get_width(), ico_canio->get_height());
94 void DnDWindow::on_y_drag_begin(const Glib::RefPtr<Gdk::DragContext> &context)
96 context->set_icon(ico_y, 0, 0); //ico_canio->get_width(), ico_canio->get_height());
99 void DnDWindow::on_codo_drag_begin(const Glib::RefPtr<Gdk::DragContext> &context)
101 context->set_icon(ico_codo, 0, 0); //ico_canio->get_width(), ico_canio->get_height());
107 void DnDWindow::on_canio_drag_data_get(const Glib::RefPtr<Gdk::DragContext>& context, GtkSelectionData* selection_data, guint, guint)
109 //TODO: The gtkmm API needs to change to use a Gtk::SelectionData instead of a GtkSelectionData.
110 //That should happen for gtkmm 2.4.
111 Glib::RefPtr<Gdk::Pixbuf> image;
112 image = Gdk::Pixbuf::create_from_file("canio.png");
113 // Cambio el icono de DND seteando el "hot spot" en el centro.
114 context->set_icon(image, image->get_width() / 2, image->get_height() / 2);
115 gtk_selection_data_set (selection_data, selection_data->target, 8, (const guchar*)"canio.png", 9);
118 void DnDWindow::on_y_drag_data_get(const Glib::RefPtr<Gdk::DragContext>&, GtkSelectionData* selection_data, guint, guint)
120 //TODO: The gtkmm API needs to change to use a Gtk::SelectionData instead of a GtkSelectionData.
121 //That should happen for gtkmm 2.4.
122 gtk_selection_data_set (selection_data, selection_data->target, 8, (const guchar*)"y.png", 5);
125 void DnDWindow::on_codo_drag_data_get(const Glib::RefPtr<Gdk::DragContext>&, GtkSelectionData* selection_data, guint, guint)
127 //TODO: The gtkmm API needs to change to use a Gtk::SelectionData instead of a GtkSelectionData.
128 //That should happen for gtkmm 2.4.
129 gtk_selection_data_set (selection_data, selection_data->target, 8, (const guchar*)"codo.png", 8);
132 void DnDWindow::on_label_drop_drag_data_received(const Glib::RefPtr<Gdk::DragContext>& context, int x, int y, GtkSelectionData* selection_data, guint, guint time)
134 //TODO: The gtkmm API needs to change to use a Gtk::SelectionData instead of a GtkSelectionData.
135 //That should happen for gtkmm 2.4.
137 if ((selection_data->length >= 0) && (selection_data->format == 8))
139 CItem *a = Gtk::manage( new CItem((const char *)selection_data->data) );
140 /* Ajusto coordenada x e y para que caigan en un lugar de una cuadricula de
143 // el +1 es para evitar un bug cuando se selecciona muy cerce de la
144 // separacion de 2 cuadritos
147 m_WorkPlace.put(*a, i*32, j*32);
149 //listaItems.push_back(a);
152 context->drag_finish(false, false, time);