use PluginInfo::reconfigurable_io() in the plugin selector dialog
[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 <lrdf.h>
25 #include <map>
26
27 #include <algorithm>
28
29 #include <gtkmm/table.h>
30 #include <gtkmm/stock.h>
31 #include <gtkmm/button.h>
32 #include <gtkmm/notebook.h>
33
34 #include <gtkmm2ext/utils.h>
35
36 #include "pbd/convert.h"
37
38 #include "ardour/plugin_manager.h"
39 #include "ardour/plugin.h"
40 #include "ardour/configuration.h"
41 #include "ardour/session.h"
42
43 #include "ardour_ui.h"
44 #include "plugin_selector.h"
45 #include "gui_thread.h"
46
47 #include "i18n.h"
48
49 using namespace ARDOUR;
50 using namespace PBD;
51 using namespace Gtk;
52 using namespace std;
53
54 static const char* _filter_mode_strings[] = {
55         N_("Name contains"),
56         N_("Type contains"),
57         N_("Category contains"),
58         N_("Author contains"),
59         N_("Library contains"),
60         N_("Favorites only"),
61         N_("Hidden only"),
62         0
63 };
64
65 PluginSelector::PluginSelector (PluginManager& mgr)
66         : ArdourDialog (_("Plugin Manager"), true, false)
67         , filter_button (Stock::CLEAR)
68         , manager (mgr)
69           
70 {
71         set_position (Gtk::WIN_POS_MOUSE);
72         set_name ("PluginSelectorWindow");
73         add_events (Gdk::KEY_PRESS_MASK|Gdk::KEY_RELEASE_MASK);
74
75         _plugin_menu = 0;
76         in_row_change = false;
77
78         manager.PluginListChanged.connect (plugin_list_changed_connection, invalidator (*this), boost::bind (&PluginSelector::build_plugin_menu, this), gui_context());
79         build_plugin_menu ();
80
81         plugin_model = Gtk::ListStore::create (plugin_columns);
82         plugin_display.set_model (plugin_model);
83         /* XXX translators: try to convert "Fav" into a short term
84            related to "favorite" and "Hid" into a short term
85            related to "hidden"
86         */
87         plugin_display.append_column (_("Fav"), plugin_columns.favorite);
88         plugin_display.append_column (_("Hid"), plugin_columns.hidden);
89         plugin_display.append_column (_("Available Plugins"), plugin_columns.name);
90         plugin_display.append_column (_("Type"), plugin_columns.type_name);
91         plugin_display.append_column (_("Category"), plugin_columns.category);
92         plugin_display.append_column (_("Creator"), plugin_columns.creator);
93         plugin_display.append_column (_("# Audio In"),plugin_columns.audio_ins);
94         plugin_display.append_column (_("# Audio Out"), plugin_columns.audio_outs);
95         plugin_display.append_column (_("# MIDI In"),plugin_columns.midi_ins);
96         plugin_display.append_column (_("# MIDI Out"), plugin_columns.midi_outs);
97         plugin_display.set_headers_visible (true);
98         plugin_display.set_headers_clickable (true);
99         plugin_display.set_reorderable (false);
100         plugin_display.set_rules_hint (true);
101
102         CellRendererToggle* fav_cell = dynamic_cast<CellRendererToggle*>(plugin_display.get_column_cell_renderer (0));
103         fav_cell->property_activatable() = true;
104         fav_cell->property_radio() = true;
105         fav_cell->signal_toggled().connect (sigc::mem_fun (*this, &PluginSelector::favorite_changed));
106
107         CellRendererToggle* hidden_cell = dynamic_cast<CellRendererToggle*>(plugin_display.get_column_cell_renderer (1));
108         hidden_cell->property_activatable() = true;
109         hidden_cell->property_radio() = true;
110         hidden_cell->signal_toggled().connect (sigc::mem_fun (*this, &PluginSelector::hidden_changed));
111
112         scroller.set_border_width(10);
113         scroller.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
114         scroller.add(plugin_display);
115
116         amodel = Gtk::ListStore::create(acols);
117         added_list.set_model (amodel);
118         added_list.append_column (_("Plugins to be connected"), acols.text);
119         added_list.set_headers_visible (true);
120         added_list.set_reorderable (false);
121
122         for (int i = 0; i <=8; i++) {
123                 Gtk::TreeView::Column* column = plugin_display.get_column(i);
124                 column->set_sort_column(i);
125         }
126
127         ascroller.set_border_width(10);
128         ascroller.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
129         ascroller.add(added_list);
130         btn_add = manage(new Gtk::Button(Stock::ADD));
131         ARDOUR_UI::instance()->set_tip(*btn_add, _("Add a plugin to the effect list"));
132         btn_add->set_sensitive (false);
133         btn_remove = manage(new Gtk::Button(Stock::REMOVE));
134         btn_remove->set_sensitive (false);
135         ARDOUR_UI::instance()->set_tip(*btn_remove, _("Remove a plugin from the effect list"));
136         Gtk::Button *btn_update = manage(new Gtk::Button(Stock::REFRESH));
137         ARDOUR_UI::instance()->set_tip(*btn_update, _("Update available plugins"));
138
139         btn_add->set_name("PluginSelectorButton");
140         btn_remove->set_name("PluginSelectorButton");
141
142         Gtk::Table* table = manage(new Gtk::Table(7, 11));
143         table->set_size_request(750, 500);
144         table->attach(scroller, 0, 7, 0, 5);
145
146         HBox* filter_box = manage (new HBox);
147
148         vector<string> filter_strings = I18N (_filter_mode_strings);
149         Gtkmm2ext::set_popdown_strings (filter_mode, filter_strings);
150         filter_mode.set_active_text (filter_strings.front());
151
152         filter_box->pack_start (filter_mode, false, false);
153         filter_box->pack_start (filter_entry, true, true);
154         filter_box->pack_start (filter_button, false, false);
155
156         filter_entry.signal_changed().connect (sigc::mem_fun (*this, &PluginSelector::filter_entry_changed));
157         filter_button.signal_clicked().connect (sigc::mem_fun (*this, &PluginSelector::filter_button_clicked));
158         filter_mode.signal_changed().connect (sigc::mem_fun (*this, &PluginSelector::filter_mode_changed));
159
160         filter_box->show ();
161         filter_mode.show ();
162         filter_entry.show ();
163         filter_button.show ();
164
165         table->attach (*filter_box, 0, 7, 5, 6, FILL|EXPAND, FILL, 5, 5);
166
167         table->attach(*btn_add, 1, 2, 6, 7, FILL, FILL, 5, 5);
168         table->attach(*btn_remove, 3, 4, 6, 7, FILL, FILL, 5, 5);
169         table->attach(*btn_update, 5, 6, 6, 7, FILL, FILL, 5, 5);
170
171         table->attach(ascroller, 0, 7, 8, 10);
172
173         add_button (Stock::CLOSE, RESPONSE_CLOSE);
174         add_button (_("Insert Plugin(s)"), RESPONSE_APPLY);
175         set_default_response (RESPONSE_APPLY);
176         set_response_sensitive (RESPONSE_APPLY, false);
177         get_vbox()->pack_start (*table);
178
179         table->set_name("PluginSelectorTable");
180         plugin_display.set_name("PluginSelectorDisplay");
181         //plugin_display.set_name("PluginSelectorList");
182         added_list.set_name("PluginSelectorList");
183
184         plugin_display.signal_row_activated().connect_notify (sigc::mem_fun(*this, &PluginSelector::row_activated));
185         plugin_display.get_selection()->signal_changed().connect (sigc::mem_fun(*this, &PluginSelector::display_selection_changed));
186         plugin_display.grab_focus();
187
188         btn_update->signal_clicked().connect (sigc::mem_fun(*this, &PluginSelector::btn_update_clicked));
189         btn_add->signal_clicked().connect(sigc::mem_fun(*this, &PluginSelector::btn_add_clicked));
190         btn_remove->signal_clicked().connect(sigc::mem_fun(*this, &PluginSelector::btn_remove_clicked));
191         added_list.get_selection()->signal_changed().connect (sigc::mem_fun(*this, &PluginSelector::added_list_selection_changed));
192
193         refill ();
194 }
195
196 PluginSelector::~PluginSelector ()
197 {
198         delete _plugin_menu;
199 }
200
201 void
202 PluginSelector::row_activated(Gtk::TreeModel::Path, Gtk::TreeViewColumn*)
203 {
204         btn_add_clicked();
205 }
206
207 bool
208 PluginSelector::show_this_plugin (const PluginInfoPtr& info, const std::string& filterstr)
209 {
210         std::string compstr;
211         std::string mode = filter_mode.get_active_text ();
212
213         if (mode == _("Favorites only")) {
214                 return manager.get_status (info) == PluginManager::Favorite;
215         }
216
217         if (mode == _("Hidden only")) {
218                 return manager.get_status (info) == PluginManager::Hidden;
219         }
220
221         if (!filterstr.empty()) {
222
223                 if (mode == _("Name contains")) {
224                         compstr = info->name;
225                 } else if (mode == _("Category contains")) {
226                         compstr = info->category;
227                 } else if (mode == _("Type contains")) {
228
229                         switch (info->type) {
230                         case LADSPA:
231                                 compstr = X_("LADSPA");
232                                 break;
233                         case AudioUnit:
234                                 compstr = X_("AudioUnit");
235                                 break;
236                         case LV2:
237                                 compstr = X_("LV2");
238                                 break;
239                         case Windows_VST:
240                                 compstr = X_("VST");
241                                 break;
242                         case LXVST:
243                                 compstr = X_("LXVST");
244                                 break;
245                         }
246
247                 } else if (mode == _("Author contains")) {
248                         compstr = info->creator;
249                 } else if (mode == _("Library contains")) {
250                         compstr = info->path;
251                 }
252
253                 if (compstr.empty()) {
254                         return false;
255                 }
256
257                 transform (compstr.begin(), compstr.end(), compstr.begin(), ::toupper);
258
259                 if (compstr.find (filterstr) != string::npos) {
260                         return true;
261                 } else {
262                         return false;
263                 }
264         }
265
266         return true;
267 }
268
269 void
270 PluginSelector::setup_filter_string (string& filterstr)
271 {
272         filterstr = filter_entry.get_text ();
273         transform (filterstr.begin(), filterstr.end(), filterstr.begin(), ::toupper);
274 }
275
276 void
277 PluginSelector::refill ()
278 {
279         std::string filterstr;
280
281         in_row_change = true;
282
283         plugin_model->clear ();
284
285         setup_filter_string (filterstr);
286
287         ladspa_refiller (filterstr);
288         lv2_refiller (filterstr);
289         vst_refiller (filterstr);
290         lxvst_refiller (filterstr);
291         au_refiller (filterstr);
292
293         in_row_change = false;
294 }
295
296 void
297 PluginSelector::refiller (const PluginInfoList& plugs, const::std::string& filterstr, const char* type)
298 {
299         char buf[16];
300
301         for (PluginInfoList::const_iterator i = plugs.begin(); i != plugs.end(); ++i) {
302
303                 if (show_this_plugin (*i, filterstr)) {
304
305                         TreeModel::Row newrow = *(plugin_model->append());
306                         newrow[plugin_columns.favorite] = (manager.get_status (*i) == PluginManager::Favorite);
307                         newrow[plugin_columns.hidden] = (manager.get_status (*i) == PluginManager::Hidden);
308                         newrow[plugin_columns.name] = (*i)->name;
309                         newrow[plugin_columns.type_name] = type;
310                         newrow[plugin_columns.category] = (*i)->category;
311
312                         string creator = (*i)->creator;
313                         string::size_type pos = 0;
314
315                         /* stupid LADSPA creator strings */
316
317                         while (pos < creator.length() && (isalnum (creator[pos]) || isspace (creator[pos]))) ++pos;
318                         creator = creator.substr (0, pos);
319
320                         newrow[plugin_columns.creator] = creator;
321
322                         if ((*i)->reconfigurable_io ()) {
323                                 newrow[plugin_columns.audio_ins] = _("variable");
324                                 newrow[plugin_columns.midi_ins] = _("variable");
325                                 newrow[plugin_columns.audio_outs] = _("variable");
326                                 newrow[plugin_columns.midi_outs] = _("variable");
327                         } else {
328                                 snprintf (buf, sizeof(buf), "%d", (*i)->n_inputs.n_audio());
329                                 newrow[plugin_columns.audio_ins] = buf;
330                                 snprintf (buf, sizeof(buf), "%d", (*i)->n_inputs.n_midi());
331                                 newrow[plugin_columns.midi_ins] = buf;
332                                 
333                                 snprintf (buf, sizeof(buf), "%d", (*i)->n_outputs.n_audio());
334                                 newrow[plugin_columns.audio_outs] = buf;
335                                 snprintf (buf, sizeof(buf), "%d", (*i)->n_outputs.n_midi());
336                                 newrow[plugin_columns.midi_outs] = buf;
337                         }
338
339                         newrow[plugin_columns.plugin] = *i;
340                 }
341         }
342 }
343
344 void
345 PluginSelector::ladspa_refiller (const std::string& filterstr)
346 {
347         refiller (manager.ladspa_plugin_info(), filterstr, "LADSPA");
348 }
349
350 void
351 PluginSelector::lv2_refiller (const std::string& filterstr)
352 {
353 #ifdef LV2_SUPPORT
354         refiller (manager.lv2_plugin_info(), filterstr, "LV2");
355 #endif
356 }
357
358 void
359 #ifdef WINDOWS_VST_SUPPORT
360 PluginSelector::vst_refiller (const std::string& filterstr)
361 #else
362 PluginSelector::vst_refiller (const std::string&)
363 #endif
364 {
365 #ifdef WINDOWS_VST_SUPPORT
366         refiller (manager.windows_vst_plugin_info(), filterstr, "VST");
367 #endif
368 }
369
370 void
371 #ifdef LXVST_SUPPORT
372 PluginSelector::lxvst_refiller (const std::string& filterstr)
373 #else
374 PluginSelector::lxvst_refiller (const std::string&)
375 #endif
376 {
377 #ifdef LXVST_SUPPORT
378         refiller (manager.lxvst_plugin_info(), filterstr, "LXVST");
379 #endif
380 }
381
382 void
383 #ifdef AUDIOUNIT_SUPPORT
384 PluginSelector::au_refiller (const std::string& filterstr)
385 #else
386 PluginSelector::au_refiller (const std::string&)
387 #endif
388 {
389 #ifdef AUDIOUNIT_SUPPORT
390         refiller (manager.au_plugin_info(), filterstr, "AU");
391 #endif
392 }
393
394 PluginPtr
395 PluginSelector::load_plugin (PluginInfoPtr pi)
396 {
397         if (_session == 0) {
398                 return PluginPtr();
399         }
400
401         return pi->load (*_session);
402 }
403
404 void
405 PluginSelector::btn_add_clicked()
406 {
407         std::string name;
408         PluginInfoPtr pi;
409         TreeModel::Row newrow = *(amodel->append());
410         TreeModel::Row row;
411
412         row = *(plugin_display.get_selection()->get_selected());
413         name = row[plugin_columns.name];
414         pi = row[plugin_columns.plugin];
415
416         newrow[acols.text] = name;
417         newrow[acols.plugin] = pi;
418
419         if (!amodel->children().empty()) {
420                 set_response_sensitive (RESPONSE_APPLY, true);
421         }
422 }
423
424 void
425 PluginSelector::btn_remove_clicked()
426 {
427         TreeModel::iterator iter = added_list.get_selection()->get_selected();
428
429         amodel->erase(iter);
430         if (amodel->children().empty()) {
431                 set_response_sensitive (RESPONSE_APPLY, false);
432         }
433 }
434
435 void
436 PluginSelector::btn_update_clicked()
437 {
438         manager.refresh ();
439         refill();
440 }
441
442 void
443 PluginSelector::display_selection_changed()
444 {
445         if (plugin_display.get_selection()->count_selected_rows() != 0) {
446                 btn_add->set_sensitive (true);
447         } else {
448                 btn_add->set_sensitive (false);
449         }
450 }
451
452 void
453 PluginSelector::added_list_selection_changed()
454 {
455         if (added_list.get_selection()->count_selected_rows() != 0) {
456                 btn_remove->set_sensitive (true);
457         } else {
458                 btn_remove->set_sensitive (false);
459         }
460 }
461
462 int
463 PluginSelector::run ()
464 {
465         ResponseType r;
466         TreeModel::Children::iterator i;
467
468         bool finish = false;
469
470         while (!finish) {
471
472                 SelectedPlugins plugins;
473                 r = (ResponseType) Dialog::run ();
474
475                 switch (r) {
476                 case RESPONSE_APPLY:
477                         for (i = amodel->children().begin(); i != amodel->children().end(); ++i) {
478                                 PluginInfoPtr pp = (*i)[acols.plugin];
479                                 PluginPtr p = load_plugin (pp);
480                                 if (p) {
481                                         plugins.push_back (p);
482                                 } else {
483                                         MessageDialog msg (string_compose (_("The plugin \"%1\" could not be loaded\n\nSee the Log window for more details (maybe)"), pp->name));
484                                         msg.run ();
485                                 }
486                         }
487                         if (interested_object && !plugins.empty()) {
488                                 finish = !interested_object->use_plugins (plugins);
489                         }
490
491                         break;
492
493                 default:
494                         finish = true;
495                         break;
496                 }
497         }
498
499
500         hide();
501         amodel->clear();
502         interested_object = 0;
503
504         return (int) r;
505 }
506
507 void
508 PluginSelector::filter_button_clicked ()
509 {
510         filter_entry.set_text ("");
511 }
512
513 void
514 PluginSelector::filter_entry_changed ()
515 {
516         refill ();
517 }
518
519 void
520 PluginSelector::filter_mode_changed ()
521 {
522         std::string mode = filter_mode.get_active_text ();
523
524         if (mode == _("Favorites only") || mode == _("Hidden only")) {
525                 filter_entry.set_sensitive (false);
526         } else {
527                 filter_entry.set_sensitive (true);
528         }
529
530         refill ();
531 }
532
533 void
534 PluginSelector::on_show ()
535 {
536         ArdourDialog::on_show ();
537         filter_entry.grab_focus ();
538 }
539
540 struct PluginMenuCompareByCreator {
541     bool operator() (PluginInfoPtr a, PluginInfoPtr b) const {
542             int cmp;
543
544             cmp = strcasecmp (a->creator.c_str(), b->creator.c_str());
545
546             if (cmp < 0) {
547                     return true;
548             } else if (cmp == 0) {
549                     /* same creator ... compare names */
550                     if (strcasecmp (a->name.c_str(), b->name.c_str()) < 0) {
551                             return true;
552                     }
553             }
554             return false;
555     }
556 };
557
558 struct PluginMenuCompareByName {
559     bool operator() (PluginInfoPtr a, PluginInfoPtr b) const {
560             int cmp;
561
562             cmp = strcasecmp (a->name.c_str(), b->name.c_str());
563
564             if (cmp < 0) {
565                     return true;
566             } else if (cmp == 0) {
567                     /* same name ... compare type */
568                     if (a->type < b->type) {
569                             return true;
570                     }
571             }
572             return false;
573     }
574 };
575
576 struct PluginMenuCompareByCategory {
577     bool operator() (PluginInfoPtr a, PluginInfoPtr b) const {
578             int cmp;
579
580             cmp = strcasecmp (a->category.c_str(), b->category.c_str());
581
582             if (cmp < 0) {
583                     return true;
584             } else if (cmp == 0) {
585                     /* same category ... compare names */
586                     if (strcasecmp (a->name.c_str(), b->name.c_str()) < 0) {
587                             return true;
588                     }
589             }
590             return false;
591     }
592 };
593
594 /** @return Plugin menu. The caller should not delete it */
595 Gtk::Menu*
596 PluginSelector::plugin_menu()
597 {
598         return _plugin_menu;
599 }
600
601 void
602 PluginSelector::build_plugin_menu ()
603 {
604         PluginInfoList all_plugs;
605
606         all_plugs.insert (all_plugs.end(), manager.ladspa_plugin_info().begin(), manager.ladspa_plugin_info().end());
607 #ifdef WINDOWS_VST_SUPPORT
608         all_plugs.insert (all_plugs.end(), manager.windows_vst_plugin_info().begin(), manager.windows_vst_plugin_info().end());
609 #endif
610 #ifdef LXVST_SUPPORT
611         all_plugs.insert (all_plugs.end(), manager.lxvst_plugin_info().begin(), manager.lxvst_plugin_info().end());
612 #endif
613 #ifdef AUDIOUNIT_SUPPORT
614         all_plugs.insert (all_plugs.end(), manager.au_plugin_info().begin(), manager.au_plugin_info().end());
615 #endif
616 #ifdef LV2_SUPPORT
617         all_plugs.insert (all_plugs.end(), manager.lv2_plugin_info().begin(), manager.lv2_plugin_info().end());
618 #endif
619
620         using namespace Menu_Helpers;
621
622         delete _plugin_menu;
623
624         _plugin_menu = manage (new Menu);
625         _plugin_menu->set_name("ArdourContextMenu");
626
627         MenuList& items = _plugin_menu->items();
628         items.clear ();
629
630         Gtk::Menu* favs = create_favs_menu(all_plugs);
631         items.push_back (MenuElem (_("Favorites"), *manage (favs)));
632
633         items.push_back (MenuElem (_("Plugin Manager..."), sigc::mem_fun (*this, &PluginSelector::show_manager)));
634         items.push_back (SeparatorElem ());
635
636         Menu* by_creator = create_by_creator_menu(all_plugs);
637         items.push_back (MenuElem (_("By Creator"), *manage (by_creator)));
638
639         Menu* by_category = create_by_category_menu(all_plugs);
640         items.push_back (MenuElem (_("By Category"), *manage (by_category)));
641 }
642
643 Gtk::Menu*
644 PluginSelector::create_favs_menu (PluginInfoList& all_plugs)
645 {
646         using namespace Menu_Helpers;
647
648         Menu* favs = new Menu();
649         favs->set_name("ArdourContextMenu");
650
651         PluginMenuCompareByName cmp_by_name;
652         all_plugs.sort (cmp_by_name);
653
654         for (PluginInfoList::const_iterator i = all_plugs.begin(); i != all_plugs.end(); ++i) {
655                 if (manager.get_status (*i) == PluginManager::Favorite) {
656                         MenuElem elem ((*i)->name, (sigc::bind (sigc::mem_fun (*this, &PluginSelector::plugin_chosen_from_menu), *i)));
657                         elem.get_child()->set_use_underline (false);
658                         favs->items().push_back (elem);
659                 }
660         }
661         return favs;
662 }
663
664 Gtk::Menu*
665 PluginSelector::create_by_creator_menu (ARDOUR::PluginInfoList& all_plugs)
666 {
667         using namespace Menu_Helpers;
668
669         typedef std::map<std::string,Gtk::Menu*> SubmenuMap;
670         SubmenuMap creator_submenu_map;
671
672         Menu* by_creator = new Menu();
673         by_creator->set_name("ArdourContextMenu");
674
675         MenuList& by_creator_items = by_creator->items();
676         PluginMenuCompareByCreator cmp_by_creator;
677         all_plugs.sort (cmp_by_creator);
678
679         for (PluginInfoList::const_iterator i = all_plugs.begin(); i != all_plugs.end(); ++i) {
680
681                 if (manager.get_status (*i) == PluginManager::Hidden) continue;
682
683                 string creator = (*i)->creator;
684
685                 /* stupid LADSPA creator strings */
686                 string::size_type pos = 0;
687                 while (pos < creator.length() && (isalnum (creator[pos]) || isspace (creator[pos]))) ++pos;
688                 creator = creator.substr (0, pos);
689
690                 SubmenuMap::iterator x;
691                 Gtk::Menu* submenu;
692                 if ((x = creator_submenu_map.find (creator)) != creator_submenu_map.end()) {
693                         submenu = x->second;
694                 } else {
695                         submenu = new Gtk::Menu;
696                         by_creator_items.push_back (MenuElem (creator, *manage (submenu)));
697                         creator_submenu_map.insert (pair<std::string,Menu*> (creator, submenu));
698                         submenu->set_name("ArdourContextMenu");
699                 }
700                 MenuElem elem ((*i)->name, (sigc::bind (sigc::mem_fun (*this, &PluginSelector::plugin_chosen_from_menu), *i)));
701                 elem.get_child()->set_use_underline (false);
702                 submenu->items().push_back (elem);
703         }
704         return by_creator;
705 }
706
707 Gtk::Menu*
708 PluginSelector::create_by_category_menu (ARDOUR::PluginInfoList& all_plugs)
709 {
710         using namespace Menu_Helpers;
711
712         typedef std::map<std::string,Gtk::Menu*> SubmenuMap;
713         SubmenuMap category_submenu_map;
714
715         Menu* by_category = new Menu();
716         by_category->set_name("ArdourContextMenu");
717
718         MenuList& by_category_items = by_category->items();
719         PluginMenuCompareByCategory cmp_by_category;
720         all_plugs.sort (cmp_by_category);
721
722         for (PluginInfoList::const_iterator i = all_plugs.begin(); i != all_plugs.end(); ++i) {
723
724                 if (manager.get_status (*i) == PluginManager::Hidden) continue;
725
726                 string category = (*i)->category;
727
728                 SubmenuMap::iterator x;
729                 Gtk::Menu* submenu;
730                 if ((x = category_submenu_map.find (category)) != category_submenu_map.end()) {
731                         submenu = x->second;
732                 } else {
733                         submenu = new Gtk::Menu;
734                         by_category_items.push_back (MenuElem (category, *manage (submenu)));
735                         category_submenu_map.insert (pair<std::string,Menu*> (category, submenu));
736                         submenu->set_name("ArdourContextMenu");
737                 }
738                 MenuElem elem ((*i)->name, (sigc::bind (sigc::mem_fun (*this, &PluginSelector::plugin_chosen_from_menu), *i)));
739                 elem.get_child()->set_use_underline (false);
740                 submenu->items().push_back (elem);
741         }
742         return by_category;
743 }
744
745 void
746 PluginSelector::plugin_chosen_from_menu (const PluginInfoPtr& pi)
747 {
748         PluginPtr p = load_plugin (pi);
749
750         if (p && interested_object) {
751                 SelectedPlugins plugins;
752                 plugins.push_back (p);
753                 interested_object->use_plugins (plugins);
754         }
755
756         interested_object = 0;
757 }
758
759 void
760 PluginSelector::favorite_changed (const std::string& path)
761 {
762         PluginInfoPtr pi;
763
764         if (in_row_change) {
765                 return;
766         }
767
768         in_row_change = true;
769
770         TreeModel::iterator iter = plugin_model->get_iter (path);
771
772         if (iter) {
773
774                 bool favorite = !(*iter)[plugin_columns.favorite];
775
776                 /* change state */
777
778                 (*iter)[plugin_columns.favorite] = favorite;
779                 (*iter)[plugin_columns.hidden] = false;
780                 PluginManager::PluginStatusType status = (favorite ? PluginManager::Favorite : PluginManager::Normal);
781
782                 /* save new statuses list */
783
784                 pi = (*iter)[plugin_columns.plugin];
785
786                 manager.set_status (pi->type, pi->unique_id, status);
787
788                 manager.save_statuses ();
789
790                 build_plugin_menu ();
791         }
792         in_row_change = false;
793 }
794
795 void
796 PluginSelector::hidden_changed (const std::string& path)
797 {
798         PluginInfoPtr pi;
799
800         if (in_row_change) {
801                 return;
802         }
803
804         in_row_change = true;
805
806         TreeModel::iterator iter = plugin_model->get_iter (path);
807
808         if (iter) {
809
810                 bool hidden = !(*iter)[plugin_columns.hidden];
811
812                 /* change state */
813
814                 (*iter)[plugin_columns.favorite] = false;
815                 (*iter)[plugin_columns.hidden] = hidden;
816                 PluginManager::PluginStatusType status = (hidden ? PluginManager::Hidden : PluginManager::Normal);
817
818                 /* save new statuses list */
819
820                 pi = (*iter)[plugin_columns.plugin];
821
822                 manager.set_status (pi->type, pi->unique_id, status);
823
824                 manager.save_statuses ();
825         }
826         in_row_change = false;
827 }
828
829 void
830 PluginSelector::show_manager ()
831 {
832         show_all();
833         run ();
834 }
835
836 void
837 PluginSelector::set_interested_object (PluginInterestedObject& obj)
838 {
839         interested_object = &obj;
840 }