more hacking on the processor list and processor box - note that ctrl-x/c/v now work...
[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
29 #include "pbd/stl_delete.h"
30 #include "pbd/xml++.h"
31 #include "pbd/failed_constructor.h"
32
33 #include <gtkmm2ext/click_box.h>
34 #include <gtkmm2ext/fastmeter.h>
35 #include <gtkmm2ext/barcontroller.h>
36 #include <gtkmm2ext/utils.h>
37 #include <gtkmm2ext/doi.h>
38 #include <gtkmm2ext/slider_controller.h>
39
40 #include "midi++/manager.h"
41
42 #include "ardour/plugin.h"
43 #include "ardour/plugin_insert.h"
44 #include "ardour/ladspa_plugin.h"
45 #ifdef HAVE_SLV2
46 #include "ardour/lv2_plugin.h"
47 #endif
48
49 #include <lrdf.h>
50
51 #include "ardour_ui.h"
52 #include "prompter.h"
53 #include "plugin_ui.h"
54 #include "utils.h"
55 #include "gui_thread.h"
56 #include "automation_controller.h"
57
58 #include "i18n.h"
59
60 using namespace std;
61 using namespace ARDOUR;
62 using namespace PBD;
63 using namespace Gtkmm2ext;
64 using namespace Gtk;
65 using namespace sigc;
66
67 GenericPluginUI::GenericPluginUI (boost::shared_ptr<PluginInsert> pi, bool scrollable)
68         : PlugUIBase (pi),
69           button_table (initial_button_rows, initial_button_cols),
70           output_table (initial_output_rows, initial_output_cols),
71           hAdjustment(0.0, 0.0, 0.0),
72           vAdjustment(0.0, 0.0, 0.0),
73           scroller_view(hAdjustment, vAdjustment),
74           automation_menu (0),
75           is_scrollable(scrollable)
76 {
77         set_name ("PluginEditor");
78         set_border_width (10);
79         //set_homogeneous (false);
80
81         pack_start(main_contents);
82         settings_box.set_homogeneous (false);
83
84         HBox* constraint_hbox = manage (new HBox);
85         HBox* smaller_hbox = manage (new HBox);
86         Label* combo_label = manage (new Label (_("<span size=\"large\">Presets</span>")));
87         combo_label->set_use_markup (true);
88
89         latency_button.add (latency_label);
90         latency_button.signal_clicked().connect (mem_fun (*this, &PlugUIBase::latency_button_clicked));
91         set_latency_label ();
92         
93         smaller_hbox->pack_start (latency_button, false, false, 10);
94         smaller_hbox->pack_start (preset_combo, false, false);
95         smaller_hbox->pack_start (save_button, false, false);
96         smaller_hbox->pack_start (bypass_button, false, true);
97
98         constraint_hbox->set_spacing (5);
99         constraint_hbox->set_homogeneous (false);
100         
101         VBox* v1_box = manage (new VBox);
102         VBox* v2_box = manage (new VBox);
103         pack_end(plugin_analysis_expander);
104
105         v1_box->pack_start (*smaller_hbox, false, true);
106         v2_box->pack_start (focus_button, false, true);
107
108         main_contents.pack_start (settings_box, false, false);
109
110         constraint_hbox->pack_end (*v2_box, false, false);
111         constraint_hbox->pack_end (*v1_box, false, false);
112
113         main_contents.pack_start (*constraint_hbox, false, false);
114         
115         if ( is_scrollable ) {
116                 scroller.set_policy (Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
117                 scroller.set_name ("PluginEditor");
118                 scroller_view.set_name("PluginEditor");
119                 scroller_view.add (hpacker);
120                 scroller.add (scroller_view);
121                 
122                 main_contents.pack_start (scroller, true, true);
123
124         }
125         else {
126                 main_contents.pack_start (hpacker, false, false);
127         }
128
129         pi->ActiveChanged.connect (bind(mem_fun(*this, &GenericPluginUI::processor_active_changed),
130                                         boost::weak_ptr<Processor>(pi)));
131         
132         bypass_button.set_active (!pi->active());
133
134         build ();
135 }
136
137 GenericPluginUI::~GenericPluginUI ()
138 {
139         if (output_controls.size() > 0) {
140                 screen_update_connection.disconnect();
141         }
142 }
143
144 void
145 GenericPluginUI::build ()
146
147 {
148         guint32 i = 0;
149         guint32 x = 0;
150         Frame* frame;
151         Frame* bt_frame;
152         VBox* box;
153         int output_row, output_col;
154         int button_row, button_col;
155         int output_rows, output_cols;
156         int button_rows, button_cols;
157
158         prefheight = 30;
159         hpacker.set_spacing (10);
160
161         output_rows = initial_output_rows;
162         output_cols = initial_output_cols;
163         button_rows = initial_button_rows;
164         button_cols = initial_button_cols;
165         output_row = 0;
166         button_row = 0;
167         output_col = 0;
168         button_col = 0;
169
170         button_table.set_homogeneous (false);
171         button_table.set_row_spacings (2);
172         button_table.set_col_spacings (2);
173         output_table.set_homogeneous (true);
174         output_table.set_row_spacings (2);
175         output_table.set_col_spacings (2);
176         button_table.set_border_width (5);
177         output_table.set_border_width (5);
178
179         hpacker.set_border_width (10);
180
181         bt_frame = manage (new Frame);
182         bt_frame->set_name ("BaseFrame");
183         bt_frame->add (button_table);
184         hpacker.pack_start(*bt_frame, true, true);
185
186         box = manage (new VBox);
187         box->set_border_width (5);
188         box->set_spacing (1);
189
190         frame = manage (new Frame);
191         frame->set_name ("BaseFrame");
192         frame->set_label (_("Controls"));
193         frame->add (*box);
194         hpacker.pack_start(*frame, true, true);
195
196         /* find all ports. build control elements for all appropriate control ports */
197
198         for (i = 0; i < plugin->parameter_count(); ++i) {
199
200                 if (plugin->parameter_is_control (i)) {
201                         
202                         /* Don't show latency control ports */
203
204                         if (plugin->describe_parameter (Evoral::Parameter(PluginAutomation, 0, i)) == X_("latency")) {
205                                 continue;
206                         }
207
208                         ControlUI* cui;
209         
210                         /* if we are scrollable, just use one long column */
211
212                         if (!is_scrollable) {
213                                 if (x++ > 20){
214                                         frame = manage (new Frame);
215                                         frame->set_name ("BaseFrame");
216                                         box = manage (new VBox);
217                                         
218                                         box->set_border_width (5);
219                                         box->set_spacing (1);
220
221                                         frame->add (*box);
222                                         hpacker.pack_start(*frame,true,true);
223
224                                         x = 1;
225                                 }
226                         }
227
228                         boost::shared_ptr<ARDOUR::AutomationControl> c
229                                 = boost::dynamic_pointer_cast<ARDOUR::AutomationControl>(
230                                         insert->data().control(Evoral::Parameter(PluginAutomation, 0, i)));
231
232                         if ((cui = build_control_ui (i, c)) == 0) {
233                                 error << string_compose(_("Plugin Editor: could not build control element for port %1"), i) << endmsg;
234                                 continue;
235                         }
236                                 
237                         if (cui->controller || cui->clickbox || cui->combo) {
238
239                                 box->pack_start (*cui, false, false);
240
241                         } else if (cui->button) {
242
243                                 if (button_row == button_rows) {
244                                         button_row = 0;
245                                         if (++button_col == button_cols) {
246                                                 button_cols += 2;
247                                                 button_table.resize (button_rows, button_cols);
248                                         }
249                                 }
250
251                                 button_table.attach (*cui, button_col, button_col + 1, button_row, button_row+1, 
252                                                      FILL|EXPAND, FILL);
253                                 button_row++;
254
255                         } else if (cui->display) {
256
257                                 output_table.attach (*cui, output_col, output_col + 1, output_row, output_row+1, 
258                                                      FILL|EXPAND, FILL);
259                                 
260                                 // TODO: The meters should be divided into multiple rows 
261                                 
262                                 if (++output_col == output_cols) {
263                                         output_cols ++;
264                                         output_table.resize (output_rows, output_cols);
265                                 }
266                                 
267                                 /* old code, which divides meters into
268                                  * columns first, rows later. New code divides into one row
269                                  
270                                 if (output_row == output_rows) {
271                                         output_row = 0;
272                                         if (++output_col == output_cols) {
273                                                 output_cols += 2;
274                                                 output_table.resize (output_rows, output_cols);
275                                         }
276                                 }
277                                 
278                                 output_table.attach (*cui, output_col, output_col + 1, output_row, output_row+1, 
279                                                      FILL|EXPAND, FILL);
280  
281                                 output_row++;
282                                 */
283                         }
284                                 
285                         /* HACK: ideally the preferred height would be queried from
286                            the complete hpacker, but I can't seem to get that
287                            information in time, so this is an estimation 
288                         */
289
290                         prefheight += 30;
291
292                 } 
293         }
294
295         if (box->children().empty()) {
296                 hpacker.remove (*frame);
297         }
298
299         if (button_table.children().empty()) {
300                 hpacker.remove (*bt_frame);
301         }
302
303         if (!output_table.children().empty()) {
304                 frame = manage (new Frame);
305                 frame->set_name ("BaseFrame");
306                 frame->add (output_table);
307                 hpacker.pack_end (*frame, true, true);
308         }
309
310         output_update ();
311
312         output_table.show_all ();
313         button_table.show_all ();
314 }
315
316 GenericPluginUI::ControlUI::ControlUI ()
317         : automate_button (X_("")) // force creation of a label 
318 {
319         automate_button.set_name ("PluginAutomateButton");
320         ARDOUR_UI::instance()->tooltips().set_tip (automate_button, _("Automation control"));
321
322         /* XXX translators: use a string here that will be at least as long
323            as the longest automation label (see ::automation_state_changed()
324            below). be sure to include a descender.
325         */
326
327         set_size_request_to_display_given_text (*automate_button.get_child(), _("Mgnual"), 5, 5);
328
329         ignore_change = 0;
330         display = 0;
331         button = 0;
332         clickbox = 0;
333         meterinfo = 0;
334 }
335
336 GenericPluginUI::ControlUI::~ControlUI() 
337 {
338         if (meterinfo) {
339                 delete meterinfo->meter;
340                 delete meterinfo;
341         }
342 }
343
344 void
345 GenericPluginUI::automation_state_changed (ControlUI* cui)
346 {
347         /* update button label */
348
349         // don't lock to avoid deadlock because we're triggered by
350         // AutomationControl::Changed() while the automation lock is taken
351         switch (insert->get_parameter_automation_state (cui->parameter(), false)
352                         & (Off|Play|Touch|Write)) {
353         case Off:
354                 cui->automate_button.set_label (_("Manual"));
355                 break;
356         case Play:
357                 cui->automate_button.set_label (_("Play"));
358                 break;
359         case Write:
360                 cui->automate_button.set_label (_("Write"));
361                 break;
362         case Touch:
363                 cui->automate_button.set_label (_("Touch"));
364                 break;
365         default:
366                 cui->automate_button.set_label (_("???"));
367                 break;
368         }
369 }
370
371
372 static void integer_printer (char buf[32], Adjustment &adj, void *arg)
373 {
374         snprintf (buf, 32, "%.0f", adj.get_value());
375 }
376
377 void
378 GenericPluginUI::print_parameter (char *buf, uint32_t len, uint32_t param)
379 {
380         plugin->print_parameter (param, buf, len);
381 }
382
383 GenericPluginUI::ControlUI*
384 GenericPluginUI::build_control_ui (guint32 port_index, boost::shared_ptr<AutomationControl> mcontrol)
385 {
386         ControlUI* control_ui = NULL;
387         if (!mcontrol)
388                 return control_ui;
389
390         Plugin::ParameterDescriptor desc;
391
392         plugin->get_parameter_descriptor (port_index, desc);
393
394         control_ui = manage (new ControlUI ());
395         control_ui->combo = 0;
396         control_ui->combo_map = 0;
397         control_ui->control = mcontrol;
398         control_ui->update_pending = false;
399         control_ui->label.set_text (desc.label);
400         control_ui->label.set_alignment (0.0, 0.5);
401         control_ui->label.set_name ("PluginParameterLabel");
402
403         control_ui->set_spacing (5);
404
405         Gtk::Requisition req (control_ui->automate_button.size_request());
406
407         if (plugin->parameter_is_input (port_index)) {
408
409                 boost::shared_ptr<LadspaPlugin> lp;
410 #ifdef HAVE_SLV2
411                 boost::shared_ptr<LV2Plugin> lv2p;
412 #endif
413                 if ((lp = boost::dynamic_pointer_cast<LadspaPlugin>(plugin)) != 0) {
414                         
415                         // FIXME: not all plugins have a numeric unique ID
416                         uint32_t id = atol (lp->unique_id().c_str());
417                         lrdf_defaults* defaults = lrdf_get_scale_values(id, port_index);
418                         
419                         if (defaults && defaults->count > 0)    {
420                                 
421                                 control_ui->combo = new Gtk::ComboBoxText;
422                                 //control_ui->combo->set_value_in_list(true, false);
423                                 set_popdown_strings (*control_ui->combo, setup_scale_values(port_index, control_ui));
424                                 control_ui->combo->signal_changed().connect (bind (mem_fun(*this, &GenericPluginUI::control_combo_changed), control_ui));
425                                 mcontrol->Changed.connect (bind (mem_fun (*this, &GenericPluginUI::parameter_changed), control_ui));
426                                 control_ui->pack_start(control_ui->label, true, true);
427                                 control_ui->pack_start(*control_ui->combo, false, true);
428                                 
429                                 update_control_display(control_ui);
430                                 
431                                 lrdf_free_setting_values(defaults);
432                                 return control_ui;
433                         }
434
435 #ifdef HAVE_SLV2
436                 } else if ((lv2p = boost::dynamic_pointer_cast<LV2Plugin>(plugin)) != 0) {
437
438                         SLV2Port port = lv2p->slv2_port(port_index);
439                         SLV2ScalePoints points = slv2_port_get_scale_points(lv2p->slv2_plugin(), port);
440
441                         if (points) {
442                                 control_ui->combo = new Gtk::ComboBoxText;
443                                 //control_ui->combo->set_value_in_list(true, false);
444                                 set_popdown_strings (*control_ui->combo, setup_scale_values(port_index, control_ui));
445                                 control_ui->combo->signal_changed().connect (bind (mem_fun(*this, &GenericPluginUI::control_combo_changed), control_ui));
446                                 mcontrol->Changed.connect (bind (mem_fun (*this, &GenericPluginUI::parameter_changed), control_ui));
447                                 control_ui->pack_start(control_ui->label, true, true);
448                                 control_ui->pack_start(*control_ui->combo, false, true);
449                                 
450                                 update_control_display(control_ui);
451                                 
452                                 slv2_scale_points_free(points);
453                                 return control_ui;
454                         }
455 #endif
456                 }
457                         
458                 if (desc.toggled) {
459
460                         /* Build a button */
461                 
462                         control_ui->button = manage (new ToggleButton ());
463                         control_ui->button->set_name ("PluginEditorButton");
464                         control_ui->button->set_size_request (20, 20);
465
466                         control_ui->pack_start (control_ui->label, true, true);
467                         control_ui->pack_start (*control_ui->button, false, true);
468                         // control_ui->pack_start (control_ui->automate_button, false, false);
469
470                         control_ui->button->signal_clicked().connect (bind (mem_fun(*this, &GenericPluginUI::control_port_toggled), control_ui));
471                         mcontrol->Changed.connect (bind (mem_fun (*this, &GenericPluginUI::toggle_parameter_changed), control_ui));
472                         
473                         if (plugin->get_parameter (port_index) > 0.5){
474                                 control_ui->button->set_active(true);
475                         }
476                         
477                         return control_ui;
478                 }
479
480                 /* create the controller */
481
482                 control_ui->controller = AutomationController::create(insert, mcontrol->parameter(), mcontrol);
483                 /* XXX this code is not right yet, because it doesn't handle
484                    the absence of bounds in any sensible fashion.
485                 */
486
487 //#if 0
488                 control_ui->controller->adjustment()->set_lower (desc.lower);
489                 control_ui->controller->adjustment()->set_upper (desc.upper);
490
491                 control_ui->logarithmic = false; // just disable it for now
492                 /*
493                 control_ui->logarithmic = desc.logarithmic;
494                 if (control_ui->logarithmic) {
495                         if (control_ui->controller->adjustment()->get_lower() == 0.0) {
496                                 control_ui->controller->adjustment()->set_lower (control_ui->controller->adjustment()->get_upper()/10000);
497                         }
498                         control_ui->controller->adjustment()->set_upper (log(control_ui->controller->adjustment()->get_upper()));
499                         control_ui->controller->adjustment()->set_lower (log(control_ui->controller->adjustment()->get_lower()));
500                 }*/
501                 
502         
503                 control_ui->controller->adjustment()->set_step_increment (desc.step);
504                 control_ui->controller->adjustment()->set_page_increment (desc.largestep);
505 //#endif
506
507                 if (desc.integer_step) {
508                         control_ui->clickbox = new ClickBox (control_ui->controller->adjustment(), "PluginUIClickBox");
509                         Gtkmm2ext::set_size_request_to_display_given_text (*control_ui->clickbox, "g9999999", 2, 2);
510                         control_ui->clickbox->set_print_func (integer_printer, 0);
511                 } else {
512                         //sigc::slot<void,char*,uint32_t> pslot = sigc::bind (mem_fun(*this, &GenericPluginUI::print_parameter), (uint32_t) port_index);
513
514                         control_ui->controller->set_size_request (200, req.height);
515                         control_ui->controller->set_name (X_("PluginSlider"));
516                         control_ui->controller->set_style (BarController::LeftToRight);
517                         control_ui->controller->set_use_parent (true);
518
519                         control_ui->controller->StartGesture.connect (bind (mem_fun(*this, &GenericPluginUI::start_touch), control_ui));
520                         control_ui->controller->StopGesture.connect (bind (mem_fun(*this, &GenericPluginUI::stop_touch), control_ui));
521                         
522                 }
523
524                 if (control_ui->logarithmic) {
525                         control_ui->controller->adjustment()->set_value(log(plugin->get_parameter(port_index)));
526                 } else{
527                         control_ui->controller->adjustment()->set_value(plugin->get_parameter(port_index));
528                 }
529
530                 /* XXX memory leak: SliderController not destroyed by ControlUI
531                    destructor, and manage() reports object hierarchy
532                    ambiguity.
533                 */
534
535                 control_ui->pack_start (control_ui->label, true, true);
536                 if (desc.integer_step) {
537                         control_ui->pack_start (*control_ui->clickbox, false, false);
538                 } else {
539                         control_ui->pack_start (*control_ui->controller, false, false);
540                 }
541
542                 control_ui->pack_start (control_ui->automate_button, false, false);
543                 control_ui->automate_button.signal_clicked().connect (bind (mem_fun(*this, &GenericPluginUI::astate_clicked), control_ui, (uint32_t) port_index));
544
545                 automation_state_changed (control_ui);
546
547                 mcontrol->Changed.connect (bind (mem_fun (*this, &GenericPluginUI::parameter_changed), control_ui));
548                 mcontrol->alist()->automation_state_changed.connect 
549                         (bind (mem_fun(*this, &GenericPluginUI::automation_state_changed), control_ui));
550
551         } else if (plugin->parameter_is_output (port_index)) {
552
553                 control_ui->display = manage (new EventBox);
554                 control_ui->display->set_name ("ParameterValueDisplay");
555
556                 control_ui->display_label = manage (new Label);
557
558                 control_ui->display_label->set_name ("ParameterValueDisplay");
559
560                 control_ui->display->add (*control_ui->display_label);
561                 Gtkmm2ext::set_size_request_to_display_given_text (*control_ui->display, "-99,99", 2, 2);
562
563                 control_ui->display->show_all ();
564
565                 /* set up a meter */
566                 /* TODO: only make a meter if the port is Hinted for it */
567
568                 MeterInfo * info = new MeterInfo(port_index);
569                 control_ui->meterinfo = info;
570                 
571                 info->meter = new FastMeter (5, 5, FastMeter::Vertical);
572
573                 info->min_unbound = desc.min_unbound;
574                 info->max_unbound = desc.max_unbound;
575
576                 info->min = desc.lower;
577                 info->max = desc.upper;
578
579                 control_ui->vbox = manage (new VBox);
580                 control_ui->hbox = manage (new HBox);
581                 
582                 control_ui->label.set_angle(90);
583                 control_ui->hbox->pack_start (control_ui->label, false, false);
584                 control_ui->hbox->pack_start (*info->meter, false, false);
585
586                 control_ui->vbox->pack_start (*control_ui->hbox, false, false);
587                 
588                 control_ui->vbox->pack_start (*control_ui->display, false, false);
589
590                 control_ui->pack_start (*control_ui->vbox);
591
592                 control_ui->meterinfo->meter->show_all();
593                 control_ui->meterinfo->packed = true;
594                 
595                 output_controls.push_back (control_ui);
596         }
597         
598         mcontrol->Changed.connect (bind (mem_fun (*this, &GenericPluginUI::parameter_changed), control_ui));
599         
600         return control_ui;
601 }
602
603 void
604 GenericPluginUI::start_touch (GenericPluginUI::ControlUI* cui)
605 {
606         cui->control->start_touch ();
607 }
608
609 void
610 GenericPluginUI::stop_touch (GenericPluginUI::ControlUI* cui)
611 {
612         cui->control->stop_touch ();
613 }
614
615 void
616 GenericPluginUI::astate_clicked (ControlUI* cui, uint32_t port)
617 {
618         using namespace Menu_Helpers;
619
620         if (automation_menu == 0) {
621                 automation_menu = manage (new Menu);
622                 automation_menu->set_name ("ArdourContextMenu");
623         } 
624
625         MenuList& items (automation_menu->items());
626
627         items.clear ();
628         items.push_back (MenuElem (_("Manual"), 
629                                    bind (mem_fun(*this, &GenericPluginUI::set_automation_state), (AutoState) Off, cui)));
630         items.push_back (MenuElem (_("Play"),
631                                    bind (mem_fun(*this, &GenericPluginUI::set_automation_state), (AutoState) Play, cui)));
632         items.push_back (MenuElem (_("Write"),
633                                    bind (mem_fun(*this, &GenericPluginUI::set_automation_state), (AutoState) Write, cui)));
634         items.push_back (MenuElem (_("Touch"),
635                                    bind (mem_fun(*this, &GenericPluginUI::set_automation_state), (AutoState) Touch, cui)));
636
637         automation_menu->popup (1, gtk_get_current_event_time());
638 }
639
640 void
641 GenericPluginUI::set_automation_state (AutoState state, ControlUI* cui)
642 {
643         insert->set_parameter_automation_state (cui->parameter(), state);
644 }
645
646 void
647 GenericPluginUI::toggle_parameter_changed (ControlUI* cui)
648 {
649         float val = cui->control->get_value();
650         
651         if (!cui->ignore_change) {
652                 if (val > 0.5) {
653                         cui->button->set_active (true);
654                 } else {
655                         cui->button->set_active (false);
656                 }
657         }
658 }
659
660 void
661 GenericPluginUI::parameter_changed (ControlUI* cui)
662 {
663         if (!cui->update_pending) {
664                 cui->update_pending = true;
665                 Gtkmm2ext::UI::instance()->call_slot (bind (mem_fun(*this, &GenericPluginUI::update_control_display), cui));
666         }
667 }
668
669 void
670 GenericPluginUI::update_control_display (ControlUI* cui)        
671 {
672         /* XXX how do we handle logarithmic stuff here ? */
673         
674         cui->update_pending = false;
675
676         float val = cui->control->get_value();
677
678         cui->ignore_change++;
679
680         if (cui->combo) {
681                 std::map<string,float>::iterator it;
682                 for (it = cui->combo_map->begin(); it != cui->combo_map->end(); ++it) {
683                         if (it->second == val) {
684                                 cui->combo->set_active_text(it->first);
685                                 break;
686                         }
687                 }
688         } else if (cui->button) {
689
690                 if (val > 0.5) {
691                         cui->button->set_active (true);
692                 } else {
693                         cui->button->set_active (false);
694                 }
695         }
696
697         if( cui->controller ) {
698             cui->controller->display_effective_value();
699         }
700
701
702         /*} else {
703                 if (cui->logarithmic) {
704                         val = log(val);
705                 }
706                 if (val != cui->adjustment->get_value()) {
707                         cui->adjustment->set_value (val);
708                 }
709         }*/
710         cui->ignore_change--;
711 }
712
713 void
714 GenericPluginUI::control_port_toggled (ControlUI* cui)
715 {
716         cui->ignore_change++;
717         insert->automation_control (cui->parameter())->set_value (cui->button->get_active());
718         cui->ignore_change--;
719 }
720
721 void
722 GenericPluginUI::control_combo_changed (ControlUI* cui)
723 {
724         if (!cui->ignore_change) {
725                 string value = cui->combo->get_active_text();
726                 std::map<string,float> mapping = *cui->combo_map;
727                 insert->automation_control(cui->parameter())->set_value(mapping[value]);
728         }
729 }
730
731 void
732 GenericPluginUI::processor_active_changed (boost::weak_ptr<Processor> weak_processor)
733 {
734         ENSURE_GUI_THREAD(bind (mem_fun(*this, &GenericPluginUI::processor_active_changed), weak_processor));
735         
736         boost::shared_ptr<Processor> processor = weak_processor.lock();
737
738         bypass_button.set_active (!processor || !processor->active());
739 }
740
741 bool
742 GenericPluginUI::start_updating (GdkEventAny* ignored)
743 {
744         if (output_controls.size() > 0 ) {
745                 screen_update_connection.disconnect();
746                 screen_update_connection = ARDOUR_UI::instance()->RapidScreenUpdate.connect 
747                         (mem_fun(*this, &GenericPluginUI::output_update));
748         }
749         return false;
750 }
751
752 bool
753 GenericPluginUI::stop_updating (GdkEventAny* ignored)
754 {
755         if (output_controls.size() > 0 ) {
756                 screen_update_connection.disconnect();
757         }
758         return false;
759 }
760
761 void
762 GenericPluginUI::output_update ()
763 {
764         for (vector<ControlUI*>::iterator i = output_controls.begin(); i != output_controls.end(); ++i) {
765                 float val = plugin->get_parameter ((*i)->parameter().id());
766                 char buf[32];
767                 snprintf (buf, sizeof(buf), "%.2f", val);
768                 (*i)->display_label->set_text (buf);
769
770                 /* autoscaling for the meter */
771                 if ((*i)->meterinfo && (*i)->meterinfo->packed) {
772                         
773                         if (val < (*i)->meterinfo->min) {
774                                 if ((*i)->meterinfo->min_unbound)
775                                         (*i)->meterinfo->min = val;
776                                 else
777                                         val = (*i)->meterinfo->min;
778                         }
779
780                         if (val > (*i)->meterinfo->max) {
781                                 if ((*i)->meterinfo->max_unbound)
782                                         (*i)->meterinfo->max = val;
783                                 else
784                                         val = (*i)->meterinfo->max;
785                         }
786                         
787                         if ((*i)->meterinfo->max > (*i)->meterinfo->min ) {
788                                 float lval = (val - (*i)->meterinfo->min) / ((*i)->meterinfo->max - (*i)->meterinfo->min) ;
789                                 (*i)->meterinfo->meter->set (lval );
790                         }
791                 }
792         }
793 }
794
795 vector<string> 
796 GenericPluginUI::setup_scale_values(guint32 port_index, ControlUI* cui)
797 {
798         vector<string> enums;
799         boost::shared_ptr<LadspaPlugin> lp;
800 #ifdef HAVE_SLV2
801         boost::shared_ptr<LV2Plugin> lv2p;
802 #endif
803
804         if ((lp = boost::dynamic_pointer_cast<LadspaPlugin>(plugin)) != 0) {
805                 // all LADPSA plugins have a numeric unique ID
806                 uint32_t id = atol (lp->unique_id().c_str());
807
808                 cui->combo_map = new std::map<string, float>;
809                 lrdf_defaults* defaults = lrdf_get_scale_values(id, port_index);
810                 if (defaults)   {
811                         for (uint32_t i = 0; i < defaults->count; ++i) {
812                                 enums.push_back(defaults->items[i].label);
813                                 pair<string, float> newpair;
814                                 newpair.first = defaults->items[i].label;
815                                 newpair.second = defaults->items[i].value;
816                                 cui->combo_map->insert(newpair);
817                         }
818
819                         lrdf_free_setting_values(defaults);
820                 }
821
822 #ifdef HAVE_SLV2
823         } else if ((lv2p = boost::dynamic_pointer_cast<LV2Plugin>(plugin)) != 0) {
824
825                 SLV2Port port = lv2p->slv2_port(port_index);
826                 SLV2ScalePoints points = slv2_port_get_scale_points(lv2p->slv2_plugin(), port);
827                 cui->combo_map = new std::map<string, float>;
828         
829                 for (unsigned i=0; i < slv2_scale_points_size(points); ++i) {
830                         SLV2ScalePoint p = slv2_scale_points_get_at(points, i);
831                         SLV2Value label = slv2_scale_point_get_label(p);
832                         SLV2Value value = slv2_scale_point_get_value(p);
833                         if (label && (slv2_value_is_float(value) || slv2_value_is_int(value))) {
834                                 enums.push_back(slv2_value_as_string(label));
835                                 pair<string, float> newpair;
836                                 newpair.first = slv2_value_as_string(label);
837                                 newpair.second = slv2_value_as_float(value);
838                                 cui->combo_map->insert(newpair);
839                         }
840                 }
841
842                 slv2_scale_points_free(points);
843 #endif
844         }
845         
846
847         return enums;
848 }
849
850
851