4

I just ported a C program from GTK2 to GTK3. The program uses a GtkDrawingArea on which the mouse scroll wheel is used. After the switch to GTK3 I am not able to detect the scroll wheel usage properly anymore. Here is what I do and get:

GTK2 (2.24.10)

  1. I set the event mask of the GtkDrawingArea to GDK_EXPOSURE_MASK | GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK | GDK_LEAVE_NOTIFY_MASK.
  2. I connect a signal handler to scroll-event of the GtkDrawingArea.
  3. Once I use the scroll wheel, the signal handler is called and the property direction of the GdkEventScroll is either GDK_SCROLL_UP or GDK_SCROLL_DOWN.

GTK3 (3.4.2)

  1. In addition to the above, I include GDK_SMOOTH_SCROLL_MASK in the event mask of the GtkDrawingArea. Without that, my signal handler is never called.
  2. I use the same signal handler as described above.
  3. Once I use the scroll wheel, the signal handler is called and the property direction of the GdkEventScroll is GDK_SCROLL_SMOOTH. Calling gdk_event_get_scroll_deltas() gives me always 0 for both delta_x and delta_y. I never see any other directions than GDK_SCROLL_SMOOTH.

I can live with receiving smooth scroll events although my mouse itself only delivers "discrete" button4/5 events, but how do I figure out the direction of the scroll event?

BTW: I am using Ubuntu 12.04 on amd64.

6
  • I found a discussion about the switch from GDK_SCROLL_{UP|DOWN} to GDK_SCROLL_SMOOTH but the problem with delta_x and delta_y being 0 is not mentioned there.
    – dasup
    Commented Aug 2, 2012 at 11:07
  • Did you try adding GDK_SCROLL_MASK? Assuming you don't care about smooth scrolling that is. See bugzilla.gnome.org/show_bug.cgi?id=671305
    – ergosys
    Commented Aug 2, 2012 at 17:31
  • @ergosys: Yes, I included GDK_SCROLL_MASK (see 1. under GTK3). Without this flag my signal handler is never called under GTK3.
    – dasup
    Commented Aug 3, 2012 at 8:44
  • I don't see where you do. You have GDK_SMOOTH_SCROLL_MASK, which is a different mask.
    – ergosys
    Commented Aug 3, 2012 at 19:09
  • Sorry, I misread the constant name from your reply. I added GDK_SCROLL_MASK but it does not make any difference. I still only get GDK_SCROLL_SMOOTH events.
    – dasup
    Commented Aug 6, 2012 at 12:33

2 Answers 2

4

There is a bug in Gtk3 associated with the "scroll-event" signal. First, in your callback you should use the delta_x and delta_y members of the GdkEventScroll structure. Second, the first time you scroll your mouse, these members will both be zero (bug!). Subsequent scrolls will give you correct values. Third, it seems this bug will not be fixed, unfortunately.

1
  • 2
    Thank you for pointing this out. However, in my tests delta_x and delta_y were always 0, not only for the first event. Anyway, I gave up finding the source of the problem and switched to GDK_SCROLL_UP/DOWN instead of GDK_SCROLL_SMOOTH.
    – dasup
    Commented Sep 3, 2012 at 14:05
0

I'm on gnome 3.38 (X11 based) using the newest stable GTK4 build (as of aug 2022) and to this day, if GDK_SCROLL_MASK is enabled, all GdkScrollEvents will have a delta of 0 (at all times, not just the first), so it just doesn't work at all I guess.

The alternative was to instead enable GDK_SMOOTH_SCROLL_MASK, connect to the "scroll-event" signal using the same signature as with GDK_SCROLL_MASK, then access the delta like so:

gboolean on_scroll_event(GtkWidget*, GdkEventScroll* event, gpointer*)
{
    // ignore all events fitting GDK_SCROLL_MASK
    if (event->type != GDK_SCROLL_SMOOTH)
        return false;

    double delta_x, delta_y;
    gdk_event_get_scroll_deltas((GdkEvent*) event, &x, &y);

    // use delta_x and delta_y here
    // do NOT use event->delta_x / event->delta_y
}

Note that I don't see a reason to ever enable GDK_SCROLL_MASK at all, if you don't enable it the if branch above is unnecessary.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.