PtTree Troubles

i am trying to load 3 images into my PtTree widget but the program keeps
crashing in the include code segment. i have been unable to locate any
examples using the PtTree widget so if you could look at what i have and
tell me where i have gone wrong it would be greatly appreciated.

thanks,
ross


code segment:

int
init_tree( PtWidget_t *widget, ApInfo_t *apinfo, PtCallbackInfo_t *cbinfo )
{
ApDBase_t *db;
int i;

db = ApOpenDBase( ABM_dbase );

for( i = 0; i < 3; i++ )
{
PhImage_t *img;
char b[20];

sprintf( b, “image_%i”, i );

img = ApGetImageRes( db, b );
printf( “image_%i loaded\n”, i );
PtTreeAddImages( ABW_tree_widget, img, 1 ) );
}

ApCloseDBase( db );

/* eliminate ‘unreferenced’ warnings */
widget = widget, apinfo = apinfo, cbinfo = cbinfo;

return( Pt_CONTINUE );
}



output:

image_o loaded

file://1/home/progs/example/src/default/example terminated (SIGSEGV) at
0007:000B258C.
%1 1505 Memory fault ./example

Ross Brantner <brantner@nrc.net> wrote:
: i am trying to load 3 images into my PtTree widget but the program keeps
: crashing in the include code segment. i have been unable to locate any
: examples using the PtTree widget so if you could look at what i have and
: tell me where i have gone wrong it would be greatly appreciated.

Don’t close the widget database while you’re still using the images.
Here’s what the docs for ApGetImageRes() say:


This function returns a pointer into the widget database; don’t
close the database while still using the image. If you must close
the widget database, do the following first:

  1. Copy the image data and palette.

  2. Update the PhImage_t structure to point to the new copies
    of the data.


    Here’s an example from the QNX 6 docs for PtTree. I think it will work
    in Photon 1.14 as well:

Using images in tree items

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.

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);


Steve Reid stever@qnx.com
TechPubs (Technical Publications)
QNX Software Systems