375. How to display options of GtkComboBox in a grid with use of set_column_span_column and 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? and set_row_span_column() in the article How to display options of GtkComboBox in a grid with use of set_row_span_column?

Of course, you are free to combine both column span and row span in a combobox, as illustrated by this example below:

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


Solution

Please refer to:


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   
48   
49   
51   
52   
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,150);
$window->add($vbox = new GtkVBox());

// display title
$title = new GtkLabel(" Display options of GtkComboBox in a grid\n".
"with 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, GObject::TYPE_LONG, 
        GObject::TYPE_LONG); // note 2
} else {
    $model = new GtkListStore(Gtk::TYPE_STRING, Gtk::TYPE_LONG, 
        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) {
        $model->append(array($choice, 1, 3));  // note 3
    } elseif ($i>=12 && $i<=13) {
        $model->append(array($choice, 3, 1)); // note 4
    } elseif ($i>=14 && $i<=16) {
        $model->append(array($choice, 2, 1)); // note 5
    } else {
        $model->append(array($choice, 1, 1)); // note 6
    }
    ++$i;
}

// Set up the combobox
$combobox->set_model($model);
$combobox->set_column_span_column(1); // note 7
$combobox->set_row_span_column(2); // note 8
$combobox->set_wrap_width(7); // note 9
$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, the second column (column 1) to store the column-span information, and the thrid column (column 2) to store the row-span information
  3. Span the items 'ABCD' and 'EFGH' across four rows.
  4. Span the items 'ABCDEFG' and 'HIJKLMNOPQR' across three columns.
  5. Span the items '1001', '1002' and '1003' across two columns.
  6. No spanning for all the other items. So let's just put a 1 for both column-span and row-span.
  7. Inform php-gtk that column 1 contains the column-spanning information.
  8. Inform php-gtk that column 2 contains the row-spanning information.
  9. Don't forget this! 7 means there will be seven 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_column_span_column and set_row_span_column? too.

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

Related Links

Add comment


Security code
Refresh