Show an example filename in the main export dialog
[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 "pbd/filesystem.h"
26
27 #include "gui_thread.h"
28 #include "utils.h"
29 #include "i18n.h"
30
31 using namespace ARDOUR;
32 using namespace PBD;
33
34 ExportFileNotebook::ExportFileNotebook () :
35   page_counter (1)
36 {
37         /* Last page */
38
39         new_file_button.set_image (*Gtk::manage (new Gtk::Image (::get_icon("add"))));
40         new_file_button.set_label (_(" Click here to add another format"));
41         new_file_button.set_alignment (0, 0.5);
42         new_file_button.set_relief (Gtk::RELIEF_NONE);
43
44         new_file_hbox.pack_start (new_file_button, true, true);
45         append_page (new_file_dummy, new_file_hbox);
46         set_tab_label_packing (new_file_dummy, true, true, Gtk::PACK_START);
47         new_file_hbox.show_all_children ();
48
49         page_change_connection = signal_switch_page().connect (sigc::mem_fun (*this, &ExportFileNotebook::handle_page_change));
50         new_file_button.signal_clicked().connect (sigc::mem_fun (*this, &ExportFileNotebook::add_new_file_page));
51 }
52
53 void
54 ExportFileNotebook::set_session_and_manager (ARDOUR::Session * s, boost::shared_ptr<ARDOUR::ExportProfileManager> manager)
55 {
56         SessionHandlePtr::set_session (s);
57         profile_manager = manager;
58
59         sync_with_manager ();
60 }
61
62 void
63 ExportFileNotebook::sync_with_manager ()
64 {
65         /* Clear pages from notebook
66            The page switch handling has to be disabled during removing all pages due to a gtk bug
67          */
68
69         page_change_connection.block();
70         while (get_n_pages() > 1) {
71                 remove_page (0);
72         }
73         page_change_connection.block(false);
74
75         page_counter = 1;
76         last_visible_page = 0;
77
78         /* File notebook */
79
80         ExportProfileManager::FormatStateList const & formats = profile_manager->get_formats ();
81         ExportProfileManager::FormatStateList::const_iterator format_it;
82         ExportProfileManager::FilenameStateList const & filenames = profile_manager->get_filenames ();
83         ExportProfileManager::FilenameStateList::const_iterator filename_it;
84         for (format_it = formats.begin(), filename_it = filenames.begin();
85              format_it != formats.end() && filename_it != filenames.end();
86              ++format_it, ++filename_it) {
87                 add_file_page (*format_it, *filename_it);
88         }
89
90         set_current_page (0);
91         CriticalSelectionChanged ();
92 }
93
94 void
95 ExportFileNotebook::update_example_filenames()
96 {
97         int i = 0;
98         FilePage * page;
99         while ((page = dynamic_cast<FilePage *> (get_nth_page (i++)))) {
100                 page->update_example_filename();
101         }
102 }
103
104 std::string
105 ExportFileNotebook::get_nth_format_name (uint32_t n)
106 {
107         FilePage * page;
108         if ((page = dynamic_cast<FilePage *> (get_nth_page (n - 1)))) {
109                 return page->get_format_name();
110         }
111         return "";
112 }
113
114 void
115 ExportFileNotebook::add_new_file_page ()
116 {
117         FilePage * page;
118         if ((page = dynamic_cast<FilePage *> (get_nth_page (get_current_page())))) {
119                 add_file_page (profile_manager->duplicate_format_state (page->get_format_state()),
120                                profile_manager->duplicate_filename_state (page->get_filename_state()));
121         }
122 }
123
124 void
125 ExportFileNotebook::add_file_page (ARDOUR::ExportProfileManager::FormatStatePtr format_state, ARDOUR::ExportProfileManager::FilenameStatePtr filename_state)
126 {
127         FilePage * page = Gtk::manage (new FilePage (_session, profile_manager, this, page_counter, format_state, filename_state));
128         page->CriticalSelectionChanged.connect (CriticalSelectionChanged.make_slot());
129         insert_page (*page, page->get_tab_widget(), get_n_pages() - 1);
130
131         update_remove_file_page_sensitivity ();
132         show_all_children();
133         ++page_counter;
134
135         CriticalSelectionChanged ();
136 }
137
138 void
139 ExportFileNotebook::remove_file_page (FilePage * page)
140 {
141         profile_manager->remove_format_state (page->get_format_state());
142         profile_manager->remove_filename_state (page->get_filename_state());
143
144         remove_page (*page);
145         update_remove_file_page_sensitivity ();
146
147         CriticalSelectionChanged ();
148 }
149
150 void
151 ExportFileNotebook::update_remove_file_page_sensitivity ()
152 {
153         FilePage * page;
154         if ((page = dynamic_cast<FilePage *> (get_nth_page (0)))) {
155                 if (get_n_pages() > 2) {
156                         page->set_remove_sensitive (true);
157                 } else {
158                         page->set_remove_sensitive (false);
159                 }
160         }
161 }
162
163 void
164 ExportFileNotebook::handle_page_change (GtkNotebookPage*, uint32_t page)
165 {
166         if (page + 1 == (uint32_t) get_n_pages()) {
167                 set_current_page (last_visible_page);
168         } else {
169                 last_visible_page = page;
170         }
171 }
172
173 ExportFileNotebook::FilePage::FilePage (Session * s, ManagerPtr profile_manager, ExportFileNotebook * parent, uint32_t number,
174                                         ExportProfileManager::FormatStatePtr format_state,
175                                         ExportProfileManager::FilenameStatePtr filename_state) :
176   format_state (format_state),
177   filename_state (filename_state),
178   profile_manager (profile_manager),
179
180   format_label (_("Format"), Gtk::ALIGN_LEFT),
181   filename_label (_("Location"), Gtk::ALIGN_LEFT),
182   tab_number (number)
183 {
184         set_border_width (12);
185
186         pack_start (format_label, false, false, 0);
187         pack_start (format_align, false, false, 0);
188         pack_start (filename_label, false, false, 0);
189         pack_start (filename_align, false, false, 0);
190
191         format_align.add (format_selector);
192         format_align.set_padding (6, 12, 18, 0);
193
194         filename_align.add (filename_selector);
195         filename_align.set_padding (0, 12, 18, 0);
196
197         Pango::AttrList bold;
198         Pango::Attribute b = Pango::Attribute::create_attr_weight (Pango::WEIGHT_BOLD);
199         bold.insert (b);
200
201         format_label.set_attributes (bold);
202         filename_label.set_attributes (bold);
203         tab_label.set_attributes (bold);
204
205         /* Set states */
206         format_selector.set_state (format_state, s);
207         filename_selector.set_state (filename_state, s);
208
209         /* Signals */
210
211         tab_close_button.signal_clicked().connect (sigc::bind (sigc::mem_fun (*parent, &ExportFileNotebook::remove_file_page), this));
212
213         profile_manager->FormatListChanged.connect (format_connection, invalidator (*this), boost::bind (&ExportFormatSelector::update_format_list, &format_selector), gui_context());
214
215         format_selector.FormatEdited.connect (sigc::mem_fun (*this, &ExportFileNotebook::FilePage::save_format_to_manager));
216         format_selector.FormatRemoved.connect (sigc::mem_fun (*profile_manager, &ExportProfileManager::remove_format_profile));
217         format_selector.NewFormat.connect (sigc::mem_fun (*profile_manager, &ExportProfileManager::get_new_format));
218
219         format_selector.CriticalSelectionChanged.connect (
220                 sigc::mem_fun (*this, &ExportFileNotebook::FilePage::critical_selection_changed));
221         filename_selector.CriticalSelectionChanged.connect (
222                 sigc::mem_fun (*this, &ExportFileNotebook::FilePage::critical_selection_changed));
223
224         /* Tab widget */
225
226         tab_close_button.add (*Gtk::manage (new Gtk::Image (::get_icon("close"))));
227         tab_close_alignment.add (tab_close_button);
228         tab_close_alignment.set (0.5, 0.5, 0, 0);
229
230         tab_widget.pack_start (tab_label, false, false, 3);
231         tab_widget.pack_end (tab_close_alignment, false, false, 0);
232         tab_widget.show_all_children ();
233         update_tab_label ();
234         update_example_filename();
235
236         /* Done */
237
238         show_all_children ();
239 }
240
241 ExportFileNotebook::FilePage::~FilePage ()
242 {
243 }
244
245 void
246 ExportFileNotebook::FilePage::set_remove_sensitive (bool value)
247 {
248         tab_close_button.set_sensitive (value);
249 }
250
251 std::string
252 ExportFileNotebook::FilePage::get_format_name () const
253 {
254         if (format_state && format_state->format) {
255                 return format_state->format->name();
256         }
257         return "No format!";
258 }
259
260 void
261 ExportFileNotebook::FilePage::save_format_to_manager (FormatPtr format)
262 {
263         profile_manager->save_format_to_disk (format);
264 }
265
266 void
267 ExportFileNotebook::FilePage::update_tab_label ()
268 {
269         tab_label.set_text (string_compose ("Format %1: %2", tab_number, get_format_name()));
270 }
271
272 void
273 ExportFileNotebook::FilePage::update_example_filename()
274 {
275         if (profile_manager) {
276                 std::string example = profile_manager->get_sample_filename_for_format (
277                         filename_state->filename, format_state->format);
278                 if (example != "") {
279                         sys::path path(example);
280                         filename_selector.set_example_filename(path.leaf());
281                 } else {
282                         filename_selector.set_example_filename("");
283                 }
284         }
285 }
286
287 void
288 ExportFileNotebook::FilePage::critical_selection_changed ()
289 {
290         update_tab_label();
291         update_example_filename();
292         CriticalSelectionChanged();
293 }