+ GUI: move control wrapper classes to a separate set of files
[calf.git] / src / gui_controls.cpp
blobbf3f2e28878a4d12346d62d23bf38dd19788e773
1 /* Calf DSP Library
2 * GUI widget object implementations.
3 * Copyright (C) 2007-2009 Krzysztof Foltman
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this program; if not, write to the
17 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301 USA
21 #include <config.h>
22 #include <assert.h>
23 #include <calf/ctl_curve.h>
24 #include <calf/ctl_keyboard.h>
25 #include <calf/ctl_led.h>
26 #include <calf/giface.h>
27 #include <calf/gui.h>
28 #include <calf/gui_controls.h>
29 #include <calf/preset.h>
30 #include <calf/preset_gui.h>
31 #include <calf/main_win.h>
32 #include <gdk/gdk.h>
34 using namespace calf_plugins;
35 using namespace std;
37 /******************************** controls ********************************/
39 // combo box
41 GtkWidget *combo_box_param_control::create(plugin_gui *_gui, int _param_no)
43 gui = _gui;
44 param_no = _param_no;
45 lstore = gtk_list_store_new(2, G_TYPE_STRING, G_TYPE_STRING); // value, key
47 parameter_properties &props = get_props();
48 widget = gtk_combo_box_new_text ();
49 if (props.choices)
51 for (int j = (int)props.min; j <= (int)props.max; j++)
52 gtk_list_store_insert_with_values (lstore, NULL, j - (int)props.min, 0, props.choices[j - (int)props.min], 1, calf_utils::i2s(j).c_str(), -1);
54 gtk_combo_box_set_model (GTK_COMBO_BOX(widget), GTK_TREE_MODEL(lstore));
55 gtk_signal_connect (GTK_OBJECT (widget), "changed", G_CALLBACK (combo_value_changed), (gpointer)this);
57 return widget;
60 void combo_box_param_control::set()
62 _GUARD_CHANGE_
63 parameter_properties &props = get_props();
64 gtk_combo_box_set_active (GTK_COMBO_BOX (widget), (int)gui->plugin->get_param_value(param_no) - (int)props.min);
67 void combo_box_param_control::get()
69 parameter_properties &props = get_props();
70 gui->set_param_value(param_no, gtk_combo_box_get_active (GTK_COMBO_BOX(widget)) + props.min, this);
73 void combo_box_param_control::combo_value_changed(GtkComboBox *widget, gpointer value)
75 combo_box_param_control *jhp = (combo_box_param_control *)value;
76 if (jhp->attribs.count("setter-key"))
78 GtkTreeIter iter;
79 gchar *key = NULL;
80 if (gtk_combo_box_get_active_iter (GTK_COMBO_BOX (jhp->widget), &iter))
82 gtk_tree_model_get (GTK_TREE_MODEL (jhp->lstore), &iter, 1, &key, -1);
83 if (key) {
84 jhp->gui->plugin->configure(jhp->attribs["setter-key"].c_str(), key);
85 free(key);
89 else
90 jhp->get();
93 void combo_box_param_control::send_status(const char *key, const char *value)
95 if (attribs.count("key") && key == attribs["key"])
97 gtk_list_store_clear (lstore);
98 key2pos.clear();
99 std::string v = value;
100 int i = 0;
101 size_t pos = 0;
102 while (pos < v.length()) {
103 size_t endpos = v.find("\n", pos);
104 if (endpos == string::npos)
105 break;
106 string line = v.substr(pos, endpos - pos);
107 string key, label;
108 size_t tabpos = line.find('\t');
109 if (tabpos == string::npos)
110 key = label = line;
111 else {
112 key = line.substr(0, tabpos);
113 label = line.substr(tabpos + 1);
115 GtkTreeIter gti;
116 gtk_list_store_insert_with_values (lstore, &gti, i, 0, label.c_str(), 1, key.c_str(), -1);
117 key2pos[key] = gti;
118 pos = endpos + 1;
119 i++;
121 set_to_last_key();
123 if (attribs.count("current-key") && key == attribs["current-key"])
125 last_key = value;
126 set_to_last_key();
130 void combo_box_param_control::set_to_last_key()
132 map<string, GtkTreeIter>::iterator i = key2pos.find(last_key);
133 if (i != key2pos.end())
135 gtk_combo_box_set_active_iter (GTK_COMBO_BOX (widget), &i->second);
138 else
139 gtk_combo_box_set_active (GTK_COMBO_BOX (widget), -1);
142 // horizontal fader
144 GtkWidget *hscale_param_control::create(plugin_gui *_gui, int _param_no)
146 gui = _gui;
147 param_no = _param_no;
149 widget = gtk_hscale_new_with_range (0, 1, get_props().get_increment());
150 gtk_signal_connect (GTK_OBJECT (widget), "value-changed", G_CALLBACK (hscale_value_changed), (gpointer)this);
151 gtk_signal_connect (GTK_OBJECT (widget), "format-value", G_CALLBACK (hscale_format_value), (gpointer)this);
152 gtk_widget_set_size_request (widget, 200, -1);
154 return widget;
157 void hscale_param_control::init_xml(const char *element)
159 if (attribs.count("width"))
160 gtk_widget_set_size_request (widget, get_int("width", 200), -1);
161 if (attribs.count("position"))
163 string v = attribs["position"];
164 if (v == "top") gtk_scale_set_value_pos(GTK_SCALE(widget), GTK_POS_TOP);
165 if (v == "bottom") gtk_scale_set_value_pos(GTK_SCALE(widget), GTK_POS_BOTTOM);
169 void hscale_param_control::set()
171 _GUARD_CHANGE_
172 parameter_properties &props = get_props();
173 gtk_range_set_value (GTK_RANGE (widget), props.to_01 (gui->plugin->get_param_value(param_no)));
174 // hscale_value_changed (GTK_HSCALE (widget), (gpointer)this);
177 void hscale_param_control::get()
179 parameter_properties &props = get_props();
180 float cvalue = props.from_01 (gtk_range_get_value (GTK_RANGE (widget)));
181 gui->set_param_value(param_no, cvalue, this);
184 void hscale_param_control::hscale_value_changed(GtkHScale *widget, gpointer value)
186 hscale_param_control *jhp = (hscale_param_control *)value;
187 jhp->get();
190 gchar *hscale_param_control::hscale_format_value(GtkScale *widget, double arg1, gpointer value)
192 hscale_param_control *jhp = (hscale_param_control *)value;
193 const parameter_properties &props = jhp->get_props();
194 float cvalue = props.from_01 (arg1);
196 // for testing
197 // return g_strdup_printf ("%s = %g", props.to_string (cvalue).c_str(), arg1);
198 return g_strdup (props.to_string (cvalue).c_str());
201 // vertical fader
203 GtkWidget *vscale_param_control::create(plugin_gui *_gui, int _param_no)
205 gui = _gui;
206 param_no = _param_no;
208 widget = gtk_vscale_new_with_range (0, 1, get_props().get_increment());
209 gtk_signal_connect (GTK_OBJECT (widget), "value-changed", G_CALLBACK (vscale_value_changed), (gpointer)this);
210 gtk_scale_set_draw_value(GTK_SCALE(widget), FALSE);
211 gtk_widget_set_size_request (widget, -1, 200);
213 return widget;
216 void vscale_param_control::init_xml(const char *element)
218 if (attribs.count("height"))
219 gtk_widget_set_size_request (widget, -1, get_int("height", 200));
222 void vscale_param_control::set()
224 _GUARD_CHANGE_
225 parameter_properties &props = get_props();
226 gtk_range_set_value (GTK_RANGE (widget), props.to_01 (gui->plugin->get_param_value(param_no)));
227 // vscale_value_changed (GTK_HSCALE (widget), (gpointer)this);
230 void vscale_param_control::get()
232 parameter_properties &props = get_props();
233 float cvalue = props.from_01 (gtk_range_get_value (GTK_RANGE (widget)));
234 gui->set_param_value(param_no, cvalue, this);
237 void vscale_param_control::vscale_value_changed(GtkHScale *widget, gpointer value)
239 vscale_param_control *jhp = (vscale_param_control *)value;
240 jhp->get();
243 // label
245 GtkWidget *label_param_control::create(plugin_gui *_gui, int _param_no)
247 gui = _gui, param_no = _param_no;
248 string text;
249 if (param_no != -1)
250 text = get_props().name;
251 else
252 text = attribs["text"];
253 widget = gtk_label_new(text.c_str());
254 gtk_misc_set_alignment (GTK_MISC (widget), get_float("align-x", 0.5), get_float("align-y", 0.5));
255 return widget;
258 // value
260 GtkWidget *value_param_control::create(plugin_gui *_gui, int _param_no)
262 gui = _gui;
263 param_no = _param_no;
265 widget = gtk_label_new ("");
266 if (param_no != -1)
268 parameter_properties &props = get_props();
269 gtk_label_set_width_chars (GTK_LABEL (widget), props.get_char_count());
271 else
273 require_attribute("key");
274 require_int_attribute("width");
275 param_variable = attribs["key"];
276 gtk_label_set_width_chars (GTK_LABEL (widget), get_int("width"));
278 gtk_misc_set_alignment (GTK_MISC (widget), get_float("align-x", 0.5), get_float("align-y", 0.5));
279 return widget;
282 void value_param_control::set()
284 if (param_no == -1)
285 return;
286 _GUARD_CHANGE_
287 parameter_properties &props = get_props();
288 gtk_label_set_text (GTK_LABEL (widget), props.to_string(gui->plugin->get_param_value(param_no)).c_str());
291 void value_param_control::send_status(const char *key, const char *value)
293 if (key == param_variable)
295 gtk_label_set_text (GTK_LABEL (widget), value);
299 // VU meter
301 GtkWidget *vumeter_param_control::create(plugin_gui *_gui, int _param_no)
303 gui = _gui, param_no = _param_no;
304 // parameter_properties &props = get_props();
305 widget = calf_vumeter_new ();
306 calf_vumeter_set_mode (CALF_VUMETER (widget), (CalfVUMeterMode)get_int("mode", 0));
307 return widget;
310 void vumeter_param_control::set()
312 _GUARD_CHANGE_
313 parameter_properties &props = get_props();
314 calf_vumeter_set_value (CALF_VUMETER (widget), props.to_01(gui->plugin->get_param_value(param_no)));
315 if (label)
316 update_label();
319 // LED
321 GtkWidget *led_param_control::create(plugin_gui *_gui, int _param_no)
323 gui = _gui, param_no = _param_no;
324 // parameter_properties &props = get_props();
325 widget = calf_led_new ();
326 return widget;
329 void led_param_control::set()
331 _GUARD_CHANGE_
332 // parameter_properties &props = get_props();
333 calf_led_set_state (CALF_LED (widget), gui->plugin->get_param_value(param_no) > 0);
334 if (label)
335 update_label();
338 // check box
340 GtkWidget *check_param_control::create(plugin_gui *_gui, int _param_no)
342 gui = _gui;
343 param_no = _param_no;
345 widget = gtk_check_button_new ();
346 gtk_signal_connect (GTK_OBJECT (widget), "toggled", G_CALLBACK (check_value_changed), (gpointer)this);
347 return widget;
350 void check_param_control::check_value_changed(GtkCheckButton *widget, gpointer value)
352 param_control *jhp = (param_control *)value;
353 jhp->get();
356 void check_param_control::get()
358 const parameter_properties &props = get_props();
359 gui->set_param_value(param_no, gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON(widget)) + props.min, this);
362 void check_param_control::set()
364 _GUARD_CHANGE_
365 const parameter_properties &props = get_props();
366 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (widget), (int)gui->plugin->get_param_value(param_no) - (int)props.min);
369 // spin button
371 GtkWidget *spin_param_control::create(plugin_gui *_gui, int _param_no)
373 gui = _gui;
374 param_no = _param_no;
376 const parameter_properties &props = get_props();
377 if (props.step > 1)
378 widget = gtk_spin_button_new_with_range (props.min, props.max, (props.max - props.min) / (props.step - 1));
379 if (props.step > 0)
380 widget = gtk_spin_button_new_with_range (props.min, props.max, props.step);
381 else
382 widget = gtk_spin_button_new_with_range (props.min, props.max, 1);
383 gtk_spin_button_set_digits (GTK_SPIN_BUTTON(widget), get_int("digits", 0));
384 gtk_signal_connect (GTK_OBJECT (widget), "value-changed", G_CALLBACK (value_changed), (gpointer)this);
385 return widget;
388 void spin_param_control::value_changed(GtkSpinButton *widget, gpointer value)
390 param_control *jhp = (param_control *)value;
391 jhp->get();
394 void spin_param_control::get()
396 // const parameter_properties &props = get_props();
397 gui->set_param_value(param_no, gtk_spin_button_get_value_as_float (GTK_SPIN_BUTTON (widget)), this);
400 void spin_param_control::set()
402 _GUARD_CHANGE_
403 // const parameter_properties &props = get_props();
404 gtk_spin_button_set_value (GTK_SPIN_BUTTON (widget), gui->plugin->get_param_value(param_no));
407 // button
409 GtkWidget *button_param_control::create(plugin_gui *_gui, int _param_no)
411 gui = _gui;
412 param_no = _param_no;
414 widget = gtk_button_new_with_label (get_props().name);
415 gtk_signal_connect (GTK_OBJECT (widget), "clicked", G_CALLBACK (button_clicked), (gpointer)this);
416 return widget;
419 void button_param_control::button_clicked(GtkButton *widget, gpointer value)
421 param_control *jhp = (param_control *)value;
423 jhp->get();
426 void button_param_control::get()
428 const parameter_properties &props = get_props();
429 gui->set_param_value(param_no, props.max, this);
432 void button_param_control::set()
434 _GUARD_CHANGE_
435 const parameter_properties &props = get_props();
436 if (gui->plugin->get_param_value(param_no) - props.min >= 0.5)
437 gtk_button_clicked (GTK_BUTTON (widget));
440 // knob
442 GtkWidget *knob_param_control::create(plugin_gui *_gui, int _param_no)
444 gui = _gui;
445 param_no = _param_no;
446 const parameter_properties &props = get_props();
448 //widget = calf_knob_new_with_range (props.to_01 (gui->plugin->get_param_value(param_no)), 0, 1, 0.01);
449 widget = calf_knob_new();
450 float increment = props.get_increment();
451 gtk_range_get_adjustment(GTK_RANGE(widget))->step_increment = increment;
452 CALF_KNOB(widget)->knob_type = get_int("type");
453 CALF_KNOB(widget)->knob_size = get_int("size", 2);
454 if(CALF_KNOB(widget)->knob_size > 4) {
455 CALF_KNOB(widget)->knob_size = 4;
456 } else if (CALF_KNOB(widget)->knob_size < 1) {
457 CALF_KNOB(widget)->knob_size = 1;
459 gtk_signal_connect(GTK_OBJECT(widget), "value-changed", G_CALLBACK(knob_value_changed), (gpointer)this);
460 return widget;
463 void knob_param_control::get()
465 const parameter_properties &props = get_props();
466 float value = props.from_01(gtk_range_get_value(GTK_RANGE(widget)));
467 gui->set_param_value(param_no, value, this);
468 if (label)
469 update_label();
472 void knob_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 if (label)
478 update_label();
481 void knob_param_control::knob_value_changed(GtkWidget *widget, gpointer value)
483 param_control *jhp = (param_control *)value;
484 jhp->get();
487 // Toggle Button
489 GtkWidget *toggle_param_control::create(plugin_gui *_gui, int _param_no)
491 gui = _gui;
492 param_no = _param_no;
493 widget = calf_toggle_new ();
495 CALF_TOGGLE(widget)->size = get_int("size", 2);
496 if(CALF_TOGGLE(widget)->size > 2) {
497 CALF_TOGGLE(widget)->size = 2;
498 } else if (CALF_TOGGLE(widget)->size < 1) {
499 CALF_TOGGLE(widget)->size = 1;
502 gtk_signal_connect (GTK_OBJECT (widget), "value-changed", G_CALLBACK (toggle_value_changed), (gpointer)this);
503 return widget;
506 void toggle_param_control::get()
508 const parameter_properties &props = get_props();
509 float value = props.from_01(gtk_range_get_value(GTK_RANGE(widget)));
510 gui->set_param_value(param_no, value, this);
511 if (label)
512 update_label();
515 void toggle_param_control::set()
517 _GUARD_CHANGE_
518 const parameter_properties &props = get_props();
519 gtk_range_set_value(GTK_RANGE(widget), props.to_01 (gui->plugin->get_param_value(param_no)));
520 if (label)
521 update_label();
524 void toggle_param_control::toggle_value_changed(GtkWidget *widget, gpointer value)
526 param_control *jhp = (param_control *)value;
527 jhp->get();
530 // keyboard
532 GtkWidget *keyboard_param_control::create(plugin_gui *_gui, int _param_no)
534 gui = _gui;
535 param_no = _param_no;
536 // const parameter_properties &props = get_props();
538 widget = calf_keyboard_new();
539 kb = CALF_KEYBOARD(widget);
540 kb->nkeys = get_int("octaves", 4) * 7 + 1;
541 kb->sink = new CalfKeyboard::EventAdapter;
542 return widget;
545 // curve
547 struct curve_param_control_callback: public CalfCurve::EventAdapter
549 curve_param_control *ctl;
551 curve_param_control_callback(curve_param_control *_ctl)
552 : ctl(_ctl) {}
554 virtual void curve_changed(CalfCurve *src, const CalfCurve::point_vector &data) {
555 stringstream ss;
556 ss << data.size() << endl;
557 for (size_t i = 0; i < data.size(); i++)
558 ss << data[i].first << " " << data[i].second << endl;
559 ctl->gui->plugin->configure(ctl->attribs["key"].c_str(), ss.str().c_str());
561 virtual void clip(CalfCurve *src, int pt, float &x, float &y, bool &hide)
563 // int gridpt = floor(x * 71 * 2);
564 // clip to the middle of the nearest white key
565 x = (floor(x * 71) + 0.5)/ 71.0;
569 GtkWidget *curve_param_control::create(plugin_gui *_gui, int _param_no)
571 gui = _gui;
572 param_no = _param_no;
573 require_attribute("key");
575 widget = calf_curve_new(get_int("maxpoints", -1));
576 curve = CALF_CURVE(widget);
577 curve->sink = new curve_param_control_callback(this);
578 // gtk_curve_set_curve_type(curve, GTK_CURVE_TYPE_LINEAR);
579 return widget;
582 void curve_param_control::send_configure(const char *key, const char *value)
584 // cout << "send conf " << key << endl;
585 if (attribs["key"] == key)
587 stringstream ss(value);
588 CalfCurve::point_vector pts;
589 if (*value)
591 unsigned int npoints = 0;
592 ss >> npoints;
593 unsigned int i;
594 float x = 0, y = 0;
595 for (i = 0; i < npoints && i < curve->point_limit; i++)
597 ss >> x >> y;
598 pts.push_back(CalfCurve::point(x, y));
600 calf_curve_set_points(widget, pts);
605 // entry
607 GtkWidget *entry_param_control::create(plugin_gui *_gui, int _param_no)
609 gui = _gui;
610 param_no = _param_no;
611 require_attribute("key");
613 widget = gtk_entry_new();
614 entry = GTK_ENTRY(widget);
615 gtk_signal_connect(GTK_OBJECT(widget), "changed", G_CALLBACK(entry_value_changed), (gpointer)this);
616 gtk_editable_set_editable(GTK_EDITABLE(entry), get_int("editable", 1));
617 return widget;
620 void entry_param_control::send_configure(const char *key, const char *value)
622 // cout << "send conf " << key << endl;
623 if (attribs["key"] == key)
625 gtk_entry_set_text(entry, value);
629 void entry_param_control::entry_value_changed(GtkWidget *widget, gpointer value)
631 entry_param_control *ctl = (entry_param_control *)value;
632 ctl->gui->plugin->configure(ctl->attribs["key"].c_str(), gtk_entry_get_text(ctl->entry));
635 // filechooser
637 GtkWidget *filechooser_param_control::create(plugin_gui *_gui, int _param_no)
639 gui = _gui;
640 param_no = _param_no;
641 require_attribute("key");
642 require_attribute("title");
644 widget = gtk_file_chooser_button_new(attribs["title"].c_str(), GTK_FILE_CHOOSER_ACTION_OPEN);
645 filechooser = GTK_FILE_CHOOSER_BUTTON(widget);
646 // XXXKF this is GTK+ 2.12 function, does any replacement exist?
647 gtk_signal_connect(GTK_OBJECT(widget), "file-set", G_CALLBACK(filechooser_value_changed), (gpointer)this);
648 if (attribs.count("width"))
649 gtk_widget_set_size_request (widget, get_int("width", 200), -1);
650 if (attribs.count("width_chars"))
651 gtk_file_chooser_button_set_width_chars (filechooser, get_int("width_chars"));
652 return widget;
655 void filechooser_param_control::send_configure(const char *key, const char *value)
657 // cout << "send conf " << key << endl;
658 if (attribs["key"] == key)
660 gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(filechooser), value);
664 void filechooser_param_control::filechooser_value_changed(GtkWidget *widget, gpointer value)
666 filechooser_param_control *ctl = (filechooser_param_control *)value;
667 const char *filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(ctl->filechooser));
668 if (filename)
669 ctl->gui->plugin->configure(ctl->attribs["key"].c_str(), filename);
672 // line graph
674 void line_graph_param_control::on_idle()
676 if (get_int("refresh", 0))
677 set();
680 GtkWidget *line_graph_param_control::create(plugin_gui *_gui, int _param_no)
682 gui = _gui;
683 param_no = _param_no;
684 last_generation = -1;
685 // const parameter_properties &props = get_props();
687 widget = calf_line_graph_new ();
688 CalfLineGraph *clg = CALF_LINE_GRAPH(widget);
689 widget->requisition.width = get_int("width", 40);
690 widget->requisition.height = get_int("height", 40);
691 calf_line_graph_set_square(clg, get_int("square", 0));
692 clg->source = gui->plugin->get_line_graph_iface();
693 clg->source_id = param_no;
695 return widget;
698 void line_graph_param_control::set()
700 GtkWidget *tw = gtk_widget_get_toplevel(widget);
701 if (tw && GTK_WIDGET_TOPLEVEL(tw) && widget->window)
703 int ws = gdk_window_get_state(widget->window);
704 if (ws & (GDK_WINDOW_STATE_WITHDRAWN | GDK_WINDOW_STATE_ICONIFIED))
705 return;
706 last_generation = calf_line_graph_update_if(CALF_LINE_GRAPH(widget), last_generation);
710 line_graph_param_control::~line_graph_param_control()
714 // list view
716 GtkWidget *listview_param_control::create(plugin_gui *_gui, int _param_no)
718 gui = _gui;
719 param_no = _param_no;
721 teif = gui->plugin->get_table_edit_iface();
722 const table_column_info *tci = teif->get_table_columns(param_no);
723 assert(tci);
724 cols = 0;
725 while (tci[cols].name != NULL)
726 cols++;
728 GType *p = new GType[cols];
729 for (int i = 0; i < cols; i++)
730 p[i] = G_TYPE_STRING;
731 lstore = gtk_list_store_newv(cols, p);
732 update_store();
733 widget = gtk_tree_view_new_with_model(GTK_TREE_MODEL(lstore));
734 delete []p;
735 tree = GTK_TREE_VIEW (widget);
736 assert(teif);
737 g_object_set (G_OBJECT (tree), "enable-search", FALSE, "rules-hint", TRUE, "enable-grid-lines", TRUE, NULL);
739 for (int i = 0; i < cols; i++)
741 GtkCellRenderer *cr = NULL;
743 if (tci[i].type == TCT_ENUM) {
744 cr = gtk_cell_renderer_combo_new ();
745 GtkListStore *cls = gtk_list_store_new(2, G_TYPE_INT, G_TYPE_STRING);
746 for (int j = 0; tci[i].values[j]; j++)
747 gtk_list_store_insert_with_values(cls, NULL, j, 0, j, 1, tci[i].values[j], -1);
748 g_object_set(cr, "model", cls, "editable", TRUE, "has-entry", FALSE, "text-column", 1, "mode", GTK_CELL_RENDERER_MODE_EDITABLE, NULL);
750 else {
751 bool editable = tci[i].type != TCT_LABEL;
752 cr = gtk_cell_renderer_text_new ();
753 if (editable)
754 g_object_set(cr, "editable", TRUE, "mode", GTK_CELL_RENDERER_MODE_EDITABLE, NULL);
756 g_object_set_data (G_OBJECT(cr), "column", (void *)&tci[i]);
757 gtk_signal_connect (GTK_OBJECT (cr), "edited", G_CALLBACK (on_edited), (gpointer)this);
758 gtk_signal_connect (GTK_OBJECT (cr), "editing-canceled", G_CALLBACK (on_editing_canceled), (gpointer)this);
759 gtk_tree_view_insert_column_with_attributes(tree, i, tci[i].name, cr, "text", i, NULL);
761 gtk_tree_view_set_headers_visible(tree, TRUE);
763 return widget;
766 void listview_param_control::update_store()
768 gtk_list_store_clear(lstore);
769 uint32_t rows = teif->get_table_rows(param_no);
770 for (uint32_t i = 0; i < rows; i++)
772 GtkTreeIter iter;
773 gtk_list_store_insert(lstore, &iter, i);
774 for (int j = 0; j < cols; j++)
776 gtk_list_store_set(lstore, &iter, j, teif->get_cell(param_no, i, j).c_str(), -1);
778 positions.push_back(iter);
782 void listview_param_control::send_configure(const char *key, const char *value)
784 if (attribs["key"] == key)
786 update_store();
790 void listview_param_control::on_edited(GtkCellRenderer *renderer, gchar *path, gchar *new_text, listview_param_control *pThis)
792 const table_column_info *tci = pThis->teif->get_table_columns(pThis->param_no);
793 int column = ((table_column_info *)g_object_get_data(G_OBJECT(renderer), "column")) - tci;
794 string error;
795 pThis->teif->set_cell(pThis->param_no, atoi(path), column, new_text, error);
796 if (error.empty()) {
797 pThis->update_store();
798 gtk_widget_grab_focus(pThis->widget);
799 if (atoi(path) < (int)pThis->teif->get_table_rows(pThis->param_no))
801 GtkTreePath *gpath = gtk_tree_path_new_from_string (path);
802 gtk_tree_view_set_cursor_on_cell (GTK_TREE_VIEW (pThis->widget), gpath, NULL, NULL, FALSE);
803 gtk_tree_path_free (gpath);
806 else
808 GtkWidget *dialog = gtk_message_dialog_new(pThis->gui->window->toplevel, GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK,
809 "%s", error.c_str());
810 gtk_dialog_run(GTK_DIALOG(dialog));
811 gtk_widget_destroy(dialog);
812 gtk_widget_grab_focus(pThis->widget);
816 void listview_param_control::on_editing_canceled(GtkCellRenderer *renderer, listview_param_control *pThis)
818 gtk_widget_grab_focus(pThis->widget);