Chapter 12. The DropDown Widget

Table of Contents

The DropDown widget is an alternative to the deprecated ComboBox. It uses list models instead of tree models, and the content is displayed using widgets instead of cell renderers.

The DropDown widget offers a list of choices in a dropdown menu. If appropriate, it can show extra information about each item, such as text, a picture, or a check button. The DropDown widget can optionally have an Entry in the dropdown menu, allowing the user to search in a long list.

The list is provided via a Gio::ListModel, and data from this model is added to the DropDown's view with signal handlers connected to a SignalListItemFactory. This provides flexibility, but the StringList class provides a simpler text-based specialization in case that flexibility is not required.

Reference

The model

The model for a DropDown can be defined and filled exactly as for a ListView or a ColumnView. It must be a subclass of Glib::Object. For instance, you might have a DropDown with one integer and one text column, like so:

class ModelColumns : public Glib::Object
{
public:
  int m_col_id;
  Glib::ustring m_col_name;

  static Glib::RefPtr<ModelColumns> create(
    int col_id, const Glib::ustring& col_name)
  {
    return Glib::make_refptr_for_instance<ModelColumns>(
      new ModelColumns(col_id, col_name));
  }

protected:
  ModelColumns(int col_id, const Glib::ustring& col_name)
  : m_col_id(col_id), m_col_name(col_name)
  {}
};

Glib::RefPtr<Gio::ListStore<ModelColumns>> m_ListStore;

After appending rows to this model, you should provide the model to the DropDown with the set_model() method. Unless you use the StringList model, you also need to set a ListItemFactory with set_factory(). If you want the items in the dropdown menu to look different from the item in the DropDown widget, you also need to set a separate ListItemFactory with set_list_factory().