820a614ecc793906ee62aeba98ae6befd3eb2688
[ardour.git] / gtk2_ardour / new_session_dialog.cc
1 /*
2     Copyright (C) 2005 Paul Davis 
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include "i18n.h"
21 #include "new_session_dialog.h"
22
23 #include <ardour/recent_sessions.h>
24 #include <ardour/session_state_utils.h>
25 #include <ardour/template_utils.h>
26 #include <ardour/session.h>
27 #include <ardour/profile.h>
28
29 #include <gtkmm/entry.h>
30 #include <gtkmm/filechooserbutton.h>
31 #include <gtkmm/spinbutton.h>
32 #include <gtkmm/checkbutton.h>
33 #include <gtkmm/radiobutton.h>
34 #include <gtkmm/filefilter.h>
35 #include <gtkmm/stock.h>
36 #include <gdkmm/cursor.h>
37
38 #include <gtkmm2ext/window_title.h>
39
40 using namespace Gtkmm2ext;
41 using namespace ARDOUR;
42 using namespace PBD;
43
44 #include "opts.h"
45 #include "utils.h"
46
47 NewSessionDialog::NewSessionDialog()
48         : ArdourDialog ("session control")
49 {
50         in_destructor = false;
51         session_name_label = Gtk::manage(new class Gtk::Label(_("Name :")));
52         m_name = Gtk::manage(new class Gtk::Entry());
53         m_name->set_text(ARDOUR_COMMAND_LINE::session_name);
54
55         chan_count_label_1 = Gtk::manage(new class Gtk::Label(_("channels")));
56         chan_count_label_2 = Gtk::manage(new class Gtk::Label(_("channels")));
57         chan_count_label_3 = Gtk::manage(new class Gtk::Label(_("channels")));
58         chan_count_label_4 = Gtk::manage(new class Gtk::Label(_("channels")));
59
60         chan_count_label_1->set_alignment(0,0.5);
61         chan_count_label_1->set_padding(0,0);
62         chan_count_label_1->set_line_wrap(false);
63
64         chan_count_label_2->set_alignment(0,0.5);
65         chan_count_label_2->set_padding(0,0);
66         chan_count_label_2->set_line_wrap(false);
67
68         chan_count_label_3->set_alignment(0,0.5);
69         chan_count_label_3->set_padding(0,0);
70         chan_count_label_3->set_line_wrap(false);
71
72         chan_count_label_4->set_alignment(0,0.5);
73         chan_count_label_4->set_padding(0,0);
74         chan_count_label_4->set_line_wrap(false);
75
76         bus_label = Gtk::manage(new class Gtk::Label(_("<b>Busses</b>")));
77         input_label = Gtk::manage(new class Gtk::Label(_("<b>Inputs</b>")));
78         output_label = Gtk::manage(new class Gtk::Label(_("<b>Outputs</b>")));
79
80         session_location_label = Gtk::manage(new class Gtk::Label(_("Create Folder In :")));
81         m_folder = Gtk::manage(new class Gtk::FileChooserButton(Gtk::FILE_CHOOSER_ACTION_SELECT_FOLDER));
82         session_template_label = Gtk::manage(new class Gtk::Label(_("Template :")));
83         m_template = Gtk::manage(new class Gtk::FileChooserButton());
84         m_create_control_bus = Gtk::manage(new class Gtk::CheckButton(_("Create Monitor Bus")));
85         
86         Gtk::Adjustment *m_control_bus_channel_count_adj = Gtk::manage(new class Gtk::Adjustment(2, 0, 100, 1, 10, 10));
87         m_control_bus_channel_count = Gtk::manage(new class Gtk::SpinButton(*m_control_bus_channel_count_adj, 1, 0));
88         
89         Gtk::Adjustment *m_master_bus_channel_count_adj = Gtk::manage(new class Gtk::Adjustment(2, 0, 100, 1, 10, 10));
90         m_master_bus_channel_count = Gtk::manage(new class Gtk::SpinButton(*m_master_bus_channel_count_adj, 1, 0));
91         m_create_master_bus = Gtk::manage(new class Gtk::CheckButton(_("Create Master Bus")));
92         advanced_table = Gtk::manage(new class Gtk::Table(2, 2, true));
93         m_connect_inputs = Gtk::manage(new class Gtk::CheckButton(_("Automatically Connect to Physical Inputs")));
94         m_limit_input_ports = Gtk::manage(new class Gtk::CheckButton(_("Use only")));
95         
96         Gtk::Adjustment *m_input_limit_count_adj = Gtk::manage(new class Gtk::Adjustment(1, 0, 100, 1, 10, 10));
97         m_input_limit_count = Gtk::manage(new class Gtk::SpinButton(*m_input_limit_count_adj, 1, 0));
98         input_port_limit_hbox = Gtk::manage(new class Gtk::HBox(false, 0));
99         input_port_vbox = Gtk::manage(new class Gtk::VBox(false, 0));
100         input_table = Gtk::manage(new class Gtk::Table(2, 2, false));
101
102         bus_frame = Gtk::manage(new class Gtk::Frame());
103         bus_table = Gtk::manage (new Gtk::Table (2, 3, false));
104
105         input_frame = Gtk::manage(new class Gtk::Frame());
106         m_connect_outputs = Gtk::manage(new class Gtk::CheckButton(_("Automatically Connect Outputs")));
107         m_limit_output_ports = Gtk::manage(new class Gtk::CheckButton(_("Use only")));
108         
109         Gtk::Adjustment *m_output_limit_count_adj = Gtk::manage(new class Gtk::Adjustment(1, 0, 100, 1, 10, 10));
110         m_output_limit_count = Gtk::manage(new class Gtk::SpinButton(*m_output_limit_count_adj, 1, 0));
111         output_port_limit_hbox = Gtk::manage(new class Gtk::HBox(false, 0));
112         output_port_vbox = Gtk::manage(new class Gtk::VBox(false, 0));
113         
114         Gtk::RadioButton::Group _RadioBGroup_m_connect_outputs_to_master;
115         m_connect_outputs_to_master = Gtk::manage(new class Gtk::RadioButton(_RadioBGroup_m_connect_outputs_to_master, _("... to Master Bus")));
116         m_connect_outputs_to_physical = Gtk::manage(new class Gtk::RadioButton(_RadioBGroup_m_connect_outputs_to_master, _("... to Physical Outputs")));
117         output_conn_vbox = Gtk::manage(new class Gtk::VBox(false, 0));
118         output_vbox = Gtk::manage(new class Gtk::VBox(false, 0));
119
120         output_frame = Gtk::manage(new class Gtk::Frame());
121         advanced_vbox = Gtk::manage(new class Gtk::VBox(false, 0));
122         advanced_label = Gtk::manage(new class Gtk::Label(_("Advanced Options")));
123         advanced_expander = Gtk::manage(new class Gtk::Expander());
124         new_session_table = Gtk::manage(new class Gtk::Table(2, 2, false));
125         m_open_filechooser = Gtk::manage(new class Gtk::FileChooserButton());
126         open_session_hbox = Gtk::manage(new class Gtk::HBox(false, 0));
127         m_treeview = Gtk::manage(new class Gtk::TreeView());
128         recent_scrolledwindow = Gtk::manage(new class Gtk::ScrolledWindow());
129
130         recent_sesion_label = Gtk::manage(new class Gtk::Label(_("Recent:")));
131         recent_frame = Gtk::manage(new class Gtk::Frame());
132         open_session_vbox = Gtk::manage(new class Gtk::VBox(false, 0));
133         m_notebook = Gtk::manage(new class Gtk::Notebook());
134         session_name_label->set_alignment(0, 0.5);
135         session_name_label->set_padding(6,0);
136         session_name_label->set_line_wrap(false);
137         session_name_label->set_selectable(false);
138         m_name->set_editable(true);
139         m_name->set_max_length(0);
140         m_name->set_has_frame(true);
141         m_name->set_activates_default(true);
142         m_name->set_width_chars (40);
143         session_location_label->set_alignment(0,0.5);
144         session_location_label->set_padding(6,0);
145         session_location_label->set_line_wrap(false);
146         session_location_label->set_selectable(false);
147         session_template_label->set_alignment(0,0.5);
148         session_template_label->set_padding(6,0);
149         session_template_label->set_line_wrap(false);
150         session_template_label->set_selectable(false);
151         m_create_control_bus->set_flags(Gtk::CAN_FOCUS);
152         m_create_control_bus->set_relief(Gtk::RELIEF_NORMAL);
153         m_create_control_bus->set_mode(true);
154         m_create_control_bus->set_active(false);
155         m_create_control_bus->set_border_width(0);
156         m_control_bus_channel_count->set_flags(Gtk::CAN_FOCUS);
157         m_control_bus_channel_count->set_update_policy(Gtk::UPDATE_ALWAYS);
158         m_control_bus_channel_count->set_numeric(true);
159         m_control_bus_channel_count->set_digits(0);
160         m_control_bus_channel_count->set_wrap(false);
161         m_control_bus_channel_count->set_sensitive(false);
162         m_master_bus_channel_count->set_flags(Gtk::CAN_FOCUS);
163         m_master_bus_channel_count->set_update_policy(Gtk::UPDATE_ALWAYS);
164         m_master_bus_channel_count->set_numeric(true);
165         m_master_bus_channel_count->set_digits(0);
166         m_master_bus_channel_count->set_wrap(false);
167         open_session_file_label = Gtk::manage(new class Gtk::Label(_("Browse:")));
168         open_session_file_label->set_alignment(0, 0.5);
169         m_create_master_bus->set_flags(Gtk::CAN_FOCUS);
170         m_create_master_bus->set_relief(Gtk::RELIEF_NORMAL);
171         m_create_master_bus->set_mode(true);
172         m_create_master_bus->set_active(true);
173         m_create_master_bus->set_border_width(0);
174         advanced_table->set_row_spacings(0);
175         advanced_table->set_col_spacings(0);
176
177         m_connect_inputs->set_flags(Gtk::CAN_FOCUS);
178         m_connect_inputs->set_relief(Gtk::RELIEF_NORMAL);
179         m_connect_inputs->set_mode(true);
180         m_connect_inputs->set_active(true);
181         m_connect_inputs->set_border_width(0);
182
183         m_limit_input_ports->set_flags(Gtk::CAN_FOCUS);
184         m_limit_input_ports->set_relief(Gtk::RELIEF_NORMAL);
185         m_limit_input_ports->set_mode(true);
186         m_limit_input_ports->set_sensitive(true);
187         m_limit_input_ports->set_border_width(0);
188         m_input_limit_count->set_flags(Gtk::CAN_FOCUS);
189         m_input_limit_count->set_update_policy(Gtk::UPDATE_ALWAYS);
190         m_input_limit_count->set_numeric(true);
191         m_input_limit_count->set_digits(0);
192         m_input_limit_count->set_wrap(false);
193         m_input_limit_count->set_sensitive(false);
194
195         bus_hbox = Gtk::manage (new Gtk::HBox (false, 0));
196         bus_hbox->pack_start (*bus_table, Gtk::PACK_SHRINK, 18);
197
198         bus_label->set_alignment(0, 0.5);
199         bus_label->set_padding(0,0);
200         bus_label->set_line_wrap(false);
201         bus_label->set_selectable(false);
202         bus_label->set_use_markup(true);
203         bus_frame->set_shadow_type(Gtk::SHADOW_NONE);
204         bus_frame->set_label_align(0,0.5);
205         bus_frame->add(*bus_hbox);
206         bus_frame->set_label_widget(*bus_label);
207         
208         bus_table->set_row_spacings (0);
209         bus_table->set_col_spacings (0);
210         bus_table->attach (*m_create_master_bus, 0, 1, 0, 1, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
211         bus_table->attach (*m_master_bus_channel_count, 1, 2, 0, 1, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
212         bus_table->attach (*chan_count_label_1, 2, 3, 0, 1, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 6, 0);
213         bus_table->attach (*m_create_control_bus, 0, 1, 1, 2, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
214         bus_table->attach (*m_control_bus_channel_count, 1, 2, 1, 2, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
215         bus_table->attach (*chan_count_label_2, 2, 3, 1, 2, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 6, 0);
216
217         input_port_limit_hbox->pack_start(*m_limit_input_ports, Gtk::PACK_SHRINK, 6);
218         input_port_limit_hbox->pack_start(*m_input_limit_count, Gtk::PACK_SHRINK, 0);
219         input_port_limit_hbox->pack_start(*chan_count_label_3, Gtk::PACK_SHRINK, 6);
220         input_port_vbox->pack_start(*m_connect_inputs, Gtk::PACK_SHRINK, 0);
221         input_port_vbox->pack_start(*input_port_limit_hbox, Gtk::PACK_EXPAND_PADDING, 0);
222         input_table->set_row_spacings(0);
223         input_table->set_col_spacings(0);
224         input_table->attach(*input_port_vbox, 0, 1, 0, 1, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 6, 6);
225
226         input_hbox = Gtk::manage (new Gtk::HBox (false, 0));
227         input_hbox->pack_start (*input_table, Gtk::PACK_SHRINK, 18);
228
229         input_label->set_alignment(0, 0.5);
230         input_label->set_padding(0,0);
231         input_label->set_line_wrap(false);
232         input_label->set_selectable(false);
233         input_label->set_use_markup(true);
234         input_frame->set_shadow_type(Gtk::SHADOW_NONE);
235         input_frame->set_label_align(0,0.5);
236         input_frame->add(*input_hbox);
237         input_frame->set_label_widget(*input_label);
238
239         m_connect_outputs->set_flags(Gtk::CAN_FOCUS);
240         m_connect_outputs->set_relief(Gtk::RELIEF_NORMAL);
241         m_connect_outputs->set_mode(true);
242         m_connect_outputs->set_active(true);
243         m_connect_outputs->set_border_width(0);
244         m_limit_output_ports->set_flags(Gtk::CAN_FOCUS);
245         m_limit_output_ports->set_relief(Gtk::RELIEF_NORMAL);
246         m_limit_output_ports->set_mode(true);
247         m_limit_output_ports->set_sensitive(true);
248         m_limit_output_ports->set_border_width(0);
249         m_output_limit_count->set_flags(Gtk::CAN_FOCUS);
250         m_output_limit_count->set_update_policy(Gtk::UPDATE_ALWAYS);
251         m_output_limit_count->set_numeric(false);
252         m_output_limit_count->set_digits(0);
253         m_output_limit_count->set_wrap(false);
254         m_output_limit_count->set_sensitive(false);
255         output_port_limit_hbox->pack_start(*m_limit_output_ports, Gtk::PACK_SHRINK, 6);
256         output_port_limit_hbox->pack_start(*m_output_limit_count, Gtk::PACK_SHRINK, 0);
257         output_port_limit_hbox->pack_start(*chan_count_label_4, Gtk::PACK_SHRINK, 6);
258         m_connect_outputs_to_master->set_flags(Gtk::CAN_FOCUS);
259         m_connect_outputs_to_master->set_relief(Gtk::RELIEF_NORMAL);
260         m_connect_outputs_to_master->set_mode(true);
261         m_connect_outputs_to_master->set_active(false);
262         m_connect_outputs_to_master->set_border_width(0);
263         m_connect_outputs_to_physical->set_flags(Gtk::CAN_FOCUS);
264         m_connect_outputs_to_physical->set_relief(Gtk::RELIEF_NORMAL);
265         m_connect_outputs_to_physical->set_mode(true);
266         m_connect_outputs_to_physical->set_active(false);
267         m_connect_outputs_to_physical->set_border_width(0);
268         output_conn_vbox->pack_start(*m_connect_outputs, Gtk::PACK_SHRINK, 0);
269         output_conn_vbox->pack_start(*m_connect_outputs_to_master, Gtk::PACK_SHRINK, 0);
270         output_conn_vbox->pack_start(*m_connect_outputs_to_physical, Gtk::PACK_SHRINK, 0);
271         output_vbox->set_border_width(6);
272
273         output_port_vbox->pack_start(*output_port_limit_hbox, Gtk::PACK_SHRINK, 0);
274
275         output_vbox->pack_start(*output_conn_vbox);
276         output_vbox->pack_start(*output_port_vbox);
277
278         output_label->set_alignment(0, 0.5);
279         output_label->set_padding(0,0);
280         output_label->set_line_wrap(false);
281         output_label->set_selectable(false);
282         output_label->set_use_markup(true);
283         output_frame->set_shadow_type(Gtk::SHADOW_NONE);
284         output_frame->set_label_align(0,0.5);
285
286         output_hbox = Gtk::manage (new Gtk::HBox (false, 0));
287         output_hbox->pack_start (*output_vbox, Gtk::PACK_SHRINK, 18);
288
289         output_frame->add(*output_hbox);
290         output_frame->set_label_widget(*output_label);
291
292         advanced_vbox->pack_start(*advanced_table, Gtk::PACK_SHRINK, 0);
293         advanced_vbox->pack_start(*bus_frame, Gtk::PACK_SHRINK, 6);
294         advanced_vbox->pack_start(*input_frame, Gtk::PACK_SHRINK, 6);
295         advanced_vbox->pack_start(*output_frame, Gtk::PACK_SHRINK, 0);
296         advanced_label->set_padding(0,0);
297         advanced_label->set_line_wrap(false);
298         advanced_label->set_selectable(false);
299         advanced_label->set_alignment(0, 0.5);
300         advanced_expander->set_flags(Gtk::CAN_FOCUS);
301         advanced_expander->set_border_width(0);
302         advanced_expander->set_expanded(false);
303         advanced_expander->set_spacing(0);
304         advanced_expander->add(*advanced_vbox);
305         advanced_expander->set_label_widget(*advanced_label);
306         new_session_table->set_border_width(12);
307         new_session_table->set_row_spacings(6);
308         new_session_table->set_col_spacings(0);
309         new_session_table->attach(*session_name_label, 0, 1, 0, 1, Gtk::FILL, Gtk::FILL, 0, 0);
310         new_session_table->attach(*m_name, 1, 2, 0, 1, Gtk::EXPAND|Gtk::FILL, Gtk::FILL, 0, 0);
311         new_session_table->attach(*session_location_label, 0, 1, 1, 2, Gtk::FILL, Gtk::FILL, 0, 0);
312         new_session_table->attach(*m_folder, 1, 2, 1, 2, Gtk::EXPAND|Gtk::FILL, Gtk::FILL, 0, 0);
313         new_session_table->attach(*session_template_label, 0, 1, 2, 3, Gtk::FILL, Gtk::FILL, 0, 0);
314         new_session_table->attach(*m_template, 1, 2, 2, 3, Gtk::EXPAND|Gtk::FILL, Gtk::FILL, 0, 0);
315
316         if (!ARDOUR::Profile->get_sae()) {
317                 new_session_table->attach(*advanced_expander, 0, 2, 3, 4, Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 6);
318         }
319        
320         open_session_hbox->pack_start(*open_session_file_label, false, false, 12);
321         open_session_hbox->pack_start(*m_open_filechooser, true, true, 12);
322         m_treeview->set_flags(Gtk::CAN_FOCUS);
323         m_treeview->set_headers_visible(true);
324         m_treeview->set_rules_hint(false);
325         m_treeview->set_reorderable(false);
326         m_treeview->set_enable_search(true);
327         m_treeview->set_fixed_height_mode(false);
328         m_treeview->set_hover_selection(false);
329         m_treeview->set_size_request(-1, 150);
330         recent_scrolledwindow->set_flags(Gtk::CAN_FOCUS);
331         recent_scrolledwindow->set_border_width(6);
332         recent_scrolledwindow->set_shadow_type(Gtk::SHADOW_IN);
333         recent_scrolledwindow->set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
334         recent_scrolledwindow->property_window_placement().set_value(Gtk::CORNER_TOP_LEFT);
335         recent_scrolledwindow->add(*m_treeview);
336
337         recent_sesion_label->set_padding(0,0);
338         recent_sesion_label->set_line_wrap(false);
339         recent_sesion_label->set_selectable(false);
340         recent_frame->set_border_width(12);
341         recent_frame->set_shadow_type(Gtk::SHADOW_NONE);
342         recent_frame->add(*recent_scrolledwindow);
343         recent_frame->set_label_widget(*recent_sesion_label);
344         open_session_vbox->pack_start(*recent_frame, Gtk::PACK_EXPAND_WIDGET, 0);
345         open_session_vbox->pack_start(*open_session_hbox, Gtk::PACK_SHRINK, 12);
346         m_notebook->set_flags(Gtk::CAN_FOCUS);
347         m_notebook->set_scrollable(true);
348         get_vbox()->set_homogeneous(false);
349         get_vbox()->set_spacing(0);
350         get_vbox()->pack_start(*m_notebook, Gtk::PACK_SHRINK, 0);
351
352         /* 
353            icon setting is done again in the editor (for the whole app),
354            but its all chickens and eggs at this point.
355         */
356
357         list<Glib::RefPtr<Gdk::Pixbuf> > window_icons;
358         Glib::RefPtr<Gdk::Pixbuf> icon;
359
360         if ((icon = ::get_icon ("ardour_icon_16px")) != 0) {
361                 window_icons.push_back (icon);
362         }
363         if ((icon = ::get_icon ("ardour_icon_22px")) != 0) {
364                 window_icons.push_back (icon);
365         }
366         if ((icon = ::get_icon ("ardour_icon_32px")) != 0) {
367                 window_icons.push_back (icon);
368         }
369         if ((icon = ::get_icon ("ardour_icon_48px")) != 0) {
370                 window_icons.push_back (icon);
371         }
372         if (!window_icons.empty()) {
373                 set_icon_list (window_icons);
374         }
375
376         WindowTitle title(Glib::get_application_name());
377         title += _("Session Control");
378         set_title(title.get_string());
379
380         //set_modal(false);
381         //property_window_position().set_value(Gtk::WIN_POS_NONE);
382         set_resizable(false);
383         //property_destroy_with_parent().set_value(false);
384         set_has_separator(false);
385         // add_button(Gtk::Stock::HELP, Gtk::RESPONSE_HELP);
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         m_folder->set_current_folder (dir);
530 }
531
532 std::string
533 NewSessionDialog::session_name() const
534 {
535         std::string str = Glib::filename_from_utf8(m_open_filechooser->get_filename());
536         std::string::size_type position = str.find_last_of ('/');
537         str = str.substr (position+1);
538         position = str.find_last_of ('.');
539         str = str.substr (0, position);
540
541         /*
542           XXX what to do if it's a .bak file?
543           load_session doesn't allow it!
544
545         if ((position = str.rfind(".bak")) != string::npos) {
546                 str = str.substr (0, position);
547         }         
548         */
549
550         int page = m_notebook->get_current_page();
551
552         if (page == 0 || page == 2) {
553                 /* new or audio setup pages */
554                 return Glib::filename_from_utf8(m_name->get_text());
555         } else {
556                 if (m_treeview->get_selection()->count_selected_rows() == 0) {
557                         return Glib::filename_from_utf8(str);
558                 }
559                 Gtk::TreeModel::iterator i = m_treeview->get_selection()->get_selected();
560                 return (*i)[recent_columns.visible_name];
561         }
562 }
563
564 std::string
565 NewSessionDialog::session_folder() const
566 {
567         if (m_notebook->get_current_page() == 0) {
568                 return Glib::filename_from_utf8(m_folder->get_filename());
569         } else {
570                
571                 if (m_treeview->get_selection()->count_selected_rows() == 0) {
572                         const string filename(Glib::filename_from_utf8(m_open_filechooser->get_filename()));
573                         return Glib::path_get_dirname(filename);
574                 }
575                 Gtk::TreeModel::iterator i = m_treeview->get_selection()->get_selected();
576                 return (*i)[recent_columns.fullpath];
577         }
578 }
579
580 bool
581 NewSessionDialog::use_session_template() const
582 {
583         if(m_template->get_filename().empty() && (m_notebook->get_current_page() == 0)) return false;
584         return true;
585 }
586
587 std::string
588 NewSessionDialog::session_template_name() const
589 {
590         return Glib::filename_from_utf8(m_template->get_filename());
591 }
592
593 bool
594 NewSessionDialog::create_master_bus() const
595 {
596         return m_create_master_bus->get_active();
597 }
598
599 int
600 NewSessionDialog::master_channel_count() const
601 {
602         return m_master_bus_channel_count->get_value_as_int();
603 }
604
605 bool
606 NewSessionDialog::create_control_bus() const
607 {
608         return m_create_control_bus->get_active();
609 }
610
611 int
612 NewSessionDialog::control_channel_count() const
613 {
614         return m_control_bus_channel_count->get_value_as_int();
615 }
616
617 bool
618 NewSessionDialog::connect_inputs() const
619 {
620         return m_connect_inputs->get_active();
621 }
622
623 bool
624 NewSessionDialog::limit_inputs_used_for_connection() const
625 {
626         return m_limit_input_ports->get_active();
627 }
628
629 int
630 NewSessionDialog::input_limit_count() const
631 {
632         return m_input_limit_count->get_value_as_int();
633 }
634
635 bool
636 NewSessionDialog::connect_outputs() const
637 {
638         return m_connect_outputs->get_active();
639 }
640
641 bool
642 NewSessionDialog::limit_outputs_used_for_connection() const
643 {
644         return m_limit_output_ports->get_active();
645 }
646
647 int
648 NewSessionDialog::output_limit_count() const
649 {
650         return m_output_limit_count->get_value_as_int();
651 }
652
653 bool
654 NewSessionDialog::connect_outs_to_master() const
655 {
656         return m_connect_outputs_to_master->get_active();
657 }
658
659 bool
660 NewSessionDialog::connect_outs_to_physical() const
661 {
662         return m_connect_outputs_to_physical->get_active();
663 }
664
665 int
666 NewSessionDialog::get_current_page() const
667 {
668         return m_notebook->get_current_page();
669 }
670
671 NewSessionDialog::Pages
672 NewSessionDialog::which_page ()
673 {
674         int num = m_notebook->get_current_page();
675
676         if (page_set == NewPage) {
677                 return NewPage;
678
679         } else if (page_set == OpenPage) {
680                 return OpenPage;
681
682         } else if (page_set == EnginePage) {
683                 return EnginePage;
684
685         } else if (page_set == NewPage|OpenPage) {
686                 switch (num) {
687                 case 0:
688                         return NewPage;
689                 default:
690                         return OpenPage;
691                 }
692
693         } else if (page_set == NewPage|EnginePage) {
694                 switch (num) {
695                 case 0:
696                         return NewPage;
697                 default:
698                         return EnginePage;
699                 }
700
701         } else if (page_set == NewPage|EnginePage|OpenPage) {
702                 switch (num) {
703                 case 0:
704                         return NewPage;
705                 case 1:
706                         return OpenPage;
707                 default:
708                         return EnginePage;
709                 }
710
711         } else if (page_set == OpenPage|EnginePage) {
712                 switch (num) {
713                 case 0:
714                         return OpenPage;
715                 default:
716                         return EnginePage;
717                 }
718         }
719 }
720
721 void
722 NewSessionDialog::set_current_page(int page)
723 {
724         return m_notebook->set_current_page (page);
725 }
726
727 void
728 NewSessionDialog::reset_name()
729 {
730         m_name->set_text("");
731         set_response_sensitive (Gtk::RESPONSE_OK, false);
732 }
733
734 void
735 NewSessionDialog::on_new_session_name_entry_changed ()
736 {
737         if (m_name->get_text() != "") {
738                 set_response_sensitive (Gtk::RESPONSE_OK, true);
739                 set_response_sensitive (Gtk::RESPONSE_NONE, true);
740         } else {
741                 set_response_sensitive (Gtk::RESPONSE_OK, false);
742         }
743 }
744
745 void
746 NewSessionDialog::notebook_page_changed (GtkNotebookPage* np, uint pagenum)
747 {
748         if (in_destructor) {
749                 return;
750         }
751
752         switch (which_page()) {
753         case OpenPage:
754                 on_new_session_page = false;
755                 m_okbutton->set_label(_("Open"));
756                 m_okbutton->set_image (*(manage (new Gtk::Image (Gtk::Stock::OPEN, Gtk::ICON_SIZE_BUTTON))));
757                 set_response_sensitive (Gtk::RESPONSE_NONE, false);
758                 m_okbutton->set_image (*(new Gtk::Image (Gtk::Stock::OPEN, Gtk::ICON_SIZE_BUTTON)));
759                 if (m_treeview->get_selection()->count_selected_rows() == 0) {
760                         set_response_sensitive (Gtk::RESPONSE_OK, false);
761                 } else {
762                         set_response_sensitive (Gtk::RESPONSE_OK, true);
763                 }
764                 break;
765
766         case EnginePage:
767                 on_new_session_page = false;
768                 m_okbutton->set_label(_("Open"));
769                 m_okbutton->set_image (*(manage (new Gtk::Image (Gtk::Stock::OPEN, Gtk::ICON_SIZE_BUTTON))));
770                 set_response_sensitive (Gtk::RESPONSE_NONE, false);
771                 set_response_sensitive (Gtk::RESPONSE_OK, true);
772                 break;
773
774         default:
775                 m_okbutton->set_label(_("New"));
776                 m_okbutton->set_image (*(new Gtk::Image (Gtk::Stock::NEW, Gtk::ICON_SIZE_BUTTON)));
777                 m_okbutton->set_label(_("New"));
778                 m_okbutton->set_image (*(new Gtk::Image (Gtk::Stock::NEW, Gtk::ICON_SIZE_BUTTON)));
779                 if (m_name->get_text() == "") {
780                         set_response_sensitive (Gtk::RESPONSE_OK, false);
781                         m_name->grab_focus();
782                 } else {
783                         set_response_sensitive (Gtk::RESPONSE_OK, true);
784                 }
785         }
786 }
787
788 void
789 NewSessionDialog::treeview_selection_changed ()
790 {
791         if (m_treeview->get_selection()->count_selected_rows() == 0) {
792                 if (!m_open_filechooser->get_filename().empty()) {
793                         set_response_sensitive (Gtk::RESPONSE_OK, true);
794                 } else {
795                         set_response_sensitive (Gtk::RESPONSE_OK, false);
796                 }
797         } else {
798                 set_response_sensitive (Gtk::RESPONSE_OK, true);
799         }
800 }
801
802 void
803 NewSessionDialog::file_chosen ()
804 {
805         switch (which_page()) {
806       case OpenPage:
807          break;
808            case NewPage:
809            case EnginePage:
810                    return;
811         }
812
813         m_treeview->get_selection()->unselect_all();
814
815         Glib::RefPtr<Gdk::Window> win (get_window());
816
817         if (win) {
818                 win->set_cursor(Gdk::Cursor(Gdk::WATCH));
819         }
820
821         if (!m_open_filechooser->get_filename().empty()) {
822                 set_response_sensitive (Gtk::RESPONSE_OK, true);
823                 response (Gtk::RESPONSE_OK);
824         } else {
825                 set_response_sensitive (Gtk::RESPONSE_OK, false);
826         }
827 }
828
829 void
830 NewSessionDialog::template_chosen ()
831 {
832         if (m_template->get_filename() != "" ) {;
833                 set_response_sensitive (Gtk::RESPONSE_NONE, true);
834         } else {
835                 set_response_sensitive (Gtk::RESPONSE_NONE, false);
836         }
837 }
838
839 void
840 NewSessionDialog::recent_row_activated (const Gtk::TreePath& path, Gtk::TreeViewColumn* col)
841 {
842         response (Gtk::RESPONSE_YES);
843 }
844
845 void
846 NewSessionDialog::connect_inputs_clicked ()
847 {
848         m_limit_input_ports->set_sensitive(m_connect_inputs->get_active());
849
850         if (m_connect_inputs->get_active() && m_limit_input_ports->get_active()) {
851                 m_input_limit_count->set_sensitive(true);
852         } else {
853                 m_input_limit_count->set_sensitive(false);
854         }
855 }
856
857 void
858 NewSessionDialog::connect_outputs_clicked ()
859 {
860         m_limit_output_ports->set_sensitive(m_connect_outputs->get_active());
861
862         if (m_connect_outputs->get_active() && m_limit_output_ports->get_active()) {
863                 m_output_limit_count->set_sensitive(true);
864         } else {
865                 m_output_limit_count->set_sensitive(false);
866         }
867 }
868
869 void
870 NewSessionDialog::limit_inputs_clicked ()
871 {
872         m_input_limit_count->set_sensitive(m_limit_input_ports->get_active());
873 }
874
875 void
876 NewSessionDialog::limit_outputs_clicked ()
877 {
878         m_output_limit_count->set_sensitive(m_limit_output_ports->get_active());
879 }
880
881 void
882 NewSessionDialog::master_bus_button_clicked ()
883 {
884         m_master_bus_channel_count->set_sensitive(m_create_master_bus->get_active());
885 }
886
887 void
888 NewSessionDialog::monitor_bus_button_clicked ()
889 {
890         m_control_bus_channel_count->set_sensitive(m_create_control_bus->get_active());
891 }
892
893 void
894 NewSessionDialog::reset_template()
895 {
896         m_template->set_filename("");
897 }
898
899 void
900 NewSessionDialog::reset_recent()
901 {
902         std::vector<sys::path> session_directories;
903         RecentSessionsSorter cmp;
904         
905         recent_model->clear ();
906
907         ARDOUR::RecentSessions rs;
908         ARDOUR::read_recent_sessions (rs);
909         
910         // sort them alphabetically
911         sort (rs.begin(), rs.end(), cmp);
912         
913         for (ARDOUR::RecentSessions::iterator i = rs.begin(); i != rs.end(); ++i) {
914                 session_directories.push_back ((*i).second);
915         }
916         
917         for (vector<sys::path>::const_iterator i = session_directories.begin();
918                         i != session_directories.end(); ++i)
919         {
920                 std::vector<sys::path> state_file_paths;
921             
922                 // now get available states for this session
923
924                 get_state_files_in_directory (*i, state_file_paths);
925
926                 if (state_file_paths.empty()) {
927                         // no state file?
928                         continue;
929                 }
930           
931                 std::vector<string> state_file_names(get_file_names_no_extension (state_file_paths));
932
933                 Gtk::TreeModel::Row row = *(recent_model->append());
934
935                 const string fullpath = (*i).to_string();
936                 
937                 row[recent_columns.visible_name] = Glib::path_get_basename (fullpath);
938                 row[recent_columns.fullpath] = fullpath;
939                 
940                 if (state_file_names.size() > 1) {
941
942                         // add the children
943
944                         for (std::vector<std::string>::iterator i2 = state_file_names.begin();
945                                         i2 != state_file_names.end(); ++i2)
946                         {
947
948                                 Gtk::TreeModel::Row child_row = *(recent_model->append (row.children()));
949
950                                 child_row[recent_columns.visible_name] = *i2;
951                                 child_row[recent_columns.fullpath] = fullpath;
952                         }
953                 }
954         }
955 }
956
957 void
958 NewSessionDialog::reset()
959 {
960         reset_name();
961         reset_template();
962         set_response_sensitive (Gtk::RESPONSE_NONE, false);
963 }