Re: Working With Trees



On Thu, 2003-05-22 at 00:30, Charles Tassell wrote:
> Doesn't make any difference, I've tried the code below, and it seems to
> do the same thing.  Am I wrong here, or is the GtkTreeStore type not
> compatible with GtkTreeModel?  From the docs, I think I should be able
> to use a GtkTreeStore pointer with any function that takes a
> GtkTreeModel, but maybe I'm misreading something.
> 
> int SetFolders(void)
> {
>   GtkTreeStore    *tstoreFolders;
>   GtkTreeIter     iterStore, iterTest;
>   char            *Test, *readTest;
>   
>   /* Create test strings */
>   Test = malloc(100);
>   memset(Test, 0, 100);
>   strcpy(Test, "This is a test");
> 
>   readTest = malloc(100);
>   memset(readTest, 0, 100);

How about replacing the above two lines with:

gchar *readTest;

>   /* Create the storage area */
>   tstoreFolders = gtk_tree_store_new(NUM_COLUMNS, G_TYPE_STRING);
> 
>   /* Add the test entry */
>   gtk_tree_store_append(GTK_TREE_MODEL(tstoreFolders), &iterStore,
> NULL);
>   gtk_tree_store_set(GTK_TREE_MODEL(tstoreFolders), &iterStore,
> FOLDER_NAME, Test, -1);
> 
>   
>   /* Read the value we just wrote back */
>   if (!gtk_tree_model_get_iter_first(GTK_TREE_MODEL(tstoreFolders),
> &iterTest)) {
>     printf("Couldn't get first iterator\n");
>   }
>   gtk_tree_model_get(GTK_TREE_MODEL(tstoreFolders), &iterTest,
> FOLDER_NAME, readTest, -1);
>   printf("Test is %s\n", readTest);

and adding:

g_free (readTest);

>   return 0;
> }

I've also attached a C file that show how to create a GtkTreeStore,
populate it, and retrieve the data.  It also does some other stuff like
setting editable cells.  Tested on Red Hat 9.  Might be of some help...

Keith.
/*
  Example Code for GtkTreeStore and GtkTreeView Tutorial Part 2
  =============================================================

  This example shows how to create a tree model, add
  some data to the model, and then extract some data
  from the model, all while displaying on the screen.

  To compile this:

  gcc -o example `pkg-config --libs gtk+-2.0 --cflags gtk+-2.0` example.c

*/

#include <gtk/gtk.h>

enum {
  FIRSTNAME = 0,
  LASTNAME,
  EDITABLE
};

/* Standard callback to halt the application correctly 
   when closed using the Window Manager */
gint delete_event_cb (GtkWidget *widget, GdkEvent *event, gpointer data) {
  gtk_main_quit ();
  return FALSE;
}

/* This function creates a GTK_TREE_STORE object and populates
   it with some data */
GtkTreeModel *create_and_populate_model () {

  GtkTreeStore *treestore;
  GtkTreeIter parent, child;

  /* Create the new Tree Store object, the arguments to the function
     are the number of columns and the then the type of each column.
     In this example we have two columns, both of type string */
  treestore = gtk_tree_store_new (3, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_BOOLEAN);

  /* Next we need to create some new rows, and then populate them
     with data, the structure we create is:

           TOP LEVEL
           - Jane Doe
	         - Susan Clark
		 - Babs Jensen
	   - John Smith
  */

  /* The first row at the top level */
  gtk_tree_store_append (treestore, &parent, NULL);
  gtk_tree_store_set (treestore, &parent, FIRSTNAME, "Jane", LASTNAME, "Doe", EDITABLE, TRUE, -1);

  /* The first child row of the first row at the top level */
  gtk_tree_store_append (treestore, &child, &parent);
  gtk_tree_store_set (treestore, &child, FIRSTNAME, "Susan", LASTNAME, "Clark", EDITABLE, TRUE, -1);

  /* The second child row of the first row at the top level */
  gtk_tree_store_append (treestore, &child, &parent);
  gtk_tree_store_set (treestore, &child, FIRSTNAME, "Babs", LASTNAME, "Jensen", EDITABLE, TRUE, -1);

  /* The second row at the top level */
  gtk_tree_store_append (treestore, &parent, NULL);
  gtk_tree_store_set (treestore, &parent, FIRSTNAME, "John", LASTNAME, "Smith", EDITABLE, TRUE, -1);

  /* And finally return our model */
  return GTK_TREE_MODEL(treestore);
}

/* This function is called recursively to extract all the data from
   the GtkTreeModel */
void dump_tree_data (GtkTreeModel *model, GtkTreeIter iter) {

  GtkTreeIter child;
  gchar *first, *last;

  /* Dump data from this node */
  gtk_tree_model_get (model, &iter, 0, &first, -1);
  gtk_tree_model_get (model, &iter, 1, &last, -1);
  g_print ("Firstname: %s, Lastname: %s\n", first, last);
  g_free (first);
  g_free (last);

  /* Check if this node has a child and if so recursively call
     this function again for that child */
  if(gtk_tree_model_iter_children (model, &child, &iter)) {
    dump_tree_data (model, child);
  }
  /* Next, check if this node has a sibling and if so recursively
     call this function again for that sibling */
  if (gtk_tree_model_iter_next (model, &iter)) {
    dump_tree_data (model, iter);
  }
  return;
}

/* The callback for the editing of text in our GtkTreeView */
void lastname_edited (GtkCellRendererText *cell, gchar *path_string,
		      gchar *new_text, gpointer data) {

  GtkTreeModel *treemodel = (GtkTreeModel *)data;
  GtkTreeIter iter;

  /* Convert the string path to the row that has changed to a GtkIter */
  gtk_tree_model_get_iter (treemodel, &iter, gtk_tree_path_new_from_string (path_string));

  /* Update the GtkTreeModel with the new value */
  gtk_tree_store_set (GTK_TREE_STORE(treemodel), &iter, LASTNAME, new_text, -1);

  /* Dump the model out to STDOUT to show the changes */
  gtk_tree_model_get_iter_first (GTK_TREE_MODEL(treemodel), &iter);
  dump_tree_data (GTK_TREE_MODEL(treemodel), iter);

  return;
}

/* The main function of our example application */
int main (int argc, char *argv[]) {

  GtkWidget *window;

  GtkTreeModel *treemodel;
  GtkCellRenderer *render;
  GtkWidget *treeview;
  GtkTreeIter iter;
  
  gtk_init (&argc, &argv);

  /* Create the application window, and connect the delete event
     callback to the delete event signal */
  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  g_signal_connect (G_OBJECT(window), "delete_event",
                    G_CALLBACK(delete_event_cb), NULL);

  /* This calls a function which creates the new model and 
     then populates it with some data.  This function is 
     defined above */
  treemodel = create_and_populate_model ();

  /* Create the tree view widget which shows the data in the GUI*/
  treeview = gtk_tree_view_new ();

  /* Attach this widget to the model we have already created */
  gtk_tree_view_set_model (GTK_TREE_VIEW(treeview), GTK_TREE_MODEL(treemodel));

  /* For each column we want to display in our view we need to 
     create a renderer and attach it to the view */
  render = gtk_cell_renderer_text_new ();
  g_signal_connect (G_OBJECT(render), "edited", G_CALLBACK(lastname_edited), treemodel);
  gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW(treeview),
                                               -1, "Last name", render,
                                               "text", LASTNAME, 
					       "editable", EDITABLE, NULL);
  render = gtk_cell_renderer_text_new ();
  gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW(treeview),
                                               -1, "First name", render,
                                               "text", FIRSTNAME, NULL);

  /* Add the view widget to the window */
  gtk_container_add (GTK_CONTAINER (window), treeview);

  /* Get a GtkTreeIter for the first node in the model */
  gtk_tree_model_get_iter_first (GTK_TREE_MODEL(treemodel), &iter);

  /* Call our recursive function to print the data in the model
     to STDOUT */
  dump_tree_data (GTK_TREE_MODEL(treemodel), iter);

  /* Show all of the widgets, and then enter the gtk_main loop */
  gtk_widget_show_all  (window);    
  gtk_main ();
    
  return 0;
}


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