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

     $RCSfile: CloneScreen.mod $
  Description: A port of clonescreen.c

               Clone an existing public screen.

     Requires: AmigaOS 2.04+ (Kickstart 37+)

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

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

<* STANDARD- *>

MODULE CloneScreen;

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

CONST
  VersionTag = "$VER: CloneScreen 1.3 (24.1.95)\r\n";

  pubScreenName = "Workbench";

(*------------------------------------*)
PROCEDURE cloneScreen (pubScreenName : ARRAY OF CHAR);

  VAR
    myScreen : I.ScreenPtr;
    screenModeID : LONGINT;
    pubScrFontName : E.LSTRPTR;
    fontName : E.LSTRPTR;
    fontNameSize : LONGINT;
    pubScreenFont : G.TextAttr;
    openedFont : G.TextFontPtr;
    pubScreen : I.ScreenPtr;
    screenDrawInfo : I.DrawInfoPtr;

<*$CopyArrays-*>
BEGIN (* cloneScreen *)
  pubScreen := I.LockPubScreen (pubScreenName);
  IF pubScreen # NIL THEN
    (*
    ** Get the DrawInfo structure from the locked screen
    ** This returns pen, depth and font info.
    *)
    screenDrawInfo := I.GetScreenDrawInfo (pubScreen);
    IF screenDrawInfo # NIL THEN
      screenModeID := G.GetVPModeID (SYS.ADR (pubScreen.viewPort));
      IF screenModeID # G.invalidID THEN
        (*
        ** Get a copy of the font
        ** The name of the font must be copied as the public screen may
        ** go away at any time after we unlock it.
        ** Allocate enough memory to copy the font name, create a
        ** TextAttr that matches the font, and open the font.
        *)
        pubScrFontName := screenDrawInfo.font.message.node.name;
        fontNameSize := 1 + Str.Length (pubScrFontName^);
        (* Program will halt if allocation fails *)
        SYS.NEW (fontName, fontNameSize);
        COPY (pubScrFontName^, fontName^);
        pubScreenFont.name := fontName;
        pubScreenFont.ySize := screenDrawInfo.font.ySize;
        pubScreenFont.style := screenDrawInfo.font.style;
        pubScreenFont.flags := screenDrawInfo.font.flags;
        openedFont := G.OpenFont (pubScreenFont);
        IF openedFont # NIL THEN
          (*
          ** screenModeID may now be used in a call to
          ** OpenScreenTags () with the tag saDisplayID
          *)
          myScreen := I.OpenScreenTagsA ( NIL,
              I.saWidth,      LONG (pubScreen.width),
              I.saHeight,     LONG (pubScreen.height),
              I.saDepth,      LONG (screenDrawInfo.depth),
              I.saOverscan,   I.oScanText,
              I.saAutoScroll, E.LTRUE,
              I.saPens,       screenDrawInfo.pens,
              I.saFont,       SYS.ADR (pubScreenFont),
              I.saDisplayID,  screenModeID,
              I.saTitle,      SYS.ADR ("Cloned screen"),
              U.end );
          IF myScreen # NIL THEN
            (*
            ** Free the drawinfo an public screen as we don't
            ** need them any more. We now have our own screen.
            *)
            I.FreeScreenDrawInfo (pubScreen, screenDrawInfo);
            screenDrawInfo := NIL;
            I.UnlockPubScreen (pubScreenName, pubScreen);
            pubScreen := NIL;

            D.Delay (300); (* should be rest_of_program *)

            I.OldCloseScreen (myScreen)
          END;
          G.CloseFont (openedFont)
        END
      END
    END
  END
END cloneScreen;

BEGIN (* CloneScreen *)
  ASSERT (I.base.libNode.version >= 37);
  ASSERT (G.base.libNode.version >= 37);

  cloneScreen (pubScreenName)
END CloneScreen.
