Pick up newly-added route groups correctly.
[ardour.git] / gtk2_ardour / add_route_dialog.cc
1 /*
2   Copyright (C) 2003 Paul Davis 
3
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU General Public License as published by
6   the Free Software Foundation; either version 2 of the License, or
7   (at your option) any later version.
8
9   This program is distributed in the hope that it will be useful,
10   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   GNU General Public License for more details.
13
14   You should have received a copy of the GNU General Public License
15   along with this program; if not, write to the Free Software
16   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include <cstdio>
21 #include <cmath>
22
23 #include <sigc++/bind.h>
24 #include <gtkmm/stock.h>
25 #include <gtkmm/separator.h>
26 #include <gtkmm/table.h>
27
28 #include "pbd/error.h"
29 #include "pbd/convert.h"
30 #include "gtkmm2ext/utils.h"
31 #include "ardour/profile.h"
32 #include "ardour/template_utils.h"
33 #include "ardour/route_group.h"
34 #include "ardour/session.h"
35
36 #include "utils.h"
37 #include "add_route_dialog.h"
38 #include "i18n.h"
39
40 using namespace Gtk;
41 using namespace Gtkmm2ext;
42 using namespace sigc;
43 using namespace std;
44 using namespace PBD;
45 using namespace ARDOUR;
46
47 static const char* track_mode_names[] = {
48         N_("Normal"),
49         N_("Non Layered"),
50         N_("Tape"),
51         0
52 };
53
54 AddRouteDialog::AddRouteDialog (Session & s)
55         : Dialog (_("ardour: add track/bus")),
56           _session (s),
57           track_button (_("Tracks")),
58           bus_button (_("Busses")),
59           routes_adjustment (1, 1, 128, 1, 4),
60           routes_spinner (routes_adjustment)
61 {
62         if (track_mode_strings.empty()) {
63                 track_mode_strings = I18N (track_mode_names);
64
65                 if (ARDOUR::Profile->get_sae()) {
66                         /* remove all but the first track mode (Normal) */
67
68                         while (track_mode_strings.size() > 1) {
69                                 track_mode_strings.pop_back();
70                         }
71                 }
72         }
73         
74         set_name ("AddRouteDialog");
75         set_wmclass (X_("ardour_add_track_bus"), "Ardour");
76         set_position (Gtk::WIN_POS_MOUSE);
77         set_resizable (false);
78
79         name_template_entry.set_name ("AddRouteDialogNameTemplateEntry");
80         track_button.set_name ("AddRouteDialogRadioButton");
81         bus_button.set_name ("AddRouteDialogRadioButton");
82         routes_spinner.set_name ("AddRouteDialogSpinner");
83
84         refill_channel_setups ();
85         refill_route_groups ();
86         set_popdown_strings (track_mode_combo, track_mode_strings, true);
87
88         channel_combo.set_active_text (channel_combo_strings.front());
89         track_mode_combo.set_active_text (track_mode_strings.front());
90
91         RadioButton::Group g = track_button.get_group();
92         bus_button.set_group (g);
93         track_button.set_active (true);
94
95         Table* table = manage (new Table (5, 2));
96         table->set_spacings (4);
97
98         /* add */
99
100         Label* l = manage (new Label (_("Add this many:")));
101         l->set_alignment (1, 0.5);
102         table->attach (*l, 0, 1, 0, 1);
103         table->attach (routes_spinner, 1, 2, 0, 1, FILL | EXPAND);
104
105         /* track/bus choice & modes */
106
107         HBox* hbox = manage (new HBox);
108         hbox->set_spacing (6);
109         hbox->pack_start (track_button, PACK_EXPAND_PADDING);
110         hbox->pack_start (bus_button, PACK_EXPAND_PADDING);
111         table->attach (*hbox, 0, 2, 1, 2);
112
113         channel_combo.set_name (X_("ChannelCountSelector"));
114         track_mode_combo.set_name (X_("ChannelCountSelector"));
115
116         track_button.signal_clicked().connect (mem_fun (*this, &AddRouteDialog::track_type_chosen));
117         bus_button.signal_clicked().connect (mem_fun (*this, &AddRouteDialog::track_type_chosen));
118
119         l = manage (new Label (_("Channel configuration:")));
120         l->set_alignment (1, 0.5);
121         table->attach (*l, 0, 1, 2, 3);
122         table->attach (channel_combo, 1, 2, 2, 3, FILL | EXPAND);
123
124         if (!ARDOUR::Profile->get_sae ()) {
125                 l = manage (new Label (_("Track mode:")));
126                 l->set_alignment (1, 0.5);
127                 table->attach (*l, 0, 1, 3, 4);
128                 table->attach (track_mode_combo, 1, 2, 3, 4, FILL | EXPAND);
129         }
130         
131         l = manage (new Label (_("Add to edit group:")));
132         l->set_alignment (1, 0.5);
133         table->attach (*l, 0, 1, 4, 5);
134         table->attach (route_group_combo, 1, 2, 4, 5, FILL | EXPAND);
135         get_vbox()->pack_start (*table);
136
137         get_vbox()->set_spacing (6);
138         get_vbox()->set_border_width (6);
139         get_vbox()->show_all ();
140
141         /* track template info will be managed whenever
142            this dialog is shown, via ::on_show()
143         */
144
145         add_button (Stock::CANCEL, RESPONSE_CANCEL);
146         add_button (Stock::ADD, RESPONSE_ACCEPT);
147
148         track_type_chosen ();
149 }
150
151 AddRouteDialog::~AddRouteDialog ()
152 {
153 }
154
155 void
156 AddRouteDialog::track_type_chosen ()
157 {
158         if (track_button.get_active()) {
159                 track_mode_combo.set_sensitive (true);
160         } else {
161                 track_mode_combo.set_sensitive (false);
162         }
163 }
164
165 bool
166 AddRouteDialog::track ()
167 {
168         return track_button.get_active ();
169 }
170
171 ARDOUR::DataType
172 AddRouteDialog::type ()
173 {
174         // FIXME: ew
175         
176         const string str = channel_combo.get_active_text();
177         if (str == _("MIDI"))
178                 return ARDOUR::DataType::MIDI;
179         else
180                 return ARDOUR::DataType::AUDIO;
181 }
182
183 string
184 AddRouteDialog::name_template ()
185 {
186         return name_template_entry.get_text ();
187 }
188
189 int
190 AddRouteDialog::count ()
191 {
192         return (int) floor (routes_adjustment.get_value ());
193 }
194
195 ARDOUR::TrackMode
196 AddRouteDialog::mode ()
197 {
198         if (ARDOUR::Profile->get_sae()) {
199                 return ARDOUR::Normal;
200         }
201
202         Glib::ustring str = track_mode_combo.get_active_text();
203         if (str == _("Normal")) {
204                 return ARDOUR::Normal;
205         } else if (str == _("Non Layered")){
206                 return ARDOUR::NonLayered;
207         } else if (str == _("Tape")) {
208                 return ARDOUR::Destructive;
209         } else {
210                 fatal << string_compose (X_("programming error: unknown track mode in add route dialog combo = %1"), str)
211                       << endmsg;
212                 /*NOTREACHED*/
213         }
214         /* keep gcc happy */
215         return ARDOUR::Normal;
216 }
217
218 int
219 AddRouteDialog::channels ()
220 {
221         string str = channel_combo.get_active_text();
222         
223         for (ChannelSetups::iterator i = channel_setups.begin(); i != channel_setups.end(); ++i) {
224                 if (str == (*i).name) {
225                         return (*i).channels;
226                 }
227         }
228
229         return 0;
230 }
231
232 string
233 AddRouteDialog::track_template ()
234 {
235         string str = channel_combo.get_active_text();
236         
237         for (ChannelSetups::iterator i = channel_setups.begin(); i != channel_setups.end(); ++i) {
238                 if (str == (*i).name) {
239                         return (*i).template_path;
240                 }
241         }
242
243         return string();
244 }
245
246 void
247 AddRouteDialog::on_show ()
248 {
249         refill_channel_setups ();
250         refill_route_groups ();
251         
252         Dialog::on_show ();
253 }
254
255 void
256 AddRouteDialog::refill_channel_setups ()
257 {
258         ChannelSetup chn;
259         
260         route_templates.clear ();
261         channel_combo_strings.clear ();
262         channel_setups.clear ();
263
264         chn.name = X_("MIDI");
265         chn.channels = 0;
266         channel_setups.push_back (chn);
267
268         chn.name = _("Mono");
269         chn.channels = 1;
270         channel_setups.push_back (chn);
271
272         chn.name = _("Stereo");
273         chn.channels = 2;
274         channel_setups.push_back (chn);
275
276         ARDOUR::find_route_templates (route_templates);
277
278         if (!ARDOUR::Profile->get_sae()) {
279                 if (!route_templates.empty()) {
280                         vector<string> v;
281                         for (vector<TemplateInfo>::iterator x = route_templates.begin(); x != route_templates.end(); ++x) {
282                                 chn.name = x->name;
283                                 chn.channels = 0;
284                                 chn.template_path = x->path;
285                                 channel_setups.push_back (chn);
286                         }
287                 } 
288
289                 chn.name = _("3 Channel");
290                 chn.channels = 3;
291                 channel_setups.push_back (chn);
292
293                 chn.name = _("4 Channel");
294                 chn.channels = 4;
295                 channel_setups.push_back (chn);
296
297                 chn.name = _("5 Channel");
298                 chn.channels = 5;
299                 channel_setups.push_back (chn);
300
301                 chn.name = _("6 Channel");
302                 chn.channels = 6;
303                 channel_setups.push_back (chn);
304
305                 chn.name = _("8 Channel");
306                 chn.channels = 8;
307                 channel_setups.push_back (chn);
308
309                 chn.name = _("12 Channel");
310                 chn.channels = 12;
311                 channel_setups.push_back (chn);
312
313                 chn.name = X_("Custom");
314                 chn.channels = 0;
315                 channel_setups.push_back (chn);
316         }
317
318         for (ChannelSetups::iterator i = channel_setups.begin(); i != channel_setups.end(); ++i) {
319                 channel_combo_strings.push_back ((*i).name);
320         }
321
322         set_popdown_strings (channel_combo, channel_combo_strings, true);
323         channel_combo.set_active_text (channel_combo_strings.front());
324 }
325
326 void
327 AddRouteDialog::add_route_group (RouteGroup* g)
328 {
329         route_group_combo.append_text (g->name ());
330 }
331
332 RouteGroup*
333 AddRouteDialog::route_group ()
334 {
335         if (route_group_combo.get_active_row_number () == 0) {
336                 return 0;
337         }
338
339         return _session.route_group_by_name (route_group_combo.get_active_text());
340 }
341
342 void
343 AddRouteDialog::refill_route_groups ()
344 {
345         route_group_combo.clear ();
346         route_group_combo.append_text (_("No group"));
347         _session.foreach_route_group (mem_fun (*this, &AddRouteDialog::add_route_group));
348         route_group_combo.set_active (0);
349 }
350