Tutorial

Inti Logo

« Container Widgets
The Alignment Widget »

The EventBox

Some 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 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 th following constructor:

EventBox();

A child widget can then be added to this EventBox:

eventbox->add(*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.

EventBox

The header file for Event Box is eventbox.h:

#include<inti/main.h>
#include <inti/core.h>
#include <inti/gdk/cursor.h>


using namespace Inti;

class EventBoxWindow : public Gtk::Window
{
    Pointer<Gdk::Cursor> hand_cursor;

protected:
    bool on_button_press_event(GdkEventButton *event);

public:
    EventBoxWindow();
    virtual ~EventBoxWindow();
};


and the source file is eventbox.cc:

#include"eventbox.h"
#include <inti/gtk/eventbox.h>
#include <inti/gtk/label.h>
#include <inti/gdk/window.h>

EventBoxWindow::EventBoxWindow()
{
    set_title("Event Box");
    set_border_width(10);

    // Create an EventBox and add it to our toplevel window
    Gtk::EventBox *event_box = new Gtk::EventBox;
    add(*event_box);
    event_box->show();

    // Create a long label
    Gtk::Label *label = new Gtk::Label("Click here to quit, quit, quit, quit, quit");
    event_box->add(*label);
    label->show();

    // Clip it short
    label->set_size_request(110, 20);

    // And bind an action to it
    event_box->set_events(Gdk::BUTTON_PRESS_MASK);
    event_box->sig_button_press_event().connect(slot(this, &EventBoxWindow::on_button_press_event));

    // Yet one more thing you need an X window for ...
    event_box->realize();
    hand_cursor = new Gdk::Cursor(GDK_HAND1);
    event_box->get_window()->set_cursor(*hand_cursor);
    show();
}

EventBoxWindow::~EventBoxWindow()
{
}

bool
EventBoxWindow::on_button_press_event(GdkEventButton*)
{
    dispose();
    return true;
}

int main (int argc, char *argv[])
{
    using namespace Main;

    init(&argc, &argv);

    EventBoxWindow window;
    window.sig_destroy().connect(slot(&Inti::Main::quit));

    run();
    return 0;
}




« Container Widgets Index
Top
 The Alignment Widget »