+/* $Id$
+
+This file is part of the MSP Märklin suite
+Copyright © 2006-2010 Mikkosoft Productions, Mikko Rasa
+Distributed under the GPL
+*/
+
+#include <msp/gltk/button.h>
+#include <msp/gltk/label.h>
+#include <msp/strings/formatter.h>
+#include <msp/strings/lexicalcast.h>
+#include "libmarklin/locotype.h"
+#include "engineer.h"
#include "trainproperties.h"
+using namespace std;
using namespace Msp;
+using namespace Marklin;
-TrainProperties::TrainProperties(GLtk::Resources &r, Train &t):
- Panel(r),
+TrainProperties::TrainProperties(Engineer &e, const GLtk::Resources &r, Train *t):
+ Widget(r),
+ Dialog(r),
+ engineer(e),
train(t)
{
- set_size(200, 50);
+ set_size(200, 145);
+
+ GLtk::Label *label;
+ add(*(label=new GLtk::Label(res, "Train properties")));
+ label->set_geometry(GLtk::Geometry(10, geom.h-25, geom.w-20, 20));
+
+ add(*(ent_addr=new GLtk::Entry(res)));
+ ent_addr->set_geometry(GLtk::Geometry(10, geom.h-50, 40, 20));
+
+ add(*(drp_type=new GLtk::Dropdown(res)));
+ drp_type->set_geometry(GLtk::Geometry(60, geom.h-50, geom.w-70, 20));
+
+ const map<unsigned, VehicleType *> &vehs = engineer.get_catalogue().get_vehicles();
+ unsigned n = 0;
+ for(map<unsigned, VehicleType *>::const_iterator i=vehs.begin(); i!=vehs.end(); ++i, ++n)
+ {
+ if(!dynamic_cast<LocoType *>(i->second))
+ continue;
- add(*(ent_name=new GLtk::Entry(res, train.get_name())));
- ent_name->set_geometry(
+ drp_type->append(format("%d %s", i->second->get_article_number(), i->second->get_name()));
+ if(train && i->second==&train->get_locomotive_type())
+ drp_type->set_selected_index(n);
+ }
- GLtk::Button *btn;
+ add(*(ent_name=new GLtk::Entry(res)));
+ ent_name->set_geometry(GLtk::Geometry(10, geom.h-75, geom.w-20, 20));
+
+ add(*(drp_priority=new GLtk::Dropdown(res)));
+ drp_priority->set_geometry(GLtk::Geometry(10, geom.h-100, geom.w-20, 20));
+ drp_priority->append("Standard freight");
+ drp_priority->append("Express freight");
+ drp_priority->append("Unspecified");
+ drp_priority->append("Standard passenger");
+ drp_priority->append("Express passenger");
+
+ if(train)
+ {
+ ent_addr->set_text(lexical_cast(train->get_address()));
+ ent_name->set_text(train->get_name());
+ drp_priority->set_selected_index(train->get_priority()+2);
+ }
+ else
+ {
+ ent_name->set_text(format("Train %d", engineer.get_layout().get_trains().size()+1));
+ drp_priority->set_selected_index(2);
+ }
+}
+
+void TrainProperties::on_ok_clicked()
+{
+ if(!train)
+ {
+ const map<unsigned, VehicleType *> &vehs = engineer.get_catalogue().get_vehicles();
+ map<unsigned, VehicleType *>::const_iterator i = vehs.begin();
+ unsigned n = drp_type->get_selected_index();
+ while(!dynamic_cast<LocoType *>(i->second))
+ ++i;
+ while(n)
+ {
+ if(dynamic_cast<LocoType *>(i->second))
+ --n;
+ ++i;
+ }
- add(*(btn=new GLtk::Button(res, "OK")));
- btn->set_geometry(geom.width-45, 5, 40, 25);
+ unsigned addr = lexical_cast<unsigned>(ent_addr->get_text());
+ train = new Train(engineer.get_layout(), *dynamic_cast<LocoType *>(i->second), addr);
+ }
- add(*(btn=new GLtk::Button(res, "Cncl")));
- btn->set_geometry(geom.width-45, 5, 40, 25);
+ train->set_name(ent_name->get_text());
+ train->set_priority(drp_priority->get_selected_index()-2);
}