Friday, March 27, 2009

Display text using file in GTK View

Here's a simple code to extract data from a text file and display it in a View.

FILE *fp; // FILE pointer
char buff[100]; // To hold text extracted from FILE

GtkWidget *det ;
GtkWidget *view1=NULL ; // View used to display text (initialized to NULL)

view1=lookup_widget(GTK_WIDGET(widget),"view_tm");

/* "view_tm" is the view name given using GLADE
widget (GtkWidget *) -> This pointer is passed automatically when an event takes place */

fp=fopen("file_to_display.txt","r");

while(!feof(fp))
{
fgets(buff,60,fp);

det=gtk_list_item_new_with_label (buff);

gtk_container_add (GTK_CONTAINER (view1), det);

}
/* gtk_list_item_new_with_label ()
Creates a new GtkListItem with a child label containing the given string.
label :
the string to use for the child label.
Returns :
a new GtkListItem with a child GtkLabel with the text set to label.
*/


gtk_widget_show(det);
fclose(fp);


}