#!/bin/sh
#
# Powered By Regina Ashley
# tcl.tk@outlook.com
#
# The next line is executed by /bin/sh, but not tcl \
exec wish "$0" ${1+"$@"}

package require Tk
package require Img

#############################################################################
proc main {} {
    
    wm title . "TkQR"
    
    menu .mb
    menu .mb.save
    menu .mb.about
    
    .mb.save  add command -label "Save QRcode to PNG" -command { saveFile }
    .mb.about add command -label "About" -command { showAbout }
    
    .mb add cascade -label "File" -menu .mb.save
    .mb add cascade -label "Help" -menu .mb.about
    . configure -menu .mb
    
    text .t -font {{} 12}
    label .img
    
    bind .t <KeyRelease> {
    
        set temp_png_filename "/tmp/tkqr.png"
        
        exec qrencode [.t get -displaychars 0.0 end] -s 6 -o $temp_png_filename
        .img configure -image [image create photo -file $temp_png_filename]
    }

    grid .t .img -padx 4 -pady 4
    grid .t   -sticky e
    grid .img -sticky ew
    
    focus .t
}

#############################################################################
proc saveFile {} {
    set save_filename [tk_getSaveFile -parent . -filetypes {{PNG {.png}}}]
    
    if {$save_filename != ""} {
        file copy -force "/tmp/tkqr.png" $save_filename
    }
}

#############################################################################
proc showAbout {} {
    tk_messageBox -title "About" -message "Powered By Regina Ashley\ntcl.tk@outlook.com"
}

#############################################################################
main
