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