-- FormCreate.mesa  Edited by Sweet, 15-Jan-81  9:23:51

DIRECTORY
  IODefs: FROM "iodefs" USING [SP],
  LedgerDefs: FROM "ledgerdefs",
  PressDefs: FROM "pressdefs",
  StringDefs: FROM "stringdefs";

FormCreate: PROGRAM
  IMPORTS LedgerDefs, PressDefs, StringDefs
  EXPORTS LedgerDefs =
  BEGIN OPEN IODefs, PressDefs, LedgerDefs;


-- **************************************************************
--	Shared variables
-- **************************************************************

  LastCheckRow: PUBLIC Row ← LAST[Row];
  bindingMargin: PUBLIC Mica ← 0;


-- **************************************************************
--	Draw the blank form
-- **************************************************************

  SplatString: PUBLIC PROCEDURE [x, y: Mica, s: STRING] =
    BEGIN
    IF currentHalf = 1 THEN
      PressDefs.PutText[ -- rotated 90 degrees
        p: pfd, 
        str: s, 
        xleft: MicaWidth-(y+leftY), 
        ybase: x+leftX]
    ELSE PressDefs.PutText[ -- rotated 270 degrees
        p: pfd, 
        str: s, 
        xleft: y+rightY, 
        ybase: MicaHeight-(x+rightX)]
    END;

  DrawHoriz: PROCEDURE [x, y, length, thick: Mica] =
    BEGIN -- rotated 90 degrees
    IF currentHalf = 1 THEN
      PressDefs.PutRectangle[
        p: pfd, 
        xstart: MicaWidth-(y+leftY)-thick, 
        ystart: x+leftX, 
        xlen: thick, 
        ylen: length]
    ELSE PressDefs.PutRectangle[
        p: pfd, 
        xstart: y+rightY, 
        ystart: MicaHeight-(x+rightX)-length, 
        xlen: thick, 
        ylen: length];
    END;

  DrawVert: PROCEDURE [x, y, length, thick: Mica] =
    BEGIN -- rotated 90 degrees
    IF currentHalf = 1 THEN
      PressDefs.PutRectangle[
        p: pfd, 
        xstart: MicaWidth-(y+leftY)-length, 
        ystart: x+leftX, 
        xlen: length, 
        ylen: thick]
    ELSE PressDefs.PutRectangle[
        p: pfd, 
        xstart: y+rightY, 
        ystart: MicaHeight-(x+rightX)-thick, 
        xlen: length, 
        ylen: thick];
    END;

  FormVert: PROCEDURE [x: Mica, d: CARDINAL, thick: Mica] = INLINE
    BEGIN
    DrawVert[x:x, y: BottomMar + RowHeight*d,
	length: FullHeight - RowHeight*d, thick: thick];
    END;

  DrawHorizontals: PROCEDURE [width: Mica] =
    BEGIN
    r: Row;
    DrawHoriz[x: LeftMar, y: BottomMar + FullHeight,
	length: width, thick: WideLine];
    DrawHoriz[x: LeftMar, y: RowY[0],
	length: width, thick: MediumLine];
    FOR r IN [1..LastCheckRow) DO
      DrawHoriz[x: LeftMar, y: RowY[r],
	length: width, thick: NarrowLine];
      ENDLOOP;
    FOR r ← LastCheckRow, r+(IF budgetGiven THEN 3 ELSE 1)
        WHILE r < LAST[Row] DO
      DrawHoriz[x: LeftMar, y: RowY[r],
	length: width, thick: MediumLine];
      IF budgetGiven THEN 
	BEGIN
	DrawHoriz[x: LeftMar, y: RowY[r+1],
	  length: width, thick: NarrowLine];
        DrawHoriz[x: LeftMar, y: RowY[r+2],
	  length: width, thick: NarrowLine];
	END;
      ENDLOOP;
    DrawHoriz[x: LeftMar, y: BottomMar, length: width, thick: WideLine];
    END;

  ColPos: ARRAY [0..7] OF Mica = [LineNoPos, DateColPos, CheckColPos, PayeeColPos, IncomeColPos, ExpenseColPos, CategoryColPos, FinalLineNoPos1];
  ColThick: ARRAY [0..7] OF Mica = [WideLine, MediumLine, MediumLine, MediumLine, MediumLine, MediumLine, WideLine, MediumLine];
  ColDelta: ARRAY [0..7] OF INTEGER = [0, 0, TotalRows, TotalRows, TotalRows-1, TotalRows-1, 0, 0];
  ColDeltaNB: ARRAY [0..7] OF INTEGER = [0, 0, 2, 2, 1, 1, 0, 0];

  DrawVerticals1: PROCEDURE [totals: BOOLEAN] =
    BEGIN
    c: Column;
    i: CARDINAL;
    FOR i IN [0..7] DO
      FormVert[x: ColPos[i],
	d: IF totals THEN
	    (IF budgetGiven THEN ColDelta[i] ELSE ColDeltaNB[i])
	  ELSE 0,
	thick: ColThick[i]];
      ENDLOOP;
    IF totals THEN
      BEGIN
      DrawVert[x: NetColPos, y: RowY[LastCheckRow+1],
	length: RowHeight, thick: MediumLine];
      DrawVert[x: NetColPos,
	y: RowY[LastCheckRow+(IF budgetGiven THEN 4 ELSE 2)],
	length: RowHeight, thick: MediumLine];
      DrawVert[x: IncomeColPos,
	y: RowY[LastCheckRow+(IF budgetGiven THEN 4 ELSE 2)],
	length: RowHeight, thick: MediumLine];
      DrawVert[x: ExpenseColPos,
	y: RowY[LastCheckRow+(IF budgetGiven THEN 4 ELSE 2)],
	length: RowHeight, thick: MediumLine];
      END;
    FOR c IN [1..BreakColumn) DO
      BEGIN
      x: Mica = CategoryX[c];
      SELECT TRUE FROM
	totalInfo[c].isTotal => FormVert[x: x, d: 0, thick: MediumLine];
	totalInfo[c-1].isTotal => FormVert[x: x, d: 0, thick: WideLine];
	ENDCASE =>
	  BEGIN
	  DrawVert[x: x+(MediumLine-NarrowLine)/2, y: BottomMar,
	    length: FullHeight-2*RowHeight,
	    thick: NarrowLine];
	  DrawVert[x: x, y: RowY[0],
	    length: 2*RowHeight, thick: MediumLine];
	  END;
      END;
      ENDLOOP;
    DrawVert[x: FinalLineNoPos1 + LineNoColWidth, y: BottomMar,
	length: FullHeight+WideLine, thick: WideLine];
    END;

  DrawVerticals2: PROCEDURE [totals: BOOLEAN] =
    BEGIN
    c: Column;
    FormVert[x: LineNoPos, d: 0, thick: WideLine];
    FormVert[x: DateColPos, d: 0, thick: MediumLine];
    FOR c IN (BreakColumn..LAST[Column]] DO
      BEGIN
      x: Mica = CategoryX[c];
      SELECT TRUE FROM
	totalInfo[c].isTotal => FormVert[x: x, d: 0, thick: MediumLine];
	totalInfo[c-1].isTotal => FormVert[x: x, d: 0, thick: WideLine];
	ENDCASE =>
	  BEGIN
	  DrawVert[x: x+(MediumLine-NarrowLine)/2, y: BottomMar,
	    length: FullHeight-2*RowHeight,
	    thick: NarrowLine];
	  DrawVert[x: x, y: RowY[0],
	    length: 2*RowHeight, thick: MediumLine];
	  END;
      END;
      ENDLOOP;
    DrawVert[x: FinalLineNoPos2, y: BottomMar,
	length: FullHeight, thick: MediumLine];
    DrawVert[x: FinalLineNoPos2 + LineNoColWidth, y: BottomMar,
	length: FullHeight+WideLine, thick: WideLine];
    END;

  MonthName: PUBLIC ARRAY [1..12] OF STRING ← ["January", "February", "March",
	"April", "May", "June", "July", "August", "September", "October",
	"November", "December"];


  currentHalf: [1..2];

  CreateForm: PUBLIC PROCEDURE [half: [1..2], date: SmallDate, totals: BOOLEAN] =
    BEGIN
    c: Column;
    i: CARDINAL;
    s: STRING ← [40];
    delta: Mica;

    currentHalf ← half;
    SetFontRotation[IF half = 1 THEN 5400 ELSE 16200];
    SetCurrentFont[Hv6, medium, regular];
    DrawHorizontals[IF half = 1 THEN FormWidth1 ELSE FormWidth2];
    FOR i IN [1..LastCheckRow] DO
      s.length ← 0;
      StringDefs.AppendDecimal[s, i];
      CenterString[x: LineNoPos + WideLine,
	y: RowY[i],
	s: s,
	width: LineNoColWidth - WideLine];
      CenterString[
	x: (IF half = 1 THEN FinalLineNoPos1 ELSE FinalLineNoPos2)+MediumLine,
	y: RowY[i],
	s: s,
	width: LineNoColWidth - MediumLine];
      ENDLOOP;
    SetCurrentFont[Hv10, medium, regular];
    s.length ← 0;
    StringDefs.AppendString[s, MonthName[date.month]];
    StringDefs.AppendString[s, " 19"];
    StringDefs.AppendDecimal[s, date.year];
    IF half = 1 THEN
      BEGIN
      LJString[x: LeftMar, y: TitleRowY, s: s];
      DrawVerticals1[totals];
      CenterString[x: DateColPos,
	y: HeadingRowY,
	s: "Da",
	width: DateColWidth];
      CenterString[x: CheckColPos,
	y: HeadingRowY,
	s: "Chk",
	width: CheckColWidth];
      CenterString[x: PayeeColPos,
	y: HeadingRowY,
	s: "Payee",
	width: PayeeColWidth];
      CenterString[x: IncomeColPos,
	y: HeadingRowY,
	s: "Deposits",
	width: AmountColWidth];
      CenterString[x: ExpenseColPos,
	y: HeadingRowY,
	s: "With-",
	width: AmountColWidth];
      CenterString[x: ExpenseColPos,
	y: HeadingRowY-RowHeight+4*Dot,
	s: "drawals",
	width: AmountColWidth];
      SetCurrentFont[Hv8, medium, regular];
      delta ← WideLine;
      FOR c IN [0..BreakColumn) DO
	IF categoryName[c] # NIL THEN HeadingString[
	  x: CategoryColPos + delta + c*CategoryColWidth,
	  y: HeadingRowY,
	  s: categoryName[c],
	  width: CategoryColWidth - delta];
        delta ← MediumLine;
	ENDLOOP;
      IF totals THEN
	BEGIN
        SetCurrentFont[Hv10, medium, regular];
        s.length ← 0;
        StringDefs.AppendString[s, MonthName[date.month]];
        StringDefs.AppendString[s, " Total"];
        LJString[x: DateColPos,
          y: RowY[LastCheckRow+1],
          s: s];
        LJString[x: DateColPos,
          y: RowY[LastCheckRow+(IF budgetGiven THEN 4 ELSE 2)],
          s: "YTD Total"];
        IF budgetGiven THEN
	  BEGIN
          s.length ← 0;
          StringDefs.AppendString[s, MonthName[date.month]];
          StringDefs.AppendString[s, " Budget"];
          LJString[x: DateColPos,
            y: RowY[LastCheckRow+2],
            s: s];
          LJString[x: DateColPos,
            y: RowY[LastCheckRow+3],
            s: "  variance"];
          LJString[x: DateColPos,
            y: RowY[LastCheckRow+5],
            s: "YTD Budget"];
          LJString[x: DateColPos,
            y: RowY[LastCheckRow+6],
            s: "  variance"];
	  END;
	END;
      END
    ELSE
      BEGIN
      RJString[x: LeftMar, y: TitleRowY, s: s, width: FormWidth2];
      DrawVerticals2[totals];
      SetCurrentFont[Hv8, medium, regular];
      delta ← WideLine;
      FOR c IN [BreakColumn..LAST[Column]] DO
	IF categoryName[c] # NIL THEN HeadingString[
	  x: ExtendedCategoryPos + delta + (c-BreakColumn)*CategoryColWidth,
	  y: HeadingRowY,
	  s: categoryName[c],
	  width: CategoryColWidth - delta];
        delta ← NarrowLine;
	ENDLOOP;
      END;
    END;

  HeadingString: PROCEDURE [x, y: Mica, s: STRING, width: Mica] =
    BEGIN OPEN StringDefs;
    s1: STRING ← [50];
    adesc: SubStringDescriptor ← 
      [base: s1, offset: 0, length: 1];
    ss: SubString = @adesc;
    clen, tlen: Mica;
    lastBlank: CARDINAL ← 0;
    twoLines: BOOLEAN ← FALSE;
    AppendString[s1, s];
    tlen ← 0;
    UNTIL Exhausted[ss] DO
      clen ← GetWidthOfCharacter[CurrentChar[ss]];
      IF CurrentChar[ss] = '/ THEN 
	BEGIN lastBlank ← ss.offset; twoLines ← TRUE; EXIT END;
      IF CurrentChar[ss] = SP THEN lastBlank ← ss.offset;
      IF clen + tlen > width THEN BEGIN twoLines ← TRUE; EXIT END;
      tlen ← tlen + clen;
      Bump[ss];
      ENDLOOP;
    IF ~ twoLines THEN CenterString[x,y,s,width]
    ELSE
      BEGIN
      s1.length ← lastBlank;
      CenterString[x, y, s1, width];
      s1.length ← s.length; ss.offset ← 0; ss.length ← lastBlank+1;
      DeleteSubString[ss];
      CenterString[x, y-(RowHeight-4*Dot), s1, width];
      END;
    END;

  END.