A tree of items that collapse and expand as requested
PtWidget → PtBasic → PtContainer → PtCompound → PtGenList → PtGenTree → PtTree
For more information, see the diagram of the widget hierarchy.
<photon/PtTree.h>
The PtTree widget displays a tree of items that collapse or expand according to your selections. PtTree assumes that each tree item contains two images and a string.
The items in a PtTree are stored in structures of type PtTreeItem_t, which you should allocate by calling PtTreeAllocItem() or PtTreeCreateItem(). The PtTree widget frees all its items automatically when it's destroyed.
The item structure contains a “user data” pointer that you can use to store any data you want to associate with the item; the widget ignores this pointer. There are some functions that free items, but none of them attempts to free the user data. |
PtTreeAllocItem() takes as arguments:
This function uses the font and image list stored in the tree widget to determine the item's dimensions. This means that between creating an item and adding it to the widget, you mustn't change the widget's font or images.
PtTreeCreateItem() is similar to PtTreeAllocItem(), except instead of the set and unset image indexes, it takes a pointer to a PtTreeItemAttributes_t structure which defines the set and unset images, as well as font and color attributes for the item's text.
Use PtTreeAddFirst() and PtTreeAddAfter() to build a tree. For example, suppose you wanted to build the following tree:
Assuming your tree widget is referenced by tree_wgt, use the following code:
PtTreeItem_t *item, *brother; char *text; /* Add "Item 1" as first root item */ text = "Item 1"; item = PtTreeCreateItem(tree_wgt, text, NULL); PtTreeAddFirst(tree_wgt, item, NULL); brother = item; /* Add "Item 2" as root item after "Item 1" */ text = "Item 2"; item = PtTreeCreateItem(tree_wgt, text, NULL); PtTreeAddAfter(tree_wgt, item, brother); brother = item; /* Add "Item 2a" as first child of "Item 2" */ text = "Item 2a"; item = PtTreeCreateItem(tree_wgt, text, NULL); PtTreeAddFirst(tree_wgt, item, brother); /* Add "Item 3" as root item after "Item 2" */ text = "Item 3"; item = PtTreeCreateItem(tree_wgt, text, NULL); PtTreeAddAfter(tree_wgt, item, brother);
The array of all available images is stored in the widget's Pt_ARG_TREE_IMAGES resource; the items contain indexes into this array.
Before allocating the tree items, set up the array of images; PtTreeAddImages() provides a convenient way to do this.
When you call PtTreeAllocItem() to allocate the tree items, specify the index of the icons to use when the item is set or unset. To specify the meaning of “set” and “unset”, use the Pt_ARG_TREE_IMGMASK resource.
Alternatively, you can use PtTreeCreateItem() or PtTreeChangeItem() to assign images not in the Pt_ARG_TREE_IMAGES resource to a tree item.
Here's some code that sets up a tree with images:
PtTreeItem_t *item, *brother; char *text; int closed_dir, closed_file, open_dir, open_file; /* Extract the images from a widget database and add them to the tree's array of images: */ closed_dir = PtTreeAddImages (tree_wgt, ApGetImageRes (database, "dir_closed"), 1); closed_file = PtTreeAddImages (tree_wgt, ApGetImageRes (database, "file_closed"), 1); open_dir = PtTreeAddImages (tree_wgt, ApGetImageRes (database, "dir_open"), 1); open_file = PtTreeAddImages (tree_wgt, ApGetImageRes (database, "file_open"), 1); /* Add "Directory 1" as first root item */ text = "Directory 1"; item = PtTreeAllocItem(tree_wgt, text, open_dir, closed_dir); PtTreeAddFirst(tree_wgt, item, NULL); brother = item; /* Add "Directory 2" as root item after "Directory 1" */ text = "Directory 2"; item = PtTreeAllocItem(tree_wgt, text, open_dir, closed_dir); PtTreeAddAfter(tree_wgt, item, brother); brother = item; /* Add "My File" as first child of "Directory 2" */ text = "My File"; item = PtTreeAllocItem(tree_wgt, text, open_file, closed_file); PtTreeAddFirst(tree_wgt, item, brother); /* Add "Directory 3" as root item after "Directory 2" */ text = "Directory 3"; item = PtTreeAllocItem(tree_wgt, text, open_dir, closed_dir); PtTreeAddAfter(tree_wgt, item, brother);
The tree's Pt_ARG_TREE_IMGMASK is set to Pt_TREE_ITEM_EXPANDED so that the “open” icon is displayed when the tree item is expanded. The tree looks like this:
To display items in columns, you can:
Or
With both methods, use the Tab character in the item strings as a column separator.
Even if you use columns, each line in the tree remains a single item. When you select any part of the line, the entire line is selected — having columns doesn't make the tree into a spreadsheet. |
You can make your PtTree display clickable images instead of text in selected columns. Here's how:
Set the Pt_LIST_COLUMN_NO_STRING flag in the PtGenList column attributes (Pt_ARG_LIST_COLUMN_ATTR) for each column that you want to display images. This prevents text from being drawn on top of images. For example:
static const PtListColumnAttributes_t latrs[] = { { Pt_LIST_COLUMN_LEFT }, { Pt_LIST_COLUMN_CENTER | Pt_LIST_COLUMN_NO_STRING } }; PtSetResource( ABW_tree, Pt_ARG_LIST_COLUMN_ATTR, latrs, sizeof(latrs) / sizeof(latrs[0]) );
A tab character in an item's string skips over a column flagged as Pt_LIST_COLUMN_NO_STRING. For example, if the second column has the flag set, you need only one tab character between the strings for the first and the third column. |
Set up an array of image pointers for each column and an array of tree column attributes (Pt_ARG_TREE_COLUMN_ATTR):
static const PhImage_t *images[3] = { &image1, &image2, &image3 }; static const PtTreeColumnAttributes_t tatrs[] = { /* The first column has no images: */ { NULL, 0, Pt_TREE_COLUMN_NOCB }, { images, sizeof(images) / sizeof(images[0]), Pt_TREE_COLUMN_NOSELECT, Pt_LIST_ITEM_FLAG_USER1 | Pt_LIST_ITEM_FLAG_USER2 } }; PtSetResource( ABW_tree, Pt_ARG_TREE_COLUMN_ATTR, tatrs, sizeof(tatrs) / sizeof(tatrs[0]) );
Optionally, write an image-selector function and attach it to the Pt_ARG_TREE_COLUMN_IMGFUN resource.
This function takes as arguments a tree item, a column index, and a column attributes structure, and returns an index into the column's image array (or -1 for no image). You need this function only if:
Or:
For example:
static int image_selector( PtWidget_t *widget, PtTreeItem_t *item, PtTreeColumnAttributes_t const *cattr, unsigned cindex ) { unsigned const flags = item->gen.list.flags; int result = -1; /* We don't really need to check if cindex is one because we only have one column that displays images. But for the sake of example... */ if ( cindex == 1 ) { /* This column has three images and four possible states (no image is the fourth state). The function should return -1 for no image and 0...3 to pick an image. */ if ( flags & Pt_LIST_ITEM_FLAG_USER2 ) result += 2; if ( flags & Pt_LIST_ITEM_FLAG_USER1 ) result += 1; } return result; } … PtSetResource( ABW_tree, Pt_ARG_TREE_COLUMN_IMGFUN, image_selector, 0 );
Optionally, attach a Pt_CB_TREE_COLUMN_SEL callback that both notifies your application when you click on a column, and lets you control how the item's state changes.
For instance, the following callback makes your items cycle through the four possible states:
static int column_cb( PtWidget_t *widget, void *data, PtCallbackInfo_t *cbinfo ) { PtTreeCallback_t *tcb = cbinfo->cbdata; if ( tcb->column == 1 ) { if ( tcb->new_flags & Pt_LIST_ITEM_FLAG_USER1 ) tcb->new_flags ^= Pt_LIST_ITEM_FLAG_USER2; } return Pt_CONTINUE; }
Resource | C type | Pt type | Default |
---|---|---|---|
Pt_ARG_TREE_BALLOON | PtTreeBalloonF_t * | Pointer | See below |
Pt_ARG_TREE_COLUMN_ATTR | See below | Array | NULL |
Pt_ARG_TREE_COLUMN_IMGFUN | See below | Pointer | See below |
Pt_ARG_TREE_IMAGES | PhImage_t *, short | Array | NULL |
Pt_ARG_TREE_IMGMASK | unsigned | Scalar | Pt_LIST_ITEM_SELECTED |
Pt_CB_TREE_COLUMN_SEL | PtCallback_t * | Link | NULL |
Pt_CB_TREE_SELECTION | PtCallback_t * | Link | NULL |
Pt_CB_TREE_STATE | PtCallback_t * | Link | NULL |
C type | Pt type | Default |
---|---|---|
PtTreeBalloonF_t * | Pointer | See below |
A function that inflates a balloon for the item the pointer is on. PtTreeBalloonF_t is a function type:
typedef PtWidget_t *PtTreeBalloonF_t( PtWidget_t *widget, PtWidget_t *parent, PhArea_t *area, PtListColumn_t *column, PtTreeItem_t *item, int coln, const char *font );
The parameters are as follows:
The default function does this:
return PtGenListCreateTextBalloon( widget, parent, PtGenListSetColumnBalloon ( area, column ), item->string, coln, font);
C type | Pt type | Default |
---|---|---|
PtTreeColumnAttributes_t, short | Array | NULL |
Attributes for the tree's columns. This resource is an array of PtTreeColumnAttributes_t structures. The structure contains at least the following members (in the following order — static initialization is OK):
C type | Pt type | Default |
---|---|---|
PtTreeColumnImageF_t * | Pointer | See below |
This function is called by the draw function for each item and each column that has the Pt_LIST_COLUMN_NO_STRING flag in the Pt_ARG_LIST_COLUMN_ATTR resource (see PtGenList) and a nonempty image array defined in the Pt_ARG_TREE_COLUMN_ATTR resource.
The function must be of the following type:
typedef int PtTreeColumnImageF_t( PtWidget_t *widget, PtTreeItem_t *item, PtTreeColumnAttributes_t const *cattr, unsigned cindex );
The arguments are:
The function should return an index into the image array, or -1 if no image should be drawn. If you return a value that's equal to or greater than the column's nimages, no image is drawn.
The default function returns 0 or 1, depending on whether the column's iflags ANDed with the item's flags gives a nonzero result.
The function attached to this resource isn't allowed to modify anything and should be quick, since it's called a lot. |
For an example, see “Displaying images in columns,” above.
C type | Pt type | Default |
---|---|---|
PhImage_t *, short | Array | NULL |
A list of images that can be displayed with tree items. For more information about the image structure, see PhImage_t in the Photon Library Reference.
Set the flags member of the PhImage_t structures
to: Ph_RELEASE_IMAGE | Ph_RELEASE_PALETTE | Ph_RELEASE_TRANSPARENCY_MASK | Ph_RELEASE_GHOST_BITMAP before providing the images to the widget. If you do this, the memory used for the images is released when the widget is destroyed or Pt_ARG_TREE_IMAGES is set. |
For a convenient way to add images, see PtTreeAddImages(); for an example, see “Using images in tree items,” above.
C type | Pt type | Default |
---|---|---|
unsigned | Scalar | Pt_LIST_ITEM_SELECTED |
Defines the mask for selecting an item's image.
When you create an item by calling PtTreeAllocItem(), you specify the images to be used when the item is set or unset. An item is considered set if its flags masked with the tree's Pt_ARG_TREE_IMGMASK resource give a nonzero value. The flags include:
C type | Pt type | Default |
---|---|---|
PtCallback_t * | Link | NULL |
A list of PtCallback_t structures that define the callbacks that are invoked when you click on a column that has an entry in the Pt_ARG_TREE_COLUMN_ATTR array but the Pt_TREE_COLUMN_NOCB flag in that entry isn't set.
Each callback is passed a PtCallbackInfo_t structure that contains at least the following members:
Only the “user flags” (Pt_LIST_ITEM_FLAG_USER[1234] defined in PtGenList.h) and Pt_LIST_ITEM_DAMAGED are looked at. The initial value of new_flags is calculated as follows:
After the callback functions return, the user flags are copied back from new_iflags and if the Pt_LIST_ITEM_DAMAGED flag is set in new_iflags, the item is damaged. Make sure that you set the Pt_LIST_ITEM_DAMAGED flag if you modify new_iflags in a way that requires the item to be redrawn, particularly if you have your own function attached to the Pt_ARG_TREE_COLUMN_IMGFUN resource.
These callbacks should return Pt_CONTINUE.
For an example, see “Displaying images in columns,” above.
C type | Pt type | Default |
---|---|---|
PtCallback_t * | Link | NULL |
A list of PtCallback_t structures that define the callbacks invoked when you select an item from the tree.
Each callback is passed a PtCallbackInfo_t structure that contains at least the following members:
These callbacks should return Pt_CONTINUE.
If you multi-click on a tree, its Pt_CB_TREE_SELECTION callbacks are invoked once for each click. The callback data includes the click count (1 for the first click, 2 for the second, and so on).
If you want to process only the last of a series of clicks, use a Pt_CB_RAW callback to look for a Ph_EV_BUT_RELEASE event with a subtype of Ph_EV_RELEASE_ENDCLICK. For more information, see PhEvent_t in the Photon Library Reference.
The Ph_EV_BUT_RELEASE event with a subtype of Ph_EV_RELEASE_ENDCLICK occurs approximately half a second after the click, which could be annoying to the user. Most Photon applications don't use multiple clicks because of this. |
C type | Pt type | Default |
---|---|---|
PtCallback_t * | Link | NULL |
A list of PtCallback_t structures that define the callbacks that are called before an item is expanded and after an item is collapsed. Each callback is passed a PtCallbackInfo_t structure that contains at least the following members:
These callbacks should return Pt_CONTINUE.
If the widget modifies an inherited resource, the “Default override” column indicates the new value. This modification affects any subclasses of the widget.
In addition to the flags defined by PtGenTree, the following flags are defined:
By default, neither is set. The width and location of the balloon depend on these flags:
To make the balloons appear, set one of the balloon bits (e.g. Pt_LIST_BALLOON_AS_REQUIRED) in the Pt_ARG_LIST_FLAGS resource inherited from PtGenList. |
For Pt_CB_DND callbacks for a PtTree, the cbinfo->cbdata is a pointer to a PtTreeDndCallback_t structure, containing at least the following members:
The PtTree widget defines the following structures and convenience functions that make it easier to use the tree once it's been created: