Mixer pane can be collapsed again, some cleanup, give up and go back to 'Off' for...
[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|Gdk::BUTTON_PRESS_MASK|Gdk::BUTTON_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\">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 (5);
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->set_label (_("Controls"));
230         frame->add (*box);
231         hpacker.pack_start(*frame, true, true);
232
233         /* find all ports. build control elements for all appropriate control ports */
234
235         for (i = 0; i < plugin.parameter_count(); ++i) {
236
237                 if (plugin.parameter_is_control (i)) {
238                         
239                         /* Don't show latency control ports */
240
241                         if (plugin.describe_parameter (i) == X_("latency")) {
242                                 continue;
243                         }
244
245                         ControlUI* cui;
246         
247                         /* if we are scrollable, just use one long column */
248
249                         if (!is_scrollable) {
250                                 if (x++ > 7){
251                                         frame = manage (new Frame);
252                                         frame->set_name ("BaseFrame");
253                                         box = manage (new VBox);
254                                         
255                                         box->set_border_width (5);
256                                         box->set_spacing (1);
257
258                                         frame->add (*box);
259                                         hpacker.pack_start(*frame,true,true);
260
261                                         x = 0;
262                                 }
263                         }
264
265                         if ((cui = build_control_ui (engine, i, plugin.get_nth_midi_control (i))) == 0) {
266                                 error << string_compose(_("Plugin Editor: could not build control element for port %1"), i) << endmsg;
267                                 continue;
268                         }
269                                 
270                         if (cui->control || cui->clickbox || cui->combo) {
271
272                                 box->pack_start (*cui, false, false);
273
274                         } else if (cui->button) {
275
276                                 if (button_row == button_rows) {
277                                         button_row = 0;
278                                         if (++button_col == button_cols) {
279                                                 button_cols += 2;
280                                                 button_table.resize (button_rows, button_cols);
281                                         }
282                                 }
283
284                                 button_table.attach (*cui, button_col, button_col + 1, button_row, button_row+1, 
285                                                      FILL|EXPAND, FILL);
286                                 button_row++;
287
288                         } else if (cui->display) {
289
290                                 output_table.attach (*cui, output_col, output_col + 1, output_row, output_row+1, 
291                                                      FILL|EXPAND, FILL);
292                                 
293                                 // TODO: The meters should be divided into multiple rows 
294                                 
295                                 if (++output_col == output_cols) {
296                                         output_cols ++;
297                                         output_table.resize (output_rows, output_cols);
298                                 }
299                                 
300                                 /* old code, which divides meters into
301                                  * columns first, rows later. New code divides into one row
302                                  
303                                 if (output_row == output_rows) {
304                                         output_row = 0;
305                                         if (++output_col == output_cols) {
306                                                 output_cols += 2;
307                                                 output_table.resize (output_rows, output_cols);
308                                         }
309                                 }
310                                 
311                                 output_table.attach (*cui, output_col, output_col + 1, output_row, output_row+1, 
312                                                      FILL|EXPAND, FILL);
313  
314                                 output_row++;
315                                 */
316                         }
317                                 
318                         /* HACK: ideally the preferred height would be queried from
319                            the complete hpacker, but I can't seem to get that
320                            information in time, so this is an estimation 
321                         */
322
323                         prefheight += 30;
324
325                 } 
326         }
327
328         n_ins = plugin.get_info().n_inputs;
329         n_outs = plugin.get_info().n_outputs;
330
331         if (box->children().empty()) {
332                 hpacker.remove (*frame);
333         }
334
335         if (button_table.children().empty()) {
336                 hpacker.remove (*bt_frame);
337         }
338
339         if (!output_table.children().empty()) {
340                 frame = manage (new Frame);
341                 frame->set_name ("BaseFrame");
342                 frame->add (output_table);
343                 hpacker.pack_end (*frame, true, true);
344         }
345
346         output_update ();
347
348         output_table.show_all ();
349         button_table.show_all ();
350 }
351
352 PluginUI::ControlUI::ControlUI ()
353         : automate_button (X_("")) // force creation of a label 
354 {
355         automate_button.set_name ("PluginAutomateButton");
356         ARDOUR_UI::instance()->tooltips().set_tip (automate_button,
357                                                    _("Automation control"));
358
359         /* don't fix the height, it messes up the bar controllers */
360
361         set_size_request_to_display_given_text (automate_button, X_("lngnuf"), 2, 2);
362
363         ignore_change = 0;
364         display = 0;
365         button = 0;
366         control = 0;
367         clickbox = 0;
368         adjustment = 0;
369         meterinfo = 0;
370 }
371
372 PluginUI::ControlUI::~ControlUI() 
373 {
374         if (adjustment) {
375                 delete adjustment;
376         }
377
378         if (meterinfo) {
379                 delete meterinfo->meter;
380                 delete meterinfo;
381         }
382 }
383
384 void
385 PluginUI::automation_state_changed (ControlUI* cui)
386 {
387         /* update button label */
388
389         switch (insert.get_port_automation_state (cui->port_index) & (Off|Play|Touch|Write)) {
390         case Off:
391                 cui->automate_button.set_label (_("Off"));
392                 break;
393         case Play:
394                 cui->automate_button.set_label (_("Play"));
395                 break;
396         case Write:
397                 cui->automate_button.set_label (_("Write"));
398                 break;
399         case Touch:
400                 cui->automate_button.set_label (_("Touch"));
401                 break;
402         default:
403                 cui->automate_button.set_label (_("???"));
404                 break;
405         }
406 }
407
408
409 static void integer_printer (char buf[32], Adjustment &adj, void *arg)
410 {
411         snprintf (buf, 32, "%.0f", adj.get_value());
412 }
413
414 void
415 PluginUI::print_parameter (char *buf, uint32_t len, uint32_t param)
416 {
417         plugin.print_parameter (param, buf, len);
418 }
419
420 PluginUI::ControlUI*
421 PluginUI::build_control_ui (AudioEngine &engine, guint32 port_index, MIDI::Controllable* mcontrol)
422
423 {
424         ControlUI* control_ui;
425         Plugin::ParameterDescriptor desc;
426
427         plugin.get_parameter_descriptor (port_index, desc);
428
429         control_ui = manage (new ControlUI ());
430         control_ui->adjustment = 0;
431         control_ui->combo = 0;
432         control_ui->combo_map = 0;
433         control_ui->port_index = port_index;
434         control_ui->update_pending = false;
435         control_ui->label.set_text (desc.label);
436         control_ui->label.set_alignment (0.0, 0.5);
437         control_ui->label.set_name ("PluginParameterLabel");
438
439         control_ui->set_spacing (5);
440
441         if (plugin.parameter_is_input (port_index)) {
442
443                 LadspaPlugin* lp;
444
445                 if ((lp = dynamic_cast<LadspaPlugin*>(&plugin)) != 0) {
446                         
447                         lrdf_defaults* defaults = lrdf_get_scale_values(lp->unique_id(), port_index);
448                         
449                         if (defaults && defaults->count > 0)    {
450                                 
451                                 control_ui->combo = new Gtk::ComboBoxText;
452                                 //control_ui->combo->set_value_in_list(true, false);
453                                 set_popdown_strings (*control_ui->combo, setup_scale_values(port_index, control_ui));
454                                 control_ui->combo->signal_changed().connect (bind (mem_fun(*this, &PluginUI::control_combo_changed), control_ui));
455                                 plugin.ParameterChanged.connect (bind (mem_fun (*this, &PluginUI::parameter_changed), control_ui));
456                                 control_ui->pack_start(control_ui->label, true, true);
457                                 control_ui->pack_start(*control_ui->combo, false, true);
458                                 
459                                 update_control_display(control_ui);
460                                 
461                                 lrdf_free_setting_values(defaults);
462                                 return control_ui;
463                         }
464                 }
465                         
466                 if (desc.toggled) {
467
468                         /* Build a button */
469                 
470                         control_ui->button = manage (new ToggleButton ());
471                         control_ui->button->set_name ("PluginEditorButton");
472                         control_ui->button->set_size_request (20, 20);
473
474                         control_ui->pack_start (control_ui->label, true, true);
475                         control_ui->pack_start (*control_ui->button, false, true);
476                         control_ui->pack_start (control_ui->automate_button, false, false);
477
478                         control_ui->button->signal_clicked().connect (bind (mem_fun(*this, &PluginUI::control_port_toggled), control_ui));
479                 
480                         if(plugin.get_parameter (port_index) == 1){
481                                 control_ui->button->set_active(true);
482                         }
483
484                         return control_ui;
485                 }
486         
487                 control_ui->adjustment = new Adjustment (0, 0, 0, 0, 0);
488
489                 /* XXX this code is not right yet, because it doesn't handle
490                    the absence of bounds in any sensible fashion.
491                 */
492
493                 control_ui->adjustment->set_lower (desc.lower);
494                 control_ui->adjustment->set_upper (desc.upper);
495
496                 control_ui->logarithmic = desc.logarithmic;
497                 if (control_ui->logarithmic) {
498                         if (control_ui->adjustment->get_lower() == 0.0) {
499                                 control_ui->adjustment->set_lower (control_ui->adjustment->get_upper()/10000);
500                         }
501                         control_ui->adjustment->set_upper (log(control_ui->adjustment->get_upper()));
502                         control_ui->adjustment->set_lower (log(control_ui->adjustment->get_lower()));
503                 }
504         
505                 float delta = desc.upper - desc.lower;
506
507                 control_ui->adjustment->set_page_size (delta/100.0);
508                 control_ui->adjustment->set_step_increment (desc.step);
509                 control_ui->adjustment->set_page_increment (desc.largestep);
510
511                 if (desc.integer_step) {
512                         control_ui->clickbox = new ClickBox (control_ui->adjustment, "PluginUIClickBox");
513                         Gtkmm2ext::set_size_request_to_display_given_text (*control_ui->clickbox, "g9999999", 2, 2);
514                         control_ui->clickbox->set_print_func (integer_printer, 0);
515                 } else {
516                         sigc::slot<void,char*,uint32_t> pslot = sigc::bind (mem_fun(*this, &PluginUI::print_parameter), (uint32_t) port_index);
517
518                         control_ui->control = new BarController (*control_ui->adjustment, mcontrol, pslot);
519                         // should really match the height of the text in the automation button+label
520                         control_ui->control->set_size_request (200, 22);
521                         control_ui->control->set_name (X_("PluginSlider"));
522                         control_ui->control->set_style (BarController::LeftToRight);
523                         control_ui->control->set_use_parent (true);
524
525                         control_ui->control->StartGesture.connect (bind (mem_fun(*this, &PluginUI::start_touch), control_ui));
526                         control_ui->control->StopGesture.connect (bind (mem_fun(*this, &PluginUI::stop_touch), control_ui));
527                         
528                 }
529
530                 if (control_ui->logarithmic) {
531                         control_ui->adjustment->set_value(log(plugin.get_parameter(port_index)));
532                 } else{
533                         control_ui->adjustment->set_value(plugin.get_parameter(port_index));
534                 }
535
536                 /* XXX memory leak: SliderController not destroyed by ControlUI
537                    destructor, and manage() reports object hierarchy
538                    ambiguity.
539                 */
540
541                 control_ui->pack_start (control_ui->label, true, true);
542                 if (desc.integer_step) {
543                         control_ui->pack_start (*control_ui->clickbox, false, false);
544                 } else {
545                         control_ui->pack_start (*control_ui->control, false, false);
546                 }
547
548                 control_ui->pack_start (control_ui->automate_button, false, false);
549                 control_ui->adjustment->signal_value_changed().connect (bind (mem_fun(*this, &PluginUI::control_adjustment_changed), control_ui));
550                 control_ui->automate_button.signal_clicked().connect (bind (mem_fun(*this, &PluginUI::astate_clicked), control_ui, (uint32_t) port_index));
551
552                 automation_state_changed (control_ui);
553
554                 plugin.ParameterChanged.connect (bind (mem_fun(*this, &PluginUI::parameter_changed), control_ui));
555                 insert.automation_list (port_index).automation_state_changed.connect 
556                         (bind (mem_fun(*this, &PluginUI::automation_state_changed), control_ui));
557
558         } else if (plugin.parameter_is_output (port_index)) {
559
560                 control_ui->display = manage (new EventBox);
561                 control_ui->display->set_name ("ParameterValueDisplay");
562
563                 control_ui->display_label = manage (new Label);
564
565                 control_ui->display_label->set_name ("ParameterValueDisplay");
566
567                 control_ui->display->add (*control_ui->display_label);
568                 Gtkmm2ext::set_size_request_to_display_given_text (*control_ui->display, "-99,99", 2, 2);
569
570                 control_ui->display->show_all ();
571
572                 /* set up a meter */
573                 /* TODO: only make a meter if the port is Hinted for it */
574
575                 MeterInfo * info = new MeterInfo(port_index);
576                 control_ui->meterinfo = info;
577                 
578                 info->meter = new FastMeter (5, 100, FastMeter::Vertical);
579
580                 info->min_unbound = desc.min_unbound;
581                 info->max_unbound = desc.max_unbound;
582
583                 info->min = desc.lower;
584                 info->max = desc.upper;
585
586                 control_ui->vbox = manage (new VBox);
587                 control_ui->hbox = manage (new HBox);
588                 
589                 control_ui->label.set_angle(90);
590                 control_ui->hbox->pack_start (control_ui->label, false, false);
591                 control_ui->hbox->pack_start (*info->meter, false, false);
592
593                 control_ui->vbox->pack_start (*control_ui->hbox, false, false);
594                 
595                 control_ui->vbox->pack_start (*control_ui->display, false, false);
596
597                 control_ui->pack_start (*control_ui->vbox);
598
599                 control_ui->meterinfo->meter->show_all();
600                 control_ui->meterinfo->packed = true;
601                 
602                 output_controls.push_back (control_ui);
603         }
604         
605         plugin.ParameterChanged.connect (bind (mem_fun(*this, &PluginUI::parameter_changed), control_ui));
606         return control_ui;
607 }
608
609 void
610 PluginUI::start_touch (PluginUI::ControlUI* cui)
611 {
612         insert.automation_list (cui->port_index).start_touch ();
613 }
614
615 void
616 PluginUI::stop_touch (PluginUI::ControlUI* cui)
617 {
618         insert.automation_list (cui->port_index).stop_touch ();
619 }
620
621 void
622 PluginUI::astate_clicked (ControlUI* cui, uint32_t port)
623 {
624         using namespace Menu_Helpers;
625
626         if (automation_menu == 0) {
627                 automation_menu = manage (new Menu);
628                 automation_menu->set_name ("ArdourContextMenu");
629         } 
630
631         MenuList& items (automation_menu->items());
632
633         items.clear ();
634         items.push_back (MenuElem (_("Off"), 
635                                    bind (mem_fun(*this, &PluginUI::set_automation_state), (AutoState) Off, cui)));
636         items.push_back (MenuElem (_("Play"),
637                                    bind (mem_fun(*this, &PluginUI::set_automation_state), (AutoState) Play, cui)));
638         items.push_back (MenuElem (_("Write"),
639                                    bind (mem_fun(*this, &PluginUI::set_automation_state), (AutoState) Write, cui)));
640         items.push_back (MenuElem (_("Touch"),
641                                    bind (mem_fun(*this, &PluginUI::set_automation_state), (AutoState) Touch, cui)));
642
643         automation_menu->popup (1, 0);
644 }
645
646 void
647 PluginUI::set_automation_state (AutoState state, ControlUI* cui)
648 {
649         insert.set_port_automation_state (cui->port_index, state);
650 }
651
652 void
653 PluginUI::control_adjustment_changed (ControlUI* cui)
654 {
655         if (cui->ignore_change) {
656                 return;
657         }
658
659         double value = cui->adjustment->get_value();
660
661         if (cui->logarithmic) {
662                 value = exp(value);
663         }
664
665         insert.set_parameter (cui->port_index, (float) value);
666 }
667
668 void
669 PluginUI::parameter_changed (uint32_t abs_port_id, float val, ControlUI* cui)
670 {
671         if (cui->port_index == abs_port_id) {
672                 if (!cui->update_pending) {
673                         cui->update_pending = true;
674                         Gtkmm2ext::UI::instance()->call_slot (bind (mem_fun(*this, &PluginUI::update_control_display), cui));
675                 }
676         }
677 }
678
679 void
680 PluginUI::update_control_display (ControlUI* cui)       
681 {
682         /* XXX how do we handle logarithmic stuff here ? */
683         
684         cui->update_pending = false;
685
686         float val = plugin.get_parameter (cui->port_index);
687
688         cui->ignore_change++;
689         if (cui->combo) {
690                 std::map<string,float>::iterator it;
691                 for (it = cui->combo_map->begin(); it != cui->combo_map->end(); ++it) {
692                         if (it->second == val) {
693                                 cui->combo->set_active_text(it->first);
694                                 break;
695                         }
696                 }
697         } else if (cui->adjustment == 0) {
698
699                 if (val > 0.5) {
700                         cui->button->set_active (true);
701                 } else {
702                         cui->button->set_active (false);
703                 }
704
705         } else {
706                 if (cui->logarithmic) {
707                         val = log(val);
708                 }
709                 if (val != cui->adjustment->get_value()) {
710                         cui->adjustment->set_value (val);
711                 }
712         }
713         cui->ignore_change--;
714 }
715
716 void
717 PluginUI::control_port_toggled (ControlUI* cui)
718 {
719         if (!cui->ignore_change) {
720                 insert.set_parameter (cui->port_index, cui->button->get_active());
721         }
722 }
723
724 void
725 PluginUI::control_combo_changed (ControlUI* cui)
726 {
727         if (!cui->ignore_change) {
728                 string value = cui->combo->get_active_text();
729                 std::map<string,float> mapping = *cui->combo_map;
730                 insert.set_parameter (cui->port_index, mapping[value]);
731         }
732
733 }
734
735 void
736 PluginUIWindow::plugin_going_away (ARDOUR::Redirect* ignored)
737 {
738         ENSURE_GUI_THREAD(bind (mem_fun(*this, &PluginUIWindow::plugin_going_away), ignored));
739         
740         _pluginui->stop_updating(0);
741         delete_when_idle (this);
742 }
743
744 void
745 PluginUI::redirect_active_changed (Redirect* r, void* src)
746 {
747         ENSURE_GUI_THREAD(bind (mem_fun(*this, &PluginUI::redirect_active_changed), r, src));
748         
749         bypass_button.set_active (!r->active());
750 }
751
752 bool
753 PluginUI::start_updating (GdkEventAny* ignored)
754 {
755         if (output_controls.size() > 0 ) {
756                 screen_update_connection.disconnect();
757                 screen_update_connection = ARDOUR_UI::instance()->RapidScreenUpdate.connect 
758                         (mem_fun(*this, &PluginUI::output_update));
759         }
760         return false;
761 }
762
763 bool
764 PluginUI::stop_updating (GdkEventAny* ignored)
765 {
766         if (output_controls.size() > 0 ) {
767                 screen_update_connection.disconnect();
768         }
769         return false;
770 }
771
772 void
773 PluginUI::output_update ()
774 {
775         for (vector<ControlUI*>::iterator i = output_controls.begin(); i != output_controls.end(); ++i) {
776                 float val = plugin.get_parameter ((*i)->port_index);
777                 char buf[32];
778                 snprintf (buf, sizeof(buf), "%.2f", val);
779                 (*i)->display_label->set_text (buf);
780
781                 /* autoscaling for the meter */
782                 if ((*i)->meterinfo && (*i)->meterinfo->packed) {
783                         
784                         if (val < (*i)->meterinfo->min) {
785                                 if ((*i)->meterinfo->min_unbound)
786                                         (*i)->meterinfo->min = val;
787                                 else
788                                         val = (*i)->meterinfo->min;
789                         }
790
791                         if (val > (*i)->meterinfo->max) {
792                                 if ((*i)->meterinfo->max_unbound)
793                                         (*i)->meterinfo->max = val;
794                                 else
795                                         val = (*i)->meterinfo->max;
796                         }
797                         
798                         if ((*i)->meterinfo->max > (*i)->meterinfo->min ) {
799                                 float lval = (val - (*i)->meterinfo->min) / ((*i)->meterinfo->max - (*i)->meterinfo->min) ;
800                                 (*i)->meterinfo->meter->set (lval );
801                         }
802                 }
803         }
804 }
805
806 vector<string> 
807 PluginUI::setup_scale_values(guint32 port_index, ControlUI* cui)
808 {
809         vector<string> enums;
810         LadspaPlugin* lp = dynamic_cast<LadspaPlugin*> (&plugin);
811
812         cui->combo_map = new std::map<string, float>;
813         lrdf_defaults* defaults = lrdf_get_scale_values(lp->unique_id(), port_index);
814         if (defaults)   {
815                 for (uint32_t i = 0; i < defaults->count; ++i) {
816                         enums.push_back(defaults->items[i].label);
817                         pair<string, float> newpair;
818                         newpair.first = defaults->items[i].label;
819                         newpair.second = defaults->items[i].value;
820                         cui->combo_map->insert(newpair);
821                 }
822
823                 lrdf_free_setting_values(defaults);
824         }
825
826         return enums;
827 }
828
829 PlugUIBase::PlugUIBase (PluginInsert& pi)
830         : insert (pi),
831           plugin (insert.plugin()),
832           save_button(_("Add")),
833           bypass_button (_("Bypass"))
834 {
835         //combo.set_use_arrows_always(true);
836         set_popdown_strings (combo, plugin.get_presets());
837         combo.set_size_request (100, -1);
838         combo.set_active_text ("");
839         combo.signal_changed().connect(mem_fun(*this, &PlugUIBase::setting_selected));
840
841         save_button.set_name ("PluginSaveButton");
842         save_button.signal_clicked().connect(mem_fun(*this, &PlugUIBase::save_plugin_setting));
843
844         bypass_button.set_name ("PluginBypassButton");
845         bypass_button.signal_toggled().connect (mem_fun(*this, &PlugUIBase::bypass_toggled));
846 }
847
848 void
849 PlugUIBase::setting_selected()
850 {
851         if (combo.get_active_text().length() > 0) {
852                 if (!plugin.load_preset(combo.get_active_text())) {
853                         warning << string_compose(_("Plugin preset %1 not found"), combo.get_active_text()) << endmsg;
854                 }
855         }
856
857 }
858
859 void
860 PlugUIBase::save_plugin_setting ()
861 {
862         ArdourPrompter prompter (true);
863         prompter.set_prompt(_("Name of New Preset:"));
864         prompter.add_button (Gtk::Stock::ADD, Gtk::RESPONSE_ACCEPT);
865         prompter.set_response_sensitive (Gtk::RESPONSE_ACCEPT, false);
866
867         prompter.show_all();
868
869         switch (prompter.run ()) {
870         case Gtk::RESPONSE_ACCEPT:
871
872                 string name;
873
874                 prompter.get_result(name);
875
876                 if (name.length()) {
877                         if(plugin.save_preset(name)){
878                                 set_popdown_strings (combo, plugin.get_presets());
879                                 combo.set_active_text (name);
880                         }
881                 }
882                 break;
883         }
884 }
885
886 void
887 PlugUIBase::bypass_toggled ()
888 {
889         bool x;
890
891         if ((x = bypass_button.get_active()) == insert.active()) {
892                 insert.set_active (!x, this);
893         }
894 }
895