A.9. Event Watcher

event_watcher_enter_id := 0;
event_watcher_leave_id := 0;

/* An event watcher function is given three special variables:
   object: the object on which the event took place
   signal_id: the signal ID in question (faster than signal name)
   gtk_args: an array of the arguments to the event in question
*/

function event_watcher()
{
  princ ("Watch: ", gtk_signal_name(signal_id),  " emitted for ", 
	 class_name(object), "\n");
  TRUE;
}

function event_watcher_down()
{
  if (event_watcher_enter_id != 0)
    {
      local signal_id;

      signal_id = gtk_signal_lookup ("enter_notify_event",
				     gtk_widget_get_type());
      gtk_signal_remove_emission_hook (signal_id, event_watcher_enter_id);
      event_watcher_enter_id = 0;
	  
      signal_id = gtk_signal_lookup ("leave_notify_event",
				     gtk_widget_get_type());
      gtk_signal_remove_emission_hook (signal_id, event_watcher_leave_id);
      event_watcher_leave_id = 0;
    }
}

function event_watcher_toggle(button)
{
  if(button.get_active() == FALSE)
    event_watcher_down();
  else
    {
      local signal_id;
      signal_id = gtk_signal_lookup ("enter_notify_event",
				     gtk_widget_get_type());
      event_watcher_enter_id =
	gtk_signal_add_emission_hook (signal_id, #event_watcher());
	  
      signal_id = gtk_signal_lookup ("leave_notify_event",
				     gtk_widget_get_type());
      event_watcher_leave_id =
		  gtk_signal_add_emission_hook (signal_id, #event_watcher());
    }
}

function create_event_watcher ()
{
  local button, vbox, separator, action_area;
  
  win_event_watcher = new (GtkDialog);
  win_event_watcher.signal ("destroy", #event_watcher_down());
  win_event_watcher.signal ("destroy", #win_event_watcher = nil);
  win_event_watcher.title = "Event Watcher";
  win_event_watcher.border_width = 0;
  win_event_watcher.set_usize(200, 110);
  
  vbox = car(win_event_watcher.children());
  separator = car(vbox.children());
  action_area = cadr(vbox.children());
  
  button = new(GtkToggleButton);
  button.label = "Activate Watch";
  button.signal("clicked", `event_watcher_toggle(@button));
  button.set_border_width(10);
  vbox.pack_start(button, TRUE, TRUE, 0);
  button.show();
  
  button = new (GtkButton);
  button.label = "Close";
  button.signal ("clicked", `(@win_event_watcher).destroy());
  action_area.pack_start (button, TRUE, TRUE, 0);
  button.can_default = TRUE;
  button.grab_default();
  button.show();
  
  win_event_watcher.show_all();
  win_event_watcher;
}

function main ()
{
  local window, win_event_watcher;
  TRUE=1;
  FALSE=0;
  window = create_event_watcher ();
  window.signal ("destroy", #exit_program(0));
  init_ipc("event_wat", "event_watq");
  gtk_main ();
}