rlong@felix.UUCP (06/06/87)
[SimpleTools2 Demos Source]
---
#!/bin/sh
# shar: Shell Archiver
# Run the following text with /bin/sh to create:
# LittleDemo.c
# SimpleDemo.c
sed 's/^X//' << 'SHAR_EOF' > LittleDemo.c
X
X/* This is a very simple demonstration of a complete application
X using SimpleTools.
X
X Link this file with SimpleTools.o
X (SimpleTools.o is created by compiling SimpleTools.c)
X */
X
X#include "simple.h" /* SimpleTools header file */
X
X#ifdef MEGAMAX
X #include <qd.h> /* Quickdraw header file */
X#endif
X
X#ifdef LIGHTSPEED
X #include <Quickdraw.h>
X#endif
X
Xgotbeep () /* to be executed when beep is picked */
X{
X SysBeep (10);
X}
X
Xinwindow (x, y) /* executed when click in our window */
Xint x, y;
X{
X Point m, lm;
X MoveTo (x, y); /* draw a Point where the mouse is */
X LineTo (x, y);
X #ifdef MEGAMAX
X lm.a.h = x; lm.a.v = y;
X while (StillDown()) { /* and keep drawing like a pencil */
X GetMouse (&m); /* but only when the mouse moves */
X if ((m.a.h != lm.a.h) || (m.a.v != lm.a.v)) {
X LineTo (m.a.h, m.a.v);
X lm = m;
X }
X }
X #else
X lm.h = x; lm.v = y;
X while (StillDown()) { /* and keep drawing like a pencil */
X GetMouse (&m); /* but only when the mouse moves */
X if ((m.h != lm.h) || (m.v != lm.v)) {
X LineTo (m.h, m.v);
X lm = m;
X }
X }
X #endif
X}
X
Xredraw () /* to redraw our window */
X{
X MoveTo (50, 50); /* draw a box the hard way */
X LineTo (50, 100);
X LineTo (100, 100);
X LineTo (100, 50);
X LineTo (50, 50);
X}
X
Xno_edit () /* turn off edit menu (on activation) */
X{
X menu ("Edit", "", itemdisable);
X}
X
Xyes_edit () /* turn on edit menu (on deactivation) */
X{
X menu ("Edit", "", itemenable);
X}
X
Xaboutme() /* About message */
X{
X message ("LittleDemo\rSimpleTools demonstration\rBy Erik Kilk");
X}
X
Xsetup () /* Setup the menus and windows */
X{
X menu (applestring, "About LittleDemo...", aboutme);
X /* Default About is disabled */
X menu (applestring, "About LittleDemo...", itemenable);
X menu ("File", "Beep", gotbeep);
X simplequits ();
X window ("LittleDemo", 20, 50, 490, 325,
X no_edit, yes_edit, redraw, inwindow);
X}
X
Xmain ()
X{
X simpletools ("About LittleDemo..."); /* Initialize SimpleTools */
X setup (); /* Install our menus and window */
X for (;;) simpleevents (); /* Handle all events */
X}
SHAR_EOF
sed 's/^X//' << 'SHAR_EOF' > SimpleDemo.c
X
X/*
X Title : SimpleDemo.c
X Author : Erik Kilk
X Date : June 9, 1985
X Revised : July 17, 1985, November 1986
X
X Synopsis: A demonstration of the use of SimpleTools with
X other toolbox calls. This demonstrates a very
X advanced use of SimpleTools. I've always disliked
X being given simple examples. So, this example
X uses many, many optional features of SimpleTools and
X shows how you can use the standard toolbox calls.
X
X The SimpleTools routines are being freely distributed.
X You may use them, distribute your applications which use them,
X and copy them if you send $20 or more to:
X
X Erik Kilk
X 4949 Snyder Lane, #247
X Rohnert Park, CA 94928
X
X Link this demonstration program with SimpleTools.
X
X*/
X
X/*********************************/
X/* DEFINITIONS FOR SIMPLETOOLS */
X/*********************************/
X
X#include "simple.h"
X
X
X/*******************************/
X/* INCLUDE FILES FOR TOOLBOX */
X/*******************************/
X
X#ifdef MEGAMAX
X #include <qd.h>
X #include <event.h>
X #include <mem.h>
X #include <font.h>
X #include <qdvars.h>
X #include <misc.h>
X #include <win.h>
X #include <string.h>
X #include <stdio.h>
X #define TRUE (-1) /* Common boolean constants */
X #define FALSE 0
X#else
X #include <Quickdraw.h>
X #include <EventMgr.h>
X #include <MemoryMgr.h>
X #include <fontMgr.h>
X #include <WindowMgr.h>
X #include <strings.h>
X #include <stdio.h>
X#endif
X
X/**********************/
X/* GLOBAL VARIABLES */
X/**********************/
X
Xint circspeed = 50; /* rate of drawing new circles */
Xint ourpensize = 1; /* global pen size instead of windows */
XRect drag; /* for moving our free mem manually */
Xchar name[100];
Xchar outstring[100];
X
X
X/*********************/
X/* Odds and Ends */
X/*********************/
X
Xnop() /* Attached as a null pointer */
X{
X}
X
X/***********************/
X/* Sketch Window */
X/***********************/
X
Xsketactivate() /* Attached to sketch's activate */
X{ /* Disable the whole sketch menu */
X menu ("Sketch", "", itemenable);
X menu ("Edit", "Clear", home); /* put our clear routine into place */
X}
X
Xsketdeactivate() /* Attached to sketch's deactivate */
X{ /* Enable the whole sketch menu */
X menu ("Sketch", "", itemdisable);
X menu ("Edit", "Clear", nop); /* take off our clear routine */
X}
X
Xsketch(x,y) /* follow mouse while it's down */
Xint x,y; /* we will get passed the mouse Point */
X{
X Point xy, lastxy; /* points of interest */
X WindowPtr ourport;
X
X GetPort (&ourport);
X PenSize (ourpensize, ourpensize);
X MoveTo (x, y); /* position pen at mouse down Point */
X while ( StillDown() ) { /* while the Button is pressed */
X GetMouse (&xy); /* read the mouse position */
X #ifdef MEGAMAX
X if ( (xy.a.v != lastxy.a.v) || (xy.a.h != lastxy.a.h) ) {
X LineTo (xy.a.h, xy.a.v); /* if mouse moved, draw a Line to it */
X lastxy.a.v = xy.a.v; /* record position so we can detect */
X lastxy.a.h = xy.a.h; /* a Move. */
X } /* could runruns here to have circles */
X #else
X if ( (xy.v != lastxy.v) || (xy.h != lastxy.h) ) {
X LineTo (xy.h, xy.v); /* if mouse moved, draw a Line to it */
X lastxy.v = xy.v; /* record position so we can detect */
X lastxy.h = xy.h; /* a Move. */
X } /* could runruns here to have circles */
X #endif
X runruns(0L);
X SetPort (ourport);
X }
X}
X
Xsetpensize(size) /* Attached to pen size menus */
Xchar *size; /* menu Item name given to us */
X{
X static char lastsize[5]={'1','\0'}; /* keep last setting */
X menu ("Sketch", lastsize, itemuncheck); /* remove old check mark */
X menu ("Sketch", size, itemcheck); /* install a new check mark */
X strcpy (lastsize, size); /* save size for the next setpensize */
X ourpensize = *size - '0'; /* convert to a number */
X}
X
X/**************************/
X/* The Circles Window */
X/**************************/
X
Xint newabs(x) /* an absolute value funtion */
Xint x;
X{
X if (x < 0)
X x = -x;
X return (x);
X}
X
Xcircles() /* draw a Random sized circle */
X{
X static WindowPtr wind = (WindowPtr) 0; /* keep "circle" WindowPtr */
X static int speed = 1; /* current count between new circles */
X register int cx, cy; /* center of the circle */
X int r, b, rd; /* right and bottom edges */
X register Pattern *color; /* color for the circle */
X Rect therect; /* rectangle to draw circle in */
X if ( --speed == 0) { /* count, and if it reaches zero... */
X speed = circspeed; /* reset the count */
X if ((long)wind == 0L) /* get window pointer if we need */
X wind = windowpoint ("Circles"); /* save window for next time */
X SetPort (wind); /* set output to the circle window */
X #ifdef MEGAMAX
X r = wind->portRect.a.right; /* get current size of window */
X b = wind->portRect.a.bottom;
X #else
X r = wind->portRect.right; /* get current size of window */
X b = wind->portRect.bottom;
X #endif
X cx = newabs(Random()) % (r-30) + 1; /* pick a Random location */
X cy = newabs(Random()) % (b-30) + 1;
X rd = newabs(Random()) % 25 + 5; /* and size */
X SetRect ( &therect, cx, cy, cx+rd, cy+rd); /* make a rectangle */
X switch ( newabs(Random()) % 4) { /* pick a color */
X case 0: color = (Pattern *)(&dkGray); break;
X case 1: color = (Pattern *)(<Gray); break;
X case 2: color = (Pattern *)(&gray); break;
X case 3: color = (Pattern *)(&black); break;
X }
X FillOval ( &therect, color); /* make the circle */
X }
X}
X
Xsetspeed(name) /* attached to circle's menu */
Xchar *name;
X{
X menu ("Circles","Slow/S", itemuncheck); /* another way to uncheck last */
X menu ("Circles","Medium/M", itemuncheck); /* just uncheck everything */
X menu ("Circles","Fast/F", itemuncheck);
X menu ("Circles", name, itemcheck); /* then check the current */
X if (strcmp("Slow/S", name) ==0) circspeed = 100; /* set the reset count */
X else if (strcmp("Medium/M", name) ==0) circspeed = 50;
X else if (strcmp("Fast/F", name) ==0) circspeed = 1;
X}
X
X/***********************************************/
X/* Generate a new sketch window by command */
X/***********************************************/
X
Xnwindow() /* command to attatch to new window menu */
X{
X static topplace = 100; /* remember where to put next window */
X char newname[255]; /* string storage for window's name */
X strcpy (newname, "New Window"); /* default window name */
Xretry: /* prompt for the name from user */
X if (prompt ("Give me a unique name for the new window:", newname)) {
X if ( (long)windowpoint(newname) != 0L ) {
X if (message ("Sorry, a window by that name already exists."))
X goto retry; /* if exists, ask for another name */
X } else {
X if ( strlen(newname) > 0) { /* if ok, make the new window */
X /* then it is new */
X window (newname, 20,topplace, 200,topplace+60,
X sketactivate, sketdeactivate, nop, sketch);
X topplace += 5; /* adjust top for next new window */
X if (topplace > 300) topplace = 100; /* reset top if too low */
X }
X }
X }
X}
X
X/**********************/
X/* About SimpleDemo */
X/**********************/
X
Xusageinfo() /* messages for Usage Info menu choice */
X{
X char mess[255]; /* string to form a message */
X strcpy (mess,"You may use, distribute your programs which use, ");
X strcat (mess,"and copy SimpleTools if you send ");
X strcat (mess,"$20 or more to ...");
X if (message (mess)) { /* if OK pressed for first message */
X strcpy (mess, "Erik Kilk\r4949 Snyder Lane, #247\r");
X strcat (mess, "Rohnert Park, CA 94928");
X if (message (mess)) /* if OK pressed for 2nd message */
X message ("Thank you for your support."); /* show final message */
X }
X}
X
Xtellabout() /* for the About SimpleDemo menu choice */
X{
X char mess[255]; /* string to form a message */
X strcpy (mess,"SimpleDemo -- Copyright 1986 Erik Kilk\r");
X strcat (mess,"A demonstration of the use of SimpleTools");
X strcat (mess," as a Macintosh toolbox aid.");
X if (message (mess)) /* display the string in a dialog */
X usageinfo();
X}
X
X/**************************/
X/* The free memory window */
X/**************************/
X
Xwritemem(forsure) /* write the FreeMem value */
Xint forsure; /* true if force write */
X{ /* false if only on change */
X static long lastmem;
X long mem;
X int foo;
X char thestring[10], outstr[10];
X Rect erasearea;
X mem = FreeMem(); /* get new free memory */
X foo = mem != lastmem; /* compare it to last reading */
X if ( forsure | foo ) { /* if forsure or different */
X withwindow ("FreeMem"); /* set window for output */
X home();
X MoveTo (0, 12); /* write new value */
X NumToString (mem, thestring);
X #ifndef MEGAMAX
X PtoCstr (thestring);
X #endif
X strcpy (outstr, " Free ");
X strcat (outstr, thestring);
X #ifndef MEGAMAX
X CtoPstr (outstr);
X #endif
X DrawString (outstr);
X lastmem = mem; /* record this for next time */
X }
X}
X
Xshowmem(windp) /* write FreeMem on an update */
XWindowPtr windp;
X{
X EndUpdate (windp); /* allow use of entire window */
X writemem(TRUE); /* clear and write new value */
X BeginUpdate (windp);
X}
X
Xnewmemvalue() /* write FreeMem occasionally */
X{
X writemem(FALSE); /* write only if value changed */
X}
X
X/* This is a special, non-standard, method of moving a window. Our
X free memory window does not have a title bar. So, I have decided
X to allow it to be moved if the mouse is clicked in its content
X Region. This routine is attached to the free memory in content
X procedure with SimpleTools. SimpleTools convenienly gives us
X various parameters, here we need the window pointer and the event
X record. The event record has the coordinates of the mouse in
X globals */
X
Xmovemem(x,y,windp,event) /* free mem's inContent procedure */
Xint x,y; /* SimpleTools gives us these for */
XWindowPtr windp; /* an in-content procedure */
XEventRecord *event;
X{
X #ifdef MEGAMAX
X DragWindow (windp, &(event->where), &drag);
X #else
X DragWindow (windp, event->where, &drag);
X #endif
X}
X
X/************************/
X/* Question window */
X/************************/
X
Xquest_update () /* repaint question window */
X{
X stgotoxy (0, 0); /* top left corner textwise */
X strcpy (outstring, "Hello, ");
X strcat (outstring, name);
X #ifndef MEGAMAX
X CtoPstr(outstring);
X #endif
X DrawString(outstring);
X}
X
Xquest_getname () /* get a name in quest window */
X{
X getlinecaps = TRUE; /* force each word to be cap'd */
X showawindow ("Questions"); /* make window active */
X quest_update ();
X stgotoxy (0, 2); /* 3rd row, far left textwise */
X strcpy (outstring,"What is your name? ");
X #ifndef MEGAMAX
X CtoPstr (outstring);
X #endif
X DrawString (outstring);
X getline ("", name); /* ask for name of user */
X home (); /* erase the window */
X quest_update (); /* repaint the window */
X}
X
X/**********************/
X/* Initialization */
X/**********************/
X
Xsetup() /* to be called with Start Demo menu choice */
X{
X int temp;
X strcpy (name, "World");
X SetRect (&drag, 4, 24, 508, 338); /* drag area for free mem window */
X windmenu = FALSE; /* modify default values, optional */
X dogoaway = FALSE; /* optional change */
X window("Circles", 20, 55, 245, 315,nop, nop, nop, nop); /* circles */
X temp = wprocid; /* save value for an optional change */
X wprocid = 3; /* optional change to set to altDBoxProc */
X window ("FreeMem", 407, 322, 502, 337, nop, nop, showmem, movemem);
X wprocid = temp; /* return to defaults */
X windmenu = TRUE;
X dogoaway = TRUE;
X window ("Sketch", 265, 55, 490, 315,sketactivate,sketdeactivate,
X nop, sketch); /* display the sketch pad window */
X window ("Questions", 146, 276, 368, 337, nop, nop, quest_update, nop);
X menu ( applestring, "About SimpleDemo", tellabout);
X menu ( applestring, "About SimpleDemo", itemenable);
X menu ( "File", "New", nwindow); /* setup File menu */
X menu ( "File", "New", itemenable);
X menu ( "File", "Set Name", quest_getname);
X simplequits (); /* Transfer and Quit */
X menu ("Circles", "Speed", itemdisable); /* install circles menu */
X menu ("Circles", "Slow/S", setspeed); /* optional key equivs */
X menu ("Circles", "Medium/M", setspeed);
X menu ("Circles", "Fast/F", setspeed);
X menu ("Circles", "Medium/M", itemcheck);
X menu ("Sketch", "Pen Size", itemdisable); /* install sketches menu */
X menu ("Sketch", "", itemdisable);
X menu ("Sketch", "1", setpensize);
X menu ("Sketch", "1", itemcheck);
X menu ("Sketch", "2", setpensize);
X menu ("Sketch", "3", setpensize);
X menu ("Sketch", "4", setpensize);
X menu ("Sketch", "5", setpensize);
X menu ("Sketch", "6", setpensize);
X run (circles); /* keep drawing circles */
X run (newmemvalue); /* check if mem changed occasionally */
X}
X
Xmain()
X{
X simpletools("About SimpleDemo"); /* setup Apple and Edit menus */
X setup(); /* setup our program */
X for (;;) simpleevents(); /* process events */
X}
SHAR_EOF
exit
---