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