/*------------------------------------------------------------------------------
  spreadsheet_demo_issues.p  the spreadsheet issues met in the topic 15 demo

  One sheet, one case per issue of spreadsheet_demo_issues.textile.
  Every case writes its values to spreadsheet_demo_issues.out in the working
  directory (deploy/client in FWD), so the FWD and OpenEdge runs can be
  compared line by line.  Run it on FWD and on OpenEdge (OE-F1) the same way.

  Automatic, at start:
    1  style range:  A1 1234 "#,##0", B1 0.25 "0.0%", then SetFont on A1:B1;
                     reports FormattedTextRC and NumberFormat of A1 and B1
    2  alignment:    A2 "right" with SetAlignment(4, ...); visual only
    5  scrolling:    SetActiveCell(4, 2), then TopRow = 1, LeftCol = 1;
                     reports TopRow and LeftCol after each step
    6b read after write, no event: B9 = 20 (EntryRC), then NumberRC of C9
                     (=SUM(B9)) right away

  Interactive (keys; the program starts with B4 active):
    3  type abc in B4, Enter: EndEdit sets Cancel = 1.  Press Escape if the
       editor stays open.
    4  Down to B5, type abc, Enter: EndEdit sets the edit string to 999.
    6  Down, Down to B8: SelChange writes B7 = 20 (EntryRC) and reads C7
       (=SUM(B7)) right away, again after Recalc.
    then press Report: the final state of B4, C4, B5, C5, B7, C7, A1, B1,
    TopRow and LeftCol.

  Every EndEdit and SelChange is logged with the active cell and, for
  EndEdit, the edit string and the values of the edited cell and its sum.

  Preprocessor names the OpenEdge wrapper defines first (&GLOBAL-DEFINE):
    OE-F1     run on OpenEdge (32-bit prowin32) with the real Formula One OCX,
               loaded from wrx/wssheet.wrx (metacom)
------------------------------------------------------------------------------*/

&SCOPED-DEFINE F1 chCtrlFrame:F1Book

DEFINE VARIABLE wWin        AS WIDGET-HANDLE NO-UNDO.
DEFINE VARIABLE CtrlFrame   AS WIDGET-HANDLE NO-UNDO.
DEFINE VARIABLE chCtrlFrame AS COM-HANDLE    NO-UNDO.
DEFINE VARIABLE lEventDone  AS LOGICAL       NO-UNDO.

DEFINE VARIABLE cLog AS CHARACTER NO-UNDO
   VIEW-AS EDITOR SCROLLBAR-VERTICAL SIZE 150 BY 10.

DEFINE STREAM sOut.

DEFINE BUTTON btnReport LABEL "Report".
DEFINE BUTTON btnExit   LABEL "Exit".

DEFINE FRAME fMain
   btnReport btnExit SKIP(0.5)
   cLog NO-LABEL
   WITH SIDE-LABELS NO-BOX THREE-D AT COLUMN 1 ROW 1 SIZE 158 BY 32.

ON CHOOSE OF btnReport IN FRAME fMain
DO:
   RUN report ("final").
END.

ON CHOOSE OF btnExit IN FRAME fMain
DO:
   APPLY "CLOSE" TO THIS-PROCEDURE.
END.

RUN create-ui.

OUTPUT STREAM sOut TO VALUE("spreadsheet_demo_issues.out").
&IF DEFINED(OE-F1) = 0 &THEN
RUN log-line ("# spreadsheet_demo_issues FWD").
&ELSE
RUN log-line ("# spreadsheet_demo_issues OE-F1").
&ENDIF

RUN build-sheet.
RUN case-1.
RUN case-2.
RUN case-6b.
RUN case-5.
RUN log-line ("# now: abc + Enter in B4 (Escape if the editor stays open), Down, abc + Enter in B5, "
              + "Down, Down to B8, then Report").

WAIT-FOR CLOSE OF THIS-PROCEDURE.

OUTPUT STREAM sOut CLOSE.
RUN destroy-ui.

/*------------------------------------------------------------------------------
  Creates the test window and the spreadsheet.
------------------------------------------------------------------------------*/
PROCEDURE create-ui:
   CREATE WINDOW wWin ASSIGN
      TITLE     = "spreadsheet_demo_issues"
      HEIGHT    = 33
      WIDTH     = 160
      THREE-D   = yes
      SENSITIVE = yes
      HIDDEN    = yes.
   CURRENT-WINDOW = wWin.

   CREATE CONTROL-FRAME CtrlFrame ASSIGN
      FRAME     = FRAME fMain:HANDLE
      ROW       = 13
      COLUMN    = 1
      HEIGHT    = 19
      WIDTH     = 158
      TAB-STOP  = yes
      HIDDEN    = yes
      SENSITIVE = yes.

&IF DEFINED(OE-F1) = 0 &THEN
   /* FWD ignores the .wrx, so the OCX name of the event procedures (sheet.F1Book.EndEdit) comes from
      the NAME */
   CtrlFrame:NAME = "sheet.F1Book".
&ELSE
   CtrlFrame:NAME = "sheet".
&ENDIF
   chCtrlFrame = CtrlFrame:COM-HANDLE.
&IF DEFINED(OE-F1) = 0 &THEN
   chCtrlFrame:F1Book:LoadControls("", "spreadsheet").
&ELSE
   chCtrlFrame:LoadControls(SEARCH("wrx/wssheet.wrx"), "ssheet").
&ENDIF

   VIEW FRAME fMain IN WINDOW wWin.
   ENABLE btnReport btnExit cLog WITH FRAME fMain.
   VIEW wWin.
   CtrlFrame:HIDDEN = no.
END PROCEDURE.

/*------------------------------------------------------------------------------
  Deletes the test window.
------------------------------------------------------------------------------*/
PROCEDURE destroy-ui:
   IF VALID-HANDLE(CtrlFrame) THEN
      DELETE WIDGET CtrlFrame.
   IF VALID-HANDLE(wWin) THEN
      DELETE WIDGET wWin.
END PROCEDURE.

/*------------------------------------------------------------------------------
  Appends a line to the editor and to the result file.
------------------------------------------------------------------------------*/
PROCEDURE log-line:
   DEFINE INPUT PARAMETER pcLine AS CHARACTER NO-UNDO.

   cLog = cLog + pcLine + CHR(10).
   DISPLAY cLog WITH FRAME fMain.
   PUT STREAM sOut UNFORMATTED pcLine SKIP.
END PROCEDURE.

/*------------------------------------------------------------------------------
  One cell: TypeRC | [TextRC] | [FormattedTextRC] | NumberRC | [NumberFormat]
------------------------------------------------------------------------------*/
FUNCTION cell-state RETURNS CHARACTER (INPUT piRow AS INTEGER, INPUT piCol AS INTEGER):
   DEFINE VARIABLE iType AS INTEGER   NO-UNDO.
   DEFINE VARIABLE cText AS CHARACTER NO-UNDO.
   DEFINE VARIABLE cFmtd AS CHARACTER NO-UNDO.
   DEFINE VARIABLE dNum  AS DECIMAL   NO-UNDO.
   DEFINE VARIABLE cFmt  AS CHARACTER NO-UNDO.

   iType = {&F1}:TypeRC(piRow, piCol) NO-ERROR.
   cText = {&F1}:TextRC(piRow, piCol) NO-ERROR.
   cFmtd = {&F1}:FormattedTextRC(piRow, piCol) NO-ERROR.
   dNum  = {&F1}:NumberRC(piRow, piCol) NO-ERROR.
   {&F1}:SetSelection(piRow, piCol, piRow, piCol) NO-ERROR.
   cFmt  = {&F1}:NumberFormat NO-ERROR.

   RETURN CHR(64 + piCol) + STRING(piRow)
          + " | " + STRING(iType)
          + " | [" + cText + "]"
          + " | [" + cFmtd + "]"
          + " | " + STRING(dNum)
          + " | [" + cFmt + "]".
END FUNCTION.

/*------------------------------------------------------------------------------
  One cell without touching the selection, safe inside EndEdit (SetSelection
  there crashes prowin32 with Formula One): TypeRC | [TextRC] | NumberRC
------------------------------------------------------------------------------*/
FUNCTION cell-value RETURNS CHARACTER (INPUT piRow AS INTEGER, INPUT piCol AS INTEGER):
   DEFINE VARIABLE iType AS INTEGER   NO-UNDO.
   DEFINE VARIABLE cText AS CHARACTER NO-UNDO.
   DEFINE VARIABLE dNum  AS DECIMAL   NO-UNDO.

   iType = {&F1}:TypeRC(piRow, piCol) NO-ERROR.
   cText = {&F1}:TextRC(piRow, piCol) NO-ERROR.
   dNum  = {&F1}:NumberRC(piRow, piCol) NO-ERROR.

   RETURN CHR(64 + piCol) + STRING(piRow)
          + " | " + STRING(iType)
          + " | [" + cText + "]"
          + " | " + STRING(dNum).
END FUNCTION.

/*------------------------------------------------------------------------------
  The sheet for all cases.
------------------------------------------------------------------------------*/
PROCEDURE build-sheet:
   DEFINE VARIABLE iRow AS INTEGER NO-UNDO.

   /* cases 3 and 4: a number referenced by a formula */
   {&F1}:TextRC(3, 1)    = "EndEdit".
   {&F1}:TextRC(4, 1)    = "3 cancel".
   {&F1}:TextRC(5, 1)    = "4 edit string".
   DO iRow = 4 TO 5:
      {&F1}:NumberRC(iRow, 2)  = 33500.
      {&F1}:FormulaRC(iRow, 3) = "SUM(B" + STRING(iRow) + ")".
      {&F1}:SetSelection(iRow, 2, iRow, 3).
      {&F1}:NumberFormat = "#,##0".
   END.

   /* case 6: event; case 6b: no event */
   {&F1}:TextRC(7, 1)    = "6 SelChange".
   {&F1}:NumberRC(7, 2)  = 10.
   {&F1}:FormulaRC(7, 3) = "SUM(B7)".
   {&F1}:TextRC(8, 1)    = "go here".
   {&F1}:TextRC(9, 1)    = "6b no event".
   {&F1}:NumberRC(9, 2)  = 10.
   {&F1}:FormulaRC(9, 3) = "SUM(B9)".

   /* rows below, so that the sheet can scroll */
   DO iRow = 11 TO 60:
      {&F1}:NumberRC(iRow, 2) = iRow.
   END.
END PROCEDURE.

/*------------------------------------------------------------------------------
  1: SetFont on a range whose cells have different number formats.
------------------------------------------------------------------------------*/
PROCEDURE case-1:
   {&F1}:NumberRC(1, 1) = 1234.
   {&F1}:NumberRC(1, 2) = 0.25.
   {&F1}:SetSelection(1, 1, 1, 1).
   {&F1}:NumberFormat = "#,##0".
   {&F1}:SetSelection(1, 2, 1, 2).
   {&F1}:NumberFormat = "0.0%".
   RUN log-line ("1 before | " + cell-state(1, 1) + " || " + cell-state(1, 2)).

   {&F1}:SetSelection(1, 1, 1, 2).
   {&F1}:SetFont("Arial", 10, TRUE, FALSE, FALSE, FALSE, RGB-VALUE(0, 0, 255), FALSE, FALSE).
   RUN log-line ("1 after SetFont A1:B1 | " + cell-state(1, 1) + " || " + cell-state(1, 2)).
   RUN log-line ("1 expected | B1 keeps [25.0%] and [0.0%]; A1 and B1 bold blue (screenshot)").
END PROCEDURE.

/*------------------------------------------------------------------------------
  2: SetAlignment right; visual only (screenshot).
------------------------------------------------------------------------------*/
PROCEDURE case-2:
   {&F1}:TextRC(2, 1) = "right".
   {&F1}:SetSelection(2, 1, 2, 1).
   {&F1}:SetAlignment(4, FALSE, 2, 0) NO-ERROR.
   RUN log-line ("2 SetAlignment(4) on A2 | " + (IF ERROR-STATUS:ERROR THEN "ERR" ELSE "done")
                 + " | expected: right aligned (screenshot)").
END PROCEDURE.

/*------------------------------------------------------------------------------
  5: scrolling: SetActiveCell, then TopRow and LeftCol.  Leaves B4 active for
  the interactive cases.
------------------------------------------------------------------------------*/
PROCEDURE case-5:
   DEFINE VARIABLE iTop  AS INTEGER NO-UNDO.
   DEFINE VARIABLE iLeft AS INTEGER NO-UNDO.

   {&F1}:SetActiveCell(4, 2).
   iTop  = {&F1}:TopRow NO-ERROR.
   iLeft = {&F1}:LeftCol NO-ERROR.
   RUN log-line ("5 after SetActiveCell(4, 2) | TopRow " + STRING(iTop) + " | LeftCol " + STRING(iLeft)).

   {&F1}:TopRow  = 1.
   {&F1}:LeftCol = 1.
   iTop  = {&F1}:TopRow NO-ERROR.
   iLeft = {&F1}:LeftCol NO-ERROR.
   RUN log-line ("5 after TopRow = 1, LeftCol = 1 | TopRow " + STRING(iTop) + " | LeftCol " + STRING(iLeft)
                 + " | expected 1, 1; A1 visible at the top left (screenshot)").
END PROCEDURE.

/*------------------------------------------------------------------------------
  6b: a read right after a write, outside any sheet event.
------------------------------------------------------------------------------*/
PROCEDURE case-6b:
   DEFINE VARIABLE dSum AS DECIMAL NO-UNDO.

   {&F1}:EntryRC(9, 2) = "20".
   dSum = {&F1}:NumberRC(9, 3).
   RUN log-line ("6b no event: B9 = 20, then C9 | " + STRING(dSum) + " | expected 20").
END PROCEDURE.

/*------------------------------------------------------------------------------
  The final state, on Report.
------------------------------------------------------------------------------*/
PROCEDURE report:
   DEFINE INPUT PARAMETER pcWhen AS CHARACTER NO-UNDO.

   DEFINE VARIABLE iTop  AS INTEGER NO-UNDO.
   DEFINE VARIABLE iLeft AS INTEGER NO-UNDO.

   RUN log-line ("# " + pcWhen + ": cell | TypeRC | [TextRC] | [FormattedTextRC] | NumberRC | [NumberFormat]").
   RUN log-line ("1 " + cell-state(1, 1)).
   RUN log-line ("1 " + cell-state(1, 2)).
   RUN log-line ("3 " + cell-state(4, 2)).
   RUN log-line ("3 " + cell-state(4, 3)).
   RUN log-line ("4 " + cell-state(5, 2)).
   RUN log-line ("4 " + cell-state(5, 3)).
   RUN log-line ("6 " + cell-state(7, 2)).
   RUN log-line ("6 " + cell-state(7, 3)).
   RUN log-line ("6b " + cell-state(9, 2)).
   RUN log-line ("6b " + cell-state(9, 3)).
   iTop  = {&F1}:TopRow NO-ERROR.
   iLeft = {&F1}:LeftCol NO-ERROR.
   RUN log-line ("5 TopRow " + STRING(iTop) + " | LeftCol " + STRING(iLeft)).
END PROCEDURE.

/*------------------------------------------------------------------------------
  3 and 4: B4 cancels the edit, B5 replaces the edit string with 999.  Logs the
  edited cell and its sum as the event sees them.
------------------------------------------------------------------------------*/
PROCEDURE sheet.F1Book.EndEdit:
   DEFINE INPUT-OUTPUT PARAMETER p-EditString AS CHARACTER NO-UNDO.
   DEFINE INPUT-OUTPUT PARAMETER p-Cancel     AS INTEGER   NO-UNDO.

   DEFINE VARIABLE iRow AS INTEGER NO-UNDO.
   DEFINE VARIABLE iCol AS INTEGER NO-UNDO.

   {&F1}:GetActiveCell(OUTPUT iRow, OUTPUT iCol) NO-ERROR.
   RUN log-line ("EndEdit " + CHR(64 + iCol) + STRING(iRow) + " | [" + p-EditString + "] | in event: "
                 + cell-value(iRow, iCol) + " || " + cell-value(iRow, 3)).

   IF iRow = 4 AND iCol = 2 THEN
   DO:
      p-Cancel = 1.
      RUN log-line ("3 EndEdit B4: Cancel = 1").
   END.
   ELSE IF iRow = 5 AND iCol = 2 THEN
   DO:
      p-EditString = "999".
      RUN log-line ("4 EndEdit B5: edit string set to [999]").
   END.
END PROCEDURE.

/*------------------------------------------------------------------------------
  6: logs every selection change; the first time B8 becomes active, writes B7
  and reads its sum in the same handler.
------------------------------------------------------------------------------*/
PROCEDURE sheet.F1Book.SelChange:
   DEFINE VARIABLE iRow AS INTEGER NO-UNDO.
   DEFINE VARIABLE iCol AS INTEGER NO-UNDO.
   DEFINE VARIABLE dSum AS DECIMAL NO-UNDO.

   {&F1}:GetActiveCell(OUTPUT iRow, OUTPUT iCol) NO-ERROR.
   RUN log-line ("SelChange " + CHR(64 + iCol) + STRING(iRow)).

   IF iRow = 8 AND iCol = 2 AND NOT lEventDone THEN
   DO:
      lEventDone = TRUE.
      {&F1}:EntryRC(7, 2) = "20".
      dSum = {&F1}:NumberRC(7, 3).
      RUN log-line ("6 SelChange: B7 = 20, then C7 | " + STRING(dSum) + " | expected 20").
      {&F1}:Recalc() NO-ERROR.
      dSum = {&F1}:NumberRC(7, 3).
      RUN log-line ("6 after Recalc, C7 | " + STRING(dSum) + " | expected 20").
   END.
END PROCEDURE.
