the basics of step editing, more details to follow
[ardour.git] / gtk2_ardour / export_file_notebook.cc
1 /*
2     Copyright (C) 2008 Paul Davis
3     Author: Sakari Bergen
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18
19 */
20
21 #include "export_file_notebook.h"
22
23 #include "ardour/export_format_specification.h"
24
25 #include "utils.h"
26 #include "i18n.h"
27
28 using namespace ARDOUR;
29
30 ExportFileNotebook::ExportFileNotebook () :
31   page_counter (1)
32 {
33         /* Last page */
34         
35         new_file_button.set_image (*Gtk::manage (new Gtk::Image (::get_icon("add"))));
36         new_file_button.set_label (_(" Click here to add another format"));
37         new_file_button.set_alignment (0, 0.5);
38         new_file_button.set_relief (Gtk::RELIEF_NONE);
39         
40         new_file_hbox.pack_start (new_file_button, true, true);
41         append_page (new_file_dummy, new_file_hbox);
42         set_tab_label_packing (new_file_dummy, true, true, Gtk::PACK_START);
43         new_file_hbox.show_all_children ();
44         
45         page_change_connection = signal_switch_page().connect (sigc::mem_fun (*this, &ExportFileNotebook::handle_page_change));
46         new_file_button.signal_clicked().connect (sigc::mem_fun (*this, &ExportFileNotebook::add_new_file_page));
47 }
48
49 void
50 ExportFileNotebook::set_session_and_manager (ARDOUR::Session * s, boost::shared_ptr<ARDOUR::ExportProfileManager> manager)
51 {
52         session = s;
53         profile_manager = manager;
54         
55         sync_with_manager ();
56 }
57
58 void
59 ExportFileNotebook::sync_with_manager ()
60 {
61         /* Clear pages from notebook
62            The page switch handling has to be disabled during removing all pages due to a gtk bug
63          */
64
65         page_change_connection.block();
66         while (get_n_pages() > 1) {
67                 remove_page (0);
68         }
69         page_change_connection.block(false);
70         
71         page_counter = 1;
72         last_visible_page = 0;
73
74         /* File notebook */
75
76         ExportProfileManager::FormatStateList const & formats = profile_manager->get_formats ();
77         ExportProfileManager::FormatStateList::const_iterator format_it;
78         ExportProfileManager::FilenameStateList const & filenames = profile_manager->get_filenames ();
79         ExportProfileManager::FilenameStateList::const_iterator filename_it;
80         for (format_it = formats.begin(), filename_it = filenames.begin();
81              format_it != formats.end() && filename_it != filenames.end();
82              ++format_it, ++filename_it) {
83                 add_file_page (*format_it, *filename_it);
84         }
85         
86         set_current_page (0);
87         CriticalSelectionChanged ();
88 }
89
90 Glib::ustring
91 ExportFileNotebook::get_nth_format_name (uint32_t n)
92 {
93         FilePage * page;
94         if ((page = dynamic_cast<FilePage *> (get_nth_page (n - 1)))) {
95                 return page->get_format_name();
96         }
97         return "";
98 }
99
100 void
101 ExportFileNotebook::add_new_file_page ()
102 {
103         FilePage * page;
104         if ((page = dynamic_cast<FilePage *> (get_nth_page (get_current_page())))) {
105                 add_file_page (profile_manager->duplicate_format_state (page->get_format_state()),
106                                profile_manager->duplicate_filename_state (page->get_filename_state()));
107         }
108 }
109
110 void
111 ExportFileNotebook::add_file_page (ARDOUR::ExportProfileManager::FormatStatePtr format_state, ARDOUR::ExportProfileManager::FilenameStatePtr filename_state)
112 {
113         FilePage * page = Gtk::manage (new FilePage (session, profile_manager, this, page_counter, format_state, filename_state));
114         page->CriticalSelectionChanged.connect (CriticalSelectionChanged.make_slot());
115         insert_page (*page, page->get_tab_widget(), get_n_pages() - 1);
116
117         update_remove_file_page_sensitivity ();
118         show_all_children();
119         ++page_counter;
120         
121         CriticalSelectionChanged ();
122 }
123
124 void
125 ExportFileNotebook::remove_file_page (FilePage * page)
126 {
127         profile_manager->remove_format_state (page->get_format_state());
128         profile_manager->remove_filename_state (page->get_filename_state());
129
130         remove_page (*page);
131         update_remove_file_page_sensitivity ();
132         
133         CriticalSelectionChanged ();
134 }
135
136 void
137 ExportFileNotebook::update_remove_file_page_sensitivity ()
138 {
139         FilePage * page;
140         if ((page = dynamic_cast<FilePage *> (get_nth_page (0)))) {
141                 if (get_n_pages() > 2) {
142                         page->set_remove_sensitive (true);
143                 } else {
144                         page->set_remove_sensitive (false);
145                 }
146         }
147 }
148
149 void
150 ExportFileNotebook::handle_page_change (GtkNotebookPage*, uint32_t page)
151 {
152         if (page + 1 == (uint32_t) get_n_pages()) {
153                 set_current_page (last_visible_page);
154         } else {
155                 last_visible_page = page;
156         }
157 }
158
159 ExportFileNotebook::FilePage::FilePage (Session * s, ManagerPtr profile_manager, ExportFileNotebook * parent, uint32_t number,
160                                         ExportProfileManager::FormatStatePtr format_state,
161                                         ExportProfileManager::FilenameStatePtr filename_state) :
162   format_state (format_state),
163   filename_state (filename_state),
164   profile_manager (profile_manager),
165
166   format_label (_("Format"), Gtk::ALIGN_LEFT),
167   filename_label (_("Location"), Gtk::ALIGN_LEFT),
168   tab_number (number)
169 {
170         set_border_width (12);
171
172         pack_start (format_label, false, false, 0);
173         pack_start (format_align, false, false, 0);
174         pack_start (filename_label, false, false, 0);
175         pack_start (filename_align, false, false, 0);
176         
177         format_align.add (format_selector);
178         format_align.set_padding (6, 12, 18, 0);
179         
180         filename_align.add (filename_selector);
181         filename_align.set_padding (0, 12, 18, 0);
182         
183         Pango::AttrList bold;
184         Pango::Attribute b = Pango::Attribute::create_attr_weight (Pango::WEIGHT_BOLD);
185         bold.insert (b);
186         
187         format_label.set_attributes (bold);
188         filename_label.set_attributes (bold);
189         tab_label.set_attributes (bold);
190         
191         /* Set states */
192         format_selector.set_state (format_state, s);
193         filename_selector.set_state (filename_state, s);
194         
195         /* Signals */
196         
197         tab_close_button.signal_clicked().connect (sigc::bind (sigc::mem_fun (*parent, &ExportFileNotebook::remove_file_page), this));
198         
199         profile_manager->FormatListChanged.connect (sigc::mem_fun (format_selector, &ExportFormatSelector::update_format_list));
200         
201         format_selector.FormatEdited.connect (sigc::mem_fun (*this, &ExportFileNotebook::FilePage::save_format_to_manager));
202         format_selector.FormatRemoved.connect (sigc::mem_fun (*profile_manager, &ExportProfileManager::remove_format_profile));
203         format_selector.NewFormat.connect (sigc::mem_fun (*profile_manager, &ExportProfileManager::get_new_format));
204         
205         format_selector.CriticalSelectionChanged.connect (sigc::mem_fun (*this, &ExportFileNotebook::FilePage::update_tab_label));
206         filename_selector.CriticalSelectionChanged.connect (CriticalSelectionChanged.make_slot());
207         
208         /* Tab widget */
209         
210         tab_close_button.add (*Gtk::manage (new Gtk::Image (::get_icon("close"))));
211         tab_close_alignment.add (tab_close_button);
212         tab_close_alignment.set (0.5, 0.5, 0, 0);
213         
214         tab_widget.pack_start (tab_label, false, false, 3);
215         tab_widget.pack_end (tab_close_alignment, false, false, 0);
216         tab_widget.show_all_children ();
217         update_tab_label ();
218         
219         /* Done */
220         
221         show_all_children ();
222 }
223
224 ExportFileNotebook::FilePage::~FilePage ()
225 {
226 }
227
228 void
229 ExportFileNotebook::FilePage::set_remove_sensitive (bool value)
230 {
231         tab_close_button.set_sensitive (value);
232 }
233
234 Glib::ustring
235 ExportFileNotebook::FilePage::get_format_name () const
236 {
237         if (format_state && format_state->format) {
238                 return format_state->format->name();
239         }
240         return "No format!";
241 }
242
243 void
244 ExportFileNotebook::FilePage::save_format_to_manager (FormatPtr format)
245 {
246         profile_manager->save_format_to_disk (format);
247 }
248
249 void
250 ExportFileNotebook::FilePage::update_tab_label ()
251 {
252         tab_label.set_text (string_compose ("Format %1: %2", tab_number, get_format_name()));
253         CriticalSelectionChanged();
254 }