#!/bin/sh
#-*- tcl -*- \
exec /usr/bin/tkdesksh "$0" "$@"

# drag and drop file handler for Afterstep Wharf and TkDesk

# requires 3 arguments: bg color, pixmap for wharf
# and a command to execute on drop.  any "%s" in the command will
# be converted to the dropped file name.

# sample afterstep system.steprc wharf configuration:
# *Wharf Vi nil Swallow "dropV" ../bin/dropV "#8e8a9e" ../pixmaps/text.xpm "xterm -T \"Vi %s\" -e vi %s" &
# *Wharf Vi nil Exec ""  xterm -title Vi -e vi &
# note that multiple uses of this script in the wharf require multiple names for it
# because the Swallow syntax requires the 5th and 6th args refer to the same name.
# Therefore ../bin/dropV above is a symlink to this script. 

# sources for this program were /usr/bin/tkdesk and comments in 
# /usr/lib/tkdesk/dd-file.tcl.  also see "man 3blt dragdrop"

if { $argc > 0 && $argc != 3 } {
        puts "Requires 3 arguments: bg color, pixmap and command to execute on drop"
        exit 1
}

tk_focusFollowsMouse
wm title . Drop
wm geometry . 48x48-1-1

if { $argc > 0 } {
        set bgcolor [lindex $argv 0]
        set argv [lreplace $argv 0 0]
        set pixmap [lindex $argv 0]
        set argv [lreplace $argv 0 0]
        canvas .c -width 48 -height 48 -bg $bgcolor -highlightthickness 0
        image create pixmap im -file $pixmap
        .c create image 0 0 -anchor nw -image im
        pack .c
} else {
        set color gray96
        label .win -bd 1 -padx 3 -pady 3 -relief groove -bg $color -text "           "
        pack .win
}

# "Uglify" ;-) namespaced BLT:

if {[info command bgexec] != ""} {
    rename bgexec blt_bgexec
}

if {[info command busy] != ""} {
    rename busy blt_busy
}

if {[info command drag&drop] != ""} {
    # this must be handled differently due to internal bindings:
    proc blt_drag&drop {args} {
                eval drag&drop $args
    }
} else {
        puts "Error: no drag and drop"
        exit 1
}

proc drop_handler {} {
        global DragDrop argc argv
        if { $argc > 0 } {
                set str [join [concat $argv] " "]
                regsub -all %s $str $DragDrop(file) str2
                exec sh -c "$str2" &
        } else {
                .win config -text $DragDrop(file)
        }
        update idletasks
}

blt_drag&drop target . handler file "drop_handler"

tkwait window .
