#!/usr/bin/wish
# GUI Frontend for j3c, j3w and j3dasm
#   Jun Mizutani  12/23/1997, 5/13/2003

. configure -width 400 -height 500

frame .f0
frame .f1
text .f1.t0 -wrap none \
            -xscrollcommand ".f1.sx set" \
            -yscrollcommand ".f1.sy set"
scrollbar .f1.sx -orient horizontal -command ".f1.t0 xview"
scrollbar .f1.sy -orient vertical   -command ".f1.t0 yview"

set pipe 0

set FullSpeed 0;
checkbutton .cbFullSpeed -text "Full Speed" -variable FullSpeed

button .b0 -text "Execute j3w" -command {
    global pipe
    set types {
        {{j3w object files} {.j3d}}
        {{All files}        *     }
    }
    set filename [tk_getOpenFile -filetypes $types]
    if {$FullSpeed == 1} {
        set fs "-f"
    } else {
        set fs ""
    }

    if {$filename != ""} {
        set pipe [open "|j3w $fs $filename"]
        fconfigure $pipe -buffering none
        fileevent $pipe readable showtext
    }
}

button .b1 -text "Compile" -command {
    set types {
        {{j3c source files} {.j3c}}
        {{All files}        *     }
    }
    set filename [tk_getOpenFile -filetypes $types]
    if {$filename != ""} {
        set pipe2 [open "|j3c $filename"]
        set t [read $pipe2]
        .f1.t0 insert end $t
    }
}

button .b2 -text "Assemble" -command {
    set types {
        {{j3dasm source files} {.j3s .j3m}}
        {{All files}        *     }
    }
    set filename [tk_getOpenFile -filetypes $types]
    if {$filename != ""} {
        set pipe2 [open "|j3dasm $filename"]
        set t [read $pipe2]
        .f1.t0 insert end $t
    }
}

button .b3 -text "Quit" -command "exit"

button .b4 -text "Compile & Run" -command {
    set types {
        {{j3c source files} {.j3c}}
        {{All files}        *     }
    }
    set filename [tk_getOpenFile -filetypes $types]
    if {$filename != ""} {
        set pipe2 [open "|j3cc -r $filename"]
        set t [read $pipe2]
        .f1.t0 insert end $t
    }
}

proc showtext { } {
    global pipe
    if [eof $pipe] {
        catch {close $pipe}
        return
    }
        set t [read $pipe 1]
        set s $t
        while { $s > 127 } {
            set s [read $pipe 1]
            append t $s
        }
        .f1.t0 insert end $t
        update idletasks
}

place .f0 -relx 0.0 -rely 0.0  -relwidth 1.0 -relheight 0.12
place .f1 -relx 0.0 -rely 0.12 -relwidth 1.0 -relheight 0.88
place .b0 -in .f0 -relx 0.0  -rely 0.0 -relwidth 0.30 -relheight 0.5
place .b1 -in .f0 -relx 0.30 -rely 0.0 -relwidth 0.25 -relheight 0.5
place .b2 -in .f0 -relx 0.55 -rely 0.0 -relwidth 0.25 -relheight 0.5
place .b3 -in .f0 -relx 0.80 -rely 0.0 -relwidth 0.20 -relheight 0.5
place .b4 -in .f0 -relx 0.0  -rely 0.5 -relwidth 0.55 -relheight 0.5
place .cbFullSpeed -in .f0 -relx 0.55 -rely 0.5 -relwidth 0.45 -relheight 0.5

pack .f1.sx -side bottom -fill x
pack .f1.sy -side right  -fill y
pack .f1.t0 -side left -fill both -expand true

while {1} {
    update
    after 100
}
