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