Sample Code 25: How to setup and process GtkComboBoxEntry - Part 1?
Written by kksou   
Tuesday, 19 September 2006
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   
30   
31   
<?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);
  • Note that this is only 70% of the sample code. You have to be a registered member to see the entire sample code. Please login or register.
  • Registration is free and immediate.
  • Have some doubt about the registration? Please read this forum article.
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

User reviews   Average user ratings:    5.0   (from 1 user)
  1. marcos
    April 04, 2008 5:53pm

Note: You have to be a registered member to leave a comment. Free registration here.

 
< Prev   Next >

Copyright © 2006-2008. kksou.com. All Rights Reserved