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