{***********************************************************************}
{									}
{ __ZapMnu    Remove a menu record					}
{									}
{ function  __ZapMnu(var MenuPtr : _MenuPtr) : boolean; 		}
{									}
{	      MenuPtr	Pointer to _Menu record to be removed		}
{									}
{ Description This procedure removes the specified _Menu descriptor,	}
{	      freeing the heap memory occupied by the _Menu record and	}
{	      all structures associated with it, including the window	}
{	      record pointed to by _MWinPtr, all _Key records in the	}
{	      linked list pointed to by _MKeysPtr, and all _Item re-	}
{	      cords in the linked list pointed to by _MItemsPtr.  The	}
{	      function calls __ZapWin to free the window record,	}
{	      and walks through the lists of _Keys and _Items, freeing	}
{	      each structure as it proceeds.  Finally, if all its	}
{	      asscoiated structures are successfully deleted, the	}
{	      pointer to the removed menu is set to NIL and the space	}
{	      allocated for the _Menu record is released.		}
{									}
{	      On error _MenuError may be set to:  _InvalidWindow	}
{						  _InvalidMenu		}
{						  _InvalidKey		}
{						  _InvalidItem		}
{									}
{ Version     4.00 (C)Copyright Blaise Computing Inc. 1987		}
{_______________________________________________________________________}

function __ZapMnu(var MenuPtr : _MenuPtr) : boolean;

  var
    TempPtr,LastPtr   : _ItemPtr;
    TempKPtr,LastKPtr : _KeyPtr;

  begin

    __ZapMnu := FALSE;

    if (not ValidSig(MenuPtr,MENUSIG_)) then
       Exit;

    if (__ZapWin(MenuPtr^._MWinPtr)) then	 { Zap the window	}
       begin

	 { First release the _Item records and associated structures.	}

	 TempPtr := MenuPtr^._MItemsPtr;
	 while (TempPtr <> NIL) do
	   begin
	     if (not ValidSig(TempPtr,ITEMSIG_)) then
		Exit;

	     { Now free the item string, and, if it is a Lotus item,	 }
	     { free the Lotus description string as well.		 }

	     FreeMem(TempPtr^._ItemStrPtr,TempPtr^._Len + 1);
	     if (TempPtr^._LDescPtr <> NIL) then
		FreeMem(TempPtr^._LDescPtr,TempPtr^._LDescLen + 1);

	     { Find the next item, then free the _Item structure itself. }

	     LastPtr := TempPtr;
	     TempPtr := TempPtr^._NextItem;
	     FreeMem(LastPtr,sizeof(_Item))
	   end;

	 { Next trace through the list of _Key records associated with	 }
	 { this MenuPtr, freeing all the keys.				 }

	 TempKPtr := MenuPtr^._MKeysPtr;
	 while (TempKPtr <> NIL) do
	   begin
	     if (not ValidSig(TempKPtr,KEYSIG_)) then
		Exit;
	     LastKPtr := TempKPtr;
	     TempKPtr := TempKPtr^._NextKey;
	     FreeMem(LastKPtr,sizeof(_Key))
	   end;
	 MenuPtr^._MSig := ' ';
	 FreeMem(MenuPtr,sizeof(_Menu));
	 MenuPtr  := NIL;
	 __ZapMnu := TRUE
       end
    else
       SetMenuError(_InvalidWindow)    { The window cannot be released. }

  end; {__ZapMnu}
