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