GTK g_idle_add
- From: António Pinho <shinigami godspeed gmail com>
- To: gtk-list gnome org
- Subject: GTK g_idle_add
- Date: Sat, 2 May 2009 18:49:16 +0100
I am making an application for identifying a person. It opens a window and displays information. There is an authentication process that will be running in the background of the window. As so i would like to show each different state of the autentication in the "status_label" and in my code i am trying to test that using the label_event function. This is giving me error because of the " g_idle_add(label_event,G_OBJECT(status_label));" . Am i doing this in the correct way? Thank you
Code :
GtkWidget *window;
GtkWidget *button;
GtkWidget *vbox;
GtkWidget *hbox;
GtkWidget *hbox2;
GtkWidget *hbox3;
GtkWidget *halign;
GtkWidget *valign;
GtkWidget *status_label;
GtkWidget *table;
GtkWidget *status_frame;
GtkWidget *id_frame;
GtkWidget *image1;
GtkWidget *alignment1;
/* another callback */
static gboolean delete_event( GtkWidget *widget, GdkEvent *event, gpointer data )
{
gtk_main_quit ();
return FALSE;
}
static gboolean label_event( gpointer widget)
{
int x =1;
do
{
write_label("Uploading information.\n",widget);
usleep(100);
clear_label(widget);
write_label("Uploading information..\n",widget);
usleep(100);
clear_label(widget);
write_label("Uploading information...\n",widget);
usleep(100);
clear_label(widget);
x++ ;
}
while (x!=3);
write_label("Uploading info done!\n",widget);
}
void clear_label(GtkWidget *label)
{
gtk_label_set_text(GTK_LABEL(label)," ");
}
void write_label(const char * message, GtkWidget *label)
{
char * newmessage = (char *)calloc(strlen(gtk_label_get_text(GTK_LABEL(label) )) + strlen(message) + 1, sizeof(char));
strcat(newmessage,gtk_label_get_text( GTK_LABEL(label)));
strcat(newmessage,message);
gtk_label_set_text(GTK_LABEL(label),newmessage);
}
GdkPixbuf *create_pixbuf(const gchar * filename)
{
GdkPixbuf *pixbuf;
GError *error = NULL;
pixbuf = gdk_pixbuf_new_from_file(filename, &error);
if(!pixbuf) {
fprintf(stderr, "%s\n", error->message);
g_error_free(error);
}
return pixbuf;
}
void init( int argc, char *argv[] )
{
/* GtkWidget is the storage type for widgets */
/* This is called in all GTK applications. Arguments are parsed
* from the command line and are returned to the application. */
gtk_init (&argc, &argv);
/* Create a new window */
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
/* This is a new call, which just sets the title of our Window */
gtk_window_set_title (GTK_WINDOW (window), "Autenticação");
gtk_window_set_default_size(GTK_WINDOW(window), 450, 300);
gtk_container_set_border_width(GTK_CONTAINER(window), 10);
gtk_window_set_icon(GTK_WINDOW(window), create_pixbuf("icon.png"));
/* Here we just set a handler for delete_event that immediately
* exits GTK. */
g_signal_connect (G_OBJECT (window), "delete_event",
G_CALLBACK (delete_event), NULL);
g_idle_add(label_event,G_OBJECT(status_label));
table=gtk_table_new(4,1,FALSE);
//gtk_table_set_row_spacing(GTK_TABLE(table), 1, 3);
/* Creates a new button with the label "Button 1". */
button = gtk_button_new_with_label ("Sair");
/* Now when the button is clicked, we call the "callback" function
* with a pointer to "button 1" as its argument */
g_signal_connect (G_OBJECT (button), "clicked",
G_CALLBACK (delete_event), NULL);
vbox = gtk_vbox_new(FALSE, 5);
valign = gtk_alignment_new(0, 1, 0, 0);
gtk_container_add(GTK_CONTAINER(vbox), valign);
hbox = gtk_hbox_new(TRUE, 3);
gtk_widget_set_size_request(button, 70, 30);
gtk_container_add(GTK_CONTAINER(hbox), button);
halign = gtk_alignment_new(1, 0, 0, 0);
gtk_container_add(GTK_CONTAINER(halign), hbox);
gtk_box_pack_start(GTK_BOX(vbox), halign, FALSE, FALSE, 0);
gtk_table_attach_defaults(GTK_TABLE(table),vbox,0,1,3,4);
/* Create the status frame Frame */
status_frame = gtk_frame_new (NULL);
/* Set the status frame label */
gtk_frame_set_label (GTK_FRAME (status_frame), "Estado da autenticação");
/* Set the frame to the first row and first column */
gtk_table_attach_defaults(GTK_TABLE(table),status_frame,0,1,0,1);
hbox3 = gtk_hbox_new (FALSE, 0);
gtk_container_add (GTK_CONTAINER (status_frame), hbox3);
/* Create the label containing the text */
status_label = gtk_label_new("");
gtk_label_set_justify(GTK_LABEL(status_label),GTK_JUSTIFY_LEFT);
gtk_box_pack_start(GTK_BOX(hbox3),status_label,FALSE,FALSE,0);
id_frame=gtk_frame_new(NULL);
gtk_table_attach_defaults(GTK_TABLE(table),id_frame,0,1,1,3);
gtk_frame_set_label (GTK_FRAME (id_frame), "Identificação do utilizador");
alignment1 = gtk_alignment_new (0.5, 0.5, 1, 1);
gtk_widget_show (alignment1);
gtk_container_add (GTK_CONTAINER (id_frame), alignment1);
gtk_alignment_set_padding (GTK_ALIGNMENT (alignment1), 0, 0, 12, 0);
hbox2 = gtk_hbox_new (FALSE, 0);
gtk_container_add (GTK_CONTAINER (alignment1), hbox2);
image1=gtk_image_new_from_file("petrelli.jpg");
gtk_box_pack_end(GTK_BOX (hbox2), image1, FALSE, FALSE, 0);
gtk_container_add(GTK_CONTAINER(window), table);
gtk_widget_show_all(window);
gtk_main();
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]