{***********************************************************************}
{									}
{ __CurWin    Make an active window current				}
{									}
{ function  __CurWin(WinPtr : _WindowPtr) : boolean;			}
{									}
{	      WinPtr	Pointer to _Window record describing the active }
{			window to be made current.			}
{									}
{ Description __CurWin makes an active window (one that has been dis-	}
{	      played with the __DispWin function) the current window.	}
{	      When a window is current, all window procedures and	}
{	      functions are relative to that window.  The window	}
{	      specified by the window pointer WinPtr is made current.	}
{									}
{	      If the specified window pointer does not point to an	}
{	      active window, the function returns FALSE, and if 	}
{	      the window is successfully made active, TRUE is returned. }
{									}
{	      The cursor position of the current window is saved in its }
{	      window record, and then the cursor position, size and	}
{	      text attributes are set for the new window.  The global	}
{	      variable _WinCur is set so that the window functions	}
{	      know the current window, and the Turbo Pascal variables	}
{	      WindMin, WindMax and TextAttr are set so that Turbo	}
{	      Pascal I/O routines know the current window dimensions	}
{	      and attributes.						}
{									}
{ Version     4.00 (C)Copyright Blaise Computing Inc.  1987		}
{_______________________________________________________________________}

function  __CurWin(WinPtr : _WindowPtr) : boolean;

  var
    TempPtr   : _WindowPtr;
    Found     : boolean;
    CTop,CBot : byte;

  begin {__CurWin}

    { First make sure that WinPtr^ really exists and is active. 	}

    if (WinPtr = _WinCur) then	      { The window is already current.	}
       begin
	 __CurWin := TRUE;
	 Exit
       end;

    Found   := FALSE;
    TempPtr := _WinRoot;
    while ((not Found) and (TempPtr <> NIL)) do
      if (TempPtr = WinPtr) then
	 if (WinPtr^._Active) then
	    Found := TRUE	       { Window found, and it is active.}
	 else
	    begin
	      TempPtr := NIL;	       { Terminate the loop, because	}
	      Found := FALSE	       { window found, but not active.	}
	    end
      else
	 TempPtr := TempPtr^._WinTop;
    if (not Found) then
       begin
	 __CurWin := FALSE;	       { No active window record	}
	 Exit			       { found, so return immediately	}
       end;

     with _WinCur^ do		       { Save the cursor position of	}
       __WhereWin(_XCursor,_YCursor,   { the currently active window.	}
			  CTop,CBot);

    { Set the Turbo Pascal variables WindMin, WindMax, TextAttr, and	}
    { the POWER TOOLS PLUS global variable _CurWindow to reflect the	}
    { the new current window.  Also set the window cursor size, and	}
    { position the cursor.						}

    with WinPtr^ do
      begin
	with _Dim do
	  begin
	    WindMin := __2WordSup(_YUpper - 1,_XUpper - 1);
	    WindMax := __2WordSup(_YLower - 1,_XLower - 1)
	  end;
	_CurWindow := _Dim;
	TextAttr   := __2ByteSup(_Back,_Fore);
	__CSizeScn(_TopScan,_BotScan);
	__GotoWin (_XCursor,_YCursor)
      end;

    { Finally adjust the window record pointers so that this record is	}
    { the last one in the list (i.e., it is on top of all others).	}

    if (WinLast_ <> WinPtr) then	 { There is something to do,	}
       with WinPtr^ do			 { because there are windows	}
	 begin				 { on top of this one.		}
	   if (WinPtr = _WinRoot) then	 { It is the first record, so	}
	      _WinRoot := _WinTop	 { the root must be altered.	}
	   else 			 { If not the first, the one	}
	      _WinUnder^._WinTop := _WinTop;  {underneath is altered.	}
	   _WinTop^._WinUnder := _WinUnder;
	   WinLast_^._WinTop  := WinPtr;
	   _WinTop	      := NIL;
	   _WinUnder	      := WinLast_;
	   WinLast_	      := WinPtr
	 end;
    _WinCur  := WinPtr;        { Set the current window pointer }
    __CurWin := TRUE

  end;	{__CurWin}
