376. How to display options of GtkComboBox in a grid without use of set_column_span_column and set_row_span_column?

Problem

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

This example shows how the combobox will look like if we did not use the method GtkCombobox::set_column_span_column() and GtkCombobox::set_row_span_column() as shown below.

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


Solution

Please refer to How to display options of GtkComboBox in a grid with use of set_column_span_column and set_row_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   
43   
46   
47   
48   
49   
50   
51   
52   
53   
54   
55   
56   
57   
58   
<?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 both set_column_span_column()\n".
"          and 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, 80);
$vbox->pack_start($title, 0, 0);

$list = array("W\nX\nY\nZ", '1', '2', '3', '4', '5', '6', // note 1
'7', '8', '9', '10', '11',  
'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_wrap_width(7); // 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";
}

?>

Output

As shown above.
 

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 7.

Related Links

Add comment


Security code
Refresh