GtkTreeView – Part I

#include <gtk/gtk.h>

static GtkListStore *create_model ( void )
{
    GtkListStore *model;
    GtkTreeIter iter;

    /// ***
    model = gtk_list_store_new ( 1, G_TYPE_STRING );

    /// ***
    for ( gint i = 1; i <= 10; i++ )
    {
        gchar *msg = g_strdup_printf ( "Row %d", i );

        /// ***
        gtk_list_store_append ( GTK_LIST_STORE ( model ), &iter );
        gtk_list_store_set    ( GTK_LIST_STORE ( model ), &iter, 0, msg, -1 );

        /// ***
        g_free ( msg );
    }

    /// ***
    return model;
}

static void activate_clbk ( GtkApplication *application )
{
    GtkWidget *window;
    GtkWidget *grid;

    /// ***
    GtkListStore      *model;
    GtkCellRenderer   *renderer;

    /// ***
    GtkWidget *tree_view;

    /// ***
    window = gtk_application_window_new   ( application );
    gtk_window_set_default_size ( GTK_WINDOW ( window ), 400, 400 );

    /// ***
    grid = gtk_grid_new ();

    /// ***
    model = create_model();

    /// ***
    renderer = gtk_cell_renderer_text_new ();

    /// ***
    tree_view = gtk_tree_view_new();

    /// ***
    gtk_tree_view_set_model ( GTK_TREE_VIEW ( tree_view ), GTK_TREE_MODEL ( model ) );

    /// *** Create the First Column
    ///gtk_tree_view_append_column    ( GTK_TREE_VIEW ( tree_view ), column );
    ///gtk_tree_view_insert_column ( GTK_TREE_VIEW( tree_view ), column, 0 );
    gtk_tree_view_insert_column_with_attributes ( GTK_TREE_VIEW ( tree_view ), 0, "First Column", renderer, "text", 0, NULL );

    /// *** Create the Second Column
    renderer = gtk_cell_renderer_text_new ();
    gtk_tree_view_insert_column_with_attributes ( GTK_TREE_VIEW ( tree_view ), 0, "First Column", renderer, "text", 0, NULL );

    /// ***
    gtk_grid_attach ( GTK_GRID ( grid ), tree_view, 0, 0, 1, 1 );

    /// ***
    gtk_window_set_child ( GTK_WINDOW ( window ), grid );

    /// ***
    gtk_window_present ( GTK_WINDOW ( window ) );
}

int main ( void )
{
    GtkApplication *application;
    gint status;

    /// ***
    application = gtk_application_new ( "this.is.my-nice.app", G_APPLICATION_FLAGS_NONE );

    /// ***
    g_signal_connect ( application, "activate", G_CALLBACK ( activate_clbk ), NULL );

    /// ***
    status = g_application_run ( G_APPLICATION ( application ), FALSE, NULL );

    /// ***
    g_object_unref ( application );
    return status;
}
Author: MichaelB

Leave a Reply

Your email address will not be published. Required fields are marked *