GTK+ Forums Forum Index GTK+ Forums
Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Interacting via terminal after a window is showed?

 
Post new topic   Reply to topic    GTK+ Forums Forum Index -> GTK+ Programming
Author Message
roscarby
Familiar Face


Joined: 27 Jun 2007
Posts: 5

PostPosted: Wed Jun 27, 2007 12:13 pm    Post subject: Interacting via terminal after a window is showed? Reply with quote

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: (Plaintext)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67

/* 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]
Back to top
openldev
Never Seen the Sunlight


Joined: 21 Sep 2005
Posts: 387
Location: Fairfax, Virginia

PostPosted: Wed Jun 27, 2007 12:43 pm    Post subject: Reply with quote

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: (Plaintext)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

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);
   }
}
Back to top
roscarby
Familiar Face


Joined: 27 Jun 2007
Posts: 5

PostPosted: Wed Jun 27, 2007 1:05 pm    Post subject: Reply with quote

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
Back to top
caracal
GTK+ Geek


Joined: 21 Jun 2007
Posts: 91
Location: Wilkes Barre Pa

PostPosted: Thu Jul 05, 2007 7:12 pm    Post subject: Reply with quote

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.
Back to top
openldev
Never Seen the Sunlight


Joined: 21 Sep 2005
Posts: 387
Location: Fairfax, Virginia

PostPosted: Thu Jul 05, 2007 7:22 pm    Post subject: Reply with quote

No, you don't have to. However, the terminal will block the GUI.
Back to top
roscarby
Familiar Face


Joined: 27 Jun 2007
Posts: 5

PostPosted: Fri Jul 06, 2007 2:00 pm    Post subject: Reply with quote

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]
Back to top
Display posts from previous:   
Post new topic   Reply to topic    GTK+ Forums Forum Index -> GTK+ Programming All times are GMT
Page 1 of 1

 


Powered by phpBB © 2001, 2005 phpBB Group
CodeBB 1.0 Beta 2
Protected by Anti-Spam ACP