GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Wed Apr 23, 2014 6:45 am

All times are UTC




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: gtk_main
PostPosted: Wed Apr 13, 2011 9:07 am 
Offline

Joined: Wed Apr 13, 2011 8:12 am
Posts: 3
Hi,
I have newly joined in this forum and a novice of gtk programming. I have my project in linux where I am using c with gtk library. I have a main.c and display_1.c and display_2.c upto .... display_N.c files. In each of the display files i have a function Disp1() ---- DispN() like--

Code:
void disp1(void)
{
-------------------
//window and buttons generation codes
wintag=1 // global variable which sets to 1 to --n as per buttton click.
-------------------
gtk_main(); 
}

In the main.c file I have an infinite loop like:-

void main(void)
{
   Disp1();
   while (1)
   {
      switch (wintag)
        case 2:  disp2(); break;
        --------
        case N:  dispN(); break;
    }
   }
}

My problem is that once i am calling display functions (from main) gtk_main(at the last statement of the disp function in the display file)haults the program flow so I can't get back to the infinite loop of main unless any signal is not being emited by clicking a button in the display programms. I want the infinite loop should work as its own way in the main instead of genearting any event in the disp functions in the display files...

Please help......


Top
 Profile  
 
 Post subject: Re: gtk_main
PostPosted: Wed Apr 13, 2011 1:09 pm 
Offline
Never Seen the Sunlight

Joined: Mon Apr 28, 2008 5:52 am
Posts: 709
Location: UK
Hi and welcome to the forum,

By the way you are trying to explain how you are using GTK+, you are using it in a way that it is not designed to be used. It looks like you are trying to have your own busy loop, but GTK+ like most graphical tools kits is event based. So gtk_main() enters its own loop emitting signals on events such as on button presses, menu clicks, time outs etc.... It will only exit once gtk_main_quit() is called from a callback from one of the callback functions;

So your main should look a little like this.

Code:
int main(int argc, char *argv[])
{
   /*  ...
     * variables in main
     */

    gtk_init (&argc, &argv);

  /* ...
    * Initialise window and various bits of the GUI
    * Connect signals to the various callbacks.
    */

/* Enter the GTK+ event loop */
    gtk_main();

  /* Clean up, the application has ended
    * ...
    */

    return 0;
}

I would advise having a look at a tutorial which shows the best way to use the GTK+ library. This is a link to GTK+ own tutorial which I hope would help http://developer.gnome.org/gtk-tutorial/stable/

E.

_________________
E.


Top
 Profile  
 
 Post subject: Re: gtk_main
PostPosted: Thu Apr 14, 2011 8:47 am 
Offline

Joined: Wed Apr 13, 2011 8:12 am
Posts: 3
Dear Sir,
Thanks so much. Actually I have already done like this. I have many GUIs whci are designed in various functions and I am calling them from the main (using infinite loop ). I have a serial port data pulling routine. Using a timer I am pulling the data from serial port which data are required to be displayed in the GUIs. I have to always refresh the values in the GUI displays.Once the gtk_main is called in the display function infinite loop breaks. I don't have any way to use call backs in the display functions to refresh the values. If I use any button click like events in the GUI files then only I can go back into the main loop which is not my moto. here is my program logic:-

Code:
int main(....,...)
{
  gtk_init(&argc,&argv);
  //calltimer to pull data from serial port
  while(1)// infinite loop
    {
      swich tag
     case 1
      display1()  // each display function has its own call function and gtk_main at last.

     case N
       displayn()
    }
  }
}


errol wrote:
Hi and welcome to the forum,

By the way you are trying to explain how you are using GTK+, you are using it in a way that it is not designed to be used. It looks like you are trying to have your own busy loop, but GTK+ like most graphical tools kits is event based. So gtk_main() enters its own loop emitting signals on events such as on button presses, menu clicks, time outs etc.... It will only exit once gtk_main_quit() is called from a callback from one of the callback functions;

So your main should look a little like this.

Code:
int main(int argc, char *argv[])
{
   /*  ...
     * variables in main
     */

    gtk_init (&argc, &argv);

  /* ...
    * Initialise window and various bits of the GUI
    * Connect signals to the various callbacks.
    */

/* Enter the GTK+ event loop */
    gtk_main();

  /* Clean up, the application has ended
    * ...
    */

    return 0;
}

I would advise having a look at a tutorial which shows the best way to use the GTK+ library. This is a link to GTK+ own tutorial which I hope would help http://developer.gnome.org/gtk-tutorial/stable/

E.


Top
 Profile  
 
 Post subject: Re: gtk_main
PostPosted: Thu Apr 14, 2011 10:12 am 
Offline

Joined: Wed Apr 13, 2011 8:12 am
Posts: 3
Dear Sir,
Thanks so much. Actually I have already done like this. I have many GUIs which are designed in various functions and I am calling them from the main (using infinite loop ). I have a serial port data pulling routine. Using a timer I am pulling the data from serial port which data are required to be displayed in the GUIs. I have to always refresh the values in the GUI displays.Once the gtk_main is called in the display function infinite loop breaks. I don't have any way to use call backs in the display functions to refresh the values. If I use any button click like events in the GUI files then only I can go back into the main loop which is not my moto. -

Can u help me to say how can I display data in one GUI. The GUI function will be stored in separate .c file. It will be called frmo main. Main is also pulling a data from serial port (whch is already done using timer). infinite loop in main sends the in_buff values (data pulled fom serial port) from main into the display function as parametrrs to display the GUIs .
GUI.C
-----
Code:
int display1(int int_buff)
{
   // various buttons and windows to store int_buff values
  //
   gtk_main();  /* due to gtk_main the GUI is not being refreshed. */
   return 1;
}

int main(int argc,char* argv[])
{
   gtk_init(&argc,&argv);

   /* calltimer to pull data from serial port and storing values in variable say in_buff */

while(1) /* infinite loop to see always the latest values from in the window i.e. refreshed */
  {
   display1(in_buff)
  }
  /* there will be n numer of displays which will be called using logic under this loop */
}


Am I clear with y thoughts. Please suggest


Top
 Profile  
 
 Post subject: Re: gtk_main
PostPosted: Fri Apr 15, 2011 4:24 pm 
Offline
Never Seen the Sunlight

Joined: Mon Apr 28, 2008 5:52 am
Posts: 709
Location: UK
Hi,

You are still trying to use GTK+ in a busy loop, when you should be thinking in an event driven way.

Code:
#include <gtk/gtk.h>

void setup_serial_port()
{
    /* set up the serial port */
}

void create_display_1(void)
{
    /* create the user interface for display 1 and connect any signals for it */
}

void update_display_1(()
{
    /* update display using data from serial port and return */
}

void create_display_2(void)
{
    /* create the user interface for display 2 and connect any signals for it */
}

void update_display_2()
{
    /* update display using data from serial port and return */
}

gboolean timer(gpointer data)
{
    /* read data from serial port */
    /* Your code here ............ */
    /* How you transfer your data is up to you */

    update_display_1();
    update_display_2();

    return TRUE;
}

int main (int argc, char *argv[])
{
    gtk_init(&argc, &argv);

    create_display_1();
    create_display_2();
    setup_serial_port();

/* set a timer to be called every 0.1s, but this can be set to any timeout */
    g_timeout_add(100, timer, NULL);

/* Run until gtk_main_quit() is called */
    gtk_main();

    return 0;
}

This is an outline of an event driven GTK+ application.

Basically what it does is set up the user interfaces, connect the signals. It also sets up the serial port, and sets a timer that would be called every 0.1 seconds. The timeout callback would read from the serial port and then update the user interface (without needing to call gtk_main()).

The application then calls gtk_main(). This is where the application becomes alive. gtk_main() sits in its own special loop calling any callback that is connected. Such as button presses, menu selections, timeouts etc.... It also performs the displaying of the user interface.

In this case I have added a timeout which is called every 0.1 seconds. The callback function then reads the serial port and updates the user interface.

I do not know the data rate or what the data being read from the serial port is, so reading the serial every 0.1 seconds may well be too slow. You could increase the rate (this may slow down the user interface). Or you could add a different callback to gtk_main() which watches for any data on the serial port. This is a slightly more complex interface so ask once you have got to grips with event driven programming.

Your application only needs to have ONE gtk_main(), and certainly no busy loops trying to read data that is not there.

E.

_________________
E.


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

All times are UTC


Who is online

Users browsing this forum: Google [Bot] 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