Make Route Group dialog instant-apply. Disallow identically-named route group; fixes...
[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 "route_group_dialog.h"
39 #include "i18n.h"
40
41 using namespace Gtk;
42 using namespace Gtkmm2ext;
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 std::vector<std::string> AddRouteDialog::channel_combo_strings;
55 std::vector<std::string> AddRouteDialog::track_mode_strings;
56
57 AddRouteDialog::AddRouteDialog (Session* s)
58         : ArdourDialog (_("Add Track or Bus"))
59         , routes_adjustment (1, 1, 128, 1, 4)
60         , routes_spinner (routes_adjustment)
61         , mode_label (_("Track mode:"))
62 {
63         set_session (s);
64
65         if (track_mode_strings.empty()) {
66                 track_mode_strings = I18N (track_mode_names);
67
68                 if (ARDOUR::Profile->get_sae()) {
69                         /* remove all but the first track mode (Normal) */
70
71                         while (track_mode_strings.size() > 1) {
72                                 track_mode_strings.pop_back();
73                         }
74                 }
75         }
76
77         set_name ("AddRouteDialog");
78         set_position (Gtk::WIN_POS_MOUSE);
79         set_modal (true);
80         set_skip_taskbar_hint (true);
81         set_resizable (false);
82
83         name_template_entry.set_name (X_("AddRouteDialogNameTemplateEntry"));
84         routes_spinner.set_name (X_("AddRouteDialogSpinner"));
85         channel_combo.set_name (X_("ChannelCountSelector"));
86         mode_combo.set_name (X_("ChannelCountSelector"));
87
88         refill_channel_setups ();
89         refill_route_groups ();
90         set_popdown_strings (mode_combo, track_mode_strings, true);
91
92         channel_combo.set_active_text (channel_combo_strings.front());
93         mode_combo.set_active_text (track_mode_strings.front());
94
95         track_bus_combo.append_text (_("tracks"));
96         track_bus_combo.append_text (_("busses"));
97         track_bus_combo.set_active (0);
98
99         VBox* vbox = manage (new VBox);
100         Gtk::Label* l;
101
102         get_vbox()->set_spacing (4);
103
104         vbox->set_spacing (18);
105         vbox->set_border_width (5);
106
107         HBox *type_hbox = manage (new HBox);
108         type_hbox->set_spacing (6);
109
110         /* track/bus choice */
111
112         type_hbox->pack_start (*manage (new Label (_("Add:"))));
113         type_hbox->pack_start (routes_spinner);
114         type_hbox->pack_start (track_bus_combo);
115
116         vbox->pack_start (*type_hbox, false, true);
117
118         VBox* options_box = manage (new VBox);
119         Table *table2 = manage (new Table (3, 3, false));
120
121         options_box->set_spacing (6);
122         table2->set_row_spacings (6);
123         table2->set_col_spacing (1, 6);
124
125         l = manage (new Label (_("<b>Options</b>"), Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false));
126         l->set_use_markup ();
127         options_box->pack_start (*l, false, true);
128
129         l = manage (new Label ("", Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false));
130         l->set_padding (8, 0);
131         table2->attach (*l, 0, 1, 0, 3, Gtk::FILL, Gtk::FILL, 0, 0);
132
133         /* Route configuration */
134
135         l = manage (new Label (_("Configuration:"), Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false));
136         table2->attach (*l, 1, 2, 0, 1, Gtk::FILL, Gtk::EXPAND, 0, 0);
137         table2->attach (channel_combo, 2, 3, 0, 1, Gtk::FILL, Gtk::EXPAND & Gtk::FILL, 0, 0);
138
139         if (!ARDOUR::Profile->get_sae ()) {
140
141                 /* Track mode */
142
143                 mode_label.set_alignment (Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER);
144                 table2->attach (mode_label, 1, 2, 1, 2, Gtk::FILL, Gtk::EXPAND, 0, 0);
145                 table2->attach (mode_combo, 2, 3, 1, 2, Gtk::FILL, Gtk::EXPAND & Gtk::FILL, 0, 0);
146
147         }
148
149         /* Group choise */
150
151         l = manage (new Label (_("Group:"), Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false));
152         table2->attach (*l, 1, 2, 2, 3, Gtk::FILL, Gtk::EXPAND, 0, 0);
153         table2->attach (route_group_combo, 2, 3, 2, 3, Gtk::FILL, Gtk::EXPAND & Gtk::FILL, 0, 0);
154
155         options_box->pack_start (*table2, false, true);
156         vbox->pack_start (*options_box, false, true);
157
158         get_vbox()->pack_start (*vbox, false, false);
159
160         track_bus_combo.signal_changed().connect (sigc::mem_fun (*this, &AddRouteDialog::track_type_chosen));
161         channel_combo.set_row_separator_func (sigc::mem_fun (*this, &AddRouteDialog::channel_separator));
162         route_group_combo.set_row_separator_func (sigc::mem_fun (*this, &AddRouteDialog::route_separator));
163         route_group_combo.signal_changed ().connect (sigc::mem_fun (*this, &AddRouteDialog::group_changed));
164
165         show_all_children ();
166
167         /* track template info will be managed whenever
168            this dialog is shown, via ::on_show()
169         */
170
171         add_button (Stock::CANCEL, RESPONSE_CANCEL);
172         add_button (Stock::ADD, RESPONSE_ACCEPT);
173
174         track_type_chosen ();
175 }
176
177 AddRouteDialog::~AddRouteDialog ()
178 {
179 }
180
181 void
182 AddRouteDialog::track_type_chosen ()
183 {
184         if (track()) {
185                 mode_label.set_text (_("Track mode:"));
186                 set_popdown_strings (mode_combo, track_mode_strings);
187                 mode_combo.set_sensitive (true);
188                 mode_combo.set_active_text (track_mode_strings.front());
189         } else {
190                 mode_combo.set_sensitive (false);
191         }
192 }
193
194 bool
195 AddRouteDialog::track ()
196 {
197         return track_bus_combo.get_active_row_number () == 0;
198 }
199
200 ARDOUR::DataType
201 AddRouteDialog::type ()
202 {
203         return (channel_combo.get_active_text() == _("MIDI"))
204                         ? ARDOUR::DataType::MIDI
205                         : ARDOUR::DataType::AUDIO;
206 }
207
208 string
209 AddRouteDialog::name_template ()
210 {
211         return name_template_entry.get_text ();
212 }
213
214 int
215 AddRouteDialog::count ()
216 {
217         return (int) floor (routes_adjustment.get_value ());
218 }
219
220 ARDOUR::TrackMode
221 AddRouteDialog::mode ()
222 {
223         if (ARDOUR::Profile->get_sae()) {
224                 return ARDOUR::Normal;
225         }
226
227         std::string str = mode_combo.get_active_text();
228         if (str == _("Normal")) {
229                 return ARDOUR::Normal;
230         } else if (str == _("Non Layered")){
231                 return ARDOUR::NonLayered;
232         } else if (str == _("Tape")) {
233                 return ARDOUR::Destructive;
234         } else {
235                 fatal << string_compose (X_("programming error: unknown track mode in add route dialog combo = %1"), str)
236                       << endmsg;
237                 /*NOTREACHED*/
238         }
239         /* keep gcc happy */
240         return ARDOUR::Normal;
241 }
242
243 int
244 AddRouteDialog::channels ()
245 {
246         string str = channel_combo.get_active_text();
247
248         for (ChannelSetups::iterator i = channel_setups.begin(); i != channel_setups.end(); ++i) {
249                 if (str == (*i).name) {
250                         return (*i).channels;
251                 }
252         }
253
254         return 0;
255 }
256
257 string
258 AddRouteDialog::track_template ()
259 {
260         string str = channel_combo.get_active_text();
261
262         for (ChannelSetups::iterator i = channel_setups.begin(); i != channel_setups.end(); ++i) {
263                 if (str == (*i).name) {
264                         return (*i).template_path;
265                 }
266         }
267
268         return string();
269 }
270
271 void
272 AddRouteDialog::on_show ()
273 {
274         refill_channel_setups ();
275         refill_route_groups ();
276
277         Dialog::on_show ();
278 }
279
280 void
281 AddRouteDialog::refill_channel_setups ()
282 {
283         ChannelSetup chn;
284
285         route_templates.clear ();
286         channel_combo_strings.clear ();
287         channel_setups.clear ();
288
289         chn.name = _("Mono");
290         chn.channels = 1;
291         channel_setups.push_back (chn);
292
293         chn.name = _("Stereo");
294         chn.channels = 2;
295         channel_setups.push_back (chn);
296
297         chn.name = "separator";
298         channel_setups.push_back (chn);
299
300         chn.name = _("MIDI");
301         chn.channels = 0;
302         channel_setups.push_back (chn);
303
304         chn.name = "separator";
305         channel_setups.push_back (chn);
306
307         ARDOUR::find_route_templates (route_templates);
308
309         if (!ARDOUR::Profile->get_sae()) {
310                 if (!route_templates.empty()) {
311                         vector<string> v;
312                         for (vector<TemplateInfo>::iterator x = route_templates.begin(); x != route_templates.end(); ++x) {
313                                 chn.name = x->name;
314                                 chn.channels = 0;
315                                 chn.template_path = x->path;
316                                 channel_setups.push_back (chn);
317                         }
318                 }
319
320                 /* clear template path for the rest */
321
322                 chn.template_path = "";
323
324                 chn.name = _("3 Channel");
325                 chn.channels = 3;
326                 channel_setups.push_back (chn);
327
328                 chn.name = _("4 Channel");
329                 chn.channels = 4;
330                 channel_setups.push_back (chn);
331
332                 chn.name = _("5 Channel");
333                 chn.channels = 5;
334                 channel_setups.push_back (chn);
335
336                 chn.name = _("6 Channel");
337                 chn.channels = 6;
338                 channel_setups.push_back (chn);
339
340                 chn.name = _("8 Channel");
341                 chn.channels = 8;
342                 channel_setups.push_back (chn);
343
344                 chn.name = _("12 Channel");
345                 chn.channels = 12;
346                 channel_setups.push_back (chn);
347
348                 chn.name = _("Custom");
349                 chn.channels = 0;
350                 channel_setups.push_back (chn);
351         }
352
353         for (ChannelSetups::iterator i = channel_setups.begin(); i != channel_setups.end(); ++i) {
354                 channel_combo_strings.push_back ((*i).name);
355         }
356
357         set_popdown_strings (channel_combo, channel_combo_strings, true);
358         channel_combo.set_active_text (channel_combo_strings.front());
359 }
360
361 void
362 AddRouteDialog::add_route_group (RouteGroup* g)
363 {
364         route_group_combo.insert_text (3, g->name ());
365 }
366
367 RouteGroup*
368 AddRouteDialog::route_group ()
369 {
370         if (route_group_combo.get_active_row_number () == 2) {
371                 return 0;
372         }
373
374         return _session->route_group_by_name (route_group_combo.get_active_text());
375 }
376
377 void
378 AddRouteDialog::refill_route_groups ()
379 {
380         route_group_combo.clear ();
381         route_group_combo.append_text (_("New group..."));
382
383         route_group_combo.append_text ("separator");
384
385         route_group_combo.append_text (_("No group"));
386
387         _session->foreach_route_group (sigc::mem_fun (*this, &AddRouteDialog::add_route_group));
388
389         route_group_combo.set_active (2);
390 }
391
392 void
393 AddRouteDialog::group_changed ()
394 {
395         if (_session && route_group_combo.get_active_text () == _("New group...")) {
396                 RouteGroup* g = new RouteGroup (*_session, "");
397
398                 PropertyList plist;
399                 plist.add (Properties::active, true);
400                 g->apply_changes (plist);
401
402                 RouteGroupDialog d (g, true);
403
404                 if (d.do_run ()) {
405                         delete g;
406                         route_group_combo.set_active (2);
407                 } else {
408                         _session->add_route_group (g);
409                         add_route_group (g);
410                         route_group_combo.set_active (3);
411                 }
412         }
413 }
414
415 bool
416 AddRouteDialog::channel_separator (const Glib::RefPtr<Gtk::TreeModel> &, const Gtk::TreeModel::iterator &i)
417 {
418         channel_combo.set_active (i);
419
420         return channel_combo.get_active_text () == "separator";
421 }
422
423 bool
424 AddRouteDialog::route_separator (const Glib::RefPtr<Gtk::TreeModel> &, const Gtk::TreeModel::iterator &i)
425 {
426         route_group_combo.set_active (i);
427
428         return route_group_combo.get_active_text () == "separator";
429 }
430