Fix a bunch of warnings.
[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) { // FIXME: Impossible (unsigned)
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
294                         if ((*i)->n_outputs.n_total() < 0) { // FIXME: Impossible (unsigned)
295                                 newrow[plugin_columns.audio_outs] = "various";
296                                 newrow[plugin_columns.midi_outs] = "various";
297                         } else {
298                                 snprintf (buf, sizeof(buf), "%d", (*i)->n_outputs.n_audio());           
299                                 newrow[plugin_columns.audio_outs] = buf;
300                                 snprintf (buf, sizeof(buf), "%d", (*i)->n_outputs.n_midi());            
301                                 newrow[plugin_columns.midi_outs] = buf;
302                         }
303
304                         newrow[plugin_columns.plugin] = *i;
305                 }
306         }       
307 }
308
309 void
310 PluginSelector::ladspa_refiller (const std::string& filterstr)
311 {
312         refiller (manager->ladspa_plugin_info(), filterstr, "LADSPA");
313 }
314
315 void
316 PluginSelector::lv2_refiller (const std::string& filterstr)
317 {
318 #ifdef HAVE_LV2
319         refiller (manager->lv2_plugin_info(), filterstr, "LV2");
320 #endif
321 }
322
323 void
324 PluginSelector::vst_refiller (const std::string& filterstr)
325 {
326 #ifdef VST_SUPPORT
327         refiller (manager->vst_plugin_info(), filterstr, "VST");
328 #endif
329 }
330
331 void
332 PluginSelector::au_refiller (const std::string& filterstr)
333 {
334 #ifdef HAVE_AUDIOUNITS
335         refiller (manager->au_plugin_info(), filterstr, "AU");
336 #endif
337 }
338
339 PluginPtr
340 PluginSelector::load_plugin (PluginInfoPtr pi)
341 {
342         if (session == 0) {
343                 return PluginPtr();
344         }
345
346         return pi->load (*session);
347 }
348
349 void
350 PluginSelector::btn_add_clicked()
351 {
352         std::string name;
353         PluginInfoPtr pi;
354         TreeModel::Row newrow = *(amodel->append());
355         TreeModel::Row row;
356
357         row = *(plugin_display.get_selection()->get_selected());
358         name = row[plugin_columns.name];
359         pi = row[plugin_columns.plugin];
360
361         newrow[acols.text] = name;
362         newrow[acols.plugin] = pi;
363
364         if (!amodel->children().empty()) {
365                 set_response_sensitive (RESPONSE_APPLY, true);
366         }
367 }
368
369 void
370 PluginSelector::btn_remove_clicked()
371 {
372         TreeModel::iterator iter = added_list.get_selection()->get_selected();
373         
374         amodel->erase(iter);
375         if (amodel->children().empty()) {
376                 set_response_sensitive (RESPONSE_APPLY, false);
377         }
378 }
379
380 void
381 PluginSelector::btn_update_clicked()
382 {
383         manager->refresh ();
384         refill();
385 }
386
387 void
388 PluginSelector::display_selection_changed()
389 {
390         if (plugin_display.get_selection()->count_selected_rows() != 0) {
391                 btn_add->set_sensitive (true);
392         } else {
393                 btn_add->set_sensitive (false);
394         }
395 }
396
397 void
398 PluginSelector::added_list_selection_changed()
399 {
400         if (added_list.get_selection()->count_selected_rows() != 0) {
401                 btn_remove->set_sensitive (true);
402         } else {
403                 btn_remove->set_sensitive (false);
404         }
405 }
406
407 int
408 PluginSelector::run ()
409 {
410         ResponseType r;
411         TreeModel::Children::iterator i;
412         SelectedPlugins plugins;
413
414         r = (ResponseType) Dialog::run ();
415
416         switch (r) {
417         case RESPONSE_APPLY:
418                 for (i = amodel->children().begin(); i != amodel->children().end(); ++i) {
419                         PluginInfoPtr pp = (*i)[acols.plugin];
420                         PluginPtr p = load_plugin (pp);
421                         if (p) {
422                                 plugins.push_back (p);
423                         }
424                 }
425                 if (interested_object && !plugins.empty()) {
426                         interested_object->use_plugins (plugins);
427                 }
428                 
429                 break;
430
431         default:
432                 break;
433         }
434
435         hide();
436         amodel->clear();
437         interested_object = 0;
438
439         return (int) r;
440 }
441
442 void
443 PluginSelector::filter_button_clicked ()
444 {
445         filter_entry.set_text ("");
446 }
447
448 void
449 PluginSelector::filter_entry_changed ()
450 {
451         refill ();
452 }
453
454 void 
455 PluginSelector::filter_mode_changed ()
456 {
457         std::string mode = filter_mode.get_active_text ();
458
459         if (mode == _("Favorites only")) {
460                 filter_entry.set_sensitive (false);
461         } else {
462                 filter_entry.set_sensitive (true);
463         }
464
465         refill ();
466 }
467
468 void
469 PluginSelector::on_show ()
470 {
471         ArdourDialog::on_show ();
472         filter_entry.grab_focus ();
473 }
474
475 struct PluginMenuCompare {
476     bool operator() (PluginInfoPtr a, PluginInfoPtr b) const {
477             int cmp;
478
479             cmp = strcasecmp (a->creator.c_str(), b->creator.c_str());
480
481             if (cmp < 0) {
482                     return true;
483             } else if (cmp == 0) {
484                     /* same creator ... compare names */
485                     if (strcasecmp (a->name.c_str(), b->name.c_str()) < 0) {
486                             return true;
487                     } 
488             }
489             return false;
490     }
491 };
492
493 Gtk::Menu&
494 PluginSelector::plugin_menu()
495 {
496         using namespace Menu_Helpers;
497
498         typedef std::map<Glib::ustring,Gtk::Menu*> SubmenuMap;
499         SubmenuMap submenu_map;
500
501         if (!_menu) {
502                 _menu = new Menu();
503                 _menu->set_name("ArdourContextMenu");
504         } 
505
506         MenuList& items = _menu->items();
507         Menu* favs = new Menu();
508         favs->set_name("ArdourContextMenu");
509
510         items.clear ();
511         items.push_back (MenuElem (_("Favorites"), *favs));
512         items.push_back (MenuElem (_("Plugin Manager"), mem_fun (*this, &PluginSelector::show_manager)));
513         items.push_back (SeparatorElem ());
514
515         PluginInfoList all_plugs;
516
517         all_plugs.insert (all_plugs.end(), manager->ladspa_plugin_info().begin(), manager->ladspa_plugin_info().end());
518 #ifdef VST_SUPPORT
519         all_plugs.insert (all_plugs.end(), manager->vst_plugin_info().begin(), manager->vst_plugin_info().end());
520 #endif
521 #ifdef HAVE_AUDIOUNITS
522         all_plugs.insert (all_plugs.end(), manager->au_plugin_info().begin(), manager->au_plugin_info().end());
523 #endif
524 #ifdef HAVE_LV2
525         all_plugs.insert (all_plugs.end(), manager->lv2_plugin_info().begin(), manager->lv2_plugin_info().end());
526 #endif
527
528         PluginMenuCompare cmp;
529         all_plugs.sort (cmp);
530
531         for (PluginInfoList::const_iterator i = all_plugs.begin(); i != all_plugs.end(); ++i) {
532                 SubmenuMap::iterator x;
533                 Gtk::Menu* submenu;
534
535                 string creator = (*i)->creator;
536                 string::size_type pos = 0;
537
538                 if (manager->is_a_favorite_plugin (*i)) {
539                         favs->items().push_back (MenuElem ((*i)->name, (bind (mem_fun (*this, &PluginSelector::plugin_chosen_from_menu), *i))));
540                 }
541                 
542                 /* stupid LADSPA creator strings */
543                 
544                 while (pos < creator.length() && (isalnum (creator[pos]) || isspace (creator[pos]))) ++pos;
545                 creator = creator.substr (0, pos);
546
547                 if ((x = submenu_map.find (creator)) != submenu_map.end()) {
548                         submenu = x->second;
549                 } else {
550                         submenu = new Gtk::Menu;
551                         items.push_back (MenuElem (creator, *submenu));
552                         submenu_map.insert (pair<Glib::ustring,Menu*> (creator, submenu));
553                         submenu->set_name("ArdourContextMenu");
554                 }
555                 
556                 submenu->items().push_back (MenuElem ((*i)->name, (bind (mem_fun (*this, &PluginSelector::plugin_chosen_from_menu), *i))));
557         }
558         
559         return *_menu;
560 }
561
562 void
563 PluginSelector::plugin_chosen_from_menu (const PluginInfoPtr& pi)
564 {
565         PluginPtr p = load_plugin (pi);
566
567         if (p && interested_object) {
568                 SelectedPlugins plugins;
569                 plugins.push_back (p);
570                 interested_object->use_plugins (plugins);
571         }
572
573         interested_object = 0;
574 }
575
576 void 
577 PluginSelector::favorite_changed (const Glib::ustring& path)
578 {
579         PluginInfoPtr pi;
580
581         if (in_row_change) {
582                 return;
583         }
584
585         in_row_change = true;
586         
587         TreeModel::iterator iter = plugin_model->get_iter (path);
588         
589         if (iter) {
590
591                 bool favorite = !(*iter)[plugin_columns.favorite];
592
593                 /* change state */
594
595                 (*iter)[plugin_columns.favorite] = favorite;
596
597                 /* save new favorites list */
598
599                 pi = (*iter)[plugin_columns.plugin];
600                 
601                 if (favorite) {
602                         manager->add_favorite (pi->type, pi->unique_id);
603                 } else {
604                         manager->remove_favorite (pi->type, pi->unique_id);
605                 }
606                 
607                 manager->save_favorites ();
608         }
609         in_row_change = false;
610 }
611
612 void
613 PluginSelector::show_manager ()
614 {
615         show_all();
616         run ();
617 }
618
619 void
620 PluginSelector::set_interested_object (PluginInterestedObject& obj)
621 {
622         interested_object = &obj;
623 }