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