How to use the set_cell_data_func function in vala to change the layout of my cell depending on its contents?

I am trying to use set_cell_data_func on Gtk.TreeViewColumn . My code compiles, but when doing a segmentation error. I also tried insert_column_with_data_func , but the result is the same.

Here is my code, hope you can help me :)

 public class Application : Gtk.Window { public Application () { this.destroy.connect (Gtk.main_quit); this.set_default_size (600, 400); Gtk.ListStore store = new Gtk.ListStore (2, typeof(int), typeof(string)); Gtk.TreeIter iter; store.append (out iter); store.set (iter, 0, 0, 1, "A"); store.append (out iter); store.set (iter, 0, 1, 1, "B"); store.append (out iter); store.set (iter, 0, 0, 1, "C"); store.append (out iter); store.set (iter, 0, 0, 1, "D"); Gtk.TreeView view = new Gtk.TreeView.with_model (store); this.add(view); Gtk.CellRendererText cell = new Gtk.CellRendererText (); Gtk.TreeViewColumn col = new Gtk.TreeViewColumn.with_attributes ( "Value", cell, "text", 1); col.set_cell_data_func (cell, (Gtk.CellLayoutDataFunc)render_value); view.append_column (col); } public void render_value (Gtk.TreeViewColumn column, Gtk.CellRendererText cell, Gtk.TreeModel model, Gtk.TreeIter iter) { var val = Value (typeof(int)); model.get_value (iter, 0, out val); if ((int)val==1) (cell as Gtk.CellRendererText).background = "#b8cb04"; val.unset (); } public static int main (string[] args) { Gtk.init (ref args); Application app = new Application (); app.show_all (); Gtk.main (); return 0; } } 
+4
source share
1 answer

After debugging the translated c sources, I found an error.

vala translates

 public void render_value (Gtk.TreeViewColumn column, Gtk.CellRendererText cell, Gtk.TreeModel model, Gtk.TreeIter iter) 

from your code to the next c equivalent

 void application_render_value (Application* self, GtkTreeViewColumn* column, GtkCellRendererText* cell, GtkTreeModel* model, GtkTreeIter* iter) 

I compared this to reference documents.

There, the signature of the data function is defined as follows

 void (*GtkCellLayoutDataFunc) (GtkCellLayout *cell_layout, GtkCellRenderer *cell, GtkTreeModel *tree_model, GtkTreeIter *iter, gpointer data); 

As for the method that you implemented, this means that the arguments are shifted one right. Thus, for your func / method functions, the following applies:

  • column actually a cell renderer
  • cell is a tree model
  • model is an iterator and
  • iter is the data that is passed to the function / method

If you change the declaration of a function / data method to

 public void render_value (Gtk.CellRendererText cell, Gtk.TreeModel model, Gtk.TreeIter iter, Object data) 

everything should work fine.

Perhaps the reason for this is because CellLayoutDataFunc is defined as public delegate void . But since I don't know anything about vala , this is just a hunch. If this is not the case, you can post it on the appropriate vala mailing list.

+4
source

Source: https://habr.com/ru/post/1495368/


All Articles