Move knob code to separate files.
[calf.git] / src / gui_controls.cpp
blob5a33651b5971cc64d4c0e4b383d9226ec31fef28
1 /* Calf DSP Library
2 * GUI widget object implementations.
3 * Copyright (C) 2007-2009 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., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301 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_knob.h>
26 #include <calf/ctl_led.h>
27 #include <calf/ctl_tube.h>
28 #include <calf/ctl_vumeter.h>
29 #include <calf/custom_ctl.h>
30 #include <calf/giface.h>
31 #include <calf/gui.h>
32 #include <calf/gui_controls.h>
33 #include <calf/utils.h>
34 #include <gdk/gdk.h>
36 using namespace calf_plugins;
37 using namespace calf_utils;
38 using namespace std;
40 /******************************** control/container base class **********************/
42 void control_base::require_attribute(const char *name)
44 if (attribs.count(name) == 0) {
45 g_error("Missing attribute '%s' in control '%s'", name, control_name.c_str());
49 void control_base::require_int_attribute(const char *name)
51 require_attribute(name);
52 if (attribs[name].empty() || attribs[name].find_first_not_of("0123456789") != string::npos) {
53 g_error("Wrong data type on attribute '%s' in control '%s' (required integer)", name, control_name.c_str());
57 int control_base::get_int(const char *name, int def_value)
59 if (attribs.count(name) == 0)
60 return def_value;
61 const std::string &v = attribs[name];
62 if (v.empty() || v.find_first_not_of("-+0123456789") != string::npos)
63 return def_value;
64 return atoi(v.c_str());
67 float control_base::get_float(const char *name, float def_value)
69 if (attribs.count(name) == 0)
70 return def_value;
71 const std::string &v = attribs[name];
72 if (v.empty() || v.find_first_not_of("-+0123456789.") != string::npos)
73 return def_value;
74 stringstream ss(v);
75 float value;
76 ss >> value;
77 return value;
80 /******************************** container base class **********************/
82 void control_container::set_std_properties()
84 if (attribs.find("widget-name") != attribs.end())
86 string name = attribs["widget-name"];
87 if (container) {
88 gtk_widget_set_name(GTK_WIDGET(container), name.c_str());
93 /************************* param-associated control base class **************/
95 param_control::param_control()
97 gui = NULL;
98 param_no = -1;
99 label = NULL;
100 in_change = 0;
101 old_displayed_value = -1.f;
105 void param_control::set_std_properties()
107 if (attribs.find("widget-name") != attribs.end())
109 string name = attribs["widget-name"];
110 if (widget) {
111 gtk_widget_set_name(widget, name.c_str());
116 GtkWidget *param_control::create_label()
118 label = gtk_label_new ("");
119 gtk_label_set_width_chars (GTK_LABEL (label), 12);
120 gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
121 return label;
124 void param_control::update_label()
126 const parameter_properties &props = get_props();
128 float value = gui->plugin->get_param_value(param_no);
129 if (value == old_displayed_value)
130 return;
131 gtk_label_set_text (GTK_LABEL (label), props.to_string(value).c_str());
132 old_displayed_value = value;
135 void param_control::hook_params()
137 if (param_no != -1) {
138 gui->add_param_ctl(param_no, this);
140 gui->params.push_back(this);
143 param_control::~param_control()
145 if (label)
146 gtk_widget_destroy(label);
147 if (widget)
148 gtk_widget_destroy(widget);
151 /******************************** controls ********************************/
153 // combo box
155 GtkWidget *combo_box_param_control::create(plugin_gui *_gui, int _param_no)
157 gui = _gui;
158 param_no = _param_no;
159 lstore = gtk_list_store_new(2, G_TYPE_STRING, G_TYPE_STRING); // value, key
161 const parameter_properties &props = get_props();
162 widget = gtk_combo_box_new_text ();
163 if (param_no != -1 && props.choices)
165 for (int j = (int)props.min; j <= (int)props.max; j++)
166 gtk_list_store_insert_with_values (lstore, NULL, j - (int)props.min, 0, props.choices[j - (int)props.min], 1, calf_utils::i2s(j).c_str(), -1);
168 gtk_combo_box_set_model (GTK_COMBO_BOX(widget), GTK_TREE_MODEL(lstore));
169 gtk_signal_connect (GTK_OBJECT (widget), "changed", G_CALLBACK (combo_value_changed), (gpointer)this);
170 gtk_widget_set_name(GTK_WIDGET(widget), "Calf-Combobox");
171 return widget;
174 void combo_box_param_control::set()
176 _GUARD_CHANGE_
177 if (param_no != -1)
179 const parameter_properties &props = get_props();
180 gtk_combo_box_set_active (GTK_COMBO_BOX (widget), (int)gui->plugin->get_param_value(param_no) - (int)props.min);
184 void combo_box_param_control::get()
186 if (param_no != -1)
188 const parameter_properties &props = get_props();
189 gui->set_param_value(param_no, gtk_combo_box_get_active (GTK_COMBO_BOX(widget)) + props.min, this);
193 void combo_box_param_control::combo_value_changed(GtkComboBox *widget, gpointer value)
195 combo_box_param_control *jhp = (combo_box_param_control *)value;
196 if (jhp->attribs.count("setter-key"))
198 GtkTreeIter iter;
199 gchar *key = NULL;
200 if (gtk_combo_box_get_active_iter (GTK_COMBO_BOX (jhp->widget), &iter))
202 gtk_tree_model_get (GTK_TREE_MODEL (jhp->lstore), &iter, 1, &key, -1);
203 if (key) {
204 jhp->gui->plugin->configure(jhp->attribs["setter-key"].c_str(), key);
205 free(key);
209 else
210 jhp->get();
213 void combo_box_param_control::send_status(const char *key, const char *value)
215 if (attribs.count("key") && key == attribs["key"])
217 gtk_list_store_clear (lstore);
218 key2pos.clear();
219 std::string v = value;
220 int i = 0;
221 size_t pos = 0;
222 while (pos < v.length()) {
223 size_t endpos = v.find("\n", pos);
224 if (endpos == string::npos)
225 break;
226 string line = v.substr(pos, endpos - pos);
227 string key, label;
228 size_t tabpos = line.find('\t');
229 if (tabpos == string::npos)
230 key = label = line;
231 else {
232 key = line.substr(0, tabpos);
233 label = line.substr(tabpos + 1);
235 GtkTreeIter gti;
236 gtk_list_store_insert_with_values (lstore, &gti, i, 0, label.c_str(), 1, key.c_str(), -1);
237 key2pos[key] = gti;
238 pos = endpos + 1;
239 i++;
241 set_to_last_key();
243 if (attribs.count("current-key") && key == attribs["current-key"])
245 last_key = value;
246 set_to_last_key();
250 void combo_box_param_control::set_to_last_key()
252 map<string, GtkTreeIter>::iterator i = key2pos.find(last_key);
253 if (i != key2pos.end())
255 gtk_combo_box_set_active_iter (GTK_COMBO_BOX (widget), &i->second);
258 else
259 gtk_combo_box_set_active (GTK_COMBO_BOX (widget), -1);
262 // horizontal fader
264 GtkWidget *hscale_param_control::create(plugin_gui *_gui, int _param_no)
266 gui = _gui;
267 param_no = _param_no;
269 widget = gtk_hscale_new_with_range (0, 1, get_props().get_increment());
270 gtk_signal_connect (GTK_OBJECT (widget), "value-changed", G_CALLBACK (hscale_value_changed), (gpointer)this);
271 gtk_signal_connect (GTK_OBJECT (widget), "format-value", G_CALLBACK (hscale_format_value), (gpointer)this);
273 if(get_int("inverted", 0) > 0) {
274 gtk_range_set_inverted(GTK_RANGE(widget), TRUE);
276 int size = get_int("size", 2);
277 if(size < 1)
278 size = 1;
279 if(size > 2)
280 size = 2;
281 char *name = g_strdup_printf("Calf-HScale%i", size);
282 gtk_widget_set_name(GTK_WIDGET(widget), name);
283 gtk_widget_set_size_request (widget, size * 100, -1);
284 g_free(name);
285 return widget;
288 void hscale_param_control::init_xml(const char *element)
290 if (attribs.count("width"))
291 gtk_widget_set_size_request (widget, get_int("width", 200), -1);
292 if (attribs.count("position"))
294 string v = attribs["position"];
295 if (v == "top") gtk_scale_set_value_pos(GTK_SCALE(widget), GTK_POS_TOP);
296 if (v == "bottom") gtk_scale_set_value_pos(GTK_SCALE(widget), GTK_POS_BOTTOM);
300 void hscale_param_control::set()
302 _GUARD_CHANGE_
303 const parameter_properties &props = get_props();
304 gtk_range_set_value (GTK_RANGE (widget), props.to_01 (gui->plugin->get_param_value(param_no)));
305 // hscale_value_changed (GTK_HSCALE (widget), (gpointer)this);
308 void hscale_param_control::get()
310 const parameter_properties &props = get_props();
311 float cvalue = props.from_01 (gtk_range_get_value (GTK_RANGE (widget)));
312 gui->set_param_value(param_no, cvalue, this);
315 void hscale_param_control::hscale_value_changed(GtkHScale *widget, gpointer value)
317 hscale_param_control *jhp = (hscale_param_control *)value;
318 jhp->get();
321 gchar *hscale_param_control::hscale_format_value(GtkScale *widget, double arg1, gpointer value)
323 hscale_param_control *jhp = (hscale_param_control *)value;
324 const parameter_properties &props = jhp->get_props();
325 float cvalue = props.from_01 (arg1);
327 // for testing
328 // return g_strdup_printf ("%s = %g", props.to_string (cvalue).c_str(), arg1);
329 return g_strdup (props.to_string (cvalue).c_str());
332 // vertical fader
334 GtkWidget *vscale_param_control::create(plugin_gui *_gui, int _param_no)
336 gui = _gui;
337 param_no = _param_no;
338 widget = gtk_vscale_new_with_range (0, 1, get_props().get_increment());
339 gtk_signal_connect (GTK_OBJECT (widget), "value-changed", G_CALLBACK (vscale_value_changed), (gpointer)this);
340 gtk_scale_set_draw_value(GTK_SCALE(widget), FALSE);
342 if(get_int("inverted", 0) > 0) {
343 gtk_range_set_inverted(GTK_RANGE(widget), TRUE);
345 int size = get_int("size", 2);
346 if(size < 1)
347 size = 1;
348 if(size > 2)
349 size = 2;
350 char *name = g_strdup_printf("Calf-VScale%i", size);
351 gtk_widget_set_size_request (widget, -1, size * 100);
352 gtk_widget_set_name(GTK_WIDGET(widget), name);
353 g_free(name);
354 return widget;
357 void vscale_param_control::init_xml(const char *element)
359 if (attribs.count("height"))
360 gtk_widget_set_size_request (widget, -1, get_int("height", 200));
363 void vscale_param_control::set()
365 _GUARD_CHANGE_
366 const parameter_properties &props = get_props();
367 gtk_range_set_value (GTK_RANGE (widget), props.to_01 (gui->plugin->get_param_value(param_no)));
368 // vscale_value_changed (GTK_HSCALE (widget), (gpointer)this);
371 void vscale_param_control::get()
373 const parameter_properties &props = get_props();
374 float cvalue = props.from_01 (gtk_range_get_value (GTK_RANGE (widget)));
375 gui->set_param_value(param_no, cvalue, this);
378 void vscale_param_control::vscale_value_changed(GtkHScale *widget, gpointer value)
380 vscale_param_control *jhp = (vscale_param_control *)value;
381 jhp->get();
384 // label
386 GtkWidget *label_param_control::create(plugin_gui *_gui, int _param_no)
388 gui = _gui, param_no = _param_no;
389 string text;
390 if (param_no != -1 && !attribs.count("text"))
391 text = get_props().name;
392 else
393 text = attribs["text"];
394 widget = gtk_label_new(text.c_str());
395 gtk_misc_set_alignment (GTK_MISC (widget), get_float("align-x", 0.5), get_float("align-y", 0.5));
396 gtk_widget_set_name(GTK_WIDGET(widget), "Calf-Label");
397 return widget;
400 // value
402 GtkWidget *value_param_control::create(plugin_gui *_gui, int _param_no)
404 gui = _gui;
405 param_no = _param_no;
407 widget = gtk_label_new ("");
408 if (param_no != -1)
410 const parameter_properties &props = get_props();
411 gtk_label_set_width_chars (GTK_LABEL (widget), props.get_char_count());
413 else
415 require_attribute("key");
416 require_int_attribute("width");
417 param_variable = attribs["key"];
418 gtk_label_set_width_chars (GTK_LABEL (widget), get_int("width"));
420 gtk_misc_set_alignment (GTK_MISC (widget), get_float("align-x", 0.5), get_float("align-y", 0.5));
421 gtk_widget_set_name(GTK_WIDGET(widget), "Calf-Value");
422 return widget;
425 void value_param_control::set()
427 if (param_no == -1)
428 return;
429 _GUARD_CHANGE_
431 const parameter_properties &props = get_props();
432 string value = props.to_string(gui->plugin->get_param_value(param_no));
434 if (value == old_value)
435 return;
436 old_value = value;
437 gtk_label_set_text (GTK_LABEL (widget), value.c_str());
440 void value_param_control::send_status(const char *key, const char *value)
442 if (key == param_variable)
444 gtk_label_set_text (GTK_LABEL (widget), value);
448 // VU meter
450 GtkWidget *vumeter_param_control::create(plugin_gui *_gui, int _param_no)
452 gui = _gui, param_no = _param_no;
453 // const parameter_properties &props = get_props();
454 widget = calf_vumeter_new ();
455 gtk_widget_set_name(GTK_WIDGET(widget), "calf-vumeter");
456 calf_vumeter_set_mode (CALF_VUMETER (widget), (CalfVUMeterMode)get_int("mode", 0));
457 CALF_VUMETER(widget)->vumeter_hold = get_float("hold", 0);
458 CALF_VUMETER(widget)->vumeter_falloff = get_float("falloff", 0.f);
459 CALF_VUMETER(widget)->vumeter_width = get_int("width", 50);
460 CALF_VUMETER(widget)->vumeter_height = get_int("height", 18);
461 CALF_VUMETER(widget)->vumeter_position = get_int("position", 0);
462 gtk_widget_set_name(GTK_WIDGET(widget), "Calf-VUMeter");
463 return widget;
466 void vumeter_param_control::set()
468 _GUARD_CHANGE_
469 const parameter_properties &props = get_props();
470 calf_vumeter_set_value (CALF_VUMETER (widget), props.to_01(gui->plugin->get_param_value(param_no)));
471 if (label)
472 update_label();
475 // LED
477 GtkWidget *led_param_control::create(plugin_gui *_gui, int _param_no)
479 gui = _gui, param_no = _param_no;
480 // const parameter_properties &props = get_props();
481 widget = calf_led_new ();
482 gtk_widget_set_name(GTK_WIDGET(widget), "calf-led");
483 CALF_LED(widget)->led_mode = get_int("mode", 0);
484 gtk_widget_set_name(GTK_WIDGET(widget), "Calf-LED");
485 return widget;
488 void led_param_control::set()
490 _GUARD_CHANGE_
491 // const parameter_properties &props = get_props();
492 calf_led_set_value (CALF_LED (widget), gui->plugin->get_param_value(param_no));
493 if (label)
494 update_label();
497 // tube
499 GtkWidget *tube_param_control::create(plugin_gui *_gui, int _param_no)
501 gui = _gui, param_no = _param_no;
502 // const parameter_properties &props = get_props();
503 widget = calf_tube_new ();
504 gtk_widget_set_name(GTK_WIDGET(widget), "calf-tube");
505 CALF_TUBE(widget)->size = get_int("size", 2);
506 CALF_TUBE(widget)->direction = get_int("direction", 2);
507 gtk_widget_set_name(GTK_WIDGET(widget), "Calf-Tube");
508 return widget;
511 void tube_param_control::set()
513 _GUARD_CHANGE_
514 // const parameter_properties &props = get_props();
515 calf_tube_set_value (CALF_TUBE (widget), gui->plugin->get_param_value(param_no));
516 if (label)
517 update_label();
520 // check box
522 GtkWidget *check_param_control::create(plugin_gui *_gui, int _param_no)
524 gui = _gui;
525 param_no = _param_no;
527 widget = gtk_check_button_new ();
528 gtk_signal_connect (GTK_OBJECT (widget), "toggled", G_CALLBACK (check_value_changed), (gpointer)this);
529 gtk_widget_set_name(GTK_WIDGET(widget), "Calf-Checkbox");
530 return widget;
533 void check_param_control::check_value_changed(GtkCheckButton *widget, gpointer value)
535 param_control *jhp = (param_control *)value;
536 jhp->get();
539 void check_param_control::get()
541 const parameter_properties &props = get_props();
542 gui->set_param_value(param_no, gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON(widget)) + props.min, this);
545 void check_param_control::set()
547 _GUARD_CHANGE_
548 const parameter_properties &props = get_props();
549 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (widget), (int)gui->plugin->get_param_value(param_no) - (int)props.min);
552 // radio button
554 GtkWidget *radio_param_control::create(plugin_gui *_gui, int _param_no)
556 gui = _gui;
557 param_no = _param_no;
558 require_attribute("value");
559 value = -1;
560 string value_name = attribs["value"];
561 const parameter_properties &props = get_props();
562 if (props.choices && (value_name < "0" || value_name > "9"))
564 for (int i = 0; props.choices[i]; i++)
566 if (value_name == props.choices[i])
568 value = i + (int)props.min;
569 break;
573 if (value == -1)
574 value = get_int("value");
576 if (attribs.count("label"))
577 widget = gtk_radio_button_new_with_label (gui->get_radio_group(param_no), attribs["label"].c_str());
578 else
579 widget = gtk_radio_button_new_with_label (gui->get_radio_group(param_no), props.choices[value - (int)props.min]);
580 gtk_toggle_button_set_mode (GTK_TOGGLE_BUTTON (widget), FALSE);
582 gui->set_radio_group(param_no, gtk_radio_button_get_group (GTK_RADIO_BUTTON (widget)));
583 gtk_signal_connect (GTK_OBJECT (widget), "clicked", G_CALLBACK (radio_clicked), (gpointer)this);
584 gtk_widget_set_name(GTK_WIDGET(widget), "Calf-RadioButton");
585 return widget;
588 void radio_param_control::radio_clicked(GtkRadioButton *widget, gpointer value)
590 param_control *jhp = (param_control *)value;
591 jhp->get();
594 void radio_param_control::get()
596 // const parameter_properties &props = get_props();
597 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget)))
598 gui->set_param_value(param_no, value, this);
601 void radio_param_control::set()
603 _GUARD_CHANGE_
604 const parameter_properties &props = get_props();
605 float pv = gui->plugin->get_param_value(param_no);
606 if (fabs(value-pv) < 0.5)
607 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (widget), value == ((int)gui->plugin->get_param_value(param_no) - (int)props.min));
610 // spin button
612 GtkWidget *spin_param_control::create(plugin_gui *_gui, int _param_no)
614 gui = _gui;
615 param_no = _param_no;
617 const parameter_properties &props = get_props();
618 if (props.step > 1)
619 widget = gtk_spin_button_new_with_range (props.min, props.max, (props.max - props.min) / (props.step - 1));
620 if (props.step > 0)
621 widget = gtk_spin_button_new_with_range (props.min, props.max, props.step);
622 else
623 widget = gtk_spin_button_new_with_range (props.min, props.max, 1);
624 gtk_spin_button_set_digits (GTK_SPIN_BUTTON(widget), get_int("digits", 0));
625 gtk_signal_connect (GTK_OBJECT (widget), "value-changed", G_CALLBACK (value_changed), (gpointer)this);
626 gtk_widget_set_name(GTK_WIDGET(widget), "Calf-SpinButton");
627 return widget;
630 void spin_param_control::value_changed(GtkSpinButton *widget, gpointer value)
632 param_control *jhp = (param_control *)value;
633 jhp->get();
636 void spin_param_control::get()
638 // const parameter_properties &props = get_props();
639 gui->set_param_value(param_no, gtk_spin_button_get_value_as_float (GTK_SPIN_BUTTON (widget)), this);
642 void spin_param_control::set()
644 _GUARD_CHANGE_
645 // const parameter_properties &props = get_props();
646 gtk_spin_button_set_value (GTK_SPIN_BUTTON (widget), gui->plugin->get_param_value(param_no));
649 // button
651 GtkWidget *button_param_control::create(plugin_gui *_gui, int _param_no)
653 gui = _gui;
654 param_no = _param_no;
656 widget = gtk_button_new_with_label (get_props().name);
657 gtk_signal_connect (GTK_OBJECT (widget), "clicked", G_CALLBACK (button_clicked), (gpointer)this);
658 gtk_signal_connect (GTK_OBJECT (widget), "button-press-event", G_CALLBACK (button_press_event), (gpointer)this);
659 gtk_widget_set_name(GTK_WIDGET(widget), "Calf-Button");
660 return widget;
663 void button_param_control::button_clicked(GtkButton *widget, gpointer value)
665 param_control *jhp = (param_control *)value;
667 jhp->get();
670 void button_param_control::button_press_event(GtkButton *widget, GdkEvent *event, gpointer value)
672 param_control *jhp = (param_control *)value;
674 #if 0
675 static int last_time = 0;
677 if (event->button.type == GDK_BUTTON_PRESS)
679 printf("tempo=%f\n", 60000.0 / (event->button.time - last_time));
680 last_time = event->button.time;
682 #endif
685 void button_param_control::get()
687 const parameter_properties &props = get_props();
688 gui->set_param_value(param_no, props.max, this);
691 void button_param_control::set()
693 _GUARD_CHANGE_
694 const parameter_properties &props = get_props();
695 if (gui->plugin->get_param_value(param_no) - props.min >= 0.5)
696 gtk_button_clicked (GTK_BUTTON (widget));
699 // knob
701 GtkWidget *knob_param_control::create(plugin_gui *_gui, int _param_no)
703 gui = _gui;
704 param_no = _param_no;
705 const parameter_properties &props = get_props();
707 //widget = calf_knob_new_with_range (props.to_01 (gui->plugin->get_param_value(param_no)), 0, 1, 0.01);
708 widget = calf_knob_new();
709 float increment = props.get_increment();
710 gtk_range_get_adjustment(GTK_RANGE(widget))->step_increment = increment;
711 CALF_KNOB(widget)->knob_type = get_int("type");
712 CALF_KNOB(widget)->knob_size = get_int("size", 2);
713 if(CALF_KNOB(widget)->knob_size > 5) {
714 CALF_KNOB(widget)->knob_size = 5;
715 } else if (CALF_KNOB(widget)->knob_size < 1) {
716 CALF_KNOB(widget)->knob_size = 1;
718 gtk_signal_connect(GTK_OBJECT(widget), "value-changed", G_CALLBACK(knob_value_changed), (gpointer)this);
719 gtk_widget_set_name(GTK_WIDGET(widget), "Calf-Knob");
720 return widget;
723 void knob_param_control::get()
725 const parameter_properties &props = get_props();
726 float value = props.from_01(gtk_range_get_value(GTK_RANGE(widget)));
727 gui->set_param_value(param_no, value, this);
728 if (label)
729 update_label();
732 void knob_param_control::set()
734 _GUARD_CHANGE_
735 const parameter_properties &props = get_props();
736 gtk_range_set_value(GTK_RANGE(widget), props.to_01 (gui->plugin->get_param_value(param_no)));
737 if (label)
738 update_label();
741 void knob_param_control::knob_value_changed(GtkWidget *widget, gpointer value)
743 param_control *jhp = (param_control *)value;
744 jhp->get();
747 // Toggle Button
749 GtkWidget *toggle_param_control::create(plugin_gui *_gui, int _param_no)
751 gui = _gui;
752 param_no = _param_no;
753 widget = calf_toggle_new ();
755 CALF_TOGGLE(widget)->size = get_int("size", 2);
756 if(CALF_TOGGLE(widget)->size > 2) {
757 CALF_TOGGLE(widget)->size = 2;
758 } else if (CALF_TOGGLE(widget)->size < 1) {
759 CALF_TOGGLE(widget)->size = 1;
762 gtk_signal_connect (GTK_OBJECT (widget), "value-changed", G_CALLBACK (toggle_value_changed), (gpointer)this);
763 gtk_widget_set_name(GTK_WIDGET(widget), "Calf-ToggleButton");
764 return widget;
767 void toggle_param_control::get()
769 const parameter_properties &props = get_props();
770 float value = props.from_01(gtk_range_get_value(GTK_RANGE(widget)));
771 gui->set_param_value(param_no, value, this);
772 if (label)
773 update_label();
776 void toggle_param_control::set()
778 _GUARD_CHANGE_
779 const parameter_properties &props = get_props();
780 gtk_range_set_value(GTK_RANGE(widget), props.to_01 (gui->plugin->get_param_value(param_no)));
781 if (label)
782 update_label();
785 void toggle_param_control::toggle_value_changed(GtkWidget *widget, gpointer value)
787 param_control *jhp = (param_control *)value;
788 jhp->get();
791 // keyboard
793 GtkWidget *keyboard_param_control::create(plugin_gui *_gui, int _param_no)
795 gui = _gui;
796 param_no = _param_no;
797 // const parameter_properties &props = get_props();
799 widget = calf_keyboard_new();
800 kb = CALF_KEYBOARD(widget);
801 kb->nkeys = get_int("octaves", 4) * 7 + 1;
802 kb->sink = new CalfKeyboard::EventAdapter;
803 gtk_widget_set_name(GTK_WIDGET(widget), "Calf-Keyboard");
804 return widget;
807 // curve
809 struct curve_param_control_callback: public CalfCurve::EventAdapter
811 curve_param_control *ctl;
813 curve_param_control_callback(curve_param_control *_ctl)
814 : ctl(_ctl) {}
816 virtual void curve_changed(CalfCurve *src, const CalfCurve::point_vector &data) {
817 stringstream ss;
818 ss << data.size() << endl;
819 for (size_t i = 0; i < data.size(); i++)
820 ss << data[i].first << " " << data[i].second << endl;
821 ctl->gui->plugin->configure(ctl->attribs["key"].c_str(), ss.str().c_str());
823 virtual void clip(CalfCurve *src, int pt, float &x, float &y, bool &hide)
825 // int gridpt = floor(x * 71 * 2);
826 // clip to the middle of the nearest white key
827 x = (floor(x * 71) + 0.5)/ 71.0;
831 GtkWidget *curve_param_control::create(plugin_gui *_gui, int _param_no)
833 gui = _gui;
834 param_no = _param_no;
835 require_attribute("key");
837 widget = calf_curve_new(get_int("maxpoints", -1));
838 curve = CALF_CURVE(widget);
839 curve->sink = new curve_param_control_callback(this);
840 // gtk_curve_set_curve_type(curve, GTK_CURVE_TYPE_LINEAR);
841 gtk_widget_set_name(GTK_WIDGET(widget), "Calf-Curve");
842 return widget;
845 void curve_param_control::send_configure(const char *key, const char *value)
847 // cout << "send conf " << key << endl;
848 if (attribs["key"] == key)
850 stringstream ss(value);
851 CalfCurve::point_vector pts;
852 if (*value)
854 unsigned int npoints = 0;
855 ss >> npoints;
856 unsigned int i;
857 float x = 0, y = 0;
858 for (i = 0; i < npoints && i < curve->point_limit; i++)
860 ss >> x >> y;
861 pts.push_back(CalfCurve::point(x, y));
863 calf_curve_set_points(widget, pts);
868 // entry
870 GtkWidget *entry_param_control::create(plugin_gui *_gui, int _param_no)
872 gui = _gui;
873 param_no = _param_no;
874 require_attribute("key");
876 widget = gtk_entry_new();
877 entry = GTK_ENTRY(widget);
878 gtk_signal_connect(GTK_OBJECT(widget), "changed", G_CALLBACK(entry_value_changed), (gpointer)this);
879 gtk_editable_set_editable(GTK_EDITABLE(entry), get_int("editable", 1));
880 gtk_widget_set_name(GTK_WIDGET(widget), "Calf-Entry");
881 return widget;
884 void entry_param_control::send_configure(const char *key, const char *value)
886 // cout << "send conf " << key << endl;
887 if (attribs["key"] == key)
889 gtk_entry_set_text(entry, value);
893 void entry_param_control::entry_value_changed(GtkWidget *widget, gpointer value)
895 entry_param_control *ctl = (entry_param_control *)value;
896 ctl->gui->plugin->configure(ctl->attribs["key"].c_str(), gtk_entry_get_text(ctl->entry));
899 // filechooser
901 GtkWidget *filechooser_param_control::create(plugin_gui *_gui, int _param_no)
903 gui = _gui;
904 param_no = _param_no;
905 require_attribute("key");
906 require_attribute("title");
908 widget = gtk_file_chooser_button_new(attribs["title"].c_str(), GTK_FILE_CHOOSER_ACTION_OPEN);
909 filechooser = GTK_FILE_CHOOSER_BUTTON(widget);
910 // XXXKF this is GTK+ 2.12 function, does any replacement exist?
911 gtk_signal_connect(GTK_OBJECT(widget), "file-set", G_CALLBACK(filechooser_value_changed), (gpointer)this);
912 if (attribs.count("width"))
913 gtk_widget_set_size_request (widget, get_int("width", 200), -1);
914 if (attribs.count("width_chars"))
915 gtk_file_chooser_button_set_width_chars (filechooser, get_int("width_chars"));
916 gtk_widget_set_name(GTK_WIDGET(widget), "Calf-FileButton");
917 return widget;
920 void filechooser_param_control::send_configure(const char *key, const char *value)
922 // cout << "send conf " << key << endl;
923 if (attribs["key"] == key)
925 gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(filechooser), value);
929 void filechooser_param_control::filechooser_value_changed(GtkWidget *widget, gpointer value)
931 filechooser_param_control *ctl = (filechooser_param_control *)value;
932 const char *filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(ctl->filechooser));
933 if (filename)
934 ctl->gui->plugin->configure(ctl->attribs["key"].c_str(), filename);
937 // line graph
939 void line_graph_param_control::on_idle()
941 if (get_int("refresh", 0))
942 set();
945 GtkWidget *line_graph_param_control::create(plugin_gui *_gui, int _param_no)
947 gui = _gui;
948 param_no = _param_no;
949 last_generation = -1;
951 widget = calf_line_graph_new ();
952 gtk_widget_set_name(GTK_WIDGET(widget), "calf-graph");
953 CalfLineGraph *clg = CALF_LINE_GRAPH(widget);
954 widget->requisition.width = get_int("width", 40);
955 widget->requisition.height = get_int("height", 40);
956 calf_line_graph_set_square(clg, get_int("square", 0));
957 clg->source = gui->plugin->get_line_graph_iface();
958 clg->source_id = param_no;
959 CALF_LINE_GRAPH(widget)->use_fade = get_int("use_fade", 0);
960 CALF_LINE_GRAPH(widget)->fade = get_float("fade", 0.5);
961 CALF_LINE_GRAPH(widget)->mode = get_int("mode", 0);
962 gtk_widget_set_name(GTK_WIDGET(widget), "Calf-LineGraph");
963 return widget;
966 void line_graph_param_control::set()
968 GtkWidget *tw = gtk_widget_get_toplevel(widget);
969 if (tw && GTK_WIDGET_TOPLEVEL(tw) && widget->window)
971 int ws = gdk_window_get_state(widget->window);
972 if (ws & (GDK_WINDOW_STATE_WITHDRAWN | GDK_WINDOW_STATE_ICONIFIED))
973 return;
974 last_generation = calf_line_graph_update_if(CALF_LINE_GRAPH(widget), last_generation);
978 line_graph_param_control::~line_graph_param_control()
982 // phase graph
984 void phase_graph_param_control::on_idle()
986 if (get_int("refresh", 0))
987 set();
990 GtkWidget *phase_graph_param_control::create(plugin_gui *_gui, int _param_no)
992 gui = _gui;
993 param_no = _param_no;
994 last_generation = -1;
996 widget = calf_phase_graph_new ();
997 gtk_widget_set_name(GTK_WIDGET(widget), "calf-phase");
998 CalfPhaseGraph *clg = CALF_PHASE_GRAPH(widget);
999 widget->requisition.width = get_int("size", 40);
1000 widget->requisition.height = get_int("size", 40);
1001 clg->source = gui->plugin->get_phase_graph_iface();
1002 clg->source_id = param_no;
1003 gtk_widget_set_name(GTK_WIDGET(widget), "Calf-PhaseGraph");
1004 return widget;
1007 void phase_graph_param_control::set()
1009 GtkWidget *tw = gtk_widget_get_toplevel(widget);
1010 gtk_widget_queue_draw(tw);
1011 // if (tw && GTK_WIDGET_TOPLEVEL(tw) && widget->window)
1012 // {
1013 // int ws = gdk_window_get_state(widget->window);
1014 // if (ws & (GDK_WINDOW_STATE_WITHDRAWN | GDK_WINDOW_STATE_ICONIFIED))
1015 // return;
1016 // //last_generation = calf_phase_graph_update_if(CALF_PHASE_GRAPH(widget), last_generation);
1017 // }
1020 phase_graph_param_control::~phase_graph_param_control()
1024 // list view
1026 GtkWidget *listview_param_control::create(plugin_gui *_gui, int _param_no)
1028 gui = _gui;
1029 param_no = _param_no;
1031 string key = attribs["key"];
1032 tmif = gui->plugin->get_metadata_iface()->get_table_metadata_iface(key.c_str());
1033 if (!tmif)
1035 g_error("Missing table_metadata_iface for variable '%s'", key.c_str());
1036 return NULL;
1038 positions.clear();
1039 const table_column_info *tci = tmif->get_table_columns();
1040 assert(tci);
1041 cols = 0;
1042 while (tci[cols].name != NULL)
1043 cols++;
1045 GType *p = new GType[cols];
1046 for (int i = 0; i < cols; i++)
1047 p[i] = G_TYPE_STRING;
1048 lstore = gtk_list_store_newv(cols, p);
1049 if (tmif->get_table_rows() != 0)
1050 set_rows(tmif->get_table_rows());
1051 widget = gtk_tree_view_new_with_model(GTK_TREE_MODEL(lstore));
1052 delete []p;
1053 tree = GTK_TREE_VIEW (widget);
1054 g_object_set (G_OBJECT (tree), "enable-search", FALSE, "rules-hint", TRUE, "enable-grid-lines", TRUE, NULL);
1056 for (int i = 0; i < cols; i++)
1058 GtkCellRenderer *cr = NULL;
1060 if (tci[i].type == TCT_ENUM) {
1061 cr = gtk_cell_renderer_combo_new ();
1062 GtkListStore *cls = gtk_list_store_new(2, G_TYPE_INT, G_TYPE_STRING);
1063 for (int j = 0; tci[i].values[j]; j++)
1064 gtk_list_store_insert_with_values(cls, NULL, j, 0, j, 1, tci[i].values[j], -1);
1065 g_object_set(cr, "model", cls, "editable", TRUE, "has-entry", FALSE, "text-column", 1, "mode", GTK_CELL_RENDERER_MODE_EDITABLE, NULL);
1067 else {
1068 bool editable = tci[i].type != TCT_LABEL;
1069 cr = gtk_cell_renderer_text_new ();
1070 if (editable)
1071 g_object_set(cr, "editable", TRUE, "mode", GTK_CELL_RENDERER_MODE_EDITABLE, NULL);
1073 g_object_set_data (G_OBJECT(cr), "column", (void *)&tci[i]);
1074 gtk_signal_connect (GTK_OBJECT (cr), "edited", G_CALLBACK (on_edited), (gpointer)this);
1075 gtk_signal_connect (GTK_OBJECT (cr), "editing-canceled", G_CALLBACK (on_editing_canceled), (gpointer)this);
1076 gtk_tree_view_insert_column_with_attributes(tree, i, tci[i].name, cr, "text", i, NULL);
1078 gtk_tree_view_set_headers_visible(tree, TRUE);
1079 gtk_widget_set_name(GTK_WIDGET(widget), "Calf-ListView");
1080 return widget;
1083 void listview_param_control::set_rows(unsigned int needed_rows)
1085 while(positions.size() < needed_rows)
1087 GtkTreeIter iter;
1088 gtk_list_store_insert(lstore, &iter, positions.size());
1089 for (int j = 0; j < cols; j++)
1091 gtk_list_store_set(lstore, &iter, j, "", -1);
1093 positions.push_back(iter);
1097 void listview_param_control::send_configure(const char *key, const char *value)
1099 string orig_key = attribs["key"] + ":";
1100 bool is_rows = false;
1101 int row = -1, col = -1;
1102 if (parse_table_key(key, orig_key.c_str(), is_rows, row, col))
1104 string suffix = string(key + orig_key.length());
1105 if (is_rows && tmif->get_table_rows() == 0)
1107 int rows = atoi(value);
1108 set_rows(rows);
1109 return;
1111 else
1112 if (row != -1 && col != -1)
1114 int max_rows = tmif->get_table_rows();
1115 if (col < 0 || col >= cols)
1117 g_warning("Invalid column %d in key %s", col, key);
1118 return;
1120 if (max_rows && (row < 0 || row >= max_rows))
1122 g_warning("Invalid row %d in key %s, this is a fixed table with row count = %d", row, key, max_rows);
1123 return;
1126 if (row >= (int)positions.size())
1127 set_rows(row + 1);
1129 gtk_list_store_set(lstore, &positions[row], col, value, -1);
1130 return;
1135 void listview_param_control::on_edited(GtkCellRenderer *renderer, gchar *path, gchar *new_text, listview_param_control *pThis)
1137 const table_column_info *tci = pThis->tmif->get_table_columns();
1138 int column = ((table_column_info *)g_object_get_data(G_OBJECT(renderer), "column")) - tci;
1139 string key = pThis->attribs["key"] + ":" + i2s(atoi(path)) + "," + i2s(column);
1140 string error;
1141 const char *error_or_null = pThis->gui->plugin->configure(key.c_str(), new_text);
1142 if (error_or_null)
1143 error = error_or_null;
1145 if (error.empty()) {
1146 pThis->send_configure(key.c_str(), new_text);
1147 gtk_widget_grab_focus(pThis->widget);
1148 GtkTreePath *gpath = gtk_tree_path_new_from_string (path);
1149 gtk_tree_view_set_cursor_on_cell (GTK_TREE_VIEW (pThis->widget), gpath, NULL, NULL, FALSE);
1150 gtk_tree_path_free (gpath);
1152 else
1154 GtkWidget *dialog = gtk_message_dialog_new(pThis->gui->window->toplevel, GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK,
1155 "%s", error.c_str());
1156 gtk_dialog_run(GTK_DIALOG(dialog));
1157 gtk_widget_destroy(dialog);
1158 gtk_widget_grab_focus(pThis->widget);
1162 void listview_param_control::on_editing_canceled(GtkCellRenderer *renderer, listview_param_control *pThis)
1164 gtk_widget_grab_focus(pThis->widget);
1167 /******************************** GtkTable container ********************************/
1169 GtkWidget *table_container::create(plugin_gui *_gui, const char *element, xml_attribute_map &attributes)
1171 require_int_attribute("rows");
1172 require_int_attribute("cols");
1173 int homog = get_int("homogeneous", 0);
1174 GtkWidget *table = gtk_table_new(get_int("rows", 1), get_int("cols", 1), false);
1175 if(homog > 0) {
1176 gtk_table_set_homogeneous(GTK_TABLE(table), TRUE);
1178 container = GTK_CONTAINER(table);
1179 gtk_widget_set_name(GTK_WIDGET(table), "Calf-Table");
1180 return table;
1183 void table_container::add(GtkWidget *widget, control_base *base)
1185 base->require_int_attribute("attach-x");
1186 base->require_int_attribute("attach-y");
1187 int x = base->get_int("attach-x"), y = base->get_int("attach-y");
1188 int w = base->get_int("attach-w", 1), h = base->get_int("attach-h", 1);
1189 int shrinkx = base->get_int("shrink-x", 0);
1190 int shrinky = base->get_int("shrink-y", 0);
1191 int fillx = (base->get_int("fill-x", !shrinkx) ? GTK_FILL : 0) | (base->get_int("expand-x", !shrinkx) ? GTK_EXPAND : 0) | (shrinkx ? GTK_SHRINK : 0);
1192 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);
1193 int padx = base->get_int("pad-x", 2);
1194 int pady = base->get_int("pad-y", 2);
1195 gtk_table_attach(GTK_TABLE(container), widget, x, x + w, y, y + h, (GtkAttachOptions)fillx, (GtkAttachOptions)filly, padx, pady);
1198 /******************************** alignment contaner ********************************/
1200 GtkWidget *alignment_container::create(plugin_gui *_gui, const char *element, xml_attribute_map &attributes)
1202 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));
1203 container = GTK_CONTAINER(align);
1204 gtk_widget_set_name(GTK_WIDGET(align), "Calf-Align");
1205 return align;
1208 /******************************** GtkFrame contaner ********************************/
1210 GtkWidget *frame_container::create(plugin_gui *_gui, const char *element, xml_attribute_map &attributes)
1212 GtkWidget *frame = gtk_frame_new(attribs["label"].c_str());
1213 container = GTK_CONTAINER(frame);
1214 gtk_widget_set_name(GTK_WIDGET(frame), "Calf-Frame");
1215 return frame;
1218 /******************************** GtkBox type of containers ********************************/
1220 void box_container::add(GtkWidget *w, control_base *base)
1222 gtk_container_add_with_properties(container, w, "expand", get_int("expand", 1), "fill", get_int("fill", 1), NULL);
1225 /******************************** GtkHBox container ********************************/
1227 GtkWidget *hbox_container::create(plugin_gui *_gui, const char *element, xml_attribute_map &attributes)
1229 GtkWidget *hbox = gtk_hbox_new(get_int("homogeneous") >= 1, get_int("spacing", 2));
1230 container = GTK_CONTAINER(hbox);
1231 gtk_widget_set_name(GTK_WIDGET(hbox), "Calf-HBox");
1232 return hbox;
1235 /******************************** GtkVBox container ********************************/
1237 GtkWidget *vbox_container::create(plugin_gui *_gui, const char *element, xml_attribute_map &attributes)
1239 GtkWidget *vbox = gtk_vbox_new(get_int("homogeneous") >= 1, get_int("spacing", 2));
1240 container = GTK_CONTAINER(vbox);
1241 gtk_widget_set_name(GTK_WIDGET(vbox), "Calf-VBox");
1242 return vbox;
1245 /******************************** GtkNotebook container ********************************/
1247 GtkWidget *notebook_container::create(plugin_gui *_gui, const char *element, xml_attribute_map &attributes)
1249 GtkWidget *nb = gtk_notebook_new();
1250 container = GTK_CONTAINER(nb);
1251 gtk_widget_set_name(GTK_WIDGET(nb), "Calf-Notebook");
1252 return nb;
1255 void notebook_container::add(GtkWidget *w, control_base *base)
1257 gtk_notebook_append_page(GTK_NOTEBOOK(container), w, gtk_label_new_with_mnemonic(base->attribs["page"].c_str()));
1260 /******************************** GtkNotebook container ********************************/
1262 GtkWidget *scrolled_container::create(plugin_gui *_gui, const char *element, xml_attribute_map &attributes)
1264 GtkAdjustment *horiz = NULL, *vert = NULL;
1265 int width = get_int("width", 0), height = get_int("height", 0);
1266 if (width)
1267 horiz = GTK_ADJUSTMENT(gtk_adjustment_new(get_int("x", 0), 0, width, get_int("step-x", 1), get_int("page-x", width / 10), 100));
1268 if (height)
1269 vert = GTK_ADJUSTMENT(gtk_adjustment_new(get_int("y", 0), 0, width, get_int("step-y", 1), get_int("page-y", height / 10), 10));
1270 GtkWidget *sw = gtk_scrolled_window_new(horiz, vert);
1271 gtk_widget_set_size_request(sw, get_int("req-x", -1), get_int("req-y", -1));
1272 container = GTK_CONTAINER(sw);
1273 gtk_widget_set_name(GTK_WIDGET(sw), "Calf-ScrolledWindow");
1274 return sw;
1277 void scrolled_container::add(GtkWidget *w, control_base *base)
1279 gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(container), w);