#define MYLIB_INTUITION

#include "MyLib.h"

/****** MyLib.lib/CloseWindowSafely **************************************
*
*    NAME
*	CloseWindowSafely -- close window without UserPort
*
*    SYNOPSIS
*	CloseWindowSafely(Window)
*
*	void CloseWindowSafely(struct Window *);
*
*    FUNCTION
*	Close a window that does not have an intuition-allocated
*	UserPort.
*
*    INPUTS
*	Window - a window that uses a foreign message port
*
*    NOTE
*	Some people believe that CloseWindowSafely() must
*	be called for all windows. This is incorrect. If
*	you call this function on a window with an intuition-
*	allocated UserPort, the port will not be freed, and
*	memory will be lost forever.
*
*    SEE ALSO
*	intuition.library/CloseWindow()
*
*************************************************************************/

void CloseWindowSafely(struct Window *Window)

{
  if (Window->UserPort!=NULL)
    {
      struct IntuiMessage *Message;
      struct Node *Next;

      Forbid();
      Message=(struct IntuiMessage *)Window->UserPort->mp_MsgList.lh_Head;
      while ((Next=Message->ExecMessage.mn_Node.ln_Succ)!=NULL)
	{
	  if (Message->IDCMPWindow==Window)
	    {
	      Remove(&Message->ExecMessage.mn_Node);
	      ReplyMsg(&Message->ExecMessage);
	    }
	  Message=(struct IntuiMessage *)Next;
	}
      Window->UserPort = NULL;
      ModifyIDCMP(Window,0);
      Permit();
    }
  CloseWindow(Window);
}
