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