
/* Compile with
   gcc `pkg-config --cflags --libs gtk+-2.0` event-test.c
*/

#include <stdlib.h>
#include <string.h>
#include <gtk/gtk.h>
        
        
static gboolean on_delete_event      (GtkWidget      *window,
				      GdkEvent       *event,
				      gpointer        unused_data);
        
        
static gboolean
on_widget_enter_notify (GtkWidget *widget,
			GdkEventCrossing *event,
			char *item_id)
{
  g_print ("%s received 'enter-notify' signal   (window: %p)\n", item_id,
	   event->window);
  return TRUE;
}


static gboolean
on_widget_leave_notify (GtkWidget *widget,
			GdkEventCrossing *event,
			char *item_id)
{
  g_print ("%s received 'leave-notify' signal   (window: %p)\n", item_id,
	   event->window);
  return TRUE;
}


static gboolean
on_widget_motion_notify (GtkWidget *widget,
			 GdkEventMotion *event,
			 char *item_id)
{
  g_print ("%s received 'motion-notify' signal  (window: %p)\n", item_id,
	   event->window);
  if (event->is_hint)
    gdk_window_get_pointer (event->window, NULL, NULL, NULL);
  return TRUE;
}


static gboolean
on_widget_button_press (GtkWidget *widget,
			GdkEventButton *event,
			char *item_id)
{
  g_print ("%s received 'button-press' signal   (window: %p)\n", item_id,
	   event->window);
  return TRUE;
}


static gboolean
on_widget_button_release (GtkWidget *widget,
			  GdkEventButton *event,
			  char *item_id)
{
  g_print ("%s received 'button-release' signal (window: %p)\n", item_id,
	   event->window);
  return TRUE;
}


int
main (int argc, char *argv[])
{
  GtkWidget *window, *drawing_area;
  char *view_id = "Drawing Area";
        
  /* Initialize GTK+. */
  gtk_set_locale ();
  gtk_init (&argc, &argv);
        
  /* Create the window and widgets. */
  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  gtk_window_set_default_size (GTK_WINDOW (window), 640, 600);
  gtk_widget_show (window);
  g_signal_connect (window, "delete_event", G_CALLBACK (on_delete_event),
		    NULL);
        
  drawing_area = gtk_drawing_area_new ();
  gtk_widget_set_events (drawing_area,
			 GDK_EXPOSURE_MASK
			 | GDK_BUTTON_PRESS_MASK
			 | GDK_BUTTON_RELEASE_MASK
			 | GDK_POINTER_MOTION_MASK
			 | GDK_POINTER_MOTION_HINT_MASK
			 | GDK_KEY_PRESS_MASK
			 | GDK_KEY_RELEASE_MASK
			 | GDK_ENTER_NOTIFY_MASK
			 | GDK_LEAVE_NOTIFY_MASK
			 | GDK_FOCUS_CHANGE_MASK);
			 
  gtk_container_add (GTK_CONTAINER (window), drawing_area);
  gtk_widget_show (drawing_area);

  g_signal_connect (drawing_area, "enter_notify_event",
		    G_CALLBACK (on_widget_enter_notify), view_id);
  g_signal_connect (drawing_area, "leave_notify_event",
		    G_CALLBACK (on_widget_leave_notify), view_id);
  g_signal_connect (drawing_area, "motion_notify_event",
		    G_CALLBACK (on_widget_motion_notify), view_id);
  g_signal_connect (drawing_area, "button_press_event",
		    G_CALLBACK (on_widget_button_press), view_id);
  g_signal_connect (drawing_area, "button_release_event",
		    G_CALLBACK (on_widget_button_release), view_id);
        
  gtk_main ();
        
  return 0;
}

        
static gboolean
on_delete_event (GtkWidget *window,
		 GdkEvent  *event,
		 gpointer   unused_data)
{
  exit (0);
}
