home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Carousel
/
CAROUSEL.cdr
/
mactosh
/
lang
/
lsc_tran
/
DumbEdit.c
next >
Wrap
Text File
|
1989-02-18
|
10KB
|
545 lines
/*
DumbEdit - Multiple-window TransEdit Demonstration.
The project should include DumbEdit.c (this file), TransEdit.c (or
a project made from TransEdit.c), FakeAlert.c, TransSkel.c (or a
project made from TransSkel.c) and MacTraps.
28 Oct 86 Paul DuBois
02 Feb 89 Modified to work with TransSkel 2.0 and TransEdit 2.0.
2-byte and 4-byte integer types are typedef'ed to
Integer and Longint to ease porting.
*/
# include <MenuMgr.h>
# include <FontMgr.h>
# include "TransEdit.h"
typedef int Integer; /* compiler 2-byte integer type */
typedef long Longint; /* compiler 4-byte integer type */
# define maxSize 8 /* no. font sizes made available */
# define hSize 300 /* horiz, vert size of new windows */
# define vSize 205
# define aboutAlrt 1000
typedef enum /* File menu item numbers */
{
new = 1, /* begin new window */
open, /* open existing file */
close, /* close file */
/* --- */
save = 5, /* save file */
saveAs, /* save under another name */
saveCopy, /* save a copy w/o switching file binding */
revert, /* revert to version on disk */
/* --- */
quit = 10
};
typedef enum /* Edit menu item numbers */
{
undo = 1,
/* --- */
cut = 3,
copy,
paste,
clear
};
typedef enum /* Format menu item numbers */
{
wordWrap = 1,
noWrap,
/* --- */
leftJust = 4,
centerJust,
rightJust
};
WindowPtr lastFront = nil; /* keeps track of front window */
MenuHandle fileMenu;
MenuHandle editMenu;
MenuHandle fontMenu;
MenuHandle sizeMenu;
MenuHandle formatMenu;
Integer sizes[maxSize] = { 9, 10, 12, 14, 18, 20, 24, 48 };
/*
Uncheck all the items in a menu
*/
UncheckMenu (theMenu)
MenuHandle theMenu;
{
register Integer i, nItems;
nItems = CountMItems (theMenu);
for (i = 1; i <= nItems; ++i)
{
CheckItem (theMenu, i, false);
SetItemStyle (theMenu, i, 0);
}
}
/*
Set the Font, Size and Format menus so that the items corresponding
to the text characteristics of the window are checked. If the
window isn't an edit window, dim all three menus.
*/
SetTextMenus (drawBar)
Boolean drawBar;
{
WindowPtr theWind;
Str255 wFontName;
Str255 mFontName;
register Integer i, nItems;
register TEHandle te;
theWind = FrontWindow ();
UncheckMenu (fontMenu); /* toss current check marks */
UncheckMenu (sizeMenu);
UncheckMenu (formatMenu);
if (!IsEWindow (theWind)) /* disable the menus */
{
DisableItem (fontMenu, 0);
DisableItem (sizeMenu, 0);
DisableItem (formatMenu, 0);
}
else
{
EnableItem (fontMenu, 0);
EnableItem (sizeMenu, 0);
EnableItem (formatMenu, 0);
te = GetEWindowTE (theWind);
/*
Check appropriate word wrap item
*/
CheckItem (formatMenu, (**te).crOnly < 0 ? noWrap : wordWrap, true);
/*
Check appropriate justification item
*/
switch ((**te).just)
{
case teJustLeft:
CheckItem (formatMenu, leftJust, true);
break;
case teJustRight:
CheckItem (formatMenu, rightJust, true);
break;
case teJustCenter:
CheckItem (formatMenu, centerJust, true);
break;
}
/*
Check appropriate font size item, and outline items for sizes
present in resource files
*/
for (i = 0; i < maxSize; ++i)
{
if ((**te).txSize == sizes[i])
CheckItem (sizeMenu, i + 1, true);
if (RealFont ((**te).txFont, sizes[i]))
SetItemStyle (sizeMenu, i + 1, outline);
else
SetItemStyle (sizeMenu, i + 1, 0); /* plain */
}
/*
Check appropriate font name item
*/
GetFontName ((**te).txFont, wFontName); /* name of window font */
nItems = CountMItems (fontMenu); /* # fonts in menu */
for (i = 1; i <= nItems; ++i)
{
GetItem (fontMenu, i, mFontName); /* get font name */
if (EqualString (wFontName, mFontName, false, true))
{
CheckItem (fontMenu, i, true);
break;
}
}
}
if (drawBar)
DrawMenuBar ();
}
/*
Set File/Edit menu items according to type of front window.
The general behavior is:
New and Open always enabled, since a new edit window can always be
opened.
Close enabled when an edit or DA window in front (i.e., when there's
a window at all).
Save enabled for edit windows not bound to a file, and edit windows
bound to a file when they're dirty (typed into, Edit menu used to
do something to them).
Save As and Save a Copy As enabled for edit windows.
Revert enabled for edit windows bound to a file when they're dirty.
Undo disabled when there's an edit window in front.
*/
SetNonTextMenus ()
{
WindowPtr theWind;
Integer theKind;
DisableItem (fileMenu, close); /* assume no window at all */
DisableItem (fileMenu, save);
DisableItem (fileMenu, saveAs);
DisableItem (fileMenu, saveCopy);
DisableItem (fileMenu, revert);
EnableItem (editMenu, undo);
theKind = 0;
if ((theWind = FrontWindow ()) != nil)
theKind = ((WindowPeek) theWind)->windowKind;
if (theKind < 0) /* DA in front */
{
EnableItem (fileMenu, close);
}
else if (IsEWindow (theWind)) /* edit window in front */
{
EnableItem (fileMenu, close);
EnableItem (fileMenu, saveAs);
EnableItem (fileMenu, saveCopy);
if (GetEWindowFile (theWind, nil) == false) /* not bound to file */
{
EnableItem (fileMenu, save);
}
else if (IsEWindowDirty (theWind)) /* bound - is it dirty? */
{
EnableItem (fileMenu, save);
EnableItem (fileMenu, revert);
}
DisableItem (editMenu, undo);
}
}
/*
Got an activate or deactivate. It doesn't matter which, really.
Set the text menus appropriately for the front window, and draw
the menu bar, as these menus might change state from enabled to
disabled or vice-versa.
*/
Activate (active)
Boolean active;
{
CheckFront ();
}
/*
Got a keyclick in an edit window.
*/
Key ()
{
SetNonTextMenus ();
}
/*
Close selected from File menu, or close box of edit window
clicked.
*/
Close ()
{
WindowPtr theWind;
GetPort (&theWind);
(void) EWindowClose (theWind);
CheckFront ();
}
MakeWind (bindToFile)
Boolean bindToFile;
{
Rect r;
static Integer windCount = 0;
Integer offset;
if (FrontWindow () == nil)
windCount = 0;
SetRect (&r, 0, 0, hSize, vSize);
offset = 50 + 25 * (windCount++ % 4);
OffsetRect (&r, offset, offset);
(void) NewEWindow (&r, nil, true, -1L, true, 0L, bindToFile);
}
/*
File menu handler
*/
DoFileMenu (item)
Integer item;
{
WindowPtr theWind;
theWind = FrontWindow ();
switch (item)
{
case new:
MakeWind (false);
break;
case open:
MakeWind (true);
break;
case close:
if (IsEWindow (theWind))
(void) EWindowClose (theWind);
else
CloseDeskAcc (((WindowPeek) theWind)->windowKind); /* DA in front */
break;
case save:
(void) EWindowSave (theWind);
break;
case saveAs:
(void) EWindowSaveAs (theWind);
break;
case saveCopy:
(void) EWindowSaveCopy (theWind);
break;
case revert:
(void) EWindowRevert (theWind);
break;
case quit:
if (ClobberEWindows () == true)
SkelWhoa ();
break;
}
SetNonTextMenus ();
}
/*
Handle Font menu items
*/
DoFontMenu (item)
Integer item;
{
Integer font;
TEHandle te;
WindowPtr theWind;
Str255 theFontName;
theWind = FrontWindow ();
if ((te = GetEWindowTE (theWind)) == nil)
return; /* not an edit window */
GetItem (fontMenu, item, theFontName);
GetFNum (theFontName, &font);
SetEWindowStyle (theWind, font, (**te).txSize, (**te).crOnly, (**te).just);
SetTextMenus (false);
}
/*
Handle Size menu items
*/
DoSizeMenu (item)
Integer item;
{
TEHandle te;
WindowPtr theWind;
theWind = FrontWindow ();
if ((te = GetEWindowTE (theWind)) == nil)
return; /* not an edit window */
SetEWindowStyle (theWind, (**te).txFont, sizes[item-1], (**te).crOnly, (**te).just);
SetTextMenus (false);
}
/*
Handle Format menu items
*/
DoFormatMenu (item)
Integer item;
{
Integer font, size, just, wrap;
TEHandle te;
WindowPtr theWind;
theWind = FrontWindow ();
if ((te = GetEWindowTE (theWind)) == nil)
return; /* not an edit window */
font = (**te).txFont;
size = (**te).txSize;
just = (**te).just;
wrap = (**te).crOnly;
switch (item)
{
case wordWrap:
wrap = 0;
break;
case noWrap:
wrap = -1;
break;
case leftJust:
just = teJustLeft;
break;
case centerJust:
just = teJustCenter;
break;
case rightJust:
just = teJustRight;
break;
}
SetEWindowStyle (theWind, font, size, wrap, just);
SetTextMenus (false);
}
/*
Handle selection of About╔ item from Apple menu
*/
DoAbout ()
{
(void) Alert (aboutAlrt, nil);
}
/*
Background procedure. Check front window, reset menus if it
changes. The menu bar doesn't need redrawing by SetTextMenus
if the previous and current front window are either both edit
windows or both not edit windows. This check eliminates some
needless menu flashing.
*/
CheckFront ()
{
if (FrontWindow () != lastFront)
{
SetNonTextMenus ();
if (IsEWindow (FrontWindow ()) == IsEWindow (lastFront))
SetTextMenus (false);
else
SetTextMenus (true);
lastFront = FrontWindow ();
}
}
main ()
{
/*
Initialize TransSkel, create menus and install handlers.
*/
SkelInit (6, nil);
SkelApple ("\pAbout DumbEdit╔", DoAbout);
fileMenu = NewMenu (1000, "\pFile");
AppendMenu (fileMenu, "\pNew/N;Open.../O;Close;(-;Save/S;Save As...");
AppendMenu (fileMenu, "\pSave a Copy As...;Revert/R;(-;Quit/Q");
(void) SkelMenu (fileMenu, DoFileMenu, nil, false);
editMenu = NewMenu (1001, "\pEdit");
AppendMenu (editMenu, "\pUndo/Z;(-;Cut/X;Copy/C;Paste/V;Clear");
(void) SkelMenu (editMenu, EWindowEditOp, nil, false);
fontMenu = NewMenu (1002, "\pFont");
DisableItem (fontMenu, 0);
AddResMenu (fontMenu, 'FONT');
(void) SkelMenu (fontMenu, DoFontMenu, nil, false);
sizeMenu = NewMenu (1003, "\pSize");
DisableItem (sizeMenu, 0);
AppendMenu (sizeMenu, "\p9 Point;10 Point;12 Point;14 Point");
AppendMenu (sizeMenu, "\p18 Point;20 Point;24 Point; 48 Point");
(void) SkelMenu (sizeMenu, DoSizeMenu, nil, false);
formatMenu = NewMenu (1004, "\pFormat");
DisableItem (formatMenu, 0);
AppendMenu (formatMenu, "\pWord Wrap;No Word Wrap;(-;Left;Center;Right");
(void) SkelMenu (formatMenu, DoFormatMenu, nil, true);
SetNonTextMenus ();
SetTextMenus (true);
/*
Do TransEdit-specific setup: set creator for any files created,
set default text style and event notification procedures for
new windows.
*/
SetEWindowCreator ('DUMB');
SetEWindowStyle (nil, monaco, 9, 0, teJustLeft);
SetEWindowProcs (nil, Key, Activate, Close);
/*
Process events until user quits,
then clean up and exit
*/
SkelBackground (CheckFront);
SkelMain ();
SkelClobber ();
}