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