Hello.
Let me explain how things generally work.
Drawing is generally done on demand. This means that expose event is only triggered when windowing system needs some input. And even when expose event is triggered, you usually only need to redraw part of your GUI.
For example, let's assume that two windows are arranged like this:
Window1 (red rectangle) represents your script window and window2 (blue rectangle) represents main window. In this sample scenario, we move window1 from position window1' (marked as transparent rectangle) into final position. During this move, window1 hasn't been obscured or otherwise damaged, which also means that windowing system does know how this window should be drawn. But during this move, parts of window2 that have been previously obscured are now visible. since windowing system doesn't know what should be there, it asks your application for some input and this is why your main window will receive expose event.
As you can see, expose events have nothing to do with active window. They are emitted when part of window needs to be redrawn.
Now to solving your problem. What you need to monitor is currently active window. Easiest way of doing this would be to monitor GtkWindow's "is-active" property, which will change when window is switched.
And since "notify" signal is not one of the best known ones, I'm providing some simple code to give you a kick start:
Code:
#include <gtk/gtk.h>
static void
monitor_active_window( GObject *object,
GParamSpec *pspec,
gpointer data )
{
gboolean active;
g_object_get( object, "is-active", &active, NULL );
if( active )
g_print( "Active: WINDOW%d\n", GPOINTER_TO_INT( data ) );
}
int
main( int argc,
char **argv )
{
GtkWidget *window,
*label;
gtk_init( &argc, &argv );
/* Window 1 */
window = gtk_window_new( GTK_WINDOW_TOPLEVEL );
gtk_window_set_default_size( GTK_WINDOW( window ), 400, 300 );
g_signal_connect( G_OBJECT( window ), "destroy",
G_CALLBACK( gtk_main_quit ), NULL );
g_signal_connect( G_OBJECT( window ), "notify::is-active",
G_CALLBACK( monitor_active_window ),
GINT_TO_POINTER( 1 ) );
label = gtk_label_new( "WINDOW1" );
gtk_container_add( GTK_CONTAINER( window ), label );
gtk_widget_show_all( window );
/* Window 2 */
window = gtk_window_new( GTK_WINDOW_TOPLEVEL );
gtk_window_set_default_size( GTK_WINDOW( window ), 400, 300 );
g_signal_connect( G_OBJECT( window ), "destroy",
G_CALLBACK( gtk_main_quit ), NULL );
g_signal_connect( G_OBJECT( window ), "notify::is-active",
G_CALLBACK( monitor_active_window ),
GINT_TO_POINTER( 2 ) );
label = gtk_label_new( "WINDOW2" );
gtk_container_add( GTK_CONTAINER( window ), label );
gtk_widget_show_all( window );
gtk_main();
return( 0 );
}
Tadej