IMPLEMENTATION MODULE GSXBUTON;

FROM STORAGE IMPORT ALLOCATE, DEALLOCATE;
FROM GSXMAIN IMPORT DrawMode, xor;
FROM GSXFILL IMPORT FillStyle, solid, FillBar;
FROM GSXIN   IMPORT CursorX, CursorY;

TYPE
  BUTTON      = POINTER TO ButtonEntry;
  ButtonEntry = RECORD
  		 X ,  Y : CARDINAL;	(* Lower left edge of button *)
  		 dX, dY : CARDINAL;	(* Dimensions of button      *)
		 P      : ButtonProc;	(* Associated actions        *)
   	        END;

PROCEDURE CreateButton ( x, y, dx, dy : CARDINAL; p : ButtonProc ) : BUTTON;
VAR
  NewButton : BUTTON;
BEGIN
  NEW ( NewButton );
  WITH NewButton^ DO
    X  := x;
    Y  := y;
    dX := dx;
    dY := dy;
    P  := p;
  END;
  RETURN NewButton;
END CreateButton;

PROCEDURE DeleteButton ( VAR Button : BUTTON );
BEGIN
  DISPOSE ( Button );
  Button := NIL;
END DeleteButton;

PROCEDURE CursorAtButton ( Button : BUTTON ) : BOOLEAN;
BEGIN
  IF Button = NIL THEN
    RETURN FALSE
  ELSE
    WITH Button^ DO
      RETURN ( X <= CursorX )
         AND ( Y <= CursorY )
         AND ( X + dX > CursorX )
         AND ( Y + dY > CursorY );
    END;
  END;
END CursorAtButton;

PROCEDURE FlipButtonArea ( Button : BUTTON );
BEGIN
  IF Button <> NIL THEN
    DrawMode ( xor );
    FillStyle ( solid );
    WITH Button^ DO
      FillBar ( X, Y, dX, dY );
    END;
  END;
END FlipButtonArea;

PROCEDURE ExecuteButton ( Button : BUTTON; C : CHAR );
BEGIN
  IF Button <> NIL THEN
    Button^.P ( C );
  END;
END ExecuteButton;

END GSXBUTON.
                                                                                                                  