Strange behaviour... (maybe QNX developers would like to tes

Hi all!

These days I’ve programmed a little app. This app creates ‘on the fly’ a
matrix of 26x26 PtRect widgets in a window. To every rectangle created I
attach the same callback, executed when the user clicks over these
rectangles. This callback examinates the color of the rectangle, and then
paints it black if it’s white, and viceversa.

The curious thing is that this works only if I create the PtRect widgets ‘in
descending order’.

It’s difficult to explain the meaning of this, so I include the code and
I’ll show u what’s going on.

// IniWindow: startup function for the main plain window of the app

int
IniWindow( PtWidget_t *link_instance, ApInfo_t *apinfo, PtCallbackInfo_t
*cbinfo )
{
PtArg_t args[20];
int n,i,j,k;
PhPoint_t p;
int push_rect_cb( PtWidget_t *, void *, PtCallbackInfo_t *);
PtCallback_t callbacks[] = {{push_rect_cb, NULL}};

link_instance = link_instance, apinfo = apinfo, cbinfo = cbinfo;

for (i=26;i>0;i–)
{
for (j=26;j>0;j–)


n = 0;
p.x = i13; // Coords x , y
p.y = j
13;
PtSetArg(&args[n++], Pt_ARG_ORIGIN, &p, sizeof(p));
PtSetArg(&args[n++], Pt_ARG_COLOR, Pg_BLACK, 0);
PtSetArg(&args[n++], Pt_ARG_INSIDE_COLOR, Pg_WHITE, 0);
PtSetArg(&args[n++], Pt_ARG_HEIGHT, 13, 0);
PtSetArg(&args[n++], Pt_ARG_WIDTH, 13, 0);
PtSetArg(&args[n++], Pt_ARG_FLAGS, Pt_SELECTABLE, Pt_SELECTABLE);
PtSetArg(&args[n++], Pt_CB_ACTIVATE, callbacks,
sizeof(callbacks)/sizeof(callbacks[0]));
k=i+(26*(26-j));
PtSetArg(&args[n++], Pt_ARG_USER_DATA, &k, sizeof(k));

PtCreateWidget(PtRect, link_instance, n, args);
}
}
return( Pt_CONTINUE );
}

int push_rect_cb(PtWidget_t *w, void *data, PtCallbackInfo_t *cbinfo)
{
PtArg_t args[2];
PgColor_t *color;
int n;

n=0;
PtSetArg( &args[n++], Pt_ARG_INSIDE_COLOR, &c
olor, 0 );
PtGetResources( w, n, args );

if ( *color == Pg_BLACK )
PtSetResource(w, Pt_ARG_INSIDE_COLOR, Pg_WHITE, 0);
else PtSetResource(w, Pt_ARG_INSIDE_COLOR, Pg_BLACK, 0);

return( Pt_CONTINUE );
}

As you see, I create the rectangles starting from bottom-right to top-left
direction. The first rectangle created is (26, 26), and the last one is
(1,1). This code works perfectly.

OK. Now the strange thing: If I change the double ‘for’ statement with
this…

<…>
for (i=1;i<27;i++)
{
for (j=1;j<27;j++)
<…>

…then doesn’t work! (the rectangles are created, yes, but only one of them
can be painted… a big mistery, certainly). I can’t figure why is this
happening, because the only thing that is changed here is the order where
the rectangles appear on the screen, and this should not have to affect.

Curious things… for curious programmers! I hope somebody will be able to
throw some light on this subject.

Thanks!


Jordi Garcia Busquets
Computer Engineer
University of Girona. Catalunya. Spain

I don’t know if this is the problem, but it is dangerous to use ‘n++’ in
PtSetArg() because this is often a macro
and you probably get side effects.
cheers, Peter

“Jordi Garcia” <jgbusquets@yahoo.com> schrieb im Newsbeitrag
news:abpi3j$b0$1@inn.qnx.com

Hi all!

These days I’ve programmed a little app. This app creates ‘on the fly’ a
matrix of 26x26 PtRect widgets in a window. To every rectangle created I
attach the same callback, executed when the user clicks over these
rectangles. This callback examinates the color of the rectangle, and then
paints it black if it’s white, and viceversa.

The curious thing is that this works only if I create the PtRect widgets
‘in
descending order’.

It’s difficult to explain the meaning of this, so I include the code and
I’ll show u what’s going on.

// IniWindow: startup function for the main plain window of the app

int
IniWindow( PtWidget_t *link_instance, ApInfo_t *apinfo, PtCallbackInfo_t
*cbinfo )
{
PtArg_t args[20];
int n,i,j,k;
PhPoint_t p;
int push_rect_cb( PtWidget_t *, void *, PtCallbackInfo_t *);
PtCallback_t callbacks[] = {{push_rect_cb, NULL}};

link_instance = link_instance, apinfo = apinfo, cbinfo = cbinfo;

for (i=26;i>0;i–)
{
for (j=26;j>0;j–)


n = 0;
p.x = i13; // Coords x , y
p.y = j
13;
PtSetArg(&args[n++], Pt_ARG_ORIGIN, &p, sizeof(p));
PtSetArg(&args[n++], Pt_ARG_COLOR, Pg_BLACK, 0);
PtSetArg(&args[n++], Pt_ARG_INSIDE_COLOR, Pg_WHITE, 0);
PtSetArg(&args[n++], Pt_ARG_HEIGHT, 13, 0);
PtSetArg(&args[n++], Pt_ARG_WIDTH, 13, 0);
PtSetArg(&args[n++], Pt_ARG_FLAGS, Pt_SELECTABLE,
Pt_SELECTABLE);
PtSetArg(&args[n++], Pt_CB_ACTIVATE, callbacks,
sizeof(callbacks)/sizeof(callbacks[0]));
k=i+(26*(26-j));
PtSetArg(&args[n++], Pt_ARG_USER_DATA, &k, sizeof(k));

PtCreateWidget(PtRect, link_instance, n, args);
}
}
return( Pt_CONTINUE );
}

int push_rect_cb(PtWidget_t *w, void *data, PtCallbackInfo_t *cbinfo)
{
PtArg_t args[2];
PgColor_t *color;
int n;

n=0;
PtSetArg( &args[n++], Pt_ARG_INSIDE_COLOR, &c
olor, 0 );
PtGetResources( w, n, args );

if ( *color == Pg_BLACK )
PtSetResource(w, Pt_ARG_INSIDE_COLOR, Pg_WHITE, 0);
else PtSetResource(w, Pt_ARG_INSIDE_COLOR, Pg_BLACK, 0);

return( Pt_CONTINUE );
}

As you see, I create the rectangles starting from bottom-right to top-left
direction. The first rectangle created is (26, 26), and the last one is
(1,1). This code works perfectly.

OK. Now the strange thing: If I change the double ‘for’ statement with
this…


for (i=1;i<27;i++)
{
for (j=1;j<27;j++)

…then doesn’t work! (the rectangles are created, yes, but only one of
them
can be painted… a big mistery, certainly). I can’t figure why is this
happening, because the only thing that is changed here is the order where
the rectangles appear on the screen, and this should not have to affect.

Curious things… for curious programmers! I hope somebody will be able to
throw some light on this subject.

Thanks!


Jordi Garcia Busquets
Computer Engineer
University of Girona. Catalunya. Spain

Hi Jordi,

These days I’ve programmed a little app. This app creates ‘on the fly’ a
matrix of 26x26 PtRect widgets in a window. To every rectangle created I
attach the same callback, executed when the user clicks over these
rectangles. This callback examinates the color of the rectangle, and then
paints it black if it’s white, and viceversa.

The curious thing is that this works only if I create the PtRect widgets ‘in
descending order’.

It’s difficult to explain the meaning of this, so I include the code and
I’ll show u what’s going on.

// IniWindow: startup function for the main plain window of the app

int
IniWindow( PtWidget_t *link_instance, ApInfo_t *apinfo, PtCallbackInfo_t
*cbinfo )
{
PtArg_t args[20];
int n,i,j,k;
PhPoint_t p;
int push_rect_cb( PtWidget_t *, void *, PtCallbackInfo_t *);
PtCallback_t callbacks[] = {{push_rect_cb, NULL}};

link_instance = link_instance, apinfo = apinfo, cbinfo = cbinfo;

for (i=26;i>0;i–)
{
for (j=26;j>0;j–)



n = 0;
p.x = i13; // Coords x , y
p.y = j
13;
PtSetArg(&args[n++], Pt_ARG_ORIGIN, &p, sizeof(p));
PtSetArg(&args[n++], Pt_ARG_COLOR, Pg_BLACK, 0);
PtSetArg(&args[n++], Pt_ARG_INSIDE_COLOR, Pg_WHITE, 0);
PtSetArg(&args[n++], Pt_ARG_HEIGHT, 13, 0);
PtSetArg(&args[n++], Pt_ARG_WIDTH, 13, 0);
PtSetArg(&args[n++], Pt_ARG_FLAGS, Pt_SELECTABLE, Pt_SELECTABLE);
PtSetArg(&args[n++], Pt_CB_ACTIVATE, callbacks,
sizeof(callbacks)/sizeof(callbacks[0]));
k=i+(26*(26-j));
PtSetArg(&args[n++], Pt_ARG_USER_DATA, &k, sizeof(k));

PtCreateWidget(PtRect, link_instance, n, args);
}
}
return( Pt_CONTINUE );
}

int push_rect_cb(PtWidget_t *w, void *data, PtCallbackInfo_t *cbinfo)
{
PtArg_t args[2];
PgColor_t *color;
int n;

n=0;
PtSetArg( &args[n++], Pt_ARG_INSIDE_COLOR, &c
olor, 0 );
PtGetResources( w, n, args );

if ( *color == Pg_BLACK )
PtSetResource(w, Pt_ARG_INSIDE_COLOR, Pg_WHITE, 0);
else PtSetResource(w, Pt_ARG_INSIDE_COLOR, Pg_BLACK, 0);

return( Pt_CONTINUE );
}

As you see, I create the rectangles starting from bottom-right to top-left
direction. The first rectangle created is (26, 26), and the last one is
(1,1). This code works perfectly.

OK. Now the strange thing: If I change the double ‘for’ statement with
this…


for (i=1;i<27;i++)
{
for (j=1;j<27;j++)

…then doesn’t work! (the rectangles are created, yes, but only one of them
can be painted… a big mistery, certainly). I can’t figure why is this
happening, because the only thing that is changed here is the order where
the rectangles appear on the screen, and this should not have to affect.

Curious things… for curious programmers! I hope somebody will be able to
throw some light on this subject.

The problem is you’re not setting the Pt_ARG_POS resource. By just
setting the origin each widget’s upper left position is at {0, 0}
so you have widgets overlapping each other. The order in which you
create the widgets changes the way they overlap, which is where the
strangeness comes from.

Change the PtSetArg(Pt_ARG_ORIGIN) to Pt_ARG_POS and it will work much
better. You could also look into using Control surfaces which act like
lightweight widgets.

Julian Kinraid