Re: problem with gtk filetree



Helmethead wrote:

On Wed, Apr 25, 2001 at 11:58:36PM -0400, Glitch wrote:

hello,

I got Developing Linux Applications Using GTK+ (written by Eric Harlow).

In it there is an example of a Gtk Tree and Gtk List. It basically uses the directories and files in the CWD and displays the contents in a tree with leafs.

I can get the application to compile just fine however when I try selecting a directory or file I get a seg fault. I can hit the + or - sign to expand or collapse a branch but selecting anything but the top node causes a seg fault. Here is the actual message:

Gtk-WARNING **: invalid cast from `(unknown)' to `GtkList'
Segmentation fault



Such errors usually happen when u pass a NULL to a gtk_list_* function that doesn't want a NULL
Do you know that (as of gtk2.0) GtkList is by twice obselete? use GtkCList ferchrissakes, or if you are writing for 2.0, GtkTreeView/GtkListStore :)

Course I don't know how to write for any of the above. Maybe GtkList is easier or something. And you do have a book on it. But it's gone way out of favor..

well, i haven't installed 2.0 and so i'm working with old code, and since im learning *and* learning from a now old book, i feel in some ways i should stick to the old code then since the book uses that.

anyway, I looked thru my code and didn't seee any explicit use of NULL but I know that it isn't necessarily that easy of a fix. After looking at my code and what is printed to the xterm i noticed that a sprintf statement i have isnt being reached so the program must stop somewhere before that. The sprintf statement is on line 59. I think my cast of GTK_LIST(listbox) in the PopulateListbox function isn't working right for some reason, but I could be wrong.

If anyone would be so kind to look at my code (all 178) lines i'd be very grateful.

#define MAX_PATH  5000

gint CloseAppWindow (GtkWidget *widget, gpointer gdata)
{
  g_print ("Quitting...\n");
  gtk_main_quit();
  return(FALSE);
}
void AddListItem (GtkWidget *listbox, char *sText)
{
 GtkWidget *item;
 item = gtk_list_item_new_with_label(sText);
 gtk_container_add(GTK_CONTAINER(listbox), item);
 gtk_widget_show(item);
}

int IsDirectory(char *buffer)
 {
   struct stat buf;
   if (stat (buffer, &buf) < 0)
     {
       return (FALSE);
     }
   return (S_ISDIR(buf.st_mode));
 }

static void PopulateListbox(char *szPath)
{
 GtkWidget *listbox;
 GtkWidget *item;
 DIR *directory;
 struct dirent *dirEntry;
 struct stat fileStatus;
 char buffer[MAX_PATH];
 gtk_list_clear_items(GTK_LIST(listbox),
                      0,
                     g_list_length(GTK_LIST(listbox)->children));
 directory = opendir(szPath);
 while (dirEntry == readdir(directory))
  {
    if (!strcmp(dirEntry->d_name, "..") ||
        !strcmp(dirEntry->d_name, ".")) {
    }
     else {
          sprintf(buffer, "%s/%s", szPath, dirEntry->d_name);
          if (!IsDirectory(buffer)) {
           item = gtk_list_item_new_with_label(dirEntry->d_name);
           gtk_container_add(GTK_CONTAINER(listbox), item);
           gtk_widget_show(item);
           AddListItem(listbox, dirEntry->d_name);
     }
   }
 }
closedir(directory);
}

void select_item (GtkWidget *widget, gpointer data)
 {
   printf("item selected %s\n", (char *) data);
   PopulateListbox((char *) data);
 }

static void CreateSubTree(char *szPath, char *szDir, GtkWidget* item)
 {
  DIR *directory;
  struct dirent *dirEntry;
  struct stat fileStatus;
  GtkWidget *item_subtree = NULL;
  GtkWidget *item_new;
  char buffer[MAX_PATH];
  int nSubDirectories =0;
  directory = opendir (szPath);
  while (dirEntry = readdir(directory))
   {
    if (!strcmp (dirEntry->d_name, "..") ||
       !strcmp (dirEntry->d_name, "."))
     {}
     else
      {
         sprintf(buffer, "%s/%s", szPath, dirEntry->d_name);
      if(IsDirectory(buffer))
       {
         if (item_subtree  == NULL)
          {
           item_subtree = gtk_tree_new();
           gtk_tree_item_set_subtree(GTK_TREE_ITEM(item), item_subtree);
          }
       item_new = gtk_tree_item_new_with_label(dirEntry->d_name);
       gtk_tree_append(GTK_TREE(item_subtree), item_new);
       CreateSubTree(buffer, dirEntry->d_name, item_new);
       gtk_widget_show(item_new);
       gtk_signal_connect(GTK_OBJECT(item_new), "select",
GTK_SIGNAL_FUNC(select_item), g_strdup(buffer));
      }
    }
  }
  closedir(directory);
  gtk_widget_show(item);
}

static void CreateTree (GtkWidget *window)
 {
  char buffer[MAX_PATH];
  GtkWidget *box1;
  GtkWidget *scrolled_win;
  GtkWidget *tree;
  GtkWidget *listbox;
  GtkWidget *leaf;
  GtkWidget *item1, *item2, *item3;
  GtkWidget *tree1, *tree2, *tree3;
  GtkWidget *item;
  box1=gtk_hbox_new(FALSE, 0);
  gtk_container_add(GTK_CONTAINER(window), box1);
  gtk_widget_show(box1);
  scrolled_win = gtk_scrolled_window_new(NULL, NULL);
  gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled_win),
GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
  gtk_box_pack_start(GTK_BOX(box1), scrolled_win, TRUE, TRUE, 0);
  gtk_widget_set_usize(scrolled_win, 450, 450);
  gtk_widget_show(scrolled_win);
  tree = gtk_tree_new();
  listbox = gtk_list_new();
  gtk_widget_set_usize(listbox, 450,450);
  gtk_box_pack_start(GTK_BOX(box1), listbox, TRUE, TRUE, 0);
  gtk_widget_show(listbox);

gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(scrolled_win), tree);
  gtk_widget_show(tree);
  leaf= gtk_tree_item_new_with_label(getcwd(buffer, sizeof(buffer)));
  gtk_tree_append(GTK_TREE(tree), leaf);
  gtk_widget_show(leaf);

  CreateSubTree(getcwd(buffer, sizeof(buffer)),
                getcwd(buffer, sizeof(buffer)), leaf);
  gtk_widget_show(window);
}

int main(int argc, char *argv[])
 {
  GtkWidget *window;
  GtkWidget *table;
  gtk_init(&argc, &argv);
  window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  gtk_window_set_title(GTK_WINDOW(window), "Files using trees");
  gtk_widget_set_usize(window, 450, 450);
  gtk_signal_connect(GTK_OBJECT(window), "delete_event",
                     GTK_SIGNAL_FUNC(CloseAppWindow), NULL);
  gtk_widget_show(window);
  CreateTree(window);
  gtk_main();
  exit(0);
}





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