home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 …ember: Reference Library / Dev.CD Dec 00 RL Disk 1.toast / pc / technical documentation / develop / develop issue 28 / develop issue 28 code / sketch / source / utilities / windowutils.c < prev    next >
Encoding:
Text File  |  1996-08-18  |  7.7 KB  |  295 lines

  1. // ---------------------------------------------------------------------------
  2. // 
  3. // WindowUtils.c
  4. //
  5. // ---------------------------------------------------------------------------
  6.  
  7. #include <WIndows.h>
  8. #include "WindowUtils.h"
  9.  
  10. /*****************************************************************************
  11.  * 
  12.  * CountWindows
  13.  * 
  14.  * Counts the number of windows
  15.  * 
  16.  *****************************************************************************/
  17.  
  18. long CountWindows(void)
  19. {
  20.     long                numWindows     = 0L;
  21.     WindowPtr            window         = GetFirstWindow();
  22.     
  23.     while (window != nil)
  24.     {
  25.         numWindows++;
  26.         window = GetNextWindow(window);
  27.     }
  28.     return numWindows;
  29. }
  30.  
  31. /*****************************************************************************
  32.  * 
  33.  * GetFirstWindow
  34.  * 
  35.  * Return the first window in the application’s window list from low memory.
  36.  * 
  37.  *****************************************************************************/
  38. WindowPtr GetFirstWindow(void)
  39. {
  40.     return (WindowPtr) LMGetWindowList();
  41. }
  42.  
  43. /*****************************************************************************
  44.  * 
  45.  * GetNextWindow
  46.  * 
  47.  * get the window after this window
  48.  * 
  49.  *****************************************************************************/
  50. #ifndef GetNextWindow
  51. WindowPtr GetNextWindow(WindowPtr window)
  52. {
  53.     return (WindowPtr)window->nextWindow;
  54. }
  55. #endif
  56.  
  57. /*****************************************************************************
  58.  * 
  59.  * GetPreviousWindow
  60.  * 
  61.  * gets the window before this window, or nil if first window.
  62.  *
  63.  *****************************************************************************/
  64. WindowPtr GetPreviousWindow(WindowPtr anchor)
  65. {
  66.     WindowPtr     window;
  67.  
  68.     window = GetFirstWindow();                                                                
  69.  
  70.     if (anchor == window)                                                                    
  71.         return nil;                                                                                
  72.  
  73.     if (window)                                                                                    
  74.         while ((WindowPtr)GetNextWindow(window) != anchor)                                        
  75.             window = (WindowPtr)GetNextWindow(window);                                                
  76.         
  77.     return(window);
  78. }
  79.    
  80. /*****************************************************************************
  81.  * 
  82.  * GetLastWindow
  83.  * 
  84.  *****************************************************************************/
  85. WindowPtr GetLastWindow(void)
  86. {
  87.     WindowPtr     window;
  88.  
  89.     window = GetFirstWindow();
  90.     
  91.     while (window != NULL && GetNextWindow(window) != NULL)                                            
  92.         window = GetNextWindow(window);                            
  93.     
  94.     return window;
  95. }
  96.  
  97. //----------------------------------------------------------------------------------
  98. // returns the portRect from the window’s GrafPort
  99. //----------------------------------------------------------------------------------
  100.  
  101. void GetWindowPortRect(WindowPtr window, Rect *portRect)
  102. {
  103.     *portRect = ((WindowPtr) window)->portRect;
  104. }
  105.  
  106. //----------------------------------------------------------------------------------
  107. // GetWindowBounds
  108. //----------------------------------------------------------------------------------
  109.  
  110. void GetWindowBounds(WindowPtr window, Rect *bounds)
  111. {
  112.     Rect    portRect;
  113.     Rect    bitMapRect;
  114.     
  115.     *bounds = (*((WindowPeek)window)->contRgn)->rgnBBox; // content region
  116.     
  117.     if (bounds->top == 0 && bounds->left == 0 && bounds->bottom == 0 && bounds->right == 0)
  118.     {
  119.         portRect = window->portRect;
  120.         bitMapRect = window->portBits.bounds;
  121.         
  122.         bounds->top     = portRect.top         - bitMapRect.top;
  123.         bounds->left     = portRect.left     - bitMapRect.left;
  124.         bounds->bottom     = portRect.bottom     - bitMapRect.top;
  125.         bounds->right     = portRect.right     - bitMapRect.left;
  126.     }
  127. }
  128.  
  129. //----------------------------------------------------------------------------------
  130. // Determines if a window has a grow icon based on its defproc
  131. //----------------------------------------------------------------------------------
  132.  
  133. Boolean WindowHasGrowIcon(WindowPtr window)
  134. {
  135.     short    windowVariant = GetWVariant(window);
  136.  
  137.     return((windowVariant == documentProc) || (windowVariant == zoomDocProc));
  138. }
  139.  
  140. //----------------------------------------------------------------------------------
  141. // Determines if a window has a close box
  142. //----------------------------------------------------------------------------------
  143.  
  144. Boolean WindowHasCloseBox(WindowPtr window)
  145. {        
  146.     return ((WindowPeek)window)->goAwayFlag;
  147. }
  148.  
  149.  
  150. //----------------------------------------------------------------------------------
  151. // Determines if a window has a zoom box based on its defproc
  152. //----------------------------------------------------------------------------------
  153.  
  154. Boolean WindowHasZoomBox(WindowPtr window)
  155. {
  156.     short    windowVariant = GetWVariant(window);
  157.  
  158.     return((windowVariant == zoomDocProc) || (windowVariant == zoomNoGrow));
  159. }
  160.  
  161. //----------------------------------------------------------------------------------
  162. // Determines if a window is modal based upon the value of its windowKind
  163. // and window variant.
  164. //----------------------------------------------------------------------------------
  165.  
  166. Boolean WindowIsModal(WindowPtr window)
  167. {
  168.     short    windowVariant;
  169.     
  170.     if (window == nil)
  171.         return false;
  172.         
  173.     windowVariant = GetWVariant(window);
  174.     if ((windowVariant == dBoxProc) || (windowVariant == plainDBox) || (windowVariant == altDBoxProc) || (windowVariant == movableDBoxProc))
  175.         return true;
  176.     else
  177.         return false;
  178. }
  179.  
  180. //----------------------------------------------------------------------------------
  181. // Returns true if the specified window is modeless
  182. //----------------------------------------------------------------------------------
  183.  
  184. Boolean WindowIsModeless(WindowPtr window)
  185. {
  186.     return !WindowIsModal(window);
  187. }
  188.  
  189. //----------------------------------------------------------------------------------
  190. // Returns true if the specified window is a movable modal dialog
  191. //----------------------------------------------------------------------------------
  192.  
  193. Boolean WindowIsMovableModal(WindowPtr window)
  194. {
  195.     short        variant;
  196.     
  197.     if (window == nil)
  198.         return false;
  199.     else
  200.     {
  201.         variant = GetWVariant(window);
  202.         return variant == movableDBoxProc;
  203.     }
  204. }
  205.  
  206. //----------------------------------------------------------------------------------
  207. // get the visible field for a window
  208. //----------------------------------------------------------------------------------
  209.  
  210. Boolean WindowIsVisible(WindowPtr window)
  211. {
  212.     return ((WindowPeek)window)->visible;
  213. }
  214.  
  215. #pragma mark -
  216. //----------------------------------------------------------------------------------
  217. //    Returns the window with the given title if found.  Otherwise, nil is returned.
  218. //----------------------------------------------------------------------------------
  219.  
  220. WindowPtr 
  221. GetWindowByTitle(ConstStr255Param titleToFind)
  222. {
  223.     WindowPtr    window         = GetFirstWindow();
  224.     Str255        windowTitle;
  225.         
  226.     while (window != nil) {
  227.         GetWTitle(window, windowTitle);
  228.         if (EqualString(windowTitle, titleToFind, false, false))
  229.             break;
  230.         window = (WindowPtr) GetNextWindow(window);
  231.     }
  232.     
  233.     return window;
  234. }
  235.  
  236. //----------------------------------------------------------------------------------
  237. //    Returns the window with the given index if found.  Otherwise, nil is returned.
  238. //----------------------------------------------------------------------------------
  239.  
  240. WindowPtr 
  241. GetWindowByIndex(long index)
  242. {
  243.     WindowPtr    window   = GetFirstWindow();
  244.     Boolean     foundIt  = false;
  245.     long            i            = 0L;
  246.             
  247.     while (window != nil)
  248.     {
  249.         if (++i == index)
  250.         {
  251.             foundIt = true;
  252.             break;
  253.         }
  254.             
  255.         window = (WindowPtr) GetNextWindow(window);
  256.     }
  257.     
  258.     if (!foundIt)
  259.         window = nil;
  260.  
  261.     return window;
  262. }
  263.  
  264. //----------------------------------------------------------------------------------
  265. //    Returns the index (front to back) for the specified window
  266. //----------------------------------------------------------------------------------
  267.  
  268. long 
  269. GetWindowIndex(WindowPtr window)
  270. {
  271.     WindowPtr    iter       = GetFirstWindow();
  272.     Boolean     foundIt = false;
  273.     long        i        = 1L;
  274.             
  275.     while (iter != nil)
  276.     {
  277.         if (iter == window)
  278.         {
  279.             foundIt = true;
  280.             break;
  281.         }
  282.             
  283.         iter = GetNextWindow(iter);
  284.         i++;
  285.     }
  286.     
  287.     if (!foundIt)
  288.         i = 0;
  289.  
  290.     return i;
  291. }
  292.  
  293. //----------------------------------------------------------------------------------
  294.  
  295.