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