
# 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@aegis> on Thu Oct 24 13:21:57 1996
#
# This archive contains:
#	example3	
#

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

echo mkdir - example3
mkdir example3

echo x - example3/Makefile
cat >example3/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 fill.o

all:: main

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

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

chmod 644 example3/Makefile

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


void SetWidth(environment * env, int width){
    XGCValues  values;

    values.line_width = width;

    XChangeGC(env->disp, env->gc,GCLineWidth,&values);
}

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

void DrawLine(int x1, int y1, int x2, int y2, environment * env){
       XDrawLine(env->disp, XtWindowOfObject(env->db), env->gc,x1,y1,x2,y2);
}

void Get_Image_From_Server(environment * env) {
      env->the_screen = XGetImage(env->disp,XtWindowOfObject(env->db),0,0,
		 env->xmax, env->ymax, AllPlanes,XYPixmap);
}

unsigned long Get_Pixel(environment * env, int x,int y){
      return(XGetPixel(env->the_screen,x,y));
}

void Clear_Screen(environment * env){
     XSetForeground(env->disp,env->gc,env->bg);
     XFillRectangle(env->disp, XtWindowOfObject(env->db),env->gc
	   ,0,0,env->xmax,env->ymax);
     XSetForeground(env->disp,env->gc,env->fg);
}

void SetDashList(environment * env, char * dash_list, int offset, int count){

          XSetDashes(env->disp, env->gc,offset,dash_list,count);
}

void SetDashStyle(environment * env, char * token){
          XGCValues  values;
         
          if (!strcmp("solid",token)) {
               values.line_style = LineSolid;
          }
          if (!strcmp("onoff",token)) {
               values.line_style = LineOnOffDash;
          }
          if (!strcmp("double",token)) {
               values.line_style = LineDoubleDash;
          }
          XChangeGC(env->disp, env->gc,GCLineStyle,&values);
          return;
}
void SetCapStyle(environment * env, char * token){
          XGCValues  values;
         
          if (!strcmp("round",token)) {
               values.cap_style = CapRound;
          }
          if (!strcmp("butt",token)) {
               values.cap_style = CapButt;
          }
          if (!strcmp("project",token)) {
               values.cap_style = CapProjecting;
          }
          XChangeGC(env->disp, env->gc,GCCapStyle,&values);
          return;
}

int Set_Color(environment * env, int color) {

     
     XGCValues  value_return;

     XGetGCValues(env->disp,env->gc,GCForeground,&value_return);


     if (color) {
	XSetForeground(env->disp,env->gc,env->bg);
     } else {
	XSetForeground(env->disp,env->gc,env->fg);
     }

     if (value_return.foreground == env->fg) {
	return (1);
     } else {
	return (0);
     }

}

void realize_environment(Widget mf, environment * env){
    XColor     red,green,sr,er;
    Window     w_list[3];

    w_list[0] = XtWindow(XtParent(mf));
    w_list[1] = XtWindow(mf);
    w_list[2] = XtWindow(env->db);

    env->cm = XCreateColormap(env->disp,XtWindow(env->db),
	  DefaultVisual(env->disp,XDefaultScreen(env->disp)),AllocNone);
    XSetWindowColormap(env->disp, XtWindow(env->db), env->cm);    
    XSetWindowColormap(env->disp, XtWindow(mf), env->cm);    
    XSetWindowColormap(env->disp, XtWindow(XtParent(mf)), env->cm);    
    if (XSetWMColormapWindows(env->disp, XtWindow(XtParent(mf)),w_list,2)==0) {
	printf("Could not set colormap property\n");
    }

    if(XAllocNamedColor(env->disp,env->cm,"blue",&sr,&er)==0) {
	red = er;
    }  else{
	red = sr;
    }

    if(XAllocNamedColor(env->disp,env->cm,"LawnGreen",&sr,&er)==0) {
	green = er;
    }  else{
	green = sr;
    }

    env->fg = green.pixel;
    env->bg = red.pixel;
    XSetForeground(env->disp,env->gc,env->fg);
    XSetBackground(env->disp,env->gc,env->bg);
}


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);
    env->the_screen = NULL;
}
@EOF

chmod 644 example3/Xroutines.c

echo x - example3/Xroutines.h
cat >example3/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 realize_environment(Widget mf,environment * env);
extern void SetWidth(environment * env, int width);
extern void SetPixel(environment *  env, int x, int y );
extern void setup_environment(environment * env);
extern void DrawLine(int x1, int y1, int x2, int y2, environment * env);
extern void Get_Image_From_Server(environment * env);
extern unsigned long Get_Pixel(environment * env, int x,int y);
extern void Clear_Screen(environment * env);
extern void SetDashList(environment * env, char * dash_list, int offset, int count);
extern void SetDashStyle(environment * env, char * token);
extern void SetCapStyle(environment * env, char * token);
int Set_Color(environment * env, int color);

#define MY_XROUTINES
#endif
@EOF

chmod 644 example3/Xroutines.h

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

void print_screen(environment * env) {
    int x,y;
    unsigned long k;

    Get_Image_From_Server(env);

    for (y=0;y<env->ymax;y++) {
        for (x=0;x<env->xmax;x++){
	    k = Get_Pixel(env,x,y);
	    printf("%3i",k);
        }
	printf("\n");
    }
    printf("\n");
}

void fill_it(int x, int y,int fg,  int bg, environment * env) {
    int here;

    here = Get_Pixel(env,x,y);
    if ((here != bg) && (here != fg)) {
	   SetPixel(env,x,y); 
	   if (fg == 0) { XPutPixel(env->the_screen,x,y,env->fg);}
	   if (fg == 1) { XPutPixel(env->the_screen,x,y,env->bg);}
	   fill_it(x+1,y,fg,bg,env);
	   fill_it(x,y+1,fg,bg,env);
	   fill_it(x-1,y,fg,bg,env);
	   fill_it(x,y-1,fg,bg,env);
    }
}

void fill(int x, int y, int fg, int bg, environment * env) {
    int hold_color;
 
    hold_color = Set_Color(env, fg);

    Get_Image_From_Server(env);
    fill_it(x,y,fg,bg,env);
    Set_Color(env,hold_color);
}

void paint_screen(environment * env, int color) {

    int x,y;
    int hold_color;
 
    hold_color = Set_Color(env, color);
    for (y=0;y<env->ymax;y++) {
        for (x=0;x<env->xmax;x++){
	   SetPixel(env,x,y); 
        }
    }
    Set_Color(env,hold_color);

}
@EOF

chmod 644 example3/fill.c

echo x - example3/fill.h
cat >example3/fill.h <<'@EOF'
#ifndef MY_FILL

#include "mygraph.h"

extern void print_screen(environment * env);
extern void paint_screen(environment * env, int color);
void fill(int x, int y, int fg, int bg, environment * env) ;
#define MY_FILL
#endif
@EOF

chmod 644 example3/fill.h

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

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

#  define M_PI              3.14159265358979323846

dc_draw_line ( line aline, environment * env) {
    DrawLine(aline.e1.x, aline.e1.y,aline.e2.x, aline.e2.y, env);
}

draw_circle(environment * env,int xc,int yc,int r) {

     int x, y;
     int p;

     y = r;
     p = 3-2*r;
     for(x=0;x<=y;x++) {
          SetPixel(env,xc+x,yc+y);
          SetPixel(env,xc-x,yc+y);
          SetPixel(env,xc+x,yc-y);
          SetPixel(env,xc-x,yc-y);
          SetPixel(env,xc+y,yc+x);
          SetPixel(env,xc-y,yc+x);
          SetPixel(env,xc+y,yc-x);
          SetPixel(env,xc-y,yc-x);

	  if (p < 0) {
	     p += 4*x+6;
	  } else {
	     p += 4*(x-y)+10;
	     y -= 1;
	  }
     }
}
@EOF

chmod 644 example3/line.c

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

#include "mygraph.h"

extern void dc_draw_line ( line aline, environment  * env);
extern draw_circle(environment * env,int xc, int yc, int r) ;

#define MY_LINE_DEFS
#endif
@EOF

chmod 644 example3/line.h

echo x - example3/main.c
cat >example3/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"
#include "Xroutines.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) {

      Clear_Screen((environment * )cd);
}

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

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

    env.xmax = 600;
    env.ymax = 600;


    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);

    realize_environment(main_form,&env);

    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 read_from_file(char * fn, environment * env)
{
    FILE * fd;
    char buf[400];

    fd = fopen(fn,"r");
    if (fd != NULL) {
	 while ( fgets(buf,400,fd) != NULL) {
	      printf("%s read\n",buf);
	      parse_text(buf,env);
	 }
         fclose(fd);     
    }

}

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);
	  return;
      }
      if (!strcmp("point",tolk1)) { 
          point   pt;
	  pt.x = atoi(strtok(NULL," "));
	  pt.y = atoi(strtok(NULL," "));
	  dc_draw_point(pt,env);
	  return;
      }
      if (!strcmp("read",tolk1)) { 
	  char * tolk2;

	  tolk2 = strtok(NULL," ");
	  read_from_file(tolk2,env);
	  return;
      }
      if (!strcmp("circle",tolk1)) { 
	  int xc,yc,r;

	  xc = atoi(strtok(NULL," "));
	  yc = atoi(strtok(NULL," "));
	  r = atoi(strtok(NULL," "));
	  draw_circle(env,xc,yc,r);
	  return;
      }
      if (!strcmp("output",tolk1)) { 
	  print_screen(env);
	  return;
      }
      if (!strcmp("clear",tolk1)) { 
	  Clear_Screen(env);
	  return;
      }
      if (!strcmp("paint",tolk1)) { 
	  int color;

	  color = atoi(strtok(NULL," "));
	  paint_screen(env,color);
	  return;
      }
      if (!strcmp("width",tolk1)) { 
	  int width;

	  width = atoi(strtok(NULL," "));
	  SetWidth(env,width);
	  return;
      }
      if (!strcmp("dashp",tolk1)) { 
	  int count,offset,i;
	  char * the_list;

	  count = atoi(strtok(NULL," "));
	  the_list = malloc(count);
	  for(i=0;i<count;i++) {
	     the_list[i] = atoi(strtok(NULL," "));
	  }
	  offset = atoi(strtok(NULL," "));

	  SetDashList(env,the_list,offset,count);
	  free(the_list);
	  return;
      }
      if (!strcmp("cap",tolk1)) { 
	  char * tolk2;

	  tolk2 = strtok(NULL," ");
	  SetCapStyle(env, tolk2);
	  return;
      }
      if (!strcmp("dash",tolk1)) { 
	  char * tolk2;

	  tolk2 = strtok(NULL," ");
	  SetDashStyle(env, tolk2);
	  return;
      }
      if (!strcmp("fill",tolk1)) { 
	  int x,y,fg,bg;

	  x = atoi(strtok(NULL," "));
	  y = atoi(strtok(NULL," "));
	  fg = atoi(strtok(NULL," "));
	  bg = atoi(strtok(NULL," "));
	  fill(x,y,fg,bg,env);
	  return;
      }
      printf("Error, command %s unknown\n",tolk1);
}
@EOF

chmod 644 example3/main.c

echo x - example3/mygraph.h
cat >example3/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;
   XImage   *   the_screen;
   Colormap     cm;
   unsigned long fg,bg;
} environment;

typedef struct {
   int x,y;
} point ;

typedef struct {
   point  e1, e2;
} line;

#define MY_GRAPHICS_DEFS
#endif
@EOF

chmod 644 example3/mygraph.h

echo x - example3/overview.html
cat >example3/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.   Some additional X routines have been
added.

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> Routines to demonstrate ideas needed for an area fill routine. 
<ul>
<li> <a href="fill.c">fill.c </a>
<li> <a href="fill.h">fill.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>

The entire directory can be retrived via this <a href="examp3.shar">
shar file</a>.
@EOF

chmod 644 example3/overview.html

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

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

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

chmod 644 example3/point.c

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

void dc_draw_point (point a_point, environment *  env);

#define MY_POINT_DEFS
#endif
@EOF

chmod 644 example3/point.h

chmod 755 example3

exit 0
