' Multi-Level Menu — GOSUB, nested selections
#COMPILE EXE
#DIM ALL

SUB PrintHeader(title AS STRING)
    PRINT
    PRINT STRING$(50, 61)
    PRINT title
    PRINT STRING$(50, 61)
END SUB

SUB MainMenu()
    LOCAL choice AS LONG
    CALL PrintHeader("MAIN MENU")
    PRINT "1. File Operations"
    PRINT "2. Edit Operations"
    PRINT "3. View Operations"
    PRINT "4. Tools"
    PRINT "5. Exit"
    INPUT "Choice: ", choice
    PRINT
    SELECT CASE choice
        CASE 1
            CALL FileMenu()
        CASE 2
            CALL EditMenu()
        CASE 3
            CALL ViewMenu()
        CASE 4
            CALL ToolsMenu()
        CASE 5
            PRINT "Goodbye!"
        CASE ELSE
            PRINT "Invalid choice."
    END SELECT
END SUB

SUB FileMenu()
    LOCAL choice AS LONG
    CALL PrintHeader("FILE OPERATIONS")
    PRINT "1. Open File"
    PRINT "2. Save File"
    PRINT "3. Save As..."
    PRINT "4. Print"
    PRINT "5. Back to Main"
    INPUT "Choice: ", choice
    PRINT
    SELECT CASE choice
        CASE 1
            CALL OpenFile()
        CASE 2
            CALL SaveFile()
        CASE 3
            CALL SaveAsFile()
        CASE 4
            CALL PrintFile()
        CASE 5
        CASE ELSE
            PRINT "Invalid."
    END SELECT
END SUB

SUB EditMenu()
    LOCAL choice AS LONG
    CALL PrintHeader("EDIT OPERATIONS")
    PRINT "1. Cut"
    PRINT "2. Copy"
    PRINT "3. Paste"
    PRINT "4. Undo"
    PRINT "5. Redo"
    PRINT "6. Back to Main"
    INPUT "Choice: ", choice
    PRINT
    SELECT CASE choice
        CASE 1
            PRINT "Cut selected text."
        CASE 2
            PRINT "Copied to clipboard."
        CASE 3
            PRINT "Pasted from clipboard."
        CASE 4
            PRINT "Undone last action."
        CASE 5
            PRINT "Redone last undo."
        CASE 6
        CASE ELSE
            PRINT "Invalid."
    END SELECT
END SUB

SUB ViewMenu()
    LOCAL choice AS LONG
    CALL PrintHeader("VIEW OPERATIONS")
    PRINT "1. Zoom In"
    PRINT "2. Zoom Out"
    PRINT "3. Full Screen"
    PRINT "4. Show Toolbar"
    PRINT "5. Back to Main"
    INPUT "Choice: ", choice
    PRINT
    SELECT CASE choice
        CASE 1
            PRINT "Zoomed in."
        CASE 2
            PRINT "Zoomed out."
        CASE 3
            PRINT "Toggled full screen."
        CASE 4
            PRINT "Toggled toolbar."
        CASE 5
        CASE ELSE
            PRINT "Invalid."
    END SELECT
END SUB

SUB ToolsMenu()
    LOCAL choice AS LONG
    CALL PrintHeader("TOOLS")
    PRINT "1. Spell Check"
    PRINT "2. Word Count"
    PRINT "3. Find/Replace"
    PRINT "4. Preferences"
    PRINT "5. Back to Main"
    INPUT "Choice: ", choice
    PRINT
    SELECT CASE choice
        CASE 1
            CALL SpellCheck()
        CASE 2
            CALL WordCount()
        CASE 3
            CALL FindReplace()
        CASE 4
            CALL Preferences()
        CASE 5
        CASE ELSE
            PRINT "Invalid."
    END SELECT
END SUB

SUB OpenFile()
    LOCAL filename AS STRING
    INPUT "Filename to open: ", filename
    PRINT "Opening " & filename & "..."
    PRINT "File loaded successfully."
END SUB

SUB SaveFile()
    PRINT "Saving current file..."
    PRINT "File saved."
END SUB

SUB SaveAsFile()
    LOCAL filename AS STRING
    INPUT "Save as: ", filename
    PRINT "Saving as " & filename & "..."
    PRINT "File saved."
END SUB

SUB PrintFile()
    PRINT "Sending to printer..."
    PRINT "Print job completed."
END SUB

SUB SpellCheck()
    PRINT "Running spell checker..."
    PRINT "No errors found."
END SUB

SUB WordCount()
    PRINT "Counting words..."
    PRINT "Words: 0, Characters: 0, Lines: 0"
END SUB

SUB FindReplace()
    LOCAL find AS STRING
    LOCAL replace AS STRING
    INPUT "Find: ", find
    INPUT "Replace with: ", replace
    PRINT "Replaced all occurrences."
END SUB

SUB Preferences()
    LOCAL choice AS LONG
    CALL PrintHeader("PREFERENCES")
    PRINT "1. Editor"
    PRINT "2. Display"
    PRINT "3. Advanced"
    PRINT "4. Back"
    INPUT "Choice: ", choice
    IF choice = 1 THEN PRINT "Editor prefs."
    IF choice = 2 THEN PRINT "Display prefs."
    IF choice = 3 THEN PRINT "Advanced prefs."
END SUB

SUB MenuDrivenDemo()
    LOCAL running AS LONG
    running = 1
    DO WHILE running
        CALL MainMenu()
        PRINT
        PRINT "Returning to main menu..."
    LOOP
END SUB

FUNCTION PBMAIN() AS LONG
    PRINT "==========================="
    PRINT " Multi-Level Menu System"
    PRINT "==========================="
    CALL MainMenu()
    PRINT
    PRINT "Menu demo complete."
    FUNCTION = 0
END FUNCTION
