/*------------------------------------------------------------------------------
  test-11180-g-teardown-apply.p                                    (APPLY path)

  DRAFT probe for #11180 review findings 1 & 2 (window-teardown misread as a
  "steal").  PURPOSE: establish the native-OpenEdge baseline for the case the
  existing suite does NOT cover - a bare requestFocus focus fallback (window
  teardown) rather than a nested APPLY 'ENTRY' steal.  The correct FWD behaviour
  cannot be decided by reasoning; it must be read off a native-OE run of this
  probe (per the project rule).  Adjust the topology if it does not reach the
  ThinClient.processProgressEvent "stolen" branch on FWD.

  Background.  The #11180 fix classifies a "steal" as: old focus's frame deleted
  mid-LEAVE AND current focus moved off the old focus.  The pre-fix .~2~ revision
  additionally required  redirected == pendingFocus  - i.e. the redirect came
  from a nested APPLY 'ENTRY' (which records its target in pendingFocus), NOT a
  bare requestFocus.  That clause was dropped.  A window teardown moves focus via
  GuiWindowCommons.hide -> WindowManager.setFocusWindow -> requestFocus (a bare
  refocus that sets no pendingFocus), so FWD 11180b now misclassifies it as a
  steal and DROPS the outer APPLY 'ENTRY'.

  Topology (window teardown, NO nested APPLY):
    - secondary WINDOW W2 holds fill-in fO; focus is parked in fO.
    - the default window holds fill-in fT (the outer APPLY target) in frame frmT.
    - sequence: APPLY 'ENTRY' TO fO (park in W2), then APPLY 'ENTRY' TO fT.
    - fO's frame LEAVE DELETEs window W2 and does NO APPLY.  Deleting the window
      makes FWD reassign focus via a bare requestFocus to a surviving-window
      widget (NOT via APPLY 'ENTRY').

  OPEN QUESTION (unmeasured by any existing probe): does OpenEdge treat the
  bare-refocus fallback as a steal (keep the fallback focus, DROP the outer
  APPLY 'ENTRY' TO fT) or let the outer APPLY target win (enter fT)?

  Expected (native OpenEdge): <to be recorded from the OE run>
  FWD 11180b (post-review):   fT ENTRY dropped; focus kept on the teardown
                              fallback widget (candidate bug per findings 1/2).

  FWD drives the APPLY path synchronously, so this runs head-less: log to
  test-11180-g-apply.log, then QUIT.
------------------------------------------------------------------------------*/

DEFINE VARIABLE fO  AS CHARACTER NO-UNDO FORMAT "x(8)" INITIAL "ooo".
DEFINE VARIABLE fT  AS CHARACTER NO-UNDO FORMAT "x(8)" INITIAL "ttt".
DEFINE VARIABLE seq AS INTEGER   NO-UNDO.

DEFINE VARIABLE hWin2 AS HANDLE NO-UNDO.
DEFINE VARIABLE hFrmO AS HANDLE NO-UNDO.
DEFINE VARIABLE hFO   AS HANDLE NO-UNDO.

DEFINE FRAME frmT fT WITH TITLE "FRAME-T (APPLY target, default window)"
   AT ROW 3 COLUMN 2 SIZE 44 BY 3.

PROCEDURE logit:
   DEFINE INPUT PARAMETER m AS CHARACTER NO-UNDO.
   seq = seq + 1.
   OUTPUT TO "test-11180-g-apply.log" APPEND.
   PUT UNFORMATTED STRING(seq, "99") " " m SKIP.
   OUTPUT CLOSE.
END PROCEDURE.

/* fO's frame LEAVE: tear down the WHOLE secondary window (bare-refocus fallback),
   with NO APPLY 'ENTRY' - this is the window-teardown case, not a nested steal. */
PROCEDURE oLeave:
   RUN logit("frmO  LEAVE  (begin - delete secondary WINDOW W2, NO APPLY)").
   IF VALID-HANDLE(hWin2) THEN DELETE WIDGET hWin2.
   RUN logit("frmO  LEAVE  (end - no APPLY performed)").
END PROCEDURE.

/* target frame T triggers (default window) */
ON ENTRY OF FRAME frmT
   RUN logit("frmT  ENTRY  <== target FRAME ENTRY").
ON ENTRY OF fT IN FRAME frmT
   RUN logit("fT    ENTRY  <== outer APPLY target ENTRY (dropped by candidate bug?)").
ON LEAVE OF fT IN FRAME frmT
   RUN logit("fT    LEAVE").

/* ---- main ---- */
OUTPUT TO "test-11180-g-apply.log".
PUT UNFORMATTED "=== test-11180-g-teardown-apply start ===" SKIP.
OUTPUT CLOSE.

ENABLE fT WITH FRAME frmT.
VIEW FRAME frmT.

/* secondary WINDOW W2 with its own frame + fill-in fO */
CREATE WINDOW hWin2
   ASSIGN TITLE = "WINDOW-2 (torn down in its own LEAVE)"
          WIDTH = 40 HEIGHT = 6 VISIBLE = FALSE.
CREATE FRAME hFrmO
   ASSIGN TITLE = "FRAME-O (source, in window 2)"
          PARENT = hWin2
          ROW = 2 COLUMN = 2 WIDTH = 34 HEIGHT = 3
          BOX = TRUE VISIBLE = FALSE.
CREATE FILL-IN hFO
   ASSIGN FRAME = hFrmO
          NAME = "fO" FORMAT = "x(8)" SCREEN-VALUE = "ooo"
          X = 4 Y = 4 WIDTH = 20
          SENSITIVE = TRUE VISIBLE = TRUE.
hFrmO:VISIBLE = TRUE.
hWin2:VISIBLE  = TRUE.

/* LEAVE on the source frame tears down window W2 (no APPLY) */
ON "LEAVE":U OF hFrmO PERSISTENT RUN oLeave.

RUN logit("--- APPLY ENTRY to fO (park focus in secondary window W2) ---").
APPLY "ENTRY" TO hFO.

IF VALID-HANDLE(FOCUS) THEN
   RUN logit("--- FOCUS before move: TYPE=" + STRING(FOCUS:TYPE) + " ---").
ELSE
   RUN logit("--- FOCUS before move: <none> ---").

RUN logit("--- APPLY ENTRY to fT (outer target; fO's LEAVE deletes window W2) ---").
APPLY "ENTRY" TO fT IN FRAME frmT.
PROCESS EVENTS.
RUN logit("--- done; FOCUS is now: "
          + (IF VALID-HANDLE(FOCUS) THEN STRING(FOCUS:TYPE) + "/" + FOCUS:NAME
             ELSE "<none/invalid>") + " ---").
RUN logit("--- OPEN QUESTION: OE = does fT win (ENTRY fires) or is it a steal? ---").
RUN logit("--- FWD 11180b:    fT ENTRY expected DROPPED (candidate findings 1/2) ---").
QUIT.
