home *** CD-ROM | disk | FTP | other *** search
- /************************************************************************************************/
- /* */
- /* Program Name: Stiletto */
- /* */
- /* File Name: Utilities.c */
- /* */
- /* © Apple Computer, Inc. 1991-1995 */
- /* All Rights Reserved */
- /* */
- /* Revision History: */
- /* */
- /* Date Who Modification */
- /* */
- /* 1991-06-30 Chris Halim Original version */
- /* 1995-06-26 Jaakko Railo Version 2.0 */
- /* */
- /************************************************************************************************/
-
- /****************************************** DESCRIPTION ******************************************
-
- *************************************************************************************************/
-
- /******************************************** HEADERS *******************************************/
-
- #include "Aliases.h"
- #include "Files.h"
- #include "Folders.h"
- #include "Fonts.h"
- #include "Gestalt.h"
- #include "Resources.h"
- #include "string.h"
- #include "TextUtils.h"
-
- #include "Constants.h"
- #include "Utilities.h"
-
- /****************************************** DEFINITIONS *****************************************/
-
- const char * nullString = "Nil";
-
- /****************************************** PROTOTYPES ******************************************/
-
- /******************************************** GLOBALS *******************************************/
-
- extern ModalFilterUPP gAlertUserFilterUPP;
- extern UserItemUPP gDrawDefaultUPP;
-
- /************************************************************************************************/
- /************************************************************************************************/
-
-
- #pragma segment Utilities
- Boolean IsAlias (Str255 fileName, short vRefNum, long dirID)
- {
- /**
- ** IsAlias() returns true if the file specified by fileName, vRefNum & dirID
- ** is an alias. Otherwise it will return false.
- **
- **/
-
- FInfo theFInfo;
- OSErr errCode;
- FSSpec theFileSpec;
-
- FSMakeFSSpec (vRefNum, dirID, fileName, &theFileSpec);
-
- errCode = FSpGetFInfo (&theFileSpec, &theFInfo);
- if (errCode != noErr) {
- AlertUser ("\pGetFInfo failed !", errCode);
- return (false);
- }
-
- return ((theFInfo.fdFlags & 0x8000) != 0); // IM VI, p.9-29, bit 15 -> alias flag
- }
-
-
- #pragma segment Utilities
- pascal Boolean AlertUserFilterProc(DialogPtr theDialog, EventRecord * theEvent, short * itemHit)
- {
- Boolean wasHandled = false;
- char key;
-
- switch (theEvent->what) {
- case keyDown :
- key = theEvent->message & charCodeMask;
- if ( theEvent->modifiers & cmdKey ) // Command key is down
- if (key == 'd') {
- *itemHit = kDebuggerButton;
- HiliteItem (theDialog, *itemHit, 1);
- wasHandled = true;
- break;
- }
- switch (key) {
- case crKey :
- case enterKey :
- *itemHit = ok;
- HiliteItem (theDialog, *itemHit, 1); // hilite the ok button
- wasHandled = true;
- break;
- }
- break;
- }
-
- return (wasHandled);
- }
-
-
- #pragma segment Utilities
- void AlertUser (Str255 pString, short errCode)
- {
- //
- // For future update: think of implementing STR#, instead of hardcoded err strings!
- //
-
- short itemHit;
- Str255 str1;
- DialogPtr theDialog;
- short itemType;
- Handle item;
- Rect box;
-
- SetCursor(&qd.arrow);
-
- theDialog = GetNewDialog (rUserAlertDLOG, nil, (WindowPtr)(-1L));
- if (theDialog == nil) {
- DebugStr ("\pUnable to find DLOG resource : rUserAlertDLOG !");
- return;
- }
- SetPort (theDialog);
-
- GetDItem(theDialog, kDefaultUserItem, &itemType, &item, &box);
- SetDItem(theDialog, kDefaultUserItem, itemType, (Handle) gDrawDefaultUPP, &box);
-
- NumToString ((long) errCode, str1);
-
- ParamText (pString, str1, "\p", "\p");
-
- ShowWindow (theDialog);
-
- ModalDialog (gAlertUserFilterUPP, &itemHit);
-
- if (itemHit == kDebuggerButton)
- DebugStr ("\p;sc;ip");
-
- DisposDialog (theDialog);
- }
-
-
- #pragma segment Utilities
- void SetPopupValue (DialogPtr theDialog, short itemNumber, short theValue)
- {
- short itemKind;
- Handle itemHand;
- Rect itemRect;
-
- GetDItem(theDialog, itemNumber, &itemKind, &itemHand, &itemRect);
-
- SetCtlValue ((ControlHandle) itemHand, theValue);
- }
-
-
- #pragma segment Utilities
- short GetPopupValue (DialogPtr theDialog, short itemNumber)
- {
- short itemKind;
- Handle itemHand;
- Rect itemRect;
-
- GetDItem(theDialog, itemNumber, &itemKind, &itemHand, &itemRect);
-
- return (GetCtlValue ((ControlHandle) itemHand));
- }
-
-
- #pragma segment Utilities
- void HiliteItem (DialogPtr theDialog, short itemNumber, short hiliteState)
- {
- short itemKind;
- Handle itemHand;
- Rect itemRect;
-
- GetDItem(theDialog, itemNumber, &itemKind, &itemHand, &itemRect);
-
- HiliteControl ((ControlHandle) itemHand, hiliteState);
- }
-
-
- #pragma segment Utilities
- void BlinkItem (DialogPtr theDialog, short itemNumber)
- {
- short itemKind;
- Handle itemHand;
- Rect itemRect;
- long finalTicks;
-
- GetDItem (theDialog, itemNumber, &itemKind, &itemHand, &itemRect);
-
- HiliteControl ((ControlHandle) itemHand, 1);
- Delay (8, &finalTicks);
- HiliteControl ((ControlHandle) itemHand, 0);
- }
-
-
- #pragma segment Utilities
- Boolean ToggleCheckBox (DialogPtr theDialog, short itemNumber)
- {
- short itemKind;
- Handle itemHand;
- Rect itemRect;
- short tShort;
-
- GetDItem(theDialog, itemNumber, &itemKind, &itemHand, &itemRect);
-
- tShort = GetCtlValue ((ControlHandle) itemHand);
- tShort = ((tShort) ? 0 : 1);
-
- SetCtlValue ((ControlHandle) itemHand, tShort);
-
- return ((tShort) ? true : false);
- }
-
-
- #pragma segment Utilities
- pascal void DrawDefault (DialogPtr theDialog, short itemNumber)
- {
- short itemKind;
- Handle itemHand;
- Rect itemRect;
- PenState penState;
-
- GetDItem(theDialog, itemNumber, &itemKind, &itemHand, &itemRect);
-
- GetPenState(&penState);
-
- PenSize(3,3);
- PenPat(&(qd.black));
-
- FrameRoundRect(&itemRect,16,16);
-
- SetPenState(&penState);
- }
-
-
- #pragma segment Utilities
- pascal void DrawBox (DialogPtr theDialog, short itemNumber)
- {
- short itemKind;
- Handle itemHand;
- Rect itemRect;
- PenState penState;
-
- GetDItem(theDialog, itemNumber, &itemKind, &itemHand, &itemRect);
-
- GetPenState(&penState);
-
- PenSize(1,1);
- PenPat(&(qd.black));
-
- FrameRect(&itemRect);
-
- SetPenState(&penState);
- }
-
-
- #pragma segment Utilities
- void CenterRect (const Rect * bounds, Rect * theRect)
- {
- OffsetRect(theRect, - theRect->left, - theRect->top);
-
- OffsetRect(theRect, bounds->left + (bounds->right - bounds->left - theRect->right) / 2,
- bounds->top + (bounds->bottom - bounds->top - theRect->bottom) / 2);
- }
-
-
- #pragma segment Utilities
- void SetRectFromResource (Rect * theRect, short resID)
- {
- Handle tHandle = Get1Resource ('RECT', resID);
-
- BlockMove (*tHandle, theRect, sizeof (Rect));
-
- ReleaseResource (tHandle);
- }
-
-
- #pragma segment Utilities
- Boolean OutOfBound (const Rect * theRect, const Rect * bounds)
- {
- if ((theRect->top < bounds->top) || (theRect->top > bounds->bottom))
- return (true);
-
- if (theRect->right < bounds->left)
- return (true);
-
- if (theRect->left > bounds->right)
- return (true);
-
- if ((theRect->bottom - theRect->top) > (bounds->bottom - bounds->top))
- return (true);
-
- if ((theRect->right - theRect->left) > (bounds->right - bounds->left))
- return (true);
-
- return (false);
- }
-
-
- #pragma segment Utilities
- Boolean ToolFileStillExists (ConstStr255Param toolName)
- {
- short errCode;
- short vRefNum;
- long dirID;
- FSSpec theSpec;
- Boolean targetIsFolder, wasAliased;
-
- errCode = FindFolder (kOnSystemDisk, kExtensionFolderType, kDontCreateFolder, &vRefNum, &dirID);
- if (errCode != noErr) {
- AlertUser ("\pFindFolder failed !", errCode);
- return (false);
- }
-
- FSMakeFSSpec (vRefNum, dirID, toolName, &theSpec);
-
- errCode = ResolveAliasFile (&theSpec, true, &targetIsFolder, &wasAliased);
- if (errCode != noErr) {
- AlertUser ("\pResolveAliasFile failed !", errCode);
- return (false);
- }
-
- if (targetIsFolder) {
- AlertUser ("\pThis is really strange, dude ! The tool suddenly becomes a folder.", errCode);
- return (false);
- }
-
- return (true);
- }
-
-
- #pragma segment Utilities
- VBLTask * InstallVBLTask (ProcPtr taskFunc, short ticks)
- {
- OSErr errCode;
- VBLTask *taskPtr;
- VBLUPP theVBLUPP;
-
- taskPtr = (VBLTask *) NewPtr (sizeof (VBLTask));
-
- if (taskPtr != nil) {
- theVBLUPP = NewVBLProc (taskFunc);
- if (theVBLUPP != NULL) {
- taskPtr->qType = vType;
- taskPtr->vblAddr = theVBLUPP;
- taskPtr->vblCount = ticks;
- taskPtr->vblPhase = 0;
-
- errCode = VInstall ((QElemPtr) taskPtr);
- if (errCode != noErr) {
- DisposPtr ((Ptr) taskPtr);
- taskPtr = nil;
- }
- }
- }
-
- return (taskPtr);
- }
-
-
- #pragma segment Utilities
- void RemoveVBLTask (VBLTask * taskPtr)
- {
- VRemove ((QElemPtr) taskPtr);
- DisposPtr ((Ptr) taskPtr);
- }
-
-
- #pragma segment Utilities
- long GetAppFreeMem (void)
- {
- Size largestContig, grow;
-
- largestContig = MaxMem (&grow);
- return (FreeMem ());
- }
-
-
- #pragma segment Utilities
- long GetSysFreeMem (void)
- {
- Size largestContig, grow;
-
- largestContig = MaxMemSys (&grow);
- return (FreeMemSys ());
- }
-
-
- #pragma segment Utilities
- void SetCheckMark (short menuID, short theItem)
- {
- MenuHandle aMenuHandle = GetMenu (menuID);
-
- SetItemMark (aMenuHandle, theItem, checkMark);
- }
-
- #pragma segment Utilities
- pascal Boolean StandardFilterProc (DialogPtr theDialog, EventRecord * theEvent, short * itemHit)
- {
- Boolean wasHandled = false;
- char key;
-
- switch (theEvent->what) {
-
- case keyDown :
-
- key = theEvent->message & charCodeMask;
-
- switch (key) {
- case '.' :
- if (theEvent->modifiers & cmdKey) {
- *itemHit = cancel;
- BlinkItem (theDialog, *itemHit);
- wasHandled = true;
- }
- break;
-
- case 0x1B : // Esc
- *itemHit = cancel;
- BlinkItem (theDialog, *itemHit);
- wasHandled = true;
- break;
-
- case 0x0D : // CR
- case 0x03 : // Enter
- *itemHit = ok;
- BlinkItem (theDialog, *itemHit);
- wasHandled = true;
- break;
- }
- }
-
- return (wasHandled);
- }
-
-
- #pragma segment Utilities
- void SelectPreviousCell (ListHandle theList)
- {
- Cell tCell;
-
- if (theList) {
- SetPt (&tCell, 0, 0);
- if (LGetSelect (true, &tCell, theList)) {
- if (tCell.v != (*theList)->dataBounds.top) {
- LSetSelect (false, tCell, theList); // deselect current cell
- --tCell.v;
- LSetSelect (true, tCell, theList); // select next cell
- }
- }
- else {
- tCell.v = (*theList)->dataBounds.bottom - 1;
- LSetSelect (true, tCell, theList);
- }
-
- LAutoScroll (theList);
- }
- }
-
-
- #pragma segment Utilities
- void SelectNextCell (ListHandle theList)
- {
- Cell tCell;
-
- if (theList) {
- SetPt (&tCell, 0, 0);
- if (LGetSelect (true, &tCell, theList)) {
- if (tCell.v != (*theList)->dataBounds.bottom - 1) {
- LSetSelect (false, tCell, theList); // deselect current cell
- ++tCell.v;
- LSetSelect (true, tCell, theList); // select next cell
- }
- }
- else
- LSetSelect (true, tCell, theList);
-
- LAutoScroll (theList);
- }
- }
-
-
- #pragma segment Utilities
- void ClearAllSelectedCells (ListHandle theList)
- {
- Cell tCell;
-
- if (theList) {
- SetPt (&tCell, 0, 0);
- while (LGetSelect (true, &tCell, theList)) {
- LSetSelect (false, tCell, theList);
- }
- }
- }
-
-
- #pragma segment Utilities
- short GetQDVersion ()
- {
- return ((GetGestaltResult (gestaltQuickdrawVersion) >> 8) & 0xFF);
- }
-
-
- #pragma segment Utilities
- Boolean HasColorQD ()
- {
- return (((GetGestaltResult (gestaltQuickdrawVersion) >> 8) & 0xFF) != 0);
- }
-
-
- #pragma segment Utilities
- long GetGestaltResult (OSType gestaltSelector)
- {
- long gestaltResult;
-
- if (Gestalt(gestaltSelector, &gestaltResult) == noErr)
- return(gestaltResult);
- else
- return(0);
- }
-
-
- #pragma segment Utilities
- void ZoomWindowInCurrentDevice (WindowPtr theWindow, short maxWidth, short maxHeight,
- short zoomDirection, Boolean front)
- {
- WindowPtr savedPort;
- Rect contentRect, structureRect, deviceRect, newRect;
- short width, height, dx, dy;
- GDHandle maxDevice;
-
- GetPort (&savedPort);
- SetPort (theWindow);
- EraseRect (&theWindow->portRect);
-
- if ((zoomDirection == inZoomOut) && (GetQDVersion() > 0)) {
-
- contentRect = (**((WindowPeek) theWindow)->contRgn).rgnBBox;
- structureRect = (**((WindowPeek) theWindow)->strucRgn).rgnBBox;
-
- maxDevice = GetMaxDevice (&contentRect);
- deviceRect = (*maxDevice)->gdRect;
- if (maxDevice == GetMainDevice ())
- deviceRect.top += GetMBarHeight ();
-
- deviceRect.left += (contentRect.left - structureRect.left + 2);
- deviceRect.top += (contentRect.top - structureRect.top + 2);
- deviceRect.right -= (structureRect.right - contentRect.right + 2);
- deviceRect.bottom -= (structureRect.bottom - contentRect.bottom + 2);
- newRect = deviceRect;
-
- if (maxWidth)
- if ((width = deviceRect.right - deviceRect.left) > maxWidth)
- newRect.right = (newRect.left = contentRect.left) + maxWidth;
- if (maxHeight)
- if ((height = deviceRect.bottom - deviceRect.top) > maxHeight)
- newRect.bottom = (newRect.top = contentRect.top) + maxHeight;
- if ((dx = deviceRect.left - newRect.left) < 0)
- if ((dx = deviceRect.right - newRect.right) > 0)
- dx = 0;
- if ((dy = deviceRect.top - newRect.top) < 0)
- if ((dy = deviceRect.bottom - newRect.bottom) > 0)
- dy = 0;
- OffsetRect(&newRect, dx, dy);
-
- (*(WStateDataHandle)(((WindowPeek)theWindow)->dataHandle))->stdState = newRect;
- }
-
- ZoomWindow (theWindow, zoomDirection, front);
- SetPort (savedPort);
- }
-
-
- #pragma segment Utilities
- char * ConvertToCString (char * s1, const char * s2)
- {
- if (s2) {
- strncpy (s1, s2+1, s2[0]);
- s1[s2[0]] = '\0';
- }
- else
- strcpy (s1, nullString);
-
- return (s1);
- }
-
-
- #pragma segment Utilities
- MenuHandle GetPopupMHandle (DialogPtr dialog, short resID)
- {
- short itemKind;
- Handle itemHand;
- Rect itemRect;
-
- GetDItem (dialog, resID, &itemKind, &itemHand, &itemRect);
-
- return (MenuHandle) **((long**) (**((ControlHandle) itemHand)).contrlData);
- }
-
-
-