Make EQ Gui optional and seize updating the graph when the analysis is not visible...
[ardour.git] / gtk2_ardour / generic_pluginui.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 */
19
20 #include <climits>
21 #include <cerrno>
22 #include <cmath>
23 #include <string>
24
25 #include <pbd/stl_delete.h>
26 #include <pbd/xml++.h>
27 #include <pbd/failed_constructor.h>
28
29 #include <gtkmm2ext/click_box.h>
30 #include <gtkmm2ext/fastmeter.h>
31 #include <gtkmm2ext/barcontroller.h>
32 #include <gtkmm2ext/utils.h>
33 #include <gtkmm2ext/doi.h>
34 #include <gtkmm2ext/slider_controller.h>
35
36 #include <midi++/manager.h>
37
38 #include <ardour/plugin.h>
39 #include <ardour/plugin_insert.h>
40 #include <ardour/ladspa_plugin.h>
41 #ifdef HAVE_LV2
42 #include <ardour/lv2_plugin.h>
43 #endif
44
45 #include <lrdf.h>
46
47 #include "ardour_ui.h"
48 #include "prompter.h"
49 #include "plugin_ui.h"
50 #include "utils.h"
51 #include "gui_thread.h"
52 #include "automation_controller.h"
53 #include "plugin_eq_gui.h"
54
55 #include "i18n.h"
56
57 using namespace std;
58 using namespace ARDOUR;
59 using namespace PBD;
60 using namespace Gtkmm2ext;
61 using namespace Gtk;
62 using namespace sigc;
63
64 GenericPluginUI::GenericPluginUI (boost::shared_ptr<PluginInsert> pi, bool scrollable)
65         : PlugUIBase (pi),
66           button_table (initial_button_rows, initial_button_cols),
67           output_table (initial_output_rows, initial_output_cols),
68           eqgui_toggle(_("Freq Analysis")),
69           hAdjustment(0.0, 0.0, 0.0),
70           vAdjustment(0.0, 0.0, 0.0),
71           scroller_view(hAdjustment, vAdjustment),
72           automation_menu (0),
73           is_scrollable(scrollable)
74 {
75         set_name ("PluginEditor");
76         set_border_width (10);
77         //set_homogeneous (false);
78
79         pack1(main_contents);
80
81         settings_box.set_homogeneous (false);
82
83         HBox* constraint_hbox = manage (new HBox);
84         HBox* smaller_hbox = manage (new HBox);
85         Label* combo_label = manage (new Label (_("<span size=\"large\">Presets</span>")));
86         combo_label->set_use_markup (true);
87
88         Label* latency_label = manage (new Label (_("<span size=\"large\">Latency</span>")));
89         latency_label->set_use_markup (true);
90
91         smaller_hbox->pack_start (*latency_label, false, false, 10);
92         smaller_hbox->pack_start (latency_gui, false, false, 10);
93         smaller_hbox->pack_start (preset_combo, false, false);
94         smaller_hbox->pack_start (save_button, false, false);
95
96         constraint_hbox->set_spacing (5);
97         constraint_hbox->pack_start (*smaller_hbox, true, false);
98         constraint_hbox->pack_end (bypass_button, false, false);
99         constraint_hbox->pack_start (eqgui_toggle, false, false);
100
101         settings_box.pack_end (*constraint_hbox, false, false);
102
103         main_contents.pack_start (settings_box, false, false);
104
105         if ( is_scrollable ) {
106                 scroller.set_policy (Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
107                 scroller.set_name ("PluginEditor");
108                 scroller_view.set_name("PluginEditor");
109                 scroller_view.add (hpacker);
110                 scroller.add (scroller_view);
111                 
112                 main_contents.pack_start (scroller, true, true);
113
114         }
115         else {
116                 main_contents.pack_start (hpacker, false, false);
117         }
118
119         eqgui_toggle.set_active (false);
120         eqgui_toggle.signal_toggled().connect( mem_fun(*this, &GenericPluginUI::toggle_plugin_analysis));
121
122         pi->ActiveChanged.connect (bind(mem_fun(*this, &GenericPluginUI::processor_active_changed),
123                                 boost::weak_ptr<Processor>(pi)));
124         bypass_button.set_active (!pi->active());
125         
126         build ();
127 }
128
129 GenericPluginUI::~GenericPluginUI ()
130 {
131         if (output_controls.size() > 0) {
132                 screen_update_connection.disconnect();
133         }
134 }
135
136 void
137 GenericPluginUI::build ()
138
139 {
140         guint32 i = 0;
141         guint32 x = 0;
142         Frame* frame;
143         Frame* bt_frame;
144         VBox* box;
145         int output_row, output_col;
146         int button_row, button_col;
147         int output_rows, output_cols;
148         int button_rows, button_cols;
149
150         prefheight = 30;
151         hpacker.set_spacing (10);
152
153         output_rows = initial_output_rows;
154         output_cols = initial_output_cols;
155         button_rows = initial_button_rows;
156         button_cols = initial_button_cols;
157         output_row = 0;
158         button_row = 0;
159         output_col = 0;
160         button_col = 0;
161
162         button_table.set_homogeneous (false);
163         button_table.set_row_spacings (2);
164         button_table.set_col_spacings (2);
165         output_table.set_homogeneous (true);
166         output_table.set_row_spacings (2);
167         output_table.set_col_spacings (2);
168         button_table.set_border_width (5);
169         output_table.set_border_width (5);
170
171         hpacker.set_border_width (10);
172
173         bt_frame = manage (new Frame);
174         bt_frame->set_name ("BaseFrame");
175         bt_frame->add (button_table);
176         hpacker.pack_start(*bt_frame, true, true);
177
178         box = manage (new VBox);
179         box->set_border_width (5);
180         box->set_spacing (1);
181
182         frame = manage (new Frame);
183         frame->set_name ("BaseFrame");
184         frame->set_label (_("Controls"));
185         frame->add (*box);
186         hpacker.pack_start(*frame, true, true);
187
188         /* find all ports. build control elements for all appropriate control ports */
189
190         for (i = 0; i < plugin->parameter_count(); ++i) {
191
192                 if (plugin->parameter_is_control (i)) {
193                         
194                         /* Don't show latency control ports */
195
196                         if (plugin->describe_parameter (Evoral::Parameter(PluginAutomation, 0, i)) == X_("latency")) {
197                                 continue;
198                         }
199
200                         ControlUI* cui;
201         
202                         /* if we are scrollable, just use one long column */
203
204                         if (!is_scrollable) {
205                                 if (x++ > 7){
206                                         frame = manage (new Frame);
207                                         frame->set_name ("BaseFrame");
208                                         box = manage (new VBox);
209                                         
210                                         box->set_border_width (5);
211                                         box->set_spacing (1);
212
213                                         frame->add (*box);
214                                         hpacker.pack_start(*frame,true,true);
215
216                                         x = 1;
217                                 }
218                         }
219
220                         boost::shared_ptr<ARDOUR::AutomationControl> c
221                                 = boost::dynamic_pointer_cast<ARDOUR::AutomationControl>(
222                                         insert->data().control(Evoral::Parameter(PluginAutomation, 0, i)));
223
224                         if ((cui = build_control_ui (i, c)) == 0) {
225                                 error << string_compose(_("Plugin Editor: could not build control element for port %1"), i) << endmsg;
226                                 continue;
227                         }
228                                 
229                         if (cui->controller || cui->clickbox || cui->combo) {
230
231                                 box->pack_start (*cui, false, false);
232
233                         } else if (cui->button) {
234
235                                 if (button_row == button_rows) {
236                                         button_row = 0;
237                                         if (++button_col == button_cols) {
238                                                 button_cols += 2;
239                                                 button_table.resize (button_rows, button_cols);
240                                         }
241                                 }
242
243                                 button_table.attach (*cui, button_col, button_col + 1, button_row, button_row+1, 
244                                                      FILL|EXPAND, FILL);
245                                 button_row++;
246
247                         } else if (cui->display) {
248
249                                 output_table.attach (*cui, output_col, output_col + 1, output_row, output_row+1, 
250                                                      FILL|EXPAND, FILL);
251                                 
252                                 // TODO: The meters should be divided into multiple rows 
253                                 
254                                 if (++output_col == output_cols) {
255                                         output_cols ++;
256                                         output_table.resize (output_rows, output_cols);
257                                 }
258                                 
259                                 /* old code, which divides meters into
260                                  * columns first, rows later. New code divides into one row
261                                  
262                                 if (output_row == output_rows) {
263                                         output_row = 0;
264                                         if (++output_col == output_cols) {
265                                                 output_cols += 2;
266                                                 output_table.resize (output_rows, output_cols);
267                                         }
268                                 }
269                                 
270                                 output_table.attach (*cui, output_col, output_col + 1, output_row, output_row+1, 
271                                                      FILL|EXPAND, FILL);
272  
273                                 output_row++;
274                                 */
275                         }
276                                 
277                         /* HACK: ideally the preferred height would be queried from
278                            the complete hpacker, but I can't seem to get that
279                            information in time, so this is an estimation 
280                         */
281
282                         prefheight += 30;
283
284                 } 
285         }
286
287         if (box->children().empty()) {
288                 hpacker.remove (*frame);
289         }
290
291         if (button_table.children().empty()) {
292                 hpacker.remove (*bt_frame);
293         }
294
295         if (!output_table.children().empty()) {
296                 frame = manage (new Frame);
297                 frame->set_name ("BaseFrame");
298                 frame->add (output_table);
299                 hpacker.pack_end (*frame, true, true);
300         }
301
302         output_update ();
303
304         output_table.show_all ();
305         button_table.show_all ();
306 }
307
308 GenericPluginUI::ControlUI::ControlUI ()
309         : automate_button (X_("")) // force creation of a label 
310 {
311         automate_button.set_name ("PluginAutomateButton");
312         ARDOUR_UI::instance()->tooltips().set_tip (automate_button, _("Automation control"));
313
314         /* XXX translators: use a string here that will be at least as long
315            as the longest automation label (see ::automation_state_changed()
316            below). be sure to include a descender.
317         */
318
319         set_size_request_to_display_given_text (*automate_button.get_child(), _("Mgnual"), 5, 5);
320
321         ignore_change = 0;
322         display = 0;
323         button = 0;
324         clickbox = 0;
325         meterinfo = 0;
326 }
327
328 GenericPluginUI::ControlUI::~ControlUI() 
329 {
330         if (meterinfo) {
331                 delete meterinfo->meter;
332                 delete meterinfo;
333         }
334 }
335
336 void
337 GenericPluginUI::automation_state_changed (ControlUI* cui)
338 {
339         /* update button label */
340
341         // don't lock to avoid deadlock because we're triggered by
342         // AutomationControl::Changed() while the automation lock is taken
343         switch (insert->get_parameter_automation_state (cui->parameter(), false)
344                         & (Off|Play|Touch|Write)) {
345         case Off:
346                 cui->automate_button.set_label (_("Manual"));
347                 break;
348         case Play:
349                 cui->automate_button.set_label (_("Play"));
350                 break;
351         case Write:
352                 cui->automate_button.set_label (_("Write"));
353                 break;
354         case Touch:
355                 cui->automate_button.set_label (_("Touch"));
356                 break;
357         default:
358                 cui->automate_button.set_label (_("???"));
359                 break;
360         }
361 }
362
363
364 static void integer_printer (char buf[32], Adjustment &adj, void *arg)
365 {
366         snprintf (buf, 32, "%.0f", adj.get_value());
367 }
368
369 void
370 GenericPluginUI::print_parameter (char *buf, uint32_t len, uint32_t param)
371 {
372         plugin->print_parameter (param, buf, len);
373 }
374
375 GenericPluginUI::ControlUI*
376 GenericPluginUI::build_control_ui (guint32 port_index, boost::shared_ptr<AutomationControl> mcontrol)
377 {
378         ControlUI* control_ui = NULL;
379         if (!mcontrol)
380                 return control_ui;
381
382         Plugin::ParameterDescriptor desc;
383
384         plugin->get_parameter_descriptor (port_index, desc);
385
386         control_ui = manage (new ControlUI ());
387         control_ui->combo = 0;
388         control_ui->combo_map = 0;
389         control_ui->control = mcontrol;
390         control_ui->update_pending = false;
391         control_ui->label.set_text (desc.label);
392         control_ui->label.set_alignment (0.0, 0.5);
393         control_ui->label.set_name ("PluginParameterLabel");
394
395         control_ui->set_spacing (5);
396
397         Gtk::Requisition req (control_ui->automate_button.size_request());
398
399         if (plugin->parameter_is_input (port_index)) {
400
401                 boost::shared_ptr<LadspaPlugin> lp;
402 #ifdef HAVE_LV2
403                 boost::shared_ptr<LV2Plugin> lv2p;
404 #endif
405                 if ((lp = boost::dynamic_pointer_cast<LadspaPlugin>(plugin)) != 0) {
406                         
407                         // FIXME: not all plugins have a numeric unique ID
408                         uint32_t id = atol (lp->unique_id().c_str());
409                         lrdf_defaults* defaults = lrdf_get_scale_values(id, port_index);
410                         
411                         if (defaults && defaults->count > 0)    {
412                                 
413                                 control_ui->combo = new Gtk::ComboBoxText;
414                                 //control_ui->combo->set_value_in_list(true, false);
415                                 set_popdown_strings (*control_ui->combo, setup_scale_values(port_index, control_ui));
416                                 control_ui->combo->signal_changed().connect (bind (mem_fun(*this, &GenericPluginUI::control_combo_changed), control_ui));
417                                 mcontrol->Changed.connect (bind (mem_fun (*this, &GenericPluginUI::parameter_changed), control_ui));
418                                 control_ui->pack_start(control_ui->label, true, true);
419                                 control_ui->pack_start(*control_ui->combo, false, true);
420                                 
421                                 update_control_display(control_ui);
422                                 
423                                 lrdf_free_setting_values(defaults);
424                                 return control_ui;
425                         }
426
427 #ifdef HAVE_LV2
428                 } else if ((lv2p = boost::dynamic_pointer_cast<LV2Plugin>(plugin)) != 0) {
429
430                         SLV2Port port = lv2p->slv2_port(port_index);
431                         SLV2ScalePoints points = slv2_port_get_scale_points(lv2p->slv2_plugin(), port);
432
433                         if (points) {
434                                 control_ui->combo = new Gtk::ComboBoxText;
435                                 //control_ui->combo->set_value_in_list(true, false);
436                                 set_popdown_strings (*control_ui->combo, setup_scale_values(port_index, control_ui));
437                                 control_ui->combo->signal_changed().connect (bind (mem_fun(*this, &GenericPluginUI::control_combo_changed), control_ui));
438                                 mcontrol->Changed.connect (bind (mem_fun (*this, &GenericPluginUI::parameter_changed), control_ui));
439                                 control_ui->pack_start(control_ui->label, true, true);
440                                 control_ui->pack_start(*control_ui->combo, false, true);
441                                 
442                                 update_control_display(control_ui);
443                                 
444                                 slv2_scale_points_free(points);
445                                 return control_ui;
446                         }
447 #endif
448                 }
449                         
450                 if (desc.toggled) {
451
452                         /* Build a button */
453                 
454                         control_ui->button = manage (new ToggleButton ());
455                         control_ui->button->set_name ("PluginEditorButton");
456                         control_ui->button->set_size_request (20, 20);
457
458                         control_ui->pack_start (control_ui->label, true, true);
459                         control_ui->pack_start (*control_ui->button, false, true);
460                         control_ui->pack_start (control_ui->automate_button, false, false);
461
462                         control_ui->button->signal_clicked().connect (bind (mem_fun(*this, &GenericPluginUI::control_port_toggled), control_ui));
463                 
464                         if(plugin->get_parameter (port_index) == 1){
465                                 control_ui->button->set_active(true);
466                         }
467
468                         return control_ui;
469                 }
470
471                 /* create the controller */
472
473                 control_ui->controller = AutomationController::create(insert, mcontrol->parameter(), mcontrol);
474                 /* XXX this code is not right yet, because it doesn't handle
475                    the absence of bounds in any sensible fashion.
476                 */
477
478 //#if 0
479                 control_ui->controller->adjustment()->set_lower (desc.lower);
480                 control_ui->controller->adjustment()->set_upper (desc.upper);
481
482                 control_ui->logarithmic = false; // just disable it for now
483                 /*
484                 control_ui->logarithmic = desc.logarithmic;
485                 if (control_ui->logarithmic) {
486                         if (control_ui->controller->adjustment()->get_lower() == 0.0) {
487                                 control_ui->controller->adjustment()->set_lower (control_ui->controller->adjustment()->get_upper()/10000);
488                         }
489                         control_ui->controller->adjustment()->set_upper (log(control_ui->controller->adjustment()->get_upper()));
490                         control_ui->controller->adjustment()->set_lower (log(control_ui->controller->adjustment()->get_lower()));
491                 }*/
492                 
493         
494                 float delta = desc.upper - desc.lower;
495
496                 control_ui->controller->adjustment()->set_page_size (delta/100.0);
497                 control_ui->controller->adjustment()->set_step_increment (desc.step);
498                 control_ui->controller->adjustment()->set_page_increment (desc.largestep);
499 //#endif
500
501                 if (desc.integer_step) {
502                         control_ui->clickbox = new ClickBox (control_ui->controller->adjustment(), "PluginUIClickBox");
503                         Gtkmm2ext::set_size_request_to_display_given_text (*control_ui->clickbox, "g9999999", 2, 2);
504                         control_ui->clickbox->set_print_func (integer_printer, 0);
505                 } else {
506                         //sigc::slot<void,char*,uint32_t> pslot = sigc::bind (mem_fun(*this, &GenericPluginUI::print_parameter), (uint32_t) port_index);
507
508                         control_ui->controller->set_size_request (200, req.height);
509                         control_ui->controller->set_name (X_("PluginSlider"));
510                         control_ui->controller->set_style (BarController::LeftToRight);
511                         control_ui->controller->set_use_parent (true);
512
513                         control_ui->controller->StartGesture.connect (bind (mem_fun(*this, &GenericPluginUI::start_touch), control_ui));
514                         control_ui->controller->StopGesture.connect (bind (mem_fun(*this, &GenericPluginUI::stop_touch), control_ui));
515                         
516                 }
517
518                 if (control_ui->logarithmic) {
519                         control_ui->controller->adjustment()->set_value(log(plugin->get_parameter(port_index)));
520                 } else{
521                         control_ui->controller->adjustment()->set_value(plugin->get_parameter(port_index));
522                 }
523
524                 /* XXX memory leak: SliderController not destroyed by ControlUI
525                    destructor, and manage() reports object hierarchy
526                    ambiguity.
527                 */
528
529                 control_ui->pack_start (control_ui->label, true, true);
530                 if (desc.integer_step) {
531                         control_ui->pack_start (*control_ui->clickbox, false, false);
532                 } else {
533                         control_ui->pack_start (*control_ui->controller, false, false);
534                 }
535
536                 control_ui->pack_start (control_ui->automate_button, false, false);
537                 control_ui->automate_button.signal_clicked().connect (bind (mem_fun(*this, &GenericPluginUI::astate_clicked), control_ui, (uint32_t) port_index));
538
539                 automation_state_changed (control_ui);
540
541                 mcontrol->Changed.connect (bind (mem_fun (*this, &GenericPluginUI::parameter_changed), control_ui));
542                 mcontrol->alist()->automation_state_changed.connect 
543                         (bind (mem_fun(*this, &GenericPluginUI::automation_state_changed), control_ui));
544
545         } else if (plugin->parameter_is_output (port_index)) {
546
547                 control_ui->display = manage (new EventBox);
548                 control_ui->display->set_name ("ParameterValueDisplay");
549
550                 control_ui->display_label = manage (new Label);
551
552                 control_ui->display_label->set_name ("ParameterValueDisplay");
553
554                 control_ui->display->add (*control_ui->display_label);
555                 Gtkmm2ext::set_size_request_to_display_given_text (*control_ui->display, "-99,99", 2, 2);
556
557                 control_ui->display->show_all ();
558
559                 /* set up a meter */
560                 /* TODO: only make a meter if the port is Hinted for it */
561
562                 MeterInfo * info = new MeterInfo(port_index);
563                 control_ui->meterinfo = info;
564                 
565                 info->meter = new FastMeter (5, 5, FastMeter::Vertical);
566
567                 info->min_unbound = desc.min_unbound;
568                 info->max_unbound = desc.max_unbound;
569
570                 info->min = desc.lower;
571                 info->max = desc.upper;
572
573                 control_ui->vbox = manage (new VBox);
574                 control_ui->hbox = manage (new HBox);
575                 
576                 control_ui->label.set_angle(90);
577                 control_ui->hbox->pack_start (control_ui->label, false, false);
578                 control_ui->hbox->pack_start (*info->meter, false, false);
579
580                 control_ui->vbox->pack_start (*control_ui->hbox, false, false);
581                 
582                 control_ui->vbox->pack_start (*control_ui->display, false, false);
583
584                 control_ui->pack_start (*control_ui->vbox);
585
586                 control_ui->meterinfo->meter->show_all();
587                 control_ui->meterinfo->packed = true;
588                 
589                 output_controls.push_back (control_ui);
590         }
591         
592         mcontrol->Changed.connect (bind (mem_fun (*this, &GenericPluginUI::parameter_changed), control_ui));
593         
594         return control_ui;
595 }
596
597 void
598 GenericPluginUI::start_touch (GenericPluginUI::ControlUI* cui)
599 {
600         cui->control->start_touch ();
601 }
602
603 void
604 GenericPluginUI::stop_touch (GenericPluginUI::ControlUI* cui)
605 {
606         cui->control->stop_touch ();
607 }
608
609 void
610 GenericPluginUI::astate_clicked (ControlUI* cui, uint32_t port)
611 {
612         using namespace Menu_Helpers;
613
614         if (automation_menu == 0) {
615                 automation_menu = manage (new Menu);
616                 automation_menu->set_name ("ArdourContextMenu");
617         } 
618
619         MenuList& items (automation_menu->items());
620
621         items.clear ();
622         items.push_back (MenuElem (_("Manual"), 
623                                    bind (mem_fun(*this, &GenericPluginUI::set_automation_state), (AutoState) Off, cui)));
624         items.push_back (MenuElem (_("Play"),
625                                    bind (mem_fun(*this, &GenericPluginUI::set_automation_state), (AutoState) Play, cui)));
626         items.push_back (MenuElem (_("Write"),
627                                    bind (mem_fun(*this, &GenericPluginUI::set_automation_state), (AutoState) Write, cui)));
628         items.push_back (MenuElem (_("Touch"),
629                                    bind (mem_fun(*this, &GenericPluginUI::set_automation_state), (AutoState) Touch, cui)));
630
631         automation_menu->popup (1, gtk_get_current_event_time());
632 }
633
634 void
635 GenericPluginUI::set_automation_state (AutoState state, ControlUI* cui)
636 {
637         insert->set_parameter_automation_state (cui->parameter(), state);
638 }
639
640 void
641 GenericPluginUI::parameter_changed (ControlUI* cui)
642 {
643         if (!cui->update_pending) {
644                 cui->update_pending = true;
645                 Gtkmm2ext::UI::instance()->call_slot (bind (mem_fun(*this, &GenericPluginUI::update_control_display), cui));
646         }
647 }
648
649 void
650 GenericPluginUI::update_control_display (ControlUI* cui)        
651 {
652         /* XXX how do we handle logarithmic stuff here ? */
653         
654         cui->update_pending = false;
655
656         float val = cui->control->get_value();
657
658         cui->ignore_change++;
659         if (cui->combo) {
660                 std::map<string,float>::iterator it;
661                 for (it = cui->combo_map->begin(); it != cui->combo_map->end(); ++it) {
662                         if (it->second == val) {
663                                 cui->combo->set_active_text(it->first);
664                                 break;
665                         }
666                 }
667         } else if (cui->button) {
668
669                 if (val > 0.5) {
670                         cui->button->set_active (true);
671                 } else {
672                         cui->button->set_active (false);
673                 }
674         }
675
676         if( cui->controller ) {
677             cui->controller->display_effective_value();
678         }
679
680
681         /*} else {
682                 if (cui->logarithmic) {
683                         val = log(val);
684                 }
685                 if (val != cui->adjustment->get_value()) {
686                         cui->adjustment->set_value (val);
687                 }
688         }*/
689         cui->ignore_change--;
690 }
691
692 void
693 GenericPluginUI::control_port_toggled (ControlUI* cui)
694 {
695         if (!cui->ignore_change) {
696                 insert->automation_control(cui->parameter())->set_value(cui->button->get_active());
697         }
698 }
699
700 void
701 GenericPluginUI::control_combo_changed (ControlUI* cui)
702 {
703         if (!cui->ignore_change) {
704                 string value = cui->combo->get_active_text();
705                 std::map<string,float> mapping = *cui->combo_map;
706                 insert->automation_control(cui->parameter())->set_value(mapping[value]);
707         }
708 }
709
710 void
711 GenericPluginUI::processor_active_changed (boost::weak_ptr<Processor> weak_processor)
712 {
713         ENSURE_GUI_THREAD(bind (mem_fun(*this, &GenericPluginUI::processor_active_changed), weak_processor));
714         
715         boost::shared_ptr<Processor> processor = weak_processor.lock();
716
717         bypass_button.set_active (!processor || !processor->active());
718 }
719
720 bool
721 GenericPluginUI::start_updating (GdkEventAny* ignored)
722 {
723         if (output_controls.size() > 0 ) {
724                 screen_update_connection.disconnect();
725                 screen_update_connection = ARDOUR_UI::instance()->RapidScreenUpdate.connect 
726                         (mem_fun(*this, &GenericPluginUI::output_update));
727         }
728         return false;
729 }
730
731 bool
732 GenericPluginUI::stop_updating (GdkEventAny* ignored)
733 {
734         if (output_controls.size() > 0 ) {
735                 screen_update_connection.disconnect();
736         }
737         return false;
738 }
739
740 void
741 GenericPluginUI::output_update ()
742 {
743         for (vector<ControlUI*>::iterator i = output_controls.begin(); i != output_controls.end(); ++i) {
744                 float val = plugin->get_parameter ((*i)->parameter().id());
745                 char buf[32];
746                 snprintf (buf, sizeof(buf), "%.2f", val);
747                 (*i)->display_label->set_text (buf);
748
749                 /* autoscaling for the meter */
750                 if ((*i)->meterinfo && (*i)->meterinfo->packed) {
751                         
752                         if (val < (*i)->meterinfo->min) {
753                                 if ((*i)->meterinfo->min_unbound)
754                                         (*i)->meterinfo->min = val;
755                                 else
756                                         val = (*i)->meterinfo->min;
757                         }
758
759                         if (val > (*i)->meterinfo->max) {
760                                 if ((*i)->meterinfo->max_unbound)
761                                         (*i)->meterinfo->max = val;
762                                 else
763                                         val = (*i)->meterinfo->max;
764                         }
765                         
766                         if ((*i)->meterinfo->max > (*i)->meterinfo->min ) {
767                                 float lval = (val - (*i)->meterinfo->min) / ((*i)->meterinfo->max - (*i)->meterinfo->min) ;
768                                 (*i)->meterinfo->meter->set (lval );
769                         }
770                 }
771         }
772 }
773
774 vector<string> 
775 GenericPluginUI::setup_scale_values(guint32 port_index, ControlUI* cui)
776 {
777         vector<string> enums;
778         boost::shared_ptr<LadspaPlugin> lp;
779 #ifdef HAVE_LV2
780         boost::shared_ptr<LV2Plugin> lv2p;
781 #endif
782
783         if ((lp = boost::dynamic_pointer_cast<LadspaPlugin>(plugin)) != 0) {
784                 // all LADPSA plugins have a numeric unique ID
785                 uint32_t id = atol (lp->unique_id().c_str());
786
787                 cui->combo_map = new std::map<string, float>;
788                 lrdf_defaults* defaults = lrdf_get_scale_values(id, port_index);
789                 if (defaults)   {
790                         for (uint32_t i = 0; i < defaults->count; ++i) {
791                                 enums.push_back(defaults->items[i].label);
792                                 pair<string, float> newpair;
793                                 newpair.first = defaults->items[i].label;
794                                 newpair.second = defaults->items[i].value;
795                                 cui->combo_map->insert(newpair);
796                         }
797
798                         lrdf_free_setting_values(defaults);
799                 }
800
801 #ifdef HAVE_LV2
802         } else if ((lv2p = boost::dynamic_pointer_cast<LV2Plugin>(plugin)) != 0) {
803
804                 SLV2Port port = lv2p->slv2_port(port_index);
805                 SLV2ScalePoints points = slv2_port_get_scale_points(lv2p->slv2_plugin(), port);
806                 cui->combo_map = new std::map<string, float>;
807         
808                 for (unsigned i=0; i < slv2_scale_points_size(points); ++i) {
809                         SLV2ScalePoint p = slv2_scale_points_get_at(points, i);
810                         SLV2Value label = slv2_scale_point_get_label(p);
811                         SLV2Value value = slv2_scale_point_get_value(p);
812                         if (label && (slv2_value_is_float(value) || slv2_value_is_int(value))) {
813                                 enums.push_back(slv2_value_as_string(label));
814                                 pair<string, float> newpair;
815                                 newpair.first = slv2_value_as_string(label);
816                                 newpair.second = slv2_value_as_float(value);
817                                 cui->combo_map->insert(newpair);
818                         }
819                 }
820
821                 slv2_scale_points_free(points);
822 #endif
823         }
824         
825
826         return enums;
827 }
828
829
830 void
831 GenericPluginUI::toggle_plugin_analysis()
832 {
833
834
835
836         if (eqgui_toggle.get_active() && !get_child2()) {
837                 // Create the GUI
838                 PluginEqGui *foo = new PluginEqGui(insert);
839                 pack2( *foo );
840                 show_all();
841         } 
842         
843         Gtk::Widget *gui;
844
845         if (!eqgui_toggle.get_active() && (gui = get_child2())) {
846                 // Hide & remove
847                 gui->hide();
848                 remove(*gui);
849
850                 delete gui;
851
852                 Gtk::Widget *toplevel = get_toplevel();
853                 if (!toplevel) {
854                         std::cerr << "No toplevel widget?!?!" << std::endl;
855                         return;
856                 }
857
858                 Gtk::Container *cont = dynamic_cast<Gtk::Container *>(toplevel);
859                 if (!cont) {
860                         std::cerr << "Toplevel widget is not a container?!?" << std::endl;
861                         return;
862                 }
863
864                 Gtk::Allocation alloc(0, 0, 50, 50); // Just make it small
865                 toplevel->size_allocate(alloc);
866         }
867 }