Using derived widgets
Prev | Chapter 19. Glade and libglademm | Next

## Using derived widgets

You can use Glade to layout your own custom widgets derived from gtkmm widget classes. This keeps your code organised and encapsulated. Of course you won't see the exact appearance and properties of your derived widget in Glade, but you can specify its location and child widgets and the properties of its gtkmm base class.

Use `Glade::Xml::get_widget_derived()` like so:

    DerivedDialog* pDialog = 0;
    refXml->get_widget_derived("DialogBasic", pDialog);

Your derived class must have a constructor that takes a pointer to the underlying C type (there is a typedef for this), and the Gnome::Glade::Xml instance. You must call the base class's constructor in the initialization list, providing the C pointer. For instance,

    DerivedDialog::DerivedDialog(BaseObjectType* cobject, const Glib::RefPtr<Gnome::Glade::Xml>& refGlade)
    : Gtk::Dialog(cobject)
    {
    }

You could then encapsulate the manipulation of the child widgets in the constructor of the derived class, maybe using get_widget() or get_widget_derived() again. For instance,

    DerivedDialog::DerivedDialog(BaseObjectType* cobject, const Glib::RefPtr<Gnome::Glade::Xml>& refGlade)
    : Gtk::Dialog(cobject),
      m_refGlade(refGlade),
      m_pButton(0)
    {
      //Get the Glade-instantiated Button, and connect a signal handler:
      m_refGlade->get_widget("quit_button", m_pButton);
      if(m_pButton)
      {
        m_pButton->signal_clicked().connect( sigc::mem_fun(*this, &DerivedDialog::on_button_quit) );
      }
    }

### Example

The `derived` example in the `libglademm` package shows how to load a Glade file at runtime and access a widgets via a derived class.

Prev | Up | Next
Accessing widgets | Home | Chapter 20. Internationalization and Localization