Fix various snap issues when dragging control points (AutomationRegionView).
[ardour.git] / gtk2_ardour / instrument_selector.cc
1 /*
2   Copyright (C) 2003-2014 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 #include "pbd/convert.h"
20 #include "ardour/plugin_manager.h"
21 #include "gtkmm2ext/gui_thread.h"
22 #include "instrument_selector.h"
23
24 #include "pbd/i18n.h"
25
26 using namespace Gtk;
27 using namespace ARDOUR;
28
29 InstrumentSelector::InstrumentSelector()
30         : _reasonable_synth_id(0)
31 {
32         refill ();
33
34         PluginManager::instance ().PluginListChanged.connect (_update_connection, invalidator (*this), boost::bind (&InstrumentSelector::refill, this), gui_context());
35 }
36
37 void
38 InstrumentSelector::refill()
39 {
40         TreeModel::iterator iter = get_active();
41         std::string selected;
42         if (iter) {
43                 const TreeModel::Row& row = (*iter);
44                 selected = row[_instrument_list_columns.name];
45         }
46
47         unset_model ();
48         clear ();
49         build_instrument_list();
50         set_model(_instrument_list);
51         pack_start(_instrument_list_columns.name);
52         if (selected.empty ()) {
53                 set_active(_reasonable_synth_id);
54         } else {
55                 TreeModel::Children rows = _instrument_list->children();
56                 TreeModel::Children::iterator i;
57                 for (i = rows.begin(); i != rows.end(); ++i) {
58                         std::string cn = (*i)[_instrument_list_columns.name];
59                         if (cn == selected) {
60                                 set_active(*i);
61                                 break;
62                         }
63                 }
64         }
65         set_button_sensitivity(Gtk::SENSITIVITY_AUTO);
66 }
67
68 static bool
69 pluginsort (const PluginInfoPtr& a, const PluginInfoPtr& b)
70 {
71         return PBD::downcase(a->name) < PBD::downcase(b->name);
72 }
73
74 void
75 InstrumentSelector::build_instrument_list()
76 {
77         PluginManager& manager = PluginManager::instance();
78
79         PluginInfoList all_plugs;
80         all_plugs.insert(all_plugs.end(), manager.ladspa_plugin_info().begin(), manager.ladspa_plugin_info().end());
81         all_plugs.insert(all_plugs.end(), manager.lua_plugin_info().begin(), manager.lua_plugin_info().end());
82 #ifdef WINDOWS_VST_SUPPORT
83         all_plugs.insert(all_plugs.end(), manager.windows_vst_plugin_info().begin(), manager.windows_vst_plugin_info().end());
84 #endif
85 #ifdef LXVST_SUPPORT
86         all_plugs.insert(all_plugs.end(), manager.lxvst_plugin_info().begin(), manager.lxvst_plugin_info().end());
87 #endif
88 #ifdef AUDIOUNIT_SUPPORT
89         all_plugs.insert(all_plugs.end(), manager.au_plugin_info().begin(), manager.au_plugin_info().end());
90 #endif
91 #ifdef LV2_SUPPORT
92         all_plugs.insert(all_plugs.end(), manager.lv2_plugin_info().begin(), manager.lv2_plugin_info().end());
93 #endif
94
95         all_plugs.sort (pluginsort);
96
97         _instrument_list = ListStore::create(_instrument_list_columns);
98
99         TreeModel::Row row = *(_instrument_list->append());
100         row[_instrument_list_columns.info_ptr] = PluginInfoPtr();
101         row[_instrument_list_columns.name]     = _("-none-");
102
103         uint32_t n = 1;
104         for (PluginInfoList::const_iterator i = all_plugs.begin(); i != all_plugs.end(); ++i) {
105                 if (manager.get_status(*i) == PluginManager::Hidden) {
106                         continue;
107                 }
108
109                 if ((*i)->is_instrument()) {
110                         row                                    = *(_instrument_list->append());
111                         row[_instrument_list_columns.name]     = (*i)->name;
112                         row[_instrument_list_columns.info_ptr] = *i;
113                         if ((*i)->unique_id == "https://community.ardour.org/node/7596") {
114                                 _reasonable_synth_id = n;
115                         }
116                         n++;
117                 }
118         }
119 }
120
121 PluginInfoPtr
122 InstrumentSelector::selected_instrument()
123 {
124         TreeModel::iterator iter = get_active();
125         if (!iter) {
126                 return PluginInfoPtr();
127         }
128
129         const TreeModel::Row& row = (*iter);
130         return row[_instrument_list_columns.info_ptr];
131 }