[mod.mac.sources] TEDemos.pit.Hqx Human Readable Version

dubois@uwmacc.UUCP (Paul DuBois) (12/05/86)

#!/bin/sh
# shar:	Shell Archiver
#	Run the following text with /bin/sh to create:
#	TEDemos/TinyEdit.c
#	TEDemos/DumbEdit.c
#
#	MODERATOR'S NOTE:  Tab-width is 4
#
sed 's/^X//' << 'SHAR_EOF' > TEDemos/TinyEdit.c
X/*
X	TinyEdit - Minimal TransEdit Demonstration.
X
X	The project should include TinyEdit.c (this file), TransEdit.c,
X	FakeAlert.c, TransSkel.c (or a project made from TransSkel.c)
X	and MacTraps.
X
X	8 November 1986		Paul DuBois
X*/
X
X# include	<MenuMgr.h>
X# include	<FontMgr.h>
X# include	"TransEdit.h"
X
X
X# define	aboutAlrt	1000	/* "About..." alert number */
X
X
Xtypedef enum		/* File menu item numbers */
X{
X	new = 1,		/* begin new window */
X	open,			/* open existing file */
X	close,			/* close window */
X	/* --- */
X	quit = 5
X};
X
X
Xtypedef enum			/* Edit menu item numbers */
X{
X	undo = 1,
X	/* --- */
X	cut = 3,
X	copy,
X	paste,
X	clear
X};
X
X
XWindowPtr	lastFront = nil;	/* keeps track of front window */
XWindowPtr	editWind = nil;		/* non-nil if edit window open */
XMenuHandle	fileMenu;
XMenuHandle	editMenu;
X
X
X
X/*
X	Set File/Edit menu items according to type of front window.
X
X	The general behavior is:
X
X	New and Open enabled if an edit window is not open, otherwise they
X	are disabled.
X
X	Close enabled when an edit or DA window is in front (i.e.,
X	when there's a window at all).
X
X	Undo disabled when the edit window is in front.
X*/
X
XSetMenus ()
X{
X	DisableItem (fileMenu, close);	/* assume no window at all */
X	EnableItem (editMenu, undo);
X
X	if (FrontWindow () != nil)
X	{
X		EnableItem (fileMenu, close);
X		if (IsEWindow (FrontWindow ()))	/* the edit window's in front */
X			DisableItem (editMenu, undo);
X	}
X
X	if (editWind == nil)
X	{
X		EnableItem (fileMenu, new);
X		EnableItem (fileMenu, open);
X	}
X	else
X	{
X		DisableItem (fileMenu, new);
X		DisableItem (fileMenu, open);
X	}
X}
X
X
X/*
X	Got an activate or deactivate.  It doesn't matter which, really.
X	Set the text menus appropriately for the front window, and draw
X	the menu bar, as these menus might change state from enabled to
X	disabled or vice-versa.
X*/
X
XActivate (active)
XBoolean	active;
X{
X	SetMenus ();
X}
X
X
X/*
X	Close selected from File menu, or close box of edit window was
X	clicked.
X*/
X
XClose ()
X{
X	if (EWindowClose (editWind))
X		editWind = nil;
X	SetMenus ();
X}
X
X
X/*
X	Make a new edit window.  Set the title to "Untitled" if not bound
X	to file, so that the window titling works the same whether
X	TransEdit is compiled in single or multiple window mode.
X*/
X
XMakeWind (bindToFile)
XBoolean	bindToFile;
X{
XRect		r;
X
X	SetRect (&r, 4, 45, 504, 335);
X	editWind = NewEWindow (&r, nil, false, -1L, true, 0L, bindToFile);
X	if (editWind != nil)
X	{
X		if (!bindToFile)
X			SetWTitle (editWind, "\pUntitled");
X		ShowWindow (editWind);
X	}
X}
X
X
X/*
X	File menu handler
X*/
X
XDoFileMenu (item)
Xint		item;
X{
XWindowPtr	theWind;
X
X	theWind = FrontWindow ();
X	switch (item)
X	{
X
X	case new:
X		MakeWind (false);
X		break;
X
X	case open:
X		MakeWind (true);
X		break;
X
X	case close:
X		if (IsEWindow (theWind))
X			Close ();
X		else	/* DA in front */
X			CloseDeskAcc (((WindowPeek) theWind)->windowKind);
X		break;
X
X	case quit:
X		if (ClobberEWindows () == true)
X			SkelWhoa ();
X		break;
X
X	}
X	SetMenus ();
X}
X
X
X/*
X	Handle selection of AboutI item from Apple menu
X*/
X
XDoAbout ()
X{
X	(void) Alert (aboutAlrt, nil);
X}
X
X
X/*
X	Background procedure.  Check front window, reset menus if it
X	changes.
X*/
X
XCheckFront ()
X{
X	if (FrontWindow () != lastFront)
X	{
X		SetMenus ();
X		lastFront = FrontWindow ();
X	}
X}
X
X
Xmain ()
X{
X
X/*
X	Initialize TransSkel, create menus and install handlers.
X*/
X
X	SkelInit ();
X
X	SkelApple ("\pAbout TinyEditI", DoAbout);
X
X	fileMenu = NewMenu (1000, "\pFile");
X	AppendMenu (fileMenu, "\pNew/N;Open.../O;(Close/K;(-;Quit/Q");
X	SkelMenu (fileMenu, DoFileMenu, nil);
X
X	editMenu = NewMenu (1001, "\pEdit");
X	AppendMenu (editMenu, "\pUndo/Z;(-;Cut/X;Copy/C;Paste/V;Clear");
X	SkelMenu (editMenu, EWindowEditOp, nil);
X
X/*
X	Do TransEdit-specific setup:  set creator for any files created,
X	set default event notification procedures for new windows.
X*/
X
X	SetEWindowProcs (nil, nil, Activate, Close);
X
X/*
X	Process events until user quits,
X	then clean up and exit
X*/
X
X	SkelBackground (CheckFront);
X	SkelMain ();
X	SkelClobber ();
X}
SHAR_EOF
sed 's/^X//' << 'SHAR_EOF' > TEDemos/DumbEdit.c
X/*
X	DumbEdit - Multiple-window TransEdit Demonstration.
X
X	The project should include DumbEdit.c (this file), TransEdit.c,
X	FakeAlert.c, TransSkel.c (or a project made from TransSkel.c)
X	and MacTraps.
X
X	28 October 1986		Paul DuBois
X*/
X
X# include	<MenuMgr.h>
X# include	<FontMgr.h>
X# include	"TransEdit.h"
X
X
X# define	maxSize		8		/* no. font sizes made available */
X# define	hSize		300		/* horiz, vert size of new windows */
X# define	vSize		205
X# define	aboutAlrt	1000
X
X
Xtypedef enum		/* File menu item numbers */
X{
X	new = 1,		/* begin new window */
X	open,			/* open existing file */
X	close,			/* close file */
X	/* --- */
X	save = 5,		/* save file */
X	saveAs,			/* save under another name */
X	saveCopy,		/* save a copy w/o switching file binding */
X	revert,			/* revert to version on disk */
X	/* --- */
X	quit = 10
X};
X
X
Xtypedef enum			/* Edit menu item numbers */
X{
X	undo = 1,
X	/* --- */
X	cut = 3,
X	copy,
X	paste,
X	clear
X};
X
X
X
Xtypedef enum		/* Format menu item numbers */
X{
X	wordWrap = 1,
X	noWrap,
X	/* --- */
X	leftJust = 4,
X	centerJust,
X	rightJust
X};
X
X
XWindowPtr	lastFront = nil;	/* keeps track of front window */
XMenuHandle	fileMenu;
XMenuHandle	editMenu;
XMenuHandle	fontMenu;
XMenuHandle	sizeMenu;
XMenuHandle	formatMenu;
X
Xint		sizes[maxSize] = { 9, 10, 12, 14, 18, 20, 24, 48 };
X
X
X/*
X	Uncheck all the items in a menu
X*/
X
XUncheckMenu (theMenu)
XMenuHandle	theMenu;
X{
Xregister int	i, nItems;
X
X	nItems = CountMItems (theMenu);
X
X	for (i = 1; i <= nItems; ++i)
X	{
X		CheckItem (theMenu, i, false);
X		SetItemStyle (theMenu, i, 0);
X	}
X}
X
X
X/*
X	Set the Font, Size and Format menus so that the items corresponding
X	to the text characteristics of the window are checked.  If the
X	window isn't an edit window, dim all three menus.
X*/
X
XSetTextMenus (drawBar)
XBoolean		drawBar;
X{
XWindowPtr	theWind;
XStr255		wFontName;
XStr255		mFontName;
Xregister int	i, nItems;
Xregister TEHandle	te;
X
X	theWind = FrontWindow ();
X	UncheckMenu (fontMenu);				/* toss current check marks */
X	UncheckMenu (sizeMenu);
X	UncheckMenu (formatMenu);
X
X	if (!IsEWindow (theWind))			/* disable the menus */
X	{
X		DisableItem (fontMenu, 0);
X		DisableItem (sizeMenu, 0);
X		DisableItem (formatMenu, 0);
X	}
X	else
X	{
X		EnableItem (fontMenu, 0);
X		EnableItem (sizeMenu, 0);
X		EnableItem (formatMenu, 0);
X
X		te = GetEWindowTE (theWind);
X
X/*
X	Check appropriate word wrap item
X*/
X
X		CheckItem (formatMenu, (**te).crOnly < 0 ? noWrap : wordWrap, true);
X
X/*
X	Check appropriate justification item
X*/
X
X		switch ((**te).just)
X		{
X
X		case teJustLeft:
X			CheckItem (formatMenu, leftJust, true);
X			break;
X
X		case teJustRight:
X			CheckItem (formatMenu, rightJust, true);
X			break;
X
X		case teJustCenter:
X			CheckItem (formatMenu, centerJust, true);
X			break;
X
X		}
X
X/*
X	Check appropriate font size item, and outline items for sizes
X	present in resource files
X*/
X
X		for (i = 0; i < maxSize; ++i)
X		{
X			if ((**te).txSize == sizes[i])
X				CheckItem (sizeMenu, i + 1, true);
X
X			if (RealFont ((**te).txFont, sizes[i]))
X				SetItemStyle (sizeMenu, i + 1, outline);
X			else
X				SetItemStyle (sizeMenu, i + 1, 0);		/* plain */
X		}
X
X/*
X	Check appropriate font name item
X*/
X		
X		GetFontName ((**te).txFont, wFontName);	/* name of window font */
X		nItems = CountMItems (fontMenu);		/* # fonts in menu */
X		for (i = 1; i <= nItems; ++i)
X		{
X			GetItem (fontMenu, i, mFontName);	/* get font name */
X			if (EqualString (wFontName, mFontName, false, true))
X			{
X				CheckItem (fontMenu, i, true);
X				break;
X			}
X		}
X
X	}
X
X	if (drawBar)
X		DrawMenuBar ();
X}
X
X
X
X/*
X	Set File/Edit menu items according to type of front window.
X
X	The general behavior is:
X
X	New and Open always enabled, since a new edit window can always be
X	opened.
X
X	Close enabled when an edit or DA window in front (i.e., when there's
X	a window at all).
X
X	Save enabled for edit windows not bound to a file, and edit windows
X	bound to a file when they're dirty (typed into, Edit menu used to
X	do something to them).
X
X	Save As and Save a Copy As enabled for edit windows.
X
X	Revert enabled for edit windows bound to a file when they're dirty.
X
X	Undo disabled when there's an edit window in front.
X*/
X
XSetNonTextMenus ()
X{
XWindowPtr	theWind;
Xint			theKind;
X
X	DisableItem (fileMenu, close);	/* assume no window at all */
X	DisableItem (fileMenu, save);
X	DisableItem (fileMenu, saveAs);
X	DisableItem (fileMenu, saveCopy);
X	DisableItem (fileMenu, revert);
X	EnableItem (editMenu, undo);
X
X	theKind = 0;
X	if ((theWind = FrontWindow ()) != nil)
X		theKind = ((WindowPeek) theWind)->windowKind;
X
X	if (theKind < 0)						/* DA in front */
X	{
X		EnableItem (fileMenu, close);
X	}
X	else if (IsEWindow (theWind))			/* edit window in front */
X	{
X		EnableItem (fileMenu, close);
X		EnableItem (fileMenu, saveAs);
X		EnableItem (fileMenu, saveCopy);
X		if (GetEWindowFile (theWind, nil) == false)	/* not bound to file */
X		{
X			EnableItem (fileMenu, save);
X		}
X		else if (IsEWindowDirty (theWind))	/* bound - is it dirty? */
X		{
X			EnableItem (fileMenu, save);
X			EnableItem (fileMenu, revert);
X		}
X		DisableItem (editMenu, undo);
X	}
X}
X
X
X/*
X	Got an activate or deactivate.  It doesn't matter which, really.
X	Set the text menus appropriately for the front window, and draw
X	the menu bar, as these menus might change state from enabled to
X	disabled or vice-versa.
X*/
X
XActivate (active)
XBoolean	active;
X{
X	CheckFront ();
X}
X
X
X/*
X	Got a keyclick in an edit window.
X*/
X
XKey ()
X{
X	SetNonTextMenus ();
X}
X
X
X/*
X	Close selected from File menu, or close box of edit window
X	clicked.
X*/
X
XClose ()
X{
XWindowPtr	theWind;
X
X	GetPort (&theWind);
X	(void) EWindowClose (theWind);
X	CheckFront ();
X}
X
X
XMakeWind (bindToFile)
XBoolean	bindToFile;
X{
XRect		r;
Xstatic int	windCount = 0;
Xint			offset;
X
X	if (FrontWindow () == nil)
X		windCount = 0;
X	SetRect (&r, 0, 0, hSize, vSize);
X	offset = 50 + 25 * (windCount++ % 4);
X	OffsetRect (&r, offset, offset);
X	(void) NewEWindow (&r, nil, true, -1L, true, 0L, bindToFile);
X}
X
X
X/*
X	File menu handler
X*/
X
XDoFileMenu (item)
Xint		item;
X{
XWindowPtr	theWind;
X
X	theWind = FrontWindow ();
X	switch (item)
X	{
X
X	case new:
X		MakeWind (false);
X		break;
X
X	case open:
X		MakeWind (true);
X		break;
X
X	case close:
X		if (IsEWindow (theWind))
X			(void) EWindowClose (theWind);
X		else
X			CloseDeskAcc (((WindowPeek) theWind)->windowKind);	/* DA in front */
X		break;
X
X	case save:
X		(void) EWindowSave (theWind);
X		break;
X
X	case saveAs:
X		(void) EWindowSaveAs (theWind);
X		break;
X
X	case saveCopy:
X		(void) EWindowSaveCopy (theWind);
X		break;
X
X	case revert:
X		(void) EWindowRevert (theWind);
X		break;
X
X	case quit:
X		if (ClobberEWindows () == true)
X			SkelWhoa ();
X		break;
X
X	}
X	SetNonTextMenus ();
X}
X
X
X/*
X	Handle Font menu items
X*/
X
XDoFontMenu (item)
Xint		item;
X{
Xint			font;
XTEHandle	te;
XWindowPtr	theWind;
XStr255		theFontName;
X
X	theWind = FrontWindow ();
X	if ((te = GetEWindowTE (theWind)) == nil)
X		return;				/* not an edit window */
X	GetItem (fontMenu, item, theFontName);
X	GetFNum (theFontName, &font);
X	SetEWindowStyle (theWind, font, (**te).txSize, (**te).crOnly, (**te).just);
X	SetTextMenus (false);
X
X}
X
X
X/*
X	Handle Size menu items
X*/
X
XDoSizeMenu (item)
Xint		item;
X{
XTEHandle	te;
XWindowPtr	theWind;
X
X	theWind = FrontWindow ();
X	if ((te = GetEWindowTE (theWind)) == nil)
X		return;				/* not an edit window */
X	SetEWindowStyle (theWind, (**te).txFont, sizes[item-1], (**te).crOnly, (**te).just);
X	SetTextMenus (false);
X}
X
X
X/*
X	Handle Format menu items
X*/
X
XDoFormatMenu (item)
Xint		item;
X{
Xint			font, size, just, wrap;
XTEHandle	te;
XWindowPtr	theWind;
X
X	theWind = FrontWindow ();
X	if ((te = GetEWindowTE (theWind)) == nil)
X		return;				/* not an edit window */
X	font = (**te).txFont;
X	size = (**te).txSize;
X	just = (**te).just;
X	wrap = (**te).crOnly;
X
X	switch (item)
X	{
X
X	case wordWrap:
X		wrap = 0;
X		break;
X
X	case noWrap:
X		wrap = -1;
X		break;
X
X	case leftJust:
X		just = teJustLeft;
X		break;
X
X	case centerJust:
X		just = teJustCenter;
X		break;
X
X	case rightJust:
X		just = teJustRight;
X		break;
X	}
X	SetEWindowStyle (theWind, font, size, wrap, just);
X	SetTextMenus (false);
X}
X
X
X/*
X	Handle selection of AboutI item from Apple menu
X*/
X
XDoAbout ()
X{
X	(void) Alert (aboutAlrt, nil);
X}
X
X
X/*
X	Background procedure.  Check front window, reset menus if it
X	changes.  The menu bar doesn't need redrawing by SetTextMenus
X	if the previous and current front window are either both edit
X	windows or both not edit windows.  This check eliminates some
X	needless menu flashing.
X*/
X
XCheckFront ()
X{
X	if (FrontWindow () != lastFront)
X	{
X		SetNonTextMenus ();
X		if (IsEWindow (FrontWindow ()) == IsEWindow (lastFront))
X			SetTextMenus (false);
X		else
X			SetTextMenus (true);
X		lastFront = FrontWindow ();
X	}
X}
X
X
Xmain ()
X{
X
X/*
X	Initialize TransSkel, create menus and install handlers.
X*/
X
X	SkelInit ();
X
X	SkelApple ("\pAbout DumbEditI", DoAbout);
X
X	fileMenu = NewMenu (1000, "\pFile");
X	AppendMenu (fileMenu, "\pNew/N;Open.../O;Close/K;(-;Save/S;Save As...");
X	AppendMenu (fileMenu, "\pSave a Copy As...;Revert/R;(-;Quit/Q");
X	SkelMenu (fileMenu, DoFileMenu, nil);
X
X	editMenu = NewMenu (1001, "\pEdit");
X	AppendMenu (editMenu, "\pUndo/Z;(-;Cut/X;Copy/C;Paste/V;Clear");
X	SkelMenu (editMenu, EWindowEditOp, nil);
X
X	fontMenu = NewMenu (1002, "\pFont");
X	DisableItem (fontMenu, 0);
X	AddResMenu (fontMenu, 'FONT');
X	SkelMenu (fontMenu, DoFontMenu, nil);
X
X	sizeMenu = NewMenu (1003, "\pSize");
X	DisableItem (sizeMenu, 0);
X	AppendMenu (sizeMenu, "\p9 Point;10 Point;12 Point;14 Point");
X	AppendMenu (sizeMenu, "\p18 Point;20 Point;24 Point; 48 Point");
X	SkelMenu (sizeMenu, DoSizeMenu, nil);
X
X	formatMenu = NewMenu (1004, "\pFormat");
X	DisableItem (formatMenu, 0);
X	AppendMenu (formatMenu, "\pWord Wrap;No Word Wrap;(-;Left;Center;Right");
X	SkelMenu (formatMenu, DoFormatMenu, nil);
X
X	SetNonTextMenus ();
X	SetTextMenus (true);
X
X/*
X	Do TransEdit-specific setup:  set creator for any files created,
X	set default text style and event notification procedures for
X	new windows.
X*/
X
X	SetEWindowCreator ('DUMB');
X	SetEWindowStyle (nil, monaco, 9, 0, teJustLeft);
X	SetEWindowProcs (nil, Key, Activate, Close);
X
X/*
X	Process events until user quits,
X	then clean up and exit
X*/
X
X	SkelBackground (CheckFront);
X	SkelMain ();
X	SkelClobber ();
X}
SHAR_EOF
exit