Chapter 9. Combo Boxes

## Chapter 9. Combo Boxes

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

For both widgets, the list is provided via a `TreeModel`, and columns from this model are added to the ComboBox's view with the `ComboBox::pack_start()`. This provides a great deal of flexibility and compile-time type-safety, but the `ComboBoxText` and `ComboBoxEntryText` classes provide a simple text-based specialisation in case that flexibility is not required.

## ComboBox

Reference

### 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 columns, like so:

    ModelColumns()
    { 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 methods 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.

### The chosen item

To discover what item, if any, the user has chosen from the ComboBox, call `ComboBox::get_active()`. This returns a `TreeModel::iterator` that you can dereference to a `Row` in order to read the values in your columns. For instance, you might read an integer ID value from the model, even though you have chosen only to show the human-readable description in the Combo. For instance:

    Gtk::TreeModel::iterator iter = m_Combo.get_active();
    if(iter)
    {
      Gtk::TreeModel::Row row = *iter;

      //Get the data for the selected row, using our knowledge
      //of the tree model:
      int id = row[m_Columns.m_col_id];
      set_something_id_chosen(id); //Your own function.
    }
    else
      set_nothing_chosen(); //Your own function.

### Responding to changes

You might need to react to every change of selection in the ComboBox, for instance to update other widgets. To do so, you should handle the "changed" signal. For instance:

    m_combo.signal_changed().connect( sigc::mem_fun(*this,
          &ExampleWindow::on_combo_changed) );

### Full Example

Figure 9.1. ComboBox

Source Code

File: `examplewindow.h`

    #ifndef GTKMM_EXAMPLEWINDOW_H
    #define GTKMM_EXAMPLEWINDOW_H

    #include <gtkmm/window.h>
    #include <gtkmm/comboboxtext.h>
    #include <gtkmm/liststore.h>

    class ExampleWindow : public Gtk::Window
    {
    public:
      ExampleWindow();
      virtual ~ExampleWindow();

    protected:
      //Signal handlers:
      virtual void on_combo_changed();

      //Tree model columns:
      class ModelColumns : public Gtk::TreeModel::ColumnRecord
      {
      public:

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

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

      ModelColumns m_Columns;

      //Child widgets:
      Gtk::ComboBox m_Combo;
      Glib::RefPtr<Gtk::ListStore> m_refTreeModel;
    };

    #endif //GTKMM_EXAMPLEWINDOW_H

File: `examplewindow.cc`

    #include "examplewindow.h"
    #include <gtkmm/stock.h>
    #include <iostream>

    ExampleWindow::ExampleWindow()
    {
      set_title("ComboBox example");

      //Create the Tree model:
      //m_refTreeModel = Gtk::TreeStore::create(m_Columns);
      m_refTreeModel = Gtk::ListStore::create(m_Columns);
      m_Combo.set_model(m_refTreeModel);

      //Fill the ComboBox's Tree Model:
      Gtk::TreeModel::Row row = *(m_refTreeModel->append());
      row[m_Columns.m_col_id] = 1;
      row[m_Columns.m_col_name] = "Billy Bob";
      /*
      Gtk::TreeModel::Row childrow = *(m_refTreeModel->append(row.children()));
      childrow[m_Columns.m_col_id] = 11;
      childrow[m_Columns.m_col_name] = "Billy Bob Junior";

      childrow = *(m_refTreeModel->append(row.children()));
      childrow[m_Columns.m_col_id] = 12;
      childrow[m_Columns.m_col_name] = "Sue Bob";
      */

      row = *(m_refTreeModel->append());
      row[m_Columns.m_col_id] = 2;
      row[m_Columns.m_col_name] = "Joey Jojo";

      row = *(m_refTreeModel->append());
      row[m_Columns.m_col_id] = 3;
      row[m_Columns.m_col_name] = "Rob McRoberts";

      /*
      childrow = *(m_refTreeModel->append(row.children()));
      childrow[m_Columns.m_col_id] = 31;
      childrow[m_Columns.m_col_name] = "Xavier McRoberts";
      */

      //Add the model columns to the Combo (which is a kind of view),
      //rendering them in the default way:
      m_Combo.pack_start(m_Columns.m_col_id);
      m_Combo.pack_start(m_Columns.m_col_name);

      //Add the ComboBox to the window.
      add(m_Combo);

      //Connect signal handler:
      m_Combo.signal_changed().connect( sigc::mem_fun(*this, &ExampleWindow::on_combo_changed) );

      show_all_children();
    }

    ExampleWindow::~ExampleWindow()
    {
    }

    void ExampleWindow::on_combo_changed()
    {
      Gtk::TreeModel::iterator iter = m_Combo.get_active();
      if(iter)
      {
        Gtk::TreeModel::Row row = *iter;
        if(row)
        {
          //Get the data for the selected row, using our knowledge of the tree
          //model:
          int id = row[m_Columns.m_col_id];
          Glib::ustring name = row[m_Columns.m_col_name];

          std::cout << " ID=" << id << ", name=" << name << std::endl;
        }
      }
      else
        std::cout << "invalid iter" << std::endl;
    }

File: `main.cc`

    #include <gtkmm/main.h>
    #include "examplewindow.h"

    int main(int argc, char *argv[])
    {
      Gtk::Main kit(argc, argv);

      ExampleWindow window;
      //Shows the window and returns when it is closed.
      Gtk::Main::run(window);

      return 0;
    }

### Simple Text Example

Figure 9.2. ComboBox

Source Code

File: `examplewindow.h`

    #ifndef GTKMM_EXAMPLEWINDOW_H
    #define GTKMM_EXAMPLEWINDOW_H

    #include <gtkmm/window.h>
    #include <gtkmm/comboboxtext.h>

    class ExampleWindow : public Gtk::Window
    {
    public:
      ExampleWindow();
      virtual ~ExampleWindow();

    protected:
      //Signal handlers:
      virtual void on_combo_changed();

      //Child widgets:
      Gtk::ComboBoxText m_Combo;
    };

    #endif //GTKMM_EXAMPLEWINDOW_H

File: `examplewindow.cc`

    #include "examplewindow.h"
    #include <gtkmm/stock.h>
    #include <iostream>

    ExampleWindow::ExampleWindow()
    {
      set_title("ComboBoxText example");

      //Fill the combo:
      m_Combo.append_text("something");
      m_Combo.append_text("something else");
      m_Combo.append_text("something or other");

      add(m_Combo);

      //Connect signal handler:
      m_Combo.signal_changed().connect(sigc::mem_fun(*this,
                  &ExampleWindow::on_combo_changed) );

      show_all_children();
    }

    ExampleWindow::~ExampleWindow()
    {
    }

    void ExampleWindow::on_combo_changed()
    {
      Glib::ustring text = m_Combo.get_active_text();
      if(!(text.empty()))
        std::cout << "Combo changed: " << text << std::endl;
    }

File: `main.cc`

    #include <gtkmm/main.h>
    #include "examplewindow.h"

    int main(int argc, char *argv[])
    {
      Gtk::Main kit(argc, argv);

      ExampleWindow window;
      //Shows the window and returns when it is closed.
      Gtk::Main::run(window);

      return 0;
    }

Examples |  | ComboBoxEntry