extend filtering to VST + AU, tweak details, add filter-by-creator and by library
[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
20 #include <cstdio>
21 #include <lrdf.h>
22
23 #include <algorithm>
24
25 #include <gtkmm/table.h>
26 #include <gtkmm/stock.h>
27 #include <gtkmm/button.h>
28 #include <gtkmm/notebook.h>
29
30 #include <gtkmm2ext/utils.h>
31
32 #include <pbd/convert.h>
33
34 #include <ardour/plugin_manager.h>
35 #include <ardour/plugin.h>
36 #include <ardour/configuration.h>
37
38 #include "ardour_ui.h"
39 #include "plugin_selector.h"
40 #include "gui_thread.h"
41
42 #include "i18n.h"
43
44 using namespace ARDOUR;
45 using namespace PBD;
46 using namespace Gtk;
47 using namespace std;
48
49 static const char* _filter_mode_strings[] = {
50         N_("Name contains"),
51         N_("Type contains"),
52         N_("Author contains"),
53         N_("Library contains"),
54         0
55 };
56
57 PluginSelector::PluginSelector (PluginManager *mgr)
58         : ArdourDialog (_("ardour: plugins"), true, false),
59           filter_button (Stock::CLEAR)
60 {
61         set_position (Gtk::WIN_POS_MOUSE);
62         set_name ("PluginSelectorWindow");
63         add_events (Gdk::KEY_PRESS_MASK|Gdk::KEY_RELEASE_MASK);
64
65         manager = mgr;
66         session = 0;
67         
68         current_selection = ARDOUR::LADSPA;
69
70         lmodel = Gtk::ListStore::create(lcols);
71         ladspa_display.set_model (lmodel);
72         ladspa_display.append_column (_("Available LADSPA Plugins"), lcols.name);
73         ladspa_display.append_column (_("Type"), lcols.type);
74         ladspa_display.append_column (_("# Inputs"),lcols.ins);
75         ladspa_display.append_column (_("# Outputs"), lcols.outs);
76         ladspa_display.set_headers_visible (true);
77         ladspa_display.set_headers_clickable (true);
78         ladspa_display.set_reorderable (false);
79         lscroller.set_border_width(10);
80         lscroller.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
81         lscroller.add(ladspa_display);
82
83         amodel = Gtk::ListStore::create(acols);
84         added_list.set_model (amodel);
85         added_list.append_column (_("Plugins to be connected"), acols.text);
86         added_list.set_headers_visible (true);
87         added_list.set_reorderable (false);
88
89         for (int i = 0; i <=3; i++) {
90                 Gtk::TreeView::Column* column = ladspa_display.get_column(i);
91                 column->set_sort_column(i);
92         }
93
94 #ifdef VST_SUPPORT
95         vmodel = ListStore::create(vcols);
96         vst_display.set_model (vmodel);
97         vst_display.append_column (_("Available plugins"), vcols.name);
98         vst_display.append_column (_("# Inputs"), vcols.ins);
99         vst_display.append_column (_("# Outputs"), vcols.outs);
100         vst_display.set_headers_visible (true);
101         vst_display.set_headers_clickable (true);
102         vst_display.set_reorderable (false);
103         vscroller.set_border_width(10);
104         vscroller.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
105         vscroller.add(vst_display);
106
107         for (int i = 0; i <=2; i++) {
108                 Gtk::TreeView::Column* column = vst_display.get_column(i);
109                 column->set_sort_column(i);
110         }
111 #endif
112
113 #ifdef HAVE_AUDIOUNIT
114         aumodel = ListStore::create(aucols);
115         au_display.set_model (aumodel);
116         au_display.append_column (_("Available plugins"), aucols.name);
117         au_display.append_column (_("# Inputs"), aucols.ins);
118         au_display.append_column (_("# Outputs"), aucols.outs);
119         au_display.set_headers_visible (true);
120         au_display.set_headers_clickable (true);
121         au_display.set_reorderable (false);
122         auscroller.set_border_width(10);
123         auscroller.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
124         auscroller.add(au_display);
125
126         for (int i = 0; i <=2; i++) {
127                 Gtk::TreeView::Column* column = au_display.get_column(i);
128                 column->set_sort_column(i);
129         }
130 #endif
131
132         ascroller.set_border_width(10);
133         ascroller.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
134         ascroller.add(added_list);
135         btn_add = manage(new Gtk::Button(Stock::ADD));
136         ARDOUR_UI::instance()->tooltips().set_tip(*btn_add, _("Add a plugin to the effect list"));
137         btn_add->set_sensitive (false);
138         btn_remove = manage(new Gtk::Button(Stock::REMOVE));
139         btn_remove->set_sensitive (false);
140         ARDOUR_UI::instance()->tooltips().set_tip(*btn_remove, _("Remove a plugin from the effect list"));
141         Gtk::Button *btn_update = manage(new Gtk::Button(Stock::REFRESH));
142         ARDOUR_UI::instance()->tooltips().set_tip(*btn_update, _("Update available plugins"));
143
144         btn_add->set_name("PluginSelectorButton");
145         btn_remove->set_name("PluginSelectorButton");
146
147         Gtk::Table* table = manage(new Gtk::Table(7, 11));
148         table->set_size_request(750, 500);
149         table->attach(notebook, 0, 7, 0, 5);
150
151         HBox* filter_box = manage (new HBox);
152
153         vector<string> filter_strings = I18N (_filter_mode_strings);
154         Gtkmm2ext::set_popdown_strings (filter_mode, filter_strings);
155         filter_mode.set_active_text (filter_strings.front());
156
157         filter_box->pack_start (filter_mode, false, false);
158         filter_box->pack_start (filter_entry, true, true);
159         filter_box->pack_start (filter_button, false, false);
160
161         filter_entry.signal_changed().connect (mem_fun (*this, &PluginSelector::filter_entry_changed));
162         filter_button.signal_clicked().connect (mem_fun (*this, &PluginSelector::filter_button_clicked));
163         filter_mode.signal_changed().connect (mem_fun (*this, &PluginSelector::filter_mode_changed));
164
165         filter_box->show ();
166         filter_mode.show ();
167         filter_entry.show ();
168         filter_button.show ();
169
170         table->attach (*filter_box, 0, 7, 5, 6, FILL|EXPAND, FILL, 5, 5);
171
172         table->attach(*btn_add, 1, 2, 6, 7, FILL, FILL, 5, 5); 
173         table->attach(*btn_remove, 3, 4, 6, 7, FILL, FILL, 5, 5);
174         table->attach(*btn_update, 5, 6, 6, 7, FILL, FILL, 5, 5);
175
176         table->attach(ascroller, 0, 7, 8, 10);
177
178         add_button (Stock::CANCEL, RESPONSE_CANCEL);
179         add_button (Stock::CONNECT, RESPONSE_APPLY);
180         set_default_response (RESPONSE_APPLY);
181         set_response_sensitive (RESPONSE_APPLY, false);
182         get_vbox()->pack_start (*table);
183
184
185         // Notebook tab order must be the same in here as in set_correct_focus()
186         using namespace Notebook_Helpers;
187         notebook.pages().push_back (TabElem (lscroller, _("LADSPA")));
188
189 #ifdef VST_SUPPORT
190         if (Config->get_use_vst()) {
191                 notebook.pages().push_back (TabElem (vscroller, _("VST")));
192         }
193 #endif
194
195 #ifdef HAVE_AUDIOUNIT
196         notebook.pages().push_back (TabElem (auscroller, _("AudioUnit")));
197 #endif
198
199         table->set_name("PluginSelectorTable");
200         ladspa_display.set_name("PluginSelectorDisplay");
201         //ladspa_display.set_name("PluginSelectorList");
202         added_list.set_name("PluginSelectorList");
203
204         ladspa_display.signal_button_press_event().connect_notify (mem_fun(*this, &PluginSelector::row_clicked));
205         ladspa_display.get_selection()->signal_changed().connect (mem_fun(*this, &PluginSelector::ladspa_display_selection_changed));
206         ladspa_display.grab_focus();
207         
208 #ifdef VST_SUPPORT
209         if (Config->get_use_vst()) {
210                 vst_display.signal_button_press_event().connect_notify (mem_fun(*this, &PluginSelector::row_clicked));
211                 vst_display.get_selection()->signal_changed().connect (mem_fun(*this, &PluginSelector::vst_display_selection_changed));
212         }
213 #endif
214
215 #ifdef HAVE_AUDIOUNIT
216         au_display.signal_button_press_event().connect_notify (mem_fun(*this, &PluginSelector::row_clicked));
217         au_display.get_selection()->signal_changed().connect (mem_fun(*this, &PluginSelector::au_display_selection_changed));
218 #endif
219
220         btn_update->signal_clicked().connect (mem_fun(*this, &PluginSelector::btn_update_clicked));
221         btn_add->signal_clicked().connect(mem_fun(*this, &PluginSelector::btn_add_clicked));
222         btn_remove->signal_clicked().connect(mem_fun(*this, &PluginSelector::btn_remove_clicked));
223         added_list.get_selection()->signal_changed().connect (mem_fun(*this, &PluginSelector::added_list_selection_changed));
224
225         ladspa_refiller ();
226         
227 #ifdef VST_SUPPORT
228         vst_refiller ();
229 #endif
230
231 #ifdef HAVE_AUDIOUNIT
232         au_refiller ();
233 #endif
234
235         signal_show().connect (mem_fun (*this, &PluginSelector::set_correct_focus));
236 }
237
238 /**
239  * Makes sure keyboard focus is always in the plugin list
240  * of the selected notebook tab.
241  **/
242 void
243 PluginSelector::set_correct_focus()
244 {
245         int cp = notebook.get_current_page();
246
247         if (cp == 0) {
248                 ladspa_display.grab_focus();
249                 return;
250         }
251
252 #ifdef VST_SUPPORT
253         if (Config->get_use_vst()) {
254                 cp--;
255         
256                 if (cp == 0) {
257                         vst_display.grab_focus();
258                         return;
259                 }
260         }
261 #endif
262
263 #ifdef HAVE_AUDIOUNIT
264         cp--;
265
266         if (cp == 0) {
267                 au_display.grab_focus();
268                 return;
269         }
270 #endif
271 }
272
273 void
274 PluginSelector::row_clicked(GdkEventButton* event)
275 {
276         if (event->type == GDK_2BUTTON_PRESS)
277                 btn_add_clicked();
278 }
279
280 void
281 PluginSelector::set_session (Session* s)
282 {
283         ENSURE_GUI_THREAD(bind (mem_fun(*this, &PluginSelector::set_session), s));
284         
285         session = s;
286
287         if (session) {
288                 session->GoingAway.connect (bind (mem_fun(*this, &PluginSelector::set_session), static_cast<Session*> (0)));
289         }
290 }
291
292 bool
293 PluginSelector::show_this_plugin (PluginInfoPtr& info, const std::string& filterstr)
294 {
295         std::string compstr;
296         std::string mode = filter_mode.get_active_text ();
297
298         if (!filterstr.empty()) {
299                 
300                 if (mode == _("Name contains")) {
301                         compstr = info->name;
302                 } else if (mode == _("Type contains")) {
303                         compstr = info->category;
304                 } else if (mode == _("Author contains")) {
305                         compstr = info->creator;
306                 } else if (mode == _("Library contains")) {
307                         compstr = info->path;
308                 }
309                 
310                 transform (compstr.begin(), compstr.end(), compstr.begin(), ::toupper);
311
312                 if (compstr.find (filterstr) != string::npos) {
313                         return true;
314                 } else {
315                         return false;
316                 }
317                 
318         }
319
320         return true;
321 }
322
323 void
324 PluginSelector::setup_filter_string (string& filterstr)
325 {
326         filterstr = filter_entry.get_text ();
327         transform (filterstr.begin(), filterstr.end(), filterstr.begin(), ::toupper);
328 }       
329
330 void
331 PluginSelector::ladspa_refiller ()
332 {
333         guint row;
334         PluginInfoList &plugs = manager->ladspa_plugin_info ();
335         PluginInfoList::iterator i;
336         char ibuf[16], obuf[16];
337
338         lmodel->clear();
339
340         std::string filterstr;
341         setup_filter_string (filterstr);
342         
343         for (row = 0, i=plugs.begin(); i != plugs.end(); ++i, ++row) {
344                 if (show_this_plugin (*i, filterstr)) {
345                         snprintf (ibuf, sizeof(ibuf)-1, "%u", (*i)->n_inputs.n_total());
346                         snprintf (obuf, sizeof(obuf)-1, "%u", (*i)->n_outputs.n_total());               
347
348                         TreeModel::Row newrow = *(lmodel->append());
349                         newrow[lcols.name] = (*i)->name.c_str();
350                         newrow[lcols.type] = (*i)->category.c_str();
351                         newrow[lcols.ins] = ibuf;
352                         newrow[lcols.outs] = obuf;
353                         newrow[lcols.plugin] = *i;
354                 }
355         }
356
357         lmodel->set_sort_column (0, SORT_ASCENDING);
358 }
359
360 #ifdef VST_SUPPORT
361
362 void
363 PluginSelector::vst_refiller ()
364 {
365         guint row;
366         PluginInfoList &plugs = manager->vst_plugin_info ();
367         PluginInfoList::iterator i;
368         char ibuf[16], obuf[16];
369         vmodel->clear();
370         
371         std::string filterstr;
372         setup_filter_string (filterstr);
373         
374         for (row = 0, i=plugs.begin(); i != plugs.end(); ++i, ++row) {
375
376                 if (show_this_plugin (*i, filterstr)) {
377                         snprintf (ibuf, sizeof(ibuf)-1, "%d", (*i)->n_inputs);
378                         snprintf (obuf, sizeof(obuf)-1, "%d", (*i)->n_outputs);         
379                         
380                         TreeModel::Row newrow = *(vmodel->append());
381                         newrow[vcols.name] = (*i)->name.c_str();
382                         newrow[vcols.ins] = ibuf;
383                         newrow[vcols.outs] = obuf;
384                         newrow[vcols.plugin] = *i;
385                 }
386         }
387         vmodel->set_sort_column (0, SORT_ASCENDING);
388 }
389
390 void
391 PluginSelector::vst_display_selection_changed()
392 {
393         if (vst_display.get_selection()->count_selected_rows() != 0) {
394                 btn_add->set_sensitive (true);
395         } else {
396                 btn_add->set_sensitive (false);
397         }
398
399         current_selection = ARDOUR::VST;
400 }
401
402 #endif //VST_SUPPORT
403
404 #ifdef HAVE_AUDIOUNIT
405
406 void
407 PluginSelector::au_refiller ()
408 {
409         guint row;
410         PluginInfoList plugs (AUPluginInfo::discover ());
411         PluginInfoList::iterator i;
412         char ibuf[16], obuf[16];
413         aumodel->clear();
414         
415         std::string filterstr;
416         setup_filter_string (filterstr);
417         
418         for (row = 0, i=plugs.begin(); i != plugs.end(); ++i, ++row) {
419
420                 if (show_this_plugin (*i, filterstr)) {
421
422                         snprintf (ibuf, sizeof(ibuf)-1, "%d", (*i)->n_inputs);
423                         snprintf (obuf, sizeof(obuf)-1, "%d", (*i)->n_outputs);         
424                         
425                         TreeModel::Row newrow = *(aumodel->append());
426                         newrow[aucols.name] = (*i)->name.c_str();
427                         newrow[aucols.ins] = ibuf;
428                         newrow[aucols.outs] = obuf;
429                         newrow[aucols.plugin] = *i;
430                 }
431         }
432
433         aumodel->set_sort_column (0, SORT_ASCENDING);
434 }
435
436 void
437 PluginSelector::au_display_selection_changed()
438 {
439         if (au_display.get_selection()->count_selected_rows() != 0) {
440                 btn_add->set_sensitive (true);
441         } else {
442                 btn_add->set_sensitive (false);
443         }
444         
445         current_selection = ARDOUR::AudioUnit;
446 }
447
448 #endif //HAVE_AUDIOUNIT
449
450 void
451 PluginSelector::use_plugin (PluginInfoPtr pi)
452 {
453         if (session == 0) {
454                 return;
455         }
456
457         PluginPtr plugin = pi->load (*session);
458
459         if (plugin) {
460                 PluginCreated (plugin);
461         }
462 }
463
464 void
465 PluginSelector::btn_add_clicked()
466 {
467         std::string name;
468         PluginInfoPtr pi;
469         TreeModel::Row newrow = *(amodel->append());
470         
471         TreeModel::Row row;
472
473         switch (current_selection) {
474                 case ARDOUR::LADSPA:
475                         row = *(ladspa_display.get_selection()->get_selected());
476                         name = row[lcols.name];
477                         pi = row[lcols.plugin];
478                         break;
479                 case ARDOUR::VST:
480 #ifdef VST_SUPPORT
481                         row = *(vst_display.get_selection()->get_selected());
482                         name = row[vcols.name];
483                         pi = row[vcols.plugin];
484 #endif
485                         break;
486                 case ARDOUR::AudioUnit:
487 #ifdef HAVE_AUDIOUNIT
488                         row = *(au_display.get_selection()->get_selected());
489                         name = row[aucols.name];
490                         pi = row[aucols.plugin];
491 #endif
492                         break;
493                 default:
494                         error << "Programming error.  Unknown plugin selected." << endmsg;
495                         return;
496         }
497
498         newrow[acols.text] = name;
499         newrow[acols.plugin] = pi;
500
501         if (!amodel->children().empty()) {
502                 set_response_sensitive (RESPONSE_APPLY, true);
503         }
504 }
505
506 void
507 PluginSelector::btn_remove_clicked()
508 {
509         TreeModel::iterator iter = added_list.get_selection()->get_selected();
510         
511         amodel->erase(iter);
512         if (amodel->children().empty()) {
513                 set_response_sensitive (RESPONSE_APPLY, false);
514         }
515 }
516
517 void
518 PluginSelector::btn_update_clicked()
519 {
520         manager->refresh ();
521         refill();
522 }
523
524 void
525 PluginSelector::refill()
526 {
527         ladspa_refiller ();
528 #ifdef VST_SUPPORT
529         vst_refiller ();
530 #endif  
531 #ifdef HAVE_AUDIOUNIT
532         au_refiller ();
533 #endif
534 }
535
536 void
537 PluginSelector::ladspa_display_selection_changed()
538 {
539         if (ladspa_display.get_selection()->count_selected_rows() != 0) {
540                 btn_add->set_sensitive (true);
541         } else {
542                 btn_add->set_sensitive (false);
543         }
544         
545         current_selection = ARDOUR::LADSPA;
546 }
547
548 void
549 PluginSelector::added_list_selection_changed()
550 {
551         if (added_list.get_selection()->count_selected_rows() != 0) {
552                 btn_remove->set_sensitive (true);
553         } else {
554                 btn_remove->set_sensitive (false);
555         }
556 }
557
558 int
559 PluginSelector::run ()
560 {
561         ResponseType r;
562         TreeModel::Children::iterator i;
563
564         r = (ResponseType) Dialog::run ();
565
566         switch (r) {
567         case RESPONSE_APPLY:
568                 for (i = amodel->children().begin(); i != amodel->children().end(); ++i) {
569                         use_plugin ((*i)[acols.plugin]);
570                 }
571                 break;
572
573         default:
574                 break;
575         }
576
577         cleanup ();
578
579         return (int) r;
580 }
581
582 void
583 PluginSelector::cleanup ()
584 {
585         hide();
586         amodel->clear();
587 }
588
589 void
590 PluginSelector::filter_button_clicked ()
591 {
592         filter_entry.set_text ("");
593 }
594
595 void
596 PluginSelector::filter_entry_changed ()
597 {
598         refill ();
599 }
600
601 void 
602 PluginSelector::filter_mode_changed ()
603 {
604         refill ();
605 }
606