Remove some debugging code which causes slow flashy screen updates.
[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_state_utils.h>
25 #include <ardour/template_utils.h>
26 #include <ardour/session.h>
27 #include <ardour/profile.h>
28
29 #include <gtkmm/entry.h>
30 #include <gtkmm/filechooserbutton.h>
31 #include <gtkmm/spinbutton.h>
32 #include <gtkmm/checkbutton.h>
33 #include <gtkmm/radiobutton.h>
34 #include <gtkmm/filefilter.h>
35 #include <gtkmm/stock.h>
36 #include <gdkmm/cursor.h>
37
38 #include <gtkmm2ext/window_title.h>
39
40 using namespace Gtkmm2ext;
41 using namespace ARDOUR;
42 using namespace PBD;
43
44 #include "opts.h"
45
46 NewSessionDialog::NewSessionDialog()
47         : ArdourDialog ("session control")
48 {
49         in_destructor = false;
50         session_name_label = Gtk::manage(new class Gtk::Label(_("Name :")));
51         m_name = Gtk::manage(new class Gtk::Entry());
52         m_name->set_text(ARDOUR_COMMAND_LINE::session_name);
53
54         chan_count_label_1 = Gtk::manage(new class Gtk::Label(_("channels")));
55         chan_count_label_2 = Gtk::manage(new class Gtk::Label(_("channels")));
56         chan_count_label_3 = Gtk::manage(new class Gtk::Label(_("channels")));
57         chan_count_label_4 = Gtk::manage(new class 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 = Gtk::manage(new class Gtk::Label(_("<b>Busses</b>")));
76         input_label = Gtk::manage(new class Gtk::Label(_("<b>Inputs</b>")));
77         output_label = Gtk::manage(new class Gtk::Label(_("<b>Outputs</b>")));
78
79         session_location_label = Gtk::manage(new class Gtk::Label(_("Create Folder In :")));
80         m_folder = Gtk::manage(new class Gtk::FileChooserButton(Gtk::FILE_CHOOSER_ACTION_SELECT_FOLDER));
81         session_template_label = Gtk::manage(new class Gtk::Label(_("Template :")));
82         m_template = Gtk::manage(new class Gtk::FileChooserButton());
83         m_create_control_bus = Gtk::manage(new class Gtk::CheckButton(_("Create Monitor Bus")));
84         
85         Gtk::Adjustment *m_control_bus_channel_count_adj = Gtk::manage(new class Gtk::Adjustment(2, 0, 100, 1, 10, 10));
86         m_control_bus_channel_count = Gtk::manage(new class Gtk::SpinButton(*m_control_bus_channel_count_adj, 1, 0));
87         
88         Gtk::Adjustment *m_master_bus_channel_count_adj = Gtk::manage(new class Gtk::Adjustment(2, 0, 100, 1, 10, 10));
89         m_master_bus_channel_count = Gtk::manage(new class Gtk::SpinButton(*m_master_bus_channel_count_adj, 1, 0));
90         m_create_master_bus = Gtk::manage(new class Gtk::CheckButton(_("Create Master Bus")));
91         advanced_table = Gtk::manage(new class Gtk::Table(2, 2, true));
92         m_connect_inputs = Gtk::manage(new class Gtk::CheckButton(_("Automatically Connect to Physical Inputs")));
93         m_limit_input_ports = Gtk::manage(new class Gtk::CheckButton(_("Use only")));
94         
95         Gtk::Adjustment *m_input_limit_count_adj = Gtk::manage(new class Gtk::Adjustment(1, 0, 100, 1, 10, 10));
96         m_input_limit_count = Gtk::manage(new class Gtk::SpinButton(*m_input_limit_count_adj, 1, 0));
97         input_port_limit_hbox = Gtk::manage(new class Gtk::HBox(false, 0));
98         input_port_vbox = Gtk::manage(new class Gtk::VBox(false, 0));
99         input_table = Gtk::manage(new class Gtk::Table(2, 2, false));
100
101         bus_frame = Gtk::manage(new class Gtk::Frame());
102         bus_table = Gtk::manage (new Gtk::Table (2, 3, false));
103
104         input_frame = Gtk::manage(new class Gtk::Frame());
105         m_connect_outputs = Gtk::manage(new class Gtk::CheckButton(_("Automatically Connect Outputs")));
106         m_limit_output_ports = Gtk::manage(new class Gtk::CheckButton(_("Use only")));
107         
108         Gtk::Adjustment *m_output_limit_count_adj = Gtk::manage(new class Gtk::Adjustment(1, 0, 100, 1, 10, 10));
109         m_output_limit_count = Gtk::manage(new class Gtk::SpinButton(*m_output_limit_count_adj, 1, 0));
110         output_port_limit_hbox = Gtk::manage(new class Gtk::HBox(false, 0));
111         output_port_vbox = Gtk::manage(new class Gtk::VBox(false, 0));
112         
113         Gtk::RadioButton::Group _RadioBGroup_m_connect_outputs_to_master;
114         m_connect_outputs_to_master = Gtk::manage(new class Gtk::RadioButton(_RadioBGroup_m_connect_outputs_to_master, _("... to Master Bus")));
115         m_connect_outputs_to_physical = Gtk::manage(new class Gtk::RadioButton(_RadioBGroup_m_connect_outputs_to_master, _("... to Physical Outputs")));
116         output_conn_vbox = Gtk::manage(new class Gtk::VBox(false, 0));
117         output_vbox = Gtk::manage(new class Gtk::VBox(false, 0));
118
119         output_frame = Gtk::manage(new class Gtk::Frame());
120         advanced_vbox = Gtk::manage(new class Gtk::VBox(false, 0));
121         advanced_label = Gtk::manage(new class Gtk::Label(_("Advanced Options")));
122         advanced_expander = Gtk::manage(new class Gtk::Expander());
123         new_session_table = Gtk::manage(new class Gtk::Table(2, 2, false));
124         m_open_filechooser = Gtk::manage(new class Gtk::FileChooserButton());
125         open_session_hbox = Gtk::manage(new class Gtk::HBox(false, 0));
126         m_treeview = Gtk::manage(new class Gtk::TreeView());
127         recent_scrolledwindow = Gtk::manage(new class Gtk::ScrolledWindow());
128
129         recent_sesion_label = Gtk::manage(new class Gtk::Label(_("Recent:")));
130         recent_frame = Gtk::manage(new class Gtk::Frame());
131         open_session_vbox = Gtk::manage(new class Gtk::VBox(false, 0));
132         m_notebook = Gtk::manage(new class 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 = Gtk::manage(new class 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 = Gtk::manage (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 = Gtk::manage (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 = Gtk::manage (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         m_notebook->set_flags(Gtk::CAN_FOCUS);
346         m_notebook->set_scrollable(true);
347         m_notebook->append_page(*new_session_table, _("New Session"));
348         m_notebook->pages().back().set_tab_label_packing(false, true, Gtk::PACK_START);
349         m_notebook->append_page(*open_session_vbox, _("Open Session"));
350         m_notebook->pages().back().set_tab_label_packing(false, true, Gtk::PACK_START);
351         get_vbox()->set_homogeneous(false);
352         get_vbox()->set_spacing(0);
353         get_vbox()->pack_start(*m_notebook, Gtk::PACK_SHRINK, 0);
354
355         WindowTitle title(Glib::get_application_name());
356         title += _("Session Control");
357         set_title(title.get_string());
358
359         //set_modal(false);
360         //property_window_position().set_value(Gtk::WIN_POS_NONE);
361         set_resizable(false);
362         //property_destroy_with_parent().set_value(false);
363         set_has_separator(false);
364         // add_button(Gtk::Stock::HELP, Gtk::RESPONSE_HELP);
365         add_button(Gtk::Stock::QUIT, Gtk::RESPONSE_CANCEL);
366         add_button(Gtk::Stock::CLEAR, Gtk::RESPONSE_NONE);
367         m_okbutton = add_button(Gtk::Stock::OPEN, Gtk::RESPONSE_OK);
368
369         recent_model = Gtk::TreeStore::create (recent_columns);
370         m_treeview->set_model (recent_model);
371         m_treeview->append_column (_("Recent Sessions"), recent_columns.visible_name);
372         m_treeview->set_headers_visible (false);
373         m_treeview->get_selection()->set_mode (Gtk::SELECTION_SINGLE);
374
375         if (is_directory (user_template_directory ()))
376         {
377                 m_template->set_current_folder (user_template_directory().to_string());
378         }
379         else if (is_directory (system_template_directory ()))
380         {
381                 m_template->set_current_folder (system_template_directory().to_string());
382         }
383
384         if (is_directory (system_template_directory ()))
385         {
386                 m_template->add_shortcut_folder (system_template_directory().to_string());
387         }
388
389         m_template->set_title(_("select template"));
390         Gtk::FileFilter* session_filter = manage (new (Gtk::FileFilter));
391         session_filter->add_pattern(X_("*.ardour"));
392         session_filter->add_pattern(X_("*.ardour.bak"));
393         m_open_filechooser->set_filter (*session_filter);
394         m_open_filechooser->set_current_folder(getenv ("HOME"));
395         m_open_filechooser->set_title(_("select session file"));
396
397         Gtk::FileFilter* template_filter = manage (new (Gtk::FileFilter));
398         template_filter->add_pattern(X_("*.template"));
399         m_template->set_filter (*template_filter);
400
401         m_folder->set_current_folder(getenv ("HOME"));
402         m_folder->set_title(_("select directory"));
403
404         m_notebook->set_current_page(0);
405         m_notebook->show();
406         m_notebook->show_all_children();
407
408
409         set_default_response (Gtk::RESPONSE_OK);
410         if (!ARDOUR_COMMAND_LINE::session_name.length()) {
411                 set_response_sensitive (Gtk::RESPONSE_OK, false);
412                 set_response_sensitive (Gtk::RESPONSE_NONE, false);
413         } else {
414                 set_response_sensitive (Gtk::RESPONSE_OK, true);
415                 set_response_sensitive (Gtk::RESPONSE_NONE, true);
416         }
417
418         ///@ connect some signals
419
420         m_connect_inputs->signal_clicked().connect (mem_fun (*this, &NewSessionDialog::connect_inputs_clicked));
421         m_connect_outputs->signal_clicked().connect (mem_fun (*this, &NewSessionDialog::connect_outputs_clicked));
422         m_limit_input_ports->signal_clicked().connect (mem_fun (*this, &NewSessionDialog::limit_inputs_clicked));
423         m_limit_output_ports->signal_clicked().connect (mem_fun (*this, &NewSessionDialog::limit_outputs_clicked));
424         m_create_master_bus->signal_clicked().connect (mem_fun (*this, &NewSessionDialog::master_bus_button_clicked));
425         m_create_control_bus->signal_clicked().connect (mem_fun (*this, &NewSessionDialog::monitor_bus_button_clicked));
426         m_name->signal_changed().connect(mem_fun (*this, &NewSessionDialog::on_new_session_name_entry_changed));
427         m_notebook->signal_switch_page().connect (mem_fun (*this, &NewSessionDialog::notebook_page_changed));
428         m_treeview->get_selection()->signal_changed().connect (mem_fun (*this, &NewSessionDialog::treeview_selection_changed));
429         m_treeview->signal_row_activated().connect (mem_fun (*this, &NewSessionDialog::recent_row_activated));
430         m_open_filechooser->signal_selection_changed ().connect (mem_fun (*this, &NewSessionDialog::file_chosen));
431         m_template->signal_selection_changed ().connect (mem_fun (*this, &NewSessionDialog::template_chosen));
432         m_name->grab_focus();
433 }
434
435 NewSessionDialog::~NewSessionDialog()
436 {
437        in_destructor = true;
438 }
439
440 void
441 NewSessionDialog::set_have_engine (bool yn)
442 {
443        if (yn) {
444                m_notebook->remove_page (engine_control);
445        } else {
446                // XXX this is a bit of crude hack. if we ever add or remove
447                // pages from the notebook, this is going to break.
448                if (m_notebook->get_n_pages () != 3) {
449                        m_notebook->append_page (engine_control, _("Audio Setup"));
450                        m_notebook->show_all_children();
451                }
452        }
453 }
454
455
456 void
457 NewSessionDialog::set_session_name(const Glib::ustring& name)
458 {
459         m_name->set_text(name);
460 }
461
462 void
463 NewSessionDialog::set_session_folder(const Glib::ustring& dir)
464 {
465         // XXX DO SOMETHING
466 }
467
468 std::string
469 NewSessionDialog::session_name() const
470 {
471         std::string str = Glib::filename_from_utf8(m_open_filechooser->get_filename());
472         std::string::size_type position = str.find_last_of ('/');
473         str = str.substr (position+1);
474         position = str.find_last_of ('.');
475         str = str.substr (0, position);
476
477         /*
478           XXX what to do if it's a .bak file?
479           load_session doesn't allow it!
480
481         if ((position = str.rfind(".bak")) != string::npos) {
482                 str = str.substr (0, position);
483         }         
484         */
485
486         if (on_newable_page()) {
487                 return Glib::filename_from_utf8(m_name->get_text());
488         } else {
489                 if (m_treeview->get_selection()->count_selected_rows() == 0) {
490                         return Glib::filename_from_utf8(str);
491                 }
492                 Gtk::TreeModel::iterator i = m_treeview->get_selection()->get_selected();
493                 return (*i)[recent_columns.visible_name];
494         }
495 }
496
497 std::string
498 NewSessionDialog::session_folder() const
499 {
500         if (on_newable_page()) {
501                 return Glib::filename_from_utf8(m_folder->get_filename());
502         } else {
503                 if (m_treeview->get_selection()->count_selected_rows() == 0) {
504                         std::string str = Glib::filename_from_utf8(m_open_filechooser->get_filename());
505                         return Glib::path_get_dirname(str);
506                 }
507                 Gtk::TreeModel::iterator i = m_treeview->get_selection()->get_selected();
508                 return (*i)[recent_columns.fullpath];
509         }
510 }
511
512 bool
513 NewSessionDialog::use_session_template() const
514 {
515         if(m_template->get_filename().empty() && (m_notebook->get_current_page() == 0)) return false;
516         return true;
517 }
518
519 std::string
520 NewSessionDialog::session_template_name() const
521 {
522         return Glib::filename_from_utf8(m_template->get_filename());
523 }
524
525 bool
526 NewSessionDialog::create_master_bus() const
527 {
528         return m_create_master_bus->get_active();
529 }
530
531 int
532 NewSessionDialog::master_channel_count() const
533 {
534         return m_master_bus_channel_count->get_value_as_int();
535 }
536
537 bool
538 NewSessionDialog::create_control_bus() const
539 {
540         return m_create_control_bus->get_active();
541 }
542
543 int
544 NewSessionDialog::control_channel_count() const
545 {
546         return m_control_bus_channel_count->get_value_as_int();
547 }
548
549 bool
550 NewSessionDialog::connect_inputs() const
551 {
552         return m_connect_inputs->get_active();
553 }
554
555 bool
556 NewSessionDialog::limit_inputs_used_for_connection() const
557 {
558         return m_limit_input_ports->get_active();
559 }
560
561 int
562 NewSessionDialog::input_limit_count() const
563 {
564         return m_input_limit_count->get_value_as_int();
565 }
566
567 bool
568 NewSessionDialog::connect_outputs() const
569 {
570         return m_connect_outputs->get_active();
571 }
572
573 bool
574 NewSessionDialog::limit_outputs_used_for_connection() const
575 {
576         return m_limit_output_ports->get_active();
577 }
578
579 int
580 NewSessionDialog::output_limit_count() const
581 {
582         return m_output_limit_count->get_value_as_int();
583 }
584
585 bool
586 NewSessionDialog::connect_outs_to_master() const
587 {
588         return m_connect_outputs_to_master->get_active();
589 }
590
591 bool
592 NewSessionDialog::connect_outs_to_physical() const
593 {
594         return m_connect_outputs_to_physical->get_active();
595 }
596
597 bool
598 NewSessionDialog::on_newable_page() const
599 {
600         return (m_notebook->get_current_page() == 0 ||
601                 m_notebook->get_current_page() == 2);
602 }
603
604 int
605 NewSessionDialog::get_current_page() const
606 {
607         return m_notebook->get_current_page();
608 }
609
610 void
611 NewSessionDialog::set_current_page(int page)
612 {
613         return m_notebook->set_current_page (page);
614 }
615
616 void
617 NewSessionDialog::reset_name()
618 {
619         m_name->set_text("");
620         set_response_sensitive (Gtk::RESPONSE_OK, false);
621 }
622
623 void
624 NewSessionDialog::on_new_session_name_entry_changed ()
625 {
626         if (m_name->get_text() != "") {
627                 set_response_sensitive (Gtk::RESPONSE_OK, true);
628                 set_response_sensitive (Gtk::RESPONSE_NONE, true);
629         } else {
630                 set_response_sensitive (Gtk::RESPONSE_OK, false);
631         }
632 }
633
634 void
635 NewSessionDialog::notebook_page_changed (GtkNotebookPage* np, uint pagenum)
636 {
637         if (in_destructor) {
638                 return;
639         }
640
641         if (!on_newable_page ()) {
642                 m_okbutton->set_label(_("Open"));
643                 set_response_sensitive (Gtk::RESPONSE_NONE, false);
644                 m_okbutton->set_image (*(new Gtk::Image (Gtk::Stock::OPEN, Gtk::ICON_SIZE_BUTTON)));
645                 if (m_treeview->get_selection()->count_selected_rows() == 0) {
646                         set_response_sensitive (Gtk::RESPONSE_OK, false);
647                 } else {
648                         set_response_sensitive (Gtk::RESPONSE_OK, true);
649                 }
650         } else {
651                 if (m_name->get_text() != "") {
652                         set_response_sensitive (Gtk::RESPONSE_NONE, true);
653                 }
654                 m_okbutton->set_label(_("New"));
655                 m_okbutton->set_image (*(new Gtk::Image (Gtk::Stock::NEW, Gtk::ICON_SIZE_BUTTON)));
656                 if (m_name->get_text() == "") {
657                         set_response_sensitive (Gtk::RESPONSE_OK, false);
658                 } else {
659                         set_response_sensitive (Gtk::RESPONSE_OK, true);
660                 }
661         }
662 }
663
664 void
665 NewSessionDialog::treeview_selection_changed ()
666 {
667         if (m_treeview->get_selection()->count_selected_rows() == 0) {
668                 if (!m_open_filechooser->get_filename().empty()) {
669                         set_response_sensitive (Gtk::RESPONSE_OK, true);
670                 } else {
671                         set_response_sensitive (Gtk::RESPONSE_OK, false);
672                 }
673         } else {
674                 set_response_sensitive (Gtk::RESPONSE_OK, true);
675         }
676 }
677
678 void
679 NewSessionDialog::file_chosen ()
680 {
681         if (on_newable_page ()) return;
682
683         m_treeview->get_selection()->unselect_all();
684
685         get_window()->set_cursor(Gdk::Cursor(Gdk::WATCH));
686
687         if (!m_open_filechooser->get_filename().empty()) {
688                 set_response_sensitive (Gtk::RESPONSE_OK, true);
689                 response (Gtk::RESPONSE_OK);
690         } else {
691                 set_response_sensitive (Gtk::RESPONSE_OK, false);
692         }
693 }
694
695 void
696 NewSessionDialog::template_chosen ()
697 {
698         if (m_template->get_filename() != "" ) {;
699                 set_response_sensitive (Gtk::RESPONSE_NONE, true);
700         } else {
701                 set_response_sensitive (Gtk::RESPONSE_NONE, false);
702         }
703 }
704
705 void
706 NewSessionDialog::recent_row_activated (const Gtk::TreePath& path, Gtk::TreeViewColumn* col)
707 {
708         response (Gtk::RESPONSE_YES);
709 }
710
711 void
712 NewSessionDialog::connect_inputs_clicked ()
713 {
714         m_limit_input_ports->set_sensitive(m_connect_inputs->get_active());
715
716         if (m_connect_inputs->get_active() && m_limit_input_ports->get_active()) {
717                 m_input_limit_count->set_sensitive(true);
718         } else {
719                 m_input_limit_count->set_sensitive(false);
720         }
721 }
722
723 void
724 NewSessionDialog::connect_outputs_clicked ()
725 {
726         m_limit_output_ports->set_sensitive(m_connect_outputs->get_active());
727
728         if (m_connect_outputs->get_active() && m_limit_output_ports->get_active()) {
729                 m_output_limit_count->set_sensitive(true);
730         } else {
731                 m_output_limit_count->set_sensitive(false);
732         }
733 }
734
735 void
736 NewSessionDialog::limit_inputs_clicked ()
737 {
738         m_input_limit_count->set_sensitive(m_limit_input_ports->get_active());
739 }
740
741 void
742 NewSessionDialog::limit_outputs_clicked ()
743 {
744         m_output_limit_count->set_sensitive(m_limit_output_ports->get_active());
745 }
746
747 void
748 NewSessionDialog::master_bus_button_clicked ()
749 {
750         m_master_bus_channel_count->set_sensitive(m_create_master_bus->get_active());
751 }
752
753 void
754 NewSessionDialog::monitor_bus_button_clicked ()
755 {
756         m_control_bus_channel_count->set_sensitive(m_create_control_bus->get_active());
757 }
758
759 void
760 NewSessionDialog::reset_template()
761 {
762         m_template->set_filename("");
763 }
764
765 void
766 NewSessionDialog::reset_recent()
767 {
768         std::vector<sys::path> session_directories;
769         RecentSessionsSorter cmp;
770         
771         recent_model->clear ();
772
773         ARDOUR::RecentSessions rs;
774         ARDOUR::read_recent_sessions (rs);
775         
776         // sort them alphabetically
777         sort (rs.begin(), rs.end(), cmp);
778         
779         for (ARDOUR::RecentSessions::iterator i = rs.begin(); i != rs.end(); ++i) {
780                 session_directories.push_back ((*i).second);
781         }
782         
783         for (vector<sys::path>::const_iterator i = session_directories.begin();
784                         i != session_directories.end(); ++i)
785         {
786                 std::vector<sys::path> state_file_paths;
787             
788                 // now get available states for this session
789
790                 get_state_files_in_directory (*i, state_file_paths);
791
792                 if (state_file_paths.empty()) {
793                         // no state file?
794                         continue;
795                 }
796           
797                 std::vector<string> state_file_names(get_file_names_no_extension (state_file_paths));
798
799                 Gtk::TreeModel::Row row = *(recent_model->append());
800
801                 const string fullpath = (*i).to_string();
802                 
803                 row[recent_columns.visible_name] = Glib::path_get_basename (fullpath);
804                 row[recent_columns.fullpath] = fullpath;
805                 
806                 if (state_file_names.size() > 1) {
807
808                         // add the children
809
810                         for (std::vector<std::string>::iterator i2 = state_file_names.begin();
811                                         i2 != state_file_names.end(); ++i2)
812                         {
813
814                                 Gtk::TreeModel::Row child_row = *(recent_model->append (row.children()));
815
816                                 child_row[recent_columns.visible_name] = *i2;
817                                 child_row[recent_columns.fullpath] = fullpath;
818                         }
819                 }
820         }
821 }
822
823 void
824 NewSessionDialog::reset()
825 {
826         reset_name();
827         reset_template();
828         set_response_sensitive (Gtk::RESPONSE_NONE, false);
829 }