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 Jul 30, 2014 11:09 am

All times are UTC




Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post subject: Interacting via terminal after a window is showed?
PostPosted: Wed Jun 27, 2007 12:13 pm 
Offline
Familiar Face

Joined: Wed Jun 27, 2007 11:45 am
Posts: 5
I am totally new to Gtk+ and in order to get a first overview of these libraries I am trying to understand some basic examples taken by the net. I have a problem about the following example code:

Code:
/* compile using:
cc -Wall -g `pkg-config --cflags --libs gtk+-2.0`  -o example main.c
*/

#include <gtk/gtk.h>

void on_window_map (GtkWidget*, gpointer);
void on_window_realize (GtkWidget*, gpointer);
void on_window_show (GtkWidget*, gpointer);
               
int
main (int argc, char *argv[])
{
       
        GtkWidget *window;
   int a;
       
        /* initialize the GTK+ library */
        gtk_init (&argc, &argv);

        /* create main window */
        window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
        gtk_window_set_title (GTK_WINDOW (window), "Example");
        gtk_container_set_border_width (GTK_CONTAINER (window), 10);
        gtk_widget_set_size_request (window, 200, 100);
       
        /* connect signals */
        g_signal_connect (G_OBJECT(window), "destroy",
                        G_CALLBACK (gtk_main_quit), NULL);
        g_signal_connect (G_OBJECT(window), "realize",
                        G_CALLBACK (on_window_realize), NULL);
        g_signal_connect (G_OBJECT(window), "map",
                        G_CALLBACK (on_window_map), NULL);
        g_signal_connect (G_OBJECT(window), "show",
                        G_CALLBACK (on_window_show), NULL);       
               
        /* show the main window */
        gtk_widget_show (window);
     
       
       gtk_main ();
       
        return 0;     
}

void
on_window_map (GtkWidget *w,gpointer user_data)
{
        g_print("Window was mapped.\n");
}

void
on_window_realize (GtkWidget *w,gpointer user_data)
{
        g_print("Window was realized.\n");
}

void
on_window_show (GtkWidget *w,gpointer user_data)
{
   int x;
        g_print("Window was shown.\n");
   printf("Insert x  :  ");
   scanf("%d",&x);
}



In the function on_window_show I added the request to insert an integer via terminal, I would like to be asked for it after the window has been showed, what am I missing?

Can you suggest me some good tutorials?

Thanks in advance
Roscarby

[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 27, 2007 12:43 pm 
Offline
Never Seen the Sunlight

Joined: Wed Sep 21, 2005 3:07 am
Posts: 384
Location: Fairfax, Virginia
The problem with this code is that, after the window is set as visible, it is not actually visible immediately. When "show" is emitted, then the window is exposed with "expose-event". So, you should use the "event-after" signal, checking whether the expose-event is done. For example:

Code:
g_signal_connect (G_OBJECT(window), "event-after",
                G_CALLBACK (on_event_after), NULL);

/* ... */

void
on_event_after (GtkWidget *widget,
                GdkEvent *event,
                gpointer user_data)
{
   if (event->type == GDK_EXPOSE)
   {
        int x;
        g_print("Window was shown & exposed.\n");
        printf("Insert x  :  ");
        scanf("%d",&x);
   }
}

_________________
Andrew Krause

Foundations of GTK+ Development: Buy now for only $31.49!


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 27, 2007 1:05 pm 
Offline
Familiar Face

Joined: Wed Jun 27, 2007 11:45 am
Posts: 5
Thanks a lot for your hint, this is a good step forward, but I realized that
I have been asked for "x" each time I minimize and maximize again the
window, how can I avoid this and being asked for x only once?

Thanks again for the patience
Roscarby


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 05, 2007 7:12 pm 
Offline
GTK+ Guru

Joined: Thu Jun 21, 2007 1:52 pm
Posts: 198
Location: Wilkes Barre Pa
Are you saying that you want the "Window" to show and then have the terminal ask you for input?

If this is the case then you need to free your terminal i would look into using threads and dbus.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 05, 2007 7:22 pm 
Offline
Never Seen the Sunlight

Joined: Wed Sep 21, 2005 3:07 am
Posts: 384
Location: Fairfax, Virginia
No, you don't have to. However, the terminal will block the GUI.

_________________
Andrew Krause

Foundations of GTK+ Development: Buy now for only $31.49!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 06, 2007 2:00 pm 
Offline
Familiar Face

Joined: Wed Jun 27, 2007 11:45 am
Posts: 5
Yes this was my problem,
in another post
http://www.gtkforums.com/viewtopic.php?t=709

I exposed the problem, I first tried to solve with
idle function, but i realized this was locking the gtk loop,
so I used threads. I am a newbye with both C and GTK+
particularly I am learning C using the book
"C/C++ von den Grundlagen zur professionellen Programmierung"
from Ulrich Kaiser and Kecher, and when it comes to the graphical
staff, they used some simplified libraries shipped with a CD,
but since I am on linux (cd is 4 windows) I choose to
face the problem by using GTK+.

Thanks for the comments
Roscarby[/url]


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 5 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