PHP-GTK2 Newsletter

PHP-GTK2 Tips & Techniques
FREE Newsletter
by kksou



Sample Code 6: How to place a background image in GtkWindow?
Written by kksou   
Tuesday, 12 September 2006
Problem

You want to place a background image in a GtkWindow as shown below:

How to place a background image in GtkWindow?


Solution
  • Load the image with GdkPixbuf::new_from_file().
  • Get the corresponding pixmap using GdkPixbuf::render_pixmap_and_mask().
  • Get the GtkStyle corresponding to the window with GtkWidget::get_style().
  • Set the new style with GtkWidget::set_style().

Sample Code

The following image file (sample6.png) is required by the sample code below. Please save a copy of the image file and put it in the same directory where you store the sample code.

1   
2   
3   
4   
5   
6   
7   
8   
9   
10   
11   
12   
13   
14   
<?php
$window = new GtkWindow();
$window->connect_simple('destroy', array('Gtk','main_quit'));
$window->set_size_request(480, 240);

// set background image
$pixbuf=GdkPixbuf::new_from_file("sample6.png"); // note 1
list($pixmap,$mask)=$pixbuf-> render_pixmap_and_mask(255); // note 2
$style = $window->get_style(); // note 3
$style=$style->copy(); // note 4
$style->bg_pixmap[Gtk::STATE_NORMAL]=$pixmap; // note 5
$window->set_style($style); // note 6

// prints hello world
  • 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. Load the background image. This works with all standard image file format such as .gif, .jpg or .png.
  2. Convert to pixmap.
  3. Get the GtkStyle corresponding to the window.
  4. Make a copy of the style.
  5. Set the background image.
  6. Set the window with the new style.

Related Links

User reviews   Average user ratings:    4.0   (from 1 user)
  1. forgive me
    April 04, 2008 9:09pm

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

 
< Prev   Next >

Copyright © 2006-2008. kksou.com. All Rights Reserved