dubois@uwmacc.UUCP (Paul DuBois) (11/25/86)
#!/bin/sh # shar: Shell Archiver # Run the following text with /bin/sh to create: # TDDemos/EventLog.c # TDDemos/MiniDisplay.c # # MODERATOR'S NOTE: Tab-width is 4 # sed 's/^X//' << 'SHAR_EOF' > TDDemos/EventLog.c X/* X EventLog - TransDisplay event-logging demonstration program X X The project should include EventLog.c (this file), TransDisplay.c X (or a project made from TransDisplay.c), TransSkel.c (or a project X made from TransSkel.c), and MacTraps. X X 8 November 1986 Paul DuBois X*/ X X X# include <EventMgr.h> X# include <MenuMgr.h> X# include <ControlMgr.h> X# include <FontMgr.h> X# include "TransDisplay.h" X X Xenum /* declare zoom box part codes */ X{ X inZoomIn = 7, X inZoomOut X}; X X X# define maxButton 14 X X X# define helpTextRes 1000 /* help text resource number */ X# define aboutAlrtRes 1000 /* About... alert resource number */ X X Xtypedef enum /* Menu resource numbers */ X{ X fileMenuRes = 1000, X editMenuRes, X logMenuRes X}; X X Xtypedef enum /* Window resource numbers */ X{ X logWindRes = 1000, X helpWindRes, X selectWindRes X}; X X Xtypedef enum /* File menu item numbers */ X{ X showLog = 1, /* make windows visible/bring to front */ X showHelp, X showSelect, 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 Xtypedef enum /* Log menu item numbers */ X{ X logEvents = 1, /* whether events are logged */ X excludeLWind, X /* --- */ X flushLog = 4, /* flush log output */ X /* --- */ X wrapStyle = 6, /* word wrap or not */ X /* --- */ X leftJust = 8, /* justification */ X centerJust, X rightJust, X /* --- */ X small = 12, /* text size */ X medium, X large, X /* --- */ X top = 16, /* scroll home */ X bottom /* scroll to bottom */ X}; X X Xtypedef struct CtrlInfo X{ X Point loc; /* upper left of control */ X Str255 title; /* control title */ X Boolean *flagAddr; /* associated boolean */ X ControlHandle ctrl; /* associated control */ X struct CtrlInfo *subInfo; /* subsidiary control */ X X} CtrlInfo; X X XWindowPtr selectWind; /* event selection window */ XWindowPtr helpWind; /* help text window */ XWindowPtr logWind; /* log output window */ XMenuHandle fileMenu; XMenuHandle editMenu; XMenuHandle logMenu; XBoolean reportEvents; /* report events or not */ XBoolean excludeLog; /* exclude log window events or not */ Xint logFont; Xint logSize; Xint logWrap; Xint logJust; X X XBoolean /* event type selection flags */ X rMouseDown = true, X rMouseMods = false, X rMouseWind = true, X rMouseLoc = false, X rMousePart = true, X rMouseSys = false, X rMouseUp = false, X rKeyDown = true, X rKDMods = false, X rAutoKey = true, X rAKMods = false, X rUpdate = true, X rActivate = true, X rDisk = true; X X X/* X Control information. The last field is used to tell which controls X are "owned" by another. When the owner is unchecked, all the owned X controls go dim. X*/ X XCtrlInfo ctrlInfo [maxButton] = X{ X { { 5, 10}, "\pMouse Down", &rMouseDown, nil, nil }, X { { 25, 30}, "\pModifiers", &rMouseMods, nil, &ctrlInfo[0] }, X { { 45, 30}, "\pWindow", &rMouseWind, nil, &ctrlInfo[0] }, X { { 65, 30}, "\pLocation", &rMouseLoc, nil, &ctrlInfo[0] }, X { { 85, 30}, "\pPart Code", &rMousePart, nil, &ctrlInfo[0] }, X { { 105, 30}, "\pSystem Clicks", &rMouseSys, nil, &ctrlInfo[0] }, X { { 125, 10}, "\pMouse Up", &rMouseUp, nil, nil }, X { { 5, 160}, "\pKey Down", &rKeyDown, nil, nil }, X { { 25, 180}, "\pModifiers", &rKDMods, nil, &ctrlInfo[7] }, X { { 45, 160}, "\pAutoKey", &rAutoKey, nil, nil }, X { { 65, 180}, "\pModifiers", &rAKMods, nil, &ctrlInfo[9] }, X { { 85, 160}, "\pUpdate", &rUpdate, nil, nil }, X { { 105, 160}, "\pActivate", &rActivate, nil, nil }, X { { 125, 160}, "\pDisk", &rDisk, nil, nil } X}; X X/* Window that was in front last time checked */ X XWindowPtr lastFront = nil; X X X/* X Print information about a window. If it's a window with a title, X print the title. Print whether it's a X desk accessory window. X*/ X XWindowInfo (theWind) XWindowPeek theWind; X{ XStr255 title; X X GetWTitle (theWind, title); X if (title[0] != 0) /* window has title */ X { X DisplayChar (' '); X DisplayString (title); X } X X if (theWind->windowKind < 0) X DisplayString ("\p (DA)"); X} X X XModifiers (mods) Xint mods; X{ X DisplayString ("\p mods (0x"); X DisplayHexInt (mods); X DisplayChar (')'); X} X X XMouseLoc (thePt, thePort) XPoint thePt; XGrafPtr thePort; X{ XGrafPtr savePort; X X GetPort (&savePort); X SetPort (thePort); X GlobalToLocal (&thePt); X SetPort (savePort); X if (rMouseLoc) X { X DisplayString ("\p loc ("); X DisplayInt (thePt.h); X DisplayString ("\p, "); X DisplayInt (thePt.v); X DisplayChar (')'); X } X} X X X/* X Mouse click. Get the window that the click occurred in, and the X part of the window. X X Make sure to get all the part codes! (incl. zoom box stuff) X*/ X XReportMouse (theEvent) XEventRecord *theEvent; X{ XPoint evtPt; Xint evtPart; XGrafPtr evtPort; X X evtPt = theEvent->where; X evtPart = FindWindow (evtPt, &evtPort); X if (excludeLog && evtPort == logWind) X return; X DisplayString ("\pMouse click"); X X switch (evtPart) X { X X/* X Click in a desk accessory window. X*/ X case inSysWindow: X if (rMouseSys) X { X if (rMousePart) X DisplayString ("\p in system window:"); X if (rMouseWind) X WindowInfo (evtPort); X MouseLoc (evtPt, evtPort); X } X break; X X/* X Click in desk top. X*/ X case inDesk: X if (rMousePart) X DisplayString ("\p in desktop"); X break; X X/* X Click in menu bar. X*/ X case inMenuBar: X if (rMousePart) X DisplayString ("\p in menu bar"); X break; X X/* X Click in grow box. X*/ X case inGrow: X if (rMousePart) X DisplayString ("\p in grow region:"); X if (rMouseWind) X WindowInfo (evtPort); X MouseLoc (evtPt, evtPort); X break; X X/* X Click in title bar. X*/ X case inDrag: X if (rMousePart) X DisplayString ("\p in drag region:"); X if (rMouseWind) X WindowInfo (evtPort); X break; X X/* X Click in close box. X*/ X case inGoAway: X if (rMousePart) X DisplayString ("\p in close box:"); X if (rMouseWind) X WindowInfo (evtPort); X break; X X/* X Click in zoom-in box. X*/ X case inZoomIn: X if (rMousePart) X DisplayString ("\p in zoom-in box:"); X if (rMouseWind) X WindowInfo (evtPort); X break; X X/* X Click in zoom-out box. X*/ X case inZoomOut: X if (rMousePart) X DisplayString ("\p in zoom-out box:"); X if (rMouseWind) X WindowInfo (evtPort); X break; X X/* X Click in content region. X X (Might also check in in control, and if so, print control information) X*/ X case inContent: X if (rMousePart) X DisplayString ("\p in content region:"); X if (rMouseWind) X WindowInfo (evtPort); X MouseLoc (evtPt, evtPort); X break; X X } X if (rMouseMods) X Modifiers (theEvent->modifiers); X DisplayLn (); X} X X XReportKey (what, c, mods, modFlag) Xint what; Xchar c; Xint mods; XBoolean modFlag; X{ X if (what == keyDown) X DisplayString ("\pKey down: char '"); X else X DisplayString ("\pAutokey: char '"); X DisplayChar (c); X DisplayString ("\p' "); X if (modFlag) X Modifiers (mods); X DisplayLn (); X} X X XReportActivate (theWind, mods) XWindowPtr theWind; Xint mods; X{ X if ((mods & activeFlag) != 0) X DisplayString ("\pActivate:"); X else X DisplayString ("\pDeactivate:"); X WindowInfo (theWind); X DisplayLn (); X} X X XReportUpdate (theWind) XWindowPtr theWind; X{ X DisplayString ("\pUpdate:"); X WindowInfo (theWind); X DisplayLn (); X} X X X/* X General event logger X*/ X XLogEvent (theEvt) XEventRecord *theEvt; X X{ Xregister EventRecord *theEvent; XPoint evtPt; XGrafPtr evtPort; Xregister int evtPart; Xregister char evtChar; Xregister int evtMods; XRect r; X X if (reportEvents == false) X return (false); /* don't do anything */ X theEvent = theEvt; X evtPt = theEvent->where; X switch (theEvent->what) X { X X/* X Mouse click. X*/ X case mouseDown: X if (rMouseDown) X ReportMouse (theEvent); X break; X X case mouseUp: X if (rMouseUp) X DisplayString ("\pMouse up\r"); X break; X X/* X Key event. X*/ X case keyDown: X if (excludeLog && FrontWindow () == logWind) X break; X if (rKeyDown) X { X evtChar = theEvent->message & charCodeMask; X evtMods = theEvent->modifiers; X ReportKey (keyDown, evtChar, evtMods, rKDMods); X } X break; X X case autoKey: X if (excludeLog && FrontWindow () == logWind) X break; X if (rKeyDown) X { X evtChar = theEvent->message & charCodeMask; X evtMods = theEvent->modifiers; X ReportKey (autoKey, evtChar, evtMods, rAKMods); X } X break; X X/* X Update a window. If it's an update for the log window, invalidate X it, because the message is written and will cause a scroll BEFORE X the window actually gets updated. This means that part of what X needs redrawing will be scrolled out of the update region and won't X be redrawn properly. Invalidating the entire port is wasteful but X makes sure the whole window can be drawn properly. X*/ X case updateEvt: X if ((WindowPtr) theEvent->message == logWind) X { X SetPort (logWind); X InvalRect (&logWind->portRect); X } X if (excludeLog && (WindowPtr) theEvent->message == logWind) X break; X if (rUpdate) X ReportUpdate (theEvent->message); X break; X X/* X Activate or deactivate a window. X*/ X case activateEvt: X if (excludeLog && (WindowPtr) theEvent->message == logWind) X break; X if (rActivate) X ReportActivate (theEvent->message, theEvent->modifiers); X break; X X/* X handle inserts of uninitialized disks X*/ X case diskEvt: X if (rDisk) X { X DisplayString ("\pDisk insertion"); X if (HiWord (theEvent->message) != noErr) X { X DisplayString ("\p (needs initializing)"); X } X DisplayLn (); X } X break; X X } X return (false); X} X X X/* X Background procedure. Check front window, reset edit menu if window X changes from an application window to a non-application window. X Disable the Edit menu whenever an application window is active, X enable it otherwise. X Also called whenever it is known that the active window has changed. X*/ X XCheckFront () X{ XWindowPtr curWind; Xint theKind; XBoolean lastIsApp = false, X curIsApp = false; X X curWind = FrontWindow (); X if (IsDWindow (lastFront) || lastFront == selectWind) X lastIsApp = true; X if (IsDWindow (curWind) || curWind == selectWind) X curIsApp = true; X if (lastFront != curWind) X { X if (IsDWindow (lastFront) || lastFront == selectWind) X lastIsApp = true; X if (IsDWindow (curWind) || curWind == selectWind) X curIsApp = true; X if (lastIsApp != curIsApp) X { X theKind = 0; X if (curWind != nil) X theKind = ((WindowPeek) curWind)->windowKind; X if (curWind == nil || theKind < 0) /* no window or DA in front */ X EnableItem (editMenu, 0); X else X DisableItem (editMenu, 0); X DrawMenuBar (); X } X lastFront = curWind; X } X} X X X/* ------------------------------------------------------------ */ X/* Event Selection Window Handler Routines */ X/* ------------------------------------------------------------ */ X X X/* X Activate event procedure for both display windows and the checkbox X window. X*/ X XActivate (active) XBoolean active; X{ X CheckFront (); X} X X/* X Update window. This is easy, just draw the controls. X*/ X XUpdate (resized) XBoolean resized; /* ignored */ X{ X DrawControls (selectWind); X} X X X/* X Handle hits in check boxes: X Toggle check box, sync the associated flag, and enable or disable X any subsidiary check boxes accordingly. (Subsidiaries have X information in the control structure that points back to the owner X check box.) X X*/ X XMouse (thePt, t, mods) XPoint thePt; Xlong t; Xint mods; X{ XControlHandle ctl; XCtrlInfo *ci; XBoolean val; Xint i; X X if (FindControl (thePt, selectWind, &ctl)) X { X if (TrackControl (ctl, thePt, nil)) X { X ci = (CtrlInfo *) GetCRefCon (ctl); X val = !GetCtlValue (ctl); X *(ci->flagAddr) = val; X SetCtlValue (ctl, val); X X /* enable/disable any subsidiaries */ X X for (i = 0; i < maxButton; ++i) X { X if (ctrlInfo[i].subInfo->ctrl == ci->ctrl) X HiliteControl (ctrlInfo[i].ctrl, val ? 0 : 255); X } X } X } X} X X X/* X File menu handler X*/ X XDoFileMenu (item) Xint item; X{ X switch (item) X { X case showHelp: X SelectWindow (helpWind); X ShowWindow (helpWind); X break; X X case showSelect: X SelectWindow (selectWind); X ShowWindow (selectWind); X break; X X case showLog: X SelectWindow (logWind); X ShowWindow (logWind); X break; X X case quit: X SkelWhoa (); X break; X X } X} X X/* X Put the right check marks in the Log menu X*/ X XSetLogMenu () X{ X CheckItem (logMenu, logEvents, reportEvents); X CheckItem (logMenu, excludeLWind, excludeLog); X CheckItem (logMenu, wrapStyle, logWrap >= 0); X CheckItem (logMenu, leftJust, logJust == teJustLeft); X CheckItem (logMenu, centerJust, logJust == teJustCenter); X CheckItem (logMenu, rightJust, logJust == teJustRight); X CheckItem (logMenu, small, logSize == 9); X CheckItem (logMenu, medium, logSize == 12); X CheckItem (logMenu, large, logSize == 24); X} X X X/* X Set display style of log window X*/ X XSetStyle () X{ X SetDWindowStyle (logWind, logFont, logSize, logWrap, logJust); X SetLogMenu (); X} X X X/* X Log menu handler X*/ X XDoLogMenu (item) Xint item; X{ X switch (item) X { X case logEvents: X reportEvents = !reportEvents; X SetLogMenu (); X break; X X case excludeLWind: X excludeLog = !excludeLog; X SetLogMenu (); X break; X X case flushLog: X FlushDWindow (logWind, 32767L); X break; X X case wrapStyle: X logWrap = (logWrap >= 0 ? -1 : 0); X SetStyle (); X break; X X case leftJust: X logJust = teJustLeft; X SetStyle (); X break; X X case centerJust: X logJust = teJustCenter; X SetStyle (); X break; X X case rightJust: X logJust = teJustRight; X SetStyle (); X break; X X case small: X logFont = monaco; X logSize = 9; X SetStyle (); X break; X X case medium: X logFont = systemFont; X logSize = 12; X SetStyle (); X break; X X case large: X logFont = geneva; X logSize = 24; X SetStyle (); X break; X X case top: X SetDWindowPos (logWind, 0); X break; X X case bottom: X SetDWindowPos (logWind, 32767); X break; X } X} X X X/* X Handle selection of AboutI item from Apple menu X*/ X XDoAbout () X{ X (void) Alert (aboutAlrtRes, nil); X} X X X/* X Dispose of event selection window (and controls) X*/ X XWClobber () X{ X DisposeWindow (selectWind); X} X X X X/* X Create controls X*/ X XMakeControls (theWind) XWindowPtr theWind; X{ Xint i; XCtrlInfo *ci; XRect r; X X for (i = 0; i < maxButton; ++i) X { X ci = &ctrlInfo[i]; X SetRect (&r, ci->loc.h, ci->loc.v, X ci->loc.h + StringWidth (ci->title) + 30, X ci->loc.v + 20); X ci->ctrl = NewControl (theWind, &r, ci->title, true, X *(ci->flagAddr), 0, 1, X checkBoxProc, ci); X } X ValidRect (&theWind->portRect); X} X X Xmain () X{ XHandle h; X X SkelInit (); X SkelApple ("\pAbout EventLogI", DoAbout); X X fileMenu = GetMenu (fileMenuRes); X SkelMenu (fileMenu, DoFileMenu, nil); X X editMenu = GetMenu (editMenuRes); X DisableItem (editMenu, 0); X SkelMenu (editMenu, nil, nil); X X logMenu = GetMenu (logMenuRes); X SkelMenu (logMenu, DoLogMenu, nil); X X/* X Create windows and install handlers. X*/ X X SetDWindowNotify (nil, Activate); X X helpWind = GetNewDWindow (helpWindRes, -1L); X SetDWindowStyle (helpWind, 0, 0, 0, teJustLeft); X X h = GetResource ('TEXT', helpTextRes); /* read help text */ X HLock (h); /* lock it and write to window */ X DisplayText (*h, GetHandleSize (h)); X HUnlock (h); X ReleaseResource (h); /* done with it, so goodbye */ X SetDWindowPos (helpWind, 0); /* scroll back to top */ X ShowWindow (helpWind); X X logWind = GetNewDWindow (logWindRes, -1L); X X SkelEventHook (LogEvent); X reportEvents = true; X excludeLog = false; X X logFont = monaco; X logSize = 9; X logWrap = 0; X logJust = teJustLeft; X SetStyle (); X ShowWindow (logWind); X X selectWind = GetNewWindow (selectWindRes, nil, -1L); X X SkelWindow (selectWind, /* the window */ X Mouse, /* mouse click handler */ X nil, /* key clicks are ignored */ X Update, /* window updating procedure */ X Activate, /* window activate/deactivate procedure */ X nil, /* hide window */ X WClobber, /* window disposal procedure */ X nil, /* idle proc */ X true); /* irrelevant */ X X MakeControls (selectWind); X X/* X Process events until user quits, X then clean up and exit X*/ X X CheckFront (); X SkelBackground (CheckFront); X SkelMain (); X SkelClobber (); X} SHAR_EOF sed 's/^X//' << 'SHAR_EOF' > TDDemos/MiniDisplay.c X/* X MiniDisplay - TransDisplay Demonstration. Very simple: just X demonstrates the various output calls. X X The project should include MiniDisplay.c (this file), X TransDisplay.c (or a project made from TransDisplay.c), X TransSkel.c (or a project made from TransSkel.c), and MacTraps. X X 4 October 1986 Paul DuBois X*/ X X# include <MenuMgr.h> X# include "TransDisplay.h" X X X XDoFileMenu (item) Xint item; /* ignored - there's only Quit */ X{ X SkelWhoa (); /* tell SkelMain to quit */ X} X X Xmain () X{ XRect r; XMenuHandle m; XWindowPtr w; X X SkelInit (); /* initialize */ X SkelApple (nil, nil); /* handle desk accessories */ X X m = NewMenu (2, "\pFile"); /* create menu */ X AppendMenu (m, "\pQuit/Q"); X SkelMenu (m, DoFileMenu, nil); /* tell TransSkel to handle it */ X X SetRect (&r, 100, 75, 400, 250); X w = NewDWindow (&r, "\pMiniDisplay", false, -1L, false, 0L); X X DisplayString ("\pThis is MiniDisplay, a minimal demonstration of "); X DisplayString ("\pTransDisplay. The following types of output may "); X DisplayString ("\pbe written with the built-in output calls:\r"); X X DisplayString ("\p\rArbitrary length text: "); X DisplayText ("Some text", 9L); X DisplayString ("\p\rString: "); X DisplayString ("\p\"\\pThis is a string.\""); X DisplayString ("\p\rChar: '"); X DisplayChar ('x'); X DisplayString ("\p' Hex char: "); X DisplayHexChar ('x'); X DisplayString ("\p\rInt: "); X DisplayInt (1023); X DisplayString ("\p Hex int: "); X DisplayHexInt (1023); X DisplayString ("\p\rLong: "); X DisplayLong (32768L); X DisplayString ("\p Hex long: "); X DisplayHexLong (32768L); X DisplayString ("\p\rBoolean: "); X DisplayBoolean (true); X DisplayString ("\p, "); X DisplayBoolean (false); X DisplayString ("\p\rCarriage return. "); X DisplayString ("\p\r\rSelect Quit from the File menu to exit."); X SetDWindowPos (w, 0); /* scroll back to top */ X ShowWindow (w); X X SkelMain (); /* loop 'til Quit selected */ X SkelClobber (); /* clean up */ X} SHAR_EOF exit