1 #include "constructor.h"
3 Constructor::Constructor(BaseObjectType* cobject, const Glib::RefPtr<Gnome::Glade::Xml>& refGlade):Gtk::Window(cobject)
6 set_title("Constructor");
9 ico_canio = Gdk::Pixbuf::create_from_file("canio_n.png");
10 ico_y = Gdk::Pixbuf::create_from_file("y_n.png");
11 ico_codo = Gdk::Pixbuf::create_from_file("codo_o.png");
12 ico_tanque = Gdk::Pixbuf::create_from_file("tanque_e.png");
14 refGlade->get_widget("btn_canio", btn_canio);
15 refGlade->get_widget("btn_codo", btn_codo);
16 refGlade->get_widget("btn_y", btn_y);
17 refGlade->get_widget("btn_tanque",btn_tanque);
18 refGlade->get_widget("main_menu_quit",main_menu_quit);
19 refGlade->get_widget("main_menu_save",main_menu_save);
20 refGlade->get_widget("edit_menu_del",edit_menu_del);
21 refGlade->get_widget("chk_btn_logica",chk_btn_logica);
22 refGlade->get_widget("file_selection",file_selection);
23 refGlade->get_widget("combo_entry",combo_entry);
24 refGlade->get_widget("btn_check",btn_check);
25 refGlade->get_widget("dlg_connect", dlg_connect);
26 refGlade->get_widget("btn_dlg_close", btn_dlg_close);
28 refGlade->get_widget_derived("workplace", workplace); //fixed
31 listTargets.push_back( Gtk::TargetEntry("STRING") );
32 listTargets.push_back( Gtk::TargetEntry("text/plain") );
33 listTargets.push_back( Gtk::TargetEntry("POINTER") );
34 listTargets.push_back( Gtk::TargetEntry("application/pointer") );
36 btn_canio->drag_source_set(listTargets);
37 btn_y->drag_source_set(listTargets);
38 btn_codo->drag_source_set(listTargets);
39 btn_tanque->drag_source_set(listTargets);
41 btn_canio->signal_drag_data_get().connect( SigC::slot(*this, &Constructor::on_btn_canio_drag_get));
42 btn_y->signal_drag_data_get().connect( SigC::slot(*this, &Constructor::on_btn_y_drag_get));
43 btn_codo->signal_drag_data_get().connect( SigC::slot(*this, &Constructor::on_btn_codo_drag_get));
44 btn_tanque->signal_drag_data_get().connect( SigC::slot(*this, &Constructor::on_btn_tanque_drag_get));
45 btn_check->signal_clicked().connect(SigC::slot(*this,&Constructor::on_btn_check_clicked));
46 btn_dlg_close->signal_clicked().connect(SigC::slot(*this,&Constructor::on_btn_dlg_connect_clicked));
47 chk_btn_logica->signal_clicked().connect(SigC::slot(*this, &Constructor::on_chk_btn_clicked));
49 main_menu_quit->signal_activate().connect(SigC::slot(*this, &Constructor::on_main_menu_quit));
50 main_menu_save->signal_activate().connect(SigC::slot(*this, &Constructor::on_main_menu_save));
51 edit_menu_del->signal_activate().connect(SigC::slot(*this,&Constructor::on_edit_menu_del));
53 btn_file_cancel = file_selection->get_cancel_button();
54 btn_file_ok = file_selection->get_ok_button();
55 btn_file_cancel->signal_clicked().connect(SigC::slot(*this,&Constructor::on_btn_file_cancel_clicked));
56 btn_file_ok->signal_clicked().connect(SigC::slot(*this,&Constructor::on_btn_file_ok_clicked));
58 // Señales para cambiar el icono cuando empieza el drag.
59 btn_canio->signal_drag_begin().connect( SigC::slot(*this, &Constructor::on_canio_drag_begin));
60 btn_y->signal_drag_begin().connect( SigC::slot(*this, &Constructor::on_y_drag_begin));
61 btn_codo->signal_drag_begin().connect( SigC::slot(*this, &Constructor::on_codo_drag_begin));
62 btn_tanque->signal_drag_begin().connect( SigC::slot(*this, &Constructor::on_tanque_drag_begin));
64 workplace->drag_dest_set(listTargets);
65 workplace->signal_drag_data_received().connect( SigC::slot(*this, &Constructor::on_item_drop_drag_received) );
66 workplace->listaItems = &listaItems;
70 Constructor::~Constructor()
72 std::list<CItem *>::iterator i = listaItems.begin();
73 std::cout << "ok" << std::endl;
74 while ( i != listaItems.end() ){
77 std::cout << "Elimnando ... " << std::endl;
79 i = listaItems.begin();
83 void Constructor::on_btn_canio_drag_get(const Glib::RefPtr<Gdk::DragContext>& context, GtkSelectionData* selection_data, guint info, guint time)
85 gtk_selection_data_set (selection_data, selection_data->target, 8, (const guchar*)"item_canio.png", 14);
88 void Constructor::on_btn_y_drag_get(const Glib::RefPtr<Gdk::DragContext>& context, GtkSelectionData* selection_data, guint info, guint time)
90 gtk_selection_data_set(selection_data, selection_data->target, 8,(const guchar*)"item_y.png",10);
93 void Constructor::on_btn_codo_drag_get(const Glib::RefPtr<Gdk::DragContext>& context, GtkSelectionData* selection_data, guint info, guint time)
95 gtk_selection_data_set(selection_data, selection_data->target, 8 ,(const guchar*)"item_codo.png",13);
98 void Constructor::on_btn_tanque_drag_get(const Glib::RefPtr<Gdk::DragContext>& context, GtkSelectionData* selection_data, guint info, guint time)
100 gtk_selection_data_set(selection_data, selection_data->target, 8,(const guchar*)"item_tanque.png",15);
103 void Constructor::on_main_menu_quit()
110 void Constructor::on_main_menu_save()
112 file_selection->show();
115 void Constructor::on_edit_menu_del()
117 // hay que meter algo aca.
120 void Constructor::on_chk_btn_clicked()
125 void Constructor::on_btn_file_ok_clicked()
127 std::list<CItem *>::iterator i = listaItems.begin();
128 std::string file_name = file_selection->get_filename();
129 if ( (archivo = fopen( file_name.c_str(), "w+")) != NULL ){
130 fprintf (archivo,"<planta>\n");
131 while ( i != listaItems.end() ){
136 fprintf(archivo,"</planta>\n");
138 std::cout<<"NO SE ABRIO EL ARCHIVO"<<std::endl;
140 file_selection->hide();
143 void Constructor::on_btn_file_cancel_clicked()
145 file_selection->hide();
148 void Constructor::on_btn_dlg_connect_clicked()
153 void Constructor::on_canio_drag_begin(const Glib::RefPtr<Gdk::DragContext>& context)
155 context->set_icon(ico_canio, 5, 5);
158 void Constructor::on_y_drag_begin(const Glib::RefPtr<Gdk::DragContext>& context)
160 context->set_icon(ico_y, 5, 5);
163 void Constructor::on_codo_drag_begin(const Glib::RefPtr<Gdk::DragContext>& context)
165 context->set_icon(ico_codo, 5, 5);
168 void Constructor::on_tanque_drag_begin(const Glib::RefPtr<Gdk::DragContext>& context)
170 context->set_icon(ico_tanque, 5, 5);
173 void Constructor::on_item_drag_begin(const Glib::RefPtr<Gdk::DragContext>& context, CItem *item)
175 context->set_icon(item->get_image(), 5, 5);
178 void Constructor::on_item_drag_data_get(const Glib::RefPtr<Gdk::DragContext>& context, GtkSelectionData* selection_data, guint info, guint time)
180 gtk_selection_data_set (selection_data, selection_data->target, 10, (const guchar*)"item_codo.png", 13);
183 bool Constructor::can_drop(CItem *item, int _x, int _y)
185 std::list<CItem*>::iterator i = listaItems.begin();
186 while( i != listaItems.end() ) {
188 if ( temp->get_id() != item->get_id() ) {
189 if ( (temp->is_occupied_area(_x, _y)) ||
190 ( temp->is_occupied_area(_x+item->get_image()->get_width()-1, _y+item->get_image()->get_height()-1)) ||
191 ( temp->is_occupied_area(_x, _y+item->get_image()->get_height()-1)) ||
192 ( temp->is_occupied_area(_x+item->get_image()->get_width()-1, _y) ) )
201 void Constructor::on_item_drop_drag_received(const Glib::RefPtr<Gdk::DragContext>& context, int x, int y, GtkSelectionData* selection_data, guint info, guint time)
204 /* Ajusto coordenada x e y para que caigan en un lugar de una cuadricula de 32x32 */
206 // el +1 es para evitar un bug cuando se selecciona muy cerce de la
207 // separacion de 2 cuadritos
210 // El drag es de un item
211 if (selection_data->format == 10) {
212 if (can_drop(((CItem*)drag_get_source_widget(context)),i*32, j*32)){
213 ((CItem*)drag_get_source_widget(context))->set_position(i*32, j*32);
214 workplace->move(*drag_get_source_widget(context), i*32, j*32);
218 // El Drag es desde la barra de tareas
219 if ((selection_data->length >= 0) && (selection_data->format == 8)) {
221 if (strcmp((const char *)selection_data->data, "item_codo.png")==0)
223 else if (strcmp((const char *)selection_data->data, "item_canio.png")==0)
225 else if (strcmp((const char *)selection_data->data, "item_y.png")==0)
227 else if (strcmp((const char *)selection_data->data, "item_tanque.png")==0)
230 //Seteo el ID del item
231 Glib::ustring name("item");
234 sprintf(char_id," %d",id);
236 a->set_name( name+char_id );
237 if ( can_drop(a, i*32, j*32) ) {
238 workplace->put(*a, i*32, j*32);
239 //Apunto al workplace
240 a->workplace= workplace;
242 a->combo_entry = combo_entry;
243 //Apunto a la listaItems.
244 a->listaItems = &listaItems;
245 //Seteo la posicion del item
246 a->set_position(i*32,j*32);
247 // Seteo la lista de tipos de drags
248 a->drag_source_set(listTargets);
249 // Conecto las señales
250 a->signal_drag_data_get().connect( SigC::slot(*this, &Constructor::on_item_drag_data_get));
252 // Utilizo el SigC::bind para que el callback on_drag_begin acepte un
253 // parametro extra, en este caso un CItem *. Esto se hace para
254 // que cuando el usuario quiera mover un item, saber que item es
255 // y pedirle su ícono para mostrar cono icono durante la operacion,
256 // Esto va a permitir, que si un widget tiene una imagen rotara o algo
257 // raro se vea el widget tal cual.
258 a->signal_drag_begin().connect(SigC::bind( SigC::slot(*this, &Constructor::on_item_drag_begin), a));
260 listaItems.push_back(a);
266 context->drag_finish(false, false, time);
270 void Constructor::on_btn_check_clicked()
272 std::list<CItem *>::iterator i = listaItems.begin();
273 while ( i != listaItems.end() ) {
275 if ( !temp->check_connection() ) {