]> git.tdb.fi Git - r2c2.git/blob - routebar.cpp
77493647a529e2f302482677ac59755d8c2942b7
[r2c2.git] / routebar.cpp
1 /* $Id$
2
3 This file is part of R²C²
4 Copyright © 2010-2011 Mikkosoft Productions, Mikko Rasa
5 Distributed under the GPL
6 */
7
8 #include <msp/strings/formatter.h>
9 #include "libr2c2/route.h"
10 #include "designer.h"
11 #include "routebar.h"
12
13 using namespace std;
14 using namespace Msp;
15 using namespace R2C2;
16
17 Routebar::Routebar(Designer &d):
18         Toolbar("Route", 370),
19         designer(d)
20 {
21         pnl_content->add(*(drp_routes = new GLtk::Dropdown));
22         drp_routes->set_geometry(GLtk::Geometry(0, 10, 250, 20));
23         drp_routes->set_tooltip("Select route to edit");
24         drp_routes->append("(new route)");
25         drp_routes->signal_item_selected.connect(sigc::mem_fun(this, &Routebar::route_selected));
26
27         GLtk::Button *btn;
28
29         pnl_content->add(*(btn = new GLtk::Button("Del")));
30         btn->set_geometry(GLtk::Geometry(250, 10, 40, 24));
31         btn->set_style("red");
32         btn->set_tooltip("Delete the current route");
33         btn->signal_clicked.connect(sigc::mem_fun(this, &Routebar::delete_route_clicked));
34
35         pnl_content->add(*(btn = new GLtk::Button("Name")));
36         btn->set_geometry(GLtk::Geometry(290, 10, 40, 24));
37         btn->set_tooltip("Rename the current route");
38         btn->signal_clicked.connect(sigc::mem_fun(&designer, &Designer::rename_route));
39
40         pnl_content->add(*(btn = new GLtk::Button("Add")));
41         btn->set_geometry(GLtk::Geometry(330, 10, 40, 24));
42         btn->set_tooltip("Add selected tracks to current route (A)");
43         btn->signal_clicked.connect(sigc::mem_fun(&designer, &Designer::add_selection_to_route));
44
45         designer.get_layout().signal_route_added.connect(sigc::mem_fun(this, &Routebar::route_added));
46         designer.get_layout().signal_route_removed.connect(sigc::hide(sigc::mem_fun(this, &Routebar::update_routes)));
47
48         const set<Route *> &routes = designer.get_layout().get_routes();
49         for(set<Route *>::const_iterator i=routes.begin(); i!=routes.end(); ++i)
50                 (*i)->signal_name_changed.connect(sigc::hide(sigc::mem_fun(this, &Routebar::update_routes)));
51
52         update_routes();
53 }
54
55 void Routebar::route_selected(unsigned index, const string &)
56 {
57         if(index==drp_routes->get_n_items()-1)
58         {
59                 Layout &rlayout = designer.get_layout();
60                 const set<Route *> &routes = rlayout.get_routes();
61                 Route *route = new Route(rlayout);
62                 route->set_name(format("Route %d", routes.size()));
63                 designer.edit_route(route);
64
65                 int selected = -1;
66                 unsigned n = 0;
67                 for(set<Route *>::const_iterator i=routes.begin(); (selected<0 && i!=routes.end()); ++i, ++n)
68                         if(*i==route)
69                                 selected = n;
70                 drp_routes->set_selected_index(selected);
71         }
72         else
73         {
74                 const set<Route *> &routes = designer.get_layout().get_routes();
75                 set<Route *>::const_iterator i = routes.begin();
76                 advance(i, index);
77                 designer.edit_route(*i);
78         }
79 }
80
81 void Routebar::delete_route_clicked()
82 {
83         Route *route = designer.get_current_route();
84         designer.edit_route(0);
85         delete route;
86 }
87
88 void Routebar::route_added(Route &r)
89 {
90         r.signal_name_changed.connect(sigc::hide(sigc::mem_fun(this, &Routebar::update_routes)));
91         update_routes();
92 }
93
94 void Routebar::update_routes()
95 {
96         drp_routes->clear();
97         const set<Route *> &routes = designer.get_layout().get_routes();
98         int selected = -1;
99         unsigned n = 0;
100         for(set<Route *>::const_iterator i=routes.begin(); i!=routes.end(); ++i, ++n)
101         {
102                 drp_routes->append((*i)->get_name());
103                 if(*i==designer.get_current_route())
104                         selected = n;
105         }
106         drp_routes->append("(new route)");
107         drp_routes->set_selected_index(selected);
108 }