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