Hi,
At first I used PtSetResources() to update my PtList widget items. It works
fine, but has one major disadvantage. When I have a list long enough to be
scrolled, after every data update the list jumps to its topmost position. I
decided to check does the PtListReplaceItemPos() behaves the same way.
Unfortunately, the function doesn’t do anything (I mean anything visible) at
all. What is wrong in this example?
Thanks in advance,
Serge
/*
- With ‘-f’ option program uses PtListReplaceItemPos()function to update
list items. - By default Pt_ARG_ITEM is used.
*/
#include <Pt.h>
int use_func;
void *thread( void *data )
{
PtWidget_t *list = (PtWidget_t *) data;
PtArg_t arg[ 1 ];
int i, j;
char *pitems[ 5 ];
char items[ 5 ][ 1024 ];
char *labels[ 5 ] = {
“Line 1”,
“Line 2”,
“Line 3”,
“Line 4”,
“Line 5”
};
for( i = 0; i < 5; i++ )
pitems[ i ] = items[ i ];
for( i = 0; ; i += 5 ) {
for( j = 0; j < 5; j++ )
sprintf( pitems[ j ], “%s\t%d\t%d”, labels[ j ], i + j, i +
j + 1 );
PtEnter( 0 );
if( use_func ) {
PtListReplaceItemPos( list, (const char **) pitems, 5, 1 );
}
else {
PtSetArg( &arg[ 0 ], Pt_ARG_ITEMS, pitems, 5 );
PtSetResources( list, 1, arg );
}
PtLeave( 0 );
delay( 1000 );
}
return NULL;
}
int main( int argc, char *argv[] )
{
int i, n;
PtArg_t arg[ 5 ];
PhArea_t area = {{ 0, 0 }, { 400, 400 }};
PtWidget_t *window, *list, *divider;
PhDim_t but[ 3 ] = {{100, 5}, {50, 5}, {50, 5}};
PtListColumnAttributes_t atr[ 3 ] = {
{ Pt_LIST_COLUMN_LEFT },
{ Pt_LIST_COLUMN_RIGHT },
{ Pt_LIST_COLUMN_RIGHT }
};
while( -1 != ( n = getopt( argc, argv, “f” ))) {
switch( n ) {
case ‘f’:
use_func = 1;
break;
}
}
if( -1 == PtInit( NULL )) {
perror( “PtInit()” );
PtExit( EXIT_FAILURE );
}
n = 0;
PtSetArg( &arg[ n++ ], Pt_ARG_WINDOW_TITLE, “PtList Test”, 0 );
PtSetArg( &arg[ n++ ], Pt_ARG_AREA, &area, 0 );
if( NULL == ( window = PtCreateWidget( PtWindow, Pt_NO_PARENT, n, arg ))) {
perror( “PtCreateWidget()” );
PtExit( EXIT_FAILURE );
}
n = 0;
PtSetArg( &arg[ n++ ], Pt_ARG_AREA, &area, 0 );
PtSetArg( &arg[ n++ ], Pt_ARG_LIST_COLUMN_ATTR, atr, 3 );
PtSetArg( &arg[ n++ ], Pt_ARG_LIST_FLAGS, Pt_TRUE, Pt_LIST_NON_SELECT );
PtSetArg( &arg[ n++ ], Pt_ARG_ANCHOR_FLAGS, Pt_LEFT_ANCHORED_LEFT |
Pt_RIGHT_ANCHORED_RIGHT |
Pt_TOP_ANCHORED_TOP |
Pt_BOTTOM_ANCHORED_BOTTOM,
Pt_LEFT_ANCHORED_LEFT |
Pt_RIGHT_ANCHORED_RIGHT |
Pt_TOP_ANCHORED_TOP |
Pt_BOTTOM_ANCHORED_BOTTOM );
list = PtCreateWidget( PtList, window, n, arg );
area.size.h = but[ 0 ].h;
n = 0;
PtSetArg( &arg[ n++ ], Pt_ARG_AREA, &area, 0 );
PtSetArg( &arg[ n++ ], Pt_ARG_ANCHOR_FLAGS, Pt_LEFT_ANCHORED_LEFT |
Pt_RIGHT_ANCHORED_RIGHT |
Pt_TOP_ANCHORED_TOP |
Pt_BOTTOM_ANCHORED_TOP,
Pt_LEFT_ANCHORED_LEFT |
Pt_RIGHT_ANCHORED_RIGHT |
Pt_TOP_ANCHORED_TOP |
Pt_BOTTOM_ANCHORED_TOP );
divider = PtCreateWidget( PtDivider, list, n, arg );
for( i = 0; i < 3; i++ ) {
n = 0;
PtSetArg( &arg[ n++ ], Pt_ARG_DIM, &but[ i ], 0 );
PtSetArg( &arg[ n++ ], Pt_ARG_FLAGS, Pt_FALSE, Pt_GETS_FOCUS );
PtCreateWidget( PtButton, divider, n, arg );
}
PtRealizeWidget( window );
pthread_create( NULL, NULL, thread, list );
PtMainLoop();
return( EXIT_SUCCESS );
}