4 * Copyright (C) 2018 GG
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 #include "pluginlv2client.h"
30 #include "pluginlv2config.h"
31 #include "pluginlv2gui.h"
32 #include "pluginserver.h"
33 #include "preferences.h"
40 PluginLV2ClientUI::PluginLV2ClientUI(PluginLV2ClientWindow *gui, int x, int y)
41 : BC_GenericButton(x, y, _("UI"))
46 PluginLV2ClientUI::~PluginLV2ClientUI()
50 int PluginLV2ClientUI::handle_event()
52 PluginLV2ParentUI *ui = gui->get_ui();
58 PluginLV2ClientReset::
59 PluginLV2ClientReset(PluginLV2ClientWindow *gui, int x, int y)
60 : BC_GenericButton(x, y, _("Reset"))
65 PluginLV2ClientReset::
66 ~PluginLV2ClientReset()
70 int PluginLV2ClientReset::handle_event()
72 PluginLV2Client *client = gui->client;
73 client->config.init_lv2(client->lilv, client);
74 client->config.update();
75 client->update_lv2(LV2_LOAD);
77 client->send_configure_change();
81 PluginLV2ClientText::PluginLV2ClientText(PluginLV2ClientWindow *gui, int x, int y, int w)
82 : BC_TextBox(x, y, w, 1, (char *)"")
87 PluginLV2ClientText::~PluginLV2ClientText()
91 int PluginLV2ClientText::handle_event()
97 PluginLV2ClientApply::PluginLV2ClientApply(PluginLV2ClientWindow *gui, int x, int y)
98 : BC_GenericButton(x, y, _("Apply"))
103 PluginLV2ClientApply::~PluginLV2ClientApply()
107 int PluginLV2ClientApply::handle_event()
109 const char *text = gui->text->get_text();
110 if( text && gui->selected ) {
111 gui->selected->update(atof(text));
112 gui->update_selected();
113 gui->client->send_configure_change();
119 PluginLV2Client_OptPanel::PluginLV2Client_OptPanel(PluginLV2ClientWindow *gui,
120 int x, int y, int w, int h)
121 : BC_ListBox(x, y, w, h, LISTBOX_TEXT), opts(items[0]), vals(items[1])
124 update(); // init col/wid/columns
127 PluginLV2Client_OptPanel::~PluginLV2Client_OptPanel()
131 int PluginLV2Client_OptPanel::selection_changed()
133 PluginLV2Client_Opt *opt = 0;
134 BC_ListBoxItem *item = get_selection(0, 0);
136 PluginLV2Client_OptName *opt_name = (PluginLV2Client_OptName *)item;
143 void PluginLV2Client_OptPanel::update()
147 PluginLV2ClientConfig &conf = gui->client->config;
148 for( int i=0; i<conf.size(); ++i ) {
149 PluginLV2Client_Opt *opt = conf[i];
150 opts.append(opt->item_name);
151 vals.append(opt->item_value);
153 const char *cols[] = { "option", "value", };
154 const int col1_w = xS(150);
155 int wids[] = { col1_w, get_w()-col1_w };
156 BC_ListBox::update(&items[0], &cols[0], &wids[0], sizeof(items)/sizeof(items[0]),
157 get_xposition(), get_yposition(), get_highlighted_item());
160 PluginLV2ClientPot::PluginLV2ClientPot(PluginLV2ClientWindow *gui, int x, int y)
161 : BC_FPot(x, y, 0.f, 0.f, 0.f)
166 int PluginLV2ClientPot::handle_event()
168 if( gui->selected ) {
169 gui->selected->update(get_value());
170 gui->update_selected();
171 gui->client->send_configure_change();
176 PluginLV2ClientSlider::PluginLV2ClientSlider(PluginLV2ClientWindow *gui, int x, int y)
177 : BC_FSlider(x, y, 0, gui->get_w()-x-xS(20), gui->get_w()-x-xS(20), 0.f, 0.f, 0.f)
182 int PluginLV2ClientSlider::handle_event()
184 if( gui->selected ) {
185 gui->selected->update(get_value());
186 gui->update_selected();
187 gui->client->send_configure_change();
192 PluginLV2ClientWindow::PluginLV2ClientWindow(PluginLV2Client *client)
193 : PluginClientWindow(client, xS(500), yS(300), xS(500), yS(300), 1)
195 this->client = client;
199 void PluginLV2ClientWindow::done_event(int result)
201 PluginLV2ParentUI *ui = PluginLV2ParentUI::plugin_lv2.del_ui(this);
205 PluginLV2ClientWindow::~PluginLV2ClientWindow()
211 void PluginLV2ClientWindow::create_objects()
214 int xs8 = xS(8), xs10 = xS(10), ys10 = yS(10);
215 int x = xs10, y = ys10, x1;
216 add_subwindow(title = new BC_Title(x, y, client->title));
217 x1 = get_w() - BC_GenericButton::calculate_w(this, _("UI")) - xs8;
218 add_subwindow(client_ui = new PluginLV2ClientUI(this, x1, y));
219 y += title->get_h() + ys10;
220 add_subwindow(varbl = new BC_Title(x, y, ""));
221 add_subwindow(range = new BC_Title(x+xS(160), y, ""));
222 x1 = get_w() - BC_GenericButton::calculate_w(this, _("Reset")) - xs8;
223 add_subwindow(reset = new PluginLV2ClientReset(this, x1, y));
224 y += title->get_h() + ys10;
225 x1 = get_w() - BC_GenericButton::calculate_w(this, _("Apply")) - xs8;
226 add_subwindow(apply = new PluginLV2ClientApply(this, x1, y));
227 add_subwindow(text = new PluginLV2ClientText(this, x, y, x1-x - xs8));
228 y += title->get_h() + ys10;
229 add_subwindow(pot = new PluginLV2ClientPot(this, x, y));
230 x1 = x + pot->get_w() + xs10;
231 add_subwindow(slider = new PluginLV2ClientSlider(this, x1, y+ys10));
232 y += pot->get_h() + ys10;
234 client->load_configuration();
235 client->config.update();
237 int panel_x = x, panel_y = y;
238 int panel_w = get_w()-xs10 - panel_x;
239 int panel_h = get_h()-ys10 - panel_y;
240 panel = new PluginLV2Client_OptPanel(this, panel_x, panel_y, panel_w, panel_h);
241 add_subwindow(panel);
245 if( client->server->mwindow->preferences->autostart_lv2ui ) {
246 client_ui->disable();
247 PluginLV2ParentUI *ui = get_ui();
252 int PluginLV2ClientWindow::resize_event(int w, int h)
255 int xs8 = xS(8), xs10 = xS(10), ys10 = yS(10);
256 x1 = w - client_ui->get_w() - xs8;
257 client_ui->reposition_window(x1, client_ui->get_y());
258 x1 = w - reset->get_w() - xs8;
259 reset->reposition_window(x1, reset->get_y());
260 x1 = w - apply->get_w() - xs8;
261 apply->reposition_window(x1, apply->get_y());
262 text->reposition_window(text->get_x(), text->get_y(), x1-text->get_x() - xs8);
263 x1 = pot->get_x() + pot->get_w() + xs10;
264 int w1 = w - slider->get_x() - xS(20);
265 slider->set_pointer_motion_range(w1);
266 slider->reposition_window(x1, slider->get_y(), w1, slider->get_h());
267 int panel_x = panel->get_x(), panel_y = panel->get_y();
268 panel->reposition_window(panel_x, panel_y, w-xs10-panel_x, h-ys10-panel_y);
272 void PluginLV2ClientWindow::update_selected()
275 if( !selected ) return;
276 PluginLV2ParentUI *ui = find_ui();
278 PluginLV2ClientConfig &conf = client->config;
279 ui->send_child(LV2_UPDATE, conf.ctls, sizeof(float)*conf.nb_ports);
282 int PluginLV2ClientWindow::scalar(float f, char *rp)
285 if( f == FLT_MAX ) cp = "FLT_MAX";
286 else if( f == FLT_MIN ) cp = "FLT_MIN";
287 else if( f == -FLT_MAX ) cp = "-FLT_MAX";
288 else if( f == -FLT_MIN ) cp = "-FLT_MIN";
289 else if( f == 0 ) cp = signbit(f) ? "-0" : "0";
290 else if( isnan(f) ) cp = signbit(f) ? "-NAN" : "NAN";
291 else if( isinf(f) ) cp = signbit(f) ? "-INF" : "INF";
292 else return sprintf(rp, "%g", f);
293 return sprintf(rp, "%s", cp);
296 void PluginLV2ClientWindow::update(PluginLV2Client_Opt *opt)
298 if( selected != opt ) {
299 if( selected ) selected->item_name->set_selected(0);
301 if( selected ) selected->item_name->set_selected(1);
303 char var[BCSTRLEN]; var[0] = 0;
304 char val[BCSTRLEN]; val[0] = 0;
305 char rng[BCTEXTLEN]; rng[0] = 0;
307 sprintf(var,"%s:", opt->conf->names[opt->idx]);
309 cp += sprintf(cp,"( ");
310 float min = opt->conf->mins[opt->idx];
311 cp += scalar(min, cp);
312 cp += sprintf(cp, " .. ");
313 float max = opt->conf->maxs[opt->idx];
314 cp += scalar(max, cp);
315 cp += sprintf(cp, " )");
316 float v = opt->get_value();
317 sprintf(val, "%f", v);
318 float p = (max-min) / slider->get_w();
319 slider->set_precision(p);
320 slider->update(slider->get_w(), v, min, max);
321 pot->set_precision(p);
322 pot->update(v, min, max);
325 slider->update(slider->get_w(), 0.f, 0.f, 0.f);
326 pot->update(0.f, 0.f, 0.f);
334 void PluginLV2ClientWindow::lv2_update()
336 lock_window("PluginLV2ClientWindow::lv2_update");
337 PluginLV2ClientConfig &conf = client->config;
338 int ret = conf.update();
339 if( ret > 0 ) update(0);
342 client->send_configure_change();
345 void PluginLV2ClientWindow::lv2_update(float *vals)
347 PluginLV2ClientConfig &conf = client->config;
348 int nb_ports = conf.nb_ports;
349 float *ctls = conf.ctls;
350 for( int i=0; i<nb_ports; ++i ) *ctls++ = *vals++;
354 void PluginLV2ClientWindow::lv2_set(int idx, float val)
356 PluginLV2ClientConfig &conf = client->config;
357 conf[idx]->set_value(val);
361 void PluginLV2ClientWindow::lv2_ui_enable()
363 lock_window("PluginLV2ClientWindow::lv2_update");