]> git.llucax.com Git - z.facultad/75.42/plaqui.git/blob - Constructor/constructor.cpp
af1263ba7b031b84b3f0f8215ab50e590c650d40
[z.facultad/75.42/plaqui.git] / Constructor / constructor.cpp
1 //constructor.cpp
2 #include "constructor.h"
3
4 Constructor::Constructor(BaseObjectType* cobject, const Glib::RefPtr<Gnome::Glade::Xml>& refGlade):Gtk::Window(cobject)
5 {
6         id = 0;
7         set_title("Constructor");
8         
9         
10         ico_canio = Gdk::Pixbuf::create_from_file("canio_n.png");
11         ico_y     = Gdk::Pixbuf::create_from_file("y_n.png");
12         ico_codo  = Gdk::Pixbuf::create_from_file("codo_o.png");
13         ico_tanque  = Gdk::Pixbuf::create_from_file("tanque_e.png");
14         
15         refGlade->get_widget("btn_canio", btn_canio);
16         refGlade->get_widget("btn_codo", btn_codo);
17         refGlade->get_widget("btn_y", btn_y);
18         refGlade->get_widget("btn_tanque",btn_tanque);
19         refGlade->get_widget("main_menu_quit",main_menu_quit);
20         refGlade->get_widget("edit_menu_del",edit_menu_del);
21         refGlade->get_widget("chk_btn_logica",chk_btn_logica);
22         
23         refGlade->get_widget_derived("workplace", workplace);  //fixed
24         
25         
26         
27         //Targets
28         listTargets.push_back( Gtk::TargetEntry("STRING") );
29         listTargets.push_back( Gtk::TargetEntry("text/plain") );
30         listTargets.push_back( Gtk::TargetEntry("POINTER") );
31         listTargets.push_back( Gtk::TargetEntry("application/pointer") );
32         
33         btn_canio->drag_source_set(listTargets);
34         btn_y->drag_source_set(listTargets);
35         btn_codo->drag_source_set(listTargets);
36         btn_tanque->drag_source_set(listTargets);
37         
38         btn_canio->signal_drag_data_get().connect( SigC::slot(*this, &Constructor::on_btn_canio_drag_get));
39         btn_y->signal_drag_data_get().connect( SigC::slot(*this, &Constructor::on_btn_y_drag_get));
40         btn_codo->signal_drag_data_get().connect( SigC::slot(*this, &Constructor::on_btn_codo_drag_get));
41         btn_tanque->signal_drag_data_get().connect( SigC::slot(*this, &Constructor::on_btn_tanque_drag_get));
42         chk_btn_logica->signal_clicked().connect(SigC::slot(*this, &Constructor::on_chk_btn_clicked));
43         
44         main_menu_quit->signal_activate().connect(SigC::slot(*this, &Constructor::on_main_menu_quit));
45         edit_menu_del->signal_activate().connect(SigC::slot(*this,&Constructor::on_edit_menu_del));
46         
47         // Señales para cambiar el icono cuando empieza el drag.
48         btn_canio->signal_drag_begin().connect( SigC::slot(*this, &Constructor::on_canio_drag_begin));
49         btn_y->signal_drag_begin().connect( SigC::slot(*this, &Constructor::on_y_drag_begin));
50         btn_codo->signal_drag_begin().connect( SigC::slot(*this, &Constructor::on_codo_drag_begin));
51         btn_tanque->signal_drag_begin().connect( SigC::slot(*this, &Constructor::on_tanque_drag_begin));
52         
53         
54         workplace->drag_dest_set(listTargets);
55         workplace->signal_drag_data_received().connect( SigC::slot(*this, &Constructor::on_item_drop_drag_received) );  
56         workplace->listaItems = &listaItems;    
57         logica = false;
58 }
59
60 Constructor::~Constructor()
61 {
62         std::list<CItem *>::iterator i = listaItems.begin();
63         std::cout << "ok" << std::endl;
64         while ( i != listaItems.end() ){
65                 CItem *temp = *i;
66                 listaItems.erase(i);
67                 std::cout << "Elimnando ... " << std::endl;
68                 delete temp;
69                 i = listaItems.begin();
70         }
71 }
72
73 void Constructor::on_btn_canio_drag_get(const Glib::RefPtr<Gdk::DragContext>& context, GtkSelectionData* selection_data, guint info, guint time)
74 {
75         gtk_selection_data_set (selection_data, selection_data->target, 8, (const guchar*)"item_canio.png", 14);
76 }
77
78 void Constructor::on_btn_y_drag_get(const Glib::RefPtr<Gdk::DragContext>& context, GtkSelectionData* selection_data, guint info, guint time)
79 {
80         gtk_selection_data_set(selection_data, selection_data->target, 8,(const guchar*)"item_y.png",10);
81 }
82
83 void Constructor::on_btn_codo_drag_get(const Glib::RefPtr<Gdk::DragContext>& context, GtkSelectionData* selection_data, guint info, guint time)
84 {
85         gtk_selection_data_set(selection_data, selection_data->target, 8 ,(const guchar*)"item_codo.png",13);
86 }
87
88 void Constructor::on_btn_tanque_drag_get(const Glib::RefPtr<Gdk::DragContext>& context, GtkSelectionData* selection_data, guint info, guint time)
89 {
90         gtk_selection_data_set(selection_data, selection_data->target, 8,(const guchar*)"item_tanque.png",15);
91 }
92
93 void Constructor::on_main_menu_quit()
94 {
95         delete this;
96         Gtk::Main::quit();
97         //hide();
98 }
99
100 void Constructor::on_edit_menu_del()
101 {
102         // hay que meter algo aca.
103 }
104
105 void Constructor::on_chk_btn_clicked()
106 {
107         logica = !logica;
108 }
109
110 void Constructor::on_canio_drag_begin(const Glib::RefPtr<Gdk::DragContext>& context)
111 {
112         context->set_icon(ico_canio, 5, 5); 
113 }
114
115 void Constructor::on_y_drag_begin(const Glib::RefPtr<Gdk::DragContext>& context)
116 {
117         context->set_icon(ico_y, 5, 5); 
118 }
119
120 void Constructor::on_codo_drag_begin(const Glib::RefPtr<Gdk::DragContext>& context)
121 {
122         context->set_icon(ico_codo, 5, 5); 
123 }
124
125 void Constructor::on_tanque_drag_begin(const Glib::RefPtr<Gdk::DragContext>& context)
126 {
127         context->set_icon(ico_tanque, 5, 5); 
128 }
129
130 void Constructor::on_item_drag_begin(const Glib::RefPtr<Gdk::DragContext>& context, CItem *item)
131 {
132         context->set_icon(item->get_image(), 5, 5);
133 }
134
135 void Constructor::on_item_drag_data_get(const Glib::RefPtr<Gdk::DragContext>& context, GtkSelectionData* selection_data, guint info, guint time)
136 {       
137                 gtk_selection_data_set (selection_data, selection_data->target, 10, (const guchar*)"item_codo.png", 13);
138 }
139
140 bool Constructor::can_drop(CItem *item, int _x, int _y)
141 {
142         std::list<CItem*>::iterator  i = listaItems.begin();
143         while( i != listaItems.end() ) {
144                 CItem *temp = *i;
145                 if ( temp->get_id() != item->get_id()   ) {
146                         if (  (temp->is_occupied_area(_x, _y))  ||  
147                                 ( temp->is_occupied_area(_x+item->get_image()->get_width()-1, _y+item->get_image()->get_height()-1))  ||
148                                 ( temp->is_occupied_area(_x, _y+item->get_image()->get_height()-1)) ||
149                                 ( temp->is_occupied_area(_x+item->get_image()->get_width()-1, _y) ) ) 
150                                 return false;
151                         else i++;
152                 }
153                 else i++;
154         }
155         return true;
156 }
157
158 void Constructor::on_item_drop_drag_received(const Glib::RefPtr<Gdk::DragContext>& context, int x, int y, GtkSelectionData* selection_data, guint info, guint time)
159 {
160         if ( !logica ) {
161                 /* Ajusto coordenada x e y para que caigan en un lugar de una cuadricula de 32x32 */
162                 int i,j;
163                 // el +1 es para evitar un bug cuando se selecciona muy cerce de la
164                 // separacion de 2 cuadritos
165                 i = (x+1)/32;
166                 j = (y+1)/32;
167                 // El drag es de un item
168                 if (selection_data->format == 10) {
169                         if (can_drop(((CItem*)drag_get_source_widget(context)),i*32, j*32)){
170                                 ((CItem*)drag_get_source_widget(context))->set_position(i*32, j*32);
171                                 workplace->move(*drag_get_source_widget(context), i*32, j*32);
172                         }
173                 }
174
175                 // El Drag es desde la barra de tareas
176                 if ((selection_data->length >= 0) && (selection_data->format == 8))     {
177                         CItem *a;
178                         if (strcmp((const char *)selection_data->data, "item_codo.png")==0) 
179                                 a = new Splitter();//Gtk::manage( new Splitter() );
180                                         else    if (strcmp((const char *)selection_data->data, "item_canio.png")==0) 
181                                                 a = new Conduct();//Gtk::manage( new Conduct() );
182                                                         else if (strcmp((const char *)selection_data->data, "item_y.png")==0) 
183                                                                 a = new Union();//Gtk::manage( new Union() );
184                                                                         else if (strcmp((const char *)selection_data->data, "item_tanque.png")==0) 
185                                                                                 a = new Cistern();//Gtk::manage( new Cistern() );
186                                                                                         else
187                                                                                                 a = new CItem();//Gtk::manage( new CItem((const char *)selection_data->data) );
188                         //Seteo el ID del item
189                         a->set_id(++id);                
190                         if ( can_drop(a, i*32, j*32) ) {
191                                 workplace->put(*a, i*32, j*32);
192                                 //Apunto al workplace
193                                 a->workplace= workplace;
194                                 //Seteo la posicion del item
195                                 a->set_position(i*32,j*32);
196                                 // Seteo la lista de tipos de drags 
197                                 a->drag_source_set(listTargets);
198                                 // Conecto las señales
199                                 a->signal_drag_data_get().connect( SigC::slot(*this, &Constructor::on_item_drag_data_get));
200
201                                 // Utilizo el SigC::bind para que el callback on_drag_begin acepte un
202                                 // parametro extra, en este caso un CItem *. Esto se hace para
203                                 // que cuando el usuario quiera mover un item, saber que item es
204                                 // y pedirle su ícono para mostrar cono icono durante la operacion,
205                                 // Esto va a permitir, que si un widget tiene una imagen rotara o algo
206                                 // raro se vea el widget tal cual.
207                                 a->signal_drag_begin().connect(SigC::bind( SigC::slot(*this, &Constructor::on_item_drag_begin), a));
208                                 a->show();      
209                                 listaItems.push_back(a);
210                         } else {
211                                         id--;
212                                         delete a;
213                                 }
214                 }
215                 context->drag_finish(false, false, time);
216         }
217 }