Grabbing the pointer...



Hello,
I'm trying to grab a widget out of a window. Listening to leafox's
advice, I think I am close to the solution but I've got one problem yet.
I don't manage to re-grab the pointer when the window goes out. My
source widget is a drawing area:

        GtkWidget *drawing_area = gtk_drawing_area_new ();
        gtk_signal_connect(GTK_OBJECT(drawing_area), "event",
                           GTK_SIGNAL_FUNC(event), 0);
        gtk_widget_set_events (drawing_area, GDK_EXPOSURE_MASK
                               | GDK_BUTTON_PRESS_MASK
                               | GDK_BUTTON_RELEASE_MASK
            );

The callback function looks like that (_x and _y are two static
variables):

int event ( GtkWidget *widget, GdkEvent *event, gpointer ptr )
    {
        GdkEventMotion mevent = event->motion;

        switch( event->type ){
        case GDK_CONFIGURE:
            // Configuring the drawing area
        .....
            break;
        case GDK_EXPOSE:
            // Exposing the drawing area
        .......
            break;
        case GDK_BUTTON_PRESS:
            _x = int(event->button.x);
            _y = int(event->button.y);
            // I grab the pointer
            gdk_pointer_grab(widget->window, 0,
                             GdkEventMask(GDK_BUTTON_RELEASE_MASK
                                          | GDK_POINTER_MOTION_MASK
                                          | GDK_POINTER_MOTION_HINT_MASK),
                             0, gdk_cursor_new(GDK_HAND2),
                             event->button.time );
            break;
        case GDK_BUTTON_RELEASE:
            // I ungrab the pointer
            gdk_pointer_ungrab( event->button.time );
            break;
        case GDK_MOTION_NOTIFY:
            int x,y;
            GdkModifierType state;
            if ( mevent.is_hint)
                gdk_window_get_pointer (mevent.window, &x, &y, &state);
            else{
                x = int(mevent.x);
                y = int(mevent.y);
                state = (GdkModifierType) mevent.state;
            }
            if ( !(state && GDK_BUTTON1_MASK) ){
                return 1;
            }
            if ( !GTK_IS_WINDOW(widget->parent->parent) )
            {
                // Is the pointer move enought?
                if ( abs(_x - x)>5 || abs(_y - y)>5 )
                {
                    gdk_pointer_ungrab( mevent.time );
                    // I reparent the widget->parent with a new toplevel window.
                    // I place it and I show it.
                ..........
                    gdk_pointer_grab(widget->window, 0,
                                 GdkEventMask(
                                    GDK_BUTTON_RELEASE_MASK
                                     | GDK_POINTER_MOTION_MASK
                                     | GDK_POINTER_MOTION_HINT_MASK),
                                 0, gdk_cursor_new(GDK_HAND2),
                                 mevent.time );
                    return 1;
                }else return 1;
            }
            // I move the window
        .....................
            break;
        default: return 0;
        }
        return 1;
    }

It doesn't work because the gtk_pointer_grab doesn't grab the pointer
when the widget is out. Besides, when the widget is out, the pointer
ungrabs on his own. Can someone explain me how gdk_pointer_grab and
gdk_pointer_ungrab work?

Clement.




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