Puzzled by gtk_grab_add() behaviour, can anyone help?



Hi all, I'm trying to use gtk_grab_add(), and I think I'm doing something
wrong. 

As expected, after I grab I get motion events wherever I move the mouse
(outside the window, into other event boxes, into buttons etc), and these
other widgets don't get events.

However, if (during the grab) I mouse over a GtkEntry or over the central
raised part of a scollbar, my motion events stop. Why is this? Is there
anything I can do to stop this happening? All advice warmly welcomed.

This is gtk 1.2.1 (if that affects things). Here's a test program:
--
#include <gtk/gtk.h>

static gboolean
peek_event( GtkWidget *widget, GdkEvent *ev, char *name )
{
        g_message( "%s: %d", name, ev->type );

        return( FALSE );
}

/* Grab and ungrab a widget on button 1.
 */
static gboolean
grab_release( GtkWidget *widget, GdkEvent *ev, gpointer dummy )
{
        switch( ev->type ) {
        case GDK_BUTTON_PRESS:
                g_message( "*** grabbing" );
                gtk_grab_add( widget );
                break;

        case GDK_BUTTON_RELEASE:
                g_message( "*** releasing" );
                gtk_grab_remove( widget );
                break;
        }

        return( FALSE );
}

int 
main( int argc, char *argv[] )
{
        GtkWidget *window;
        GtkWidget *table;
        GtkWidget *box;
        GtkWidget *label;
        GtkWidget *button;
        GtkWidget *entry;
        GtkWidget *sbar;
  
        gtk_init( &argc, &argv );
        window = gtk_window_new( GTK_WINDOW_TOPLEVEL );
        gtk_signal_connect( GTK_OBJECT( window ), "destroy", 
                GTK_SIGNAL_FUNC( gtk_main_quit ), NULL );

        table = gtk_table_new( 3, 3, TRUE );
        gtk_container_add( GTK_CONTAINER( window ), table );

        box = gtk_event_box_new();
        gtk_signal_connect( GTK_OBJECT( box ), "event",
                GTK_SIGNAL_FUNC( peek_event ), "event box #1" );
        gtk_signal_connect( GTK_OBJECT( box ), "event",
                GTK_SIGNAL_FUNC( grab_release ), NULL );
        gtk_table_attach_defaults( GTK_TABLE( table ), box,
                0, 1, 0, 1 );
        label = gtk_label_new( "Event box #1\nGrab and release with button 1"
);
        gtk_container_add( GTK_CONTAINER( box ), label );

        sbar = gtk_hscrollbar_new( NULL );
        gtk_table_attach_defaults( GTK_TABLE( table ), sbar,
                1, 2, 0, 1 );

        entry = gtk_entry_new();
        gtk_table_attach_defaults( GTK_TABLE( table ), entry,
                2, 3, 0, 1 );

        button = gtk_button_new_with_label( "Button" );
        gtk_table_attach_defaults( GTK_TABLE( table ), button,
                0, 1, 1, 2 );

        box = gtk_event_box_new();
        gtk_signal_connect( GTK_OBJECT( box ), "event",
                GTK_SIGNAL_FUNC( peek_event ), "event box #2" );
        gtk_table_attach_defaults( GTK_TABLE( table ), box,
                1, 2, 1, 2 );
        label = gtk_label_new( "Event box #2" );
        gtk_container_add( GTK_CONTAINER( box ), label );

        gtk_widget_show_all( window );

        gtk_main();

        return( 0 );
}
--

John
--
John Cupitt, john.cupitt@ng-london.org.uk, +44 (0)171 930 2108
VASARI Lab, The National Gallery, Trafalgar Square, London, WC2N 5DN



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]