Problem
We have displayed a 2D array in a table with borders in Part 3
Now we would like to center the table as shown below:
Solution
- Stuff two GtkHBox, one on the left of the table, and the other on the right.
- PHP-GTK will "auto-expand" both GtkHBox, indirectly centering the table.
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 32 33 35 36 37 38 39 40 41 42 43 44 45 | <?php $window = new GtkWindow(); $window->set_size_request(400, 200); $window->connect_simple('destroy', array('Gtk','main_quit')); $window->add($vbox = new GtkVBox()); // display title $title = new GtkLabel("Display 2D Array in Table - Part 4\n (with borders and centered)"); $title->modify_font(new PangoFontDescription("Times New Roman Italic 10")); $title->modify_fg(Gtk::STATE_NORMAL, GdkColor::parse("#0000ff")); $title->set_size_request(-1, 40); $vbox->pack_start($title, 0, 0); $vbox->pack_start($hbox = new GtkHBox()); // note 1 $hbox->pack_start(new GtkHBox()); // note 2 $hbox->pack_start($table = new GtkTable(), 0, 0); // note 3 $hbox->pack_start(new GtkHBox()); // note 4 $data = array( array('', 'header1', 'header2', 'header3'), array('row0', 1, 2, 3), array('row1', 4, 5, 6), array('row2', 7, 8, 9), array('row3', 10, 11, 12), array('row4', 13, 14, 15)); display_table ($table, $data); function display_table($table, $a) { for ($row=0; $row<count($a); ++$row) { for ($col=0; $col<count($a[$row]); ++$col) { $frame = new GtkFrame(); $frame->add(new GtkLabel($a[$row][$col])); $table->attach($frame, $col, $col+1, $row, $row+1, Gtk::FILL, Gtk::SHRINK, 0, 0); // note 2 } } } $window->show_all(); Gtk::main(); ?> |
Output
As shown above.Explanation
Please refer to:
- Part 1 which explains how to display a 2D array in a table.
- Part 2 which explains how to add borders to table.
- Part 3 which explains how to add borders to table, and display the cells in a tightly packed manner.
What's new here in Part 4:
- Create a GtkHBox to hold the following 3 widgets.
- Stuff one GtkHBox which acts as "auto-expand" buffer on the left of the table.
- Put the table in the middle.
- Stuff one more "auto-expand" buffer on the right of the table.
Try to change the window size. You will see that the two "auto-expand" buffers will always "force" the table to stay at the center.
Read more...