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