Some fixes to the new session dialog.
[ardour.git] / gtk2_ardour / new_session_dialog.cc
1 /*
2     Copyright (C) 2005 Paul Davis 
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18     $Id$
19 */
20
21 #include "i18n.h"
22 #include "new_session_dialog.h"
23 #include "glade_path.h"
24
25 #include <ardour/recent_sessions.h>
26 #include <ardour/session.h>
27
28 #include <pbd/basename.h>
29
30 #include <gtkmm/entry.h>
31 #include <gtkmm/filechooserbutton.h>
32 #include <gtkmm/spinbutton.h>
33 #include <gtkmm/checkbutton.h>
34 #include <gtkmm/radiobutton.h>
35 #include <gtkmm/filefilter.h>
36 #include <gtkmm/stock.h>
37
38
39 const char* NewSessionDialogFactory::s_m_top_level_widget_name = X_("NewSessionDialog");
40 const char* NewSessionDialogFactory::top_level_widget_name() { return s_m_top_level_widget_name; }
41
42 Glib::RefPtr<Gnome::Glade::Xml>
43 NewSessionDialogFactory::create()
44 {
45         return GladeFactory::create(GladePath::path(X_("new_session_dialog.glade")));
46 }
47
48
49 NewSessionDialog::NewSessionDialog(BaseObjectType* cobject,
50                                    const Glib::RefPtr<Gnome::Glade::Xml>& xml)
51         : Gtk::Dialog(cobject)    
52 {
53         // look up the widgets we care about.
54         xml->get_widget(X_("NewSessionDialog"), m_new_session_dialog);
55         xml->get_widget(X_("SessionNameEntry"), m_name);
56         xml->get_widget(X_("SessionFolderChooser"), m_folder);
57         xml->get_widget(X_("SessionTemplateChooser"), m_template);
58         
59         xml->get_widget(X_("CreateMasterBus"), m_create_master_bus);
60         xml->get_widget(X_("MasterChannelCount"), m_master_bus_channel_count);
61         
62         xml->get_widget(X_("CreateControlBus"), m_create_control_bus);
63         xml->get_widget(X_("ControlChannelCount"), m_control_bus_channel_count);
64
65         xml->get_widget(X_("ConnectInputs"), m_connect_inputs);
66         xml->get_widget(X_("LimitInputPorts"), m_limit_input_ports);
67         xml->get_widget(X_("InputLimitCount"), m_input_limit_count);
68
69         xml->get_widget(X_("ConnectOutputs"), m_connect_outputs);
70         xml->get_widget(X_("LimitOutputPorts"), m_limit_output_ports);
71         xml->get_widget(X_("OutputLimitCount"), m_output_limit_count);  
72         xml->get_widget(X_("ConnectOutsToMaster"), m_connect_outputs_to_master);
73         xml->get_widget(X_("ConnectOutsToPhysical"), m_connect_outputs_to_physical);
74
75         xml->get_widget(X_("OpenFilechooserButton"), m_open_filechooser);
76         xml->get_widget(X_("TheNotebook"), m_notebook);
77         xml->get_widget(X_("TheTreeview"), m_treeview);
78         xml->get_widget(X_("OkButton"), m_okbutton);
79
80         if (m_treeview) {
81                 recent_model = Gtk::TreeStore::create (recent_columns);
82                 m_treeview->set_model (recent_model);
83                 m_treeview->append_column (_("Recent Sessions"), recent_columns.visible_name);
84                 m_treeview->set_headers_visible (false);
85                 m_treeview->get_selection()->set_mode (Gtk::SELECTION_SINGLE);
86
87         }
88
89         m_new_session_dialog->set_response_sensitive (Gtk::RESPONSE_OK, false);
90         m_new_session_dialog->set_response_sensitive (0, false);
91         m_new_session_dialog->set_default_response (Gtk::RESPONSE_OK);
92         m_notebook->show_all_children();
93         m_notebook->set_current_page(0);
94
95         Gtk::FileFilter* filter = manage (new (Gtk::FileFilter));
96
97         filter->add_pattern(X_("*.ardour"));
98         filter->add_pattern(X_("*.ardour.bak"));
99         m_open_filechooser->set_filter (*filter);
100
101         ///@ todo connect some signals
102
103         m_name->signal_key_release_event().connect(mem_fun (*this, &NewSessionDialog::entry_key_release));
104         m_notebook->signal_switch_page().connect (mem_fun (*this, &NewSessionDialog::notebook_page_changed));
105         m_treeview->get_selection()->signal_changed().connect (mem_fun (*this, &NewSessionDialog::treeview_selection_changed));
106         m_treeview->signal_row_activated().connect (mem_fun (*this, &NewSessionDialog::recent_row_activated));
107         m_open_filechooser->signal_selection_changed ().connect (mem_fun (*this, &NewSessionDialog::file_chosen));
108 }
109
110 void
111 NewSessionDialog::set_session_name(const Glib::ustring& name)
112 {
113         m_name->set_text(name);
114 }
115
116 std::string
117 NewSessionDialog::session_name() const
118 {
119         std::string str = Glib::filename_from_utf8(m_open_filechooser->get_filename());
120         std::string::size_type position = str.find_last_of ('/');
121         str = str.substr (position+1);
122         position = str.find_last_of ('.');
123         str = str.substr (0, position);
124
125         /*
126           XXX what to do if it's a .bak file?
127           load_session doesn't allow it!
128
129         if ((position = str.rfind(".bak")) != string::npos) {
130                 str = str.substr (0, position);
131         }         
132         */
133
134         if (m_notebook->get_current_page() == 0) {
135                 return Glib::filename_from_utf8(m_name->get_text());
136         } else {
137                 if (m_treeview->get_selection()->count_selected_rows() == 0) {
138                         return Glib::filename_from_utf8(str);
139                 }
140                 Gtk::TreeModel::iterator i = m_treeview->get_selection()->get_selected();
141                 return (*i)[recent_columns.visible_name];
142         }
143 }
144
145 std::string
146 NewSessionDialog::session_folder() const
147 {
148         if (m_notebook->get_current_page() == 0) {
149                 return Glib::filename_from_utf8(m_folder->get_current_folder());
150         } else {
151                
152                 if (m_treeview->get_selection()->count_selected_rows() == 0) {
153                         return Glib::filename_from_utf8(m_open_filechooser->get_current_folder());
154                 }
155                 Gtk::TreeModel::iterator i = m_treeview->get_selection()->get_selected();
156                 return (*i)[recent_columns.fullpath];
157         }
158 }
159
160 bool
161 NewSessionDialog::use_session_template() const
162 {
163         if(m_template->get_filename().empty() && (m_notebook->get_current_page() == 0)) return false;
164         return true;
165 }
166
167 std::string
168 NewSessionDialog::session_template_name() const
169 {
170         return Glib::filename_from_utf8(m_template->get_filename());
171 }
172
173 bool
174 NewSessionDialog::create_master_bus() const
175 {
176         return m_create_master_bus->get_active();
177 }
178
179 int
180 NewSessionDialog::master_channel_count() const
181 {
182         return m_master_bus_channel_count->get_value_as_int();
183 }
184
185 bool
186 NewSessionDialog::create_control_bus() const
187 {
188         return m_create_control_bus->get_active();
189 }
190
191 int
192 NewSessionDialog::control_channel_count() const
193 {
194         return m_control_bus_channel_count->get_value_as_int();
195 }
196
197 bool
198 NewSessionDialog::connect_inputs() const
199 {
200         return m_connect_inputs->get_active();
201 }
202
203 bool
204 NewSessionDialog::limit_inputs_used_for_connection() const
205 {
206         return m_limit_input_ports->get_active();
207 }
208
209 int
210 NewSessionDialog::input_limit_count() const
211 {
212         return m_input_limit_count->get_value_as_int();
213 }
214
215 bool
216 NewSessionDialog::connect_outputs() const
217 {
218         return m_connect_outputs->get_active();
219 }
220
221 bool
222 NewSessionDialog::limit_outputs_used_for_connection() const
223 {
224         return m_limit_output_ports->get_active();
225 }
226
227 int
228 NewSessionDialog::output_limit_count() const
229 {
230         return m_output_limit_count->get_value_as_int();
231 }
232
233 bool
234 NewSessionDialog::connect_outs_to_master() const
235 {
236         return m_connect_outputs_to_master->get_active();
237 }
238
239 bool
240 NewSessionDialog::connect_outs_to_physical() const
241 {
242         return m_connect_outputs_to_physical->get_active();
243 }
244
245 int
246 NewSessionDialog::get_current_page()
247 {
248         return m_notebook->get_current_page();
249         
250 }
251
252 void
253 NewSessionDialog::reset_name()
254 {
255         m_name->set_text(Glib::ustring());
256         
257 }
258
259 bool
260 NewSessionDialog::entry_key_release (GdkEventKey* ev)
261 {
262         if (m_name->get_text() != "") {
263                 m_new_session_dialog->set_response_sensitive (Gtk::RESPONSE_OK, true);
264                 m_new_session_dialog->set_response_sensitive (0, true);
265         } else {
266                 m_new_session_dialog->set_response_sensitive (Gtk::RESPONSE_OK, false);
267         }
268         return true;
269 }
270
271 void
272 NewSessionDialog::notebook_page_changed (GtkNotebookPage* np, uint pagenum)
273 {
274         if (pagenum == 1) {
275                 m_new_session_dialog->set_response_sensitive (0, false);
276                 m_okbutton->set_label(_("Open"));
277                 m_okbutton->set_image (*(new Gtk::Image (Gtk::Stock::OPEN, Gtk::ICON_SIZE_BUTTON)));
278                 if (m_treeview->get_selection()->count_selected_rows() == 0) {
279                         m_new_session_dialog->set_response_sensitive (Gtk::RESPONSE_OK, false);
280                 } else {
281                         m_new_session_dialog->set_response_sensitive (Gtk::RESPONSE_OK, true);
282                 }
283         } else {
284                 if (m_name->get_text() != "") {
285                         m_new_session_dialog->set_response_sensitive (0, true);
286                 }
287                 m_okbutton->set_label(_("New"));
288                 m_okbutton->set_image (*(new Gtk::Image (Gtk::Stock::NEW, Gtk::ICON_SIZE_BUTTON)));
289                 if (m_name->get_text() == "") {
290                         m_new_session_dialog->set_response_sensitive (Gtk::RESPONSE_OK, false);
291                 } else {
292                         m_new_session_dialog->set_response_sensitive (Gtk::RESPONSE_OK, true);
293                 }
294         }
295 }
296
297 void
298 NewSessionDialog::treeview_selection_changed ()
299 {
300   if (m_treeview->get_selection()->count_selected_rows() == 0) {
301           if (!m_open_filechooser->get_filename().empty()) {
302                   m_new_session_dialog->set_response_sensitive (Gtk::RESPONSE_OK, true);
303           } else {
304                   m_new_session_dialog->set_response_sensitive (Gtk::RESPONSE_OK, false);
305           }
306   } else {
307           m_new_session_dialog->set_response_sensitive (Gtk::RESPONSE_OK, true);
308   }
309 }
310
311 void
312 NewSessionDialog::file_chosen ()
313 {
314         m_treeview->get_selection()->unselect_all();
315   
316         if (m_treeview->get_selection()->count_selected_rows() == 0) {
317                 m_new_session_dialog->set_response_sensitive (Gtk::RESPONSE_OK, true);
318         }
319 }
320
321 void
322 NewSessionDialog::recent_row_activated (const Gtk::TreePath& path, Gtk::TreeViewColumn* col)
323 {
324         m_new_session_dialog->response (Gtk::RESPONSE_YES);
325 }
326
327 /// @todo
328 void
329 NewSessionDialog::reset_template()
330 {
331
332 }
333
334 void
335 NewSessionDialog::reset_recent()
336 {
337                 /* Shamelessly ripped from ardour_ui.cc */
338                 std::vector<string *> *sessions;
339                 std::vector<string *>::iterator i;
340                 RecentSessionsSorter cmp;
341                 
342                 recent_model->clear ();
343                 
344                 ARDOUR::RecentSessions rs;
345                 ARDOUR::read_recent_sessions (rs);
346                 
347                 /* sort them alphabetically */
348                 sort (rs.begin(), rs.end(), cmp);
349                 sessions = new std::vector<std::string*>;
350                 
351                 for (ARDOUR::RecentSessions::iterator i = rs.begin(); i != rs.end(); ++i) {
352                         sessions->push_back (new string ((*i).second));
353                 }
354           
355                 for (i = sessions->begin(); i != sessions->end(); ++i) {
356                   
357                   std::vector<std::string*>* states;
358                   std::vector<const gchar*> item;
359                   std::string fullpath = *(*i);
360                   
361                   /* remove any trailing / */
362                   
363                   if (fullpath[fullpath.length()-1] == '/') {
364                           fullpath = fullpath.substr (0, fullpath.length()-1);
365                   }
366             
367                   /* now get available states for this session */
368                   
369                   if ((states = ARDOUR::Session::possible_states (fullpath)) == 0) {
370                           /* no state file? */
371                           continue;
372                   }
373             
374                   Gtk::TreeModel::Row row = *(recent_model->append());
375                   
376                   row[recent_columns.visible_name] = PBD::basename (fullpath);
377                   row[recent_columns.fullpath] = fullpath;
378                   
379                   if (states->size() > 1) {
380                     
381                           /* add the children */
382                     
383                           for (std::vector<std::string*>::iterator i2 = states->begin(); i2 != states->end(); ++i2) {
384                             
385                                   Gtk::TreeModel::Row child_row = *(recent_model->append (row.children()));
386                                   
387                                   child_row[recent_columns.visible_name] = **i2;
388                                   child_row[recent_columns.fullpath] = fullpath;
389                                   
390                                   delete *i2;
391                           }
392                   }
393                   
394                   delete states;
395                 }
396                 delete sessions;
397 }
398
399 void
400 NewSessionDialog::reset()
401 {
402         reset_name();
403         reset_template();
404 }