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