+ JACK host: fix compilation with LASH (thanks Joeboy&drobilla) and printf format...
[calf.git] / src / gui_controls.cpp
blobeab1fa9353bd5ef38cf49698ec01ba257207a92e
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_led.h>
26 #include <calf/giface.h>
27 #include <calf/gui.h>
28 #include <calf/gui_controls.h>
29 #include <calf/preset.h>
30 #include <calf/preset_gui.h>
31 #include <calf/main_win.h>
32 #include <gdk/gdk.h>
34 using namespace calf_plugins;
35 using namespace std;
37 /******************************** controls ********************************/
39 // combo box
41 GtkWidget *combo_box_param_control::create(plugin_gui *_gui, int _param_no)
43 gui = _gui;
44 param_no = _param_no;
45 lstore = gtk_list_store_new(2, G_TYPE_STRING, G_TYPE_STRING); // value, key
47 parameter_properties &props = get_props();
48 widget = gtk_combo_box_new_text ();
49 if (props.choices)
51 for (int j = (int)props.min; j <= (int)props.max; j++)
52 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);
54 gtk_combo_box_set_model (GTK_COMBO_BOX(widget), GTK_TREE_MODEL(lstore));
55 gtk_signal_connect (GTK_OBJECT (widget), "changed", G_CALLBACK (combo_value_changed), (gpointer)this);
56 gtk_widget_set_name(GTK_WIDGET(widget), "Calf-Combobox");
57 return widget;
60 void combo_box_param_control::set()
62 _GUARD_CHANGE_
63 parameter_properties &props = get_props();
64 gtk_combo_box_set_active (GTK_COMBO_BOX (widget), (int)gui->plugin->get_param_value(param_no) - (int)props.min);
67 void combo_box_param_control::get()
69 parameter_properties &props = get_props();
70 gui->set_param_value(param_no, gtk_combo_box_get_active (GTK_COMBO_BOX(widget)) + props.min, this);
73 void combo_box_param_control::combo_value_changed(GtkComboBox *widget, gpointer value)
75 combo_box_param_control *jhp = (combo_box_param_control *)value;
76 if (jhp->attribs.count("setter-key"))
78 GtkTreeIter iter;
79 gchar *key = NULL;
80 if (gtk_combo_box_get_active_iter (GTK_COMBO_BOX (jhp->widget), &iter))
82 gtk_tree_model_get (GTK_TREE_MODEL (jhp->lstore), &iter, 1, &key, -1);
83 if (key) {
84 jhp->gui->plugin->configure(jhp->attribs["setter-key"].c_str(), key);
85 free(key);
89 else
90 jhp->get();
93 void combo_box_param_control::send_status(const char *key, const char *value)
95 if (attribs.count("key") && key == attribs["key"])
97 gtk_list_store_clear (lstore);
98 key2pos.clear();
99 std::string v = value;
100 int i = 0;
101 size_t pos = 0;
102 while (pos < v.length()) {
103 size_t endpos = v.find("\n", pos);
104 if (endpos == string::npos)
105 break;
106 string line = v.substr(pos, endpos - pos);
107 string key, label;
108 size_t tabpos = line.find('\t');
109 if (tabpos == string::npos)
110 key = label = line;
111 else {
112 key = line.substr(0, tabpos);
113 label = line.substr(tabpos + 1);
115 GtkTreeIter gti;
116 gtk_list_store_insert_with_values (lstore, &gti, i, 0, label.c_str(), 1, key.c_str(), -1);
117 key2pos[key] = gti;
118 pos = endpos + 1;
119 i++;
121 set_to_last_key();
123 if (attribs.count("current-key") && key == attribs["current-key"])
125 last_key = value;
126 set_to_last_key();
130 void combo_box_param_control::set_to_last_key()
132 map<string, GtkTreeIter>::iterator i = key2pos.find(last_key);
133 if (i != key2pos.end())
135 gtk_combo_box_set_active_iter (GTK_COMBO_BOX (widget), &i->second);
138 else
139 gtk_combo_box_set_active (GTK_COMBO_BOX (widget), -1);
142 // horizontal fader
144 GtkWidget *hscale_param_control::create(plugin_gui *_gui, int _param_no)
146 gui = _gui;
147 param_no = _param_no;
149 widget = gtk_hscale_new_with_range (0, 1, get_props().get_increment());
150 gtk_signal_connect (GTK_OBJECT (widget), "value-changed", G_CALLBACK (hscale_value_changed), (gpointer)this);
151 gtk_signal_connect (GTK_OBJECT (widget), "format-value", G_CALLBACK (hscale_format_value), (gpointer)this);
153 if(get_int("inverted", 0) > 0) {
154 gtk_range_set_inverted(GTK_RANGE(widget), TRUE);
156 int size = get_int("size", 2);
157 if(size < 1)
158 size = 1;
159 if(size > 2)
160 size = 2;
161 char *name = g_strdup_printf("Calf-HScale%i", size);
162 gtk_widget_set_name(GTK_WIDGET(widget), name);
163 gtk_widget_set_size_request (widget, size * 100, -1);
164 g_free(name);
165 return widget;
168 void hscale_param_control::init_xml(const char *element)
170 if (attribs.count("width"))
171 gtk_widget_set_size_request (widget, get_int("width", 200), -1);
172 if (attribs.count("position"))
174 string v = attribs["position"];
175 if (v == "top") gtk_scale_set_value_pos(GTK_SCALE(widget), GTK_POS_TOP);
176 if (v == "bottom") gtk_scale_set_value_pos(GTK_SCALE(widget), GTK_POS_BOTTOM);
180 void hscale_param_control::set()
182 _GUARD_CHANGE_
183 parameter_properties &props = get_props();
184 gtk_range_set_value (GTK_RANGE (widget), props.to_01 (gui->plugin->get_param_value(param_no)));
185 // hscale_value_changed (GTK_HSCALE (widget), (gpointer)this);
188 void hscale_param_control::get()
190 parameter_properties &props = get_props();
191 float cvalue = props.from_01 (gtk_range_get_value (GTK_RANGE (widget)));
192 gui->set_param_value(param_no, cvalue, this);
195 void hscale_param_control::hscale_value_changed(GtkHScale *widget, gpointer value)
197 hscale_param_control *jhp = (hscale_param_control *)value;
198 jhp->get();
201 gchar *hscale_param_control::hscale_format_value(GtkScale *widget, double arg1, gpointer value)
203 hscale_param_control *jhp = (hscale_param_control *)value;
204 const parameter_properties &props = jhp->get_props();
205 float cvalue = props.from_01 (arg1);
207 // for testing
208 // return g_strdup_printf ("%s = %g", props.to_string (cvalue).c_str(), arg1);
209 return g_strdup (props.to_string (cvalue).c_str());
212 // vertical fader
214 GtkWidget *vscale_param_control::create(plugin_gui *_gui, int _param_no)
216 gui = _gui;
217 param_no = _param_no;
218 widget = gtk_vscale_new_with_range (0, 1, get_props().get_increment());
219 gtk_signal_connect (GTK_OBJECT (widget), "value-changed", G_CALLBACK (vscale_value_changed), (gpointer)this);
220 gtk_scale_set_draw_value(GTK_SCALE(widget), FALSE);
222 if(get_int("inverted", 0) > 0) {
223 gtk_range_set_inverted(GTK_RANGE(widget), TRUE);
225 int size = get_int("size", 2);
226 if(size < 1)
227 size = 1;
228 if(size > 2)
229 size = 2;
230 char *name = g_strdup_printf("Calf-VScale%i", size);
231 gtk_widget_set_size_request (widget, -1, size * 100);
232 gtk_widget_set_name(GTK_WIDGET(widget), name);
233 g_free(name);
234 return widget;
237 void vscale_param_control::init_xml(const char *element)
239 if (attribs.count("height"))
240 gtk_widget_set_size_request (widget, -1, get_int("height", 200));
243 void vscale_param_control::set()
245 _GUARD_CHANGE_
246 parameter_properties &props = get_props();
247 gtk_range_set_value (GTK_RANGE (widget), props.to_01 (gui->plugin->get_param_value(param_no)));
248 // vscale_value_changed (GTK_HSCALE (widget), (gpointer)this);
251 void vscale_param_control::get()
253 parameter_properties &props = get_props();
254 float cvalue = props.from_01 (gtk_range_get_value (GTK_RANGE (widget)));
255 gui->set_param_value(param_no, cvalue, this);
258 void vscale_param_control::vscale_value_changed(GtkHScale *widget, gpointer value)
260 vscale_param_control *jhp = (vscale_param_control *)value;
261 jhp->get();
264 // label
266 GtkWidget *label_param_control::create(plugin_gui *_gui, int _param_no)
268 gui = _gui, param_no = _param_no;
269 string text;
270 if (param_no != -1 && !attribs.count("text"))
271 text = get_props().name;
272 else
273 text = attribs["text"];
274 widget = gtk_label_new(text.c_str());
275 gtk_misc_set_alignment (GTK_MISC (widget), get_float("align-x", 0.5), get_float("align-y", 0.5));
276 gtk_widget_set_name(GTK_WIDGET(widget), "Calf-Label");
277 return widget;
280 // value
282 GtkWidget *value_param_control::create(plugin_gui *_gui, int _param_no)
284 gui = _gui;
285 param_no = _param_no;
287 widget = gtk_label_new ("");
288 if (param_no != -1)
290 parameter_properties &props = get_props();
291 gtk_label_set_width_chars (GTK_LABEL (widget), props.get_char_count());
293 else
295 require_attribute("key");
296 require_int_attribute("width");
297 param_variable = attribs["key"];
298 gtk_label_set_width_chars (GTK_LABEL (widget), get_int("width"));
300 gtk_misc_set_alignment (GTK_MISC (widget), get_float("align-x", 0.5), get_float("align-y", 0.5));
301 gtk_widget_set_name(GTK_WIDGET(widget), "Calf-Value");
302 return widget;
305 void value_param_control::set()
307 if (param_no == -1)
308 return;
309 _GUARD_CHANGE_
310 parameter_properties &props = get_props();
311 gtk_label_set_text (GTK_LABEL (widget), props.to_string(gui->plugin->get_param_value(param_no)).c_str());
314 void value_param_control::send_status(const char *key, const char *value)
316 if (key == param_variable)
318 gtk_label_set_text (GTK_LABEL (widget), value);
322 // VU meter
324 GtkWidget *vumeter_param_control::create(plugin_gui *_gui, int _param_no)
326 gui = _gui, param_no = _param_no;
327 // parameter_properties &props = get_props();
328 widget = calf_vumeter_new ();
329 gtk_widget_set_name(GTK_WIDGET(widget), "calf-vumeter");
330 calf_vumeter_set_mode (CALF_VUMETER (widget), (CalfVUMeterMode)get_int("mode", 0));
331 CALF_VUMETER(widget)->vumeter_hold = get_float("hold", 0);
332 CALF_VUMETER(widget)->vumeter_falloff = get_float("falloff", 0.f);
333 gtk_widget_set_name(GTK_WIDGET(widget), "Calf-VUMeter");
334 return widget;
337 void vumeter_param_control::set()
339 _GUARD_CHANGE_
340 parameter_properties &props = get_props();
341 calf_vumeter_set_value (CALF_VUMETER (widget), props.to_01(gui->plugin->get_param_value(param_no)));
342 if (label)
343 update_label();
346 // LED
348 GtkWidget *led_param_control::create(plugin_gui *_gui, int _param_no)
350 gui = _gui, param_no = _param_no;
351 // parameter_properties &props = get_props();
352 widget = calf_led_new ();
353 gtk_widget_set_name(GTK_WIDGET(widget), "calf-led");
354 CALF_LED(widget)->led_mode = get_int("mode", 0);
355 gtk_widget_set_name(GTK_WIDGET(widget), "Calf-LED");
356 return widget;
359 void led_param_control::set()
361 _GUARD_CHANGE_
362 // parameter_properties &props = get_props();
363 calf_led_set_value (CALF_LED (widget), gui->plugin->get_param_value(param_no));
364 if (label)
365 update_label();
368 // tube
370 GtkWidget *tube_param_control::create(plugin_gui *_gui, int _param_no)
372 gui = _gui, param_no = _param_no;
373 // parameter_properties &props = get_props();
374 widget = calf_tube_new ();
375 gtk_widget_set_name(GTK_WIDGET(widget), "calf-tube");
376 CALF_TUBE(widget)->size = get_int("size", 2);
377 CALF_TUBE(widget)->direction = get_int("direction", 2);
378 gtk_widget_set_name(GTK_WIDGET(widget), "Calf-Tube");
379 return widget;
382 void tube_param_control::set()
384 _GUARD_CHANGE_
385 // parameter_properties &props = get_props();
386 calf_tube_set_value (CALF_TUBE (widget), gui->plugin->get_param_value(param_no));
387 if (label)
388 update_label();
391 // check box
393 GtkWidget *check_param_control::create(plugin_gui *_gui, int _param_no)
395 gui = _gui;
396 param_no = _param_no;
398 widget = gtk_check_button_new ();
399 gtk_signal_connect (GTK_OBJECT (widget), "toggled", G_CALLBACK (check_value_changed), (gpointer)this);
400 gtk_widget_set_name(GTK_WIDGET(widget), "Calf-Checkbox");
401 return widget;
404 void check_param_control::check_value_changed(GtkCheckButton *widget, gpointer value)
406 param_control *jhp = (param_control *)value;
407 jhp->get();
410 void check_param_control::get()
412 const parameter_properties &props = get_props();
413 gui->set_param_value(param_no, gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON(widget)) + props.min, this);
416 void check_param_control::set()
418 _GUARD_CHANGE_
419 const parameter_properties &props = get_props();
420 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (widget), (int)gui->plugin->get_param_value(param_no) - (int)props.min);
423 // radio button
425 GtkWidget *radio_param_control::create(plugin_gui *_gui, int _param_no)
427 gui = _gui;
428 param_no = _param_no;
429 require_attribute("value");
430 int value = -1;
431 string value_name = attribs["value"];
432 const parameter_properties &props = get_props();
433 if (props.choices && (value_name < "0" || value_name > "9"))
435 for (int i = 0; props.choices[i]; i++)
437 if (value_name == props.choices[i])
439 value = i + (int)props.min;
440 break;
444 if (value == -1)
445 value = get_int("value");
447 if (attribs.count("label"))
448 widget = gtk_radio_button_new_with_label (gui->get_radio_group(param_no), attribs["label"].c_str());
449 else
450 widget = gtk_radio_button_new_with_label (gui->get_radio_group(param_no), props.choices[value - (int)props.min]);
451 gtk_toggle_button_set_mode (GTK_TOGGLE_BUTTON (widget), FALSE);
453 gui->set_radio_group(param_no, gtk_radio_button_get_group (GTK_RADIO_BUTTON (widget)));
454 gtk_signal_connect (GTK_OBJECT (widget), "clicked", G_CALLBACK (radio_clicked), (gpointer)this);
455 gtk_widget_set_name(GTK_WIDGET(widget), "Calf-RadioButton");
456 return widget;
459 void radio_param_control::radio_clicked(GtkRadioButton *widget, gpointer value)
461 param_control *jhp = (param_control *)value;
462 jhp->get();
465 void radio_param_control::get()
467 // const parameter_properties &props = get_props();
468 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget)))
469 gui->set_param_value(param_no, value, this);
472 void radio_param_control::set()
474 _GUARD_CHANGE_
475 const parameter_properties &props = get_props();
476 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (widget), value == ((int)gui->plugin->get_param_value(param_no) - (int)props.min));
479 // spin button
481 GtkWidget *spin_param_control::create(plugin_gui *_gui, int _param_no)
483 gui = _gui;
484 param_no = _param_no;
486 const parameter_properties &props = get_props();
487 if (props.step > 1)
488 widget = gtk_spin_button_new_with_range (props.min, props.max, (props.max - props.min) / (props.step - 1));
489 if (props.step > 0)
490 widget = gtk_spin_button_new_with_range (props.min, props.max, props.step);
491 else
492 widget = gtk_spin_button_new_with_range (props.min, props.max, 1);
493 gtk_spin_button_set_digits (GTK_SPIN_BUTTON(widget), get_int("digits", 0));
494 gtk_signal_connect (GTK_OBJECT (widget), "value-changed", G_CALLBACK (value_changed), (gpointer)this);
495 gtk_widget_set_name(GTK_WIDGET(widget), "Calf-SpinButton");
496 return widget;
499 void spin_param_control::value_changed(GtkSpinButton *widget, gpointer value)
501 param_control *jhp = (param_control *)value;
502 jhp->get();
505 void spin_param_control::get()
507 // const parameter_properties &props = get_props();
508 gui->set_param_value(param_no, gtk_spin_button_get_value_as_float (GTK_SPIN_BUTTON (widget)), this);
511 void spin_param_control::set()
513 _GUARD_CHANGE_
514 // const parameter_properties &props = get_props();
515 gtk_spin_button_set_value (GTK_SPIN_BUTTON (widget), gui->plugin->get_param_value(param_no));
518 // button
520 GtkWidget *button_param_control::create(plugin_gui *_gui, int _param_no)
522 gui = _gui;
523 param_no = _param_no;
525 widget = gtk_button_new_with_label (get_props().name);
526 gtk_signal_connect (GTK_OBJECT (widget), "clicked", G_CALLBACK (button_clicked), (gpointer)this);
527 gtk_widget_set_name(GTK_WIDGET(widget), "Calf-Button");
528 return widget;
531 void button_param_control::button_clicked(GtkButton *widget, gpointer value)
533 param_control *jhp = (param_control *)value;
535 jhp->get();
538 void button_param_control::get()
540 const parameter_properties &props = get_props();
541 gui->set_param_value(param_no, props.max, this);
544 void button_param_control::set()
546 _GUARD_CHANGE_
547 const parameter_properties &props = get_props();
548 if (gui->plugin->get_param_value(param_no) - props.min >= 0.5)
549 gtk_button_clicked (GTK_BUTTON (widget));
552 // knob
554 GtkWidget *knob_param_control::create(plugin_gui *_gui, int _param_no)
556 gui = _gui;
557 param_no = _param_no;
558 const parameter_properties &props = get_props();
560 //widget = calf_knob_new_with_range (props.to_01 (gui->plugin->get_param_value(param_no)), 0, 1, 0.01);
561 widget = calf_knob_new();
562 float increment = props.get_increment();
563 gtk_range_get_adjustment(GTK_RANGE(widget))->step_increment = increment;
564 CALF_KNOB(widget)->knob_type = get_int("type");
565 CALF_KNOB(widget)->knob_size = get_int("size", 2);
566 if(CALF_KNOB(widget)->knob_size > 5) {
567 CALF_KNOB(widget)->knob_size = 5;
568 } else if (CALF_KNOB(widget)->knob_size < 1) {
569 CALF_KNOB(widget)->knob_size = 1;
571 gtk_signal_connect(GTK_OBJECT(widget), "value-changed", G_CALLBACK(knob_value_changed), (gpointer)this);
572 gtk_widget_set_name(GTK_WIDGET(widget), "Calf-Knob");
573 return widget;
576 void knob_param_control::get()
578 const parameter_properties &props = get_props();
579 float value = props.from_01(gtk_range_get_value(GTK_RANGE(widget)));
580 gui->set_param_value(param_no, value, this);
581 if (label)
582 update_label();
585 void knob_param_control::set()
587 _GUARD_CHANGE_
588 const parameter_properties &props = get_props();
589 gtk_range_set_value(GTK_RANGE(widget), props.to_01 (gui->plugin->get_param_value(param_no)));
590 if (label)
591 update_label();
594 void knob_param_control::knob_value_changed(GtkWidget *widget, gpointer value)
596 param_control *jhp = (param_control *)value;
597 jhp->get();
600 // Toggle Button
602 GtkWidget *toggle_param_control::create(plugin_gui *_gui, int _param_no)
604 gui = _gui;
605 param_no = _param_no;
606 widget = calf_toggle_new ();
608 CALF_TOGGLE(widget)->size = get_int("size", 2);
609 if(CALF_TOGGLE(widget)->size > 2) {
610 CALF_TOGGLE(widget)->size = 2;
611 } else if (CALF_TOGGLE(widget)->size < 1) {
612 CALF_TOGGLE(widget)->size = 1;
615 gtk_signal_connect (GTK_OBJECT (widget), "value-changed", G_CALLBACK (toggle_value_changed), (gpointer)this);
616 gtk_widget_set_name(GTK_WIDGET(widget), "Calf-ToggleButton");
617 return widget;
620 void toggle_param_control::get()
622 const parameter_properties &props = get_props();
623 float value = props.from_01(gtk_range_get_value(GTK_RANGE(widget)));
624 gui->set_param_value(param_no, value, this);
625 if (label)
626 update_label();
629 void toggle_param_control::set()
631 _GUARD_CHANGE_
632 const parameter_properties &props = get_props();
633 gtk_range_set_value(GTK_RANGE(widget), props.to_01 (gui->plugin->get_param_value(param_no)));
634 if (label)
635 update_label();
638 void toggle_param_control::toggle_value_changed(GtkWidget *widget, gpointer value)
640 param_control *jhp = (param_control *)value;
641 jhp->get();
644 // keyboard
646 GtkWidget *keyboard_param_control::create(plugin_gui *_gui, int _param_no)
648 gui = _gui;
649 param_no = _param_no;
650 // const parameter_properties &props = get_props();
652 widget = calf_keyboard_new();
653 kb = CALF_KEYBOARD(widget);
654 kb->nkeys = get_int("octaves", 4) * 7 + 1;
655 kb->sink = new CalfKeyboard::EventAdapter;
656 gtk_widget_set_name(GTK_WIDGET(widget), "Calf-Keyboard");
657 return widget;
660 // curve
662 struct curve_param_control_callback: public CalfCurve::EventAdapter
664 curve_param_control *ctl;
666 curve_param_control_callback(curve_param_control *_ctl)
667 : ctl(_ctl) {}
669 virtual void curve_changed(CalfCurve *src, const CalfCurve::point_vector &data) {
670 stringstream ss;
671 ss << data.size() << endl;
672 for (size_t i = 0; i < data.size(); i++)
673 ss << data[i].first << " " << data[i].second << endl;
674 ctl->gui->plugin->configure(ctl->attribs["key"].c_str(), ss.str().c_str());
676 virtual void clip(CalfCurve *src, int pt, float &x, float &y, bool &hide)
678 // int gridpt = floor(x * 71 * 2);
679 // clip to the middle of the nearest white key
680 x = (floor(x * 71) + 0.5)/ 71.0;
684 GtkWidget *curve_param_control::create(plugin_gui *_gui, int _param_no)
686 gui = _gui;
687 param_no = _param_no;
688 require_attribute("key");
690 widget = calf_curve_new(get_int("maxpoints", -1));
691 curve = CALF_CURVE(widget);
692 curve->sink = new curve_param_control_callback(this);
693 // gtk_curve_set_curve_type(curve, GTK_CURVE_TYPE_LINEAR);
694 gtk_widget_set_name(GTK_WIDGET(widget), "Calf-Curve");
695 return widget;
698 void curve_param_control::send_configure(const char *key, const char *value)
700 // cout << "send conf " << key << endl;
701 if (attribs["key"] == key)
703 stringstream ss(value);
704 CalfCurve::point_vector pts;
705 if (*value)
707 unsigned int npoints = 0;
708 ss >> npoints;
709 unsigned int i;
710 float x = 0, y = 0;
711 for (i = 0; i < npoints && i < curve->point_limit; i++)
713 ss >> x >> y;
714 pts.push_back(CalfCurve::point(x, y));
716 calf_curve_set_points(widget, pts);
721 // entry
723 GtkWidget *entry_param_control::create(plugin_gui *_gui, int _param_no)
725 gui = _gui;
726 param_no = _param_no;
727 require_attribute("key");
729 widget = gtk_entry_new();
730 entry = GTK_ENTRY(widget);
731 gtk_signal_connect(GTK_OBJECT(widget), "changed", G_CALLBACK(entry_value_changed), (gpointer)this);
732 gtk_editable_set_editable(GTK_EDITABLE(entry), get_int("editable", 1));
733 gtk_widget_set_name(GTK_WIDGET(widget), "Calf-Entry");
734 return widget;
737 void entry_param_control::send_configure(const char *key, const char *value)
739 // cout << "send conf " << key << endl;
740 if (attribs["key"] == key)
742 gtk_entry_set_text(entry, value);
746 void entry_param_control::entry_value_changed(GtkWidget *widget, gpointer value)
748 entry_param_control *ctl = (entry_param_control *)value;
749 ctl->gui->plugin->configure(ctl->attribs["key"].c_str(), gtk_entry_get_text(ctl->entry));
752 // filechooser
754 GtkWidget *filechooser_param_control::create(plugin_gui *_gui, int _param_no)
756 gui = _gui;
757 param_no = _param_no;
758 require_attribute("key");
759 require_attribute("title");
761 widget = gtk_file_chooser_button_new(attribs["title"].c_str(), GTK_FILE_CHOOSER_ACTION_OPEN);
762 filechooser = GTK_FILE_CHOOSER_BUTTON(widget);
763 // XXXKF this is GTK+ 2.12 function, does any replacement exist?
764 gtk_signal_connect(GTK_OBJECT(widget), "file-set", G_CALLBACK(filechooser_value_changed), (gpointer)this);
765 if (attribs.count("width"))
766 gtk_widget_set_size_request (widget, get_int("width", 200), -1);
767 if (attribs.count("width_chars"))
768 gtk_file_chooser_button_set_width_chars (filechooser, get_int("width_chars"));
769 gtk_widget_set_name(GTK_WIDGET(widget), "Calf-FileButton");
770 return widget;
773 void filechooser_param_control::send_configure(const char *key, const char *value)
775 // cout << "send conf " << key << endl;
776 if (attribs["key"] == key)
778 gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(filechooser), value);
782 void filechooser_param_control::filechooser_value_changed(GtkWidget *widget, gpointer value)
784 filechooser_param_control *ctl = (filechooser_param_control *)value;
785 const char *filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(ctl->filechooser));
786 if (filename)
787 ctl->gui->plugin->configure(ctl->attribs["key"].c_str(), filename);
790 // line graph
792 void line_graph_param_control::on_idle()
794 if (get_int("refresh", 0))
795 set();
798 GtkWidget *line_graph_param_control::create(plugin_gui *_gui, int _param_no)
800 gui = _gui;
801 param_no = _param_no;
802 last_generation = -1;
803 // const parameter_properties &props = get_props();
805 widget = calf_line_graph_new ();
806 gtk_widget_set_name(GTK_WIDGET(widget), "calf-graph");
807 CalfLineGraph *clg = CALF_LINE_GRAPH(widget);
808 widget->requisition.width = get_int("width", 40);
809 widget->requisition.height = get_int("height", 40);
810 calf_line_graph_set_square(clg, get_int("square", 0));
811 clg->source = gui->plugin->get_line_graph_iface();
812 clg->source_id = param_no;
813 gtk_widget_set_name(GTK_WIDGET(widget), "Calf-LineGraph");
814 return widget;
817 void line_graph_param_control::set()
819 GtkWidget *tw = gtk_widget_get_toplevel(widget);
820 if (tw && GTK_WIDGET_TOPLEVEL(tw) && widget->window)
822 int ws = gdk_window_get_state(widget->window);
823 if (ws & (GDK_WINDOW_STATE_WITHDRAWN | GDK_WINDOW_STATE_ICONIFIED))
824 return;
825 last_generation = calf_line_graph_update_if(CALF_LINE_GRAPH(widget), last_generation);
829 line_graph_param_control::~line_graph_param_control()
833 // list view
835 GtkWidget *listview_param_control::create(plugin_gui *_gui, int _param_no)
837 gui = _gui;
838 param_no = _param_no;
840 teif = gui->plugin->get_table_edit_iface();
841 const table_column_info *tci = teif->get_table_columns(param_no);
842 assert(tci);
843 cols = 0;
844 while (tci[cols].name != NULL)
845 cols++;
847 GType *p = new GType[cols];
848 for (int i = 0; i < cols; i++)
849 p[i] = G_TYPE_STRING;
850 lstore = gtk_list_store_newv(cols, p);
851 update_store();
852 widget = gtk_tree_view_new_with_model(GTK_TREE_MODEL(lstore));
853 delete []p;
854 tree = GTK_TREE_VIEW (widget);
855 assert(teif);
856 g_object_set (G_OBJECT (tree), "enable-search", FALSE, "rules-hint", TRUE, "enable-grid-lines", TRUE, NULL);
858 for (int i = 0; i < cols; i++)
860 GtkCellRenderer *cr = NULL;
862 if (tci[i].type == TCT_ENUM) {
863 cr = gtk_cell_renderer_combo_new ();
864 GtkListStore *cls = gtk_list_store_new(2, G_TYPE_INT, G_TYPE_STRING);
865 for (int j = 0; tci[i].values[j]; j++)
866 gtk_list_store_insert_with_values(cls, NULL, j, 0, j, 1, tci[i].values[j], -1);
867 g_object_set(cr, "model", cls, "editable", TRUE, "has-entry", FALSE, "text-column", 1, "mode", GTK_CELL_RENDERER_MODE_EDITABLE, NULL);
869 else {
870 bool editable = tci[i].type != TCT_LABEL;
871 cr = gtk_cell_renderer_text_new ();
872 if (editable)
873 g_object_set(cr, "editable", TRUE, "mode", GTK_CELL_RENDERER_MODE_EDITABLE, NULL);
875 g_object_set_data (G_OBJECT(cr), "column", (void *)&tci[i]);
876 gtk_signal_connect (GTK_OBJECT (cr), "edited", G_CALLBACK (on_edited), (gpointer)this);
877 gtk_signal_connect (GTK_OBJECT (cr), "editing-canceled", G_CALLBACK (on_editing_canceled), (gpointer)this);
878 gtk_tree_view_insert_column_with_attributes(tree, i, tci[i].name, cr, "text", i, NULL);
880 gtk_tree_view_set_headers_visible(tree, TRUE);
881 gtk_widget_set_name(GTK_WIDGET(widget), "Calf-ListView");
882 return widget;
885 void listview_param_control::update_store()
887 gtk_list_store_clear(lstore);
888 uint32_t rows = teif->get_table_rows(param_no);
889 for (uint32_t i = 0; i < rows; i++)
891 GtkTreeIter iter;
892 gtk_list_store_insert(lstore, &iter, i);
893 for (int j = 0; j < cols; j++)
895 gtk_list_store_set(lstore, &iter, j, teif->get_cell(param_no, i, j).c_str(), -1);
897 positions.push_back(iter);
901 void listview_param_control::send_configure(const char *key, const char *value)
903 if (attribs["key"] == key)
905 update_store();
909 void listview_param_control::on_edited(GtkCellRenderer *renderer, gchar *path, gchar *new_text, listview_param_control *pThis)
911 const table_column_info *tci = pThis->teif->get_table_columns(pThis->param_no);
912 int column = ((table_column_info *)g_object_get_data(G_OBJECT(renderer), "column")) - tci;
913 string error;
914 pThis->teif->set_cell(pThis->param_no, atoi(path), column, new_text, error);
915 if (error.empty()) {
916 pThis->update_store();
917 gtk_widget_grab_focus(pThis->widget);
918 if (atoi(path) < (int)pThis->teif->get_table_rows(pThis->param_no))
920 GtkTreePath *gpath = gtk_tree_path_new_from_string (path);
921 gtk_tree_view_set_cursor_on_cell (GTK_TREE_VIEW (pThis->widget), gpath, NULL, NULL, FALSE);
922 gtk_tree_path_free (gpath);
925 else
927 GtkWidget *dialog = gtk_message_dialog_new(pThis->gui->window->toplevel, GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK,
928 "%s", error.c_str());
929 gtk_dialog_run(GTK_DIALOG(dialog));
930 gtk_widget_destroy(dialog);
931 gtk_widget_grab_focus(pThis->widget);
935 void listview_param_control::on_editing_canceled(GtkCellRenderer *renderer, listview_param_control *pThis)
937 gtk_widget_grab_focus(pThis->widget);
940 /******************************** GtkTable container ********************************/
942 GtkWidget *table_container::create(plugin_gui *_gui, const char *element, xml_attribute_map &attributes)
944 require_int_attribute("rows");
945 require_int_attribute("cols");
946 int homog = get_int("homogeneous", 0);
947 GtkWidget *table = gtk_table_new(get_int("rows", 1), get_int("cols", 1), false);
948 if(homog > 0) {
949 gtk_table_set_homogeneous(GTK_TABLE(table), TRUE);
951 container = GTK_CONTAINER(table);
952 gtk_widget_set_name(GTK_WIDGET(table), "Calf-Table");
953 return table;
956 void table_container::add(GtkWidget *widget, control_base *base)
958 base->require_int_attribute("attach-x");
959 base->require_int_attribute("attach-y");
960 int x = base->get_int("attach-x"), y = base->get_int("attach-y");
961 int w = base->get_int("attach-w", 1), h = base->get_int("attach-h", 1);
962 int shrinkx = base->get_int("shrink-x", 0);
963 int shrinky = base->get_int("shrink-y", 0);
964 int fillx = (base->get_int("fill-x", !shrinkx) ? GTK_FILL : 0) | (base->get_int("expand-x", !shrinkx) ? GTK_EXPAND : 0) | (shrinkx ? GTK_SHRINK : 0);
965 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);
966 int padx = base->get_int("pad-x", 2);
967 int pady = base->get_int("pad-y", 2);
968 gtk_table_attach(GTK_TABLE(container), widget, x, x + w, y, y + h, (GtkAttachOptions)fillx, (GtkAttachOptions)filly, padx, pady);
971 /******************************** alignment contaner ********************************/
973 GtkWidget *alignment_container::create(plugin_gui *_gui, const char *element, xml_attribute_map &attributes)
975 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));
976 container = GTK_CONTAINER(align);
977 gtk_widget_set_name(GTK_WIDGET(align), "Calf-Align");
978 return align;
981 /******************************** GtkFrame contaner ********************************/
983 GtkWidget *frame_container::create(plugin_gui *_gui, const char *element, xml_attribute_map &attributes)
985 GtkWidget *frame = gtk_frame_new(attribs["label"].c_str());
986 container = GTK_CONTAINER(frame);
987 gtk_widget_set_name(GTK_WIDGET(frame), "Calf-Frame");
988 return frame;
991 /******************************** GtkBox type of containers ********************************/
993 void box_container::add(GtkWidget *w, control_base *base)
995 gtk_container_add_with_properties(container, w, "expand", get_int("expand", 1), "fill", get_int("fill", 1), NULL);
998 /******************************** GtkHBox container ********************************/
1000 GtkWidget *hbox_container::create(plugin_gui *_gui, const char *element, xml_attribute_map &attributes)
1002 GtkWidget *hbox = gtk_hbox_new(get_int("homogeneous") >= 1, get_int("spacing", 2));
1003 container = GTK_CONTAINER(hbox);
1004 gtk_widget_set_name(GTK_WIDGET(hbox), "Calf-HBox");
1005 return hbox;
1008 /******************************** GtkVBox container ********************************/
1010 GtkWidget *vbox_container::create(plugin_gui *_gui, const char *element, xml_attribute_map &attributes)
1012 GtkWidget *vbox = gtk_vbox_new(get_int("homogeneous") >= 1, get_int("spacing", 2));
1013 container = GTK_CONTAINER(vbox);
1014 gtk_widget_set_name(GTK_WIDGET(vbox), "Calf-VBox");
1015 return vbox;
1018 /******************************** GtkNotebook container ********************************/
1020 GtkWidget *notebook_container::create(plugin_gui *_gui, const char *element, xml_attribute_map &attributes)
1022 GtkWidget *nb = gtk_notebook_new();
1023 container = GTK_CONTAINER(nb);
1024 gtk_widget_set_name(GTK_WIDGET(nb), "Calf-Notebook");
1025 return nb;
1028 void notebook_container::add(GtkWidget *w, control_base *base)
1030 gtk_notebook_append_page(GTK_NOTEBOOK(container), w, gtk_label_new_with_mnemonic(base->attribs["page"].c_str()));
1033 /******************************** GtkNotebook container ********************************/
1035 GtkWidget *scrolled_container::create(plugin_gui *_gui, const char *element, xml_attribute_map &attributes)
1037 GtkAdjustment *horiz = NULL, *vert = NULL;
1038 int width = get_int("width", 0), height = get_int("height", 0);
1039 if (width)
1040 horiz = GTK_ADJUSTMENT(gtk_adjustment_new(get_int("x", 0), 0, width, get_int("step-x", 1), get_int("page-x", width / 10), 100));
1041 if (height)
1042 vert = GTK_ADJUSTMENT(gtk_adjustment_new(get_int("y", 0), 0, width, get_int("step-y", 1), get_int("page-y", height / 10), 10));
1043 GtkWidget *sw = gtk_scrolled_window_new(horiz, vert);
1044 gtk_widget_set_size_request(sw, get_int("req-x", -1), get_int("req-y", -1));
1045 container = GTK_CONTAINER(sw);
1046 gtk_widget_set_name(GTK_WIDGET(sw), "Calf-ScrolledWindow");
1047 return sw;
1050 void scrolled_container::add(GtkWidget *w, control_base *base)
1052 gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(container), w);