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