Chapter 28. Combo Widgets

Inheritance Hierarchy

   +--- Widget
         +--- Container
               +--- Box
                     +--- HBox
                           +--- Combo

The combo box is 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 pulldown 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 following extracts from the structure that defines a Combo Box identifies several of the components:

          -> hbox
          -> entry
          -> button
          -> popup
          -> popwin
          -> list

As you can see, the Combo Box has two principal parts that you really care about: an entry and a list.

First off, to create a combo box, use:

$combo = new Gtk::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 function:

$combo->set_popdown_strings( @strings );

Before you can do this, you have to assemble a list of the strings that you want. Any Perl expression that can be evaluated as a list can be use as an argument. A comma seperated, double-quoted list of strings is most common. To append to this list, use:

$combo->list = ( $combo->list, "append1", "append2", ... );

As suggested above, anything that you can do to a Perl list (which is a lot), you can also do to the Combo List.

Here's a typical code segment for creating a set of options:

@list = ( "String1", "String2", "String3" );
$combo->set_popdown_strings( @list );

Or, if you prefer terse code:

$combo->set_popdown_strings( "String1", "String2", "String3" );

If you want to let the user change the value in the entry using the up/down arrow keys, use:

$combo->set_use_arrows( $use_arrows );

where $use_arrows is a true or false value. 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 function of set_use_arrows() is disabled.

If you want the up/down arrow keys to cycle through the choices in the dropdown list, except that it wraps around the values in the list, use:

$combo->set_use_arrows_always( $use_arrows );

This will completely disable the use of the up and down arrow keys for changing focus.

To determine whether GTK searches for entries in a case sensitive manner or not, use:

$combo->set_case_sensitive( $case );

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 function. 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 GTK.

If you wish to specify whether the value entered into the entry must be one of the items in the list, you can do so with:

$combo->set_value_in_list( $must_match, $ok_if_empty );

Where if $must_match is a true value, the value entered must be one of the values in the list, and $ok_if_empty is a true or false value that determines if an empty field is acceptable.

If an item in the list isn't a simple label, you might have to tell GTK what that label is. You would do so with:

$combo->set_item_string( $item, $string );

where $item is the item in the list you wish to set, and $string is the label. Without this, GTK might not know what to put the the entry field.

You can stop the Combo widget from showing the popup list when the entry emits an 'activate' signal using:


This could be useful if you want the Return key to close a dialog instead. In most cases, though, you should leave it alone.

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 straightforward. Most of the time, all you are going to care about getting data from is the entry. The entry is accessed simply by combo->entry . The two principal things that you are going to want to do with it are attach 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:

$combo->entry->signal_connect( "activate", \&callback );

Getting the text at any arbitrary time is accomplished by simply using the entry function: