373. How to display options of GtkComboBox in a grid with use of set_row_span_column?

Problem

I've showed you the use of set_column_span_column() in the article How to display options of GtkComboBox in a grid with use of set_column_span_column?

As it occurs, whoever designed this knew that you're definitely going to ask for the row-span too.

So this example illustrates the use of set_row_span_column() as shown below:

How to display options of GtkComboBox in a grid with use of set_row_span_column?


Solution

  • In the liststore used by the combobox, create one more column to store the number of rows each item should span. If an item doesn't span across other columns, then you have to put a '1'.
  • Use the method GtkComboBox::set_row_span_column() to inform php-gtk2 which column it is that stores the row-span information. Note: this method exists even in the alpha version!

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   
45   
46   
47   
48   
49   
51   
52   
53   
54   
55   
56   
57   
58   
59   
60   
61   
62   
63   
64   
<?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".
"       with use of set_row_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("A\nB\nC\nD", '1', '2', '3', '4', '5', '6', // note 1
"E\nF\nG\nH",
'7', '8', '9', '10', '11', '12', 
'13', '14', '15', '16', '17', '18', 
'19', '20', '21', '22', '23', '24', 
);

$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, GObject::TYPE_LONG); // note 2
} else {
    $model = new GtkListStore(Gtk::TYPE_STRING, Gtk::TYPE_LONG); // note 2
}
$cellRenderer = new GtkCellRendererText();
$combobox->pack_start($cellRenderer);
$combobox->set_attributes($cellRenderer, 'text', 0);

// populate the model, setting the size of the column span at the same time
$i=0;
foreach($list as $choice) {
    if ($i==0 || $i==7) {
        $model->append(array($choice, 3)); // note 3
    } else {
        $model->append(array($choice, 1)); // note 4
    }
    ++$i;
}

// Set up the combobox
$combobox->set_model($model);
$combobox->set_row_span_column(1); // note 5
$combobox->set_wrap_width(8); // note 6
$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 first column (column 0) to store the option, and the second column (column 1) to store the row-span information.
  3. Span the items 'ABCD' and 'EFGH' across four rows.
  4. No spanning for all the other items. So let's just put a 1.
  5. Inform php-gtk that column 1 contains the row-spanning information.
  6. Don't forget this! 8 means there will be eight columns in a row.

Note

Do try out the sample code in How to display options of GtkComboBox in a grid without use of set_row_span_column? too.

It will allow you to understand better the use of the method GtkCombobox::set_row_span_column().

Related Links

Add comment


Security code
Refresh