[comp.windows.x] Multiple Parameters to a Callback Function

brown@csd.uwo.ca (Mike Brown) (03/09/90)

  We would like to pass multiple parameters to a function that has been 
  registered with XtAddCallback.  Normally you can only pass one param
  which then shows up as client_data.

  We've tried passing a pointer to an array of parameters (cast to XtPointer),
  but that failed - the contents are scrambled.

  We've tried to wrestle with Action Tables, with no success.

  Are we on the right track?  There seem to be no examples in the documentation
  or the sample code.  Any pointers to solutions or running chunks of
code would
  be appreciated.   Our current solution of global variables is NOT
satisfactory.

------------------------------------------------------------------------------

Mike Brown, Gradual Student in Computer Science, University of Western Ontario

brown@csd.uwo.ca      "Another day dulling the leading edge of technology...."

klee@wsl.dec.com (Ken Lee) (03/09/90)

In article <186@ria.ccs.uwo.ca>, brown@csd.uwo.ca (Mike Brown) writes:
>   We've tried passing a pointer to an array of parameters (cast to
XtPointer),
>   but that failed - the contents are scrambled.

This should work.  Make sure you don't deallocate the structure before
the callback is called.

Ken Lee
DEC Western Software Laboratory, Palo Alto, Calif.
Internet: klee@wsl.dec.com
uucp: uunet!decwrl!klee

ben@hpcvlx.cv.hp.com (Benjamin Ellsworth) (03/10/90)

> We've tried passing a pointer to an array of parameters (cast to 
> XtPointer), but that failed - the contents are scrambled.

This will work.  Something else must be wrong.  Is your pointer
pointing onto the stack (a non static variable local to a procedure),
and then the procedure exits before the callback is called?




------------------
Benjamin Ellsworth
ben@cv.hp.com

bjaspan@athena.mit.edu (Barr3y Jaspan) (03/13/90)

In article <186@ria.ccs.uwo.ca>, brown@csd.uwo.ca (Mike Brown) writes:
> 
>   We would like to pass multiple parameters to a function that has been 
>   registered with XtAddCallback.  Normally you can only pass one param
>   which then shows up as client_data.
> 
>   We've tried passing a pointer to an array of parameters (cast to
XtPointer),
>   but that failed - the contents are scrambled.

I sounds like you are doing something wrong.  I've passed pointers to allocated
structures as client_data to a callback before, and it worked perfectly --
after I very carefully made sure I was dereferencing everything in exactly
the right way.

Something like this should work:

{
    random_type *data;

    data = (random_type *) malloc(n*sizeof(random_type));
    magically_fill_in_array(data);

    XtAddCallback(widget, XtNcallback, cb_function, data);
}

void cb_function(w, call_data, client_data)
   Widget w;
   caddr_t *call_data, *client_data;
{
   print_random_type(((random_type *) client_data)[0]);
}

Note that I just typed this off the top of my head.  The point is that you
can pass ANY 4-byte value as client_data, even the address of an array.

Barry Jaspan, MIT-Project Athena
bjaspan@athena.mit.edu