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