
# This is a shell archive.  Remove anything before this line,
# then unpack it by saving it in a file and typing "sh file".
#
# Wrapped by Dan Bennett <bennett@intrepid> on Thu Sep 12 13:42:15 1996
#
# This archive contains:
#	example1	
#

LANG=""; export LANG
PATH=/bin:/usr/bin:/usr/sbin:/usr/ccs/bin:$PATH; export PATH

echo mkdir - example1
mkdir example1

echo x - example1/Makefile
cat >example1/Makefile <<'@EOF'
XINCSEARCH = -I/usr/include/X11R5 -I/usr/include/Motif1.2
XLIBSEARCH = -L/usr/lib/X11R5 -L/usr/lib/Motif1.2
SEARCH = ${XINCSEARCH} ${XLIBSEARCH}

CC = cc
CDEBUGFLAGS = -g
CFLAGS = -Aa ${CDEBUGFLAGS} ${SEARCH}

CLIBS = -lm
XLIBS = -lXm -lXmu -lXt -lX11 -lXext
LIBS = ${CLIBS} ${XLIBS}

RM = /bin/rm
RMFLAGS = -f

OBJS = point.o line.o Xroutines.o

all:: main

main: ${OBJS} main.c
	${CC} ${CFLAGS} -o $@  main.c ${OBJS} ${SEARCH} ${LIBS}

clean:
	${RM} ${RMFLAGS} main ${OBJS}	
@EOF

chmod 644 example1/Makefile

echo x - example1/line.c
cat >example1/line.c <<'@EOF'
#include <stdio.h>
#include <math.h>

#include "mygraph.h"
#include "point.h"
#include "Xroutines.h"

dc_draw_line ( line aline, environment * env) {
   float dx,dy,y,m;
   point  pt;

   dy = aline.e1.y - aline.e2.y;
   dx = aline.e1.x - aline.e2.x;
   m = dy/dx;
   y = aline.e1.y;

   for (pt.x=aline.e1.x; pt.x < (1+aline.e2.x); pt.x++){
       pt.y = (int) y;
       dc_draw_point(pt, env);
       y = y + m;
   }
}
@EOF

chmod 644 example1/line.c

echo x - example1/line.h
cat >example1/line.h <<'@EOF'
#ifndef MY_LINE_DEFS

#include "mygraph.h"

extern void dc_draw_line ( line aline, environment  * env);

#define MY_LINE_DEFS
#endif
@EOF

chmod 644 example1/line.h

echo x - example1/overview.html
cat >example1/overview.html <<'@EOF'
<TITLE> Computer Graphics: Simple X Program</TITLE>
<h1> A simple X program </h1>

The programs in this directory provide a simple x based application for 
us to develop some graphics routines.  

Please notice the following:<p>
<ul>
<li> All points are integers.
<li> The line drawing algorithm is very simple and has some definate flaws.
</ul>
<p>

The files are as follows
<ul>
<li> The routines to Access X functions directly.
<ul>
<li> <a href="Xroutines.c">Xroutines.c </a>
<li> <a href="Xroutines.h">Xroutines.h </a>
</ul>
<li> The routines to draw a point 
<ul>
<li> <a href="point.c">point.c </a>
<li> <a href="point.h">point.h </a>
</ul>
<li> The routines to draw a line
<ul>
<li> <a href="line.c">line.c </a>
<li> <a href="line.h">line.h </a>
</ul>
<li> <a href="main.c">main.c </a> The main routine.
<li> <a href="mygraph.h">mygraph.h </a> 
The definition of data structures needed.
<li> A <a href="Makefile">Makefile</a> to compile the program.
</ul>
@EOF

chmod 644 example1/overview.html

echo x - example1/main.c
cat >example1/main.c <<'@EOF'
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <Xm/DrawingA.h>
#include <Xm/MainW.h>
#include <Xm/TextF.h>
#include "mygraph.h"
#include "point.h"
#include "line.h"

static void parse_command(Widget where, XtPointer cd,  XtPointer cld);
void parse_text(char * ch, environment * env);

static void drawing_board_expose(Widget where, XtPointer cd,  XtPointer cld) {

}

main (int argc, char * argv[]) {

    XtAppContext  app_context;
    Widget        toplevel,main_form,dialog_box;
    Arg           args[10];
    int           nargs=0;
    environment   env;

    env.xmax = 400;
    env.ymax = 400;

    toplevel = XtAppInitialize(&app_context,NULL,NULL,0,
                &argc,argv,NULL,args,nargs);

    nargs = 0;
    XtSetArg(args[nargs],XmNshowSeparator,True); nargs++;
    main_form = XtCreateManagedWidget("form",xmMainWindowWidgetClass,
	      toplevel,args,nargs);

    XtManageChild(main_form);

    nargs=0;
    XtSetArg(args[nargs],XmNwidth,env.xmax); nargs++;
    XtSetArg(args[nargs],XmNheight,env.ymax); nargs++;

    env.db = XtCreateManagedWidget("msgs",xmDrawingAreaWidgetClass,
               main_form, args,nargs);
    XtManageChild(env.db);
    XtAddCallback(env.db,XmNexposeCallback, drawing_board_expose,&env);

    setup_environment(&env);

    nargs = 0;
    dialog_box = XmCreateTextField(main_form,"Command",args,nargs);
    XtManageChild(dialog_box);
    XtAddCallback(dialog_box,XmNactivateCallback,parse_command ,&env);

    XmMainWindowSetAreas(main_form, NULL, dialog_box, NULL, NULL, env.db);

    XtRealizeWidget(toplevel);

    XtAppMainLoop(app_context);
}

static void parse_command(Widget where, XtPointer cd,  XtPointer cld) {

       char * p;
       char * value;

       value = NULL;

       p = XmTextFieldGetString(where);
       XmTextFieldSetString(where,value);
       parse_text(p,(environment *) cd);
}

void parse_text(char * ch, environment * env)
{
      char * tolk1;

      tolk1 = strtok(ch," ");
      if (!strcmp("quit",tolk1)) { exit(0); }
      if (!strcmp("line",tolk1)) { 
          line   ln;
	  ln.e1.x = atoi(strtok(NULL," "));
	  ln.e1.y = atoi(strtok(NULL," "));
	  ln.e2.x = atoi(strtok(NULL," "));
	  ln.e2.y = atoi(strtok(NULL," "));
	  dc_draw_line(ln,env);
      }
      if (!strcmp("point",tolk1)) { 
          point   pt;
	  pt.x = atoi(strtok(NULL," "));
	  pt.y = atoi(strtok(NULL," "));
	  dc_draw_point(pt,env);
      }
}
@EOF

chmod 644 example1/main.c

echo x - example1/mygraph.h
cat >example1/mygraph.h <<'@EOF'
#ifndef MY_GRAPHICS_DEFS

#include <X11/Xlib.h>
#include <Xm/Xm.h>

typedef struct {
   GC           gc;
   Widget       db;
   Display  *   disp;
   int          xmax,ymax;
} environment;

typedef struct {
   int x,y;
} point ;

typedef struct {
   point  e1, e2;
} line;

#define MY_GRAPHICS_DEFS
#endif
@EOF

chmod 644 example1/mygraph.h

echo x - example1/point.c
cat >example1/point.c <<'@EOF'
#include <stdio.h>

#include "mygraph.h"

void dc_draw_point (point a_point, environment *  env) {
    SetPixel(env,a_point.x,a_point.y);
}
@EOF

chmod 644 example1/point.c

echo x - example1/point.h
cat >example1/point.h <<'@EOF'
#ifndef MY_POINT_DEFS

void dc_draw_point (point a_point, environment *  env);

#define MY_POINT_DEFS
#endif
@EOF

chmod 644 example1/point.h

echo x - example1/Xroutines.c
cat >example1/Xroutines.c <<'@EOF'
#include "Xroutines.h" 


void SetPixel(environment *  env, int x, int y ){
       
       XDrawPoint (env->disp, XtWindowOfObject(env->db), env->gc, x, y);
}

void setup_environment(environment * env){
    int       screen_num;

    env->disp = XtDisplay(env->db);
    screen_num = DefaultScreen(env->disp);
    env->gc = DefaultGC(env->disp,screen_num);
}
@EOF

chmod 644 example1/Xroutines.c

echo x - example1/Xroutines.h
cat >example1/Xroutines.h <<'@EOF'
#ifndef MY_XROUTINES

#include <stdio.h>
#include <errno.h>
#include <X11/Xlib.h>
#include <Xm/Xm.h>
#include "mygraph.h"

extern void SetPixel(environment *  env, int x, int y );
extern void setup_environment(environment * env);

#define MY_XROUTINES
#endif
@EOF

chmod 644 example1/Xroutines.h

chmod 755 example1

exit 0
