Siêu thị PDFTải ngay đi em, trời tối mất

Thư viện tri thức trực tuyến

Kho tài liệu với 50,000+ tài liệu học thuật

© 2023 Siêu thị PDF - Kho tài liệu học thuật hàng đầu Việt Nam

Apress Pro PHP-GTK phần 8 potx
MIỄN PHÍ
Số trang
37
Kích thước
792.0 KB
Định dạng
PDF
Lượt xem
858

Tài liệu đang bị lỗi

File tài liệu này hiện đang bị hỏng, chúng tôi đang cố gắng khắc phục.

Apress Pro PHP-GTK phần 8 potx

Nội dung xem thử

Mô tả chi tiết

258 CHAPTER 12 ■ ADDING IMAGES

Figure 12-1. A broken image icon

Using a Stock ID

In previous examples, you have seen how to create images for buttons and menu items by using

stock IDs. A stock ID is a shortcut that identifies a commonly used image. You can also create

a regular image from a stock ID by using new_from_stock. This static constructor takes the stock

ID plus a size. The size is defined by a GtkIconSize constant. Stock images come in different sizes

so that they can be used for different purposes. Each use has its own size constant, as follows:

• Gtk::ICON_SIZE_MENU: A small image normally used in menus.

• Gtk::ICON_SIZE_SMALL_TOOLBAR: A small version of the icon used for toolbars.

• Gtk::ICON_SIZE_LARGE_TOOLBAR: A larger version of the toolbar icon.

• Gtk::ICON_SIZE_BUTTON: The version normally used for buttons.

• Gtk::ICON_SIZE_DND: The icon size used when an item is dragged.

• Gtk::ICON_SIZE_DIALOG: The version normally used in dialog windows.

Using a Pixel Buffer

Another way to create an image is from data in memory. An image stored in memory can usu￾ally be found in a GdkPixbuf. A pixel buffer (or pixbuf ) is simply a representation of the image

in memory. It cannot be displayed on its own; it is just data. You can put an image into a pixbuf

when an application is started to allow commonly used images to be loaded more quickly.

A pixbuf can be created from a file, just as an image can be created. To create a pixbuf

from a file, call the static constructor new_from_file and pass a file path. Listing 12-1 creates

a pixbuf from a file and then creates an image from the pixbuf using the static constructor

new_from_pixbuf.

Listing 12-1. Loading an Image into a GdkPixbuf and Then a GtkImage

<?php

// Create a window to display the image.

$window = new GtkWindow();

// Close the application cleanly.

$window->connect_simple('destroy', array('Gtk', 'main_quit'));

// Load a pixbuf from a file.

$pb = GdkPixbuf::new_from_file('Crisscott/images/logo.png');

6137ch12.qxd 3/14/06 2:29 PM Page 258

CHAPTER 12 ■ ADDING IMAGES 259

// Create the image from the pixbuf.

$image = GtkImage::new_from_pixbuf($pb);

// Add the image to the window.

$window->add($image);

// Show the image and the window.

$window->show_all();

// Start the main loop.

Gtk::main();

?>

Using GdkPixbuf and GtkImage, we can now add product images to the Crisscott PIMS

application. Product images should appear in two places: in the product summary area and in

the product editing tool.

Listing 12-2 shows the code added to the Crisscott_Tools_ProductEdit class to allow the

user to add or change a product image. The added lines include a GtkEntry for entering the path

to the image file and the code to display the images. In this example, the creation of the pixbuf

is wrapped in a try/catch block. That is because if the file for the pixbuf is not found, an excep￾tion will be thrown. This allows the application to detect a failure to load an image.

Listing 12-2. Adding Product Images to the Application

<?php

// The added lines from the product summary area.

class Crisscott_Tools_ProductSummary extends GtkTable {

// ...

public function displaySummary(Crisscott_Product $product)

{

// Set the product.

$this->product = $product;

// Set the attribute labels to the values of the product.

$this->productName->set_text($product->name);

$this->productType->set_text($product->type);

// Get the category information.

require_once 'Crisscott/Inventory.php';

$inv = Crisscott_Inventory::singleton();

$cat = $inv->getCategoryById($product->categoryId);

// Set the category name.

$this->productCategory->set_text($cat->name);

// Set the product price.

$this->productPrice->set_text($product->price);

// Remove the current product image.

$this->productImage->remove($this->productImage->get_child());

6137ch12.qxd 3/14/06 2:29 PM Page 259

260 CHAPTER 12 ■ ADDING IMAGES

// Try to add the product image.

try {

// Create a pixbuf.

$pixbuf = GdkPixbuf::new_from_file($product->imagePath);

// Create an image from the pixbuf.

$this->productImage->add(GtkImage::new_from_pixbuf($pixbuf));

// Show the image.

$this->productImage->show_all();

} catch (Exception $e) {

// Just fail silently.

}

}

// ...

}

class Crisscott_Tools_ProductEdit extends GtkTable {

// ...

private function _layout()

{

// Set up the data entry widgets.

// ...

$this->imageContainer = new GtkFrame();

$this->imagePathEntry = new GtkEntry();

// ...

// Set up the labels.

// ...

$this->imageLabel = new GtkLabel('Image');

// Set the labels' size.

// ...

$this->imageLabel->set_size_request(100, -1);

// ...

// Next align each label within the parent container.

// ...

$this->imageLabel->set_alignment(0, .5);

// Make all of the labels use markup.

// ...

$this->imageLabel->set_use_markup(true);

// ...

6137ch12.qxd 3/14/06 2:29 PM Page 260

Tải ngay đi em, còn do dự, trời tối mất!