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 = j13;
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