creating routes from templates now properly chooses between track & bus; fix up glitc...
[ardour.git] / gtk2_ardour / new_session_dialog.cc
1 /*
2     Copyright (C) 2005 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 <pbd/error.h>
21
22 #include <ardour/recent_sessions.h>
23 #include <ardour/session.h>
24 #include <ardour/profile.h>
25
26 #include <gtkmm/entry.h>
27 #include <gtkmm/filechooserbutton.h>
28 #include <gtkmm/spinbutton.h>
29 #include <gtkmm/checkbutton.h>
30 #include <gtkmm/radiobutton.h>
31 #include <gtkmm/filefilter.h>
32 #include <gtkmm/stock.h>
33 #include <gdkmm/cursor.h>
34
35 #include <gtkmm2ext/window_title.h>
36
37 using namespace Gtk;
38 using namespace Gtkmm2ext;
39 using namespace PBD;
40
41 #include "opts.h"
42 #include "utils.h"
43 #include "i18n.h"
44 #include "new_session_dialog.h"
45
46 NewSessionDialog::NewSessionDialog()
47         : ArdourDialog ("session control")
48 {
49         in_destructor = false;
50         session_name_label = new Gtk::Label(_("Name :"));
51         m_name = new Gtk::Entry();
52         m_name->set_text(ARDOUR_COMMAND_LINE::session_name);
53
54         chan_count_label_1 = new Gtk::Label(_("channels"));
55         chan_count_label_2 = new Gtk::Label(_("channels"));
56         chan_count_label_3 = new Gtk::Label(_("channels"));
57         chan_count_label_4 = new Gtk::Label(_("channels"));
58
59         chan_count_label_1->set_alignment(0,0.5);
60         chan_count_label_1->set_padding(0,0);
61         chan_count_label_1->set_line_wrap(false);
62
63         chan_count_label_2->set_alignment(0,0.5);
64         chan_count_label_2->set_padding(0,0);
65         chan_count_label_2->set_line_wrap(false);
66
67         chan_count_label_3->set_alignment(0,0.5);
68         chan_count_label_3->set_padding(0,0);
69         chan_count_label_3->set_line_wrap(false);
70
71         chan_count_label_4->set_alignment(0,0.5);
72         chan_count_label_4->set_padding(0,0);
73         chan_count_label_4->set_line_wrap(false);
74
75         bus_label = new Gtk::Label(_("<b>Busses</b>"));
76         input_label = new Gtk::Label(_("<b>Inputs</b>"));
77         output_label = new Gtk::Label(_("<b>Outputs</b>"));
78
79         session_location_label = new Gtk::Label(_("Create Folder In :"));
80         m_folder = new Gtk::FileChooserButton(Gtk::FILE_CHOOSER_ACTION_SELECT_FOLDER);
81         session_template_label = new Gtk::Label(_("Template :"));
82         m_template = new Gtk::FileChooserButton();
83         m_create_control_bus = new Gtk::CheckButton(_("Create Monitor Bus"));
84         
85         Gtk::Adjustment *m_control_bus_channel_count_adj = Gtk::manage(new Gtk::Adjustment(2, 0, 100, 1, 10, 10));
86         m_control_bus_channel_count = new Gtk::SpinButton(*m_control_bus_channel_count_adj, 1, 0);
87         
88         Gtk::Adjustment *m_master_bus_channel_count_adj = Gtk::manage(new Gtk::Adjustment(2, 0, 100, 1, 10, 10));
89         m_master_bus_channel_count = new Gtk::SpinButton(*m_master_bus_channel_count_adj, 1, 0);
90         m_create_master_bus = new Gtk::CheckButton(_("Create Master Bus"));
91         advanced_table = new Gtk::Table(2, 2, true);
92         m_connect_inputs = new Gtk::CheckButton(_("Automatically Connect to Physical Inputs"));
93         m_limit_input_ports = new Gtk::CheckButton(_("Use only"));
94         
95         Gtk::Adjustment *m_input_limit_count_adj = Gtk::manage(new Gtk::Adjustment(1, 0, 100, 1, 10, 10));
96         m_input_limit_count = new Gtk::SpinButton(*m_input_limit_count_adj, 1, 0);
97         input_port_limit_hbox = new Gtk::HBox(false, 0);
98         input_port_vbox = new Gtk::VBox(false, 0);
99         input_table = new Gtk::Table(2, 2, false);
100
101         bus_frame = new Gtk::Frame();
102         bus_table = new Gtk::Table (2, 3, false);
103         
104         input_frame = new Gtk::Frame();
105         m_connect_outputs = new Gtk::CheckButton(_("Automatically Connect Outputs"));
106         m_limit_output_ports = new Gtk::CheckButton(_("Use only"));
107         
108         Gtk::Adjustment *m_output_limit_count_adj = Gtk::manage(new Gtk::Adjustment(1, 0, 100, 1, 10, 10));
109         m_output_limit_count = new Gtk::SpinButton(*m_output_limit_count_adj, 1, 0);
110         output_port_limit_hbox = new Gtk::HBox(false, 0);
111         output_port_vbox = new Gtk::VBox(false, 0);
112         
113         Gtk::RadioButton::Group _RadioBGroup_m_connect_outputs_to_master;
114         m_connect_outputs_to_master = new Gtk::RadioButton(_RadioBGroup_m_connect_outputs_to_master, _("... to Master Bus"));
115         m_connect_outputs_to_physical = new Gtk::RadioButton(_RadioBGroup_m_connect_outputs_to_master, _("... to Physical Outputs"));
116         output_conn_vbox = new Gtk::VBox(false, 0);
117         output_vbox = new Gtk::VBox(false, 0);
118
119         output_frame = new Gtk::Frame();
120         advanced_vbox = new Gtk::VBox(false, 0);
121         advanced_label = new Gtk::Label(_("Advanced Options"));
122         advanced_expander = new Gtk::Expander();
123         new_session_table = new Gtk::Table(2, 2, false);
124         m_open_filechooser = new Gtk::FileChooserButton();
125         open_session_hbox = new Gtk::HBox(false, 0);
126         m_treeview = new Gtk::TreeView();
127         recent_scrolledwindow = new Gtk::ScrolledWindow();
128
129         recent_sesion_label = new Gtk::Label(_("Recent:"));
130         recent_frame = new Gtk::Frame();
131         open_session_vbox = new Gtk::VBox(false, 0);
132         m_notebook = new Gtk::Notebook();
133         session_name_label->set_alignment(0, 0.5);
134         session_name_label->set_padding(6,0);
135         session_name_label->set_line_wrap(false);
136         session_name_label->set_selectable(false);
137         m_name->set_editable(true);
138         m_name->set_max_length(0);
139         m_name->set_has_frame(true);
140         m_name->set_activates_default(true);
141         m_name->set_width_chars (40);
142         session_location_label->set_alignment(0,0.5);
143         session_location_label->set_padding(6,0);
144         session_location_label->set_line_wrap(false);
145         session_location_label->set_selectable(false);
146         session_template_label->set_alignment(0,0.5);
147         session_template_label->set_padding(6,0);
148         session_template_label->set_line_wrap(false);
149         session_template_label->set_selectable(false);
150         m_create_control_bus->set_flags(Gtk::CAN_FOCUS);
151         m_create_control_bus->set_relief(Gtk::RELIEF_NORMAL);
152         m_create_control_bus->set_mode(true);
153         m_create_control_bus->set_active(false);
154         m_create_control_bus->set_border_width(0);
155         m_control_bus_channel_count->set_flags(Gtk::CAN_FOCUS);
156         m_control_bus_channel_count->set_update_policy(Gtk::UPDATE_ALWAYS);
157         m_control_bus_channel_count->set_numeric(true);
158         m_control_bus_channel_count->set_digits(0);
159         m_control_bus_channel_count->set_wrap(false);
160         m_control_bus_channel_count->set_sensitive(false);
161         m_master_bus_channel_count->set_flags(Gtk::CAN_FOCUS);
162         m_master_bus_channel_count->set_update_policy(Gtk::UPDATE_ALWAYS);
163         m_master_bus_channel_count->set_numeric(true);
164         m_master_bus_channel_count->set_digits(0);
165         m_master_bus_channel_count->set_wrap(false);
166         open_session_file_label = new Gtk::Label(_("Browse:"));
167         open_session_file_label->set_alignment(0, 0.5);
168         m_create_master_bus->set_flags(Gtk::CAN_FOCUS);
169         m_create_master_bus->set_relief(Gtk::RELIEF_NORMAL);
170         m_create_master_bus->set_mode(true);
171         m_create_master_bus->set_active(true);
172         m_create_master_bus->set_border_width(0);
173         advanced_table->set_row_spacings(0);
174         advanced_table->set_col_spacings(0);
175         
176         m_connect_inputs->set_flags(Gtk::CAN_FOCUS);
177         m_connect_inputs->set_relief(Gtk::RELIEF_NORMAL);
178         m_connect_inputs->set_mode(true);
179         m_connect_inputs->set_active(true);
180         m_connect_inputs->set_border_width(0);
181
182         m_limit_input_ports->set_flags(Gtk::CAN_FOCUS);
183         m_limit_input_ports->set_relief(Gtk::RELIEF_NORMAL);
184         m_limit_input_ports->set_mode(true);
185         m_limit_input_ports->set_sensitive(true);
186         m_limit_input_ports->set_border_width(0);
187         m_input_limit_count->set_flags(Gtk::CAN_FOCUS);
188         m_input_limit_count->set_update_policy(Gtk::UPDATE_ALWAYS);
189         m_input_limit_count->set_numeric(true);
190         m_input_limit_count->set_digits(0);
191         m_input_limit_count->set_wrap(false);
192         m_input_limit_count->set_sensitive(false);
193
194         bus_hbox = new Gtk::HBox (false, 0);
195         bus_hbox->pack_start (*bus_table, Gtk::PACK_SHRINK, 18);
196
197         bus_label->set_alignment(0, 0.5);
198         bus_label->set_padding(0,0);
199         bus_label->set_line_wrap(false);
200         bus_label->set_selectable(false);
201         bus_label->set_use_markup(true);
202         bus_frame->set_shadow_type(Gtk::SHADOW_NONE);
203         bus_frame->set_label_align(0,0.5);
204         bus_frame->add(*bus_hbox);
205         bus_frame->set_label_widget(*bus_label);
206         
207         bus_table->set_row_spacings (0);
208         bus_table->set_col_spacings (0);
209         bus_table->attach (*m_create_master_bus, 0, 1, 0, 1, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
210         bus_table->attach (*m_master_bus_channel_count, 1, 2, 0, 1, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
211         bus_table->attach (*chan_count_label_1, 2, 3, 0, 1, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 6, 0);
212         bus_table->attach (*m_create_control_bus, 0, 1, 1, 2, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
213         bus_table->attach (*m_control_bus_channel_count, 1, 2, 1, 2, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
214         bus_table->attach (*chan_count_label_2, 2, 3, 1, 2, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 6, 0);
215
216         input_port_limit_hbox->pack_start(*m_limit_input_ports, Gtk::PACK_SHRINK, 6);
217         input_port_limit_hbox->pack_start(*m_input_limit_count, Gtk::PACK_SHRINK, 0);
218         input_port_limit_hbox->pack_start(*chan_count_label_3, Gtk::PACK_SHRINK, 6);
219         input_port_vbox->pack_start(*m_connect_inputs, Gtk::PACK_SHRINK, 0);
220         input_port_vbox->pack_start(*input_port_limit_hbox, Gtk::PACK_EXPAND_PADDING, 0);
221         input_table->set_row_spacings(0);
222         input_table->set_col_spacings(0);
223         input_table->attach(*input_port_vbox, 0, 1, 0, 1, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 6, 6);
224
225         input_hbox = new Gtk::HBox (false, 0);
226         input_hbox->pack_start (*input_table, Gtk::PACK_SHRINK, 18);
227
228         input_label->set_alignment(0, 0.5);
229         input_label->set_padding(0,0);
230         input_label->set_line_wrap(false);
231         input_label->set_selectable(false);
232         input_label->set_use_markup(true);
233         input_frame->set_shadow_type(Gtk::SHADOW_NONE);
234         input_frame->set_label_align(0,0.5);
235         input_frame->add(*input_hbox);
236         input_frame->set_label_widget(*input_label);
237
238         m_connect_outputs->set_flags(Gtk::CAN_FOCUS);
239         m_connect_outputs->set_relief(Gtk::RELIEF_NORMAL);
240         m_connect_outputs->set_mode(true);
241         m_connect_outputs->set_active(true);
242         m_connect_outputs->set_border_width(0);
243         m_limit_output_ports->set_flags(Gtk::CAN_FOCUS);
244         m_limit_output_ports->set_relief(Gtk::RELIEF_NORMAL);
245         m_limit_output_ports->set_mode(true);
246         m_limit_output_ports->set_sensitive(true);
247         m_limit_output_ports->set_border_width(0);
248         m_output_limit_count->set_flags(Gtk::CAN_FOCUS);
249         m_output_limit_count->set_update_policy(Gtk::UPDATE_ALWAYS);
250         m_output_limit_count->set_numeric(false);
251         m_output_limit_count->set_digits(0);
252         m_output_limit_count->set_wrap(false);
253         m_output_limit_count->set_sensitive(false);
254         output_port_limit_hbox->pack_start(*m_limit_output_ports, Gtk::PACK_SHRINK, 6);
255         output_port_limit_hbox->pack_start(*m_output_limit_count, Gtk::PACK_SHRINK, 0);
256         output_port_limit_hbox->pack_start(*chan_count_label_4, Gtk::PACK_SHRINK, 6);
257         m_connect_outputs_to_master->set_flags(Gtk::CAN_FOCUS);
258         m_connect_outputs_to_master->set_relief(Gtk::RELIEF_NORMAL);
259         m_connect_outputs_to_master->set_mode(true);
260         m_connect_outputs_to_master->set_active(false);
261         m_connect_outputs_to_master->set_border_width(0);
262         m_connect_outputs_to_physical->set_flags(Gtk::CAN_FOCUS);
263         m_connect_outputs_to_physical->set_relief(Gtk::RELIEF_NORMAL);
264         m_connect_outputs_to_physical->set_mode(true);
265         m_connect_outputs_to_physical->set_active(false);
266         m_connect_outputs_to_physical->set_border_width(0);
267         output_conn_vbox->pack_start(*m_connect_outputs, Gtk::PACK_SHRINK, 0);
268         output_conn_vbox->pack_start(*m_connect_outputs_to_master, Gtk::PACK_SHRINK, 0);
269         output_conn_vbox->pack_start(*m_connect_outputs_to_physical, Gtk::PACK_SHRINK, 0);
270         output_vbox->set_border_width(6);
271
272         output_port_vbox->pack_start(*output_port_limit_hbox, Gtk::PACK_SHRINK, 0);
273
274         output_vbox->pack_start(*output_conn_vbox);
275         output_vbox->pack_start(*output_port_vbox);
276
277         output_label->set_alignment(0, 0.5);
278         output_label->set_padding(0,0);
279         output_label->set_line_wrap(false);
280         output_label->set_selectable(false);
281         output_label->set_use_markup(true);
282         output_frame->set_shadow_type(Gtk::SHADOW_NONE);
283         output_frame->set_label_align(0,0.5);
284
285         output_hbox = new Gtk::HBox (false, 0);
286         output_hbox->pack_start (*output_vbox, Gtk::PACK_SHRINK, 18);
287
288         output_frame->add(*output_hbox);
289         output_frame->set_label_widget(*output_label);
290
291         advanced_vbox->pack_start(*advanced_table, Gtk::PACK_SHRINK, 0);
292         advanced_vbox->pack_start(*bus_frame, Gtk::PACK_SHRINK, 6);
293         advanced_vbox->pack_start(*input_frame, Gtk::PACK_SHRINK, 6);
294         advanced_vbox->pack_start(*output_frame, Gtk::PACK_SHRINK, 0);
295         advanced_label->set_padding(0,0);
296         advanced_label->set_line_wrap(false);
297         advanced_label->set_selectable(false);
298         advanced_label->set_alignment(0, 0.5);
299         advanced_expander->set_flags(Gtk::CAN_FOCUS);
300         advanced_expander->set_border_width(0);
301         advanced_expander->set_expanded(false);
302         advanced_expander->set_spacing(0);
303         advanced_expander->add(*advanced_vbox);
304         advanced_expander->set_label_widget(*advanced_label);
305         new_session_table->set_border_width(12);
306         new_session_table->set_row_spacings(6);
307         new_session_table->set_col_spacings(0);
308         new_session_table->attach(*session_name_label, 0, 1, 0, 1, Gtk::FILL, Gtk::FILL, 0, 0);
309         new_session_table->attach(*m_name, 1, 2, 0, 1, Gtk::EXPAND|Gtk::FILL, Gtk::FILL, 0, 0);
310         new_session_table->attach(*session_location_label, 0, 1, 1, 2, Gtk::FILL, Gtk::FILL, 0, 0);
311         new_session_table->attach(*m_folder, 1, 2, 1, 2, Gtk::EXPAND|Gtk::FILL, Gtk::FILL, 0, 0);
312         new_session_table->attach(*session_template_label, 0, 1, 2, 3, Gtk::FILL, Gtk::FILL, 0, 0);
313         new_session_table->attach(*m_template, 1, 2, 2, 3, Gtk::EXPAND|Gtk::FILL, Gtk::FILL, 0, 0);
314
315         if (!ARDOUR::Profile->get_sae()) {
316                 new_session_table->attach(*advanced_expander, 0, 2, 3, 4, Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 6);
317         }
318
319         open_session_hbox->pack_start(*open_session_file_label, false, false, 12);
320         open_session_hbox->pack_start(*m_open_filechooser, true, true, 12);
321         m_treeview->set_flags(Gtk::CAN_FOCUS);
322         m_treeview->set_headers_visible(true);
323         m_treeview->set_rules_hint(false);
324         m_treeview->set_reorderable(false);
325         m_treeview->set_enable_search(true);
326         m_treeview->set_fixed_height_mode(false);
327         m_treeview->set_hover_selection(false);
328         m_treeview->set_size_request(-1, 150);
329         recent_scrolledwindow->set_flags(Gtk::CAN_FOCUS);
330         recent_scrolledwindow->set_border_width(6);
331         recent_scrolledwindow->set_shadow_type(Gtk::SHADOW_IN);
332         recent_scrolledwindow->set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
333         recent_scrolledwindow->property_window_placement().set_value(Gtk::CORNER_TOP_LEFT);
334         recent_scrolledwindow->add(*m_treeview);
335
336         recent_sesion_label->set_padding(0,0);
337         recent_sesion_label->set_line_wrap(false);
338         recent_sesion_label->set_selectable(false);
339         recent_frame->set_border_width(12);
340         recent_frame->set_shadow_type(Gtk::SHADOW_NONE);
341         recent_frame->add(*recent_scrolledwindow);
342         recent_frame->set_label_widget(*recent_sesion_label);
343         open_session_vbox->pack_start(*recent_frame, Gtk::PACK_EXPAND_WIDGET, 0);
344         open_session_vbox->pack_start(*open_session_hbox, Gtk::PACK_SHRINK, 12);
345
346         m_notebook->set_flags(Gtk::CAN_FOCUS);
347         m_notebook->set_scrollable(true);
348         
349         get_vbox()->set_homogeneous(false);
350         get_vbox()->set_spacing(0);
351         get_vbox()->pack_start(*m_notebook, Gtk::PACK_SHRINK, 0);
352
353         /* 
354            icon setting is done again in the editor (for the whole app),
355            but its all chickens and eggs at this point.
356         */
357
358         list<Glib::RefPtr<Gdk::Pixbuf> > window_icons;
359         Glib::RefPtr<Gdk::Pixbuf> icon;
360
361         if ((icon = ::get_icon ("ardour_icon_16px")) != 0) {
362                 window_icons.push_back (icon);
363         }
364         if ((icon = ::get_icon ("ardour_icon_22px")) != 0) {
365                 window_icons.push_back (icon);
366         }
367         if ((icon = ::get_icon ("ardour_icon_32px")) != 0) {
368                 window_icons.push_back (icon);
369         }
370         if ((icon = ::get_icon ("ardour_icon_48px")) != 0) {
371                 window_icons.push_back (icon);
372         }
373         if (!window_icons.empty()) {
374                 set_icon_list (window_icons);
375         }
376
377         WindowTitle title(Glib::get_application_name());
378         title += _("Session Control");
379         set_title(title.get_string());
380
381         set_position (Gtk::WIN_POS_MOUSE);
382         set_resizable(false);
383         set_has_separator(false);
384         add_button(Gtk::Stock::QUIT, Gtk::RESPONSE_CANCEL);
385         add_button(Gtk::Stock::CLEAR, Gtk::RESPONSE_NONE);
386         m_okbutton = add_button(Gtk::Stock::NEW, Gtk::RESPONSE_OK);
387
388         recent_model = Gtk::TreeStore::create (recent_columns);
389         m_treeview->set_model (recent_model);
390         m_treeview->append_column (_("Recent Sessions"), recent_columns.visible_name);
391         m_treeview->set_headers_visible (false);
392         m_treeview->get_selection()->set_mode (Gtk::SELECTION_SINGLE);
393
394         std::string path = ARDOUR::get_user_ardour_path();
395         
396         if (path.empty()) {
397                 path = ARDOUR::get_system_data_path();
398         }
399
400         const char * const template_dir_name = X_("templates");
401
402         if (!path.empty()) {
403                 string user_template_path = path + template_dir_name;
404
405                 if (Glib::file_test(user_template_path, Glib::FILE_TEST_IS_DIR))
406                 {
407                         m_template->set_current_folder (user_template_path);
408                 }
409         }
410
411         const std::string sys_templates_dir = ARDOUR::get_system_data_path() + template_dir_name;
412         
413         if (Glib::file_test(sys_templates_dir, Glib::FILE_TEST_IS_DIR))
414         {
415                 m_template->add_shortcut_folder(sys_templates_dir);
416         }
417
418         m_template->set_title(_("select template"));
419         Gtk::FileFilter* session_filter = manage (new (Gtk::FileFilter));
420         session_filter->add_pattern(X_("*.ardour"));
421         session_filter->add_pattern(X_("*.ardour.bak"));
422         m_open_filechooser->set_filter (*session_filter);
423         m_open_filechooser->set_current_folder(getenv ("HOME"));
424         m_open_filechooser->set_title(_("select session file"));
425
426         Gtk::FileFilter* template_filter = manage (new (Gtk::FileFilter));
427         template_filter->add_pattern(X_("*.ardour"));
428         template_filter->add_pattern(X_("*.ardour.bak"));
429         template_filter->add_pattern(X_("*.template"));
430         m_template->set_filter (*template_filter);
431
432         m_folder->set_current_folder(getenv ("HOME"));
433         m_folder->set_title(_("select directory"));
434
435         on_new_session_page = true;
436         m_notebook->set_current_page(0);
437         m_notebook->show();
438         m_notebook->show_all_children();
439
440         engine_page_session_folder = X_("");
441         engine_page_session_name = X_("");
442
443         set_default_response (Gtk::RESPONSE_OK);
444         if (!ARDOUR_COMMAND_LINE::session_name.length()) {
445                 set_response_sensitive (Gtk::RESPONSE_OK, false);
446                 set_response_sensitive (Gtk::RESPONSE_NONE, false);
447         } else {
448                 set_response_sensitive (Gtk::RESPONSE_OK, true);
449                 set_response_sensitive (Gtk::RESPONSE_NONE, true);
450         }
451
452         ///@ connect some signals
453
454         m_connect_inputs->signal_clicked().connect (mem_fun (*this, &NewSessionDialog::connect_inputs_clicked));
455         m_connect_outputs->signal_clicked().connect (mem_fun (*this, &NewSessionDialog::connect_outputs_clicked));
456         m_limit_input_ports->signal_clicked().connect (mem_fun (*this, &NewSessionDialog::limit_inputs_clicked));
457         m_limit_output_ports->signal_clicked().connect (mem_fun (*this, &NewSessionDialog::limit_outputs_clicked));
458         m_create_master_bus->signal_clicked().connect (mem_fun (*this, &NewSessionDialog::master_bus_button_clicked));
459         m_create_control_bus->signal_clicked().connect (mem_fun (*this, &NewSessionDialog::monitor_bus_button_clicked));
460         m_name->signal_changed().connect(mem_fun (*this, &NewSessionDialog::on_new_session_name_entry_changed));
461         m_notebook->signal_switch_page().connect (mem_fun (*this, &NewSessionDialog::notebook_page_changed));
462         m_treeview->get_selection()->signal_changed().connect (mem_fun (*this, &NewSessionDialog::treeview_selection_changed));
463         m_treeview->signal_row_activated().connect (mem_fun (*this, &NewSessionDialog::recent_row_activated));
464         m_open_filechooser->signal_selection_changed ().connect (mem_fun (*this, &NewSessionDialog::file_chosen));
465         m_template->signal_selection_changed ().connect (mem_fun (*this, &NewSessionDialog::template_chosen));
466         
467         page_set = Pages (0);
468 }
469
470 NewSessionDialog::~NewSessionDialog()
471 {
472         in_destructor = true;
473 }
474
475 int
476 NewSessionDialog::run ()
477 {
478         if (!page_set) {
479                 /* nothing to display */
480                 return Gtk::RESPONSE_OK;
481         }
482         if (!(page_set & NewPage) && !(page_set & OpenPage)) {
483                 set_response_sensitive (Gtk::RESPONSE_OK, true);
484         }
485         return ArdourDialog::run ();
486 }
487
488 void
489 NewSessionDialog::set_have_engine (bool yn)
490 {
491         if (yn) {
492                 m_notebook->remove_page (engine_control);
493                 page_set = Pages (page_set & ~EnginePage);
494         } else {
495                 if (!(page_set & EnginePage)) {
496                         m_notebook->append_page (engine_control, _("Audio Setup"));
497                         m_notebook->show_all_children();
498                         page_set = Pages (page_set | EnginePage);
499                 }
500         }
501 }
502
503 void
504 NewSessionDialog::set_existing_session (bool yn)
505 {
506         if (yn) {
507
508                 if (page_set & NewPage) {
509                         m_notebook->remove_page (*new_session_table);
510                         page_set = Pages (page_set & ~NewPage);
511                 }
512
513                 if (page_set & OpenPage) {
514                         m_notebook->remove_page (*open_session_vbox);
515                         page_set = Pages (page_set & ~OpenPage);
516                 }
517
518         } else {
519                 if (!(page_set & NewPage)) {
520                         m_notebook->append_page(*new_session_table, _("New Session"));
521                         m_notebook->pages().back().set_tab_label_packing(false, true, Gtk::PACK_START);
522                         page_set = Pages (page_set | NewPage);
523                 }
524                 if (!(page_set & OpenPage)) {
525                         m_notebook->append_page(*open_session_vbox, _("Open Session"));
526                         m_notebook->pages().back().set_tab_label_packing(false, true, Gtk::PACK_START);
527                         page_set = Pages (page_set | OpenPage);
528                 }
529
530                 m_notebook->show_all_children();
531         }
532 }
533
534 void
535 NewSessionDialog::set_session_name (const Glib::ustring& name)
536 {
537         m_name->set_text (name);
538         engine_page_session_name = name;
539 }
540
541 void
542 NewSessionDialog::set_session_folder(const Glib::ustring& dir)
543 {
544         Glib::ustring realdir = dir;
545
546         /* this little tangled mess is a result of 4 things:
547
548             1) GtkFileChooser vomits when given a non-absolute directory
549                    argument to set_current_folder()
550             2) canonicalize_file_name() doesn't exist on OS X
551             3) linux man page for realpath() says "do not use this function"
552             4) canonicalize_file_name() & realpath() have entirely
553                    different semantics on OS X and Linux when given
554                    a non-existent path.
555                    
556            as result of all this, we take two distinct pathways through the code.
557         */
558
559
560 #ifdef __APPLE__
561
562         char buf[PATH_MAX];
563
564         if(realpath (dir.c_str(), buf) != 0) {
565                 if (!Glib::file_test (dir, Glib::FILE_TEST_IS_DIR)) {
566                         realdir = Glib::path_get_dirname (realdir);
567                 }
568                 m_folder->set_current_folder (realdir);
569                 engine_page_session_folder = realdir;
570         }
571
572         
573 #else 
574         char* res;
575         if (!Glib::file_test (dir, Glib::FILE_TEST_IS_DIR)) {
576                 realdir = Glib::path_get_dirname (realdir);
577                 cerr << "didn't exist, use " << realdir << endl;
578         }
579
580         if ((res = canonicalize_file_name (realdir.c_str())) != 0) {
581                 cerr << "canonical, use " << res << endl;
582                 m_folder->set_current_folder (res);
583                 free (res);
584         }
585         
586 #endif
587
588 }
589
590 std::string
591 NewSessionDialog::session_name() const
592 {
593         std::string str = Glib::filename_from_utf8(m_open_filechooser->get_filename());
594         std::string::size_type position = str.find_last_of ('/');
595         str = str.substr (position+1);
596         position = str.find_last_of ('.');
597         str = str.substr (0, position);
598
599         /*
600           XXX what to do if it's a .bak file?
601           load_session doesn't allow it!
602
603         if ((position = str.rfind(".bak")) != string::npos) {
604                 str = str.substr (0, position);
605         }         
606         */
607
608         switch (which_page()) {
609         case NewPage:
610         case EnginePage:
611                 /* new or audio setup pages */
612                 if (!(page_set & OpenPage) && !(page_set & NewPage)) {
613                         return Glib::filename_from_utf8(engine_page_session_name);
614                 }
615                 return Glib::filename_from_utf8(m_name->get_text());
616         default:
617                 break;
618         } 
619
620         if (m_treeview->get_selection()->count_selected_rows() == 0) {
621                 return Glib::filename_from_utf8(str);
622         }
623         Gtk::TreeModel::iterator i = m_treeview->get_selection()->get_selected();
624         return (*i)[recent_columns.visible_name];
625 }
626
627 std::string
628 NewSessionDialog::session_folder() const
629 {
630         switch (which_page()) {
631         case NewPage:
632                 return Glib::filename_from_utf8(m_folder->get_filename());
633                 
634         case EnginePage:
635                 if (!(page_set & OpenPage) && !(page_set & NewPage)) {
636                         /* just engine page, nothing else */
637                         return Glib::filename_from_utf8(engine_page_session_folder);
638                 }
639                 if (page_set == EnginePage) {
640                         /* use m_folder since it should be set */
641                         return Glib::filename_from_utf8(m_folder->get_filename());
642                 }
643                 break;
644
645         default:
646                 break;
647         }
648                
649         if (m_treeview->get_selection()->count_selected_rows() == 0) {
650                 const string filename(Glib::filename_from_utf8(m_open_filechooser->get_filename()));
651                 return Glib::path_get_dirname(filename);
652         }
653
654         Gtk::TreeModel::iterator i = m_treeview->get_selection()->get_selected();
655         return (*i)[recent_columns.fullpath];
656 }
657
658 bool
659 NewSessionDialog::use_session_template() const
660 {
661         if(m_template->get_filename().empty() && (which_page() == NewPage)) return false;
662         return true;
663 }
664
665 std::string
666 NewSessionDialog::session_template_name() const
667 {
668         return Glib::filename_from_utf8(m_template->get_filename());
669 }
670
671 bool
672 NewSessionDialog::create_master_bus() const
673 {
674         return m_create_master_bus->get_active();
675 }
676
677 int
678 NewSessionDialog::master_channel_count() const
679 {
680         return m_master_bus_channel_count->get_value_as_int();
681 }
682
683 bool
684 NewSessionDialog::create_control_bus() const
685 {
686         return m_create_control_bus->get_active();
687 }
688
689 int
690 NewSessionDialog::control_channel_count() const
691 {
692         return m_control_bus_channel_count->get_value_as_int();
693 }
694
695 bool
696 NewSessionDialog::connect_inputs() const
697 {
698         return m_connect_inputs->get_active();
699 }
700
701 bool
702 NewSessionDialog::limit_inputs_used_for_connection() const
703 {
704         return m_limit_input_ports->get_active();
705 }
706
707 int
708 NewSessionDialog::input_limit_count() const
709 {
710         return m_input_limit_count->get_value_as_int();
711 }
712
713 bool
714 NewSessionDialog::connect_outputs() const
715 {
716         return m_connect_outputs->get_active();
717 }
718
719 bool
720 NewSessionDialog::limit_outputs_used_for_connection() const
721 {
722         return m_limit_output_ports->get_active();
723 }
724
725 int
726 NewSessionDialog::output_limit_count() const
727 {
728         return m_output_limit_count->get_value_as_int();
729 }
730
731 bool
732 NewSessionDialog::connect_outs_to_master() const
733 {
734         return m_connect_outputs_to_master->get_active();
735 }
736
737 bool
738 NewSessionDialog::connect_outs_to_physical() const
739 {
740         return m_connect_outputs_to_physical->get_active();
741 }
742
743 int
744 NewSessionDialog::get_current_page()
745 {
746         return m_notebook->get_current_page();
747 }
748
749 NewSessionDialog::Pages
750 NewSessionDialog::which_page () const
751 {
752         int num = m_notebook->get_current_page();
753
754         if (page_set == NewPage) {
755                 return NewPage;
756
757         } else if (page_set == OpenPage) {
758                 return OpenPage;
759
760         } else if (page_set == EnginePage) {
761                 return EnginePage;
762
763         } else if (page_set == (NewPage|OpenPage)) {
764                 switch (num) {
765                 case 0:
766                         return NewPage;
767                 default:
768                         return OpenPage;
769                 }
770
771         } else if (page_set == (NewPage|EnginePage)) {
772                 switch (num) {
773                 case 0:
774                         return NewPage;
775                 default:
776                         return EnginePage;
777                 }
778
779         } else if (page_set == (NewPage|EnginePage|OpenPage)) {
780                 switch (num) {
781                 case 0:
782                         return NewPage;
783                 case 1:
784                         return OpenPage;
785                 default:
786                         return EnginePage;
787                 }
788
789         } else if (page_set == (OpenPage|EnginePage)) {
790                 switch (num) {
791                 case 0:
792                         return OpenPage;
793                 default:
794                         return EnginePage;
795                 }
796         }
797
798         return NewPage; /* shouldn't get here */
799 }
800
801 void
802 NewSessionDialog::set_current_page(int page)
803 {
804         return m_notebook->set_current_page (page);
805 }
806
807 void
808 NewSessionDialog::reset_name()
809 {
810         m_name->set_text("");
811         set_response_sensitive (Gtk::RESPONSE_OK, false);
812         
813 }
814
815 void
816 NewSessionDialog::on_new_session_name_entry_changed ()
817 {
818         if (m_name->get_text() != "") {
819                 set_response_sensitive (Gtk::RESPONSE_OK, true);
820                 set_response_sensitive (Gtk::RESPONSE_NONE, true);
821         } else {
822                 set_response_sensitive (Gtk::RESPONSE_OK, false);
823         }
824 }
825
826 void
827 NewSessionDialog::notebook_page_changed (GtkNotebookPage* np, uint pagenum)
828 {
829         if (in_destructor) {
830                 return;
831         }
832
833         switch (which_page()) {
834         case OpenPage:
835                 on_new_session_page = false;
836                 m_okbutton->set_label(_("Open"));
837                 m_okbutton->set_image (*(manage (new Gtk::Image (Gtk::Stock::OPEN, Gtk::ICON_SIZE_BUTTON))));
838                 set_response_sensitive (Gtk::RESPONSE_NONE, false);
839                 if (m_treeview->get_selection()->count_selected_rows() == 0) {
840                         set_response_sensitive (Gtk::RESPONSE_OK, false);
841                 } else {
842                         set_response_sensitive (Gtk::RESPONSE_OK, true);
843                 }
844                 break;
845
846         case EnginePage:
847                 on_new_session_page = false;
848                 m_okbutton->set_label(_("Open"));
849                 m_okbutton->set_image (*(manage (new Gtk::Image (Gtk::Stock::OPEN, Gtk::ICON_SIZE_BUTTON))));
850                 set_response_sensitive (Gtk::RESPONSE_NONE, false);
851                 set_response_sensitive (Gtk::RESPONSE_OK, true);
852                 break;
853
854         default:
855                 on_new_session_page = true;
856                 m_okbutton->set_label(_("New"));
857                 m_okbutton->set_image (*(new Gtk::Image (Gtk::Stock::NEW, Gtk::ICON_SIZE_BUTTON)));
858                 if (m_name->get_text() == "") {
859                         set_response_sensitive (Gtk::RESPONSE_OK, false);
860                         m_name->grab_focus();
861                 } else {
862                         set_response_sensitive (Gtk::RESPONSE_OK, true);
863                 }
864         }
865 }
866
867 void
868 NewSessionDialog::treeview_selection_changed ()
869 {
870         if (m_treeview->get_selection()->count_selected_rows() == 0) {
871                 if (!m_open_filechooser->get_filename().empty()) {
872                         set_response_sensitive (Gtk::RESPONSE_OK, true);
873                 } else {
874                         set_response_sensitive (Gtk::RESPONSE_OK, false);
875                 }
876         } else {
877                 set_response_sensitive (Gtk::RESPONSE_OK, true);
878         }
879 }
880
881 void
882 NewSessionDialog::file_chosen ()
883 {
884         switch (which_page()) {
885       case OpenPage:
886          break;
887            case NewPage:
888            case EnginePage:
889                    return;
890         }
891
892         m_treeview->get_selection()->unselect_all();
893
894         Glib::RefPtr<Gdk::Window> win (get_window());
895
896         if (win) {
897                 win->set_cursor(Gdk::Cursor(Gdk::WATCH));
898         }
899
900         if (!m_open_filechooser->get_filename().empty()) {
901                 set_response_sensitive (Gtk::RESPONSE_OK, true);
902                 response (Gtk::RESPONSE_OK);
903         } else {
904                 set_response_sensitive (Gtk::RESPONSE_OK, false);
905         }
906 }
907
908 void
909 NewSessionDialog::template_chosen ()
910 {
911         if (m_template->get_filename() != "" ) {;
912                 set_response_sensitive (Gtk::RESPONSE_NONE, true);
913         } else {
914                 set_response_sensitive (Gtk::RESPONSE_NONE, false);
915         }
916 }
917
918 void
919 NewSessionDialog::recent_row_activated (const Gtk::TreePath& path, Gtk::TreeViewColumn* col)
920 {
921         response (Gtk::RESPONSE_OK);
922 }
923
924 void
925 NewSessionDialog::connect_inputs_clicked ()
926 {
927         m_limit_input_ports->set_sensitive(m_connect_inputs->get_active());
928
929                 if (m_connect_inputs->get_active() && m_limit_input_ports->get_active()) {
930                 m_input_limit_count->set_sensitive(true);
931                 } else {
932                 m_input_limit_count->set_sensitive(false);
933                 }
934 }
935
936 void
937 NewSessionDialog::connect_outputs_clicked ()
938 {
939         m_limit_output_ports->set_sensitive(m_connect_outputs->get_active());
940
941                 if (m_connect_outputs->get_active() && m_limit_output_ports->get_active()) {
942                 m_output_limit_count->set_sensitive(true);
943                 } else {
944                 m_output_limit_count->set_sensitive(false);
945                 }
946 }
947
948 void
949 NewSessionDialog::limit_inputs_clicked ()
950 {
951         m_input_limit_count->set_sensitive(m_limit_input_ports->get_active());
952 }
953
954 void
955 NewSessionDialog::limit_outputs_clicked ()
956 {
957         m_output_limit_count->set_sensitive(m_limit_output_ports->get_active());
958 }
959
960 void
961 NewSessionDialog::master_bus_button_clicked ()
962 {
963         m_master_bus_channel_count->set_sensitive(m_create_master_bus->get_active());
964 }
965
966 void
967 NewSessionDialog::monitor_bus_button_clicked ()
968 {
969         m_control_bus_channel_count->set_sensitive(m_create_control_bus->get_active());
970 }
971
972 void
973 NewSessionDialog::reset_template()
974 {
975         m_template->unselect_all ();
976 }
977
978 void
979 NewSessionDialog::reset_recent()
980 {
981         /* Shamelessly ripped from ardour_ui.cc */
982         std::vector<string *> *sessions;
983         std::vector<string *>::iterator i;
984         RecentSessionsSorter cmp;
985         
986         recent_model->clear ();
987
988         ARDOUR::RecentSessions rs;
989         ARDOUR::read_recent_sessions (rs);
990         
991         /* sort them alphabetically */
992         sort (rs.begin(), rs.end(), cmp);
993         sessions = new std::vector<std::string*>;
994         
995         for (ARDOUR::RecentSessions::iterator i = rs.begin(); i != rs.end(); ++i) {
996                 sessions->push_back (new string ((*i).second));
997         }
998         
999         for (i = sessions->begin(); i != sessions->end(); ++i) {
1000
1001                 std::vector<std::string*>* states;
1002                 std::vector<const gchar*> item;
1003                 std::string fullpath = *(*i);
1004                 
1005                 /* remove any trailing / */
1006                 
1007                 if (fullpath[fullpath.length()-1] == '/') {
1008                         fullpath = fullpath.substr (0, fullpath.length()-1);
1009                 }
1010             
1011                 /* check whether session still exists */
1012                 if (!Glib::file_test(fullpath, Glib::FILE_TEST_EXISTS)) {
1013                         /* session doesn't exist */
1014                         continue;
1015                 }               
1016                 
1017                 /* now get available states for this session */
1018                   
1019                 if ((states = ARDOUR::Session::possible_states (fullpath)) == 0) {
1020                         /* no state file? */
1021                         continue;
1022                 }
1023             
1024                 Gtk::TreeModel::Row row = *(recent_model->append());
1025                 
1026                 row[recent_columns.visible_name] = Glib::path_get_basename (fullpath);
1027                 row[recent_columns.fullpath] = fullpath;
1028                 
1029                 if (states->size()) {
1030                     
1031                         /* add the children */
1032                     
1033                         for (std::vector<std::string*>::iterator i2 = states->begin(); i2 != states->end(); ++i2) {
1034
1035                                 Gtk::TreeModel::Row child_row = *(recent_model->append (row.children()));
1036                                 
1037                                 child_row[recent_columns.visible_name] = **i2;
1038                                 child_row[recent_columns.fullpath] = fullpath;
1039                                 
1040                                 delete *i2;
1041                         }
1042                 }
1043
1044                 delete states;
1045         }
1046         delete sessions;
1047 }
1048
1049 void
1050 NewSessionDialog::reset()
1051 {
1052         reset_name();
1053         reset_template();
1054         set_response_sensitive (Gtk::RESPONSE_NONE, false);
1055 }