+/* $Id$
+
+This file is part of the MSP Märklin suite
+Copyright © 2010 Mikkosoft Productions, Mikko Rasa
+Distributed under the GPL
+*/
+
+#include <msp/gltk/button.h>
+#include <msp/io/print.h>
+#include <msp/strings/utils.h>
+#include "timetabledialog.h"
+
+using namespace std;
+using namespace Msp;
+using namespace Marklin;
+
+TimetableDialog::TimetableDialog(const GLtk::Resources &r, Timetable &tt):
+ GLtk::Widget(r),
+ GLtk::Dialog(r),
+ timetable(tt)
+{
+ set_size(200, 200);
+
+ GLtk::Button *btn;
+
+ add_button(*(btn = new GLtk::Button(res, "Cncl")), 0);
+ btn->set_geometry(GLtk::Geometry(geom.w-80, 10, 30, 25));
+ btn->set_style("red");
+
+ add_button(*(btn = new GLtk::Button(res, "OK")), 1);
+ btn->set_geometry(GLtk::Geometry(geom.w-40, 10, 30, 25));
+ btn->set_style("green");
+
+ add(*(tgl_enabled = new GLtk::Toggle(res, "On")));
+ tgl_enabled->set_geometry(GLtk::Geometry(10, 10, 40, 27));
+ tgl_enabled->set_value(timetable.is_enabled());
+ tgl_enabled->signal_toggled.connect(sigc::mem_fun(this, &TimetableDialog::enabled_toggled));
+
+ add(*(ent_timetable = new GLtk::Entry(res)));
+ ent_timetable->set_geometry(GLtk::Geometry(10, 45, geom.w-20, geom.h-55));
+ ent_timetable->set_multiline(true);
+
+ string text;
+ for(unsigned i=0; i<timetable.get_n_rows(); ++i)
+ {
+ if(!text.empty())
+ text += '\n';
+ text += timetable.get_row(i).str();
+ }
+ ent_timetable->set_text(text);
+}
+
+void TimetableDialog::enabled_toggled(bool value)
+{
+ timetable.set_enabled(value);
+}
+
+void TimetableDialog::on_response(int code)
+{
+ if(code)
+ {
+ timetable.clear();
+ vector<string> lines = split(ent_timetable->get_text(), '\n');
+ vector<Timetable::Row> rows;
+ rows.reserve(lines.size());
+ try
+ {
+ for(vector<string>::const_iterator i=lines.begin(); i!=lines.end(); ++i)
+ rows.push_back(Timetable::Row::parse(*i));
+ for(vector<Timetable::Row>::const_iterator i=rows.begin(); i!=rows.end(); ++i)
+ timetable.append(*i);
+ }
+ catch(const Exception &e)
+ {
+ // XXX Need a better way to report errors. Also, should not let the dialog close.
+ IO::print("%s\n", e.what());
+ }
+ }
+}