/*
    ####             #    #     # #
    #   #            #    #       #          The FreeWare C library for 
    #   #  ##   ###  #  # #     # ###             RISC OS machines
    #   # #  # #     # #  #     # #  #   ___________________________________
    #   # ####  ###  ##   #     # #  #                                      
    #   # #        # # #  #     # #  #    Please refer to the accompanying
    ####   ### ####  #  # ##### # ###    documentation for conditions of use
    ________________________________________________________________________

    File:    Window.Delete.c
    Author:  Copyright  1992 Jason Williams
    Version: 1.00 (19 Mar 1992)
    Purpose: High-level window management functions: Show/hide a window
*/


#include <stdlib.h>
#include <string.h>

#include "Desk.LinkList.h"
#include "Desk.WimpSWIs.h"
#include "Desk.Template.h"
#include "Desk.Event.h"
#include "Desk.EventMsg.h"
#include "Desk.Window.h"
#include "Desk.Screen.h"
#include "Desk.Error.h"
#include "Desk.DeskMem.h"

#include "WindowDefs.h"


extern Desk_linklist_header Desk_window_listanchor;



extern void Desk_Window_Delete(Desk_window_handle window)
{
  windowrec       *ptr;

  Desk_Event_ReleaseWindow(window);       /* Release all handlers for this window */
  Desk_EventMsg_ReleaseWindow(window);

  Desk_Wimp_CloseWindow(window);                                  /* say bye bye! */
  Desk_Wimp_DeleteWindow(window);

  ptr = (windowrec *) Desk_window_listanchor.next;
  while (ptr != NULL)
  {
    if (ptr->window == window)
      break;
    ptr = (windowrec *) ptr->header.next;
  }

  if (ptr == NULL)
    return;              /* Window not created with Desk_Window_Show(). Bad user! */

  Desk_LinkList_Unlink(&Desk_window_listanchor, &(ptr->header));

  Desk_Template_Free(&(ptr->memory));     /* Free up the window's indirected data */
  Desk_DeskMem_Free(ptr);                         /* ... and the window's list entry      */
}
