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 6 docx
MIỄN PHÍ
Số trang
40
Kích thước
496.2 KB
Định dạng
PDF
Lượt xem
983

Apress Pro PHP-GTK phần 6 docx

Nội dung xem thử

Mô tả chi tiết

178 CHAPTER 8 ■ USING MULTILINE TEXT

Summary

Multiline text is a powerful tool, not only for displaying large amounts of text, but also for

collecting large amounts of data from the user. Using multiline text can be simple or rather

complex. If plain black text is all you need, you can easily set up a GtkTextView with a buffer. If

the text needs to be formatted or modified, GtkTextIter, GtkTextMark, and GtkTextTag allow that

to happen. All of these widgets and objects make for a well-designed and specialized tool set.

Text is not the only type of data that comes in large quantities. There are other types of large

data sets, such as arrays and trees, that cannot be properly displayed with any of the tools seen

so far. In Chapter 9, we will look at how to display large amounts of data. We will look at using

trees and lists as the models behind several different ways to display data. Among the types of

data that our sample application will display are a list of news headlines and a sortable and

expandable list of products.

6137ch08.qxd 3/14/06 2:12 PM Page 178

Working with Trees and Lists

In the past two chapters, you’ve learned how to display and edit both small and large blocks

of text. Yet there is still another type of data that requires special handling: collections. Collec￾tions are made up of several elements grouped using data structures such as arrays, lists, and

trees. For a collection, you may need to show the relationship between individual elements,

sort the collection, and filter certain values. The tools that have been introduced so far cannot

easily fulfill these needs.

PHP-GTK handles collections of data in a manner similar to how it manages multiline

text. One group of objects organizes the data, while another concentrates on the display. This

allows you to show one set of data in multiple ways at the same time. Without this separation

of responsibility, each piece of the application that wanted to gather information from a data

set would have to create and manage its own instance of the data. Using models to manage the

data and views to handle the display allows for more flexibility with less code.

You can use several models to represent data. First, we will examine the unique uses of

each type of model. Then we will look at how to use these models to view the collection of data

depending on the needs of the application.

Models

Collections of data can be organized into trees. A tree is a set of data in which the elements

have a parent-child relationship. This relationship may be obvious as it is with a directory list￾ing, or it may be more subtle, such as an array. In a directory listing, a directory is a parent and

its files and subdirectories are its children. An array is really a list with multiple columns and

elements. A list is just a tree in which each element has at most one child.

Keeping track of this type of data is the responsibility of two types of models: GtkListStore

and GtkTreeStore. Each object represents data as a set of rows, where a row is one element in

the list or tree but may contain more than one value. This is because a model may have many

columns. Each column in a row represents one atomic piece of data. With both trees and lists,

data can be prepended, inserted, and appended to a collection. The difference is that elements

in trees may have children, but elements in lists cannot. The main objective of both models is

the same, but lists are less complex and therefore easier to work with.

179

CHAPTER 9

■ ■ ■

6137ch09.qxd 3/14/06 2:14 PM Page 179

180 CHAPTER 9 ■ WORKING WITH TREES AND LISTS

The GtkListStore Model

GtkListStore represents a tree of order one, meaning that each element has at most one child.

Restricting each element to having only one child makes managing the data a little easier than

when there are multiple children. Lists can put data only before or after another piece of data.

It is not possible for two pieces of data to occupy the same level in a list. This may sound like

a strange restriction to impose on a set of data, but it makes life easier. Lists are well suited as

the data behind widgets like GtkComboBox and GtkEntryCompletion, where one value should

follow another. In fact, in Chapter 7, we used a GtkListStore to populate both types of widgets.

Listing 9-1 shows the portion of GtkEntryCompletion example from Chapter 7 (Listing 7-6) that

creates a simple GtkListStore.

Listing 9-1. Creating a Simple GtkListStore

<?php

// ...

public static function createStateList()

{

// Create a new list store.

$listStore = new GtkListStore(GTK::TYPE_STRING);

// Get an iterator for appending a value.

$iter = $listStore->append();

// Append a value.

$listStore->set($iter, 0, 'Alabama');

// Get an iterator for appending a value.

$iter = $listStore->append();

// Append a value.

$listStore->set($iter, 0, 'Alaska');

// Get an iterator for appending a value.

$iter = $listStore->append();

// Append a value.

$listStore->set($iter, 0, 'Arizona');

// Get an iterator for appending a value.

$iter = $listStore->append();

// Append a value.

$listStore->set($iter, 0, 'Arkansas');

// Get an iterator for appending a value.

$iter = $listStore->append();

// Append a value.

$listStore->set($iter, 0, 'California');

// Get an iterator for appending a value.

$iter = $listStore->append();

// Append a value.

$listStore->set($iter, 0, 'Colorado');

// ...

6137ch09.qxd 3/14/06 2:14 PM Page 180

CHAPTER 9 ■ WORKING WITH TREES AND LISTS 181

return $listStore;

}

// ...

?>

The first step is creating the list store, which represents the model in the Model-View￾Controller (MVC) design pattern. This is done in typical PHP fashion using the new operator.

In Listing 9-1, one argument is passed to the constructor. The constructor expects a variable list

of arguments. Each argument passed in corresponds to a column in the list. The value that is

passed for each column defines the expected data type for that column. It may seem odd to have

to explicitly give the column type in a loosely typed language, but keep in mind that PHP-GTK

is based on GTK+ which is written in C, a strictly typed language. Providing the data type helps

the view component determine the best way to show the data and keeps memory usage under

control.

There are many acceptable column types, but not all of them are relevant to PHP-GTK.

The following are the relevant values:

• Gtk::TYPE_BOOLEAN: For values that have only two states, such as on/off or true/false.

• Gtk::TYPE_LONG: For integers.

• Gtk::TYPE_DOUBLE: For floating-point values, like 1.234.

• Gtk::TYPE_STRING: For text values or values that should be treated like text, such as

“crissscott” or “321”.

• Gtk::TYPE_OBJECT: For objects that extend from GObject, like GtkObject or GtkButton.

• Gtk::TYPE_PHP_VALUE: For any PHP data type, including user-defined classes, arrays,

integers and even resource handles like those used for database connections.

■Note If a column is set to type Gtk::TYPE_OBJECT, the value in the column must be a descendant of

GObject. You may use your own custom classes only if they extend GObject or some descendant of that

class, such as GtkObject or GtkWidget.

Adding Data to a List

After you’ve created the list with all of its column types, the next task is to add data. First, you

add a row to the list, and then you set the data for the row.

After a row is added, the position of the new row is identified by an iterator. This type of itera￾tor is similar to the iterator described in Chapter 8 (GtkTextIter) in that it identifies a location.

In this case, the iterator is an instance of GtkTreeIter. GtkTreeIter cannot be instantiated

directly using the new operator. GtkTreeIter has two methods: copy and free. The only method

you’re likely to call is copy. This method simply makes another instance of GtkTreeIter that

points to the same location.

6137ch09.qxd 3/14/06 2:14 PM Page 181

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