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
345                 if (show_this_plugin (*i, filterstr)) {
346
347                         snprintf (ibuf, sizeof(ibuf)-1, "%d", (*i)->n_inputs);
348                         snprintf (obuf, sizeof(obuf)-1, "%d", (*i)->n_outputs);         
349                         TreeModel::Row newrow = *(lmodel->append());
350                         newrow[lcols.name] = (*i)->name.c_str();
351                         newrow[lcols.type] = (*i)->category.c_str();
352                         newrow[lcols.ins] = ibuf;
353                         newrow[lcols.outs] = obuf;
354                         newrow[lcols.plugin] = *i;
355                 }
356         }
357
358         lmodel->set_sort_column (0, SORT_ASCENDING);
359 }
360
361 #ifdef VST_SUPPORT
362
363 void
364 PluginSelector::vst_refiller ()
365 {
366         guint row;
367         PluginInfoList &plugs = manager->vst_plugin_info ();
368         PluginInfoList::iterator i;
369         char ibuf[16], obuf[16];
370         vmodel->clear();
371         
372         std::string filterstr;
373         setup_filter_string (filterstr);
374         
375         for (row = 0, i=plugs.begin(); i != plugs.end(); ++i, ++row) {
376
377                 if (show_this_plugin (*i, filterstr)) {
378                         snprintf (ibuf, sizeof(ibuf)-1, "%d", (*i)->n_inputs);
379                         snprintf (obuf, sizeof(obuf)-1, "%d", (*i)->n_outputs);         
380                         
381                         TreeModel::Row newrow = *(vmodel->append());
382                         newrow[vcols.name] = (*i)->name.c_str();
383                         newrow[vcols.ins] = ibuf;
384                         newrow[vcols.outs] = obuf;
385                         newrow[vcols.plugin] = *i;
386                 }
387         }
388         vmodel->set_sort_column (0, SORT_ASCENDING);
389 }
390
391 void
392 PluginSelector::vst_display_selection_changed()
393 {
394         if (vst_display.get_selection()->count_selected_rows() != 0) {
395                 btn_add->set_sensitive (true);
396         } else {
397                 btn_add->set_sensitive (false);
398         }
399
400         current_selection = ARDOUR::VST;
401 }
402
403 #endif //VST_SUPPORT
404
405 #ifdef HAVE_AUDIOUNIT
406
407 void
408 PluginSelector::au_refiller ()
409 {
410         guint row;
411         PluginInfoList plugs (AUPluginInfo::discover ());
412         PluginInfoList::iterator i;
413         char ibuf[16], obuf[16];
414         aumodel->clear();
415         
416         std::string filterstr;
417         setup_filter_string (filterstr);
418         
419         for (row = 0, i=plugs.begin(); i != plugs.end(); ++i, ++row) {
420
421                 if (show_this_plugin (*i, filterstr)) {
422
423                         snprintf (ibuf, sizeof(ibuf)-1, "%d", (*i)->n_inputs);
424                         snprintf (obuf, sizeof(obuf)-1, "%d", (*i)->n_outputs);         
425                         
426                         TreeModel::Row newrow = *(aumodel->append());
427                         newrow[aucols.name] = (*i)->name.c_str();
428                         newrow[aucols.ins] = ibuf;
429                         newrow[aucols.outs] = obuf;
430                         newrow[aucols.plugin] = *i;
431                 }
432         }
433
434         aumodel->set_sort_column (0, SORT_ASCENDING);
435 }
436
437 void
438 PluginSelector::au_display_selection_changed()
439 {
440         if (au_display.get_selection()->count_selected_rows() != 0) {
441                 btn_add->set_sensitive (true);
442         } else {
443                 btn_add->set_sensitive (false);
444         }
445         
446         current_selection = ARDOUR::AudioUnit;
447 }
448
449 #endif //HAVE_AUDIOUNIT
450
451 void
452 PluginSelector::use_plugin (PluginInfoPtr pi)
453 {
454         if (session == 0) {
455                 return;
456         }
457
458         PluginPtr plugin = pi->load (*session);
459
460         if (plugin) {
461                 PluginCreated (plugin);
462         }
463 }
464
465 void
466 PluginSelector::btn_add_clicked()
467 {
468         std::string name;
469         PluginInfoPtr pi;
470         TreeModel::Row newrow = *(amodel->append());
471         
472         TreeModel::Row row;
473
474         switch (current_selection) {
475                 case ARDOUR::LADSPA:
476                         row = *(ladspa_display.get_selection()->get_selected());
477                         name = row[lcols.name];
478                         pi = row[lcols.plugin];
479                         break;
480                 case ARDOUR::VST:
481 #ifdef VST_SUPPORT
482                         row = *(vst_display.get_selection()->get_selected());
483                         name = row[vcols.name];
484                         pi = row[vcols.plugin];
485 #endif
486                         break;
487                 case ARDOUR::AudioUnit:
488 #ifdef HAVE_AUDIOUNIT
489                         row = *(au_display.get_selection()->get_selected());
490                         name = row[aucols.name];
491                         pi = row[aucols.plugin];
492 #endif
493                         break;
494                 default:
495                         error << "Programming error.  Unknown plugin selected." << endmsg;
496                         return;
497         }
498
499         newrow[acols.text] = name;
500         newrow[acols.plugin] = pi;
501
502         if (!amodel->children().empty()) {
503                 set_response_sensitive (RESPONSE_APPLY, true);
504         }
505 }
506
507 void
508 PluginSelector::btn_remove_clicked()
509 {
510         TreeModel::iterator iter = added_list.get_selection()->get_selected();
511         
512         amodel->erase(iter);
513         if (amodel->children().empty()) {
514                 set_response_sensitive (RESPONSE_APPLY, false);
515         }
516 }
517
518 void
519 PluginSelector::btn_update_clicked()
520 {
521         manager->refresh ();
522         refill();
523 }
524
525 void
526 PluginSelector::refill()
527 {
528         ladspa_refiller ();
529 #ifdef VST_SUPPORT
530         vst_refiller ();
531 #endif  
532 #ifdef HAVE_AUDIOUNIT
533         au_refiller ();
534 #endif
535 }
536
537 void
538 PluginSelector::ladspa_display_selection_changed()
539 {
540         if (ladspa_display.get_selection()->count_selected_rows() != 0) {
541                 btn_add->set_sensitive (true);
542         } else {
543                 btn_add->set_sensitive (false);
544         }
545         
546         current_selection = ARDOUR::LADSPA;
547 }
548
549 void
550 PluginSelector::added_list_selection_changed()
551 {
552         if (added_list.get_selection()->count_selected_rows() != 0) {
553                 btn_remove->set_sensitive (true);
554         } else {
555                 btn_remove->set_sensitive (false);
556         }
557 }
558
559 int
560 PluginSelector::run ()
561 {
562         ResponseType r;
563         TreeModel::Children::iterator i;
564
565         r = (ResponseType) Dialog::run ();
566
567         switch (r) {
568         case RESPONSE_APPLY:
569                 for (i = amodel->children().begin(); i != amodel->children().end(); ++i) {
570                         use_plugin ((*i)[acols.plugin]);
571                 }
572                 break;
573
574         default:
575                 break;
576         }
577
578         cleanup ();
579
580         return (int) r;
581 }
582
583 void
584 PluginSelector::cleanup ()
585 {
586         hide();
587         amodel->clear();
588 }
589
590 void
591 PluginSelector::filter_button_clicked ()
592 {
593         filter_entry.set_text ("");
594 }
595
596 void
597 PluginSelector::filter_entry_changed ()
598 {
599         refill ();
600 }
601
602 void 
603 PluginSelector::filter_mode_changed ()
604 {
605         refill ();
606 }
607