; EXITWIN.WBT  --  Author: Richard Merit

; Places an "Exit Windows" button in the lower right corner of the screen,
; and makes this button the only possible way of exiting Windows.

; --------------------------------------------------------------------------

; If we're being called with a command-line parameter, we're the second
; instance of ourself (recursion).  Go down to the label marked "activate".
If param0 != 0 Then Goto activate

; Otherwise, we're just starting out.  So hide the WinBatch icon.
WinHide("")

; Refuse to allow ourself to be terminated, and refuse to allow the user to
; exit Windows in any manner other than pressing our "Exit Windows" button.
IntControl(12, 10, "Use Exit Windows Button to Exit", 0, 0)

; Define the dialog format.  It's just a single pushbutton in a tiny window.
; This dialog box is designed to appear in the lower right-hand corner of
; the screen if you are using the standard Windows VGA driver, and will also
; be correct for some OEM drivers using large (8514) fonts.  If the dialog
; box does not appear in the correct location on your screen, you should
; adjust the "ExitX" and "ExitY" coordinates below accordingly (of course,
; you can change them to make the dialog appear in any location you desire).
; If you make these numbers too high, so the dialog box would be completely
; off-screen, WinBatch may override them and display the dialog box in the
; center of the screen.  If this happens, make the numbers a little smaller.
ExitFormat = `WWWDLGED,4.0`
ExitCaption = `Exit Windows`
ExitX = 274
ExitY = 225
ExitWidth = 45
ExitHeight = 14
ExitNumControls = 1
Exit01 = `1, 1, 43, DEFAULT, PUSHBUTTON, DEFAULT, "Now!", 1`

; Parse the name of the currently-executing WBT file from our window title
; (eg, "WBT - EXITWIN.WBT") as follows: search backwards for the first space
; from the end of the window title, start with the next character, and
; extract everything down to the end of the title string.
wbtwin = WinName()
space = StrIndex(wbtwin, " ", 0, @BACKSCAN)
wbt = StrSub(wbtwin, space + 1, StrLen(wbtwin) - space)

; Get the name of the currently-active window, so that we can restore the
; focus to it later on.  "Double up" any quotes, apostrophes, or back quotes
; embedded in the window title, so that they won't be treated as delimiters.
activewin = WinGetActive()
activewin = StrReplace(activewin, "'", "''")
activewin = StrReplace(activewin, "`", "``")
activewin = StrReplace(activewin, `"`, `""`)

; Run ourself, passing the active window title as a command-line parameter.
; By placing an extra pair of delimiters around the window title, we force
; the second copy of WinBatch to treat it as a single parameter, even if it
; contains spaces (normally, it would get parsed as param1, param2, etc.).
:launch
wbtx=FileNameShort(wbt)
If strupper(FileExtension(wbt)) == "EXE" RunHide(wbtx, "`%activewin%`")
Else RunHide(WinExeName(""), "%wbtx% `%activewin%`")
endif

; Display the dialog.  Wait for the user to press the (only) pushbutton.
Dialog("Exit")

; One last chance for the user to change his or her mind after clicking on
; the Exit button.  Comment this line out if you don't want this safety net.
If AskYesNo("Exit Windows", "Are you sure?") == @NO Then Goto launch

; Do it.
EndSession()

; If we got here, it means that the Windows session didn't end.  Either
; there was a DOS window open, or there was a Windows application with
; unsaved data and the user selected "Cancel" at the "Save file?" prompt.
:cancel
Goto launch

:activate

; Give the first instance of WinBatch a chance to display the dialog.
Yield

; Now take the focus away from our dialog box by activating whichever window
; had previously been active (assuming it's still there).
If WinExist(param1) Then WinActivate(param1)
