enough with umpteen "i18n.h" files. Consolidate on pbd/i18n.h
[ardour.git] / gtk2_ardour / export_preset_selector.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_preset_selector.h"
22
23 #include "ardour/export_preset.h"
24
25 #include "pbd/i18n.h"
26
27 ExportPresetSelector::ExportPresetSelector () :
28   label (_("Preset"), Gtk::ALIGN_LEFT),
29   save_button (Gtk::Stock::SAVE),
30   remove_button (Gtk::Stock::REMOVE),
31   new_button (Gtk::Stock::NEW)
32 {
33         list = Gtk::ListStore::create (cols);
34         list->set_sort_column (cols.label, Gtk::SORT_ASCENDING);
35         entry.set_model (list);
36         entry.set_text_column (cols.label);
37
38         pack_start (label, false, false, 0);
39         pack_start (entry, true, true, 6);
40         pack_start (save_button, false, false, 0);
41         pack_start (remove_button, false, false, 6);
42         pack_start (new_button, false, false, 0);
43
44         save_button.set_sensitive (false);
45         remove_button.set_sensitive (false);
46         new_button.set_sensitive (false);
47
48         select_connection = entry.signal_changed().connect (sigc::mem_fun (*this, &ExportPresetSelector::update_selection));
49         save_button.signal_clicked().connect (sigc::mem_fun (*this, &ExportPresetSelector::save_current));
50         new_button.signal_clicked().connect (sigc::mem_fun (*this, &ExportPresetSelector::create_new));
51         remove_button.signal_clicked().connect (sigc::mem_fun (*this, &ExportPresetSelector::remove_current));
52
53         show_all_children ();
54 }
55
56 void
57 ExportPresetSelector::set_manager (boost::shared_ptr<ARDOUR::ExportProfileManager> manager)
58 {
59         profile_manager = manager;
60         sync_with_manager ();
61 }
62
63 void
64 ExportPresetSelector::sync_with_manager ()
65 {
66         list->clear();
67
68         PresetList const & presets = profile_manager->get_presets();
69         Gtk::ListStore::iterator tree_it;
70
71         for (PresetList::const_iterator it = presets.begin(); it != presets.end(); ++it) {
72                 tree_it = list->append();
73                 tree_it->set_value (cols.preset, *it);
74                 tree_it->set_value (cols.label, std::string ((*it)->name()));
75
76                 if (*it == current) {
77                         select_connection.block (true);
78                         entry.set_active (tree_it);
79                         select_connection.block (false);
80                 }
81         }
82 }
83
84 void
85 ExportPresetSelector::update_selection ()
86 {
87         Gtk::ListStore::iterator it = entry.get_active ();
88         std::string text = entry.get_entry()->get_text();
89         bool preset_name_exists = false;
90
91         for (PresetList::const_iterator it = profile_manager->get_presets().begin(); it != profile_manager->get_presets().end(); ++it) {
92                 if (!(*it)->name().compare (text)) { preset_name_exists = true; }
93         }
94
95         if (list->iter_is_valid (it)) {
96
97                 previous = current = it->get_value (cols.preset);
98                 if (!profile_manager->load_preset (current)) {
99                         Gtk::MessageDialog dialog (_("The selected preset did not load successfully!\nPerhaps it references a format that has been removed?"),
100                                                    false, Gtk::MESSAGE_WARNING);
101                         dialog.run ();
102                 }
103                 sync_with_manager ();
104                 CriticalSelectionChanged();
105
106                 /* Make an edit, so that signal changed will be emitted on re-selection */
107
108                 select_connection.block (true);
109                 entry.get_entry()->set_text ("");
110                 entry.get_entry()->set_text (text);
111                 select_connection.block (false);
112
113         } else { // Text has been edited, this should not make any changes in the profile manager
114                 if (previous && !text.compare (previous->name())) {
115                         current = previous;
116                 } else {
117                         current.reset ();
118                 }
119         }
120
121         save_button.set_sensitive (current != 0);
122         remove_button.set_sensitive (current != 0);
123         new_button.set_sensitive (!current && !text.empty() && !preset_name_exists);
124 }
125
126 void
127 ExportPresetSelector::create_new ()
128 {
129         if (!profile_manager) { return; }
130
131         previous = current = profile_manager->new_preset (entry.get_entry()->get_text());
132         sync_with_manager ();
133         update_selection (); // Update preset widget states
134 }
135
136 void
137 ExportPresetSelector::save_current ()
138 {
139         if (!profile_manager) { return; }
140
141         previous = current = profile_manager->save_preset (entry.get_entry()->get_text());
142         sync_with_manager ();
143         update_selection (); // Update preset widget states
144 }
145
146 void
147 ExportPresetSelector::remove_current ()
148 {
149         if (!profile_manager) { return; }
150
151         Gtk::MessageDialog dialog (_("Do you really want to remove this preset?"),
152                         false,
153                         Gtk::MESSAGE_QUESTION,
154                         Gtk::BUTTONS_YES_NO);
155
156         if (Gtk::RESPONSE_YES != dialog.run ()) {
157                 /* User has selected "no" or closed the dialog, better
158                  * abort
159                  */
160                 return;
161         }
162
163         profile_manager->remove_preset();
164         entry.get_entry()->set_text ("");
165         sync_with_manager ();
166 }