204. How to use stock images for buttons but with different labels - Part 1?

Problem

In the article How to use stock images for buttons? you have created buttons using pre-defined stock images. However, you would like to change some of these labels to your own as shown below:

How to use stock images for buttons but with different labels - Part 1?


Solution

We make use of the technique as described in How to display button with image?

  • Create a standard GtkButton.
  • Create a standard GtkHBox and stuff it inside the button.
  • Instead of a standard image file, we load a pre-defined stock image using GtkImage::new_from_stock().
  • Pack the image in the hbox.
  • Create the button label with GtkLabel and pack it inside the hbox too.

You now have a button with stock image on the left and label on the right.


Sample Code

1   
2   
3   
4   
5   
6   
7   
8   
9   
10   
11   
12   
13   
14   
15   
17   
18   
19   
20   
23   
26   
27   
28   
34   
35   
36   
37   
38   
39   
40   
41   
46   
47   
48   
49   
50   
51   
52   
55   
56   
57   
60   
61   
62   
63   
64   
65   
66   
67   
68   
70   
71   
72   
73   
79   
<?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("Using Stock Images but with different labels\n".
"Part 1 - using hbox");
$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, 0);
$vbox->pack_start(new GtkLabel(), 0, 0);

// setup button
$vbox->pack_start($hbox = new GtkHBox(), 0);
$hbox->pack_start(create_button(Gtk::STOCK_NEW, 'New File'), 0);
$hbox->pack_start(create_button(Gtk::STOCK_OPEN, 'Open File'), 0);
$hbox->pack_start(create_button(Gtk::STOCK_CLOSE, 'Close File'), 0);

$vbox->pack_start(new GtkLabel(), 0);

$vbox->pack_start($hbox = new GtkHBox(), 0);
$hbox->pack_start(create_button(Gtk::STOCK_MEDIA_PREVIOUS, 'Prev'), 0);
$hbox->pack_start(create_button(Gtk::STOCK_MEDIA_PLAY), 0);
$hbox->pack_start(create_button(Gtk::STOCK_MEDIA_STOP), 0);
$hbox->pack_start(create_button(Gtk::STOCK_MEDIA_NEXT), 0);

$window->show_all();
Gtk::main();

function create_button($stock_id, $label='') {
    if ($label=='') {
        return GtkButton::new_from_stock($stock_id); // note 1
    }

    $button = new GtkButton();
    $hbox = new GtkHBox(); // note 2
    $button->add($hbox);

    $img=GtkImage::new_from_stock($stock_id, 
        Gtk::ICON_SIZE_SMALL_TOOLBAR); // note 3
    $hbox->pack_start($img, 0, 0);

    $hbox->pack_start(new GtkLabel(), 0, 0);
    $hbox->pack_start(new GtkLabel($label), 0, 0); // note 4

    return $button;
}

?>

Output

As shown above.

 

Explanation

  1. If there is no alternative label being specified, we just use the standard way of creating button with stock image.
  2. The hbox is used to hold the image and the label.
  3. Load the stock image and pack it into the hbox.
  4. Create the button label and also pack it into the hbox.

Related Links

Add comment


Security code
Refresh