Chapter 10. Combo Boxes

Table of Contents

The ComboBox widget offers a list (or tree) of choices in a dropdown menu. If appropriate, it can show extra information about each item, such as text, a picture, a checkbox, or a progress bar. The ComboBox widget usually restricts the user to the available choices, but it can optionally have an Entry, allowing the user to enter arbitrary text if none of the available choices are suitable.

The list is provided via a TreeModel, and columns from this model are added to the ComboBox's view with the ComboBox::pack_start() method. This provides flexibility and compile-time type-safety, but the ComboBoxText class provides a simpler text-based specialization in case that flexibility is not required.


The model

The model for a ComboBox can be defined and filled exactly as for a TreeView. For instance, you might derive a ComboBox class with one integer and one text column, like so:

{ add(m_col_id); add(m_col_name); }

  Gtk::TreeModelColumn<int> m_col_id;
  Gtk::TreeModelColumn<Glib::ustring> m_col_name;

ModelColumns m_columns;

After appending rows to this model, you should provide the model to the ComboBox with the set_model() method. Then use the pack_start() or pack_end() methods to specify what columns will be displayed in the ComboBox. As with the TreeView you may either use the default cell renderer by passing the TreeModelColumn to the pack methods, or you may instantiate a specific CellRenderer and specify a particular mapping with either add_attribute() or set_cell_data_func(). Note that these methods are in the CellLayout base class.