71036b72da10f9d762aaaac34fbaebc390f97b11
[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_button_press_event().connect_notify (sigc::mem_fun(*this, &PluginSelector::row_clicked));
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_clicked(GdkEventButton* event)
203 {
204         if (event->type == GDK_2BUTTON_PRESS) {
205                 btn_add_clicked();
206         }
207 }
208
209 bool
210 PluginSelector::show_this_plugin (const PluginInfoPtr& info, const std::string& filterstr)
211 {
212         std::string compstr;
213         std::string mode = filter_mode.get_active_text ();
214
215         if (mode == _("Favorites only")) {
216                 return manager.get_status (info) == PluginManager::Favorite;
217         }
218
219         if (mode == _("Hidden only")) {
220                 return manager.get_status (info) == PluginManager::Hidden;
221         }
222
223         if (!filterstr.empty()) {
224
225                 if (mode == _("Name contains")) {
226                         compstr = info->name;
227                 } else if (mode == _("Category contains")) {
228                         compstr = info->category;
229                 } else if (mode == _("Type contains")) {
230
231                         switch (info->type) {
232                         case LADSPA:
233                                 compstr = X_("LADSPA");
234                                 break;
235                         case AudioUnit:
236                                 compstr = X_("AudioUnit");
237                                 break;
238                         case LV2:
239                                 compstr = X_("LV2");
240                                 break;
241                         case VST:
242                                 compstr = X_("VST");
243                                 break;
244                         case LXVST:
245                                 compstr = X_("LXVST");
246                                 break;
247                         }
248
249                 } else if (mode == _("Author contains")) {
250                         compstr = info->creator;
251                 } else if (mode == _("Library contains")) {
252                         compstr = info->path;
253                 }
254
255                 if (compstr.empty()) {
256                         return false;
257                 }
258
259                 transform (compstr.begin(), compstr.end(), compstr.begin(), ::toupper);
260
261                 if (compstr.find (filterstr) != string::npos) {
262                         return true;
263                 } else {
264                         return false;
265                 }
266         }
267
268         return true;
269 }
270
271 void
272 PluginSelector::setup_filter_string (string& filterstr)
273 {
274         filterstr = filter_entry.get_text ();
275         transform (filterstr.begin(), filterstr.end(), filterstr.begin(), ::toupper);
276 }
277
278 void
279 PluginSelector::refill ()
280 {
281         std::string filterstr;
282
283         in_row_change = true;
284
285         plugin_model->clear ();
286
287         setup_filter_string (filterstr);
288
289         ladspa_refiller (filterstr);
290         lv2_refiller (filterstr);
291         vst_refiller (filterstr);
292         lxvst_refiller (filterstr);
293         au_refiller (filterstr);
294
295         in_row_change = false;
296 }
297
298 void
299 PluginSelector::refiller (const PluginInfoList& plugs, const::std::string& filterstr, const char* type)
300 {
301         char buf[16];
302
303         for (PluginInfoList::const_iterator i = plugs.begin(); i != plugs.end(); ++i) {
304
305                 if (show_this_plugin (*i, filterstr)) {
306
307                         TreeModel::Row newrow = *(plugin_model->append());
308                         newrow[plugin_columns.favorite] = (manager.get_status (*i) == PluginManager::Favorite);
309                         newrow[plugin_columns.hidden] = (manager.get_status (*i) == PluginManager::Hidden);
310                         newrow[plugin_columns.name] = (*i)->name;
311                         newrow[plugin_columns.type_name] = type;
312                         newrow[plugin_columns.category] = (*i)->category;
313
314                         string creator = (*i)->creator;
315                         string::size_type pos = 0;
316
317                         /* stupid LADSPA creator strings */
318
319                         while (pos < creator.length() && (isalnum (creator[pos]) || isspace (creator[pos]))) ++pos;
320                         creator = creator.substr (0, pos);
321
322                         newrow[plugin_columns.creator] = creator;
323
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                         newrow[plugin_columns.plugin] = *i;
335                 }
336         }
337 }
338
339 void
340 PluginSelector::ladspa_refiller (const std::string& filterstr)
341 {
342         refiller (manager.ladspa_plugin_info(), filterstr, "LADSPA");
343 }
344
345 void
346 PluginSelector::lv2_refiller (const std::string& filterstr)
347 {
348 #ifdef LV2_SUPPORT
349         refiller (manager.lv2_plugin_info(), filterstr, "LV2");
350 #endif
351 }
352
353 void
354 #ifdef VST_SUPPORT
355 PluginSelector::vst_refiller (const std::string& filterstr)
356 #else
357 PluginSelector::vst_refiller (const std::string&)
358 #endif
359 {
360 #ifdef VST_SUPPORT
361         refiller (manager.vst_plugin_info(), filterstr, "VST");
362 #endif
363 }
364
365 void
366 #ifdef LXVST_SUPPORT
367 PluginSelector::lxvst_refiller (const std::string& filterstr)
368 #else
369 PluginSelector::lxvst_refiller (const std::string&)
370 #endif
371 {
372 #ifdef LXVST_SUPPORT
373         refiller (manager.lxvst_plugin_info(), filterstr, "LXVST");
374 #endif
375 }
376
377 void
378 #ifdef AUDIOUNIT_SUPPORT
379 PluginSelector::au_refiller (const std::string& filterstr)
380 #else
381 PluginSelector::au_refiller (const std::string&)
382 #endif
383 {
384 #ifdef AUDIOUNIT_SUPPORT
385         refiller (manager.au_plugin_info(), filterstr, "AU");
386 #endif
387 }
388
389 PluginPtr
390 PluginSelector::load_plugin (PluginInfoPtr pi)
391 {
392         if (_session == 0) {
393                 return PluginPtr();
394         }
395
396         return pi->load (*_session);
397 }
398
399 void
400 PluginSelector::btn_add_clicked()
401 {
402         std::string name;
403         PluginInfoPtr pi;
404         TreeModel::Row newrow = *(amodel->append());
405         TreeModel::Row row;
406
407         row = *(plugin_display.get_selection()->get_selected());
408         name = row[plugin_columns.name];
409         pi = row[plugin_columns.plugin];
410
411         newrow[acols.text] = name;
412         newrow[acols.plugin] = pi;
413
414         if (!amodel->children().empty()) {
415                 set_response_sensitive (RESPONSE_APPLY, true);
416         }
417 }
418
419 void
420 PluginSelector::btn_remove_clicked()
421 {
422         TreeModel::iterator iter = added_list.get_selection()->get_selected();
423
424         amodel->erase(iter);
425         if (amodel->children().empty()) {
426                 set_response_sensitive (RESPONSE_APPLY, false);
427         }
428 }
429
430 void
431 PluginSelector::btn_update_clicked()
432 {
433         manager.refresh ();
434         refill();
435 }
436
437 void
438 PluginSelector::display_selection_changed()
439 {
440         if (plugin_display.get_selection()->count_selected_rows() != 0) {
441                 btn_add->set_sensitive (true);
442         } else {
443                 btn_add->set_sensitive (false);
444         }
445 }
446
447 void
448 PluginSelector::added_list_selection_changed()
449 {
450         if (added_list.get_selection()->count_selected_rows() != 0) {
451                 btn_remove->set_sensitive (true);
452         } else {
453                 btn_remove->set_sensitive (false);
454         }
455 }
456
457 int
458 PluginSelector::run ()
459 {
460         ResponseType r;
461         TreeModel::Children::iterator i;
462
463         bool finish = false;
464
465         while (!finish) {
466
467                 SelectedPlugins plugins;
468                 r = (ResponseType) Dialog::run ();
469
470                 switch (r) {
471                 case RESPONSE_APPLY:
472                         for (i = amodel->children().begin(); i != amodel->children().end(); ++i) {
473                                 PluginInfoPtr pp = (*i)[acols.plugin];
474                                 PluginPtr p = load_plugin (pp);
475                                 if (p) {
476                                         plugins.push_back (p);
477                                 } else {
478                                         MessageDialog msg (string_compose (_("The plugin \"%1\" could not be loaded\n\nSee the Log window for more details (maybe)"), pp->name));
479                                         msg.run ();
480                                 }
481                         }
482                         if (interested_object && !plugins.empty()) {
483                                 finish = !interested_object->use_plugins (plugins);
484                         }
485
486                         break;
487
488                 default:
489                         finish = true;
490                         break;
491                 }
492         }
493
494
495         hide();
496         amodel->clear();
497         interested_object = 0;
498
499         return (int) r;
500 }
501
502 void
503 PluginSelector::filter_button_clicked ()
504 {
505         filter_entry.set_text ("");
506 }
507
508 void
509 PluginSelector::filter_entry_changed ()
510 {
511         refill ();
512 }
513
514 void
515 PluginSelector::filter_mode_changed ()
516 {
517         std::string mode = filter_mode.get_active_text ();
518
519         if (mode == _("Favorites only") || mode == _("Hidden only")) {
520                 filter_entry.set_sensitive (false);
521         } else {
522                 filter_entry.set_sensitive (true);
523         }
524
525         refill ();
526 }
527
528 void
529 PluginSelector::on_show ()
530 {
531         ArdourDialog::on_show ();
532         filter_entry.grab_focus ();
533 }
534
535 struct PluginMenuCompareByCreator {
536     bool operator() (PluginInfoPtr a, PluginInfoPtr b) const {
537             int cmp;
538
539             cmp = strcasecmp (a->creator.c_str(), b->creator.c_str());
540
541             if (cmp < 0) {
542                     return true;
543             } else if (cmp == 0) {
544                     /* same creator ... compare names */
545                     if (strcasecmp (a->name.c_str(), b->name.c_str()) < 0) {
546                             return true;
547                     }
548             }
549             return false;
550     }
551 };
552
553 struct PluginMenuCompareByName {
554     bool operator() (PluginInfoPtr a, PluginInfoPtr b) const {
555             int cmp;
556
557             cmp = strcasecmp (a->name.c_str(), b->name.c_str());
558
559             if (cmp < 0) {
560                     return true;
561             } else if (cmp == 0) {
562                     /* same name ... compare type */
563                     if (a->type < b->type) {
564                             return true;
565                     }
566             }
567             return false;
568     }
569 };
570
571 struct PluginMenuCompareByCategory {
572     bool operator() (PluginInfoPtr a, PluginInfoPtr b) const {
573             int cmp;
574
575             cmp = strcasecmp (a->category.c_str(), b->category.c_str());
576
577             if (cmp < 0) {
578                     return true;
579             } else if (cmp == 0) {
580                     /* same category ... compare names */
581                     if (strcasecmp (a->name.c_str(), b->name.c_str()) < 0) {
582                             return true;
583                     }
584             }
585             return false;
586     }
587 };
588
589 /** @return Plugin menu. The caller should not delete it */
590 Gtk::Menu*
591 PluginSelector::plugin_menu()
592 {
593         return _plugin_menu;
594 }
595
596 void
597 PluginSelector::build_plugin_menu ()
598 {
599         PluginInfoList all_plugs;
600
601         all_plugs.insert (all_plugs.end(), manager.ladspa_plugin_info().begin(), manager.ladspa_plugin_info().end());
602 #ifdef VST_SUPPORT
603         all_plugs.insert (all_plugs.end(), manager.vst_plugin_info().begin(), manager.vst_plugin_info().end());
604 #endif
605 #ifdef LXVST_SUPPORT
606         all_plugs.insert (all_plugs.end(), manager.lxvst_plugin_info().begin(), manager.lxvst_plugin_info().end());
607 #endif
608 #ifdef AUDIOUNIT_SUPPORT
609         all_plugs.insert (all_plugs.end(), manager.au_plugin_info().begin(), manager.au_plugin_info().end());
610 #endif
611 #ifdef LV2_SUPPORT
612         all_plugs.insert (all_plugs.end(), manager.lv2_plugin_info().begin(), manager.lv2_plugin_info().end());
613 #endif
614
615         using namespace Menu_Helpers;
616
617         delete _plugin_menu;
618
619         _plugin_menu = manage (new Menu);
620         _plugin_menu->set_name("ArdourContextMenu");
621
622         MenuList& items = _plugin_menu->items();
623         items.clear ();
624
625         Gtk::Menu* favs = create_favs_menu(all_plugs);
626         items.push_back (MenuElem (_("Favorites"), *manage (favs)));
627
628         items.push_back (MenuElem (_("Plugin Manager..."), sigc::mem_fun (*this, &PluginSelector::show_manager)));
629         items.push_back (SeparatorElem ());
630
631         Menu* by_creator = create_by_creator_menu(all_plugs);
632         items.push_back (MenuElem (_("By Creator"), *manage (by_creator)));
633
634         Menu* by_category = create_by_category_menu(all_plugs);
635         items.push_back (MenuElem (_("By Category"), *manage (by_category)));
636 }
637
638 Gtk::Menu*
639 PluginSelector::create_favs_menu (PluginInfoList& all_plugs)
640 {
641         using namespace Menu_Helpers;
642
643         Menu* favs = new Menu();
644         favs->set_name("ArdourContextMenu");
645
646         PluginMenuCompareByName cmp_by_name;
647         all_plugs.sort (cmp_by_name);
648
649         for (PluginInfoList::const_iterator i = all_plugs.begin(); i != all_plugs.end(); ++i) {
650                 if (manager.get_status (*i) == PluginManager::Favorite) {
651                         favs->items().push_back (MenuElem ((*i)->name, (sigc::bind (sigc::mem_fun (*this, &PluginSelector::plugin_chosen_from_menu), *i))));
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                 submenu->items().push_back (MenuElem ((*i)->name, (sigc::bind (sigc::mem_fun (*this, &PluginSelector::plugin_chosen_from_menu), *i))));
694         }
695         return by_creator;
696 }
697
698 Gtk::Menu*
699 PluginSelector::create_by_category_menu (ARDOUR::PluginInfoList& all_plugs)
700 {
701         using namespace Menu_Helpers;
702
703         typedef std::map<std::string,Gtk::Menu*> SubmenuMap;
704         SubmenuMap category_submenu_map;
705
706         Menu* by_category = new Menu();
707         by_category->set_name("ArdourContextMenu");
708
709         MenuList& by_category_items = by_category->items();
710         PluginMenuCompareByCategory cmp_by_category;
711         all_plugs.sort (cmp_by_category);
712
713         for (PluginInfoList::const_iterator i = all_plugs.begin(); i != all_plugs.end(); ++i) {
714
715                 if (manager.get_status (*i) == PluginManager::Hidden) continue;
716
717                 string category = (*i)->category;
718
719                 SubmenuMap::iterator x;
720                 Gtk::Menu* submenu;
721                 if ((x = category_submenu_map.find (category)) != category_submenu_map.end()) {
722                         submenu = x->second;
723                 } else {
724                         submenu = new Gtk::Menu;
725                         by_category_items.push_back (MenuElem (category, *manage (submenu)));
726                         category_submenu_map.insert (pair<std::string,Menu*> (category, submenu));
727                         submenu->set_name("ArdourContextMenu");
728                 }
729                 submenu->items().push_back (MenuElem ((*i)->name, (sigc::bind (sigc::mem_fun (*this, &PluginSelector::plugin_chosen_from_menu), *i))));
730         }
731         return by_category;
732 }
733
734 void
735 PluginSelector::plugin_chosen_from_menu (const PluginInfoPtr& pi)
736 {
737         PluginPtr p = load_plugin (pi);
738
739         if (p && interested_object) {
740                 SelectedPlugins plugins;
741                 plugins.push_back (p);
742                 interested_object->use_plugins (plugins);
743         }
744
745         interested_object = 0;
746 }
747
748 void
749 PluginSelector::favorite_changed (const std::string& path)
750 {
751         PluginInfoPtr pi;
752
753         if (in_row_change) {
754                 return;
755         }
756
757         in_row_change = true;
758
759         TreeModel::iterator iter = plugin_model->get_iter (path);
760
761         if (iter) {
762
763                 bool favorite = !(*iter)[plugin_columns.favorite];
764
765                 /* change state */
766
767                 (*iter)[plugin_columns.favorite] = favorite;
768                 (*iter)[plugin_columns.hidden] = false;
769                 PluginManager::PluginStatusType status = (favorite ? PluginManager::Favorite : PluginManager::Normal);
770
771                 /* save new statuses list */
772
773                 pi = (*iter)[plugin_columns.plugin];
774
775                 manager.set_status (pi->type, pi->unique_id, status);
776
777                 manager.save_statuses ();
778
779                 build_plugin_menu ();
780         }
781         in_row_change = false;
782 }
783
784 void
785 PluginSelector::hidden_changed (const std::string& path)
786 {
787         PluginInfoPtr pi;
788
789         if (in_row_change) {
790                 return;
791         }
792
793         in_row_change = true;
794
795         TreeModel::iterator iter = plugin_model->get_iter (path);
796
797         if (iter) {
798
799                 bool hidden = !(*iter)[plugin_columns.hidden];
800
801                 /* change state */
802
803                 (*iter)[plugin_columns.favorite] = false;
804                 (*iter)[plugin_columns.hidden] = hidden;
805                 PluginManager::PluginStatusType status = (hidden ? PluginManager::Hidden : PluginManager::Normal);
806
807                 /* save new statuses list */
808
809                 pi = (*iter)[plugin_columns.plugin];
810
811                 manager.set_status (pi->type, pi->unique_id, status);
812
813                 manager.save_statuses ();
814         }
815         in_row_change = false;
816 }
817
818 void
819 PluginSelector::show_manager ()
820 {
821         show_all();
822         run ();
823 }
824
825 void
826 PluginSelector::set_interested_object (PluginInterestedObject& obj)
827 {
828         interested_object = &obj;
829 }