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