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
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>
32 #include <calf/gui_controls.h>
33 #include <calf/utils.h>
36 using namespace calf_plugins
;
37 using namespace calf_utils
;
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)
61 const std::string
&v
= attribs
[name
];
62 if (v
.empty() || v
.find_first_not_of("-+0123456789") != string::npos
)
64 return atoi(v
.c_str());
67 float control_base::get_float(const char *name
, float def_value
)
69 if (attribs
.count(name
) == 0)
71 const std::string
&v
= attribs
[name
];
72 if (v
.empty() || v
.find_first_not_of("-+0123456789.") != string::npos
)
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"];
88 gtk_widget_set_name(GTK_WIDGET(container
), name
.c_str());
93 /************************* param-associated control base class **************/
95 param_control::param_control()
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"];
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);
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
)
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()
146 gtk_widget_destroy(label
);
148 gtk_widget_destroy(widget
);
151 /******************************** controls ********************************/
155 GtkWidget
*combo_box_param_control::create(plugin_gui
*_gui
, int _param_no
)
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");
174 void combo_box_param_control::set()
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()
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"))
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);
204 jhp
->gui
->plugin
->configure(jhp
->attribs
["setter-key"].c_str(), key
);
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
);
219 std::string v
= value
;
222 while (pos
< v
.length()) {
223 size_t endpos
= v
.find("\n", pos
);
224 if (endpos
== string::npos
)
226 string line
= v
.substr(pos
, endpos
- pos
);
228 size_t tabpos
= line
.find('\t');
229 if (tabpos
== string::npos
)
232 key
= line
.substr(0, tabpos
);
233 label
= line
.substr(tabpos
+ 1);
236 gtk_list_store_insert_with_values (lstore
, >i
, i
, 0, label
.c_str(), 1, key
.c_str(), -1);
243 if (attribs
.count("current-key") && key
== attribs
["current-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
);
259 gtk_combo_box_set_active (GTK_COMBO_BOX (widget
), -1);
264 GtkWidget
*hscale_param_control::create(plugin_gui
*_gui
, int _param_no
)
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);
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);
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()
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
;
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
);
328 // return g_strdup_printf ("%s = %g", props.to_string (cvalue).c_str(), arg1);
329 return g_strdup (props
.to_string (cvalue
).c_str());
334 GtkWidget
*vscale_param_control::create(plugin_gui
*_gui
, int _param_no
)
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);
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
);
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()
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
;
386 GtkWidget
*label_param_control::create(plugin_gui
*_gui
, int _param_no
)
388 gui
= _gui
, param_no
= _param_no
;
390 if (param_no
!= -1 && !attribs
.count("text"))
391 text
= get_props().name
;
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");
402 GtkWidget
*value_param_control::create(plugin_gui
*_gui
, int _param_no
)
405 param_no
= _param_no
;
407 widget
= gtk_label_new ("");
410 const parameter_properties
&props
= get_props();
411 gtk_label_set_width_chars (GTK_LABEL (widget
), props
.get_char_count());
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");
425 void value_param_control::set()
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
)
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
);
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");
466 void vumeter_param_control::set()
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
)));
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");
488 void led_param_control::set()
491 // const parameter_properties &props = get_props();
492 calf_led_set_value (CALF_LED (widget
), gui
->plugin
->get_param_value(param_no
));
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");
511 void tube_param_control::set()
514 // const parameter_properties &props = get_props();
515 calf_tube_set_value (CALF_TUBE (widget
), gui
->plugin
->get_param_value(param_no
));
522 GtkWidget
*check_param_control::create(plugin_gui
*_gui
, int _param_no
)
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");
533 void check_param_control::check_value_changed(GtkCheckButton
*widget
, gpointer value
)
535 param_control
*jhp
= (param_control
*)value
;
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()
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
);
554 GtkWidget
*radio_param_control::create(plugin_gui
*_gui
, int _param_no
)
557 param_no
= _param_no
;
558 require_attribute("value");
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
;
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());
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");
588 void radio_param_control::radio_clicked(GtkRadioButton
*widget
, gpointer value
)
590 param_control
*jhp
= (param_control
*)value
;
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()
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
));
612 GtkWidget
*spin_param_control::create(plugin_gui
*_gui
, int _param_no
)
615 param_no
= _param_no
;
617 const parameter_properties
&props
= get_props();
619 widget
= gtk_spin_button_new_with_range (props
.min
, props
.max
, (props
.max
- props
.min
) / (props
.step
- 1));
621 widget
= gtk_spin_button_new_with_range (props
.min
, props
.max
, props
.step
);
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");
630 void spin_param_control::value_changed(GtkSpinButton
*widget
, gpointer value
)
632 param_control
*jhp
= (param_control
*)value
;
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()
645 // const parameter_properties &props = get_props();
646 gtk_spin_button_set_value (GTK_SPIN_BUTTON (widget
), gui
->plugin
->get_param_value(param_no
));
651 GtkWidget
*button_param_control::create(plugin_gui
*_gui
, int _param_no
)
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");
663 void button_param_control::button_clicked(GtkButton
*widget
, gpointer value
)
665 param_control
*jhp
= (param_control
*)value
;
670 void button_param_control::button_press_event(GtkButton
*widget
, GdkEvent
*event
, gpointer value
)
672 param_control
*jhp
= (param_control
*)value
;
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
;
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()
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
));
701 GtkWidget
*knob_param_control::create(plugin_gui
*_gui
, int _param_no
)
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");
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);
732 void knob_param_control::set()
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
)));
741 void knob_param_control::knob_value_changed(GtkWidget
*widget
, gpointer value
)
743 param_control
*jhp
= (param_control
*)value
;
749 GtkWidget
*toggle_param_control::create(plugin_gui
*_gui
, int _param_no
)
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");
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);
776 void toggle_param_control::set()
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
)));
785 void toggle_param_control::toggle_value_changed(GtkWidget
*widget
, gpointer value
)
787 param_control
*jhp
= (param_control
*)value
;
793 GtkWidget
*keyboard_param_control::create(plugin_gui
*_gui
, int _param_no
)
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");
809 struct curve_param_control_callback
: public CalfCurve::EventAdapter
811 curve_param_control
*ctl
;
813 curve_param_control_callback(curve_param_control
*_ctl
)
816 virtual void curve_changed(CalfCurve
*src
, const CalfCurve::point_vector
&data
) {
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
)
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");
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
;
854 unsigned int npoints
= 0;
858 for (i
= 0; i
< npoints
&& i
< curve
->point_limit
; i
++)
861 pts
.push_back(CalfCurve::point(x
, y
));
863 calf_curve_set_points(widget
, pts
);
870 GtkWidget
*entry_param_control::create(plugin_gui
*_gui
, int _param_no
)
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");
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
));
901 GtkWidget
*filechooser_param_control::create(plugin_gui
*_gui
, int _param_no
)
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");
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
));
934 ctl
->gui
->plugin
->configure(ctl
->attribs
["key"].c_str(), filename
);
939 void line_graph_param_control::on_idle()
941 if (get_int("refresh", 0))
945 GtkWidget
*line_graph_param_control::create(plugin_gui
*_gui
, int _param_no
)
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");
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
))
974 last_generation
= calf_line_graph_update_if(CALF_LINE_GRAPH(widget
), last_generation
);
978 line_graph_param_control::~line_graph_param_control()
984 void phase_graph_param_control::on_idle()
986 if (get_int("refresh", 0))
990 GtkWidget
*phase_graph_param_control::create(plugin_gui
*_gui
, int _param_no
)
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");
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)
1013 // int ws = gdk_window_get_state(widget->window);
1014 // if (ws & (GDK_WINDOW_STATE_WITHDRAWN | GDK_WINDOW_STATE_ICONIFIED))
1016 // //last_generation = calf_phase_graph_update_if(CALF_PHASE_GRAPH(widget), last_generation);
1020 phase_graph_param_control::~phase_graph_param_control()
1026 GtkWidget
*listview_param_control::create(plugin_gui
*_gui
, int _param_no
)
1029 param_no
= _param_no
;
1031 string key
= attribs
["key"];
1032 tmif
= gui
->plugin
->get_metadata_iface()->get_table_metadata_iface(key
.c_str());
1035 g_error("Missing table_metadata_iface for variable '%s'", key
.c_str());
1039 const table_column_info
*tci
= tmif
->get_table_columns();
1042 while (tci
[cols
].name
!= NULL
)
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
));
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
);
1068 bool editable
= tci
[i
].type
!= TCT_LABEL
;
1069 cr
= gtk_cell_renderer_text_new ();
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");
1083 void listview_param_control::set_rows(unsigned int needed_rows
)
1085 while(positions
.size() < needed_rows
)
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
);
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
);
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
);
1126 if (row
>= (int)positions
.size())
1129 gtk_list_store_set(lstore
, &positions
[row
], col
, value
, -1);
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
);
1141 const char *error_or_null
= pThis
->gui
->plugin
->configure(key
.c_str(), new_text
);
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
);
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);
1176 gtk_table_set_homogeneous(GTK_TABLE(table
), TRUE
);
1178 container
= GTK_CONTAINER(table
);
1179 gtk_widget_set_name(GTK_WIDGET(table
), "Calf-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");
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");
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");
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");
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");
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);
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));
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");
1277 void scrolled_container::add(GtkWidget
*w
, control_base
*base
)
1279 gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(container
), w
);