Crusher: fix UI crash
[calf.git] / src / gui_controls.cpp
blob0b71a2041c3ef89e5a901beb24d1bd7fe8f7279f
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 <sys/time.h>
24 #include <calf/ctl_curve.h>
25 #include <calf/ctl_keyboard.h>
26 #include <calf/ctl_knob.h>
27 #include <calf/ctl_led.h>
28 #include <calf/ctl_tube.h>
29 #include <calf/ctl_vumeter.h>
30 #include <calf/custom_ctl.h>
31 #include <calf/giface.h>
32 #include <calf/gui.h>
33 #include <calf/gui_controls.h>
34 #include <calf/utils.h>
35 #include <gdk/gdk.h>
36 #include <gdk/gdkkeysyms.h>
37 #include <calf/ctl_linegraph.h>
38 #include <iostream>
40 using namespace calf_plugins;
41 using namespace calf_utils;
42 using namespace std;
44 #define SANITIZE(value) (std::abs(value) < small_value<float>()) ? 0.f : value
46 /******************************** control/container base class **********************/
48 void control_base::require_attribute(const char *name)
50 if (attribs.count(name) == 0) {
51 g_error("Missing attribute '%s' in control '%s'", name, control_name.c_str());
55 void control_base::require_int_attribute(const char *name)
57 require_attribute(name);
58 if (attribs[name].empty() || attribs[name].find_first_not_of("0123456789") != string::npos) {
59 g_error("Wrong data type on attribute '%s' in control '%s' (required integer)", name, control_name.c_str());
63 int control_base::get_int(const char *name, int def_value)
65 if (attribs.count(name) == 0)
66 return def_value;
67 const std::string &v = attribs[name];
68 if (v.empty() || v.find_first_not_of("-+0123456789") != string::npos)
69 return def_value;
70 return atoi(v.c_str());
73 float control_base::get_float(const char *name, float def_value)
75 if (attribs.count(name) == 0)
76 return def_value;
77 const std::string &v = attribs[name];
78 if (v.empty() || v.find_first_not_of("-+0123456789.") != string::npos)
79 return def_value;
80 stringstream ss(v);
81 float value;
82 ss >> value;
83 return value;
86 /******************************** container base class **********************/
88 void control_container::set_std_properties()
90 if (attribs.find("widget-name") != attribs.end())
92 string name = attribs["widget-name"];
93 if (container) {
94 gtk_widget_set_name(GTK_WIDGET(container), name.c_str());
99 /************************* param-associated control base class **************/
101 param_control::param_control()
103 gui = NULL;
104 param_no = -1;
105 in_change = 0;
106 old_displayed_value = -1.f;
107 has_entry = false;
111 void param_control::set_std_properties()
113 if (attribs.find("widget-name") != attribs.end())
115 string name = attribs["widget-name"];
116 if (widget) {
117 gtk_widget_set_name(widget, name.c_str());
122 void param_control::set_visibilty(bool state)
124 if (state)
125 gtk_widget_show(widget);
126 else
127 gtk_widget_hide(widget);
130 void param_control::hook_params()
132 if (param_no != -1) {
133 gui->add_param_ctl(param_no, this);
135 gui->params.push_back(this);
138 param_control::~param_control()
140 //if (GTK_IS_WIDGET(widget))
141 // gtk_widget_destroy(widget);
144 void param_control::add_context_menu_handler()
146 if (widget)
148 g_signal_connect(GTK_OBJECT(widget), "button-press-event", (GCallback)on_button_press_event, this);
152 gboolean param_control::on_button_press_event(GtkWidget *widget, GdkEventButton *event, void *user_data)
154 param_control *self = (param_control *)user_data;
155 const parameter_properties &props = self->get_props();
156 if (event->button == 3 && !(props.flags & PF_PROP_OUTPUT))
158 self->do_popup_menu();
159 return TRUE;
161 else if (event->button == 2)
163 self->create_value_entry(widget, event->x_root, event->y_root);
164 return TRUE;
166 return FALSE;
169 void param_control::do_popup_menu()
171 if (gui)
172 gui->on_control_popup(this, param_no);
175 void param_control::destroy_value_entry ()
177 // remove the window containing the entry
178 gtk_widget_destroy(GTK_WIDGET(entrywin));
179 has_entry = false;
181 gboolean param_control::value_entry_unfocus(GtkWidget *widget, GdkEventFocus *event, void *user_data)
183 // destroy window if it looses focus
184 param_control *self = (param_control *)user_data;
185 self->destroy_value_entry();
186 return TRUE;
188 gboolean param_control::value_entry_action(GtkEntry *widget, GdkEvent *event, void *user_data)
190 // called when a key was hit, sorts out and treats RETURN and ESC
191 param_control *self = (param_control *)user_data;
192 const parameter_properties &props = self->get_props();
193 GdkEventKey *key = (GdkEventKey*)event;
194 if(key->keyval == GDK_Escape)
195 self->destroy_value_entry();
196 else if (key->keyval == GDK_Return) {
197 float val = props.string_to_value(gtk_entry_get_text(widget));
198 self->gui->plugin->set_param_value(self->param_no, val);
199 self->set();
200 self->destroy_value_entry();
202 return FALSE;
204 void param_control::create_value_entry(GtkWidget *widget, int x, int y)
206 if (has_entry) {
207 // kill an existing entry window on re-trigger
208 destroy_value_entry();
209 return;
212 if (param_no < 0)
213 return;
215 const parameter_properties &props = get_props();
216 float value = gui->plugin->get_param_value(param_no);
218 // no chance for a menu, so we have to do everything by hand
219 entrywin = gtk_window_new(GTK_WINDOW_TOPLEVEL);
220 gtk_widget_set_name(GTK_WIDGET(entrywin), "Calf-Value-Entry");
221 gtk_window_set_title (GTK_WINDOW(entrywin), "Calf Value Entry");
222 gtk_window_set_resizable (GTK_WINDOW(entrywin), FALSE);
223 gtk_window_set_decorated (GTK_WINDOW(entrywin), FALSE);
224 gtk_window_set_skip_taskbar_hint (GTK_WINDOW(entrywin), TRUE);
225 gtk_window_set_skip_pager_hint (GTK_WINDOW(entrywin), TRUE);
226 gtk_window_set_transient_for (GTK_WINDOW(entrywin), GTK_WINDOW (gui->window->toplevel));
227 gtk_window_set_gravity(GTK_WINDOW(entrywin), GDK_GRAVITY_CENTER);
228 gtk_widget_set_events (GTK_WIDGET(entrywin), GDK_FOCUS_CHANGE_MASK);
229 g_signal_connect (G_OBJECT(entrywin), "focus-out-event", G_CALLBACK (value_entry_unfocus), this);
231 // create the text entry
232 GtkWidget *entry = gtk_entry_new();
233 gtk_widget_set_name(GTK_WIDGET(entry), "Calf-Entry");
234 gtk_entry_set_width_chars(GTK_ENTRY(entry), props.get_char_count());
235 gtk_entry_set_text(GTK_ENTRY(entry), props.to_string(value).c_str());
236 gtk_widget_add_events (entry, GDK_KEY_PRESS_MASK);
237 g_signal_connect (entry, "key-press-event", (GCallback)value_entry_action, this);
239 // stitch together and show
240 gtk_container_add(GTK_CONTAINER (entrywin), entry);
241 gtk_widget_show_all(entrywin);
242 gtk_window_move(GTK_WINDOW (entrywin), x, y);
244 has_entry = true;
248 /******************************** Combo Box ********************************/
250 GtkWidget *combo_box_param_control::create(plugin_gui *_gui, int _param_no)
252 gui = _gui;
253 param_no = _param_no;
254 lstore = gtk_list_store_new(2, G_TYPE_STRING, G_TYPE_STRING); // value, key
256 const parameter_properties &props = get_props();
257 widget = calf_combobox_new ();
258 if (param_no != -1 && props.choices)
260 for (int j = (int)props.min; j <= (int)props.max; j++)
261 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);
263 gtk_combo_box_set_model (GTK_COMBO_BOX(widget), GTK_TREE_MODEL(lstore));
264 g_signal_connect (GTK_OBJECT (widget), "changed", G_CALLBACK (combo_value_changed), (gpointer)this);
265 gtk_widget_set_name(GTK_WIDGET(widget), "Calf-Combobox");
266 return widget;
269 void combo_box_param_control::set()
271 _GUARD_CHANGE_
272 if (param_no != -1)
274 const parameter_properties &props = get_props();
275 gtk_combo_box_set_active (GTK_COMBO_BOX (widget), (int)gui->plugin->get_param_value(param_no) - (int)props.min);
279 void combo_box_param_control::get()
281 if (param_no != -1)
283 const parameter_properties &props = get_props();
284 gui->set_param_value(param_no, gtk_combo_box_get_active (GTK_COMBO_BOX(widget)) + props.min, this);
288 void combo_box_param_control::combo_value_changed(GtkComboBox *widget, gpointer value)
290 combo_box_param_control *jhp = (combo_box_param_control *)value;
291 if (jhp->attribs.count("setter-key"))
293 GtkTreeIter iter;
294 gchar *key = NULL;
295 if (gtk_combo_box_get_active_iter (GTK_COMBO_BOX (jhp->widget), &iter))
297 gtk_tree_model_get (GTK_TREE_MODEL (jhp->lstore), &iter, 1, &key, -1);
298 if (key) {
299 jhp->gui->plugin->configure(jhp->attribs["setter-key"].c_str(), key);
300 free(key);
304 else
305 jhp->get();
308 void combo_box_param_control::send_status(const char *key, const char *value)
310 if (attribs.count("key") && key == attribs["key"])
312 gtk_list_store_clear (lstore);
313 key2pos.clear();
314 std::string v = value;
315 int i = 0;
316 size_t pos = 0;
317 while (pos < v.length()) {
318 size_t endpos = v.find("\n", pos);
319 if (endpos == string::npos)
320 break;
321 string line = v.substr(pos, endpos - pos);
322 string key, label;
323 size_t tabpos = line.find('\t');
324 if (tabpos == string::npos)
325 key = label = line;
326 else {
327 key = line.substr(0, tabpos);
328 label = line.substr(tabpos + 1);
330 GtkTreeIter gti;
331 gtk_list_store_insert_with_values (lstore, &gti, i, 0, label.c_str(), 1, key.c_str(), -1);
332 key2pos[key] = gti;
333 pos = endpos + 1;
334 i++;
336 set_to_last_key();
338 if (attribs.count("current-key") && key == attribs["current-key"])
340 last_key = value;
341 set_to_last_key();
345 void combo_box_param_control::set_to_last_key()
347 map<string, GtkTreeIter>::iterator i = key2pos.find(last_key);
348 if (i != key2pos.end())
350 gtk_combo_box_set_active_iter (GTK_COMBO_BOX (widget), &i->second);
353 else
354 gtk_combo_box_set_active (GTK_COMBO_BOX (widget), -1);
357 /******************************** Horizontal Fader ********************************/
359 static gboolean
360 scale_to_default (gpointer data)
362 hscale_param_control *jhp = (hscale_param_control *)data;
363 const parameter_properties &props = jhp->get_props();
364 gtk_range_set_value (GTK_RANGE (jhp->widget), props.to_01(props.def_value));
366 return FALSE;
369 static gboolean
370 scale_button_press (GtkWidget *widget, GdkEventKey *event, gpointer *user_data)
372 if (event->type == GDK_2BUTTON_PRESS) {
373 // this actually creates a harmless race condition, but diving deep
374 // into gtk signal handling code wouldn't and the resulting complexity
375 // would not really be worth the effort
376 // The timeout is set high enough that most of the time the race
377 // will turn out in our/the users favor
378 g_timeout_add (200, (GSourceFunc)scale_to_default, user_data);
379 return TRUE;
382 return FALSE;
385 GtkWidget *hscale_param_control::create(plugin_gui *_gui, int _param_no)
387 gui = _gui;
388 param_no = _param_no;
390 widget = calf_fader_new(1, get_int("size", 2), 0, 1, get_props().get_increment());
392 g_signal_connect (GTK_OBJECT (widget), "value-changed", G_CALLBACK (hscale_value_changed), (gpointer)this);
393 g_signal_connect (GTK_OBJECT (widget), "format-value", G_CALLBACK (hscale_format_value), (gpointer)this);
394 g_signal_connect (GTK_OBJECT (widget), "button-press-event", G_CALLBACK (scale_button_press), (gpointer)this);
396 if(get_int("inverted", 0) > 0) {
397 gtk_range_set_inverted(GTK_RANGE(widget), TRUE);
399 int size = get_int("size", 2);
400 char *name = g_strdup_printf("Calf-HScale%i", size);
401 gtk_widget_set_name(GTK_WIDGET(widget), name);
402 gtk_widget_set_size_request (widget, size * 100, -1);
403 g_free(name);
404 return widget;
407 void hscale_param_control::init_xml(const char *element)
409 if (attribs.count("width"))
410 gtk_widget_set_size_request (widget, get_int("width", 200), -1);
411 if (attribs.count("position"))
413 string v = attribs["position"];
414 if (v == "top") gtk_scale_set_value_pos(GTK_SCALE(widget), GTK_POS_TOP);
415 if (v == "bottom") gtk_scale_set_value_pos(GTK_SCALE(widget), GTK_POS_BOTTOM);
419 void hscale_param_control::set()
421 _GUARD_CHANGE_
422 const parameter_properties &props = get_props();
423 gtk_range_set_value (GTK_RANGE (widget), props.to_01 (gui->plugin->get_param_value(param_no)));
424 // hscale_value_changed (GTK_HSCALE (widget), (gpointer)this);
427 void hscale_param_control::get()
429 const parameter_properties &props = get_props();
430 float cvalue = props.from_01 (gtk_range_get_value (GTK_RANGE (widget)));
431 gui->set_param_value(param_no, cvalue, this);
434 void hscale_param_control::hscale_value_changed(GtkHScale *widget, gpointer value)
436 hscale_param_control *jhp = (hscale_param_control *)value;
437 jhp->get();
440 gchar *hscale_param_control::hscale_format_value(GtkScale *widget, double arg1, gpointer value)
442 hscale_param_control *jhp = (hscale_param_control *)value;
443 const parameter_properties &props = jhp->get_props();
444 float cvalue = props.from_01 (arg1);
446 // for testing
447 // return g_strdup_printf ("%s = %g", props.to_string (cvalue).c_str(), arg1);
448 return g_strdup (props.to_string (cvalue).c_str());
451 /******************************** Vertical Fader ********************************/
453 GtkWidget *vscale_param_control::create(plugin_gui *_gui, int _param_no)
455 gui = _gui;
456 param_no = _param_no;
457 widget = calf_fader_new(0, get_int("size", 2), 0, 1, get_props().get_increment());
458 g_signal_connect (GTK_OBJECT (widget), "value-changed", G_CALLBACK (vscale_value_changed), (gpointer)this);
459 g_signal_connect (GTK_OBJECT (widget), "button-press-event", G_CALLBACK (scale_button_press), (gpointer)this);
461 gtk_scale_set_draw_value(GTK_SCALE(widget), FALSE);
463 if(get_int("inverted", 0) > 0) {
464 gtk_range_set_inverted(GTK_RANGE(widget), TRUE);
466 int size = get_int("size", 2);
467 char *name = g_strdup_printf("Calf-VScale%i", size);
468 gtk_widget_set_size_request (widget, -1, size * 100);
469 gtk_widget_set_name(GTK_WIDGET(widget), name);
470 g_free(name);
471 return widget;
474 void vscale_param_control::init_xml(const char *element)
476 if (attribs.count("height"))
477 gtk_widget_set_size_request (widget, -1, get_int("height", 200));
480 void vscale_param_control::set()
482 _GUARD_CHANGE_
483 const parameter_properties &props = get_props();
484 gtk_range_set_value (GTK_RANGE (widget), props.to_01 (gui->plugin->get_param_value(param_no)));
485 // vscale_value_changed (GTK_HSCALE (widget), (gpointer)this);
488 void vscale_param_control::get()
490 const parameter_properties &props = get_props();
491 float cvalue = props.from_01 (gtk_range_get_value (GTK_RANGE (widget)));
492 gui->set_param_value(param_no, cvalue, this);
495 void vscale_param_control::vscale_value_changed(GtkHScale *widget, gpointer value)
497 vscale_param_control *jhp = (vscale_param_control *)value;
498 jhp->get();
501 /******************************** Label ********************************/
503 GtkWidget *label_param_control::create(plugin_gui *_gui, int _param_no)
505 gui = _gui, param_no = _param_no;
506 string text;
507 if (param_no != -1 && !attribs.count("text"))
508 text = get_props().name;
509 else
510 text = attribs["text"];
511 widget = gtk_label_new(text.c_str());
512 gtk_misc_set_alignment (GTK_MISC (widget), get_float("align-x", 0.5), get_float("align-y", 0.5));
513 gtk_widget_set_name(GTK_WIDGET(widget), "Calf-Label");
514 return widget;
517 /******************************** Value ********************************/
519 GtkWidget *value_param_control::create(plugin_gui *_gui, int _param_no)
521 gui = _gui;
522 param_no = _param_no;
524 widget = gtk_label_new ("");
525 if (param_no != -1)
527 const parameter_properties &props = get_props();
528 gtk_label_set_width_chars (GTK_LABEL (widget), props.get_char_count());
530 else
532 require_attribute("key");
533 require_int_attribute("width");
534 param_variable = attribs["key"];
535 gtk_label_set_width_chars (GTK_LABEL (widget), get_int("width"));
537 gtk_misc_set_alignment (GTK_MISC (widget), get_float("align-x", 0.5), get_float("align-y", 0.5));
538 gtk_widget_set_name(GTK_WIDGET(widget), "Calf-Value");
539 return widget;
542 void value_param_control::set()
544 if (param_no == -1)
545 return;
546 _GUARD_CHANGE_
548 const parameter_properties &props = get_props();
549 string value = props.to_string(gui->plugin->get_param_value(param_no));
551 if (value == old_value)
552 return;
553 old_value = value;
554 gtk_label_set_text (GTK_LABEL (widget), value.c_str());
557 void value_param_control::send_status(const char *key, const char *value)
559 if (key == param_variable)
561 gtk_label_set_text (GTK_LABEL (widget), value);
565 /******************************** VU Meter ********************************/
567 GtkWidget *vumeter_param_control::create(plugin_gui *_gui, int _param_no)
569 gui = _gui, param_no = _param_no;
570 // const parameter_properties &props = get_props();
571 widget = calf_vumeter_new ();
572 gtk_widget_set_name(GTK_WIDGET(widget), "calf-vumeter");
573 calf_vumeter_set_mode (CALF_VUMETER (widget), (CalfVUMeterMode)get_int("mode", 0));
574 CALF_VUMETER(widget)->vumeter_hold = get_float("hold", 0);
575 CALF_VUMETER(widget)->vumeter_falloff = get_float("falloff", 0.f);
576 CALF_VUMETER(widget)->vumeter_width = get_int("width", 80);
577 CALF_VUMETER(widget)->vumeter_height = get_int("height", 18);
578 CALF_VUMETER(widget)->vumeter_position = get_int("position", 0);
579 gtk_widget_set_name(GTK_WIDGET(widget), "Calf-VUMeter");
580 return widget;
583 void vumeter_param_control::set()
585 _GUARD_CHANGE_
586 // const parameter_properties &props = get_props();
587 calf_vumeter_set_value (CALF_VUMETER (widget), gui->plugin->get_param_value(param_no));
590 // LED
592 GtkWidget *led_param_control::create(plugin_gui *_gui, int _param_no)
594 gui = _gui, param_no = _param_no;
595 // const parameter_properties &props = get_props();
596 widget = calf_led_new ();
597 gtk_widget_set_name(GTK_WIDGET(widget), "calf-led");
598 CALF_LED(widget)->led_mode = get_int("mode", 0);
599 CALF_LED(widget)->size = get_int("size", 1);
600 gtk_widget_set_name(GTK_WIDGET(widget), "Calf-LED");
601 return widget;
604 void led_param_control::set()
606 _GUARD_CHANGE_
607 // const parameter_properties &props = get_props();
608 calf_led_set_value (CALF_LED (widget), gui->plugin->get_param_value(param_no));
611 // tube
613 GtkWidget *tube_param_control::create(plugin_gui *_gui, int _param_no)
615 gui = _gui, param_no = _param_no;
616 // const parameter_properties &props = get_props();
617 widget = calf_tube_new ();
618 gtk_widget_set_name(GTK_WIDGET(widget), "calf-tube");
619 CALF_TUBE(widget)->size = get_int("size", 2);
620 CALF_TUBE(widget)->direction = get_int("direction", 2);
621 gtk_widget_set_name(GTK_WIDGET(widget), "Calf-Tube");
622 return widget;
625 void tube_param_control::set()
627 _GUARD_CHANGE_
628 // const parameter_properties &props = get_props();
629 calf_tube_set_value (CALF_TUBE (widget), gui->plugin->get_param_value(param_no));
632 /******************************** Check Box ********************************/
634 GtkWidget *check_param_control::create(plugin_gui *_gui, int _param_no)
636 gui = _gui;
637 param_no = _param_no;
639 widget = gtk_check_button_new ();
640 g_signal_connect (GTK_OBJECT (widget), "toggled", G_CALLBACK (check_value_changed), (gpointer)this);
641 gtk_widget_set_name(GTK_WIDGET(widget), "Calf-Checkbox");
642 return widget;
645 void check_param_control::check_value_changed(GtkCheckButton *widget, gpointer value)
647 param_control *jhp = (param_control *)value;
648 jhp->get();
651 void check_param_control::get()
653 const parameter_properties &props = get_props();
654 gui->set_param_value(param_no, gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON(widget)) + props.min, this);
657 void check_param_control::set()
659 _GUARD_CHANGE_
660 const parameter_properties &props = get_props();
661 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (widget), (int)gui->plugin->get_param_value(param_no) - (int)props.min);
664 /******************************** Radio Button ********************************/
666 GtkWidget *radio_param_control::create(plugin_gui *_gui, int _param_no)
668 gui = _gui;
669 param_no = _param_no;
670 require_attribute("value");
671 value = -1;
672 string value_name = attribs["value"];
673 const parameter_properties &props = get_props();
674 if (props.choices && (value_name < "0" || value_name > "9"))
676 for (int i = 0; props.choices[i]; i++)
678 if (value_name == props.choices[i])
680 value = i + (int)props.min;
681 break;
685 if (value == -1)
686 value = get_int("value");
688 if (attribs.count("label"))
689 widget = gtk_radio_button_new_with_label (gui->get_radio_group(param_no), attribs["label"].c_str());
690 else
691 widget = gtk_radio_button_new_with_label (gui->get_radio_group(param_no), props.choices[value - (int)props.min]);
692 gtk_toggle_button_set_mode (GTK_TOGGLE_BUTTON (widget), FALSE);
694 gui->set_radio_group(param_no, gtk_radio_button_get_group (GTK_RADIO_BUTTON (widget)));
695 g_signal_connect (GTK_OBJECT (widget), "clicked", G_CALLBACK (radio_clicked), (gpointer)this);
696 gtk_widget_set_name(GTK_WIDGET(widget), "Calf-RadioButton");
697 return widget;
700 void radio_param_control::radio_clicked(GtkRadioButton *widget, gpointer value)
702 param_control *jhp = (param_control *)value;
703 jhp->get();
706 void radio_param_control::get()
708 // const parameter_properties &props = get_props();
709 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget)))
710 gui->set_param_value(param_no, value, this);
713 void radio_param_control::set()
715 _GUARD_CHANGE_
716 const parameter_properties &props = get_props();
717 float pv = gui->plugin->get_param_value(param_no);
718 if (fabs(value-pv) < 0.5)
719 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (widget), value == ((int)gui->plugin->get_param_value(param_no) - (int)props.min));
722 /******************************** Spin Button ********************************/
724 GtkWidget *spin_param_control::create(plugin_gui *_gui, int _param_no)
726 gui = _gui;
727 param_no = _param_no;
729 const parameter_properties &props = get_props();
730 if (props.step > 1)
731 widget = gtk_spin_button_new_with_range (props.min, props.max, (props.max - props.min) / (props.step - 1));
732 if (props.step > 0)
733 widget = gtk_spin_button_new_with_range (props.min, props.max, props.step);
734 else
735 widget = gtk_spin_button_new_with_range (props.min, props.max, 1);
736 gtk_spin_button_set_digits (GTK_SPIN_BUTTON(widget), get_int("digits", 0));
737 g_signal_connect (GTK_OBJECT (widget), "value-changed", G_CALLBACK (value_changed), (gpointer)this);
738 gtk_widget_set_name(GTK_WIDGET(widget), "Calf-SpinButton");
739 return widget;
742 void spin_param_control::value_changed(GtkSpinButton *widget, gpointer value)
744 param_control *jhp = (param_control *)value;
745 jhp->get();
748 void spin_param_control::get()
750 // const parameter_properties &props = get_props();
751 gui->set_param_value(param_no, gtk_spin_button_get_value_as_float (GTK_SPIN_BUTTON (widget)), this);
754 void spin_param_control::set()
756 _GUARD_CHANGE_
757 // const parameter_properties &props = get_props();
758 gtk_spin_button_set_value (GTK_SPIN_BUTTON (widget), gui->plugin->get_param_value(param_no));
761 /******************************** Button ********************************/
763 GtkWidget *button_param_control::create(plugin_gui *_gui, int _param_no)
765 gui = _gui;
766 param_no = _param_no;
767 widget = calf_button_new ((gchar*)get_props().name);
768 g_signal_connect (GTK_OBJECT (widget), "pressed", G_CALLBACK (button_clicked), (gpointer)this);
769 g_signal_connect (GTK_OBJECT (widget), "released", G_CALLBACK (button_clicked), (gpointer)this);
770 gtk_widget_set_name(GTK_WIDGET(widget), "Calf-Button");
771 return widget;
774 void button_param_control::button_clicked(GtkButton *widget, gpointer value)
776 param_control *jhp = (param_control *)value;
777 jhp->get();
780 void button_param_control::get()
782 const parameter_properties &props = get_props();
783 gui->set_param_value(param_no, gtk_widget_get_state(widget) == GTK_STATE_ACTIVE ? props.max : props.min, this);
786 void button_param_control::set()
788 _GUARD_CHANGE_
789 const parameter_properties &props = get_props();
790 if (gui->plugin->get_param_value(param_no) - props.min >= 0.5)
791 gtk_button_clicked (GTK_BUTTON (widget));
794 /******************************** Knob ********************************/
796 GtkWidget *knob_param_control::create(plugin_gui *_gui, int _param_no)
798 gui = _gui;
799 param_no = _param_no;
800 const parameter_properties &props = get_props();
802 //widget = calf_knob_new_with_range (props.to_01 (gui->plugin->get_param_value(param_no)), 0, 1, 0.01);
803 widget = calf_knob_new();
804 float increment = props.get_increment();
805 gtk_range_get_adjustment(GTK_RANGE(widget))->step_increment = increment;
806 CALF_KNOB(widget)->default_value = props.to_01(props.def_value);
807 CALF_KNOB(widget)->knob_type = get_int("type");
808 CALF_KNOB(widget)->knob_size = get_int("size", 2);
809 if(CALF_KNOB(widget)->knob_size > 5) {
810 CALF_KNOB(widget)->knob_size = 5;
811 } else if (CALF_KNOB(widget)->knob_size < 1) {
812 CALF_KNOB(widget)->knob_size = 1;
814 g_signal_connect(GTK_OBJECT(widget), "value-changed", G_CALLBACK(knob_value_changed), (gpointer)this);
815 gtk_widget_set_name(GTK_WIDGET(widget), "Calf-Knob");
816 return widget;
819 void knob_param_control::get()
821 const parameter_properties &props = get_props();
822 float value = props.from_01(gtk_range_get_value(GTK_RANGE(widget)));
823 gui->set_param_value(param_no, value, this);
826 void knob_param_control::set()
828 _GUARD_CHANGE_
829 const parameter_properties &props = get_props();
830 gtk_range_set_value(GTK_RANGE(widget), props.to_01 (gui->plugin->get_param_value(param_no)));
833 void knob_param_control::knob_value_changed(GtkWidget *widget, gpointer value)
835 param_control *jhp = (param_control *)value;
836 jhp->get();
839 /******************************** Toggle Button ********************************/
841 GtkWidget *toggle_param_control::create(plugin_gui *_gui, int _param_no)
843 gui = _gui;
844 param_no = _param_no;
845 widget = calf_toggle_new ();
847 CALF_TOGGLE(widget)->size = get_int("size", 2);
849 g_signal_connect (GTK_OBJECT (widget), "value-changed", G_CALLBACK (toggle_value_changed), (gpointer)this);
850 gtk_widget_set_name(GTK_WIDGET(widget), "Calf-ToggleButton");
851 return widget;
854 void toggle_param_control::get()
856 const parameter_properties &props = get_props();
857 float value = props.from_01(gtk_range_get_value(GTK_RANGE(widget)));
858 gui->set_param_value(param_no, value, this);
861 void toggle_param_control::set()
863 _GUARD_CHANGE_
864 const parameter_properties &props = get_props();
865 gtk_range_set_value(GTK_RANGE(widget), props.to_01 (gui->plugin->get_param_value(param_no)));
868 void toggle_param_control::toggle_value_changed(GtkWidget *widget, gpointer value)
870 param_control *jhp = (param_control *)value;
871 jhp->get();
874 /******************************** Tap Button ********************************/
876 GtkWidget *tap_button_param_control::create(plugin_gui *_gui, int _param_no)
878 gui = _gui;
879 param_no = _param_no;
880 last_time = 0;
881 init_time = 0;
882 avg_value = 0;
883 value = 0;
884 widget = calf_tap_button_new ();
885 //CALF_TAP(widget)->size = get_int("size", 2);
886 g_signal_connect (GTK_OBJECT (widget), "button-press-event", G_CALLBACK (tap_button_pressed), (gpointer)this);
887 g_signal_connect (GTK_OBJECT (widget), "released", G_CALLBACK (tap_button_released), (gpointer)this);
888 g_signal_connect (GTK_OBJECT (widget), "leave", G_CALLBACK (tap_button_released), (gpointer)this);
889 gtk_widget_set_name(GTK_WIDGET(widget), "Calf-TapButton");
890 return widget;
893 void tap_button_param_control::get()
895 gui->set_param_value(param_no, value, this);
898 void tap_button_param_control::set()
900 _GUARD_CHANGE_
901 if(last_time) {
902 timeval tv;
903 gettimeofday(&tv, 0);
904 unsigned long _now = tv.tv_sec * 1000;
905 if(_now > init_time + 2000) {
906 // user stopped tapping
907 avg_value = 0;
908 last_time = 0;
909 init_time = 0;
910 CALF_TAP_BUTTON(widget)->state = 0;
911 gtk_widget_queue_draw(widget);
916 gboolean tap_button_param_control::tap_button_pressed(GtkWidget *widget, GdkEventButton *event, gpointer value)
918 tap_button_param_control *ctl = (tap_button_param_control *)value;
919 CalfTapButton *tap = CALF_TAP_BUTTON(widget);
921 guint time = 0;
922 if (event->type == GDK_BUTTON_PRESS and event->button == 1)
924 timeval tv;
925 gettimeofday(&tv, 0);
926 ctl->init_time = tv.tv_sec * 1000;
927 time = event->time;
928 tap->state = 2;
930 if(ctl->last_time) {
931 if(ctl->avg_value)
932 ctl->avg_value = (ctl->avg_value * 3 + (time - ctl->last_time)) / 4.f;
933 else
934 ctl->avg_value = time - ctl->last_time;
935 ctl->value = 60.f / (float)(ctl->avg_value / 1000.f);
937 if (ctl->value > 30 and ctl->value < 300)
938 ctl->get();
940 ctl->last_time = time;
941 gtk_widget_queue_draw(widget);
943 return FALSE;
945 gboolean tap_button_param_control::tap_button_released(GtkWidget *widget, gpointer value)
947 tap_button_param_control *ctl = (tap_button_param_control *)value;
948 CalfTapButton *tap = CALF_TAP_BUTTON(widget);
949 tap->state = ctl->last_time ? 1 : 0;
950 gtk_widget_queue_draw(widget);
951 return FALSE;
954 /******************************** Keyboard ********************************/
956 GtkWidget *keyboard_param_control::create(plugin_gui *_gui, int _param_no)
958 gui = _gui;
959 param_no = _param_no;
960 // const parameter_properties &props = get_props();
962 widget = calf_keyboard_new();
963 kb = CALF_KEYBOARD(widget);
964 kb->nkeys = get_int("octaves", 4) * 7 + 1;
965 kb->sink = new CalfKeyboard::EventAdapter;
966 gtk_widget_set_name(GTK_WIDGET(widget), "Calf-Keyboard");
967 return widget;
970 /******************************** Curve ********************************/
972 struct curve_param_control_callback: public CalfCurve::EventAdapter
974 curve_param_control *ctl;
976 curve_param_control_callback(curve_param_control *_ctl)
977 : ctl(_ctl) {}
979 virtual void curve_changed(CalfCurve *src, const CalfCurve::point_vector &data) {
980 stringstream ss;
981 ss << data.size() << endl;
982 for (size_t i = 0; i < data.size(); i++)
983 ss << data[i].first << " " << data[i].second << endl;
984 ctl->gui->plugin->configure(ctl->attribs["key"].c_str(), ss.str().c_str());
986 virtual void clip(CalfCurve *src, int pt, float &x, float &y, bool &hide)
988 // int gridpt = floor(x * 71 * 2);
989 // clip to the middle of the nearest white key
990 x = (floor(x * 71) + 0.5)/ 71.0;
994 GtkWidget *curve_param_control::create(plugin_gui *_gui, int _param_no)
996 gui = _gui;
997 param_no = _param_no;
998 require_attribute("key");
1000 widget = calf_curve_new(get_int("maxpoints", -1));
1001 curve = CALF_CURVE(widget);
1002 curve->sink = new curve_param_control_callback(this);
1003 // gtk_curve_set_curve_type(curve, GTK_CURVE_TYPE_LINEAR);
1004 gtk_widget_set_name(GTK_WIDGET(widget), "Calf-Curve");
1005 return widget;
1008 void curve_param_control::send_configure(const char *key, const char *value)
1010 // cout << "send conf " << key << endl;
1011 if (attribs["key"] == key)
1013 stringstream ss(value);
1014 CalfCurve::point_vector pts;
1015 if (*value)
1017 unsigned int npoints = 0;
1018 ss >> npoints;
1019 unsigned int i;
1020 float x = 0, y = 0;
1021 for (i = 0; i < npoints && i < curve->point_limit; i++)
1023 ss >> x >> y;
1024 pts.push_back(CalfCurve::point(x, y));
1026 calf_curve_set_points(widget, pts);
1031 /******************************** Entry ********************************/
1033 GtkWidget *entry_param_control::create(plugin_gui *_gui, int _param_no)
1035 gui = _gui;
1036 param_no = _param_no;
1037 require_attribute("key");
1039 widget = gtk_entry_new();
1040 entry = GTK_ENTRY(widget);
1041 g_signal_connect(GTK_OBJECT(widget), "changed", G_CALLBACK(entry_value_changed), (gpointer)this);
1042 gtk_editable_set_editable(GTK_EDITABLE(entry), get_int("editable", 1));
1043 gtk_widget_set_name(GTK_WIDGET(widget), "Calf-Entry");
1044 return widget;
1047 void entry_param_control::send_configure(const char *key, const char *value)
1049 // cout << "send conf " << key << endl;
1050 if (attribs["key"] == key)
1052 gtk_entry_set_text(entry, value);
1056 void entry_param_control::entry_value_changed(GtkWidget *widget, gpointer value)
1058 entry_param_control *ctl = (entry_param_control *)value;
1059 ctl->gui->plugin->configure(ctl->attribs["key"].c_str(), gtk_entry_get_text(ctl->entry));
1062 /******************************** File Chooser ********************************/
1064 GtkWidget *filechooser_param_control::create(plugin_gui *_gui, int _param_no)
1066 gui = _gui;
1067 param_no = _param_no;
1068 require_attribute("key");
1069 require_attribute("title");
1071 widget = gtk_file_chooser_button_new(attribs["title"].c_str(), GTK_FILE_CHOOSER_ACTION_OPEN);
1072 filechooser = GTK_FILE_CHOOSER_BUTTON(widget);
1073 // XXXKF this is GTK+ 2.12 function, does any replacement exist?
1074 // MS: switched from g_signal_connect to g_signal_connect for better emission of signals
1075 g_signal_connect(GTK_OBJECT(widget), "file-set", G_CALLBACK(filechooser_value_changed), (gpointer)this);
1076 if (attribs.count("width"))
1077 gtk_widget_set_size_request (widget, get_int("width", 200), -1);
1078 if (attribs.count("width_chars"))
1079 gtk_file_chooser_button_set_width_chars (filechooser, get_int("width_chars"));
1080 gtk_widget_set_name(GTK_WIDGET(widget), "Calf-FileButton");
1081 return widget;
1084 void filechooser_param_control::send_configure(const char *key, const char *value)
1086 // cout << "send conf " << key << endl;
1087 if (attribs["key"] == key)
1089 gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(filechooser), value);
1093 void filechooser_param_control::filechooser_value_changed(GtkWidget *widget, gpointer value)
1095 filechooser_param_control *ctl = (filechooser_param_control *)value;
1096 const char *filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(ctl->filechooser));
1097 if (filename)
1098 ctl->gui->plugin->configure(ctl->attribs["key"].c_str(), filename);
1101 /******************************** Line Graph ********************************/
1103 void line_graph_param_control::on_idle()
1105 if (get_int("refresh", 0))
1106 set();
1109 static float to_x_pos(float freq)
1111 return log(freq / 20.0) / log(1000);
1114 static float from_x_pos(float pos)
1116 float a = pos * 3.0;
1117 float b = powf(10.0, a);
1118 float c = b * 20.0;
1119 return c;
1122 static float to_y_pos(CalfLineGraph *lg, float gain)
1124 //log(gain) * (1.0 / log(32));
1125 return 0.5 - dB_grid(gain, 128 * lg->zoom, lg->offset) / 2.0;
1128 static float from_y_pos(CalfLineGraph *lg, float pos)
1130 float gain = powf(128.0 * lg->zoom, (0.5 - pos) * 2.0 - lg->offset);
1131 return gain;
1134 GtkWidget *line_graph_param_control::create(plugin_gui *a_gui, int a_param_no)
1136 gui = a_gui;
1137 param_no = a_param_no;
1138 //last_generation = -1;
1140 widget = calf_line_graph_new ();
1142 gtk_widget_set_name(GTK_WIDGET(widget), "calf-graph");
1144 CalfLineGraph *clg = CALF_LINE_GRAPH(widget);
1145 widget->requisition.width = get_int("width", 40);
1146 widget->requisition.height = get_int("height", 40);
1148 calf_line_graph_set_square(clg, get_int("square", 0));
1150 clg->source = gui->plugin->get_line_graph_iface();
1151 clg->source_id = param_no;
1152 clg->fade = get_float("fade", 1.0);
1153 clg->mode = get_int("mode", 0);
1154 clg->use_crosshairs = get_int("crosshairs", 0);
1155 clg->freqhandles = get_int("freqhandles", 0);
1156 clg->enforce_handle_order = get_int("enforce-handle-order", 0);
1157 clg->min_handle_distance = get_float("min-handle-distance", 0.01);
1159 const string &zoom_name = attribs["zoom"];
1160 if (zoom_name != "")
1161 clg->param_zoom = gui->get_param_no_by_name(zoom_name);
1163 const string &offset_name = attribs["offset"];
1164 if (offset_name != "")
1165 clg->param_offset = gui->get_param_no_by_name(offset_name);
1167 if (clg->freqhandles > 0)
1169 for(int i = 0; i < clg->freqhandles; i++)
1171 FreqHandle *handle = &clg->freq_handles[i];
1173 stringstream handle_x_attribute;
1174 handle_x_attribute << "handle" << i + 1 << "-x";
1175 const string &param_x_name = attribs[handle_x_attribute.str()];
1176 if(param_x_name == "")
1177 break;
1179 int param_x_no = gui->get_param_no_by_name(param_x_name);
1180 const parameter_properties &handle_x_props = *gui->plugin->get_metadata_iface()->get_param_props(param_x_no);
1181 handle->dimensions = 1;
1182 handle->param_x_no = param_x_no;
1183 handle->value_x = to_x_pos(gui->plugin->get_param_value(param_x_no));
1184 handle->default_value_x = to_x_pos(handle_x_props.def_value);
1186 stringstream handle_y_attribute;
1187 handle_y_attribute << "handle" << i + 1 << "-y";
1188 const string &param_y_name = attribs[handle_y_attribute.str()];
1189 if(param_y_name != "") {
1190 int param_y_no = gui->get_param_no_by_name(param_y_name);
1191 const parameter_properties &handle_y_props = *gui->plugin->get_metadata_iface()->get_param_props(param_y_no);
1192 handle->dimensions = 2;
1193 handle->param_y_no = param_y_no;
1194 handle->value_y = to_y_pos(clg, gui->plugin->get_param_value(param_y_no));
1195 handle->default_value_y = to_y_pos(clg, handle_y_props.def_value);
1196 } else {
1197 handle->param_y_no = -1;
1200 stringstream handle_z_attribute;
1201 handle_z_attribute << "handle" << i + 1 << "-z";
1202 const string &param_z_name = attribs[handle_z_attribute.str()];
1203 if(param_z_name != "") {
1204 int param_z_no = gui->get_param_no_by_name(param_z_name);
1205 const parameter_properties &handle_z_props = *gui->plugin->get_metadata_iface()->get_param_props(param_z_no);
1206 handle->dimensions = 3;
1207 handle->param_z_no = param_z_no;
1208 handle->value_z = handle_z_props.to_01(gui->plugin->get_param_value(param_z_no));
1209 handle->default_value_z = handle_z_props.to_01(handle_z_props.def_value);
1210 } else {
1211 handle->param_z_no = -1;
1214 stringstream label_attribute;
1215 label_attribute << "label" << i + 1;
1216 string label = attribs[label_attribute.str()];
1217 if (!label.empty()) {
1218 handle->label = strdup(label.c_str());
1221 stringstream active_attribute;
1222 active_attribute << "active" << i + 1;
1223 const string &active_name = attribs[active_attribute.str()];
1224 if (active_name != "") {
1225 handle->param_active_no = gui->get_param_no_by_name(active_name);
1226 } else {
1227 handle->param_active_no = -1;
1230 stringstream style_attribute;
1231 style_attribute << "style" << i + 1;
1232 const string style = style_attribute.str();
1233 clg->freq_handles[i].style = get_int(style.c_str(), 0);
1234 if(clg->freq_handles[i].style == 1 or clg->freq_handles[i].style == 4) {
1235 clg->freq_handles[i].dimensions = 1;
1237 handle->data = (gpointer) this;
1239 g_signal_connect(G_OBJECT(widget), "freqhandle-changed", G_CALLBACK(freqhandle_value_changed), this);
1242 gtk_widget_set_name(GTK_WIDGET(widget), "Calf-LineGraph");
1243 return widget;
1246 void line_graph_param_control::get()
1248 GtkWidget *tw = gtk_widget_get_toplevel(widget);
1249 CalfLineGraph *clg = CALF_LINE_GRAPH(widget);
1251 if (tw && GTK_WIDGET_TOPLEVEL(tw) && widget->window)
1253 int ws = gdk_window_get_state(widget->window);
1254 if (ws & (GDK_WINDOW_STATE_WITHDRAWN | GDK_WINDOW_STATE_ICONIFIED))
1255 return;
1257 if(clg->handle_grabbed >= 0) {
1258 FreqHandle *handle = &clg->freq_handles[clg->handle_grabbed];
1259 if(handle->dimensions >= 2) {
1260 float value_y = from_y_pos(clg, handle->value_y);
1261 gui->set_param_value(handle->param_y_no, value_y, this);
1264 float value_x = from_x_pos(handle->value_x);
1265 gui->set_param_value(handle->param_x_no, value_x, this);
1266 } else if(clg->handle_hovered >= 0) {
1267 FreqHandle *handle = &clg->freq_handles[clg->handle_hovered];
1269 if(handle->dimensions == 3) {
1270 const parameter_properties &handle_z_props = *gui->plugin->get_metadata_iface()->get_param_props(handle->param_z_no);
1271 float value_z = handle_z_props.from_01(handle->value_z);
1272 gui->set_param_value(handle->param_z_no, value_z, this);
1278 void line_graph_param_control::set()
1280 _GUARD_CHANGE_
1281 GtkWidget *tw = gtk_widget_get_toplevel(widget);
1282 CalfLineGraph *clg = CALF_LINE_GRAPH(widget);
1283 if (tw && GTK_WIDGET_TOPLEVEL(tw) && widget->window)
1285 bool force = false;
1286 int ws = gdk_window_get_state(widget->window);
1287 if (ws & (GDK_WINDOW_STATE_WITHDRAWN | GDK_WINDOW_STATE_ICONIFIED))
1288 return;
1290 if (clg->param_zoom >= 0) {
1291 float _z = gui->plugin->get_param_value(clg->param_zoom);
1292 if (_z != clg->zoom) {
1293 force = true;
1294 clg->zoom = _z;
1295 clg->force_redraw = true;
1299 if (clg->param_offset >= 0) {
1300 float _z = gui->plugin->get_param_value(clg->param_offset);
1301 if (_z != clg->offset) {
1302 force = true;
1303 clg->offset = _z;
1304 clg->force_redraw = true;
1308 for (int i = 0; i < clg->freqhandles; i++) {
1309 FreqHandle *handle = &clg->freq_handles[i];
1311 if (handle->param_x_no >= 0)
1313 float value_x = gui->plugin->get_param_value(handle->param_x_no);
1314 handle->value_x = to_x_pos(value_x);
1315 if (dsp::_sanitize(handle->value_x - handle->last_value_x)) {
1316 clg->handle_redraw = 1;
1318 handle->last_value_x = handle->value_x;
1319 if(handle->dimensions >= 2 && handle->param_y_no >= 0) {
1320 float value_y = gui->plugin->get_param_value(handle->param_y_no);
1321 handle->value_y = to_y_pos(clg, value_y);
1322 if (dsp::_sanitize(handle->value_y - handle->last_value_y)) {
1323 clg->handle_redraw = 1;
1325 handle->last_value_y = handle->value_y;
1329 if(handle->dimensions == 3 && handle->param_z_no >= 0) {
1330 const parameter_properties &handle_z_props = *gui->plugin->get_metadata_iface()->get_param_props(handle->param_z_no);
1331 float value_z = gui->plugin->get_param_value(handle->param_z_no);
1332 handle->value_z = handle_z_props.to_01(value_z);
1333 if (dsp::_sanitize(handle->value_z - handle->last_value_z)) {
1334 clg->handle_redraw = 1;
1336 handle->last_value_z = handle->value_z;
1338 bool _a = handle->active;
1339 if(handle->param_active_no >= 0) {
1340 handle->active = bool(gui->plugin->get_param_value(handle->param_active_no));
1341 } else {
1342 handle->active = true;
1344 if (handle->active != _a) {
1345 force = true;
1346 clg->handle_redraw = true;
1349 calf_line_graph_expose_request(widget, force);
1353 void line_graph_param_control::freqhandle_value_changed(GtkWidget *widget, gpointer p)
1355 assert(p!=NULL);
1356 FreqHandle *handle = (FreqHandle *)p;
1357 param_control *jhp = (param_control *)handle->data;
1358 jhp->get();
1362 line_graph_param_control::~line_graph_param_control()
1367 /******************************** Phase Graph ********************************/
1369 void phase_graph_param_control::on_idle()
1371 if (get_int("refresh", 0))
1372 set();
1375 GtkWidget *phase_graph_param_control::create(plugin_gui *_gui, int _param_no)
1377 gui = _gui;
1378 param_no = _param_no;
1379 widget = calf_phase_graph_new ();
1380 gtk_widget_set_name(GTK_WIDGET(widget), "calf-phase");
1381 CalfPhaseGraph *clg = CALF_PHASE_GRAPH(widget);
1382 widget->requisition.width = get_int("size", 40);
1383 widget->requisition.height = get_int("size", 40);
1384 clg->source = gui->plugin->get_phase_graph_iface();
1385 clg->source_id = param_no;
1386 gtk_widget_set_name(GTK_WIDGET(widget), "Calf-PhaseGraph");
1387 return widget;
1390 void phase_graph_param_control::set()
1392 _GUARD_CHANGE_
1393 GtkWidget *tw = gtk_widget_get_toplevel(widget);
1394 if (tw && GTK_WIDGET_TOPLEVEL(tw) && widget->window) {
1395 gtk_widget_queue_draw(widget);
1399 phase_graph_param_control::~phase_graph_param_control()
1403 /******************************** List View ********************************/
1405 GtkWidget *listview_param_control::create(plugin_gui *_gui, int _param_no)
1407 gui = _gui;
1408 param_no = _param_no;
1410 string key = attribs["key"];
1411 tmif = gui->plugin->get_metadata_iface()->get_table_metadata_iface(key.c_str());
1412 if (!tmif)
1414 g_error("Missing table_metadata_iface for variable '%s'", key.c_str());
1415 return NULL;
1417 positions.clear();
1418 const table_column_info *tci = tmif->get_table_columns();
1419 assert(tci);
1420 cols = 0;
1421 while (tci[cols].name != NULL)
1422 cols++;
1424 GType *p = new GType[cols];
1425 for (int i = 0; i < cols; i++)
1426 p[i] = G_TYPE_STRING;
1427 lstore = gtk_list_store_newv(cols, p);
1428 if (tmif->get_table_rows() != 0)
1429 set_rows(tmif->get_table_rows());
1430 widget = gtk_tree_view_new_with_model(GTK_TREE_MODEL(lstore));
1431 delete []p;
1432 tree = GTK_TREE_VIEW (widget);
1433 g_object_set (G_OBJECT (tree), "enable-search", FALSE, "rules-hint", TRUE, "enable-grid-lines", TRUE, NULL);
1435 for (int i = 0; i < cols; i++)
1437 GtkCellRenderer *cr = NULL;
1439 if (tci[i].type == TCT_ENUM) {
1440 cr = gtk_cell_renderer_combo_new ();
1441 GtkListStore *cls = gtk_list_store_new(2, G_TYPE_INT, G_TYPE_STRING);
1442 for (int j = 0; tci[i].values[j]; j++)
1443 gtk_list_store_insert_with_values(cls, NULL, j, 0, j, 1, tci[i].values[j], -1);
1444 g_object_set(cr, "model", cls, "editable", TRUE, "has-entry", FALSE, "text-column", 1, "mode", GTK_CELL_RENDERER_MODE_EDITABLE, NULL);
1446 else {
1447 bool editable = tci[i].type != TCT_LABEL;
1448 cr = gtk_cell_renderer_text_new ();
1449 if (editable)
1450 g_object_set(cr, "editable", TRUE, "mode", GTK_CELL_RENDERER_MODE_EDITABLE, NULL);
1452 g_object_set_data (G_OBJECT(cr), "column", (void *)&tci[i]);
1453 g_signal_connect (GTK_OBJECT (cr), "edited", G_CALLBACK (on_edited), (gpointer)this);
1454 g_signal_connect (GTK_OBJECT (cr), "editing-canceled", G_CALLBACK (on_editing_canceled), (gpointer)this);
1455 gtk_tree_view_insert_column_with_attributes(tree, i, tci[i].name, cr, "text", i, NULL);
1457 gtk_tree_view_set_headers_visible(tree, TRUE);
1458 gtk_widget_set_name(GTK_WIDGET(widget), "Calf-ListView");
1459 return widget;
1462 void listview_param_control::set_rows(unsigned int needed_rows)
1464 while(positions.size() < needed_rows)
1466 GtkTreeIter iter;
1467 gtk_list_store_insert(lstore, &iter, positions.size());
1468 for (int j = 0; j < cols; j++)
1470 gtk_list_store_set(lstore, &iter, j, "", -1);
1472 positions.push_back(iter);
1476 void listview_param_control::send_configure(const char *key, const char *value)
1478 string orig_key = attribs["key"] + ":";
1479 bool is_rows = false;
1480 int row = -1, col = -1;
1481 if (parse_table_key(key, orig_key.c_str(), is_rows, row, col))
1483 string suffix = string(key + orig_key.length());
1484 if (is_rows && tmif->get_table_rows() == 0)
1486 int rows = atoi(value);
1487 set_rows(rows);
1488 return;
1490 else
1491 if (row != -1 && col != -1)
1493 int max_rows = tmif->get_table_rows();
1494 if (col < 0 || col >= cols)
1496 g_warning("Invalid column %d in key %s", col, key);
1497 return;
1499 if (max_rows && (row < 0 || row >= max_rows))
1501 g_warning("Invalid row %d in key %s, this is a fixed table with row count = %d", row, key, max_rows);
1502 return;
1505 if (row >= (int)positions.size())
1506 set_rows(row + 1);
1508 gtk_list_store_set(lstore, &positions[row], col, value, -1);
1509 return;
1514 void listview_param_control::on_edited(GtkCellRenderer *renderer, gchar *path, gchar *new_text, listview_param_control *pThis)
1516 const table_column_info *tci = pThis->tmif->get_table_columns();
1517 int column = ((table_column_info *)g_object_get_data(G_OBJECT(renderer), "column")) - tci;
1518 string key = pThis->attribs["key"] + ":" + i2s(atoi(path)) + "," + i2s(column);
1519 string error;
1520 const char *error_or_null = pThis->gui->plugin->configure(key.c_str(), new_text);
1521 if (error_or_null)
1522 error = error_or_null;
1524 if (error.empty()) {
1525 pThis->send_configure(key.c_str(), new_text);
1526 gtk_widget_grab_focus(pThis->widget);
1527 GtkTreePath *gpath = gtk_tree_path_new_from_string (path);
1528 gtk_tree_view_set_cursor_on_cell (GTK_TREE_VIEW (pThis->widget), gpath, NULL, NULL, FALSE);
1529 gtk_tree_path_free (gpath);
1531 else
1533 GtkWidget *dialog = gtk_message_dialog_new(pThis->gui->window->toplevel, GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK,
1534 "%s", error.c_str());
1535 gtk_dialog_run(GTK_DIALOG(dialog));
1536 gtk_widget_destroy(dialog);
1537 gtk_widget_grab_focus(pThis->widget);
1541 void listview_param_control::on_editing_canceled(GtkCellRenderer *renderer, listview_param_control *pThis)
1543 gtk_widget_grab_focus(pThis->widget);
1546 /******************************** GtkNotebook control ********************************/
1548 GtkWidget *notebook_param_control::create(plugin_gui *_gui, int _param_no)
1550 gui = _gui;
1551 param_no = _param_no;
1552 //const parameter_properties &props = get_props();
1553 page = gui->plugin->get_param_value(param_no);
1554 GtkWidget *nb = calf_notebook_new();
1555 widget = GTK_WIDGET(nb);
1556 container = GTK_CONTAINER(nb);
1557 gtk_widget_set_name(GTK_WIDGET(nb), "Calf-Notebook");
1558 gtk_notebook_set_current_page(GTK_NOTEBOOK(widget), page);
1559 return nb;
1561 void notebook_param_control::created() {
1562 g_signal_connect (GTK_OBJECT (widget), "switch-page", G_CALLBACK (notebook_page_changed), (gpointer)this);
1563 set();
1565 void notebook_param_control::get()
1567 if (param_no >= 0)
1568 gui->set_param_value(param_no, page, this);
1570 void notebook_param_control::set()
1572 if (param_no < 0)
1573 return;
1574 _GUARD_CHANGE_
1575 page = (gint)gui->plugin->get_param_value(param_no);
1576 gtk_notebook_set_current_page(GTK_NOTEBOOK(widget), page);
1578 void notebook_param_control::add(GtkWidget *w, control_base *base)
1580 gtk_notebook_append_page(GTK_NOTEBOOK(widget), w, gtk_label_new_with_mnemonic(base->attribs["page"].c_str()));
1582 void notebook_param_control::notebook_page_changed(GtkWidget *widget, GtkWidget *page, guint id, gpointer user)
1584 notebook_param_control *jhp = (notebook_param_control *)user;
1585 jhp->page = (int)id;
1586 jhp->get();
1589 /******************************** GtkTable container ********************************/
1591 GtkWidget *table_container::create(plugin_gui *_gui, const char *element, xml_attribute_map &attributes)
1593 require_int_attribute("rows");
1594 require_int_attribute("cols");
1595 int homog = get_int("homogeneous", 0);
1596 int sx = get_int("spacing-x", 2);
1597 int sy = get_int("spacing-y", 2);
1598 GtkWidget *table = gtk_table_new(get_int("rows", 1), get_int("cols", 1), false);
1599 if(homog > 0) {
1600 gtk_table_set_homogeneous(GTK_TABLE(table), TRUE);
1602 gtk_table_set_col_spacings(GTK_TABLE(table), sx);
1603 gtk_table_set_row_spacings(GTK_TABLE(table), sy);
1604 container = GTK_CONTAINER(table);
1605 gtk_widget_set_name(GTK_WIDGET(table), "Calf-Table");
1606 return table;
1609 void table_container::add(GtkWidget *widget, control_base *base)
1611 base->require_int_attribute("attach-x");
1612 base->require_int_attribute("attach-y");
1613 int x = base->get_int("attach-x"), y = base->get_int("attach-y");
1614 int w = base->get_int("attach-w", 1), h = base->get_int("attach-h", 1);
1615 int shrinkx = base->get_int("shrink-x", 0);
1616 int shrinky = base->get_int("shrink-y", 0);
1617 int fillx = (base->get_int("fill-x", !shrinkx) ? GTK_FILL : 0) | (base->get_int("expand-x", !shrinkx) ? GTK_EXPAND : 0) | (shrinkx ? GTK_SHRINK : 0);
1618 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);
1619 int padx = base->get_int("pad-x", 2);
1620 int pady = base->get_int("pad-y", 2);
1621 gtk_table_attach(GTK_TABLE(container), widget, x, x + w, y, y + h, (GtkAttachOptions)fillx, (GtkAttachOptions)filly, padx, pady);
1624 /******************************** alignment container ********************************/
1626 GtkWidget *alignment_container::create(plugin_gui *_gui, const char *element, xml_attribute_map &attributes)
1628 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));
1629 container = GTK_CONTAINER(align);
1630 gtk_widget_set_name(GTK_WIDGET(align), "Calf-Align");
1631 return align;
1634 /******************************** GtkFrame container ********************************/
1636 GtkWidget *frame_container::create(plugin_gui *_gui, const char *element, xml_attribute_map &attributes)
1638 GtkWidget *widget = calf_frame_new(attribs["label"].c_str());
1639 container = GTK_CONTAINER(widget);
1640 gtk_widget_set_name(GTK_WIDGET(widget), "Calf-Frame");
1641 return widget;
1644 /******************************** GtkBox type of containers ********************************/
1646 void box_container::add(GtkWidget *w, control_base *base)
1648 gtk_container_add_with_properties(container, w, "expand", get_int("expand", 1), "fill", get_int("fill", 1), NULL);
1651 /******************************** GtkHBox container ********************************/
1653 GtkWidget *hbox_container::create(plugin_gui *_gui, const char *element, xml_attribute_map &attributes)
1655 GtkWidget *hbox = gtk_hbox_new(get_int("homogeneous") >= 1, get_int("spacing", 2));
1656 container = GTK_CONTAINER(hbox);
1657 gtk_widget_set_name(GTK_WIDGET(hbox), "Calf-HBox");
1658 return hbox;
1661 /******************************** GtkVBox container ********************************/
1663 GtkWidget *vbox_container::create(plugin_gui *_gui, const char *element, xml_attribute_map &attributes)
1665 GtkWidget *vbox = gtk_vbox_new(get_int("homogeneous") >= 1, get_int("spacing", 2));
1666 container = GTK_CONTAINER(vbox);
1667 gtk_widget_set_name(GTK_WIDGET(vbox), "Calf-VBox");
1668 return vbox;
1671 /******************************** GtkNotebook container ********************************/
1673 GtkWidget *scrolled_container::create(plugin_gui *_gui, const char *element, xml_attribute_map &attributes)
1675 GtkAdjustment *horiz = NULL, *vert = NULL;
1676 int width = get_int("width", 0), height = get_int("height", 0);
1677 if (width)
1678 horiz = GTK_ADJUSTMENT(gtk_adjustment_new(get_int("x", 0), 0, width, get_int("step-x", 1), get_int("page-x", width / 10), 100));
1679 if (height)
1680 vert = GTK_ADJUSTMENT(gtk_adjustment_new(get_int("y", 0), 0, width, get_int("step-y", 1), get_int("page-y", height / 10), 10));
1681 GtkWidget *sw = gtk_scrolled_window_new(horiz, vert);
1682 gtk_widget_set_size_request(sw, get_int("req-x", -1), get_int("req-y", -1));
1683 container = GTK_CONTAINER(sw);
1684 gtk_widget_set_name(GTK_WIDGET(sw), "Calf-ScrolledWindow");
1685 return sw;
1688 void scrolled_container::add(GtkWidget *w, control_base *base)
1690 gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(container), w);