PHP-GTK2 Newsletter

PHP-GTK2 Tips & Techniques
FREE Newsletter
by kksou



Sample Code 394: How to change the stock image of a button on the fly - Part 2 - stock image only?
Written by kksou   
Wednesday, 19 December 2007
Problem

We have showed you how to change the button image on the fly in Part 1.

Suppose you only want the button to display the image (as oppose to the standard button image + button label) as shown below:

How to change the stock image of a button on the fly - Part 2 - stock image only?


Solution
  • To change the label of a button, use GtkButton::set_label(). If you only want the image without any label for the button, just don't set the label of the button.
  • To change the image of a button, use GtkButton::set_image().
  • For windows users, in order to see the stock images, look for a file called gtkrc. If you have used Gnope Installer to set up php-gtk2, it should be in <dir>\etc\gtk-2.0, where <dir> is the directory where you have installed php-gtk2. Open the file with an editor, on line 5, you should see a line gtk-button-images = 0. Change the 0 to 1 and save the file. Run the script again. You should be able to see the stock images now.

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   
<?php
$window = new GtkWindow();
$window->set_title($argv[0]);
$window->set_size_request(400, 200);
$window->connect_simple('destroy', array('Gtk','main_quit'));
$window->add($vbox = new GtkVBox());

// display title
$title = new GtkLabel("Change the stock image of a button\n".
"    Part 2 - stock image only");
$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);

$vbox->pack_start(new GtkLabel("Click the play button below.\n".
"The button will change to stop button.\n".
"Click the button again. The stop button will change to play button."), 0);
$vbox->pack_start(new GtkLabel(), 0);

// setup button
$vbox->pack_start($hbox = new GtkHBox(), 0);
$button = new GtkButton();
$img = GtkImage::new_from_stock(Gtk::STOCK_MEDIA_PLAY, 
    Gtk::ICON_SIZE_BUTTON); // note 1
$button->set_image($img);
$button->connect('clicked', 'on_click');
$button_status = 'play';

$hbox->pack_start($button, 0);

$window->show_all();
  • 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 code above is almost exactly the same as that of Part 1, except that we do not set any label for the button.

  1. Set the initial stock image to Gtk::STOCK_MEDIA_PLAY.
  2. Change the button image from Play to Stop.
  3. Change the button image from Stop to Play.

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.

 
< Prev   Next >

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