Mouse movement callbacks in GTK4

I’m tracking the mouse movement in a GtkDrawingArea.
I have set up the callbacks for movement, enter and leave (which all work).
I’m using the same callback for all three event types, examining the event returned to differentiate them.
I get the expected EventType (GDK_MOTION_NOTIFY) for movement but I get 0 for enter and leave (rather than 6 & 7 - GDK_ENTER_NOTIFY & GDK_LEAVE_NOTIFY). What am I missing?

Michael

void on_PlotMouseMotion (
  GtkEventControllerMotion* self, gdouble x,  gdouble y, gpointer wDrawingArea
) {
    GdkEventType eventType = gdk_event_get_event_type( gtk_event_controller_get_current_event( GTK_EVENT_CONTROLLER( self ) ) );
    g_print( "%d - x: %.1lf y: %.1lf\n", eventType, x, y );
}

    GtkEventController *eventMouseMotion = gtk_event_controller_motion_new ();
    gtk_event_controller_set_propagation_phase(eventMouseMotion, GTK_PHASE_CAPTURE);
    g_signal_connect(eventMouseMotion, "motion", G_CALLBACK( on_PlotMouseMotion ), wDrawingArea);
    g_signal_connect(eventMouseMotion, "enter",  G_CALLBACK( on_PlotMouseMotion ), wDrawingArea);
    g_signal_connect(eventMouseMotion, "leave",  G_CALLBACK( on_PlotMouseMotion ), wDrawingArea);
    gtk_widget_add_controller (wDrawingArea, GTK_EVENT_CONTROLLER (eventMouseMotion));

Enter and leave signals are emitted from synthetic crossing events. The gtk_event_controller_get_current_event() function can return NULL: are you sure you’re not getting a critical warning from gdk_event_get_event_type() as well?

There are very few cases in which you may want to get the GdkEvent from an event controller.

In your specific case, do not use the same function for all three callback handlers; rather, if you want to keep all the event handling logic in a single function, add an argument to it, and call it from the corresponding signal handler, e.g.

enum {
  EVENT_ENTER,
  EVENT_MOTION,
  EVENT_LEAVE
} event_type;

static void
event_handler (GtkDrawingArea *area,
               double x,
               double y,
               enum event_type);

static void
on_plot_motion (GtkEventController *motion,
                double x,
                double y,
                gpointer data)
{
  event_handler (gtk_event_controller_get_widget (motion), x, y, EVENT_MOTION);
}

static void
on_plot_enter (GtkEventController *motion,
               double x,
               double y,
               gpointer data)
{
  event_handler (gtk_event_controller_get_widget (motion), x, y, EVENT_ENTER);
}

static void
on_plot_leave (GtkEventController *motion,
               double x,
               double y,
               gpointer data)
{
  event_handler (gtk_event_controller_get_widget (motion), x, y, EVENT_LEAVE);
}

This way it’ll be easier to refactor code in the case of more events, or in case you will end up splitting up the event handling code into separate functions when it gets inevitably too big to maintain.

Yes, I was getting NULL from that call when getting the callback from enter and leave.

OK, I’ll use separate callback routines. thanks.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.