(* ------------------------------------------------------------------------
  :Program.       DiskLabels
  :Contents.      little disklabel print program
  :Author.        Holger Bolay
  :Address.       Hoffmannstraße 168
  :Address.       D-W7250 Leonberg 1
  :History.       v1.0 22-Feb-91 [Holger]
  :History.       v1.1 18-Aug-91 [Holger] (enhanced, + NEC P6 button)
  :Copyright.     Freeware, © 1991 by Holger Bolay
  :Language.      Oberon
  :Translator.    AMIGA OBERON v2.07e
  :Imports.       RenderInfo [kai] / Borders [bne]
------------------------------------------------------------------------ *)
MODULE DiskLabels;

IMPORT d:   Dos,
       e:   Exec,
       I:   Intuition,
       ri:  RenderInfo,
       r:   Requests,
       bo:  Borders,
       y:   SYSTEM;


TYPE IntArray126 = ARRAY 2, 126 OF INTEGER;
     IntArray102 = ARRAY 2, 102 OF INTEGER;

CONST version = "DiskLabels v1.1\o$VER: DiskLabels v1.1 (18-Aug-91) © 1991 by Holger Bolay\n\r";

      header  = 1;       (* Gadget IDs...          *)
      text1   = 2;
      text2   = 3;
      print   = 4;
      necP6   = 5;

      printWidth  =  47; (* Width of Print Gadget  *)
      printHeight =  21; (* Height of Print Gadget *)

      necP6Width  =  47; (* Width of necP6 Gadget  *)
      necP6Height =  17; (* Height of necP6 Gadget *)

      (* $DataChip+ -------- bool gadgets' images -------*)
      PrinterData = IntArray126(
        (* [0] *)
        00000U,00000U,00000U,
        00000U,00000U,00000U,
        00000U,00000U,00000U,
        00000U,00000U,00000U,
        00080U,00000U,03000U,
        00080U,00000U,00C00U,
        00780U,07FFFU,003C0U,
        00D80U,00000U,00360U,
        017FFU,0FFFFU,0FFD0U,
        03555U,05555U,05558U,
        07FFFU,0FFFFU,0FFFCU,
        0D555U,05555U,0FFF6U,
        0D555U,05555U,05556U,
        0FFFFU,0FFFFU,0FFFEU,
        07FFFU,0FFFFU,0FFFCU,
        00000U,00000U,00000U,
        007C7U,0C187U,0C7E0U,
        00C6CU,0618CU,06180U,
        00DCDU,0C18CU,06180U,
        00C0CU,0618CU,06180U,
        00C0CU,0618CU,06180U,
        00000U,00000U,00000U,
        00000U,00000U,00000U,
        00000U,00000U,00000U,
        00000U,00000U,00000U,
        0007FU,0FFFFU,0C000U,
        0007FU,0FFFFU,0F000U,
        0007FU,08000U,0FC00U,
        0067FU,0FFFFU,0FCC0U,
        00C00U,00000U,00060U,
        01FFFU,0FFFFU,0FFF0U,
        00000U,00000U,09240U,
        03FFFU,0FFFEU,00018U,
        07FFFU,0FFFFU,0FFF8U,
        01555U,05555U,05550U,
        00000U,00000U,00000U,
        00000U,00000U,00000U,
        00000U,00000U,00000U,
        00000U,00000U,00000U,
        00000U,00000U,00000U,
        00000U,00000U,00000U,
        00000U,00000U,00000U,
        (* [1] *)
        00080U,00000U,03000U,
        00080U,00000U,00C00U,
        00080U,07FFFU,00200U,
        00080U,00000U,00200U,
        00083U,0FFFFU,00200U,
        00080U,00000U,00200U,
        00783U,0FFFFU,003C0U,
        00D80U,00000U,00360U,
        017FFU,0FFFFU,0FFD0U,
        03555U,05555U,05558U,
        07FFFU,0FFFFU,0FFFCU,
        0D555U,05555U,0FFF6U,
        0D555U,05555U,05556U,
        0FFFFU,0FFFFU,0FFFEU,
        07FFFU,0FFFFU,0FFFCU,
        00000U,00000U,00000U,
        007C7U,0C187U,0C7E0U,
        00C6CU,0618CU,06180U,
        00DCDU,0C18CU,06180U,
        00C0CU,0618CU,06180U,
        00C0CU,0618CU,06180U,
        0007FU,0FFFFU,0C000U,
        0007FU,0FFFFU,0F000U,
        0007FU,08000U,0FC00U,
        0007FU,0FFFFU,0FC00U,
        0007CU,00000U,0FC00U,
        0007FU,0FFFFU,0FC00U,
        0007CU,00000U,0FC00U,
        0067FU,0FFFFU,0FCC0U,
        00C00U,00000U,00060U,
        01FFFU,0FFFFU,0FFF0U,
        00000U,00000U,09240U,
        03FFFU,0FFFEU,00018U,
        07FFFU,0FFFFU,0FFF8U,
        01555U,05555U,05550U,
        00000U,00000U,00000U,
        00000U,00000U,00000U,
        00000U,00000U,00000U,
        00000U,00000U,00000U,
        00000U,00000U,00000U,
        00000U,00000U,00000U,
        00000U,00000U,00000U);

      necP6Data = IntArray102(
        (* [0] *)
        00080U,00000U,03000U,
        00080U,00000U,00C00U,
        00780U,07FFFU,003C0U,
        00D80U,00000U,00360U,
        017FFU,0FFFFU,0FFD0U,
        03555U,05555U,05558U,
        07FFFU,0FFFFU,0FFFCU,
        0D555U,05555U,0FFF6U,
        0D555U,05555U,05556U,
        0FFFFU,0FFFFU,0FFFEU,
        07FFFU,0FFFFU,0FFFCU,
        00000U,00000U,00000U,
        07C7EU,07E00U,07C7CU,
        0C6C0U,0C000U,0C6C0U,
        0C6DCU,0C000U,0DCFCU,
        0C6C0U,0C000U,0C0C6U,
        0C67EU,07E00U,0C07CU,
        0007FU,0FFFFU,0C000U,
        0007FU,0FFFFU,0F000U,
        0007FU,08000U,0FC00U,
        0067FU,0FFFFU,0FCC0U,
        00C00U,00000U,00060U,
        01FFFU,0FFFFU,0FFF0U,
        00000U,00000U,09240U,
        03FFFU,0FFFEU,00018U,
        07FFFU,0FFFFU,0FFF8U,
        01555U,05555U,05550U,
        00000U,00000U,00000U,
        00000U,00000U,00000U,
        00000U,00000U,00000U,
        00000U,00000U,00000U,
        00000U,00000U,00000U,
        00000U,00000U,00000U,
        00000U,00000U,00000U,
        (* [1] *)
        00080U,00000U,03000U,
        00080U,00000U,00C00U,
        00780U,07FFFU,003C0U,
        00D80U,00000U,00360U,
        017FFU,0FFFFU,0FFD0U,
        03555U,05555U,05558U,
        07FFFU,0FFFFU,0FFFCU,
        0D555U,05555U,0FFF6U,
        0D555U,05555U,05556U,
        0FFFFU,0FFFFU,0FFFEU,
        07FFFU,0FFFFU,0FFFCU,
        00000U,00000U,00000U,
        07C7CU,07C7EU,07CC0U,
        0C6C6U,0C6DBU,0C6C0U,
        0C6C6U,0DCDBU,0DEC0U,
        0C6C6U,0C6DBU,0C6E0U,
        0C67CU,0C6DBU,0C67EU,
        0007FU,0FFFFU,0C000U,
        0007FU,0FFFFU,0F000U,
        0007FU,08000U,0FC00U,
        0067FU,0FFFFU,0FCC0U,
        00C00U,00000U,00060U,
        01FFFU,0FFFFU,0FFF0U,
        00000U,00000U,09240U,
        03FFFU,0FFFEU,00018U,
        07FFFU,0FFFFU,0FFF8U,
        01555U,05555U,05550U,
        00000U,00000U,00000U,
        00000U,00000U,00000U,
        00000U,00000U,00000U,
        00000U,00000U,00000U,
        00000U,00000U,00000U,
        00000U,00000U,00000U,
        00000U,00000U,00000U);

VAR MyWindow:     I.WindowPtr;             (* DiskLabels' Window         *)
    Msg:          I.IntuiMessagePtr;       (* Received Message           *)
    Gadget:       I.GadgetPtr;             (* Gadget causing a Message   *)

    PrintGadg,                             (* The execute gadget         *)
    necP6Gadg,                             (* NEC P6 or other switch     *)
    HeaderGadg:   I.Gadget;                (* 1st (Header) text gadget   *)
    TextGadg:     ARRAY 2 OF I.Gadget;     (* 2nd & 3rd text gadget      *)
    HeaderInfo:   I.StringInfo;            (* Header gadget's StringInfo *)
    TextInfo:     ARRAY 2 OF I.StringInfo; (* Text gadgets' StringInfo   *)
    HeaderTxt,                             (* Header text memory         *)
    UndoBuff:     ARRAY 30 OF CHAR;        (* Undo Buffer of gadgets     *)
    TextTxt:      ARRAY 2, 30 OF CHAR;     (* Texts' text memory         *)
    RI:           ri.RenderInfoPtr;        (* rendering information      *)
    HeaderBorder,                          (* border of Header gadget    *)
    TextBorder:   bo.Border3D;             (* borders of text gadgets    *)
    PrintImage,                            (* Print gadget images        *)
    necP6Image:   ARRAY 2 OF I.Image;      (* necP6 gadget images        *)
    out:          d.FileHandlePtr;         (* pointer to a file handle   *)


(*-----------------------------------------------------------------------*)
(* Print the disklabel and init escape sequences                         *)
(*-----------------------------------------------------------------------*)
PROCEDURE PrintIt;

CONST initSeq       = "\e[2p\e[1 F\e[17t\e[3;27s";
      (* contains: - proportion spacing on            (ESC[2p)
                   - auto center                      (ESC[1 F)
                   - set form length to 17            (ESC[nt)
                   - left and right margins to 3 & 27 (ESC[Pn1;Pn2s) *)

      enlargedOn    = "\e[6w";
      enlargedOff   = "\e[5w";

      (* NEC P6 vertical enlargement *)
      p6verticalOn  = "\e[3\"r\x1cV\x01";
      p6verticalOff = "\e[3\"r\x1cV\x00";

VAR succ: BOOLEAN;

  PROCEDURE Print(str: ARRAY OF CHAR);
  BEGIN
    IF succ THEN
      succ := d.Write(out, str, LEN(str)-1) = LEN(str)-1;
    END;
  END Print;

BEGIN
  out := d.Open("PRT:", d.newFile);
  IF out = NIL THEN
    IF NOT r.RequestWin("DiskLabels Message:", "Unable to open PRT:", "",
                        "OK", MyWindow) THEN RETURN END;
  ELSE
    succ := TRUE;
    Print(initSeq);
    Print(enlargedOn);
    IF I.selected IN necP6Gadg.flags THEN
      Print("\n");
    ELSE
      Print(p6verticalOn);
      Print("\n");
    END;
    Print(HeaderTxt);
    Print("\n");
    Print(enlargedOff);
    IF I.selected IN necP6Gadg.flags THEN
      Print("\n\n");
    ELSE
      Print(p6verticalOff);
      Print("\n");
    END;
    Print(TextTxt[0]);
    Print("\n\n");
    Print(TextTxt[1]);
    Print("\n\f");
    IF NOT d.Close(out) OR NOT succ THEN
      IF NOT r.RequestWin("DiskLabels Message:", "Error while printing...",
                          "", "OK", MyWindow) THEN RETURN END;
    END;
    out := NIL;
  END;
END PrintIt;

(*-----------------------------------------------------------------------*)
(* Build up display                                                      *)
(*-----------------------------------------------------------------------*)
PROCEDURE OpenDisplay;

VAR nw: I.NewWindow;

BEGIN
  RI := ri.GetRenderInfo(NIL);

  (*-------- Window --------*)
  nw := I.NewWindow(0, 0, 0, 0, 0, 1, LONGSET{I.gadgetUp, I.closeWindow},
                    LONGSET{I.rmbTrap, I.windowDrag, I.windowDepth,
                    I.windowClose, I.activate}, NIL, NIL, y.ADR(version),
                    NIL, NIL, 0, 0, -1, -1, SET{I.wbenchScreen});
  nw.height := RI^.windowTitle+(11*RI^.fontSize) DIV 2+3*2+printHeight+
               RI^.windowBottom;
  nw.width := RI^.windowLeft+RI^.windowRight+27*RI^.fontSize+4;
  nw.leftEdge := (RI^.screenWidth-nw.width) DIV 2;
  nw.topEdge := (RI^.screenHeight-nw.height) DIV 2;

  (*-------- Print button --------*)
  PrintImage[0] := I.Image(0, 0, printWidth, printHeight, 2, NIL,
                           SHORTSET{0..1}, SHORTSET{}, NIL);
  PrintImage[0].imageData := y.ADR(PrinterData[0]);

  PrintImage[1] := I.Image(0, 0, printWidth, printHeight, 2, NIL,
                           SHORTSET{0..1}, SHORTSET{}, NIL);
  PrintImage[1].imageData := y.ADR(PrinterData[1]);

  PrintGadg := I.Gadget(NIL, 0, 0, printWidth, printHeight,
                        SET{I.gadgImage, I.gadgHImage},
                        SET{I.relVerify}, I.boolGadget, NIL, NIL, NIL,
                        LONGSET{}, NIL, print, NIL);
  PrintGadg.leftEdge := nw.width-RI^.fontSize-RI^.windowRight-printWidth;
  PrintGadg.topEdge := nw.height-RI^.windowBottom-RI^.fontSize DIV 2-
                       printHeight;
  PrintGadg.gadgetRender := y.ADR(PrintImage[0]);
  PrintGadg.selectRender := y.ADR(PrintImage[1]);

  (*-------- NEC P6/other switch --------*)
  necP6Image[0] := I.Image(0, 0, necP6Width, necP6Height, 2, NIL,
                           SHORTSET{0..1}, SHORTSET{}, NIL);
  necP6Image[0].imageData := y.ADR(necP6Data[0]);

  necP6Image[1] := I.Image(0, 0, necP6Width, necP6Height, 2, NIL,
                           SHORTSET{0..1}, SHORTSET{}, NIL);
  necP6Image[1].imageData := y.ADR(necP6Data[1]);

  necP6Gadg := I.Gadget(NIL, 0, 0, necP6Width, necP6Height,
                        SET{I.gadgImage, I.gadgHImage},
                        SET{I.relVerify, I.toggleSelect}, I.boolGadget, NIL,
                        NIL, NIL, LONGSET{}, NIL, necP6, NIL);
  necP6Gadg.leftEdge := RI^.windowLeft+RI^.fontSize;
                     (* nw.width-2*RI^.fontSize-RI^.windowRight-printWidth-
                        necP6Width; *)
  necP6Gadg.topEdge := nw.height-RI^.windowBottom-RI^.fontSize DIV 2-
                       necP6Height;
  necP6Gadg.gadgetRender := y.ADR(necP6Image[0]);
  necP6Gadg.selectRender := y.ADR(necP6Image[1]);
  necP6Gadg.nextGadget := y.ADR(PrintGadg);

  (*-------- 3rd Text Gadget --------*)
  TextInfo[1] := I.StringInfo(NIL, NIL, 0, 25, 0, 0, 0, 0, 0, 0, NIL, 0,
                               NIL);
  TextInfo[1].buffer := y.ADR(TextTxt[1]);
  TextInfo[1].undoBuffer := y.ADR(UndoBuff);
  TextGadg[1] := I.Gadget(NIL, 0, 0, 0, 0, SET{},
                          SET{I.relVerify, I.stringCenter}, I.strGadget,
                          NIL, NIL, NIL, LONGSET{}, NIL, text2, NIL);
  TextGadg[1].width := 25*RI^.fontSize;
  TextGadg[1].height := RI^.fontSize;
  TextGadg[1].leftEdge := (nw.width - TextGadg[1].width) DIV 2;
  TextGadg[1].topEdge := RI^.windowTitle+(7*RI^.fontSize) DIV 2+4;
  TextGadg[1].gadgetRender := bo.InitBorder(TextBorder, RI^.highlight,
                                            RI^.shadow, RI^.backPen,
                                            TextGadg[1].width+4,
                                            TextGadg[1].height+2);
  bo.MoveBorder(TextBorder, -2, -1);
  TextGadg[1].nextGadget := y.ADR(necP6Gadg);
  TextGadg[1].specialInfo := y.ADR(TextInfo[1]);

  (*-------- 2nd Text Gadget --------*)
  TextInfo[0] := I.StringInfo(NIL, NIL, 0, 25, 0, 0, 0, 0, 0, 0, NIL, 0,
                               NIL);
  TextInfo[0].buffer := y.ADR(TextTxt[0]);
  TextInfo[0].undoBuffer := y.ADR(UndoBuff);
  TextGadg[0] := I.Gadget(NIL, 0, 0, 0, 0, SET{},
                          SET{I.relVerify, I.stringCenter}, I.strGadget,
                          NIL, NIL, NIL, LONGSET{}, NIL, text1, NIL);
  TextGadg[0].width := 25*RI^.fontSize;
  TextGadg[0].height := RI^.fontSize;
  TextGadg[0].leftEdge := (nw.width - TextGadg[0].width) DIV 2;
  TextGadg[0].topEdge := RI^.windowTitle+2*RI^.fontSize+2;
  TextGadg[0].gadgetRender := TextGadg[1].gadgetRender;
  TextGadg[0].nextGadget := y.ADR(TextGadg[1]);
  TextGadg[0].specialInfo := y.ADR(TextInfo[0]);

  (*-------- 1st (Header) Text Gadget --------*)
  HeaderInfo := I.StringInfo(NIL, NIL, 0, 13, 0, 0, 0, 0, 0, 0, NIL, 0,
                            NIL);
  HeaderInfo.buffer := y.ADR(HeaderTxt);
  HeaderInfo.undoBuffer := y.ADR(UndoBuff);
  HeaderGadg := I.Gadget(NIL, 66, 20, 112, 9, SET{},
                         SET{I.relVerify, I.stringCenter}, I.strGadget,
                         NIL, NIL, NIL, LONGSET{}, NIL, header, NIL);
  HeaderGadg.width := 13*RI^.fontSize;
  HeaderGadg.height := RI^.fontSize;
  HeaderGadg.leftEdge := (nw.width - HeaderGadg.width) DIV 2;
  HeaderGadg.topEdge := RI^.windowTitle+RI^.fontSize DIV 2;
  HeaderGadg.gadgetRender := bo.InitBorder(HeaderBorder, RI^.highlight,
                                           RI^.shadow, RI^.backPen,
                                           HeaderGadg.width+4,
                                           HeaderGadg.height+2);
  bo.MoveBorder(HeaderBorder, -2, -1);
  HeaderGadg.nextGadget := y.ADR(TextGadg[0]);
  HeaderGadg.specialInfo := y.ADR(HeaderInfo);

  (*----------------*)

  nw.firstGadget := y.ADR(HeaderGadg);
  MyWindow := I.OpenWindow(nw);
  r.Assert(MyWindow#NIL, "Unable to open Window!");
  (*-------- Unlock screen (!!) and free the mem of RI --------*)
  ri.CleanUpRenderInfo(RI);
END OpenDisplay;

(*-----------------------------------------------------------------------*)
(* Get Messages                                                          *)
(*-----------------------------------------------------------------------*)
BEGIN (* Main *)
  OpenDisplay;
  WHILE NOT I.ActivateGadget(HeaderGadg, MyWindow, NIL) DO
    d.Delay(10);
  END; (* WHILE *)
  LOOP
    e.WaitPort(MyWindow^.userPort);
    REPEAT
      Msg := e.GetMsg(MyWindow^.userPort);
      IF Msg # NIL THEN
        IF I.closeWindow IN Msg.class THEN e.ReplyMsg(Msg); EXIT END;
        Gadget   := Msg^.iAddress;
        e.ReplyMsg(Msg);
        CASE Gadget^.gadgetID OF
        | header:
          y.SETREG(0, I.ActivateGadget(TextGadg[0], MyWindow, NIL));
        | text1:
          y.SETREG(0, I.ActivateGadget(TextGadg[1], MyWindow, NIL));
        | text2:
          y.SETREG(0, I.ActivateGadget(HeaderGadg, MyWindow, NIL));
        | necP6:
        | print:
          PrintIt;
        END; (* IF *)
      END; (* IF *)
    UNTIL Msg = NIL;
  END; (* LOOP *)
(*-----------------------------------------------------------------------*)
(* Execute »InitPrinter« and free the memory...                          *)
(*-----------------------------------------------------------------------*)
CLOSE
  IF MyWindow # NIL THEN I.CloseWindow(MyWindow); MyWindow := NIL END;
  y.SETREG(0, d.Execute("InitPrinter >NIL:", NIL, NIL));
END DiskLabels.
