]> git.tdb.fi Git - r2c2.git/blob - source/remote/remote.cpp
a193b2bf599ef87507fa9aedc037c4572c7a3c10
[r2c2.git] / source / remote / remote.cpp
1 /* $Id$
2
3 This file is part of the MSP Märklin suite
4 Copyright © 2009-2010  Mikkosoft Productions, Mikko Rasa
5 Distributed under the GPL
6 */
7
8 #include <gtkmm/separator.h>
9 #include <msp/net/resolve.h>
10 #include <msp/time/units.h>
11 #include "remote.h"
12 #include "trainpanel.h"
13
14 using namespace std;
15 using namespace Msp;
16
17 Application::RegApp<Remote> Remote::reg;
18
19 Remote::Remote(int argc, char **argv):
20         client(catalogue),
21         gtk(argc, argv)
22 {
23         if(argc<2)
24                 throw UsageError("No address given");
25
26         DataFile::load(catalogue, "locos.dat");
27
28         client.use_event_dispatcher(event_disp);
29         client.signal_train_added.connect(sigc::mem_fun(this, &Remote::train_added));
30         string addr_str = argv[1];
31         if(addr_str.find(':')==string::npos)
32                 addr_str += ":8315";
33         Net::SockAddr *addr = Net::resolve(addr_str);
34         client.connect(*addr);
35         delete addr;
36
37         window.signal_hide().connect(sigc::bind(sigc::mem_fun(this, &Remote::exit), 0));
38         window.set_default_size(300, 200);
39         window.set_border_width(5);
40
41         train_box = new Gtk::VBox(false, 5);
42         window.add(*manage(train_box));
43
44         window.show_all();
45 }
46
47 void Remote::tick()
48 {
49         event_disp.tick(Time::zero);
50         gtk.iteration(false);
51 }
52
53 void Remote::train_added(Marklin::NetTrain &t)
54 {
55         TrainPanel *panel = new TrainPanel(*this, client, t);
56         if(!train_panels.empty())
57         {
58                 Gtk::HSeparator *sep = new Gtk::HSeparator;
59                 train_box->pack_start(*manage(sep), false, true);
60                 sep->show();
61         }
62         train_box->pack_start(*manage(panel), false, true);
63         train_panels.push_back(panel);
64 }