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