changes to help strp silence
[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
66 GenericPluginUI::GenericPluginUI (boost::shared_ptr<PluginInsert> pi, bool scrollable)
67         : PlugUIBase (pi),
68           button_table (initial_button_rows, initial_button_cols),
69           output_table (initial_output_rows, initial_output_cols),
70           hAdjustment(0.0, 0.0, 0.0),
71           vAdjustment(0.0, 0.0, 0.0),
72           scroller_view(hAdjustment, vAdjustment),
73           automation_menu (0),
74           is_scrollable(scrollable)
75 {
76         set_name ("PluginEditor");
77         set_border_width (10);
78         //set_homogeneous (false);
79
80         pack_start(main_contents);
81         settings_box.set_homogeneous (false);
82
83         HBox* constraint_hbox = manage (new HBox);
84         HBox* smaller_hbox = manage (new HBox);
85         Label* combo_label = manage (new Label (_("<span size=\"large\">Presets</span>")));
86         combo_label->set_use_markup (true);
87
88         latency_button.add (latency_label);
89         latency_button.signal_clicked().connect (sigc::mem_fun (*this, &PlugUIBase::latency_button_clicked));
90         set_latency_label ();
91
92         smaller_hbox->pack_start (latency_button, false, false, 10);
93         smaller_hbox->pack_start (preset_combo, false, false);
94         smaller_hbox->pack_start (save_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);
103
104         v1_box->pack_start (*smaller_hbox, false, true);
105         v2_box->pack_start (focus_button, false, true);
106
107         main_contents.pack_start (settings_box, false, false);
108
109         constraint_hbox->pack_end (*v2_box, false, false);
110         constraint_hbox->pack_end (*v1_box, false, false);
111
112         main_contents.pack_start (*constraint_hbox, false, false);
113
114         if ( is_scrollable ) {
115                 scroller.set_policy (Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
116                 scroller.set_name ("PluginEditor");
117                 scroller_view.set_name("PluginEditor");
118                 scroller_view.add (hpacker);
119                 scroller.add (scroller_view);
120
121                 main_contents.pack_start (scroller, true, true);
122
123         }
124         else {
125                 main_contents.pack_start (hpacker, false, false);
126         }
127
128         pi->ActiveChanged.connect (active_connection, boost::bind (&GenericPluginUI::processor_active_changed, this, boost::weak_ptr<Processor>(pi)), gui_context());
129
130         bypass_button.set_active (!pi->active());
131
132         build ();
133 }
134
135 GenericPluginUI::~GenericPluginUI ()
136 {
137         if (output_controls.size() > 0) {
138                 screen_update_connection.disconnect();
139         }
140 }
141
142 void
143 GenericPluginUI::build ()
144
145 {
146         guint32 i = 0;
147         guint32 x = 0;
148         Frame* frame;
149         Frame* bt_frame;
150         VBox* box;
151         int output_row, output_col;
152         int button_row, button_col;
153         int output_rows, output_cols;
154         int button_rows, button_cols;
155
156         prefheight = 30;
157         hpacker.set_spacing (10);
158
159         output_rows = initial_output_rows;
160         output_cols = initial_output_cols;
161         button_rows = initial_button_rows;
162         button_cols = initial_button_cols;
163         output_row = 0;
164         button_row = 0;
165         output_col = 0;
166         button_col = 0;
167
168         button_table.set_homogeneous (false);
169         button_table.set_row_spacings (2);
170         button_table.set_col_spacings (2);
171         output_table.set_homogeneous (true);
172         output_table.set_row_spacings (2);
173         output_table.set_col_spacings (2);
174         button_table.set_border_width (5);
175         output_table.set_border_width (5);
176
177         hpacker.set_border_width (10);
178
179         bt_frame = manage (new Frame);
180         bt_frame->set_name ("BaseFrame");
181         bt_frame->add (button_table);
182         hpacker.pack_start(*bt_frame, true, true);
183
184         box = manage (new VBox);
185         box->set_border_width (5);
186         box->set_spacing (1);
187
188         frame = manage (new Frame);
189         frame->set_name ("BaseFrame");
190         frame->set_label (_("Controls"));
191         frame->add (*box);
192         hpacker.pack_start(*frame, true, true);
193
194         /* find all ports. build control elements for all appropriate control ports */
195
196         for (i = 0; i < plugin->parameter_count(); ++i) {
197
198                 if (plugin->parameter_is_control (i)) {
199
200                         /* Don't show latency control ports */
201
202                         if (plugin->describe_parameter (Evoral::Parameter(PluginAutomation, 0, i)) == X_("latency")) {
203                                 continue;
204                         }
205
206                         ControlUI* cui;
207
208                         /* if we are scrollable, just use one long column */
209
210                         if (!is_scrollable) {
211                                 if (x++ > 20){
212                                         frame = manage (new Frame);
213                                         frame->set_name ("BaseFrame");
214                                         box = manage (new VBox);
215
216                                         box->set_border_width (5);
217                                         box->set_spacing (1);
218
219                                         frame->add (*box);
220                                         hpacker.pack_start(*frame,true,true);
221
222                                         x = 1;
223                                 }
224                         }
225
226                         boost::shared_ptr<ARDOUR::AutomationControl> c
227                                 = boost::dynamic_pointer_cast<ARDOUR::AutomationControl>(
228                                         insert->control(Evoral::Parameter(PluginAutomation, 0, i)));
229
230                         if ((cui = build_control_ui (i, c)) == 0) {
231                                 error << string_compose(_("Plugin Editor: could not build control element for port %1"), i) << endmsg;
232                                 continue;
233                         }
234
235                         if (cui->controller || cui->clickbox || cui->combo) {
236
237                                 box->pack_start (*cui, false, false);
238
239                         } else if (cui->button) {
240
241                                 if (button_row == button_rows) {
242                                         button_row = 0;
243                                         if (++button_col == button_cols) {
244                                                 button_cols += 2;
245                                                 button_table.resize (button_rows, button_cols);
246                                         }
247                                 }
248
249                                 button_table.attach (*cui, button_col, button_col + 1, button_row, button_row+1,
250                                                      FILL|EXPAND, FILL);
251                                 button_row++;
252
253                         } else if (cui->display) {
254
255                                 output_table.attach (*cui, output_col, output_col + 1, output_row, output_row+1,
256                                                      FILL|EXPAND, FILL);
257
258                                 // TODO: The meters should be divided into multiple rows
259
260                                 if (++output_col == output_cols) {
261                                         output_cols ++;
262                                         output_table.resize (output_rows, output_cols);
263                                 }
264
265                                 /* old code, which divides meters into
266                                  * columns first, rows later. New code divides into one row
267
268                                 if (output_row == output_rows) {
269                                         output_row = 0;
270                                         if (++output_col == output_cols) {
271                                                 output_cols += 2;
272                                                 output_table.resize (output_rows, output_cols);
273                                         }
274                                 }
275
276                                 output_table.attach (*cui, output_col, output_col + 1, output_row, output_row+1,
277                                                      FILL|EXPAND, FILL);
278
279                                 output_row++;
280                                 */
281                         }
282
283                         /* HACK: ideally the preferred height would be queried from
284                            the complete hpacker, but I can't seem to get that
285                            information in time, so this is an estimation
286                         */
287
288                         prefheight += 30;
289
290                 }
291         }
292
293         if (box->children().empty()) {
294                 hpacker.remove (*frame);
295         }
296
297         if (button_table.children().empty()) {
298                 hpacker.remove (*bt_frame);
299         }
300
301         if (!output_table.children().empty()) {
302                 frame = manage (new Frame);
303                 frame->set_name ("BaseFrame");
304                 frame->add (output_table);
305                 hpacker.pack_end (*frame, true, true);
306         }
307
308         output_update ();
309
310         output_table.show_all ();
311         button_table.show_all ();
312 }
313
314 GenericPluginUI::ControlUI::ControlUI ()
315         : automate_button (X_("")) // force creation of a label
316 {
317         automate_button.set_name ("PluginAutomateButton");
318         ARDOUR_UI::instance()->set_tip (automate_button, _("Automation control"));
319
320         /* XXX translators: use a string here that will be at least as long
321            as the longest automation label (see ::automation_state_changed()
322            below). be sure to include a descender.
323         */
324
325         set_size_request_to_display_given_text (*automate_button.get_child(), _("Mgnual"), 5, 5);
326
327         ignore_change = 0;
328         display = 0;
329         button = 0;
330         clickbox = 0;
331         meterinfo = 0;
332 }
333
334 GenericPluginUI::ControlUI::~ControlUI()
335 {
336         if (meterinfo) {
337                 delete meterinfo->meter;
338                 delete meterinfo;
339         }
340 }
341
342 void
343 GenericPluginUI::automation_state_changed (ControlUI* cui)
344 {
345         /* update button label */
346
347         // don't lock to avoid deadlock because we're triggered by
348         // AutomationControl::Changed() while the automation lock is taken
349         switch (insert->get_parameter_automation_state (cui->parameter(), false)
350                         & (Off|Play|Touch|Write)) {
351         case Off:
352                 cui->automate_button.set_label (_("Manual"));
353                 break;
354         case Play:
355                 cui->automate_button.set_label (_("Play"));
356                 break;
357         case Write:
358                 cui->automate_button.set_label (_("Write"));
359                 break;
360         case Touch:
361                 cui->automate_button.set_label (_("Touch"));
362                 break;
363         default:
364                 cui->automate_button.set_label (_("???"));
365                 break;
366         }
367 }
368
369
370 static void integer_printer (char buf[32], Adjustment &adj, void */*arg*/)
371 {
372         snprintf (buf, 32, "%.0f", adj.get_value());
373 }
374
375 void
376 GenericPluginUI::print_parameter (char *buf, uint32_t len, uint32_t param)
377 {
378         plugin->print_parameter (param, buf, len);
379 }
380
381 GenericPluginUI::ControlUI*
382 GenericPluginUI::build_control_ui (guint32 port_index, boost::shared_ptr<AutomationControl> mcontrol)
383 {
384         ControlUI* control_ui = 0;
385         if (!mcontrol) {
386                 return control_ui;
387         }
388
389         Plugin::ParameterDescriptor desc;
390
391         plugin->get_parameter_descriptor (port_index, desc);
392
393         control_ui = manage (new ControlUI ());
394         control_ui->combo = 0;
395         control_ui->combo_map = 0;
396         control_ui->control = mcontrol;
397         control_ui->update_pending = false;
398         control_ui->label.set_text (desc.label);
399         control_ui->label.set_alignment (0.0, 0.5);
400         control_ui->label.set_name ("PluginParameterLabel");
401
402         control_ui->set_spacing (5);
403
404         Gtk::Requisition req (control_ui->automate_button.size_request());
405
406         if (plugin->parameter_is_input (port_index)) {
407
408                 boost::shared_ptr<LadspaPlugin> lp;
409 #ifdef HAVE_SLV2
410                 boost::shared_ptr<LV2Plugin> lv2p;
411 #endif
412                 if ((lp = boost::dynamic_pointer_cast<LadspaPlugin>(plugin)) != 0) {
413
414                         // FIXME: not all plugins have a numeric unique ID
415                         uint32_t id = atol (lp->unique_id().c_str());
416                         lrdf_defaults* defaults = lrdf_get_scale_values(id, port_index);
417
418                         if (defaults && defaults->count > 0)    {
419
420                                 control_ui->combo = new Gtk::ComboBoxText;
421                                 //control_ui->combo->set_value_in_list(true, false);
422                                 set_popdown_strings (*control_ui->combo, setup_scale_values(port_index, control_ui));
423                                 control_ui->combo->signal_changed().connect (sigc::bind (sigc::mem_fun(*this, &GenericPluginUI::control_combo_changed), control_ui));
424                                 mcontrol->Changed.connect (control_connections, boost::bind (&GenericPluginUI::parameter_changed, this, control_ui), gui_context());
425                                 control_ui->pack_start(control_ui->label, true, true);
426                                 control_ui->pack_start(*control_ui->combo, false, true);
427
428                                 update_control_display(control_ui);
429
430                                 lrdf_free_setting_values(defaults);
431                                 return control_ui;
432                         }
433
434 #ifdef HAVE_SLV2
435                 } else if ((lv2p = boost::dynamic_pointer_cast<LV2Plugin>(plugin)) != 0) {
436
437                         SLV2Port port = lv2p->slv2_port(port_index);
438                         SLV2ScalePoints points = slv2_port_get_scale_points(lv2p->slv2_plugin(), port);
439
440                         if (points) {
441                                 control_ui->combo = new Gtk::ComboBoxText;
442                                 //control_ui->combo->set_value_in_list(true, false);
443                                 set_popdown_strings (*control_ui->combo, setup_scale_values(port_index, control_ui));
444                                 control_ui->combo->signal_changed().connect (sigc::bind (sigc::mem_fun(*this, &GenericPluginUI::control_combo_changed), control_ui));
445                                 mcontrol->Changed.connect (control_connections, boost::bind (&GenericPluginUI::parameter_changed, this, control_ui), gui_context());
446                                 control_ui->pack_start(control_ui->label, true, true);
447                                 control_ui->pack_start(*control_ui->combo, false, true);
448
449                                 update_control_display(control_ui);
450
451                                 slv2_scale_points_free(points);
452                                 return control_ui;
453                         }
454 #endif
455                 }
456
457                 if (desc.toggled) {
458
459                         /* Build a button */
460
461                         control_ui->button = manage (new ToggleButton ());
462                         control_ui->button->set_name ("PluginEditorButton");
463                         control_ui->button->set_size_request (20, 20);
464
465                         control_ui->pack_start (control_ui->label, true, true);
466                         control_ui->pack_start (*control_ui->button, false, true);
467                         // control_ui->pack_start (control_ui->automate_button, false, false);
468
469                         control_ui->button->signal_clicked().connect (sigc::bind (sigc::mem_fun(*this, &GenericPluginUI::control_port_toggled), control_ui));
470                         mcontrol->Changed.connect (control_connections, boost::bind (&GenericPluginUI::toggle_parameter_changed, this, control_ui), gui_context());
471
472                         if (plugin->get_parameter (port_index) > 0.5){
473                                 control_ui->button->set_active(true);
474                         }
475
476                         return control_ui;
477                 }
478
479                 /* create the controller */
480
481                 control_ui->controller = AutomationController::create(insert, mcontrol->parameter(), mcontrol);
482                 /* XXX this code is not right yet, because it doesn't handle
483                    the absence of bounds in any sensible fashion.
484                 */
485
486 //#if 0
487                 control_ui->controller->adjustment()->set_lower (desc.lower);
488                 control_ui->controller->adjustment()->set_upper (desc.upper);
489
490                 control_ui->logarithmic = false; // just disable it for now
491                 /*
492                 control_ui->logarithmic = desc.logarithmic;
493                 if (control_ui->logarithmic) {
494                         if (control_ui->controller->adjustment()->get_lower() == 0.0) {
495                                 control_ui->controller->adjustment()->set_lower (control_ui->controller->adjustment()->get_upper()/10000);
496                         }
497                         control_ui->controller->adjustment()->set_upper (log(control_ui->controller->adjustment()->get_upper()));
498                         control_ui->controller->adjustment()->set_lower (log(control_ui->controller->adjustment()->get_lower()));
499                 }*/
500
501
502                 control_ui->controller->adjustment()->set_step_increment (desc.step);
503                 control_ui->controller->adjustment()->set_page_increment (desc.largestep);
504 //#endif
505
506                 if (desc.integer_step) {
507                         control_ui->clickbox = new ClickBox (control_ui->controller->adjustment(), "PluginUIClickBox");
508                         Gtkmm2ext::set_size_request_to_display_given_text (*control_ui->clickbox, "g9999999", 2, 2);
509                         control_ui->clickbox->set_print_func (integer_printer, 0);
510                 } else {
511                         //sigc::slot<void,char*,uint32_t> pslot = sigc::bind (sigc::mem_fun(*this, &GenericPluginUI::print_parameter), (uint32_t) port_index);
512
513                         control_ui->controller->set_size_request (200, req.height);
514                         control_ui->controller->set_name (X_("PluginSlider"));
515                         control_ui->controller->set_style (BarController::LeftToRight);
516                         control_ui->controller->set_use_parent (true);
517
518                         control_ui->controller->StartGesture.connect (sigc::bind (sigc::mem_fun(*this, &GenericPluginUI::start_touch), control_ui));
519                         control_ui->controller->StopGesture.connect (sigc::bind (sigc::mem_fun(*this, &GenericPluginUI::stop_touch), control_ui));
520
521                 }
522
523                 if (control_ui->logarithmic) {
524                         control_ui->controller->adjustment()->set_value(log(plugin->get_parameter(port_index)));
525                 } else{
526                         control_ui->controller->adjustment()->set_value(plugin->get_parameter(port_index));
527                 }
528
529                 /* XXX memory leak: SliderController not destroyed by ControlUI
530                    destructor, and manage() reports object hierarchy
531                    ambiguity.
532                 */
533
534                 control_ui->pack_start (control_ui->label, true, true);
535                 if (desc.integer_step) {
536                         control_ui->pack_start (*control_ui->clickbox, false, false);
537                 } else {
538                         control_ui->pack_start (*control_ui->controller, false, false);
539                 }
540
541                 control_ui->pack_start (control_ui->automate_button, false, false);
542                 control_ui->automate_button.signal_clicked().connect (sigc::bind (sigc::mem_fun(*this, &GenericPluginUI::astate_clicked), control_ui, (uint32_t) port_index));
543
544                 automation_state_changed (control_ui);
545
546                 mcontrol->Changed.connect (control_connections, boost::bind (&GenericPluginUI::parameter_changed, this, control_ui), gui_context());
547                 mcontrol->alist()->automation_state_changed.connect (control_connections, boost::bind (&GenericPluginUI::automation_state_changed, this, control_ui), gui_context());
548
549                 input_controls.push_back (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 (control_connections, boost::bind (&GenericPluginUI::parameter_changed, this, control_ui), gui_context());
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                                    sigc::bind (sigc::mem_fun(*this, &GenericPluginUI::set_automation_state), (AutoState) Off, cui)));
630         items.push_back (MenuElem (_("Play"),
631                                    sigc::bind (sigc::mem_fun(*this, &GenericPluginUI::set_automation_state), (AutoState) Play, cui)));
632         items.push_back (MenuElem (_("Write"),
633                                    sigc::bind (sigc::mem_fun(*this, &GenericPluginUI::set_automation_state), (AutoState) Write, cui)));
634         items.push_back (MenuElem (_("Touch"),
635                                    sigc::bind (sigc::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 (boost::bind (&GenericPluginUI::update_control_display, this, 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 (*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*)
743 {
744         if (output_controls.size() > 0 ) {
745                 screen_update_connection.disconnect();
746                 screen_update_connection = ARDOUR_UI::instance()->RapidScreenUpdate.connect
747                         (sigc::mem_fun(*this, &GenericPluginUI::output_update));
748         }
749         return false;
750 }
751
752 bool
753 GenericPluginUI::stop_updating (GdkEventAny*)
754 {
755         for (vector<ControlUI*>::iterator i = input_controls.begin(); i != input_controls.end(); ++i) {
756                 (*i)->controller->stop_updating ();
757         }
758         
759         if (output_controls.size() > 0 ) {
760                 screen_update_connection.disconnect();
761         }
762         
763         return false;
764 }
765
766 void
767 GenericPluginUI::output_update ()
768 {
769         for (vector<ControlUI*>::iterator i = output_controls.begin(); i != output_controls.end(); ++i) {
770                 float val = plugin->get_parameter ((*i)->parameter().id());
771                 char buf[32];
772                 snprintf (buf, sizeof(buf), "%.2f", val);
773                 (*i)->display_label->set_text (buf);
774
775                 /* autoscaling for the meter */
776                 if ((*i)->meterinfo && (*i)->meterinfo->packed) {
777
778                         if (val < (*i)->meterinfo->min) {
779                                 if ((*i)->meterinfo->min_unbound)
780                                         (*i)->meterinfo->min = val;
781                                 else
782                                         val = (*i)->meterinfo->min;
783                         }
784
785                         if (val > (*i)->meterinfo->max) {
786                                 if ((*i)->meterinfo->max_unbound)
787                                         (*i)->meterinfo->max = val;
788                                 else
789                                         val = (*i)->meterinfo->max;
790                         }
791
792                         if ((*i)->meterinfo->max > (*i)->meterinfo->min ) {
793                                 float lval = (val - (*i)->meterinfo->min) / ((*i)->meterinfo->max - (*i)->meterinfo->min) ;
794                                 (*i)->meterinfo->meter->set (lval );
795                         }
796                 }
797         }
798 }
799
800 vector<string>
801 GenericPluginUI::setup_scale_values(guint32 port_index, ControlUI* cui)
802 {
803         vector<string> enums;
804         boost::shared_ptr<LadspaPlugin> lp;
805 #ifdef HAVE_SLV2
806         boost::shared_ptr<LV2Plugin> lv2p;
807 #endif
808
809         if ((lp = boost::dynamic_pointer_cast<LadspaPlugin>(plugin)) != 0) {
810                 // all LADPSA plugins have a numeric unique ID
811                 uint32_t id = atol (lp->unique_id().c_str());
812
813                 cui->combo_map = new std::map<string, float>;
814                 lrdf_defaults* defaults = lrdf_get_scale_values(id, port_index);
815                 if (defaults)   {
816                         for (uint32_t i = 0; i < defaults->count; ++i) {
817                                 enums.push_back(defaults->items[i].label);
818                                 pair<string, float> newpair;
819                                 newpair.first = defaults->items[i].label;
820                                 newpair.second = defaults->items[i].value;
821                                 cui->combo_map->insert(newpair);
822                         }
823
824                         lrdf_free_setting_values(defaults);
825                 }
826
827 #ifdef HAVE_SLV2
828         } else if ((lv2p = boost::dynamic_pointer_cast<LV2Plugin>(plugin)) != 0) {
829
830                 SLV2Port port = lv2p->slv2_port(port_index);
831                 SLV2ScalePoints points = slv2_port_get_scale_points(lv2p->slv2_plugin(), port);
832                 cui->combo_map = new std::map<string, float>;
833
834                 for (unsigned i=0; i < slv2_scale_points_size(points); ++i) {
835                         SLV2ScalePoint p = slv2_scale_points_get_at(points, i);
836                         SLV2Value label = slv2_scale_point_get_label(p);
837                         SLV2Value value = slv2_scale_point_get_value(p);
838                         if (label && (slv2_value_is_float(value) || slv2_value_is_int(value))) {
839                                 enums.push_back(slv2_value_as_string(label));
840                                 pair<string, float> newpair;
841                                 newpair.first = slv2_value_as_string(label);
842                                 newpair.second = slv2_value_as_float(value);
843                                 cui->combo_map->insert(newpair);
844                         }
845                 }
846
847                 slv2_scale_points_free(points);
848 #endif
849         }
850
851
852         return enums;
853 }
854
855
856