Re: Mouse x/y coordinates?



>>>>> "D" == David J Topper <topper@virginia.edu> writes:

 D> Hello, I've leafed through the GTK tutorial (up to chap 4 so far)
 D> and have looked at the table of contents.  It seems to me that I
 D> should be able to set up an app with a blank window, then output
 D> x/y mouse coordinates whenever I "click" on that window.  But I
 D> see no such event / data type for doing that.

You need the button press events here.  You can connect that as you
would any signal. But to actually *get* the signal you must change the
event mask...this is what happens in the lines

  mask = gtk_widget_get_events (window);
  mask |= GDK_BUTTON_PRESS_MASK;
  gtk_widget_set_events (window, mask);

of the attached program.

You can the get the coordinates from the second parameter to the
callback.

/mailund

===File ~/tmp/foo.c=========================================
#include <gtk/gtk.h>

static gboolean
button_press (GtkWidget *win, GdkEventButton *ev)
{
  g_return_val_if_fail (win != NULL, FALSE);
  g_return_val_if_fail (ev != NULL, FALSE);

  g_print ("(x,y) == (%d,%d)\n",
	   (gint)ev->x, (gint)ev->y);

  return TRUE;
}

int
main (int argc, char *argv[])
{
  GtkWidget *window;
  GdkWindowAttr attributes;
  GdkEventMask mask;

  gtk_init (&argc, &argv);

  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);

  gtk_signal_connect (GTK_OBJECT (window), "button_press_event",
		      GTK_SIGNAL_FUNC (button_press), NULL);
  mask = gtk_widget_get_events (window);
  mask |= GDK_BUTTON_PRESS_MASK;
  gtk_widget_set_events (window, mask);


  gtk_widget_show (window);

  gtk_main ();

  return 0;
}
============================================================



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