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