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