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