Source/SVAEWindowUtils.c

/*
    File:       SVAEWindowUtils.c
 
    Contains:   
 
    Written by: Original version by Jon Lansdell and Nigel Humphreys.
                3.1 updates by Greg Sutton.
 
    Copyright:  Copyright © 1995-1999 by Apple Computer, Inc., All Rights Reserved.
 
                You may incorporate this Apple sample source code into your program(s) without
                restriction. This Apple sample source code has been provided "AS IS" and the
                responsibility for its operation is yours. You are not permitted to redistribute
                this Apple sample source code as "Apple sample source code" after having made
                changes. If you're going to re-distribute the source, we require that you make
                it clear in the source that the code was descended from Apple sample source
                code, but that you've made changes.
 
    Change History (most recent first):
                            7/20/1999   Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
                
 
*/
 
 
#include "SVAEWindowUtils.h"
 
#include "SVEditWindow.h"       // for DPtrFromWindowPtr()
 
 
 
#include <LowMem.h>
#include <TextUtils.h>
 
 
 
 
WindowPtr WindowNameToWindowPtr(StringPtr nameStr)
/* 
    Returns the WindowPtr of the window with title nameStr
    or nil if there is no matching window.
*/
    { 
        WindowPtr theWindow;
        Str255    windTitle;
            
        theWindow =(WindowPtr)LMGetWindowList();
        /* 
            iterate through windows - we use WindowList 'cos we could
            have made the window invisible and  we lose it - so we
            can't set it back to visible!!
        */
        while (theWindow)
            {
                GetWTitle(theWindow, windTitle);
                if (EqualString(windTitle,
                                nameStr,
                                                false,
                                                true))  /* ignore case, don't ignore diacriticals */
                    return(theWindow);
              theWindow = (WindowPtr)((WindowPeek)theWindow)->nextWindow;
            }
        return(theWindow);
    }   /* WindowNameToWindowPtr */
 
    
OSErr   GetDescOfNamedWindow(StringPtr nameStr, AEDesc* result)
{
    WindowToken     theToken;
    OSErr           err = noErr;
    
    theToken.tokenWindow = WindowNameToWindowPtr(nameStr);
    
    if (theToken.tokenWindow)
        err = AECreateDesc(typeMyWndw, (Ptr)&theToken, sizeof(theToken), result);
    else
        err = errAENoSuchObject;
 
    return(err);
}
 
 
short CountWindows(void)
{
    WindowPtr theWindow;
    short     index;
            
    index = 0;
    theWindow = (WindowPtr)LMGetWindowList();
 
    // iterate through windows
    while (theWindow)
        {
            index++;                    
          theWindow = (WindowPtr)((WindowPeek)theWindow)->nextWindow;
        }
    
    return(index);
} // CountWindows
 
 
WindowPtr GetWindowPtrOfNthWindow(short index)
/* returns a ptr to the window with the given index
  (front window is 1, behind that is 2, etc.).  if
  there's no window with that index (inc. no windows
  at all), returns nil.
*/
  {
      WindowPtr theWindow;
        
        theWindow = (WindowPtr)LMGetWindowList();
    
        /* iterate through windows */
        
        while (theWindow)
            {
                index --;
                if (index <= 0) 
                    return(theWindow);
                    
              theWindow = (WindowPtr)((WindowPeek)theWindow)->nextWindow;
            }
        return(nil);
    }   /* GetWindowPtrOfNthWindow */
    
short   GetNthWindowOfWindowPtr(WindowPtr aWindow)
// Given a WindowPtr this routine tries to find the
// index to that window. If not found it will return
// zero
{
    WindowPtr theWindow;
    short     index;
            
    index = 0;
    theWindow = (WindowPtr)LMGetWindowList();
 
        // iterate through windows
    while (theWindow)
    {
        index++;
        if (theWindow == aWindow)
            return(index);
                        
        theWindow = (WindowPtr)((WindowPeek)theWindow)->nextWindow;
    }
    
    return(0);
} // GetNthWindowOfWindowPtr
 
 
OSErr   GetDescOfNthWindow(short index, AEDesc* result)
{
    WindowToken     theToken;
    OSErr           err = noErr;
    
    theToken.tokenWindow = GetWindowPtrOfNthWindow(index);
    
    if (theToken.tokenWindow)
        err = AECreateDesc(typeMyWndw, (Ptr)&theToken, sizeof(theToken), result);
    else
        err = errAEIllegalIndex;
 
    return(err);
}