PHP-GTK2 Newsletter
PHP-GTK2 Tips & Techniques
FREE Newsletter
by kksou
Sample Code 424: How to place a background image in GtkEventBox - Part 2 - using GdkDrawable draw_pixbuf? |
|
Written by kksou
|
|
Monday, 28 January 2008 |
|
Problem This is in response to Andreas' Post titled "Possible to add not tiled backgrounds".
Suppose you want to have a single background image that always stayed at the top right corner of a GtkEventBox (no matter how the user changes the window size) as shown below:

Solution
Sample Code Note: The following image files are required by the sample code below. Please save a copy of the image files and put them in the same directory where you store the sample code.
 | ball_green3.png |
 | ball_blue3.png |
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
| <?php $window = new GtkWindow(); $window->set_size_request(400, 240); $window->connect_simple('destroy', array('Gtk','main_quit')); $window->add($vbox = new GtkVBox());
// display title
$title = new GtkLabel(" Place a background image in GtkEventBox\n". "Part 2 - using GdkDrawable::draw_pixbuf()"); $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); $vbox->pack_start($title, 0); $vbox->pack_start(new GtkLabel(), 0);
$vbox->pack_start($hbox = new GtkHBox());
$eventbox_left = new GtkEventBox(); $hbox->pack_start($eventbox_left); $eventbox_left->add($vbox_left = new GtkVBox()); $vbox_left->pack_start(new GtkLabel("This is left eventbox.")); $vbox_left->pack_start(new GtkLabel("The green ball is the bg image.")); $vbox_left->pack_start(new GtkLabel("Note that this eventbox")); $vbox_left->pack_start(new GtkLabel("uses the default gray backgd color.")); $eventbox_left->connect('expose_event', 'expose_event', "ball_green4.png"); // note 1
$eventbox_right = new GtkEventBox(); $hbox->pack_start($eventbox_right); $eventbox_right->add($vbox_right = new GtkVBox()); $vbox_right->pack_start(new GtkLabel("This is right eventbox.")); $vbox_right->pack_start(new GtkLabel("The blue ball is the bg image.")); $vbox_right->pack_start(new GtkLabel("Note that you can also set")); $vbox_right->pack_start(new GtkLabel("backgd color for the eventbox!")); $eventbox_right->modify_bg(Gtk::STATE_NORMAL, GdkColor::parse("#BAFFB3")); $eventbox_right->connect('expose_event', 'expose_event', "ball_blue4.png"); // note 1
$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
- Register for the signal 'expose_event'.
- Load the background image. This works with all standard image file format such as .gif, .jpg or .png.
- Calculate where to place the background image. Here we place the image at the op right corner of the GtkEventBox.
- Sets the background image.
Note
Try compare this method with that described in How to place a background image in GtkEventBox - Part 1 - tiled background image?
- Did you notice that this method supports transparencies in .png and .gif file?
- As we're drawing each image directly onto the underlying GdkWindow, there's no issue of tiled images.
- You can set a different background color for the eventbox too, i.e. a colored background color + a background image.
- Note that the image will always stay at the top right, no matter how the user changes the window size as shown below:

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. |
|
March 29, 2008 9:32am