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

    File:    Coord.h
    Author:  Copyright  1992, 1993, 1994 Edouard Poor, Jason Williams
                                          and Tim Browse
    Version: 1.02 (02 Mar 1994)
    Purpose: Coord (point and rectangle) handling functions
*/

#ifndef __dl_coord_h
#define __dl_coord_h

#ifdef __cplusplus
extern "C" {
#endif


#ifndef __dl_core_h
#include "Core.h"
#endif

#ifndef __dl_wimp_h
#include "Wimp.h"
#endif



typedef struct
{
  wimp_rect  screenrect;
  wimp_point scroll;
} convert_block;


extern BOOL Coord_PointInRect(wimp_point *point, wimp_rect *rect);
/*
  Inputs:   point - the point to test
            rect - the rectangle to check for containment with.
  Returns:  TRUE if point is in rectangle; FALSE otherwise.
  Purpose:  Tests whether the point is within the rectangle. If it's on the
            line it's counted as in (just like in tennis).
  SeeAlso:  Coord_RectContained; Coord_RectsOverlap
*/




extern BOOL Coord_RectContained(wimp_rect *InsideRect,
                                  wimp_rect *OutsideRect);
/*
  Inputs:   See purpose.
  Returns:  TRUE if InsideRect is withing OutsideRect;
            FALSE otherwise.
  Purpose:  Test whether the InsideRect is wholly contained by the
            OutsideRect.  Shared vertices/edges are considered to be inside.
  SeeAlso:  Coord_PointInRect; Coord_RectsOverlap

*/




extern BOOL Coord_RectsOverlap(wimp_rect *rect1, wimp_rect *rect2);
/*
  Inputs:   rect1, rect2 - the rectangles to check for overlap.
  Returns:  TRUE if rectangles overlap;
            FALSE otherwise.
  Purpose:  Checks to see if two rectangles overlap each other in any
            way (includes containment).
  SeeAlso:  Coord_RectsIntersect
*/




#define Coord_RectsIntersect(r1, r2) (Coord_RectsOverlap(r1, r2) && \
                                     !Coord_RectContained(r1, r2) && \
                                     !Coord_RectContained(r2, r1))
/*
  MACRO:    BOOL Coord_RectsIntersect(wimp_rect *rect1, wimp_rect *rect2)

  Inputs:   rect1, rect2 - the rectangles to check for intersection
  Returns:  TRUE if rectangles intersect but do not contain each other;
            FALSE otherwise.
  Purpose:  Tests if two rectangles intersect each other, but wil return
            failure if either rectangle wholly contains the other one.
            This is different to the behaviour of Coord_RectsOverlap.
  SeeAlso:  Coord_RectsOverlap
*/




/*K**************************************************************************

> Coordinate conversion.

  Screen <---> Work Area conversion routines.
  NOTE:
    "Screen Coordinates" refers to OS coordinates, with the bottom
      left corner of the screen being placed at the screen origin, (0,0)
    "Work Area Coordinates" refers to Coordinates within the Window's
      work area, where the (0,0) origin is at the TOP left of the work area

  Some of these routines have been defined as macros because they are
  very elementary, and are common, so efficiency will be improved by
  removing the function call overhead.

  To keep compatibility with the syntax of Acorn's coords_ calls, these
  macros still accept a pointer to a convert_block.

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



extern void Coord_WindowOrigin(wimp_point *origin, convert_block *convert);
/*
  Inputs:   convert - the standard convert_block.
  Outputs:  origin - the window origin, in screen coordinates.
  Purpose:  Returns the origin (TOP LEFT (0,0) corner) of the window's
            work-area in screen coordinates. This can then be used as a
            redraw-origin for redraws - any drawing done relative to this
            origin will appear at the correct screen position regardless of
            scroll bar offsets and screen position of the window.
            Remember to call this at the start of each redraw - whenever the
            window is moved or scrolled, the position of this origin (in
            screen coordinates) will change, so it must be recalculated.
  SeeAlso:  Coord_PointToScreen
*/




#define Coord_XToScreen(X, C) \
            (((X) - (C)->scroll.x) + (C)->screenrect.min.x)
/*
  MACRO:    int Coord_XToScreen(int xpos, convert_block *convert)

  Inputs:   xpos - the x coordinate to translate.
            convert - the standard convert_block.
  Returns:  The translated x coordinate.
  Purpose:  Translate a x coordinate from the work-area to the screen
            coordinate space.
  SeeAlso:  Coord_PointToScreen; Coord_YToScreen
*/




#define Coord_YToScreen(Y, C) \
        ( ((Y) - (C)->scroll.y) + (C)->screenrect.max.y )
/*
  MACRO:    int Coord_YToScreen(int ypos, convert_block *convert)

  Inputs:   ypos - the y coordinate to translate.
            convert - the standard convert_block.
  Returns:  The translated y coordinate.
  Purpose:  Translate a y coordinate from the work-area to the screen
            coordinate space.
  SeeAlso:  Coord_PointToScreen; Coord_XToScreen
*/




extern void Coord_PointToScreen(wimp_point *point, convert_block *convert);
/*
  Inputs:   point - a point in Work Area coords.
            convert - the standard convert_block.
  Outputs:  point - the same point in screen coordinates
  Purpose:  Convert a work-area coordinate to a screen coordinate.
  SeeAlso:  Coord_RectToScreen; Coord_XToScreen; Coord_YToScreen;
            Coord_PointToWorkArea
*/




extern void Coord_RectToScreen(wimp_rect *rect, convert_block *convert);
/*
  Inputs:   rect - a rectangle in Work Area coords.
            convert - the standard convert_block.
  Outputs:  rect - the same rectangle in screen coordinates
  Purpose:  Convert a rectangle in work-area coordinates to screen
            coordinates.
  SeeAlso:  Coord_PointToScreen
*/




#define Coord_XToWorkArea(X, C) (((X)-(C)->screenrect.min.x)+(C)->scroll.x)
/*
  MACRO:    int Coord_XToWorkArea(int xpos, convert_block *convert)

  Inputs:   xpos - the screen x coordinate to convert.
            convert - the standard convert_block.
  Returns:  The x coordinate translated to work-area coordinates.
  Purpose:  Convert an x coordinate from the screen coordinate space to
            the work-area coordinate space.
  SeeAlso:  Coord_YToWorkArea; Coord_PointToWorkArea
*/




#define Coord_YToWorkArea(Y, C) (((Y)-(C)->screenrect.max.y)+(C)->scroll.y)
/*
  MACRO:    int Coord_YToWorkArea(int ypos, convert_block *convert)

  Inputs:   ypos - the screen y coordinate to convert.
            convert - the standard convert_block.
  Returns:  The y coordinate translated to work-area coordinates.
  Purpose:  Convert an y coordinate from the screen coordinate space to
            the work-area coordinate space.
  SeeAlso:  Coord_XToWorkArea; Coord_PointToWorkArea
*/




extern void Coord_PointToWorkArea(wimp_point *point,
                                    convert_block *convert);
/*
  Inputs:   point - a point in screen coords.
            convert - the standard convert_block.
  Outputs:  point - the same point in work area coords.
  Purpose:  Convert a coordinate from the screen coordinate space to the
            work-area coordinate space.
  SeeAlso:  Coord_PointToScreen; Coord_RectToWorkArea
*/




extern void Coord_RectToWorkArea(wimp_rect *rect, convert_block *convert);
/*
  Inputs:   rect - a rectangle in screen coords.
            convert - the standard convert_block.
  Outputs:  rect - the same rectangle in work area coords.
  Purpose:  Convert a rectangle from the screen coordinate space to the
            work-area coordinate space.
  SeeAlso:  Coord_PointToWorkArea; Coord_RectToScreen
*/




extern void Coord_RectUnion(wimp_rect *dest,
                              wimp_rect *src1, wimp_rect *src2);
/*
  Inputs:   src1, src2 - the two rectangles to find the union of.
  Outputs:  dest - the rectangle structure to hold the union.
  Purpose:  Find the union of two rectangles.  dest can be the same as
            either src1 or src2 if you want to use this as an
            accumulator.  (dest can be the same as src1 *and* src2, if
            you're feeling really silly).
  SeeAlso:  Coord_RectsOverlap; Coord_RectsIntersect
*/



#ifdef __cplusplus
}
#endif


#endif
