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);
39 m_VBox.pack_start(m_Button_Canio);
40 m_VBox.pack_start(m_Button_Y);
41 m_VBox.pack_start(m_Button_Codo);
43 m_HBox.pack_start(m_WorkPlace);
44 m_WorkPlace.set_size_request(600, 600);
47 std::list<Gtk::TargetEntry> listTargets;
48 listTargets.push_back( Gtk::TargetEntry("STRING") );
49 listTargets.push_back( Gtk::TargetEntry("text/plain") );
53 //Make m_Button_Drag a DnD drag source:
54 m_Button_Canio.drag_source_set(listTargets);
55 m_Button_Y.drag_source_set(listTargets);
56 m_Button_Codo.drag_source_set(listTargets);
59 m_Button_Canio.signal_drag_data_get().connect( SigC::slot(*this, &DnDWindow::on_canio_drag_data_get));
60 m_Button_Y.signal_drag_data_get().connect( SigC::slot(*this, &DnDWindow::on_y_drag_data_get));
61 m_Button_Codo.signal_drag_data_get().connect( SigC::slot(*this, &DnDWindow::on_codo_drag_data_get));
62 // Señales para cambiar el icono cuando empieza el drag.
63 m_Button_Canio.signal_drag_begin().connect( SigC::slot(*this, &DnDWindow::on_canio_drag_begin));
64 m_Button_Y.signal_drag_begin().connect( SigC::slot(*this, &DnDWindow::on_y_drag_begin));
65 m_Button_Codo.signal_drag_begin().connect( SigC::slot(*this, &DnDWindow::on_codo_drag_begin));
68 m_WorkPlace.drag_dest_set(listTargets);
71 m_WorkPlace.signal_drag_data_received().connect( SigC::slot(*this, &DnDWindow::on_label_drop_drag_data_received) );
76 DnDWindow::~DnDWindow()
80 // Boton presionado en el codo
81 void DnDWindow::on_item_button_down()
83 std::cout << "boton abajo" << std::endl;
86 void DnDWindow::on_canio_drag_begin(const Glib::RefPtr<Gdk::DragContext> &context)
88 context->set_icon(ico_canio, 0, 0); //ico_canio->get_width(), ico_canio->get_height());
91 void DnDWindow::on_y_drag_begin(const Glib::RefPtr<Gdk::DragContext> &context)
93 context->set_icon(ico_y, 0, 0); //ico_canio->get_width(), ico_canio->get_height());
96 void DnDWindow::on_codo_drag_begin(const Glib::RefPtr<Gdk::DragContext> &context)
98 context->set_icon(ico_codo, 0, 0); //ico_canio->get_width(), ico_canio->get_height());
104 void DnDWindow::on_canio_drag_data_get(const Glib::RefPtr<Gdk::DragContext>& context, GtkSelectionData* selection_data, guint, guint)
106 //TODO: The gtkmm API needs to change to use a Gtk::SelectionData instead of a GtkSelectionData.
107 //That should happen for gtkmm 2.4.
108 gtk_selection_data_set (selection_data, selection_data->target, 8, (const guchar*)"canio.png", 9);
111 void DnDWindow::on_y_drag_data_get(const Glib::RefPtr<Gdk::DragContext>&, GtkSelectionData* selection_data, guint, guint)
113 //TODO: The gtkmm API needs to change to use a Gtk::SelectionData instead of a GtkSelectionData.
114 //That should happen for gtkmm 2.4.
115 gtk_selection_data_set (selection_data, selection_data->target, 8, (const guchar*)"y.png", 5);
118 void DnDWindow::on_codo_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*)"codo.png", 8);
125 void DnDWindow::on_label_drop_drag_data_received(const Glib::RefPtr<Gdk::DragContext>& context, int x, int y, GtkSelectionData* selection_data, guint, guint time)
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.
130 if ((selection_data->length >= 0) && (selection_data->format == 8))
132 CItem *a = Gtk::manage( new CItem((const char *)selection_data->data) );
133 /* Ajusto coordenada x e y para que caigan en un lugar de una cuadricula de
136 // el +1 es para evitar un bug cuando se selecciona muy cerce de la
137 // separacion de 2 cuadritos
140 m_WorkPlace.put(*a, i*32, j*32);
142 //listaItems.push_back(a);
145 context->drag_finish(false, false, time);