home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Usenet 1994 October
/
usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso
/
x
/
volume10
/
xt-examples
/
part04
/
Graph.c
< prev
next >
Wrap
C/C++ Source or Header
|
1990-11-04
|
11KB
|
353 lines
/***********************************************************
Copyright 1990 by Digital Equipment Corporation, Maynard, Massachusetts.
All Rights Reserved
Permission to use, copy, modify, and distribute these examples for any
purpose and without fee is hereby granted, provided that the above
copyright notice appear in all copies and that both that copyright
notice and this permission notice appear in supporting documentation,
and that the name of Digital not be used in advertising or publicity
pertaining to distribution of the software without specific, written
prior permission.
DIGITAL AND THE AUTHORS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS, IN NO EVENT SHALL DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT
OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE
OR PERFORMANCE OF THIS SOFTWARE.
******************************************************************/
#include <X11/IntrinsicP.h> /* Intrinsics header file */
#include <X11/StringDefs.h> /* Resource string definitions */
#include "GraphP.h" /* Graph private header file */
#include "GraphDispP.h" /* Graph display object */
#define Offset(field) XtOffsetOf(GraphRec, graph.field)
static XtResource resources[] = {
{XtNnumEntries, XtCNumEntries, XtRInt, sizeof(int),
Offset(num_entries), XtRImmediate, (XtPointer) 0},
{XtNlabels, XtCLabels, XtRStringTable, sizeof(String *),
Offset(labels), XtRImmediate, (XtPointer) NULL},
{XtNvalues, XtCValues, XtRPointer, sizeof(int *),
Offset(values), XtRImmediate, (XtPointer) NULL},
{XtNmaxValue, XtCMaxValue, XtRInt, sizeof(int),
Offset(max_value), XtRImmediate, (XtPointer) 100},
{XtNscale, XtCScale, XtRInt, sizeof(int),
Offset(scale), XtRImmediate, (XtPointer) 1}
};
#undef Offset
/* Forward declarations */
static void ClassInitialize(), Initialize(),
Redisplay(), Destroy(), Resize(), Realize(),
InsertChild();
static Boolean SetValues();
static CompositeClassExtensionRec compositeExtension = {
/* next_extension */ NULL,
/* record_type */ NULLQUARK,
/* version */ XtCompositeExtensionVersion,
/* record_size */ sizeof(CompositeClassExtensionRec),
/* accepts_objects */ TRUE
};
GraphClassRec graphClassRec = {
/* Core class part */
{
/* superclass */ (WidgetClass) &compositeClassRec,
/* class_name */ "Graph",
/* widget_size */ sizeof(GraphRec),
/* class_initialize */ ClassInitialize,
/* class_part_initialize */ NULL,
/* class_inited */ FALSE,
/* initialize */ Initialize,
/* initialize_hook */ NULL,
/* realize */ Realize,
/* actions */ NULL,
/* num_actions */ 0,
/* resources */ resources,
/* num_resources */ XtNumber(resources),
/* xrm_class */ NULLQUARK,
/* compress_motion */ TRUE,
/* compress_exposure */ XtExposeCompressMultiple,
/* compress_enterleave */ TRUE,
/* visible_interest */ FALSE,
/* destroy */ Destroy,
/* resize */ Resize,
/* expose */ Redisplay,
/* set_values */ SetValues,
/* set_values_hook */ NULL,
/* set_values_almost */ XtInheritSetValuesAlmost,
/* get_values_hook */ NULL,
/* accept_focus */ NULL,
/* version */ XtVersion,
/* callback offsets */ NULL,
/* tm_table */ NULL,
/* query_geometry */ NULL,
/* display_accelerator */ NULL,
/* extension */ NULL
},
/* Composite class part */
{
/* geometry_manager */ NULL,
/* change_managed */ NULL,
/* insert_child */ InsertChild,
/* delete_child */ XtInheritDeleteChild,
/* extension */ (XtPointer) &compositeExtension,
},
/* Graph class part */
{
/* extension */ NULL
}
};
/* Class record pointer */
WidgetClass graphWidgetClass = (WidgetClass) &graphClassRec;
static Boolean CvtStringToStringList(dpy, args, num_args,
from, to, data)
Display *dpy;
XrmValuePtr args;
Cardinal *num_args;
XrmValuePtr from, to;
XtPointer *data;
{
register int i, count = 1;
register char *ch, *start = from->addr;
static String *list;
int len;
if (*num_args != 0) {
XtAppErrorMsg(XtDisplayToApplicationContext(dpy),
"cvtStringToStringList", "wrongParameters",
"XtToolkitError",
"String to string list conversion needs no extra arguments",
(String *) NULL, (Cardinal *) NULL);
}
if (to->addr != NULL && to->size < sizeof(String *)) {
to->size = sizeof(String *);
return FALSE;
}
if (start == NULL || *start == '\0') list = NULL;
else {
for (ch = start; *ch != '\0'; ch++) { /* Count strings */
if (*ch == '\n') count++;
}
list = (String *) XtCalloc(count+1, sizeof(String));
for (i = 0; i < count; i++) {
for (ch = start; *ch != '\n' && *ch != '\0'; ch++) {}
len = ch - start;
list[i] = XtMalloc(len + 1);
(void) strncpy(list[i], start, len);
list[i][len] = '\0';
start = ch + 1;
}
}
if (to->addr == NULL) to->addr = (caddr_t) &list;
else *(String **) to->addr = list;
to->size = sizeof(String *);
return TRUE;
}
static void StringListDestructor(app, to, converter_data,
args, num_args)
XtAppContext app;
XrmValuePtr to;
XtPointer converter_data;
XrmValuePtr args;
Cardinal *num_args;
{
String *list = (String *) to->addr;
register String *entry;
if (list == NULL) return;
for (entry = list; entry != NULL; entry++) {
XtFree((XtPointer) entry);
}
XtFree((XtPointer) list);
}
static void ClassInitialize()
{
/* Register a converter for string to string list */
XtSetTypeConverter(XtRString, XtRStringTable,
CvtStringToStringList, (XtConvertArgList) NULL, 0,
XtCacheAll | XtCacheRefCount, StringListDestructor);
}
static void Initialize(request, new, args, num_args)
Widget request, new;
ArgList args;
Cardinal *num_args;
{
register GraphWidget gw = (GraphWidget) new;
int *values;
register int i;
String label;
/* Copy the values */
values = (int *) XtCalloc(gw->graph.num_entries, sizeof(int));
for (i = 0; i < gw->graph.num_entries; i++) {
values[i] = gw->graph.values[i];
}
gw->graph.values = values;
/* If labels is not NULL, make sure there are enough of them now
to avoid an error some random other place. */
if (gw->graph.labels != NULL) {
for (i = 0; i < gw->graph.num_entries; i++) {
label = gw->graph.labels[i];
}
}
}
static Boolean SetValues(old, req, new, args, num_args)
Widget old, req, new;
ArgList args;
Cardinal *num_args;
{
register GraphWidget oldgraph = (GraphWidget) old;
register GraphWidget newgraph = (GraphWidget) new;
int *values;
String label;
register int i;
#define NE(field) (newgraph->graph.field != oldgraph->graph.field)
#define EQ(field) (newgraph->graph.field == oldgraph->graph.field)
if (NE(values)) {
values = (int *) XtCalloc(newgraph->graph.num_entries,
sizeof(int));
XtFree(oldgraph->graph.values);
for (i = 0; i < newgraph->graph.num_entries; i++) {
values[i] = newgraph->graph.values[i];
}
newgraph->graph.values = values;
return TRUE;
}
/* If num_entries changed but not the labels or values,
something's wrong */
if (NE(num_entries) && (EQ(labels) || EQ(values))) {
XtAppErrorMsg(XtWidgetToApplicationContext(new),
"countError", "numEntries", "WidgetError",
"Number of graph entries changed but not labels or values",
(String *) NULL, (Cardinal *) NULL);
}
if (NE(labels) && newgraph->graph.labels != NULL) {
for (i = 0; i < newgraph->graph.num_entries; i++) {
label = newgraph->graph.labels[i];
}
}
return NE(num_entries) || NE(labels) || NE(max_value);
#undef NE
}
static void Destroy(w)
Widget w;
{
GraphWidget gw = (GraphWidget) w;
XtFree((char *) gw->graph.values);
}
static void InsertChild(w)
Widget w;
{
String params[2];
Cardinal num_params;
CompositeWidget parent = (CompositeWidget) XtParent(w);
GraphDisplayObjectClass childClass;
if (!XtIsSubclass(w, graphDisplayObjectClass)) {
params[0] = XtClass(w)->core_class.class_name;
params[1] = XtClass(parent)->core_class.class_name;
num_params = 2;
XtAppErrorMsg(XtWidgetToApplicationContext(w),
"childError", "class", "WidgetError",
"Children of class %s cannot be added to %n widgets",
params, &num_params);
}
if (parent->composite.num_children != 0) {
params[0] = XtClass(parent)->core_class.class_name;
num_params = 1;
XtAppErrorMsg(XtWidgetToApplicationContext(w),
"childError", "number", "WidgetError",
"%s widgets can only take one child",
params, &num_params);
}
(*((CompositeWidgetClass)(graphWidgetClass->
core_class.superclass))->composite_class.insert_child) (w);
/* Give the child a chance to compute our dimensions */
childClass = (GraphDisplayObjectClass) XtClass(w);
if (childClass->graphDisplay_class.compute_size != NULL) {
(*childClass->graphDisplay_class.compute_size) (parent);
}
}
static void Realize(w, valueMask, attributes)
Widget w;
XtValueMask *valueMask;
XSetWindowAttributes *attributes;
{
GraphWidget gw = (GraphWidget) w;
String params[2];
Cardinal num_params;
if (gw->composite.num_children != 1) {
params[0] = XtClass(w)->core_class.class_name;
num_params = 1;
XtAppErrorMsg(XtWidgetToApplicationContext(w),
"childError", "number", "WidgetError",
"%s widgets must have exactly one child",
params, &num_params);
}
(*graphWidgetClass->core_class.superclass->core_class.realize)
(w, valueMask, attributes);
}
static void Redisplay(w, event, region)
Widget w;
XEvent *event;
Region region;
{
GraphWidget gw = (GraphWidget) w;
GraphDisplayObject d =
(GraphDisplayObject) gw->composite.children[0];
GraphDisplayObjectClass childClass;
childClass = (GraphDisplayObjectClass) XtClass((Widget) d);
if (childClass->graphDisplay_class.expose != NULL) {
(*childClass->graphDisplay_class.expose) (w, event, region);
}
}
static void Resize(w)
Widget w;
{
/* If widget is realized, clear and redisplay */
if (XtIsRealized(w)) {
XClearWindow(XtDisplay(w), XtWindow(w));
(*(XtClass(w)->core_class.expose))(w,
(XEvent *) NULL, (Region) NULL);
}
}