GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Mon Dec 22, 2014 7:54 pm

All times are UTC




Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post subject: Gtk main loop problem
PostPosted: Sat Sep 06, 2008 4:58 pm 
Offline
Familiar Face

Joined: Thu Aug 28, 2008 11:04 pm
Posts: 28
I have a function in my program that works with sockets on the program startup, but i noticed that the gtk_main(); loop it's a problem, because if i start the socket function before it (the function has an infinite loop) the gtk_main (); won't ever load, and if i put it after the gtk_main (); function it's not beeing executed.. what can i do?
Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 06, 2008 10:36 pm 
Offline
Never Seen the Sunlight

Joined: Thu Jun 14, 2007 11:02 pm
Posts: 923
Location: Falun, WI USA
can you create your socket loop in a function callback (like a "clicked" signal on a button)?
If you can, I'd recommend putting one of these in you loop so the GUI stays updated:
Code:
while(gtk_events_pending())
    gtk_main_iteration();


Top
 Profile  
 
 Post subject:
PostPosted: Sun Sep 07, 2008 12:04 am 
Offline
Familiar Face

Joined: Thu Aug 28, 2008 11:04 pm
Posts: 28
Isn't there an event like on form load?
That code would go in the main form?


Top
 Profile  
 
 Post subject:
PostPosted: Sun Sep 07, 2008 4:05 am 
Offline
Never Seen the Sunlight

Joined: Thu Jun 14, 2007 11:02 pm
Posts: 923
Location: Falun, WI USA
maybe the "show" signal is what you want?
http://library.gnome.org/devel/gtk/stab ... idget-show
Code:
g_signal_connect(toplevel_window, "show", G_CALLBACK(callback_function), NULL);


Top
 Profile  
 
 Post subject:
PostPosted: Sun Sep 07, 2008 4:29 pm 
Offline
Familiar Face

Joined: Thu Aug 28, 2008 11:04 pm
Posts: 28
But if im doing an infinite loop on my other function, when the form shows, it won't do anything on the signals.. wouldn't it?


Top
 Profile  
 
 Post subject:
PostPosted: Sun Sep 07, 2008 6:58 pm 
Offline
Never Seen the Sunlight

Joined: Thu Jun 14, 2007 11:02 pm
Posts: 923
Location: Falun, WI USA
if you do the gtk_events_pending thing, the GUI will stay up to date,
however, I'm running into a problem with using gtk_main_quit in this kind of a loop since gtk_main hasn't been
called yet when the show signal is emitted.
I've gotten around this by using exit instead of gtk_main_quit:
Code:
#include <gtk/gtk.h>
#include <stdlib.h>

void show_cb(GtkWidget *wi, gpointer user_data)
{
   while(TRUE)
   {
      /* ...do whatever you need to do... */

      /* these two lines will allow the GUI to be updated */
      while(gtk_events_pending())
         gtk_main_iteration();
   }
}

int main(int argc, char **argv)
{
   GtkWidget *win;

   gtk_init(&argc, &argv);

   win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
   g_signal_connect(win, "show", G_CALLBACK(show_cb), NULL);
   /* connecting to exit rather than gtk_main_quit isn't ideal, but may work */
   g_signal_connect_swapped(win, "delete-event", G_CALLBACK(exit), (gpointer)0);

   gtk_widget_show(win);

   gtk_main();

   return 0;
}


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group