PHP-GTK2 Newsletter

PHP-GTK2 Tips & Techniques
FREE Newsletter
by kksou



Sample Code 309: How to setup GtkComboBox with label value pair - Part 2 - set active?
Written by kksou   
Sunday, 26 August 2007
Problem

This is in response to the note from yacatl from Mexico.

In Part 1, we have set up a month combo showing the text months January, February, March, etc. When the user selects say March, we return '03' instead of 'March'.

In addition to the above, he would like to set the year and month combobox to the current year and month as shown below:

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


Solution
  • PHP-GTK2 provides two methods to set the value of a combobox.
  • The first one is GtkComboBox::set_active_iter($iter). You need to provide the $iter that points to the iter of the option that you want to select.
  • The second one is GtkComboBox::set_active($index). For this one, you need to provide the index of the option that you want to select (with the first option as index 0).
  • There is no method GtkComboBox::set_active($selected_str). However, as you can see from the sample code below, it is easy to simulate one.

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   
43   
44   
45   
46   
47   
48   
49   
50   
51   
52   
53   
54   
55   
56   
57   
58   
59   
60   
61   
62   
63   
64   
65   
66   
67   
68   
69   
70   
71   
72   
73   
<?php
$window = new GtkWindow();
$window->connect_simple('destroy', array( 'Gtk', 'main_quit'));
$window->set_size_request(400, 160);
$window->add($vbox = new GtkVBox());

// display title
$title = new GtkLabel("Set up GtkComboBox ".
"with label-value pair\n".
"                     Part 2 - set active");
$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 month label-value-pair
$month_options = array( // note 1
'Januaary' => '01',
'February' => '02',
'March' => '03',
'April' => '04',
'May' => '05',
'June' => '06',
'July' => '07',
'August' => '08',
'September' => '09',
'October' => '10',
'November' => '11',
'December' => '12',
);

// the year label-value-pair
$year_options = array( // note 2
'2007' => '2007',
'2008' => '2008',
'2009' => '2009',
);

$year_combo = setup_combobox('Year', $year_options, $vbox);
$month_combo = setup_combobox('Month', $month_options, $vbox);

set_current_year_month(); // note 3

// setup submit button
$vbox->pack_start($hbox=new GtkHBox(), 0, 0);
$hbox->pack_start($button = new GtkButton('Submit'), 0);
$button->set_size_request(60, 24);
$button->connect('clicked', "on_button");

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

function setup_combobox($combo_label, $options, $vbox) {
    $vbox->pack_start($hbox=new GtkHBox(), 0);
    $hbox->pack_start(new GtkLabel("$combo_label: "), 0);

    // Create a combobox
    $combobox = GtkComboBox::new_text();

    // populates the options
    foreach($options as $label=>$value) {
        $combobox->append_text($label);
    }

    // Set up a hbox to contain the combobox as well as the Submit button
    $hbox->pack_start($combobox, 0, 0);
    return $combobox;
}

// set the current year and month
function set_current_year_month() {
    global $year_combo, $month_combo;
    global $year_options, $month_options;
  • 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 Part 1

What's new here:

  1. The months option in label-value pair. The left side is the label to be displayed. The right side is the corresponding value for each month.
  2. The years option in label-value pair.
  3. Set the current year and month.
  4. Get the current year and month.
  5. The variable $i keeps track of the index of the combobox.
  6. Loop through each label-value pair of the combobox.
  7. Check if the label is the same as the current value.
  8. Yes, select this!
  9. Get the labels of the selected year and month.
  10. Get the values corresponding year and month.

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.

 
< Prev   Next >

Blog - Forum - Privacy Policy - Contact Us
Copyright © 2006-2008. kksou.com. All Rights Reserved