#!/bin/sh
# The next line is executed by /bin/sh, but not tcl \
        exec wish8.4 "$0" ${1+"$@"}

#############################################################################
#############################################################################
## This script implements only drop targets, that utilise our cross-platform
## formats Text, Files, Image. This is a simple demo showing the ability to
## understand a wide range of Windows platform specific formats...
##
## Georgios Petasis, 03/01/2000
#############################################################################
#############################################################################

## Check Tk version:
package require Tk 8.3

if {$::tcl_version == "8.3" && ![package vsatisfies $::tcl_patchLevel 8.3.3]} {
    tk_messageBox -type ok -icon error \
            -message "  =====> TkDND requires at least tk8.3.3! <====="
    exit 1
}

if {[string equal $tcl_platform(platform) unix]} {
    option add *font {arial 12}
    option add *borderWidth 1
}

## Load tkdnd package...
set DIR [file dirname [file normalize [info script]]]
## Make sure that we can find the tkdnd package even if the user has not yet
## installed the package.
lappend auto_path [file dirname $DIR] [file dirname $DIR]/lib
package require tkdnd

##
## Create our main window...
##
wm overrideredirect . 1
wm geometry . 1x1-10-10
set win .demo_window
toplevel $win
wm title $win "Cross-Platform Drag and Drop Demo..."
wm protocol $win WM_DELETE_WINDOW exit
bind $win <Destroy> {exit}

## Add drag sources...
set dwin $win.drag_sources
frame $dwin
label $dwin.text  -bg red -fg white -relief raised -bd 1 -text Text

label $dwin.files  -bg green -fg black -relief raised -bd 1 -text Files

label $dwin.images -bg green -fg black -relief raised -bd 1 -text Image
## Select the image to be transfered...
radiobutton $dwin.i1 -variable tImage -value itk
radiobutton $dwin.i2 -variable tImage -value iteapot
set tImage itk
## Select the canvas background...
button $dwin.white -text {    } -bg white   -bd 1 -command \
  "$win.frame.dropped_data configure -bg white"
button $dwin.green -text {    } -bg #8fbc8f -bd 1 -command \
  "$win.frame.dropped_data configure -bg #8fbc8f"
button $dwin.black -text {    } -bg black   -bd 1 -command \
  "$win.frame.dropped_data configure -bg black"

grid $dwin.text     -padx 2 -pady 2 -sticky snew
grid $dwin.files    -padx 2 -pady 2 -sticky snew
grid $dwin.images   -padx 2 -pady 2 -sticky snew
grid $dwin.i1       -padx 2 -pady 2 -sticky snew
grid $dwin.i2       -padx 2 -pady 2 -sticky snew
grid $dwin.white    -padx 2 -pady 2 -sticky snew
grid $dwin.green    -padx 2 -pady 2 -sticky snew
grid $dwin.black    -padx 2 -pady 2 -sticky snew

## Add drop targets...
label  $win.text  -height 3 -bg red    -text {Drop any textual format here!}
label  $win.files -height 3 -bg green  -text {Drop any file(s) here!}
label  $win.image -height 3 -bg yellow -text {Drop any image format here!}
frame $win.frame
canvas $win.frame.dropped_data -bg white -relief sunken -bd 2 \
  -xscrollcommand "$win.frame.x set" -yscrollcommand "$win.frame.y set"
scrollbar $win.frame.x -orient horizontal \
  -command "$win.frame.dropped_data xview"
scrollbar $win.frame.y -orient vertical \
  -command "$win.frame.dropped_data yview"
$win.frame.dropped_data create text  150 150 -tags {text}  -width 500 -justify left
$win.frame.dropped_data create image 150 150 -tags {image} -anchor n
grid $win.frame.dropped_data $win.frame.y -sticky snew
grid $win.frame.x -sticky snew
grid columnconfigure $win.frame 0 -weight 1
grid rowconfigure    $win.frame 0 -weight 1

## Pack widgets...
grid $win.drag_sources -row 0 -column 0 -sticky new  -padx 2 -pady 4 -rowspan 4
grid $win.text         -row 0 -column 1 -sticky snew -padx 2 -pady 4
grid $win.files        -row 1 -column 1 -sticky snew -padx 2 -pady 4
grid $win.image        -row 2 -column 1 -sticky snew -padx 2 -pady 4
grid $win.frame        -row 3 -column 1 -sticky snew -padx 2 -pady 4
grid rowconfigure    $win 3 -weight 1
grid columnconfigure $win 0 -weight 0
grid columnconfigure $win 1 -weight 1

## Register our drop targets. Do not forget that widgets must have been
## created in order to be drag&drop aware :-)
update idletasks
dnd bindtarget $win.text  Text  <Drop> {HandleTextDrop  %D %T}
dnd bindtarget $win.files Files <Drop> {HandleFileDrop  %D %T}
dnd bindtarget $win.image Image <Drop> {HandleImageDrop %D %T}

## Register our drag sources...
dnd bindsource $dwin.text     Text \
        {return "Greek Text: \u03b1\u03b2\u03ac\u03c1\u03b5\u03c4\u03bf\u03c2"}
dnd bindsource $dwin.files    Files       {return [pwd]}
dnd bindsource $dwin.images   Image       {return $::tImage}
foreach widget {text files images} {
    bind $dwin.$widget <1> {dnd drag %W}
}

proc HandleTextDrop {data type} {
    global win
    
    $win.frame.dropped_data itemconfigure image -image {}
    $win.frame.dropped_data itemconfigure text  -text $data
    $win.frame.dropped_data configure -scrollregion [$win.frame.dropped_data bbox all]
}

proc HandleFileDrop {data type} {
    global win
    
    $win.frame.dropped_data itemconfigure image -image {}
    $win.frame.dropped_data itemconfigure text  -text $data
    $win.frame.dropped_data configure -scrollregion [$win.frame.dropped_data bbox all]
}

proc HandleImageDrop {data type} {
    global win

    ## Destroy any previous image...
    catch {
      set image [$win.frame.dropped_data itemcget image -image]
      $win.frame.dropped_data itemconfigure image -image {}
      if {![image inuse $image]} {image delete $image}
    }
    ## Is the data an image name?
    if {[catch {image inuse $data}]} {
      $win.frame.dropped_data itemconfigure text  -text $data
    } else {
      $win.frame.dropped_data itemconfigure image -image $data
      $win.frame.dropped_data itemconfigure text  -text {}
    }
    $win.frame.dropped_data configure -scrollregion [$win.frame.dropped_data bbox all]
}

#############################################################################
## What follows is a sample tk image (tk logo :-))

image create photo itk -data {
R0lGODlhKwBAAPf/AP//////zP//mf//Zv//M///AP/M///MzP/Mmf/MZv/M
M//MAP+Z//+ZzP+Zmf+ZZv+ZM/+ZAP9m//9mzP9mmf9mZv9mM/9mAP8z//8z
zP8zmf8zZv8zM/8zAP8A//8AzP8Amf8AZv8AM/8AAMz//8z/zMz/mcz/Zsz/
M8z/AMzM/8zMzMzMmczMZszMM8zMAMyZ/8yZzMyZmcyZZsyZM8yZAMxm/8xm
zMxmmcxmZsxmM8xmAMwz/8wzzMwzmcwzZswzM8wzAMwA/8wAzMwAmcwAZswA
M8wAAJn//5n/zJn/mZn/Zpn/M5n/AJnM/5nMzJnMmZnMZpnMM5nMAJmZ/5mZ
zJmZmZmZZpmZM5mZAJlm/5lmzJlmmZlmZplmM5lmAJkz/5kzzJkzmZkzZpkz
M5kzAJk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+DmSTVWYhyeUfUECQEqEzqxyg
X4It+SXQAb95GOE+KK2HYUibAYAihyoKmM+B7N0U44xAqUhKfTLQ9VNJMgLV
1CogAlIdKTO8yBKR9nAYBJKH5CObPlXdBJxARelUEZJd6GEeXw4elOVKRyDw
hIdhIscRhwWduZIDHj6TgyfmrYDZT+4NJKdTMni4SqAD3vPnWQN1eRuhg6qI
DAJB9UnUoW09AGGjppFyIJwDRanTERVAeAg+1QGyaZkD8XibDlYeQp1p+jz/
4ORKzxXpYKuvOjOGrBzWkyqHXTjjanVd6Nfrr0DlgMywpq0GFJEN5hQaMqvc
4+EhFjirE7RGBkHKKvh4eN+pQw4ULU4frYKpM8jIMOtKCJj703gHkPKMDHg6
Q4qxQMV4Lk7U3ctbPkC8q1KM4H2ag4c4xNbuUQgbpWwXxpGyZ1AmAiCcUfro
Y9wY2PU1kLY/3UdbVBpyBhpOM7gbhAM+6QWEAxevtKCzQu7VVg4HHgFzSB2F
JOJTLN0MtKJAvDlezxlBJaLQOSRd9Mjt1UPXETM4wPNTWx+hg7GTbRzEATPM
kGWfOYtW8NjRfbR0U0CIjQC/CiGA4XNpA3B1RUFca50DpCCZDRICZr9G0r/P
LYVRSBullJHbUYP0eGgbfV1V4qDpRbRrMqe0eVtP3e0nXFttSSnpO6GFulEN
Kbr6s5O+zrqtDp7+E2M8epRRyxUpVA9CEtVd0fAyFF+2RhnpoEPrCPhu0vPQ
PxQQADs=====}

image create photo iteapot -data {
[encoded data, 4453 characters]
yH/X1hEBAQA7}
$dwin.i1 configure -image itk
$dwin.i2 configure -image iteapot


