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.

How to display arrays of buttons using GtkButtonBox - Part 1 - with different GtkButtonBoxStyle?


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
  1. The array of buttons.
  2. Note that we set up five GtkHButtonBox here to show you the five different layouts pre-defined by php-gtk2.
  3. Create the GtkHButtonBox.
  4. Set the layout.
  5. Create the button.
  6. Register the signal 'clicked' for each button.
  7. 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)
  1. Rasmus Rimestad
    April 30, 2008 12:17pm

Note: You have to be a registered member to leave a comment. Free registration here.

 
< Prev   Next >

Blog - Forum - Privacy Policy - Contact Us
Copyright © 2006-2008. kksou.com. All Rights Reserved