+ GUI: add two new widget wrappers - entry and filechooser
[calf.git] / src / gui.cpp
blobec6f08b67b71f92417e42ba4d38c1dfcf2008b23
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 // entry
498 GtkWidget *entry_param_control::create(plugin_gui *_gui, int _param_no)
500 gui = _gui;
501 param_no = _param_no;
502 require_attribute("key");
504 widget = gtk_entry_new();
505 entry = GTK_ENTRY(widget);
506 gtk_signal_connect(GTK_OBJECT(widget), "changed", G_CALLBACK(entry_value_changed), (gpointer)this);
507 gtk_editable_set_editable(GTK_EDITABLE(entry), get_int("editable", 1));
508 return widget;
511 void entry_param_control::send_configure(const char *key, const char *value)
513 // cout << "send conf " << key << endl;
514 if (attribs["key"] == key)
516 gtk_entry_set_text(entry, value);
520 void entry_param_control::entry_value_changed(GtkWidget *widget, gpointer value)
522 entry_param_control *ctl = (entry_param_control *)value;
523 ctl->gui->plugin->configure(ctl->attribs["key"].c_str(), gtk_entry_get_text(ctl->entry));
526 // filechooser
528 GtkWidget *filechooser_param_control::create(plugin_gui *_gui, int _param_no)
530 gui = _gui;
531 param_no = _param_no;
532 require_attribute("key");
533 require_attribute("title");
535 widget = gtk_file_chooser_button_new(attribs["title"].c_str(), GTK_FILE_CHOOSER_ACTION_OPEN);
536 filechooser = GTK_FILE_CHOOSER_BUTTON(widget);
537 // XXXKF this is GTK+ 2.12 function, does any replacement exist?
538 gtk_signal_connect(GTK_OBJECT(widget), "file-set", G_CALLBACK(filechooser_value_changed), (gpointer)this);
539 if (attribs.count("width"))
540 gtk_widget_set_size_request (widget, get_int("width", 200), -1);
541 if (attribs.count("width_chars"))
542 gtk_file_chooser_button_set_width_chars (filechooser, get_int("width_chars"));
543 return widget;
546 void filechooser_param_control::send_configure(const char *key, const char *value)
548 // cout << "send conf " << key << endl;
549 if (attribs["key"] == key)
551 gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(filechooser), value);
555 void filechooser_param_control::filechooser_value_changed(GtkWidget *widget, gpointer value)
557 filechooser_param_control *ctl = (filechooser_param_control *)value;
558 ctl->gui->plugin->configure(ctl->attribs["key"].c_str(), gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(ctl->filechooser)));
561 // line graph
563 void line_graph_param_control::on_idle()
565 if (get_int("refresh", 0))
566 set();
569 GtkWidget *line_graph_param_control::create(plugin_gui *_gui, int _param_no)
571 gui = _gui;
572 param_no = _param_no;
573 last_generation = -1;
574 // const parameter_properties &props = get_props();
576 widget = calf_line_graph_new ();
577 CalfLineGraph *clg = CALF_LINE_GRAPH(widget);
578 widget->requisition.width = get_int("width", 40);
579 widget->requisition.height = get_int("height", 40);
580 calf_line_graph_set_square(clg, get_int("square", 0));
581 clg->source = gui->plugin->get_line_graph_iface();
582 clg->source_id = param_no;
584 return widget;
587 void line_graph_param_control::set()
589 GtkWidget *tw = gtk_widget_get_toplevel(widget);
590 if (tw && GTK_WIDGET_TOPLEVEL(tw) && widget->window)
592 int ws = gdk_window_get_state(widget->window);
593 if (ws & (GDK_WINDOW_STATE_WITHDRAWN | GDK_WINDOW_STATE_ICONIFIED))
594 return;
595 last_generation = calf_line_graph_update_if(CALF_LINE_GRAPH(widget), last_generation);
599 line_graph_param_control::~line_graph_param_control()
603 /******************************** GUI proper ********************************/
605 plugin_gui::plugin_gui(plugin_gui_window *_window)
606 : window(_window)
611 static void window_destroyed(GtkWidget *window, gpointer data)
613 delete (plugin_gui_window *)data;
616 static void action_destroy_notify(gpointer data)
618 delete (activate_preset_params *)data;
621 void control_base::require_attribute(const char *name)
623 if (attribs.count(name) == 0) {
624 g_error("Missing attribute: %s", name);
628 void control_base::require_int_attribute(const char *name)
630 if (attribs.count(name) == 0) {
631 g_error("Missing attribute: %s", name);
633 if (attribs[name].empty() || attribs[name].find_first_not_of("0123456789") != string::npos) {
634 g_error("Wrong data type on attribute: %s (required integer)", name);
638 int control_base::get_int(const char *name, int def_value)
640 if (attribs.count(name) == 0)
641 return def_value;
642 const std::string &v = attribs[name];
643 if (v.empty() || v.find_first_not_of("-+0123456789") != string::npos)
644 return def_value;
645 return atoi(v.c_str());
648 float control_base::get_float(const char *name, float def_value)
650 if (attribs.count(name) == 0)
651 return def_value;
652 const std::string &v = attribs[name];
653 if (v.empty() || v.find_first_not_of("-+0123456789.") != string::npos)
654 return def_value;
655 stringstream ss(v);
656 float value;
657 ss >> value;
658 return value;
661 /******************************** GtkTable container ********************************/
663 GtkWidget *table_container::create(plugin_gui *_gui, const char *element, xml_attribute_map &attributes)
665 require_int_attribute("rows");
666 require_int_attribute("cols");
667 GtkWidget *table = gtk_table_new(get_int("rows", 1), get_int("cols", 1), false);
668 container = GTK_CONTAINER(table);
669 return table;
672 void table_container::add(GtkWidget *widget, control_base *base)
674 base->require_int_attribute("attach-x");
675 base->require_int_attribute("attach-y");
676 int x = base->get_int("attach-x"), y = base->get_int("attach-y");
677 int w = base->get_int("attach-w", 1), h = base->get_int("attach-h", 1);
678 int shrinkx = base->get_int("shrink-x", 0);
679 int shrinky = base->get_int("shrink-y", 0);
680 int fillx = (base->get_int("fill-x", !shrinkx) ? GTK_FILL : 0) | (base->get_int("expand-x", !shrinkx) ? GTK_EXPAND : 0) | (shrinkx ? GTK_SHRINK : 0);
681 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);
682 int padx = base->get_int("pad-x", 2);
683 int pady = base->get_int("pad-y", 2);
684 gtk_table_attach(GTK_TABLE(container), widget, x, x + w, y, y + h, (GtkAttachOptions)fillx, (GtkAttachOptions)filly, padx, pady);
687 /******************************** alignment contaner ********************************/
689 GtkWidget *alignment_container::create(plugin_gui *_gui, const char *element, xml_attribute_map &attributes)
691 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));
692 container = GTK_CONTAINER(align);
693 return align;
696 /******************************** GtkFrame contaner ********************************/
698 GtkWidget *frame_container::create(plugin_gui *_gui, const char *element, xml_attribute_map &attributes)
700 GtkWidget *frame = gtk_frame_new(attribs["label"].c_str());
701 container = GTK_CONTAINER(frame);
702 return frame;
705 /******************************** GtkBox type of containers ********************************/
707 void box_container::add(GtkWidget *w, control_base *base)
709 gtk_container_add_with_properties(container, w, "expand", get_int("expand", 1), "fill", get_int("fill", 1), NULL);
712 /******************************** GtkHBox container ********************************/
714 GtkWidget *hbox_container::create(plugin_gui *_gui, const char *element, xml_attribute_map &attributes)
716 GtkWidget *hbox = gtk_hbox_new(false, get_int("spacing", 2));
717 container = GTK_CONTAINER(hbox);
718 return hbox;
721 /******************************** GtkVBox container ********************************/
723 GtkWidget *vbox_container::create(plugin_gui *_gui, const char *element, xml_attribute_map &attributes)
725 GtkWidget *vbox = gtk_vbox_new(false, get_int("spacing", 2));
726 container = GTK_CONTAINER(vbox);
727 return vbox;
730 /******************************** GtkNotebook container ********************************/
732 GtkWidget *notebook_container::create(plugin_gui *_gui, const char *element, xml_attribute_map &attributes)
734 GtkWidget *nb = gtk_notebook_new();
735 container = GTK_CONTAINER(nb);
736 return nb;
739 void notebook_container::add(GtkWidget *w, control_base *base)
741 gtk_notebook_append_page(GTK_NOTEBOOK(container), w, gtk_label_new_with_mnemonic(base->attribs["page"].c_str()));
744 /******************************** GtkNotebook container ********************************/
746 GtkWidget *scrolled_container::create(plugin_gui *_gui, const char *element, xml_attribute_map &attributes)
748 GtkAdjustment *horiz = NULL, *vert = NULL;
749 int width = get_int("width", 0), height = get_int("height", 0);
750 if (width)
751 horiz = GTK_ADJUSTMENT(gtk_adjustment_new(get_int("x", 0), 0, width, get_int("step-x", 1), get_int("page-x", width / 10), 100));
752 if (height)
753 vert = GTK_ADJUSTMENT(gtk_adjustment_new(get_int("y", 0), 0, width, get_int("step-y", 1), get_int("page-y", height / 10), 10));
754 GtkWidget *sw = gtk_scrolled_window_new(horiz, vert);
755 gtk_widget_set_size_request(sw, get_int("req-x", -1), get_int("req-y", -1));
756 container = GTK_CONTAINER(sw);
757 return sw;
760 void scrolled_container::add(GtkWidget *w, control_base *base)
762 gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(container), w);
765 /******************************** GUI proper ********************************/
767 param_control *plugin_gui::create_control_from_xml(const char *element, const char *attributes[])
769 if (!strcmp(element, "knob"))
770 return new knob_param_control;
771 if (!strcmp(element, "hscale"))
772 return new hscale_param_control;
773 if (!strcmp(element, "vscale"))
774 return new vscale_param_control;
775 if (!strcmp(element, "combo"))
776 return new combo_box_param_control;
777 if (!strcmp(element, "toggle"))
778 return new toggle_param_control;
779 if (!strcmp(element, "spin"))
780 return new spin_param_control;
781 if (!strcmp(element, "button"))
782 return new button_param_control;
783 if (!strcmp(element, "label"))
784 return new label_param_control;
785 if (!strcmp(element, "value"))
786 return new value_param_control;
787 if (!strcmp(element, "vumeter"))
788 return new vumeter_param_control;
789 if (!strcmp(element, "line-graph"))
790 return new line_graph_param_control;
791 if (!strcmp(element, "keyboard"))
792 return new keyboard_param_control;
793 if (!strcmp(element, "curve"))
794 return new curve_param_control;
795 if (!strcmp(element, "led"))
796 return new led_param_control;
797 if (!strcmp(element, "entry"))
798 return new entry_param_control;
799 if (!strcmp(element, "filechooser"))
800 return new filechooser_param_control;
801 return NULL;
804 control_container *plugin_gui::create_container_from_xml(const char *element, const char *attributes[])
806 if (!strcmp(element, "table"))
807 return new table_container;
808 if (!strcmp(element, "vbox"))
809 return new vbox_container;
810 if (!strcmp(element, "hbox"))
811 return new hbox_container;
812 if (!strcmp(element, "align"))
813 return new alignment_container;
814 if (!strcmp(element, "frame"))
815 return new frame_container;
816 if (!strcmp(element, "notebook"))
817 return new notebook_container;
818 if (!strcmp(element, "scrolled"))
819 return new scrolled_container;
820 return NULL;
823 void plugin_gui::xml_element_start(void *data, const char *element, const char *attributes[])
825 plugin_gui *gui = (plugin_gui *)data;
826 gui->xml_element_start(element, attributes);
829 void plugin_gui::xml_element_start(const char *element, const char *attributes[])
831 if (ignore_stack) {
832 ignore_stack++;
833 return;
835 control_base::xml_attribute_map xam;
836 while(*attributes)
838 xam[attributes[0]] = attributes[1];
839 attributes += 2;
842 if (!strcmp(element, "if"))
844 if (!xam.count("cond") || xam["cond"].empty())
846 g_error("Incorrect <if cond=\"[!]symbol\"> element");
848 string cond = xam["cond"];
849 bool state = true;
850 if (cond.substr(0, 1) == "!") {
851 state = false;
852 cond.erase(0, 1);
854 if (window->main->check_condition(cond.c_str()) == state)
855 return;
856 ignore_stack = 1;
857 return;
859 control_container *cc = create_container_from_xml(element, attributes);
860 if (cc != NULL)
862 cc->attribs = xam;
863 cc->create(this, element, xam);
864 gtk_container_set_border_width(cc->container, cc->get_int("border"));
866 container_stack.push_back(cc);
867 current_control = NULL;
868 return;
870 if (!container_stack.empty())
872 current_control = create_control_from_xml(element, attributes);
873 if (current_control)
875 current_control->attribs = xam;
876 int param_no = -1;
877 if (xam.count("param"))
879 map<string, int>::iterator it = param_name_map.find(xam["param"]);
880 if (it == param_name_map.end())
881 g_error("Unknown parameter %s", xam["param"].c_str());
882 else
883 param_no = it->second;
885 current_control->create(this, param_no);
886 current_control->init_xml(element);
887 current_control->set();
888 current_control->hook_params();
889 return;
892 g_error("Unxpected element %s in GUI definition\n", element);
895 void plugin_gui::xml_element_end(void *data, const char *element)
897 plugin_gui *gui = (plugin_gui *)data;
898 if (gui->ignore_stack) {
899 gui->ignore_stack--;
900 return;
902 if (!strcmp(element, "if"))
904 return;
906 if (gui->current_control)
908 (*gui->container_stack.rbegin())->add(gui->current_control->widget, gui->current_control);
909 gui->current_control = NULL;
910 return;
912 unsigned int ss = gui->container_stack.size();
913 if (ss > 1) {
914 gui->container_stack[ss - 2]->add(GTK_WIDGET(gui->container_stack[ss - 1]->container), gui->container_stack[ss - 1]);
916 else
917 gui->top_container = gui->container_stack[0];
918 gui->container_stack.pop_back();
922 GtkWidget *plugin_gui::create_from_xml(plugin_ctl_iface *_plugin, const char *xml)
924 current_control = NULL;
925 top_container = NULL;
926 parser = XML_ParserCreate("UTF-8");
927 plugin = _plugin;
928 container_stack.clear();
929 ignore_stack = 0;
931 param_name_map.clear();
932 int size = plugin->get_param_count();
933 for (int i = 0; i < size; i++)
934 param_name_map[plugin->get_param_props(i)->short_name] = i;
936 XML_SetUserData(parser, this);
937 XML_SetElementHandler(parser, xml_element_start, xml_element_end);
938 XML_Status status = XML_Parse(parser, xml, strlen(xml), 1);
939 if (status == XML_STATUS_ERROR)
941 g_error("Parse error: %s in XML", XML_ErrorString(XML_GetErrorCode(parser)));
944 XML_ParserFree(parser);
945 return GTK_WIDGET(top_container->container);
948 void plugin_gui::send_configure(const char *key, const char *value)
950 // XXXKF this should really be replaced by a separate list of SCI-capable param controls
951 for (unsigned int i = 0; i < params.size(); i++)
953 assert(params[i] != NULL);
954 send_configure_iface *sci = dynamic_cast<send_configure_iface *>(params[i]);
955 if (sci)
956 sci->send_configure(key, value);
960 void plugin_gui::on_idle()
962 for (unsigned int i = 0; i < params.size(); i++)
964 parameter_properties &props = *plugin->get_param_props(params[i]->param_no);
965 bool is_output = (props.flags & PF_PROP_OUTPUT) != 0;
966 if (is_output) {
967 params[i]->set();
969 params[i]->on_idle();
971 // XXXKF iterate over par2ctl, too...
974 void plugin_gui::refresh()
976 for (unsigned int i = 0; i < params.size(); i++)
978 params[i]->set();
979 send_configure_iface *sci = dynamic_cast<send_configure_iface *>(params[i]);
980 if (sci)
981 plugin->send_configures(sci);
985 void plugin_gui::refresh(int param_no, param_control *originator)
987 std::multimap<int, param_control *>::iterator it = par2ctl.find(param_no);
988 while(it != par2ctl.end() && it->first == param_no)
990 if (it->second != originator)
991 it->second->set();
992 it++;
996 void plugin_gui::set_param_value(int param_no, float value, param_control *originator)
998 plugin->set_param_value(param_no, value);
999 refresh(param_no);
1002 plugin_gui::~plugin_gui()
1004 for (std::vector<param_control *>::iterator i = params.begin(); i != params.end(); i++)
1006 delete *i;
1011 /******************************* Actions **************************************************/
1013 static void store_preset_action(GtkAction *action, plugin_gui_window *gui_win)
1015 store_preset(GTK_WINDOW(gui_win->toplevel), gui_win->gui);
1018 static const GtkActionEntry actions[] = {
1019 { "PresetMenuAction", "", "_Preset", NULL, "Preset operations", NULL },
1020 { "BuiltinPresetMenuAction", "", "_Built-in", NULL, "Built-in (factory) presets", NULL },
1021 { "UserPresetMenuAction", "", "_User", NULL, "User (your) presets", NULL },
1022 { "CommandMenuAction", "", "_Command", NULL, "Plugin-related commands", NULL },
1023 { "store-preset", "", "Store preset", NULL, "Store a current setting as preset", (GCallback)store_preset_action },
1026 /***************************** GUI window ********************************************/
1028 static const char *ui_xml =
1029 "<ui>\n"
1030 " <menubar>\n"
1031 " <menu action=\"PresetMenuAction\">\n"
1032 " <menuitem action=\"store-preset\"/>\n"
1033 " <separator/>\n"
1034 " <placeholder name=\"builtin_presets\"/>\n"
1035 " <separator/>\n"
1036 " <placeholder name=\"user_presets\"/>\n"
1037 " </menu>\n"
1038 " <placeholder name=\"commands\"/>\n"
1039 " </menubar>\n"
1040 "</ui>\n"
1043 static const char *general_preset_pre_xml =
1044 "<ui>\n"
1045 " <menubar>\n"
1046 " <menu action=\"PresetMenuAction\">\n";
1048 static const char *builtin_preset_pre_xml =
1049 // " <menu action=\"BuiltinPresetMenuAction\">\n"
1050 " <placeholder name=\"builtin_presets\">\n";
1052 static const char *user_preset_pre_xml =
1053 // " <menu action=\"UserPresetMenuAction\">\n"
1054 " <placeholder name=\"user_presets\">\n";
1056 static const char *preset_post_xml =
1057 " </placeholder>\n"
1058 // " </menu>\n"
1059 " </menu>\n"
1060 " </menubar>\n"
1061 "</ui>\n"
1064 static const char *command_pre_xml =
1065 "<ui>\n"
1066 " <menubar>\n"
1067 " <placeholder name=\"commands\">\n"
1068 " <menu action=\"CommandMenuAction\">\n";
1070 static const char *command_post_xml =
1071 " </menu>\n"
1072 " </placeholder>\n"
1073 " </menubar>\n"
1074 "</ui>\n"
1077 plugin_gui_window::plugin_gui_window(main_window_iface *_main)
1079 toplevel = NULL;
1080 ui_mgr = NULL;
1081 std_actions = NULL;
1082 builtin_preset_actions = NULL;
1083 user_preset_actions = NULL;
1084 command_actions = NULL;
1085 main = _main;
1086 assert(main);
1089 string plugin_gui_window::make_gui_preset_list(GtkActionGroup *grp, bool builtin, char &ch)
1091 string preset_xml = string(general_preset_pre_xml) + (builtin ? builtin_preset_pre_xml : user_preset_pre_xml);
1092 preset_vector &pvec = (builtin ? get_builtin_presets() : get_user_presets()).presets;
1093 GtkActionGroup *preset_actions = builtin ? builtin_preset_actions : user_preset_actions;
1094 for (unsigned int i = 0; i < pvec.size(); i++)
1096 if (pvec[i].plugin != gui->effect_name)
1097 continue;
1098 stringstream ss;
1099 ss << (builtin ? "builtin_preset" : "user_preset") << i;
1100 preset_xml += " <menuitem name=\"" + pvec[i].name+"\" action=\""+ss.str()+"\"/>\n";
1101 if (ch != ' ' && ++ch == ':')
1102 ch = 'A';
1103 if (ch > 'Z')
1104 ch = ' ';
1106 string sv = ss.str();
1107 string prefix = ch == ' ' ? string() : string("_")+ch+" ";
1108 string name = prefix + pvec[i].name;
1109 GtkActionEntry ae = { sv.c_str(), NULL, name.c_str(), NULL, NULL, (GCallback)activate_preset };
1110 gtk_action_group_add_actions_full(preset_actions, &ae, 1, (gpointer)new activate_preset_params(gui, i, builtin), action_destroy_notify);
1112 preset_xml += preset_post_xml;
1113 return preset_xml;
1116 string plugin_gui_window::make_gui_command_list(GtkActionGroup *grp)
1118 string command_xml = command_pre_xml;
1119 plugin_command_info *ci = gui->plugin->get_commands();
1120 if (!ci)
1121 return "";
1122 for(int i = 0; ci->name; i++, ci++)
1124 stringstream ss;
1125 ss << " <menuitem name=\"" << ci->name << "\" action=\"" << ci->label << "\"/>\n";
1127 GtkActionEntry ae = { ci->label, NULL, ci->name, NULL, ci->description, (GCallback)activate_command };
1128 gtk_action_group_add_actions_full(command_actions, &ae, 1, (gpointer)new activate_command_params(gui, i), action_destroy_notify);
1129 command_xml += ss.str();
1131 command_xml += command_post_xml;
1132 return command_xml;
1135 void plugin_gui_window::fill_gui_presets(bool builtin, char &ch)
1137 GtkActionGroup *&preset_actions = builtin ? builtin_preset_actions : user_preset_actions;
1138 if(preset_actions) {
1139 gtk_ui_manager_remove_action_group(ui_mgr, preset_actions);
1140 preset_actions = NULL;
1143 if (builtin)
1144 builtin_preset_actions = gtk_action_group_new("builtin_presets");
1145 else
1146 user_preset_actions = gtk_action_group_new("user_presets");
1147 string preset_xml = make_gui_preset_list(preset_actions, builtin, ch);
1148 gtk_ui_manager_insert_action_group(ui_mgr, preset_actions, 0);
1149 GError *error = NULL;
1150 gtk_ui_manager_add_ui_from_string(ui_mgr, preset_xml.c_str(), -1, &error);
1153 gboolean plugin_gui_window::on_idle(void *data)
1155 plugin_gui_window *self = (plugin_gui_window *)data;
1156 self->gui->on_idle();
1157 return TRUE;
1160 void plugin_gui_window::create(plugin_ctl_iface *_jh, const char *title, const char *effect)
1162 toplevel = GTK_WINDOW(gtk_window_new (GTK_WINDOW_TOPLEVEL));
1163 gtk_window_set_default_icon_name("calf");
1164 gtk_window_set_type_hint(toplevel, GDK_WINDOW_TYPE_HINT_DIALOG);
1165 GtkVBox *vbox = GTK_VBOX(gtk_vbox_new(false, 5));
1167 GtkRequisition req, req2;
1168 gtk_window_set_title(GTK_WINDOW (toplevel), title);
1169 gtk_container_add(GTK_CONTAINER(toplevel), GTK_WIDGET(vbox));
1171 gui = new plugin_gui(this);
1172 gui->effect_name = effect;
1174 ui_mgr = gtk_ui_manager_new();
1175 std_actions = gtk_action_group_new("default");
1176 gtk_action_group_add_actions(std_actions, actions, sizeof(actions)/sizeof(actions[0]), this);
1177 GError *error = NULL;
1178 gtk_ui_manager_insert_action_group(ui_mgr, std_actions, 0);
1179 gtk_ui_manager_add_ui_from_string(ui_mgr, ui_xml, -1, &error);
1181 command_actions = gtk_action_group_new("commands");
1183 char ch = '0';
1184 fill_gui_presets(true, ch);
1185 fill_gui_presets(false, ch);
1187 gtk_box_pack_start(GTK_BOX(vbox), gtk_ui_manager_get_widget(ui_mgr, "/ui/menubar"), false, false, 0);
1189 // determine size without content
1190 gtk_widget_show_all(GTK_WIDGET(vbox));
1191 gtk_widget_size_request(GTK_WIDGET(vbox), &req2);
1192 // printf("size request %dx%d\n", req2.width, req2.height);
1194 GtkWidget *container;
1195 const char *xml = _jh->get_gui_xml();
1196 assert(xml);
1197 container = gui->create_from_xml(_jh, xml);
1199 string command_xml = make_gui_command_list(command_actions);
1200 gtk_ui_manager_insert_action_group(ui_mgr, command_actions, 0);
1201 gtk_ui_manager_add_ui_from_string(ui_mgr, command_xml.c_str(), -1, &error);
1203 GtkWidget *sw = gtk_scrolled_window_new(NULL, NULL);
1204 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(sw), GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
1205 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(sw), GTK_SHADOW_NONE);
1206 gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(sw), container);
1208 gtk_box_pack_start(GTK_BOX(vbox), sw, true, true, 0);
1210 gtk_widget_show_all(GTK_WIDGET(sw));
1211 gtk_widget_size_request(container, &req);
1212 int wx = max(req.width + 10, req2.width);
1213 int wy = req.height + req2.height + 10;
1214 // printf("size request %dx%d\n", req.width, req.height);
1215 // printf("resize to %dx%d\n", max(req.width + 10, req2.width), req.height + req2.height + 10);
1216 gtk_window_set_default_size(GTK_WINDOW(toplevel), wx, wy);
1217 gtk_window_resize(GTK_WINDOW(toplevel), wx, wy);
1218 //gtk_widget_set_size_request(GTK_WIDGET(toplevel), max(req.width + 10, req2.width), req.height + req2.height + 10);
1219 // printf("size set %dx%d\n", wx, wy);
1220 // gtk_scrolled_window_set_vadjustment(GTK_SCROLLED_WINDOW(sw), GTK_ADJUSTMENT(gtk_adjustment_new(0, 0, req.height, 20, 100, 100)));
1221 gtk_signal_connect (GTK_OBJECT (toplevel), "destroy", G_CALLBACK (window_destroyed), (plugin_gui_window *)this);
1222 main->set_window(gui->plugin, this);
1224 source_id = g_timeout_add_full(G_PRIORITY_LOW, 1000/30, on_idle, this, NULL); // 30 fps should be enough for everybody
1225 gtk_ui_manager_ensure_update(ui_mgr);
1226 gui->plugin->send_configures(gui);
1229 void plugin_gui_window::close()
1231 if (source_id)
1232 g_source_remove(source_id);
1233 source_id = 0;
1234 gtk_widget_destroy(GTK_WIDGET(toplevel));
1237 plugin_gui_window::~plugin_gui_window()
1239 if (source_id)
1240 g_source_remove(source_id);
1241 main->set_window(gui->plugin, NULL);
1242 delete gui;
1245 void calf_plugins::activate_command(GtkAction *action, activate_command_params *params)
1247 plugin_gui *gui = params->gui;
1248 gui->plugin->execute(params->function_idx);
1249 gui->refresh();