GTK+ 2.0 Tutorial
<<< Previous |  | Next >>>

# Container Widgets

# The EventBox

Some GTK widgets don't have associated X windows, so they just draw on their parents. Because of this, they cannot receive events and if they are incorrectly sized, they don't clip so you can get messy overwriting, etc. If you require more from these widgets, the EventBox is for you.

At first glance, the EventBox widget might appear to be totally useless. It draws nothing on the screen and responds to no events. However, it does serve a function - it provides an X window for its child widget. This is important as many GTK widgets do not have an associated X window. Not having an X window saves memory and improves performance, but also has some drawbacks. A widget without an X window cannot receive events, and does not perform any clipping on its contents. Although the name EventBox emphasizes the event-handling function, the widget can also be used for clipping. (and more, see the example below).

To create a new EventBox widget, use:

    GtkWidget *gtk_event_box_new( void );

A child widget can then be added to this EventBox:

        gtk_container_add (GTK_CONTAINER (event_box), child_widget);

The following example demonstrates both uses of an EventBox - a label is created that is clipped to a small box, and set up so that a mouse-click on the label causes the program to exit. Resizing the window reveals varying amounts of the label.

    #include <stdlib.h>
    #include <gtk/gtk.h>

    int main( int argc,
              char *argv[] )
    {
        GtkWidget *window;
        GtkWidget *event_box;
        GtkWidget *label;

        gtk_init (&argc, &argv);

        window = gtk_window_new (GTK_WINDOW_TOPLEVEL);

        gtk_window_set_title (GTK_WINDOW (window), "Event Box");

        g_signal_connect (G_OBJECT (window), "destroy",
    	              G_CALLBACK (exit), NULL);

        gtk_container_set_border_width (GTK_CONTAINER (window), 10);

        /* Create an EventBox and add it to our toplevel window */

        event_box = gtk_event_box_new ();
        gtk_container_add (GTK_CONTAINER (window), event_box);
        gtk_widget_show (event_box);

        /* Create a long label */

        label = gtk_label_new ("Click here to quit, quit, quit, quit, quit");
        gtk_container_add (GTK_CONTAINER (event_box), label);
        gtk_widget_show (label);

        /* Clip it short. */
        gtk_widget_set_size_request (label, 110, 20);

        /* And bind an action to it */
        gtk_widget_set_events (event_box, GDK_BUTTON_PRESS_MASK);
        g_signal_connect (G_OBJECT (event_box), "button_press_event",
    	              G_CALLBACK (exit), NULL);

        /* Yet one more thing you need an X window for ... */

        gtk_widget_realize (event_box);
        gdk_window_set_cursor (event_box->window, gdk_cursor_new (GDK_HAND1));

        gtk_widget_show (window);

        gtk_main ();

        return 0;
    }

<<< Previous | Home | Next >>>
File Selections |  | The Alignment widget