Bill Caroselli <Bill@sattel.com> wrote:
OK. Let me push my luck here.
Suppose I wanted to add a PtToggleButton to each list item. Can that be
done? Or am I trying to put a square peg in a round hole.
Not a PtToggleButton (list items are not widgets), but you can have
columns in your widget that display an image instead of a string and
let you toggle the image when you click. Vmail does that.
You will need to use a PtTree rather than a PtList, and it requires some
code to set it up – here’s more or less how you do it:
1
Set the Pt_LIST_COLUMN_NO_STRING flag in PtGenList column attributes
(Pt_ARG_LIST_COLUMN_ATTR) for each column that will display images.
This will prevent text from being drawn on top of images.
Remember that a tab character in an item’s string will skip over a
column flagged as Pt_LIST_COLUMN_NO_STRING. For instance: if the
second column has the flag set, you only need one tab character
between the strings for the first and the third column.
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]) );
2
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[] = {
{ NULL, 0, Pt_TREE_COLUMN_NOCB }, // This column has no images
{ 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_LIST_COLUMN_ATTR, tatrs, sizeof(tatrs) / sizeof(tatrs[0]) );
3
Optionally, write an image selector function and attach it to the
Pt_ARG_TREE_COLUMN_IMGFUN resource. This function takes a tree
item, a column index, and a column attributes structure, and returns
an index into the column’s image array (return -1 for no image).
You don’t need this function if you don’t need more than two states
per column (i.e. either two images to choose from or one image that
can be either present or absent) and you’re using an item flag to
distinguish between these two states.
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;
if ( cindex == 1 ) {
// (We don’t really need to check if cindex is one
// because we only have one column that displays images.)
// This column has three images and four possible states
// (no image is the fourth state).
// The function returns -1 for no image and 0, 1, or 2 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 );
4
Optionally, attach a Pt_CB_TREE_COLUMN_SEL callback that will both
notify you when the user clicks on a column and will allow you to
control how the item’s state changes. For instance, the following
callback will make 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;
}
\
Wojtek Lerch (wojtek@qnx.com) QNX Software Systems Ltd.