2

based on this exemple

example-columnview-with-strings-gtk4-c

I test this new widget with my data below a mwe code in order to select part with error

I have an error when I implement 7 callcak "bind"

#include <gtk/gtk.h>
#include <string.h>
#include <test_ColumnView.h>

struct _BlocLatexItem
{
    GObject parent_instance;

    const  char  *family_name;
    const  char  *titre;
    const  char  *information;
    const  char  *commentaires;
    const  char  *num_bloc_audit;
    const  char  *date;
    const  char  *usage;
};

G_DEFINE_TYPE (BlocLatexItem, bloclatex_item, G_TYPE_OBJECT)

//Initialize the template children when initializing the widgdet instance
static void bloclatex_item_init(BlocLatexItem *item)
{
}
//Clear the template children when disposing the widget instance
static void bloclatex_item_dispose (GObject *gobject)
{
    gtk_widget_dispose_template (GTK_WIDGET (gobject), BLOCLATEX_TYPE_ITEM);

    G_OBJECT_CLASS (bloclatex_item_parent_class)->dispose (gobject);
}

static void bloclatex_item_class_init(BlocLatexItemClass *class)
{
    /* le mini syndical est de prévoir le destructeur de la classe */
    G_OBJECT_CLASS (class)->dispose = bloclatex_item_dispose;
}

static BlocLatexItem * bloclatex_item_new(const char *family_name,
                const char *titre,
                const char *information,
                const char *commentaires,
                const char *num_bloc_audit,
                const char *date,
                const char *usage )
{
    BlocLatexItem  *item = g_object_new(BLOCLATEX_TYPE_ITEM, NULL);
    
    item->family_name = g_strdup(family_name);
    item->titre = g_strdup(titre);
    item->information = g_strdup(information);
    item->commentaires = g_strdup(commentaires);
    item->num_bloc_audit = g_strdup(num_bloc_audit);
    item->date = g_strdup(date);
    item->usage = g_strdup(usage);
    
    return item;
}


static const char* family_name_item_get_name(BlocLatexItem *item)
{
    return item->family_name;
}

static const char* titre_item_get_name(BlocLatexItem *item)
{
    return item->titre;
}

static const char* information_item_get_name(BlocLatexItem *item)
{
    return item->information;
}

static const char* commentaires_item_get_name(BlocLatexItem *item)
{
    return item->commentaires;
}

static const char* num_bloc_audit_item_get_name(BlocLatexItem *item)
{
    return item->num_bloc_audit;
}

static const char* date_item_get_name(BlocLatexItem *item)
{
    return item->date;
}

static const char* usage_item_get_name(BlocLatexItem *item)
{
    return item->usage;
}

/* We could play the same game with the setup function, to produce a custom
 * widget type for the items in each column, but for now we'll just use a
 * GtkLabel for all items in the view.
 */
static void factory_setup( GtkListItemFactory *factory, GtkListItem *list_item )
{
    GtkWidget *label = gtk_label_new( "" );
    gtk_list_item_set_child( list_item, label );
}

static void factory_bind_family_name(GtkSignalListItemFactory *factory, GtkListItem *listitem)
//static void factory_bind_titre(GtkListItemFactory *factory, GtkListItem *listitem)
{
    //on récupère le widget ici un label issu du modèle de présentation
    GtkWidget *label = gtk_list_item_get_child(listitem);
    
    // on récupère la postion de la rangée de donnée du modèle// magasin
    GObject *item = gtk_list_item_get_item(GTK_LIST_ITEM(listitem));
    
    //on extrait la donnée titre ici pour l'affecter au label pour l'afficher
    const char *string = family_name_item_get_name(BlocLatexItem(item));
    gtk_label_set_text(GTK_LABEL (label), string);
}

static void factory_bind_titre(GtkSignalListItemFactory *factory, GtkListItem *listitem)
//static void factory_bind_titre(GtkListItemFactory *factory, GtkListItem *listitem)
{
    //on récupère le widget ici un label issu du modèle de présentation
    GtkWidget *label = gtk_list_item_get_child(listitem);
    
    // on récupère la postion de la rangée de donnée du modèle// magasin
    GObject *item = gtk_list_item_get_item(GTK_LIST_ITEM(listitem));
    
    //on extrait la donnée titre ici pour l'affecter au label pour l'afficher
    const char *string = titre_item_get_name(BlocLatexItem(item));
    gtk_label_set_text(GTK_LABEL (label), string);
}

static void factory_bind_information(GtkListItemFactory *factory, GtkListItem *listitem)
{
    GtkWidget *label = gtk_list_item_get_child(listitem);
    GObject *item = gtk_list_item_get_item(GTK_LIST_ITEM(listitem));
    const char *string = information_item_get_name(BlocLatexItem(item));
    gtk_label_set_text(GTK_LABEL (label), string);
}

static void factory_bind_commentaires(GtkSignalListItemFactory *factory, GtkListItem *listitem)
{
    GtkWidget *label = gtk_list_item_get_child(listitem);
    GObject *item = gtk_list_item_get_item(GTK_LIST_ITEM(listitem));
    const char *string = commentaires_item_get_name(BlocLatexItem(item));
    gtk_label_set_text(GTK_LABEL (label), string);
}

static void factory_bind_num_bloc_audit(GtkSignalListItemFactory *factory, GtkListItem *listitem)
{
    GtkWidget *label = gtk_list_item_get_child(listitem);
    GObject *item = gtk_list_item_get_item(GTK_LIST_ITEM(listitem));
    const char *string = num_bloc_audit_item_get_name(BlocLatexItem(item));
    gtk_label_set_text(GTK_LABEL (label), string);
}

static void factory_bind_date(GtkSignalListItemFactory *factory, GtkListItem *listitem)
{
    GtkWidget *label = gtk_list_item_get_child(listitem);
    GObject *item = gtk_list_item_get_item(GTK_LIST_ITEM(listitem));
    const char *string = date_item_get_name(BlocLatexItem(item));
    gtk_label_set_text(GTK_LABEL (label), string);
}

static void factory_bind_usage(GtkSignalListItemFactory *factory, GtkListItem *listitem)
{
    GtkWidget *label = gtk_list_item_get_child(listitem);
    GObject *item = gtk_list_item_get_item(GTK_LIST_ITEM(listitem));
    const char *string = usage_item_get_name(BlocLatexItem(item));
    gtk_label_set_text(GTK_LABEL (label), string);
}
                   
GListModel* creer_bloclatex_model(void) 
{
    GListStore *bloclatex_store = g_list_store_new (BLOCLATEX_TYPE_ITEM);
    /* boucle de lecture du fichier */
    
    g_list_store_append(bloclatex_store, bloclatex_item_new("boite à listing","boite à listing C sécable",
    "boites pour gérer du code en language C avec indexation automatique du vocabulaire de Gtk.avec titre Clisting, sans titre Clisting*","comment1","1","23/1/2023","100"));
    
    g_list_store_append(bloclatex_store, bloclatex_item_new("boite à listing","boite à listing latex sécable",
    "boites pour gérer du code en language latex avec indexation automatique du vocabulaire de Gtk.avec titre latex, sans titre latex*","comment12","1","23/1/2023","100"));
        
    g_list_store_append(bloclatex_store, bloclatex_item_new("family_name","titre3","information","commentaires","1","23/1/2023","100"));
    g_list_store_append(bloclatex_store, bloclatex_item_new("family_name","titre4","information","commentaires","1","23/1/2023","100"));
    g_list_store_append(bloclatex_store, bloclatex_item_new("family_name","titre5","information","commentaires","1","23/1/2023","100"));
    g_list_store_append(bloclatex_store, bloclatex_item_new("family_name","titre6","information","commentaires","1","23/1/2023","100"));
    g_list_store_append(bloclatex_store, bloclatex_item_new("family_name","titre7","information","commentaires","1","23/1/2023","100"));

    return  G_LIST_MODEL(bloclatex_store);
}

// Fonction pour configurer le GtkColumnView
GtkWidget* configurer_columnview(GtkWidget *columnview) 
{
   
    GtkColumnViewColumn* column= NULL;
    

    char *column_names[] = { "Famille", "titre", "Information","Commentaires","num_bloc_audit","date","usage" };

    /* Initialize the array of GtkListItemFactory - one for each column.
     */
    GtkListItemFactory *factories[4];

    /* Connect handlers to the factories.
     */
    for ( int i = 0; i < 7; i++ ) 
    {
        factories[i] = gtk_signal_list_item_factory_new();
        g_signal_connect( factories[i], "setup", G_CALLBACK( factory_setup ), NULL );
    }
    
    g_signal_connect( factories[0], "bind", G_CALLBACK( factory_bind_family_name ), NULL);
    g_signal_connect( factories[1], "bind", G_CALLBACK( factory_bind_titre ), NULL);
    g_signal_connect( factories[2], "bind", G_CALLBACK( factory_bind_information ), NULL );
    g_signal_connect( factories[3], "bind", G_CALLBACK( factory_bind_commentaires ), NULL);
    g_signal_connect( factories[4], "bind", G_CALLBACK( factory_bind_num_bloc_audit ), NULL);
    g_signal_connect( factories[5], "bind", G_CALLBACK( factory_bind_date ), NULL);
    g_signal_connect( factories[6], "bind", G_CALLBACK( factory_bind_usage ), NULL);

    /* Create the columns and  add to the columnview
     */
    for ( int i = 0; i < 4; i++ ) {
        column = gtk_column_view_column_new( column_names[i], factories[i] );
        gtk_column_view_append_column( GTK_COLUMN_VIEW( columnview ), column );
    }
    return columnview;
}

static void activate (GtkApplication *app,gpointer user_data)
{
    // Créer le ListStore retourné dans le modele bloclatex_model 
    GListModel *bloclatex_model = creer_bloclatex_model();
    GtkSingleSelection *selection= gtk_single_selection_new(G_LIST_MODEL(bloclatex_model));
    gtk_single_selection_set_autoselect(selection,TRUE);

    // Créer le ColumnView
    GtkWidget *columnview = gtk_column_view_new(GTK_SELECTION_MODEL (selection)); //columnview alias cv

    columnview = configurer_columnview(columnview);

    // Créer une fenêtre et ajouter le ColumnView ds le scroll
    GtkWidget *window = gtk_application_window_new(app);
    gtk_window_set_default_size(GTK_WINDOW(window), 600, 400);
    gtk_window_set_title (GTK_WINDOW(window), "Proto bloclatex");

    GtkWidget * scrolled_window = gtk_scrolled_window_new ();
    gtk_window_set_child (GTK_WINDOW(window), scrolled_window);

    //gtk_window_set_child(GTK_WINDOW(window), columnview);
    gtk_scrolled_window_set_child(GTK_SCROLLED_WINDOW(scrolled_window),columnview);

    gtk_window_present (GTK_WINDOW(window));
}

int main (int    argc, char **argv)
{
    GtkApplication *app;
    app = gtk_application_new ("org.gtk.gtk_xml", G_APPLICATION_DEFAULT_FLAGS);
    g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
    int status = g_application_run (G_APPLICATION (app), argc, argv);
    g_object_unref (app);

    return status;
}

and the header file

/* inclusion guard */
#pragma once

#include <glib-object.h>

/*
 * Potentially, include other headers on which this header depends.
 */

G_BEGIN_DECLS

/*
 * Type declaration.
 */
 
#define BLOCLATEX_TYPE_ITEM (bloclatex_item_get_type())

G_DECLARE_FINAL_TYPE (BlocLatexItem, bloclatex_item, BLOCLATEX, ITEM, GObject)

struct _BlocLatexItemClass
{
    GObjectClass parent_class;
};

G_END_DECLS

I tested the code given in the link with success. After more test googleize and check in devhelp a don't understand what it's expected with my callback "bind". I gave only one parameter like on the test code.

for each 7 column i have this message, what is about?

../test_ColumnView.c: In function ‘factory_bind_family_name’:
../test_ColumnView.c:115:52: error: expected expression before ‘BlocLatexItem’
  115 |     const char *string = family_name_item_get_name(BlocLatexItem(item));
      |                                                    ^~~~~~~~~~~~~
../test_ColumnView.c:112:14: warning: unused variable ‘item’ [-Wunused-variable]
  112 |     GObject *item = gtk_list_item_get_item(GTK_LIST_ITEM(listitem));

1 Answer 1

0

Thank you for using my example. In your script I have been able to find three errors so far.

  1. the integration of the header file.
#include "test-columnview.h"
  1. the casting of item.
  //const char *string = titre_item_get_name(BlocLatexItem(item));
    const char *string = titre_item_get_name(BLOCLATEX_ITEM(item));

// or

const char *string = commentaires_item_get_name((BlocLatexItem*)item);

Then the script can be compiled.

However, there is still an error in the execution:

(_column-view:9930): Pango-WARNING **: 10:46:16.623: Invalid UTF-8 string passed to pango_layout_set_text()

(_column-view:9930): Pango-WARNING **: 10:46:16.635: Invalid UTF-8 string passed to pango_layout_set_text()

This may be related to localisation.

I like to look again later on :-).

No, the error is not in the localization.

/* Initialize the array of GtkListItemFactory - one for each column.
     */
    GtkListItemFactory *factories[7];  // Not 4 !!

Have fun programming.

Sign up to request clarification or add additional context in comments.

1 Comment

OK Thank you for your answer. I testing this élément NEXT Day. I forgot to check the header.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.