PHP-GTK2 Newsletter

PHP-GTK2 Tips & Techniques
FREE Newsletter
by kksou



Sample Code 24: How to setup and process GtkComboBox?
Written by kksou   
Monday, 18 September 2006
Problem

You want to set up a pulldown menu as shown below:

How to setup and process GtkComboBox?


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   
38   
39   
40   
41   
42   
<?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 GtkComboBox");
$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 combobox
$combobox = new GtkComboBox();

// Create a model
if (defined("GObject::TYPE_STRING")) {
    $model = new GtkListStore(GObject::TYPE_STRING);
} else {
    $model = new GtkListStore(Gtk::TYPE_STRING);
}

// Set up the combobox
$combobox->set_model($model); // note 1
$cellRenderer = new GtkCellRendererText(); // note 2
$combobox->pack_start($cellRenderer);
$combobox->set_attributes($cellRenderer, 'text', 0); // note 3

// 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
  • 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. Binds the model to the combobox.
  2. The GtkCellrenderertext is just a class used to display the cell contents as text.
  3. The '0' here refers to the first column of the data model $model.
  4. Note that we pass $combobox along so that we can have access to this when the user clicks the submit button.
  5. Retrieves the $model from $combobox with $combobox->get_model()
  6. The selection is read using GtkTreemodel::get_value()(GtkTreeIter iter, int column). Note that we can get the selected iter with GtkCombobox::get_active()

Notes

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


Related Links

User reviews   Average user ratings:    4.0   (from 3 users)
  1. yacatl from Mexico
    August 21, 2007 10:35am

    Hi kksou. Not only this but all your works has been extremely useful to me. However I have one question. ¿What if I need a classic html combo value-text to show? I mean, I want to to show for example a month combo, showing the text mont January, February, March but when I retrieve de data value for this combo I need the numeric value 01,02,03 . Also I want to to set an active item, I have a years combo array(2007=>"2007",2008=>"2008") and I wish to choose somthig like $combobox->set_active(date("Y"));

    How can I do that?

  2. kksou
    August 26, 2007 9:49am

    Please refer to the following two articles:

    How to setup GtkComboBox with label value pair - Part 1?

    How to setup GtkComboBox with label value pair - Part 2 - set active?

  3. Wilhelm
    April 30, 2008 7:50am

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