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