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