(*************************************************************************

     $RCSfile: EasyIntuition37.mod $
  Description: A port of easyintuition37.c from the RKM:Libraries, Ch 2.
               "easyintuition37.c -- Simple Intuition program for V37"
               "(Release 2) and later versions of the operating system."

   Created by: fjc (Frank Copeland)
    $Revision: 1.5 $
      $Author: fjc $
        $Date: 1995/01/25 23:52:19 $

*************************************************************************)

<* STANDARD- *>

MODULE EasyIntuition37;

IMPORT
  SYS := SYSTEM, E := Exec, U := Utility, D := Dos, G := Graphics,
  I := Intuition;

CONST

(* Position and sizes for our window *)

  winLeftEdge  =  20;
  winTopEdge   =  20;
  winWidth     = 400;
  winMinWidth  =  80;
  winHeight    = 150;
  winMinHeight =  20;

(*------------------------------------*)
PROCEDURE cleanExit (scrn : I.ScreenPtr; wind : I.WindowPtr);

BEGIN (* cleanExit *)
  (* Close things in the reverse order of opening *)
  IF wind # NIL THEN I.CloseWindow (wind) END;
  IF scrn # NIL THEN IF I.CloseScreen (scrn) THEN END END;
END cleanExit;

(*------------------------------------*)
PROCEDURE handleIDCMP (win : I.WindowPtr) : BOOLEAN;

  VAR
    done    : BOOLEAN;
    message : I.IntuiMessagePtr;
    class   : SET;

BEGIN (* handleIDCMP *)
  done := FALSE; message := NIL;
  (* Examine pending messages *)
  LOOP
    message := SYS.VAL (I.IntuiMessagePtr, E.GetMsg (win.userPort));
    IF message = NIL THEN EXIT END;
    class := message.class; (* get all the data we need from message *)

    (* When we're through with a message, reply *)
    E.ReplyMsg (message);

    (* See what events occurred *)
    IF I.closeWindow IN class THEN done := TRUE END;
  END; (* LOOP *)
  RETURN done
END handleIDCMP;

(*------------------------------------*)
PROCEDURE main ();

  VAR
    signalmask, signals : SET;
    winsignal : SHORTINT;
    done      : BOOLEAN;
    pens      : ARRAY 1 OF E.UWORD;
    screen1   : I.ScreenPtr;
    window1   : I.WindowPtr;
    tags      : ARRAY 19 OF U.TagItem;

BEGIN (* main *)
  done := FALSE; pens [0] := -1; (* ~0 *)
  screen1 := NIL; window1 := NIL;

  (* Open the screen *)
  screen1 :=
    I.OpenScreenTagsA (
      NIL,
      I.saPens,      SYS.ADR (pens),
      I.saDisplayID, G.hiresKey,
      I.saDepth,     2,
      I.saTitle,     SYS.ADR ("Our Screen"),
      U.done );
  IF screen1 = NIL THEN cleanExit (screen1, window1); HALT (D.warn) END;

  (* ... and open the window *)
  window1 :=
    I.OpenWindowTagsA (
      NIL,
      (* Specify window dimensions and limits *)

      I.waLeft,           winLeftEdge,
      I.waTop,            winTopEdge,
      I.waWidth,          winWidth,
      I.waHeight,         winHeight,
      I.waMinWidth,       winMinWidth,
      I.waMinHeight,      winMinHeight,
      I.waMaxWidth,       -1, (* ~0 *)
      I.waMaxHeight,      -1, (* ~0 *)

      (* Specify the system gadgets we want *)

      I.waCloseGadget,    E.LTRUE,
      I.waSizeGadget,     E.LTRUE,
      I.waDepthGadget,    E.LTRUE,
      I.waDragBar,        E.LTRUE,

      (* Specify other attributes *)

      I.waActivate,       E.LTRUE,
      I.waNoCareRefresh,  E.LTRUE,

      (* Specify the events we want to know about *)

      I.waIDCMP,          {I.closeWindow},

      (* Attach the window to the open screen *)

      I.waCustomScreen,   screen1,
      I.waTitle,          SYS.ADR ("EasyWindow"),
      I.waScreenTitle,    SYS.ADR ("Our Screen - EasyWindow is Active"),

      U.done );
  IF window1 = NIL THEN cleanExit (screen1, window1); HALT (D.warn) END;

  (* Set up the signals for the events we want to hear about ... *)
  winsignal := window1.userPort.sigBit;
  signalmask := {winsignal}; (* we are only waiting on IDCMP events. *)

  (* Here's the main input event loop where we wait for events.  *)
  (* We have asked Intuition to send us CLOSEWINDOW IDCMP events *)
  (* Exec will wake us if any event we are waiting for occurs.   *)
  WHILE ~done DO
    signals := E.Wait (signalmask);
    (* An event occurred - now act on the signal(s) we received. *)
    (* We were only waiting on one signal (winsignal) in our     *)
    (* signalmask, so we actually know we received winsignal.    *)
    IF winsignal IN signals THEN
      done := handleIDCMP (window1)
    END; (* IF *)
  END; (* WHILE *)
  cleanExit (screen1, window1); HALT (D.ok); (* Exit the program *)
END main;

BEGIN (* EasyIntuition37 *)
  main ();
END EasyIntuition37.
