prototype [LV2]patch-change support for generic plugin UIs.
[ardour.git] / gtk2_ardour / generic_pluginui.cc
1 /*
2     Copyright (C) 2000 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 #ifdef WAF_BUILD
21 #include "gtk2ardour-config.h"
22 #endif
23
24 #include <climits>
25 #include <cerrno>
26 #include <cmath>
27 #include <string>
28 #include <vector>
29
30 #include "pbd/stl_delete.h"
31 #include "pbd/xml++.h"
32 #include "pbd/failed_constructor.h"
33
34 #include <gtkmm2ext/click_box.h>
35 #include <gtkmm2ext/fastmeter.h>
36 #include <gtkmm2ext/barcontroller.h>
37 #include <gtkmm2ext/utils.h>
38 #include <gtkmm2ext/doi.h>
39 #include <gtkmm2ext/slider_controller.h>
40
41 #include "ardour/plugin.h"
42 #include "ardour/plugin_insert.h"
43 #include "ardour/session.h"
44 #ifdef LV2_SUPPORT
45 #include "ardour/lv2_plugin.h"
46 #endif
47
48 #include "ardour_ui.h"
49 #include "prompter.h"
50 #include "plugin_ui.h"
51 #include "gui_thread.h"
52 #include "automation_controller.h"
53
54 #include "i18n.h"
55
56 using namespace std;
57 using namespace ARDOUR;
58 using namespace PBD;
59 using namespace Gtkmm2ext;
60 using namespace Gtk;
61
62 GenericPluginUI::GenericPluginUI (boost::shared_ptr<PluginInsert> pi, bool scrollable)
63         : PlugUIBase (pi),
64           button_table (initial_button_rows, initial_button_cols),
65           output_table (initial_output_rows, initial_output_cols),
66           hAdjustment(0.0, 0.0, 0.0),
67           vAdjustment(0.0, 0.0, 0.0),
68           scroller_view(hAdjustment, vAdjustment),
69           automation_menu (0),
70           is_scrollable(scrollable)
71 {
72         set_name ("PluginEditor");
73         set_border_width (10);
74         //set_homogeneous (false);
75
76         pack_start (main_contents, true, true);
77         settings_box.set_homogeneous (false);
78
79         HBox* constraint_hbox = manage (new HBox);
80         HBox* smaller_hbox = manage (new HBox);
81         smaller_hbox->set_spacing (4);
82         Label* combo_label = manage (new Label (_("<span size=\"large\">Presets</span>")));
83         combo_label->set_use_markup (true);
84
85         latency_button.add (latency_label);
86         latency_button.signal_clicked().connect (sigc::mem_fun (*this, &PlugUIBase::latency_button_clicked));
87         set_latency_label ();
88
89         smaller_hbox->pack_start (latency_button, false, false, 10);
90         smaller_hbox->pack_start (_preset_combo, false, false);
91         smaller_hbox->pack_start (_preset_modified, false, false);
92         smaller_hbox->pack_start (add_button, false, false);
93         smaller_hbox->pack_start (save_button, false, false);
94         smaller_hbox->pack_start (delete_button, false, false);
95         smaller_hbox->pack_start (bypass_button, false, true);
96
97         constraint_hbox->set_spacing (5);
98         constraint_hbox->set_homogeneous (false);
99
100         VBox* v1_box = manage (new VBox);
101         VBox* v2_box = manage (new VBox);
102         pack_end (plugin_analysis_expander, false, false);
103         if (!plugin->get_docs().empty()) {
104                 pack_end (description_expander, false, false);
105         }
106
107         v1_box->pack_start (*smaller_hbox, false, true);
108         v2_box->pack_start (focus_button, false, true);
109
110         main_contents.pack_start (settings_box, false, false);
111
112         constraint_hbox->pack_end (*v2_box, false, false);
113         constraint_hbox->pack_end (*v1_box, false, false);
114
115         main_contents.pack_start (*constraint_hbox, false, false);
116
117         if (is_scrollable ) {
118                 scroller.set_policy (Gtk::POLICY_NEVER, Gtk::POLICY_AUTOMATIC);
119                 scroller.set_name ("PluginEditor");
120                 scroller_view.set_name("PluginEditor");
121                 scroller_view.add (hpacker);
122                 scroller.add (scroller_view);
123
124                 main_contents.pack_start (scroller, true, true);
125
126         } else {
127                 main_contents.pack_start (hpacker, false, false);
128         }
129
130         pi->ActiveChanged.connect (active_connection, invalidator (*this), boost::bind (&GenericPluginUI::processor_active_changed, this, boost::weak_ptr<Processor>(pi)), gui_context());
131
132         bypass_button.set_active (!pi->active());
133
134         prefheight = 0;
135         build ();
136 }
137
138 GenericPluginUI::~GenericPluginUI ()
139 {
140         if (output_controls.size() > 0) {
141                 screen_update_connection.disconnect();
142         }
143 }
144
145 // Some functions for calculating the 'similarity' of two plugin
146 // control labels.
147
148 static int get_number(string label) {
149 static const char *digits = "0123456789";
150 int value = -1;
151
152         std::size_t first_digit_pos = label.find_first_of(digits);
153         if (first_digit_pos != string::npos) {
154                 // found some digits: there's a number in there somewhere
155                 stringstream s;
156                 s << label.substr(first_digit_pos);
157                 s >> value;
158         }
159         return value;
160 }
161
162 static int match_or_digit(char c1, char c2) {
163         return c1 == c2 || (isdigit(c1) && isdigit(c2));
164 }       
165
166 static std::size_t matching_chars_at_head(const string s1, const string s2) {
167 std::size_t length, n = 0;
168
169         length = min(s1.length(), s2.length());
170         while (n < length) {
171                 if (!match_or_digit(s1[n], s2[n]))
172                         break;
173                 n++;
174         } 
175         return n;
176 }
177
178 static std::size_t matching_chars_at_tail(const string s1, const string s2) {
179 std::size_t s1pos, s2pos, n = 0;
180
181         s1pos = s1.length();
182         s2pos = s2.length();
183         while (s1pos-- > 0 && s2pos-- > 0) {
184                 if (!match_or_digit(s1[s1pos], s2[s2pos])       )
185                         break;
186                 n++;
187         } 
188         return n;
189 }
190
191 static const guint32 min_controls_per_column = 17, max_controls_per_column = 24;
192 static const float default_similarity_threshold = 0.3;
193
194 void
195 GenericPluginUI::build ()
196 {
197         guint32 i = 0;
198         guint32 x = 0;
199         Frame* frame;
200         Frame* bt_frame;
201         VBox* box;
202         int output_row, output_col;
203         int button_row, button_col;
204         int output_rows, output_cols;
205         int button_rows, button_cols;
206
207         hpacker.set_spacing (10);
208
209         output_rows = initial_output_rows;
210         output_cols = initial_output_cols;
211         button_rows = initial_button_rows;
212         button_cols = initial_button_cols;
213         output_row = 0;
214         button_row = 0;
215         output_col = 0;
216         button_col = 0;
217
218         button_table.set_homogeneous (false);
219         button_table.set_row_spacings (2);
220         button_table.set_col_spacings (2);
221         output_table.set_homogeneous (true);
222         output_table.set_row_spacings (2);
223         output_table.set_col_spacings (2);
224         button_table.set_border_width (5);
225         output_table.set_border_width (5);
226
227         hpacker.set_border_width (10);
228
229         bt_frame = manage (new Frame);
230         bt_frame->set_name ("BaseFrame");
231         bt_frame->set_label (_("Switches"));
232         bt_frame->add (button_table);
233         hpacker.pack_start(*bt_frame, true, true);
234
235         box = manage (new VBox);
236         box->set_border_width (5);
237         box->set_spacing (1);
238
239         frame = manage (new Frame);
240         frame->set_name ("BaseFrame");
241         frame->set_label (_("Controls"));
242         frame->add (*box);
243         hpacker.pack_start(*frame, true, true);
244
245         /* find all ports. build control elements for all appropriate control ports */
246         std::vector<ControlUI *> cui_controls_list;
247
248         for (i = 0; i < plugin->parameter_count(); ++i) {
249
250                 if (plugin->parameter_is_control (i)) {
251
252                         /* Don't show latency control ports */
253
254                         if (plugin->describe_parameter (Evoral::Parameter(PluginAutomation, 0, i)) == X_("latency")) {
255                                 continue;
256                         }
257
258                         if (plugin->describe_parameter (Evoral::Parameter(PluginAutomation, 0, i)) == X_("hidden")) {
259                                 continue;
260                         }
261
262                         ControlUI* cui;
263
264                         boost::shared_ptr<ARDOUR::AutomationControl> c
265                                 = boost::dynamic_pointer_cast<ARDOUR::AutomationControl>(
266                                         insert->control(Evoral::Parameter(PluginAutomation, 0, i)));
267
268                         if ((cui = build_control_ui (i, c)) == 0) {
269                                 error << string_compose(_("Plugin Editor: could not build control element for port %1"), i) << endmsg;
270                                 continue;
271                         }
272
273                         const std::string param_docs = plugin->get_parameter_docs(i);
274                         if (!param_docs.empty()) {
275                                 ARDOUR_UI::instance()->set_tip(cui, param_docs.c_str());
276                         }
277
278                         if (cui->controller || cui->clickbox || cui->combo) {
279                                 // Get all of the controls into a list, so that
280                                 // we can lay them out a bit more nicely later.
281                                 cui_controls_list.push_back(cui);
282                         } else if (cui->button) {
283
284                                 if (!is_scrollable && button_row == button_rows) {
285                                         button_row = 0;
286                                         if (++button_col == button_cols) {
287                                                 button_cols += 2;
288                                                 button_table.resize (button_rows, button_cols);
289                                         }
290                                 }
291
292                                 button_table.attach (*cui, button_col, button_col + 1, button_row, button_row+1,
293                                                      FILL|EXPAND, FILL);
294                                 button_row++;
295
296                         } else if (cui->display) {
297
298                                 output_table.attach (*cui, output_col, output_col + 1, output_row, output_row+1,
299                                                      FILL|EXPAND, FILL);
300
301                                 // TODO: The meters should be divided into multiple rows
302
303                                 if (++output_col == output_cols) {
304                                         output_cols ++;
305                                         output_table.resize (output_rows, output_cols);
306                                 }
307                         }
308                 } 
309         }
310
311 #ifdef LV2_SUPPORT
312         boost::shared_ptr<ARDOUR::LV2Plugin> lv2p = boost::dynamic_pointer_cast<LV2Plugin> (plugin);
313         if (lv2p) {
314                 _fcb = (Gtk::FileChooserButton**) malloc(lv2p->patch_count() * sizeof(Gtk::FileChooserButton*));
315                 for (uint32_t p = 0; p < lv2p->patch_count(); ++p) {
316                         _fcb[p] = manage (new Gtk::FileChooserButton (Gtk::FILE_CHOOSER_ACTION_OPEN));
317                         _fcb[p]->signal_file_set().connect (sigc::bind(sigc::mem_fun (*this, &GenericPluginUI::patch_set_file), p));
318                         lv2p->PatchChanged.connect (*this, invalidator (*this), boost::bind (&GenericPluginUI::patch_changed, this, _1), gui_context());
319                         // when user cancels file selection the FileChooserButton will display "None"
320                         // TODO hack away around this..
321                         if (lv2p->patch_val(p)) {
322                                 _fcb[p]->set_filename(lv2p->patch_val(p));
323                         }
324                         if (lv2p->patch_key(p)) {
325                                 _fcb[p]->set_title(lv2p->patch_key(p));
326                                 Gtk::Label* fcl = manage (new Label (lv2p->patch_key(p)));
327                                 button_table.attach (*fcl, 0, button_cols, button_row, button_row + 1, FILL|EXPAND, FILL);
328                                 ++button_row;
329                         } else {
330                                 _fcb[p]->set_title(_("LV2 Patch"));
331                         }
332
333                         button_table.attach (*_fcb[p], 0, button_cols, button_row, button_row + 1, FILL|EXPAND, FILL);
334                         ++button_row;
335                 }
336         }
337 #endif
338
339         // Iterate over the list of controls to find which adjacent controls
340         // are similar enough to be grouped together.
341         
342         string label, previous_label = "";
343         std::vector<int> numbers_in_labels(cui_controls_list.size());
344         
345         std::vector<float> similarity_scores(cui_controls_list.size());
346         float most_similar = 0.0, least_similar = 1.0;
347         
348         i = 0;
349         for (vector<ControlUI*>::iterator cuip = cui_controls_list.begin(); cuip != cui_controls_list.end(); ++cuip, ++i) {
350                 label = (*cuip)->label.get_text();
351                 numbers_in_labels[i] = get_number(label);
352
353                 if (i > 0) {
354                         // A hand-wavy calculation of how similar this control's
355                         // label is to the previous.
356                         similarity_scores[i] = 
357                                 (float) ( 
358                                         ( matching_chars_at_head(label, previous_label) + 
359                                           matching_chars_at_tail(label, previous_label) +
360                                           1 
361                                         ) 
362                                 ) / (label.length() + previous_label.length());
363                         if (numbers_in_labels[i] >= 0) {
364                                 similarity_scores[i] += (numbers_in_labels[i] == numbers_in_labels[i-1]);
365                         }
366                         least_similar = min(least_similar, similarity_scores[i]);
367                         most_similar  = max(most_similar, similarity_scores[i]);
368                 } else {
369                         similarity_scores[0] = 1.0;
370                 }
371
372                 // cerr << "label: " << label << " sim: " << fixed << setprecision(3) << similarity_scores[i] << " num: " << numbers_in_labels[i] << endl;
373                 previous_label = label;                                
374         }
375
376         
377         // cerr << "most similar: " << most_similar << ", least similar: " << least_similar << endl;
378         float similarity_threshold;
379         
380         if (most_similar > 1.0) {
381                 similarity_threshold = default_similarity_threshold;
382         } else {
383                 similarity_threshold = most_similar - (1 - default_similarity_threshold);
384         }
385         
386         // Now iterate over the list of controls to display them, placing an
387         // HSeparator between controls of less than a certain similarity, and 
388         // starting a new column when necessary.
389         
390         i = 0;
391         for (vector<ControlUI*>::iterator cuip = cui_controls_list.begin(); cuip != cui_controls_list.end(); ++cuip, ++i) {
392
393                 ControlUI* cui = *cuip;
394                 
395                 if (!is_scrollable) {
396                         x++;
397                 }
398                 
399                 if (x > max_controls_per_column || similarity_scores[i] <= similarity_threshold) {
400                         if (x > min_controls_per_column) {
401                                 frame = manage (new Frame);
402                                 frame->set_name ("BaseFrame");
403                                 frame->set_label (_("Controls"));
404                                 box = manage (new VBox);
405                                 box->set_border_width (5);
406                                 box->set_spacing (1);
407                                 frame->add (*box);
408                                 hpacker.pack_start(*frame, true, true);
409                                 x = 0;
410                         } else {
411                                 HSeparator *split = new HSeparator();
412                                 split->set_size_request(-1, 5);
413                                 box->pack_start(*split, false, false, 0);
414                         }
415
416                 }
417                 box->pack_start (*cui, false, false);
418         }
419
420         if (is_scrollable) {
421                 prefheight = 30 * i;
422         }
423
424         if (box->children().empty()) {
425                 hpacker.remove (*frame);
426         }
427
428         if (button_table.children().empty()) {
429                 hpacker.remove (*bt_frame);
430         }
431
432         if (!output_table.children().empty()) {
433                 frame = manage (new Frame);
434                 frame->set_name ("BaseFrame");
435                 frame->set_label(_("Meters"));
436                 frame->add (output_table);
437                 hpacker.pack_end (*frame, true, true);
438         }
439
440         output_update ();
441
442         output_table.show_all ();
443         button_table.show_all ();
444 }
445
446 GenericPluginUI::ControlUI::ControlUI ()
447         : automate_button (X_("")) // force creation of a label
448 {
449         automate_button.set_name ("PluginAutomateButton");
450         ARDOUR_UI::instance()->set_tip (automate_button, _("Automation control"));
451
452         /* XXX translators: use a string here that will be at least as long
453            as the longest automation label (see ::automation_state_changed()
454            below). be sure to include a descender.
455         */
456
457         set_size_request_to_display_given_text (automate_button, _("Mgnual"), 15, 10);
458
459         ignore_change = 0;
460         display = 0;
461         button = 0;
462         clickbox = 0;
463         meterinfo = 0;
464 }
465
466 GenericPluginUI::ControlUI::~ControlUI()
467 {
468         if (meterinfo) {
469                 delete meterinfo->meter;
470                 delete meterinfo;
471         }
472 }
473
474 void
475 GenericPluginUI::automation_state_changed (ControlUI* cui)
476 {
477         /* update button label */
478
479         // don't lock to avoid deadlock because we're triggered by
480         // AutomationControl::Changed() while the automation lock is taken
481         switch (insert->get_parameter_automation_state (cui->parameter()) & (ARDOUR::Off|Play|Touch|Write)) {
482         case ARDOUR::Off:
483                 cui->automate_button.set_label (S_("Automation|Manual"));
484                 break;
485         case Play:
486                 cui->automate_button.set_label (_("Play"));
487                 break;
488         case Write:
489                 cui->automate_button.set_label (_("Write"));
490                 break;
491         case Touch:
492                 cui->automate_button.set_label (_("Touch"));
493                 break;
494         default:
495                 cui->automate_button.set_label (_("???"));
496                 break;
497         }
498 }
499
500
501 bool
502 GenericPluginUI::integer_printer (char buf[32], Adjustment &adj, ControlUI* cui)
503 {
504         float const v = adj.get_value ();
505         
506         if (cui->scale_points) {
507                 Plugin::ScalePoints::const_iterator i = cui->scale_points->begin ();
508                 while (i != cui->scale_points->end() && i->second != v) {
509                         ++i;
510                 }
511
512                 if (i != cui->scale_points->end ()) {
513                         snprintf (buf, 32, "%s", i->first.c_str());
514                         return true;
515                 }
516         }
517                 
518         snprintf (buf, 32, "%.0f", v);
519         return true;
520 }
521
522 bool
523 GenericPluginUI::midinote_printer (char buf[32], Adjustment &adj, ControlUI* cui)
524 {
525         float const v = adj.get_value ();
526
527         if (cui->scale_points) {
528                 Plugin::ScalePoints::const_iterator i = cui->scale_points->begin ();
529                 while (i != cui->scale_points->end() && i->second != v) {
530                         ++i;
531                 }
532
533                 if (i != cui->scale_points->end ()) {
534                         snprintf (buf, 32, "%s", i->first.c_str());
535                         return true;
536                 }
537         }
538         if (v >= 0 && v <= 127) {
539                 int mn = rint(v);
540                 const char notename[12][3] = { "C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B" };
541                 snprintf (buf, 32, "%s %d", notename[mn%12], (mn/12)-2);
542         } else {
543                 snprintf (buf, 32, "%.0f", v);
544         }
545         return true;
546 }
547
548 void
549 GenericPluginUI::print_parameter (char *buf, uint32_t len, uint32_t param)
550 {
551         plugin->print_parameter (param, buf, len);
552 }
553
554 GenericPluginUI::ControlUI*
555 GenericPluginUI::build_control_ui (guint32 port_index, boost::shared_ptr<AutomationControl> mcontrol)
556 {
557         ControlUI* control_ui = 0;
558
559         Plugin::ParameterDescriptor desc;
560
561         plugin->get_parameter_descriptor (port_index, desc);
562
563         control_ui = manage (new ControlUI ());
564         control_ui->combo = 0;
565         control_ui->control = mcontrol;
566         control_ui->update_pending = false;
567         control_ui->label.set_text (desc.label);
568         control_ui->label.set_alignment (0.0, 0.5);
569         control_ui->label.set_name ("PluginParameterLabel");
570         control_ui->port_index = port_index;
571
572         control_ui->set_spacing (5);
573
574         Gtk::Requisition req (control_ui->automate_button.size_request());
575
576         if (plugin->parameter_is_input(port_index)) {
577
578                 /* See if there any named values for our input value */
579                 control_ui->scale_points = plugin->get_scale_points (port_index);
580
581                 /* If this parameter is an integer, work out the number of distinct values
582                    it can take on (assuming that lower and upper values are allowed).
583                 */
584                 int const steps = desc.integer_step ? (desc.upper - desc.lower + 1) / desc.step : 0;
585
586                 if (control_ui->scale_points && ((steps && int (control_ui->scale_points->size()) == steps) || desc.enumeration)) {
587                         
588                         /* Either:
589                          *   a) There is a label for each possible value of this input, or
590                          *   b) This port is marked as being an enumeration.
591                          */
592
593                         std::vector<std::string> labels;
594                         for (
595                                 ARDOUR::Plugin::ScalePoints::const_iterator i = control_ui->scale_points->begin();
596                                 i != control_ui->scale_points->end();
597                                 ++i) {
598                                 
599                                 labels.push_back(i->first);
600                         }
601
602                         control_ui->combo = new Gtk::ComboBoxText();
603                         set_popdown_strings(*control_ui->combo, labels);
604                         control_ui->combo->signal_changed().connect(
605                                 sigc::bind (sigc::mem_fun(*this, &GenericPluginUI::control_combo_changed),
606                                             control_ui));
607                         mcontrol->Changed.connect(control_connections, invalidator(*this),
608                                                   boost::bind(&GenericPluginUI::ui_parameter_changed,
609                                                               this, control_ui),
610                                                   gui_context());
611                         control_ui->pack_start(control_ui->label, true, true);
612                         control_ui->pack_start(*control_ui->combo, false, true);
613
614                         update_control_display(control_ui);
615
616                         return control_ui;
617                 }
618
619                 if (desc.toggled) {
620
621                         /* Build a button */
622
623                         control_ui->button = manage (new ToggleButton ());
624                         control_ui->button->set_name ("PluginEditorButton");
625                         control_ui->button->set_size_request (20, 20);
626
627                         control_ui->pack_start (control_ui->label, true, true);
628                         control_ui->pack_start (*control_ui->button, false, true);
629                         control_ui->pack_start (control_ui->automate_button, false, false);
630
631                         control_ui->button->signal_clicked().connect (sigc::bind (sigc::mem_fun(*this, &GenericPluginUI::control_port_toggled), control_ui));
632                         control_ui->automate_button.signal_clicked().connect (bind (mem_fun(*this, &GenericPluginUI::astate_clicked), control_ui, (uint32_t) port_index));
633
634                         mcontrol->Changed.connect (control_connections, invalidator (*this), boost::bind (&GenericPluginUI::toggle_parameter_changed, this, control_ui), gui_context());
635                         mcontrol->alist()->automation_state_changed.connect (control_connections, invalidator (*this), boost::bind (&GenericPluginUI::automation_state_changed, this, control_ui), gui_context());
636
637                         if (plugin->get_parameter (port_index) > 0.5){
638                                 control_ui->button->set_active(true);
639                         }
640
641                         automation_state_changed (control_ui);
642
643                         return control_ui;
644                 }
645
646                 /* create the controller */
647
648                 if (mcontrol) {
649                         control_ui->controller = AutomationController::create(insert, mcontrol->parameter(), mcontrol);
650                 }
651
652                 /* XXX this code is not right yet, because it doesn't handle
653                    the absence of bounds in any sensible fashion.
654                 */
655
656                 Adjustment* adj = control_ui->controller->adjustment();
657                 boost::shared_ptr<PluginInsert::PluginControl> pc = boost::dynamic_pointer_cast<PluginInsert::PluginControl> (control_ui->control);
658
659                 adj->set_lower (desc.lower);
660                 adj->set_upper (desc.upper);
661
662                 adj->set_step_increment (desc.step);
663                 adj->set_page_increment (desc.largestep);
664
665                 if (desc.integer_step) {
666                         control_ui->clickbox = new ClickBox (adj, "PluginUIClickBox");
667                         Gtkmm2ext::set_size_request_to_display_given_text (*control_ui->clickbox, "g9999999", 2, 2);
668                         if (desc.midinote) {
669                                 control_ui->clickbox->set_printer (sigc::bind (sigc::mem_fun (*this, &GenericPluginUI::midinote_printer), control_ui));
670                         } else {
671                                 control_ui->clickbox->set_printer (sigc::bind (sigc::mem_fun (*this, &GenericPluginUI::integer_printer), control_ui));
672                         }
673                 } else {
674                         //sigc::slot<void,char*,uint32_t> pslot = sigc::bind (sigc::mem_fun(*this, &GenericPluginUI::print_parameter), (uint32_t) port_index);
675
676                         control_ui->controller->set_size_request (200, req.height);
677                         control_ui->controller->set_name (X_("ProcessorControlSlider"));
678
679                         control_ui->controller->StartGesture.connect (sigc::bind (sigc::mem_fun(*this, &GenericPluginUI::start_touch), control_ui));
680                         control_ui->controller->StopGesture.connect (sigc::bind (sigc::mem_fun(*this, &GenericPluginUI::stop_touch), control_ui));
681
682                 }
683
684                 adj->set_value (plugin->get_parameter (port_index));
685
686                 /* XXX memory leak: SliderController not destroyed by ControlUI
687                    destructor, and manage() reports object hierarchy
688                    ambiguity.
689                 */
690
691                 control_ui->pack_start (control_ui->label, true, true);
692                 if (desc.integer_step) {
693                         control_ui->pack_start (*control_ui->clickbox, false, false);
694                 } else {
695                         control_ui->pack_start (*control_ui->controller, false, false);
696                 }
697
698                 control_ui->pack_start (control_ui->automate_button, false, false);
699                 control_ui->automate_button.signal_clicked().connect (sigc::bind (sigc::mem_fun(*this, &GenericPluginUI::astate_clicked), control_ui, (uint32_t) port_index));
700
701                 automation_state_changed (control_ui);
702
703                 mcontrol->Changed.connect (control_connections, invalidator (*this), boost::bind (&GenericPluginUI::ui_parameter_changed, this, control_ui), gui_context());
704                 mcontrol->alist()->automation_state_changed.connect (control_connections, invalidator (*this), boost::bind (&GenericPluginUI::automation_state_changed, this, control_ui), gui_context());
705
706                 input_controls.push_back (control_ui);
707
708         } else if (plugin->parameter_is_output (port_index)) {
709
710                 control_ui->display = manage (new EventBox);
711                 control_ui->display->set_name ("ParameterValueDisplay");
712
713                 control_ui->display_label = manage (new Label);
714
715                 control_ui->display_label->set_name ("ParameterValueDisplay");
716
717                 control_ui->display->add (*control_ui->display_label);
718                 Gtkmm2ext::set_size_request_to_display_given_text (*control_ui->display, "-888.8g", 2, 6);
719
720                 control_ui->display->show_all ();
721
722                 /* set up a meter */
723                 /* TODO: only make a meter if the port is Hinted for it */
724
725                 MeterInfo * info = new MeterInfo(port_index);
726                 control_ui->meterinfo = info;
727
728                 info->meter = new FastMeter (
729                                 5, 5, FastMeter::Vertical, 0,
730                                 0x0000aaff,
731                                 0x008800ff, 0x008800ff,
732                                 0x00ff00ff, 0x00ff00ff,
733                                 0xcccc00ff, 0xcccc00ff,
734                                 0xffaa00ff, 0xffaa00ff,
735                                 0xff0000ff,
736                                 ARDOUR_UI::config()->get_MeterBackgroundBot(),
737                                 ARDOUR_UI::config()->get_MeterBackgroundTop()
738                                 );
739
740                 info->min_unbound = desc.min_unbound;
741                 info->max_unbound = desc.max_unbound;
742
743                 info->min = desc.lower;
744                 info->max = desc.upper;
745
746                 control_ui->vbox = manage (new VBox);
747                 control_ui->hbox = manage (new HBox);
748
749                 control_ui->hbox->set_spacing(1);
750                 control_ui->vbox->set_spacing(3);
751
752                 control_ui->label.set_angle(90);
753                 control_ui->hbox->pack_start (control_ui->label, false, false);
754                 control_ui->hbox->pack_start (*info->meter, false, false);
755
756                 control_ui->vbox->pack_start (*control_ui->hbox, false, false);
757
758                 control_ui->vbox->pack_start (*control_ui->display, false, false);
759
760                 control_ui->pack_start (*control_ui->vbox);
761
762                 control_ui->meterinfo->meter->show_all();
763                 control_ui->meterinfo->packed = true;
764
765                 output_controls.push_back (control_ui);
766         }
767
768         if (mcontrol) {
769                 mcontrol->Changed.connect (control_connections, invalidator (*this), boost::bind (&GenericPluginUI::ui_parameter_changed, this, control_ui), gui_context());
770         }
771
772         return control_ui;
773 }
774
775 void
776 GenericPluginUI::start_touch (GenericPluginUI::ControlUI* cui)
777 {
778         cui->control->start_touch (cui->control->session().transport_frame());
779 }
780
781 void
782 GenericPluginUI::stop_touch (GenericPluginUI::ControlUI* cui)
783 {
784         cui->control->stop_touch (false, cui->control->session().transport_frame());
785 }
786
787 void
788 GenericPluginUI::astate_clicked (ControlUI* cui, uint32_t /*port*/)
789 {
790         using namespace Menu_Helpers;
791
792         if (automation_menu == 0) {
793                 automation_menu = manage (new Menu);
794                 automation_menu->set_name ("ArdourContextMenu");
795         }
796
797         MenuList& items (automation_menu->items());
798
799         items.clear ();
800         items.push_back (MenuElem (S_("Automation|Manual"),
801                                    sigc::bind (sigc::mem_fun(*this, &GenericPluginUI::set_automation_state), (AutoState) ARDOUR::Off, cui)));
802         items.push_back (MenuElem (_("Play"),
803                                    sigc::bind (sigc::mem_fun(*this, &GenericPluginUI::set_automation_state), (AutoState) Play, cui)));
804         items.push_back (MenuElem (_("Write"),
805                                    sigc::bind (sigc::mem_fun(*this, &GenericPluginUI::set_automation_state), (AutoState) Write, cui)));
806         items.push_back (MenuElem (_("Touch"),
807                                    sigc::bind (sigc::mem_fun(*this, &GenericPluginUI::set_automation_state), (AutoState) Touch, cui)));
808
809         automation_menu->popup (1, gtk_get_current_event_time());
810 }
811
812 void
813 GenericPluginUI::set_automation_state (AutoState state, ControlUI* cui)
814 {
815         insert->set_parameter_automation_state (cui->parameter(), state);
816 }
817
818 void
819 GenericPluginUI::toggle_parameter_changed (ControlUI* cui)
820 {
821         float val = cui->control->get_value();
822
823         if (!cui->ignore_change) {
824                 if (val > 0.5) {
825                         cui->button->set_active (true);
826                         cui->button->set_name ("PluginEditorButton-active");
827                 } else {
828                         cui->button->set_active (false);
829                         cui->button->set_name ("PluginEditorButton");
830                 }
831         }
832 }
833
834 void
835 GenericPluginUI::ui_parameter_changed (ControlUI* cui)
836 {
837         if (!cui->update_pending) {
838                 cui->update_pending = true;
839                 Gtkmm2ext::UI::instance()->call_slot (MISSING_INVALIDATOR, boost::bind (&GenericPluginUI::update_control_display, this, cui));
840         }
841 }
842
843 void
844 GenericPluginUI::update_control_display (ControlUI* cui)
845 {
846         /* XXX how do we handle logarithmic stuff here ? */
847
848         cui->update_pending = false;
849
850         float val = cui->control->get_value();
851
852         cui->ignore_change++;
853
854         if (cui->combo && cui->scale_points) {
855                 for (ARDOUR::Plugin::ScalePoints::iterator it = cui->scale_points->begin(); it != cui->scale_points->end(); ++it) {
856                         if (it->second == val) {
857                                 cui->combo->set_active_text(it->first);
858                                 break;
859                         }
860                 }
861         } else if (cui->button) {
862
863                 if (val > 0.5) {
864                         cui->button->set_active (true);
865                 } else {
866                         cui->button->set_active (false);
867                 }
868         }
869
870         if( cui->controller ) {
871             cui->controller->display_effective_value();
872         }
873
874
875         /*} else {
876                 if (cui->logarithmic) {
877                         val = log(val);
878                 }
879                 if (val != cui->adjustment->get_value()) {
880                         cui->adjustment->set_value (val);
881                 }
882         }*/
883         cui->ignore_change--;
884 }
885
886 void
887 GenericPluginUI::control_port_toggled (ControlUI* cui)
888 {
889         cui->ignore_change++;
890         const bool active = cui->button->get_active();
891         if (active) {
892                 cui->button->set_name ("PluginEditorButton-active");
893         } else {
894                 cui->button->set_name ("PluginEditorButton");
895         }
896         insert->automation_control (cui->parameter())->set_value (active);
897         cui->ignore_change--;
898 }
899
900 void
901 GenericPluginUI::control_combo_changed (ControlUI* cui)
902 {
903         if (!cui->ignore_change && cui->scale_points) {
904                 string value = cui->combo->get_active_text();
905                 insert->automation_control (cui->parameter())->set_value ((*cui->scale_points)[value]);
906         }
907 }
908
909 bool
910 GenericPluginUI::start_updating (GdkEventAny*)
911 {
912         if (output_controls.size() > 0 ) {
913                 screen_update_connection.disconnect();
914                 screen_update_connection = ARDOUR_UI::instance()->SuperRapidScreenUpdate.connect
915                         (sigc::mem_fun(*this, &GenericPluginUI::output_update));
916         }
917         return false;
918 }
919
920 bool
921 GenericPluginUI::stop_updating (GdkEventAny*)
922 {
923         for (vector<ControlUI*>::iterator i = input_controls.begin(); i != input_controls.end(); ++i) {
924                 (*i)->controller->stop_updating ();
925         }
926
927         if (output_controls.size() > 0 ) {
928                 screen_update_connection.disconnect();
929         }
930
931         return false;
932 }
933
934 void
935 GenericPluginUI::output_update ()
936 {
937         for (vector<ControlUI*>::iterator i = output_controls.begin(); i != output_controls.end(); ++i) {
938                 float val = plugin->get_parameter ((*i)->port_index);
939                 char buf[32];
940                 snprintf (buf, sizeof(buf), "%.2f", val);
941                 (*i)->display_label->set_text (buf);
942
943                 /* autoscaling for the meter */
944                 if ((*i)->meterinfo && (*i)->meterinfo->packed) {
945
946                         if (val < (*i)->meterinfo->min) {
947                                 if ((*i)->meterinfo->min_unbound)
948                                         (*i)->meterinfo->min = val;
949                                 else
950                                         val = (*i)->meterinfo->min;
951                         }
952
953                         if (val > (*i)->meterinfo->max) {
954                                 if ((*i)->meterinfo->max_unbound)
955                                         (*i)->meterinfo->max = val;
956                                 else
957                                         val = (*i)->meterinfo->max;
958                         }
959
960                         if ((*i)->meterinfo->max > (*i)->meterinfo->min ) {
961                                 float lval = (val - (*i)->meterinfo->min) / ((*i)->meterinfo->max - (*i)->meterinfo->min) ;
962                                 (*i)->meterinfo->meter->set (lval );
963                         }
964                 }
965         }
966 }
967
968 #ifdef LV2_SUPPORT
969
970 void
971 GenericPluginUI::patch_set_file (uint32_t p)
972 {
973         boost::shared_ptr<ARDOUR::LV2Plugin> lv2p = boost::dynamic_pointer_cast<LV2Plugin> (plugin);
974         lv2p->patch_set(p, _fcb[p]->get_filename ().c_str());
975 }
976
977 void
978 GenericPluginUI::patch_changed (uint32_t p)
979 {
980         boost::shared_ptr<ARDOUR::LV2Plugin> lv2p = boost::dynamic_pointer_cast<LV2Plugin> (plugin);
981         _fcb[p]->set_filename(lv2p->patch_val(p));
982 }
983
984 #endif