home *** CD-ROM | disk | FTP | other *** search
- /********************************************************************************
- * window.c
- *
- * Window Management Package
- *
- * Written by Paco Xander Nathan
- * ⌐1990, Motorola Inc. Public domain source code.
- ********************************************************************************/
-
- #include "applic.h"
- #include "window.h"
- #include "dialog.h"
- #include "context.h"
- #include "document.h"
- #include "error.h"
- #include "text.h"
- #include "list.h"
-
- #include "test.h"
-
-
- #define MAXPTRS 50
-
-
- typedef struct {
- short count;
- WindowPtr wind[MAXPTRS];
- } WindPtrList;
-
-
- /* Local Data Structures
- */
- static WindPtrList
- windList;
-
-
- /* External Data Structures
- */
- WindowPtr
- wPtrText = NULL,
- wPtrTest = NULL,
- wPtrGnos = NULL;
-
- Rect
- dragArea,
- growArea;
-
- Boolean
- windTrip = FALSE;
-
-
- /* Local Function Prototypes
- */
- #ifdef PROTOTYPES
- void WindInvalGrow (WindowPtr theWindow);
- #endif
-
-
- /* Invalidate the grow box and scroll bar rectangles for the given window
- */
- static void
- WindInvalGrow (theWindow)
- register WindowPtr theWindow;
- {
- Rect inval;
-
- /* Invalidate the verticle scroll
- */
- inval = theWindow->portRect;
- inval.left = inval.right - 16;
- InvalRect(&inval);
-
- /* Invalidate the horizontal scroll
- */
- inval = theWindow->portRect;
- inval.top = inval.bottom - 16;
- InvalRect(&inval);
- }
-
-
- /* Check if the front window is a modeless dialog that belongs to our application
- */
- Boolean
- WindIsDlog (theWindow)
- register WindowPtr theWindow;
- {
- register InfoPtr infoPtr = (InfoPtr) GetWRefCon(theWindow);
-
- return (theWindow != NULL) && (((WindowPeek) theWindow)->windowKind >= 0) && (infoPtr->kind == wkDlog);
- }
-
-
- /* Check if the front window belongs to our application
- */
- Boolean
- WindIsApp (theWindow)
- register WindowPtr theWindow;
- {
- return (theWindow != NULL) && (((WindowPeek) theWindow)->windowKind >= 0);
- }
-
-
- /* Check if the front window belongs to a DA
- */
- Boolean
- WindIsDA (theWindow)
- register WindowPtr theWindow;
- {
- return (theWindow != NULL) && (((WindowPeek) theWindow)->windowKind < 0);
- }
-
-
- /* Resize the window
- */
- void
- WindGrow (theWindow, theEvent)
- register WindowPtr theWindow;
- register EventRecord *theEvent;
- {
- GrafPtr savePort;
- register long newSize;
- register short newH, newV;
-
- GetPort(&savePort);
- SetPort(theWindow);
-
- if (newSize = GrowWindow(theWindow, theEvent->where, &growArea)) {
- newV = Max(HiWord(newSize), 20);
- newH = Max(LoWord(newSize), 20);
-
- /* Resize the window, adjust window dimensions
- */
- SizeWindow(theWindow, newH, newV, TRUE);
- WindAdjust(theWindow);
- InvalRect(&theWindow->portRect);
- }
-
- SetPort(savePort);
- }
-
-
- /* Zoom the window in or out
- */
- void
- WindZoom (theWindow, thePart)
- register WindowPtr theWindow;
- register short thePart;
- {
- GrafPtr savePort;
-
- GetPort(&savePort);
- SetPort(theWindow);
-
- /* Zoom the window and adjust the window rect dimensions
- */
- HideWindow(theWindow);
-
- ZoomWindow(theWindow, thePart, TRUE);
- WindAdjust(theWindow);
- /* WindInvalGrow(theWindow); */
-
- ShowWindow(theWindow);
- SetPort(savePort);
- }
-
-
- /* Reposition a window in the center of the screen
- */
- void
- WindCenter (theWindow)
- register WindowPtr theWindow;
- {
- register short hWind, vWind, hScrn, vScrn;
-
- hScrn = screenBits.bounds.right - screenBits.bounds.left;
- vScrn = screenBits.bounds.bottom - screenBits.bounds.top;
-
- hWind = theWindow->portRect.right - theWindow->portRect.left;
- vWind = theWindow->portRect.bottom - theWindow->portRect.top;
-
- /* Reset the horizontal and the vertical position
- */
- MoveWindow(theWindow, (hScrn - hWind) / 2, ((vScrn - vWind) / 3) + MBarHeight, FALSE);
- }
-
-
- /* Moves and sizes window to new location
- */
- void
- WindPlace (theWindow, left, top, right, bottom)
- register WindowPtr theWindow;
- register short left, top, right, bottom;
- {
- /* Move window to a specified location in global coordinates
- */
- MoveWindow(theWindow, left, top, FALSE);
- SizeWindow(theWindow, right - left, bottom - top, TRUE);
- WindAdjust(theWindow);
- }
-
-
- /* Stacks windows diagonally based on constraint rect
- */
- void
- WindTileStack ()
- {
- register WORD width, height, vStagger, hStagger, i, j;
- register WindowPtr theWindow = FrontWindow();
- register WORD numWindows = 0;
- GrafPtr savePort;
-
- GetPort(&savePort);
-
- /* Now we take a trip through the looking glass to build the list of
- * OUR windows
- */
- while (theWindow != NULL) {
- if (((WindowPeek) theWindow)->visible)
- windList.wind[numWindows++] = theWindow;
-
- theWindow = (WindowPtr) ((WindowPeek) theWindow)->nextWindow;
- }
-
- if ((numWindows > 1) && (numWindows <= MAXPTRS)) {
- height = (dragArea.bottom - dragArea.top) / 2;
- width = (dragArea.right - dragArea.left) / 2;
- vStagger = (height / (numWindows - 1)) + 1;
- hStagger = (width / (numWindows - 1)) + 1;
-
- for (i = numWindows - 1; i >= 0; i--) {
- if (theWindow = windList.wind[i]) {
- SetPort(theWindow);
- WindActivate(theWindow, FALSE);
-
- j = numWindows - i - 1;
- MoveWindow(theWindow, dragArea.left + (hStagger * j), dragArea.top + (vStagger * j) + 20, FALSE);
- }
- }
-
- SelectWindow(theWindow);
- }
-
- /* Thank you for coming, see you later
- */
- windTrip = FALSE;
- SetPort(savePort);
- }
-
-
- /* Draw an application window - switch on particular window to draw all the fancy
- * bells and whistles
- */
- void
- WindReDraw (theWindow)
- register WindowPtr theWindow;
- {
- register InfoPtr infoPtr = (InfoPtr) GetWRefCon(theWindow);
-
- /* Determine which kind of window this is, then lock and update
- */
- switch(infoPtr->kind) {
- case wkText:
- TEUpdate(&theWindow->portRect, infoPtr->item.text.teHdl);
- DrawControls(theWindow);
- break;
-
- case wkList:
- ListDraw(theWindow, TRUE);
- break;
-
- default:
- break;
- }
- }
-
-
- /* Update a window
- */
- void
- WindUpdate (theWindow)
- register WindowPtr theWindow;
- {
- GrafPtr savePort;
-
- GetPort(&savePort);
- SetPort(theWindow);
-
- BeginUpdate(theWindow);
-
- if (!EmptyRgn(theWindow->visRgn)) {
- ClipRect(&theWindow->portRect);
- EraseRect(&theWindow->portRect);
-
- WindReDraw(theWindow);
- /* DrawGrowIcon(theWindow); */
-
- /* Specially handle custom windows
- */
- if (theWindow == wPtrTest)
- TestDraw();
- }
-
- EndUpdate(theWindow);
- SetPort(savePort);
- }
-
-
- /* Handle an activate/deactivate event
- */
- void
- WindActivate (theWindow, isActive)
- register WindowPtr theWindow;
- register Boolean isActive;
- {
- register InfoPtr infoPtr = (InfoPtr) GetWRefCon(theWindow);
- GrafPtr savePort;
-
- GetPort(&savePort);
- SetPort(theWindow);
-
- /* Determine which kind of window this is, then lock and activate/deactivate
- */
- switch(infoPtr->kind) {
- case wkText:
- TextActivate(isActive, theWindow);
- break;
-
- case wkList:
- LActivate(isActive, infoPtr->item.list);
- break;
-
- default:
- break;
- }
-
- /* Redraw the grow box
- */
- /* WindInvalGrow(theWindow); */
-
- SetPort(savePort);
- }
-
-
- /* Determine which kind of window to adjust
- */
- void
- WindAdjust (theWindow)
- register WindowPtr theWindow;
- {
- register InfoPtr infoPtr = (InfoPtr) GetWRefCon(theWindow);
-
- switch (infoPtr->kind) {
- case wkText:
- TextAdjust(theWindow);
- break;
-
- case wkList:
- ListAdjust(theWindow);
- break;
-
- default:
- break;
- }
-
- /* Reset cliprect to new size.
- */
- ClipRect(&(theWindow->portRect));
- }
-
-
- /* Decide how to handle a mouse click in the content of a window
- */
- void
- WindContent (theWindow, theEvent)
- register WindowPtr theWindow;
- register EventRecord *theEvent;
- {
- register InfoPtr infoPtr = (InfoPtr) GetWRefCon(theWindow);
- GrafPtr savePort;
- Point thePoint;
-
- /* Convert to global coordinates
- */
- GetPort(&savePort);
- SetPort(theWindow);
-
- thePoint = theEvent->where;
- GlobalToLocal(&thePoint);
-
- if (theWindow == wPtrTest)
- TestClick(thePoint);
-
- switch (infoPtr->kind) {
- case wkText:
- /* Click in one of the text windows
- */
- TextContent(theWindow, theEvent->modifiers, thePoint);
- break;
-
- case wkList:
- /* Click in one of the list windows
- */
- ListContent(theWindow, theEvent->modifiers, thePoint);
- break;
-
- default:
- break;
- }
-
- SetPort(savePort);
- }
-
-
- /* Open or close a window context, depending on how "pleaseOpen" is set
- */
- void
- WindSwitch (theWindow, pleaseOpen)
- register WindowPtr theWindow;
- register Boolean pleaseOpen;
- {
- register WindowPtr frontWind = FrontWindow();
-
- /* Open/Close the specified window
- */
- if (pleaseOpen) {
- if (!((WindowPeek) theWindow)->visible) {
- ShowHide(theWindow, TRUE);
- windTrip = TRUE;
- }
-
- if (theWindow != frontWind)
- SelectWindow(theWindow);
- }
- else {
- if (((WindowPeek) theWindow)->visible) {
- WindActivate(theWindow, FALSE);
- ShowHide(theWindow, FALSE);
- windTrip = TRUE;
- }
- }
- }
-
-
- /* Allocate a new window based on the specified window template
- */
- WindowPtr
- WindAllocate (windID, isActive)
- register short windID;
- register Boolean isActive;
- {
- register WindowPtr theWindow;
- register InfoPtr infoPtr;
- register Rect bounds;
- static short offset = 0;
- GrafPtr savePort;
-
- /* Create the window record from the window template resources
- */
- if (laMachine.hasColor)
- theWindow = (WindowPtr) GetNewCWindow(windID, NULL, -1L);
- else
- theWindow = (WindowPtr) GetNewWindow(windID, NULL, -1L);
-
- if (theWindow) {
- GetPort(&savePort);
- SetPort(theWindow);
-
- if (infoPtr = (InfoPtr) ErrNewPtr(sizeof(InfoRecord))) {
- infoPtr->kind = wkNone;
- infoPtr->active = isActive;
- infoPtr->dirty = infoPtr->named = FALSE;
- SetWRefCon(theWindow, (long) infoPtr);
-
- offset += 10;
- bounds.left = dragArea.left + offset;
- bounds.top = dragArea.top + offset + 30;
- bounds.right = bounds.left + (theWindow->portRect.right - theWindow->portRect.left);
- bounds.bottom = bounds.top + (theWindow->portRect.bottom - theWindow->portRect.top);
- WindPlace(theWindow, bounds.left, bounds.top, bounds.right, bounds.bottom);
- }
- else {
- DisposeWindow(theWindow);
- theWindow = NULL;
- }
-
- SetPort(savePort);
- }
-
- return theWindow;
- }
-
-
- /* Close the specified window
- */
- void
- WindClose (theWindow)
- register WindowPtr theWindow;
- {
- register InfoPtr infoPtr = (InfoPtr) GetWRefCon(theWindow);
-
- if ((theWindow == wPtrText) || (theWindow == dPtrAnal))
- WindSwitch(theWindow, FALSE);
- else if (theWindow == wPtrTest)
- WindZapPort(theWindow, FALSE);
- else if (infoPtr->kind == wkText) {
- /* Deallocate a text window
- */
- DocClose(theWindow);
- WindZapPort(theWindow, FALSE);
- TEDispose(infoPtr->item.text.teHdl);
- DisposeControl(infoPtr->item.text.scroll);
- DisposPtr(infoPtr);
- DisposeWindow(theWindow);
- }
- else if (infoPtr->kind == wkList) {
- /* Deallocate a list window
- */
- WindZapPort(theWindow, FALSE);
- LDispose(infoPtr->item.list);
- DisposPtr(infoPtr);
- DisposeWindow(theWindow);
- }
- else if (infoPtr->kind == wkDlog) {
- /* Deallocate a modeless dialog window
- */
- DlogDispose(theWindow);
- }
- }
-
-
- /* Zap the specified port open or closed
- */
- void
- WindZapPort (wind, up)
- register WindowPtr wind;
- register Boolean up;
- {
- /* Can't give it all away...
- */
- ShowHide(wind, up);
- }
-