Virgil Dupras avatar Virgil Dupras committed 3a62f8d

Added 08-list-store.

Comments (0)

Files changed (2)

08-list-store/main.c

+#include <gtk/gtk.h>
+
+typedef struct {
+    gchar *title;
+    gchar *author;
+    gboolean checked;
+} Book;
+
+static Book BOOKS[] = {
+    {"The Human Condition", "Arendt, Hannah", TRUE},
+    {"Les trois mousquetaires", "Dumas, Alexandre", FALSE},
+    {"De la democratie en Amerique", "De Tocqueville, Alexis", TRUE},
+    {"1984", "Orwell, George", FALSE},
+    {"The Shock Doctrine", "Klein, Naomi", TRUE},
+    {"No Logo", "Klein, Naomi", TRUE},
+    {"Le comte de Monte Christo", "Dumas, Alexandre", FALSE},
+    {"Ravages", "Barjavel, Rene", FALSE},
+    {"Souvenirs", "De Tocqueville, Alexis", FALSE},
+    NULL
+};
+
+enum
+{
+    TITLE_COLUMN,
+    AUTHOR_COLUMN,
+    CHECKED_COLUMN,
+    N_COLUMNS
+};
+
+static GtkListStore*
+create_book_list_store()
+{
+    GtkListStore *store;
+    GtkTreeIter iter;
+    int i;
+    
+    store = gtk_list_store_new(N_COLUMNS, G_TYPE_STRING, G_TYPE_STRING,
+        G_TYPE_BOOLEAN);
+
+    Book *book = &BOOKS[0];
+    while (book->title != NULL) {
+        gtk_list_store_append(store, &iter);
+        gtk_list_store_set(store, &iter,
+            TITLE_COLUMN, book->title,
+            AUTHOR_COLUMN, book->author,
+            CHECKED_COLUMN, book->checked,
+            -1);
+        book++;
+    }
+
+    return store;
+}
+
+static GtkTreeViewColumn*
+create_column(gint column_id, gchar *title, gboolean is_checkbox)
+{
+    GtkCellRenderer *renderer;
+    GtkTreeViewColumn *column;
+    gchar *data_attrname;
+
+    if (is_checkbox) {
+        renderer = gtk_cell_renderer_toggle_new();
+        data_attrname = "active";
+    }
+    else {
+        renderer = gtk_cell_renderer_text_new();
+        data_attrname = "text";
+    }
+    column = gtk_tree_view_column_new_with_attributes(title, renderer,
+        data_attrname, column_id, NULL);
+    gtk_tree_view_column_set_sort_indicator(column, TRUE);
+    gtk_tree_view_column_set_sort_column_id(column, column_id);
+
+    return column;
+}
+
+static GtkWidget*
+create_book_list_view(GtkListStore *store)
+{
+    GtkWidget *list_view;
+    GtkTreeViewColumn *column;
+
+    list_view = gtk_tree_view_new_with_model(GTK_TREE_MODEL(store));
+
+    column = create_column(TITLE_COLUMN, "Title", FALSE);
+    gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);
+    column = create_column(AUTHOR_COLUMN, "Author", FALSE);
+    gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);
+    column = create_column(CHECKED_COLUMN, "Checked", TRUE);
+    gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);
+
+    return list_view;
+}
+
+int
+main(int argc, char *argv[])
+{
+    GtkWidget *window;
+    GtkListStore *store;
+    GtkWidget *list_view;
+
+    gtk_init(&argc, &argv);
+
+    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
+    gtk_window_set_title(GTK_WINDOW(window), "List Store");
+    g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
+
+    store = create_book_list_store();
+    list_view = create_book_list_view(store);
+
+    gtk_container_add(GTK_CONTAINER(window), list_view);
+    gtk_widget_show_all(window);
+    gtk_main();
+    return 0;
+}

08-list-store/wscript

+#! /usr/bin/env python
+
+top = '.'
+out = 'build'
+
+def options(opt):
+    opt.load ('compiler_c')
+
+def configure(conf):
+    conf.load ('compiler_c')
+
+    conf.check_cfg (package='gtk+-3.0', uselib_store='GTKLIB', args='--cflags --libs')
+
+
+def build(bld):
+    app = bld (
+        features = 'c cprogram',
+        uselib   = 'GTKLIB',
+        source   = 'main.c',
+        target   = 'list-store'
+    )
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.