PHP-GTK2 Newsletter
PHP-GTK2 Tips & Techniques
FREE Newsletter
by kksou
Sample Code 296: How to setup and process GtkComboBoxEntry - Part 2 - using data view model? |
|
Written by kksou
|
|
Saturday, 28 July 2007 |
|
Problem For more advanced stuff using GtkCombobox and GtkComboBoxEntry, you will need access to its underlying data model.
This example produces the same result as How to setup and process GtkComboBoxEntry - Part 1?
The only difference is that it uses the data-view model as shown below:

Solution
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 32 33 34 35 36 37
| <?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 process ComboBoxEntry\n". " Part 2 - using data-view model"); $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 the data model
if (defined("GObject::TYPE_STRING")) { $model = new GtkListStore(GObject::TYPE_STRING); // note 1
} else { $model = new GtkListStore(Gtk::TYPE_STRING); // note 1
}
// Create the comboboxentry
$combobox = &GtkComboBoxEntry::new_with_model($model, 0); // note 2
// Stuff the choices in the model
foreach($list as $choice) { $model->append(array($choice)); }
// Set up a hbox to contain the combobox as well as the Submit button
$hbox->pack_start($combobox, 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 We make use of the code from How to setup and process GtkComboBox?
What's new here:
- Creates the data model.
- Creates the GtkComboBoxEntry, binds the data model to the GtkComboBoxEntry, and informs php-gtk2 to use the first column (col 0).
- Get the selection.
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 There are no user reviews yet. Note: You have to be a registered member to leave a comment. Free registration here. |
|