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

     $RCSfile: AppIcon.mod $
  Description: A port of appicon.c from the RKM:Libraries

   Created by: fjc (Frank Copeland)
    $Revision: 1.6 $
      $Author: fjc $
        $Date: 1995/07/02 17:01:00 $

  Copyright © 1994-1995, Frank Copeland.
  This example program is part of Oberon-A.
  See Oberon-A.doc for conditions of use and distribution.

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

<* STANDARD- *>

MODULE AppIcon;

IMPORT
  SYS := SYSTEM,
  Errors,
  e   := Exec,
  u   := Utility,
  i   := Icon,
  wb  := Workbench,
  Out;

CONST
  VersionTag = "$VER: AppIcon 1.3 (25.1.95)\r\n";

VAR
  dobj    : wb.DiskObjectPtr;
  myport  : e.MsgPortPtr;
  appicon : wb.AppIconPtr;
  appmsg  : wb.AppMessagePtr;

  dropcount : LONGINT;
  x         : LONGINT;
  success   : BOOLEAN;


BEGIN (* AppIcon *)
  Errors.Init;
  IF e.SysBase.libNode.version >= 37 THEN
    dobj := NIL; myport := NIL; appicon := NIL; appmsg := NIL;
    dropcount := 0; x := 0; success := FALSE;
    IF (i.base # NIL) & (i.base.version >= 37) THEN
      IF (wb.base # NIL) & (wb.base.version >= 37) THEN
        (* This is the easy way to get some icon imagery *)
        (* Real applications should use custom imagery   *)
        dobj := i.GetDefDiskObject (wb.disk);
        IF dobj # NIL THEN
          (* The type must be set to 0 for an appIcon *)
          dobj.type := 0;

          (* The CreateMsgPort() function is on Exec version 37+ *)
          myport := e.CreateMsgPort();
          IF myport # NIL THEN
            (* Put the AppIcon up on the Workbench window *)
            appicon := wb.AddAppIcon
              ( 0, 0, "TestAppIcon", myport, NIL, dobj, u.end );
            IF appicon # NIL THEN
              (* For the sake of this example, we allow the AppIcon *)
              (* to be activated only five times.                   *)
              Out.String ("Drop files on the Workbench AppIcon\n");
              Out.String ("Example exits after 5 drops\n");

              WHILE dropcount < 5 DO
                e.WaitPort (myport);

                (* Might be more than one message at the port... *)
                LOOP
                  appmsg := SYS.VAL (wb.AppMessagePtr, e.GetMsg (myport));
                  IF appmsg = NIL THEN EXIT END;
                  IF appmsg.numArgs = 0 THEN
                    (* If numArgs is 0 the AppIcon was activated directly *)
                    Out.String ("User activated the AppIcon.\n");
                    Out.String
                      ("A Help window for the user would be good here\n")
                  ELSIF appmsg.numArgs > 0 THEN
                    (* If numArgs is >0 the AppIcon was activated by *)
                    (* having one or more Icons dropped on top of it *)
                    Out.String ( "User dropped ");
                    Out.Int (appmsg.numArgs, 0);
                    Out.String (" icons on the AppIcon.\n");
                    FOR x := 0 TO appmsg.numArgs - 1 DO
                      Out.Char ("#"); Out.Int (x + 1, 0);
                      Out.String (" name = '");
                      Out.String (appmsg.argList[x].name^);
                      Out.String ("'\n");
                    END
                  END;
                  (* Let Workbench know we're done with the message *)
                  e.ReplyMsg (appmsg)
                END;
                INC (dropcount)
              END;
              success := wb.RemoveAppIcon (appicon)
            END;
            (* Clear away any messages that arrived at the last moment *)
            LOOP
              appmsg := SYS.VAL (wb.AppMessagePtr, e.GetMsg (myport));
              IF appmsg = NIL THEN EXIT END;
              e.ReplyMsg (appmsg)
            END;
            e.DeleteMsgPort (myport)
          END;
          i.FreeDiskObject (dobj)
        END
      END
    END
  END
END AppIcon.
