GtkCombo - load list dynamically



What follows is a piece of C/Gtk/Glib code from a project I'm working on,
the gdb session it produces, and the 9-line input file. I can't get the 
GtkCombo widget to dynamically load from a variable in a loop. If I 
hard-code the list items, it works fine. I'm using gtk 1.2 on Mandrake
Linux 8.1, gcc 3.0. The initial source was produced with Glade.

Running the code below produces a combo box loaded with 9 items (this is
good). Each item is the same - the last line of the file, 3.5" Disk 
(this is bad). If the line 
        // strncpy(line, "X", 128);
is uncommented, the list is 9 X's.
If the line
        // gtk_combo_set_popdown_strings (GTK_COMBO (combo1), c_items);
is uncommented, the list is 1 item, the last line of the file.
What am I doing wrong? I am clearly not a C programmer (at home with 
shell, sed, awk, perl), but I need to get this thing working soon. 
Any help will be appreciated.

Thanks,
Tim

===============================================

  combo1 = gtk_combo_new ();
  gtk_widget_set_name (combo1, "combo1");
  gtk_widget_ref (combo1);
  gtk_object_set_data_full (GTK_OBJECT (DymoMainForm), "combo1", combo1,
                            (GtkDestroyNotify) gtk_widget_unref);
  gtk_widget_show (combo1);
  gtk_toolbar_append_widget (GTK_TOOLBAR (toolbar1), combo1, NULL, NULL);
  gtk_widget_set_usize (combo1, 335, 22);
  gtk_container_set_border_width (GTK_CONTAINER (combo1), 1);

  // Read /tmp/label_n.db, 1 append per line until EOF
  if( (fp = fopen( "/tmp/label_n.db", "r" )) == NULL )
  {
      printf("Cannot open file.\n");
      exit(1);
  }
  while(!feof(fp))
  {
      if(fgets(line, 128, fp)) {
          printf("%p %s", line, line);
          combo1_items = g_list_append (combo1_items, line);
          if( combo1_items == NULL ) {
                  printf("NULL list; aborting!\n");
                  exit(EXIT_FAILURE);
          }
          printf("Number of items in list is %i \n", g_list_length
(combo1_items));
          printf("First item is %p \n", g_list_first (combo1_items));
          c_items = g_list_last (combo1_items);
          printf("Last item is %p \n", g_list_last (combo1_items));
          // strncpy(line, "X", 128);
      }
  }
  fclose(fp);
  printf("combo1_items = %p \n", combo1_items);
  printf("c_items = %p \n", c_items);
  g_list_foreach (combo1_items, printf, "");
  gtk_combo_set_popdown_strings (GTK_COMBO (combo1), combo1_items);
  // gtk_combo_set_popdown_strings (GTK_COMBO (combo1), c_items);
  g_list_free (combo1_items);
  g_list_free (c_items);

===============================================

[timbo boromir Dymo]$ gdb ./dymo 
GNU gdb 5.1.1
Copyright 2002 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for details.
This GDB was configured as "i386-mandrake-linux"...
(gdb) run
Starting program: /home/timbo/src/Dymo/./dymo 
0xbffff700 Address
Number of items in list is 1 
First item is 0x807cfb4 
Last item is 0x807cfb4 
0xbffff700 Large Address
Number of items in list is 2 
First item is 0x807cfb4 
Last item is 0x807cfc0 
0xbffff700 Return Address
Number of items in list is 3 
First item is 0x807cfb4 
Last item is 0x807cfcc 
0xbffff700 Shipping Address
Number of items in list is 4 
First item is 0x807cfb4 
Last item is 0x807cfd8 
0xbffff700 Internet Postage 2-Part
Number of items in list is 5 
First item is 0x807cfb4 
Last item is 0x807cfe4 
0xbffff700 Internet Postage 3-Part
Number of items in list is 6 
First item is 0x807cfb4 
Last item is 0x807cff0 
0xbffff700 File Folder
Number of items in list is 7 
First item is 0x807cfb4 
Last item is 0x807cffc 
0xbffff700 Hanging Folder
Number of items in list is 8 
First item is 0x807cfb4 
Last item is 0x807d008 
0xbffff700 3.5" Disk
Number of items in list is 9 
First item is 0x807cfb4 
Last item is 0x807d014 
combo1_items = 0x807cfb4 
c_items = 0x807d014 
3.5" Disk
3.5" Disk
3.5" Disk
3.5" Disk
3.5" Disk
3.5" Disk
3.5" Disk
3.5" Disk
3.5" Disk

Program exited normally.
(gdb) quit

=====================================

[timbo boromir Dymo]$ cat /tmp/label_n.db 
Address
Large Address
Return Address
Shipping Address
Internet Postage 2-Part
Internet Postage 3-Part
File Folder
Hanging Folder
3.5" Disk
[timbo boromir Dymo]$ 

=====================================


__________________________________________________
Do You Yahoo!?
LAUNCH - Your Yahoo! Music Experience
http://launch.yahoo.com



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