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