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