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