PHP-GTK2 Newsletter

PHP-GTK2 Tips & Techniques
FREE Newsletter
by kksou



Sample Code 372: How to display options of GtkComboBox in a grid without use of set_column_span_column?
Written by kksou   
Wednesday, 21 November 2007
Problem

This article supplements the article How to display options of GtkComboBox in a grid with use of set_column_span_column?

This example shows how the combobox will look like if we did not use the method GtkCombobox::set_column_span_column() as shown below. Not too neat, isn't it?

How to display options of GtkComboBox in a grid without use of set_column_span_column?


Solution

Please refer to How to display options of GtkComboBox in a grid with use of set_column_span_column?


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   
44   
45   
46   
47   
48   
49   
50   
51   
52   
53   
54   
55   
56   
<?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("Display options of GtkComboBox in a grid\n".
"without use of set_column_span_column()");
$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);

$list = array('1', '2', '3', '4', '5', '6', // note 1
'7', '8', '9', '10', '11', '12', 
'ABCDEFG', 'HIJKLMNOPQR', 
'1001', '1002', '1003', 
);

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

// setup the combobox
$combobox = new GtkComboBox();
if (defined("GObject::TYPE_STRING")) {
    $model = new GtkListStore(GObject::TYPE_STRING); // note 2
} else {
    $model = new GtkListStore(Gtk::TYPE_STRING); // note 2
}
$cellRenderer = new GtkCellRendererText();
$combobox->pack_start($cellRenderer);
$combobox->set_attributes($cellRenderer, 'text', 0);

// populate the model
foreach($list as $choice) {
    $model->append(array($choice)); // note 3
}

// Set up the combobox
$combobox->set_model($model);
//$combobox->set_column_span_column(1);
$combobox->set_wrap_width(6); // note 4
$combobox->connect('changed', 'on_change');
$hbox->pack_start($combobox, 0, 0);
$window->show_all();
Gtk::main();

function on_change($combobox) {
    $model = $combobox->get_model();
    $selection = $model->get_value($combobox->get_active_iter(), 0);
    echo "You have selected: $selection!\n";
}

?>
Explanation

We make use of the code from How to setup and process GtkComboBox? to set up the combobox.

What's new here:

  1. These are the options that will appear in the combobox.
  2. We use one column here to store the option.
  3. Populate the options.
  4. Sets the number of columns in a row to be 6.

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-2009. kksou.com. All Rights Reserved