Update "By Tags" menu when Plugin-tags change -- #7800
[ardour.git] / gtk2_ardour / plugin_selector.cc
1 /*
2  * Copyright (C) 2005-2006 Doug McLain <doug@nostar.net>
3  * Copyright (C) 2005-2007 Taybin Rutkin <taybin@taybin.com>
4  * Copyright (C) 2005-2017 Paul Davis <paul@linuxaudiosystems.com>
5  * Copyright (C) 2006 Nick Mainsbridge <mainsbridge@gmail.com>
6  * Copyright (C) 2007-2012 David Robillard <d@drobilla.net>
7  * Copyright (C) 2009-2011 Carl Hetherington <carl@carlh.net>
8  * Copyright (C) 2013-2014 John Emmas <john@creativepost.co.uk>
9  * Copyright (C) 2014-2018 Ben Loftis <ben@harrisonconsoles.com>
10  * Copyright (C) 2014-2019 Robin Gareus <robin@gareus.org>
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License along
23  * with this program; if not, write to the Free Software Foundation, Inc.,
24  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25  */
26 #ifdef WAF_BUILD
27 #include "gtk2ardour-config.h"
28 #endif
29
30 #include <cstdio>
31 #include <map>
32
33 #include <algorithm>
34
35 #include <gtkmm/button.h>
36 #include <gtkmm/comboboxtext.h>
37 #include <gtkmm/frame.h>
38 #include <gtkmm/messagedialog.h>
39 #include <gtkmm/notebook.h>
40 #include <gtkmm/stock.h>
41 #include <gtkmm/table.h>
42 #include <gtkmm/treestore.h>
43
44 #include "gtkmm2ext/utils.h"
45
46 #include "widgets/tooltips.h"
47
48 #include "pbd/convert.h"
49 #include "pbd/tokenizer.h"
50
51 #include "ardour/utils.h"
52
53 #include "plugin_selector.h"
54 #include "gui_thread.h"
55 #include "ui_config.h"
56
57 #include "pbd/i18n.h"
58
59 using namespace ARDOUR;
60 using namespace PBD;
61 using namespace Gtk;
62 using namespace std;
63 using namespace ArdourWidgets;
64
65 static const uint32_t MAX_CREATOR_LEN = 24;
66
67 PluginSelector::PluginSelector (PluginManager& mgr)
68         : ArdourDialog (_("Plugin Manager"), true, false)
69         , search_clear_button (Stock::CLEAR)
70         , manager (mgr)
71         , _need_tag_save (false)
72         , _need_status_save (false)
73         , _need_menu_rebuild (false)
74         , _inhibit_refill (false)
75 {
76         set_name ("PluginSelectorWindow");
77         add_events (Gdk::KEY_PRESS_MASK|Gdk::KEY_RELEASE_MASK);
78
79         _plugin_menu = 0;
80         in_row_change = false;
81
82         manager.PluginListChanged.connect (plugin_list_changed_connection, invalidator (*this), boost::bind (&PluginSelector::build_plugin_menu, this), gui_context());
83         manager.PluginStatusChanged.connect (plugin_list_changed_connection, invalidator (*this), boost::bind (&PluginSelector::build_plugin_menu, this), gui_context());
84         manager.PluginTagChanged.connect (plugin_list_changed_connection, invalidator (*this), boost::bind (&PluginSelector::build_plugin_menu, this), gui_context());
85
86         manager.PluginStatusChanged.connect (plugin_list_changed_connection, invalidator (*this), boost::bind (&PluginSelector::plugin_status_changed, this, _1, _2, _3), gui_context());
87         manager.PluginTagChanged.connect(plugin_list_changed_connection, invalidator (*this), boost::bind (&PluginSelector::tags_changed, this, _1, _2, _3), gui_context());
88
89         plugin_model = Gtk::ListStore::create (plugin_columns);
90         plugin_display.set_model (plugin_model);
91         /* XXX translators: try to convert "Fav" into a short term
92          * related to "favorite" and "Hid" into a short term
93          * related to "hidden"
94          */
95         plugin_display.append_column (_("Fav"), plugin_columns.favorite);
96         plugin_display.append_column (_("Hide"), plugin_columns.hidden);
97         plugin_display.append_column (_("Name"), plugin_columns.name);
98         plugin_display.append_column (_("Tags"), plugin_columns.tags);
99         plugin_display.append_column (_("Creator"), plugin_columns.creator);
100         plugin_display.append_column (_("Type"), plugin_columns.type_name);
101         plugin_display.append_column (_("Audio I/O"),plugin_columns.audio_io);
102         plugin_display.append_column (_("MIDI I/O"), plugin_columns.midi_io);
103         plugin_display.set_headers_visible (true);
104         plugin_display.set_headers_clickable (true);
105         plugin_display.set_reorderable (false);
106         plugin_display.set_rules_hint (true);
107         plugin_display.add_object_drag (plugin_columns.plugin.index(), "PluginInfoPtr");
108         plugin_display.set_drag_column (plugin_columns.name.index());
109
110         // setting a sort-column prevents re-ordering via Drag/Drop
111         plugin_model->set_sort_column (plugin_columns.name.index(), Gtk::SORT_ASCENDING);
112
113         plugin_display.set_name("PluginSelectorDisplay");
114         plugin_display.signal_row_activated().connect_notify (sigc::mem_fun(*this, &PluginSelector::row_activated));
115         plugin_display.get_selection()->signal_changed().connect (sigc::mem_fun(*this, &PluginSelector::display_selection_changed));
116
117         CellRendererToggle* fav_cell = dynamic_cast<CellRendererToggle*>(plugin_display.get_column_cell_renderer (0));
118         fav_cell->property_activatable() = true;
119         fav_cell->property_radio() = true;
120         fav_cell->signal_toggled().connect (sigc::mem_fun (*this, &PluginSelector::favorite_changed));
121
122         CellRendererToggle* hidden_cell = dynamic_cast<CellRendererToggle*>(plugin_display.get_column_cell_renderer (1));
123         hidden_cell->property_activatable() = true;
124         hidden_cell->property_radio() = true;
125         hidden_cell->signal_toggled().connect (sigc::mem_fun (*this, &PluginSelector::hidden_changed));
126
127         scroller.set_border_width(10);
128         scroller.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
129         scroller.add(plugin_display);
130
131         amodel = Gtk::ListStore::create(acols);
132         added_list.set_model (amodel);
133         added_list.append_column (_("Plugins to be connected"), acols.text);
134         added_list.set_headers_visible (true);
135         added_list.set_reorderable (false);
136
137         for (int i = 2; i <= 7; ++i) {
138                 Gtk::TreeView::Column* column = plugin_display.get_column(i);
139                 if (column) {
140                         column->set_sort_column(i);
141                 }
142         }
143
144         ascroller.set_border_width(10);
145         ascroller.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
146         ascroller.add(added_list);
147         btn_add = manage(new Gtk::Button(Stock::ADD));
148         set_tooltip(*btn_add, _("Add a plugin to the effect list"));
149         btn_add->set_sensitive (false);
150         btn_remove = manage(new Gtk::Button(Stock::REMOVE));
151         btn_remove->set_sensitive (false);
152         set_tooltip(*btn_remove, _("Remove a plugin from the effect list"));
153
154         btn_add->set_name("PluginSelectorButton");
155         btn_remove->set_name("PluginSelectorButton");
156
157         /* SEARCH */
158
159         Gtk::Table* search_table = manage(new Gtk::Table(2, 2));
160
161         search_entry.signal_changed().connect (sigc::mem_fun (*this, &PluginSelector::search_entry_changed));
162         search_clear_button.signal_clicked().connect (sigc::mem_fun (*this, &PluginSelector::search_clear_button_clicked));
163
164         _search_name_checkbox = manage (new ArdourButton (_("Name"), ArdourButton::led_default_elements, true));
165         _search_name_checkbox->set_active(true);
166         _search_name_checkbox->set_name ("pluginlist filter button");
167
168         _search_tags_checkbox = manage (new ArdourButton (_("Tags"), ArdourButton::led_default_elements, true));
169         _search_tags_checkbox->set_active(true);
170         _search_tags_checkbox->set_name ("pluginlist filter button");
171
172         _search_ignore_checkbox = manage (new ArdourButton(_("Ignore Filters when searching"), ArdourButton::led_default_elements, true));
173         _search_ignore_checkbox->set_active(true);
174         _search_ignore_checkbox->set_name ("pluginlist filter button");
175
176         Gtk::Label* search_help_label1 = manage (new Label(
177                 _("All search terms must be matched."), Gtk::ALIGN_LEFT));
178
179         Gtk::Label* search_help_label2 = manage (new Label(
180                 _("Ex: \"ess dyn\" will find \"dynamic de-esser\" but not \"de-esser\"."), Gtk::ALIGN_LEFT));
181
182         search_table->attach (search_entry,            0, 3, 0, 1, FILL|EXPAND, FILL);
183         search_table->attach (search_clear_button,     3, 4, 0, 1, FILL, FILL);
184         search_table->attach (*_search_name_checkbox,  0, 1, 1, 2, FILL, FILL);
185         search_table->attach (*_search_tags_checkbox,  1, 2, 1, 2, FILL, FILL);
186         search_table->attach (*_search_ignore_checkbox,2, 3, 1, 2, FILL, FILL);
187         search_table->attach (*search_help_label1,     0, 3, 2, 3, FILL, FILL);
188         search_table->attach (*search_help_label2,     0, 3, 3, 4, FILL, FILL);
189
190         search_table->set_border_width (4);
191         search_table->set_col_spacings (4);
192         search_table->set_row_spacings (4);
193
194         Frame* search_frame = manage (new Frame);
195         search_frame->set_name ("BaseFrame");
196         search_frame->set_label (_("Search"));
197         search_frame->add (*search_table);
198         search_frame->show_all ();
199
200         _search_name_checkbox->signal_clicked.connect (sigc::mem_fun (*this, &PluginSelector::refill));
201         _search_tags_checkbox->signal_clicked.connect (sigc::mem_fun (*this, &PluginSelector::refill));
202         _search_ignore_checkbox->signal_clicked.connect (sigc::mem_fun (*this, &PluginSelector::set_sensitive_widgets));
203
204         /* FILTER */
205
206         Gtk::RadioButtonGroup fil_radio_group;
207
208         _fil_effects_radio = manage (new RadioButton (fil_radio_group, _("Show Effects Only")));
209         _fil_instruments_radio = manage (new RadioButton (fil_radio_group, _("Show Instruments Only")));
210         _fil_utils_radio = manage (new RadioButton (fil_radio_group, _("Show Utilities Only")));
211         _fil_favorites_radio = manage (new RadioButton (fil_radio_group, _("Show Favorites Only")));
212         _fil_hidden_radio = manage (new RadioButton (fil_radio_group, _("Show Hidden Only")));
213         _fil_all_radio = manage (new RadioButton (fil_radio_group, _("Show All")));
214
215         //_fil_type_combo = manage (new ComboBoxText);
216         _fil_type_combo.append_text_item (_("Show All Formats"));
217         _fil_type_combo.append_text_item (X_("VST"));
218 #ifdef AUDIOUNIT_SUPPORT
219         _fil_type_combo.append_text_item (X_("AudioUnit"));
220 #endif
221 #ifdef LV2_SUPPORT
222         _fil_type_combo.append_text_item (X_("LV2"));
223 #endif
224         _fil_type_combo.append_text_item (X_("Lua"));
225         _fil_type_combo.append_text_item (X_("LADSPA"));
226         _fil_type_combo.set_text (_("Show All Formats"));
227
228         /* note: _fil_creator_combo menu gets filled in build_plugin_menu */
229         _fil_creator_combo.set_text_ellipsize (Pango::ELLIPSIZE_END);
230         _fil_creator_combo.set_layout_ellipsize_width (PANGO_SCALE * 160 * UIConfiguration::instance ().get_ui_scale ());
231
232         VBox* filter_vbox = manage (new VBox);
233         filter_vbox->pack_start (*_fil_effects_radio,     false, false);
234         filter_vbox->pack_start (*_fil_instruments_radio, false, false);
235         filter_vbox->pack_start (*_fil_utils_radio,       false, false);
236         filter_vbox->pack_start (*_fil_favorites_radio,   false, false);
237         filter_vbox->pack_start (*_fil_hidden_radio,      false, false);
238         filter_vbox->pack_start (*_fil_all_radio,         false, false);
239         filter_vbox->pack_start (_fil_type_combo,         false, false);
240         filter_vbox->pack_start (_fil_creator_combo,      false, false);
241
242         filter_vbox->set_border_width (4);
243         filter_vbox->set_spacing (4);
244
245         Frame* filter_frame = manage (new Frame);
246         filter_frame->set_name ("BaseFrame");
247         filter_frame->set_label (_("Filter"));
248         filter_frame->add (*filter_vbox);
249         filter_frame->show_all ();
250
251         _fil_effects_radio->signal_clicked().connect (sigc::mem_fun (*this, &PluginSelector::refill));
252         _fil_instruments_radio->signal_clicked().connect (sigc::mem_fun (*this, &PluginSelector::refill));
253         _fil_utils_radio->signal_clicked().connect (sigc::mem_fun (*this, &PluginSelector::refill));
254         _fil_favorites_radio->signal_clicked().connect (sigc::mem_fun (*this, &PluginSelector::refill));
255         _fil_hidden_radio->signal_clicked().connect (sigc::mem_fun (*this, &PluginSelector::refill));
256
257         _fil_type_combo.StateChanged.connect (sigc::mem_fun (*this, &PluginSelector::refill));
258         _fil_creator_combo.StateChanged.connect (sigc::mem_fun (*this, &PluginSelector::refill));
259
260         /* TAG entry */
261
262         Gtk::Table* tagging_table = manage(new Gtk::Table(1, 2));
263         tagging_table->set_border_width (4);
264         tagging_table->set_col_spacings (4);
265         tagging_table->set_row_spacings (4);
266
267         tag_entry = manage (new Gtk::Entry);
268         tag_entry_connection = tag_entry->signal_changed().connect (sigc::mem_fun (*this, &PluginSelector::tag_entry_changed));
269
270         tag_reset_button = manage (new Button (_("Reset")));
271         tag_reset_button->signal_clicked().connect (sigc::mem_fun (*this, &PluginSelector::tag_reset_button_clicked));
272
273         Gtk::Label* tagging_help_label1 = manage (new Label(
274                 _("Enter space-separated, one-word Tags for the selected plugin."), Gtk::ALIGN_LEFT));
275
276         Gtk::Label* tagging_help_label2 = manage (new Label(
277                 _("You can include dashes, colons or underscores in a Tag."), Gtk::ALIGN_LEFT));
278
279         Gtk::Label* tagging_help_label3 = manage (new Label(
280                 _("Ex: \"dynamic de-esser vocal\" applies 3 Tags."), Gtk::ALIGN_LEFT));
281
282         int p = 0;
283         tagging_table->attach (*tag_entry,           0, 1, p, p+1, FILL|EXPAND, FILL);
284         tagging_table->attach (*tag_reset_button,    1, 2, p, p+1, FILL, FILL); p++;
285         tagging_table->attach (*tagging_help_label1, 0, 2, p, p+1, FILL, FILL); p++;
286         tagging_table->attach (*tagging_help_label2, 0, 2, p, p+1, FILL, FILL); p++;
287         tagging_table->attach (*tagging_help_label3, 0, 2, p, p+1, FILL, FILL); p++;
288
289         Frame* tag_frame = manage (new Frame);
290         tag_frame->set_name ("BaseFrame");
291         tag_frame->set_label (_("Tags for Selected Plugin"));
292         tag_frame->add (*tagging_table);
293         tag_frame->show_all ();
294
295         /* Add & remove buttons */
296
297         HBox* add_remove = manage (new HBox);
298         add_remove->pack_start (*btn_add, true, true);
299         add_remove->pack_start (*btn_remove, true, true);
300
301         btn_add->signal_clicked().connect(sigc::mem_fun(*this, &PluginSelector::btn_add_clicked));
302         btn_remove->signal_clicked().connect(sigc::mem_fun(*this, &PluginSelector::btn_remove_clicked));
303         added_list.get_selection()->signal_changed().connect (sigc::mem_fun(*this, &PluginSelector::added_list_selection_changed));
304         added_list.signal_button_press_event().connect_notify (mem_fun(*this, &PluginSelector::added_row_clicked));
305
306         added_list.set_name("PluginSelectorList");
307
308         /* Top-level Layout */
309
310         VBox* to_be_inserted_vbox = manage (new VBox);
311         to_be_inserted_vbox->pack_start (ascroller);
312         to_be_inserted_vbox->pack_start (*add_remove, false, false);
313         to_be_inserted_vbox->set_size_request (200, -1);
314
315         Gtk::Table* table = manage(new Gtk::Table(3, 3));
316         table->set_size_request(-1, 600);
317         table->attach (scroller,               0, 3, 0, 5); /* this is the main plugin list */
318         table->attach (*search_frame,          0, 1, 6, 7, FILL, FILL, 5, 5);
319         table->attach (*tag_frame,             0, 1, 7, 8, FILL, FILL, 5, 5);
320         table->attach (*filter_frame,          1, 2, 6, 8, FILL, FILL, 5, 5);
321         table->attach (*to_be_inserted_vbox,   2, 3, 6, 8, FILL|EXPAND, FILL, 5, 5); /* to be inserted... */
322
323         add_button (Stock::CLOSE, RESPONSE_CLOSE);
324         add_button (_("Insert Plugin(s)"), RESPONSE_APPLY);
325         set_default_response (RESPONSE_APPLY);
326         set_response_sensitive (RESPONSE_APPLY, false);
327         get_vbox()->pack_start (*table);
328
329         table->set_name("PluginSelectorTable");
330
331         plugin_display.grab_focus();
332
333         build_plugin_menu ();
334         display_selection_changed ();
335 }
336
337 PluginSelector::~PluginSelector ()
338 {
339         delete _plugin_menu;
340 }
341
342 void
343 PluginSelector::row_activated(Gtk::TreeModel::Path, Gtk::TreeViewColumn*)
344 {
345         btn_add_clicked();
346 }
347
348 void
349 PluginSelector::added_row_clicked(GdkEventButton* event)
350 {
351         if (event->type == GDK_2BUTTON_PRESS)
352                 btn_remove_clicked();
353 }
354
355 bool
356 PluginSelector::show_this_plugin (const PluginInfoPtr& info, const std::string& searchstr)
357 {
358         string mode;
359         bool maybe_show = false;
360         PluginManager::PluginStatusType status = manager.get_status (info);
361
362         if (!searchstr.empty()) {
363
364                 std::string compstr;
365
366                 if (_search_name_checkbox->get_active()) { /* name contains */
367                         compstr = info->name;
368                         transform (compstr.begin(), compstr.end(), compstr.begin(), ::toupper);
369                         if (compstr.find (searchstr) != string::npos) {
370                                 maybe_show = true;
371                         }
372                 }
373
374                 if (_search_tags_checkbox->get_active()) { /* tag contains */
375                         compstr = manager.get_tags_as_string (info);
376                         transform (compstr.begin(), compstr.end(), compstr.begin(), ::toupper);
377                         if (compstr.find (searchstr) != string::npos) {
378                                 maybe_show = true;
379                         }
380                 }
381
382                 if (!maybe_show) {
383                         return false;
384                 }
385
386                 /* user asked to ignore filters */
387                 if (maybe_show && _search_ignore_checkbox->get_active()) {
388                         if (status == PluginManager::Hidden) {
389                                 return false;
390                         }
391                         if (status == PluginManager::Concealed) {
392                                 return false;
393                         }
394                         return true;
395                 }
396         }
397
398         if (_fil_effects_radio->get_active() && !info->is_effect()) {
399                 return false;
400         }
401
402         if (_fil_instruments_radio->get_active() && !info->is_instrument()) {
403                 return false;
404         }
405
406         if (_fil_utils_radio->get_active() && !(info->is_utility() || info->is_analyzer())) {
407                 return false;
408         }
409
410         if (_fil_favorites_radio->get_active() && status != PluginManager::Favorite) {
411                 return false;
412         }
413
414         if (_fil_hidden_radio->get_active() && (status != PluginManager::Hidden && status != PluginManager::Concealed)) {
415                 return false;
416         }
417
418         if (!_fil_hidden_radio->get_active() && status == PluginManager::Hidden) {
419                 return false;
420         }
421
422         if (!_fil_hidden_radio->get_active() && status == PluginManager::Concealed) {
423                 return false;
424         }
425
426         /* Filter "type" combobox */
427
428         if (_fil_type_combo.get_text() == X_("VST") && PluginManager::to_generic_vst(info->type) != LXVST) {
429                 return false;
430         }
431
432         if (_fil_type_combo.get_text() == X_("AudioUnit") && info->type != AudioUnit) {
433                 return false;
434         }
435
436 #ifdef LV2_SUPPORT
437         if (_fil_type_combo.get_text() == X_("LV2") && info->type != LV2) {
438                 return false;
439         }
440 #endif
441
442         if (_fil_type_combo.get_text() == X_("Lua") && info->type != Lua) {
443                 return false;
444         }
445
446         if (_fil_type_combo.get_text() == X_("LADSPA") && info->type != LADSPA) {
447                 return false;
448         }
449
450         /* Filter "creator" combobox */
451
452         if (_fil_creator_combo.get_text() != _("Show All Creators")) {
453                 if (_fil_creator_combo.get_text() != info->creator) {
454                         return false;
455                 }
456         }
457
458         return true;
459 }
460
461 void
462 PluginSelector::setup_search_string (string& searchstr)
463 {
464         searchstr = search_entry.get_text ();
465         transform (searchstr.begin(), searchstr.end(), searchstr.begin(), ::toupper);
466 }
467
468 void
469 PluginSelector::set_sensitive_widgets ()
470 {
471         if (_search_ignore_checkbox->get_active() && !search_entry.get_text().empty()) {
472                 _fil_effects_radio->set_sensitive(false);
473                 _fil_instruments_radio->set_sensitive(false);
474                 _fil_utils_radio->set_sensitive(false);
475                 _fil_favorites_radio->set_sensitive(false);
476                 _fil_hidden_radio->set_sensitive(false);
477                 _fil_all_radio->set_sensitive(false);
478                 _inhibit_refill = true;
479                 _fil_type_combo.set_sensitive(false);
480                 _fil_creator_combo.set_sensitive(false);
481                 _inhibit_refill = false;
482         } else {
483                 _fil_effects_radio->set_sensitive(true);
484                 _fil_instruments_radio->set_sensitive(true);
485                 _fil_utils_radio->set_sensitive(true);
486                 _fil_favorites_radio->set_sensitive(true);
487                 _fil_hidden_radio->set_sensitive(true);
488                 _fil_all_radio->set_sensitive(true);
489                 _inhibit_refill = true;
490                 _fil_type_combo.set_sensitive(true);
491                 _fil_creator_combo.set_sensitive(true);
492                 _inhibit_refill = false;
493         }
494         if (!search_entry.get_text().empty()) {
495                 refill ();
496         }
497 }
498
499 void
500 PluginSelector::refill ()
501 {
502         if (_inhibit_refill) {
503                 return;
504         }
505
506         std::string searchstr;
507
508         in_row_change = true;
509
510         plugin_display.set_model (Glib::RefPtr<Gtk::TreeStore>(0));
511
512         int sort_col;
513         SortType sort_type;
514         bool sorted = plugin_model->get_sort_column_id (sort_col, sort_type);
515
516         /* Disable sorting to gain performance */
517         plugin_model->set_sort_column (-2, SORT_ASCENDING);
518
519         plugin_model->clear ();
520
521         setup_search_string (searchstr);
522
523         ladspa_refiller (searchstr);
524         lv2_refiller (searchstr);
525         vst_refiller (searchstr);
526         lxvst_refiller (searchstr);
527         mac_vst_refiller (searchstr);
528         au_refiller (searchstr);
529         lua_refiller (searchstr);
530
531         in_row_change = false;
532
533         plugin_display.set_model (plugin_model);
534         if (sorted) {
535                 plugin_model->set_sort_column (sort_col, sort_type);
536         }
537 }
538
539 void
540 PluginSelector::refiller (const PluginInfoList& plugs, const::std::string& searchstr, const char* type)
541 {
542         char buf[16];
543
544         for (PluginInfoList::const_iterator i = plugs.begin(); i != plugs.end(); ++i) {
545
546                 if (show_this_plugin (*i, searchstr)) {
547
548                         TreeModel::Row newrow = *(plugin_model->append());
549
550                         PluginManager::PluginStatusType status = manager.get_status (*i);
551                         newrow[plugin_columns.favorite] = status == PluginManager::Favorite;
552                         newrow[plugin_columns.hidden] = status == PluginManager::Hidden;
553
554                         string name = (*i)->name;
555                         if (name.length() > 48) {
556                                 name = name.substr (0, 48);
557                                 name.append("...");
558                         }
559                         newrow[plugin_columns.name] = name;
560
561                         newrow[plugin_columns.type_name] = type;
562
563                         /* Creator */
564                         string creator = (*i)->creator;
565                         string::size_type pos = 0;
566                         if ((*i)->type == ARDOUR::LADSPA) {
567                                 /* stupid LADSPA creator strings */
568 #ifdef PLATFORM_WINDOWS
569                                 while (pos < creator.length() && creator[pos] > -2 && creator[pos] < 256 && (isalnum (creator[pos]) || isspace (creator[pos]))) ++pos;
570 #else
571                                 while (pos < creator.length() && (isalnum (creator[pos]) || isspace (creator[pos]))) ++pos;
572 #endif
573                         } else {
574                                 pos = creator.length ();
575                         }
576                         // If there were too few characters to create a
577                         // meaningful name, mark this creator as 'Unknown'
578                         if (creator.length() < 2 || pos < 3) {
579                                 creator = "Unknown";
580                         } else{
581                                 creator = creator.substr (0, pos);
582                         }
583
584                         if (creator.length() > MAX_CREATOR_LEN) {
585                                 creator = creator.substr (0, MAX_CREATOR_LEN);
586                                 creator.append("...");
587                         }
588                         newrow[plugin_columns.creator] = creator;
589
590                         /* Tags */
591                         string tags = manager.get_tags_as_string(*i);
592                         if (tags.length() > 32) {
593                                 tags = tags.substr (0, 32);
594                                 tags.append("...");
595                         }
596                         newrow[plugin_columns.tags] = tags;
597
598                         if ((*i)->reconfigurable_io ()) {
599                                 newrow[plugin_columns.audio_io] = "* / *";
600                                 newrow[plugin_columns.midi_io] = "* / *";
601                         } else {
602                                 snprintf (buf, sizeof(buf), "%d / %d", (*i)->n_inputs.n_audio(), (*i)->n_outputs.n_audio());
603                                 newrow[plugin_columns.audio_io] = buf;
604                                 snprintf (buf, sizeof(buf), "%d / %d", (*i)->n_inputs.n_midi(), (*i)->n_outputs.n_midi());
605                                 newrow[plugin_columns.midi_io] = buf;
606                         }
607
608                         newrow[plugin_columns.plugin] = *i;
609                 }
610         }
611 }
612
613 void
614 PluginSelector::ladspa_refiller (const std::string& searchstr)
615 {
616         refiller (manager.ladspa_plugin_info(), searchstr, "LADSPA");
617 }
618
619 void
620 PluginSelector::lua_refiller (const std::string& searchstr)
621 {
622         refiller (manager.lua_plugin_info(), searchstr, "Lua");
623 }
624
625 void
626 PluginSelector::lv2_refiller (const std::string& searchstr)
627 {
628 #ifdef LV2_SUPPORT
629         refiller (manager.lv2_plugin_info(), searchstr, "LV2");
630 #endif
631 }
632
633 void
634 #ifdef WINDOWS_VST_SUPPORT
635 PluginSelector::vst_refiller (const std::string& searchstr)
636 #else
637 PluginSelector::vst_refiller (const std::string&)
638 #endif
639 {
640 #ifdef WINDOWS_VST_SUPPORT
641         refiller (manager.windows_vst_plugin_info(), searchstr, "VST");
642 #endif
643 }
644
645 void
646 #ifdef LXVST_SUPPORT
647 PluginSelector::lxvst_refiller (const std::string& searchstr)
648 #else
649 PluginSelector::lxvst_refiller (const std::string&)
650 #endif
651 {
652 #ifdef LXVST_SUPPORT
653         refiller (manager.lxvst_plugin_info(), searchstr, "LXVST");
654 #endif
655 }
656
657 void
658 #ifdef MACVST_SUPPORT
659 PluginSelector::mac_vst_refiller (const std::string& searchstr)
660 #else
661 PluginSelector::mac_vst_refiller (const std::string&)
662 #endif
663 {
664 #ifdef MACVST_SUPPORT
665         refiller (manager.mac_vst_plugin_info(), searchstr, "MacVST");
666 #endif
667 }
668
669 void
670 #ifdef AUDIOUNIT_SUPPORT
671 PluginSelector::au_refiller (const std::string& searchstr)
672 #else
673 PluginSelector::au_refiller (const std::string&)
674 #endif
675 {
676 #ifdef AUDIOUNIT_SUPPORT
677         refiller (manager.au_plugin_info(), searchstr, "AU");
678 #endif
679 }
680
681 PluginPtr
682 PluginSelector::load_plugin (PluginInfoPtr pi)
683 {
684         if (_session == 0) {
685                 return PluginPtr();
686         }
687
688         return pi->load (*_session);
689 }
690
691 void
692 PluginSelector::btn_add_clicked()
693 {
694         if (plugin_display.get_selection()->count_selected_rows() == 0) {
695                 /* may happen with ctrl + double-click un-selecting but activating a row */
696                 return;
697         }
698         std::string name;
699         PluginInfoPtr pi;
700         TreeModel::Row newrow = *(amodel->append());
701         TreeModel::Row row;
702
703         row = *(plugin_display.get_selection()->get_selected());
704         name = row[plugin_columns.name];
705         pi = row[plugin_columns.plugin];
706
707         newrow[acols.text] = name;
708         newrow[acols.plugin] = pi;
709
710         if (!amodel->children().empty()) {
711                 set_response_sensitive (RESPONSE_APPLY, true);
712         }
713 }
714
715 void
716 PluginSelector::btn_remove_clicked()
717 {
718         TreeModel::iterator iter = added_list.get_selection()->get_selected();
719
720         amodel->erase(iter);
721         if (amodel->children().empty()) {
722                 set_response_sensitive (RESPONSE_APPLY, false);
723         }
724 }
725
726 void
727 PluginSelector::display_selection_changed()
728 {
729         tag_entry_connection.block ();
730         if (plugin_display.get_selection()->count_selected_rows() != 0) {
731
732                 /* a plugin row is selected; allow the user to edit the "tags" on it. */
733                 TreeModel::Row row = *(plugin_display.get_selection()->get_selected());
734                 string tags = manager.get_tags_as_string (row[plugin_columns.plugin]);
735                 tag_entry->set_text (tags);
736
737                 tag_entry->set_sensitive (true);
738                 tag_reset_button->set_sensitive (true);
739                 btn_add->set_sensitive (true);
740
741         } else {
742                 tag_entry->set_text ("");
743
744                 tag_entry->set_sensitive (false);
745                 tag_reset_button->set_sensitive (false);
746                 btn_add->set_sensitive (false);
747         }
748         tag_entry_connection.unblock ();
749 }
750
751 void
752 PluginSelector::added_list_selection_changed()
753 {
754         if (added_list.get_selection()->count_selected_rows() != 0) {
755                 btn_remove->set_sensitive (true);
756         } else {
757                 btn_remove->set_sensitive (false);
758         }
759 }
760
761 int
762 PluginSelector::run ()
763 {
764         ResponseType r;
765         TreeModel::Children::iterator i;
766
767         bool finish = false;
768
769         while (!finish) {
770
771                 SelectedPlugins plugins;
772                 r = (ResponseType) Dialog::run ();
773
774                 switch (r) {
775                 case RESPONSE_APPLY:
776                         for (i = amodel->children().begin(); i != amodel->children().end(); ++i) {
777                                 PluginInfoPtr pp = (*i)[acols.plugin];
778                                 PluginPtr p = load_plugin (pp);
779                                 if (p) {
780                                         plugins.push_back (p);
781                                 } else {
782                                         MessageDialog msg (string_compose (_("The plugin \"%1\" could not be loaded\n\nSee the Log window for more details (maybe)"), pp->name));
783                                         msg.run ();
784                                 }
785                         }
786                         if (interested_object && !plugins.empty()) {
787                                 finish = !interested_object->use_plugins (plugins);
788                         }
789
790                         break;
791
792                 default:
793                         finish = true;
794                         break;
795                 }
796         }
797
798
799         hide();
800         amodel->clear();
801         interested_object = 0;
802
803         if (_need_tag_save) {
804                 manager.save_tags();
805         }
806
807         if (_need_status_save) {
808                 manager.save_statuses();
809         }
810         if (_need_menu_rebuild) {
811                 build_plugin_menu ();
812         }
813
814         return (int) r;
815 }
816
817 void
818 PluginSelector::search_clear_button_clicked ()
819 {
820         search_entry.set_text ("");
821 }
822
823 void
824 PluginSelector::tag_reset_button_clicked ()
825 {
826         if (plugin_display.get_selection()->count_selected_rows() != 0) {
827                 TreeModel::Row row = *(plugin_display.get_selection()->get_selected());
828                 ARDOUR::PluginInfoPtr pi = row[plugin_columns.plugin];
829                 manager.reset_tags (pi);
830                 display_selection_changed ();
831                 _need_tag_save = true;
832         }
833 }
834
835 void
836 PluginSelector::search_entry_changed ()
837 {
838         set_sensitive_widgets();
839         if (search_entry.get_text().empty()) {
840                 refill ();
841         }
842 }
843
844 void
845 PluginSelector::tag_entry_changed ()
846 {
847         if (plugin_display.get_selection()->count_selected_rows() != 0) {
848                 TreeModel::Row row = *(plugin_display.get_selection()->get_selected());
849
850                 ARDOUR::PluginInfoPtr pi = row[plugin_columns.plugin];
851                 manager.set_tags (pi->type, pi->unique_id, tag_entry->get_text(), pi->name, PluginManager::FromGui);
852
853                 _need_tag_save = true;
854         }
855 }
856
857 void
858 PluginSelector::tags_changed (PluginType t, std::string unique_id, std::string tags)
859 {
860         if (plugin_display.get_selection()->count_selected_rows() != 0) {
861                 TreeModel::Row row = *(plugin_display.get_selection()->get_selected());
862                 if (tags.length() > 32) {
863                         tags = tags.substr (0, 32);
864                         tags.append ("...");
865                 }
866                 row[plugin_columns.tags] = tags;
867         }
868 }
869
870 void
871 PluginSelector::plugin_status_changed (PluginType t, std::string uid, PluginManager::PluginStatusType stat)
872 {
873         Gtk::TreeModel::iterator i;
874         for (i = plugin_model->children().begin(); i != plugin_model->children().end(); ++i) {
875                 PluginInfoPtr pp = (*i)[plugin_columns.plugin];
876                 if ((pp->type == t) && (pp->unique_id == uid)) {
877                         (*i)[plugin_columns.favorite] = (stat == PluginManager::Favorite) ? true : false;
878                         (*i)[plugin_columns.hidden] = (stat == PluginManager::Hidden) ? true : false;
879
880                         /* if plug was hidden, remove it from the view */
881                         if (stat == PluginManager::Hidden || stat == PluginManager::Concealed) {
882                                 if (!_fil_hidden_radio->get_active() && !_fil_all_radio->get_active()) {
883                                         plugin_model->erase(i);
884                                 }
885                         } else if (_fil_hidden_radio->get_active()) {
886                                 plugin_model->erase(i);
887                         }
888                         /* if no longer a favorite, remove it from the view */
889                         if (stat != PluginManager::Favorite && _fil_favorites_radio->get_active()) {
890                                         plugin_model->erase(i);
891                         }
892
893                         return;
894                 }
895         }
896 }
897
898 void
899 PluginSelector::on_show ()
900 {
901         ArdourDialog::on_show ();
902         search_entry.grab_focus ();
903
904         refill ();
905
906         _need_tag_save = false;
907         _need_status_save = false;
908 }
909
910 struct PluginMenuCompareByCreator {
911         bool operator() (PluginInfoPtr a, PluginInfoPtr b) const {
912                 int cmp;
913
914                 cmp = cmp_nocase_utf8 (a->creator, b->creator);
915
916                 if (cmp < 0) {
917                         return true;
918                 } else if (cmp == 0) {
919                         /* same creator ... compare names */
920                         if (cmp_nocase_utf8 (a->name, b->name) < 0) {
921                                 return true;
922                         }
923                 }
924                 return false;
925         }
926 };
927
928 struct PluginMenuCompareByName {
929         bool operator() (PluginInfoPtr a, PluginInfoPtr b) const {
930                 int cmp;
931
932                 cmp = cmp_nocase_utf8 (a->name, b->name);
933
934                 if (cmp < 0) {
935                         return true;
936                 } else if (cmp == 0) {
937                         /* same name ... compare type */
938                         if (a->type < b->type) {
939                                 return true;
940                         }
941                 }
942                 return false;
943         }
944 };
945
946 /** @return Plugin menu. The caller should not delete it */
947 Gtk::Menu*
948 PluginSelector::plugin_menu()
949 {
950         return _plugin_menu;
951 }
952
953 void
954 PluginSelector::build_plugin_menu ()
955 {
956         if (is_visible ()) {
957                 _need_menu_rebuild = true;
958                 return;
959         }
960         _need_menu_rebuild = false;
961         PluginInfoList all_plugs;
962
963         all_plugs.insert (all_plugs.end(), manager.ladspa_plugin_info().begin(), manager.ladspa_plugin_info().end());
964         all_plugs.insert (all_plugs.end(), manager.lua_plugin_info().begin(), manager.lua_plugin_info().end());
965 #ifdef WINDOWS_VST_SUPPORT
966         all_plugs.insert (all_plugs.end(), manager.windows_vst_plugin_info().begin(), manager.windows_vst_plugin_info().end());
967 #endif
968 #ifdef LXVST_SUPPORT
969         all_plugs.insert (all_plugs.end(), manager.lxvst_plugin_info().begin(), manager.lxvst_plugin_info().end());
970 #endif
971 #ifdef MACVST_SUPPORT
972         all_plugs.insert (all_plugs.end(), manager.mac_vst_plugin_info().begin(), manager.mac_vst_plugin_info().end());
973 #endif
974 #ifdef AUDIOUNIT_SUPPORT
975         all_plugs.insert (all_plugs.end(), manager.au_plugin_info().begin(), manager.au_plugin_info().end());
976 #endif
977 #ifdef LV2_SUPPORT
978         all_plugs.insert (all_plugs.end(), manager.lv2_plugin_info().begin(), manager.lv2_plugin_info().end());
979 #endif
980
981         using namespace Menu_Helpers;
982
983         delete _plugin_menu;
984
985         _plugin_menu = new Menu;
986         _plugin_menu->set_name("ArdourContextMenu");
987
988         MenuList& items = _plugin_menu->items();
989         items.clear ();
990
991         Gtk::Menu* favs = create_favs_menu(all_plugs);
992         items.push_back (MenuElem (_("Favorites"), *manage (favs)));
993
994         items.push_back (MenuElem (_("Plugin Manager..."), sigc::mem_fun (*this, &PluginSelector::show_manager)));
995         items.push_back (SeparatorElem ());
996
997         Menu* by_creator = create_by_creator_menu(all_plugs);
998         items.push_back (MenuElem (_("By Creator"), *manage (by_creator)));
999
1000         Menu* by_tags = create_by_tags_menu(all_plugs);
1001         items.push_back (MenuElem (_("By Tags"), *manage (by_tags)));
1002 }
1003
1004 string
1005 GetPluginTypeStr(PluginInfoPtr info)
1006 {
1007         string type;
1008
1009         switch (info->type) {
1010         case LADSPA:
1011                 type = X_(" (LADSPA)");
1012                 break;
1013         case AudioUnit:
1014                 type = X_(" (AU)");
1015                 break;
1016         case LV2:
1017                 type = X_(" (LV2)");
1018                 break;
1019         case Windows_VST:
1020         case LXVST:
1021         case MacVST:
1022                 type = X_(" (VST)");
1023                 break;
1024         case Lua:
1025                 type = X_(" (Lua)");
1026                 break;
1027         }
1028
1029         return type;
1030 }
1031
1032 Gtk::Menu*
1033 PluginSelector::create_favs_menu (PluginInfoList& all_plugs)
1034 {
1035         using namespace Menu_Helpers;
1036
1037         Menu* favs = new Menu();
1038         favs->set_name("ArdourContextMenu");
1039
1040         PluginMenuCompareByName cmp_by_name;
1041         all_plugs.sort (cmp_by_name);
1042
1043         for (PluginInfoList::const_iterator i = all_plugs.begin(); i != all_plugs.end(); ++i) {
1044                 if (manager.get_status (*i) == PluginManager::Favorite) {
1045                         string typ = GetPluginTypeStr(*i);
1046                         MenuElem elem ((*i)->name + typ, (sigc::bind (sigc::mem_fun (*this, &PluginSelector::plugin_chosen_from_menu), *i)));
1047                         elem.get_child()->set_use_underline (false);
1048                         favs->items().push_back (elem);
1049                 }
1050         }
1051         return favs;
1052 }
1053
1054 Gtk::Menu*
1055 PluginSelector::create_by_creator_menu (ARDOUR::PluginInfoList& all_plugs)
1056 {
1057         _inhibit_refill = true;
1058         _fil_creator_combo.clear_items ();
1059         _fil_creator_combo.append_text_item (_("Show All Creators"));
1060         _fil_creator_combo.set_text (_("Show All Creators"));
1061         _inhibit_refill = false;
1062
1063         using namespace Menu_Helpers;
1064
1065         typedef std::map<std::string,Gtk::Menu*> SubmenuMap;
1066         SubmenuMap creator_submenu_map;
1067
1068         Menu* by_creator = new Menu();
1069         by_creator->set_name("ArdourContextMenu");
1070
1071         MenuList& by_creator_items = by_creator->items();
1072         PluginMenuCompareByCreator cmp_by_creator;
1073         all_plugs.sort (cmp_by_creator);
1074
1075         for (PluginInfoList::const_iterator i = all_plugs.begin(); i != all_plugs.end(); ++i) {
1076
1077                 PluginManager::PluginStatusType status = manager.get_status (*i);
1078                 if (status == PluginManager::Hidden) continue;
1079                 if (status == PluginManager::Concealed) continue;
1080
1081                 string creator = (*i)->creator;
1082
1083                 /* If there were too few characters to create a
1084                  * meaningful name, mark this creator as 'Unknown'
1085                  */
1086                 if (creator.length() < 2) {
1087                         creator = "Unknown";
1088                 }
1089
1090                 SubmenuMap::iterator x;
1091                 Gtk::Menu* submenu;
1092                 if ((x = creator_submenu_map.find (creator)) != creator_submenu_map.end()) {
1093                         submenu = x->second;
1094                 } else {
1095
1096                         _fil_creator_combo.append_text_item (creator);
1097
1098                         submenu = new Gtk::Menu;
1099                         by_creator_items.push_back (MenuElem (creator, *manage (submenu)));
1100                         creator_submenu_map.insert (pair<std::string,Menu*> (creator, submenu));
1101                         submenu->set_name("ArdourContextMenu");
1102                 }
1103                 string typ = GetPluginTypeStr(*i);
1104                 MenuElem elem ((*i)->name+typ, (sigc::bind (sigc::mem_fun (*this, &PluginSelector::plugin_chosen_from_menu), *i)));
1105                 elem.get_child()->set_use_underline (false);
1106                 submenu->items().push_back (elem);
1107         }
1108
1109         return by_creator;
1110 }
1111
1112 Gtk::Menu*
1113 PluginSelector::create_by_tags_menu (ARDOUR::PluginInfoList& all_plugs)
1114 {
1115         using namespace Menu_Helpers;
1116
1117         typedef std::map<std::string,Gtk::Menu*> SubmenuMap;
1118         SubmenuMap tags_submenu_map;
1119
1120         Menu* by_tags = new Menu();
1121         by_tags->set_name("ArdourContextMenu");
1122         MenuList& by_tags_items = by_tags->items();
1123
1124         std::vector<std::string> all_tags = manager.get_all_tags (PluginManager::NoHidden);
1125         for (vector<string>::iterator t = all_tags.begin(); t != all_tags.end(); ++t) {
1126                 Gtk::Menu *submenu = new Gtk::Menu;
1127                 by_tags_items.push_back (MenuElem (*t, *manage (submenu)));
1128                 tags_submenu_map.insert (pair<std::string,Menu*> (*t, submenu));
1129                 submenu->set_name("ArdourContextMenu");
1130         }
1131
1132         PluginMenuCompareByName cmp_by_name;
1133         all_plugs.sort (cmp_by_name);
1134
1135         for (PluginInfoList::const_iterator i = all_plugs.begin(); i != all_plugs.end(); ++i) {
1136
1137                 PluginManager::PluginStatusType status = manager.get_status (*i);
1138                 if (status == PluginManager::Hidden) continue;
1139                 if (status == PluginManager::Concealed) continue;
1140
1141                 /* for each tag in the plugins tag list, add it to that submenu */
1142                 vector<string> tokens = manager.get_tags(*i);
1143                 for (vector<string>::iterator t = tokens.begin(); t != tokens.end(); ++t) {
1144                         SubmenuMap::iterator x;
1145                         Gtk::Menu* submenu;
1146                         if ((x = tags_submenu_map.find (*t)) != tags_submenu_map.end()) {
1147                                 submenu = x->second;
1148                                 string typ = GetPluginTypeStr(*i);
1149                                 MenuElem elem ((*i)->name + typ, (sigc::bind (sigc::mem_fun (*this, &PluginSelector::plugin_chosen_from_menu), *i)));
1150                                 elem.get_child()->set_use_underline (false);
1151                                 submenu->items().push_back (elem);
1152                         }
1153                 }
1154         }
1155         return by_tags;
1156 }
1157
1158 void
1159 PluginSelector::plugin_chosen_from_menu (const PluginInfoPtr& pi)
1160 {
1161         PluginPtr p = load_plugin (pi);
1162
1163         if (p && interested_object) {
1164                 SelectedPlugins plugins;
1165                 plugins.push_back (p);
1166                 interested_object->use_plugins (plugins);
1167         }
1168
1169         interested_object = 0;
1170 }
1171
1172 void
1173 PluginSelector::favorite_changed (const std::string& path)
1174 {
1175         PluginInfoPtr pi;
1176
1177         if (in_row_change) {
1178                 return;
1179         }
1180
1181         in_row_change = true;
1182
1183         TreeModel::iterator iter = plugin_model->get_iter (path);
1184
1185         if (iter) {
1186
1187                 bool favorite = !(*iter)[plugin_columns.favorite];
1188
1189                 /* change state */
1190
1191                 PluginManager::PluginStatusType status = (favorite ? PluginManager::Favorite : PluginManager::Normal);
1192
1193                 /* save new statuses list */
1194
1195                 pi = (*iter)[plugin_columns.plugin];
1196
1197                 manager.set_status (pi->type, pi->unique_id, status);
1198
1199                 _need_status_save = true;
1200         }
1201         in_row_change = false;
1202 }
1203
1204 void
1205 PluginSelector::hidden_changed (const std::string& path)
1206 {
1207         PluginInfoPtr pi;
1208
1209         if (in_row_change) {
1210                 return;
1211         }
1212
1213         in_row_change = true;
1214
1215         TreeModel::iterator iter = plugin_model->get_iter (path);
1216
1217         if (iter) {
1218
1219                 bool hidden = !(*iter)[plugin_columns.hidden];
1220
1221                 /* change state */
1222
1223                 PluginManager::PluginStatusType status = (hidden ? PluginManager::Hidden : PluginManager::Normal);
1224
1225                 /* save new statuses list */
1226
1227                 pi = (*iter)[plugin_columns.plugin];
1228
1229                 manager.set_status (pi->type, pi->unique_id, status);
1230
1231                 _need_status_save = true;
1232         }
1233         in_row_change = false;
1234 }
1235
1236 void
1237 PluginSelector::show_manager ()
1238 {
1239         show_all();
1240         run ();
1241 }
1242
1243 void
1244 PluginSelector::set_interested_object (PluginInterestedObject& obj)
1245 {
1246         interested_object = &obj;
1247 }