### Tutorial
« Spin Buttons | Calendar »

## Combo Box

The combo box is another fairly simple widget that is really just a collection of other widgets. From the user's point of view, the widget consists of a text entry box and a pull down menu from which the user can select one of a set of predefined entries. Alternatively, the user can type a different option directly into the text box.
The components of the combo box can be accessed directly using one of the following Gtk::Combo accessors:

`Gtk::Entry* entry() const;` `Gtk::Button* button() const; Gtk::ScrolledWindow* popup() const; Gtk::Window* popwin() const; Gtk::Widget* list() const;`

As you can see, the Combo Box has two principal parts that you really care about: an entry and a list. The list is returned as a Widget pointer as the CList widget is deprecated and not wrapped in Inti.
There is only one constructor that creates a new combo box:

`Combo();`

Now, if you want to set the string in the entry section of the combo box, this is done by manipulating the entry widget directly:

`combo->entry()->set_text("My String.");`

To set the values in the popdown list use the method:

`void set_popdown_strings(const std::vector<String>& strings);`

`
` Before you can do this, you have to assemble a std::vector of the strings that you want. The easiest way to do this is:

`std::vector<String> strings; strings.push_back("String 1"); strings.push_back("String 2"); strings.push_back("String 3"); strings.push_back("String 3");` `set_popdown_strings(strings);`

The combo widget makes a copy of the strings passed to it in the vector so you don't need to store the vector strings unless that is appropriate for your application.
At this point you have a working combo box that has been set up. There are a few aspects of its behavior that you can change. These are accomplished with the methods:

`void set_use_arrows(bool use_arrows); void set_use_arrows_always(bool use_arrows_always); void set_case_sensitive(bool case_sensitive);`

Gtk::Combo::set_use_arrows() lets the user change the value in the entry using the up/down arrow keys. This doesn't bring up the list, but rather replaces the current text in the entry with the next list entry (up or down, as your key choice indicates). It does this by searching in the list for the item corresponding to the current value in the entry and selecting the previous/next item accordingly. Usually in an entry the arrow keys are used to change focus (you can do that anyway using TAB). Note that when the current item is the last of the list and you press arrow-down it changes the focus (the same applies with the first item and arrow-up).
If the current value in the entry is not in the list, then the method of Gtk::Combo::set_use_arrows() is disabled.
Gtk::Combo::set_use_arrows_always() similarly allows the use the the up/down arrow keys to cycle through the choices in the dropdown list, except that it wraps around the values in the list, completely disabling the use of the up and down arrow keys for changing focus.
Gtk::Combo::set_case_sensitive() toggles whether or not Gtk::Combo searches for entries in a case sensitive manner. This is used when the Combo widget is asked to find a value from the list using the current entry in the text box. This completion can be performed in either a case sensitive or insensitive manner, depending upon the use of this method. The Combo widget can also simply complete the current entry if the user presses the key combination MOD-1 and "Tab". MOD-1 is often mapped to the "Alt" key, by the xmodmap utility. Note, however that some window managers also use this key combination, which will override its use within Inti.
Now that we have a combo box, tailored to look and act how we want it, all that remains is being able to get data from the combo box. This is relatively straight forward. The majority of the time, all you are going to care about getting data from is the entry. The entry is accessed directly using the entry() accessor. The two principal things that you are going to want to do with it are connect to the activate signal, which indicates that the user has pressed the Return or Enter key, and read the text. The first is accomplished using something like:

`entry()->sig_activate().connect(slot(this, &MyClass::my_signal_handler));`

Getting the text at any arbitrary time is accomplished by simply using the Gtk::Entry method:

`String get_text() const;`

Such as:

`String text = combo->entry()->get_text();`

That's about all there is to it. There is a method:
`
`

`void disable_activate();`

that will disable the activate signal on the entry widget in the combo box. Personally, I can't think of why you'd want to use it, but it does exist.

« Spin Buttons | Index Miscellaneous Widgets Top | Calendar »