LV2: less heavy-handed approach to lifecycle issues
[calf.git] / src / gui_controls.cpp
blobdb0705012d5e09b0f43bef14ae67cd1d784738a4
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 void control_base::set_visibilty(bool state)
88 if (state) {
89 if (is_container())
90 gtk_widget_show(widget);
91 else
92 gtk_widget_show(GTK_WIDGET(container));
93 } else {
94 if (is_container())
95 gtk_widget_hide(widget);
96 else
97 gtk_widget_hide(GTK_WIDGET(container));
100 /******************************** container base class **********************/
102 void control_container::set_std_properties()
104 if (attribs.find("widget-name") != attribs.end())
106 string name = attribs["widget-name"];
107 if (container) {
108 gtk_widget_set_name(GTK_WIDGET(container), name.c_str());
113 /************************* param-associated control base class **************/
115 param_control::param_control()
117 gui = NULL;
118 param_no = -1;
119 in_change = 0;
120 old_displayed_value = -1.f;
121 has_entry = false;
125 void param_control::set_std_properties()
127 if (attribs.find("widget-name") != attribs.end())
129 string name = attribs["widget-name"];
130 if (widget) {
131 gtk_widget_set_name(widget, name.c_str());
136 void param_control::hook_params()
138 if (param_no != -1) {
139 gui->add_param_ctl(param_no, this);
141 gui->params.push_back(this);
144 param_control::~param_control()
146 if (GTK_IS_WIDGET(widget))
147 gtk_widget_destroy(widget);
150 void param_control::add_context_menu_handler()
152 if (widget)
154 g_signal_connect(GTK_OBJECT(widget), "button-press-event", (GCallback)on_button_press_event, this);
158 gboolean param_control::on_button_press_event(GtkWidget *widget, GdkEventButton *event, void *user_data)
160 param_control *self = (param_control *)user_data;
161 const parameter_properties &props = self->get_props();
162 if (event->button == 3 && !(props.flags & PF_PROP_OUTPUT))
164 self->do_popup_menu();
165 return TRUE;
167 else if (event->button == 2)
169 self->create_value_entry(widget, event->x_root, event->y_root);
170 return TRUE;
172 return FALSE;
175 void param_control::do_popup_menu()
177 if (gui)
178 gui->on_control_popup(this, param_no);
181 void param_control::destroy_value_entry ()
183 // remove the window containing the entry
184 gtk_widget_destroy(GTK_WIDGET(entrywin));
185 has_entry = false;
187 gboolean param_control::value_entry_unfocus(GtkWidget *widget, GdkEventFocus *event, void *user_data)
189 // destroy window if it looses focus
190 param_control *self = (param_control *)user_data;
191 self->destroy_value_entry();
192 return TRUE;
194 gboolean param_control::value_entry_action(GtkEntry *widget, GdkEvent *event, void *user_data)
196 // called when a key was hit, sorts out and treats RETURN and ESC
197 param_control *self = (param_control *)user_data;
198 const parameter_properties &props = self->get_props();
199 GdkEventKey *key = (GdkEventKey*)event;
200 if(key->keyval == GDK_Escape)
201 self->destroy_value_entry();
202 else if (key->keyval == GDK_Return) {
203 float val = props.string_to_value(gtk_entry_get_text(widget));
204 self->gui->plugin->set_param_value(self->param_no, val);
205 self->set();
206 self->destroy_value_entry();
208 return FALSE;
210 void param_control::create_value_entry(GtkWidget *widget, int x, int y)
212 if (has_entry) {
213 // kill an existing entry window on re-trigger
214 destroy_value_entry();
215 return;
218 if (param_no < 0)
219 return;
221 const parameter_properties &props = get_props();
222 float value = gui->plugin->get_param_value(param_no);
224 // no chance for a menu, so we have to do everything by hand
225 entrywin = gtk_window_new(GTK_WINDOW_TOPLEVEL);
226 gtk_widget_set_name(GTK_WIDGET(entrywin), "Calf-Value-Entry");
227 gtk_window_set_title (GTK_WINDOW(entrywin), "Calf Value Entry");
228 gtk_window_set_resizable (GTK_WINDOW(entrywin), FALSE);
229 gtk_window_set_decorated (GTK_WINDOW(entrywin), FALSE);
230 gtk_window_set_skip_taskbar_hint (GTK_WINDOW(entrywin), TRUE);
231 gtk_window_set_skip_pager_hint (GTK_WINDOW(entrywin), TRUE);
232 gtk_window_set_transient_for (GTK_WINDOW(entrywin), GTK_WINDOW (gui->window->toplevel));
233 gtk_window_set_gravity(GTK_WINDOW(entrywin), GDK_GRAVITY_CENTER);
234 gtk_widget_set_events (GTK_WIDGET(entrywin), GDK_FOCUS_CHANGE_MASK);
235 g_signal_connect (G_OBJECT(entrywin), "focus-out-event", G_CALLBACK (value_entry_unfocus), this);
237 // create the text entry
238 GtkWidget *entry = gtk_entry_new();
239 gtk_widget_set_name(GTK_WIDGET(entry), "Calf-Entry");
240 gtk_entry_set_width_chars(GTK_ENTRY(entry), props.get_char_count());
241 gtk_entry_set_text(GTK_ENTRY(entry), props.to_string(value).c_str());
242 gtk_widget_add_events (entry, GDK_KEY_PRESS_MASK);
243 g_signal_connect (entry, "key-press-event", (GCallback)value_entry_action, this);
245 // stitch together and show
246 gtk_container_add(GTK_CONTAINER (entrywin), entry);
247 gtk_widget_show_all(entrywin);
248 gtk_window_move(GTK_WINDOW (entrywin), x, y);
250 has_entry = true;
254 /******************************** Combo Box ********************************/
256 GtkWidget *combo_box_param_control::create(plugin_gui *_gui, int _param_no)
258 gui = _gui;
259 param_no = _param_no;
260 lstore = gtk_list_store_new(2, G_TYPE_STRING, G_TYPE_STRING); // value, key
262 const parameter_properties &props = get_props();
263 widget = calf_combobox_new ();
264 if (param_no != -1 && props.choices)
266 for (int j = (int)props.min; j <= (int)props.max; j++)
267 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);
269 gtk_combo_box_set_model (GTK_COMBO_BOX(widget), GTK_TREE_MODEL(lstore));
270 g_signal_connect (GTK_OBJECT (widget), "changed", G_CALLBACK (combo_value_changed), (gpointer)this);
271 gtk_widget_set_name(GTK_WIDGET(widget), "Calf-Combobox");
272 return widget;
275 void combo_box_param_control::set()
277 _GUARD_CHANGE_
278 if (param_no != -1)
280 const parameter_properties &props = get_props();
281 gtk_combo_box_set_active (GTK_COMBO_BOX (widget), (int)gui->plugin->get_param_value(param_no) - (int)props.min);
285 void combo_box_param_control::get()
287 if (param_no != -1)
289 const parameter_properties &props = get_props();
290 gui->set_param_value(param_no, gtk_combo_box_get_active (GTK_COMBO_BOX(widget)) + props.min, this);
294 void combo_box_param_control::combo_value_changed(GtkComboBox *widget, gpointer value)
296 combo_box_param_control *jhp = (combo_box_param_control *)value;
297 if (jhp->attribs.count("setter-key"))
299 GtkTreeIter iter;
300 gchar *key = NULL;
301 if (gtk_combo_box_get_active_iter (GTK_COMBO_BOX (jhp->widget), &iter))
303 gtk_tree_model_get (GTK_TREE_MODEL (jhp->lstore), &iter, 1, &key, -1);
304 if (key) {
305 jhp->gui->plugin->configure(jhp->attribs["setter-key"].c_str(), key);
306 free(key);
310 else
311 jhp->get();
314 void combo_box_param_control::send_status(const char *key, const char *value)
316 if (attribs.count("key") && key == attribs["key"])
318 gtk_list_store_clear (lstore);
319 key2pos.clear();
320 std::string v = value;
321 int i = 0;
322 size_t pos = 0;
323 while (pos < v.length()) {
324 size_t endpos = v.find("\n", pos);
325 if (endpos == string::npos)
326 break;
327 string line = v.substr(pos, endpos - pos);
328 string key, label;
329 size_t tabpos = line.find('\t');
330 if (tabpos == string::npos)
331 key = label = line;
332 else {
333 key = line.substr(0, tabpos);
334 label = line.substr(tabpos + 1);
336 GtkTreeIter gti;
337 gtk_list_store_insert_with_values (lstore, &gti, i, 0, label.c_str(), 1, key.c_str(), -1);
338 key2pos[key] = gti;
339 pos = endpos + 1;
340 i++;
342 set_to_last_key();
344 if (attribs.count("current-key") && key == attribs["current-key"])
346 last_key = value;
347 set_to_last_key();
351 void combo_box_param_control::set_to_last_key()
353 map<string, GtkTreeIter>::iterator i = key2pos.find(last_key);
354 if (i != key2pos.end())
356 gtk_combo_box_set_active_iter (GTK_COMBO_BOX (widget), &i->second);
359 else
360 gtk_combo_box_set_active (GTK_COMBO_BOX (widget), -1);
363 /******************************** Horizontal Fader ********************************/
365 static gboolean
366 scale_to_default (gpointer data)
368 hscale_param_control *jhp = (hscale_param_control *)data;
369 const parameter_properties &props = jhp->get_props();
370 gtk_range_set_value (GTK_RANGE (jhp->widget), props.to_01(props.def_value));
372 return FALSE;
375 static gboolean
376 scale_button_press (GtkWidget *widget, GdkEventKey *event, gpointer *user_data)
378 if (event->type == GDK_2BUTTON_PRESS) {
379 // this actually creates a harmless race condition, but diving deep
380 // into gtk signal handling code wouldn't and the resulting complexity
381 // would not really be worth the effort
382 // The timeout is set high enough that most of the time the race
383 // will turn out in our/the users favor
384 g_timeout_add (200, (GSourceFunc)scale_to_default, user_data);
385 return TRUE;
388 return FALSE;
391 GtkWidget *hscale_param_control::create(plugin_gui *_gui, int _param_no)
393 gui = _gui;
394 param_no = _param_no;
396 widget = calf_fader_new(1, get_int("size", 2), 0, 1, get_props().get_increment());
398 g_signal_connect (GTK_OBJECT (widget), "value-changed", G_CALLBACK (hscale_value_changed), (gpointer)this);
399 g_signal_connect (GTK_OBJECT (widget), "format-value", G_CALLBACK (hscale_format_value), (gpointer)this);
400 g_signal_connect (GTK_OBJECT (widget), "button-press-event", G_CALLBACK (scale_button_press), (gpointer)this);
402 if(get_int("inverted", 0) > 0) {
403 gtk_range_set_inverted(GTK_RANGE(widget), TRUE);
405 int size = get_int("size", 2);
406 char *name = g_strdup_printf("Calf-HScale%i", size);
407 gtk_widget_set_name(GTK_WIDGET(widget), name);
408 gtk_widget_set_size_request (widget, size * 100, -1);
409 g_free(name);
410 return widget;
413 void hscale_param_control::init_xml(const char *element)
415 if (attribs.count("width"))
416 gtk_widget_set_size_request (widget, get_int("width", 200), -1);
417 if (attribs.count("position"))
419 string v = attribs["position"];
420 if (v == "top") gtk_scale_set_value_pos(GTK_SCALE(widget), GTK_POS_TOP);
421 if (v == "bottom") gtk_scale_set_value_pos(GTK_SCALE(widget), GTK_POS_BOTTOM);
425 void hscale_param_control::set()
427 _GUARD_CHANGE_
428 const parameter_properties &props = get_props();
429 gtk_range_set_value (GTK_RANGE (widget), props.to_01 (gui->plugin->get_param_value(param_no)));
430 // hscale_value_changed (GTK_HSCALE (widget), (gpointer)this);
433 void hscale_param_control::get()
435 const parameter_properties &props = get_props();
436 float cvalue = props.from_01 (gtk_range_get_value (GTK_RANGE (widget)));
437 gui->set_param_value(param_no, cvalue, this);
440 void hscale_param_control::hscale_value_changed(GtkHScale *widget, gpointer value)
442 hscale_param_control *jhp = (hscale_param_control *)value;
443 jhp->get();
446 gchar *hscale_param_control::hscale_format_value(GtkScale *widget, double arg1, gpointer value)
448 hscale_param_control *jhp = (hscale_param_control *)value;
449 const parameter_properties &props = jhp->get_props();
450 float cvalue = props.from_01 (arg1);
452 // for testing
453 // return g_strdup_printf ("%s = %g", props.to_string (cvalue).c_str(), arg1);
454 return g_strdup (props.to_string (cvalue).c_str());
457 /******************************** Vertical Fader ********************************/
459 GtkWidget *vscale_param_control::create(plugin_gui *_gui, int _param_no)
461 gui = _gui;
462 param_no = _param_no;
463 widget = calf_fader_new(0, get_int("size", 2), 0, 1, get_props().get_increment());
464 g_signal_connect (GTK_OBJECT (widget), "value-changed", G_CALLBACK (vscale_value_changed), (gpointer)this);
465 g_signal_connect (GTK_OBJECT (widget), "button-press-event", G_CALLBACK (scale_button_press), (gpointer)this);
467 gtk_scale_set_draw_value(GTK_SCALE(widget), FALSE);
469 if(get_int("inverted", 0) > 0) {
470 gtk_range_set_inverted(GTK_RANGE(widget), TRUE);
472 int size = get_int("size", 2);
473 char *name = g_strdup_printf("Calf-VScale%i", size);
474 gtk_widget_set_size_request (widget, -1, size * 100);
475 gtk_widget_set_name(GTK_WIDGET(widget), name);
476 g_free(name);
477 return widget;
480 void vscale_param_control::init_xml(const char *element)
482 if (attribs.count("height"))
483 gtk_widget_set_size_request (widget, -1, get_int("height", 200));
486 void vscale_param_control::set()
488 _GUARD_CHANGE_
489 const parameter_properties &props = get_props();
490 gtk_range_set_value (GTK_RANGE (widget), props.to_01 (gui->plugin->get_param_value(param_no)));
491 // vscale_value_changed (GTK_HSCALE (widget), (gpointer)this);
494 void vscale_param_control::get()
496 const parameter_properties &props = get_props();
497 float cvalue = props.from_01 (gtk_range_get_value (GTK_RANGE (widget)));
498 gui->set_param_value(param_no, cvalue, this);
501 void vscale_param_control::vscale_value_changed(GtkHScale *widget, gpointer value)
503 vscale_param_control *jhp = (vscale_param_control *)value;
504 jhp->get();
507 /******************************** Label ********************************/
509 GtkWidget *label_param_control::create(plugin_gui *_gui, int _param_no)
511 gui = _gui, param_no = _param_no;
512 string text;
513 if (param_no != -1 && !attribs.count("text"))
514 text = get_props().name;
515 else
516 text = attribs["text"];
517 widget = gtk_label_new(text.c_str());
518 gtk_misc_set_alignment (GTK_MISC (widget), get_float("align-x", 0.5), get_float("align-y", 0.5));
519 gtk_widget_set_name(GTK_WIDGET(widget), "Calf-Label");
520 return widget;
523 /******************************** Value ********************************/
525 GtkWidget *value_param_control::create(plugin_gui *_gui, int _param_no)
527 gui = _gui;
528 param_no = _param_no;
530 widget = gtk_label_new ("");
531 if (param_no != -1)
533 const parameter_properties &props = get_props();
534 int width = get_int("width", 0);
535 gtk_label_set_width_chars (GTK_LABEL (widget),
536 width ? width : props.get_char_count());
538 else
540 require_attribute("key");
541 require_int_attribute("width");
542 param_variable = attribs["key"];
543 gtk_label_set_width_chars (GTK_LABEL (widget), get_int("width"));
545 gtk_misc_set_alignment (GTK_MISC (widget), get_float("align-x", 0.5), get_float("align-y", 0.5));
546 gtk_widget_set_name(GTK_WIDGET(widget), "Calf-Value");
547 return widget;
550 void value_param_control::set()
552 if (param_no == -1)
553 return;
554 _GUARD_CHANGE_
556 const parameter_properties &props = get_props();
557 string value = props.to_string(gui->plugin->get_param_value(param_no));
559 if (value == old_value)
560 return;
561 old_value = value;
562 gtk_label_set_text (GTK_LABEL (widget), value.c_str());
565 void value_param_control::send_status(const char *key, const char *value)
567 if (key == param_variable)
569 gtk_label_set_text (GTK_LABEL (widget), value);
573 /******************************** VU Meter ********************************/
575 GtkWidget *vumeter_param_control::create(plugin_gui *_gui, int _param_no)
577 gui = _gui, param_no = _param_no;
578 // const parameter_properties &props = get_props();
579 widget = calf_vumeter_new ();
580 gtk_widget_set_name(GTK_WIDGET(widget), "calf-vumeter");
581 calf_vumeter_set_mode (CALF_VUMETER (widget), (CalfVUMeterMode)get_int("mode", 0));
582 CALF_VUMETER(widget)->vumeter_hold = get_float("hold", 0);
583 CALF_VUMETER(widget)->vumeter_falloff = get_float("falloff", 0.f);
584 CALF_VUMETER(widget)->vumeter_width = get_int("width", 80);
585 CALF_VUMETER(widget)->vumeter_height = get_int("height", 18);
586 CALF_VUMETER(widget)->vumeter_position = get_int("position", 0);
587 gtk_widget_set_name(GTK_WIDGET(widget), "Calf-VUMeter");
588 return widget;
591 void vumeter_param_control::set()
593 _GUARD_CHANGE_
594 // const parameter_properties &props = get_props();
595 calf_vumeter_set_value (CALF_VUMETER (widget), gui->plugin->get_param_value(param_no));
598 // LED
600 GtkWidget *led_param_control::create(plugin_gui *_gui, int _param_no)
602 gui = _gui, param_no = _param_no;
603 // const parameter_properties &props = get_props();
604 widget = calf_led_new ();
605 gtk_widget_set_name(GTK_WIDGET(widget), "calf-led");
606 CALF_LED(widget)->led_mode = get_int("mode", 0);
607 CALF_LED(widget)->size = get_int("size", 1);
608 gtk_widget_set_name(GTK_WIDGET(widget), "Calf-LED");
609 return widget;
612 void led_param_control::set()
614 _GUARD_CHANGE_
615 // const parameter_properties &props = get_props();
616 calf_led_set_value (CALF_LED (widget), gui->plugin->get_param_value(param_no));
619 // tube
621 GtkWidget *tube_param_control::create(plugin_gui *_gui, int _param_no)
623 gui = _gui, param_no = _param_no;
624 // const parameter_properties &props = get_props();
625 widget = calf_tube_new ();
626 gtk_widget_set_name(GTK_WIDGET(widget), "calf-tube");
627 CALF_TUBE(widget)->size = get_int("size", 2);
628 CALF_TUBE(widget)->direction = get_int("direction", 2);
629 gtk_widget_set_name(GTK_WIDGET(widget), "Calf-Tube");
630 return widget;
633 void tube_param_control::set()
635 _GUARD_CHANGE_
636 // const parameter_properties &props = get_props();
637 calf_tube_set_value (CALF_TUBE (widget), gui->plugin->get_param_value(param_no));
640 /******************************** Check Box ********************************/
642 GtkWidget *check_param_control::create(plugin_gui *_gui, int _param_no)
644 gui = _gui;
645 param_no = _param_no;
647 widget = gtk_check_button_new ();
648 g_signal_connect (GTK_OBJECT (widget), "toggled", G_CALLBACK (check_value_changed), (gpointer)this);
649 gtk_widget_set_name(GTK_WIDGET(widget), "Calf-Checkbox");
650 return widget;
653 void check_param_control::check_value_changed(GtkCheckButton *widget, gpointer value)
655 param_control *jhp = (param_control *)value;
656 jhp->get();
659 void check_param_control::get()
661 const parameter_properties &props = get_props();
662 gui->set_param_value(param_no, gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON(widget)) + props.min, this);
665 void check_param_control::set()
667 _GUARD_CHANGE_
668 const parameter_properties &props = get_props();
669 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (widget), (int)gui->plugin->get_param_value(param_no) - (int)props.min);
672 /******************************** Radio Button ********************************/
674 GtkWidget *radio_param_control::create(plugin_gui *_gui, int _param_no)
676 gui = _gui;
677 param_no = _param_no;
678 require_attribute("value");
679 value = -1;
680 string value_name = attribs["value"];
681 const parameter_properties &props = get_props();
682 if (props.choices && (value_name < "0" || value_name > "9"))
684 for (int i = 0; props.choices[i]; i++)
686 if (value_name == props.choices[i])
688 value = i + (int)props.min;
689 break;
693 if (value == -1)
694 value = get_int("value");
696 if (attribs.count("label"))
697 widget = gtk_radio_button_new_with_label (gui->get_radio_group(param_no), attribs["label"].c_str());
698 else
699 widget = gtk_radio_button_new_with_label (gui->get_radio_group(param_no), props.choices[value - (int)props.min]);
700 gtk_toggle_button_set_mode (GTK_TOGGLE_BUTTON (widget), FALSE);
702 gui->set_radio_group(param_no, gtk_radio_button_get_group (GTK_RADIO_BUTTON (widget)));
703 g_signal_connect (GTK_OBJECT (widget), "clicked", G_CALLBACK (radio_clicked), (gpointer)this);
704 gtk_widget_set_name(GTK_WIDGET(widget), "Calf-RadioButton");
705 return widget;
708 void radio_param_control::radio_clicked(GtkRadioButton *widget, gpointer value)
710 param_control *jhp = (param_control *)value;
711 jhp->get();
714 void radio_param_control::get()
716 // const parameter_properties &props = get_props();
717 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget)))
718 gui->set_param_value(param_no, value, this);
721 void radio_param_control::set()
723 _GUARD_CHANGE_
724 const parameter_properties &props = get_props();
725 float pv = gui->plugin->get_param_value(param_no);
726 if (fabs(value-pv) < 0.5)
727 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (widget), value == ((int)gui->plugin->get_param_value(param_no) - (int)props.min));
730 /******************************** Spin Button ********************************/
732 GtkWidget *spin_param_control::create(plugin_gui *_gui, int _param_no)
734 gui = _gui;
735 param_no = _param_no;
737 const parameter_properties &props = get_props();
738 if (props.step > 1)
739 widget = gtk_spin_button_new_with_range (props.min, props.max, (props.max - props.min) / (props.step - 1));
740 if (props.step > 0)
741 widget = gtk_spin_button_new_with_range (props.min, props.max, props.step);
742 else
743 widget = gtk_spin_button_new_with_range (props.min, props.max, 1);
744 gtk_spin_button_set_digits (GTK_SPIN_BUTTON(widget), get_int("digits", 0));
745 g_signal_connect (GTK_OBJECT (widget), "value-changed", G_CALLBACK (value_changed), (gpointer)this);
746 gtk_widget_set_name(GTK_WIDGET(widget), "Calf-SpinButton");
747 return widget;
750 void spin_param_control::value_changed(GtkSpinButton *widget, gpointer value)
752 param_control *jhp = (param_control *)value;
753 jhp->get();
756 void spin_param_control::get()
758 // const parameter_properties &props = get_props();
759 gui->set_param_value(param_no, gtk_spin_button_get_value_as_float (GTK_SPIN_BUTTON (widget)), this);
762 void spin_param_control::set()
764 _GUARD_CHANGE_
765 // const parameter_properties &props = get_props();
766 gtk_spin_button_set_value (GTK_SPIN_BUTTON (widget), gui->plugin->get_param_value(param_no));
769 /******************************** Button ********************************/
771 GtkWidget *button_param_control::create(plugin_gui *_gui, int _param_no)
773 gui = _gui;
774 param_no = _param_no;
775 widget = calf_button_new ((gchar*)get_props().name);
776 g_signal_connect (GTK_OBJECT (widget), "pressed", G_CALLBACK (button_clicked), (gpointer)this);
777 g_signal_connect (GTK_OBJECT (widget), "released", G_CALLBACK (button_clicked), (gpointer)this);
778 gtk_widget_set_name(GTK_WIDGET(widget), "Calf-Button");
779 return widget;
782 void button_param_control::button_clicked(GtkButton *widget, gpointer value)
784 param_control *jhp = (param_control *)value;
785 jhp->get();
788 void button_param_control::get()
790 const parameter_properties &props = get_props();
791 gui->set_param_value(param_no, gtk_widget_get_state(widget) == GTK_STATE_ACTIVE ? props.max : props.min, this);
794 void button_param_control::set()
796 _GUARD_CHANGE_
797 const parameter_properties &props = get_props();
798 if (gui->plugin->get_param_value(param_no) - props.min >= 0.5)
799 gtk_button_clicked (GTK_BUTTON (widget));
802 /******************************** Knob ********************************/
804 GtkWidget *knob_param_control::create(plugin_gui *_gui, int _param_no)
806 gui = _gui;
807 param_no = _param_no;
808 const parameter_properties &props = get_props();
810 //widget = calf_knob_new_with_range (props.to_01 (gui->plugin->get_param_value(param_no)), 0, 1, 0.01);
811 widget = calf_knob_new();
812 float increment = props.get_increment();
813 gtk_range_get_adjustment(GTK_RANGE(widget))->step_increment = increment;
814 CALF_KNOB(widget)->default_value = props.to_01(props.def_value);
815 CALF_KNOB(widget)->knob_type = get_int("type");
816 CALF_KNOB(widget)->knob_size = get_int("size", 2);
817 if(CALF_KNOB(widget)->knob_size > 5) {
818 CALF_KNOB(widget)->knob_size = 5;
819 } else if (CALF_KNOB(widget)->knob_size < 1) {
820 CALF_KNOB(widget)->knob_size = 1;
822 g_signal_connect(GTK_OBJECT(widget), "value-changed", G_CALLBACK(knob_value_changed), (gpointer)this);
823 gtk_widget_set_name(GTK_WIDGET(widget), "Calf-Knob");
824 return widget;
827 void knob_param_control::get()
829 const parameter_properties &props = get_props();
830 float value = props.from_01(gtk_range_get_value(GTK_RANGE(widget)));
831 gui->set_param_value(param_no, value, this);
834 void knob_param_control::set()
836 _GUARD_CHANGE_
837 const parameter_properties &props = get_props();
838 gtk_range_set_value(GTK_RANGE(widget), props.to_01 (gui->plugin->get_param_value(param_no)));
841 void knob_param_control::knob_value_changed(GtkWidget *widget, gpointer value)
843 param_control *jhp = (param_control *)value;
844 jhp->get();
847 /******************************** Toggle Button ********************************/
849 GtkWidget *toggle_param_control::create(plugin_gui *_gui, int _param_no)
851 gui = _gui;
852 param_no = _param_no;
853 widget = calf_toggle_new ();
855 CALF_TOGGLE(widget)->size = get_int("size", 2);
857 g_signal_connect (GTK_OBJECT (widget), "value-changed", G_CALLBACK (toggle_value_changed), (gpointer)this);
858 gtk_widget_set_name(GTK_WIDGET(widget), "Calf-ToggleButton");
859 return widget;
862 void toggle_param_control::get()
864 const parameter_properties &props = get_props();
865 float value = props.from_01(gtk_range_get_value(GTK_RANGE(widget)));
866 gui->set_param_value(param_no, value, this);
869 void toggle_param_control::set()
871 _GUARD_CHANGE_
872 const parameter_properties &props = get_props();
873 gtk_range_set_value(GTK_RANGE(widget), props.to_01 (gui->plugin->get_param_value(param_no)));
876 void toggle_param_control::toggle_value_changed(GtkWidget *widget, gpointer value)
878 param_control *jhp = (param_control *)value;
879 jhp->get();
882 /******************************** Tap Button ********************************/
884 GtkWidget *tap_button_param_control::create(plugin_gui *_gui, int _param_no)
886 gui = _gui;
887 param_no = _param_no;
888 last_time = 0;
889 init_time = 0;
890 avg_value = 0;
891 value = 0;
892 widget = calf_tap_button_new ();
893 //CALF_TAP(widget)->size = get_int("size", 2);
894 g_signal_connect (GTK_OBJECT (widget), "button-press-event", G_CALLBACK (tap_button_pressed), (gpointer)this);
895 g_signal_connect (GTK_OBJECT (widget), "released", G_CALLBACK (tap_button_released), (gpointer)this);
896 g_signal_connect (GTK_OBJECT (widget), "leave", G_CALLBACK (tap_button_released), (gpointer)this);
897 gtk_widget_set_name(GTK_WIDGET(widget), "Calf-TapButton");
898 return widget;
901 void tap_button_param_control::get()
903 gui->set_param_value(param_no, value, this);
906 void tap_button_param_control::set()
908 _GUARD_CHANGE_
909 if(last_time) {
910 timeval tv;
911 gettimeofday(&tv, 0);
912 unsigned long _now = tv.tv_sec * 1000;
913 if(_now > init_time + 2000) {
914 // user stopped tapping
915 avg_value = 0;
916 last_time = 0;
917 init_time = 0;
918 CALF_TAP_BUTTON(widget)->state = 0;
919 gtk_widget_queue_draw(widget);
924 gboolean tap_button_param_control::tap_button_pressed(GtkWidget *widget, GdkEventButton *event, gpointer value)
926 tap_button_param_control *ctl = (tap_button_param_control *)value;
927 CalfTapButton *tap = CALF_TAP_BUTTON(widget);
929 guint time = 0;
930 if (event->type == GDK_BUTTON_PRESS and event->button == 1)
932 timeval tv;
933 gettimeofday(&tv, 0);
934 ctl->init_time = tv.tv_sec * 1000;
935 time = event->time;
936 tap->state = 2;
938 if(ctl->last_time) {
939 if(ctl->avg_value)
940 ctl->avg_value = (ctl->avg_value * 3 + (time - ctl->last_time)) / 4.f;
941 else
942 ctl->avg_value = time - ctl->last_time;
943 ctl->value = 60.f / (float)(ctl->avg_value / 1000.f);
945 if (ctl->value > 30 and ctl->value < 300)
946 ctl->get();
948 ctl->last_time = time;
949 gtk_widget_queue_draw(widget);
951 return FALSE;
953 gboolean tap_button_param_control::tap_button_released(GtkWidget *widget, gpointer value)
955 tap_button_param_control *ctl = (tap_button_param_control *)value;
956 CalfTapButton *tap = CALF_TAP_BUTTON(widget);
957 tap->state = ctl->last_time ? 1 : 0;
958 gtk_widget_queue_draw(widget);
959 return FALSE;
962 /******************************** Keyboard ********************************/
964 GtkWidget *keyboard_param_control::create(plugin_gui *_gui, int _param_no)
966 gui = _gui;
967 param_no = _param_no;
968 // const parameter_properties &props = get_props();
970 widget = calf_keyboard_new();
971 kb = CALF_KEYBOARD(widget);
972 kb->nkeys = get_int("octaves", 4) * 7 + 1;
973 kb->sink = new CalfKeyboard::EventAdapter;
974 gtk_widget_set_name(GTK_WIDGET(widget), "Calf-Keyboard");
975 return widget;
978 /******************************** Curve ********************************/
980 struct curve_param_control_callback: public CalfCurve::EventAdapter
982 curve_param_control *ctl;
984 curve_param_control_callback(curve_param_control *_ctl)
985 : ctl(_ctl) {}
987 virtual void curve_changed(CalfCurve *src, const CalfCurve::point_vector &data) {
988 stringstream ss;
989 ss << data.size() << endl;
990 for (size_t i = 0; i < data.size(); i++)
991 ss << data[i].first << " " << data[i].second << endl;
992 ctl->gui->plugin->configure(ctl->attribs["key"].c_str(), ss.str().c_str());
994 virtual void clip(CalfCurve *src, int pt, float &x, float &y, bool &hide)
996 // int gridpt = floor(x * 71 * 2);
997 // clip to the middle of the nearest white key
998 x = (floor(x * 71) + 0.5)/ 71.0;
1002 GtkWidget *curve_param_control::create(plugin_gui *_gui, int _param_no)
1004 gui = _gui;
1005 param_no = _param_no;
1006 require_attribute("key");
1008 widget = calf_curve_new(get_int("maxpoints", -1));
1009 curve = CALF_CURVE(widget);
1010 curve->sink = new curve_param_control_callback(this);
1011 // gtk_curve_set_curve_type(curve, GTK_CURVE_TYPE_LINEAR);
1012 gtk_widget_set_name(GTK_WIDGET(widget), "Calf-Curve");
1013 return widget;
1016 void curve_param_control::send_configure(const char *key, const char *value)
1018 // cout << "send conf " << key << endl;
1019 if (attribs["key"] == key)
1021 stringstream ss(value);
1022 CalfCurve::point_vector pts;
1023 if (*value)
1025 unsigned int npoints = 0;
1026 ss >> npoints;
1027 unsigned int i;
1028 float x = 0, y = 0;
1029 for (i = 0; i < npoints && i < curve->point_limit; i++)
1031 ss >> x >> y;
1032 pts.push_back(CalfCurve::point(x, y));
1034 calf_curve_set_points(widget, pts);
1039 /******************************** Entry ********************************/
1041 GtkWidget *entry_param_control::create(plugin_gui *_gui, int _param_no)
1043 gui = _gui;
1044 param_no = _param_no;
1045 require_attribute("key");
1047 widget = gtk_entry_new();
1048 entry = GTK_ENTRY(widget);
1049 g_signal_connect(GTK_OBJECT(widget), "changed", G_CALLBACK(entry_value_changed), (gpointer)this);
1050 gtk_editable_set_editable(GTK_EDITABLE(entry), get_int("editable", 1));
1051 gtk_widget_set_name(GTK_WIDGET(widget), "Calf-Entry");
1052 return widget;
1055 void entry_param_control::send_configure(const char *key, const char *value)
1057 // cout << "send conf " << key << endl;
1058 if (attribs["key"] == key)
1060 gtk_entry_set_text(entry, value);
1064 void entry_param_control::entry_value_changed(GtkWidget *widget, gpointer value)
1066 entry_param_control *ctl = (entry_param_control *)value;
1067 ctl->gui->plugin->configure(ctl->attribs["key"].c_str(), gtk_entry_get_text(ctl->entry));
1070 /******************************** File Chooser ********************************/
1072 GtkWidget *filechooser_param_control::create(plugin_gui *_gui, int _param_no)
1074 gui = _gui;
1075 param_no = _param_no;
1076 require_attribute("key");
1077 require_attribute("title");
1079 widget = gtk_file_chooser_button_new(attribs["title"].c_str(), GTK_FILE_CHOOSER_ACTION_OPEN);
1080 filechooser = GTK_FILE_CHOOSER_BUTTON(widget);
1081 // XXXKF this is GTK+ 2.12 function, does any replacement exist?
1082 // MS: switched from g_signal_connect to g_signal_connect for better emission of signals
1083 g_signal_connect(GTK_OBJECT(widget), "file-set", G_CALLBACK(filechooser_value_changed), (gpointer)this);
1084 if (attribs.count("width"))
1085 gtk_widget_set_size_request (widget, get_int("width", 200), -1);
1086 if (attribs.count("width_chars"))
1087 gtk_file_chooser_button_set_width_chars (filechooser, get_int("width_chars"));
1088 gtk_widget_set_name(GTK_WIDGET(widget), "Calf-FileButton");
1089 return widget;
1092 void filechooser_param_control::send_configure(const char *key, const char *value)
1094 // cout << "send conf " << key << endl;
1095 if (attribs["key"] == key)
1097 gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(filechooser), value);
1101 void filechooser_param_control::filechooser_value_changed(GtkWidget *widget, gpointer value)
1103 filechooser_param_control *ctl = (filechooser_param_control *)value;
1104 const char *filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(ctl->filechooser));
1105 if (filename)
1106 ctl->gui->plugin->configure(ctl->attribs["key"].c_str(), filename);
1109 /******************************** Line Graph ********************************/
1111 void line_graph_param_control::on_idle()
1113 if (get_int("refresh", 0))
1114 set();
1117 static float to_x_pos(float freq)
1119 return log(freq / 20.0) / log(1000);
1122 static float from_x_pos(float pos)
1124 float a = pos * 3.0;
1125 float b = powf(10.0, a);
1126 float c = b * 20.0;
1127 return c;
1130 static float to_y_pos(CalfLineGraph *lg, float gain)
1132 //log(gain) * (1.0 / log(32));
1133 return 0.5 - dB_grid(gain, 128 * lg->zoom, lg->offset) / 2.0;
1136 static float from_y_pos(CalfLineGraph *lg, float pos)
1138 float gain = powf(128.0 * lg->zoom, (0.5 - pos) * 2.0 - lg->offset);
1139 return gain;
1142 GtkWidget *line_graph_param_control::create(plugin_gui *a_gui, int a_param_no)
1144 gui = a_gui;
1145 param_no = a_param_no;
1146 //last_generation = -1;
1148 widget = calf_line_graph_new ();
1150 gtk_widget_set_name(GTK_WIDGET(widget), "calf-graph");
1152 CalfLineGraph *clg = CALF_LINE_GRAPH(widget);
1153 widget->requisition.width = get_int("width", 40);
1154 widget->requisition.height = get_int("height", 40);
1156 calf_line_graph_set_square(clg, get_int("square", 0));
1158 clg->source = gui->plugin->get_line_graph_iface();
1159 clg->source_id = param_no;
1160 clg->fade = get_float("fade", 1.0);
1161 clg->mode = get_int("mode", 0);
1162 clg->use_crosshairs = get_int("crosshairs", 0);
1163 clg->freqhandles = get_int("freqhandles", 0);
1164 clg->enforce_handle_order = get_int("enforce-handle-order", 0);
1165 clg->min_handle_distance = get_float("min-handle-distance", 0.01);
1167 const string &zoom_name = attribs["zoom"];
1168 if (zoom_name != "")
1169 clg->param_zoom = gui->get_param_no_by_name(zoom_name);
1171 const string &offset_name = attribs["offset"];
1172 if (offset_name != "")
1173 clg->param_offset = gui->get_param_no_by_name(offset_name);
1175 if (clg->freqhandles > 0)
1177 for(int i = 0; i < clg->freqhandles; i++)
1179 FreqHandle *handle = &clg->freq_handles[i];
1181 stringstream handle_x_attribute;
1182 handle_x_attribute << "handle" << i + 1 << "-x";
1183 const string &param_x_name = attribs[handle_x_attribute.str()];
1184 if(param_x_name == "")
1185 break;
1187 int param_x_no = gui->get_param_no_by_name(param_x_name);
1188 const parameter_properties &handle_x_props = *gui->plugin->get_metadata_iface()->get_param_props(param_x_no);
1189 handle->dimensions = 1;
1190 handle->param_x_no = param_x_no;
1191 handle->value_x = to_x_pos(gui->plugin->get_param_value(param_x_no));
1192 handle->default_value_x = to_x_pos(handle_x_props.def_value);
1194 stringstream handle_y_attribute;
1195 handle_y_attribute << "handle" << i + 1 << "-y";
1196 const string &param_y_name = attribs[handle_y_attribute.str()];
1197 if(param_y_name != "") {
1198 int param_y_no = gui->get_param_no_by_name(param_y_name);
1199 const parameter_properties &handle_y_props = *gui->plugin->get_metadata_iface()->get_param_props(param_y_no);
1200 handle->dimensions = 2;
1201 handle->param_y_no = param_y_no;
1202 handle->value_y = to_y_pos(clg, gui->plugin->get_param_value(param_y_no));
1203 handle->default_value_y = to_y_pos(clg, handle_y_props.def_value);
1204 } else {
1205 handle->param_y_no = -1;
1208 stringstream handle_z_attribute;
1209 handle_z_attribute << "handle" << i + 1 << "-z";
1210 const string &param_z_name = attribs[handle_z_attribute.str()];
1211 if(param_z_name != "") {
1212 int param_z_no = gui->get_param_no_by_name(param_z_name);
1213 const parameter_properties &handle_z_props = *gui->plugin->get_metadata_iface()->get_param_props(param_z_no);
1214 handle->dimensions = 3;
1215 handle->param_z_no = param_z_no;
1216 handle->value_z = handle_z_props.to_01(gui->plugin->get_param_value(param_z_no));
1217 handle->default_value_z = handle_z_props.to_01(handle_z_props.def_value);
1218 } else {
1219 handle->param_z_no = -1;
1222 stringstream label_attribute;
1223 label_attribute << "label" << i + 1;
1224 string label = attribs[label_attribute.str()];
1225 if (!label.empty()) {
1226 handle->label = strdup(label.c_str());
1229 stringstream active_attribute;
1230 active_attribute << "active" << i + 1;
1231 const string &active_name = attribs[active_attribute.str()];
1232 if (active_name != "") {
1233 handle->param_active_no = gui->get_param_no_by_name(active_name);
1234 } else {
1235 handle->param_active_no = -1;
1238 stringstream style_attribute;
1239 style_attribute << "style" << i + 1;
1240 const string style = style_attribute.str();
1241 clg->freq_handles[i].style = get_int(style.c_str(), 0);
1242 if(clg->freq_handles[i].style == 1 or clg->freq_handles[i].style == 4) {
1243 clg->freq_handles[i].dimensions = 1;
1245 handle->data = (gpointer) this;
1247 g_signal_connect(G_OBJECT(widget), "freqhandle-changed", G_CALLBACK(freqhandle_value_changed), this);
1250 gtk_widget_set_name(GTK_WIDGET(widget), "Calf-LineGraph");
1251 return widget;
1254 void line_graph_param_control::get()
1256 GtkWidget *tw = gtk_widget_get_toplevel(widget);
1257 CalfLineGraph *clg = CALF_LINE_GRAPH(widget);
1259 if (tw && GTK_WIDGET_TOPLEVEL(tw) && widget->window)
1261 int ws = gdk_window_get_state(widget->window);
1262 if (ws & (GDK_WINDOW_STATE_WITHDRAWN | GDK_WINDOW_STATE_ICONIFIED))
1263 return;
1265 if(clg->handle_grabbed >= 0) {
1266 FreqHandle *handle = &clg->freq_handles[clg->handle_grabbed];
1267 if(handle->dimensions >= 2) {
1268 float value_y = from_y_pos(clg, handle->value_y);
1269 gui->set_param_value(handle->param_y_no, value_y, this);
1272 float value_x = from_x_pos(handle->value_x);
1273 gui->set_param_value(handle->param_x_no, value_x, this);
1274 } else if(clg->handle_hovered >= 0) {
1275 FreqHandle *handle = &clg->freq_handles[clg->handle_hovered];
1277 if(handle->dimensions == 3) {
1278 const parameter_properties &handle_z_props = *gui->plugin->get_metadata_iface()->get_param_props(handle->param_z_no);
1279 float value_z = handle_z_props.from_01(handle->value_z);
1280 gui->set_param_value(handle->param_z_no, value_z, this);
1286 void line_graph_param_control::set()
1288 _GUARD_CHANGE_
1289 GtkWidget *tw = gtk_widget_get_toplevel(widget);
1290 CalfLineGraph *clg = CALF_LINE_GRAPH(widget);
1291 if (tw && GTK_WIDGET_TOPLEVEL(tw) && widget->window)
1293 bool force = false;
1294 int ws = gdk_window_get_state(widget->window);
1295 if (ws & (GDK_WINDOW_STATE_WITHDRAWN | GDK_WINDOW_STATE_ICONIFIED))
1296 return;
1298 if (clg->param_zoom >= 0) {
1299 float _z = gui->plugin->get_param_value(clg->param_zoom);
1300 if (_z != clg->zoom) {
1301 force = true;
1302 clg->zoom = _z;
1303 clg->force_redraw = true;
1307 if (clg->param_offset >= 0) {
1308 float _z = gui->plugin->get_param_value(clg->param_offset);
1309 if (_z != clg->offset) {
1310 force = true;
1311 clg->offset = _z;
1312 clg->force_redraw = true;
1316 for (int i = 0; i < clg->freqhandles; i++) {
1317 FreqHandle *handle = &clg->freq_handles[i];
1319 if (handle->param_x_no >= 0)
1321 float value_x = gui->plugin->get_param_value(handle->param_x_no);
1322 handle->value_x = to_x_pos(value_x);
1323 if (dsp::_sanitize(handle->value_x - handle->last_value_x)) {
1324 clg->handle_redraw = 1;
1326 handle->last_value_x = handle->value_x;
1327 if(handle->dimensions >= 2 && handle->param_y_no >= 0) {
1328 float value_y = gui->plugin->get_param_value(handle->param_y_no);
1329 handle->value_y = to_y_pos(clg, value_y);
1330 if (dsp::_sanitize(handle->value_y - handle->last_value_y)) {
1331 clg->handle_redraw = 1;
1333 handle->last_value_y = handle->value_y;
1337 if(handle->dimensions == 3 && handle->param_z_no >= 0) {
1338 const parameter_properties &handle_z_props = *gui->plugin->get_metadata_iface()->get_param_props(handle->param_z_no);
1339 float value_z = gui->plugin->get_param_value(handle->param_z_no);
1340 handle->value_z = handle_z_props.to_01(value_z);
1341 if (dsp::_sanitize(handle->value_z - handle->last_value_z)) {
1342 clg->handle_redraw = 1;
1344 handle->last_value_z = handle->value_z;
1346 bool _a = handle->active;
1347 if(handle->param_active_no >= 0) {
1348 handle->active = bool(gui->plugin->get_param_value(handle->param_active_no));
1349 } else {
1350 handle->active = true;
1352 if (handle->active != _a) {
1353 force = true;
1354 clg->handle_redraw = true;
1357 calf_line_graph_expose_request(widget, force);
1361 void line_graph_param_control::freqhandle_value_changed(GtkWidget *widget, gpointer p)
1363 assert(p!=NULL);
1364 FreqHandle *handle = (FreqHandle *)p;
1365 param_control *jhp = (param_control *)handle->data;
1366 jhp->get();
1370 line_graph_param_control::~line_graph_param_control()
1375 /******************************** Phase Graph ********************************/
1377 void phase_graph_param_control::on_idle()
1379 if (get_int("refresh", 0))
1380 set();
1383 GtkWidget *phase_graph_param_control::create(plugin_gui *_gui, int _param_no)
1385 gui = _gui;
1386 param_no = _param_no;
1387 widget = calf_phase_graph_new ();
1388 gtk_widget_set_name(GTK_WIDGET(widget), "calf-phase");
1389 CalfPhaseGraph *clg = CALF_PHASE_GRAPH(widget);
1390 widget->requisition.width = get_int("size", 40);
1391 widget->requisition.height = get_int("size", 40);
1392 clg->source = gui->plugin->get_phase_graph_iface();
1393 clg->source_id = param_no;
1394 gtk_widget_set_name(GTK_WIDGET(widget), "Calf-PhaseGraph");
1395 return widget;
1398 void phase_graph_param_control::set()
1400 _GUARD_CHANGE_
1401 GtkWidget *tw = gtk_widget_get_toplevel(widget);
1402 if (tw && GTK_WIDGET_TOPLEVEL(tw) && widget->window) {
1403 gtk_widget_queue_draw(widget);
1407 phase_graph_param_control::~phase_graph_param_control()
1411 /******************************** List View ********************************/
1413 GtkWidget *listview_param_control::create(plugin_gui *_gui, int _param_no)
1415 gui = _gui;
1416 param_no = _param_no;
1418 string key = attribs["key"];
1419 tmif = gui->plugin->get_metadata_iface()->get_table_metadata_iface(key.c_str());
1420 if (!tmif)
1422 g_error("Missing table_metadata_iface for variable '%s'", key.c_str());
1423 return NULL;
1425 positions.clear();
1426 const table_column_info *tci = tmif->get_table_columns();
1427 assert(tci);
1428 cols = 0;
1429 while (tci[cols].name != NULL)
1430 cols++;
1432 GType *p = new GType[cols];
1433 for (int i = 0; i < cols; i++)
1434 p[i] = G_TYPE_STRING;
1435 lstore = gtk_list_store_newv(cols, p);
1436 if (tmif->get_table_rows() != 0)
1437 set_rows(tmif->get_table_rows());
1438 widget = gtk_tree_view_new_with_model(GTK_TREE_MODEL(lstore));
1439 delete []p;
1440 tree = GTK_TREE_VIEW (widget);
1441 g_object_set (G_OBJECT (tree), "enable-search", FALSE, "rules-hint", TRUE, "enable-grid-lines", TRUE, NULL);
1443 for (int i = 0; i < cols; i++)
1445 GtkCellRenderer *cr = NULL;
1447 if (tci[i].type == TCT_ENUM) {
1448 cr = gtk_cell_renderer_combo_new ();
1449 GtkListStore *cls = gtk_list_store_new(2, G_TYPE_INT, G_TYPE_STRING);
1450 for (int j = 0; tci[i].values[j]; j++)
1451 gtk_list_store_insert_with_values(cls, NULL, j, 0, j, 1, tci[i].values[j], -1);
1452 g_object_set(cr, "model", cls, "editable", TRUE, "has-entry", FALSE, "text-column", 1, "mode", GTK_CELL_RENDERER_MODE_EDITABLE, NULL);
1454 else {
1455 bool editable = tci[i].type != TCT_LABEL;
1456 cr = gtk_cell_renderer_text_new ();
1457 if (editable)
1458 g_object_set(cr, "editable", TRUE, "mode", GTK_CELL_RENDERER_MODE_EDITABLE, NULL);
1460 g_object_set_data (G_OBJECT(cr), "column", (void *)&tci[i]);
1461 g_signal_connect (GTK_OBJECT (cr), "edited", G_CALLBACK (on_edited), (gpointer)this);
1462 g_signal_connect (GTK_OBJECT (cr), "editing-canceled", G_CALLBACK (on_editing_canceled), (gpointer)this);
1463 gtk_tree_view_insert_column_with_attributes(tree, i, tci[i].name, cr, "text", i, NULL);
1465 gtk_tree_view_set_headers_visible(tree, TRUE);
1466 gtk_widget_set_name(GTK_WIDGET(widget), "Calf-ListView");
1467 return widget;
1470 void listview_param_control::set_rows(unsigned int needed_rows)
1472 while(positions.size() < needed_rows)
1474 GtkTreeIter iter;
1475 gtk_list_store_insert(lstore, &iter, positions.size());
1476 for (int j = 0; j < cols; j++)
1478 gtk_list_store_set(lstore, &iter, j, "", -1);
1480 positions.push_back(iter);
1484 void listview_param_control::send_configure(const char *key, const char *value)
1486 string orig_key = attribs["key"] + ":";
1487 bool is_rows = false;
1488 int row = -1, col = -1;
1489 if (parse_table_key(key, orig_key.c_str(), is_rows, row, col))
1491 string suffix = string(key + orig_key.length());
1492 if (is_rows && tmif->get_table_rows() == 0)
1494 int rows = atoi(value);
1495 set_rows(rows);
1496 return;
1498 else
1499 if (row != -1 && col != -1)
1501 int max_rows = tmif->get_table_rows();
1502 if (col < 0 || col >= cols)
1504 g_warning("Invalid column %d in key %s", col, key);
1505 return;
1507 if (max_rows && (row < 0 || row >= max_rows))
1509 g_warning("Invalid row %d in key %s, this is a fixed table with row count = %d", row, key, max_rows);
1510 return;
1513 if (row >= (int)positions.size())
1514 set_rows(row + 1);
1516 gtk_list_store_set(lstore, &positions[row], col, value, -1);
1517 return;
1522 void listview_param_control::on_edited(GtkCellRenderer *renderer, gchar *path, gchar *new_text, listview_param_control *pThis)
1524 const table_column_info *tci = pThis->tmif->get_table_columns();
1525 int column = ((table_column_info *)g_object_get_data(G_OBJECT(renderer), "column")) - tci;
1526 string key = pThis->attribs["key"] + ":" + i2s(atoi(path)) + "," + i2s(column);
1527 string error;
1528 const char *error_or_null = pThis->gui->plugin->configure(key.c_str(), new_text);
1529 if (error_or_null)
1530 error = error_or_null;
1532 if (error.empty()) {
1533 pThis->send_configure(key.c_str(), new_text);
1534 gtk_widget_grab_focus(pThis->widget);
1535 GtkTreePath *gpath = gtk_tree_path_new_from_string (path);
1536 gtk_tree_view_set_cursor_on_cell (GTK_TREE_VIEW (pThis->widget), gpath, NULL, NULL, FALSE);
1537 gtk_tree_path_free (gpath);
1539 else
1541 GtkWidget *dialog = gtk_message_dialog_new(pThis->gui->window->toplevel, GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK,
1542 "%s", error.c_str());
1543 gtk_dialog_run(GTK_DIALOG(dialog));
1544 gtk_widget_destroy(dialog);
1545 gtk_widget_grab_focus(pThis->widget);
1549 void listview_param_control::on_editing_canceled(GtkCellRenderer *renderer, listview_param_control *pThis)
1551 gtk_widget_grab_focus(pThis->widget);
1554 /******************************** GtkNotebook control ********************************/
1556 GtkWidget *notebook_param_control::create(plugin_gui *_gui, int _param_no)
1558 gui = _gui;
1559 param_no = _param_no;
1560 //const parameter_properties &props = get_props();
1561 if (param_no < 0)
1562 page = 0;
1563 else
1564 page = gui->plugin->get_param_value(param_no);
1565 GtkWidget *nb = calf_notebook_new();
1566 widget = GTK_WIDGET(nb);
1567 container = GTK_CONTAINER(nb);
1568 gtk_widget_set_name(GTK_WIDGET(nb), "Calf-Notebook");
1569 gtk_notebook_set_current_page(GTK_NOTEBOOK(widget), page);
1570 return nb;
1572 void notebook_param_control::created() {
1573 g_signal_connect (GTK_OBJECT (widget), "switch-page", G_CALLBACK (notebook_page_changed), (gpointer)this);
1574 set();
1576 void notebook_param_control::get()
1578 if (param_no >= 0)
1579 gui->set_param_value(param_no, page, this);
1581 void notebook_param_control::set()
1583 if (param_no < 0)
1584 return;
1585 _GUARD_CHANGE_
1586 page = (gint)gui->plugin->get_param_value(param_no);
1587 gtk_notebook_set_current_page(GTK_NOTEBOOK(widget), page);
1589 void notebook_param_control::add(GtkWidget *w, control_base *base)
1591 gtk_notebook_append_page(GTK_NOTEBOOK(widget), w, gtk_label_new_with_mnemonic(base->attribs["page"].c_str()));
1593 void notebook_param_control::notebook_page_changed(GtkWidget *widget, GtkWidget *page, guint id, gpointer user)
1595 notebook_param_control *jhp = (notebook_param_control *)user;
1596 jhp->page = (int)id;
1597 jhp->get();
1600 /******************************** GtkTable container ********************************/
1602 GtkWidget *table_container::create(plugin_gui *_gui, const char *element, xml_attribute_map &attributes)
1604 require_int_attribute("rows");
1605 require_int_attribute("cols");
1606 int homog = get_int("homogeneous", 0);
1607 int sx = get_int("spacing-x", 2);
1608 int sy = get_int("spacing-y", 2);
1609 GtkWidget *table = gtk_table_new(get_int("rows", 1), get_int("cols", 1), false);
1610 if(homog > 0) {
1611 gtk_table_set_homogeneous(GTK_TABLE(table), TRUE);
1613 gtk_table_set_col_spacings(GTK_TABLE(table), sx);
1614 gtk_table_set_row_spacings(GTK_TABLE(table), sy);
1615 container = GTK_CONTAINER(table);
1616 gtk_widget_set_name(GTK_WIDGET(table), "Calf-Table");
1617 return table;
1620 void table_container::add(GtkWidget *widget, control_base *base)
1622 base->require_int_attribute("attach-x");
1623 base->require_int_attribute("attach-y");
1624 int x = base->get_int("attach-x"), y = base->get_int("attach-y");
1625 int w = base->get_int("attach-w", 1), h = base->get_int("attach-h", 1);
1626 int shrinkx = base->get_int("shrink-x", 0);
1627 int shrinky = base->get_int("shrink-y", 0);
1628 int fillx = (base->get_int("fill-x", !shrinkx) ? GTK_FILL : 0) | (base->get_int("expand-x", !shrinkx) ? GTK_EXPAND : 0) | (shrinkx ? GTK_SHRINK : 0);
1629 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);
1630 int padx = base->get_int("pad-x", 2);
1631 int pady = base->get_int("pad-y", 2);
1632 gtk_table_attach(GTK_TABLE(container), widget, x, x + w, y, y + h, (GtkAttachOptions)fillx, (GtkAttachOptions)filly, padx, pady);
1635 /******************************** alignment container ********************************/
1637 GtkWidget *alignment_container::create(plugin_gui *_gui, const char *element, xml_attribute_map &attributes)
1639 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));
1640 container = GTK_CONTAINER(align);
1641 gtk_widget_set_name(GTK_WIDGET(align), "Calf-Align");
1642 return align;
1645 /******************************** GtkFrame container ********************************/
1647 GtkWidget *frame_container::create(plugin_gui *_gui, const char *element, xml_attribute_map &attributes)
1649 GtkWidget *widget = calf_frame_new(attribs["label"].c_str());
1650 container = GTK_CONTAINER(widget);
1651 gtk_widget_set_name(GTK_WIDGET(widget), "Calf-Frame");
1652 return widget;
1655 /******************************** GtkBox type of containers ********************************/
1657 void box_container::add(GtkWidget *w, control_base *base)
1659 gtk_container_add_with_properties(container, w, "expand", get_int("expand", 1), "fill", get_int("fill", 1), NULL);
1662 /******************************** GtkHBox container ********************************/
1664 GtkWidget *hbox_container::create(plugin_gui *_gui, const char *element, xml_attribute_map &attributes)
1666 GtkWidget *hbox = gtk_hbox_new(get_int("homogeneous") >= 1, get_int("spacing", 2));
1667 container = GTK_CONTAINER(hbox);
1668 gtk_widget_set_name(GTK_WIDGET(hbox), "Calf-HBox");
1669 return hbox;
1672 /******************************** GtkVBox container ********************************/
1674 GtkWidget *vbox_container::create(plugin_gui *_gui, const char *element, xml_attribute_map &attributes)
1676 GtkWidget *vbox = gtk_vbox_new(get_int("homogeneous") >= 1, get_int("spacing", 2));
1677 container = GTK_CONTAINER(vbox);
1678 gtk_widget_set_name(GTK_WIDGET(vbox), "Calf-VBox");
1679 return vbox;
1682 /******************************** GtkNotebook container ********************************/
1684 GtkWidget *scrolled_container::create(plugin_gui *_gui, const char *element, xml_attribute_map &attributes)
1686 GtkAdjustment *horiz = NULL, *vert = NULL;
1687 int width = get_int("width", 0), height = get_int("height", 0);
1688 if (width)
1689 horiz = GTK_ADJUSTMENT(gtk_adjustment_new(get_int("x", 0), 0, width, get_int("step-x", 1), get_int("page-x", width / 10), 100));
1690 if (height)
1691 vert = GTK_ADJUSTMENT(gtk_adjustment_new(get_int("y", 0), 0, width, get_int("step-y", 1), get_int("page-y", height / 10), 10));
1692 GtkWidget *sw = gtk_scrolled_window_new(horiz, vert);
1693 gtk_widget_set_size_request(sw, get_int("req-x", -1), get_int("req-y", -1));
1694 container = GTK_CONTAINER(sw);
1695 gtk_widget_set_name(GTK_WIDGET(sw), "Calf-ScrolledWindow");
1696 return sw;
1699 void scrolled_container::add(GtkWidget *w, control_base *base)
1701 gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(container), w);