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