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