* first working version of editing MIDI channels of individual notes, see: http:...
[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         plugin_model = Gtk::ListStore::create (plugin_columns);
69         plugin_display.set_model (plugin_model);
70         plugin_display.append_column (_("Available Plugins"), plugin_columns.name);
71         plugin_display.append_column (_("Type"), plugin_columns.type_name);
72         plugin_display.append_column (_("Category"), plugin_columns.category);
73         plugin_display.append_column (_("Creator"), plugin_columns.creator);
74         plugin_display.append_column (_("# Audio In"),plugin_columns.audio_ins);
75         plugin_display.append_column (_("# Audio Out"), plugin_columns.audio_outs);
76         plugin_display.append_column (_("# MIDI In"),plugin_columns.midi_ins);
77         plugin_display.append_column (_("# MIDI Out"), plugin_columns.midi_outs);
78         plugin_display.set_headers_visible (true);
79         plugin_display.set_headers_clickable (true);
80         plugin_display.set_reorderable (false);
81         scroller.set_border_width(10);
82         scroller.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
83         scroller.add(plugin_display);
84
85         amodel = Gtk::ListStore::create(acols);
86         added_list.set_model (amodel);
87         added_list.append_column (_("Plugins to be connected"), acols.text);
88         added_list.set_headers_visible (true);
89         added_list.set_reorderable (false);
90
91         for (int i = 0; i <=7; i++) {
92                 Gtk::TreeView::Column* column = plugin_display.get_column(i);
93                 column->set_sort_column(i);
94         }
95
96         ascroller.set_border_width(10);
97         ascroller.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
98         ascroller.add(added_list);
99         btn_add = manage(new Gtk::Button(Stock::ADD));
100         ARDOUR_UI::instance()->tooltips().set_tip(*btn_add, _("Add a plugin to the effect list"));
101         btn_add->set_sensitive (false);
102         btn_remove = manage(new Gtk::Button(Stock::REMOVE));
103         btn_remove->set_sensitive (false);
104         ARDOUR_UI::instance()->tooltips().set_tip(*btn_remove, _("Remove a plugin from the effect list"));
105         Gtk::Button *btn_update = manage(new Gtk::Button(Stock::REFRESH));
106         ARDOUR_UI::instance()->tooltips().set_tip(*btn_update, _("Update available plugins"));
107
108         btn_add->set_name("PluginSelectorButton");
109         btn_remove->set_name("PluginSelectorButton");
110
111         Gtk::Table* table = manage(new Gtk::Table(7, 11));
112         table->set_size_request(750, 500);
113         table->attach(scroller, 0, 7, 0, 5);
114
115         HBox* filter_box = manage (new HBox);
116
117         vector<string> filter_strings = I18N (_filter_mode_strings);
118         Gtkmm2ext::set_popdown_strings (filter_mode, filter_strings);
119         filter_mode.set_active_text (filter_strings.front());
120
121         filter_box->pack_start (filter_mode, false, false);
122         filter_box->pack_start (filter_entry, true, true);
123         filter_box->pack_start (filter_button, false, false);
124
125         filter_entry.signal_changed().connect (mem_fun (*this, &PluginSelector::filter_entry_changed));
126         filter_button.signal_clicked().connect (mem_fun (*this, &PluginSelector::filter_button_clicked));
127         filter_mode.signal_changed().connect (mem_fun (*this, &PluginSelector::filter_mode_changed));
128
129         filter_box->show ();
130         filter_mode.show ();
131         filter_entry.show ();
132         filter_button.show ();
133
134         table->attach (*filter_box, 0, 7, 5, 6, FILL|EXPAND, FILL, 5, 5);
135
136         table->attach(*btn_add, 1, 2, 6, 7, FILL, FILL, 5, 5); 
137         table->attach(*btn_remove, 3, 4, 6, 7, FILL, FILL, 5, 5);
138         table->attach(*btn_update, 5, 6, 6, 7, FILL, FILL, 5, 5);
139
140         table->attach(ascroller, 0, 7, 8, 10);
141
142         add_button (Stock::CANCEL, RESPONSE_CANCEL);
143         add_button (_("Insert Plugin(s)"), RESPONSE_APPLY);
144         set_default_response (RESPONSE_APPLY);
145         set_response_sensitive (RESPONSE_APPLY, false);
146         get_vbox()->pack_start (*table);
147
148         table->set_name("PluginSelectorTable");
149         plugin_display.set_name("PluginSelectorDisplay");
150         //plugin_display.set_name("PluginSelectorList");
151         added_list.set_name("PluginSelectorList");
152
153         plugin_display.signal_button_press_event().connect_notify (mem_fun(*this, &PluginSelector::row_clicked));
154         plugin_display.get_selection()->signal_changed().connect (mem_fun(*this, &PluginSelector::display_selection_changed));
155         plugin_display.grab_focus();
156         
157         btn_update->signal_clicked().connect (mem_fun(*this, &PluginSelector::btn_update_clicked));
158         btn_add->signal_clicked().connect(mem_fun(*this, &PluginSelector::btn_add_clicked));
159         btn_remove->signal_clicked().connect(mem_fun(*this, &PluginSelector::btn_remove_clicked));
160         added_list.get_selection()->signal_changed().connect (mem_fun(*this, &PluginSelector::added_list_selection_changed));
161
162         refill ();
163 }
164
165 void
166 PluginSelector::row_clicked(GdkEventButton* event)
167 {
168         if (event->type == GDK_2BUTTON_PRESS)
169                 btn_add_clicked();
170 }
171
172 void
173 PluginSelector::set_session (Session* s)
174 {
175         ENSURE_GUI_THREAD(bind (mem_fun(*this, &PluginSelector::set_session), s));
176         
177         session = s;
178
179         if (session) {
180                 session->GoingAway.connect (bind (mem_fun(*this, &PluginSelector::set_session), static_cast<Session*> (0)));
181         }
182 }
183
184 bool
185 PluginSelector::show_this_plugin (const PluginInfoPtr& info, const std::string& filterstr)
186 {
187         std::string compstr;
188         std::string mode = filter_mode.get_active_text ();
189
190         if (!filterstr.empty()) {
191                 
192                 if (mode == _("Name contains")) {
193                         compstr = info->name;
194                 } else if (mode == _("Type contains")) {
195                         compstr = info->category;
196                 } else if (mode == _("Author contains")) {
197                         compstr = info->creator;
198                 } else if (mode == _("Library contains")) {
199                         compstr = info->path;
200                 }
201                 
202                 transform (compstr.begin(), compstr.end(), compstr.begin(), ::toupper);
203
204                 if (compstr.find (filterstr) != string::npos) {
205                         return true;
206                 } else {
207                         return false;
208                 }
209                 
210         }
211
212         return true;
213 }
214
215 void
216 PluginSelector::setup_filter_string (string& filterstr)
217 {
218         filterstr = filter_entry.get_text ();
219         transform (filterstr.begin(), filterstr.end(), filterstr.begin(), ::toupper);
220 }       
221
222 void
223 PluginSelector::refill ()
224 {
225         std::string filterstr;
226
227         plugin_model->clear ();
228
229         setup_filter_string (filterstr);
230
231         ladspa_refiller (filterstr);
232         lv2_refiller (filterstr);
233         vst_refiller (filterstr);
234         au_refiller (filterstr);
235 }
236
237 void
238 PluginSelector::refiller (const PluginInfoList& plugs, const::std::string& filterstr, const char* type)
239 {
240         char buf[16];
241
242         for (PluginInfoList::const_iterator i = plugs.begin(); i != plugs.end(); ++i) {
243
244                 if (show_this_plugin (*i, filterstr)) {
245
246                         TreeModel::Row newrow = *(plugin_model->append());
247                         newrow[plugin_columns.name] = (*i)->name;
248                         newrow[plugin_columns.type_name] = type;
249                         newrow[plugin_columns.category] = (*i)->category;
250
251
252                         string creator = (*i)->creator;
253                         string::size_type pos = 0;
254
255                         /* stupid LADSPA creator strings */
256
257                         while (pos < creator.length() && (isalnum (creator[pos]) || isspace (creator[pos]))) ++pos;
258                         creator = creator.substr (0, pos);
259
260                         newrow[plugin_columns.creator] = creator;
261
262                         if ((*i)->n_inputs.n_total() < 0) {
263                                 newrow[plugin_columns.audio_ins] = "various";
264                                 newrow[plugin_columns.midi_ins] = "various";
265                         } else {
266                                 snprintf (buf, sizeof(buf), "%d", (*i)->n_inputs.n_audio());
267                                 newrow[plugin_columns.audio_ins] = buf;
268                                 snprintf (buf, sizeof(buf), "%d", (*i)->n_inputs.n_midi());
269                                 newrow[plugin_columns.midi_ins] = buf;
270                         }
271                         if ((*i)->n_outputs.n_total() < 0) {
272                                 newrow[plugin_columns.audio_outs] = "various";
273                                 newrow[plugin_columns.midi_outs] = "various";
274                         } else {
275                                 snprintf (buf, sizeof(buf), "%d", (*i)->n_outputs.n_audio());           
276                                 newrow[plugin_columns.audio_outs] = buf;
277                                 snprintf (buf, sizeof(buf), "%d", (*i)->n_outputs.n_midi());            
278                                 newrow[plugin_columns.midi_outs] = buf;
279                         }
280
281                         newrow[plugin_columns.plugin] = *i;
282                 }
283         }       
284 }
285
286 void
287 PluginSelector::ladspa_refiller (const std::string& filterstr)
288 {
289         refiller (manager->ladspa_plugin_info(), filterstr, "LADSPA");
290 }
291
292 void
293 PluginSelector::lv2_refiller (const std::string& filterstr)
294 {
295 #ifdef HAVE_SLV2
296         refiller (manager->lv2_plugin_info(), filterstr, "LV2");
297 #endif
298 }
299
300 void
301 PluginSelector::vst_refiller (const std::string& filterstr)
302 {
303 #ifdef VST_SUPPORT
304         refiller (manager->vst_plugin_info(), filterstr, "VST");
305 #endif
306 }
307
308 void
309 PluginSelector::au_refiller (const std::string& filterstr)
310 {
311 #ifdef HAVE_AUDIOUNITS
312         refiller (manager->au_plugin_info(), filterstr, "AU");
313 #endif
314 }
315
316 void
317 PluginSelector::use_plugin (PluginInfoPtr pi)
318 {
319         if (session == 0) {
320                 return;
321         }
322
323         PluginPtr plugin = pi->load (*session);
324
325         if (plugin) {
326                 PluginCreated (plugin);
327         }
328 }
329
330 void
331 PluginSelector::btn_add_clicked()
332 {
333         std::string name;
334         PluginInfoPtr pi;
335         TreeModel::Row newrow = *(amodel->append());
336         TreeModel::Row row;
337
338         row = *(plugin_display.get_selection()->get_selected());
339         name = row[plugin_columns.name];
340         pi = row[plugin_columns.plugin];
341
342         newrow[acols.text] = name;
343         newrow[acols.plugin] = pi;
344
345         if (!amodel->children().empty()) {
346                 set_response_sensitive (RESPONSE_APPLY, true);
347         }
348 }
349
350 void
351 PluginSelector::btn_remove_clicked()
352 {
353         TreeModel::iterator iter = added_list.get_selection()->get_selected();
354         
355         amodel->erase(iter);
356         if (amodel->children().empty()) {
357                 set_response_sensitive (RESPONSE_APPLY, false);
358         }
359 }
360
361 void
362 PluginSelector::btn_update_clicked()
363 {
364         manager->refresh ();
365         refill();
366 }
367
368 void
369 PluginSelector::display_selection_changed()
370 {
371         if (plugin_display.get_selection()->count_selected_rows() != 0) {
372                 btn_add->set_sensitive (true);
373         } else {
374                 btn_add->set_sensitive (false);
375         }
376 }
377
378 void
379 PluginSelector::added_list_selection_changed()
380 {
381         if (added_list.get_selection()->count_selected_rows() != 0) {
382                 btn_remove->set_sensitive (true);
383         } else {
384                 btn_remove->set_sensitive (false);
385         }
386 }
387
388 int
389 PluginSelector::run ()
390 {
391         ResponseType r;
392         TreeModel::Children::iterator i;
393
394         r = (ResponseType) Dialog::run ();
395
396         switch (r) {
397         case RESPONSE_APPLY:
398                 for (i = amodel->children().begin(); i != amodel->children().end(); ++i) {
399                         PluginInfoPtr pp = (*i)[acols.plugin];
400                         use_plugin (pp);
401                 }
402                 break;
403
404         default:
405                 break;
406         }
407
408         cleanup ();
409
410         return (int) r;
411 }
412
413 void
414 PluginSelector::cleanup ()
415 {
416         hide();
417         amodel->clear();
418 }
419
420 void
421 PluginSelector::filter_button_clicked ()
422 {
423         filter_entry.set_text ("");
424 }
425
426 void
427 PluginSelector::filter_entry_changed ()
428 {
429         refill ();
430 }
431
432 void 
433 PluginSelector::filter_mode_changed ()
434 {
435         refill ();
436 }
437
438 void
439 PluginSelector::on_show ()
440 {
441         ArdourDialog::on_show ();
442         filter_entry.grab_focus ();
443 }