PHP-GTK2 Newsletter
PHP-GTK2 Tips & Techniques
FREE Newsletter
by kksou
Sample Code 265: How to display arrays of buttons using GtkButtonBox - Part 1 - with different GtkButtonBoxStyle? |
|
Written by kksou
|
|
Thursday, 14 June 2007 |
|
Problem Suppose you would like to display an array of buttons in a row.
Note that GtkHButtonBox provides five different types of layouts, as shown by each row below:
- First row: default style
- Second row: evently spread
- Third row: evently spread with the first and last buttons aligned with the left and right edges respectively.
- Fourth row: buttons are packed from the left edge.
- Fifth row: buttons are packed from the right edge.

Solution
- Of course we could achieve this manually by creating a series of GtkButtons and stuff all of them in a GtkHBox.
- However, GtkHButtonBox is more convenient as you can see from the code below.
- The predefined constants for the GtkButtonBoxStyle are
Gtk::BUTTONBOX_DEFAULT_STYLE, Gtk::BUTTONBOX_SPREAD, Gtk::BUTTONBOX_EDGE, Gtk::BUTTONBOX_START and Gtk::BUTTONBOX_END.
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
| <?php $window = new GtkWindow(); $window->set_size_request(420, 240); $window->connect_simple('destroy', array('Gtk','main_quit')); $window->add($vbox = new GtkVBox());
// display title
$title = new GtkLabel("Display Arrays of Buttons using GtkButtonBox\n". "Part 1 - with differnet GtkButtonBoxStyle"); $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); $title->set_justify(Gtk::JUSTIFY_CENTER); $alignment = new GtkAlignment(0.5, 0, 0, 0); $alignment->add($title); $vbox->pack_start($alignment, 0); $vbox->pack_start(new GtkLabel(), 0);
// setup button
$buttons = array('button 1', 'button 2', 'button 3', 'button 4'); // note 1
$buttonbox= setup_hbuttonbox($vbox, $buttons); // note 2
$buttonbox= setup_hbuttonbox($vbox, $buttons, Gtk::BUTTONBOX_SPREAD); $buttonbox= setup_hbuttonbox($vbox, $buttons, Gtk::BUTTONBOX_EDGE); $buttonbox= setup_hbuttonbox($vbox, $buttons, Gtk::BUTTONBOX_START); $buttonbox= setup_hbuttonbox($vbox, $buttons, Gtk::BUTTONBOX_END);
$window->show_all(); Gtk::main();
function setup_hbuttonbox($container, $buttons, $layout=Gtk::BUTTONBOX_DEFAULT_STYLE) { $buttonbox = new GtkHButtonBox(); // note 3
$buttonbox->set_layout($layout); // note 4
|
- Note that this is only 70% of the sample code. You have to be a registered member to see the entire sample code. Please login or register.
- Registration is free and immediate.
- Have some doubt about the registration? Please read this forum article.
Explanation
- The array of buttons.
- Note that we set up five GtkHButtonBox here to show you the five different layouts pre-defined by php-gtk2.
- Create the GtkHButtonBox.
- Set the layout.
- Create the button.
- Register the signal 'clicked' for each button.
- Add the button to the buttonbox.
Note
This example shows GtkHButtonBox. It works the same for GtkVButtonBox.
Related Links
User reviews Average user ratings: 5.0 (from 1 user) Note: You have to be a registered member to leave a comment. Free registration here. |
|
April 30, 2008 12:17pm