### Tutorial

« Writing Your Own Widgets | Creating a Composite Widget »

## Anatomy of a Widget

All widgets are derived from the Gtk::Widget and as such inherit all the Gtk::Widget public and protected methods. Gtk::Widget inherits from Gtk::Object, which inherits from G::Object, the base class for all objects, so a widget inherits their public and protected methods as well. There are other base classes but they are an implementation detail and not really very useful to you.
When you derive a new widget class from an existing widget class you inherit its protected virtual signal handlers. You can customize your new widget by overriding one or more of these signal handlers instead of connecting to an existing signal handler as we did in previously. For example, Gtk::Button has six virtual signal handlers:

`virtual void on_pressed(); virtual void on_released(); virtual void on_clicked(); virtual void on_enter(); virtual void on_leave(); virtual void on_activate();`

To implement a new button widget that customizes the standard button behaviour you would derive your button widget from Gtk::Button and override the appropriate signal handlers. If you require the default button behaviour you must explicitly call the base class signal handler, like this:

`void MyButton::on_pressed()` `` `{ // Put you code here // Call the base class handler` `Gtk::Button::on_pressed(); }`

Otherwise you can ignore the it.
Each widget class has a protected constructor. For Gtk::Button it is:

`explicit Button(GtkButton *button, bool reference = false);`

This constructor takes a pointer to a GtkButton and a reference flag. The reference flag is used by the Inti smart pointer (Pointer<>) to decide if a managed object needs to have its initial reference count of one removed when the object is destroyed. If reference is true the initial reference count of 1 needs to be removed and if it's false it doesn't. Most of the time the default value is appropriate. For Gtk::Widget's the default flag is usually false since a widget gets added to a container and the container manages the widget. For G::Objects the default flag is true and the initial reference count must be explicitly removed.
Other protected methods common to all classes are the static properties and static signals. To work with these you need to pass their public methods an object pointer. Remember from earlier, this is done for you when you use the public property and signal accessors.

« Writing Your Own Widgets | Index Top | Creating a Composite Widget »