+ Wavetable: new wavetables + 2 presets
[calf.git] / src / gui.cpp
blob827488656215c14a3bc821bd498837e7a1837efc
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 const char *filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(ctl->filechooser));
559 if (filename)
560 ctl->gui->plugin->configure(ctl->attribs["key"].c_str(), filename);
563 // line graph
565 void line_graph_param_control::on_idle()
567 if (get_int("refresh", 0))
568 set();
571 GtkWidget *line_graph_param_control::create(plugin_gui *_gui, int _param_no)
573 gui = _gui;
574 param_no = _param_no;
575 last_generation = -1;
576 // const parameter_properties &props = get_props();
578 widget = calf_line_graph_new ();
579 CalfLineGraph *clg = CALF_LINE_GRAPH(widget);
580 widget->requisition.width = get_int("width", 40);
581 widget->requisition.height = get_int("height", 40);
582 calf_line_graph_set_square(clg, get_int("square", 0));
583 clg->source = gui->plugin->get_line_graph_iface();
584 clg->source_id = param_no;
586 return widget;
589 void line_graph_param_control::set()
591 GtkWidget *tw = gtk_widget_get_toplevel(widget);
592 if (tw && GTK_WIDGET_TOPLEVEL(tw) && widget->window)
594 int ws = gdk_window_get_state(widget->window);
595 if (ws & (GDK_WINDOW_STATE_WITHDRAWN | GDK_WINDOW_STATE_ICONIFIED))
596 return;
597 last_generation = calf_line_graph_update_if(CALF_LINE_GRAPH(widget), last_generation);
601 line_graph_param_control::~line_graph_param_control()
605 /******************************** GUI proper ********************************/
607 plugin_gui::plugin_gui(plugin_gui_window *_window)
608 : window(_window)
613 static void window_destroyed(GtkWidget *window, gpointer data)
615 delete (plugin_gui_window *)data;
618 static void action_destroy_notify(gpointer data)
620 delete (activate_preset_params *)data;
623 void control_base::require_attribute(const char *name)
625 if (attribs.count(name) == 0) {
626 g_error("Missing attribute: %s", name);
630 void control_base::require_int_attribute(const char *name)
632 if (attribs.count(name) == 0) {
633 g_error("Missing attribute: %s", name);
635 if (attribs[name].empty() || attribs[name].find_first_not_of("0123456789") != string::npos) {
636 g_error("Wrong data type on attribute: %s (required integer)", name);
640 int control_base::get_int(const char *name, int def_value)
642 if (attribs.count(name) == 0)
643 return def_value;
644 const std::string &v = attribs[name];
645 if (v.empty() || v.find_first_not_of("-+0123456789") != string::npos)
646 return def_value;
647 return atoi(v.c_str());
650 float control_base::get_float(const char *name, float def_value)
652 if (attribs.count(name) == 0)
653 return def_value;
654 const std::string &v = attribs[name];
655 if (v.empty() || v.find_first_not_of("-+0123456789.") != string::npos)
656 return def_value;
657 stringstream ss(v);
658 float value;
659 ss >> value;
660 return value;
663 /******************************** GtkTable container ********************************/
665 GtkWidget *table_container::create(plugin_gui *_gui, const char *element, xml_attribute_map &attributes)
667 require_int_attribute("rows");
668 require_int_attribute("cols");
669 GtkWidget *table = gtk_table_new(get_int("rows", 1), get_int("cols", 1), false);
670 container = GTK_CONTAINER(table);
671 return table;
674 void table_container::add(GtkWidget *widget, control_base *base)
676 base->require_int_attribute("attach-x");
677 base->require_int_attribute("attach-y");
678 int x = base->get_int("attach-x"), y = base->get_int("attach-y");
679 int w = base->get_int("attach-w", 1), h = base->get_int("attach-h", 1);
680 int shrinkx = base->get_int("shrink-x", 0);
681 int shrinky = base->get_int("shrink-y", 0);
682 int fillx = (base->get_int("fill-x", !shrinkx) ? GTK_FILL : 0) | (base->get_int("expand-x", !shrinkx) ? GTK_EXPAND : 0) | (shrinkx ? GTK_SHRINK : 0);
683 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);
684 int padx = base->get_int("pad-x", 2);
685 int pady = base->get_int("pad-y", 2);
686 gtk_table_attach(GTK_TABLE(container), widget, x, x + w, y, y + h, (GtkAttachOptions)fillx, (GtkAttachOptions)filly, padx, pady);
689 /******************************** alignment contaner ********************************/
691 GtkWidget *alignment_container::create(plugin_gui *_gui, const char *element, xml_attribute_map &attributes)
693 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));
694 container = GTK_CONTAINER(align);
695 return align;
698 /******************************** GtkFrame contaner ********************************/
700 GtkWidget *frame_container::create(plugin_gui *_gui, const char *element, xml_attribute_map &attributes)
702 GtkWidget *frame = gtk_frame_new(attribs["label"].c_str());
703 container = GTK_CONTAINER(frame);
704 return frame;
707 /******************************** GtkBox type of containers ********************************/
709 void box_container::add(GtkWidget *w, control_base *base)
711 gtk_container_add_with_properties(container, w, "expand", get_int("expand", 1), "fill", get_int("fill", 1), NULL);
714 /******************************** GtkHBox container ********************************/
716 GtkWidget *hbox_container::create(plugin_gui *_gui, const char *element, xml_attribute_map &attributes)
718 GtkWidget *hbox = gtk_hbox_new(false, get_int("spacing", 2));
719 container = GTK_CONTAINER(hbox);
720 return hbox;
723 /******************************** GtkVBox container ********************************/
725 GtkWidget *vbox_container::create(plugin_gui *_gui, const char *element, xml_attribute_map &attributes)
727 GtkWidget *vbox = gtk_vbox_new(false, get_int("spacing", 2));
728 container = GTK_CONTAINER(vbox);
729 return vbox;
732 /******************************** GtkNotebook container ********************************/
734 GtkWidget *notebook_container::create(plugin_gui *_gui, const char *element, xml_attribute_map &attributes)
736 GtkWidget *nb = gtk_notebook_new();
737 container = GTK_CONTAINER(nb);
738 return nb;
741 void notebook_container::add(GtkWidget *w, control_base *base)
743 gtk_notebook_append_page(GTK_NOTEBOOK(container), w, gtk_label_new_with_mnemonic(base->attribs["page"].c_str()));
746 /******************************** GtkNotebook container ********************************/
748 GtkWidget *scrolled_container::create(plugin_gui *_gui, const char *element, xml_attribute_map &attributes)
750 GtkAdjustment *horiz = NULL, *vert = NULL;
751 int width = get_int("width", 0), height = get_int("height", 0);
752 if (width)
753 horiz = GTK_ADJUSTMENT(gtk_adjustment_new(get_int("x", 0), 0, width, get_int("step-x", 1), get_int("page-x", width / 10), 100));
754 if (height)
755 vert = GTK_ADJUSTMENT(gtk_adjustment_new(get_int("y", 0), 0, width, get_int("step-y", 1), get_int("page-y", height / 10), 10));
756 GtkWidget *sw = gtk_scrolled_window_new(horiz, vert);
757 gtk_widget_set_size_request(sw, get_int("req-x", -1), get_int("req-y", -1));
758 container = GTK_CONTAINER(sw);
759 return sw;
762 void scrolled_container::add(GtkWidget *w, control_base *base)
764 gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(container), w);
767 /******************************** GUI proper ********************************/
769 param_control *plugin_gui::create_control_from_xml(const char *element, const char *attributes[])
771 if (!strcmp(element, "knob"))
772 return new knob_param_control;
773 if (!strcmp(element, "hscale"))
774 return new hscale_param_control;
775 if (!strcmp(element, "vscale"))
776 return new vscale_param_control;
777 if (!strcmp(element, "combo"))
778 return new combo_box_param_control;
779 if (!strcmp(element, "toggle"))
780 return new toggle_param_control;
781 if (!strcmp(element, "spin"))
782 return new spin_param_control;
783 if (!strcmp(element, "button"))
784 return new button_param_control;
785 if (!strcmp(element, "label"))
786 return new label_param_control;
787 if (!strcmp(element, "value"))
788 return new value_param_control;
789 if (!strcmp(element, "vumeter"))
790 return new vumeter_param_control;
791 if (!strcmp(element, "line-graph"))
792 return new line_graph_param_control;
793 if (!strcmp(element, "keyboard"))
794 return new keyboard_param_control;
795 if (!strcmp(element, "curve"))
796 return new curve_param_control;
797 if (!strcmp(element, "led"))
798 return new led_param_control;
799 if (!strcmp(element, "entry"))
800 return new entry_param_control;
801 if (!strcmp(element, "filechooser"))
802 return new filechooser_param_control;
803 return NULL;
806 control_container *plugin_gui::create_container_from_xml(const char *element, const char *attributes[])
808 if (!strcmp(element, "table"))
809 return new table_container;
810 if (!strcmp(element, "vbox"))
811 return new vbox_container;
812 if (!strcmp(element, "hbox"))
813 return new hbox_container;
814 if (!strcmp(element, "align"))
815 return new alignment_container;
816 if (!strcmp(element, "frame"))
817 return new frame_container;
818 if (!strcmp(element, "notebook"))
819 return new notebook_container;
820 if (!strcmp(element, "scrolled"))
821 return new scrolled_container;
822 return NULL;
825 void plugin_gui::xml_element_start(void *data, const char *element, const char *attributes[])
827 plugin_gui *gui = (plugin_gui *)data;
828 gui->xml_element_start(element, attributes);
831 void plugin_gui::xml_element_start(const char *element, const char *attributes[])
833 if (ignore_stack) {
834 ignore_stack++;
835 return;
837 control_base::xml_attribute_map xam;
838 while(*attributes)
840 xam[attributes[0]] = attributes[1];
841 attributes += 2;
844 if (!strcmp(element, "if"))
846 if (!xam.count("cond") || xam["cond"].empty())
848 g_error("Incorrect <if cond=\"[!]symbol\"> element");
850 string cond = xam["cond"];
851 bool state = true;
852 if (cond.substr(0, 1) == "!") {
853 state = false;
854 cond.erase(0, 1);
856 if (window->main->check_condition(cond.c_str()) == state)
857 return;
858 ignore_stack = 1;
859 return;
861 control_container *cc = create_container_from_xml(element, attributes);
862 if (cc != NULL)
864 cc->attribs = xam;
865 cc->create(this, element, xam);
866 gtk_container_set_border_width(cc->container, cc->get_int("border"));
868 container_stack.push_back(cc);
869 current_control = NULL;
870 return;
872 if (!container_stack.empty())
874 current_control = create_control_from_xml(element, attributes);
875 if (current_control)
877 current_control->attribs = xam;
878 int param_no = -1;
879 if (xam.count("param"))
881 map<string, int>::iterator it = param_name_map.find(xam["param"]);
882 if (it == param_name_map.end())
883 g_error("Unknown parameter %s", xam["param"].c_str());
884 else
885 param_no = it->second;
887 current_control->create(this, param_no);
888 current_control->init_xml(element);
889 current_control->set();
890 current_control->hook_params();
891 return;
894 g_error("Unxpected element %s in GUI definition\n", element);
897 void plugin_gui::xml_element_end(void *data, const char *element)
899 plugin_gui *gui = (plugin_gui *)data;
900 if (gui->ignore_stack) {
901 gui->ignore_stack--;
902 return;
904 if (!strcmp(element, "if"))
906 return;
908 if (gui->current_control)
910 (*gui->container_stack.rbegin())->add(gui->current_control->widget, gui->current_control);
911 gui->current_control = NULL;
912 return;
914 unsigned int ss = gui->container_stack.size();
915 if (ss > 1) {
916 gui->container_stack[ss - 2]->add(GTK_WIDGET(gui->container_stack[ss - 1]->container), gui->container_stack[ss - 1]);
918 else
919 gui->top_container = gui->container_stack[0];
920 gui->container_stack.pop_back();
924 GtkWidget *plugin_gui::create_from_xml(plugin_ctl_iface *_plugin, const char *xml)
926 current_control = NULL;
927 top_container = NULL;
928 parser = XML_ParserCreate("UTF-8");
929 plugin = _plugin;
930 container_stack.clear();
931 ignore_stack = 0;
933 param_name_map.clear();
934 int size = plugin->get_param_count();
935 for (int i = 0; i < size; i++)
936 param_name_map[plugin->get_param_props(i)->short_name] = i;
938 XML_SetUserData(parser, this);
939 XML_SetElementHandler(parser, xml_element_start, xml_element_end);
940 XML_Status status = XML_Parse(parser, xml, strlen(xml), 1);
941 if (status == XML_STATUS_ERROR)
943 g_error("Parse error: %s in XML", XML_ErrorString(XML_GetErrorCode(parser)));
946 XML_ParserFree(parser);
947 return GTK_WIDGET(top_container->container);
950 void plugin_gui::send_configure(const char *key, const char *value)
952 // XXXKF this should really be replaced by a separate list of SCI-capable param controls
953 for (unsigned int i = 0; i < params.size(); i++)
955 assert(params[i] != NULL);
956 send_configure_iface *sci = dynamic_cast<send_configure_iface *>(params[i]);
957 if (sci)
958 sci->send_configure(key, value);
962 void plugin_gui::on_idle()
964 for (unsigned int i = 0; i < params.size(); i++)
966 parameter_properties &props = *plugin->get_param_props(params[i]->param_no);
967 bool is_output = (props.flags & PF_PROP_OUTPUT) != 0;
968 if (is_output) {
969 params[i]->set();
971 params[i]->on_idle();
973 // XXXKF iterate over par2ctl, too...
976 void plugin_gui::refresh()
978 for (unsigned int i = 0; i < params.size(); i++)
980 params[i]->set();
981 send_configure_iface *sci = dynamic_cast<send_configure_iface *>(params[i]);
982 if (sci)
983 plugin->send_configures(sci);
987 void plugin_gui::refresh(int param_no, param_control *originator)
989 std::multimap<int, param_control *>::iterator it = par2ctl.find(param_no);
990 while(it != par2ctl.end() && it->first == param_no)
992 if (it->second != originator)
993 it->second->set();
994 it++;
998 void plugin_gui::set_param_value(int param_no, float value, param_control *originator)
1000 plugin->set_param_value(param_no, value);
1001 refresh(param_no);
1004 plugin_gui::~plugin_gui()
1006 for (std::vector<param_control *>::iterator i = params.begin(); i != params.end(); i++)
1008 delete *i;
1013 /******************************* Actions **************************************************/
1015 static void store_preset_action(GtkAction *action, plugin_gui_window *gui_win)
1017 store_preset(GTK_WINDOW(gui_win->toplevel), gui_win->gui);
1020 static const GtkActionEntry actions[] = {
1021 { "PresetMenuAction", "", "_Preset", NULL, "Preset operations", NULL },
1022 { "BuiltinPresetMenuAction", "", "_Built-in", NULL, "Built-in (factory) presets", NULL },
1023 { "UserPresetMenuAction", "", "_User", NULL, "User (your) presets", NULL },
1024 { "CommandMenuAction", "", "_Command", NULL, "Plugin-related commands", NULL },
1025 { "store-preset", "", "Store preset", NULL, "Store a current setting as preset", (GCallback)store_preset_action },
1028 /***************************** GUI window ********************************************/
1030 static const char *ui_xml =
1031 "<ui>\n"
1032 " <menubar>\n"
1033 " <menu action=\"PresetMenuAction\">\n"
1034 " <menuitem action=\"store-preset\"/>\n"
1035 " <separator/>\n"
1036 " <placeholder name=\"builtin_presets\"/>\n"
1037 " <separator/>\n"
1038 " <placeholder name=\"user_presets\"/>\n"
1039 " </menu>\n"
1040 " <placeholder name=\"commands\"/>\n"
1041 " </menubar>\n"
1042 "</ui>\n"
1045 static const char *general_preset_pre_xml =
1046 "<ui>\n"
1047 " <menubar>\n"
1048 " <menu action=\"PresetMenuAction\">\n";
1050 static const char *builtin_preset_pre_xml =
1051 // " <menu action=\"BuiltinPresetMenuAction\">\n"
1052 " <placeholder name=\"builtin_presets\">\n";
1054 static const char *user_preset_pre_xml =
1055 // " <menu action=\"UserPresetMenuAction\">\n"
1056 " <placeholder name=\"user_presets\">\n";
1058 static const char *preset_post_xml =
1059 " </placeholder>\n"
1060 // " </menu>\n"
1061 " </menu>\n"
1062 " </menubar>\n"
1063 "</ui>\n"
1066 static const char *command_pre_xml =
1067 "<ui>\n"
1068 " <menubar>\n"
1069 " <placeholder name=\"commands\">\n"
1070 " <menu action=\"CommandMenuAction\">\n";
1072 static const char *command_post_xml =
1073 " </menu>\n"
1074 " </placeholder>\n"
1075 " </menubar>\n"
1076 "</ui>\n"
1079 plugin_gui_window::plugin_gui_window(main_window_iface *_main)
1081 toplevel = NULL;
1082 ui_mgr = NULL;
1083 std_actions = NULL;
1084 builtin_preset_actions = NULL;
1085 user_preset_actions = NULL;
1086 command_actions = NULL;
1087 main = _main;
1088 assert(main);
1091 string plugin_gui_window::make_gui_preset_list(GtkActionGroup *grp, bool builtin, char &ch)
1093 string preset_xml = string(general_preset_pre_xml) + (builtin ? builtin_preset_pre_xml : user_preset_pre_xml);
1094 preset_vector &pvec = (builtin ? get_builtin_presets() : get_user_presets()).presets;
1095 GtkActionGroup *preset_actions = builtin ? builtin_preset_actions : user_preset_actions;
1096 for (unsigned int i = 0; i < pvec.size(); i++)
1098 if (pvec[i].plugin != gui->effect_name)
1099 continue;
1100 stringstream ss;
1101 ss << (builtin ? "builtin_preset" : "user_preset") << i;
1102 preset_xml += " <menuitem name=\"" + pvec[i].name+"\" action=\""+ss.str()+"\"/>\n";
1103 if (ch != ' ' && ++ch == ':')
1104 ch = 'A';
1105 if (ch > 'Z')
1106 ch = ' ';
1108 string sv = ss.str();
1109 string prefix = ch == ' ' ? string() : string("_")+ch+" ";
1110 string name = prefix + pvec[i].name;
1111 GtkActionEntry ae = { sv.c_str(), NULL, name.c_str(), NULL, NULL, (GCallback)activate_preset };
1112 gtk_action_group_add_actions_full(preset_actions, &ae, 1, (gpointer)new activate_preset_params(gui, i, builtin), action_destroy_notify);
1114 preset_xml += preset_post_xml;
1115 return preset_xml;
1118 string plugin_gui_window::make_gui_command_list(GtkActionGroup *grp)
1120 string command_xml = command_pre_xml;
1121 plugin_command_info *ci = gui->plugin->get_commands();
1122 if (!ci)
1123 return "";
1124 for(int i = 0; ci->name; i++, ci++)
1126 stringstream ss;
1127 ss << " <menuitem name=\"" << ci->name << "\" action=\"" << ci->label << "\"/>\n";
1129 GtkActionEntry ae = { ci->label, NULL, ci->name, NULL, ci->description, (GCallback)activate_command };
1130 gtk_action_group_add_actions_full(command_actions, &ae, 1, (gpointer)new activate_command_params(gui, i), action_destroy_notify);
1131 command_xml += ss.str();
1133 command_xml += command_post_xml;
1134 return command_xml;
1137 void plugin_gui_window::fill_gui_presets(bool builtin, char &ch)
1139 GtkActionGroup *&preset_actions = builtin ? builtin_preset_actions : user_preset_actions;
1140 if(preset_actions) {
1141 gtk_ui_manager_remove_action_group(ui_mgr, preset_actions);
1142 preset_actions = NULL;
1145 if (builtin)
1146 builtin_preset_actions = gtk_action_group_new("builtin_presets");
1147 else
1148 user_preset_actions = gtk_action_group_new("user_presets");
1149 string preset_xml = make_gui_preset_list(preset_actions, builtin, ch);
1150 gtk_ui_manager_insert_action_group(ui_mgr, preset_actions, 0);
1151 GError *error = NULL;
1152 gtk_ui_manager_add_ui_from_string(ui_mgr, preset_xml.c_str(), -1, &error);
1155 gboolean plugin_gui_window::on_idle(void *data)
1157 plugin_gui_window *self = (plugin_gui_window *)data;
1158 self->gui->on_idle();
1159 return TRUE;
1162 void plugin_gui_window::create(plugin_ctl_iface *_jh, const char *title, const char *effect)
1164 toplevel = GTK_WINDOW(gtk_window_new (GTK_WINDOW_TOPLEVEL));
1165 gtk_window_set_default_icon_name("calf");
1166 gtk_window_set_type_hint(toplevel, GDK_WINDOW_TYPE_HINT_DIALOG);
1167 GtkVBox *vbox = GTK_VBOX(gtk_vbox_new(false, 5));
1169 GtkRequisition req, req2;
1170 gtk_window_set_title(GTK_WINDOW (toplevel), title);
1171 gtk_container_add(GTK_CONTAINER(toplevel), GTK_WIDGET(vbox));
1173 gui = new plugin_gui(this);
1174 gui->effect_name = effect;
1176 ui_mgr = gtk_ui_manager_new();
1177 std_actions = gtk_action_group_new("default");
1178 gtk_action_group_add_actions(std_actions, actions, sizeof(actions)/sizeof(actions[0]), this);
1179 GError *error = NULL;
1180 gtk_ui_manager_insert_action_group(ui_mgr, std_actions, 0);
1181 gtk_ui_manager_add_ui_from_string(ui_mgr, ui_xml, -1, &error);
1183 command_actions = gtk_action_group_new("commands");
1185 char ch = '0';
1186 fill_gui_presets(true, ch);
1187 fill_gui_presets(false, ch);
1189 gtk_box_pack_start(GTK_BOX(vbox), gtk_ui_manager_get_widget(ui_mgr, "/ui/menubar"), false, false, 0);
1191 // determine size without content
1192 gtk_widget_show_all(GTK_WIDGET(vbox));
1193 gtk_widget_size_request(GTK_WIDGET(vbox), &req2);
1194 // printf("size request %dx%d\n", req2.width, req2.height);
1196 GtkWidget *container;
1197 const char *xml = _jh->get_gui_xml();
1198 assert(xml);
1199 container = gui->create_from_xml(_jh, xml);
1201 string command_xml = make_gui_command_list(command_actions);
1202 gtk_ui_manager_insert_action_group(ui_mgr, command_actions, 0);
1203 gtk_ui_manager_add_ui_from_string(ui_mgr, command_xml.c_str(), -1, &error);
1205 GtkWidget *sw = gtk_scrolled_window_new(NULL, NULL);
1206 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(sw), GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
1207 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(sw), GTK_SHADOW_NONE);
1208 gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(sw), container);
1210 gtk_box_pack_start(GTK_BOX(vbox), sw, true, true, 0);
1212 gtk_widget_show_all(GTK_WIDGET(sw));
1213 gtk_widget_size_request(container, &req);
1214 int wx = max(req.width + 10, req2.width);
1215 int wy = req.height + req2.height + 10;
1216 // printf("size request %dx%d\n", req.width, req.height);
1217 // printf("resize to %dx%d\n", max(req.width + 10, req2.width), req.height + req2.height + 10);
1218 gtk_window_set_default_size(GTK_WINDOW(toplevel), wx, wy);
1219 gtk_window_resize(GTK_WINDOW(toplevel), wx, wy);
1220 //gtk_widget_set_size_request(GTK_WIDGET(toplevel), max(req.width + 10, req2.width), req.height + req2.height + 10);
1221 // printf("size set %dx%d\n", wx, wy);
1222 // gtk_scrolled_window_set_vadjustment(GTK_SCROLLED_WINDOW(sw), GTK_ADJUSTMENT(gtk_adjustment_new(0, 0, req.height, 20, 100, 100)));
1223 gtk_signal_connect (GTK_OBJECT (toplevel), "destroy", G_CALLBACK (window_destroyed), (plugin_gui_window *)this);
1224 main->set_window(gui->plugin, this);
1226 source_id = g_timeout_add_full(G_PRIORITY_LOW, 1000/30, on_idle, this, NULL); // 30 fps should be enough for everybody
1227 gtk_ui_manager_ensure_update(ui_mgr);
1228 gui->plugin->send_configures(gui);
1231 void plugin_gui_window::close()
1233 if (source_id)
1234 g_source_remove(source_id);
1235 source_id = 0;
1236 gtk_widget_destroy(GTK_WIDGET(toplevel));
1239 plugin_gui_window::~plugin_gui_window()
1241 if (source_id)
1242 g_source_remove(source_id);
1243 main->set_window(gui->plugin, NULL);
1244 delete gui;
1247 void calf_plugins::activate_command(GtkAction *action, activate_command_params *params)
1249 plugin_gui *gui = params->gui;
1250 gui->plugin->execute(params->function_idx);
1251 gui->refresh();