home *** CD-ROM | disk | FTP | other *** search
- /*________________________________________________________________________________
- BadWindows.c
-
- Copyright © 1993-1995 Onyx Technology - All rights reserved
-
- Routines to handle the text window displaying a status of what BadAPPL
- is doing.
- ________________________________________________________________________________*/
-
- #ifndef _H_BadAPPL
- #include "BadAPPL.h"
- #endif
- #ifndef _H_BadWindows
- #include "BadWindows.h"
- #endif
- #ifndef _H_BadUtils
- #include "BadUtils.h"
- #endif
- #ifndef _H_BadGlobs
- #include "BadGlobs.h"
- #endif
-
- // Prototypes for private functions.
- static void AdjustText (void);
- static void SetVScroll(void);
- static void SetView (WindowPtr w);
- static pascal void ScrollProc (ControlHandle theControl, short theCode);
-
- // This #define is in Universal Headers so we can use it to determine if
- // we need ControlActionUPP defined or if it's already available.
- #ifndef OLDROUTINENAMES
- typedef ProcPtr ControlActionUPP;
- #endif
-
- void SetUpWindows(void)
- {
- Rect viewRect;
- Rect vScrollRect;
-
- gStatusWindow = GetNewWindow(kTestWindowID, 0L, (WindowPtr) -1L);
- if (gStatusWindow)
- {
- SetPort(gStatusWindow);
- TextFont(monaco);
- TextSize(9);
- vScrollRect = (*gStatusWindow).portRect;
- vScrollRect.left = vScrollRect.right-15;
- vScrollRect.right += 1;
- vScrollRect.bottom -= 14;
- vScrollRect.top -= 1;
- gVScroll = NewControl( gStatusWindow, &vScrollRect, "\p", 1, 0, 0, 0,
- scrollBarProc, 0L);
-
- viewRect = qd.thePort->portRect;
- viewRect.right -= SBarWidth;
- viewRect.bottom -= SBarWidth;
- InsetRect(&viewRect, 4, 4);
- gTEH = TENew(&viewRect, &viewRect);
-
- SetView(qd.thePort);
- }
- else
- {DbugStr("\pGetNewWindow failed!");}
- }
-
-
- /*________________________________________________________________________________
-
- OutputString()
-
- info: Output a given string to the status window
- Note there is no status of whether the string was displayed or
- an error occured. It either makes it or it doesnt.
-
- entry: strID - id of the STR# resource to get the string from
- indxID - index into the STR# resource
-
- ________________________________________________________________________________*/
-
- void OutputString(short strID, short indxID, long theAddr)
- {
- Str255 string;
- Str255 tempStr;
- long finalTicks;
- THz savedZone;
-
- savedZone = GetZone();
- SetZone(gAppHeap); // make sure we use the BadAPPL zone
-
- GetIndString(string, strID, indxID); // get the appropriate string
-
- if (theAddr) // is there an address to add in?
- {
- HexToString(theAddr, tempStr); // yes, convert it to a string
- AppendString(string, tempStr); // and add it to the overall string
- }
-
- if (gTEH && (string[0] > 0)) // do we have gTEH and a string?
- {
- CheckTextSize(); // make sure we dont overflow gTEH
- TEInsert((Ptr) &string[1], string[0], gTEH); // push it into the TE record
- TEKey( 0x0D, gTEH); // and a return
- ShowSelect(); // and redraw everything if needed.
- }
-
- Delay((long) 30, &finalTicks); // lets pause so the user can read this
-
- SetZone(savedZone); // restore the previous zone
- }
-
- /*________________________________________________________________________________
-
- AdjustText()
-
- info: adjust the text in the gTEH record making sure we are scrolling
- if needed.
-
- ________________________________________________________________________________*/
- static void AdjustText(void)
- {
- short oldScroll, newScroll, delta;
-
- oldScroll = (**gTEH).viewRect.top - (**gTEH).destRect.top;
- newScroll = GetCtlValue(gVScroll) * (**gTEH).lineHeight;
- delta = oldScroll - newScroll;
- if (delta != 0)
- TEScroll(0, delta, gTEH);
- }
-
- /*________________________________________________________________________________
-
- CheckTextSize()
-
- info: Check the size of the text edit record and dont let it get too
- big! If it grows past 2k, then start cutting text away at the
- top 100 bytes at a time.
-
- ________________________________________________________________________________*/
- void CheckTextSize (void)
- {
- if ((**gTEH).teLength >= 2048) // are we bigger than 2k?
- {
- TESetSelect( 0, 100, gTEH); // set the selection range we want
- TEDelete(gTEH); // then chop off the first 100 bytes
- // adjust the selection/insertion point
- TESetSelect( 32768, 32768, gTEH); // by using an out of bounds limit
- // TextEdit will politely place us
- // at the end of the text.
- }
- }
-
-
- /*________________________________________________________________________________
-
- SetVScroll()
-
- info: set the vertical scroll bar to the correct value.
-
- ________________________________________________________________________________*/
- static void SetVScroll(void)
- {
- register short n;
-
- n = (**gTEH).nLines-gLinesInStatus;
-
- if ((**gTEH).teLength > 0 && (*((**gTEH).hText))[(**gTEH).teLength-1]=='\r')
- n++;
-
- SetCtlMax(gVScroll, n > 0 ? n : 0);
- }
-
- /*________________________________________________________________________________
-
- ShowSelect()
-
- info: update the text displayed in the gTEH record
-
- ________________________________________________________________________________*/
- void ShowSelect (void)
- {
- TEPtr teP;
- register short topLine, bottomLine, theLine;
-
- SetVScroll();
- AdjustText();
-
- topLine = GetCtlValue(gVScroll);
- bottomLine = topLine + gLinesInStatus;
-
- HLock((Handle)gTEH);
- teP = *gTEH;
- if ((teP->selStart < teP->lineStarts[topLine]) ||
- (teP->selStart >= teP->lineStarts[bottomLine]))
- {
- for (theLine = 0; teP->selStart >= teP->lineStarts[theLine]; theLine++);
- SetCtlValue(gVScroll, theLine - gLinesInStatus / 2);
- AdjustText();
- }
- HUnlock((Handle)gTEH);
-
- }
-
- /*________________________________________________________________________________
-
- SetView()
-
- info: update the viewing area of the gTEH record and recalc the text
- boundaries.
-
- ________________________________________________________________________________*/
- static void SetView (WindowPtr w)
- {
- TEPtr teP;
-
- HLock((Handle)gTEH);
- teP = *gTEH;
-
- teP->viewRect = w->portRect;
- teP->viewRect.right -= SBarWidth;
- teP->viewRect.bottom -= SBarWidth;
- InsetRect(&teP->viewRect, 4, 4);
-
- gLinesInStatus = (teP->viewRect.bottom-teP->viewRect.top)/teP->lineHeight;
- teP->viewRect.bottom = teP->viewRect.top + teP->lineHeight*gLinesInStatus;
- teP->destRect.right = teP->viewRect.right;
- HUnlock((Handle)gTEH);
-
- TECalText(gTEH);
- }
-
- /*________________________________________________________________________________
-
- UpdateWindow()
-
- info: update our text edit status window
-
- ________________________________________________________________________________*/
- void UpdateWindow (WindowPtr theWindow)
- {
- GrafPtr savePort;
-
- GetPort(&savePort);
- SetPort(theWindow);
-
- BeginUpdate(theWindow);
- EraseRect(&theWindow->portRect);
- DrawControls(theWindow);
- DrawGrowIcon(theWindow);
- TEUpdate(&theWindow->portRect, gTEH);
- EndUpdate(theWindow);
-
- SetPort(savePort);
- }
-
-
- /*________________________________________________________________________________
-
- ScrollProc()
-
- info: the scrolling proc used in TrackControl call on the vertical
- scroll bar.
-
- ________________________________________________________________________________*/
- static pascal void ScrollProc (ControlHandle theControl, short theCode)
- {
- short pageSize;
- short scrollAmt;
- short oldCtl;
-
- if (theCode == 0)
- return ;
-
- pageSize = ((**gTEH).viewRect.bottom-(**gTEH).viewRect.top) /
- (**gTEH).lineHeight - 1;
-
- switch (theCode) {
- case inUpButton:
- scrollAmt = -1;
- break;
- case inDownButton:
- scrollAmt = 1;
- break;
- case inPageUp:
- scrollAmt = -pageSize;
- break;
- case inPageDown:
- scrollAmt = pageSize;
- break;
- }
-
- oldCtl = GetCtlValue(theControl);
- SetCtlValue(theControl, oldCtl+scrollAmt);
-
- AdjustText();
-
- }
-
- /*________________________________________________________________________________
-
- DoContent()
-
- info: Handle a mouse down event in the content of our status window
- Note that even though we are using a text edit record for the output
- of the status strings, we do no allow editing of that record.
-
- ________________________________________________________________________________*/
- void DoContent(WindowPtr theWindow, EventRecord *theEvent)
- {
- short cntlCode;
- ControlHandle theControl;
- GrafPtr savePort;
-
- GetPort(&savePort);
- SetPort(theWindow);
-
- GlobalToLocal(&theEvent->where);
- if ((cntlCode = FindControl(theEvent->where, theWindow, &theControl)) == 0)
- {
- #ifdef _WE_WERE_EDITING_
- if (PtInRect(theEvent->where, &(**gTEH).viewRect))
- TEClick(theEvent->where, (theEvent->modifiers & shiftKey)!=0, gTEH);
- #endif //_WE_WERE_EDITING_
- }
- else if (cntlCode == inThumb)
- {
- TrackControl(theControl, theEvent->where, 0L);
- AdjustText();
- }
- else
- TrackControl(theControl, theEvent->where, (ControlActionUPP) &ScrollProc);
-
- SetPort(savePort);
- }
-
- /*________________________________________________________________________________
-
- MyGrowWindow()
-
- info: resize the status window according to the mouse point we kept
- as the last coords for the resize.
-
- ________________________________________________________________________________*/
- void MyGrowWindow(WindowPtr w, Point p)
- {
- GrafPtr savePort;
- long theResult;
- Rect oldHorizBar;
- Rect r;
-
- GetPort(&savePort);
- SetPort(w);
-
- oldHorizBar = w->portRect;
- oldHorizBar.top = oldHorizBar.bottom - (SBarWidth+1);
-
- SetRect(&r, 80, 80, qd.screenBits.bounds.right, qd.screenBits.bounds.bottom);
- theResult = GrowWindow(w, p, &r);
- if (theResult != 0)
- {
- SizeWindow( w, LoWord(theResult), HiWord(theResult), false);
-
- InvalRect(&w->portRect);
-
- SetView(w);
-
- EraseRect(&oldHorizBar);
-
- MoveControl(gVScroll, w->portRect.right - SBarWidth, w->portRect.top-1);
- SizeControl(gVScroll, SBarWidth+1, w->portRect.bottom - w->portRect.top-(SBarWidth-2));
- r = (**gVScroll).contrlRect;
- ValidRect(&r);
-
-
- SetVScroll();
- AdjustText();
-
- SetPort(savePort);
- }
- }
-
- /*________________________________________________________________________________
-
- CloseMyWindow()
-
- info: Lets hide our status window and get rid of the text edit
- record we have been using.
-
- ________________________________________________________________________________*/
- void CloseMyWindow(void)
- {
- HideWindow(gStatusWindow);
- TESetSelect(0, (**gTEH).teLength, gTEH);
- TEDelete(gTEH);
- SetVScroll();
- }
-