+ Presets: added presets for Monosynth and Reverb
[calf.git] / src / gui.cpp
blob7be087c6e5fa339d27701c344b31f8b2b8b5c2d3
1 /* Calf DSP Library
2 * GUI functions for a plugin.
3 * Copyright (C) 2007 Krzysztof Foltman
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this program; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307, USA.
21 #include <config.h>
22 #include <assert.h>
23 #include <calf/ctl_curve.h>
24 #include <calf/ctl_keyboard.h>
25 #include <calf/ctl_led.h>
26 #include <calf/giface.h>
27 #include <calf/gui.h>
28 #include <calf/preset.h>
29 #include <calf/preset_gui.h>
30 #include <calf/main_win.h>
32 #include <iostream>
34 using namespace calf_plugins;
35 using namespace std;
37 /******************************** controls ********************************/
39 GtkWidget *param_control::create_label()
41 label = gtk_label_new ("");
42 gtk_label_set_width_chars (GTK_LABEL (label), 12);
43 gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
44 return label;
47 void param_control::update_label()
49 parameter_properties &props = get_props();
50 gtk_label_set_text (GTK_LABEL (label), props.to_string(gui->plugin->get_param_value(param_no)).c_str());
53 void param_control::hook_params()
55 if (param_no != -1) {
56 gui->add_param_ctl(param_no, this);
58 gui->params.push_back(this);
61 param_control::~param_control()
63 if (label)
64 gtk_widget_destroy(label);
65 if (widget)
66 gtk_widget_destroy(widget);
69 // combo box
71 GtkWidget *combo_box_param_control::create(plugin_gui *_gui, int _param_no)
73 gui = _gui;
74 param_no = _param_no;
76 parameter_properties &props = get_props();
77 widget = gtk_combo_box_new_text ();
78 for (int j = (int)props.min; j <= (int)props.max; j++)
79 gtk_combo_box_append_text (GTK_COMBO_BOX (widget), props.choices[j - (int)props.min]);
80 gtk_signal_connect (GTK_OBJECT (widget), "changed", G_CALLBACK (combo_value_changed), (gpointer)this);
82 return widget;
85 void combo_box_param_control::set()
87 _GUARD_CHANGE_
88 parameter_properties &props = get_props();
89 gtk_combo_box_set_active (GTK_COMBO_BOX (widget), (int)gui->plugin->get_param_value(param_no) - (int)props.min);
92 void combo_box_param_control::get()
94 parameter_properties &props = get_props();
95 gui->set_param_value(param_no, gtk_combo_box_get_active (GTK_COMBO_BOX(widget)) + props.min, this);
98 void combo_box_param_control::combo_value_changed(GtkComboBox *widget, gpointer value)
100 param_control *jhp = (param_control *)value;
101 jhp->get();
104 // horizontal fader
106 GtkWidget *hscale_param_control::create(plugin_gui *_gui, int _param_no)
108 gui = _gui;
109 param_no = _param_no;
111 widget = gtk_hscale_new_with_range (0, 1, get_props().get_increment());
112 gtk_signal_connect (GTK_OBJECT (widget), "value-changed", G_CALLBACK (hscale_value_changed), (gpointer)this);
113 gtk_signal_connect (GTK_OBJECT (widget), "format-value", G_CALLBACK (hscale_format_value), (gpointer)this);
114 gtk_widget_set_size_request (widget, 200, -1);
116 return widget;
119 void hscale_param_control::init_xml(const char *element)
121 if (attribs.count("width"))
122 gtk_widget_set_size_request (widget, get_int("width", 200), -1);
123 if (attribs.count("position"))
125 string v = attribs["position"];
126 if (v == "top") gtk_scale_set_value_pos(GTK_SCALE(widget), GTK_POS_TOP);
127 if (v == "bottom") gtk_scale_set_value_pos(GTK_SCALE(widget), GTK_POS_BOTTOM);
131 void hscale_param_control::set()
133 _GUARD_CHANGE_
134 parameter_properties &props = get_props();
135 gtk_range_set_value (GTK_RANGE (widget), props.to_01 (gui->plugin->get_param_value(param_no)));
136 // hscale_value_changed (GTK_HSCALE (widget), (gpointer)this);
139 void hscale_param_control::get()
141 parameter_properties &props = get_props();
142 float cvalue = props.from_01 (gtk_range_get_value (GTK_RANGE (widget)));
143 gui->set_param_value(param_no, cvalue, this);
146 void hscale_param_control::hscale_value_changed(GtkHScale *widget, gpointer value)
148 hscale_param_control *jhp = (hscale_param_control *)value;
149 jhp->get();
152 gchar *hscale_param_control::hscale_format_value(GtkScale *widget, double arg1, gpointer value)
154 hscale_param_control *jhp = (hscale_param_control *)value;
155 const parameter_properties &props = jhp->get_props();
156 float cvalue = props.from_01 (arg1);
158 // for testing
159 // return g_strdup_printf ("%s = %g", props.to_string (cvalue).c_str(), arg1);
160 return g_strdup (props.to_string (cvalue).c_str());
163 // vertical fader
165 GtkWidget *vscale_param_control::create(plugin_gui *_gui, int _param_no)
167 gui = _gui;
168 param_no = _param_no;
170 widget = gtk_vscale_new_with_range (0, 1, get_props().get_increment());
171 gtk_signal_connect (GTK_OBJECT (widget), "value-changed", G_CALLBACK (vscale_value_changed), (gpointer)this);
172 gtk_scale_set_draw_value(GTK_SCALE(widget), FALSE);
173 gtk_widget_set_size_request (widget, -1, 200);
175 return widget;
178 void vscale_param_control::init_xml(const char *element)
180 if (attribs.count("height"))
181 gtk_widget_set_size_request (widget, -1, get_int("height", 200));
184 void vscale_param_control::set()
186 _GUARD_CHANGE_
187 parameter_properties &props = get_props();
188 gtk_range_set_value (GTK_RANGE (widget), props.to_01 (gui->plugin->get_param_value(param_no)));
189 // vscale_value_changed (GTK_HSCALE (widget), (gpointer)this);
192 void vscale_param_control::get()
194 parameter_properties &props = get_props();
195 float cvalue = props.from_01 (gtk_range_get_value (GTK_RANGE (widget)));
196 gui->set_param_value(param_no, cvalue, this);
199 void vscale_param_control::vscale_value_changed(GtkHScale *widget, gpointer value)
201 vscale_param_control *jhp = (vscale_param_control *)value;
202 jhp->get();
205 // label
207 GtkWidget *label_param_control::create(plugin_gui *_gui, int _param_no)
209 gui = _gui, param_no = _param_no;
210 string text;
211 if (param_no != -1)
212 text = get_props().name;
213 else
214 text = attribs["text"];
215 widget = gtk_label_new(text.c_str());
216 gtk_misc_set_alignment (GTK_MISC (widget), get_float("align-x", 0.5), get_float("align-y", 0.5));
217 return widget;
220 // value
222 GtkWidget *value_param_control::create(plugin_gui *_gui, int _param_no)
224 gui = _gui, param_no = _param_no;
225 parameter_properties &props = get_props();
226 widget = gtk_label_new ("");
227 gtk_label_set_width_chars (GTK_LABEL (widget), props.get_char_count());
228 gtk_misc_set_alignment (GTK_MISC (widget), get_float("align-x", 0.5), get_float("align-y", 0.5));
229 return widget;
232 void value_param_control::set()
234 _GUARD_CHANGE_
235 parameter_properties &props = get_props();
236 gtk_label_set_text (GTK_LABEL (widget), props.to_string(gui->plugin->get_param_value(param_no)).c_str());
239 // VU meter
241 GtkWidget *vumeter_param_control::create(plugin_gui *_gui, int _param_no)
243 gui = _gui, param_no = _param_no;
244 // parameter_properties &props = get_props();
245 widget = calf_vumeter_new ();
246 calf_vumeter_set_mode (CALF_VUMETER (widget), (CalfVUMeterMode)get_int("mode", 0));
247 return widget;
250 void vumeter_param_control::set()
252 _GUARD_CHANGE_
253 parameter_properties &props = get_props();
254 calf_vumeter_set_value (CALF_VUMETER (widget), props.to_01(gui->plugin->get_param_value(param_no)));
255 if (label)
256 update_label();
259 // LED
261 GtkWidget *led_param_control::create(plugin_gui *_gui, int _param_no)
263 gui = _gui, param_no = _param_no;
264 // parameter_properties &props = get_props();
265 widget = calf_led_new ();
266 return widget;
269 void led_param_control::set()
271 _GUARD_CHANGE_
272 // parameter_properties &props = get_props();
273 calf_led_set_state (CALF_LED (widget), gui->plugin->get_param_value(param_no) > 0);
274 if (label)
275 update_label();
278 // check box
280 GtkWidget *toggle_param_control::create(plugin_gui *_gui, int _param_no)
282 gui = _gui;
283 param_no = _param_no;
285 widget = gtk_check_button_new ();
286 gtk_signal_connect (GTK_OBJECT (widget), "toggled", G_CALLBACK (toggle_value_changed), (gpointer)this);
287 return widget;
290 void toggle_param_control::toggle_value_changed(GtkCheckButton *widget, gpointer value)
292 param_control *jhp = (param_control *)value;
293 jhp->get();
296 void toggle_param_control::get()
298 const parameter_properties &props = get_props();
299 gui->set_param_value(param_no, gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON(widget)) + props.min, this);
302 void toggle_param_control::set()
304 _GUARD_CHANGE_
305 const parameter_properties &props = get_props();
306 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (widget), (int)gui->plugin->get_param_value(param_no) - (int)props.min);
309 // spin button
311 GtkWidget *spin_param_control::create(plugin_gui *_gui, int _param_no)
313 gui = _gui;
314 param_no = _param_no;
316 const parameter_properties &props = get_props();
317 if (props.step > 1)
318 widget = gtk_spin_button_new_with_range (props.min, props.max, (props.max - props.min) / (props.step - 1));
319 if (props.step > 0)
320 widget = gtk_spin_button_new_with_range (props.min, props.max, props.step);
321 else
322 widget = gtk_spin_button_new_with_range (props.min, props.max, 1);
323 gtk_spin_button_set_digits (GTK_SPIN_BUTTON(widget), get_int("digits", 0));
324 gtk_signal_connect (GTK_OBJECT (widget), "value-changed", G_CALLBACK (value_changed), (gpointer)this);
325 return widget;
328 void spin_param_control::value_changed(GtkSpinButton *widget, gpointer value)
330 param_control *jhp = (param_control *)value;
331 jhp->get();
334 void spin_param_control::get()
336 // const parameter_properties &props = get_props();
337 gui->set_param_value(param_no, gtk_spin_button_get_value_as_float (GTK_SPIN_BUTTON (widget)), this);
340 void spin_param_control::set()
342 _GUARD_CHANGE_
343 // const parameter_properties &props = get_props();
344 gtk_spin_button_set_value (GTK_SPIN_BUTTON (widget), gui->plugin->get_param_value(param_no));
347 // button
349 GtkWidget *button_param_control::create(plugin_gui *_gui, int _param_no)
351 gui = _gui;
352 param_no = _param_no;
354 widget = gtk_button_new_with_label (get_props().name);
355 gtk_signal_connect (GTK_OBJECT (widget), "clicked", G_CALLBACK (button_clicked), (gpointer)this);
356 return widget;
359 void button_param_control::button_clicked(GtkButton *widget, gpointer value)
361 param_control *jhp = (param_control *)value;
363 jhp->get();
366 void button_param_control::get()
368 const parameter_properties &props = get_props();
369 gui->set_param_value(param_no, props.max, this);
372 void button_param_control::set()
374 _GUARD_CHANGE_
375 const parameter_properties &props = get_props();
376 if (gui->plugin->get_param_value(param_no) - props.min >= 0.5)
377 gtk_button_clicked (GTK_BUTTON (widget));
380 // knob
382 GtkWidget *knob_param_control::create(plugin_gui *_gui, int _param_no)
384 gui = _gui;
385 param_no = _param_no;
386 const parameter_properties &props = get_props();
388 //widget = calf_knob_new_with_range (props.to_01 (gui->plugin->get_param_value(param_no)), 0, 1, 0.01);
389 widget = calf_knob_new();
390 float increment = props.get_increment();
391 gtk_range_get_adjustment(GTK_RANGE(widget))->step_increment = increment;
392 CALF_KNOB(widget)->knob_type = get_int("type");
393 gtk_signal_connect(GTK_OBJECT(widget), "value-changed", G_CALLBACK(knob_value_changed), (gpointer)this);
394 return widget;
397 void knob_param_control::get()
399 const parameter_properties &props = get_props();
400 float value = props.from_01(gtk_range_get_value(GTK_RANGE(widget)));
401 gui->set_param_value(param_no, value, this);
402 if (label)
403 update_label();
406 void knob_param_control::set()
408 _GUARD_CHANGE_
409 const parameter_properties &props = get_props();
410 gtk_range_set_value(GTK_RANGE(widget), props.to_01 (gui->plugin->get_param_value(param_no)));
411 if (label)
412 update_label();
415 void knob_param_control::knob_value_changed(GtkWidget *widget, gpointer value)
417 param_control *jhp = (param_control *)value;
418 jhp->get();
421 // keyboard
423 GtkWidget *keyboard_param_control::create(plugin_gui *_gui, int _param_no)
425 gui = _gui;
426 param_no = _param_no;
427 // const parameter_properties &props = get_props();
429 widget = calf_keyboard_new();
430 kb = CALF_KEYBOARD(widget);
431 kb->nkeys = get_int("octaves", 4) * 7 + 1;
432 kb->sink = new CalfKeyboard::EventAdapter;
433 return widget;
436 // curve
438 struct curve_param_control_callback: public CalfCurve::EventAdapter
440 curve_param_control *ctl;
442 curve_param_control_callback(curve_param_control *_ctl)
443 : ctl(_ctl) {}
445 virtual void curve_changed(CalfCurve *src, const CalfCurve::point_vector &data) {
446 stringstream ss;
447 ss << data.size() << endl;
448 for (size_t i = 0; i < data.size(); i++)
449 ss << data[i].first << " " << data[i].second << endl;
450 ctl->gui->plugin->configure(ctl->attribs["key"].c_str(), ss.str().c_str());
452 virtual void clip(CalfCurve *src, int pt, float &x, float &y, bool &hide)
454 // int gridpt = floor(x * 71 * 2);
455 // clip to the middle of the nearest white key
456 x = (floor(x * 71) + 0.5)/ 71.0;
460 GtkWidget *curve_param_control::create(plugin_gui *_gui, int _param_no)
462 gui = _gui;
463 param_no = _param_no;
464 require_attribute("key");
466 widget = calf_curve_new(get_int("maxpoints", -1));
467 curve = CALF_CURVE(widget);
468 curve->sink = new curve_param_control_callback(this);
469 // gtk_curve_set_curve_type(curve, GTK_CURVE_TYPE_LINEAR);
470 return widget;
473 void curve_param_control::send_configure(const char *key, const char *value)
475 // cout << "send conf " << key << endl;
476 if (attribs["key"] == key)
478 stringstream ss(value);
479 CalfCurve::point_vector pts;
480 if (*value)
482 unsigned int npoints = 0;
483 ss >> npoints;
484 unsigned int i;
485 float x = 0, y = 0;
486 for (i = 0; i < npoints && i < curve->point_limit; i++)
488 ss >> x >> y;
489 pts.push_back(CalfCurve::point(x, y));
491 calf_curve_set_points(widget, pts);
496 // line graph
498 void line_graph_param_control::on_idle()
500 if (get_int("refresh", 0))
501 set();
504 GtkWidget *line_graph_param_control::create(plugin_gui *_gui, int _param_no)
506 gui = _gui;
507 param_no = _param_no;
508 last_generation = -1;
509 // const parameter_properties &props = get_props();
511 widget = calf_line_graph_new ();
512 CalfLineGraph *clg = CALF_LINE_GRAPH(widget);
513 widget->requisition.width = get_int("width", 40);
514 widget->requisition.height = get_int("height", 40);
515 calf_line_graph_set_square(clg, get_int("square", 0));
516 clg->source = gui->plugin->get_line_graph_iface();
517 clg->source_id = param_no;
519 return widget;
522 void line_graph_param_control::set()
524 GtkWidget *tw = gtk_widget_get_toplevel(widget);
525 if (tw && GTK_WIDGET_TOPLEVEL(tw) && widget->window)
527 int ws = gdk_window_get_state(widget->window);
528 if (ws & (GDK_WINDOW_STATE_WITHDRAWN | GDK_WINDOW_STATE_ICONIFIED))
529 return;
530 last_generation = calf_line_graph_update_if(CALF_LINE_GRAPH(widget), last_generation);
534 line_graph_param_control::~line_graph_param_control()
538 /******************************** GUI proper ********************************/
540 plugin_gui::plugin_gui(plugin_gui_window *_window)
541 : window(_window)
546 static void window_destroyed(GtkWidget *window, gpointer data)
548 delete (plugin_gui_window *)data;
551 static void action_destroy_notify(gpointer data)
553 delete (activate_preset_params *)data;
556 void control_base::require_attribute(const char *name)
558 if (attribs.count(name) == 0) {
559 g_error("Missing attribute: %s", name);
563 void control_base::require_int_attribute(const char *name)
565 if (attribs.count(name) == 0) {
566 g_error("Missing attribute: %s", name);
568 if (attribs[name].empty() || attribs[name].find_first_not_of("0123456789") != string::npos) {
569 g_error("Wrong data type on attribute: %s (required integer)", name);
573 int control_base::get_int(const char *name, int def_value)
575 if (attribs.count(name) == 0)
576 return def_value;
577 const std::string &v = attribs[name];
578 if (v.empty() || v.find_first_not_of("-+0123456789") != string::npos)
579 return def_value;
580 return atoi(v.c_str());
583 float control_base::get_float(const char *name, float def_value)
585 if (attribs.count(name) == 0)
586 return def_value;
587 const std::string &v = attribs[name];
588 if (v.empty() || v.find_first_not_of("-+0123456789.") != string::npos)
589 return def_value;
590 stringstream ss(v);
591 float value;
592 ss >> value;
593 return value;
596 /******************************** GtkTable container ********************************/
598 GtkWidget *table_container::create(plugin_gui *_gui, const char *element, xml_attribute_map &attributes)
600 require_int_attribute("rows");
601 require_int_attribute("cols");
602 GtkWidget *table = gtk_table_new(get_int("rows", 1), get_int("cols", 1), false);
603 container = GTK_CONTAINER(table);
604 return table;
607 void table_container::add(GtkWidget *widget, control_base *base)
609 base->require_int_attribute("attach-x");
610 base->require_int_attribute("attach-y");
611 int x = base->get_int("attach-x"), y = base->get_int("attach-y");
612 int w = base->get_int("attach-w", 1), h = base->get_int("attach-h", 1);
613 int shrinkx = base->get_int("shrink-x", 0);
614 int shrinky = base->get_int("shrink-y", 0);
615 int fillx = (base->get_int("fill-x", !shrinkx) ? GTK_FILL : 0) | (base->get_int("expand-x", !shrinkx) ? GTK_EXPAND : 0) | (shrinkx ? GTK_SHRINK : 0);
616 int filly = (base->get_int("fill-y", !shrinky) ? GTK_FILL : 0) | (base->get_int("expand-y", !shrinky) ? GTK_EXPAND : 0) | (base->get_int("shrink-y", 0) ? GTK_SHRINK : 0);
617 int padx = base->get_int("pad-x", 2);
618 int pady = base->get_int("pad-y", 2);
619 gtk_table_attach(GTK_TABLE(container), widget, x, x + w, y, y + h, (GtkAttachOptions)fillx, (GtkAttachOptions)filly, padx, pady);
622 /******************************** alignment contaner ********************************/
624 GtkWidget *alignment_container::create(plugin_gui *_gui, const char *element, xml_attribute_map &attributes)
626 GtkWidget *align = gtk_alignment_new(get_float("align-x", 0.5), get_float("align-y", 0.5), get_float("scale-x", 0), get_float("scale-y", 0));
627 container = GTK_CONTAINER(align);
628 return align;
631 /******************************** GtkFrame contaner ********************************/
633 GtkWidget *frame_container::create(plugin_gui *_gui, const char *element, xml_attribute_map &attributes)
635 GtkWidget *frame = gtk_frame_new(attribs["label"].c_str());
636 container = GTK_CONTAINER(frame);
637 return frame;
640 /******************************** GtkBox type of containers ********************************/
642 void box_container::add(GtkWidget *w, control_base *base)
644 gtk_container_add_with_properties(container, w, "expand", get_int("expand", 1), "fill", get_int("fill", 1), NULL);
647 /******************************** GtkHBox container ********************************/
649 GtkWidget *hbox_container::create(plugin_gui *_gui, const char *element, xml_attribute_map &attributes)
651 GtkWidget *hbox = gtk_hbox_new(false, get_int("spacing", 2));
652 container = GTK_CONTAINER(hbox);
653 return hbox;
656 /******************************** GtkVBox container ********************************/
658 GtkWidget *vbox_container::create(plugin_gui *_gui, const char *element, xml_attribute_map &attributes)
660 GtkWidget *vbox = gtk_vbox_new(false, get_int("spacing", 2));
661 container = GTK_CONTAINER(vbox);
662 return vbox;
665 /******************************** GtkNotebook container ********************************/
667 GtkWidget *notebook_container::create(plugin_gui *_gui, const char *element, xml_attribute_map &attributes)
669 GtkWidget *nb = gtk_notebook_new();
670 container = GTK_CONTAINER(nb);
671 return nb;
674 void notebook_container::add(GtkWidget *w, control_base *base)
676 gtk_notebook_append_page(GTK_NOTEBOOK(container), w, gtk_label_new_with_mnemonic(base->attribs["page"].c_str()));
679 /******************************** GtkNotebook container ********************************/
681 GtkWidget *scrolled_container::create(plugin_gui *_gui, const char *element, xml_attribute_map &attributes)
683 GtkAdjustment *horiz = NULL, *vert = NULL;
684 int width = get_int("width", 0), height = get_int("height", 0);
685 if (width)
686 horiz = GTK_ADJUSTMENT(gtk_adjustment_new(get_int("x", 0), 0, width, get_int("step-x", 1), get_int("page-x", width / 10), 100));
687 if (height)
688 vert = GTK_ADJUSTMENT(gtk_adjustment_new(get_int("y", 0), 0, width, get_int("step-y", 1), get_int("page-y", height / 10), 10));
689 GtkWidget *sw = gtk_scrolled_window_new(horiz, vert);
690 gtk_widget_set_size_request(sw, get_int("req-x", -1), get_int("req-y", -1));
691 container = GTK_CONTAINER(sw);
692 return sw;
695 void scrolled_container::add(GtkWidget *w, control_base *base)
697 gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(container), w);
700 /******************************** GUI proper ********************************/
702 param_control *plugin_gui::create_control_from_xml(const char *element, const char *attributes[])
704 if (!strcmp(element, "knob"))
705 return new knob_param_control;
706 if (!strcmp(element, "hscale"))
707 return new hscale_param_control;
708 if (!strcmp(element, "vscale"))
709 return new vscale_param_control;
710 if (!strcmp(element, "combo"))
711 return new combo_box_param_control;
712 if (!strcmp(element, "toggle"))
713 return new toggle_param_control;
714 if (!strcmp(element, "spin"))
715 return new spin_param_control;
716 if (!strcmp(element, "button"))
717 return new button_param_control;
718 if (!strcmp(element, "label"))
719 return new label_param_control;
720 if (!strcmp(element, "value"))
721 return new value_param_control;
722 if (!strcmp(element, "vumeter"))
723 return new vumeter_param_control;
724 if (!strcmp(element, "line-graph"))
725 return new line_graph_param_control;
726 if (!strcmp(element, "keyboard"))
727 return new keyboard_param_control;
728 if (!strcmp(element, "curve"))
729 return new curve_param_control;
730 if (!strcmp(element, "led"))
731 return new led_param_control;
732 return NULL;
735 control_container *plugin_gui::create_container_from_xml(const char *element, const char *attributes[])
737 if (!strcmp(element, "table"))
738 return new table_container;
739 if (!strcmp(element, "vbox"))
740 return new vbox_container;
741 if (!strcmp(element, "hbox"))
742 return new hbox_container;
743 if (!strcmp(element, "align"))
744 return new alignment_container;
745 if (!strcmp(element, "frame"))
746 return new frame_container;
747 if (!strcmp(element, "notebook"))
748 return new notebook_container;
749 if (!strcmp(element, "scrolled"))
750 return new scrolled_container;
751 return NULL;
754 void plugin_gui::xml_element_start(void *data, const char *element, const char *attributes[])
756 plugin_gui *gui = (plugin_gui *)data;
757 gui->xml_element_start(element, attributes);
760 void plugin_gui::xml_element_start(const char *element, const char *attributes[])
762 if (ignore_stack) {
763 ignore_stack++;
764 return;
766 control_base::xml_attribute_map xam;
767 while(*attributes)
769 xam[attributes[0]] = attributes[1];
770 attributes += 2;
773 if (!strcmp(element, "if"))
775 if (!xam.count("cond") || xam["cond"].empty())
777 g_error("Incorrect <if cond=\"[!]symbol\"> element");
779 string cond = xam["cond"];
780 bool state = true;
781 if (cond.substr(0, 1) == "!") {
782 state = false;
783 cond.erase(0, 1);
785 if (window->main->check_condition(cond.c_str()) == state)
786 return;
787 ignore_stack = 1;
788 return;
790 control_container *cc = create_container_from_xml(element, attributes);
791 if (cc != NULL)
793 cc->attribs = xam;
794 cc->create(this, element, xam);
795 gtk_container_set_border_width(cc->container, cc->get_int("border"));
797 container_stack.push_back(cc);
798 current_control = NULL;
799 return;
801 if (!container_stack.empty())
803 current_control = create_control_from_xml(element, attributes);
804 if (current_control)
806 current_control->attribs = xam;
807 int param_no = -1;
808 if (xam.count("param"))
810 map<string, int>::iterator it = param_name_map.find(xam["param"]);
811 if (it == param_name_map.end())
812 g_error("Unknown parameter %s", xam["param"].c_str());
813 else
814 param_no = it->second;
816 current_control->create(this, param_no);
817 current_control->init_xml(element);
818 current_control->set();
819 current_control->hook_params();
820 return;
823 g_error("Unxpected element %s in GUI definition\n", element);
826 void plugin_gui::xml_element_end(void *data, const char *element)
828 plugin_gui *gui = (plugin_gui *)data;
829 if (gui->ignore_stack) {
830 gui->ignore_stack--;
831 return;
833 if (!strcmp(element, "if"))
835 return;
837 if (gui->current_control)
839 (*gui->container_stack.rbegin())->add(gui->current_control->widget, gui->current_control);
840 gui->current_control = NULL;
841 return;
843 unsigned int ss = gui->container_stack.size();
844 if (ss > 1) {
845 gui->container_stack[ss - 2]->add(GTK_WIDGET(gui->container_stack[ss - 1]->container), gui->container_stack[ss - 1]);
847 else
848 gui->top_container = gui->container_stack[0];
849 gui->container_stack.pop_back();
853 GtkWidget *plugin_gui::create_from_xml(plugin_ctl_iface *_plugin, const char *xml)
855 current_control = NULL;
856 top_container = NULL;
857 parser = XML_ParserCreate("UTF-8");
858 plugin = _plugin;
859 container_stack.clear();
860 ignore_stack = 0;
862 param_name_map.clear();
863 int size = plugin->get_param_count();
864 for (int i = 0; i < size; i++)
865 param_name_map[plugin->get_param_props(i)->short_name] = i;
867 XML_SetUserData(parser, this);
868 XML_SetElementHandler(parser, xml_element_start, xml_element_end);
869 XML_Status status = XML_Parse(parser, xml, strlen(xml), 1);
870 if (status == XML_STATUS_ERROR)
872 g_error("Parse error: %s in XML", XML_ErrorString(XML_GetErrorCode(parser)));
875 XML_ParserFree(parser);
876 return GTK_WIDGET(top_container->container);
879 void plugin_gui::send_configure(const char *key, const char *value)
881 // XXXKF this should really be replaced by a separate list of SCI-capable param controls
882 for (unsigned int i = 0; i < params.size(); i++)
884 assert(params[i] != NULL);
885 send_configure_iface *sci = dynamic_cast<send_configure_iface *>(params[i]);
886 if (sci)
887 sci->send_configure(key, value);
891 void plugin_gui::on_idle()
893 for (unsigned int i = 0; i < params.size(); i++)
895 parameter_properties &props = *plugin->get_param_props(params[i]->param_no);
896 bool is_output = (props.flags & PF_PROP_OUTPUT) != 0;
897 if (is_output) {
898 params[i]->set();
900 params[i]->on_idle();
902 // XXXKF iterate over par2ctl, too...
905 void plugin_gui::refresh()
907 for (unsigned int i = 0; i < params.size(); i++)
909 params[i]->set();
910 send_configure_iface *sci = dynamic_cast<send_configure_iface *>(params[i]);
911 if (sci)
912 plugin->send_configures(sci);
916 void plugin_gui::refresh(int param_no, param_control *originator)
918 std::multimap<int, param_control *>::iterator it = par2ctl.find(param_no);
919 while(it != par2ctl.end() && it->first == param_no)
921 if (it->second != originator)
922 it->second->set();
923 it++;
927 void plugin_gui::set_param_value(int param_no, float value, param_control *originator)
929 plugin->set_param_value(param_no, value);
930 refresh(param_no);
933 plugin_gui::~plugin_gui()
935 for (std::vector<param_control *>::iterator i = params.begin(); i != params.end(); i++)
937 delete *i;
942 /******************************* Actions **************************************************/
944 static void store_preset_action(GtkAction *action, plugin_gui_window *gui_win)
946 store_preset(GTK_WINDOW(gui_win->toplevel), gui_win->gui);
949 static const GtkActionEntry actions[] = {
950 { "PresetMenuAction", "", "_Preset", NULL, "Preset operations", NULL },
951 { "BuiltinPresetMenuAction", "", "_Built-in", NULL, "Built-in (factory) presets", NULL },
952 { "UserPresetMenuAction", "", "_User", NULL, "User (your) presets", NULL },
953 { "CommandMenuAction", "", "_Command", NULL, "Plugin-related commands", NULL },
954 { "store-preset", "", "Store preset", NULL, "Store a current setting as preset", (GCallback)store_preset_action },
957 /***************************** GUI window ********************************************/
959 static const char *ui_xml =
960 "<ui>\n"
961 " <menubar>\n"
962 " <menu action=\"PresetMenuAction\">\n"
963 " <menuitem action=\"store-preset\"/>\n"
964 " <separator/>\n"
965 " <placeholder name=\"builtin_presets\"/>\n"
966 " <separator/>\n"
967 " <placeholder name=\"user_presets\"/>\n"
968 " </menu>\n"
969 " <placeholder name=\"commands\"/>\n"
970 " </menubar>\n"
971 "</ui>\n"
974 static const char *general_preset_pre_xml =
975 "<ui>\n"
976 " <menubar>\n"
977 " <menu action=\"PresetMenuAction\">\n";
979 static const char *builtin_preset_pre_xml =
980 // " <menu action=\"BuiltinPresetMenuAction\">\n"
981 " <placeholder name=\"builtin_presets\">\n";
983 static const char *user_preset_pre_xml =
984 // " <menu action=\"UserPresetMenuAction\">\n"
985 " <placeholder name=\"user_presets\">\n";
987 static const char *preset_post_xml =
988 " </placeholder>\n"
989 // " </menu>\n"
990 " </menu>\n"
991 " </menubar>\n"
992 "</ui>\n"
995 static const char *command_pre_xml =
996 "<ui>\n"
997 " <menubar>\n"
998 " <placeholder name=\"commands\">\n"
999 " <menu action=\"CommandMenuAction\">\n";
1001 static const char *command_post_xml =
1002 " </menu>\n"
1003 " </placeholder>\n"
1004 " </menubar>\n"
1005 "</ui>\n"
1008 plugin_gui_window::plugin_gui_window(main_window_iface *_main)
1010 toplevel = NULL;
1011 ui_mgr = NULL;
1012 std_actions = NULL;
1013 builtin_preset_actions = NULL;
1014 user_preset_actions = NULL;
1015 command_actions = NULL;
1016 main = _main;
1017 assert(main);
1020 string plugin_gui_window::make_gui_preset_list(GtkActionGroup *grp, bool builtin, char &ch)
1022 string preset_xml = string(general_preset_pre_xml) + (builtin ? builtin_preset_pre_xml : user_preset_pre_xml);
1023 preset_vector &pvec = (builtin ? get_builtin_presets() : get_user_presets()).presets;
1024 GtkActionGroup *preset_actions = builtin ? builtin_preset_actions : user_preset_actions;
1025 for (unsigned int i = 0; i < pvec.size(); i++)
1027 if (pvec[i].plugin != gui->effect_name)
1028 continue;
1029 stringstream ss;
1030 ss << (builtin ? "builtin_preset" : "user_preset") << i;
1031 preset_xml += " <menuitem name=\"" + pvec[i].name+"\" action=\""+ss.str()+"\"/>\n";
1032 if (ch != ' ' && ++ch == ':')
1033 ch = 'A';
1034 if (ch > 'Z')
1035 ch = ' ';
1037 string sv = ss.str();
1038 string prefix = ch == ' ' ? string() : string("_")+ch+" ";
1039 string name = prefix + pvec[i].name;
1040 GtkActionEntry ae = { sv.c_str(), NULL, name.c_str(), NULL, NULL, (GCallback)activate_preset };
1041 gtk_action_group_add_actions_full(preset_actions, &ae, 1, (gpointer)new activate_preset_params(gui, i, builtin), action_destroy_notify);
1043 preset_xml += preset_post_xml;
1044 return preset_xml;
1047 string plugin_gui_window::make_gui_command_list(GtkActionGroup *grp)
1049 string command_xml = command_pre_xml;
1050 plugin_command_info *ci = gui->plugin->get_commands();
1051 if (!ci)
1052 return "";
1053 for(int i = 0; ci->name; i++, ci++)
1055 stringstream ss;
1056 ss << " <menuitem name=\"" << ci->name << "\" action=\"" << ci->label << "\"/>\n";
1058 GtkActionEntry ae = { ci->label, NULL, ci->name, NULL, ci->description, (GCallback)activate_command };
1059 gtk_action_group_add_actions_full(command_actions, &ae, 1, (gpointer)new activate_command_params(gui, i), action_destroy_notify);
1060 command_xml += ss.str();
1062 command_xml += command_post_xml;
1063 return command_xml;
1066 void plugin_gui_window::fill_gui_presets(bool builtin, char &ch)
1068 GtkActionGroup *&preset_actions = builtin ? builtin_preset_actions : user_preset_actions;
1069 if(preset_actions) {
1070 gtk_ui_manager_remove_action_group(ui_mgr, preset_actions);
1071 preset_actions = NULL;
1074 if (builtin)
1075 builtin_preset_actions = gtk_action_group_new("builtin_presets");
1076 else
1077 user_preset_actions = gtk_action_group_new("user_presets");
1078 string preset_xml = make_gui_preset_list(preset_actions, builtin, ch);
1079 gtk_ui_manager_insert_action_group(ui_mgr, preset_actions, 0);
1080 GError *error = NULL;
1081 gtk_ui_manager_add_ui_from_string(ui_mgr, preset_xml.c_str(), -1, &error);
1084 gboolean plugin_gui_window::on_idle(void *data)
1086 plugin_gui_window *self = (plugin_gui_window *)data;
1087 self->gui->on_idle();
1088 return TRUE;
1091 void plugin_gui_window::create(plugin_ctl_iface *_jh, const char *title, const char *effect)
1093 toplevel = GTK_WINDOW(gtk_window_new (GTK_WINDOW_TOPLEVEL));
1094 gtk_window_set_default_icon_name("calf");
1095 gtk_window_set_type_hint(toplevel, GDK_WINDOW_TYPE_HINT_DIALOG);
1096 GtkVBox *vbox = GTK_VBOX(gtk_vbox_new(false, 5));
1098 GtkRequisition req, req2;
1099 gtk_window_set_title(GTK_WINDOW (toplevel), title);
1100 gtk_container_add(GTK_CONTAINER(toplevel), GTK_WIDGET(vbox));
1102 gui = new plugin_gui(this);
1103 gui->effect_name = effect;
1105 ui_mgr = gtk_ui_manager_new();
1106 std_actions = gtk_action_group_new("default");
1107 gtk_action_group_add_actions(std_actions, actions, sizeof(actions)/sizeof(actions[0]), this);
1108 GError *error = NULL;
1109 gtk_ui_manager_insert_action_group(ui_mgr, std_actions, 0);
1110 gtk_ui_manager_add_ui_from_string(ui_mgr, ui_xml, -1, &error);
1112 command_actions = gtk_action_group_new("commands");
1114 char ch = '0';
1115 fill_gui_presets(true, ch);
1116 fill_gui_presets(false, ch);
1118 gtk_box_pack_start(GTK_BOX(vbox), gtk_ui_manager_get_widget(ui_mgr, "/ui/menubar"), false, false, 0);
1120 // determine size without content
1121 gtk_widget_show_all(GTK_WIDGET(vbox));
1122 gtk_widget_size_request(GTK_WIDGET(vbox), &req2);
1123 // printf("size request %dx%d\n", req2.width, req2.height);
1125 GtkWidget *container;
1126 const char *xml = _jh->get_gui_xml();
1127 assert(xml);
1128 container = gui->create_from_xml(_jh, xml);
1130 string command_xml = make_gui_command_list(command_actions);
1131 gtk_ui_manager_insert_action_group(ui_mgr, command_actions, 0);
1132 gtk_ui_manager_add_ui_from_string(ui_mgr, command_xml.c_str(), -1, &error);
1134 GtkWidget *sw = gtk_scrolled_window_new(NULL, NULL);
1135 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(sw), GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
1136 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(sw), GTK_SHADOW_NONE);
1137 gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(sw), container);
1139 gtk_box_pack_start(GTK_BOX(vbox), sw, true, true, 0);
1141 gtk_widget_show_all(GTK_WIDGET(sw));
1142 gtk_widget_size_request(container, &req);
1143 int wx = max(req.width + 10, req2.width);
1144 int wy = req.height + req2.height + 10;
1145 // printf("size request %dx%d\n", req.width, req.height);
1146 // printf("resize to %dx%d\n", max(req.width + 10, req2.width), req.height + req2.height + 10);
1147 gtk_window_set_default_size(GTK_WINDOW(toplevel), wx, wy);
1148 gtk_window_resize(GTK_WINDOW(toplevel), wx, wy);
1149 //gtk_widget_set_size_request(GTK_WIDGET(toplevel), max(req.width + 10, req2.width), req.height + req2.height + 10);
1150 // printf("size set %dx%d\n", wx, wy);
1151 // gtk_scrolled_window_set_vadjustment(GTK_SCROLLED_WINDOW(sw), GTK_ADJUSTMENT(gtk_adjustment_new(0, 0, req.height, 20, 100, 100)));
1152 gtk_signal_connect (GTK_OBJECT (toplevel), "destroy", G_CALLBACK (window_destroyed), (plugin_gui_window *)this);
1153 main->set_window(gui->plugin, this);
1155 source_id = g_timeout_add_full(G_PRIORITY_LOW, 1000/30, on_idle, this, NULL); // 30 fps should be enough for everybody
1156 gtk_ui_manager_ensure_update(ui_mgr);
1157 gui->plugin->send_configures(gui);
1160 void plugin_gui_window::close()
1162 if (source_id)
1163 g_source_remove(source_id);
1164 source_id = 0;
1165 gtk_widget_destroy(GTK_WIDGET(toplevel));
1168 plugin_gui_window::~plugin_gui_window()
1170 if (source_id)
1171 g_source_remove(source_id);
1172 main->set_window(gui->plugin, NULL);
1173 delete gui;
1176 void calf_plugins::activate_command(GtkAction *action, activate_command_params *params)
1178 plugin_gui *gui = params->gui;
1179 gui->plugin->execute(params->function_idx);
1180 gui->refresh();