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

    File:    Pane2.AddMain.c
    Author:  Copyright  1995 Andrew Sellors.
    Version: 1.04 (4th August 1995)
    Purpose: Handles windows with panes.
*/

#include "DeskLib:Pane2.h"
#include "Pane2Defs.h"
#include "Desklib:Event.h"
#include "Desk.Template.h"
#include "Desklib:EventMsg.h"
#include "Desklib:Error.h"
#include "Desklib:DeskMem.h"

#include <stdlib.h>


/******************************************************************************/

extern Desk_bool Desk_Pane2_AddMain(Desk_window_handle mainwindow)
{
 /*
  * initialises 'mainwindow' with the library as a main window that can have
  * panes attached to it.
  */
  Desk_main_listelement *mainelement;

 /*
  * malloc memory for list element and return Desk_bool_FALSE if fails
  */
  mainelement = Desk_DeskMem_XMalloc(sizeof(Desk_main_listelement));
  if(mainelement == NULL)
     return(Desk_bool_FALSE); /* failed */

 /*
  * initialise list headers
  */
  Desk_LinkList_Init(&(mainelement->header));
  Desk_LinkList_Init(&(mainelement->paneanchor));

 /*
  * add main window handle to list element
  */
  mainelement->mainwindow = mainwindow;
  mainelement->invalideventdata = Desk_bool_FALSE;

 /*
  * add list element to end of list
  */
  Desk_LinkList_AddToTail(&Desk_pane2__mainanchor, &(mainelement->header));

 /*
  * install open window event handler for main window
  */
  Desk_Event_Claim(Desk_event_OPEN, mainwindow, Desk_event_ANY, Desk_SDLS_dllEntry( Desk_Pane2__OpenEventHandler), mainelement);

 /*
  * install message handler on mode change so that main window is opened before
  * status is read so that panes open in correct place
  */
  Desk_EventMsg_Claim(Desk_message_MODECHANGE, Desk_event_ANY, Desk_SDLS_dllEntry( Desk_Pane2__ModeChangeMessageHandler), mainelement);

  return(Desk_bool_TRUE);
}
