<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Chapter 9. Combo Boxes</title> <link rel="stylesheet" href="style.css" type="text/css"> <meta name="generator" content="DocBook XSL Stylesheets V1.75.1"> <link rel="home" href="index.html" title="Programming with gtkmm"> <link rel="up" href="index.html" title="Programming with gtkmm"> <link rel="prev" href="sec-treeview-examples.html" title="Examples"> <link rel="next" href="sec-comboboxentry.html" title="ComboBoxEntry"> </head> <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> <div class="navheader"> <table width="100%" summary="Navigation header"> <tr><th colspan="3" align="center">Chapter 9. Combo Boxes</th></tr> <tr> <td width="20%" align="left"> <a accesskey="p" href="sec-treeview-examples.html"><img src="icons/prev.png" alt="Prev"></a> </td> <th width="60%" align="center"> </th> <td width="20%" align="right"> <a accesskey="n" href="sec-comboboxentry.html"><img src="icons/next.png" alt="Next"></a> </td> </tr> </table> <hr> </div> <div class="chapter" title="Chapter 9. Combo Boxes"> <div class="titlepage"><div><div><h2 class="title"> <a name="chapter-combobox"></a>Chapter 9. Combo Boxes</h2></div></div></div> <div class="toc"> <p><b>Table of Contents</b></p> <ul> <li><span class="sect1"><a href="chapter-combobox.html#sec-combobox">ComboBox</a></span></li> <li><span class="sect1"><a href="sec-comboboxentry.html">ComboBoxEntry</a></span></li> </ul> </div> <p>The <code class="classname">ComboBox</code> and <code class="classname">ComboBoxEntry</code> 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 <code class="classname">ComboBox</code> widget restricts the user to the available choices, but the <code class="classname">ComboBoxEntry</code> contains an <code class="classname">Entry</code>, allowing the user to enter arbitrary text if the none of the available choices are suitable. </p> <p>For both widgets, the list is provided via a <code class="classname">TreeModel</code>, and columns from this model are added to the ComboBox's view with the <code class="methodname">ComboBox::pack_start()</code>. This provides a great deal of flexibility and compile-time type-safety, but the <code class="classname">ComboBoxText</code> and <code class="classname">ComboBoxEntryText</code> classes provide a simple text-based specialisation in case that flexibility is not required. </p> <div class="sect1" title="ComboBox"> <div class="titlepage"><div><div><h2 class="title" style="clear: both"> <a name="sec-combobox"></a>ComboBox</h2></div></div></div> <p><a class="ulink" href="http://library.gnome.org/devel/gtkmm/unstable/classGtk_1_1ComboBox.html" target="_top">Reference</a></p> <div class="sect2" title="The model"> <div class="titlepage"><div><div><h3 class="title"> <a name="sec-combobox-model"></a>The model</h3></div></div></div> <p>The model for a ComboBox can be defined and filled exactly as for a <code class="classname">TreeView</code>. For instance, you might derive a ComboBox class with one integer and one text columns, like so: </p> <pre class="programlisting">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;</pre> <p>After appending rows to this model, you should provide the model to the <code class="classname">ComboBox</code> with the <code class="methodname">set_model()</code> method. Then use the <code class="methodname">pack_start()</code> or <code class="methodname">pack_end()</code> 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 <code class="classname">TreeModelColumn</code> to the pack methods, or you may instantiate a specific <code class="classname">CellRenderer</code> and specify a particular mapping with either <code class="methodname">add_attribute()</code> or <code class="methodname">set_cell_data_func()</code>. Note that these methods are in the <code class="classname">CellLayout</code> base class.</p> </div> <div class="sect2" title="The chosen item"> <div class="titlepage"><div><div><h3 class="title"> <a name="sec-combobox-get"></a>The chosen item</h3></div></div></div> <p>To discover what item, if any, the user has chosen from the ComboBox, call <code class="methodname">ComboBox::get_active()</code>. This returns a <code class="classname">TreeModel::iterator</code> that you can dereference to a <code class="classname">Row</code> 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: </p> <pre class="programlisting">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.</pre> </div> <div class="sect2" title="Responding to changes"> <div class="titlepage"><div><div><h3 class="title"> <a name="sec-combobox-changes"></a>Responding to changes</h3></div></div></div> <p> 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: </p> <pre class="programlisting">m_combo.signal_changed().connect( sigc::mem_fun(*this, &ExampleWindow::on_combo_changed) );</pre> </div> <div class="sect2" title="Full Example"> <div class="titlepage"><div><div><h3 class="title"> <a name="combobox-example-full"></a>Full Example</h3></div></div></div> <div class="figure"> <a name="figure-combobox-complex"></a><p class="title"><b>Figure 9.1. ComboBox</b></p> <div class="figure-contents"><div class="screenshot"><div><img src="figures/combobox_complex.png" alt="ComboBox"></div></div></div> </div> <br class="figure-break"><p><a class="ulink" href="http://git.gnome.org/cgit/gtkmm-documentation/tree/examples/book/combobox/complex" target="_top">Source Code</a></p> <p>File: <code class="filename">examplewindow.h</code> </p> <pre class="programlisting"> #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: 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 </pre> <p>File: <code class="filename">examplewindow.cc</code> </p> <pre class="programlisting"> #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; } </pre> <p>File: <code class="filename">main.cc</code> </p> <pre class="programlisting"> #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; } </pre> </div> <div class="sect2" title="Simple Text Example"> <div class="titlepage"><div><div><h3 class="title"> <a name="combobox-example-simple"></a>Simple Text Example</h3></div></div></div> <div class="figure"> <a name="figure-combobox-text"></a><p class="title"><b>Figure 9.2. ComboBox</b></p> <div class="figure-contents"><div class="screenshot"><div><img src="figures/combobox_text.png" alt="ComboBox"></div></div></div> </div> <br class="figure-break"><p><a class="ulink" href="http://git.gnome.org/cgit/gtkmm-documentation/tree/examples/book/combobox/text" target="_top">Source Code</a></p> <p>File: <code class="filename">examplewindow.h</code> </p> <pre class="programlisting"> #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: void on_combo_changed(); //Child widgets: Gtk::ComboBoxText m_Combo; }; #endif //GTKMM_EXAMPLEWINDOW_H </pre> <p>File: <code class="filename">examplewindow.cc</code> </p> <pre class="programlisting"> #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; } </pre> <p>File: <code class="filename">main.cc</code> </p> <pre class="programlisting"> #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; } </pre> </div> </div> </div> <div class="navfooter"> <hr> <table width="100%" summary="Navigation footer"> <tr> <td width="40%" align="left"> <a accesskey="p" href="sec-treeview-examples.html"><img src="icons/prev.png" alt="Prev"></a> </td> <td width="20%" align="center"> </td> <td width="40%" align="right"> <a accesskey="n" href="sec-comboboxentry.html"><img src="icons/next.png" alt="Next"></a> </td> </tr> <tr> <td width="40%" align="left" valign="top">Examples </td> <td width="20%" align="center"><a accesskey="h" href="index.html"><img src="icons/home.png" alt="Home"></a></td> <td width="40%" align="right" valign="top"> ComboBoxEntry</td> </tr> </table> </div> </body> </html>