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