3c113cebf27a40fa4d246d5226974f1f9c899bcf
[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         std::string path = ARDOUR::get_user_ardour_path() + X_("templates/");
89         if (path == Glib::ustring()) {
90           path = ARDOUR::get_system_data_path() + X_("templates/");
91         }
92         if (path != Glib::ustring()) {
93           m_template->set_current_folder (path);
94         }
95         m_template->set_show_hidden (true);
96         m_new_session_dialog->set_response_sensitive (Gtk::RESPONSE_OK, false);
97         m_new_session_dialog->set_response_sensitive (0, false);
98         m_notebook->show_all_children();
99         m_notebook->set_current_page(0);
100
101         Gtk::FileFilter* filter = manage (new (Gtk::FileFilter));
102
103         filter->add_pattern(X_("*.ardour"));
104         filter->add_pattern(X_("*.ardour.bak"));
105         m_open_filechooser->set_filter (*filter);
106
107         ///@ todo connect some signals
108
109         m_name->signal_key_release_event().connect(mem_fun (*this, &NewSessionDialog::entry_key_release));
110         m_notebook->signal_switch_page().connect (mem_fun (*this, &NewSessionDialog::notebook_page_changed));
111         m_treeview->get_selection()->signal_changed().connect (mem_fun (*this, &NewSessionDialog::treeview_selection_changed));
112         m_treeview->signal_row_activated().connect (mem_fun (*this, &NewSessionDialog::recent_row_activated));
113         m_open_filechooser->signal_selection_changed ().connect (mem_fun (*this, &NewSessionDialog::file_chosen));
114         m_template->signal_selection_changed ().connect (mem_fun (*this, &NewSessionDialog::template_chosen));
115         m_name->grab_focus();
116 }
117
118 void
119 NewSessionDialog::set_session_name(const Glib::ustring& name)
120 {
121         m_name->set_text(name);
122 }
123
124 std::string
125 NewSessionDialog::session_name() const
126 {
127         std::string str = Glib::filename_from_utf8(m_open_filechooser->get_filename());
128         std::string::size_type position = str.find_last_of ('/');
129         str = str.substr (position+1);
130         position = str.find_last_of ('.');
131         str = str.substr (0, position);
132
133         /*
134           XXX what to do if it's a .bak file?
135           load_session doesn't allow it!
136
137         if ((position = str.rfind(".bak")) != string::npos) {
138                 str = str.substr (0, position);
139         }         
140         */
141
142         if (m_notebook->get_current_page() == 0) {
143                 return Glib::filename_from_utf8(m_name->get_text());
144         } else {
145                 if (m_treeview->get_selection()->count_selected_rows() == 0) {
146                         return Glib::filename_from_utf8(str);
147                 }
148                 Gtk::TreeModel::iterator i = m_treeview->get_selection()->get_selected();
149                 return (*i)[recent_columns.visible_name];
150         }
151 }
152
153 std::string
154 NewSessionDialog::session_folder() const
155 {
156         if (m_notebook->get_current_page() == 0) {
157                 return Glib::filename_from_utf8(m_folder->get_current_folder());
158         } else {
159                
160                 if (m_treeview->get_selection()->count_selected_rows() == 0) {
161                         return Glib::filename_from_utf8(m_open_filechooser->get_current_folder());
162                 }
163                 Gtk::TreeModel::iterator i = m_treeview->get_selection()->get_selected();
164                 return (*i)[recent_columns.fullpath];
165         }
166 }
167
168 bool
169 NewSessionDialog::use_session_template() const
170 {
171         if(m_template->get_filename().empty() && (m_notebook->get_current_page() == 0)) return false;
172         return true;
173 }
174
175 std::string
176 NewSessionDialog::session_template_name() const
177 {
178         return Glib::filename_from_utf8(m_template->get_filename());
179 }
180
181 bool
182 NewSessionDialog::create_master_bus() const
183 {
184         return m_create_master_bus->get_active();
185 }
186
187 int
188 NewSessionDialog::master_channel_count() const
189 {
190         return m_master_bus_channel_count->get_value_as_int();
191 }
192
193 bool
194 NewSessionDialog::create_control_bus() const
195 {
196         return m_create_control_bus->get_active();
197 }
198
199 int
200 NewSessionDialog::control_channel_count() const
201 {
202         return m_control_bus_channel_count->get_value_as_int();
203 }
204
205 bool
206 NewSessionDialog::connect_inputs() const
207 {
208         return m_connect_inputs->get_active();
209 }
210
211 bool
212 NewSessionDialog::limit_inputs_used_for_connection() const
213 {
214         return m_limit_input_ports->get_active();
215 }
216
217 int
218 NewSessionDialog::input_limit_count() const
219 {
220         return m_input_limit_count->get_value_as_int();
221 }
222
223 bool
224 NewSessionDialog::connect_outputs() const
225 {
226         return m_connect_outputs->get_active();
227 }
228
229 bool
230 NewSessionDialog::limit_outputs_used_for_connection() const
231 {
232         return m_limit_output_ports->get_active();
233 }
234
235 int
236 NewSessionDialog::output_limit_count() const
237 {
238         return m_output_limit_count->get_value_as_int();
239 }
240
241 bool
242 NewSessionDialog::connect_outs_to_master() const
243 {
244         return m_connect_outputs_to_master->get_active();
245 }
246
247 bool
248 NewSessionDialog::connect_outs_to_physical() const
249 {
250         return m_connect_outputs_to_physical->get_active();
251 }
252
253 int
254 NewSessionDialog::get_current_page()
255 {
256         return m_notebook->get_current_page();
257         
258 }
259
260 void
261 NewSessionDialog::reset_name()
262 {
263         m_name->set_text(Glib::ustring());
264         m_new_session_dialog->set_response_sensitive (Gtk::RESPONSE_OK, false);
265         
266 }
267
268 bool
269 NewSessionDialog::entry_key_release (GdkEventKey* ev)
270 {
271         if (m_name->get_text() != "") {
272                 m_new_session_dialog->set_response_sensitive (Gtk::RESPONSE_OK, true);
273                 m_new_session_dialog->set_response_sensitive (0, true);
274         } else {
275                 m_new_session_dialog->set_response_sensitive (Gtk::RESPONSE_OK, false);
276         }
277         return true;
278 }
279
280 void
281 NewSessionDialog::notebook_page_changed (GtkNotebookPage* np, uint pagenum)
282 {
283         if (pagenum == 1) {
284                 m_new_session_dialog->set_response_sensitive (0, false);
285                 m_okbutton->set_label(_("Open"));
286                 m_okbutton->set_image (*(new Gtk::Image (Gtk::Stock::OPEN, Gtk::ICON_SIZE_BUTTON)));
287                 if (m_treeview->get_selection()->count_selected_rows() == 0) {
288                         m_new_session_dialog->set_response_sensitive (Gtk::RESPONSE_OK, false);
289                 } else {
290                         m_new_session_dialog->set_response_sensitive (Gtk::RESPONSE_OK, true);
291                 }
292         } else {
293                 if (m_name->get_text() != "") {
294                         m_new_session_dialog->set_response_sensitive (0, true);
295                 }
296                 m_okbutton->set_label(_("New"));
297                 m_okbutton->set_image (*(new Gtk::Image (Gtk::Stock::NEW, Gtk::ICON_SIZE_BUTTON)));
298                 if (m_name->get_text() == "") {
299                         m_new_session_dialog->set_response_sensitive (Gtk::RESPONSE_OK, false);
300                 } else {
301                         m_new_session_dialog->set_response_sensitive (Gtk::RESPONSE_OK, true);
302                 }
303         }
304 }
305
306 void
307 NewSessionDialog::treeview_selection_changed ()
308 {
309   if (m_treeview->get_selection()->count_selected_rows() == 0) {
310           if (!m_open_filechooser->get_filename().empty()) {
311                   m_new_session_dialog->set_response_sensitive (Gtk::RESPONSE_OK, true);
312           } else {
313                   m_new_session_dialog->set_response_sensitive (Gtk::RESPONSE_OK, false);
314           }
315   } else {
316           m_new_session_dialog->set_response_sensitive (Gtk::RESPONSE_OK, true);
317   }
318 }
319
320 void
321 NewSessionDialog::file_chosen ()
322 {
323         m_treeview->get_selection()->unselect_all();
324   
325         if (m_treeview->get_selection()->count_selected_rows() == 0) {
326                 m_new_session_dialog->set_response_sensitive (Gtk::RESPONSE_OK, true);
327         }
328 }
329
330 void
331 NewSessionDialog::template_chosen ()
332 {
333   if (m_template->get_filename() != "" ) {;
334     m_new_session_dialog->set_response_sensitive (0, true);
335   } else {
336     m_new_session_dialog->set_response_sensitive (0, false);
337   }
338
339 }
340
341 void
342 NewSessionDialog::recent_row_activated (const Gtk::TreePath& path, Gtk::TreeViewColumn* col)
343 {
344         m_new_session_dialog->response (Gtk::RESPONSE_YES);
345 }
346
347 void
348 NewSessionDialog::reset_template()
349 {
350   m_template->set_filename("");
351 }
352
353 void
354 NewSessionDialog::reset_recent()
355 {
356                 /* Shamelessly ripped from ardour_ui.cc */
357                 std::vector<string *> *sessions;
358                 std::vector<string *>::iterator i;
359                 RecentSessionsSorter cmp;
360                 
361                 recent_model->clear ();
362                 
363                 ARDOUR::RecentSessions rs;
364                 ARDOUR::read_recent_sessions (rs);
365                 
366                 /* sort them alphabetically */
367                 sort (rs.begin(), rs.end(), cmp);
368                 sessions = new std::vector<std::string*>;
369                 
370                 for (ARDOUR::RecentSessions::iterator i = rs.begin(); i != rs.end(); ++i) {
371                         sessions->push_back (new string ((*i).second));
372                 }
373           
374                 for (i = sessions->begin(); i != sessions->end(); ++i) {
375                   
376                   std::vector<std::string*>* states;
377                   std::vector<const gchar*> item;
378                   std::string fullpath = *(*i);
379                   
380                   /* remove any trailing / */
381                   
382                   if (fullpath[fullpath.length()-1] == '/') {
383                           fullpath = fullpath.substr (0, fullpath.length()-1);
384                   }
385             
386                   /* now get available states for this session */
387                   
388                   if ((states = ARDOUR::Session::possible_states (fullpath)) == 0) {
389                           /* no state file? */
390                           continue;
391                   }
392             
393                   Gtk::TreeModel::Row row = *(recent_model->append());
394                   
395                   row[recent_columns.visible_name] = PBD::basename (fullpath);
396                   row[recent_columns.fullpath] = fullpath;
397                   
398                   if (states->size() > 1) {
399                     
400                           /* add the children */
401                     
402                           for (std::vector<std::string*>::iterator i2 = states->begin(); i2 != states->end(); ++i2) {
403                             
404                                   Gtk::TreeModel::Row child_row = *(recent_model->append (row.children()));
405                                   
406                                   child_row[recent_columns.visible_name] = **i2;
407                                   child_row[recent_columns.fullpath] = fullpath;
408                                   
409                                   delete *i2;
410                           }
411                   }
412                   
413                   delete states;
414                 }
415                 delete sessions;
416 }
417
418 void
419 NewSessionDialog::reset()
420 {
421         reset_name();
422         reset_template();
423 }