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

    File:    Window.CreateOrig.c
    Author:  Copyright  1995 Sergio Monesi (original code from
                              Window.Create.c by Jason Williams)
    Version: 1.00 (15 Feb 1995)
    Purpose: High-level window management functions: Create a window
             without making a copy of the template (ie. use the original
             template)
*/


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

#include "WindowDefs.h"

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


#define ERRBASE 1
#define ERR1 ERRBASE+0
#define ERRMESS1 "Insufficient memory to open window"


extern Desk_linklist_header Desk_window_listanchor;


extern Desk_window_handle Desk_Window_CreateOrig(const char *windowname)
{
  windowrec     *record;
  Desk_window_block  *windowptr;
  Desk_window_handle window;

  windowptr = Desk_Template_Find(windowname);
  if (windowptr == NULL)
    return(0);

  Desk_Wimp_eCreateWindow(windowptr, &window);

  record = (windowrec *) Desk_DeskMem_XMalloc(sizeof(windowrec));
  if (record == NULL)  Desk_Error_ReportFatalInternal(ERR1, ERRMESS1);

  Desk_LinkList_AddToHead(&Desk_window_listanchor, &(record->header));

  strncpy(record->templatename, windowname, Desk_wimp_MAXNAME);
  record->templatename[Desk_wimp_MAXNAME] = 0;       /* Record of name for help   */

  record->window = window;                      /* Remember window handle    */
  record->memory = windowptr;                   /* remember to dealloc later */

  return(window);
}

