Re: Working with GtkTreeView,





Phillip Phirrip ha scritto lo scorso 01/10/2004 14.16:

I need some help on how to use gtktrees. I want to create a GtkTreeView object with depth=3, i mean child iter, parent iter and grand parent iter.

I am using gtk_tree_store append, where I pass the iter and parent iter to add to the list. Is there any API to add a particular row iter as a child. I read the manual but found no APi to add. I want to add in a loop, my current loop adds in the next row, that is as a child of grand-child.
I hope I am clear.

any help, pointers or sample code would be appreciated,

Thanks,

-Phillip


Some time ago I wrote - just for exercise, so please take it with care - some code in order to understand how tree_stores and models work. Basically it is a little app showing the directory tree in a treeview (I have shortly tested it under windows only); please, forgive me if some pointers are not freed and for the fact that all comments are in italian. Of course, any comment and suggestion is welcome (but please, keep in mind that it is only a quick exercise :-) HTH.

Carlo

void CreaDirTree (GtkWidget * window1) {
    GtkWidget         * pTreeView;
    GtkTreeStore      * pTreeStore;
    GtkTreeViewColumn * pColumn;
    GtkCellRenderer   * pCellRenderer;
    GdkPixbuf         * pPixBufA;
    GdkPixbuf         * pPixBufB;
    gchar             * sPath;
    GtkTreeIter         pIterRoot;

    /* Costruzione del treestore */
pTreeStore = gtk_tree_store_new (N_COLUMN, GDK_TYPE_PIXBUF, G_TYPE_STRING);

    /* Recupero il puntatore al TreeView */
    pTreeView  = lookup_widget (GTK_WIDGET (window1), "tvPath");

    /* Caricamento delle icone */
pPixBufA = gdk_pixbuf_new_from_file("d:/Users/Carlo/Linguaggi/C_usr/GtkAW/pixmaps/Dir.ico", NULL); pPixBufB = gdk_pixbuf_new_from_file("d:/Users/Carlo/Linguaggi/C_usr/GtkAW/pixmaps/File.ico", NULL);

    //sPath = g_build_path ("/", "D:", "Users", NULL);
    sPath = g_malloc (1024);

    /* Struttura delle directory su tutti i dischi */
    g_sprintf (sPath, "C:/");
    DirWalk1L (sPath, sPath, pPixBufA, pPixBufB, pTreeStore, pIterRoot, 0);

    g_sprintf (sPath, "D:/");
    DirWalk1L (sPath, sPath, pPixBufA, pPixBufB, pTreeStore, pIterRoot, 0);

    g_sprintf (sPath, "G:/");
    DirWalker (sPath, sPath, pPixBufA, pPixBufB, pTreeStore, pIterRoot, 0);

    g_free         (sPath);
    g_object_unref (pPixBufA);
    g_object_unref (pPixBufB);

    /* Associazione del modello contenuto in TreeStore alla TreeView */
gtk_tree_view_set_model (GTK_TREE_VIEW (pTreeView), GTK_TREE_MODEL (pTreeStore));
    g_object_unref (pTreeStore);
	
/* Setta o resetta le righe alternate di colori diversi (bello nelle liste) */
    gtk_tree_view_set_rules_hint (GTK_TREE_VIEW (pTreeView), TRUE);
    //GTK_TREE_VIEW (pTreeView)->indent_expanders = TRUE;

    /* Creazione della prima colonna */
    pCellRenderer = gtk_cell_renderer_pixbuf_new ();
pColumn = gtk_tree_view_column_new_with_attributes ("Image", pCellRenderer, "pixbuf", BMP_COLUMN, NULL);
    /* Aggiunta della colonna alla TreeView */
    gtk_tree_view_append_column (GTK_TREE_VIEW (pTreeView), pColumn);
//gtk_tree_view_column_add_attribute (pColumn, pCellRenderer, "indent-expanders", 0); //gtk_tree_view_column_set_sizing (pColumn, GTK_TREE_VIEW_COLUMN_FIXED);
    //gtk_tree_view_column_set_fixed_width (pColumn, 150);

    /* Creazione della seconda colonna */
    pCellRenderer = gtk_cell_renderer_text_new();
pColumn = gtk_tree_view_column_new_with_attributes ("Label", pCellRenderer, "text", TEXT_COLUMN, NULL);

    /* Aggiunta della colonna alla TreeView */
    gtk_tree_view_append_column (GTK_TREE_VIEW (pTreeView), pColumn);
    //gtk_tree_view_expand_all (GTK_TREE_VIEW (pTreeView));
}

/*
 * DirWalker crea un TreeStore di tutte le
 * subdirectories in maniera ricorsiva; funziona
 * bene ma richiede un pò di tempo per caricarle
 * tutte.
 */
void DirWalker (gchar * sPath, gchar * sDir, GdkPixbuf * pPixBufA, GdkPixbuf * pPixBufB,
				GtkTreeStore * pTreeStore, GtkTreeIter pIterRoot, guint mode) {
    gchar             * sItem,
                      * sFile,
                      * sFile2;
    GtkTreeIter         pIter,
                        pIter2;
    GDir              * pwd;


    /* Inserzione degli elementi nel TreeStore */

    //g_print ("%s\n", sPath);
    pwd = g_dir_open (sPath, 0, NULL);

    if (pwd) {
        /* Creazione della riga principale */
        if (mode == 0) {
            /* root */
            gtk_tree_store_append (pTreeStore, & pIter, NULL);
        }
        else {
            gtk_tree_store_append (pTreeStore, & pIter, & pIterRoot);
        }
        gtk_tree_store_set (pTreeStore,  & pIter,
                            BMP_COLUMN,  pPixBufA,
                            TEXT_COLUMN, sDir,
                            -1);
        do {
            sItem = g_dir_read_name (pwd);
            if ((sItem) && (!g_strrstr (sItem, "RECYCLE"))) {
                sFile = g_build_filename (sPath, sItem, NULL);
                /* Creazione di una nuova riga */
                if (g_file_test (sFile, G_FILE_TEST_IS_DIR)) {
                    /* E' una directory */
                    sFile2 = g_strdup (sFile);
                    /* Chiamata ricorsiva */
                    DirWalker (sFile2, sItem, pPixBufA, pPixBufB,
                               pTreeStore, pIter, 1);
                    g_free (sFile2);
                }
                else {
                    /* E' un file
                    gtk_tree_store_append (pTreeStore, & pIter2, & pIter);
                    gtk_tree_store_set (pTreeStore,  & pIter2,
                                        BMP_COLUMN,  pPixBufB,
                                        TEXT_COLUMN, sItem,
                                        -1);
                    */
                }
                g_free (sFile);
            }
            //g_print ("%s\n", sItem);
        } while (sItem);
        g_dir_close (pwd);
    }
}

/*
 * A differenza di DirWalker, DirWalk1L(evel) aggiunge
 * al TreeStore un solo sottolivello. Adatto per essere
 * chiamato da un evento row_expand.
 */
void DirWalk1L (gchar * sPath, gchar * sDir, GdkPixbuf * pPixBufA,
                GdkPixbuf * pPixBufB, GtkTreeStore * pTreeStore,
                GtkTreeIter pIterRoot, guint mode) {
    gchar             * sItem,
                      * sFile,
                      * sFile2;
    GtkTreeIter         pIter,
                        pIter2;
    GDir              * pwd;


    /* Inserzione degli elementi nel TreeStore */
    pwd = g_dir_open (sPath, 0, NULL);

    if (pwd) {
        /* Creazione della riga principale */
        if (mode == 0) {
            /* root */
            gtk_tree_store_append (pTreeStore, & pIter, NULL);
    }
    else {
        gtk_tree_store_append (pTreeStore, & pIter, & pIterRoot);
    }
    gtk_tree_store_set (pTreeStore,  & pIter,
                        BMP_COLUMN,  pPixBufA,
                        TEXT_COLUMN, sDir,
                        -1);
    do {
        sItem = g_dir_read_name (pwd);
            if ((sItem) && (!g_strrstr (sItem, "RECYCLE"))) {
                sFile = g_build_filename (sPath, sItem, NULL);
                /* Creazione di una nuova riga */
                if (g_file_test (sFile, G_FILE_TEST_IS_DIR)) {
                    /* E' una directory */
                    gtk_tree_store_append (pTreeStore, & pIter2, & pIter);
                    gtk_tree_store_set (pTreeStore,  & pIter2,
                                        BMP_COLUMN,  pPixBufA,
                                        TEXT_COLUMN, sItem,
                                        -1);
                }
                else {
                    /* E' un file
                    gtk_tree_store_append (pTreeStore, & pIter2, & pIter);
                    gtk_tree_store_set (pTreeStore,  & pIter2,
                                        BMP_COLUMN,  pPixBufB,
                                        TEXT_COLUMN, sItem,
                                        -1);
                    */
                }
                g_free (sFile);
            }
            //g_print ("%s\n", sItem);
        } while (sItem);
        g_dir_close (pwd);
    }
}






[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]