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