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