+ GUI: remove more unused control pointers
[calf.git] / src / gui.cpp
blob746de21423385771270c21fe7a67d19df62103e0
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;
225 param_no = _param_no;
227 widget = gtk_label_new ("");
228 if (param_no != -1)
230 parameter_properties &props = get_props();
231 gtk_label_set_width_chars (GTK_LABEL (widget), props.get_char_count());
233 else
235 require_attribute("key");
236 require_int_attribute("width");
237 param_variable = attribs["key"];
238 gtk_label_set_width_chars (GTK_LABEL (widget), get_int("width"));
240 gtk_misc_set_alignment (GTK_MISC (widget), get_float("align-x", 0.5), get_float("align-y", 0.5));
241 return widget;
244 void value_param_control::set()
246 if (param_no == -1)
247 return;
248 _GUARD_CHANGE_
249 parameter_properties &props = get_props();
250 gtk_label_set_text (GTK_LABEL (widget), props.to_string(gui->plugin->get_param_value(param_no)).c_str());
253 void value_param_control::send_status(const char *key, const char *value)
255 if (key == param_variable)
257 gtk_label_set_text (GTK_LABEL (widget), value);
261 // VU meter
263 GtkWidget *vumeter_param_control::create(plugin_gui *_gui, int _param_no)
265 gui = _gui, param_no = _param_no;
266 // parameter_properties &props = get_props();
267 widget = calf_vumeter_new ();
268 calf_vumeter_set_mode (CALF_VUMETER (widget), (CalfVUMeterMode)get_int("mode", 0));
269 return widget;
272 void vumeter_param_control::set()
274 _GUARD_CHANGE_
275 parameter_properties &props = get_props();
276 calf_vumeter_set_value (CALF_VUMETER (widget), props.to_01(gui->plugin->get_param_value(param_no)));
277 if (label)
278 update_label();
281 // LED
283 GtkWidget *led_param_control::create(plugin_gui *_gui, int _param_no)
285 gui = _gui, param_no = _param_no;
286 // parameter_properties &props = get_props();
287 widget = calf_led_new ();
288 return widget;
291 void led_param_control::set()
293 _GUARD_CHANGE_
294 // parameter_properties &props = get_props();
295 calf_led_set_state (CALF_LED (widget), gui->plugin->get_param_value(param_no) > 0);
296 if (label)
297 update_label();
300 // check box
302 GtkWidget *toggle_param_control::create(plugin_gui *_gui, int _param_no)
304 gui = _gui;
305 param_no = _param_no;
307 widget = gtk_check_button_new ();
308 gtk_signal_connect (GTK_OBJECT (widget), "toggled", G_CALLBACK (toggle_value_changed), (gpointer)this);
309 return widget;
312 void toggle_param_control::toggle_value_changed(GtkCheckButton *widget, gpointer value)
314 param_control *jhp = (param_control *)value;
315 jhp->get();
318 void toggle_param_control::get()
320 const parameter_properties &props = get_props();
321 gui->set_param_value(param_no, gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON(widget)) + props.min, this);
324 void toggle_param_control::set()
326 _GUARD_CHANGE_
327 const parameter_properties &props = get_props();
328 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (widget), (int)gui->plugin->get_param_value(param_no) - (int)props.min);
331 // spin button
333 GtkWidget *spin_param_control::create(plugin_gui *_gui, int _param_no)
335 gui = _gui;
336 param_no = _param_no;
338 const parameter_properties &props = get_props();
339 if (props.step > 1)
340 widget = gtk_spin_button_new_with_range (props.min, props.max, (props.max - props.min) / (props.step - 1));
341 if (props.step > 0)
342 widget = gtk_spin_button_new_with_range (props.min, props.max, props.step);
343 else
344 widget = gtk_spin_button_new_with_range (props.min, props.max, 1);
345 gtk_spin_button_set_digits (GTK_SPIN_BUTTON(widget), get_int("digits", 0));
346 gtk_signal_connect (GTK_OBJECT (widget), "value-changed", G_CALLBACK (value_changed), (gpointer)this);
347 return widget;
350 void spin_param_control::value_changed(GtkSpinButton *widget, gpointer value)
352 param_control *jhp = (param_control *)value;
353 jhp->get();
356 void spin_param_control::get()
358 // const parameter_properties &props = get_props();
359 gui->set_param_value(param_no, gtk_spin_button_get_value_as_float (GTK_SPIN_BUTTON (widget)), this);
362 void spin_param_control::set()
364 _GUARD_CHANGE_
365 // const parameter_properties &props = get_props();
366 gtk_spin_button_set_value (GTK_SPIN_BUTTON (widget), gui->plugin->get_param_value(param_no));
369 // button
371 GtkWidget *button_param_control::create(plugin_gui *_gui, int _param_no)
373 gui = _gui;
374 param_no = _param_no;
376 widget = gtk_button_new_with_label (get_props().name);
377 gtk_signal_connect (GTK_OBJECT (widget), "clicked", G_CALLBACK (button_clicked), (gpointer)this);
378 return widget;
381 void button_param_control::button_clicked(GtkButton *widget, gpointer value)
383 param_control *jhp = (param_control *)value;
385 jhp->get();
388 void button_param_control::get()
390 const parameter_properties &props = get_props();
391 gui->set_param_value(param_no, props.max, this);
394 void button_param_control::set()
396 _GUARD_CHANGE_
397 const parameter_properties &props = get_props();
398 if (gui->plugin->get_param_value(param_no) - props.min >= 0.5)
399 gtk_button_clicked (GTK_BUTTON (widget));
402 // knob
404 GtkWidget *knob_param_control::create(plugin_gui *_gui, int _param_no)
406 gui = _gui;
407 param_no = _param_no;
408 const parameter_properties &props = get_props();
410 //widget = calf_knob_new_with_range (props.to_01 (gui->plugin->get_param_value(param_no)), 0, 1, 0.01);
411 widget = calf_knob_new();
412 float increment = props.get_increment();
413 gtk_range_get_adjustment(GTK_RANGE(widget))->step_increment = increment;
414 CALF_KNOB(widget)->knob_type = get_int("type");
415 gtk_signal_connect(GTK_OBJECT(widget), "value-changed", G_CALLBACK(knob_value_changed), (gpointer)this);
416 return widget;
419 void knob_param_control::get()
421 const parameter_properties &props = get_props();
422 float value = props.from_01(gtk_range_get_value(GTK_RANGE(widget)));
423 gui->set_param_value(param_no, value, this);
424 if (label)
425 update_label();
428 void knob_param_control::set()
430 _GUARD_CHANGE_
431 const parameter_properties &props = get_props();
432 gtk_range_set_value(GTK_RANGE(widget), props.to_01 (gui->plugin->get_param_value(param_no)));
433 if (label)
434 update_label();
437 void knob_param_control::knob_value_changed(GtkWidget *widget, gpointer value)
439 param_control *jhp = (param_control *)value;
440 jhp->get();
443 // keyboard
445 GtkWidget *keyboard_param_control::create(plugin_gui *_gui, int _param_no)
447 gui = _gui;
448 param_no = _param_no;
449 // const parameter_properties &props = get_props();
451 widget = calf_keyboard_new();
452 kb = CALF_KEYBOARD(widget);
453 kb->nkeys = get_int("octaves", 4) * 7 + 1;
454 kb->sink = new CalfKeyboard::EventAdapter;
455 return widget;
458 // curve
460 struct curve_param_control_callback: public CalfCurve::EventAdapter
462 curve_param_control *ctl;
464 curve_param_control_callback(curve_param_control *_ctl)
465 : ctl(_ctl) {}
467 virtual void curve_changed(CalfCurve *src, const CalfCurve::point_vector &data) {
468 stringstream ss;
469 ss << data.size() << endl;
470 for (size_t i = 0; i < data.size(); i++)
471 ss << data[i].first << " " << data[i].second << endl;
472 ctl->gui->plugin->configure(ctl->attribs["key"].c_str(), ss.str().c_str());
474 virtual void clip(CalfCurve *src, int pt, float &x, float &y, bool &hide)
476 // int gridpt = floor(x * 71 * 2);
477 // clip to the middle of the nearest white key
478 x = (floor(x * 71) + 0.5)/ 71.0;
482 GtkWidget *curve_param_control::create(plugin_gui *_gui, int _param_no)
484 gui = _gui;
485 param_no = _param_no;
486 require_attribute("key");
488 widget = calf_curve_new(get_int("maxpoints", -1));
489 curve = CALF_CURVE(widget);
490 curve->sink = new curve_param_control_callback(this);
491 // gtk_curve_set_curve_type(curve, GTK_CURVE_TYPE_LINEAR);
492 return widget;
495 void curve_param_control::send_configure(const char *key, const char *value)
497 // cout << "send conf " << key << endl;
498 if (attribs["key"] == key)
500 stringstream ss(value);
501 CalfCurve::point_vector pts;
502 if (*value)
504 unsigned int npoints = 0;
505 ss >> npoints;
506 unsigned int i;
507 float x = 0, y = 0;
508 for (i = 0; i < npoints && i < curve->point_limit; i++)
510 ss >> x >> y;
511 pts.push_back(CalfCurve::point(x, y));
513 calf_curve_set_points(widget, pts);
518 // entry
520 GtkWidget *entry_param_control::create(plugin_gui *_gui, int _param_no)
522 gui = _gui;
523 param_no = _param_no;
524 require_attribute("key");
526 widget = gtk_entry_new();
527 entry = GTK_ENTRY(widget);
528 gtk_signal_connect(GTK_OBJECT(widget), "changed", G_CALLBACK(entry_value_changed), (gpointer)this);
529 gtk_editable_set_editable(GTK_EDITABLE(entry), get_int("editable", 1));
530 return widget;
533 void entry_param_control::send_configure(const char *key, const char *value)
535 // cout << "send conf " << key << endl;
536 if (attribs["key"] == key)
538 gtk_entry_set_text(entry, value);
542 void entry_param_control::entry_value_changed(GtkWidget *widget, gpointer value)
544 entry_param_control *ctl = (entry_param_control *)value;
545 ctl->gui->plugin->configure(ctl->attribs["key"].c_str(), gtk_entry_get_text(ctl->entry));
548 // filechooser
550 GtkWidget *filechooser_param_control::create(plugin_gui *_gui, int _param_no)
552 gui = _gui;
553 param_no = _param_no;
554 require_attribute("key");
555 require_attribute("title");
557 widget = gtk_file_chooser_button_new(attribs["title"].c_str(), GTK_FILE_CHOOSER_ACTION_OPEN);
558 filechooser = GTK_FILE_CHOOSER_BUTTON(widget);
559 // XXXKF this is GTK+ 2.12 function, does any replacement exist?
560 gtk_signal_connect(GTK_OBJECT(widget), "file-set", G_CALLBACK(filechooser_value_changed), (gpointer)this);
561 if (attribs.count("width"))
562 gtk_widget_set_size_request (widget, get_int("width", 200), -1);
563 if (attribs.count("width_chars"))
564 gtk_file_chooser_button_set_width_chars (filechooser, get_int("width_chars"));
565 return widget;
568 void filechooser_param_control::send_configure(const char *key, const char *value)
570 // cout << "send conf " << key << endl;
571 if (attribs["key"] == key)
573 gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(filechooser), value);
577 void filechooser_param_control::filechooser_value_changed(GtkWidget *widget, gpointer value)
579 filechooser_param_control *ctl = (filechooser_param_control *)value;
580 const char *filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(ctl->filechooser));
581 if (filename)
582 ctl->gui->plugin->configure(ctl->attribs["key"].c_str(), filename);
585 // line graph
587 void line_graph_param_control::on_idle()
589 if (get_int("refresh", 0))
590 set();
593 GtkWidget *line_graph_param_control::create(plugin_gui *_gui, int _param_no)
595 gui = _gui;
596 param_no = _param_no;
597 last_generation = -1;
598 // const parameter_properties &props = get_props();
600 widget = calf_line_graph_new ();
601 CalfLineGraph *clg = CALF_LINE_GRAPH(widget);
602 widget->requisition.width = get_int("width", 40);
603 widget->requisition.height = get_int("height", 40);
604 calf_line_graph_set_square(clg, get_int("square", 0));
605 clg->source = gui->plugin->get_line_graph_iface();
606 clg->source_id = param_no;
608 return widget;
611 void line_graph_param_control::set()
613 GtkWidget *tw = gtk_widget_get_toplevel(widget);
614 if (tw && GTK_WIDGET_TOPLEVEL(tw) && widget->window)
616 int ws = gdk_window_get_state(widget->window);
617 if (ws & (GDK_WINDOW_STATE_WITHDRAWN | GDK_WINDOW_STATE_ICONIFIED))
618 return;
619 last_generation = calf_line_graph_update_if(CALF_LINE_GRAPH(widget), last_generation);
623 line_graph_param_control::~line_graph_param_control()
627 /******************************** GUI proper ********************************/
629 plugin_gui::plugin_gui(plugin_gui_window *_window)
630 : last_status_serial_no(0)
631 , window(_window)
633 ignore_stack = 0;
634 top_container = NULL;
635 current_control = NULL;
636 param_count = 0;
637 container = NULL;
638 effect_name = NULL;
641 static void window_destroyed(GtkWidget *window, gpointer data)
643 delete (plugin_gui_window *)data;
646 static void action_destroy_notify(gpointer data)
648 delete (activate_preset_params *)data;
651 void control_base::require_attribute(const char *name)
653 if (attribs.count(name) == 0) {
654 g_error("Missing attribute: %s", name);
658 void control_base::require_int_attribute(const char *name)
660 if (attribs.count(name) == 0) {
661 g_error("Missing attribute: %s", name);
663 if (attribs[name].empty() || attribs[name].find_first_not_of("0123456789") != string::npos) {
664 g_error("Wrong data type on attribute: %s (required integer)", name);
668 int control_base::get_int(const char *name, int def_value)
670 if (attribs.count(name) == 0)
671 return def_value;
672 const std::string &v = attribs[name];
673 if (v.empty() || v.find_first_not_of("-+0123456789") != string::npos)
674 return def_value;
675 return atoi(v.c_str());
678 float control_base::get_float(const char *name, float def_value)
680 if (attribs.count(name) == 0)
681 return def_value;
682 const std::string &v = attribs[name];
683 if (v.empty() || v.find_first_not_of("-+0123456789.") != string::npos)
684 return def_value;
685 stringstream ss(v);
686 float value;
687 ss >> value;
688 return value;
691 /******************************** GtkTable container ********************************/
693 GtkWidget *table_container::create(plugin_gui *_gui, const char *element, xml_attribute_map &attributes)
695 require_int_attribute("rows");
696 require_int_attribute("cols");
697 GtkWidget *table = gtk_table_new(get_int("rows", 1), get_int("cols", 1), false);
698 container = GTK_CONTAINER(table);
699 return table;
702 void table_container::add(GtkWidget *widget, control_base *base)
704 base->require_int_attribute("attach-x");
705 base->require_int_attribute("attach-y");
706 int x = base->get_int("attach-x"), y = base->get_int("attach-y");
707 int w = base->get_int("attach-w", 1), h = base->get_int("attach-h", 1);
708 int shrinkx = base->get_int("shrink-x", 0);
709 int shrinky = base->get_int("shrink-y", 0);
710 int fillx = (base->get_int("fill-x", !shrinkx) ? GTK_FILL : 0) | (base->get_int("expand-x", !shrinkx) ? GTK_EXPAND : 0) | (shrinkx ? GTK_SHRINK : 0);
711 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);
712 int padx = base->get_int("pad-x", 2);
713 int pady = base->get_int("pad-y", 2);
714 gtk_table_attach(GTK_TABLE(container), widget, x, x + w, y, y + h, (GtkAttachOptions)fillx, (GtkAttachOptions)filly, padx, pady);
717 /******************************** alignment contaner ********************************/
719 GtkWidget *alignment_container::create(plugin_gui *_gui, const char *element, xml_attribute_map &attributes)
721 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));
722 container = GTK_CONTAINER(align);
723 return align;
726 /******************************** GtkFrame contaner ********************************/
728 GtkWidget *frame_container::create(plugin_gui *_gui, const char *element, xml_attribute_map &attributes)
730 GtkWidget *frame = gtk_frame_new(attribs["label"].c_str());
731 container = GTK_CONTAINER(frame);
732 return frame;
735 /******************************** GtkBox type of containers ********************************/
737 void box_container::add(GtkWidget *w, control_base *base)
739 gtk_container_add_with_properties(container, w, "expand", get_int("expand", 1), "fill", get_int("fill", 1), NULL);
742 /******************************** GtkHBox container ********************************/
744 GtkWidget *hbox_container::create(plugin_gui *_gui, const char *element, xml_attribute_map &attributes)
746 GtkWidget *hbox = gtk_hbox_new(false, get_int("spacing", 2));
747 container = GTK_CONTAINER(hbox);
748 return hbox;
751 /******************************** GtkVBox container ********************************/
753 GtkWidget *vbox_container::create(plugin_gui *_gui, const char *element, xml_attribute_map &attributes)
755 GtkWidget *vbox = gtk_vbox_new(false, get_int("spacing", 2));
756 container = GTK_CONTAINER(vbox);
757 return vbox;
760 /******************************** GtkNotebook container ********************************/
762 GtkWidget *notebook_container::create(plugin_gui *_gui, const char *element, xml_attribute_map &attributes)
764 GtkWidget *nb = gtk_notebook_new();
765 container = GTK_CONTAINER(nb);
766 return nb;
769 void notebook_container::add(GtkWidget *w, control_base *base)
771 gtk_notebook_append_page(GTK_NOTEBOOK(container), w, gtk_label_new_with_mnemonic(base->attribs["page"].c_str()));
774 /******************************** GtkNotebook container ********************************/
776 GtkWidget *scrolled_container::create(plugin_gui *_gui, const char *element, xml_attribute_map &attributes)
778 GtkAdjustment *horiz = NULL, *vert = NULL;
779 int width = get_int("width", 0), height = get_int("height", 0);
780 if (width)
781 horiz = GTK_ADJUSTMENT(gtk_adjustment_new(get_int("x", 0), 0, width, get_int("step-x", 1), get_int("page-x", width / 10), 100));
782 if (height)
783 vert = GTK_ADJUSTMENT(gtk_adjustment_new(get_int("y", 0), 0, width, get_int("step-y", 1), get_int("page-y", height / 10), 10));
784 GtkWidget *sw = gtk_scrolled_window_new(horiz, vert);
785 gtk_widget_set_size_request(sw, get_int("req-x", -1), get_int("req-y", -1));
786 container = GTK_CONTAINER(sw);
787 return sw;
790 void scrolled_container::add(GtkWidget *w, control_base *base)
792 gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(container), w);
795 /******************************** GUI proper ********************************/
797 param_control *plugin_gui::create_control_from_xml(const char *element, const char *attributes[])
799 if (!strcmp(element, "knob"))
800 return new knob_param_control;
801 if (!strcmp(element, "hscale"))
802 return new hscale_param_control;
803 if (!strcmp(element, "vscale"))
804 return new vscale_param_control;
805 if (!strcmp(element, "combo"))
806 return new combo_box_param_control;
807 if (!strcmp(element, "toggle"))
808 return new toggle_param_control;
809 if (!strcmp(element, "spin"))
810 return new spin_param_control;
811 if (!strcmp(element, "button"))
812 return new button_param_control;
813 if (!strcmp(element, "label"))
814 return new label_param_control;
815 if (!strcmp(element, "value"))
816 return new value_param_control;
817 if (!strcmp(element, "vumeter"))
818 return new vumeter_param_control;
819 if (!strcmp(element, "line-graph"))
820 return new line_graph_param_control;
821 if (!strcmp(element, "keyboard"))
822 return new keyboard_param_control;
823 if (!strcmp(element, "curve"))
824 return new curve_param_control;
825 if (!strcmp(element, "led"))
826 return new led_param_control;
827 if (!strcmp(element, "entry"))
828 return new entry_param_control;
829 if (!strcmp(element, "filechooser"))
830 return new filechooser_param_control;
831 return NULL;
834 control_container *plugin_gui::create_container_from_xml(const char *element, const char *attributes[])
836 if (!strcmp(element, "table"))
837 return new table_container;
838 if (!strcmp(element, "vbox"))
839 return new vbox_container;
840 if (!strcmp(element, "hbox"))
841 return new hbox_container;
842 if (!strcmp(element, "align"))
843 return new alignment_container;
844 if (!strcmp(element, "frame"))
845 return new frame_container;
846 if (!strcmp(element, "notebook"))
847 return new notebook_container;
848 if (!strcmp(element, "scrolled"))
849 return new scrolled_container;
850 return NULL;
853 void plugin_gui::xml_element_start(void *data, const char *element, const char *attributes[])
855 plugin_gui *gui = (plugin_gui *)data;
856 gui->xml_element_start(element, attributes);
859 void plugin_gui::xml_element_start(const char *element, const char *attributes[])
861 if (ignore_stack) {
862 ignore_stack++;
863 return;
865 control_base::xml_attribute_map xam;
866 while(*attributes)
868 xam[attributes[0]] = attributes[1];
869 attributes += 2;
872 if (!strcmp(element, "if"))
874 if (!xam.count("cond") || xam["cond"].empty())
876 g_error("Incorrect <if cond=\"[!]symbol\"> element");
878 string cond = xam["cond"];
879 bool state = true;
880 if (cond.substr(0, 1) == "!") {
881 state = false;
882 cond.erase(0, 1);
884 if (window->main->check_condition(cond.c_str()) == state)
885 return;
886 ignore_stack = 1;
887 return;
889 control_container *cc = create_container_from_xml(element, attributes);
890 if (cc != NULL)
892 cc->attribs = xam;
893 cc->create(this, element, xam);
894 gtk_container_set_border_width(cc->container, cc->get_int("border"));
896 container_stack.push_back(cc);
897 current_control = NULL;
898 return;
900 if (!container_stack.empty())
902 current_control = create_control_from_xml(element, attributes);
903 if (current_control)
905 current_control->attribs = xam;
906 int param_no = -1;
907 if (xam.count("param"))
909 map<string, int>::iterator it = param_name_map.find(xam["param"]);
910 if (it == param_name_map.end())
911 g_error("Unknown parameter %s", xam["param"].c_str());
912 else
913 param_no = it->second;
915 current_control->create(this, param_no);
916 current_control->init_xml(element);
917 current_control->set();
918 current_control->hook_params();
919 return;
922 g_error("Unxpected element %s in GUI definition\n", element);
925 void plugin_gui::xml_element_end(void *data, const char *element)
927 plugin_gui *gui = (plugin_gui *)data;
928 if (gui->ignore_stack) {
929 gui->ignore_stack--;
930 return;
932 if (!strcmp(element, "if"))
934 return;
936 if (gui->current_control)
938 (*gui->container_stack.rbegin())->add(gui->current_control->widget, gui->current_control);
939 gui->current_control = NULL;
940 return;
942 unsigned int ss = gui->container_stack.size();
943 if (ss > 1) {
944 gui->container_stack[ss - 2]->add(GTK_WIDGET(gui->container_stack[ss - 1]->container), gui->container_stack[ss - 1]);
946 else
947 gui->top_container = gui->container_stack[0];
948 gui->container_stack.pop_back();
952 GtkWidget *plugin_gui::create_from_xml(plugin_ctl_iface *_plugin, const char *xml)
954 current_control = NULL;
955 top_container = NULL;
956 parser = XML_ParserCreate("UTF-8");
957 plugin = _plugin;
958 container_stack.clear();
959 ignore_stack = 0;
961 param_name_map.clear();
962 int size = plugin->get_param_count();
963 for (int i = 0; i < size; i++)
964 param_name_map[plugin->get_param_props(i)->short_name] = i;
966 XML_SetUserData(parser, this);
967 XML_SetElementHandler(parser, xml_element_start, xml_element_end);
968 XML_Status status = XML_Parse(parser, xml, strlen(xml), 1);
969 if (status == XML_STATUS_ERROR)
971 g_error("Parse error: %s in XML", XML_ErrorString(XML_GetErrorCode(parser)));
974 XML_ParserFree(parser);
975 last_status_serial_no = plugin->send_status_updates(this, 0);
976 return GTK_WIDGET(top_container->container);
979 void plugin_gui::send_configure(const char *key, const char *value)
981 // XXXKF this should really be replaced by a separate list of SCI-capable param controls
982 for (unsigned int i = 0; i < params.size(); i++)
984 assert(params[i] != NULL);
985 send_configure_iface *sci = dynamic_cast<send_configure_iface *>(params[i]);
986 if (sci)
987 sci->send_configure(key, value);
991 void plugin_gui::send_status(const char *key, const char *value)
993 // XXXKF this should really be replaced by a separate list of SUI-capable param controls
994 for (unsigned int i = 0; i < params.size(); i++)
996 assert(params[i] != NULL);
997 send_updates_iface *sui = dynamic_cast<send_updates_iface *>(params[i]);
998 if (sui)
999 sui->send_status(key, value);
1003 void plugin_gui::on_idle()
1005 for (unsigned int i = 0; i < params.size(); i++)
1007 if (params[i]->param_no != -1)
1009 parameter_properties &props = *plugin->get_param_props(params[i]->param_no);
1010 bool is_output = (props.flags & PF_PROP_OUTPUT) != 0;
1011 if (is_output) {
1012 params[i]->set();
1015 params[i]->on_idle();
1017 last_status_serial_no = plugin->send_status_updates(this, last_status_serial_no);
1018 // XXXKF iterate over par2ctl, too...
1021 void plugin_gui::refresh()
1023 for (unsigned int i = 0; i < params.size(); i++)
1024 params[i]->set();
1025 plugin->send_configures(this);
1026 last_status_serial_no = plugin->send_status_updates(this, last_status_serial_no);
1029 void plugin_gui::refresh(int param_no, param_control *originator)
1031 std::multimap<int, param_control *>::iterator it = par2ctl.find(param_no);
1032 while(it != par2ctl.end() && it->first == param_no)
1034 if (it->second != originator)
1035 it->second->set();
1036 it++;
1040 void plugin_gui::set_param_value(int param_no, float value, param_control *originator)
1042 plugin->set_param_value(param_no, value);
1043 refresh(param_no);
1046 plugin_gui::~plugin_gui()
1048 for (std::vector<param_control *>::iterator i = params.begin(); i != params.end(); i++)
1050 delete *i;
1055 /******************************* Actions **************************************************/
1057 static void store_preset_action(GtkAction *action, plugin_gui_window *gui_win)
1059 store_preset(GTK_WINDOW(gui_win->toplevel), gui_win->gui);
1062 static const GtkActionEntry actions[] = {
1063 { "PresetMenuAction", "", "_Preset", NULL, "Preset operations", NULL },
1064 { "BuiltinPresetMenuAction", "", "_Built-in", NULL, "Built-in (factory) presets", NULL },
1065 { "UserPresetMenuAction", "", "_User", NULL, "User (your) presets", NULL },
1066 { "CommandMenuAction", "", "_Command", NULL, "Plugin-related commands", NULL },
1067 { "store-preset", "", "Store preset", NULL, "Store a current setting as preset", (GCallback)store_preset_action },
1070 /***************************** GUI window ********************************************/
1072 static const char *ui_xml =
1073 "<ui>\n"
1074 " <menubar>\n"
1075 " <menu action=\"PresetMenuAction\">\n"
1076 " <menuitem action=\"store-preset\"/>\n"
1077 " <separator/>\n"
1078 " <placeholder name=\"builtin_presets\"/>\n"
1079 " <separator/>\n"
1080 " <placeholder name=\"user_presets\"/>\n"
1081 " </menu>\n"
1082 " <placeholder name=\"commands\"/>\n"
1083 " </menubar>\n"
1084 "</ui>\n"
1087 static const char *general_preset_pre_xml =
1088 "<ui>\n"
1089 " <menubar>\n"
1090 " <menu action=\"PresetMenuAction\">\n";
1092 static const char *builtin_preset_pre_xml =
1093 // " <menu action=\"BuiltinPresetMenuAction\">\n"
1094 " <placeholder name=\"builtin_presets\">\n";
1096 static const char *user_preset_pre_xml =
1097 // " <menu action=\"UserPresetMenuAction\">\n"
1098 " <placeholder name=\"user_presets\">\n";
1100 static const char *preset_post_xml =
1101 " </placeholder>\n"
1102 // " </menu>\n"
1103 " </menu>\n"
1104 " </menubar>\n"
1105 "</ui>\n"
1108 static const char *command_pre_xml =
1109 "<ui>\n"
1110 " <menubar>\n"
1111 " <placeholder name=\"commands\">\n"
1112 " <menu action=\"CommandMenuAction\">\n";
1114 static const char *command_post_xml =
1115 " </menu>\n"
1116 " </placeholder>\n"
1117 " </menubar>\n"
1118 "</ui>\n"
1121 plugin_gui_window::plugin_gui_window(main_window_iface *_main)
1123 toplevel = NULL;
1124 ui_mgr = NULL;
1125 std_actions = NULL;
1126 builtin_preset_actions = NULL;
1127 user_preset_actions = NULL;
1128 command_actions = NULL;
1129 main = _main;
1130 assert(main);
1133 string plugin_gui_window::make_gui_preset_list(GtkActionGroup *grp, bool builtin, char &ch)
1135 string preset_xml = string(general_preset_pre_xml) + (builtin ? builtin_preset_pre_xml : user_preset_pre_xml);
1136 preset_vector &pvec = (builtin ? get_builtin_presets() : get_user_presets()).presets;
1137 GtkActionGroup *preset_actions = builtin ? builtin_preset_actions : user_preset_actions;
1138 for (unsigned int i = 0; i < pvec.size(); i++)
1140 if (pvec[i].plugin != gui->effect_name)
1141 continue;
1142 stringstream ss;
1143 ss << (builtin ? "builtin_preset" : "user_preset") << i;
1144 preset_xml += " <menuitem name=\"" + pvec[i].name+"\" action=\""+ss.str()+"\"/>\n";
1145 if (ch != ' ' && ++ch == ':')
1146 ch = 'A';
1147 if (ch > 'Z')
1148 ch = ' ';
1150 string sv = ss.str();
1151 string prefix = ch == ' ' ? string() : string("_")+ch+" ";
1152 string name = prefix + pvec[i].name;
1153 GtkActionEntry ae = { sv.c_str(), NULL, name.c_str(), NULL, NULL, (GCallback)activate_preset };
1154 gtk_action_group_add_actions_full(preset_actions, &ae, 1, (gpointer)new activate_preset_params(gui, i, builtin), action_destroy_notify);
1156 preset_xml += preset_post_xml;
1157 return preset_xml;
1160 string plugin_gui_window::make_gui_command_list(GtkActionGroup *grp)
1162 string command_xml = command_pre_xml;
1163 plugin_command_info *ci = gui->plugin->get_commands();
1164 if (!ci)
1165 return "";
1166 for(int i = 0; ci->name; i++, ci++)
1168 stringstream ss;
1169 ss << " <menuitem name=\"" << ci->name << "\" action=\"" << ci->label << "\"/>\n";
1171 GtkActionEntry ae = { ci->label, NULL, ci->name, NULL, ci->description, (GCallback)activate_command };
1172 gtk_action_group_add_actions_full(command_actions, &ae, 1, (gpointer)new activate_command_params(gui, i), action_destroy_notify);
1173 command_xml += ss.str();
1175 command_xml += command_post_xml;
1176 return command_xml;
1179 void plugin_gui_window::fill_gui_presets(bool builtin, char &ch)
1181 GtkActionGroup *&preset_actions = builtin ? builtin_preset_actions : user_preset_actions;
1182 if(preset_actions) {
1183 gtk_ui_manager_remove_action_group(ui_mgr, preset_actions);
1184 preset_actions = NULL;
1187 if (builtin)
1188 builtin_preset_actions = gtk_action_group_new("builtin_presets");
1189 else
1190 user_preset_actions = gtk_action_group_new("user_presets");
1191 string preset_xml = make_gui_preset_list(preset_actions, builtin, ch);
1192 gtk_ui_manager_insert_action_group(ui_mgr, preset_actions, 0);
1193 GError *error = NULL;
1194 gtk_ui_manager_add_ui_from_string(ui_mgr, preset_xml.c_str(), -1, &error);
1197 gboolean plugin_gui_window::on_idle(void *data)
1199 plugin_gui_window *self = (plugin_gui_window *)data;
1200 self->gui->on_idle();
1201 return TRUE;
1204 void plugin_gui_window::create(plugin_ctl_iface *_jh, const char *title, const char *effect)
1206 toplevel = GTK_WINDOW(gtk_window_new (GTK_WINDOW_TOPLEVEL));
1207 gtk_window_set_default_icon_name("calf");
1208 gtk_window_set_type_hint(toplevel, GDK_WINDOW_TYPE_HINT_DIALOG);
1209 GtkVBox *vbox = GTK_VBOX(gtk_vbox_new(false, 5));
1211 GtkRequisition req, req2;
1212 gtk_window_set_title(GTK_WINDOW (toplevel), title);
1213 gtk_container_add(GTK_CONTAINER(toplevel), GTK_WIDGET(vbox));
1215 gui = new plugin_gui(this);
1216 gui->effect_name = effect;
1218 ui_mgr = gtk_ui_manager_new();
1219 std_actions = gtk_action_group_new("default");
1220 gtk_action_group_add_actions(std_actions, actions, sizeof(actions)/sizeof(actions[0]), this);
1221 GError *error = NULL;
1222 gtk_ui_manager_insert_action_group(ui_mgr, std_actions, 0);
1223 gtk_ui_manager_add_ui_from_string(ui_mgr, ui_xml, -1, &error);
1225 command_actions = gtk_action_group_new("commands");
1227 char ch = '0';
1228 fill_gui_presets(true, ch);
1229 fill_gui_presets(false, ch);
1231 gtk_box_pack_start(GTK_BOX(vbox), gtk_ui_manager_get_widget(ui_mgr, "/ui/menubar"), false, false, 0);
1233 // determine size without content
1234 gtk_widget_show_all(GTK_WIDGET(vbox));
1235 gtk_widget_size_request(GTK_WIDGET(vbox), &req2);
1236 // printf("size request %dx%d\n", req2.width, req2.height);
1238 GtkWidget *container;
1239 const char *xml = _jh->get_gui_xml();
1240 assert(xml);
1241 container = gui->create_from_xml(_jh, xml);
1243 string command_xml = make_gui_command_list(command_actions);
1244 gtk_ui_manager_insert_action_group(ui_mgr, command_actions, 0);
1245 gtk_ui_manager_add_ui_from_string(ui_mgr, command_xml.c_str(), -1, &error);
1247 GtkWidget *sw = gtk_scrolled_window_new(NULL, NULL);
1248 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(sw), GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
1249 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(sw), GTK_SHADOW_NONE);
1250 gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(sw), container);
1252 gtk_box_pack_start(GTK_BOX(vbox), sw, true, true, 0);
1254 gtk_widget_show_all(GTK_WIDGET(sw));
1255 gtk_widget_size_request(container, &req);
1256 int wx = max(req.width + 10, req2.width);
1257 int wy = req.height + req2.height + 10;
1258 // printf("size request %dx%d\n", req.width, req.height);
1259 // printf("resize to %dx%d\n", max(req.width + 10, req2.width), req.height + req2.height + 10);
1260 gtk_window_set_default_size(GTK_WINDOW(toplevel), wx, wy);
1261 gtk_window_resize(GTK_WINDOW(toplevel), wx, wy);
1262 //gtk_widget_set_size_request(GTK_WIDGET(toplevel), max(req.width + 10, req2.width), req.height + req2.height + 10);
1263 // printf("size set %dx%d\n", wx, wy);
1264 // gtk_scrolled_window_set_vadjustment(GTK_SCROLLED_WINDOW(sw), GTK_ADJUSTMENT(gtk_adjustment_new(0, 0, req.height, 20, 100, 100)));
1265 gtk_signal_connect (GTK_OBJECT (toplevel), "destroy", G_CALLBACK (window_destroyed), (plugin_gui_window *)this);
1266 main->set_window(gui->plugin, this);
1268 source_id = g_timeout_add_full(G_PRIORITY_LOW, 1000/30, on_idle, this, NULL); // 30 fps should be enough for everybody
1269 gtk_ui_manager_ensure_update(ui_mgr);
1270 gui->plugin->send_configures(gui);
1273 void plugin_gui_window::close()
1275 if (source_id)
1276 g_source_remove(source_id);
1277 source_id = 0;
1278 gtk_widget_destroy(GTK_WIDGET(toplevel));
1281 plugin_gui_window::~plugin_gui_window()
1283 if (source_id)
1284 g_source_remove(source_id);
1285 main->set_window(gui->plugin, NULL);
1286 delete gui;
1289 void calf_plugins::activate_command(GtkAction *action, activate_command_params *params)
1291 plugin_gui *gui = params->gui;
1292 gui->plugin->execute(params->function_idx);
1293 gui->refresh();