option editor and some prompters, misc stuff. this commit message contains no apostro...
[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                         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                 add (*pu);
96                 
97                 map_event.connect (mem_fun (*pu, &PluginUI::start_updating));
98                 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           automation_menu (0),
125           is_scrollable(scrollable)
126 {
127         set_name ("PluginEditor");
128         set_border_width (10);
129         set_homogeneous (false);
130
131         settings_box.set_homogeneous (false);
132
133         HBox* constraint_hbox = manage (new HBox);
134         HBox* smaller_hbox = manage (new HBox);
135
136         smaller_hbox->pack_start (combo, false, false);
137         smaller_hbox->pack_start (save_button, false, false);
138
139         constraint_hbox->set_spacing (10);
140         constraint_hbox->pack_start (*smaller_hbox, true, false);
141         constraint_hbox->pack_end (bypass_button, false, false);
142
143 //      name_ebox.add (*text_hbox);
144 //      info_vbox.pack_start (name_ebox, false, false);
145 //      info_vbox.pack_start (makerinfo_label, false, false);
146 //      info_vbox.pack_start (paraminfo_label, false, false, 5);
147
148 //      settings_box.pack_start (info_vbox, false, false, 10);
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                 // this is a hack to get the theme right
167                 name_ebox.set_name ("PluginNameBox");
168         }
169
170         insert.active_changed.connect (mem_fun(*this, &PluginUI::redirect_active_changed));
171         bypass_button.set_active (!insert.active());
172         
173         build (engine);
174 }
175
176 PluginUI::~PluginUI ()
177 {
178         if (output_controls.size() > 0) {
179                 screen_update_connection.disconnect();
180         }
181 }
182
183 void
184 PluginUI::build (AudioEngine &engine)
185
186 {
187         guint32 i = 0;
188         guint32 x = 0;
189         Frame* frame;
190         Frame* bt_frame;
191         VBox* box;
192         char info[32];
193         int output_row, output_col;
194         int button_row, button_col;
195         int output_rows, output_cols;
196         int button_rows, button_cols;
197         guint32 n_ins=0, n_outs = 0;
198
199         prefheight = 30;
200         hpacker.set_spacing (10);
201
202         output_rows = initial_output_rows;
203         output_cols = initial_output_cols;
204         button_rows = initial_button_rows;
205         button_cols = initial_button_cols;
206         output_row = 0;
207         button_row = 0;
208         output_col = 0;
209         button_col = 0;
210
211         button_table.set_homogeneous (false);
212         button_table.set_row_spacings (2);
213         button_table.set_col_spacings (2);
214         output_table.set_homogeneous (true);
215         output_table.set_row_spacings (2);
216         output_table.set_col_spacings (2);
217         button_table.set_border_width (5);
218         output_table.set_border_width (5);
219
220         hpacker.set_border_width (10);
221
222         bt_frame = manage (new Frame);
223         bt_frame->set_name ("BaseFrame");
224         bt_frame->add (button_table);
225         hpacker.pack_start(*bt_frame, true, true);
226
227         box = manage (new VBox);
228         frame = manage (new Frame);
229         frame->set_name ("BaseFrame");
230         frame->add (*box);
231         hpacker.pack_start(*frame, true, true);
232
233         box->set_border_width (5);
234         box->set_spacing (1);
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::Combo;
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_unmap_event().connect( 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->get_spin_button().signal_focus_in_event().connect (mem_fun(*this, &PluginUI::entry_focus_event));
554                         control_ui->control->get_spin_button().signal_focus_out_event().connect (mem_fun(*this, &PluginUI::entry_focus_event));
555
556                         control_ui->control->StartGesture.connect (bind (mem_fun(*this, &PluginUI::start_touch), control_ui));
557                         control_ui->control->StopGesture.connect (bind (mem_fun(*this, &PluginUI::stop_touch), control_ui));
558                         
559                 }
560
561                 if (control_ui->logarithmic) {
562                         control_ui->adjustment->set_value(log(plugin.get_parameter(port_index)));
563                 } else{
564                         control_ui->adjustment->set_value(plugin.get_parameter(port_index));
565                 }
566
567                 /* XXX memory leak: SliderController not destroyed by ControlUI
568                    destructor, and manage() reports object hierarchy
569                    ambiguity.
570                 */
571
572                 control_ui->pack_start (control_ui->label, true, true);
573                 if (desc.integer_step) {
574                         control_ui->pack_start (*control_ui->clickbox, false, false);
575                 } else {
576                         control_ui->pack_start (*control_ui->control, false, false);
577                 }
578
579                 control_ui->pack_start (control_ui->automate_button, false, false);
580 control_ui->adjustment->signal_value_changed().connect (bind (mem_fun(*this, &PluginUI::control_adjustment_changed), control_ui));
581                 control_ui->automate_button.signal_clicked().connect (bind (mem_fun(*this, &PluginUI::astate_clicked), control_ui, (uint32_t) port_index));
582
583                 automation_state_changed (control_ui);
584
585                 plugin.ParameterChanged.connect (bind (mem_fun(*this, &PluginUI::parameter_changed), control_ui));
586                 insert.automation_list (port_index).automation_state_changed.connect 
587                         (bind (mem_fun(*this, &PluginUI::automation_state_changed), control_ui));
588
589         } else if (plugin.parameter_is_output (port_index)) {
590
591                 control_ui->display = manage (new EventBox);
592                 control_ui->display->set_name ("ParameterValueDisplay");
593
594                 control_ui->display_label = manage (new Label);
595                 control_ui->display_label->set_name ("ParameterValueDisplay");
596
597                 control_ui->display->add (*control_ui->display_label);
598                 Gtkmm2ext::set_size_request_to_display_given_text (*control_ui->display, "g999999", 2, 2);
599
600                 control_ui->display->show_all ();
601
602                 /* set up a meter */
603                 /* TODO: only make a meter if the port is Hinted for it */
604
605                 MeterInfo * info = new MeterInfo(port_index);
606                 control_ui->meterinfo = info;
607                 
608                 info->meter = new FastMeter (100, 5, FastMeter::Horizontal);
609
610                 info->min_unbound = desc.min_unbound;
611                 info->max_unbound = desc.max_unbound;
612
613                 info->min = desc.lower;
614                 info->max = desc.upper;
615
616                 control_ui->vbox = manage (new VBox);
617
618                 control_ui->vbox->pack_start (control_ui->label, false, false);
619                 control_ui->vbox->pack_start (*info->meter, false, false);
620                 
621                 control_ui->pack_start (*control_ui->vbox, false, false);
622                 control_ui->pack_start (*control_ui->display, false, false);
623
624                 control_ui->meterinfo->meter->show_all();
625                 control_ui->meterinfo->packed = true;
626                 
627                 output_controls.push_back (control_ui);
628         }
629         
630         plugin.ParameterChanged.connect (bind (mem_fun(*this, &PluginUI::parameter_changed), control_ui));
631         return control_ui;
632 }
633
634 void
635 PluginUI::start_touch (PluginUI::ControlUI* cui)
636 {
637         insert.automation_list (cui->port_index).start_touch ();
638 }
639
640 void
641 PluginUI::stop_touch (PluginUI::ControlUI* cui)
642 {
643         insert.automation_list (cui->port_index).stop_touch ();
644 }
645
646 void
647 PluginUI::astate_clicked (ControlUI* cui, uint32_t port)
648 {
649         using namespace Menu_Helpers;
650
651         if (automation_menu == 0) {
652                 automation_menu = manage (new Menu);
653                 automation_menu->set_name ("ArdourContextMenu");
654         } 
655
656         MenuList& items (automation_menu->items());
657
658         items.clear ();
659         items.push_back (MenuElem (_("off"), 
660                                    bind (mem_fun(*this, &PluginUI::set_automation_state), (AutoState) Off, cui)));
661         items.push_back (MenuElem (_("play"),
662                                    bind (mem_fun(*this, &PluginUI::set_automation_state), (AutoState) Play, cui)));
663         items.push_back (MenuElem (_("write"),
664                                    bind (mem_fun(*this, &PluginUI::set_automation_state), (AutoState) Write, cui)));
665         items.push_back (MenuElem (_("touch"),
666                                    bind (mem_fun(*this, &PluginUI::set_automation_state), (AutoState) Touch, cui)));
667
668         automation_menu->popup (1, 0);
669 }
670
671 void
672 PluginUI::set_automation_state (AutoState state, ControlUI* cui)
673 {
674         insert.set_port_automation_state (cui->port_index, state);
675 }
676
677 void
678 PluginUI::control_adjustment_changed (ControlUI* cui)
679 {
680         if (cui->ignore_change) {
681                 return;
682         }
683
684         double value = cui->adjustment->get_value();
685
686         if (cui->logarithmic) {
687                 value = exp(value);
688         }
689
690         insert.set_parameter (cui->port_index, (float) value);
691 }
692
693 void
694 PluginUI::parameter_changed (uint32_t abs_port_id, float val, ControlUI* cui)
695 {
696         if (cui->port_index == abs_port_id) {
697                 if (!cui->update_pending) {
698                         cui->update_pending = true;
699                         Gtkmm2ext::UI::instance()->call_slot (bind (mem_fun(*this, &PluginUI::update_control_display), cui));
700                 }
701         }
702 }
703
704 void
705 PluginUI::update_control_display (ControlUI* cui)       
706 {
707         /* XXX how do we handle logarithmic stuff here ? */
708         
709         cui->update_pending = false;
710
711         float val = plugin.get_parameter (cui->port_index);
712
713         cui->ignore_change++;
714         if (cui->combo) {
715                 std::map<string,float>::iterator it;
716                 for (it = cui->combo_map->begin(); it != cui->combo_map->end(); ++it) {
717                         if (it->second == val) {
718                                 cui->combo->set_active_text(it->first);
719                                 break;
720                         }
721                 }
722         } else if (cui->adjustment == 0) {
723
724                 if (val > 0.5) {
725                         cui->button->set_active (true);
726                 } else {
727                         cui->button->set_active (false);
728                 }
729
730         } else {
731                 if (cui->logarithmic) {
732                         val = log(val);
733                 }
734                 if (val != cui->adjustment->get_value()) {
735                         cui->adjustment->set_value (val);
736                 }
737         }
738         cui->ignore_change--;
739 }
740
741 void
742 PluginUI::control_port_toggled (ControlUI* cui)
743 {
744         if (!cui->ignore_change) {
745                 insert.set_parameter (cui->port_index, cui->button->get_active());
746         }
747 }
748
749 gint
750 PluginUI::control_combo_changed (GdkEventAny* ignored, ControlUI* cui)
751 {
752         if (!cui->ignore_change) {
753                 string value = cui->combo->get_active_text();
754                 std::map<string,float> mapping = *cui->combo_map;
755                 insert.set_parameter (cui->port_index, mapping[value]);
756         }
757
758         return FALSE;
759 }
760
761 void
762 PluginUIWindow::plugin_going_away (ARDOUR::Redirect* ignored)
763 {
764         ENSURE_GUI_THREAD(bind (mem_fun(*this, &PluginUIWindow::plugin_going_away), ignored));
765         
766         _pluginui->stop_updating(0);
767         delete_when_idle (this);
768 }
769
770 gint
771 PluginUI::entry_focus_event (GdkEventFocus* ev)
772 {
773         if (ev->in) {
774                 ARDOUR_UI::instance()->allow_focus (true);
775         } else {
776                 ARDOUR_UI::instance()->allow_focus (false);
777         }
778         return TRUE;
779 }
780
781 void
782 PluginUI::redirect_active_changed (Redirect* r, void* src)
783 {
784         ENSURE_GUI_THREAD(bind (mem_fun(*this, &PluginUI::redirect_active_changed), r, src));
785         
786         bypass_button.set_active (!r->active());
787 }
788
789 gint
790 PluginUI::start_updating (GdkEventAny *ev)
791 {
792         if (output_controls.size() > 0 ) {
793                 screen_update_connection.disconnect();
794                 screen_update_connection = ARDOUR_UI::instance()->RapidScreenUpdate.connect 
795                         (mem_fun(*this, &PluginUI::output_update));
796         }
797         return FALSE;
798 }
799
800 gint 
801 PluginUI::stop_updating (GdkEventAny *ev)
802 {
803         if (output_controls.size() > 0 ) {
804                 screen_update_connection.disconnect();
805         }
806         return FALSE;
807 }
808
809 void
810 PluginUI::output_update ()
811 {
812         for (vector<ControlUI*>::iterator i = output_controls.begin(); i != output_controls.end(); ++i) {
813                 float val = plugin.get_parameter ((*i)->port_index);
814                 char buf[32];
815                 snprintf (buf, sizeof(buf), "%.2f", val);
816                 (*i)->display_label->set_text (buf);
817
818                 /* autoscaling for the meter */
819                 if ((*i)->meterinfo && (*i)->meterinfo->packed) {
820                         
821                         if (val < (*i)->meterinfo->min) {
822                                 if ((*i)->meterinfo->min_unbound)
823                                         (*i)->meterinfo->min = val;
824                                 else
825                                         val = (*i)->meterinfo->min;
826                         }
827
828                         if (val > (*i)->meterinfo->max) {
829                                 if ((*i)->meterinfo->max_unbound)
830                                         (*i)->meterinfo->max = val;
831                                 else
832                                         val = (*i)->meterinfo->max;
833                         }
834                         
835                         if ((*i)->meterinfo->max > (*i)->meterinfo->min ) {
836                                 float lval = (val - (*i)->meterinfo->min) / ((*i)->meterinfo->max - (*i)->meterinfo->min) ;
837                                 (*i)->meterinfo->meter->set (lval );
838                         }
839                 }
840         }
841 }
842
843 list<string> 
844 PluginUI::setup_scale_values(guint32 port_index, ControlUI* cui)
845 {
846         list<string> enums;
847         LadspaPlugin* lp = dynamic_cast<LadspaPlugin*> (&plugin);
848
849         cui->combo_map = new std::map<string, float>;
850         lrdf_defaults* defaults = lrdf_get_scale_values(lp->unique_id(), port_index);
851         if (defaults)   {
852                 for (uint32_t i = 0; i < defaults->count; ++i) {
853                         enums.push_back(defaults->items[i].label);
854                         pair<string, float> newpair;
855                         newpair.first = defaults->items[i].label;
856                         newpair.second = defaults->items[i].value;
857                         cui->combo_map->insert(newpair);
858                 }
859
860                 lrdf_free_setting_values(defaults);
861         }
862
863         return enums;
864 }
865
866 PlugUIBase::PlugUIBase (PluginInsert& pi)
867         : insert (pi),
868           plugin (insert.plugin()),
869           save_button(_("save")),
870           bypass_button (_("bypass"))
871 {
872         combo.set_value_in_list(true,true);
873         combo.set_use_arrows(true);
874         combo.set_use_arrows_always(true);
875         set_popdown_strings (combo, plugin.get_presets());
876         combo.set_active_text ("");
877         combo.signal_unmap_event().connect(mem_fun(*this, &PlugUIBase::setting_selected));
878
879         save_button.set_name ("PluginSaveButton");
880         save_button.signal_clicked().connect(mem_fun(*this, &PlugUIBase::save_plugin_setting));
881
882         bypass_button.set_name ("PluginBypassButton");
883         bypass_button.signal_toggled().connect (mem_fun(*this, &PlugUIBase::bypass_toggled));
884 }
885
886 gint
887 PlugUIBase::setting_selected(GdkEventAny* ignored)
888 {
889         if (combo.get_active_text().length() > 0) {
890                 if (!plugin.load_preset(combo.get_active_text())) {
891                         warning << string_compose(_("Plugin preset %1 not found"), combo.get_active_text()) << endmsg;
892                 }
893         }
894
895         return FALSE;
896 }
897
898 void
899 PlugUIBase::save_plugin_setting ()
900 {
901         ArdourPrompter prompter (true);
902         prompter.set_prompt(_("Name for plugin settings:"));
903
904         prompter.show_all();
905
906         switch (prompter.run ()) {
907         case GTK_RESPONSE_ACCEPT:
908
909                 string name;
910
911                 prompter.get_result(name);
912
913                 if (name.length()) {
914                         if(plugin.save_preset(name)){
915                                 set_popdown_strings (combo, plugin.get_presets());
916                                 combo.set_active_text (name);
917                         }
918                 }
919                 break;
920         }
921 }
922
923 void
924 PlugUIBase::bypass_toggled ()
925 {
926         bool x;
927
928         if ((x = bypass_button.get_active()) == insert.active()) {
929                 insert.set_active (!x, this);
930         }
931 }
932