GTK3 : TreeView and ListStore problem

Hello everyone,
I’m trying to create a ListView Widget and I have an issue. The widget is being created correctly but when I try to populate the ListStore using gtk_list_store_set_value() instruction even if the GValue structure is initialized and filled with the information I would like to use to populate the ListStore the rows in the listview remain empty.

This is the way I’m creating the widget :

  Procedure.i New(ColumnNames.s)
    
    *This.Private_Members = AllocateStructure(Private_Members)
    *This\VirtualTable = ?START_METHODS
    
    *This\ScrolledWindowID = gtk_scrolled_window_new_(#Null, #Null)
    
    gtk_scrolled_window_set_shadow_type_(*This\ScrolledWindowID, #GTK_SHADOW_IN)
    
    ColumnMax.l = CountString(ColumnNames, Chr(10))
    
    *GTYPE = AllocateMemory((ColumnMax+1) * SizeOf(Integer))
    
    FillMemory(*GTYPE, (ColumnMax+1) * SizeOf(Integer), #G_TYPE_STRING, #PB_Integer)
    
    *ListStore = gtk_list_store_newv_(ColumnMax+1, *GTYPE)
    FreeMemory(*GTYPE)
    
    *This\ListViewID = gtk_tree_view_new_with_model_(*ListStore)
    g_object_unref_(*ListStore)
    
    gtk_container_add_(*This\ScrolledWindowID, *This\ListViewID)
    
;     *Cursor = *GTYPE
    
    For ColumnID = 0 To ColumnMax
      
      *CellRenderer = gtk_cell_renderer_text_new_()
      *TreeViewColumnID = gtk_tree_view_column_new_()
      gtk_tree_view_column_add_attribute_(*TreeViewColumnID, *CellRenderer, "text", ColumnID)
      gtk_tree_view_column_set_title_(*TreeViewColumnID, StringField(ColumnNames, ColumnID+1, Chr(10)))
      gtk_tree_view_append_column_(*This\ListViewID, *TreeViewColumnID)
      
;       Debug Str(PeekI(*Cursor)) + " vs #G_TYPE_STRING (" + Str(#G_TYPE_STRING) + ")"
;       *Cursor + SizeOf(Integer)
      
    Next

    gtk_tree_view_set_grid_lines(*This\ListViewID, 3)
    
    ProcedureReturn *This
  EndProcedure

And this is the way I’m trying to populate the ListView :

  Procedure AppendItem(*This.Private_Members, Definition.s)
    
    Protected Iter.GtkTreeIter, Value.GValue
    
    *ListStore = gtk_tree_view_get_model_(*This\ListViewID)
    
    gtk_list_store_append_(*ListStore, @Iter)
    
    If gtk_list_store_iter_is_valid_(*ListStore, @Iter)
      
      ColumnMax.l = CountString(Definition, Chr(10))
      
      For ColumnID = 0 To ColumnMax 
        
        CellValue.s = StringField(Definition, ColumnID+1, Chr(10))
        g_value_init_(@Value, #G_TYPE_STRING)
        g_value_set_string_(@Value, CellValue)
        
        *ExtractedValue = g_value_get_string_(@Value)
        
        If *ExtractedValue <> #Null
          Debug CellValue + " vs " + PeekS(*ExtractedValue, -1, #PB_UTF8)
        Else
          Debug CellValue + " vs NUll Pointer"
        EndIf
        
        gtk_list_store_set_value(*ListStore, @Iter, ColumnID, @Value)
        
      Next
      
    EndIf
    
  EndProcedure

Just for the record, the programming language I’m using is PureBasic and unfortunately as far as I know this compiler as no problem dealing C functions libraries as long as those functions don’t have a variable number of argument. So telling me that I should use the gtk_list_store_new () or gtk_list_store_set () function instead, you are not helping.

My guess is the function gtk_list_store_set_value() is broken but I’m not sure. Any help will be appreciated.

Best regards

Have you tried asking the people maintaining the PureBasic bindings? I’m fairly sure they don’t read Discourse—unless it was them who sent you here.

Hello Emmanuele,

THe problem I’m facing with PureBasic is the fact that under Linux the Gadget library supplied with the language wrap the GTK3 library. But when you are using the GTK3 library directly like I do, you are on your own. I have posted in the official Purebasic forum days ago and don’t have any response so far.

GtkTreeView Example Partially working

I have hoped someone here were capable to point me any mistake I could have done.

Best regards

Hello again,

I have spent some time re-writing my previous example in C using the same function I have used in PureBasic. I have some Gtk-CRITICAL**: gtl_cell_area_attribute_connect : assertion ‘gtk_cell_area_has_renderer (area, renderer)’ failed

If someone want to try for them self, there is the code.

#include <stdlib.h>
#include <gtk/gtk.h>
#include <glib.h>

int main (int argc, char *argv[])
{

    enum {
        COLUMN_FILENAME,
        COLUMN_STATUS,
        N_COLUMNS
    };

    GtkWidget *win = NULL;
    GtkWidget *Scrollwin = NULL;
    GtkWidget *ListView = NULL;
    GtkCellRenderer *CellRenderer = NULL;
    GtkTreeViewColumn *TreeViewColumnID = NULL;
    GtkTreeModel *list_store = NULL;

    /* Initialize GTK+ */
    /*g_log_set_handler ("Gtk", G_LOG_LEVEL_WARNING, (GLogFunc) gtk_false, NULL);*/
    gtk_init (&argc, &argv);
    /*g_log_set_handler ("Gtk", G_LOG_LEVEL_WARNING, g_log_default_handler, NULL);*/

    /* Create the main window */
    win = gtk_window_new (GTK_WINDOW_TOPLEVEL);
    gtk_container_set_border_width (GTK_CONTAINER (win), 8);
    gtk_window_set_title (GTK_WINDOW (win), "Hello World");
    gtk_window_set_position (GTK_WINDOW (win), GTK_WIN_POS_CENTER);
    gtk_window_set_default_size(GTK_WINDOW (win), 400, 300);
    gtk_container_set_border_width(win, 5);
    //gtk_widget_realize (win);

    g_signal_connect (win, "destroy", gtk_main_quit, NULL);

    Scrollwin = gtk_scrolled_window_new(NULL, NULL);
    gtk_scrolled_window_set_shadow_type(Scrollwin, GTK_SHADOW_IN);

    gint64 GTYPES[N_COLUMNS];
    GTYPES[COLUMN_FILENAME] = G_TYPE_STRING;
    GTYPES[COLUMN_STATUS] = G_TYPE_STRING;

    list_store = gtk_list_store_newv(N_COLUMNS, GTYPES);

    GValue Value;
    g_value_init(&Value, G_TYPE_STRING);

    GtkTreeIter Iter;

    gtk_list_store_append(GTK_LIST_STORE(list_store), &Iter);

    if(gtk_list_store_iter_is_valid(GTK_LIST_STORE(list_store), &Iter))
    {

        g_value_set_string(&Value, "Vector2f - OOP.pb");
        gtk_list_store_set_value(GTK_LIST_STORE(list_store), &Iter, COLUMN_FILENAME, &Value);

        g_value_set_string(&Value, "OK");
        gtk_list_store_set_value(GTK_LIST_STORE(list_store), &Iter, COLUMN_STATUS, &Value);

    }

    gtk_list_store_append(GTK_LIST_STORE(list_store), &Iter);

    if(gtk_list_store_iter_is_valid(GTK_LIST_STORE(list_store), &Iter))
    {

        g_value_set_string(&Value, "Vector3f - OOP.pb");
        gtk_list_store_set_value(GTK_LIST_STORE(list_store), &Iter, COLUMN_FILENAME, &Value);

        g_value_set_string(&Value, "OK");
        gtk_list_store_set_value(GTK_LIST_STORE(list_store), &Iter, COLUMN_STATUS, &Value);

    }

    gtk_list_store_append(GTK_LIST_STORE(list_store), &Iter);

    if(gtk_list_store_iter_is_valid(GTK_LIST_STORE(list_store), &Iter))
    {
        g_value_set_string(&Value, "Vector4f - OOP.pb");
        gtk_list_store_set_value(GTK_LIST_STORE(list_store), &Iter, COLUMN_FILENAME, &Value);

        g_value_set_string(&Value, "OK");
        gtk_list_store_set_value(GTK_LIST_STORE(list_store), &Iter, COLUMN_STATUS, &Value);

    }

    ListView = gtk_tree_view_new();
    gtk_tree_view_set_model(ListView, list_store);
    gtk_tree_view_set_grid_lines(ListView, 3);

    gtk_container_add(Scrollwin, ListView);

    CellRenderer = gtk_cell_renderer_text_new();
    TreeViewColumnID = gtk_tree_view_column_new();
    gtk_tree_view_column_add_attribute(TreeViewColumnID, CellRenderer, "text", 0);
    gtk_tree_view_append_column(ListView, TreeViewColumnID);
    gtk_tree_view_column_set_title(TreeViewColumnID, "FileName");

    CellRenderer = gtk_cell_renderer_text_new();
    TreeViewColumnID = gtk_tree_view_column_new();
    gtk_tree_view_column_add_attribute(TreeViewColumnID, CellRenderer, "text", 1);
    gtk_tree_view_append_column(ListView, TreeViewColumnID);
    gtk_tree_view_column_set_title(TreeViewColumnID, "Status");

    gtk_container_add(win, Scrollwin);

    /* Enter the main loop */
    gtk_widget_show_all (win);
    gtk_main ();
    return 0;
}

Best regards

You can fix the C version with a couple of changes. No idea how they would translate to PureBasic!

The reason for the failed assertion is that the GtkCellRenderer needs to be packed into the GtkTreeViewColumn before you can use gtk_tree_view_column_add_attribute().

Also, because your GValue is stack-allocated, it needs to be cleared initially:

    GValue Value = G_VALUE_INIT;

(and you still need to use g_value_init(), of course!).

Thanks PeterB,

Yeah I didn’t use the gtk_tree_view_column_pack_start() instructions this was a part of the problem.

The other part of the problem was the use of the g_value_set_string() instruction. The trick was to do this :

          CellValue.s = StringField(Definition, ColumnID+1, Chr(10))
          CellValueUTF8.s = Space(StringByteLength(CellValue, #PB_UTF8))
          PokeS(@CellValueUTF8, CellValue, -1, #PB_UTF8)
          Value\data\v_pointer = @CellValueUTF8

Basically converting the Unicode string PureBasic uses into UTF8 string then setting the address of that string manually into the GValue structure.

Oh by the way, in PureBasic we can’t have structured constant, so a code like that has no equivalent :

GValue Value = G_VALUE_INIT

Each time a structured variable is created in the code, that structured variable is initialize with zeros everywhere. That being said thanks for your input.

Best regards

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.