Improve scrolling performance for sessions containing hundreds of regions over many...
[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 "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         entry.set_model (list);
35         entry.set_text_column (cols.label);
36         
37         pack_start (label, false, false, 0);
38         pack_start (entry, true, true, 6);
39         pack_start (save_button, false, false, 0);
40         pack_start (remove_button, false, false, 6);
41         pack_start (new_button, false, false, 0);
42         
43         save_button.set_sensitive (false);
44         remove_button.set_sensitive (false);
45         new_button.set_sensitive (false);
46         
47         select_connection = entry.signal_changed().connect (sigc::mem_fun (*this, &ExportPresetSelector::update_selection));
48         save_button.signal_clicked().connect (sigc::mem_fun (*this, &ExportPresetSelector::save_current));
49         new_button.signal_clicked().connect (sigc::mem_fun (*this, &ExportPresetSelector::save_current));
50         remove_button.signal_clicked().connect (sigc::mem_fun (*this, &ExportPresetSelector::remove_current));
51         
52         show_all_children ();
53 }
54
55 void
56 ExportPresetSelector::set_manager (boost::shared_ptr<ARDOUR::ExportProfileManager> manager)
57 {
58         profile_manager = manager;
59         sync_with_manager ();
60 }
61
62 void
63 ExportPresetSelector::sync_with_manager ()
64 {
65         list->clear();
66         
67         PresetList const & presets = profile_manager->get_presets();
68         Gtk::ListStore::iterator tree_it;
69         
70         for (PresetList::const_iterator it = presets.begin(); it != presets.end(); ++it) {
71                 tree_it = list->append();
72                 tree_it->set_value (cols.preset, *it);
73                 tree_it->set_value (cols.label, Glib::ustring ((*it)->name()));
74                 
75                 if (*it == current) {
76                         select_connection.block (true);
77                         entry.set_active (tree_it);
78                         select_connection.block (false);
79                 }
80         }
81 }
82
83 void
84 ExportPresetSelector::update_selection ()
85 {
86         Gtk::ListStore::iterator it = entry.get_active ();
87         Glib::ustring text = entry.get_entry()->get_text();
88         bool preset_name_exists = false;
89         
90         for (PresetList::const_iterator it = profile_manager->get_presets().begin(); it != profile_manager->get_presets().end(); ++it) {
91                 if (!(*it)->name().compare (text)) { preset_name_exists = true; }
92         }
93         
94         if (list->iter_is_valid (it)) {
95         
96                 previous = current = it->get_value (cols.preset);
97                 if (!profile_manager->load_preset (current)) {
98                         Gtk::MessageDialog dialog (_("The selected preset did not load successfully!\nPerhaps it references a format that has been removed?"),
99                                                    false, Gtk::MESSAGE_WARNING);
100                         dialog.run ();
101                 }
102                 sync_with_manager ();
103                 CriticalSelectionChanged();
104                 
105                 /* Make an edit, so that signal changed will be emitted on re-selection */
106                 
107                 select_connection.block (true);
108                 entry.get_entry()->set_text ("");
109                 entry.get_entry()->set_text (text);
110                 select_connection.block (false);
111         
112         } else { // Text has been edited 
113                 if (previous && !text.compare (previous->name())) {
114                         
115                         current = previous;
116                         
117                 } else {
118                         current.reset ();
119                         profile_manager->load_preset (current);
120                         CriticalSelectionChanged();
121                 }
122         }
123         
124         save_button.set_sensitive (current);
125         remove_button.set_sensitive (current);
126         new_button.set_sensitive (!current && !text.empty() && !preset_name_exists);
127 }
128
129 void
130 ExportPresetSelector::save_current ()
131 {
132         if (!profile_manager) { return; }
133         
134         previous = current = profile_manager->save_preset (entry.get_entry()->get_text());
135         sync_with_manager ();
136         update_selection (); // Update preset widget states
137 }
138
139 void
140 ExportPresetSelector::remove_current ()
141 {
142         if (!profile_manager) { return; }
143         
144         profile_manager->remove_preset();
145         entry.get_entry()->set_text ("");
146         sync_with_manager ();
147 }