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

    File:    Pane2.AddPane.c
    Author:  Copyright  1995 Andrew Sellors.
    Version: 1.05 (14 Nov 1995)
    Purpose: Handles windows with panes.
    History: 1.04 ( 4 Aug 1995)
             1.05 (14 Nov 1995) JS Made SDLS compatible
*/

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

#include <stdlib.h>


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

extern Desk_bool Desk_Pane2_AddPane(Desk_window_handle mainwindow, Desk_window_handle panewindow,
                          const Desk_wimp_point *paneoffset, const Desk_wimp_point *panesize, int flags)
{
 /*
  * Adds the pane window 'panewindow' to the main window 'mainwindow'.
  * If 'paneoffset' is not NULL then this is taken as the offset between the
  * pane and the main window instead of the positions in the template.
  * If 'panesize' is not NULL then this is taken as the size of the pane
  * instead of the size in the template.
  */
  Desk_main_listelement *mainelement;
  Desk_pane_listelement *paneelement;
  Desk_window_state mainstate;
  Desk_window_state panestate;

 /*
  * find element for main window and return Desk_bool_FALSE if mainwindow not present
  */
  mainelement = Desk_Pane2__FindMainWindow(mainwindow);
  if(mainelement == NULL)
     return(Desk_bool_FALSE); /* not found */

 /*
  * Desk_DeskMem_Malloc memory for list element and return Desk_bool_FALSE if fails
  */
  paneelement = Desk_DeskMem_Malloc(sizeof(Desk_pane_listelement));

 /*
  * get info about main and pane windows
  */
  Desk_Wimp_GetWindowState(mainwindow, &mainstate);
  Desk_Wimp_GetWindowState(panewindow, &panestate);

 /*
  * initialise list header
  */
  Desk_LinkList_Init(&(paneelement->header));

 /*
  * fill element
  */
  paneelement->panewindow = panewindow;
  paneelement->mainwindow = mainwindow;
  paneelement->paneflags.value = flags;

  if(paneoffset != NULL) /* if pane offset is provided */
     paneelement->paneoffset = *paneoffset;

  else{ /* use template pane offset */

     paneelement->paneoffset.x = panestate.openblock.screenrect.min.x -
                                 mainstate.openblock.screenrect.min.x;


     if(paneelement->paneflags.data.maintop) /* pane fixed to top of window */
        paneelement->paneoffset.y = mainstate.openblock.screenrect.max.y;

     else /* pane fixed to bottom of window */
        paneelement->paneoffset.y = mainstate.openblock.screenrect.min.y;

     if(paneelement->paneflags.data.panetop) /* pane fixed using top of pane */
        paneelement->paneoffset.y -= panestate.openblock.screenrect.max.y;

     else /* pane fixed using bottom of pane */
        paneelement->paneoffset.y -= panestate.openblock.screenrect.min.y;
 
  }

  if(panesize != NULL) /* if pane size is provided */
     paneelement->panesize = *panesize;

  else{ /* use template pane size */

     paneelement->panesize.x = panestate.openblock.screenrect.max.x -
                               panestate.openblock.screenrect.min.x;
                               
     paneelement->panesize.y = panestate.openblock.screenrect.max.y -
                               panestate.openblock.screenrect.min.y;
  }

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

 /*
  * install open window event handler for pane window if it is !ArtWorks style
  */
  if(!paneelement->paneflags.data.fixed)
     Desk_Event_Claim(Desk_event_OPEN, panewindow, Desk_event_ANY, Desk_SDLS_dllEntry( Desk_Pane2__PaneOpenEventHandler), paneelement);

  return(Desk_bool_TRUE);
}
