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