Re: GList in a list box



----- Original Message -----
From: "Jim Adams" <scurveedog yahoo com>
To: <gtk-list gnome org>
Sent: Sunday, February 10, 2002 9:07 PM
Subject: GList in a list box


> Hello,
>
> I am a beginner who has become very confused.
>
> I have a data file and can transfer it to a GList which I can then print
> out.  Can anyone help me display the complete GList in a gtk+ list box?
>
> My test program to load a GList and then print it out:
>
> #include <glib.h>
> #include <stdio.h>
>
> /***** FUNC TO DISPLAY GList *****/
> void PrintList( GList *list )
> {
>    list = g_list_first( list );
>
>    while( list != NULL )
>    {
>       g_print( "%s", (gchar *)list -> data );
>       list = g_list_next( list );
>    }
> }
>
> gint main( )
> {
>    GList *list = NULL;
>
>    FILE *infile;
>    gchar buffer[5][81];
>    gint i = 0;
>
> /***** OPEN DATA FILE *****/
>    if( ( infile = fopen( "/home/jim/gnome/data.dat", "r" )) == NULL )
>    {
>       g_print( "\nfopen() failed!\n" );
>       exit( 0 );
>    }
>
> /***** LOAD BUFFER WITH DATA *****/
>    while( ( fgets( buffer[i], 81, infile ) ) != NULL )
>    {
>       i++;
>    }
>
> /***** ADD BUFFER DATA TO GList *****/
>    for( i = 0; i < 5; i++ )
>    {
>       list = g_list_append( list, buffer[i] );
>    }
>
>    PrintList( list );
>    g_list_free( list );
>    fclose( infile );
>
>    return 0;
> }
>
> thank you,
> jim, scurveedog yahoo com
>
Hi Jim:
   What you have to do  to get your values in a Listbox is very similar to
what you did to get them
in the GList, just declare and make an instance of a GtkWindow and GtkList
widgets, then
put your values in the GList using the gtk_list_insert_items function. The
resulting code should look like this.

#include <gtk.h> /* gtk.h includes glib.h*/
> #include <stdio.h>
>
> /***** FUNC TO DISPLAY GList *****/
> void PrintList( GList *list )
> {
>    list = g_list_first( list );
>
>    while( list != NULL )
>    {
>       g_print( "%s", (gchar *)list -> data );
>       list = g_list_next( list );
>    }
> }
>
/****This is how the function should look like****/
void GListInWindow (GList *list)
{
    GtkWindow *MyWin;
    GtkList *MyList;

    MyWin=gtk_window_new(GTK_WINDOW_TOPLEVEL);
    MyList=gtk_list_new();

    gtk_list_insert_items(MyList, list, 0);
    gtk_window_add(GTK_CONTAINER(MyWin), GTK_WIDGET(MyList));
    gtk_widget_show(MyList);
    gtk_widget_show(MyWin);

}
/* In order to use gtk.h is necesary to get the command line parameters */
 gint main( &argc, &argv[])
> {
>    GList *list = NULL;
>
>    FILE *infile;
>    gchar buffer[5][81];
>    gint i = 0;
>
> /***** OPEN DATA FILE *****/
>    if( ( infile = fopen( "/home/jim/gnome/data.dat", "r" )) == NULL )
>    {
>       g_print( "\nfopen() failed!\n" );
>       exit( 0 );
>    }
>
> /***** LOAD BUFFER WITH DATA *****/
>    while( ( fgets( buffer[i], 81, infile ) ) != NULL )
>    {
>       i++;
>    }
>
> /***** ADD BUFFER DATA TO GList *****/
>    for( i = 0; i < 5; i++ )
>    {
>       list = g_list_append( list, buffer[i] );
>    }
>
 >   PrintList( list );
/*You MUST include this line before doing ANYTHNG with GTK*/
gtk_init (argc,argv);
/*Start infinite loop*/
GListInWindow ( list );
    gtk_main();

    g_list_free( list );
>    fclose( infile );
>
>    return 0;
> }
>

Of course, to you'll need to kill this program in order to end it. That's
because we haven't
implementes any event callback to manage the "delete" signal from our main
window so
let's use kill-9 PID :o)

I hope this helps!!!!!

rtriay
:)


_______________________________________________
> gtk-list mailing list
> gtk-list gnome org
> http://mail.gnome.org/mailman/listinfo/gtk-list
>





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