025. How to setup and process GtkComboBoxEntry - Part 1?

Problem

You want to set up an editable pulldown menu (called GtkComboBoxEntry in PHP-GTK2) as shown below. Note that the main difference between a GtkComboBox and GtkComboBoxEntry is that GtkComboBoxEntry allow users to enter values that not present in the selection list. The user can choose and modify existing value, or enter a completely new one.

How to setup and process GtkComboBoxEntry - Part 1?


Solution

  • An editable pulldown menu is created in php-gtk2 using GtkComboBoxEntry
  • Although the name is longer, setting this up is surprisingly much simpler than setting up a GtkComboBox
  • Unlike GtkComboBox, the selection of GtkComboBoxEntry is read more easily like a standard GtkEntry with get_text

Sample Code

1   
2   
3   
4   
5   
6   
7   
8   
9   
10   
11   
12   
13   
14   
15   
16   
17   
18   
19   
20   
21   
22   
23   
24   
25   
26   
27   
28   
29   
31   
32   
33   
36   
37   
38   
39   
40   
41   
42   
43   
44   
45   
46   
47   
48   
<?php
$window = new GtkWindow();
$window->connect_simple('destroy', array( 'Gtk', 'main_quit'));
$window->set_size_request(400,150);

$window->add($vbox = new GtkVBox());

// display title
$title = new GtkLabel("Setup and read value from ComboBoxEntry");
$title->modify_font(new PangoFontDescription("Times New Roman Italic 10"));
$title->modify_fg(Gtk::STATE_NORMAL, GdkColor::parse("#0000ff"));
$title->set_size_request(-1, 60);
$vbox->pack_start($title, 0, 0);

// the selection
$list = array('item 1', 'item 2', 'item 3', 'item 4');

$vbox->pack_start($hbox=new GtkHBox(), 0, 0);
$hbox->pack_start(new GtkLabel('Select: '), 0, 0);

// Create a new comboboxentry and populates it
$combobox = &GtkComboBoxEntry::new_text();
foreach($list as $choice) {
    $combobox->append_text($choice);
}
$combobox->get_child()->set_text(''); // note 1
$hbox->pack_start($combobox, 0, 0);

// Set up the submit butotn
$hbox->pack_start($button = new GtkLabel('  '), 0, 0);
$hbox->pack_start($button = new GtkButton('Submit'), 0, 0);
$button->set_size_request(60, 24);

// Set up the event handler to respond to button click
$button->connect('clicked', "on_button", $combobox); //note 2

// The callback function that is called when user clicks the submit button
function on_button($button, $combobox) {
    $selection = $combobox->get_child()->get_text(); // note 3
    print "You have selected: $selection\n";
}

$window->show_all();
Gtk::main();
?>

Output

As shown above.
 

Explanation

  1. This sets the default text of the comboboxentry. Here it's set to ''. You can of course set it to one of the selections, e.g. 'Item 1'.
  2. Note that we pass $combobox along so that we can have access to this when the user clicks the submit button.
  3. Note that $combobox is a GtkEntry by nature. So we read its value using get_text
  4. $combobox->get_child()->get_text();
    

Notes

You may want to take a look at How to setup and process GtkComboBox? to compare the difference between GtkComboBox and GtkComboBoxEntry.

Related Links

Add comment


Security code
Refresh