|
Problem You have displayed an array of buttons in a row using GtkHButtonBox in How to display arrays of buttons using GtkButtonBox - Part 2 - set spacing between buttons?
Now you would like to display a Help button that stays at the right edge of the row as shown below:

Solution
- GtkButtonBox supports up to two groups of buttons in a row. This means that we can put the Help button in the second group that will stay at the right edge of the rows of buttons.
- To add a button to the second group, we use GtkButtonbox::set_child_secondary()
- However, just like the method GtkBox::set_spacing(), this works only for the layouts
Gtk::BUTTONBOX_START and Gtk::BUTTONBOX_END.
- The method has no effect on the following layout:
Gtk::BUTTONBOX_DEFAULT_STYLE, Gtk::BUTTONBOX_SPREAD, Gtk::BUTTONBOX_EDGE, as you can see from the sample output.
- Note also that if you use the layout
Gtk::BUTTONBOX_END, the second group appears on the left instead of right.
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 44
| <?php $window = new GtkWindow(); $window->set_size_request(480, 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 3 - two button groups"); $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( array('Btn 1', 'Btn 2'), // note 1
array('Help')); // note 2
$buttonbox= setup_hbuttonbox($vbox, $buttons); $buttonbox= setup_hbuttonbox($vbox, $buttons, Gtk::BUTTONBOX_SPREAD); $buttonbox= setup_hbuttonbox($vbox, $buttons, Gtk::BUTTONBOX_EDGE); $buttonbox= setup_hbuttonbox($vbox, $buttons, Gtk::BUTTONBOX_START, 10); $buttonbox= setup_hbuttonbox($vbox, $buttons, Gtk::BUTTONBOX_END, 10);
$window->show_all(); Gtk::main();
function setup_hbuttonbox($container, $buttons, $layout=Gtk::BUTTONBOX_DEFAULT_STYLE, $spacing=0) { $buttonbox = new GtkHButtonBox(); $buttonbox->set_layout($layout);
foreach($buttons[0] as $button_label) { $button = new GtkButton($button_label); $button->set_size_request(60, -1); $button->connect('clicked', 'on_click'); $buttonbox->add($button); // note 3
}
foreach($buttons[1] as $button_label) {
|
- 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 above example is based on the code from Part 2.
What's new here:
- Group 1 buttons definition.
- Group 2 buttons definition.
- Add group 1 buttons.
- Add group 2 buttons.
- Set the button as group 2.
- Set any spacing if specified.
Note
This example shows GtkHButtonBox. It works the same for GtkVButtonBox.
Related Links
User reviews There are no user reviews yet. Note: You have to be a registered member to leave a comment. Free registration here. |