#!/usr/local/bin/wish8.0
#
# XShodou version 1.11 for X based on Tcl/Tk.
# Programed by Sakai Hiroaki. (E-Mail:sakai@miya.ee.kagu.sut.ac.jp)
# Title by Nemoto Miho. (Special thanks!)
# Please exec at X and Tcl/Tk environment.
#
# XShodou - a Japanese shodou program for X based on Tcl/Tk
#
# XShodou Copyright (c) 1998 Sakai Hiroaki.
# All Rights Reserved.
#
# XShodou is a Japanese shodou program for X based on Tcl/Tk. You can write
# Japanese traditional letters by using the mouse as if you used a hude -
# Japanese brush to write shodou letters. You can save the image as an EPS
# file.
#
# XShodou is a free software. You may copy or distribute the original
# XShodou freely. But, you may not modify or distribute the original
# XShodou without permission of the author.

# Initialize
set title_file /usr/X11R6/lib/X11/XShodou/xshodou-title.xbm
set hude_type 50
set hude_r 0
set line_number 0
set tag_name line
set moveline not
set filename new.sho
set help_message [ join {
    "XShodou is a Japanese shodou program for X based on Tcl/Tk. "
    "You can write Japanese traditional letters by using the mouse "
    "as if you used a hude - Japanese brush to write shodou letters.\n\n"
    "Mouse Operations:\n"
    "  Left Button (click or drag) - Put down the hude.\n"
    "  Right Button (click or drag) - Pull up the hude.\n"
    "  Middle Button (the hude is on the canvas.)\n"
    "              - Pull up and open the hude from the canvas.\n"
    "  Middle Button (click or drag) - Mark or move the line\n"
    "                                  pointed by the mouse.\n\n"
    "File Operations:\n"
    "  Open - Load an image from an XShodou file.\n"
    "  New - New canvas.\n"
    "  Save or Save As ... - Save the image as an XShodou file.\n"
    "  Merge - Merge the canvas and the image loaded from an XShodou file.\n"
    "  EPS Output - Save the image as an EPS file.\n\n"
    "Edit Operations:\n"
    "  Copy - Copy the marked line.\n" 
    "  Delete - Delete the marked line.\n" 
    "  Hude - Select the size of the hude to use.\n\n"
    "Help: Display this message."
  } {}
]
set about_message [ join { "XShodou - a Japanese shodou program for X "
  "based on Tcl/Tk. Please write Japanese traditional letters and "
  "make your mind calm!\n\n"
  "Programed by Sakai Hiroaki.\n"
  "Title by Nemoto Miho.\n\n"
  "Copyright (c) 1998 Sakai Hiroaki." } {}
]

# Main Window
wm title . "XShodou"

# Help Window
toplevel .help_window
wm title .help_window "Help"
wm withdraw .help_window
message .help_window.message -text $help_message
button .help_window.button -text "Ok" -command "wm withdraw .help_window"
pack .help_window.message .help_window.button -side top

# About Window
toplevel .about_window
wm title .about_window "About"
wm withdraw .about_window
message .about_window.message -text $about_message
button .about_window.button -text "Ok" -command "wm withdraw .about_window"
pack .about_window.message .about_window.button -side top

# Menu Bar
frame .menubar
menubutton .file -text "File" -menu .file.menu
menubutton .edit -text "Edit" -menu .edit.menu
button .help -text "Help" -command "wm deiconify .help_window"

# File Menu
menu .file.menu -tearoff 0
.file.menu add command -label "Open" -command {
  set filename [ tk_getOpenFile -initialfile $filename \
    -defaultextension ".sho" \
    -title "Select File" -filetypes { { "XShodou Files" { .sho } } \
      { "Text Files" { .txt } } { "All Files" { * } } } ]
  LoadData $filename
}
.file.menu add command -label "New" -command {
  .canvas delete find all
  set line_number 0
}
.file.menu add command -label "Save" -command { SaveData $filename }
.file.menu add command -label "Save As..." -command {
  set filename [ tk_getSaveFile -initialfile $filename \
    -defaultextension ".sho" \
    -title "Select File" -filetypes { { "XShodou Files" { .sho } } \
      { "Text Files" { .txt } } { "All Files" { * } } } ]
  SaveData $filename
}
.file.menu add command -label "Merge" -command {
  set filename [ tk_getOpenFile -initialfile $filename \
    -defaultextension ".sho" \
    -title "Select File" -filetypes { { "XShodou Files" { .sho } } \
      { "Text Files" { .txt } } { "All Files" { * } } } ]
  MergeData $filename
}
.file.menu add command -label "EPS Output" -command {
  set EPSfilename [ tk_getSaveFile -initialfile \
    "[ file rootname $filename ].eps" -defaultextension ".eps" \
    -title "Select File" -filetypes { { "EPS Files" { .eps } } \
    { "XShodou Files" { .sho } } { "Text Files" { .txt } } \
    { "All Files" { * } } } ]
  .canvas postscript -colormode mono -file $EPSfilename
#    -width [ .canvas cget -width ] -height [ .canvas cget -height ]
#    -pagewidth [ .canvas cget -width ] -pageheight [ .canvas cget -height ]
}
.file.menu add command -label "About" -command "wm deiconify .about_window"
.file.menu add separator
.file.menu add command -label "Quit" -command "exit"

# Edit Menu
menu .edit.menu -tearoff 0
.edit.menu add command -label "Copy" -command {
  foreach t0 $moveline {
    foreach t1 [ .canvas find withtag $t0 ] {
      set t2 [ .canvas coords $t1 ]
      CreateLine [ lindex $t2 0 ] [ lindex $t2 1 ] [ lindex $t2 2 ] \
        [ lindex $t2 3 ] [ .canvas itemcget $t1 -width ] \
        "${tag_name}${line_number}"
    }
    incr line_number
  }
}
.edit.menu add command -label "Delete" -command {
  foreach t0 $moveline {
    if { $t0 != "current" } { .canvas delete $t0 }
  }
}
menu .edit.menu.hude -tearoff 0
.edit.menu add cascade -label "Hude" -menu .edit.menu.hude
foreach t0 { 5 10 15 20 25 30 35 40 45 50 \
  60 70 80 90 100 125 150 175 200 250 } {
  .edit.menu.hude add radiobutton -label "Hude${t0}" -value $t0 \
    -variable hude_type -selectcolor black
}

# Canvas
image create bitmap title -file $title_file
canvas .canvas -width [ image width title ] -height [ image height title ]
#  -xscrollcommand { .xscroll set } -yscrollcommand { .yscroll set }
#scrollbar .xscroll -command { .canvas xview } -orient horizontal
#scrollbar .yscroll -command { .canvas yview } -orient vertical
.canvas create image 0 0 -anchor nw -image title

bind .canvas <Motion> {
  if { $hude_r } {
    CreateLine $hude_x $hude_y %x %y $hude_r "${tag_name}${line_number}"
  }
  set hude_x %x
  set hude_y %y
}

bind .canvas <ButtonPress-1> {
  if { $hude_r < $hude_type } { incr hude_r }
  CreateLine $hude_x $hude_y %x %y $hude_r "${tag_name}${line_number}"
}

bind .canvas <ButtonPress-2> {
  if { $hude_r } {
    set hude_r 0
    incr line_number
    set moveline not
  } else {
    set moveline [ .canvas gettags [ .canvas find closest %x %y ] ]
  }
}

bind .canvas <ButtonPress-3> {
  if { $hude_r } {
    incr hude_r -1
    if { $hude_r == 0 } {
      incr line_number
    }
  }
}

bind .canvas <B1-Motion> {
  if { $hude_r < $hude_type } { incr hude_r }
  CreateLine $hude_x $hude_y %x %y $hude_r "${tag_name}${line_number}"
  set hude_x %x
  set hude_y %y
}

bind .canvas <B2-Motion> {
  if { $moveline != "not" } {
    foreach t0 $moveline {
      if { $t0 != "current" } {
        .canvas move $t0 [ expr %x - $hude_x ] [ expr %y - $hude_y ]
      }
    }
  }
  set hude_x %x
  set hude_y %y
}

bind .canvas <B3-Motion> {
  if { $hude_r } {
    incr hude_r -1
    if { $hude_r } {
      CreateLine $hude_x $hude_y %x %y $hude_r "${tag_name}${line_number}"
    } else {
      incr line_number
    }
  }
  set hude_x %x
  set hude_y %y
}

# Save Proc
proc SaveData { filename } {
  global line_number
  set fileID [ open $filename w 0644 ]
  puts $fileID "# XShodou Data File"
  puts $fileID [ .canvas cget -width ]
  puts $fileID [ .canvas cget -height ]
  puts $fileID $line_number
  foreach t0 [ .canvas find all ] {
    puts $fileID [ .canvas coords $t0 ] nonewline
    puts $fileID " " nonewline
    puts $fileID [ .canvas itemcget $t0 -width ] nonewline
    puts $fileID " " nonewline
    puts $fileID [ .canvas gettags $t0 ]
  }
  close $fileID
}

# Load Proc
proc LoadData { filename } {
  global line_number
  set fileID [ open $filename r ]
  .canvas delete find all
  gets $fileID t0
  gets $fileID t1
  gets $fileID t2
  .canvas configure -width $t1 -height $t2
  gets $fileID line_number
  while { [ gets $fileID t0 ] >= 0 } {
    CreateLine [ lindex $t0 0 ] [ lindex $t0 1 ] [ lindex $t0 2 ] \
      [ lindex $t0 3 ] [ lindex $t0 4] [ lindex $t0 5 ]
  }
  close $fileID
}

# Merge Proc
proc MergeData { filename } {
  global line_number
  global tag_name
  set fileID [ open $filename r ]
  gets $fileID t0
  gets $fileID t0
  gets $fileID t0
  gets $fileID t1
  while { [ gets $fileID t0 ] >= 0 } {
    set t2 [ expr $line_number + [ string trimleft [ lindex $t0 5 ] \
      $tag_name ] ]
    CreateLine [ lindex $t0 0 ] [ lindex $t0 1 ] [ lindex $t0 2 ] \
      [ lindex $t0 3 ] [ lindex $t0 4] "${tag_name}${t2}"
  }
  close $fileID
  incr line_number $t1
}

# Create Line Proc
proc CreateLine { x0 y0 x1 y1 r tag } {
  .canvas create line $x0 $y0 $x1 $y1 -capstyle round \
    -width $r -fill black -tag $tag
}

# Pack
pack .file .edit -in .menubar -side left
pack .help -in .menubar -side right
pack .menubar -fill x
#pack .yscroll -side left -fill y
#pack .xscroll -side top -fill x
pack .canvas -side top -fill both -expand true

# End of Program
