My unstable treeview test application



Hi,

I have read about all that I can find on the subject of gtktreeviews. So
I tried to apply the code snippets with a gui using glade to build it.
On my laptop - Mandrake 9.1, it works - sort of, but it segfaults very
quickly. On this desktop, also mandrake9.1, immediate segfault. I
suspect it is a signal problem. Anyhow here's the code. The app is a
form with a button to set things up, a treeview widget and a button
below to add a row. Any assistance would be greatly appreciated.

Thanks,
Kim

callbacks.c:


#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif

#include <gtk/gtk.h>

#include "callbacks.h"
#include "interface.h"
#include "support.h"

    GtkTreeStore *store;
    GtkWidget *tree,*sw,*w1;
    GtkTreeViewColumn *column;
    GtkCellRenderer *renderer;
    GtkTreeIter *iter;
    GtkTreeSelection *select;

enum
{
TITLE_COLUMN,
AUTHOR_COLUMN,
CHECKED_COLUMN,
N_COLUMNS
};


static void tree_selection_changed_cb(GtkTreeSelection
*selection,gpointer data)
{
    GtkTreeIter iter1;
    GtkTreeModel *model1;
    gchar *author;
    if(gtk_tree_selection_get_selected(selection,&model1,&iter1))
    {
        gtk_tree_model_get(model1,&iter1,AUTHOR_COLUMN,&author,-1);
        g_print("you selected a book by %s \n",author);
        g_free(author);
    }
}

void
on_button1_clicked                     (GtkButton       *button,
                                        gpointer         user_data)
{
    
    //create a model
   
store=gtk_tree_store_new(N_COLUMNS,G_TYPE_STRING,G_TYPE_STRING,G_TYPE_BOOLEAN);
    //fill the model
    //populate_tree_model(store);
    gtk_tree_store_append(store,&iter,NULL);
    gtk_tree_store_set(store,&iter,TITLE_COLUMN,"The art of
programming",AUTHOR_COLUMN,"ME",CHECKED_COLUMN,FALSE,-1);    
    
    //create a view
    tree=gtk_tree_view_new_with_model(GTK_TREE_MODEL(store));
    
    //get rid of our own reference to store
    g_object_unref(G_OBJECT(store));
    
    //create a cell renderers and columns
    renderer=gtk_cell_renderer_text_new();
    g_object_set(G_OBJECT(renderer),"foreground","red",NULL);
   
column=gtk_tree_view_column_new_with_attributes("Author",renderer,"text",AUTHOR_COLUMN,NULL);
    gtk_tree_view_append_column(GTK_TREE_VIEW(tree),column);
    renderer=gtk_cell_renderer_text_new();
   
column=gtk_tree_view_column_new_with_attributes("Title",renderer,"text",TITLE_COLUMN,NULL);
    gtk_tree_view_append_column(GTK_TREE_VIEW(tree),column);
    renderer=gtk_cell_renderer_toggle_new();
    column=gtk_tree_view_column_new_with_attributes("Checked
out",renderer,"active",CHECKED_COLUMN,NULL);
    gtk_tree_view_append_column(GTK_TREE_VIEW(tree),column);
    //insert the widget
    sw=lookup_widget(button,"scrolledwindow1");
    gtk_container_add(GTK_CONTAINER(sw),tree);
    gtk_widget_show(tree);
    w1=lookup_widget(button,"window1");
    select=gtk_tree_view_get_selection(GTK_TREE_VIEW(tree));
    gtk_tree_selection_set_mode(select,GTK_SELECTION_SINGLE);
   
g_signal_connect(G_OBJECT(select),"changed",G_CALLBACK(tree_selection_changed_cb),NULL);



}


void
on_button2_clicked                     (GtkButton       *button,
                                        gpointer         user_data)
{
    gtk_tree_store_append(store,&iter,NULL);
    gtk_tree_store_set(store,&iter,TITLE_COLUMN,"The art of
programming",AUTHOR_COLUMN,"ME",CHECKED_COLUMN,FALSE,-1);    
    printf("added another row to model\n");
    gtk_widget_show(tree);
    


}


void
on_button3_clicked                     (GtkButton       *button,
                                        gpointer         user_data)
{
    gtk_main_quit();
}



interface.c


#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>

#include <gdk/gdkkeysyms.h>
#include <gtk/gtk.h>

#include "callbacks.h"
#include "interface.h"
#include "support.h"

#define GLADE_HOOKUP_OBJECT(component,widget,name) \
  g_object_set_data_full (G_OBJECT (component), name, \
    gtk_widget_ref (widget), (GDestroyNotify) gtk_widget_unref)

#define GLADE_HOOKUP_OBJECT_NO_REF(component,widget,name) \
  g_object_set_data (G_OBJECT (component), name, widget)

GtkWidget*
create_window1 (void)
{
  GtkWidget *window1;
  GtkWidget *vbox1;
  GtkWidget *button1;
  GtkWidget *scrolledwindow1;
  GtkWidget *button2;
  GtkWidget *button3;

  window1 = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  gtk_window_set_title (GTK_WINDOW (window1), _("window1"));

  vbox1 = gtk_vbox_new (FALSE, 0);
  gtk_widget_show (vbox1);
  gtk_container_add (GTK_CONTAINER (window1), vbox1);

  button1 = gtk_button_new_with_mnemonic (_("button1"));
  gtk_widget_show (button1);
  gtk_box_pack_start (GTK_BOX (vbox1), button1, FALSE, FALSE, 0);

  scrolledwindow1 = gtk_scrolled_window_new (NULL, NULL);
  gtk_widget_show (scrolledwindow1);
  gtk_box_pack_start (GTK_BOX (vbox1), scrolledwindow1, TRUE, TRUE, 0);
  gtk_widget_set_size_request (scrolledwindow1, 510, 651);

  button2 = gtk_button_new_with_mnemonic (_("button2"));
  gtk_widget_show (button2);
  gtk_box_pack_start (GTK_BOX (vbox1), button2, FALSE, FALSE, 0);

  button3 = gtk_button_new_with_mnemonic (_("button3"));
  gtk_widget_show (button3);
  gtk_box_pack_start (GTK_BOX (vbox1), button3, FALSE, FALSE, 0);

  g_signal_connect ((gpointer) button1, "clicked",
                    G_CALLBACK (on_button1_clicked),
                    NULL);
  g_signal_connect ((gpointer) button2, "clicked",
                    G_CALLBACK (on_button2_clicked),
                    NULL);
  g_signal_connect ((gpointer) button3, "clicked",
                    G_CALLBACK (on_button3_clicked),
                    NULL);

  /* Store pointers to all widgets, for use by lookup_widget(). */
  GLADE_HOOKUP_OBJECT_NO_REF (window1, window1, "window1");
  GLADE_HOOKUP_OBJECT (window1, vbox1, "vbox1");
  GLADE_HOOKUP_OBJECT (window1, button1, "button1");
  GLADE_HOOKUP_OBJECT (window1, scrolledwindow1, "scrolledwindow1");
  GLADE_HOOKUP_OBJECT (window1, button2, "button2");
  GLADE_HOOKUP_OBJECT (window1, button3, "button3");

  return window1;
}







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