Ring Modulator: Initial rudimentary implementation
[calf.git] / src / gui_controls.cpp
bloba6d3d0b759e889aebe3b1594635088b251c73c20
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::hook_params()
124 if (param_no != -1) {
125 gui->add_param_ctl(param_no, this);
127 gui->params.push_back(this);
130 param_control::~param_control()
132 //if (GTK_IS_WIDGET(widget))
133 // gtk_widget_destroy(widget);
136 void param_control::add_context_menu_handler()
138 if (widget)
140 g_signal_connect(GTK_OBJECT(widget), "button-press-event", (GCallback)on_button_press_event, this);
144 gboolean param_control::on_button_press_event(GtkWidget *widget, GdkEventButton *event, void *user_data)
146 param_control *self = (param_control *)user_data;
147 const parameter_properties &props = self->get_props();
148 if (event->button == 3 && !(props.flags & PF_PROP_OUTPUT))
150 self->do_popup_menu();
151 return TRUE;
153 else if (event->button == 2)
155 self->create_value_entry(widget, event->x_root, event->y_root);
156 return TRUE;
158 return FALSE;
161 void param_control::do_popup_menu()
163 if (gui)
164 gui->on_control_popup(this, param_no);
167 void param_control::destroy_value_entry ()
169 // remove the window containing the entry
170 gtk_widget_destroy(GTK_WIDGET(entrywin));
171 has_entry = false;
173 gboolean param_control::value_entry_unfocus(GtkWidget *widget, GdkEventFocus *event, void *user_data)
175 // destroy window if it looses focus
176 param_control *self = (param_control *)user_data;
177 self->destroy_value_entry();
178 return TRUE;
180 gboolean param_control::value_entry_action(GtkEntry *widget, GdkEvent *event, void *user_data)
182 // called when a key was hit, sorts out and treats RETURN and ESC
183 param_control *self = (param_control *)user_data;
184 const parameter_properties &props = self->get_props();
185 GdkEventKey *key = (GdkEventKey*)event;
186 if(key->keyval == GDK_Escape)
187 self->destroy_value_entry();
188 else if (key->keyval == GDK_Return) {
189 float val = props.string_to_value(gtk_entry_get_text(widget));
190 self->gui->plugin->set_param_value(self->param_no, val);
191 self->set();
192 self->destroy_value_entry();
194 return FALSE;
196 void param_control::create_value_entry(GtkWidget *widget, int x, int y)
198 if (has_entry) {
199 // kill an existing entry window on re-trigger
200 destroy_value_entry();
201 return;
204 if (param_no < 0)
205 return;
207 const parameter_properties &props = get_props();
208 float value = gui->plugin->get_param_value(param_no);
210 // no chance for a menu, so we have to do everything by hand
211 entrywin = gtk_window_new(GTK_WINDOW_TOPLEVEL);
212 gtk_widget_set_name(GTK_WIDGET(entrywin), "Calf-Value-Entry");
213 gtk_window_set_title (GTK_WINDOW(entrywin), "Calf Value Entry");
214 gtk_window_set_resizable (GTK_WINDOW(entrywin), FALSE);
215 gtk_window_set_decorated (GTK_WINDOW(entrywin), FALSE);
216 gtk_window_set_skip_taskbar_hint (GTK_WINDOW(entrywin), TRUE);
217 gtk_window_set_skip_pager_hint (GTK_WINDOW(entrywin), TRUE);
218 gtk_window_set_transient_for (GTK_WINDOW(entrywin), GTK_WINDOW (gui->window->toplevel));
219 gtk_window_set_gravity(GTK_WINDOW(entrywin), GDK_GRAVITY_CENTER);
220 gtk_widget_set_events (GTK_WIDGET(entrywin), GDK_FOCUS_CHANGE_MASK);
221 g_signal_connect (G_OBJECT(entrywin), "focus-out-event", G_CALLBACK (value_entry_unfocus), this);
223 // create the text entry
224 GtkWidget *entry = gtk_entry_new();
225 gtk_widget_set_name(GTK_WIDGET(entry), "Calf-Entry");
226 gtk_entry_set_width_chars(GTK_ENTRY(entry), props.get_char_count());
227 gtk_entry_set_text(GTK_ENTRY(entry), props.to_string(value).c_str());
228 gtk_widget_add_events (entry, GDK_KEY_PRESS_MASK);
229 g_signal_connect (entry, "key-press-event", (GCallback)value_entry_action, this);
231 // stitch together and show
232 gtk_container_add(GTK_CONTAINER (entrywin), entry);
233 gtk_widget_show_all(entrywin);
234 gtk_window_move(GTK_WINDOW (entrywin), x, y);
236 has_entry = true;
240 /******************************** Combo Box ********************************/
242 GtkWidget *combo_box_param_control::create(plugin_gui *_gui, int _param_no)
244 gui = _gui;
245 param_no = _param_no;
246 lstore = gtk_list_store_new(2, G_TYPE_STRING, G_TYPE_STRING); // value, key
248 const parameter_properties &props = get_props();
249 widget = calf_combobox_new ();
250 if (param_no != -1 && props.choices)
252 for (int j = (int)props.min; j <= (int)props.max; j++)
253 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);
255 gtk_combo_box_set_model (GTK_COMBO_BOX(widget), GTK_TREE_MODEL(lstore));
256 g_signal_connect (GTK_OBJECT (widget), "changed", G_CALLBACK (combo_value_changed), (gpointer)this);
257 gtk_widget_set_name(GTK_WIDGET(widget), "Calf-Combobox");
258 return widget;
261 void combo_box_param_control::set()
263 _GUARD_CHANGE_
264 if (param_no != -1)
266 const parameter_properties &props = get_props();
267 gtk_combo_box_set_active (GTK_COMBO_BOX (widget), (int)gui->plugin->get_param_value(param_no) - (int)props.min);
271 void combo_box_param_control::get()
273 if (param_no != -1)
275 const parameter_properties &props = get_props();
276 gui->set_param_value(param_no, gtk_combo_box_get_active (GTK_COMBO_BOX(widget)) + props.min, this);
280 void combo_box_param_control::combo_value_changed(GtkComboBox *widget, gpointer value)
282 combo_box_param_control *jhp = (combo_box_param_control *)value;
283 if (jhp->attribs.count("setter-key"))
285 GtkTreeIter iter;
286 gchar *key = NULL;
287 if (gtk_combo_box_get_active_iter (GTK_COMBO_BOX (jhp->widget), &iter))
289 gtk_tree_model_get (GTK_TREE_MODEL (jhp->lstore), &iter, 1, &key, -1);
290 if (key) {
291 jhp->gui->plugin->configure(jhp->attribs["setter-key"].c_str(), key);
292 free(key);
296 else
297 jhp->get();
300 void combo_box_param_control::send_status(const char *key, const char *value)
302 if (attribs.count("key") && key == attribs["key"])
304 gtk_list_store_clear (lstore);
305 key2pos.clear();
306 std::string v = value;
307 int i = 0;
308 size_t pos = 0;
309 while (pos < v.length()) {
310 size_t endpos = v.find("\n", pos);
311 if (endpos == string::npos)
312 break;
313 string line = v.substr(pos, endpos - pos);
314 string key, label;
315 size_t tabpos = line.find('\t');
316 if (tabpos == string::npos)
317 key = label = line;
318 else {
319 key = line.substr(0, tabpos);
320 label = line.substr(tabpos + 1);
322 GtkTreeIter gti;
323 gtk_list_store_insert_with_values (lstore, &gti, i, 0, label.c_str(), 1, key.c_str(), -1);
324 key2pos[key] = gti;
325 pos = endpos + 1;
326 i++;
328 set_to_last_key();
330 if (attribs.count("current-key") && key == attribs["current-key"])
332 last_key = value;
333 set_to_last_key();
337 void combo_box_param_control::set_to_last_key()
339 map<string, GtkTreeIter>::iterator i = key2pos.find(last_key);
340 if (i != key2pos.end())
342 gtk_combo_box_set_active_iter (GTK_COMBO_BOX (widget), &i->second);
345 else
346 gtk_combo_box_set_active (GTK_COMBO_BOX (widget), -1);
349 /******************************** Horizontal Fader ********************************/
351 static gboolean
352 scale_to_default (gpointer data)
354 hscale_param_control *jhp = (hscale_param_control *)data;
355 const parameter_properties &props = jhp->get_props();
356 gtk_range_set_value (GTK_RANGE (jhp->widget), props.to_01(props.def_value));
358 return FALSE;
361 static gboolean
362 scale_button_press (GtkWidget *widget, GdkEventKey *event, gpointer *user_data)
364 if (event->type == GDK_2BUTTON_PRESS) {
365 // this actually creates a harmless race condition, but diving deep
366 // into gtk signal handling code wouldn't and the resulting complexity
367 // would not really be worth the effort
368 // The timeout is set high enough that most of the time the race
369 // will turn out in our/the users favor
370 g_timeout_add (200, (GSourceFunc)scale_to_default, user_data);
371 return TRUE;
374 return FALSE;
377 GtkWidget *hscale_param_control::create(plugin_gui *_gui, int _param_no)
379 gui = _gui;
380 param_no = _param_no;
382 widget = calf_fader_new(1, get_int("size", 2), 0, 1, get_props().get_increment());
384 g_signal_connect (GTK_OBJECT (widget), "value-changed", G_CALLBACK (hscale_value_changed), (gpointer)this);
385 g_signal_connect (GTK_OBJECT (widget), "format-value", G_CALLBACK (hscale_format_value), (gpointer)this);
386 g_signal_connect (GTK_OBJECT (widget), "button-press-event", G_CALLBACK (scale_button_press), (gpointer)this);
388 if(get_int("inverted", 0) > 0) {
389 gtk_range_set_inverted(GTK_RANGE(widget), TRUE);
391 int size = get_int("size", 2);
392 char *name = g_strdup_printf("Calf-HScale%i", size);
393 gtk_widget_set_name(GTK_WIDGET(widget), name);
394 gtk_widget_set_size_request (widget, size * 100, -1);
395 g_free(name);
396 return widget;
399 void hscale_param_control::init_xml(const char *element)
401 if (attribs.count("width"))
402 gtk_widget_set_size_request (widget, get_int("width", 200), -1);
403 if (attribs.count("position"))
405 string v = attribs["position"];
406 if (v == "top") gtk_scale_set_value_pos(GTK_SCALE(widget), GTK_POS_TOP);
407 if (v == "bottom") gtk_scale_set_value_pos(GTK_SCALE(widget), GTK_POS_BOTTOM);
411 void hscale_param_control::set()
413 _GUARD_CHANGE_
414 const parameter_properties &props = get_props();
415 gtk_range_set_value (GTK_RANGE (widget), props.to_01 (gui->plugin->get_param_value(param_no)));
416 // hscale_value_changed (GTK_HSCALE (widget), (gpointer)this);
419 void hscale_param_control::get()
421 const parameter_properties &props = get_props();
422 float cvalue = props.from_01 (gtk_range_get_value (GTK_RANGE (widget)));
423 gui->set_param_value(param_no, cvalue, this);
426 void hscale_param_control::hscale_value_changed(GtkHScale *widget, gpointer value)
428 hscale_param_control *jhp = (hscale_param_control *)value;
429 jhp->get();
432 gchar *hscale_param_control::hscale_format_value(GtkScale *widget, double arg1, gpointer value)
434 hscale_param_control *jhp = (hscale_param_control *)value;
435 const parameter_properties &props = jhp->get_props();
436 float cvalue = props.from_01 (arg1);
438 // for testing
439 // return g_strdup_printf ("%s = %g", props.to_string (cvalue).c_str(), arg1);
440 return g_strdup (props.to_string (cvalue).c_str());
443 /******************************** Vertical Fader ********************************/
445 GtkWidget *vscale_param_control::create(plugin_gui *_gui, int _param_no)
447 gui = _gui;
448 param_no = _param_no;
449 widget = calf_fader_new(0, get_int("size", 2), 0, 1, get_props().get_increment());
450 g_signal_connect (GTK_OBJECT (widget), "value-changed", G_CALLBACK (vscale_value_changed), (gpointer)this);
451 g_signal_connect (GTK_OBJECT (widget), "button-press-event", G_CALLBACK (scale_button_press), (gpointer)this);
453 gtk_scale_set_draw_value(GTK_SCALE(widget), FALSE);
455 if(get_int("inverted", 0) > 0) {
456 gtk_range_set_inverted(GTK_RANGE(widget), TRUE);
458 int size = get_int("size", 2);
459 char *name = g_strdup_printf("Calf-VScale%i", size);
460 gtk_widget_set_size_request (widget, -1, size * 100);
461 gtk_widget_set_name(GTK_WIDGET(widget), name);
462 g_free(name);
463 return widget;
466 void vscale_param_control::init_xml(const char *element)
468 if (attribs.count("height"))
469 gtk_widget_set_size_request (widget, -1, get_int("height", 200));
472 void vscale_param_control::set()
474 _GUARD_CHANGE_
475 const parameter_properties &props = get_props();
476 gtk_range_set_value (GTK_RANGE (widget), props.to_01 (gui->plugin->get_param_value(param_no)));
477 // vscale_value_changed (GTK_HSCALE (widget), (gpointer)this);
480 void vscale_param_control::get()
482 const parameter_properties &props = get_props();
483 float cvalue = props.from_01 (gtk_range_get_value (GTK_RANGE (widget)));
484 gui->set_param_value(param_no, cvalue, this);
487 void vscale_param_control::vscale_value_changed(GtkHScale *widget, gpointer value)
489 vscale_param_control *jhp = (vscale_param_control *)value;
490 jhp->get();
493 /******************************** Label ********************************/
495 GtkWidget *label_param_control::create(plugin_gui *_gui, int _param_no)
497 gui = _gui, param_no = _param_no;
498 string text;
499 if (param_no != -1 && !attribs.count("text"))
500 text = get_props().name;
501 else
502 text = attribs["text"];
503 widget = gtk_label_new(text.c_str());
504 gtk_misc_set_alignment (GTK_MISC (widget), get_float("align-x", 0.5), get_float("align-y", 0.5));
505 gtk_widget_set_name(GTK_WIDGET(widget), "Calf-Label");
506 return widget;
509 /******************************** Value ********************************/
511 GtkWidget *value_param_control::create(plugin_gui *_gui, int _param_no)
513 gui = _gui;
514 param_no = _param_no;
516 widget = gtk_label_new ("");
517 if (param_no != -1)
519 const parameter_properties &props = get_props();
520 gtk_label_set_width_chars (GTK_LABEL (widget), props.get_char_count());
522 else
524 require_attribute("key");
525 require_int_attribute("width");
526 param_variable = attribs["key"];
527 gtk_label_set_width_chars (GTK_LABEL (widget), get_int("width"));
529 gtk_misc_set_alignment (GTK_MISC (widget), get_float("align-x", 0.5), get_float("align-y", 0.5));
530 gtk_widget_set_name(GTK_WIDGET(widget), "Calf-Value");
531 return widget;
534 void value_param_control::set()
536 if (param_no == -1)
537 return;
538 _GUARD_CHANGE_
540 const parameter_properties &props = get_props();
541 string value = props.to_string(gui->plugin->get_param_value(param_no));
543 if (value == old_value)
544 return;
545 old_value = value;
546 gtk_label_set_text (GTK_LABEL (widget), value.c_str());
549 void value_param_control::send_status(const char *key, const char *value)
551 if (key == param_variable)
553 gtk_label_set_text (GTK_LABEL (widget), value);
557 /******************************** VU Meter ********************************/
559 GtkWidget *vumeter_param_control::create(plugin_gui *_gui, int _param_no)
561 gui = _gui, param_no = _param_no;
562 // const parameter_properties &props = get_props();
563 widget = calf_vumeter_new ();
564 gtk_widget_set_name(GTK_WIDGET(widget), "calf-vumeter");
565 calf_vumeter_set_mode (CALF_VUMETER (widget), (CalfVUMeterMode)get_int("mode", 0));
566 CALF_VUMETER(widget)->vumeter_hold = get_float("hold", 0);
567 CALF_VUMETER(widget)->vumeter_falloff = get_float("falloff", 0.f);
568 CALF_VUMETER(widget)->vumeter_width = get_int("width", 80);
569 CALF_VUMETER(widget)->vumeter_height = get_int("height", 18);
570 CALF_VUMETER(widget)->vumeter_position = get_int("position", 0);
571 gtk_widget_set_name(GTK_WIDGET(widget), "Calf-VUMeter");
572 return widget;
575 void vumeter_param_control::set()
577 _GUARD_CHANGE_
578 // const parameter_properties &props = get_props();
579 calf_vumeter_set_value (CALF_VUMETER (widget), gui->plugin->get_param_value(param_no));
582 // LED
584 GtkWidget *led_param_control::create(plugin_gui *_gui, int _param_no)
586 gui = _gui, param_no = _param_no;
587 // const parameter_properties &props = get_props();
588 widget = calf_led_new ();
589 gtk_widget_set_name(GTK_WIDGET(widget), "calf-led");
590 CALF_LED(widget)->led_mode = get_int("mode", 0);
591 gtk_widget_set_name(GTK_WIDGET(widget), "Calf-LED");
592 return widget;
595 void led_param_control::set()
597 _GUARD_CHANGE_
598 // const parameter_properties &props = get_props();
599 calf_led_set_value (CALF_LED (widget), gui->plugin->get_param_value(param_no));
602 // tube
604 GtkWidget *tube_param_control::create(plugin_gui *_gui, int _param_no)
606 gui = _gui, param_no = _param_no;
607 // const parameter_properties &props = get_props();
608 widget = calf_tube_new ();
609 gtk_widget_set_name(GTK_WIDGET(widget), "calf-tube");
610 CALF_TUBE(widget)->size = get_int("size", 2);
611 CALF_TUBE(widget)->direction = get_int("direction", 2);
612 gtk_widget_set_name(GTK_WIDGET(widget), "Calf-Tube");
613 return widget;
616 void tube_param_control::set()
618 _GUARD_CHANGE_
619 // const parameter_properties &props = get_props();
620 calf_tube_set_value (CALF_TUBE (widget), gui->plugin->get_param_value(param_no));
623 /******************************** Check Box ********************************/
625 GtkWidget *check_param_control::create(plugin_gui *_gui, int _param_no)
627 gui = _gui;
628 param_no = _param_no;
630 widget = gtk_check_button_new ();
631 g_signal_connect (GTK_OBJECT (widget), "toggled", G_CALLBACK (check_value_changed), (gpointer)this);
632 gtk_widget_set_name(GTK_WIDGET(widget), "Calf-Checkbox");
633 return widget;
636 void check_param_control::check_value_changed(GtkCheckButton *widget, gpointer value)
638 param_control *jhp = (param_control *)value;
639 jhp->get();
642 void check_param_control::get()
644 const parameter_properties &props = get_props();
645 gui->set_param_value(param_no, gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON(widget)) + props.min, this);
648 void check_param_control::set()
650 _GUARD_CHANGE_
651 const parameter_properties &props = get_props();
652 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (widget), (int)gui->plugin->get_param_value(param_no) - (int)props.min);
655 /******************************** Radio Button ********************************/
657 GtkWidget *radio_param_control::create(plugin_gui *_gui, int _param_no)
659 gui = _gui;
660 param_no = _param_no;
661 require_attribute("value");
662 value = -1;
663 string value_name = attribs["value"];
664 const parameter_properties &props = get_props();
665 if (props.choices && (value_name < "0" || value_name > "9"))
667 for (int i = 0; props.choices[i]; i++)
669 if (value_name == props.choices[i])
671 value = i + (int)props.min;
672 break;
676 if (value == -1)
677 value = get_int("value");
679 if (attribs.count("label"))
680 widget = gtk_radio_button_new_with_label (gui->get_radio_group(param_no), attribs["label"].c_str());
681 else
682 widget = gtk_radio_button_new_with_label (gui->get_radio_group(param_no), props.choices[value - (int)props.min]);
683 gtk_toggle_button_set_mode (GTK_TOGGLE_BUTTON (widget), FALSE);
685 gui->set_radio_group(param_no, gtk_radio_button_get_group (GTK_RADIO_BUTTON (widget)));
686 g_signal_connect (GTK_OBJECT (widget), "clicked", G_CALLBACK (radio_clicked), (gpointer)this);
687 gtk_widget_set_name(GTK_WIDGET(widget), "Calf-RadioButton");
688 return widget;
691 void radio_param_control::radio_clicked(GtkRadioButton *widget, gpointer value)
693 param_control *jhp = (param_control *)value;
694 jhp->get();
697 void radio_param_control::get()
699 // const parameter_properties &props = get_props();
700 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget)))
701 gui->set_param_value(param_no, value, this);
704 void radio_param_control::set()
706 _GUARD_CHANGE_
707 const parameter_properties &props = get_props();
708 float pv = gui->plugin->get_param_value(param_no);
709 if (fabs(value-pv) < 0.5)
710 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (widget), value == ((int)gui->plugin->get_param_value(param_no) - (int)props.min));
713 /******************************** Spin Button ********************************/
715 GtkWidget *spin_param_control::create(plugin_gui *_gui, int _param_no)
717 gui = _gui;
718 param_no = _param_no;
720 const parameter_properties &props = get_props();
721 if (props.step > 1)
722 widget = gtk_spin_button_new_with_range (props.min, props.max, (props.max - props.min) / (props.step - 1));
723 if (props.step > 0)
724 widget = gtk_spin_button_new_with_range (props.min, props.max, props.step);
725 else
726 widget = gtk_spin_button_new_with_range (props.min, props.max, 1);
727 gtk_spin_button_set_digits (GTK_SPIN_BUTTON(widget), get_int("digits", 0));
728 g_signal_connect (GTK_OBJECT (widget), "value-changed", G_CALLBACK (value_changed), (gpointer)this);
729 gtk_widget_set_name(GTK_WIDGET(widget), "Calf-SpinButton");
730 return widget;
733 void spin_param_control::value_changed(GtkSpinButton *widget, gpointer value)
735 param_control *jhp = (param_control *)value;
736 jhp->get();
739 void spin_param_control::get()
741 // const parameter_properties &props = get_props();
742 gui->set_param_value(param_no, gtk_spin_button_get_value_as_float (GTK_SPIN_BUTTON (widget)), this);
745 void spin_param_control::set()
747 _GUARD_CHANGE_
748 // const parameter_properties &props = get_props();
749 gtk_spin_button_set_value (GTK_SPIN_BUTTON (widget), gui->plugin->get_param_value(param_no));
752 /******************************** Button ********************************/
754 GtkWidget *button_param_control::create(plugin_gui *_gui, int _param_no)
756 gui = _gui;
757 param_no = _param_no;
759 widget = calf_button_new ((gchar*)get_props().name);
760 g_signal_connect (GTK_OBJECT (widget), "clicked", G_CALLBACK (button_clicked), (gpointer)this);
761 gtk_widget_set_name(GTK_WIDGET(widget), "Calf-Button");
762 return widget;
765 void button_param_control::button_clicked(GtkButton *widget, gpointer value)
767 param_control *jhp = (param_control *)value;
768 jhp->get();
771 void button_param_control::get()
773 const parameter_properties &props = get_props();
774 gui->set_param_value(param_no, props.max, this);
777 void button_param_control::set()
779 _GUARD_CHANGE_
780 const parameter_properties &props = get_props();
781 if (gui->plugin->get_param_value(param_no) - props.min >= 0.5)
782 gtk_button_clicked (GTK_BUTTON (widget));
785 /******************************** Knob ********************************/
787 GtkWidget *knob_param_control::create(plugin_gui *_gui, int _param_no)
789 gui = _gui;
790 param_no = _param_no;
791 const parameter_properties &props = get_props();
793 //widget = calf_knob_new_with_range (props.to_01 (gui->plugin->get_param_value(param_no)), 0, 1, 0.01);
794 widget = calf_knob_new();
795 float increment = props.get_increment();
796 gtk_range_get_adjustment(GTK_RANGE(widget))->step_increment = increment;
797 CALF_KNOB(widget)->default_value = props.to_01(props.def_value);
798 CALF_KNOB(widget)->knob_type = get_int("type");
799 CALF_KNOB(widget)->knob_size = get_int("size", 2);
800 if(CALF_KNOB(widget)->knob_size > 5) {
801 CALF_KNOB(widget)->knob_size = 5;
802 } else if (CALF_KNOB(widget)->knob_size < 1) {
803 CALF_KNOB(widget)->knob_size = 1;
805 g_signal_connect(GTK_OBJECT(widget), "value-changed", G_CALLBACK(knob_value_changed), (gpointer)this);
806 gtk_widget_set_name(GTK_WIDGET(widget), "Calf-Knob");
807 return widget;
810 void knob_param_control::get()
812 const parameter_properties &props = get_props();
813 float value = props.from_01(gtk_range_get_value(GTK_RANGE(widget)));
814 gui->set_param_value(param_no, value, this);
817 void knob_param_control::set()
819 _GUARD_CHANGE_
820 const parameter_properties &props = get_props();
821 gtk_range_set_value(GTK_RANGE(widget), props.to_01 (gui->plugin->get_param_value(param_no)));
824 void knob_param_control::knob_value_changed(GtkWidget *widget, gpointer value)
826 param_control *jhp = (param_control *)value;
827 jhp->get();
830 /******************************** Toggle Button ********************************/
832 GtkWidget *toggle_param_control::create(plugin_gui *_gui, int _param_no)
834 gui = _gui;
835 param_no = _param_no;
836 widget = calf_toggle_new ();
838 CALF_TOGGLE(widget)->size = get_int("size", 2);
840 g_signal_connect (GTK_OBJECT (widget), "value-changed", G_CALLBACK (toggle_value_changed), (gpointer)this);
841 gtk_widget_set_name(GTK_WIDGET(widget), "Calf-ToggleButton");
842 return widget;
845 void toggle_param_control::get()
847 const parameter_properties &props = get_props();
848 float value = props.from_01(gtk_range_get_value(GTK_RANGE(widget)));
849 gui->set_param_value(param_no, value, this);
852 void toggle_param_control::set()
854 _GUARD_CHANGE_
855 const parameter_properties &props = get_props();
856 gtk_range_set_value(GTK_RANGE(widget), props.to_01 (gui->plugin->get_param_value(param_no)));
859 void toggle_param_control::toggle_value_changed(GtkWidget *widget, gpointer value)
861 param_control *jhp = (param_control *)value;
862 jhp->get();
865 /******************************** Tap Button ********************************/
867 GtkWidget *tap_button_param_control::create(plugin_gui *_gui, int _param_no)
869 gui = _gui;
870 param_no = _param_no;
871 last_time = 0;
872 init_time = 0;
873 avg_value = 0;
874 value = 0;
875 widget = calf_tap_button_new ();
876 //CALF_TAP(widget)->size = get_int("size", 2);
877 g_signal_connect (GTK_OBJECT (widget), "button-press-event", G_CALLBACK (tap_button_pressed), (gpointer)this);
878 g_signal_connect (GTK_OBJECT (widget), "released", G_CALLBACK (tap_button_released), (gpointer)this);
879 g_signal_connect (GTK_OBJECT (widget), "leave", G_CALLBACK (tap_button_released), (gpointer)this);
880 gtk_widget_set_name(GTK_WIDGET(widget), "Calf-TapButton");
881 return widget;
884 void tap_button_param_control::get()
886 gui->set_param_value(param_no, value, this);
889 void tap_button_param_control::set()
891 _GUARD_CHANGE_
892 if(last_time) {
893 timeval tv;
894 gettimeofday(&tv, 0);
895 unsigned long _now = tv.tv_sec * 1000;
896 if(_now > init_time + 2000) {
897 // user stopped tapping
898 avg_value = 0;
899 last_time = 0;
900 init_time = 0;
901 CALF_TAP_BUTTON(widget)->state = 0;
902 gtk_widget_queue_draw(widget);
907 gboolean tap_button_param_control::tap_button_pressed(GtkWidget *widget, GdkEventButton *event, gpointer value)
909 tap_button_param_control *ctl = (tap_button_param_control *)value;
910 CalfTapButton *tap = CALF_TAP_BUTTON(widget);
912 guint time = 0;
913 if (event->type == GDK_BUTTON_PRESS and event->button == 1)
915 timeval tv;
916 gettimeofday(&tv, 0);
917 ctl->init_time = tv.tv_sec * 1000;
918 time = event->time;
919 tap->state = 2;
921 if(ctl->last_time) {
922 if(ctl->avg_value)
923 ctl->avg_value = (ctl->avg_value * 3 + (time - ctl->last_time)) / 4.f;
924 else
925 ctl->avg_value = time - ctl->last_time;
926 ctl->value = 60.f / (float)(ctl->avg_value / 1000.f);
928 if (ctl->value > 30 and ctl->value < 300)
929 ctl->get();
931 ctl->last_time = time;
932 gtk_widget_queue_draw(widget);
934 return FALSE;
936 gboolean tap_button_param_control::tap_button_released(GtkWidget *widget, gpointer value)
938 tap_button_param_control *ctl = (tap_button_param_control *)value;
939 CalfTapButton *tap = CALF_TAP_BUTTON(widget);
940 tap->state = ctl->last_time ? 1 : 0;
941 gtk_widget_queue_draw(widget);
942 return FALSE;
945 /******************************** Keyboard ********************************/
947 GtkWidget *keyboard_param_control::create(plugin_gui *_gui, int _param_no)
949 gui = _gui;
950 param_no = _param_no;
951 // const parameter_properties &props = get_props();
953 widget = calf_keyboard_new();
954 kb = CALF_KEYBOARD(widget);
955 kb->nkeys = get_int("octaves", 4) * 7 + 1;
956 kb->sink = new CalfKeyboard::EventAdapter;
957 gtk_widget_set_name(GTK_WIDGET(widget), "Calf-Keyboard");
958 return widget;
961 /******************************** Curve ********************************/
963 struct curve_param_control_callback: public CalfCurve::EventAdapter
965 curve_param_control *ctl;
967 curve_param_control_callback(curve_param_control *_ctl)
968 : ctl(_ctl) {}
970 virtual void curve_changed(CalfCurve *src, const CalfCurve::point_vector &data) {
971 stringstream ss;
972 ss << data.size() << endl;
973 for (size_t i = 0; i < data.size(); i++)
974 ss << data[i].first << " " << data[i].second << endl;
975 ctl->gui->plugin->configure(ctl->attribs["key"].c_str(), ss.str().c_str());
977 virtual void clip(CalfCurve *src, int pt, float &x, float &y, bool &hide)
979 // int gridpt = floor(x * 71 * 2);
980 // clip to the middle of the nearest white key
981 x = (floor(x * 71) + 0.5)/ 71.0;
985 GtkWidget *curve_param_control::create(plugin_gui *_gui, int _param_no)
987 gui = _gui;
988 param_no = _param_no;
989 require_attribute("key");
991 widget = calf_curve_new(get_int("maxpoints", -1));
992 curve = CALF_CURVE(widget);
993 curve->sink = new curve_param_control_callback(this);
994 // gtk_curve_set_curve_type(curve, GTK_CURVE_TYPE_LINEAR);
995 gtk_widget_set_name(GTK_WIDGET(widget), "Calf-Curve");
996 return widget;
999 void curve_param_control::send_configure(const char *key, const char *value)
1001 // cout << "send conf " << key << endl;
1002 if (attribs["key"] == key)
1004 stringstream ss(value);
1005 CalfCurve::point_vector pts;
1006 if (*value)
1008 unsigned int npoints = 0;
1009 ss >> npoints;
1010 unsigned int i;
1011 float x = 0, y = 0;
1012 for (i = 0; i < npoints && i < curve->point_limit; i++)
1014 ss >> x >> y;
1015 pts.push_back(CalfCurve::point(x, y));
1017 calf_curve_set_points(widget, pts);
1022 /******************************** Entry ********************************/
1024 GtkWidget *entry_param_control::create(plugin_gui *_gui, int _param_no)
1026 gui = _gui;
1027 param_no = _param_no;
1028 require_attribute("key");
1030 widget = gtk_entry_new();
1031 entry = GTK_ENTRY(widget);
1032 g_signal_connect(GTK_OBJECT(widget), "changed", G_CALLBACK(entry_value_changed), (gpointer)this);
1033 gtk_editable_set_editable(GTK_EDITABLE(entry), get_int("editable", 1));
1034 gtk_widget_set_name(GTK_WIDGET(widget), "Calf-Entry");
1035 return widget;
1038 void entry_param_control::send_configure(const char *key, const char *value)
1040 // cout << "send conf " << key << endl;
1041 if (attribs["key"] == key)
1043 gtk_entry_set_text(entry, value);
1047 void entry_param_control::entry_value_changed(GtkWidget *widget, gpointer value)
1049 entry_param_control *ctl = (entry_param_control *)value;
1050 ctl->gui->plugin->configure(ctl->attribs["key"].c_str(), gtk_entry_get_text(ctl->entry));
1053 /******************************** File Chooser ********************************/
1055 GtkWidget *filechooser_param_control::create(plugin_gui *_gui, int _param_no)
1057 gui = _gui;
1058 param_no = _param_no;
1059 require_attribute("key");
1060 require_attribute("title");
1062 widget = gtk_file_chooser_button_new(attribs["title"].c_str(), GTK_FILE_CHOOSER_ACTION_OPEN);
1063 filechooser = GTK_FILE_CHOOSER_BUTTON(widget);
1064 // XXXKF this is GTK+ 2.12 function, does any replacement exist?
1065 // MS: switched from g_signal_connect to g_signal_connect for better emission of signals
1066 g_signal_connect(GTK_OBJECT(widget), "file-set", G_CALLBACK(filechooser_value_changed), (gpointer)this);
1067 if (attribs.count("width"))
1068 gtk_widget_set_size_request (widget, get_int("width", 200), -1);
1069 if (attribs.count("width_chars"))
1070 gtk_file_chooser_button_set_width_chars (filechooser, get_int("width_chars"));
1071 gtk_widget_set_name(GTK_WIDGET(widget), "Calf-FileButton");
1072 return widget;
1075 void filechooser_param_control::send_configure(const char *key, const char *value)
1077 // cout << "send conf " << key << endl;
1078 if (attribs["key"] == key)
1080 gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(filechooser), value);
1084 void filechooser_param_control::filechooser_value_changed(GtkWidget *widget, gpointer value)
1086 filechooser_param_control *ctl = (filechooser_param_control *)value;
1087 const char *filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(ctl->filechooser));
1088 if (filename)
1089 ctl->gui->plugin->configure(ctl->attribs["key"].c_str(), filename);
1092 /******************************** Line Graph ********************************/
1094 void line_graph_param_control::on_idle()
1096 if (get_int("refresh", 0))
1097 set();
1100 static float to_x_pos(float freq)
1102 return log(freq / 20.0) / log(1000);
1105 static float from_x_pos(float pos)
1107 float a = pos * 3.0;
1108 float b = powf(10.0, a);
1109 float c = b * 20.0;
1110 return c;
1113 static float to_y_pos(CalfLineGraph *lg, float gain)
1115 //log(gain) * (1.0 / log(32));
1116 return 0.5 - dB_grid(gain, 128 * lg->zoom, lg->offset) / 2.0;
1119 static float from_y_pos(CalfLineGraph *lg, float pos)
1121 float gain = powf(128.0 * lg->zoom, (0.5 - pos) * 2.0 - lg->offset);
1122 return gain;
1125 GtkWidget *line_graph_param_control::create(plugin_gui *a_gui, int a_param_no)
1127 gui = a_gui;
1128 param_no = a_param_no;
1129 //last_generation = -1;
1131 widget = calf_line_graph_new ();
1133 gtk_widget_set_name(GTK_WIDGET(widget), "calf-graph");
1135 CalfLineGraph *clg = CALF_LINE_GRAPH(widget);
1136 widget->requisition.width = get_int("width", 40);
1137 widget->requisition.height = get_int("height", 40);
1139 calf_line_graph_set_square(clg, get_int("square", 0));
1141 clg->source = gui->plugin->get_line_graph_iface();
1142 clg->source_id = param_no;
1143 clg->fade = get_float("fade", 1.0);
1144 clg->mode = get_int("mode", 0);
1145 clg->use_crosshairs = get_int("crosshairs", 0);
1146 clg->freqhandles = get_int("freqhandles", 0);
1147 clg->enforce_handle_order = get_int("enforce-handle-order", 0);
1148 clg->min_handle_distance = get_float("min-handle-distance", 0.01);
1150 const string &zoom_name = attribs["zoom"];
1151 if (zoom_name != "")
1152 clg->param_zoom = gui->get_param_no_by_name(zoom_name);
1154 const string &offset_name = attribs["offset"];
1155 if (offset_name != "")
1156 clg->param_offset = gui->get_param_no_by_name(offset_name);
1158 if (clg->freqhandles > 0)
1160 for(int i = 0; i < clg->freqhandles; i++)
1162 FreqHandle *handle = &clg->freq_handles[i];
1164 stringstream handle_x_attribute;
1165 handle_x_attribute << "handle" << i + 1 << "-x";
1166 const string &param_x_name = attribs[handle_x_attribute.str()];
1167 if(param_x_name == "")
1168 break;
1170 int param_x_no = gui->get_param_no_by_name(param_x_name);
1171 const parameter_properties &handle_x_props = *gui->plugin->get_metadata_iface()->get_param_props(param_x_no);
1172 handle->dimensions = 1;
1173 handle->param_x_no = param_x_no;
1174 handle->value_x = to_x_pos(gui->plugin->get_param_value(param_x_no));
1175 handle->default_value_x = to_x_pos(handle_x_props.def_value);
1177 stringstream handle_y_attribute;
1178 handle_y_attribute << "handle" << i + 1 << "-y";
1179 const string &param_y_name = attribs[handle_y_attribute.str()];
1180 if(param_y_name != "") {
1181 int param_y_no = gui->get_param_no_by_name(param_y_name);
1182 const parameter_properties &handle_y_props = *gui->plugin->get_metadata_iface()->get_param_props(param_y_no);
1183 handle->dimensions = 2;
1184 handle->param_y_no = param_y_no;
1185 handle->value_y = to_y_pos(clg, gui->plugin->get_param_value(param_y_no));
1186 handle->default_value_y = to_y_pos(clg, handle_y_props.def_value);
1187 } else {
1188 handle->param_y_no = -1;
1191 stringstream handle_z_attribute;
1192 handle_z_attribute << "handle" << i + 1 << "-z";
1193 const string &param_z_name = attribs[handle_z_attribute.str()];
1194 if(param_z_name != "") {
1195 int param_z_no = gui->get_param_no_by_name(param_z_name);
1196 const parameter_properties &handle_z_props = *gui->plugin->get_metadata_iface()->get_param_props(param_z_no);
1197 handle->dimensions = 3;
1198 handle->param_z_no = param_z_no;
1199 handle->value_z = handle_z_props.to_01(gui->plugin->get_param_value(param_z_no));
1200 handle->default_value_z = handle_z_props.to_01(handle_z_props.def_value);
1201 } else {
1202 handle->param_z_no = -1;
1205 stringstream label_attribute;
1206 label_attribute << "label" << i + 1;
1207 string label = attribs[label_attribute.str()];
1208 if (!label.empty()) {
1209 handle->label = strdup(label.c_str());
1212 stringstream active_attribute;
1213 active_attribute << "active" << i + 1;
1214 const string &active_name = attribs[active_attribute.str()];
1215 if (active_name != "") {
1216 handle->param_active_no = gui->get_param_no_by_name(active_name);
1217 } else {
1218 handle->param_active_no = -1;
1221 stringstream style_attribute;
1222 style_attribute << "style" << i + 1;
1223 const string style = style_attribute.str();
1224 clg->freq_handles[i].style = get_int(style.c_str(), 0);
1225 if(clg->freq_handles[i].style == 1 or clg->freq_handles[i].style == 4) {
1226 clg->freq_handles[i].dimensions = 1;
1228 handle->data = (gpointer) this;
1230 g_signal_connect(G_OBJECT(widget), "freqhandle-changed", G_CALLBACK(freqhandle_value_changed), this);
1233 gtk_widget_set_name(GTK_WIDGET(widget), "Calf-LineGraph");
1234 return widget;
1237 void line_graph_param_control::get()
1239 GtkWidget *tw = gtk_widget_get_toplevel(widget);
1240 CalfLineGraph *clg = CALF_LINE_GRAPH(widget);
1242 if (tw && GTK_WIDGET_TOPLEVEL(tw) && widget->window)
1244 int ws = gdk_window_get_state(widget->window);
1245 if (ws & (GDK_WINDOW_STATE_WITHDRAWN | GDK_WINDOW_STATE_ICONIFIED))
1246 return;
1248 if(clg->handle_grabbed >= 0) {
1249 FreqHandle *handle = &clg->freq_handles[clg->handle_grabbed];
1250 if(handle->dimensions >= 2) {
1251 float value_y = from_y_pos(clg, handle->value_y);
1252 gui->set_param_value(handle->param_y_no, value_y, this);
1255 float value_x = from_x_pos(handle->value_x);
1256 gui->set_param_value(handle->param_x_no, value_x, this);
1257 } else if(clg->handle_hovered >= 0) {
1258 FreqHandle *handle = &clg->freq_handles[clg->handle_hovered];
1260 if(handle->dimensions == 3) {
1261 const parameter_properties &handle_z_props = *gui->plugin->get_metadata_iface()->get_param_props(handle->param_z_no);
1262 float value_z = handle_z_props.from_01(handle->value_z);
1263 gui->set_param_value(handle->param_z_no, value_z, this);
1269 void line_graph_param_control::set()
1271 _GUARD_CHANGE_
1272 GtkWidget *tw = gtk_widget_get_toplevel(widget);
1273 CalfLineGraph *clg = CALF_LINE_GRAPH(widget);
1274 if (tw && GTK_WIDGET_TOPLEVEL(tw) && widget->window)
1276 bool force = false;
1277 int ws = gdk_window_get_state(widget->window);
1278 if (ws & (GDK_WINDOW_STATE_WITHDRAWN | GDK_WINDOW_STATE_ICONIFIED))
1279 return;
1281 if (clg->param_zoom >= 0) {
1282 float _z = gui->plugin->get_param_value(clg->param_zoom);
1283 if (_z != clg->zoom) {
1284 force = true;
1285 clg->zoom = _z;
1286 clg->force_redraw = true;
1290 if (clg->param_offset >= 0) {
1291 float _z = gui->plugin->get_param_value(clg->param_offset);
1292 if (_z != clg->offset) {
1293 force = true;
1294 clg->offset = _z;
1295 clg->force_redraw = true;
1299 for (int i = 0; i < clg->freqhandles; i++) {
1300 FreqHandle *handle = &clg->freq_handles[i];
1302 if (handle->param_x_no >= 0)
1304 float value_x = gui->plugin->get_param_value(handle->param_x_no);
1305 handle->value_x = to_x_pos(value_x);
1306 if (dsp::_sanitize(handle->value_x - handle->last_value_x)) {
1307 clg->handle_redraw = 1;
1309 handle->last_value_x = handle->value_x;
1310 if(handle->dimensions >= 2 && handle->param_y_no >= 0) {
1311 float value_y = gui->plugin->get_param_value(handle->param_y_no);
1312 handle->value_y = to_y_pos(clg, value_y);
1313 if (dsp::_sanitize(handle->value_y - handle->last_value_y)) {
1314 clg->handle_redraw = 1;
1316 handle->last_value_y = handle->value_y;
1320 if(handle->dimensions == 3 && handle->param_z_no >= 0) {
1321 const parameter_properties &handle_z_props = *gui->plugin->get_metadata_iface()->get_param_props(handle->param_z_no);
1322 float value_z = gui->plugin->get_param_value(handle->param_z_no);
1323 handle->value_z = handle_z_props.to_01(value_z);
1324 if (dsp::_sanitize(handle->value_z - handle->last_value_z)) {
1325 clg->handle_redraw = 1;
1327 handle->last_value_z = handle->value_z;
1329 bool _a = handle->active;
1330 if(handle->param_active_no >= 0) {
1331 handle->active = bool(gui->plugin->get_param_value(handle->param_active_no));
1332 } else {
1333 handle->active = true;
1335 if (handle->active != _a) {
1336 force = true;
1337 clg->handle_redraw = true;
1340 calf_line_graph_expose_request(widget, force);
1344 void line_graph_param_control::freqhandle_value_changed(GtkWidget *widget, gpointer p)
1346 assert(p!=NULL);
1347 FreqHandle *handle = (FreqHandle *)p;
1348 param_control *jhp = (param_control *)handle->data;
1349 jhp->get();
1353 line_graph_param_control::~line_graph_param_control()
1358 /******************************** Phase Graph ********************************/
1360 void phase_graph_param_control::on_idle()
1362 if (get_int("refresh", 0))
1363 set();
1366 GtkWidget *phase_graph_param_control::create(plugin_gui *_gui, int _param_no)
1368 gui = _gui;
1369 param_no = _param_no;
1370 widget = calf_phase_graph_new ();
1371 gtk_widget_set_name(GTK_WIDGET(widget), "calf-phase");
1372 CalfPhaseGraph *clg = CALF_PHASE_GRAPH(widget);
1373 widget->requisition.width = get_int("size", 40);
1374 widget->requisition.height = get_int("size", 40);
1375 clg->source = gui->plugin->get_phase_graph_iface();
1376 clg->source_id = param_no;
1377 gtk_widget_set_name(GTK_WIDGET(widget), "Calf-PhaseGraph");
1378 return widget;
1381 void phase_graph_param_control::set()
1383 _GUARD_CHANGE_
1384 GtkWidget *tw = gtk_widget_get_toplevel(widget);
1385 if (tw && GTK_WIDGET_TOPLEVEL(tw) && widget->window) {
1386 gtk_widget_queue_draw(widget);
1390 phase_graph_param_control::~phase_graph_param_control()
1394 /******************************** List View ********************************/
1396 GtkWidget *listview_param_control::create(plugin_gui *_gui, int _param_no)
1398 gui = _gui;
1399 param_no = _param_no;
1401 string key = attribs["key"];
1402 tmif = gui->plugin->get_metadata_iface()->get_table_metadata_iface(key.c_str());
1403 if (!tmif)
1405 g_error("Missing table_metadata_iface for variable '%s'", key.c_str());
1406 return NULL;
1408 positions.clear();
1409 const table_column_info *tci = tmif->get_table_columns();
1410 assert(tci);
1411 cols = 0;
1412 while (tci[cols].name != NULL)
1413 cols++;
1415 GType *p = new GType[cols];
1416 for (int i = 0; i < cols; i++)
1417 p[i] = G_TYPE_STRING;
1418 lstore = gtk_list_store_newv(cols, p);
1419 if (tmif->get_table_rows() != 0)
1420 set_rows(tmif->get_table_rows());
1421 widget = gtk_tree_view_new_with_model(GTK_TREE_MODEL(lstore));
1422 delete []p;
1423 tree = GTK_TREE_VIEW (widget);
1424 g_object_set (G_OBJECT (tree), "enable-search", FALSE, "rules-hint", TRUE, "enable-grid-lines", TRUE, NULL);
1426 for (int i = 0; i < cols; i++)
1428 GtkCellRenderer *cr = NULL;
1430 if (tci[i].type == TCT_ENUM) {
1431 cr = gtk_cell_renderer_combo_new ();
1432 GtkListStore *cls = gtk_list_store_new(2, G_TYPE_INT, G_TYPE_STRING);
1433 for (int j = 0; tci[i].values[j]; j++)
1434 gtk_list_store_insert_with_values(cls, NULL, j, 0, j, 1, tci[i].values[j], -1);
1435 g_object_set(cr, "model", cls, "editable", TRUE, "has-entry", FALSE, "text-column", 1, "mode", GTK_CELL_RENDERER_MODE_EDITABLE, NULL);
1437 else {
1438 bool editable = tci[i].type != TCT_LABEL;
1439 cr = gtk_cell_renderer_text_new ();
1440 if (editable)
1441 g_object_set(cr, "editable", TRUE, "mode", GTK_CELL_RENDERER_MODE_EDITABLE, NULL);
1443 g_object_set_data (G_OBJECT(cr), "column", (void *)&tci[i]);
1444 g_signal_connect (GTK_OBJECT (cr), "edited", G_CALLBACK (on_edited), (gpointer)this);
1445 g_signal_connect (GTK_OBJECT (cr), "editing-canceled", G_CALLBACK (on_editing_canceled), (gpointer)this);
1446 gtk_tree_view_insert_column_with_attributes(tree, i, tci[i].name, cr, "text", i, NULL);
1448 gtk_tree_view_set_headers_visible(tree, TRUE);
1449 gtk_widget_set_name(GTK_WIDGET(widget), "Calf-ListView");
1450 return widget;
1453 void listview_param_control::set_rows(unsigned int needed_rows)
1455 while(positions.size() < needed_rows)
1457 GtkTreeIter iter;
1458 gtk_list_store_insert(lstore, &iter, positions.size());
1459 for (int j = 0; j < cols; j++)
1461 gtk_list_store_set(lstore, &iter, j, "", -1);
1463 positions.push_back(iter);
1467 void listview_param_control::send_configure(const char *key, const char *value)
1469 string orig_key = attribs["key"] + ":";
1470 bool is_rows = false;
1471 int row = -1, col = -1;
1472 if (parse_table_key(key, orig_key.c_str(), is_rows, row, col))
1474 string suffix = string(key + orig_key.length());
1475 if (is_rows && tmif->get_table_rows() == 0)
1477 int rows = atoi(value);
1478 set_rows(rows);
1479 return;
1481 else
1482 if (row != -1 && col != -1)
1484 int max_rows = tmif->get_table_rows();
1485 if (col < 0 || col >= cols)
1487 g_warning("Invalid column %d in key %s", col, key);
1488 return;
1490 if (max_rows && (row < 0 || row >= max_rows))
1492 g_warning("Invalid row %d in key %s, this is a fixed table with row count = %d", row, key, max_rows);
1493 return;
1496 if (row >= (int)positions.size())
1497 set_rows(row + 1);
1499 gtk_list_store_set(lstore, &positions[row], col, value, -1);
1500 return;
1505 void listview_param_control::on_edited(GtkCellRenderer *renderer, gchar *path, gchar *new_text, listview_param_control *pThis)
1507 const table_column_info *tci = pThis->tmif->get_table_columns();
1508 int column = ((table_column_info *)g_object_get_data(G_OBJECT(renderer), "column")) - tci;
1509 string key = pThis->attribs["key"] + ":" + i2s(atoi(path)) + "," + i2s(column);
1510 string error;
1511 const char *error_or_null = pThis->gui->plugin->configure(key.c_str(), new_text);
1512 if (error_or_null)
1513 error = error_or_null;
1515 if (error.empty()) {
1516 pThis->send_configure(key.c_str(), new_text);
1517 gtk_widget_grab_focus(pThis->widget);
1518 GtkTreePath *gpath = gtk_tree_path_new_from_string (path);
1519 gtk_tree_view_set_cursor_on_cell (GTK_TREE_VIEW (pThis->widget), gpath, NULL, NULL, FALSE);
1520 gtk_tree_path_free (gpath);
1522 else
1524 GtkWidget *dialog = gtk_message_dialog_new(pThis->gui->window->toplevel, GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK,
1525 "%s", error.c_str());
1526 gtk_dialog_run(GTK_DIALOG(dialog));
1527 gtk_widget_destroy(dialog);
1528 gtk_widget_grab_focus(pThis->widget);
1532 void listview_param_control::on_editing_canceled(GtkCellRenderer *renderer, listview_param_control *pThis)
1534 gtk_widget_grab_focus(pThis->widget);
1537 /******************************** GtkNotebook control ********************************/
1539 GtkWidget *notebook_param_control::create(plugin_gui *_gui, int _param_no)
1541 gui = _gui;
1542 param_no = _param_no;
1543 page = 0;
1544 GtkWidget *nb = calf_notebook_new();
1545 widget = GTK_WIDGET(nb);
1546 container = GTK_CONTAINER(nb);
1547 gtk_widget_set_name(GTK_WIDGET(nb), "Calf-Notebook");
1548 g_signal_connect (GTK_OBJECT (widget), "switch-page", G_CALLBACK (notebook_page_changed), (gpointer)this);
1549 return nb;
1551 void notebook_param_control::get()
1553 if (param_no >= 0)
1554 gui->set_param_value(param_no, page, this);
1556 void notebook_param_control::set()
1558 if (param_no < 0)
1559 return;
1560 _GUARD_CHANGE_
1561 gtk_notebook_set_current_page(GTK_NOTEBOOK(widget), (gint)gui->plugin->get_param_value(param_no));
1563 void notebook_param_control::add(GtkWidget *w, control_base *base)
1565 gtk_notebook_append_page(GTK_NOTEBOOK(widget), w, gtk_label_new_with_mnemonic(base->attribs["page"].c_str()));
1567 void notebook_param_control::notebook_page_changed(GtkWidget *widget, GtkWidget *page, guint id, gpointer user)
1569 notebook_param_control *jhp = (notebook_param_control *)user;
1570 jhp->page = (int)id;
1571 jhp->get();
1574 /******************************** GtkTable container ********************************/
1576 GtkWidget *table_container::create(plugin_gui *_gui, const char *element, xml_attribute_map &attributes)
1578 require_int_attribute("rows");
1579 require_int_attribute("cols");
1580 int homog = get_int("homogeneous", 0);
1581 int sx = get_int("spacing-x", 2);
1582 int sy = get_int("spacing-y", 2);
1583 GtkWidget *table = gtk_table_new(get_int("rows", 1), get_int("cols", 1), false);
1584 if(homog > 0) {
1585 gtk_table_set_homogeneous(GTK_TABLE(table), TRUE);
1587 gtk_table_set_col_spacings(GTK_TABLE(table), sx);
1588 gtk_table_set_row_spacings(GTK_TABLE(table), sy);
1589 container = GTK_CONTAINER(table);
1590 gtk_widget_set_name(GTK_WIDGET(table), "Calf-Table");
1591 return table;
1594 void table_container::add(GtkWidget *widget, control_base *base)
1596 base->require_int_attribute("attach-x");
1597 base->require_int_attribute("attach-y");
1598 int x = base->get_int("attach-x"), y = base->get_int("attach-y");
1599 int w = base->get_int("attach-w", 1), h = base->get_int("attach-h", 1);
1600 int shrinkx = base->get_int("shrink-x", 0);
1601 int shrinky = base->get_int("shrink-y", 0);
1602 int fillx = (base->get_int("fill-x", !shrinkx) ? GTK_FILL : 0) | (base->get_int("expand-x", !shrinkx) ? GTK_EXPAND : 0) | (shrinkx ? GTK_SHRINK : 0);
1603 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);
1604 int padx = base->get_int("pad-x", 2);
1605 int pady = base->get_int("pad-y", 2);
1606 gtk_table_attach(GTK_TABLE(container), widget, x, x + w, y, y + h, (GtkAttachOptions)fillx, (GtkAttachOptions)filly, padx, pady);
1609 /******************************** alignment container ********************************/
1611 GtkWidget *alignment_container::create(plugin_gui *_gui, const char *element, xml_attribute_map &attributes)
1613 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));
1614 container = GTK_CONTAINER(align);
1615 gtk_widget_set_name(GTK_WIDGET(align), "Calf-Align");
1616 return align;
1619 /******************************** GtkFrame container ********************************/
1621 GtkWidget *frame_container::create(plugin_gui *_gui, const char *element, xml_attribute_map &attributes)
1623 GtkWidget *widget = calf_frame_new(attribs["label"].c_str());
1624 container = GTK_CONTAINER(widget);
1625 gtk_widget_set_name(GTK_WIDGET(widget), "Calf-Frame");
1626 return widget;
1629 /******************************** GtkBox type of containers ********************************/
1631 void box_container::add(GtkWidget *w, control_base *base)
1633 gtk_container_add_with_properties(container, w, "expand", get_int("expand", 1), "fill", get_int("fill", 1), NULL);
1636 /******************************** GtkHBox container ********************************/
1638 GtkWidget *hbox_container::create(plugin_gui *_gui, const char *element, xml_attribute_map &attributes)
1640 GtkWidget *hbox = gtk_hbox_new(get_int("homogeneous") >= 1, get_int("spacing", 2));
1641 container = GTK_CONTAINER(hbox);
1642 gtk_widget_set_name(GTK_WIDGET(hbox), "Calf-HBox");
1643 return hbox;
1646 /******************************** GtkVBox container ********************************/
1648 GtkWidget *vbox_container::create(plugin_gui *_gui, const char *element, xml_attribute_map &attributes)
1650 GtkWidget *vbox = gtk_vbox_new(get_int("homogeneous") >= 1, get_int("spacing", 2));
1651 container = GTK_CONTAINER(vbox);
1652 gtk_widget_set_name(GTK_WIDGET(vbox), "Calf-VBox");
1653 return vbox;
1656 /******************************** GtkNotebook container ********************************/
1658 GtkWidget *scrolled_container::create(plugin_gui *_gui, const char *element, xml_attribute_map &attributes)
1660 GtkAdjustment *horiz = NULL, *vert = NULL;
1661 int width = get_int("width", 0), height = get_int("height", 0);
1662 if (width)
1663 horiz = GTK_ADJUSTMENT(gtk_adjustment_new(get_int("x", 0), 0, width, get_int("step-x", 1), get_int("page-x", width / 10), 100));
1664 if (height)
1665 vert = GTK_ADJUSTMENT(gtk_adjustment_new(get_int("y", 0), 0, width, get_int("step-y", 1), get_int("page-y", height / 10), 10));
1666 GtkWidget *sw = gtk_scrolled_window_new(horiz, vert);
1667 gtk_widget_set_size_request(sw, get_int("req-x", -1), get_int("req-y", -1));
1668 container = GTK_CONTAINER(sw);
1669 gtk_widget_set_name(GTK_WIDGET(sw), "Calf-ScrolledWindow");
1670 return sw;
1673 void scrolled_container::add(GtkWidget *w, control_base *base)
1675 gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(container), w);