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