#COMPILE EXE
#DIM ALL

FUNCTION PBMAIN() AS LONG
    LOCAL pane1_count AS LONG
    LOCAL pane2_count AS LONG
    LOCAL pane1_scroll AS LONG
    LOCAL pane2_scroll AS LONG
    LOCAL pane_height AS LONG
    LOCAL split_pos AS LONG
    LOCAL total_width AS LONG
    LOCAL i AS LONG
    LOCAL j AS LONG
    LOCAL k AS LONG
    LOCAL line_out AS STRING
    LOCAL p1_line AS STRING
    LOCAL p2_line AS STRING
    LOCAL p1_idx AS LONG
    LOCAL p2_idx AS LONG
    LOCAL p1_end AS LONG
    LOCAL p2_end AS LONG
    LOCAL trim_len AS LONG
    LOCAL spaces AS STRING
    LOCAL temp_str AS STRING
    LOCAL pad_len AS LONG

    total_width = 78
    split_pos = 40
    pane_height = 15
    pane1_count = 25
    pane2_count = 22
    pane1_scroll = 0
    pane2_scroll = 0

    i = 0
    WHILE i < 3
        temp_str = STR$(i + 1)
        PRINT "=== Split Pane Console (Page " + temp_str + "/3) ==="
        PRINT "Pane 1 (code)                               | Pane 2 (output)"
        PRINT "=============================================================================="

        p1_end = pane1_scroll + pane_height - 1
        IF p1_end >= pane1_count THEN
            p1_end = pane1_count - 1
        END IF
        p2_end = pane2_scroll + pane_height - 1
        IF p2_end >= pane2_count THEN
            p2_end = pane2_count - 1
        END IF

        j = 0
        WHILE j < pane_height
            p1_idx = pane1_scroll + j
            p2_idx = pane2_scroll + j

            p1_line = ""
            IF p1_idx < pane1_count THEN
                IF p1_idx = 0 THEN p1_line = "' Pane 1: Source Code"
                IF p1_idx = 1 THEN p1_line = "#COMPILE EXE"
                IF p1_idx = 2 THEN p1_line = "#DIM ALL"
                IF p1_idx = 3 THEN p1_line = ""
                IF p1_idx = 4 THEN p1_line = "FUNCTION PBMAIN() AS LONG"
                IF p1_idx = 5 THEN p1_line = "    LOCAL x AS LONG"
                IF p1_idx = 6 THEN p1_line = "    LOCAL y AS LONG"
                IF p1_idx = 7 THEN p1_line = "    LOCAL result AS LONG"
                IF p1_idx = 8 THEN p1_line = ""
                IF p1_idx = 9 THEN p1_line = "    x = 42"
                IF p1_idx = 10 THEN p1_line = "    y = 7"
                IF p1_idx = 11 THEN p1_line = "    result = x * y"
                IF p1_idx = 12 THEN p1_line = ""
                IF p1_idx = 13 THEN p1_line = "    PRINT STR$(result)"
                IF p1_idx = 14 THEN p1_line = ""
                IF p1_idx = 15 THEN p1_line = "    FUNCTION = 0"
                IF p1_idx = 16 THEN p1_line = "END FUNCTION"
                IF p1_idx = 17 THEN p1_line = ""
                IF p1_idx = 18 THEN p1_line = "' End of section"
                IF p1_idx = 19 THEN p1_line = "' More code follows"
                IF p1_idx = 20 THEN p1_line = "FUNCTION Test() AS LONG"
                IF p1_idx = 21 THEN p1_line = "    LOCAL a AS LONG"
                IF p1_idx = 22 THEN p1_line = "    a = 100"
                IF p1_idx = 23 THEN p1_line = "    FUNCTION = a"
                IF p1_idx = 24 THEN p1_line = "END FUNCTION"
            END IF

            p2_line = ""
            IF p2_idx < pane2_count THEN
                IF p2_idx = 0 THEN p2_line = "Pane 2: Console Output"
                IF p2_idx = 1 THEN p2_line = "======================"
                IF p2_idx = 2 THEN p2_line = ""
                IF p2_idx = 3 THEN p2_line = "Starting compilation"
                IF p2_idx = 4 THEN p2_line = "Parsing source file"
                IF p2_idx = 5 THEN p2_line = "Tokenizing... OK"
                IF p2_idx = 6 THEN p2_line = "Building AST..."
                IF p2_idx = 7 THEN p2_line = "Semantic analysis"
                IF p2_idx = 8 THEN p2_line = "  Checking declarations"
                IF p2_idx = 9 THEN p2_line = "  Checking types"
                IF p2_idx = 10 THEN p2_line = "  Type check: OK"
                IF p2_idx = 11 THEN p2_line = "IR generation..."
                IF p2_idx = 12 THEN p2_line = "Optimization pass 1"
                IF p2_idx = 13 THEN p2_line = "  Constant folding"
                IF p2_idx = 14 THEN p2_line = "  Dead code elim"
                IF p2_idx = 15 THEN p2_line = "Register allocation"
                IF p2_idx = 16 THEN p2_line = "  Allocating regs"
                IF p2_idx = 17 THEN p2_line = "Code generation..."
                IF p2_idx = 18 THEN p2_line = "Assembly output done"
                IF p2_idx = 19 THEN p2_line = ""
                IF p2_idx = 20 THEN p2_line = "Compilation OK"
                IF p2_idx = 21 THEN p2_line = "Output: sample.obj"
            END IF

            IF LEN(p1_line) > split_pos - 1 THEN
                p1_line = LEFT$(p1_line, split_pos - 1)
            END IF

            trim_len = split_pos - LEN(p1_line)
            spaces = ""
            k = 0
            WHILE k < trim_len
                spaces = spaces + " "
                k = k + 1
            WEND
            p1_line = p1_line + spaces

            line_out = p1_line + "|" + p2_line

            IF j = 2 THEN
                temp_str = STR$(pane1_scroll + 1)
                line_out = line_out + "   [Pane1: line " + temp_str + "-" + STR$(p1_end + 1) + "]"
            END IF
            IF j = 3 THEN
                temp_str = STR$(pane2_scroll + 1)
                line_out = line_out + "   [Pane2: line " + temp_str + "-" + STR$(p2_end + 1) + "]"
            END IF

            PRINT line_out
            j = j + 1
        WEND

        PRINT "=============================================================================="
        PRINT "Tab=focus PgUp/PgDn=scroll Q=quit"
        PRINT ""

        IF i = 0 THEN
            pane1_scroll = 5
            pane2_scroll = 8
        END IF
        IF i = 1 THEN
            pane1_scroll = 12
            pane2_scroll = 16
        END IF

        i = i + 1
    WEND

    PRINT "Resize demo: split at 50"
    PRINT "========================="
    split_pos = 50

    j = 0
    WHILE j < 5
        p1_line = ""
        IF j = 0 THEN p1_line = "' Pane 1: Source Code"
        IF j = 1 THEN p1_line = "#COMPILE EXE"
        IF j = 2 THEN p1_line = "#DIM ALL"
        IF j = 3 THEN p1_line = ""
        IF j = 4 THEN p1_line = "FUNCTION PBMAIN() AS LONG"

        p2_line = ""
        IF j = 0 THEN p2_line = "Pane 2: Console Output"
        IF j = 1 THEN p2_line = "======================"
        IF j = 2 THEN p2_line = ""
        IF j = 3 THEN p2_line = "Starting compilation"
        IF j = 4 THEN p2_line = "Parsing source file"

        IF LEN(p1_line) > split_pos - 1 THEN
            p1_line = LEFT$(p1_line, split_pos - 1)
        END IF

        trim_len = split_pos - LEN(p1_line)
        spaces = ""
        k = 0
        WHILE k < trim_len
            spaces = spaces + " "
            k = k + 1
        WEND
        p1_line = p1_line + spaces

        PRINT p1_line + "|" + p2_line
        j = j + 1
    WEND

    PRINT ""
    PRINT "Split pane demo complete."

    FUNCTION = 0
END FUNCTION
