/* Copyright 1996 Acorn Computers Ltd
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
/****************************************************************************
 * This source file was written by Acorn Computers Limited. It is part of   *
 * the RISCOS library for writing applications in C for RISC OS. It may be  *
 * used freely in the creation of programs for Archimedes. It should be     *
 * used with Acorn's C Compiler Release 3 or later.                         *
 *                                                                          *
 ***************************************************************************/

/*
 * Title:  coords.h
 * Purpose:  Provide common coordinate conversion functions
 *
 */

/*
 * This file contains functions for working in the window coordinate
 * system. Functions are provided to convert between screen and work area
 * coordinates, and perform other simple operations on points, lines,
 * "boxes".
 * It is conventional to think of the point (0,0) as appearing at the top
 * left-hand corner of a document.
 *
 */

#ifndef __coords_h
#define __coords_h

#ifndef __wimp_h
#include "wimp.h"
#endif

#ifndef BOOL
#define BOOL int
#define TRUE 1
#define FALSE 0
#endif


/* Type used for box and scroll position */

typedef struct
{
  wimp_box box;
  int      scx, scy;
} coords_cvtstr;


/* Type for points */
typedef struct
{
  int x, y;
} coords_pointstr;



/* ------------------ coords_x_toscreen/coords_y_toscreen ------------------
 * Description:   Convert x/y work area coordinates into x/y screen
 *                coordinates.
 *
 * Parameters:    int x or int y -- x or y coordinate in work area coords
 *                coords_cvtstr *r -- conversion box (screen coords and
 *                                                    scroll offsets).
 * Returns:       x or y screen coordinates.
 * Other Info:    none.
 *
 */

int coords_x_toscreen(int x, coords_cvtstr *r);
int coords_y_toscreen(int y, coords_cvtstr *r);


/* ----------------- coords_x_toworkarea/coords_y_toworkarea ---------------
 * Description:   Convert x/y screen coordinates into x/y work area
 *                coordinates.
 *
 * Parameters:    int x or int y -- x or y coordinate in screen coords
 *                coords_cvtstr *r -- conversion box (screen coords and
 *                                                    scroll offsets).
 * Returns:       x or y work area coordinates.
 * Other Info:    none.
 *
 */

int coords_x_toworkarea(int x, coords_cvtstr *r);
int coords_y_toworkarea(int y, coords_cvtstr *r);


/* ------------------------ coords_box_toscreen ----------------------------
 * Description:   Converts a "box" of workarea coordinates into a "box" of
 *                screen coordinates.
 *
 * Parameters:    wimp_box *b -- workarea box to be converted
 *                coords_cvtstr *r -- conversion box (screen coords and
 *                                                    scroll offsets).
 * Returns:       void.
 * Other Info:    "b" is converted "in situ" into screen coordinates (ie.
 *                its contents change).
 *
 */

void coords_box_toscreen(wimp_box *b, coords_cvtstr *r);


/* ------------------------ coords_box_toworkarea --------------------------
 * Description:   Converts a "box" of screen coordinates into a "box" of
 *                workarea coordinates.
 *
 * Parameters:    wimp_box *b -- screen box to be converted
 *                coords_cvtstr *r -- conversion box (screen coords and
 *                                                    scroll offsets).
 * Returns:       void.
 * Other Info:    "b" is converted "in situ" into workarea coordinates (ie.
 *                its contents are changed).
 *
 */

void coords_box_toworkarea(wimp_box *b, coords_cvtstr *r);


/* ------------------------- coords_point_toscreen -------------------------
 * Description:   Converts a point (x,y) from workarea coordinates to screen
 *                coordinates.
 *
 * Parameters:    coords_pointstr *point -- the point in workarea coordinates
 *                coords_cvtstr *r -- conversion box (screen coords and
 *                                                    scroll offsets).
 * Returns:       void.
 * Other Info:    "point" is converted "in situ" into screen coordinates
 *                (ie. its contents are changed).
 *
 */

void coords_point_toscreen(coords_pointstr *point, coords_cvtstr *r);


/* ------------------------- coords_point_toworkarea -----------------------
 * Description:   Converts a point (x,y) from screen coordinates to workarea
 *                coordinates.
 *
 * Parameters:    coords_pointstr *point -- the point in screen coordinates
 *                coords_cvtstr *r -- conversion box (screen coords and
 *                                                    scroll offsets).
 * Returns:       void.
 * Other Info:    "point" is converted "in situ" into workarea coordinates
 *                (ie. its contents are changed).
 *
 */

void coords_point_toworkarea(coords_pointstr *point, coords_cvtstr *r);


/* -------------------------- coords_withinbox -----------------------------
 * Description:   Informs caller if a point (x,y) lies within a "box".
 *
 * Parameters:    coords_pointstr *point -- the point
 *                wimp_box *box -- the box.
 * Returns:       TRUE if point lies within the box.
 * Other Info:    none.
 *
 */

BOOL coords_withinbox(coords_pointstr *point, wimp_box *box);


/* ------------------------- coords_offsetbox ------------------------------
 * Description:   Offset a "box" by a given "x" and "y" displacement.
 *
 * Parameters:    wimp_box *source -- the box to be moved
 *                int byx -- "x" displacement
 *                int byy -- "y" displacement
 *                wimp_box *result -- box when offset.
 * Returns:       void.
 * Other Info:    "source" and "result" are permitted to point at the same
 *                box.
 *
 */

void coords_offsetbox(wimp_box *source, int byx, int byy, wimp_box *result);


/* ------------------------- coords_intersects -----------------------------
 * Description:   Informs caller whether a line intersects a given "box"
 *
 * Parameters:    wimp_box *line -- the line
 *                wimp_box *rect -- the box
 *                int widen -- width of line (same units as line and rect).
 * Returns:       TRUE if line intersects box.
 * Other Info:    none.
 *
 */

BOOL coords_intersects(wimp_box *line, wimp_box *rect, int widen);


/* ------------------------- coords_boxesoverlap ---------------------------
 * Description:   Informs caller whether two "boxes" cover any common area.
 *
 * Parameters:    wimp_box *box1 -- one box
 *                wimp_box *box2 -- the other box.
 * Returns:       TRUE if boxes overlap.
 * Other Info:    none.
 *
 */

BOOL coords_boxesoverlap(wimp_box *box1, wimp_box *box2);

#endif

/* end coords.h */

