[Search for users] [Overall Top Noters] [List of all Conferences] [Download this site]

Conference bulova::decw_jan-89_to_nov-90

Title:DECWINDOWS 26-JAN-89 to 29-NOV-90
Notice:See 1639.0 for VMS V5.3 kit; 2043.0 for 5.4 IFT kit
Moderator:STAR::VATNE
Created:Mon Oct 30 1989
Last Modified:Mon Dec 31 1990
Last Successful Update:Fri Jun 06 1997
Number of topics:3726
Total number of notes:19516

746.0. "Passing function pointer through TAG of callback" by CURIE::ONEAL () Tue May 09 1989 11:10

This note cross-posted in the DW_EXAMPLES and DECWINDOWS notes conferences.

    I would like an example of passing a pointer to a data
       structure through the TAG of the callback structure.

The purpose to this would be to have a single callback for the entire
application that would pass a pointer to the appropriate function depending
upon the event so that that function could be executed.
This type of thing was mentioned in the DW class in Hudson,
but it was not examined further.

Thanks,

Tim

T.RTitleUserPersonal
Name
DateLines
746.1ExampleRTL::MCOLLINSTue May 09 1989 15:26184
I wasn't sure you wanted the address of a data structure or the address
of a function so this does both.




#include "DwtWidget.h"




#define CS(c)       (DwtLatin1String(c))        /* compound string converter */


#define MAKE_PUSH(parent,name,callback) \
    DwtPushButton (parent, name, 0, 0, CS (name), callback, NULL)


static ApplicationShellWidget toplevel;
static MainWindowWidget main_widget;
static DialogWidget top_dialog;

void function_one ();				/* forward dec's */
void function_two ();


typedef struct _magic				/* a data structure which is */
{						/* common to all widgets */
    char * data;
}
    MagicRec, *Magic;


MagicRec my_magic;				/* the global instance */



static void data_cb ();
static DwtCallback data_callback[] = 
{
    data_cb, NULL, 
    NULL
};

static void function_cb ();
static DwtCallback function_callback[] = 
{
    function_cb, NULL, 
    NULL
};




set_something (w, which, what)
    Widget w;
    char *which, *what;
{
    Arg al[25];
    int ac = 0;

    XtSetArg (al[0], which, what);
    XtSetValues (w, al, 1);
}

get_something (w, which, what)
    Widget w;
    char *which, *what;
{
    Arg al[25];
    int ac = 0;

    XtSetArg (al[0], which, what);
    XtGetValues (w, al, 1);
}





static Widget
build_dialog_box (parent)
    Widget parent;
{
    Widget d;

    d = DwtDialogBox (  parent,
			"dialog",
			FALSE,
			30, 100,
			CS ("dialog title"),
			DwtWorkarea,
			NULL,
			NULL);

    XtManageChild (d);

    XtManageChild (
	DwtPushButton (d, "", 0, 0, CS ("Data one"), data_callback, NULL));

    XtManageChild (
	DwtPushButton (d, "", 0, 20, CS ("Data two"), data_callback, NULL));

    function_callback[0].tag = function_one;

    XtManageChild (
	DwtPushButton (d, "", 200, 0, CS ("Function one"), function_callback, NULL));

    function_callback[0].tag = function_two;

    XtManageChild (
	DwtPushButton (d, "", 200, 20, CS ("Function two"), function_callback, NULL));

    return (d);
}





static void 
data_cb (w, tag, reason)
    Widget   w;
    caddr_t  tag;
    caddr_t  reason;
{
    Magic m = (Magic) tag;			/* retrieve global data */

    printf ("in push callback, tag = %s\n", m->data);
}



static void 
function_cb (w, tag, reason)
    Widget   w;
    caddr_t  tag;
    caddr_t  reason;
{
    VoidProc t = (VoidProc) tag;		/* make into function */

    (*t) ();
}


void
function_one ()
{
    printf ("in function one\n");
}

void
function_two ()
{
    printf ("in function two\n");
}




main (argc, argv)
    int argc;
    char **argv;
{
    my_magic.data = "real magic";		/* init global data */
    data_callback[0].tag = &my_magic;		/* put pointer in tag of callbacks */

    toplevel = XtInitialize("gt", "gt", NULL, 0, &argc, argv);

    set_something (toplevel, XtNallowShellResize, "TRUE");

    main_widget = DwtMainWindow (toplevel, "TESTING", 100, 100, 0, 0);

    XtManageChild (main_widget);

    top_dialog = build_dialog_box (main_widget);

    XtRealizeWidget (toplevel);

    XtMainLoop();
}