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