2

I've wired all GtkCellRendererText that I want to edit to on_follow_ups_column_edited callback. How do I know which column did the signal come from?

The signal only needs two parameters: path and data. path returns the row number, while data returns the new number. My work around is to use editing_started signal to get the value and compare that with the column value to get the column. Obviously, it won't work effectively if columns have the same values.

Builder snippet:

<child>
  <object class="GtkTreeViewColumn" id="date">
    <property name="sizing">autosize</property>
    <property name="min_width">100</property>
    <property name="title" translatable="yes">Date</property>
    <property name="clickable">True</property>
    <property name="sort_indicator">True</property>
    <child>
      <object class="GtkCellRendererText" id="follow_ups_column_date">
        <property name="editable">True</property>
        <signal name="edited" handler="on_follow_ups_column_edited" swapped="no"/>
        <signal name="editing-started" handler="on_follow_ups_column_editing_started" swapped="no"/>
      </object>
      <attributes>
        <attribute name="text">1</attribute>
      </attributes>
    </child>
  </object>
</child>

Vala snippet:

        [GtkCallback]
        private void on_follow_ups_column_editing_started (Gtk.CellEditable editable, string path)
        {
            Gtk.Entry e = (Gtk.Entry) editable;
            follow_up_editable_column_previous_value = e.get_text();
        }

        [GtkCallback]
        private void on_follow_ups_column_edited(string path, string data)
        {
            Gtk.TreePath tPath = new Gtk.TreePath.from_string(path);
            var model = follow_up_list;
            Gtk.TreeIter iter;
            var res = model.get_iter(out iter, tPath);
            GLib.Value v;
            if (res == true) {
                for (var i = 0; i < 5; ++i)
                {
                    follow_up_list.get_value(iter, i, out v);
                    if (v == follow_up_editable_column_previous_value)
                    {
                        follow_up_list.set(iter, i, data);
                        break;
                    }
                }
            }
        }

I also tried adding data="date" to <signal name="edited" handler="on_follow_ups_column_edited" object="date" swapped="no"/> (through Builder/glade editor) and it does crash when trying to edit a cell.

https://en.wikibooks.org/wiki/GTK%2B_By_Example/Tree_View/Editable_Cells says it can add gpointer user_data but in vala (https://valadoc.org/gtk+-3.0/Gtk.CellRendererText.edited.html) edited only accepts 2 parameters. If I add another parameter it says:

window.vala:297.9-297.48: error: method `Pcc.Window.on_follow_ups_column_edited' is incompatible with signal `Gtk.CellRendererText.edited', expected `void on_follow_ups_column_edited (Gtk.CellRendererText, string, string)'
        private void on_follow_ups_column_edited(string path, string data, string hehe)
Shem Pasamba
  • 101
  • 1
  • 4

1 Answers1

2

It finally works for me after reading and understanding: https://en.wikibooks.org/wiki/GTK%2B_By_Example/Tree_View/Editable_Cells

  1. Set "column" data of the Gtk.CellRenderer:
            int c = 0;
            foreach(Gtk.TreeViewColumn col in this.follow_ups.get_columns())
            {
                foreach(Gtk.CellRenderer cell in col.get_cells())
                    cell.set_data("column", c);
                c++;
            }
  1. Then at the callback, get that "column" data of from the renderer. Note: Even though the documentation here doesn't show it, you may add Gtk.CellRenderer renderer as the first parameter to get the renderer as per this documentation.
        [GtkCallback]
        private void on_follow_ups_column_edited(Gtk.CellRenderer renderer, string path, string data)
        {
            int column = renderer.get_data<int>("column");
            Gtk.TreePath tPath = new Gtk.TreePath.from_string(path);
            var model = follow_up_list;
            Gtk.TreeIter iter;
            var res = model.get_iter(out iter, tPath);
            if (res == true) {
                follow_up_list.set(iter, column, data);
            }
        }
Shem Pasamba
  • 101
  • 1
  • 4