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