uejio@lll-crg.llnl.gov (Jeremy Y. Uejio) (02/06/90)
I'm having trouble understanding the use and access of class data for
widgets. Basically, I would like to have a function that is common to
a bunch of different widgets that are of different types. For
example, I have widgets which are subclasses of command widgets and
widgets which are subclasses of AsciiText. I would like to define
some sort of generic procedure for both types of widgets so that I can
use the same call for both. I.e.:
mycommonroutine(object,data);
where object is either the command widget or the text widget and data
is just some pointer.
I'm thinking of using the class data to hold this routine, but then I
don't know how to access it or even declare it. Maybe something like:
typedef struct _MyTextClassPart {
void (*mycommonroutine)();
} MyTextClassPart;
Can anyone help me with this?
Thanks in advance,
jeremy uejio@lll-crg.llnl.gov
Jeremy Uejio (pronounced 'oo-ay-joe') uejio@lll-crg.llnl.gov
uucp: {gatech,pyramid,rutgers}!lll-crg!uejio
other things to try: uejio%lll-crg.llnl.gov@relay.cs.net
kit@EXPO.LCS.MIT.EDU (Chris D. Peterson) (02/22/90)
> I'm having trouble understanding the use and access of class data for > widgets. Basically, I would like to have a function that is common to > a bunch of different widgets that are of different types. For > example, I have widgets which are subclasses of command widgets and > widgets which are subclasses of AsciiText. I would like to define > some sort of generic procedure for both types of widgets so that I can > nuse the same call for both. I.e.: > mycommonroutine(object,data); > where object is either the command widget or the text widget and data > is just some pointer. > I'm thinking of using the class data to hold this routine, but then I > don't know how to access it or even declare it. In each widget's Private Header file add the function definition void (*mycommonroutine)(); to the class part structure of your new subclasses. Then when you statically define your widgetclass you need to add something like the following to your widgets .c file. extern Void __CommonRoutine(); /* Tell the compiler that it really exists. */ NewWidgetClassRec newWidetClassRec = { { (WidgetClass) SuperClass, /* superclass */ . . . XtInheritQueryGeometry, /* query_geometry */ XtInheritDisplayAccelerator, /* display_accelerator */ NULL /* extension */ }, /* CoreClass fields initialization */ { XtInheritChangeSensitive /* change_sensitive */ }, /* SimpleClass fields initialization */ { 0, /* field not used */ }, /* LabelClass fields initialization */ { 0, /* field not used */ }, /* CommandClass fields initialization */ { __CommonRoutine, /* common routine used by many widgets. }, /* NewWidgetClass fields initialization */ }; The to call the routine use this code: (XtClass(w)->new_class_class.mycommonroutine) (w, data); Kind 'o yucky huh? It is not clear that this really saves anything over defining the function externally. The only time this is really useful is if you want a subclass to be able to substitute in a new routine. Otherwise you are probabally better off just using a global externa function with a unique name. The other reason to do this is if you control a common superclass. You can the make this routine static to that superclass, and all subclasses of this widget have access to that staticly defined function. Chris D. Peterson MIT X Consortium Net: kit@expo.lcs.mit.edu Phone: (617) 253 - 9608 Address: MIT - Room NE43-213