+ Filter: move implementation of get_changed_offsets to .cpp file
[calf.git] / src / gui.cpp
blob2e6c1b22338ce461587b7d7f8ff22444e54021d9
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_signal_connect (GTK_OBJECT (widget), "value-changed", G_CALLBACK (value_changed), (gpointer)this);
324 return widget;
327 void spin_param_control::value_changed(GtkSpinButton *widget, gpointer value)
329 param_control *jhp = (param_control *)value;
330 jhp->get();
333 void spin_param_control::get()
335 // const parameter_properties &props = get_props();
336 gui->set_param_value(param_no, gtk_spin_button_get_value_as_float (GTK_SPIN_BUTTON (widget)), this);
339 void spin_param_control::set()
341 _GUARD_CHANGE_
342 // const parameter_properties &props = get_props();
343 gtk_spin_button_set_value (GTK_SPIN_BUTTON (widget), gui->plugin->get_param_value(param_no));
346 // button
348 GtkWidget *button_param_control::create(plugin_gui *_gui, int _param_no)
350 gui = _gui;
351 param_no = _param_no;
353 widget = gtk_button_new_with_label (get_props().name);
354 gtk_signal_connect (GTK_OBJECT (widget), "clicked", G_CALLBACK (button_clicked), (gpointer)this);
355 return widget;
358 void button_param_control::button_clicked(GtkButton *widget, gpointer value)
360 param_control *jhp = (param_control *)value;
362 jhp->get();
365 void button_param_control::get()
367 const parameter_properties &props = get_props();
368 gui->set_param_value(param_no, props.max, this);
371 void button_param_control::set()
373 _GUARD_CHANGE_
374 const parameter_properties &props = get_props();
375 if (gui->plugin->get_param_value(param_no) - props.min >= 0.5)
376 gtk_button_clicked (GTK_BUTTON (widget));
379 // knob
381 GtkWidget *knob_param_control::create(plugin_gui *_gui, int _param_no)
383 gui = _gui;
384 param_no = _param_no;
385 const parameter_properties &props = get_props();
387 //widget = calf_knob_new_with_range (props.to_01 (gui->plugin->get_param_value(param_no)), 0, 1, 0.01);
388 widget = calf_knob_new();
389 float increment = props.get_increment();
390 gtk_range_get_adjustment(GTK_RANGE(widget))->step_increment = increment;
391 CALF_KNOB(widget)->knob_type = get_int("type");
392 gtk_signal_connect(GTK_OBJECT(widget), "value-changed", G_CALLBACK(knob_value_changed), (gpointer)this);
393 return widget;
396 void knob_param_control::get()
398 const parameter_properties &props = get_props();
399 float value = props.from_01(gtk_range_get_value(GTK_RANGE(widget)));
400 gui->set_param_value(param_no, value, this);
401 if (label)
402 update_label();
405 void knob_param_control::set()
407 _GUARD_CHANGE_
408 const parameter_properties &props = get_props();
409 gtk_range_set_value(GTK_RANGE(widget), props.to_01 (gui->plugin->get_param_value(param_no)));
410 if (label)
411 update_label();
414 void knob_param_control::knob_value_changed(GtkWidget *widget, gpointer value)
416 param_control *jhp = (param_control *)value;
417 jhp->get();
420 // keyboard
422 GtkWidget *keyboard_param_control::create(plugin_gui *_gui, int _param_no)
424 gui = _gui;
425 param_no = _param_no;
426 // const parameter_properties &props = get_props();
428 widget = calf_keyboard_new();
429 kb = CALF_KEYBOARD(widget);
430 kb->nkeys = get_int("octaves", 4) * 7 + 1;
431 kb->sink = new CalfKeyboard::EventAdapter;
432 return widget;
435 // curve
437 struct curve_param_control_callback: public CalfCurve::EventAdapter
439 curve_param_control *ctl;
441 curve_param_control_callback(curve_param_control *_ctl)
442 : ctl(_ctl) {}
444 virtual void curve_changed(CalfCurve *src, const CalfCurve::point_vector &data) {
445 stringstream ss;
446 ss << data.size() << endl;
447 for (size_t i = 0; i < data.size(); i++)
448 ss << data[i].first << " " << data[i].second << endl;
449 ctl->gui->plugin->configure(ctl->attribs["key"].c_str(), ss.str().c_str());
451 virtual void clip(CalfCurve *src, int pt, float &x, float &y, bool &hide)
453 // int gridpt = floor(x * 71 * 2);
454 // clip to the middle of the nearest white key
455 x = (floor(x * 71) + 0.5)/ 71.0;
459 GtkWidget *curve_param_control::create(plugin_gui *_gui, int _param_no)
461 gui = _gui;
462 param_no = _param_no;
463 require_attribute("key");
465 widget = calf_curve_new(get_int("maxpoints", -1));
466 curve = CALF_CURVE(widget);
467 curve->sink = new curve_param_control_callback(this);
468 // gtk_curve_set_curve_type(curve, GTK_CURVE_TYPE_LINEAR);
469 return widget;
472 void curve_param_control::send_configure(const char *key, const char *value)
474 // cout << "send conf " << key << endl;
475 if (attribs["key"] == key)
477 stringstream ss(value);
478 CalfCurve::point_vector pts;
479 if (*value)
481 unsigned int npoints = 0;
482 ss >> npoints;
483 unsigned int i;
484 float x = 0, y = 0;
485 for (i = 0; i < npoints && i < curve->point_limit; i++)
487 ss >> x >> y;
488 pts.push_back(CalfCurve::point(x, y));
490 calf_curve_set_points(widget, pts);
495 // line graph
497 void line_graph_param_control::on_idle()
499 if (get_int("refresh", 0))
500 set();
503 GtkWidget *line_graph_param_control::create(plugin_gui *_gui, int _param_no)
505 gui = _gui;
506 param_no = _param_no;
507 // const parameter_properties &props = get_props();
509 widget = calf_line_graph_new ();
510 CalfLineGraph *clg = CALF_LINE_GRAPH(widget);
511 widget->requisition.width = get_int("width", 40);
512 widget->requisition.height = get_int("height", 40);
513 calf_line_graph_set_square(clg, get_int("square", 0));
514 clg->source = gui->plugin->get_line_graph_iface();
515 clg->source_id = param_no;
517 return widget;
520 void line_graph_param_control::set()
522 GtkWidget *tw = gtk_widget_get_toplevel(widget);
523 if (tw && GTK_WIDGET_TOPLEVEL(tw) && widget->window)
525 int ws = gdk_window_get_state(widget->window);
526 if (ws & (GDK_WINDOW_STATE_WITHDRAWN | GDK_WINDOW_STATE_ICONIFIED))
527 return;
528 calf_line_graph_update_if(CALF_LINE_GRAPH(widget));
532 line_graph_param_control::~line_graph_param_control()
536 /******************************** GUI proper ********************************/
538 plugin_gui::plugin_gui(plugin_gui_window *_window)
539 : window(_window)
544 static void window_destroyed(GtkWidget *window, gpointer data)
546 delete (plugin_gui_window *)data;
549 static void action_destroy_notify(gpointer data)
551 delete (activate_preset_params *)data;
554 void control_base::require_attribute(const char *name)
556 if (attribs.count(name) == 0) {
557 g_error("Missing attribute: %s", name);
561 void control_base::require_int_attribute(const char *name)
563 if (attribs.count(name) == 0) {
564 g_error("Missing attribute: %s", name);
566 if (attribs[name].empty() || attribs[name].find_first_not_of("0123456789") != string::npos) {
567 g_error("Wrong data type on attribute: %s (required integer)", name);
571 int control_base::get_int(const char *name, int def_value)
573 if (attribs.count(name) == 0)
574 return def_value;
575 const std::string &v = attribs[name];
576 if (v.empty() || v.find_first_not_of("-+0123456789") != string::npos)
577 return def_value;
578 return atoi(v.c_str());
581 float control_base::get_float(const char *name, float def_value)
583 if (attribs.count(name) == 0)
584 return def_value;
585 const std::string &v = attribs[name];
586 if (v.empty() || v.find_first_not_of("-+0123456789.") != string::npos)
587 return def_value;
588 stringstream ss(v);
589 float value;
590 ss >> value;
591 return value;
594 /******************************** GtkTable container ********************************/
596 GtkWidget *table_container::create(plugin_gui *_gui, const char *element, xml_attribute_map &attributes)
598 require_int_attribute("rows");
599 require_int_attribute("cols");
600 GtkWidget *table = gtk_table_new(get_int("rows", 1), get_int("cols", 1), false);
601 container = GTK_CONTAINER(table);
602 return table;
605 void table_container::add(GtkWidget *widget, control_base *base)
607 base->require_int_attribute("attach-x");
608 base->require_int_attribute("attach-y");
609 int x = base->get_int("attach-x"), y = base->get_int("attach-y");
610 int w = base->get_int("attach-w", 1), h = base->get_int("attach-h", 1);
611 int shrinkx = base->get_int("shrink-x", 0);
612 int shrinky = base->get_int("shrink-y", 0);
613 int fillx = (base->get_int("fill-x", !shrinkx) ? GTK_FILL : 0) | (base->get_int("expand-x", !shrinkx) ? GTK_EXPAND : 0) | (shrinkx ? GTK_SHRINK : 0);
614 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);
615 int padx = base->get_int("pad-x", 2);
616 int pady = base->get_int("pad-y", 2);
617 gtk_table_attach(GTK_TABLE(container), widget, x, x + w, y, y + h, (GtkAttachOptions)fillx, (GtkAttachOptions)filly, padx, pady);
620 /******************************** alignment contaner ********************************/
622 GtkWidget *alignment_container::create(plugin_gui *_gui, const char *element, xml_attribute_map &attributes)
624 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));
625 container = GTK_CONTAINER(align);
626 return align;
629 /******************************** GtkFrame contaner ********************************/
631 GtkWidget *frame_container::create(plugin_gui *_gui, const char *element, xml_attribute_map &attributes)
633 GtkWidget *frame = gtk_frame_new(attribs["label"].c_str());
634 container = GTK_CONTAINER(frame);
635 return frame;
638 /******************************** GtkBox type of containers ********************************/
640 void box_container::add(GtkWidget *w, control_base *base)
642 gtk_container_add_with_properties(container, w, "expand", get_int("expand", 1), "fill", get_int("fill", 1), NULL);
645 /******************************** GtkHBox container ********************************/
647 GtkWidget *hbox_container::create(plugin_gui *_gui, const char *element, xml_attribute_map &attributes)
649 GtkWidget *hbox = gtk_hbox_new(false, get_int("spacing", 2));
650 container = GTK_CONTAINER(hbox);
651 return hbox;
654 /******************************** GtkVBox container ********************************/
656 GtkWidget *vbox_container::create(plugin_gui *_gui, const char *element, xml_attribute_map &attributes)
658 GtkWidget *vbox = gtk_vbox_new(false, get_int("spacing", 2));
659 container = GTK_CONTAINER(vbox);
660 return vbox;
663 /******************************** GtkNotebook container ********************************/
665 GtkWidget *notebook_container::create(plugin_gui *_gui, const char *element, xml_attribute_map &attributes)
667 GtkWidget *nb = gtk_notebook_new();
668 container = GTK_CONTAINER(nb);
669 return nb;
672 void notebook_container::add(GtkWidget *w, control_base *base)
674 gtk_notebook_append_page(GTK_NOTEBOOK(container), w, gtk_label_new_with_mnemonic(base->attribs["page"].c_str()));
677 /******************************** GtkNotebook container ********************************/
679 GtkWidget *scrolled_container::create(plugin_gui *_gui, const char *element, xml_attribute_map &attributes)
681 GtkAdjustment *horiz = NULL, *vert = NULL;
682 int width = get_int("width", 0), height = get_int("height", 0);
683 if (width)
684 horiz = GTK_ADJUSTMENT(gtk_adjustment_new(get_int("x", 0), 0, width, get_int("step-x", 1), get_int("page-x", width / 10), 100));
685 if (height)
686 vert = GTK_ADJUSTMENT(gtk_adjustment_new(get_int("y", 0), 0, width, get_int("step-y", 1), get_int("page-y", height / 10), 10));
687 GtkWidget *sw = gtk_scrolled_window_new(horiz, vert);
688 gtk_widget_set_size_request(sw, get_int("req-x", -1), get_int("req-y", -1));
689 container = GTK_CONTAINER(sw);
690 return sw;
693 void scrolled_container::add(GtkWidget *w, control_base *base)
695 gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(container), w);
698 /******************************** GUI proper ********************************/
700 param_control *plugin_gui::create_control_from_xml(const char *element, const char *attributes[])
702 if (!strcmp(element, "knob"))
703 return new knob_param_control;
704 if (!strcmp(element, "hscale"))
705 return new hscale_param_control;
706 if (!strcmp(element, "vscale"))
707 return new vscale_param_control;
708 if (!strcmp(element, "combo"))
709 return new combo_box_param_control;
710 if (!strcmp(element, "toggle"))
711 return new toggle_param_control;
712 if (!strcmp(element, "spin"))
713 return new spin_param_control;
714 if (!strcmp(element, "button"))
715 return new button_param_control;
716 if (!strcmp(element, "label"))
717 return new label_param_control;
718 if (!strcmp(element, "value"))
719 return new value_param_control;
720 if (!strcmp(element, "vumeter"))
721 return new vumeter_param_control;
722 if (!strcmp(element, "line-graph"))
723 return new line_graph_param_control;
724 if (!strcmp(element, "keyboard"))
725 return new keyboard_param_control;
726 if (!strcmp(element, "curve"))
727 return new curve_param_control;
728 if (!strcmp(element, "led"))
729 return new led_param_control;
730 return NULL;
733 control_container *plugin_gui::create_container_from_xml(const char *element, const char *attributes[])
735 if (!strcmp(element, "table"))
736 return new table_container;
737 if (!strcmp(element, "vbox"))
738 return new vbox_container;
739 if (!strcmp(element, "hbox"))
740 return new hbox_container;
741 if (!strcmp(element, "align"))
742 return new alignment_container;
743 if (!strcmp(element, "frame"))
744 return new frame_container;
745 if (!strcmp(element, "notebook"))
746 return new notebook_container;
747 if (!strcmp(element, "scrolled"))
748 return new scrolled_container;
749 return NULL;
752 void plugin_gui::xml_element_start(void *data, const char *element, const char *attributes[])
754 plugin_gui *gui = (plugin_gui *)data;
755 gui->xml_element_start(element, attributes);
758 void plugin_gui::xml_element_start(const char *element, const char *attributes[])
760 if (ignore_stack) {
761 ignore_stack++;
762 return;
764 control_base::xml_attribute_map xam;
765 while(*attributes)
767 xam[attributes[0]] = attributes[1];
768 attributes += 2;
771 if (!strcmp(element, "if"))
773 if (!xam.count("cond") || xam["cond"].empty())
775 g_error("Incorrect <if cond=\"[!]symbol\"> element");
777 string cond = xam["cond"];
778 bool state = true;
779 if (cond.substr(0, 1) == "!") {
780 state = false;
781 cond.erase(0, 1);
783 if (window->main->check_condition(cond.c_str()) == state)
784 return;
785 ignore_stack = 1;
786 return;
788 control_container *cc = create_container_from_xml(element, attributes);
789 if (cc != NULL)
791 cc->attribs = xam;
792 cc->create(this, element, xam);
793 gtk_container_set_border_width(cc->container, cc->get_int("border"));
795 container_stack.push_back(cc);
796 current_control = NULL;
797 return;
799 if (!container_stack.empty())
801 current_control = create_control_from_xml(element, attributes);
802 if (current_control)
804 current_control->attribs = xam;
805 int param_no = -1;
806 if (xam.count("param"))
808 map<string, int>::iterator it = param_name_map.find(xam["param"]);
809 if (it == param_name_map.end())
810 g_error("Unknown parameter %s", xam["param"].c_str());
811 else
812 param_no = it->second;
814 current_control->create(this, param_no);
815 current_control->init_xml(element);
816 current_control->set();
817 current_control->hook_params();
818 return;
821 g_error("Unxpected element %s in GUI definition\n", element);
824 void plugin_gui::xml_element_end(void *data, const char *element)
826 plugin_gui *gui = (plugin_gui *)data;
827 if (gui->ignore_stack) {
828 gui->ignore_stack--;
829 return;
831 if (!strcmp(element, "if"))
833 return;
835 if (gui->current_control)
837 (*gui->container_stack.rbegin())->add(gui->current_control->widget, gui->current_control);
838 gui->current_control = NULL;
839 return;
841 unsigned int ss = gui->container_stack.size();
842 if (ss > 1) {
843 gui->container_stack[ss - 2]->add(GTK_WIDGET(gui->container_stack[ss - 1]->container), gui->container_stack[ss - 1]);
845 else
846 gui->top_container = gui->container_stack[0];
847 gui->container_stack.pop_back();
851 GtkWidget *plugin_gui::create_from_xml(plugin_ctl_iface *_plugin, const char *xml)
853 current_control = NULL;
854 top_container = NULL;
855 parser = XML_ParserCreate("UTF-8");
856 plugin = _plugin;
857 container_stack.clear();
858 ignore_stack = 0;
860 param_name_map.clear();
861 int size = plugin->get_param_count();
862 for (int i = 0; i < size; i++)
863 param_name_map[plugin->get_param_props(i)->short_name] = i;
865 XML_SetUserData(parser, this);
866 XML_SetElementHandler(parser, xml_element_start, xml_element_end);
867 XML_Status status = XML_Parse(parser, xml, strlen(xml), 1);
868 if (status == XML_STATUS_ERROR)
870 g_error("Parse error: %s in XML", XML_ErrorString(XML_GetErrorCode(parser)));
873 XML_ParserFree(parser);
874 return GTK_WIDGET(top_container->container);
877 void plugin_gui::send_configure(const char *key, const char *value)
879 // XXXKF this should really be replaced by a separate list of SCI-capable param controls
880 for (unsigned int i = 0; i < params.size(); i++)
882 assert(params[i] != NULL);
883 send_configure_iface *sci = dynamic_cast<send_configure_iface *>(params[i]);
884 if (sci)
885 sci->send_configure(key, value);
889 void plugin_gui::on_idle()
891 for (unsigned int i = 0; i < params.size(); i++)
893 parameter_properties &props = *plugin->get_param_props(params[i]->param_no);
894 bool is_output = (props.flags & PF_PROP_OUTPUT) != 0;
895 if (is_output) {
896 params[i]->set();
898 params[i]->on_idle();
900 // XXXKF iterate over par2ctl, too...
903 void plugin_gui::refresh()
905 for (unsigned int i = 0; i < params.size(); i++)
907 params[i]->set();
908 send_configure_iface *sci = dynamic_cast<send_configure_iface *>(params[i]);
909 if (sci)
910 plugin->send_configures(sci);
914 void plugin_gui::refresh(int param_no, param_control *originator)
916 std::multimap<int, param_control *>::iterator it = par2ctl.find(param_no);
917 while(it != par2ctl.end() && it->first == param_no)
919 if (it->second != originator)
920 it->second->set();
921 it++;
925 void plugin_gui::set_param_value(int param_no, float value, param_control *originator)
927 plugin->set_param_value(param_no, value);
928 refresh(param_no);
931 plugin_gui::~plugin_gui()
933 for (std::vector<param_control *>::iterator i = params.begin(); i != params.end(); i++)
935 delete *i;
940 /******************************* Actions **************************************************/
942 static void store_preset_action(GtkAction *action, plugin_gui_window *gui_win)
944 store_preset(GTK_WINDOW(gui_win->toplevel), gui_win->gui);
947 static const GtkActionEntry actions[] = {
948 { "PresetMenuAction", "", "_Preset", NULL, "Preset operations", NULL },
949 { "BuiltinPresetMenuAction", "", "_Built-in", NULL, "Built-in (factory) presets", NULL },
950 { "UserPresetMenuAction", "", "_User", NULL, "User (your) presets", NULL },
951 { "CommandMenuAction", "", "_Command", NULL, "Plugin-related commands", NULL },
952 { "store-preset", "", "Store preset", NULL, "Store a current setting as preset", (GCallback)store_preset_action },
955 /***************************** GUI window ********************************************/
957 static const char *ui_xml =
958 "<ui>\n"
959 " <menubar>\n"
960 " <menu action=\"PresetMenuAction\">\n"
961 " <menuitem action=\"store-preset\"/>\n"
962 " <separator/>\n"
963 " <placeholder name=\"builtin_presets\"/>\n"
964 " <separator/>\n"
965 " <placeholder name=\"user_presets\"/>\n"
966 " </menu>\n"
967 " <placeholder name=\"commands\"/>\n"
968 " </menubar>\n"
969 "</ui>\n"
972 static const char *general_preset_pre_xml =
973 "<ui>\n"
974 " <menubar>\n"
975 " <menu action=\"PresetMenuAction\">\n";
977 static const char *builtin_preset_pre_xml =
978 // " <menu action=\"BuiltinPresetMenuAction\">\n"
979 " <placeholder name=\"builtin_presets\">\n";
981 static const char *user_preset_pre_xml =
982 // " <menu action=\"UserPresetMenuAction\">\n"
983 " <placeholder name=\"user_presets\">\n";
985 static const char *preset_post_xml =
986 " </placeholder>\n"
987 // " </menu>\n"
988 " </menu>\n"
989 " </menubar>\n"
990 "</ui>\n"
993 static const char *command_pre_xml =
994 "<ui>\n"
995 " <menubar>\n"
996 " <placeholder name=\"commands\">\n"
997 " <menu action=\"CommandMenuAction\">\n";
999 static const char *command_post_xml =
1000 " </menu>\n"
1001 " </placeholder>\n"
1002 " </menubar>\n"
1003 "</ui>\n"
1006 plugin_gui_window::plugin_gui_window(main_window_iface *_main)
1008 toplevel = NULL;
1009 ui_mgr = NULL;
1010 std_actions = NULL;
1011 builtin_preset_actions = NULL;
1012 user_preset_actions = NULL;
1013 command_actions = NULL;
1014 main = _main;
1015 assert(main);
1018 string plugin_gui_window::make_gui_preset_list(GtkActionGroup *grp, bool builtin, char &ch)
1020 string preset_xml = string(general_preset_pre_xml) + (builtin ? builtin_preset_pre_xml : user_preset_pre_xml);
1021 preset_vector &pvec = (builtin ? get_builtin_presets() : get_user_presets()).presets;
1022 GtkActionGroup *preset_actions = builtin ? builtin_preset_actions : user_preset_actions;
1023 for (unsigned int i = 0; i < pvec.size(); i++)
1025 if (pvec[i].plugin != gui->effect_name)
1026 continue;
1027 stringstream ss;
1028 ss << (builtin ? "builtin_preset" : "user_preset") << i;
1029 preset_xml += " <menuitem name=\"" + pvec[i].name+"\" action=\""+ss.str()+"\"/>\n";
1030 if (ch != ' ' && ++ch == ':')
1031 ch = 'A';
1032 if (ch > 'Z')
1033 ch = ' ';
1035 string sv = ss.str();
1036 string prefix = ch == ' ' ? string() : string("_")+ch+" ";
1037 string name = prefix + pvec[i].name;
1038 GtkActionEntry ae = { sv.c_str(), NULL, name.c_str(), NULL, NULL, (GCallback)activate_preset };
1039 gtk_action_group_add_actions_full(preset_actions, &ae, 1, (gpointer)new activate_preset_params(gui, i, builtin), action_destroy_notify);
1041 preset_xml += preset_post_xml;
1042 return preset_xml;
1045 string plugin_gui_window::make_gui_command_list(GtkActionGroup *grp)
1047 string command_xml = command_pre_xml;
1048 plugin_command_info *ci = gui->plugin->get_commands();
1049 if (!ci)
1050 return "";
1051 for(int i = 0; ci->name; i++, ci++)
1053 stringstream ss;
1054 ss << " <menuitem name=\"" << ci->name << "\" action=\"" << ci->label << "\"/>\n";
1056 GtkActionEntry ae = { ci->label, NULL, ci->name, NULL, ci->description, (GCallback)activate_command };
1057 gtk_action_group_add_actions_full(command_actions, &ae, 1, (gpointer)new activate_command_params(gui, i), action_destroy_notify);
1058 command_xml += ss.str();
1060 command_xml += command_post_xml;
1061 return command_xml;
1064 void plugin_gui_window::fill_gui_presets(bool builtin, char &ch)
1066 GtkActionGroup *&preset_actions = builtin ? builtin_preset_actions : user_preset_actions;
1067 if(preset_actions) {
1068 gtk_ui_manager_remove_action_group(ui_mgr, preset_actions);
1069 preset_actions = NULL;
1072 if (builtin)
1073 builtin_preset_actions = gtk_action_group_new("builtin_presets");
1074 else
1075 user_preset_actions = gtk_action_group_new("user_presets");
1076 string preset_xml = make_gui_preset_list(preset_actions, builtin, ch);
1077 gtk_ui_manager_insert_action_group(ui_mgr, preset_actions, 0);
1078 GError *error = NULL;
1079 gtk_ui_manager_add_ui_from_string(ui_mgr, preset_xml.c_str(), -1, &error);
1082 gboolean plugin_gui_window::on_idle(void *data)
1084 plugin_gui_window *self = (plugin_gui_window *)data;
1085 self->gui->on_idle();
1086 return TRUE;
1089 void plugin_gui_window::create(plugin_ctl_iface *_jh, const char *title, const char *effect)
1091 toplevel = GTK_WINDOW(gtk_window_new (GTK_WINDOW_TOPLEVEL));
1092 gtk_window_set_default_icon_name("calf");
1093 gtk_window_set_type_hint(toplevel, GDK_WINDOW_TYPE_HINT_DIALOG);
1094 GtkVBox *vbox = GTK_VBOX(gtk_vbox_new(false, 5));
1096 GtkRequisition req, req2;
1097 gtk_window_set_title(GTK_WINDOW (toplevel), title);
1098 gtk_container_add(GTK_CONTAINER(toplevel), GTK_WIDGET(vbox));
1100 gui = new plugin_gui(this);
1101 gui->effect_name = effect;
1103 ui_mgr = gtk_ui_manager_new();
1104 std_actions = gtk_action_group_new("default");
1105 gtk_action_group_add_actions(std_actions, actions, sizeof(actions)/sizeof(actions[0]), this);
1106 GError *error = NULL;
1107 gtk_ui_manager_insert_action_group(ui_mgr, std_actions, 0);
1108 gtk_ui_manager_add_ui_from_string(ui_mgr, ui_xml, -1, &error);
1110 command_actions = gtk_action_group_new("commands");
1112 char ch = '0';
1113 fill_gui_presets(true, ch);
1114 fill_gui_presets(false, ch);
1116 gtk_box_pack_start(GTK_BOX(vbox), gtk_ui_manager_get_widget(ui_mgr, "/ui/menubar"), false, false, 0);
1118 // determine size without content
1119 gtk_widget_show_all(GTK_WIDGET(vbox));
1120 gtk_widget_size_request(GTK_WIDGET(vbox), &req2);
1121 // printf("size request %dx%d\n", req2.width, req2.height);
1123 GtkWidget *container;
1124 const char *xml = _jh->get_gui_xml();
1125 assert(xml);
1126 container = gui->create_from_xml(_jh, xml);
1128 string command_xml = make_gui_command_list(command_actions);
1129 gtk_ui_manager_insert_action_group(ui_mgr, command_actions, 0);
1130 gtk_ui_manager_add_ui_from_string(ui_mgr, command_xml.c_str(), -1, &error);
1132 GtkWidget *sw = gtk_scrolled_window_new(NULL, NULL);
1133 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(sw), GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
1134 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(sw), GTK_SHADOW_NONE);
1135 gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(sw), container);
1137 gtk_box_pack_start(GTK_BOX(vbox), sw, true, true, 0);
1139 gtk_widget_show_all(GTK_WIDGET(sw));
1140 gtk_widget_size_request(container, &req);
1141 int wx = max(req.width + 10, req2.width);
1142 int wy = req.height + req2.height + 10;
1143 // printf("size request %dx%d\n", req.width, req.height);
1144 // printf("resize to %dx%d\n", max(req.width + 10, req2.width), req.height + req2.height + 10);
1145 gtk_window_set_default_size(GTK_WINDOW(toplevel), wx, wy);
1146 gtk_window_resize(GTK_WINDOW(toplevel), wx, wy);
1147 //gtk_widget_set_size_request(GTK_WIDGET(toplevel), max(req.width + 10, req2.width), req.height + req2.height + 10);
1148 // printf("size set %dx%d\n", wx, wy);
1149 // gtk_scrolled_window_set_vadjustment(GTK_SCROLLED_WINDOW(sw), GTK_ADJUSTMENT(gtk_adjustment_new(0, 0, req.height, 20, 100, 100)));
1150 gtk_signal_connect (GTK_OBJECT (toplevel), "destroy", G_CALLBACK (window_destroyed), (plugin_gui_window *)this);
1151 main->set_window(gui->plugin, this);
1153 source_id = g_timeout_add_full(G_PRIORITY_LOW, 1000/30, on_idle, this, NULL); // 30 fps should be enough for everybody
1154 gtk_ui_manager_ensure_update(ui_mgr);
1155 gui->plugin->send_configures(gui);
1158 void plugin_gui_window::close()
1160 if (source_id)
1161 g_source_remove(source_id);
1162 source_id = 0;
1163 gtk_widget_destroy(GTK_WIDGET(toplevel));
1166 plugin_gui_window::~plugin_gui_window()
1168 if (source_id)
1169 g_source_remove(source_id);
1170 main->set_window(gui->plugin, NULL);
1171 delete gui;
1174 void calf_plugins::activate_command(GtkAction *action, activate_command_params *params)
1176 plugin_gui *gui = params->gui;
1177 gui->plugin->execute(params->function_idx);
1178 gui->refresh();