+#include <msp/gltk/button.h>
+#include <msp/gltk/column.h>
+#include <msp/gltk/label.h>
+#include <msp/gltk/row.h>
+#include "libr2c2/track.h"
+#include "libr2c2/tracktype.h"
+#include "objectproperties.h"
+#include "selection.h"
+
+using namespace std;
+using namespace Msp;
+using namespace R2C2;
+
+ObjectProperties::ObjectProperties(const Selection &selection):
+ properties(0),
+ prev_widget(0)
+{
+ set_layout(new GLtk::Layout);
+ GLtk::Column col(*layout);
+
+ GLtk::Label *lbl_title;
+
+ add(*(lbl_title = new GLtk::Label("Object properties")));
+ lbl_title->set_style("title");
+ layout->set_expand(*lbl_title, true, false);
+
+ Object *object = selection.get_object();
+ if(Track *track = dynamic_cast<Track *>(object))
+ {
+ if(track->get_type().is_turnout())
+ {
+ if(selection.size()==1)
+ {
+ lbl_title->set_text("Turnout properties");
+ properties = new TurnoutProperties(*this, *track);
+ }
+ }
+ else
+ {
+ set<Track *> tracks = selection.get_objects<Track>();
+ if(tracks.size()==selection.size())
+ {
+ bool all_linear = true;
+ for(set<Track *>::iterator i=tracks.begin(); (all_linear && i!=tracks.end()); ++i)
+ all_linear = !(*i)->get_type().is_turnout();
+ if(all_linear)
+ {
+ lbl_title->set_text("Track properties");
+ properties = new TrackCircuitProperties(*this, tracks);
+ }
+ }
+ }
+ }
+ else if(selection.size()==1)
+ {
+ if(Signal *signal = dynamic_cast<Signal *>(object))
+ {
+ lbl_title->set_text("Signal properties");
+ properties = new SignalProperties(*this, *signal);
+ }
+ else if(BeamGate *gate = dynamic_cast<BeamGate *>(object))
+ {
+ lbl_title->set_text("Beam gate properties");
+ properties = new BeamGateProperties(*this, *gate);
+ }
+ }
+
+ if(!properties)
+ add(*(new GLtk::Label("No properties available")));
+
+ GLtk::Button *btn;
+
+ {
+ GLtk::Row row(*layout);
+ row.split();
+ add_button(*(btn = new GLtk::Button("Cncl")), 0);
+ btn->set_style("red");
+
+ add_button(*(btn = new GLtk::Button("OK")), 1);
+ btn->set_style("green");
+ }
+}
+
+GLtk::Entry *ObjectProperties::add_property(const string &label, const string &value, unsigned size)
+{
+ GLtk::Row row(*layout);
+ add(*(new GLtk::Label(label)));
+
+ GLtk::Entry *entry = new GLtk::Entry(value);
+ add(*entry);
+ if(size)
+ entry->set_edit_size(size, 1);
+
+ if(prev_widget)
+ layout->add_constraint(*entry, GLtk::Layout::ALIGN_LEFT, *prev_widget);
+ prev_widget = entry;
+
+ return entry;
+}
+
+void ObjectProperties::on_response(int code)
+{
+ if(code==1 && properties)
+ properties->apply();
+}
+
+
+ObjectProperties::TurnoutProperties::TurnoutProperties(ObjectProperties &p, Track &t):
+ track(t),
+ ent_address(p.add_property("Turnout address", lexical_cast<string>(track.get_turnout_address()), 5))
+{ }
+
+void ObjectProperties::TurnoutProperties::apply()
+{
+ track.set_turnout_address(lexical_cast<unsigned>(ent_address->get_text()));
+}
+
+
+ObjectProperties::TrackCircuitProperties::TrackCircuitProperties(ObjectProperties &p, const set<Track *> &t):
+ tracks(t),
+ ent_address(p.add_property("Sensor address", string(), 5))
+{
+ int addr = -1;
+ for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
+ {
+ int a = (*i)->get_sensor_address();
+ if(a!=addr)
+ {
+ if(addr==-1)
+ addr = a;
+ else
+ addr = -2;
+ }
+ }
+
+ if(addr>=0)
+ ent_address->set_text(lexical_cast<string>(addr));
+}
+
+void ObjectProperties::TrackCircuitProperties::apply()
+{
+ const string &text = ent_address->get_text();
+ if(!text.empty())
+ {
+ unsigned addr = lexical_cast<unsigned>(text);
+ for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
+ (*i)->set_sensor_address(addr);
+ }
+}
+
+
+ObjectProperties::SignalProperties::SignalProperties(ObjectProperties &p, Signal &s):
+ signal(s),
+ ent_address(p.add_property("Signal address", lexical_cast<string>(signal.get_address()), 5))
+{ }
+
+void ObjectProperties::SignalProperties::apply()
+{
+ signal.set_address(lexical_cast<unsigned>(ent_address->get_text()));
+}
+
+
+ObjectProperties::BeamGateProperties::BeamGateProperties(ObjectProperties &p, BeamGate &g):
+ gate(g),
+ ent_address(p.add_property("Sensor address", lexical_cast<string>(gate.get_address()), 5))
+{ }
+
+void ObjectProperties::BeamGateProperties::apply()
+{
+ gate.set_address(lexical_cast<unsigned>(ent_address->get_text()));
+}