moron
[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                         snprintf (buf, sizeof(buf), "%d", (*i)->n_inputs.n_audio());
323                         newrow[plugin_columns.audio_ins] = buf;
324                         snprintf (buf, sizeof(buf), "%d", (*i)->n_inputs.n_midi());
325                         newrow[plugin_columns.midi_ins] = buf;
326
327                         snprintf (buf, sizeof(buf), "%d", (*i)->n_outputs.n_audio());
328                         newrow[plugin_columns.audio_outs] = buf;
329                         snprintf (buf, sizeof(buf), "%d", (*i)->n_outputs.n_midi());
330                         newrow[plugin_columns.midi_outs] = buf;
331
332                         newrow[plugin_columns.plugin] = *i;
333                 }
334         }
335 }
336
337 void
338 PluginSelector::ladspa_refiller (const std::string& filterstr)
339 {
340         refiller (manager.ladspa_plugin_info(), filterstr, "LADSPA");
341 }
342
343 void
344 PluginSelector::lv2_refiller (const std::string& filterstr)
345 {
346 #ifdef LV2_SUPPORT
347         refiller (manager.lv2_plugin_info(), filterstr, "LV2");
348 #endif
349 }
350
351 void
352 #ifdef WINDOWS_VST_SUPPORT
353 PluginSelector::vst_refiller (const std::string& filterstr)
354 #else
355 PluginSelector::vst_refiller (const std::string&)
356 #endif
357 {
358 #ifdef WINDOWS_VST_SUPPORT
359         refiller (manager.windows_vst_plugin_info(), filterstr, "VST");
360 #endif
361 }
362
363 void
364 #ifdef LXVST_SUPPORT
365 PluginSelector::lxvst_refiller (const std::string& filterstr)
366 #else
367 PluginSelector::lxvst_refiller (const std::string&)
368 #endif
369 {
370 #ifdef LXVST_SUPPORT
371         refiller (manager.lxvst_plugin_info(), filterstr, "LXVST");
372 #endif
373 }
374
375 void
376 #ifdef AUDIOUNIT_SUPPORT
377 PluginSelector::au_refiller (const std::string& filterstr)
378 #else
379 PluginSelector::au_refiller (const std::string&)
380 #endif
381 {
382 #ifdef AUDIOUNIT_SUPPORT
383         refiller (manager.au_plugin_info(), filterstr, "AU");
384 #endif
385 }
386
387 PluginPtr
388 PluginSelector::load_plugin (PluginInfoPtr pi)
389 {
390         if (_session == 0) {
391                 return PluginPtr();
392         }
393
394         return pi->load (*_session);
395 }
396
397 void
398 PluginSelector::btn_add_clicked()
399 {
400         std::string name;
401         PluginInfoPtr pi;
402         TreeModel::Row newrow = *(amodel->append());
403         TreeModel::Row row;
404
405         row = *(plugin_display.get_selection()->get_selected());
406         name = row[plugin_columns.name];
407         pi = row[plugin_columns.plugin];
408
409         newrow[acols.text] = name;
410         newrow[acols.plugin] = pi;
411
412         if (!amodel->children().empty()) {
413                 set_response_sensitive (RESPONSE_APPLY, true);
414         }
415 }
416
417 void
418 PluginSelector::btn_remove_clicked()
419 {
420         TreeModel::iterator iter = added_list.get_selection()->get_selected();
421
422         amodel->erase(iter);
423         if (amodel->children().empty()) {
424                 set_response_sensitive (RESPONSE_APPLY, false);
425         }
426 }
427
428 void
429 PluginSelector::btn_update_clicked()
430 {
431         manager.refresh ();
432         refill();
433 }
434
435 void
436 PluginSelector::display_selection_changed()
437 {
438         if (plugin_display.get_selection()->count_selected_rows() != 0) {
439                 btn_add->set_sensitive (true);
440         } else {
441                 btn_add->set_sensitive (false);
442         }
443 }
444
445 void
446 PluginSelector::added_list_selection_changed()
447 {
448         if (added_list.get_selection()->count_selected_rows() != 0) {
449                 btn_remove->set_sensitive (true);
450         } else {
451                 btn_remove->set_sensitive (false);
452         }
453 }
454
455 int
456 PluginSelector::run ()
457 {
458         ResponseType r;
459         TreeModel::Children::iterator i;
460
461         bool finish = false;
462
463         while (!finish) {
464
465                 SelectedPlugins plugins;
466                 r = (ResponseType) Dialog::run ();
467
468                 switch (r) {
469                 case RESPONSE_APPLY:
470                         for (i = amodel->children().begin(); i != amodel->children().end(); ++i) {
471                                 PluginInfoPtr pp = (*i)[acols.plugin];
472                                 PluginPtr p = load_plugin (pp);
473                                 if (p) {
474                                         plugins.push_back (p);
475                                 } else {
476                                         MessageDialog msg (string_compose (_("The plugin \"%1\" could not be loaded\n\nSee the Log window for more details (maybe)"), pp->name));
477                                         msg.run ();
478                                 }
479                         }
480                         if (interested_object && !plugins.empty()) {
481                                 finish = !interested_object->use_plugins (plugins);
482                         }
483
484                         break;
485
486                 default:
487                         finish = true;
488                         break;
489                 }
490         }
491
492
493         hide();
494         amodel->clear();
495         interested_object = 0;
496
497         return (int) r;
498 }
499
500 void
501 PluginSelector::filter_button_clicked ()
502 {
503         filter_entry.set_text ("");
504 }
505
506 void
507 PluginSelector::filter_entry_changed ()
508 {
509         refill ();
510 }
511
512 void
513 PluginSelector::filter_mode_changed ()
514 {
515         std::string mode = filter_mode.get_active_text ();
516
517         if (mode == _("Favorites only") || mode == _("Hidden only")) {
518                 filter_entry.set_sensitive (false);
519         } else {
520                 filter_entry.set_sensitive (true);
521         }
522
523         refill ();
524 }
525
526 void
527 PluginSelector::on_show ()
528 {
529         ArdourDialog::on_show ();
530         filter_entry.grab_focus ();
531 }
532
533 struct PluginMenuCompareByCreator {
534     bool operator() (PluginInfoPtr a, PluginInfoPtr b) const {
535             int cmp;
536
537             cmp = strcasecmp (a->creator.c_str(), b->creator.c_str());
538
539             if (cmp < 0) {
540                     return true;
541             } else if (cmp == 0) {
542                     /* same creator ... compare names */
543                     if (strcasecmp (a->name.c_str(), b->name.c_str()) < 0) {
544                             return true;
545                     }
546             }
547             return false;
548     }
549 };
550
551 struct PluginMenuCompareByName {
552     bool operator() (PluginInfoPtr a, PluginInfoPtr b) const {
553             int cmp;
554
555             cmp = strcasecmp (a->name.c_str(), b->name.c_str());
556
557             if (cmp < 0) {
558                     return true;
559             } else if (cmp == 0) {
560                     /* same name ... compare type */
561                     if (a->type < b->type) {
562                             return true;
563                     }
564             }
565             return false;
566     }
567 };
568
569 struct PluginMenuCompareByCategory {
570     bool operator() (PluginInfoPtr a, PluginInfoPtr b) const {
571             int cmp;
572
573             cmp = strcasecmp (a->category.c_str(), b->category.c_str());
574
575             if (cmp < 0) {
576                     return true;
577             } else if (cmp == 0) {
578                     /* same category ... compare names */
579                     if (strcasecmp (a->name.c_str(), b->name.c_str()) < 0) {
580                             return true;
581                     }
582             }
583             return false;
584     }
585 };
586
587 /** @return Plugin menu. The caller should not delete it */
588 Gtk::Menu*
589 PluginSelector::plugin_menu()
590 {
591         return _plugin_menu;
592 }
593
594 void
595 PluginSelector::build_plugin_menu ()
596 {
597         PluginInfoList all_plugs;
598
599         all_plugs.insert (all_plugs.end(), manager.ladspa_plugin_info().begin(), manager.ladspa_plugin_info().end());
600 #ifdef WINDOWS_VST_SUPPORT
601         all_plugs.insert (all_plugs.end(), manager.windows_vst_plugin_info().begin(), manager.windows_vst_plugin_info().end());
602 #endif
603 #ifdef LXVST_SUPPORT
604         all_plugs.insert (all_plugs.end(), manager.lxvst_plugin_info().begin(), manager.lxvst_plugin_info().end());
605 #endif
606 #ifdef AUDIOUNIT_SUPPORT
607         all_plugs.insert (all_plugs.end(), manager.au_plugin_info().begin(), manager.au_plugin_info().end());
608 #endif
609 #ifdef LV2_SUPPORT
610         all_plugs.insert (all_plugs.end(), manager.lv2_plugin_info().begin(), manager.lv2_plugin_info().end());
611 #endif
612
613         using namespace Menu_Helpers;
614
615         delete _plugin_menu;
616
617         _plugin_menu = manage (new Menu);
618         _plugin_menu->set_name("ArdourContextMenu");
619
620         MenuList& items = _plugin_menu->items();
621         items.clear ();
622
623         Gtk::Menu* favs = create_favs_menu(all_plugs);
624         items.push_back (MenuElem (_("Favorites"), *manage (favs)));
625
626         items.push_back (MenuElem (_("Plugin Manager..."), sigc::mem_fun (*this, &PluginSelector::show_manager)));
627         items.push_back (SeparatorElem ());
628
629         Menu* by_creator = create_by_creator_menu(all_plugs);
630         items.push_back (MenuElem (_("By Creator"), *manage (by_creator)));
631
632         Menu* by_category = create_by_category_menu(all_plugs);
633         items.push_back (MenuElem (_("By Category"), *manage (by_category)));
634 }
635
636 Gtk::Menu*
637 PluginSelector::create_favs_menu (PluginInfoList& all_plugs)
638 {
639         using namespace Menu_Helpers;
640
641         Menu* favs = new Menu();
642         favs->set_name("ArdourContextMenu");
643
644         PluginMenuCompareByName cmp_by_name;
645         all_plugs.sort (cmp_by_name);
646
647         for (PluginInfoList::const_iterator i = all_plugs.begin(); i != all_plugs.end(); ++i) {
648                 if (manager.get_status (*i) == PluginManager::Favorite) {
649                         MenuElem elem ((*i)->name, (sigc::bind (sigc::mem_fun (*this, &PluginSelector::plugin_chosen_from_menu), *i)));
650                         elem.get_child()->set_use_underline (false);
651                         favs->items().push_back (elem);
652                 }
653         }
654         return favs;
655 }
656
657 Gtk::Menu*
658 PluginSelector::create_by_creator_menu (ARDOUR::PluginInfoList& all_plugs)
659 {
660         using namespace Menu_Helpers;
661
662         typedef std::map<std::string,Gtk::Menu*> SubmenuMap;
663         SubmenuMap creator_submenu_map;
664
665         Menu* by_creator = new Menu();
666         by_creator->set_name("ArdourContextMenu");
667
668         MenuList& by_creator_items = by_creator->items();
669         PluginMenuCompareByCreator cmp_by_creator;
670         all_plugs.sort (cmp_by_creator);
671
672         for (PluginInfoList::const_iterator i = all_plugs.begin(); i != all_plugs.end(); ++i) {
673
674                 if (manager.get_status (*i) == PluginManager::Hidden) continue;
675
676                 string creator = (*i)->creator;
677
678                 /* stupid LADSPA creator strings */
679                 string::size_type pos = 0;
680                 while (pos < creator.length() && (isalnum (creator[pos]) || isspace (creator[pos]))) ++pos;
681                 creator = creator.substr (0, pos);
682
683                 SubmenuMap::iterator x;
684                 Gtk::Menu* submenu;
685                 if ((x = creator_submenu_map.find (creator)) != creator_submenu_map.end()) {
686                         submenu = x->second;
687                 } else {
688                         submenu = new Gtk::Menu;
689                         by_creator_items.push_back (MenuElem (creator, *manage (submenu)));
690                         creator_submenu_map.insert (pair<std::string,Menu*> (creator, submenu));
691                         submenu->set_name("ArdourContextMenu");
692                 }
693                 MenuElem elem ((*i)->name, (sigc::bind (sigc::mem_fun (*this, &PluginSelector::plugin_chosen_from_menu), *i)));
694                 elem.get_child()->set_use_underline (false);
695                 submenu->items().push_back (elem);
696         }
697         return by_creator;
698 }
699
700 Gtk::Menu*
701 PluginSelector::create_by_category_menu (ARDOUR::PluginInfoList& all_plugs)
702 {
703         using namespace Menu_Helpers;
704
705         typedef std::map<std::string,Gtk::Menu*> SubmenuMap;
706         SubmenuMap category_submenu_map;
707
708         Menu* by_category = new Menu();
709         by_category->set_name("ArdourContextMenu");
710
711         MenuList& by_category_items = by_category->items();
712         PluginMenuCompareByCategory cmp_by_category;
713         all_plugs.sort (cmp_by_category);
714
715         for (PluginInfoList::const_iterator i = all_plugs.begin(); i != all_plugs.end(); ++i) {
716
717                 if (manager.get_status (*i) == PluginManager::Hidden) continue;
718
719                 string category = (*i)->category;
720
721                 SubmenuMap::iterator x;
722                 Gtk::Menu* submenu;
723                 if ((x = category_submenu_map.find (category)) != category_submenu_map.end()) {
724                         submenu = x->second;
725                 } else {
726                         submenu = new Gtk::Menu;
727                         by_category_items.push_back (MenuElem (category, *manage (submenu)));
728                         category_submenu_map.insert (pair<std::string,Menu*> (category, submenu));
729                         submenu->set_name("ArdourContextMenu");
730                 }
731                 MenuElem elem ((*i)->name, (sigc::bind (sigc::mem_fun (*this, &PluginSelector::plugin_chosen_from_menu), *i)));
732                 elem.get_child()->set_use_underline (false);
733                 submenu->items().push_back (elem);
734         }
735         return by_category;
736 }
737
738 void
739 PluginSelector::plugin_chosen_from_menu (const PluginInfoPtr& pi)
740 {
741         PluginPtr p = load_plugin (pi);
742
743         if (p && interested_object) {
744                 SelectedPlugins plugins;
745                 plugins.push_back (p);
746                 interested_object->use_plugins (plugins);
747         }
748
749         interested_object = 0;
750 }
751
752 void
753 PluginSelector::favorite_changed (const std::string& path)
754 {
755         PluginInfoPtr pi;
756
757         if (in_row_change) {
758                 return;
759         }
760
761         in_row_change = true;
762
763         TreeModel::iterator iter = plugin_model->get_iter (path);
764
765         if (iter) {
766
767                 bool favorite = !(*iter)[plugin_columns.favorite];
768
769                 /* change state */
770
771                 (*iter)[plugin_columns.favorite] = favorite;
772                 (*iter)[plugin_columns.hidden] = false;
773                 PluginManager::PluginStatusType status = (favorite ? PluginManager::Favorite : PluginManager::Normal);
774
775                 /* save new statuses list */
776
777                 pi = (*iter)[plugin_columns.plugin];
778
779                 manager.set_status (pi->type, pi->unique_id, status);
780
781                 manager.save_statuses ();
782
783                 build_plugin_menu ();
784         }
785         in_row_change = false;
786 }
787
788 void
789 PluginSelector::hidden_changed (const std::string& path)
790 {
791         PluginInfoPtr pi;
792
793         if (in_row_change) {
794                 return;
795         }
796
797         in_row_change = true;
798
799         TreeModel::iterator iter = plugin_model->get_iter (path);
800
801         if (iter) {
802
803                 bool hidden = !(*iter)[plugin_columns.hidden];
804
805                 /* change state */
806
807                 (*iter)[plugin_columns.favorite] = false;
808                 (*iter)[plugin_columns.hidden] = hidden;
809                 PluginManager::PluginStatusType status = (hidden ? PluginManager::Hidden : PluginManager::Normal);
810
811                 /* save new statuses list */
812
813                 pi = (*iter)[plugin_columns.plugin];
814
815                 manager.set_status (pi->type, pi->unique_id, status);
816
817                 manager.save_statuses ();
818         }
819         in_row_change = false;
820 }
821
822 void
823 PluginSelector::show_manager ()
824 {
825         show_all();
826         run ();
827 }
828
829 void
830 PluginSelector::set_interested_object (PluginInterestedObject& obj)
831 {
832         interested_object = &obj;
833 }