Strip trailing whitespace and fix other whitespace errors (e.g. space/tab mixing...
[ardour.git] / gtk2_ardour / plugin_selector.cc
1 /*
2     Copyright (C) 2000-2006 Paul Davis
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19 #ifdef WAF_BUILD
20 #include "gtk2ardour-config.h"
21 #endif
22
23 #include <cstdio>
24 #include <lrdf.h>
25 #include <map>
26
27 #include <algorithm>
28
29 #include <gtkmm/table.h>
30 #include <gtkmm/stock.h>
31 #include <gtkmm/button.h>
32 #include <gtkmm/notebook.h>
33
34 #include <gtkmm2ext/utils.h>
35
36 #include "pbd/convert.h"
37
38 #include "ardour/plugin_manager.h"
39 #include "ardour/plugin.h"
40 #include "ardour/configuration.h"
41
42 #include "ardour_ui.h"
43 #include "plugin_selector.h"
44 #include "gui_thread.h"
45
46 #include "i18n.h"
47
48 using namespace ARDOUR;
49 using namespace PBD;
50 using namespace Gtk;
51 using namespace std;
52
53 static const char* _filter_mode_strings[] = {
54         N_("Name contains"),
55         N_("Type contains"),
56         N_("Category contains"),
57         N_("Author contains"),
58         N_("Library contains"),
59         N_("Favorites only"),
60         0
61 };
62
63 PluginSelector::PluginSelector (PluginManager *mgr)
64         : ArdourDialog (_("ardour: plugins"), true, false),
65           filter_button (Stock::CLEAR)
66 {
67         set_position (Gtk::WIN_POS_MOUSE);
68         set_name ("PluginSelectorWindow");
69         add_events (Gdk::KEY_PRESS_MASK|Gdk::KEY_RELEASE_MASK);
70
71         manager = mgr;
72         session = 0;
73         _menu = 0;
74         in_row_change = false;
75
76         plugin_model = Gtk::ListStore::create (plugin_columns);
77         plugin_display.set_model (plugin_model);
78         /* XXX translators: try to convert "Fav" into a short term
79            related to "favorite"
80         */
81         plugin_display.append_column (_("Fav"), plugin_columns.favorite);
82         plugin_display.append_column (_("Available Plugins"), plugin_columns.name);
83         plugin_display.append_column (_("Type"), plugin_columns.type_name);
84         plugin_display.append_column (_("Category"), plugin_columns.category);
85         plugin_display.append_column (_("Creator"), plugin_columns.creator);
86         plugin_display.append_column (_("# Audio In"),plugin_columns.audio_ins);
87         plugin_display.append_column (_("# Audio Out"), plugin_columns.audio_outs);
88         plugin_display.append_column (_("# MIDI In"),plugin_columns.midi_ins);
89         plugin_display.append_column (_("# MIDI Out"), plugin_columns.midi_outs);
90         plugin_display.set_headers_visible (true);
91         plugin_display.set_headers_clickable (true);
92         plugin_display.set_reorderable (false);
93         plugin_display.set_rules_hint (true);
94
95         CellRendererToggle* fav_cell = dynamic_cast<CellRendererToggle*>(plugin_display.get_column_cell_renderer (0));
96         fav_cell->property_activatable() = true;
97         fav_cell->property_radio() = false;
98         fav_cell->signal_toggled().connect (mem_fun (*this, &PluginSelector::favorite_changed));
99
100         scroller.set_border_width(10);
101         scroller.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
102         scroller.add(plugin_display);
103
104         amodel = Gtk::ListStore::create(acols);
105         added_list.set_model (amodel);
106         added_list.append_column (_("Plugins to be connected"), acols.text);
107         added_list.set_headers_visible (true);
108         added_list.set_reorderable (false);
109
110         for (int i = 0; i <=7; i++) {
111                 Gtk::TreeView::Column* column = plugin_display.get_column(i);
112                 column->set_sort_column(i);
113         }
114
115         ascroller.set_border_width(10);
116         ascroller.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
117         ascroller.add(added_list);
118         btn_add = manage(new Gtk::Button(Stock::ADD));
119         ARDOUR_UI::instance()->tooltips().set_tip(*btn_add, _("Add a plugin to the effect list"));
120         btn_add->set_sensitive (false);
121         btn_remove = manage(new Gtk::Button(Stock::REMOVE));
122         btn_remove->set_sensitive (false);
123         ARDOUR_UI::instance()->tooltips().set_tip(*btn_remove, _("Remove a plugin from the effect list"));
124         Gtk::Button *btn_update = manage(new Gtk::Button(Stock::REFRESH));
125         ARDOUR_UI::instance()->tooltips().set_tip(*btn_update, _("Update available plugins"));
126
127         btn_add->set_name("PluginSelectorButton");
128         btn_remove->set_name("PluginSelectorButton");
129
130         Gtk::Table* table = manage(new Gtk::Table(7, 11));
131         table->set_size_request(750, 500);
132         table->attach(scroller, 0, 7, 0, 5);
133
134         HBox* filter_box = manage (new HBox);
135
136         vector<string> filter_strings = I18N (_filter_mode_strings);
137         Gtkmm2ext::set_popdown_strings (filter_mode, filter_strings);
138         filter_mode.set_active_text (filter_strings.front());
139
140         filter_box->pack_start (filter_mode, false, false);
141         filter_box->pack_start (filter_entry, true, true);
142         filter_box->pack_start (filter_button, false, false);
143
144         filter_entry.signal_changed().connect (mem_fun (*this, &PluginSelector::filter_entry_changed));
145         filter_button.signal_clicked().connect (mem_fun (*this, &PluginSelector::filter_button_clicked));
146         filter_mode.signal_changed().connect (mem_fun (*this, &PluginSelector::filter_mode_changed));
147
148         filter_box->show ();
149         filter_mode.show ();
150         filter_entry.show ();
151         filter_button.show ();
152
153         table->attach (*filter_box, 0, 7, 5, 6, FILL|EXPAND, FILL, 5, 5);
154
155         table->attach(*btn_add, 1, 2, 6, 7, FILL, FILL, 5, 5);
156         table->attach(*btn_remove, 3, 4, 6, 7, FILL, FILL, 5, 5);
157         table->attach(*btn_update, 5, 6, 6, 7, FILL, FILL, 5, 5);
158
159         table->attach(ascroller, 0, 7, 8, 10);
160
161         add_button (Stock::CANCEL, RESPONSE_CANCEL);
162         add_button (_("Insert Plugin(s)"), RESPONSE_APPLY);
163         set_default_response (RESPONSE_APPLY);
164         set_response_sensitive (RESPONSE_APPLY, false);
165         get_vbox()->pack_start (*table);
166
167         table->set_name("PluginSelectorTable");
168         plugin_display.set_name("PluginSelectorDisplay");
169         //plugin_display.set_name("PluginSelectorList");
170         added_list.set_name("PluginSelectorList");
171
172         plugin_display.signal_button_press_event().connect_notify (mem_fun(*this, &PluginSelector::row_clicked));
173         plugin_display.get_selection()->signal_changed().connect (mem_fun(*this, &PluginSelector::display_selection_changed));
174         plugin_display.grab_focus();
175
176         btn_update->signal_clicked().connect (mem_fun(*this, &PluginSelector::btn_update_clicked));
177         btn_add->signal_clicked().connect(mem_fun(*this, &PluginSelector::btn_add_clicked));
178         btn_remove->signal_clicked().connect(mem_fun(*this, &PluginSelector::btn_remove_clicked));
179         added_list.get_selection()->signal_changed().connect (mem_fun(*this, &PluginSelector::added_list_selection_changed));
180
181         refill ();
182 }
183
184 void
185 PluginSelector::row_clicked(GdkEventButton* event)
186 {
187         if (event->type == GDK_2BUTTON_PRESS)
188                 btn_add_clicked();
189 }
190
191 void
192 PluginSelector::set_session (Session* s)
193 {
194         ENSURE_GUI_THREAD(bind (mem_fun(*this, &PluginSelector::set_session), s));
195
196         session = s;
197
198         if (session) {
199                 session->GoingAway.connect (bind (mem_fun(*this, &PluginSelector::set_session), static_cast<Session*> (0)));
200         }
201 }
202
203 bool
204 PluginSelector::show_this_plugin (const PluginInfoPtr& info, const std::string& filterstr)
205 {
206         std::string compstr;
207         std::string mode = filter_mode.get_active_text ();
208
209         if (mode == _("Favorites only")) {
210                 return manager->is_a_favorite_plugin (info);
211         }
212
213         if (!filterstr.empty()) {
214
215                 if (mode == _("Name contains")) {
216                         compstr = info->name;
217                 } else if (mode == _("Category contains")) {
218                         compstr = info->category;
219                 } else if (mode == _("Type contains")) {
220
221                         switch (info->type) {
222                         case LADSPA:
223                                 compstr = X_("LADSPA");
224                                 break;
225                         case AudioUnit:
226                                 compstr = X_("AudioUnit");
227                                 break;
228                         case LV2:
229                                 compstr = X_("LV2");
230                                 break;
231                         case VST:
232                                 compstr = X_("VST");
233                                 break;
234                         }
235
236                 } else if (mode == _("Author contains")) {
237                         compstr = info->creator;
238                 } else if (mode == _("Library contains")) {
239                         compstr = info->path;
240                 }
241
242                 if (compstr.empty()) {
243                         return false;
244                 }
245
246                 transform (compstr.begin(), compstr.end(), compstr.begin(), ::toupper);
247
248                 if (compstr.find (filterstr) != string::npos) {
249                         return true;
250                 } else {
251                         return false;
252                 }
253         }
254
255         return true;
256 }
257
258 void
259 PluginSelector::setup_filter_string (string& filterstr)
260 {
261         filterstr = filter_entry.get_text ();
262         transform (filterstr.begin(), filterstr.end(), filterstr.begin(), ::toupper);
263 }
264
265 void
266 PluginSelector::refill ()
267 {
268         std::string filterstr;
269
270         in_row_change = true;
271
272         plugin_model->clear ();
273
274         setup_filter_string (filterstr);
275
276         ladspa_refiller (filterstr);
277         lv2_refiller (filterstr);
278         vst_refiller (filterstr);
279         au_refiller (filterstr);
280
281         in_row_change = false;
282 }
283
284 void
285 PluginSelector::refiller (const PluginInfoList& plugs, const::std::string& filterstr, const char* type)
286 {
287         char buf[16];
288
289         for (PluginInfoList::const_iterator i = plugs.begin(); i != plugs.end(); ++i) {
290
291                 if (show_this_plugin (*i, filterstr)) {
292
293                         TreeModel::Row newrow = *(plugin_model->append());
294                         newrow[plugin_columns.favorite] = manager->is_a_favorite_plugin (*i);
295                         newrow[plugin_columns.name] = (*i)->name;
296                         newrow[plugin_columns.type_name] = type;
297                         newrow[plugin_columns.category] = (*i)->category;
298
299                         string creator = (*i)->creator;
300                         string::size_type pos = 0;
301
302                         /* stupid LADSPA creator strings */
303
304                         while (pos < creator.length() && (isalnum (creator[pos]) || isspace (creator[pos]))) ++pos;
305                         creator = creator.substr (0, pos);
306
307                         newrow[plugin_columns.creator] = creator;
308
309                         snprintf (buf, sizeof(buf), "%d", (*i)->n_inputs.n_audio());
310                         newrow[plugin_columns.audio_ins] = buf;
311                         snprintf (buf, sizeof(buf), "%d", (*i)->n_inputs.n_midi());
312                         newrow[plugin_columns.midi_ins] = buf;
313
314                         snprintf (buf, sizeof(buf), "%d", (*i)->n_outputs.n_audio());
315                         newrow[plugin_columns.audio_outs] = buf;
316                         snprintf (buf, sizeof(buf), "%d", (*i)->n_outputs.n_midi());
317                         newrow[plugin_columns.midi_outs] = buf;
318
319                         newrow[plugin_columns.plugin] = *i;
320                 }
321         }
322 }
323
324 void
325 PluginSelector::ladspa_refiller (const std::string& filterstr)
326 {
327         refiller (manager->ladspa_plugin_info(), filterstr, "LADSPA");
328 }
329
330 void
331 PluginSelector::lv2_refiller (const std::string& filterstr)
332 {
333 #ifdef HAVE_SLV2
334         refiller (manager->lv2_plugin_info(), filterstr, "LV2");
335 #endif
336 }
337
338 void
339 #ifdef VST_SUPPORT
340 PluginSelector::vst_refiller (const std::string& filterstr)
341 #else
342 PluginSelector::vst_refiller (const std::string&)
343 #endif
344 {
345 #ifdef VST_SUPPORT
346         refiller (manager->vst_plugin_info(), filterstr, "VST");
347 #endif
348 }
349
350 void
351 #ifdef HAVE_AUDIOUNITS
352 PluginSelector::au_refiller (const std::string& filterstr)
353 #else
354 PluginSelector::au_refiller (const std::string&)
355 #endif
356 {
357 #ifdef HAVE_AUDIOUNITS
358         refiller (manager->au_plugin_info(), filterstr, "AU");
359 #endif
360 }
361
362 PluginPtr
363 PluginSelector::load_plugin (PluginInfoPtr pi)
364 {
365         if (session == 0) {
366                 return PluginPtr();
367         }
368
369         return pi->load (*session);
370 }
371
372 void
373 PluginSelector::btn_add_clicked()
374 {
375         std::string name;
376         PluginInfoPtr pi;
377         TreeModel::Row newrow = *(amodel->append());
378         TreeModel::Row row;
379
380         row = *(plugin_display.get_selection()->get_selected());
381         name = row[plugin_columns.name];
382         pi = row[plugin_columns.plugin];
383
384         newrow[acols.text] = name;
385         newrow[acols.plugin] = pi;
386
387         if (!amodel->children().empty()) {
388                 set_response_sensitive (RESPONSE_APPLY, true);
389         }
390 }
391
392 void
393 PluginSelector::btn_remove_clicked()
394 {
395         TreeModel::iterator iter = added_list.get_selection()->get_selected();
396
397         amodel->erase(iter);
398         if (amodel->children().empty()) {
399                 set_response_sensitive (RESPONSE_APPLY, false);
400         }
401 }
402
403 void
404 PluginSelector::btn_update_clicked()
405 {
406         manager->refresh ();
407         refill();
408 }
409
410 void
411 PluginSelector::display_selection_changed()
412 {
413         if (plugin_display.get_selection()->count_selected_rows() != 0) {
414                 btn_add->set_sensitive (true);
415         } else {
416                 btn_add->set_sensitive (false);
417         }
418 }
419
420 void
421 PluginSelector::added_list_selection_changed()
422 {
423         if (added_list.get_selection()->count_selected_rows() != 0) {
424                 btn_remove->set_sensitive (true);
425         } else {
426                 btn_remove->set_sensitive (false);
427         }
428 }
429
430 int
431 PluginSelector::run ()
432 {
433         ResponseType r;
434         TreeModel::Children::iterator i;
435         SelectedPlugins plugins;
436
437         r = (ResponseType) Dialog::run ();
438
439         switch (r) {
440         case RESPONSE_APPLY:
441                 for (i = amodel->children().begin(); i != amodel->children().end(); ++i) {
442                         PluginInfoPtr pp = (*i)[acols.plugin];
443                         PluginPtr p = load_plugin (pp);
444                         if (p) {
445                                 plugins.push_back (p);
446                         }
447                 }
448                 if (interested_object && !plugins.empty()) {
449                         interested_object->use_plugins (plugins);
450                 }
451
452                 break;
453
454         default:
455                 break;
456         }
457
458         hide();
459         amodel->clear();
460         interested_object = 0;
461
462         return (int) r;
463 }
464
465 void
466 PluginSelector::filter_button_clicked ()
467 {
468         filter_entry.set_text ("");
469 }
470
471 void
472 PluginSelector::filter_entry_changed ()
473 {
474         refill ();
475 }
476
477 void
478 PluginSelector::filter_mode_changed ()
479 {
480         std::string mode = filter_mode.get_active_text ();
481
482         if (mode == _("Favorites only")) {
483                 filter_entry.set_sensitive (false);
484         } else {
485                 filter_entry.set_sensitive (true);
486         }
487
488         refill ();
489 }
490
491 void
492 PluginSelector::on_show ()
493 {
494         ArdourDialog::on_show ();
495         filter_entry.grab_focus ();
496 }
497
498 struct PluginMenuCompare {
499     bool operator() (PluginInfoPtr a, PluginInfoPtr b) const {
500             int cmp;
501
502             cmp = strcasecmp (a->creator.c_str(), b->creator.c_str());
503
504             if (cmp < 0) {
505                     return true;
506             } else if (cmp == 0) {
507                     /* same creator ... compare names */
508                     if (strcasecmp (a->name.c_str(), b->name.c_str()) < 0) {
509                             return true;
510                     }
511             }
512             return false;
513     }
514 };
515
516 Gtk::Menu&
517 PluginSelector::plugin_menu()
518 {
519         using namespace Menu_Helpers;
520
521         typedef std::map<Glib::ustring,Gtk::Menu*> SubmenuMap;
522         SubmenuMap submenu_map;
523
524         if (!_menu) {
525                 _menu = new Menu();
526                 _menu->set_name("ArdourContextMenu");
527         }
528
529         MenuList& items = _menu->items();
530         Menu* favs = new Menu();
531         favs->set_name("ArdourContextMenu");
532
533         items.clear ();
534         items.push_back (MenuElem (_("Favorites"), *favs));
535         items.push_back (MenuElem (_("Plugin Manager"), mem_fun (*this, &PluginSelector::show_manager)));
536         items.push_back (SeparatorElem ());
537
538         PluginInfoList all_plugs;
539
540         all_plugs.insert (all_plugs.end(), manager->ladspa_plugin_info().begin(), manager->ladspa_plugin_info().end());
541 #ifdef VST_SUPPORT
542         all_plugs.insert (all_plugs.end(), manager->vst_plugin_info().begin(), manager->vst_plugin_info().end());
543 #endif
544 #ifdef HAVE_AUDIOUNITS
545         all_plugs.insert (all_plugs.end(), manager->au_plugin_info().begin(), manager->au_plugin_info().end());
546 #endif
547 #ifdef HAVE_SLV2
548         all_plugs.insert (all_plugs.end(), manager->lv2_plugin_info().begin(), manager->lv2_plugin_info().end());
549 #endif
550
551         PluginMenuCompare cmp;
552         all_plugs.sort (cmp);
553
554         for (PluginInfoList::const_iterator i = all_plugs.begin(); i != all_plugs.end(); ++i) {
555                 SubmenuMap::iterator x;
556                 Gtk::Menu* submenu;
557
558                 string creator = (*i)->creator;
559                 string::size_type pos = 0;
560
561                 if (manager->is_a_favorite_plugin (*i)) {
562                         favs->items().push_back (MenuElem ((*i)->name, (bind (mem_fun (*this, &PluginSelector::plugin_chosen_from_menu), *i))));
563                 }
564
565                 /* stupid LADSPA creator strings */
566
567                 while (pos < creator.length() && (isalnum (creator[pos]) || isspace (creator[pos]))) ++pos;
568                 creator = creator.substr (0, pos);
569
570                 if ((x = submenu_map.find (creator)) != submenu_map.end()) {
571                         submenu = x->second;
572                 } else {
573                         submenu = new Gtk::Menu;
574                         items.push_back (MenuElem (creator, *submenu));
575                         submenu_map.insert (pair<Glib::ustring,Menu*> (creator, submenu));
576                         submenu->set_name("ArdourContextMenu");
577                 }
578
579                 submenu->items().push_back (MenuElem ((*i)->name, (bind (mem_fun (*this, &PluginSelector::plugin_chosen_from_menu), *i))));
580         }
581
582         return *_menu;
583 }
584
585 void
586 PluginSelector::plugin_chosen_from_menu (const PluginInfoPtr& pi)
587 {
588         PluginPtr p = load_plugin (pi);
589
590         if (p && interested_object) {
591                 SelectedPlugins plugins;
592                 plugins.push_back (p);
593                 interested_object->use_plugins (plugins);
594         }
595
596         interested_object = 0;
597 }
598
599 void
600 PluginSelector::favorite_changed (const Glib::ustring& path)
601 {
602         PluginInfoPtr pi;
603
604         if (in_row_change) {
605                 return;
606         }
607
608         in_row_change = true;
609
610         TreeModel::iterator iter = plugin_model->get_iter (path);
611
612         if (iter) {
613
614                 bool favorite = !(*iter)[plugin_columns.favorite];
615
616                 /* change state */
617
618                 (*iter)[plugin_columns.favorite] = favorite;
619
620                 /* save new favorites list */
621
622                 pi = (*iter)[plugin_columns.plugin];
623
624                 if (favorite) {
625                         manager->add_favorite (pi->type, pi->unique_id);
626                 } else {
627                         manager->remove_favorite (pi->type, pi->unique_id);
628                 }
629
630                 manager->save_favorites ();
631         }
632         in_row_change = false;
633 }
634
635 void
636 PluginSelector::show_manager ()
637 {
638         show_all();
639         run ();
640 }
641
642 void
643 PluginSelector::set_interested_object (PluginInterestedObject& obj)
644 {
645         interested_object = &obj;
646 }