#!/bin/sh
# the next line restart using wish \
exec wish "$0" "$@"

# إץե(tkfusen_readme.euc)Υǥ쥯ȥ
# "/usr/doc/tkfusen-1.4" ʬɬפ˱ƽƲ
set helpdir /usr/doc/tkfusen-1.4

# 䵤Ȥطʿʸ (ǽΥǥե)
set def_bg lavender
set def_fg black

set tkfusen_version "ԣ version 1.4 by R.K"

#===========================================================================
#  X Window system   Tk 䵡 version 1.4
#
#    ե̾   tkfusen
#         Tcl/Tk ܸ (Tcl 7.6jp, Tk 4.2jp)
#           2000ǯ39
#            ԡ ҡ <kawagisi@yk.rim.or.jp>
#     ȯ    Plamo  Linux 1.4.4 + Tcl7.6jp/Tk4.2jp
#                   LASER5 Linux 6.0 Rel2 + Tcl/Tk 8.0.5jp
#    ưǧĶ
#        Plamo  Linux 1.3 / 1.4.4 , RedHat Linux 5.2 / 6.1
#        LASER5 Linux 6.0 , Turbo Linux 3.0 / 4.5
#        Kondara MNU/Linux 1.0 , Caldera Open Linux 2.3
#        Linux Mandrake 6.1 JP
#        FreeBSD 2.2.8-RELEASE + Tcl7.6jp/Tk4.2jp 
#        FreeBSD 2.2.8-RELEASE + Tcl/Tk 8.0.3jp
#        Solaris 2.5.1(SunOS 5.5.1) + Tcl/Tk 8.0p2
#        HP-UX 10.20 + Tcl/Tk 8.0.4jp1.3
#===========================================================================

#---------------------------------------------------------------------------
# ݥåץåס˥塼
#---------------------------------------------------------------------------
proc MakeMenu { wname } {
    global  w  wTOP  def_bg  def_fg  wBR3

    set   w(pop) $wname.popup
    menu $w(pop) -tearoff no

#   $w(pop) add command -label "ԡ          " -command StrCopy
#   $w(pop) add command -label "å          " -command StrCut
#   $w(pop) add command -label "ڡ        " -command StrPaste
#   $w(pop) add separator
    $w(pop) add cascade -label "            " -menu $w(pop).tag
    $w(pop) add command -label "Υɥ" -command {PutTag clear   }
    $w(pop) add separator
    $w(pop) add command -label "      " -command NewFusen
    $w(pop) add command -label "䵤ʣ      " -command CpFusen
    $w(pop) add separator
    $w(pop) add command -label "ȥΤɽ" -command {OnlyTitle $wTOP}
    $w(pop) add command -label "      " -command {Iconify   $wTOP}
    $w(pop) add command -label "䵤򱣤      " -command {HideFusen $wTOP}
    $w(pop) add command -label "䵤      " -command {RmFusen   $wTOP}
    $w(pop) add command -label "䵤ξɽ" -command FusenStatus
    $w(pop) add separator
    $w(pop) add command -label "(Tk8.0) " -command InsertGraph
    $w(pop) add command -label "(Tk8.0) " -command DeleteGraph
    $w(pop) add command -label "եɹ" -command LoadFile
    $w(pop) add command -label "ե¸  " -command SaveFile
    $w(pop) add separator
    $w(pop) add command -label "դ" -command InsertDate
    $w(pop) add command -label "߻  " -command InsertTime
    $w(pop) add cascade -label "  " -menu $w(pop).calender
    $w(pop) add separator
    $w(pop) add command -label "Сͭ" -command {CreateScbar $wTOP}
    $w(pop) add command -label "С̵" -command {DeleteScbar $wTOP}
    $w(pop) add separator
    $w(pop) add command -label "ȥѹ    " -command ChangeTitle
#   $w(pop) add cascade -label "եѹ    " -menu $w(pop).font
    $w(pop) add cascade -label "ط&ʸ ѹ" -menu $w(pop).color
#   $w(pop) add cascade -label "            " -command PrintFusen
    $w(pop) add cascade -label "Ѵ" -menu $w(pop).imestyle
    $w(pop) add separator
    $w(pop) add command -label "Сɽ  " -command DispVersion
    $w(pop) add command -label "إ          " -command DispHelp
    $w(pop) add command -label "λ            " -command End_Process

    #-------------------------------------------------------------------
    # ط(ϰطʿʸֳ)Υݥåץåץ˥塼
    #-------------------------------------------------------------------
    set   w(poptag) $w(pop).tag
    menu $w(poptag) -tearoff no

    $w(poptag) add radiobutton  -label " cyan     " -background cyan \
           -variable tagBG_name -command { PutTag BGblue }
    $w(poptag) add radiobutton  -label " tomato   " -background tomato  \
           -variable tagBG_name -command { PutTag BGred }
    $w(poptag) add radiobutton  -label " ԥ   " -background pink \
           -variable tagBG_name -command { PutTag BGpink }
    $w(poptag) add radiobutton  -label "        " -background yellow1 \
           -variable tagBG_name -command { PutTag BGyellow1 }
    $w(poptag) add radiobutton  -label " ¾ο   " -background white \
           -variable tagBG_name \
	   -command { set tagbg     [$wBR3 cget -bg]
                      set new_tagbg [tk_chooseColor -initialcolor $tagbg]
		      PutTag BGother }
    $w(poptag) add separator
    $w(poptag) add radiobutton  -label "    " -foreground black \
           -variable tagFG_name -command { PutTag FGblack }
    $w(poptag) add radiobutton  -label "򤤻    " -foreground white \
           -variable tagFG_name -command { PutTag FGwhite }
    $w(poptag) add radiobutton  -label "֤    " -foreground red  \
           -variable tagFG_name -command { PutTag FGred }
    $w(poptag) add radiobutton  -label "Ĥ    " -foreground blue \
           -variable tagFG_name -command { PutTag FGblue }
    $w(poptag) add separator
    $w(poptag) add radiobutton   -label "ֳ  " -foreground black  \
           -variable tagStp_name -command { PutTag StpBlack }
    $w(poptag) add radiobutton   -label "Фֳ  " -foreground green  \
           -variable tagStp_name -command { PutTag StpGreen }
    $w(poptag) add radiobutton   -label "ֳ֤  " -foreground red  \
           -variable tagStp_name -command { PutTag StpRed }
    $w(poptag) add radiobutton   -label "Ĥֳ  " -foreground blue \
           -variable tagStp_name -command { PutTag StpBlue }
    $w(poptag) add separator
    $w(poptag) add command -label "      " -command {PutTag under   }
    $w(poptag) add command -label "õ  " -command {PutTag rmunder }
    $w(poptag) add separator
    $w(poptag) add command -label "    " -command {PutTag strike  }
    $w(poptag) add command -label "õ" -command {PutTag rmstrike}

    #-------------------------------------------------------------------
    #   Υݥåץåץ˥塼
    #-------------------------------------------------------------------
    set   w(popcal) $w(pop).calender
    menu $w(popcal) -tearoff no

    $w(popcal) add command -label "" -command { InsertPMCal }
    $w(popcal) add command -label "" -command { InsertCMCal }
    $w(popcal) add command -label "" -command { InsertNMCal }
    $w(popcal) add command -label "ǯǯ" -command { InsertPYCal }
    $w(popcal) add command -label "ǯǯ" -command { InsertCYCal }
    $w(popcal) add command -label "ǯǯ" -command { InsertNYCal }
    #-------------------------------------------------------------------
    #   եΥݥåץåץ˥塼
    #-------------------------------------------------------------------
#    set   w(popfont) $w(pop).font
#    menu $w(popfont) -tearoff no
#
#    $w(popfont) add radiobutton -label "ʸ14ݥ" \
#	-variable fontsize \
#       -command { ChangeConfig "-font { marumoji 18 { normal } } " }
#    $w(popfont) add radiobutton -label "20ݥ" \
#	-variable fontsize \
#	-command { ChangeConfig "-font { times 24 { normal } } " }
#    $w(popfont) add radiobutton -label "26ݥ" \
#	-variable fontsize -command { ChangeConfig "-font { k26 }" }
    #-------------------------------------------------------------------
    #   طʿΥݥåץåץ˥塼
    #-------------------------------------------------------------------
    set   w(popcol) $w(pop).color
    menu $w(popcol) -tearoff no

    $w(popcol) add radiobutton -label " lavender   " -background lavender \
       -variable color$wname -command { set def_bg lavender
			                ChangeConfig "-bg lavender" }
    $w(popcol) add radiobutton -label " pink       " -background pink \
       -variable color$wname -command { set def_bg pink
			                ChangeConfig "-bg pink" }
    $w(popcol) add radiobutton -label " aquamarine " -background aquamarine \
       -variable color$wname  -command { set def_bg aquamarine
			                 ChangeConfig "-bg aquamarine" }
    $w(popcol) add radiobutton -label " yellow1    " -background yellow1 \
       -variable color$wname  -command { set def_bg yellow1
			                 ChangeConfig "-bg yellow1" }
    $w(popcol) add radiobutton -label " cornsilk   " -background cornsilk \
       -variable color$wname -command { set def_bg cornsilk
			                ChangeConfig "-bg cornsilk" }
    $w(popcol) add radiobutton -label " white      " -background white \
       -variable color$wname -command { set def_bg white
			                ChangeConfig "-bg white" }
    $w(popcol) add radiobutton -label " MS-DOS   " \
       -background black -foreground white \
       -variable color$wname -command { set def_bg black 
			                set def_fg white
			                ChangeConfig "-bg black -fg white" }
    $w(popcol) add radiobutton -label " ¾ο     " -background white \
       -variable color$wname \
       -command { set bgcol  [$wBR3 cget -bg]
                  set new_bg [tk_chooseColor -initialcolor $bgcol]
		  if { $new_bg != "" } then {
		      set def_bg $new_bg
		      ChangeConfig "-bg $new_bg"
		  }
		}
    set color$wname 1
    #-------------------------------------------------------------------
    #   ʸΥݥåץåץ˥塼
    #-------------------------------------------------------------------
    $w(popcol) add separator
    $w(popcol) add radiobutton -label "  " -foreground black \
		-variable fcolor$wname \
		-command { set def_fg black
			   ChangeConfig "-fg black" }
    $w(popcol) add radiobutton -label "򤤻  " -foreground white \
		-variable fcolor$wname \
		-command { set def_fg white 
			   ChangeConfig "-fg white" }
    $w(popcol) add radiobutton -label "֤  " -foreground red \
		-variable fcolor$wname \
		-command { set def_fg red 
			   ChangeConfig "-fg red" }
    $w(popcol) add radiobutton -label "Ĥ  " -foreground blue \
		-variable fcolor$wname \
		-command { set def_fg blue 
			   ChangeConfig "-fg blue" }
    $w(popcol) add radiobutton -label "¾ο  " -background white \
	        -variable fcolor$wname \
                -command { set fgcol  [$wBR3 cget -fg]
                           set new_fg [tk_chooseColor -initialcolor $fgcol]
			   if { $new_fg != "" } then {
			       set def_fg $new_fg
			       ChangeConfig "-fg $new_fg"
			   }
			 }
    set fcolor$wname 1
    #-------------------------------------------------------------------
    #   ѴΥݥåץåץ˥塼
    #-------------------------------------------------------------------
    set   w(popime) $w(pop).imestyle
    menu $w(popime) -tearoff no

    $w(popime) add radiobutton -label "Over the Spot 1" \
        -variable ximstyle$wname \
        -command { imconfigure $wBR3 -style {PreeditPosition StatusArea} }
    $w(popime) add radiobutton -label "Over the Spot 2" \
        -variable ximstyle$wname \
        -command { imconfigure $wBR3 -style {PreeditPosition StatusNothing} }
    $w(popime) add radiobutton -label "Off  the Spot" \
        -variable ximstyle$wname \
        -command { imconfigure $wBR3 -style {PreeditArea StatusArea} }
    $w(popime) add radiobutton -label "root ɥ" \
        -variable ximstyle$wname \
        -command { imconfigure $wBR3 -style {PreeditNothing StatusNothing} }
}

#--------------------------------------------------------------------------
# ʸ󥳥ԡ
#--------------------------------------------------------------------------
#proc StrCopy {} {}
#--------------------------------------------------------------------------
# ʸ󥫥å
#--------------------------------------------------------------------------
#proc StrCut {} {}
#--------------------------------------------------------------------------
# ʸڡ
#--------------------------------------------------------------------------
#proc StrPaste {} {}
#--------------------------------------------------------------------------
# 䵤ΰ
#--------------------------------------------------------------------------
#proc PrintFusen {} {}

#--------------------------------------------------------------------------
# դ
#--------------------------------------------------------------------------
proc PutTag { m } {
    global  wBR3 wTOP tagno new_tagbg

    set fno [ string trimleft $wTOP .w ]
    if { $m == "clear" } {
        $wBR3 tag delete $tagno($fno)
        if { $tagno($fno) > 0 } { incr tagno($fno) -1 }
        return
    }
    incr tagno($fno)
    set  flag [ catch [ $wBR3 tag add $tagno($fno) sel.first sel.last ] ]
    if { $flag != 0 } { incr tagno($fno) -1; return }
    switch $m {
        BGblue     { set op "-background cyan"    }
        BGred      { set op "-background tomato"  }
        BGpink     { set op "-background pink"    }
        BGyellow1  { set op "-background yellow1" }
        BGother    { set op "-background $new_tagbg" }

        FGblack    { set op "-foreground black" }
        FGwhite    { set op "-foreground white" }
        FGred      { set op "-foreground red"   }
        FGblue     { set op "-foreground blue"  }

        StpBlack   { set op "-background black -bgstipple gray25" }
        StpGreen   { set op "-background green -bgstipple gray25" }
        StpRed     { set op "-background red   -bgstipple gray25" }
        StpBlue    { set op "-background blue  -bgstipple gray25" }

        under      { set op "-underline  1 " }
        rmunder    { set op "-underline  0 " }
        strike     { set op "-overstrike 1 " }
        rmstrike   { set op "-overstrike 0 " }
    }
    eval $wBR3 tag configure $tagno($fno) $op
}
#--------------------------------------------------------------------------
# Ƥ (tag_prop ˥åȤ)
#--------------------------------------------------------------------------
proc GetTag {TOPW} {
    global  tag_prop

    set taglist [ $TOPW.frame.txt tag names ]
    set tag_prop ""
    foreach i $taglist {
        if { $i != "sel" } {
            set wk1 [ $TOPW.frame.txt tag ranges $i ]
            set wk2 [ $TOPW.frame.txt tag cget $i -background ]
            set wk3 [ $TOPW.frame.txt tag cget $i -foreground ]
            set wk4 [ $TOPW.frame.txt tag cget $i -bgstipple  ]
            set wk5 [ $TOPW.frame.txt tag cget $i -underline  ]
            set wk6 [ $TOPW.frame.txt tag cget $i -overstrike ]
            set wk7 ""
            if {$wk2 != ""} then {set wk7 [concat $wk7 -background $wk2]}
            if {$wk3 != ""} then {set wk7 [concat $wk7 -foreground $wk3]}
            if {$wk4 != ""} then {set wk7 [concat $wk7 -bgstipple  $wk4]}
            if {$wk5 != ""} then {set wk7 [concat $wk7 -underline  $wk5]}
            if {$wk6 != ""} then {set wk7 [concat $wk7 -overstrike $wk6]}
            lappend tag_prop $i $wk1 $wk7
        }
    }
}
#--------------------------------------------------------------------------
# /mark °եɤǡȲɽ
#--------------------------------------------------------------------------
proc LoadTag {fno} {
    global  tk_version  tagno  grno  iname

    #---- ֹ桢ֹνͤ򥻥åȤ
    set tagno($fno) 0
    set grno($fno)  0

    if { [file exists ~/.tkfusen/fusen$fno.tag ] } then { \

	#---- /mark °եɤ
	set tag_fileID [ open ~/.tkfusen/fusen$fno.tag r ]
	set tag_buf    [ read -nonewline $tag_fileID ]
	close $tag_fileID

	set len [ expr [llength $tag_buf] / 3]
	for { set i 0 } { $i < $len } { incr i } {
	    set work0 [lindex $tag_buf [expr $i * 3 + 0] ]
	    set work1 [lindex $tag_buf [expr $i * 3 + 1] ]
	    set work2 [lindex $tag_buf [expr $i * 3 + 2] ]
	    if { [string range $work0 0 1] == "gr" } then {
		if { [ string index $tk_version 0 ] >= 8 } then {
		    #---- ֤ mark 򥻥å
		    incr  grno($fno)
		    set   gn   $grno($fno)
		    .w$fno.frame.txt mark set gr$gn $work1

		    #---- 
		    set   iname($fno,$gn) [ file tail $work2 ]
		    image create photo $iname($fno,$gn) -file $work2
		    .w$fno.frame.txt image create $work1 \
				    -image $iname($fno,$gn) -name $gn
		}
	    } else {
		#---- ꤹ
		set work3 [lindex $work1 0 ]
		set work4 [lindex $work1 1 ]
		set work5 [lindex $work2 0 ]
		set work6 [lindex $work2 1 ]
		incr tagno($fno)
		.w$fno.frame.txt tag add       $tagno($fno) $work3 $work4
		.w$fno.frame.txt tag configure $tagno($fno) $work5 $work6
		if { [llength $work2] == 4} then { \
		    set work7 [lindex $work2 2 ]
		    set work8 [lindex $work2 3 ]
		    .w$fno.frame.txt tag configure $tagno($fno) $work7 $work8
		}
	    }
	}
    }
}
#--------------------------------------------------------------------------
#  mark Ƥ (gr_prop ˥åȤ)
#--------------------------------------------------------------------------
proc GetMark {TOPW} {
    global  tk_version  gr_prop  iname

    set fno [ string trim $TOPW .w ]
    set gr_prop($fno) ""
    if { [ string index $tk_version 0 ] >= 8 } then {
	set marklist [ $TOPW.frame.txt mark names ]
	foreach i $marklist {
	    if { [string range $i 0 1] == "gr" } then {
		set wk1 [ string trim $i "gr" ]
		set wk2 [ $TOPW.frame.txt index $i ]
		set wk3 [ $iname($fno,$wk1) cget -file ]
		lappend gr_prop($fno) $i $wk2 $wk3
	    }
	}
    }
}
#--------------------------------------------------------------------------
# ƥȥܥå
#--------------------------------------------------------------------------
proc MakeText { wname } {
    global w  def_bg  def_fg 

    set    w(main) $wname.frame
    frame $w(main)
    pack  $w(main) -side top -fill both -expand yes

    set    w(text)  $w(main).txt
    text  $w(text) -width 30 -height 4 -background $def_bg -fg $def_fg \
		   -yscrollcommand "$w(main).yscrl set"
    scrollbar $w(main).yscrl -command "$w(text) yview"

    grid $w(text)       -in $w(main) -column 0 -row 0 -sticky news
    grid rowconfigure    $w(main) 0 -weight 1
    grid columnconfigure $w(main) 0 -weight 1
}
#--------------------------------------------------------------------------
# ƥȥܥåѹ
#       顼Υܥǥޥκܥ᤯
#           ȥ顼ˤʤкȤcatch ޥɤѡ
#--------------------------------------------------------------------------
proc ChangeConfig { option } {
    global  wBR3  def_bg  wTOP
    catch { eval  $wBR3 configure $option

	#---- οطʿ˱ƹޤˤ
	set rgb [ winfo rgb $wBR3 $def_bg ]
	set r   [ lindex $rgb 0 ]
	set g   [ lindex $rgb 1 ]
	set b   [ lindex $rgb 2 ]
	if { [expr (0.25 * $r + 0.5 * $g + 0.25 * $b)] > 127 } \
	then { eval $wBR3 configure -insertbackground black -insertwidth 2 }\
	else { eval $wBR3 configure -insertbackground white -insertwidth 2 }
	Update_ConfList $wTOP
    }
}
#--------------------------------------------------------------------------
# ƥȥܥå(°°ޤ)򡢥ե˥
#--------------------------------------------------------------------------
proc SaveText {TOPW} {
    global  tag_prop  gr_prop

    set   i [ string trim $TOPW .w ]
    set   contents [ $TOPW.frame.txt get 1.0 end ]
    set   ftxt_fileID [ open ~/.tkfusen/fusen$i.txt w ]
    puts  -nonewline $ftxt_fileID $contents
    flush $ftxt_fileID
    close $ftxt_fileID

    #---- °(tag_prop) °(gr_prop)
    #---- /mark °ե(fusenxx.tag)˽񤭹
    GetTag  $TOPW
    GetMark $TOPW
    set   fno [ string trimleft $TOPW .w ]
    set   tag_fileID [ open ~/.tkfusen/fusen$fno.tag w ]
    puts  -nonewline $tag_fileID [ concat $tag_prop $gr_prop($i) ]
    close $tag_fileID
}
#--------------------------------------------------------------------------
# ơꥹȤѹ
#--------------------------------------------------------------------------
proc Change_StatusList { TOPW sts_no } {
    global wsts_list

    set idx  [ string trim $TOPW .w ]
    set work [ lreplace $wsts_list $idx $idx $sts_no ]
    set wsts_list $work

    #---- ơꥹȤ򡢥ե˳Ǽ
    set    fsts_fileID [ open ~/.tkfusen/fusen_status w ]
    puts  $fsts_fileID $work
    close $fsts_fileID
}
#--------------------------------------------------------------------------
# ơ֥ (conf_list) Ƥ򹹿
#--------------------------------------------------------------------------
proc Update_ConfList { TOPW } {
    global    oldtitle  conf_list  wno  sb_flag

    # ---- ɥΥȥ
    set cur_title    $oldtitle($TOPW) 

    # ---- x,y ɸϥȥСζ(6),⤵(25)򺹤
    set cur_width         [ winfo width  $TOPW ]
    set cur_height        [ winfo height $TOPW ]
    set cur_rootx  [ expr [ winfo rootx  $TOPW ] -6 ]
    set cur_rooty  [ expr [ winfo rooty  $TOPW ] -25 ]
    set cur_geometry [ format "%sx%s+%s+%s" \
		       $cur_width $cur_height $cur_rootx $cur_rooty ]

    # ---- طʿʸС̵ͭե饰
    set cur_bg [ $TOPW.frame.txt cget -background ]
    set cur_fg [ $TOPW.frame.txt cget -foreground ]
    set cur_sl $sb_flag($TOPW)

    set idx    [ expr 5 * [ string trim  $TOPW .w ] - 5 ]
    set work   [ lreplace $conf_list $idx [ expr $idx + 4 ] \
		 $cur_title $cur_geometry $cur_bg $cur_fg $cur_sl ]
    set conf_list $work

    #---- ơ֥򡢥ե˳Ǽ
    set    conf_fileID [ open ~/.tkfusen/fusen_conf w ]
    puts  $conf_fileID $work
    close $conf_fileID

    SaveText $TOPW
}
#--------------------------------------------------------------------------
# 䵤  (ơ:0)
#     () ǽˤ뤿ᡢ䵤򱣤ǥեϺʤ
#--------------------------------------------------------------------------
proc RmFusen  { TOPW } {
    SaveText          $TOPW
    Update_ConfList   $TOPW
    wm withdraw       $TOPW
    Change_StatusList $TOPW 0
}
#--------------------------------------------------------------------------
# ȥΤɽ  (ơ:2)
#     conf_list Υɥι⤵ϸ
#--------------------------------------------------------------------------
proc OnlyTitle { TOPW } {
    Update_ConfList   $TOPW

    # ---- ɥι⤵򣰤ˤ
    # ---- x,y ɸϥȥСζ(6),⤵(25)򺹤
    set cur_width    [ winfo width  $TOPW ]
    set cur_rootx    [ winfo rootx  $TOPW ]
    set cur_rooty    [ winfo rooty  $TOPW ]
    set new_geometry [ format "%sx%d+%s+%s" \
			      $cur_width 0 $cur_rootx $cur_rooty ]
    wm geometry $TOPW $new_geometry
    Change_StatusList $TOPW 2
}
#--------------------------------------------------------------------------
# 䵤򥢥  (ơ:3)
#--------------------------------------------------------------------------
proc Iconify { TOPW } {
    global conf_list

#    SaveText $TOPW
#    Update_ConfList $TOPW
#    Change_StatusList $TOPW 3
# ν wm iconify ǥ󲽤 NewFusen, RestoreFusen 
# bind 줿Ǽ¹ԤΤǡǤ

# ȥΤߤξ֤ǥ󲽤ȥɥι⤵ˤʤäƤޤ
# Τǥ󲽤Ƥ conf_list ι⤵᤹
    set fno          [ string trimleft $TOPW .w ]
    set idx2         [ expr ($fno - 1) * 5 + 1 ]
    set org_geometry [ lindex $conf_list $idx2 ]
    set work1 [ expr [ string first "x" $org_geometry ] + 1 ]
    set work2 [ expr [ string first "+" $org_geometry ] - 1 ]
    set org_height   [ string range $org_geometry $work1 $work2]
    set cur_width    [ winfo width $TOPW ]
    set cur_rootx    [ winfo rootx $TOPW ]
    set cur_rooty    [ winfo rooty $TOPW ]
    set new_geometry [ format "%sx%s+%s+%s" \
		       $cur_width $org_height $cur_rootx $cur_rooty ]
    wm iconify $TOPW
    set work [lreplace $conf_list $idx2 $idx2 $new_geometry]
    set conf_list $work
}
#--------------------------------------------------------------------------
# 䵤򱣤    (ơ:4)
#--------------------------------------------------------------------------
proc HideFusen { TOPW } {
    SaveText          $TOPW
    Update_ConfList   $TOPW
    wm withdraw       $TOPW
    Change_StatusList $TOPW 4
}
#--------------------------------------------------------------------------
# Сͭ
#--------------------------------------------------------------------------
proc CreateScbar { TOPW } {
    global sb_flag

    grid $TOPW.frame.yscrl -column 1 -row 0 -sticky news
    set  sb_flag($TOPW) S
    Update_ConfList $TOPW
}
#--------------------------------------------------------------------------
# С
#--------------------------------------------------------------------------
proc DeleteScbar { TOPW } {
    global sb_flag

    grid forget $TOPW.frame.yscrl
    set  sb_flag($TOPW) N
    Update_ConfList $TOPW
}
#--------------------------------------------------------------------------
# ȥѹ
#--------------------------------------------------------------------------
proc ChangeTitle {} {
    global    wTOP oldtitle deftitle curtitle conf_list

    toplevel  .ttl
    wm title  .ttl "Change Fusen title"

    entry     .ttl.ent -textvariable newtitle
    frame     .ttl.btns
    pack      .ttl.ent .ttl.btns -side top -fill both -expand yes

    set curtitle $oldtitle($wTOP)
    button    .ttl.btns.okbtn  -text "ѹ" \
		-command { regsub -all { } $newtitle "" newtitle
			   wm title  $wTOP    $newtitle
			   set curtitle       $newtitle }
    button    .ttl.btns.canbtn -text "᤹" \
		-command { .ttl.ent  delete 0 end
			   .ttl.ent  insert 0 $oldtitle($wTOP)
			   wm title  $wTOP    $oldtitle($wTOP)
			   set curtitle       $oldtitle($wTOP) }
    button    .ttl.btns.defbtn -text "ǥե" \
		-command { .ttl.ent  delete 0 end
			   .ttl.ent  insert 0 $deftitle($wTOP)
			   wm title  $wTOP    $deftitle($wTOP)
			   set curtitle       $deftitle($wTOP) }
    button    .ttl.btns.endbtn -text "λ" \
		-command { set oldtitle($wTOP) $curtitle
			   set idx  [ expr 5 * [ string trim $wTOP .w ] - 5 ]
			   set work [lreplace $conf_list $idx $idx $curtitle]
			   set conf_list $work
			   destroy .ttl
			   Update_ConfList $wTOP }
    pack      .ttl.btns.okbtn  .ttl.btns.canbtn \
	      .ttl.btns.defbtn .ttl.btns.endbtn \
		-side left -fill both -expand yes

    .ttl.ent  delete 0 end
    .ttl.ent  insert 0 $oldtitle($wTOP)
}
#--------------------------------------------------------------------------
# դ
#--------------------------------------------------------------------------
proc InsertDate {} {
    global  wBR3  wTOP

    set week {        }
    set current [ clock seconds ]
    set youbi   [ lindex $week [ clock format $current -format "%w" ]]
    set idx     [ $wBR3 index insert ]
    $wBR3 insert $idx [ clock format $current -format "%Yǯ%m%d($youbi)" ]
    SaveText $wTOP
}
#--------------------------------------------------------------------------
# 
#--------------------------------------------------------------------------
proc InsertTime {} {
    global  wBR3  wTOP

    set current [ clock seconds ]
    set idx     [ $wBR3 index insert ]
    $wBR3 insert $idx [ clock format $current -format "%H%Mʬ" ]
    SaveText $wTOP
}
#--------------------------------------------------------------------------
# Υ
#--------------------------------------------------------------------------
proc InsertCMCal {} {
    set eng_cal [ exec cal ]
    ChangeEJCal $eng_cal "M"
}
#--------------------------------------------------------------------------
# Υ
#--------------------------------------------------------------------------
proc InsertPMCal {} {
    set current [ clock seconds ]
    set month   [ clock format $current -format "%m" ]
    set year    [ clock format $current -format "%Y" ]
    if { $month == 1 } then { set month 12
			      set year  [ expr $year  - 1 ] } \
		       else { set month [ expr $month - 1 ] }
    set eng_cal [ exec cal $month $year ]
    ChangeEJCal $eng_cal "M"
}
#--------------------------------------------------------------------------
# Υ
#--------------------------------------------------------------------------
proc InsertNMCal {} {
    global wBR3

    set current [ clock seconds ]
    set month   [ clock format $current -format "%m" ]
    set year    [ clock format $current -format "%Y" ]
    if { $month == 12 } then { set month 1
			      set year  [ expr $year  + 1 ] } \
		       else { set month [ expr $month + 1 ] }
    set eng_cal [ exec cal $month $year ]
    ChangeEJCal $eng_cal "M"
}
#--------------------------------------------------------------------------
# ǯǯ֤Υ
#--------------------------------------------------------------------------
proc InsertCYCal {} {
    set eng_cal [ exec cal -y ]
    ChangeEJCal $eng_cal "Y"
}
#--------------------------------------------------------------------------
# ǯǯ֤Υ
#--------------------------------------------------------------------------
proc InsertPYCal {} {
    set current [ clock seconds ]
    set year    [ expr [ clock format $current -format "%Y" ] -1 ]
    set eng_cal [ exec cal $year ]
    ChangeEJCal $eng_cal "Y"
}
#--------------------------------------------------------------------------
# ǯǯ֤Υ
#--------------------------------------------------------------------------
proc InsertNYCal {} {
    set current [ clock seconds ]
    set year    [ expr [ clock format $current -format "%Y" ] +1 ]
    set eng_cal [ exec cal $year ]
    ChangeEJCal $eng_cal "Y"
}
#--------------------------------------------------------------------------
# αѸɽܸѴơƥȤ
#--------------------------------------------------------------------------
proc ChangeEJCal { ENG_CAL MorY } {
    global  wBR3  wTOP

    regsub -all {Su Mo Tu We Th Fr Sa} $ENG_CAL "      " jp_cal
    if { $MorY == "M" } then {
	regsub {January }  $jp_cal " 1 " jp_cal
	regsub {February } $jp_cal " 2 " jp_cal
	regsub {March }    $jp_cal " 3 " jp_cal
	regsub {April }    $jp_cal " 4 " jp_cal
	regsub {May }      $jp_cal " 5 " jp_cal
	regsub {June }     $jp_cal " 6 " jp_cal
	regsub {July }     $jp_cal " 7 " jp_cal
	regsub {August }   $jp_cal " 8 " jp_cal
	regsub {September} $jp_cal " 9 " jp_cal
	regsub {October }  $jp_cal "10 " jp_cal
	regsub {November } $jp_cal "11 " jp_cal
	regsub {December } $jp_cal "12 " jp_cal
    } else {
	regsub  {       January               February                 March       } \
	$jp_cal "January          1   February         2   March            3" jp_cal
	regsub  {        April                   May                   June        } \
	$jp_cal "April            4   May              5   June             6" jp_cal
	regsub  {        July                  August                September     } \
	$jp_cal "July             7   August           8   September        9" jp_cal
	regsub  {       October               November               December      } \
	$jp_cal "October         10   November        11   December        12" jp_cal
    }

    # ---- ǯʬξ硢ɥΥĴ
    if { $MorY == "Y" } then {
	set txtwidth  [ $wBR3 cget -width ]
	set txtheight [ $wBR3 cget -height ]
	if { $txtwidth  < 68 } then { $wBR3 configure -width  68 }
	if { $txtheight < 33 } then { $wBR3 configure -height 33 }
	tkwait visibility $wTOP
	Update_ConfList   $wTOP
    }

    set idx [ $wBR3 index insert ]
    $wBR3 insert $idx $jp_cal
    SaveText $wTOP
}
#--------------------------------------------------------------------------
# Сɽ
#--------------------------------------------------------------------------
proc DispVersion {} {
    global    tcl_version     tk_version     tkfusen_version
    global    tcl_patchLevel  tk_patchLevel  tcl_platform     wno  wTOP

    NewFusen
    set text1 $tkfusen_version
    set text2 " OS μ       $tcl_platform(os)"
    set text3 " OS ΥС $tcl_platform(osVersion)"
    set text4 " TclΥС $tcl_patchLevel"
    set text5 " Tk ΥС $tk_patchLevel"
    set text6 " 䵤ο $wno"
    set week  {        }
    set current [ clock seconds ]
    set youbi   [ lindex $week [ clock format $current -format "%w" ]]
    set current [ clock seconds ]
    set text7   [ clock format $current \
		  -format " %Yǯ%m%d($youbi) %H%Mʬ" ]
    .w$wno.frame.txt insert 1.0 [ format "%s\n\n%s\n%s\n%s\n%s\n\n%s\n%s" \
			   $text1 $text2 $text3 $text4 $text5 $text6 $text7 ]
    .w$wno.frame.txt configure -height 9

    Update_ConfList .w$wno
}
#--------------------------------------------------------------------------
# إɽ
#--------------------------------------------------------------------------
proc DispHelp {} {
    global  wno  wTOP  helpdir

    NewFusen
    CreateScbar .w$wno
    .w$wno.frame.txt configure -height 15 -width 80

    set   helpfile [ open $helpdir/tkfusen_readme.euc r ]
    set   helpmsg  [ read -nonewline $helpfile ]
    close $helpfile
    .w$wno.frame.txt insert 1.0 $helpmsg

    tkwait visibility .w$wno
    Update_ConfList   .w$wno
}
#--------------------------------------------------------------------------
# եɤ߹ߡ֤
#--------------------------------------------------------------------------
proc InsertGraph {} {
    global  wBR3  wTOP  tk_version  grno  gr_prop  iname

    if { [ string index $tk_version 0 ] >= 8 } then {
	set ftypes {
	    {"GIF/PPM/PGM/PBM" {.gif .ppm .pgm .pbm} }
	    {"GIF"         {.gif} }
	    {"PPM/PGM/PBM" {.ppm .pgm .pbm} }
	    {"ե"       * }
	}
	set fname [tk_getOpenFile -filetypes $ftypes -parent .]
	if { $fname == "" } then { return } \
	elseif { [ file readable $fname ] } {
	    set   fileid   [ open $fname "r" ]
	    set   contents [ read -nonewline $fileid ]
	    close $fileid

	    #---- ֤ mark 򥻥å
	    set   fn [ string trim $wTOP .w ]
	    incr  grno($fn)
	    set   gn  $grno($fn)
	    $wBR3 mark set gr$gn insert

	    #---- 
	    set   iname($fn,$gn) [ file tail $fname ]
	    image create photo $iname($fn,$gn) -file $fname
	    $wBR3 image create insert -image $iname($fn,$gn) -name $gn

	    SaveText $wTOP
	}
    }
}
#--------------------------------------------------------------------------
# ֤β
#--------------------------------------------------------------------------
proc DeleteGraph {} {
    global  wBR3  wTOP  tk_version  iname

    if { [ string index $tk_version 0 ] >= 8 } then {
	set idx      [ $wBR3 index insert]
	set marklist [ $wTOP.frame.txt mark names ]
	foreach i $marklist {
	    if { [string range $i 0 1] == "gr" } then {
		if {[ $wBR3 index $i ] == $idx } then {
		    $wBR3 mark unset $i
		    set fn [ string trim $wTOP .w ] 
		    set gn [ string trim $i    gr ] 
		    image delete $iname($fn,$gn)
		}
	    }
	}
	SaveText $wTOP
    }

}
#--------------------------------------------------------------------------
# ƥȥեɤ߹ߡ֤
#--------------------------------------------------------------------------
proc LoadFile {} {
    global  wBR3  wTOP

    set ftypes {
	{"ե"           * }
	{"ƥȥե"     {.txt}     }
	{"Tcl ץ"       {.tcl .tk} }
	{"C쥽ե"  {.c} }
    }
    set fname [tk_getOpenFile -filetypes $ftypes -parent .]
    if { $fname == "" } {
	return
    } else {
	if { [ file readable $fname ] } {
	    set   fileid   [ open $fname "r" ]
	    set   contents [ read -nonewline $fileid ]
	    close $fileid
	    set   idx [ $wBR3 index insert ]
	    $wBR3 insert $idx $contents
	    SaveText $wTOP
	}
    }
}
#--------------------------------------------------------------------------
# ƥȤե¸
#--------------------------------------------------------------------------
proc SaveFile {} {
    global  wBR3  wTOP

    set ftypes {
	{"ե"           * }
	{"ƥȥե"     {.txt}     }
	{"Tcl ץ"       {.tcl .tk} }
	{"C쥽ե"  {.c} }
    }
    set fname [tk_getSaveFile -filetypes $ftypes -parent . \
		-initialfile fusen -defaultextension .txt ]
    if { $fname == "" } {
	return
    } else {
	    set   contents [ $wBR3 get 1.0 end ]
	    set   fileid   [ open $fname "w" ]
	    puts  -nonewline $fileid $contents
	    close $fileid
	    SaveText $wTOP
    }
}
#--------------------------------------------------------------------------
# 䵤
#--------------------------------------------------------------------------
proc NewFusen {} {
    global  wno wsts_list deftitle oldtitle conf_list sb_flag
    global  tagno grno gr_prop

    #---- (ɥ)ֹ桢(ɥ)ơꥹȤ򹹿
    incr      wno
    lappend   wsts_list 1

    #---- ֹΥɥ
    toplevel  .w$wno
    set       deftitle(.w$wno) Fusen$wno
    set       oldtitle(.w$wno) Fusen$wno
    set       work             Fusen$wno
    wm title  .w$wno $work
    .w$wno    configure -cursor hand2

    #---- ƥȥܥå
    MakeText  .w$wno
#    SaveText  .w$wno

    #---- ֹ桢ֹνͤ򥻥åȤ
    set tagno($wno)    0
    set grno($wno)     0
    set gr_prop($wno)  ""

    #---- С̵ͭե饰 N(̵) 򥻥å
    set sb_flag(.w$wno) N

    #---- ݥåץåס˥塼
    MakeMenu  .w$wno

    #---- ɥĻˤʤäƤ顢ơꥹȤɲä롣
    #---- ȥ롿ȥ꡿طʿʸС̵ͭե饰
    #---- ơ֥ɲä롣
    tkwait visibility .w$wno
    lappend $wsts_list 1
    Change_StatusList .w$wno 1
    set conf_list [ concat $conf_list 0 0 0 0 0 ]
    Update_ConfList .w$wno

    # ---- 䵥ɥ򥢥󲽤ϡ"䵥"򤹤
    bind .w$wno.frame.txt <Unmap> { regexp {.w[0-9]*} %W w_icon
				    SaveText          $w_icon
				    Update_ConfList   $w_icon
				    Change_StatusList $w_icon 3 }

    # ---- 䵥ɥĤϡ"䵤򱣤"򤹤
    wm protocol .w$wno WM_DELETE_WINDOW "HideFusen .w$wno"
    wm protocol .w$wno WM_SAVE_YOURSELF "HideFusen .w$wno"
}
#--------------------------------------------------------------------------
# 䵤ʣ
#--------------------------------------------------------------------------
proc CpFusen {} {
    global    wTOP wno conf_list sb_flag tag_prop gr_prop tk_version iname

    #---- ɥ⤵طʿʸС̵ͭե饰
    set org_width  [ winfo width  $wTOP ]
    set org_height [ winfo height $wTOP ]
    set org_bg     [ $wTOP.frame.txt cget -background ]
    set org_fg     [ $wTOP.frame.txt cget -foreground ]
    set org_sl     $sb_flag($wTOP)

    #---- ƥȤ
    set org_contents [ $wTOP.frame.txt get 1.0 end ]

    #---- (tag_prop)Ȳ mark (gr_prop)
    GetTag  $wTOP
    GetMark $wTOP
    set fno     [ string trim $wTOP .w ]
    set tag_buf [ concat $tag_prop $gr_prop($fno) ]
#puts $tag_buf
    NewFusen

    #---- ɥΥȥѹ
    set new_rootx [ winfo rootx  .w$wno ]
    set new_rooty [ winfo rooty  .w$wno ]
    set new_geometry [ format "%sx%s+%s+%s" \
		       $org_width $org_height $new_rootx $new_rooty ]
    wm geometry .w$wno $new_geometry

    #---- ƥȤƤ򥳥ԡ
    .w$wno.frame.txt insert 1.0 $org_contents

    #---- Ȳ mark 򥳥ԡ
    set tagno($wno) 0
    set grno($wno)  0
    set len [ expr [llength $tag_buf] / 3]
    for { set i 0 } { $i < $len } { incr i } {
	set work0 [lindex $tag_buf [expr $i * 3 + 0] ]
	set work1 [lindex $tag_buf [expr $i * 3 + 1] ]
	set work2 [lindex $tag_buf [expr $i * 3 + 2] ]
#puts work0,1,2=$work0,$work1,$work2
	if { [string range $work0 0 1] == "gr" } then {
	    if { [ string index $tk_version 0 ] >= 8 } then {
		#---- ֤ mark 򥻥å
		incr  grno($wno)
		set   gn   $grno($wno)
		.w$wno.frame.txt mark set gr$gn $work1

		#---- 
		set   iname($wno,$gn) [ file tail $work2 ]
#puts iname$wno,$gn=$iname($wno,$gn)
		image create photo $iname($wno,$gn) -file $work2
		.w$wno.frame.txt image create $work1 \
				-image $iname($wno,$gn) -name $gn
	    }
	} else {
	    #---- ꤹ
	    set work3 [lindex $work1 0 ]
	    set work4 [lindex $work1 1 ]
	    set work5 [lindex $work2 0 ]
	    set work6 [lindex $work2 1 ]
	    incr tagno($wno)
	    .w$wno.frame.txt tag add       $tagno($wno) $work3 $work4
	    .w$wno.frame.txt tag configure $tagno($wno) $work5 $work6
	    if { [llength $work2] == 4} then { \
		set work7 [lindex $work2 2 ]
		set work8 [lindex $work2 3 ]
		.w$wno.frame.txt tag configure $tagno($wno) $work7 $work8
	    }
	}
    }

    #---- ƥȥܥåطʿʿ
    eval .w$wno.frame.txt configure -background $org_bg
    eval .w$wno.frame.txt configure -foreground $org_fg

    #---- Сɽ
    if { $org_sl == "S" } \
    then { CreateScbar .w$wno
	   set sb_flag(.w$wno) S } \
    else { set sb_flag(.w$wno) N }

    #---- ɥĻˤʤäƤ顢ơ֥򹹿
    tkwait visibility .w$wno
    Update_ConfList   .w$wno
}
#--------------------------------------------------------------------------
# 䵤
#--------------------------------------------------------------------------
proc RestoreFusen { FUSENNO } {
    global    wsts_list  conf_list
    global    deftitle   oldtitle  sb_flag  wTOP  tagno

    set i $FUSENNO
    set j [ expr $i -1 ]
    set cur_title    [ lindex $conf_list [ expr $j * 5 + 0 ] ]
    set cur_geometry [ lindex $conf_list [ expr $j * 5 + 1 ] ]
    set cur_bg       [ lindex $conf_list [ expr $j * 5 + 2 ] ]
    set cur_fg       [ lindex $conf_list [ expr $j * 5 + 3 ] ]
    set cur_sl       [ lindex $conf_list [ expr $j * 5 + 4 ] ]

    if { [string range $cur_title 0 4 ] == "Fusen" } then  {
	set cur_title Fusen$i
	set work [lreplace $conf_list [expr $j * 5] [expr $j * 5] $cur_title ]
	set conf_list $work
    }

    #---- ֹΥɥ
    toplevel  .w$i

    #---- ɥΥȥ
    wm geometry .w$i $cur_geometry

    set deftitle(.w$i) Fusen$i
    set oldtitle(.w$i) $cur_title
    set work           $cur_title
    wm  title .w$i     $cur_title
    .w$i configure -cursor hand2

    #---- ƥȥܥå
    MakeText  .w$i

    #---- ƥȥܥåطʿʿ
    eval .w$i.frame.txt configure -background $cur_bg
    eval .w$i.frame.txt configure -foreground $cur_fg

    #---- οطʿ˱ƹޤˤ
    set rgb [ winfo rgb .w$i.frame.txt $cur_bg ]
    set r [lindex $rgb 0]
    set g [lindex $rgb 1]
    set b [lindex $rgb 2]
    if { [expr (0.25 * $r + 0.5 * $g + 0.25 * $b)] > 127 } \
    then {eval .w$i.frame.txt configure -insertbackground black -insertwidth 2}\
    else {eval .w$i.frame.txt configure -insertbackground white -insertwidth 2}

    #---- ƥȥեƤƥȥܥåɤ߹
    set   ftxt_fileID [ open ~/.tkfusen/fusen$i.txt r ]
    set   contents    [ read  -nonewline $ftxt_fileID ]
    close $ftxt_fileID
    .w$i.frame.txt insert 1.0 $contents

    tkwait visibility .w$i

    #---- /mark °եɤǡȲɽ
    LoadTag $i

    #---- ݥåץåס˥塼
    MakeMenu  .w$i

    #---- Сɽ
    if { $cur_sl == "S" } \
    then { CreateScbar .w$i
	   set sb_flag(.w$i) S } \
    else { set sb_flag(.w$i) N }

    # ȥΤɽ (ơ:2)
    if { [ lindex $wsts_list $i ] == 2 } then {
	set cur_width    [ winfo width  .w$i ]
	set cur_rootx    [ winfo rootx  .w$i ]
	set cur_rooty    [ winfo rooty  .w$i ]
	set new_geometry [ format "%sx%d+%s+%s" \
			      $cur_width 0 $cur_rootx $cur_rooty ]
	wm geometry .w$i $new_geometry
    }

    # 䵤򥢥 (ơ:3)
    if { [ lindex $wsts_list $i ] == 3 } then { wm iconify .w$i }

    # ---- 䵥ɥ򥢥󲽤ϡ"䵥"򤹤
    bind .w$i.frame.txt <Unmap> { regexp {.w[0-9]*} %W w_icon
				  SaveText          $w_icon
				  Update_ConfList   $w_icon
				  Change_StatusList $w_icon 3 }

    # ---- 䵥ɥĤϡ"䵤򱣤"򤹤
    wm protocol .w$i WM_DELETE_WINDOW "HideFusen .w$i"
    wm protocol .w$i WM_SAVE_YOURSELF "HideFusen .w$i"
}
#--------------------------------------------------------------------------
# λ䵤
# âơ1ɽ١2ȥΤɽ١3󲽡٤Τ
#--------------------------------------------------------------------------
proc RestoreAll {} {
    global    conf_list  wsts_list  wno

    set fusen_count [ expr [ llength $conf_list ] / 5 ]
    set wno         $fusen_count
    for { set i 1 } { $i < [ expr $fusen_count + 1 ] } { incr i } {
	if { [ expr { [ lindex $wsts_list $i ] == 1 } || \
		    { [ lindex $wsts_list $i ] == 2 } || \
		    { [ lindex $wsts_list $i ] == 3 } ] } then {
	    RestoreFusen $i
	}
    }
}
#--------------------------------------------------------------------------
# 䵥ơѹ׵
#     (0) ɽ(1) ȥΤ(2) (3) (4) 
#     ׵ȸߤΥơȹ礻˱
#--------------------------------------------------------------------------
proc StausChangeRequest { REQNO FNUMBER } {
    global    wsts_list  conf_list

    set cur_status [ lindex $wsts_list $FNUMBER ]
    switch $REQNO {
	1 { switch $cur_status {
		0 -
		4  { if { [ winfo exists .w$FNUMBER ] } \
		     then { wm deiconify .w$FNUMBER }   \
		     else { RestoreFusen   $FNUMBER }
		   }
		1  {}
		2  { set idx [ expr $FNUMBER -1 ]
		     set org_geometry [ lindex $conf_list [expr $idx * 5 + 1] ]
		     set work1 [ expr [ string first "x" $org_geometry ] + 1 ]
		     set work2 [ expr [ string first "+" $org_geometry ] - 1 ]
		     set org_height [string range $org_geometry $work1 $work2]
		     set cur_width    [ winfo width .w$FNUMBER ]
		     set cur_rootx    [ winfo rootx .w$FNUMBER ]
		     set cur_rooty    [ winfo rooty .w$FNUMBER ]
		     set new_geometry [ format "%sx%s+%s+%s" \
				$cur_width $org_height $cur_rootx $cur_rooty ]
		     wm geometry  .w$FNUMBER $new_geometry
		    }
		3  { wm deiconify .w$FNUMBER
#		     set idx [ expr $FNUMBER -1 ]
#		     set org_geometry [ lindex $conf_list [expr $idx * 5 + 1] ]
#		     wm geometry  .w$FNUMBER $org_geometry
		   }
	        default {}
	    }
	    Change_StatusList .w$FNUMBER 1
          }

        2 { switch $cur_status {
	        1  { OnlyTitle .w$FNUMBER }
	        2  {}
	        3 -
	        4 -
		0  { wm deiconify .w$FNUMBER
		     OnlyTitle .w$FNUMBER }
		default {}
	    }
	    Change_StatusList .w$FNUMBER 2
	  }

	3 { switch $cur_status {
		1  { Iconify .w$FNUMBER }
		2  { Iconify .w$FNUMBER }
		3  {}
		4 -
		0  { wm deiconify .w$FNUMBER
		     Iconify      .w$FNUMBER }
		default {}
	    }
	    Change_StatusList .w$FNUMBER 3
	  }

	4 { switch $cur_status {
		1 -
		2 -
		3  { HideFusen .w$FNUMBER }
		4  {}
		0  {}
		default {}
	    }
	    Change_StatusList .w$FNUMBER 4
	  }

        0 { switch $cur_status {
		1 -
		2 -
		3  { RmFusen .w$FNUMBER }
		4  {}
		0  {}
		default {}
	    }
	    Change_StatusList .w$FNUMBER 0
	  }

	default {}
    }
}
#--------------------------------------------------------------------------
# Ƥ䵤 (0) ɽ(1) ȥΤ(2) (3) (4)
#--------------------------------------------------------------------------
proc StausChangeAll { REQNO Fcount } {
    global    wno

    for { set f 1 } { $f <= $Fcount } { incr f } {
	#---- 䵾֤бܥɽ
	.fs.but$REQNO$f select
    }
    for { set f 1 } { $f <= $wno } { incr f } {
	StausChangeRequest $REQNO $f
    }
}
#--------------------------------------------------------------------------
# 䵾֤ɽä
#--------------------------------------------------------------------------
proc FS_clear {} {
    for { set y 0 } { $y <= 10 } { incr y } {
	destroy .fs.lbl$y
	destroy .fs.but1$y
	destroy .fs.but2$y
	destroy .fs.but3$y
	destroy .fs.but4$y
	destroy .fs.but0$y
    }
    destroy .fs.lblfno
    destroy .fs.upbut
    destroy .fs.downbut
    destroy .fs.close
}
#--------------------------------------------------------------------------
# 䵾֤ɽ
#--------------------------------------------------------------------------
proc FS_disp { fsd_no } {
    global    wno  wsts_list  conf_list  FSDno

    if { $wno <= 10 } then { set roopctr $wno
			    set fn      $fsd_no } \
		     else { set roopctr 10 
			    if { [expr $wno - $fsd_no] > 9 } \
			    then { set fn $fsd_no } \
			    else { set fn [expr $wno - 9] }
		    }
    for { set y 0 } { $y <= $roopctr } { incr y } {
	set ty [ expr $fn + $y - 1 ]
	set cy [ expr $fn + $y ]

	#---- 䵤Υȥɽ
	set  m  .fs.lbl$y
	if { $y == 0 } \
	then { set cur_title Ƥ
	       label $m -text $cur_title -fg red} \
	else { set cur_title [ lindex $conf_list [expr $ty * 5 + 0] ]
	       label $m -text $cur_title }
	grid  $m -column 0 -row $y

	if { $y == 0 } \
	then { set cmd1 [ list StausChangeAll 1 $roopctr ] } \
	else { set cmd1 [ list StausChangeRequest 1 $cy ] }
	set  m .fs.but1$y
	radiobutton $m -text "ɽ" \
		       -variable fsts$y -value 1 -command $cmd1
	grid $m -column 1 -row $y

	if { $y == 0 } \
	then { set cmd2 [ list StausChangeAll 2 $roopctr ] } \
	else { set cmd2 [ list StausChangeRequest 2 $cy ] }
	set  m .fs.but2$y
	radiobutton $m -text "ȥ" \
		       -variable fsts$y -value 2 -command $cmd2
	grid $m -column 2 -row $y

	if { $y == 0 } \
	then { set cmd3 [ list StausChangeAll 3 $roopctr ] } \
	else { set cmd3 [ list StausChangeRequest 3 $cy ] }
	set  m .fs.but3$y
	radiobutton $m -text "" \
		       -variable fsts$y -value 3 -command $cmd3
	grid $m -column 3 -row $y

	if { $y == 0 } \
	then { set cmd4 [ list StausChangeAll 4 $roopctr ] } \
	else { set cmd4 [ list StausChangeRequest 4 $cy ] }
	set  m .fs.but4$y
	radiobutton $m -text "" \
		       -variable fsts$y -value 4 -command $cmd4
	grid $m -column 4 -row $y

	if { $y == 0 } \
	then { set cmd0 [ list StausChangeAll 0 $roopctr ] } \
	else { set cmd0 [ list StausChangeRequest 0 $cy ] }
	set  m .fs.but0$y
	radiobutton $m -text "" \
		       -variable fsts$y -value 0 -command $cmd0
	grid $m -column 5 -row $y

	#---- 䵤ξ֤бܥɽ
	if { $y != 0 } then {
	    set fsno [ lindex $wsts_list [ expr $fn + $y ] ]
	    .fs.but$fsno$y select
 	}
    }

    label  .fs.lblfno  -text "䵿: $wno" -fg blue
    button .fs.upbut   -text "Up" \
		       -command { if { $FSDno > 0 } \
				  then { FS_clear
					 set FSDno [expr $FSDno - 1]
					 FS_disp $FSDno }
				}
    button .fs.downbut -text "Down" \
		       -command { if { [expr $wno - $FSDno] > 10 } \
				  then { FS_clear
					 set FSDno [expr $FSDno + 1]
					 FS_disp $FSDno }
				}
    button .fs.close -text "Ĥ" -command { destroy .fs }

    grid   .fs.lblfno  -column 6 -row 0 -sticky news
    grid   .fs.upbut   -column 6 -row 1 -sticky news
    grid   .fs.downbut -column 6 -row 2 -sticky news
    grid   .fs.close   -column 6 -row 3 -sticky news
}
#--------------------------------------------------------------------------
# 䵾֤ɽѹ
#--------------------------------------------------------------------------
proc FusenStatus {} {
    global FSDno

    if { [ winfo exists .fs ] } then { return }
    toplevel .fs
    wm title .fs "Fusen Status"
    set      FSDno 0
    FS_disp $FSDno
}
#--------------------------------------------------------------------------
# λ
#--------------------------------------------------------------------------
proc End_Process {} {
    global    wno  oldtitle  wsts_list  conf_list

    #---- "ɽ"  "ȥΤ" 䵤ϡƥȤ򥻡
    for { set i 1 } { $i < [expr $wno + 1] } { incr i } {
	set fs [lindex $wsts_list $i]
	if { $fs == 1 } then { \
#	    SaveText        .w$i
	    Update_ConfList .w$i
	}
	if { $fs == 2 } then { \
#	    SaveText        .w$i
	    Update_ConfList .w$i
	}
    }
    #---- ƥȤΥե̾ȥΥե̾ѹ
    set j 1
    for { set i 1 } { $i <= $wno } { incr i } {
	set fs [lindex $wsts_list $i]
	if { $fs == 0 } then { exec rm fusen$i.txt 
			       exec rm fusen$i.tag } \
	elseif { $i != $j } then { exec mv fusen$i.txt fusen$j.txt
				   exec mv fusen$i.tag fusen$j.tag
				   incr j } \
			    else { incr j }
    }
    #---- ơꥹȤȡơ֥ľ
    cd ~/.tkfusen
    set new_wsts 0
    set new_conf ""
    for { set i 0 } { $i < $wno } { incr i } {
        set j  [ expr $i + 1 ]
	if { [ lindex $wsts_list $j ] != 0 } then { \
	    lappend new_wsts [ lindex $wsts_list $j ]
	    set work [ lrange $conf_list [expr $i * 5 +0] [expr $i * 5 +4] ]
	    set new_conf [ concat $new_conf $work ]
	}
    }
    #---- ơ֥䵥ȥѹ
    #---- "Fusen??"  "Fusen" ѹʳΤΤѹʤ
    for { set i 0 } { $i < $wno } { incr i } {
	set cur_title [ lindex $new_conf [ expr $i * 5 ] ]
	if { [string range $cur_title 0 4 ] == "Fusen" } then  {
	    set work [ lreplace $new_conf [expr $i * 5] [expr $i * 5] Fusen ]
	    set new_conf $work
	}
    }
    #---- ơ֥򡢥ե˳Ǽ
    set    conf_fileID [ open ~/.tkfusen/fusen_conf w ]
    puts  $conf_fileID $new_conf
    close $conf_fileID

    #---- ơꥹȤ򡢥ե˳Ǽ
    set    fsts_fileID [ open ~/.tkfusen/fusen_status w ]
    puts  $fsts_fileID $new_wsts
    close $fsts_fileID

###    cd   ~/.tkfusen
###    exec rm .lock
    exit
}
#--------------------------------------------------------------------------
# ᥤ
#--------------------------------------------------------------------------
    # ɥƥƵưˡTk䵡פ³ư
    wm command . "$argv0 $argv"

    # ưΥǥ쥯ȥ򥻡
    set org_pwd [pwd]

    # .lock եŵưɻߤ
    cd ~
    if { ![ file isdirectory ~/.tkfusen ] } then { \
	exec mkdir .tkfusen
    }
    cd ~/.tkfusen

###    if { [file exists .lock ] } then { \
###	 puts "Tkfusen may be already excuted, otherwise remove the .lock file as follows."
###	 puts "    rm ~/.tkfusen/.lock \n"
###	 exit } \
###    else { exec touch .lock }

    wm title    . "TkFusen"
    wm geometry .
    .  configure -cursor hand2
    button .fsbut -text 䵾 -command FusenStatus
    pack   .fsbut

    # ---- 䵥ȥ륦ɥɽΤԤ
    tkwait visibility .

    # ---- (ɥ)ơեƤꥹȤɤ߹
    # ---- ̵ϡ(ɥ)ơꥹȤ
    if { [file exists ~/.tkfusen/fusen_status ] } \
    then { set   fsts_fileID [ open ~/.tkfusen/fusen_status r ]
	   set   wstatus     [ read -nonewline $fsts_fileID ]
	   set   wsts_list   [ split $wstatus ]
	   close $fsts_fileID } \
    else { set wsts_list 0 }

    # ---- ե뤬¸ߤϡƤ˱䵤
    # ---- ̵ϡ(ɥ)ֹơ֥
    # ---- 䵤򣱤ĺ롣
    if { [ llength $wsts_list ] > 1 } then { \
	if [file exists ~/.tkfusen/fusen_conf ] \
	then { set    conf_fileID [ open ~/.tkfusen/fusen_conf r ]
	       set    conf_work   [ read  $conf_fileID ]
	       set    conf_list   [ split $conf_work ]
	       close $conf_fileID
	       RestoreAll } \
	else { set wno 0
	       set conf_list ""
	       NewFusen
	}
    } \
    else { set wno 0
	   set conf_list ""
	   NewFusen
    }

    # ---- ޥ(裳)ܥ󤬲줿顢ɤ widget ǲ줿
    # ---- wBR3 ˥åȤ롣ݥåץåץ˥塼ɽ롣
    bind all <ButtonRelease-3> { 
	set x [ expr [ winfo rootx %W ] + %x ]
	set y [ expr [ winfo rooty %W ] + %y ]
	set wBR3 %W
	regexp {.w[0-9]*} %W wTOP
	if { [ string last .frame.txt %W ] != -1 } then { \
	    tk_popup  $wTOP.popup $x $y
	}
    }

    # ---- ᥤ󥦥ɥĤ˽λ򤹤롣
    # ---- ɥޥ͡㤫 ץȥ׵ WM_DELETE_WINDOW
    # ---- å򲣼ꤷ End_Process ¹Ԥ롣
    wm protocol . WM_DELETE_WINDOW End_Process
#--------------------------------------------------------------------------
