home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!stanford.edu!morrow.stanford.edu!sep!steve
- From: steve@sep.Stanford.EDU (Steve Cole)
- Newsgroups: alt.sources
- Subject: xtpanel 2.0 - interactive program builder - part 10/10
- Followup-To: alt.sources.d
- Date: 21 Nov 1992 00:35:30 GMT
- Organization: Stanford Exploration Project
- Lines: 686
- Distribution: world
- Message-ID: <1ek08iINN194@morrow.stanford.edu>
- NNTP-Posting-Host: taal.stanford.edu
-
-
- Submitted-by: steve@sep.Stanford.EDU
- Archive-name: xtpanel/part10
-
- #!/bin/sh
- # This is part 10 of a multipart archive
- # ============= xtpanel/object.c ==============
- if test ! -d 'xtpanel'; then
- echo 'x - creating directory xtpanel'
- mkdir 'xtpanel'
- fi
- if test -f 'xtpanel/object.c' -a X"$1" != X"-c"; then
- echo 'x - skipping xtpanel/object.c (File already exists)'
- else
- echo 'x - extracting xtpanel/object.c (Text)'
- sed 's/^X//' << 'SHAR_EOF' > 'xtpanel/object.c' &&
- /*
- X * Copyright 1992 the Board of Trustees of the Leland Stanford Junior
- X * University. Official permission to use this software is included in
- X * the documentation. It authorizes you to use this file for any
- X * non-commercial purpose, provided that this copyright notice is not
- X * removed and that any modifications made to this file are commented
- X * and dated in the style of the example below.
- X */
- X
- /*
- X *
- X * source file: ./xtpanel/object.c
- X *
- X * Steve Cole, Dave Nichols (SEP), August 31 1992
- X * Inserted this sample edit history entry.
- X * Please log any further modifications made to this file:
- X * Steve Cole, Dave Nichols (SEP), November 20 1992 - version 2.00
- X * 1) added new objects: toggle, scrollbar, graph.
- X * 2) added new actions: ASSIGN, SET.
- X * 3) objects can have multiple actions.
- X * 4) backquoted strings in actions get executed at action time.
- X */
- X
- #include <stdio.h>
- X
- #include <X11/Xatom.h>
- #include <X11/Intrinsic.h>
- X
- #include <X11/Xaw/Label.h>
- X
- #include "object.h"
- #include "tree.h"
- X
- static Objdef* topobj=0;
- X
- /*
- X * Function name: new_object
- X * Description: creates a structure for a new object
- X * Arguments: none
- X * Returns: pointer to new structure of type objdef
- X */
- Objdef* new_object()
- {
- X Objdef *a, *b;
- X
- X if( topobj == (Objdef*)0 ){
- X /* make the head of the list */
- X topobj = (Objdef*) malloc( sizeof( Objdef ) );
- X topobj->name = strdupl("top");
- X topobj->action = (action *) 0;
- X topobj->value = (char *) 0;
- X topobj->widgetname = (Widget) 0;
- X topobj->info = (void *) 0;
- X topobj->updater = (void *) 0;
- X topobj->next = (Objdef*)0;
- X }
- X
- X a = (Objdef*) malloc( sizeof( Objdef ) );
- X a->name= (char *)0;
- X a->action= (action *)0;
- X a->value= (char *)0;
- X a->widgetname= (Widget)0;
- X a->info= (void *)0;
- X a->updater= (void *)0;
- X a->next= (Objdef*)0;
- X
- X /* find the tail of the list */
- X for( b=topobj; b->next != (Objdef*)0; b = b->next );
- X
- X b->next = a;
- X
- X return a;
- }
- X
- /*
- X * Function name: find_by_name
- X * Description: loop over all objects, find the one with
- X * the right name.
- X * Arguments: name - name to match
- X * Returns: pointer to object structure for matching object
- X */
- Objdef* find_by_name(name)
- X char* name;
- {
- X int iobj;
- X Objdef* obj;
- X
- X /* loop over objects */
- X for ( obj=topobj; obj != (Objdef*) 0; obj=obj->next )
- X {
- X if (!strcmp(obj->name,name)) {
- X return obj;
- X }
- X }
- X
- X fprintf(stderr,"find_by_name cannot find object %s\n",name);
- X exit(-1);
- }
- X
- /*
- X * Function name: find_widget
- X * Description: loop over all objects, find the one with
- X * the right widget name.
- X * Arguments: w - widget id to match
- X * Returns: pointer to object structure for matching object
- X */
- Objdef*
- X find_by_widget(w)
- Widget w;
- {
- X Objdef* obj;
- X
- X /* loop over objects */
- X for ( obj=topobj; obj != (Objdef*) 0; obj=obj->next )
- X {
- X if (obj->widgetname == w) {
- X return obj;
- X }
- X }
- X
- X fprintf(stderr,"find_by_widget cannot find widget %d\n", (int)w );
- X exit(-1);
- }
- X
- /*
- X * Function name: get_string
- X * Description: find the value of an object, given its name
- X * Arguments: name - object name
- X * Returns: pointer to string containing objects value
- X */
- char*
- X get_string(name)
- char* name;
- {
- char * val;
- X if( (val = find_by_name(name)->value) == (char*) 0 ){
- X return(strdupl(""));
- X }else{
- X return(strdupl(val));
- X }
- }
- X
- /*
- X * Function name: set_string
- X * Description: set the value of an object
- X * Arguments: name - object name
- X * string - pointer to string containing new object value
- X * Returns: none
- X */
- void set_string(name,string)
- X char* name,*string;
- {
- X Objdef *obj;
- X obj = find_by_name(name);
- X if( obj->value != (char*)0 ) free(obj->value);
- X obj->value = strdupl(string);
- }
- X
- /*
- X * Function name: all_actions
- X * Description: do the actions of all objects whose action type
- X * is STRING. This makes sure defaults are set.
- X * Arguments: none
- X * Returns: none
- X */
- void all_actions()
- {
- X Objdef* obj;
- X
- X if( topobj == (Objdef*)0 ) return;
- X
- X /* loop over objects, skip the top one */
- X for ( obj=topobj->next; obj != (Objdef*) 0; obj=obj->next )
- X {
- X if (obj->action != (action *) 0) {
- X perform_actions(obj->name,obj->action,0);
- X }
- X }
- }
- X
- /*
- X * Function name: update_object
- X * Description: update the value of an object
- X * Arguments: name - object name
- X * string - pointer to string containing new object value
- X * Returns: none
- X */
- void update_object(name,string)
- X char* name,*string;
- {
- X Objdef *object;
- X
- X object = find_by_name(name);
- X
- X if( object->value != (char*)0 ) free(object->value);
- X object->value = strdupl(string);
- X
- X if( object->updater != 0 ) object->updater(object,string);
- }
- X
- /*
- X * Function name: update_tag
- X * Description: update the value of an object's tag
- X * Arguments: name - object name
- X * value - pointer to string containing new object value
- X * Returns: none
- X */
- void update_tag(name,tag,value)
- X char* name,*tag,*value;
- {
- X Objdef *object;
- X Arg args[20];
- X int narg;
- X
- X object = find_by_name(name);
- X
- X narg = 0;
- X SetTag(object->widgetname,args,&narg,tag,value);
- X XtSetValues(object->widgetname,args,narg);
- }
- SHAR_EOF
- chmod 0664 xtpanel/object.c ||
- echo 'restore of xtpanel/object.c failed'
- Wc_c="`wc -c < 'xtpanel/object.c'`"
- test 5219 -eq "$Wc_c" ||
- echo 'xtpanel/object.c: original size 5219, current size' "$Wc_c"
- fi
- # ============= xtpanel/graph.c ==============
- if test -f 'xtpanel/graph.c' -a X"$1" != X"-c"; then
- echo 'x - skipping xtpanel/graph.c (File already exists)'
- else
- echo 'x - extracting xtpanel/graph.c (Text)'
- sed 's/^X//' << 'SHAR_EOF' > 'xtpanel/graph.c' &&
- /*
- X * Copyright 1992 the Board of Trustees of the Leland Stanford Junior
- X * University. Official permission to use this software is included in
- X * the documentation. It authorizes you to use this file for any
- X * non-commercial purpose, provided that this copyright notice is not
- X * removed and that any modifications made to this file are commented
- X * and dated in the style of the example below.
- X */
- X
- /*
- X *
- X * source file: ./xtpanel/graph.c
- X *
- X * Steve Cole, Dave Nichols (SEP), August 28 1992
- X * Inserted this sample edit history entry.
- X * Please log any further modifications made to this file:
- X * Steve Cole, Dave Nichols (SEP), November 20 1992 - version 2.00
- X * 1) added new objects: toggle, scrollbar, graph.
- X * 2) added new actions: ASSIGN, SET.
- X * 3) objects can have multiple actions.
- X * 4) backquoted strings in actions get executed at action time.
- X */
- X
- #include <X11/Intrinsic.h>
- #include <X11/StringDefs.h>
- X
- #include <X11/Xaw/Box.h>
- #include <X11/Xaw/Cardinals.h>
- #include <X11/Xaw/Command.h>
- #include <X11/Xaw/Label.h>
- #include <X11/Xaw/Scrollbar.h>
- X
- #include "object.h"
- #include "tree.h"
- #include "builders.h"
- #include "string_buf.h"
- X
- #include <stdio.h>
- #include <string.h>
- X
- typedef struct graphinfo {
- X float minval;
- X float maxval;
- X char* format;
- X int nsamp;
- X Widget *widgets;
- } _ginfo;
- X
- /* translation table used for graph widget */
- char graph_trans[] =
- X "<Leave>: update_graph()";
- X
- extern void graph_callback();
- extern void graph_scroll_callback();
- extern void graph_update();
- extern void parse_valuestring();
- X
- void build_graph(root,parent)
- X entry *root;
- X Widget parent;
- {
- X Objdef *object;
- X entry *curr;
- X char* label;
- X struct graphinfo *graph_info;
- X Arg args[10];
- X int narg;
- X Widget box, scrollbar, box2, button, vlabel, labl;
- X float valmin,valmax,val,top;
- X float *values;
- X char *valstring;
- X int nsamp,isamp;
- X char text[100];
- X int width,height;
- X XtOrientation orient;
- X char defname[12];
- X static int numgraph=1;
- X
- X /* graph gets its own box */
- X narg = 0;
- X common_tags(root,args,&narg,SET_BG|SET_BORDER|SET_ORIENT);
- X box = XtCreateManagedWidget("graphbox",boxWidgetClass,parent,args,narg);
- X
- X /* find box orientation - used below to set scrollbar orientation */
- X narg = 0;
- X XtSetArg(args[narg], XtNorientation, &orient); narg++;
- X XtGetValues( box, args, narg );
- X
- X /* create new object */
- X object = new_object();
- X
- X /* construct default graph name */
- X sprintf(defname,"graph%d",numgraph++);
- X
- X /* find name, action, nsamp in tree */
- X object->name = get_value(root,"name",defname);
- X label = get_value(root,"label",object->name);
- X object->action = parse_actions(object->name,root);
- X graph_info = (struct graphinfo*) malloc( sizeof( struct graphinfo ) );
- X object->info = graph_info;
- X graph_info->minval = ((float) atof(get_value(root,"min","0")));
- X graph_info->maxval = ((float) atof(get_value(root,"max","1")));
- X graph_info->nsamp = ((int) atoi(get_value(root,"nsamp","1")));
- X
- X /* label to indicate minimum value */
- X narg = 0;
- X XtSetArg(args[narg], XtNlabel, get_value(root,"min","0")); narg++;
- X vlabel = XtCreateManagedWidget(object->name,labelWidgetClass,
- X box,args,narg);
- X
- X /* the graph label */
- X narg = 0;
- X XtSetArg(args[narg], XtNborderWidth, 0); narg++;
- X XtSetArg(args[narg], XtNlabel, label); narg++;
- X
- X labl = XtCreateManagedWidget(object->name,labelWidgetClass,box,
- X args,narg);
- X
- X /* inner box holds the scrollbars*/
- X narg = 0;
- X common_tags(root,args,&narg,SET_BG|SET_BORDER);
- X /* orient must be the opposite of that of the box */
- X if (orient == XtorientHorizontal) {
- X XtSetArg(args[narg], XtNorientation, XtorientVertical); narg++;
- X } else {
- X XtSetArg(args[narg], XtNorientation, XtorientHorizontal); narg++;
- X }
- X box2 = XtCreateManagedWidget("samplebox",boxWidgetClass,box,args,narg);
- X
- X /* use translations to update value whenever mouse leaves the box */
- X XtAugmentTranslations(box2,XtParseTranslationTable(graph_trans));
- X
- X /* label to indicate maximum value */
- X narg = 0;
- X XtSetArg(args[narg], XtNlabel, get_value(root,"max","1")); narg++;
- X vlabel = XtCreateManagedWidget(object->name,labelWidgetClass,
- X box,args,narg);
- X
- X /* allocate space to hold slider values and widget names */
- X values = (float*) malloc ( graph_info->nsamp * sizeof(float));
- X graph_info->widgets = (Widget*)
- X malloc ( graph_info->nsamp * sizeof(Widget));
- X
- X /* get the string containing default values */
- X valstring = get_value(root,"value","");
- X
- X /* decode */
- X parse_valuestring(valstring,values,graph_info->nsamp," \n\t");
- X
- X graph_info->format = get_value(root,"format","%f");
- X height = (int) atoi(get_value(root,"height","5"));
- X width = (int) atoi(get_value(root,"width","100"));
- X
- X /* loop over sliders */
- X for (isamp=0; isamp<graph_info->nsamp; isamp++) {
- X
- X /* determine the correct starting point for the sliders */
- X narg = 0;
- X top = values[isamp]/graph_info->maxval;
- X if (sizeof(float) > sizeof(XtArgVal))
- X {
- X XtSetArg(args[narg], XtNtopOfThumb, top); narg++;
- X }
- X else
- X {
- X XtArgVal * l_top = (XtArgVal *) ⊤
- X XtSetArg(args[narg], XtNtopOfThumb, *l_top); narg++;
- X }
- X
- X /* common parameters */
- X common_tags(root,args,&narg,SET_FG|SET_BG);
- X
- X /* height, width */
- X /* done here instead of common_tags so we could have defaults */
- X XtSetArg(args[narg], XtNlength, width); narg++;
- X XtSetArg(args[narg], XtNthickness, height); narg++;
- X /* turn off border */
- X XtSetArg(args[narg], XtNborderWidth, 0); narg++;
- X /* same orientation as outer box */
- X XtSetArg(args[narg], XtNorientation, orient); narg++;
- X
- X /* slider is actually an athena scrollbar widget */
- X scrollbar = XtCreateManagedWidget(object->name,scrollbarWidgetClass,
- X box2,args,narg);
- X
- X /* scroll callback is for incremental scrolling with left and
- X right buttons */
- X XtAddCallback( scrollbar, XtNscrollProc, graph_scroll_callback,
- X (XtPointer) object );
- X
- X /* set to the correct starting point */
- X (void) XawScrollbarSetThumb(scrollbar,
- X (values[isamp]-graph_info->minval)/
- X (graph_info->maxval-graph_info->minval),-1.);
- X
- X graph_info->widgets[isamp] = scrollbar;
- X }
- X
- X object->value = strdupl(valstring);
- X object->widgetname = box2;
- X object->updater = graph_update;
- X
- }
- X
- void
- X graph_scroll_callback(widget, client_data, pos_ptr)
- Widget widget;
- XXtPointer client_data, pos_ptr;
- {
- X Arg arg[1];
- X float top;
- X int pos;
- X pos = (int) pos_ptr;
- X /* get the current position of the graph */
- X XtSetArg( arg[0], XtNtopOfThumb, &top );
- X XtGetValues( widget, arg, ONE );
- X /* now compute new position - 5% change */
- X top -= pos/abs(pos) * 0.05;
- X if (top > 1.) top = 1.;
- X if (top < 0.) top = 0.;
- X /* update the graph */
- X if (sizeof(float) > sizeof(XtArgVal))
- X {
- X XtSetArg(arg[0], XtNtopOfThumb, top);
- X }
- X else
- X {
- X XtArgVal * l_top = (XtArgVal *) ⊤
- X XtSetArg(arg[0], XtNtopOfThumb, *l_top);
- X }
- X XtSetValues( widget, arg, ONE );
- }
- X
- void
- X graph_update(object, value)
- Objdef *object;
- char *value;
- {
- X struct graphinfo *graph_info;
- X Arg arg[1];
- X char text[10];
- X float top,val;
- X Dimension len;
- X int pos;
- X float *values;
- X int isamp;
- X
- X graph_info = (struct graphinfo *) object->info;
- X values = (float*) malloc ( graph_info->nsamp * sizeof(float));
- X
- X /* update value string */
- X object->value = strdupl(value);
- X
- X /* decode */
- X parse_valuestring(value,values,graph_info->nsamp," \n\t");
- X
- X /* loop over sliders */
- X for (isamp=0; isamp<graph_info->nsamp; isamp++) {
- X
- X top = (values[isamp] - graph_info->minval)/
- X (graph_info->maxval-graph_info->minval);
- X if (top > 1.) top = 1.;
- X if (top < 0.) top = 0.;
- X /* compute new value */
- X sprintf( text, graph_info->format,
- X (float) graph_info->minval +
- X top*(graph_info->maxval - graph_info->minval));
- X /* update the graph */
- X if (sizeof(float) > sizeof(XtArgVal))
- X {
- X XtSetArg(arg[0], XtNtopOfThumb, top);
- X }
- X else
- X {
- X XtArgVal * l_top = (XtArgVal *) ⊤
- X XtSetArg(arg[0], XtNtopOfThumb, *l_top);
- X }
- X XtSetValues( graph_info->widgets[isamp], arg, ONE );
- X }
- X perform_actions(object->name,object->action,0);
- }
- X
- void parse_valuestring(valstring,values,nsamp,separator)
- X char *valstring;
- X float *values;
- X int nsamp;
- X char *separator;
- {
- X char *item;
- X int isamp,jsamp;
- X
- X for( item=strtok(valstring,separator),isamp=0;
- X item != (char*)0 && isamp <nsamp ;
- X item =strtok( (char*)0, separator ),isamp++){
- X sscanf(item,"%f",&values[isamp]);
- X }
- X
- X /* if valstring does not contain enough samples, reuse the last one */
- X if (isamp < nsamp) {
- X for (jsamp=isamp; jsamp<nsamp; jsamp++) {
- X values[jsamp] = values[isamp-1];
- X }
- X }
- }
- X
- /* callback used when graph button is pressed or cursor leaves graph */
- void
- graph_callback(widget, client_data, callData)
- Widget widget;
- XXtPointer client_data, callData;
- {
- X Objdef *object;
- X struct graphinfo *graph_info;
- X Arg arg[1];
- X char text[64];
- X float top;
- X Dimension len;
- X int isamp;
- X char *valstring;
- X string_buf *buffer;
- X
- X object = (Objdef *) client_data;
- X graph_info = (struct graphinfo *) object->info;
- X
- X /* loop over widgets */
- X strcpy(object->value,"");
- X buffer = buf_start();
- X
- X for (isamp=0; isamp<graph_info->nsamp; isamp++) {
- X
- X /* get the current position of the graph */
- X XtSetArg( arg[0], XtNtopOfThumb, &top );
- X XtGetValues( graph_info->widgets[isamp], arg, ONE );
- X
- X /* turn it into text, add to value string */
- X sprintf( text, graph_info->format,
- X (float) graph_info->minval +
- X top*(graph_info->maxval - graph_info->minval));
- X buf_cat( buffer,text,strlen(text));
- X buf_cat( buffer," ",1);
- X }
- X object->value = buf_fetch(buffer);
- /* buf_free(buffer);*/
- X
- X perform_actions(object->name,object->action,1);
- }
- X
- /*
- X * this routine gets called whenever we leave
- X * a graph box, to be sure the graph's value is up to date
- X */
- void graph_update_callback( w, event, params, num_params)
- X Widget w;
- X XEvent *event;
- X String *params;
- X Cardinal *num_params;
- {
- X Objdef* object;
- X struct graphinfo *graph_info;
- X char *value;
- X Arg arg[1];
- X int narg;
- X int isamp;
- X float top;
- X char text[64];
- X Dimension len;
- X char *valstring;
- X string_buf *buffer;
- X
- X object = find_by_widget(w);
- X graph_info = (struct graphinfo *) object->info;
- X
- X /* loop over widgets */
- X strcpy(object->value,"");
- X buffer = buf_start();
- X
- X for (isamp=0; isamp<graph_info->nsamp; isamp++) {
- X
- X /* get the current position of the graph */
- X XtSetArg( arg[0], XtNtopOfThumb, &top );
- X XtGetValues( graph_info->widgets[isamp], arg, ONE );
- X
- X /* turn it into text, add to value string */
- X sprintf( text, graph_info->format,
- X (float) graph_info->minval +
- X top*(graph_info->maxval - graph_info->minval));
- X buf_cat( buffer,text,strlen(text));
- X buf_cat( buffer," ",1);
- X }
- X object->value = buf_fetch(buffer);
- X
- }
- SHAR_EOF
- chmod 0664 xtpanel/graph.c ||
- echo 'restore of xtpanel/graph.c failed'
- Wc_c="`wc -c < 'xtpanel/graph.c'`"
- test 11318 -eq "$Wc_c" ||
- echo 'xtpanel/graph.c: original size 11318, current size' "$Wc_c"
- fi
- # ============= xtpanel/README.OW3 ==============
- if test -f 'xtpanel/README.OW3' -a X"$1" != X"-c"; then
- echo 'x - skipping xtpanel/README.OW3 (File already exists)'
- else
- echo 'x - extracting xtpanel/README.OW3 (Text)'
- sed 's/^X//' << 'SHAR_EOF' > 'xtpanel/README.OW3' &&
- X
- X
- How to build xtpanel under Openwindows-3 (SUN OS-4.1.1) without changing
- SUN's #$!&*# imake configuration files.
- X
- X
- Then (all on one line):
- X
- X /usr/openwin/bin/imake -DUseInstalled -DLibDir=/usr/openwin/lib \
- X -DIncRoot=/usr/openwin/include -DOW3
- X
- Then:
- X
- X make Makefiles
- X
- Then:
- X
- X make
- X
- (cross your fingers!)
- X
- SHAR_EOF
- chmod 0664 xtpanel/README.OW3 ||
- echo 'restore of xtpanel/README.OW3 failed'
- Wc_c="`wc -c < 'xtpanel/README.OW3'`"
- test 317 -eq "$Wc_c" ||
- echo 'xtpanel/README.OW3: original size 317, current size' "$Wc_c"
- fi
- exit 0
- -----------------------------------------------------------------
- Steve Cole (steve@sep.stanford.edu, apple!sep!steve)
- Department of Geophysics, Stanford University, Stanford, CA 94305
-