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 DnDWindow::DnDWindow()
23 : m_Button_Canio("Drag Canio\n"),
24 m_Button_Y("Drop Y\n"),
25 m_Button_Codo("Drag Codo\n")
29 ico_canio = Gdk::Pixbuf::create_from_file("canio.png");
30 ico_y = Gdk::Pixbuf::create_from_file("y.png");
31 ico_codo = Gdk::Pixbuf::create_from_file("codo.png");
35 m_HBox.pack_start(m_VBox, false, false, 0);
37 m_VBox.pack_start(m_Button_Canio);
38 m_VBox.pack_start(m_Button_Y);
39 m_VBox.pack_start(m_Button_Codo);
41 m_HBox.pack_start(scroll);
42 scroll.add(m_WorkPlace);
43 scroll.set_size_request(600, 600);
44 m_WorkPlace.set_size_request(600, 600);
47 listTargets.push_back( Gtk::TargetEntry("STRING") );
48 listTargets.push_back( Gtk::TargetEntry("text/plain") );
49 listTargets.push_back( Gtk::TargetEntry("POINTER") );
50 listTargets.push_back( Gtk::TargetEntry("application/pointer") );
54 //Make m_Button_Drag a DnD drag source:
55 m_Button_Canio.drag_source_set(listTargets);
56 m_Button_Y.drag_source_set(listTargets);
57 m_Button_Codo.drag_source_set(listTargets);
60 m_Button_Canio.signal_drag_data_get().connect( SigC::slot(*this, &DnDWindow::on_canio_drag_data_get));
61 m_Button_Y.signal_drag_data_get().connect( SigC::slot(*this, &DnDWindow::on_y_drag_data_get));
62 m_Button_Codo.signal_drag_data_get().connect( SigC::slot(*this, &DnDWindow::on_codo_drag_data_get));
63 // Señales para cambiar el icono cuando empieza el drag.
64 m_Button_Canio.signal_drag_begin().connect( SigC::slot(*this, &DnDWindow::on_canio_drag_begin));
65 m_Button_Y.signal_drag_begin().connect( SigC::slot(*this, &DnDWindow::on_y_drag_begin));
66 m_Button_Codo.signal_drag_begin().connect( SigC::slot(*this, &DnDWindow::on_codo_drag_begin));
69 m_WorkPlace.drag_dest_set(listTargets);
72 m_WorkPlace.signal_drag_data_received().connect( SigC::slot(*this, &DnDWindow::on_label_drop_drag_data_received) );
77 DnDWindow::~DnDWindow()
81 // Boton presionado en el codo
82 void DnDWindow::on_item_button_down()
84 std::cout << "boton abajo" << std::endl;
87 void DnDWindow::on_canio_drag_begin(const Glib::RefPtr<Gdk::DragContext> &context)
89 context->set_icon(ico_canio, 5, 5); //ico_canio->get_width(), ico_canio->get_height());
92 void DnDWindow::on_y_drag_begin(const Glib::RefPtr<Gdk::DragContext> &context)
94 context->set_icon(ico_y, 5, 5); //ico_canio->get_width(), ico_canio->get_height());
97 void DnDWindow::on_codo_drag_begin(const Glib::RefPtr<Gdk::DragContext> &context)
99 context->set_icon(ico_codo, 5, 5); //ico_canio->get_width(), ico_canio->get_height());
105 void DnDWindow::on_canio_drag_data_get(const Glib::RefPtr<Gdk::DragContext>& context, GtkSelectionData* selection_data, guint, guint)
107 //TODO: The gtkmm API needs to change to use a Gtk::SelectionData instead of a GtkSelectionData.
108 //That should happen for gtkmm 2.4.
109 gtk_selection_data_set (selection_data, selection_data->target, 8, (const guchar*)"canio.png", 9);
112 void DnDWindow::on_y_drag_data_get(const Glib::RefPtr<Gdk::DragContext>&, GtkSelectionData* selection_data, guint, guint)
114 //TODO: The gtkmm API needs to change to use a Gtk::SelectionData instead of a GtkSelectionData.
115 //That should happen for gtkmm 2.4.
116 gtk_selection_data_set (selection_data, selection_data->target, 8, (const guchar*)"y.png", 5);
119 void DnDWindow::on_codo_drag_data_get(const Glib::RefPtr<Gdk::DragContext>&, GtkSelectionData* selection_data, guint, guint)
121 //TODO: The gtkmm API needs to change to use a Gtk::SelectionData instead of a GtkSelectionData.
122 //That should happen for gtkmm 2.4.
123 gtk_selection_data_set (selection_data, selection_data->target, 8, (const guchar*)"codo.png", 8);
126 void DnDWindow::on_label_drop_drag_data_received(const Glib::RefPtr<Gdk::DragContext>& context, int x, int y, GtkSelectionData* selection_data, guint, guint time)
128 //TODO: The gtkmm API needs to change to use a Gtk::SelectionData instead of a GtkSelectionData.
129 //That should happen for gtkmm 2.4.
130 /* Ajusto coordenada x e y para que caigan en un lugar de una cuadricula de
133 // el +1 es para evitar un bug cuando se selecciona muy cerce de la
134 // separacion de 2 cuadritos
138 // El drag es de un item
139 if (selection_data->format == 10) {
140 m_WorkPlace.move(*drag_get_source_widget(context), i*32, j*32);
143 // El Drag es desde la barra de tareas
144 if ((selection_data->length >= 0) && (selection_data->format == 8))
146 CItem *a = Gtk::manage( new CItem((const char *)selection_data->data) );
147 m_WorkPlace.put(*a, i*32, j*32);
148 // Seteo la lista de tipos de drags
149 a->drag_source_set(listTargets);
150 // Conecto las señales
151 a->signal_drag_data_get().connect( SigC::slot(*this, &DnDWindow::on_item_drag_data_get));
153 // Utilizo el SigC::bind para que el callback on_drag_begin acepte un
154 // parametro extra, en este caso un CItem *. Esto se hace para
155 // que cuando el usuario quiera mover un item, saber que item es
156 // y pedirle su ícono para mostrar cono icono durante la operacion,
157 // Esto va a permitir, que si un widget tiene una imagen rotara o algo
158 // raro se vea el widget tal cual.
159 a->signal_drag_begin().connect(SigC::bind( SigC::slot(*this, &DnDWindow::on_item_drag_begin), a));
162 listaItems.push_back(a);
165 context->drag_finish(false, false, time);
168 void DnDWindow::on_item_drag_begin(const Glib::RefPtr<Gdk::DragContext>& context, CItem *item)
170 context->set_icon(item->get_image(), 5, 5);
173 void DnDWindow::on_item_drag_data_get(const Glib::RefPtr<Gdk::DragContext>& context, GtkSelectionData* selection_data, guint info, guint time)
175 // El 10 creo que es el format
176 gtk_selection_data_set (selection_data, selection_data->target, 10, (const guchar*)"codo.png", 8);