Combo
Prev | Chapter 6. Miscellaneous Widgets | Next

## Combo

A `Combo` is a combination of a text entry and a popup menu. Clicking on one of the menu entries puts it in the entry box. The entry box otherwise works just like a regular `Entry` widget.

A `Gtk::Combo` contains a `Gtk::Entry` widget, which is used to implement the entry box. You can obtain the `Gtk::Entry` using `get_entry()` method.

To set the values in the popup menu, use

    void Gtk::Combo::set_popdown_strings(const Gtk::SArray& strings);

where `strings` is a list of the strings you want to appear in the list. As mentioned in the Basics section, `Gtk::SArray` is a converter object which can take any kind of STL vector container. This means that you can pass vectors or lists to this method, and things will work as you expect. For example, the following is legal:

    list<string> gl;

    gl.push_back("String 1");
    gl.push_back("String 2");
    gl.push_back("String 3");
    gl.push_back("String 4");

    combo.set_popdown_strings(gl);

TODO: STL-style access.

Reference

### Example

Figure 6.3. Combo

Source Code

File: examplewindow.h

    #ifndef GTKMM_EXAMPLEWINDOW_H
    #define GTKMM_EXAMPLEWINDOW_H

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

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

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

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

    #endif //GTKMM_EXAMPLEWINDOW_H

File: examplewindow.cc

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

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

      //Fill the combo:
      std::list<Glib::ustring> listStrings;
      listStrings.push_back("something");
      listStrings.push_back("something else");
      listStrings.push_back("something or other");
      m_Combo.set_popdown_strings(listStrings);

      //Create a mixed entry an add it to the combo's list using the advanced interface ComboDropDown:
      Gtk::ComboDropDownItem* item = Gtk::manage(new Gtk::ComboDropDownItem);

      Gtk::HBox* hbox = Gtk::manage(new Gtk::HBox(false, 3));
      hbox->pack_start(*Gtk::manage(new Gtk::Image(Gtk::Stock::CLEAR, Gtk::ICON_SIZE_MENU)), Gtk::PACK_SHRINK);
      hbox->pack_start(*Gtk::manage(new Gtk::Label("some image - cool!")), Gtk::PACK_SHRINK);

      item->add(*hbox);
      item->show_all();
      m_Combo.get_list()->children().push_back(*item);
      m_Combo.set_item_string(*item, "you selected the image!");

      //Restrict it to these choices only:
      m_Combo.set_value_in_list();

      add(m_Combo);

      //Connect signal handler:
      m_Combo.get_entry()->signal_changed().connect( SigC::slot(*this, &ExampleWindow::on_combo_changed) );

      show_all_children();
    }

    ExampleWindow::~ExampleWindow()
    {
    }

    void ExampleWindow::on_combo_changed()
    {
      Gtk::Entry* pEntry = m_Combo.get_entry();
      if(pEntry)
      {
        Glib::ustring text = pEntry->get_text();
        if(!(text.empty())) //We seem to get 2 signals, one when the text is 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;
      Gtk::Main::run(window); //Shows the window and returns when it is closed.

      return 0;
    }

Prev | Up | Next
Entry | Home | SpinButton