cgtkgtk4

geting the coordinates of the user's mouse pointer in GTK4 in C


I am building an app that at certain points needs to know the location of the user's pointer. I am using GTK4 and C.

  1. I found this answer (How to use gdk_device_get_position()?) which pointed to the function gdk_device_get_position, but I can't find it in the GTK4 docs, suggesting that its no longer supported. If there's some equivalent that I'm missing I'd love to know.
  2. Taking another tack, I've successfully implemented an event controller for the widget in question, thus:
GtkEventController *my_controller = gtk_event_controller_motion_new ();
gtk_widget_add_controller (my_widget, my_controller);
g_signal_connect(my_controller, "motion", my_function, NULL);

This successfully fires and runs my_function when I move the mouse inside the widget. Reading the signature for the motion signal (https://docs.gtk.org/gtk4/signal.EventControllerMotion.motion.html) below, it appears that it should announce the x and y coordinates. How, so to speak, do I "get at" these?

void
motion (
  GtkEventControllerMotion* self,
  gdouble x,
  gdouble y,
  gpointer user_data
)

Sorry if I'm missing something obvious! Wouldn't be asking if I hadn't done my docs/SO due diligence.

Thank you!


Solution

  • The subject has ever emerged on the following question:How to rotate cairo circle (volume button) on mouse motion event in gtk4?

    Here is a summary of how it can be solved:

    
    
    #include"mous-coordin.h"
    
    void pointer_motion_event (GtkEventController *gesture,
                               gdouble x,
                               gdouble y,
                               gpointer user_data)
    {
        g_print(" x = %f y = %f\n", x,y);
    }
    
    void click_event (GtkEventController *gesture,
                               gdouble x,
                               gdouble y,
                               gpointer user_data)
    
    {
        g_print(" CLICK x = %f y = %f\n", x,y);
    }
    
    void end_event(GtkEventController *gesture,
                    gdouble x,
                    gdouble y,
                    gpointer user_data)
    {
        g_print(" RELEASE x = %f y = %f\n", x,y);
    
    }
    
    void activate (GtkApplication *app, gpointer data)
    {
      GtkWidget *window;
    
      window =gtk_application_window_new(app);
      gtk_widget_set_size_request(window,150,150);
     
          GtkEventController *gesture_pos;
          gesture_pos = gtk_event_controller_motion_new ();
          gtk_widget_add_controller (window, (GtkEventController *)gesture_pos);
          gtk_event_controller_set_propagation_phase((GtkEventController  *)gesture_pos, GTK_PHASE_CAPTURE);
          g_signal_connect (gesture_pos, "motion", G_CALLBACK (pointer_motion_event), window);
    
        GtkGesture *gesture = gtk_gesture_click_new();
        gtk_gesture_single_set_button(GTK_GESTURE_SINGLE(gesture), 0);
        gtk_widget_add_controller(window,(GTK_EVENT_CONTROLLER(gesture)));
        g_signal_connect (gesture, "pressed", G_CALLBACK (click_event), NULL);
        g_signal_connect (gesture,"released",G_CALLBACK(end_event),NULL);
    
     gtk_widget_set_visible(window,TRUE);
    }
    

    Have fun programming.