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

    File:    Pointer.Restrict.c
    Author:  Copyright  1994 Lee Atkinson
    Version: 1.00 (28 Sep 1994)
    Purpose: Restrict pointer to window or icon
*/

#include "Desk.Error.h"
#include "Desk.Icon.h"
#include "Desk.KernelSWIs.h"
#include "Desk.Screen.h"
#include "Desk.Wimp.h"
#include "Desk.WimpSWIs.h"
#include "Desk.Pointer.h"


typedef char byte;

static void Lower2Bytes(int integer, byte *byte0, byte *byte1)
                             /* extracts lowest 2 bytes from input integer */
{
 union
      {
       int integer;
       byte bytes[2];
      }
       u;
 u.integer=integer;
 *byte0=u.bytes[0]; /* lowest byte */
 *byte1=u.bytes[1]; /* next lowest byte */
}

void	Desk_Pointer_RestrictToWindow(Desk_window_handle window)
{
 byte         box[9]={1};
 Desk_window_state wstate;
 Desk_Wimp_eGetWindowState(window, &wstate); /* find window size & pos. */
 Lower2Bytes(wstate.openblock.screenrect.min.x, &box[1], &box[2]);
 Lower2Bytes(wstate.openblock.screenrect.min.y, &box[3], &box[4]);
 Lower2Bytes(wstate.openblock.screenrect.max.x, &box[5], &box[6]);
 Lower2Bytes(wstate.openblock.screenrect.max.y, &box[7], &box[8]);
 Desk_OS_Word(Desk_osword_DEFINEPOINTERANDMOUSE, box);
}

void	Desk_Pointer_RestrictToIcon(Desk_window_handle window, Desk_icon_handle icon)
{
 byte      box[9]={1};
 Desk_wimp_rect rect;
 Desk_Icon_ScreenPos(window, icon, &rect);
 Lower2Bytes(rect.min.x, &box[1], &box[2]);
 Lower2Bytes(rect.min.y, &box[3], &box[4]);
 Lower2Bytes(rect.max.x, &box[5], &box[6]);
 Lower2Bytes(rect.max.y, &box[7], &box[8]);
 Desk_OS_Word(Desk_osword_DEFINEPOINTERANDMOUSE, box);
}

void	void	Desk_Pointer_RestrictToRect(Desk_wimp_rect rect)
{
 byte box[9]={1};
 Lower2Bytes(rect.min.x, &box[1], &box[2]);
 Lower2Bytes(rect.min.y, &box[3], &box[4]);
 Lower2Bytes(rect.max.x, &box[5], &box[6]);
 Lower2Bytes(rect.max.y, &box[7], &box[8]);
 Desk_OS_Word(Desk_osword_DEFINEPOINTERANDMOUSE, box);
}

void	Desk_Pointer_Unrestrict(void)
{
 byte box[9]={1, 0, 0, 0, 0};
 Desk_Screen_CacheModeInfo(); /* find screen size */
 Lower2Bytes((Desk_screen_size.x-1), &box[5], &box[6]);
 Lower2Bytes((Desk_screen_size.y-1), &box[7], &box[8]);
 Desk_OS_Word(Desk_osword_DEFINEPOINTERANDMOUSE, box);
}
