All the code presented so far implements the GtkWidget and
GtkObject interfaces.
GtkEv does have some unique functionality; namely,
it responds to events on its event window by adding text
describing the event to its buffer, and queueing a
redraw. To do this, it overrides the default "event" signal handler.
Here is GtkEv's event method:
static gint
gtk_ev_event (GtkWidget *widget,
GdkEvent *event)
{
GtkEv* ev;
g_return_val_if_fail(widget != NULL, FALSE);
g_return_val_if_fail(GTK_IS_EV(widget), FALSE);
ev = GTK_EV(widget);
if (event->any.window == widget->window)
{
if (GTK_WIDGET_CLASS(parent_class)->event)
return (* GTK_WIDGET_CLASS(parent_class)->event) (widget, event);
else
return FALSE;
}
else
{
gchar* text;
/* The event is either on ev->event_window, or it is a key event
* passed down to us from the toplevel GtkWindow
*/
text = event_to_text(event);
gtk_ev_push_text(ev, text);
g_free(text);
/* If it was a motion event, make sure we get more */
if (event->type == GDK_MOTION_NOTIFY)
{
gdk_window_get_pointer(ev->event_window, NULL, NULL, NULL);
}
/* We didn't "handle" the event, just listened in on it. */
return FALSE;
}
}
|
Notice that the window
method of the event is used to distinguish events that
occur on
widget->window from events that occur on the
event subwindow. Some events will be received from a
different window entirely; for example, key events
actually occur on a toplevel window, and are passed to
GtkEv if GtkEv has the focus.
event_to_text() is a lengthy
but trivial function that creates a string describing the
event; gtk_ev_push_text()
pushes the text onto the front of the buffer and queues a
redraw. The implementation of these functions is part of
the complete GtkEv code
listing, in Appendix E.