additional API to make it nicer to get an existing action group
[ardour.git] / gtk2_ardour / plugin_presets_ui.cc
1 /*
2  * Copyright (C) 2018 Robin Gareus <robin@gareus.org>
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17  */
18
19 #include "gtkmm2ext/utils.h"
20
21 #include "ardour/plugin.h"
22 #include "gui_thread.h"
23
24 #include "plugin_presets_ui.h"
25
26 #include "pbd/i18n.h"
27
28 using namespace ARDOUR;
29
30 PluginPresetsUI::PluginPresetsUI (boost::shared_ptr<PluginInsert> insert)
31         : _insert (insert)
32         , _load_button (_("Load"))
33 {
34         _plugin_preset_model = Gtk::TreeStore::create (_plugin_preset_columns);
35         _plugin_preset_display.set_model (_plugin_preset_model);
36         _plugin_preset_display.set_headers_visible (true);
37         _plugin_preset_display.get_selection ()->set_mode (Gtk::SELECTION_SINGLE);
38         _plugin_preset_display.get_selection ()->signal_changed ().connect (sigc::mem_fun (*this, &PluginPresetsUI::preset_selected));
39         _plugin_preset_display.set_sensitive (true);
40
41         Gtk::CellRendererText* label_render = Gtk::manage (new Gtk::CellRendererText());
42         Gtk::TreeView::Column* label_col = Gtk::manage (new Gtk::TreeView::Column (_("Preset"), *label_render));
43         label_col->add_attribute (label_render->property_markup(), _plugin_preset_columns.name);
44         _plugin_preset_display.append_column (*label_col);
45
46         _preset_desc.set_editable (false);
47         _preset_desc.set_can_focus (false);
48         _preset_desc.set_wrap_mode (Gtk::WRAP_WORD);
49         _preset_desc.set_size_request (400,200);
50         _preset_desc.set_name (X_("TextOnBackground"));
51         _preset_desc.set_border_width (6);
52
53         _preset_scroller.set_policy (Gtk::POLICY_NEVER, Gtk::POLICY_AUTOMATIC);
54         _preset_scroller.add (_plugin_preset_display);
55
56         _load_button.set_name ("generic button");
57         _load_button.signal_clicked.connect (sigc::mem_fun (*this, &PluginPresetsUI::load_preset));
58         _load_button.set_sensitive (false);
59
60         attach (_preset_scroller, 0, 1, 0, 2, Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 2, 0);
61         attach (_preset_desc, 1, 2, 0, 1, Gtk::EXPAND|Gtk::FILL, Gtk::FILL, 2, 0);
62         attach (_load_button, 1, 2, 1, 2, Gtk::FILL, Gtk::SHRINK, 2, 0);
63
64         boost::shared_ptr<Plugin> plugin (_insert->plugin ());
65
66         plugin->PresetAdded.connect (_preset_connections, invalidator (*this), boost::bind (&PluginPresetsUI::update_preset_list, this), gui_context ());
67         plugin->PresetRemoved.connect (_preset_connections, invalidator (*this), boost::bind (&PluginPresetsUI::update_preset_list, this), gui_context ());
68         plugin->PresetLoaded.connect (_preset_connections, invalidator (*this), boost::bind (&PluginPresetsUI::update_preset_list, this), gui_context ());
69
70         update_preset_list ();
71 }
72
73 void
74 PluginPresetsUI::update_preset_list ()
75 {
76         boost::shared_ptr<Plugin> plugin (_insert->plugin ());
77
78         Plugin::PresetRecord const& p = plugin->last_preset ();
79         std::vector<Plugin::PresetRecord> presets = plugin->get_presets ();
80
81
82         std::string selected_uri;
83         if (_plugin_preset_display.get_selection ()->count_selected_rows () == 1) {
84                 Gtk::TreeIter iter = _plugin_preset_display.get_selection ()->get_selected ();
85                 ARDOUR::Plugin::PresetRecord const& ppr ((*iter)[_plugin_preset_columns.plugin_preset]);
86                 selected_uri = ppr.uri;
87         }
88
89         _plugin_preset_model->clear ();
90
91         bool found_active = false;
92
93         for (std::vector<Plugin::PresetRecord>::const_iterator i = presets.begin (); i != presets.end (); ++i) {
94                 Gtk::TreeModel::Row row = *(_plugin_preset_model->append ());
95                 if (p.uri == i->uri) {
96                         row[_plugin_preset_columns.name] = string_compose ("<span weight=\"bold\"  background=\"green\">%1</span>", Gtkmm2ext::markup_escape_text (i->label));
97                         found_active = true;
98                 } else {
99                         row[_plugin_preset_columns.name] = Gtkmm2ext::markup_escape_text (i->label);
100                 }
101                 row[_plugin_preset_columns.description] = i->description;
102                 row[_plugin_preset_columns.plugin_preset] = *i;
103         }
104
105         {
106                 Gtk::TreeModel::Row row = *(_plugin_preset_model->prepend ());
107                 if (found_active) {
108                         row[_plugin_preset_columns.name] = _("(none)");
109                 } else {
110                         row[_plugin_preset_columns.name] = string_compose ("<span weight=\"bold\"  background=\"green\">%1</span>", _("(none)"));
111                 }
112                 row[_plugin_preset_columns.description] = "";
113                 row[_plugin_preset_columns.plugin_preset] = Plugin::PresetRecord ();
114         }
115
116         Gtk::TreeModel::Children rows = _plugin_preset_model->children ();
117         for (Gtk::TreeModel::Children::iterator i = rows.begin (); i != rows.end (); ++i) {
118                 ARDOUR::Plugin::PresetRecord const& ppr ((*i)[_plugin_preset_columns.plugin_preset]);
119                 if (ppr.uri == selected_uri) {
120                         _plugin_preset_display.get_selection ()->select (*i);
121                         break;
122                 }
123         }
124
125 }
126
127 void
128 PluginPresetsUI::preset_selected ()
129 {
130         if (_plugin_preset_display.get_selection ()->count_selected_rows () != 1) {
131                 return;
132         }
133
134         Gtk::TreeIter iter = _plugin_preset_display.get_selection ()->get_selected ();
135         assert (iter);
136         ARDOUR::Plugin::PresetRecord const& ppr ((*iter)[_plugin_preset_columns.plugin_preset]);
137
138         std::string d;
139         if (!ppr.valid) {
140                 d = "-";
141         } else if (ppr.user) {
142                 d = _("(user preset)");
143         } else {
144                 d = (*iter)[_plugin_preset_columns.description];
145         }
146         _preset_desc.get_buffer ()->set_text (d);
147
148         Plugin::PresetRecord const& p = _insert->plugin ()->last_preset ();
149         _load_button.set_sensitive (ppr.valid && !(p.valid && p.uri == ppr.uri));
150 }
151
152 void
153 PluginPresetsUI::load_preset ()
154 {
155         if (_plugin_preset_display.get_selection ()->count_selected_rows () != 1) {
156                 return;
157         }
158
159         Gtk::TreeIter iter = _plugin_preset_display.get_selection ()->get_selected ();
160         ARDOUR::Plugin::PresetRecord const& ppr ((*iter)[_plugin_preset_columns.plugin_preset]);
161         if (ppr.valid) {
162                 _insert->load_preset (ppr);
163         }
164 }