Chapter 7. Widget Overview

Table of Contents

1. Widget Hierarchy
2. Widgets Without Windows

The general steps to creating a widget in GTK are:

  1. Gtk2::*::new() - one of various functions to create a new widget. These are all detailed in this section.
  2. Connect all signals and events we wish to use to the appropriate handlers.
  3. Set the attributes of the widget.
  4. Pack the widget into a container using the appropriate call such as Gtk2::Container::add() or Gtk2::Box::pack_start().
  5. Gtk2::Widget::show() the widget.

Gtk2::Widget::show() lets GTK know that we are done setting the attributes of the widget, and it is ready to be displayed. You may also use Gtk2::Widget::hide() to make it disappear again. The order in which you show the widgets is not important, but I suggest showing the window last so the whole window pops up at once rather than seeing the individual widgets come up on the screen as they're formed. The children of a widget (a window is a widget too) will not be displayed until the window itself is shown using the Gtk2::Widget::show() function.

1. Widget Hierarchy

For your reference, here is the class hierarchy tree used to implement widgets. (Deprecated widgets and auxiliary classes have been omitted.)

Glib::Object
 |  
 Gtk2::Object
  +Gtk2::Widget
  | +Gtk2::Misc
  | | +Gtk2::Label
  | | | `Gtk2::AccelLabel
  | | +Gtk2::Arrow
  | | `Gtk2::Image
  | +Gtk2::Container
  | | +Gtk2::Bin
  | | | +Gtk2::Alignment
  | | | +Gtk2::Frame
  | | | | `Gtk2::AspectFrame
  | | | +Gtk2::Button
  | | | | +Gtk2::ToggleButton
  | | | | | `Gtk2::CheckButton
  | | | | |   `Gtk2::RadioButton
  | | | | `Gtk2::OptionMenu
  | | | +Gtk2::Item
  | | | | +Gtk2::MenuItem
  | | | |   +Gtk2::CheckMenuItem
  | | | |   | `Gtk2::RadioMenuItem
  | | | |   +Gtk2::ImageMenuItem
  | | | |   +Gtk2::SeparatorMenuItem
  | | | |   `Gtk2::TearoffMenuItem
  | | | +Gtk2::Window
  | | | | +Gtk2::Dialog
  | | | | | +Gtk2::ColorSelectionDialog
  | | | | | +Gtk2::FileSelection
  | | | | | +Gtk2::FontSelectionDialog
  | | | | | +Gtk2::InputDialog
  | | | | | `Gtk2::MessageDialog
  | | | | `Gtk2::Plug
  | | | +Gtk2::EventBox
  | | | +Gtk2::HandleBox
  | | | +Gtk2::ScrolledWindow
  | | | `Gtk2::Viewport
  | | +Gtk2::Box
  | | | +Gtk2::ButtonBox
  | | | | +Gtk2::HButtonBox
  | | | | `Gtk2::VButtonBox
  | | | +Gtk2::VBox
  | | | | +Gtk2::ColorSelection
  | | | | +Gtk2::FontSelection
  | | | | `Gtk2::GammaCurve
  | | | `Gtk2::HBox
  | | |   +Gtk2::Combo
  | | |   `Gtk2::Statusbar
  | | +Gtk2::Fixed
  | | +Gtk2::Paned
  | | | +Gtk2::HPaned
  | | | `Gtk2::VPaned
  | | +Gtk2::Layout
  | | +Gtk2::MenuShell
  | | | +Gtk2::MenuBar
  | | | `Gtk2::Menu
  | | +Gtk2::Notebook
  | | +Gtk2::Socket
  | | +Gtk2::Table
  | | +Gtk2::TextView
  | | +Gtk2::Toolbar
  | | `Gtk2::TreeView
  | +Gtk2::Calendar
  | +Gtk2::DrawingArea
  | | `Gtk2::Curve
  | +Gtk2::Editable
  | | +Gtk2::Entry
  | |   `Gtk2::SpinButton
  | +Gtk2::Ruler
  | | +Gtk2::HRuler
  | | `Gtk2::VRuler
  | +Gtk2::Range
  | | +Gtk2::Scale
  | | | +Gtk2::HScale
  | | | `Gtk2::VScale
  | | `Gtk2::Scrollbar
  | |   +Gtk2::HScrollbar
  | |   `Gtk2::VScrollbar
  | +Gtk2::Separator
  | | +Gtk2::HSeparator
  | | `Gtk2::VSeparator
  | +Gtk2::Invisible
  | +Gtk2::Preview
  | `Gtk2::ProgressBar
  +Gtk2::Adjustment
  +Gtk2::CellRenderer
  | +Gtk2::CellRendererPixbuf
  | +Gtk2::CellRendererText
  | +Gtk2::CellRendererToggle
  +Gtk2::ItemFactory
  +Gtk2::Tooltips
  `Gtk2::TreeViewColumn

Since GTK2-Perl offers an object oriented interface to the underlying C libraries, we can take advantage of this, and use methods of generic classes on instances of classes derived by them. For instance, the Gtk2::Window class inherits its packaging methods from the Gtk2::Bin class, which, in turn, inherits methods from its parent class, Gtk2::Container. All these classes are widgets, so they inherit methods from the Gtk2::Widget class. Above all, each class inherits from Glib::Object, which is used, for instance, when handling signals and events.