home *** CD-ROM | disk | FTP | other *** search
- // ---------------------------------------------------------------------------
- //
- // WindowUtils.c
- //
- // ---------------------------------------------------------------------------
-
- #include <WIndows.h>
- #include "WindowUtils.h"
-
- /*****************************************************************************
- *
- * CountWindows
- *
- * Counts the number of windows
- *
- *****************************************************************************/
-
- long CountWindows(void)
- {
- long numWindows = 0L;
- WindowPtr window = GetFirstWindow();
-
- while (window != nil)
- {
- numWindows++;
- window = GetNextWindow(window);
- }
- return numWindows;
- }
-
- /*****************************************************************************
- *
- * GetFirstWindow
- *
- * Return the first window in the application’s window list from low memory.
- *
- *****************************************************************************/
- WindowPtr GetFirstWindow(void)
- {
- return (WindowPtr) LMGetWindowList();
- }
-
- /*****************************************************************************
- *
- * GetNextWindow
- *
- * get the window after this window
- *
- *****************************************************************************/
- #ifndef GetNextWindow
- WindowPtr GetNextWindow(WindowPtr window)
- {
- return (WindowPtr)window->nextWindow;
- }
- #endif
-
- /*****************************************************************************
- *
- * GetPreviousWindow
- *
- * gets the window before this window, or nil if first window.
- *
- *****************************************************************************/
- WindowPtr GetPreviousWindow(WindowPtr anchor)
- {
- WindowPtr window;
-
- window = GetFirstWindow();
-
- if (anchor == window)
- return nil;
-
- if (window)
- while ((WindowPtr)GetNextWindow(window) != anchor)
- window = (WindowPtr)GetNextWindow(window);
-
- return(window);
- }
-
- /*****************************************************************************
- *
- * GetLastWindow
- *
- *****************************************************************************/
- WindowPtr GetLastWindow(void)
- {
- WindowPtr window;
-
- window = GetFirstWindow();
-
- while (window != NULL && GetNextWindow(window) != NULL)
- window = GetNextWindow(window);
-
- return window;
- }
-
- //----------------------------------------------------------------------------------
- // returns the portRect from the window’s GrafPort
- //----------------------------------------------------------------------------------
-
- void GetWindowPortRect(WindowPtr window, Rect *portRect)
- {
- *portRect = ((WindowPtr) window)->portRect;
- }
-
- //----------------------------------------------------------------------------------
- // GetWindowBounds
- //----------------------------------------------------------------------------------
-
- void GetWindowBounds(WindowPtr window, Rect *bounds)
- {
- Rect portRect;
- Rect bitMapRect;
-
- *bounds = (*((WindowPeek)window)->contRgn)->rgnBBox; // content region
-
- if (bounds->top == 0 && bounds->left == 0 && bounds->bottom == 0 && bounds->right == 0)
- {
- portRect = window->portRect;
- bitMapRect = window->portBits.bounds;
-
- bounds->top = portRect.top - bitMapRect.top;
- bounds->left = portRect.left - bitMapRect.left;
- bounds->bottom = portRect.bottom - bitMapRect.top;
- bounds->right = portRect.right - bitMapRect.left;
- }
- }
-
- //----------------------------------------------------------------------------------
- // Determines if a window has a grow icon based on its defproc
- //----------------------------------------------------------------------------------
-
- Boolean WindowHasGrowIcon(WindowPtr window)
- {
- short windowVariant = GetWVariant(window);
-
- return((windowVariant == documentProc) || (windowVariant == zoomDocProc));
- }
-
- //----------------------------------------------------------------------------------
- // Determines if a window has a close box
- //----------------------------------------------------------------------------------
-
- Boolean WindowHasCloseBox(WindowPtr window)
- {
- return ((WindowPeek)window)->goAwayFlag;
- }
-
-
- //----------------------------------------------------------------------------------
- // Determines if a window has a zoom box based on its defproc
- //----------------------------------------------------------------------------------
-
- Boolean WindowHasZoomBox(WindowPtr window)
- {
- short windowVariant = GetWVariant(window);
-
- return((windowVariant == zoomDocProc) || (windowVariant == zoomNoGrow));
- }
-
- //----------------------------------------------------------------------------------
- // Determines if a window is modal based upon the value of its windowKind
- // and window variant.
- //----------------------------------------------------------------------------------
-
- Boolean WindowIsModal(WindowPtr window)
- {
- short windowVariant;
-
- if (window == nil)
- return false;
-
- windowVariant = GetWVariant(window);
- if ((windowVariant == dBoxProc) || (windowVariant == plainDBox) || (windowVariant == altDBoxProc) || (windowVariant == movableDBoxProc))
- return true;
- else
- return false;
- }
-
- //----------------------------------------------------------------------------------
- // Returns true if the specified window is modeless
- //----------------------------------------------------------------------------------
-
- Boolean WindowIsModeless(WindowPtr window)
- {
- return !WindowIsModal(window);
- }
-
- //----------------------------------------------------------------------------------
- // Returns true if the specified window is a movable modal dialog
- //----------------------------------------------------------------------------------
-
- Boolean WindowIsMovableModal(WindowPtr window)
- {
- short variant;
-
- if (window == nil)
- return false;
- else
- {
- variant = GetWVariant(window);
- return variant == movableDBoxProc;
- }
- }
-
- //----------------------------------------------------------------------------------
- // get the visible field for a window
- //----------------------------------------------------------------------------------
-
- Boolean WindowIsVisible(WindowPtr window)
- {
- return ((WindowPeek)window)->visible;
- }
-
- #pragma mark -
- //----------------------------------------------------------------------------------
- // Returns the window with the given title if found. Otherwise, nil is returned.
- //----------------------------------------------------------------------------------
-
- WindowPtr
- GetWindowByTitle(ConstStr255Param titleToFind)
- {
- WindowPtr window = GetFirstWindow();
- Str255 windowTitle;
-
- while (window != nil) {
- GetWTitle(window, windowTitle);
- if (EqualString(windowTitle, titleToFind, false, false))
- break;
- window = (WindowPtr) GetNextWindow(window);
- }
-
- return window;
- }
-
- //----------------------------------------------------------------------------------
- // Returns the window with the given index if found. Otherwise, nil is returned.
- //----------------------------------------------------------------------------------
-
- WindowPtr
- GetWindowByIndex(long index)
- {
- WindowPtr window = GetFirstWindow();
- Boolean foundIt = false;
- long i = 0L;
-
- while (window != nil)
- {
- if (++i == index)
- {
- foundIt = true;
- break;
- }
-
- window = (WindowPtr) GetNextWindow(window);
- }
-
- if (!foundIt)
- window = nil;
-
- return window;
- }
-
- //----------------------------------------------------------------------------------
- // Returns the index (front to back) for the specified window
- //----------------------------------------------------------------------------------
-
- long
- GetWindowIndex(WindowPtr window)
- {
- WindowPtr iter = GetFirstWindow();
- Boolean foundIt = false;
- long i = 1L;
-
- while (iter != nil)
- {
- if (iter == window)
- {
- foundIt = true;
- break;
- }
-
- iter = GetNextWindow(iter);
- i++;
- }
-
- if (!foundIt)
- i = 0;
-
- return i;
- }
-
- //----------------------------------------------------------------------------------
-
-