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

    File:    Pane2.ReadIcon.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 <stdlib.h>


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

extern void Desk_Pane2_ReadIcon(Desk_window_handle window, Desk_icon_handle icon, const char *panewindow,
                           Desk_wimp_point *paneoffset, Desk_wimp_point *panesize)
{
 /*
  * Calculates the pane offset and size so that the pane will be in the same
  * position and size as the icon in the window.
  * If 'panewindow' is the name of the pane window in the template file then the
  * presence of any scroll bars or the window title bar is taken into accound
  * when calculating the position and size of the pane, set it to NULL and no
  * action is taken.
  * Need to use Desk_pane2_MAINTOP and Desk_pane2_PANETOP flags.
  */
  Desk_icon_block iconinfo;
  Desk_window_block *block;
 
  Desk_Wimp_eGetIconState(window, icon, &iconinfo);

  panesize->x = iconinfo.workarearect.max.x - iconinfo.workarearect.min.x;
  panesize->y = iconinfo.workarearect.max.y - iconinfo.workarearect.min.y;
  
  paneoffset->x = iconinfo.workarearect.min.x;
  paneoffset->y = -iconinfo.workarearect.max.y;

 /*
  * Try and correct size to take into acount scroll bars etc.
  * Should use difference between window outline and window openpos to find
  * tool size but this requires the window to be already open.
  */
  if(panewindow != NULL){ 

     block = Desk_Template_Find(panewindow);

     if(block != NULL){

        if(block->flags.data.hastitle || block->flags.data.titlebar){ /* title bar */

           paneoffset->y += Desk_tool_SIZE;
           panesize->y -= Desk_tool_SIZE;

        }

        if(block->flags.data.hasvscroll || block->flags.data.vscroll){ /* vert scroll */

           panesize->x -= Desk_tool_SIZE;

        }

        if(block->flags.data.hashscroll || block->flags.data.hscroll){ /* horz scroll */

           panesize->y -= Desk_tool_SIZE;

        }

     }

  }
}
