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

#=============================================================================
# Tkversion 1.7 for Unix/Windows/Macintosh
#
#          ̾ tkhonyaku
#    ɬ  ե롧 tkhonyaku-readme tkhonyaku.gif colors.gif
#    ե롧 tkhonyaku.conf honyaku.txt error.log respons.bin
#            졧 Tcl/Tk ܸ (Tcl/Tk 8.0.5jp)
#      ȯ       LASER5 Linux 6.2 + Tcl/Tk 8.0.5jp
#                       Windows 2000 [RC2] + Tcl/Tk 8.4.a1
#               2001ǯ512
#                ԡ ҡ <kawagisi@yk.rim.or.jp>
#=============================================================================

#--------------------------------------------------------------------------
# ǥե
#     Ķȹߤ˹碌 set xxxx yyyy  yyyy ΤȤƲ
#
# աϽ硢ե ~/.tkhonyaku/tkhonyaku.conf
#           ϺƤʤѹȿǤޤ
#--------------------------------------------------------------------------
proc SetDefault {} {

  global  disp_debug
  global  tcl_platform env username server port
  global  txtin_bg txtin_fg txtout_bg txtout_fg
  global  style ximstyle win_dir ejmode jemode my_osk my_omk auto_dic

  set disp_debug no         ;# ǥХåѥɥɽʤ
  switch $tcl_platform(platform) {
    unix      { set username $env(USER) }  ;# 桼̾
    windows   { set username winuser    }  ;# 桼̾
    macintosh { set username macuser    }  ;# 桼̾
  }
  set server    localhost   ;# ФΥۥ̾
  set port      2744        ;# ݡֹ
  set txtin_bg  white       ;# ʸطʿ
  set txtin_fg  blue        ;# ʸʸ
  set txtout_bg cornsilk    ;# ʸطʿ
  set txtout_fg black       ;# ʸʸ
  set style       "A"       ;# ʸɽ ("A" : , "O" : ʸΤ)
  set ximstyle    1         ;# Ѵ
                            ;# 1: Over the Spot(1)   3: Off the Spot 
                            ;# 2: Over the Spot(2)   4: root ɥ
  set win_dir     "V"       ;# ɥ ("V" : , "H" : )
  set ejmode      "DKW"     ;# Ѵư⡼
                            ;#   C = ʸʸ̤ʤ
                            ;#   O = ϤνǤ¸
                            ;#   I = ̿ʿʸȤư
                            ;#   D = ֤ǤޤĴˤ
                            ;#   K = ʸ֡פǤĤʤ
                            ;#   W = ñؽԤ
                            ;#   P = ʻؽԤ

                            ;# ѱѴư⡼
  set jemode "10110:I,we,you,it,he,she,they:0: ,it,that,you,them,us,me,him,her"
  set my_osk ""             ;# 䤦θ (¾)
  set my_omk ""             ;# 䤦Ūθ (¾)

  #  nnnnm:s0,s1,....sx:m:s0,s1,....sx
  #  ||||| |            | |
  #  ||||| |            | +- 䤦ŪθʸΥꥹ(ޤǶڤ)
  #  ||||| |            |    (ϡʤȤ򼨤)
  #  ||||| |            +- 䤦ŪȤƲܤθѤ뤫 
  #  ||||| |               (0Ϥޤ)
  #  ||||| +- 䤦θʸΥꥹ(ޤǶڤ)
  #  ||||+- 䤦ȤƲܤθѤ뤫 (0Ϥޤ)
  #  |||+- ֡Ƥפ(ʸ'1','2','3'Τɤ줫) 
  #  ||+- άΰ(ʸ'1','2','3'Τɤ줫)
  #  |+- not û̷ ('1')/ û̷ˤʤ('0')
  #  +- ؽѤ('1')Ѥʤ('0')
  #
  #  n : ο
  #  m : ʣο
  #  s0, s1,,, sx : ñ
  #
  #  άΰ:
  #          '1'  m  s? ǻꤵ줿䤤ޤ
  #          '2'  ư֤ޤ
  #          '3'  ̿ޤ
  #
  #  ֡Ƥפ:
  #          '1'  ߷ޤ
  #          '2'  ߿ʹԷޤ
  #          '3'  λޤ
  set auto_dic  OFF         ;# 򤵤줿ϰϤưŪ˼(ON/OFF)
}
#--------------------------------------------------------------------------
# ᥤ (1)
#--------------------------------------------------------------------------
  switch $tcl_platform(platform) {
    unix {
      set respdir   ~/.tkhonyaku/           ;# 쥹ݥ󥹥եΥǥ쥯ȥ
      set logdir    ~/.tkhonyaku/           ;# 顼Υǥ쥯ȥ
      set conf_file ~/.tkhonyaku/tkhonyaku.conf ;# եΥեѥ
      set helpdir   /usr/doc/tkhonyaku-1.7  ;# إץեΥǥ쥯ȥ
      set gifdir    /usr/local/tkhonyaku    ;# GIF եΥǥ쥯ȥ
      switch $tcl_platform(os) {
        FreeBSD {
          set helpdir /usr/local/share/doc/tkhonyaku
          set gifdir  /usr/local/share/tkhonyaku
        }
      }
      if { ! [file exists $helpdir] } {
          if { [file type $argv0] == "link" } {
              set helpdir [file dirname [file readlink $argv0] ]
          } else {
              set helpdir [file dirname $argv0]
          }
      }
      if { ! [file exists $gifdir] } {
          if { [file type $argv0] == "link" } {
              set gifdir [file dirname [file readlink $argv0] ]
          } else {
              set gifdir [file dirname $argv0]
          }
      }
    }
    windows {
      set directory [string trimright [info script] \\tkhonyaku.tcl]
      set respdir   $directory              ;# 쥹ݥ󥹥եΥǥ쥯ȥ
      set logdir    $directory                 ;# 顼Υǥ쥯ȥ
      set conf_file $directory\\tkhonyaku.conf ;# եΥեѥ
      set helpdir   $directory                 ;# إץեΥǥ쥯ȥ
      set gifdir    $directory                 ;# GIF եΥǥ쥯ȥ
    }
    macintosh {
      set directory [string trimright [info script] :tkhonyaku.tcl]
      set respdir   $directory             ;# 쥹ݥ󥹥եΥǥ쥯ȥ
      set logdir    $directory                 ;# 顼Υǥ쥯ȥ
      set conf_file $directory:tkhonyaku.conf  ;# եΥեѥ
      set helpdir   $directory                 ;# إץեΥǥ쥯ȥ
      set gifdir    $directory                 ;# GIF եΥǥ쥯ȥ
    }
  }
  #--------------------------------------------------------------------------
  # 鲼ϡʤǲ
  #--------------------------------------------------------------------------
  set tkh_version "1.7"      ;# Tk٤ΥС

  set HEAD             [binary format "I" 0xaabbccdd] ;# إå
  set PROTOCOL_VERSION [binary format "I" 0x0000e001] ;# ץȥС
  set EG_INIT          [binary format "I" 0x00f00001] ;# ѳ
  set EG_END           [binary format "I" 0x00f00002] ;# ѽλ
  set EG_TRANSLATE_ONE [binary format "I" 0x00f00004] ;# ʸ
  set EG_BROWSE_DICT   [binary format "I" 0x00f00015] ;# 񻲾
  set EG_GETEQUIV      [binary format "I" 0x00f00007] ;# б
  set EG_GETWORD       [binary format "I" 0x00f00008] ;# ꥹȤμ
  set EG_SETWORD       [binary format "I" 0x00f00009] ;# ѹ
  set EG_GETHINSHI     [binary format "I" 0x00f0000a] ;# ʻꥹȤμ
  set EG_SETHINSHI     [binary format "I" 0x00f0000b] ;# ʻѹ
  set EG_GETTRNTXT     [binary format "I" 0x00f0000c] ;# ʸ֤
  set EG_SETLEARN      [binary format "I" 0x00f0000e] ;# ؽ
  set EG_SETMODE       [binary format "I" 0x00f00011] ;# ⡼ɤΥå
  set EG_SETDICTNAME   [binary format "I" 0x00f00013] ;# Ѽ
  set EG_CLEAR_CACHE   [binary format "I" 0x00f00014] ;# 񥭥åΥꥢ
  set TRNS_GET_STATUS  [binary format "I" 0x00f0001a] ;# Сơ
  set TRNS_DIVIDE_SENTENCE [binary format "I" 0x00f0001b] ;# ʸʬ
  set EG_SET_TIMEOUT   [binary format "I" 0x00f0001c] ;# ॢȤ

  set EJ               [binary format "I" 0x00000001] ;# Ѵ
  set JE               [binary format "I" 0x00000002] ;# Ѵ
  set LOCK_WAITTIME    30     ;# եåॢ
  set TRANS_WAITTIME   30     ;# ॢ

#--------------------------------------------------------------------------
# 顼å
#--------------------------------------------------------------------------
proc HonyakuError { errorno } {

  switch -- $errorno {
    -114  { set errmsg "EOF_or_Error_on_socket_to_client" }
    -126  { set errmsg "First_command_is_not_init" }
    -137  { set errmsg "Not_initialized_for_ej" }
    -139  { set errmsg "Not_initialized_for_je" }
    -141  { set errmsg "Already_initialized_for_ej" }
    -143  { set errmsg "Already_initialized_for_je" }
    -202  { set errmsg "Unknown_command" }
    -206  { set errmsg "ʸޤʸĹޤʸûڤäƺƤ" }
    -300  { set errmsg "Ф³ǤޤǤ" }
    -400  { set errmsg "եxxxܤ̵ʥɤꤵƤޤ"}
    -401  { set errmsg "եxxxܤ˽ʣxxxꤵƤޤ"}
    -402  { set errmsg "եxxxܤ̵ͤꤵƤޤ"}
    -500  { set errmsg "ɥΥ᤮ޤ" }
    -501  { set errmsg "ФȤ̿̿Ū顼ȯޤ" }
    -502  { set errmsg "ꤵ줿֤ˤñ줬¸ߤޤ" }
    -503  { set errmsg "䤬ޤ" }
    -600  { set errmsg "饤󥹤ǤޤǤ" }
    -1003 { set errmsg "ʸϤ˼ԤޤʸƺƤ" }
    -1012 { set errmsg "ʸΤΰ֤ˤñ줬¸ߤޤ" }
    -3000 { set errmsg "ǻ֤᤮ƥॢȤȯޤ" }
    -3004 { set errmsg "Ʊ桼γؽΥåǥॢȤȯޤ" }
    -4000 { set errmsg "ʸޤʸĹޤʸûڤäƺƤ" }
    -4002 { set errmsg "̤ޤѸ켭ǧƤ" }
    -4003 { set errmsg "̤ޤѸ켭ǧƤ" }
    -4004 { set errmsg "ޤƤޤ" }
    -4016 { set errmsg "˻ꤷñ줬ޤ" }
    -4017 { set errmsg "ɤ߹ߤǤޤǤ" }
    default { set errmsg "¾Υ顼" }
  }
  return  "顼 $errorno \n$errmsg"
}
#--------------------------------------------------------------------------
# ΥФ³ʥåȤ򳫤
#--------------------------------------------------------------------------
proc Connect_Server {} {

  global server port socketid use_server tcl_ver_flag

  if { [info exists socketid] && ($socketid != "") } {
          host_close            ;# ѽλ
          after 1000
  }
  set use_server 1              ;# лե饰򥻥å
  set socketid [socket $server $port]
  if { $tcl_ver_flag == "8.0" } {
      fconfigure $socketid -buffering none -translation binary
  } else {
      fconfigure $socketid -buffering none -translation binary -encoding binary
  }
  return $socketid
}
#--------------------------------------------------------------------------
# ꥯ
#
#     ֤
#       0 = 
#       1 = 쥹ݥ̵
#       2 = إåְäƤ ( != 0xaabbccdd)
#       3 = ơ 0()ǤϤʤ
#       4 = $socketid ̵
#
#       response : СΥ쥹ݥ
#--------------------------------------------------------------------------
proc request_send { cmd } {

  global  HEAD EG_END EJ JE socketid response status mode disp_debug
  global  tcl_ver_flag

  if {$disp_debug == "yes"} {
    .deb.txt insert end "socketid = $socketid \n" ;# -- for DEBUG ---
    binary scan $cmd "H*" cmdhex                  ;# -- for DEBUG ---
    .deb.txt insert end "command  = $cmdhex \n"   ;# -- for DEBUG ---
  }
  if { $socketid == "" } then { return 4 } 

  puts -nonewline $socketid $cmd

  binary scan [read $socketid 4] "H8" header
  if { [string length $header] == 0 } then { return 1 }
  if { $header != "aabbccdd" }        then { return 2 } 

  binary scan [read $socketid 4] "I" length
  binary scan [read $socketid 4] "I" status

  if {$disp_debug == "yes"} {
    .deb.txt insert end "length   = $length \n"   ;# -- for DEBUG ---
    .deb.txt insert end "status   = $status \n"   ;# -- for DEBUG ---
  }

  if { $status < 0 } then {
      tk_messageBox -parent . -title "TkHonyaku Error" -icon error -type ok \
                    -message  [HonyakuError $status]
      set  hdr_bin $HEAD
      set  len_bin [ binary format "I" 0x00000008 ]    ;# length = 8
      set  req_bin $EG_END
      if { $mode == "ej" } then { set para1 $EJ } \
                           else { set para1 $JE }
      append cmd $hdr_bin $len_bin $req_bin $para1
      puts -nonewline $socketid $cmd
      binary scan [read $socketid 4] "H8" header
      binary scan [read $socketid 4] "I"  length
      binary scan [read $socketid 4] "I"  status
      return 3
  }
  if { $length > 4 } {
    if { $tcl_ver_flag == "8.0" } {
        set response [ read $socketid [ expr $length -4 ]]
    } else {
        set response [string trimright [encoding convertfrom euc-jp \
                     [ read $socketid [ expr $length -4 ]]] \x00 ]
    }
    if {$disp_debug == "yes"} {
      binary scan $response "H*" responsehex            ;# -- for DEBUG ---
      .deb.txt insert end "response = $responsehex \n"  ;# -- for DEBUG ---
    }
  }
  return 0
}
#----------------------------------------------------------------------------
# ѳ (󥸥ν)
#----------------------------------------------------------------------------
proc init {} {

  global  HEAD EG_INIT PROTOCOL_VERSION EJ JE mode username

  #---- ޥɤ ----
  set  hdr_bin $HEAD
  set  req_bin $EG_INIT
  if { $mode == "ej" } then { set para1 $EJ } \
                       else { set para1 $JE }
  set    para2 $PROTOCOL_VERSION
  append para3 $username \x00
  set    len_bin [ binary format "I" [ expr 12 + [string length $para3]]]
  append cmd $hdr_bin $len_bin $req_bin $para1 $para2 $para3
  return [ request_send $cmd ]
}
#----------------------------------------------------------------------------
# ⡼ɤΥåȡ
#----------------------------------------------------------------------------
proc setmode {} {

  global  HEAD EG_SETMODE EJ JE mode ejmode jemode

  set    hdr_bin $HEAD
  set    req_bin $EG_SETMODE
  if { $mode == "ej" } then { set para1 $EJ
                              append para2 $ejmode \x00 } \
                       else { set para1 $JE
                              append para2 $jemode \x00 }
  append cmd2 $req_bin $para1 $para2
  set    len_bin [ binary format "I" [ string length $cmd2 ]]
  append cmd $hdr_bin $len_bin $req_bin $para1 $para2
  return [ request_send $cmd ]
}
#----------------------------------------------------------------------------
# ॢȤꡣ
#----------------------------------------------------------------------------
proc set_timeout {} {

  global HEAD EG_SET_TIMEOUT LOCK_WAITTIME TRANS_WAITTIME EJ JE mode

  set    hdr_bin $HEAD
  set    req_bin $EG_SET_TIMEOUT
  if { $mode == "ej" } then { set para1 $EJ } \
                       else { set para1 $JE}
  set    para2  [ binary format "I" $LOCK_WAITTIME  ]
  set    para3  [ binary format "I" $TRANS_WAITTIME ]
  append cmd2 $req_bin $para1 $para2 $para3
  set   len_bin [ binary format "I" [ string length $cmd2 ]]
  append cmd $hdr_bin $len_bin $req_bin $para1 $para2 $para3
  return [ request_send $cmd ]
}
#----------------------------------------------------------------------------
# Ѽ
#
#     setdictname { computer_dict_use = 1, dict_learning = 1 }
#         computer_dict_use: ԥ塼缭 (Ի:0)
#         dict_learning:     ؽ (Ի:0)
#     ֤(String)ϼμ̡
#----------------------------------------------------------------------------
proc setdictname {} {

  global  HEAD EG_SETDICTNAME EJ JE mode username

  set    hdr_bin $HEAD
  set    req_bin $EG_SETDICTNAME
  if { $mode == "ej" } then { set para1 $EJ } \
                       else { set para1 $JE}
  set    para2  [ binary format "I" 0x00000001 ]    ;# ԥ塼缭
  set    para3  [ binary format "I" 0x00000001 ]    ;# ؽ񼭽
  set    para4  [ binary format "I" 0x00000003 ]    ;# Ѥ뼭ο
  set    para5  [ binary format "S" 0x0003 ]        ;# Ѹ켭
  append para6  "C" "C" "computer" \x00             ;#   ID, ̾
  set    para7  [ binary format "S" 0x0000 ]        ;# ƥ༭
  append para8  "S" "S" "dummy"    \x00             ;#   ID, ̾
  set    para9  [ binary format "S" 0x0004 ]        ;# ؽ
  append para10 "L" "L" $username \x00              ;#   ID, ̾

  append cmd2 $req_bin $para1 $para2 $para3 $para4 $para5 $para6 $para7 \
              $para8 $para9 $para10
  set   len_bin [ binary format "I" [string length $cmd2] ]
  append cmd $hdr_bin $len_bin $cmd2
  return [ request_send $cmd ]
}
#----------------------------------------------------------------------------
# ʸʬ
#
#   ֤(ʸޤ) ϡʸʬ䤵줿
#   ʬ䤵줿ʸƬˡΣĤξʸղäޤ
#
#   裱 ʲΣĤɤ줫
#     t,l,i   ɬפʤ
#     " "     ̾ʸ
#     "T"     ȥʸ(ʸʤʸʤ)
#     "L"     վʸ
#
#   裲 ʲΣĤɤ줫)
#     "P"     (ѥ饰)λϤޤʸ
#     " "     Σʸܰʹߤʸ
#     "S"     ơ֥Τ褦ƤơʸȤ
#             ֤ʤɤǶڤ줿ʸ
#----------------------------------------------------------------------------
proc trns_divide_sentence { original } {

  global  HEAD TRNS_DIVIDE_SENTENCE EJ JE mode response tcl_ver_flag

  regsub -all "\n" $original " " noLFstring  ;# ʸβԵ򥹥ڡִ

  set    hdr_bin $HEAD
  set    req_bin $TRNS_DIVIDE_SENTENCE
  if { $mode == "ej" } then { set para1 $EJ } \
                       else { set para1 $JE }
  set    para2  [ binary format "I" 0x00000001 ]     ;# ʸղ
  if { $tcl_ver_flag == "8.0" } {
      append cmd2 $req_bin $para1 $para2 $noLFstring \x00
      set    len_bin [ binary format "I" [ string length $cmd2 ]]
      append cmd $hdr_bin $len_bin $req_bin $para1 $para2 $noLFstring \x00
  } else {
      append cmd2 $req_bin $para1 $para2 \
                  [encoding convertto euc-jp $noLFstring] \x00
      set    len_bin [ binary format "I" [ string length $cmd2 ]]
      append cmd $hdr_bin $len_bin $req_bin $para1 $para2 \
                 [encoding convertto euc-jp $noLFstring] \x00
  }
  return [ request_send $cmd ]
}
#----------------------------------------------------------------------------
# ʸ
#
#     ֤ͤʸ
#----------------------------------------------------------------------------
proc translate_one { sentence } {

    global  mode HEAD EG_TRANSLATE_ONE EJ JE mode tcl_ver_flag

  regsub -all "\n" $sentence " " noLFstring  ;# ʸβԵ򥹥ڡִ

  set  hdr_bin $HEAD
  set  req_bin $EG_TRANSLATE_ONE
  if { $mode == "ej" } then { set para1 $EJ } \
                       else { set para1 $JE}
  set  para2   [ binary format "I" 0x00000000 ]  ;#
  set  para3   [ binary format "I" 0x00001000 ]  ;# ʸѥХåե 4096
  set  para4   [ binary format "S" 0x0000 ]      ;#
  set  para5   [ binary format "S" 0x0000 ]      ;#
  set  para6   [ binary format "I" 0x00000000 ]  ;#
  if { $tcl_ver_flag == "8.0" } {
      append cmd2 $req_bin $para1 $para2 $para3 $para4 $para5 $para6 \
                  $noLFstring \x00
      set    len_bin [ binary format "I" [ string length $cmd2 ]]
      append cmd $hdr_bin $len_bin $req_bin $para1 $para2 $para3 $para4 \
                 $para5 $para6 $noLFstring \x00
  } else {
      append cmd2 $req_bin $para1 $para2 $para3 $para4 $para5 $para6 \
                  [encoding convertto euc-jp $noLFstring] \x00
      set    len_bin [ binary format "I" [ string length $cmd2 ]]
      append cmd $hdr_bin $len_bin $req_bin $para1 $para2 $para3 $para4 \
                 $para5 $para6 [encoding convertto euc-jp $noLFstring] \x00
  }
  return [ request_send $cmd ]
}
#----------------------------------------------------------------------------
# ѽλ
#----------------------------------------------------------------------------
proc host_close {} {

  global  HEAD EG_END EJ JE mode socketid use_server disp_debug

  set  hdr_bin $HEAD
  set  len_bin [ binary format "I" 0x00000008 ]    ;# length
  set  req_bin $EG_END
  if { $mode == "ej" } then { set para1 $EJ } \
                       else { set para1 $JE }
  append cmd $hdr_bin $len_bin $req_bin $para1

  if {$disp_debug == "yes"} {
    .deb.txt insert end "socketid = $socketid \n" ;# -- for DEBUG ---
    binary scan $cmd "H*" cmdhex                  ;# -- for DEBUG ---
    .deb.txt insert end "command = $cmdhex \n"    ;# -- for DEBUG ---
  }
  puts  -nonewline $socketid $cmd
  close $socketid
  set   socketid   ""
  set   use_server 0        ;# лե饰򥯥ꥢ
}
#----------------------------------------------------------------------------
# 񥭥åΥꥢ
#----------------------------------------------------------------------------
proc clear_cache {} {

  global HEAD EG_CLEAR_CACHE EJ JE mode

  set  hdr_bin $HEAD
  set  len_bin [ binary format "I" 0x00000008 ]      ;# length
  set  req_bin $EG_CLEAR_CACHE
  if { $mode == "ej" } then { set para1 $EJ } \
                       else { set para1 $JE}
  append cmd $hdr_bin $len_bin $req_bin $para1
  return [ request_send $cmd ]
}
#----------------------------------------------------------------------------
# Ƥ򻲾ȷ(ֻμפη)Ǽ
#----------------------------------------------------------------------------
proc browse_dict { word } {

  global  HEAD EG_BROWSE_DICT EJ JE mode response tcl_ver_flag

  set  hdr_bin $HEAD
  set  req_bin $EG_BROWSE_DICT
  if { $mode == "ej" } then { set para1 $EJ } \
                       else { set para1 $JE }
  set  para2  [ binary format "I" 0x00001000 ] ;# ʸѥХåե 4096
  if { $tcl_ver_flag == "8.0" } {
      append cmd2 $req_bin $para1 $para2 $word \x00
      set    len_bin [ binary format "I" [ string length $cmd2 ]]
      append cmd $hdr_bin $len_bin $req_bin $para1 $para2 $word \x00
  } else {
      append cmd2 $req_bin $para1 $para2 [encoding convertto euc-jp $word] \x00
      set    len_bin [ binary format "I" [ string length $cmd2 ]]
      append cmd $hdr_bin $len_bin $req_bin $para1 $para2 \
                 [encoding convertto euc-jp $word] \x00
  }
  return [ request_send $cmd ]
}
#----------------------------------------------------------------------------
# б (ñֹ)
#     para2 = 0: ʸñ, 1: ʸñ
#     para3 = ʸ/ʸΥ
#
#          = ñֹ
#     쥹ݥ = б¤
#----------------------------------------------------------------------------
proc getequiv {} {

  global HEAD EG_GETEQUIV EJ JE mode gyflag startcol status wordnumber

  set  hdr_bin $HEAD
  set  len_bin [ binary format "I" 0x00000010 ]  ;# length = 16
  set  req_bin $EG_GETEQUIV
  if { $mode == "ej" } then { set para1 $EJ } \
                       else { set para1 $JE}
  if { $gyflag == "g"} then { set para2 [binary format "I" 0x00000000] } \
                       else { set para2 [binary format "I" 0x00000001] }
  set para3 [binary format "I" $startcol]
  append cmd $hdr_bin $len_bin $req_bin $para1 $para2 $para3

  set ret_sts [ request_send $cmd ]
  set wordnumber $status    ;# ñֹ
  return $ret_sts
}
#----------------------------------------------------------------------------
# ꥹȤ
#----------------------------------------------------------------------------
proc getword {} {

  global HEAD EG_GETWORD EJ JE mode status wordnumber yakugosuu

  set  hdr_bin $HEAD
  set  len_bin [ binary format "I" 0x00000014 ] ;# length = 20
  set  req_bin $EG_GETWORD
  if { $mode == "ej" } then { set para1 $EJ } \
                       else { set para1 $JE}
  set  para2 [ binary format "I" $wordnumber]   ;# ñֹ
  set  para3 [ binary format "I" 0x00001000 ]   ;# Хåե 4096
# set  para4 [ binary format "I" 0x00000000 ]   ;# Ʊʻˡ
  set  para4 [ binary format "I" 0x00000001 ]   ;# Ʊʻˡ
  append cmd $hdr_bin $len_bin $req_bin $para1 $para2 $para3 $para4

  set ret_sts [ request_send $cmd ]
  set yakugosuu $status    ;# 
  return $ret_sts
}
#----------------------------------------------------------------------------
# ѹ
#----------------------------------------------------------------------------
proc setword {} {

  global HEAD EG_SETWORD EJ JE mode wordnumber listitem_num

  set  hdr_bin $HEAD
  set  len_bin [ binary format "I" 0x00000010 ]  ;# length = 16
  set  req_bin $EG_SETWORD
  if { $mode == "ej" } then { set para1 $EJ } \
                       else { set para1 $JE}
  set  para2 [ binary format "I" $wordnumber]    ;# ñֹ
  set  para3 [ binary format "I" $listitem_num ] ;# 򤷤ֹ
  append cmd $hdr_bin $len_bin $req_bin $para1 $para2 $para3
  return [ request_send $cmd ]
}
#----------------------------------------------------------------------------
# ʻꥹȤ
#----------------------------------------------------------------------------
proc gethinsi {} {

  global HEAD EG_GETHINSHI EJ JE mode status wordnumber hinsisuu

  set  hdr_bin $HEAD
  set  len_bin [ binary format "I" 0x00000010 ]  ;# length = 16
  set  req_bin $EG_GETHINSHI
  if { $mode == "ej" } then { set para1 $EJ } \
                       else { set para1 $JE}
  set  para2 [ binary format "I" $wordnumber]    ;# ñֹ
  set  para3 [ binary format "I" 0x00001000 ]    ;# Хåե 4096
  append cmd $hdr_bin $len_bin $req_bin $para1 $para2 $para3

  set ret_sts [ request_send $cmd ]
  set hinsisuu $status    ;# ʻο
  return $ret_sts
}
#----------------------------------------------------------------------------
# ʻѹ
#----------------------------------------------------------------------------
proc setthinsi {} {

  global HEAD EG_SETHINSHI EJ JE mode wordnumber listitem_num

  set  hdr_bin $HEAD
  set  len_bin [ binary format "I" 0x00000010 ]  ;# length = 16
  set  req_bin $EG_SETHINSHI
  if { $mode == "ej" } then { set para1 $EJ } \
                       else { set para1 $JE}
  set  para2 [ binary format "I" $wordnumber]    ;# ñֹ
  set  para3 [ binary format "I" $listitem_num ] ;# 򤷤ʻֹ
  append cmd $hdr_bin $len_bin $req_bin $para1 $para2 $para3
  return [ request_send $cmd ]
}
#----------------------------------------------------------------------------
# ʸ
#----------------------------------------------------------------------------
proc gettrntxt {} {

  global HEAD EG_GETTRNTXT EJ JE mode

  set  hdr_bin $HEAD
  set  len_bin [ binary format "I" 0x0000000c ]  ;# length = 12
  set  req_bin $EG_GETTRNTXT
  if { $mode == "ej" } then { set para1 $EJ } \
                       else { set para1 $JE}
  set  para2 [ binary format "I" 0x00001000 ]    ;# Хåե 4096
  append cmd $hdr_bin $len_bin $req_bin $para1 $para2
  return [ request_send $cmd ]
}
#----------------------------------------------------------------------------
# ػ
#----------------------------------------------------------------------------
proc DisableRetr {} {

    .rt.fr1.ykg  configure -state disabled -fg gray70 ;# (ʸ)
    .rt.fr1.yky  configure -state disabled -fg gray70 ;# (ʸ)
    .rt.fr1.retr configure -state disabled -fg gray70 ;# Ǻ
    .rt.fr2.hkg  configure -state disabled -fg gray70 ;# ʻ(ʸ) 
    .rt.fr2.hky  configure -state disabled -fg gray70 ;# ʻ(ʸ)
    .rt.fr2.retr configure -state disabled -fg gray70 ;# ʻǺ
    .rt.fr4.lab  configure -fg red -text \
                 " Ǥޤ󡣤ΥɥĤƲ"
}
#--------------------------------------------------------------------------
# 䡿ʻ
#     {yhflag : 졿ʻ}
#--------------------------------------------------------------------------
proc GetKouho {yhflag} {

  global  socketid use_server mode respdir response tcl_platform

  if { $socketid == "" } {
      DisableRetr           ;# ػ
      return
  }
  if { [getequiv] != 0 } {  ;# ñֹб¤Τ
      host_close            ;# ѽλ
      DisableRetr           ;# ػ
      return
  }
  binary scan $response "H*" work1   ;# 쥹ݥ󥹤 HEX ʸѴ
  set pequiv ""

  for { set i 0 } { $i < [expr [string length $work1]/4] } { incr i } {
    set work2 [string range $work1 [expr $i * 4] [expr $i * 4 +3]]
    lappend pequiv [format "%d" 0x$work2]
  }
  set gchankctr [lindex $pequiv 0]   ;# ʸΥ󥯿
  set ychankctr [lindex $pequiv 1]   ;# ʸΥ󥯿
  set galltxt   [.rt.fr3.f3l.pw.pane1.in.txt  get 1.0 end] ;# ʸƥ
  set yalltxt   [.rt.fr3.f3l.pw.pane2.out.txt get 1.0 end] ;# ʸƥ
  binary scan $galltxt "H*" gallhex  ;# ʸƥȤ HEX ʸѴ
  binary scan $yalltxt "H*" yallhex  ;# ʸƥȤ HEX ʸѴ
  set gstring ""
  set ystring ""
  for {set i 0} {$i < $gchankctr} {incr i} {
    set getpoint [expr [lindex $pequiv [expr 2+ $i*2]] *2]    ;# getݥ
    set getcount [expr [lindex $pequiv [expr 3+ $i*2]] *2 -1] ;# getХȿ
    set wordhex  [string range $gallhex $getpoint [expr $getpoint + $getcount]]
    lappend gstring [binary format "H*" $wordhex]
  }
  set yajp [expr 2 + 2 * $gchankctr]     ;# yajp=ʸɥ쥹ݥ
  for {set i 0} {$i < $ychankctr} {incr i} {
    set getpoint [expr [lindex $pequiv [expr $yajp +0 + $i*2]] *2]
    set getcount [expr [lindex $pequiv [expr $yajp +1 + $i*2]] *2 -1]
    set wordhex  [string range $yallhex $getpoint [expr $getpoint + $getcount]]
    lappend ystring [binary format "H*" $wordhex]
  }
  .rt.fr3.f3r.lab1 configure -text $gstring  ;# ʸñɽ
  .rt.fr3.f3r.lab2 configure -text $ystring  ;# ʸñɽ

  if { $yhflag == "yakugo" } {
      if { [getword]  != 0 } { return } ;# ꥹȤ
  } else {
      if { [gethinsi] != 0 } { return } ;# ʻꥹȤ
  }

  #--- 䡿ʻꥹȤΥ쥹ݥ󥹤ե˳Ǽ
  set    fname  [ file join $respdir respons.bin ]
  set    fileid [ open $fname w+ ]
  puts  $fileid $response
  set    fsize  [expr [tell $fileid] -1]
  close $fileid

  #--- 쥹ݥ󥹤Ƭ4Х(Υ)򥹥åפФ
  if { $fsize == 0 } { return }
  if { $fsize > 4 } {
      set yakugolist [split [get_output 4] \x00]
  }

  foreach i $yakugolist {               ;# ꥹȥܥåɽ
      set word [string range $i 2 end]
      .rt.fr3.f3r.list insert end $word
  }

  .rt.fr3.f3r.list selection set 0
#  .rt.fr3.f3r.lab2 configure -text [.rt.fr3.f3r.list get 0] ;# ʸñɽ
  set listitem_num 0                    ;# 򤷤ֹ
}
#--------------------------------------------------------------------------
# Ǻ
#--------------------------------------------------------------------------
proc RetrYakugo {} {

  global  socketid response retr_helptext tcl_platform

  if { [.rt.fr3.f3r.lab2 cget -text] == ""} {
       tk_messageBox -parent . -title "TkHonyaku Warning" \
         -icon warning -type ok \
         -message "줬ꤵƤޤ\n\n $retr_helptext"
       return
  }
  if { $socketid == "" } {
      DisableRetr                    ;# ػ
      return
  }
  if { [setword]   != 0 } { return } ;# ѹ
  if { [gettrntxt] != 0 } { return } ;# 
  set output [get_output 0]          ;# 쥹ݥ󥹤Ѵ̤Ф
  if { $tcl_platform(platform) != "windows" } {
      .rt.fr3.f3l.pw.pane2.out.txt insert end \n
  }
  .rt.fr3.f3l.pw.pane2.out.txt insert end $output
  .rt.fr3.f3l.pw.pane2.out.txt see end
}
#--------------------------------------------------------------------------
# ʻǺ
#--------------------------------------------------------------------------
proc RetrHinshi {} {

  global  socketid response retr_helptext tcl_platform

  if { [.rt.fr3.f3r.lab2 cget -text] == ""} {
      tk_messageBox -parent . -title "TkHonyaku Warning" \
        -icon warning -type ok \
        -message "ʻ줬ꤵƤޤ\n\n $retr_helptext"
      return }
  if { $socketid == "" } {
      DisableRetr                    ;# ػ
      return
  }
  if { [setthinsi] != 0 } { return } ;# ʻѹ
  if { [gettrntxt] != 0 } { return } ;# 
  set output [get_output 0]          ;# 쥹ݥ󥹤Ѵ̤Ф
  if { $tcl_platform(platform) != "windows" } {
      .rt.fr3.f3l.pw.pane2.out.txt insert end \n
  }
  .rt.fr3.f3l.pw.pane2.out.txt insert end $output
  .rt.fr3.f3l.pw.pane2.out.txt see end
}
#----------------------------------------------------------------------------
# 쥹ݥ󥹤Ѵ̤Ф
#     Windows  Macintosh ϡEUCɤ饷եJIS Ѵ
#----------------------------------------------------------------------------
proc get_output {skipbytes} {

  global  response respdir tcl_platform 

    #--- ̤Υ쥹ݥ󥹤ե˳Ǽ
    set   fname1  [ file join $respdir respons.bin ]
    set   fileid1 [ open $fname1 w+ ]
    puts $fileid1 $response
    set  fsize  [expr [tell $fileid1] -1]

    #--- ʸξ硢쥹ݥƬ 2ХȤ򥹥å
    #--- ξ硢åפʤ
    if { $fsize == 0 } { return }
    if { $fsize >= 2 } {
      seek $fileid1 $skipbytes
      set output_txt [read $fileid1]
    }
    close  $fileid1
    return $output_txt
}
#----------------------------------------------------------------------------
# 
#----------------------------------------------------------------------------
proc translation {} {

  global  tcl_platform response respdir style mode sel_trans

  Connect_Server                               ;# Ф³
  if { [ init ]        != 0 } { return }       ;# ѳ
  if { [ setdictname ] != 0 } { return }       ;# Ѽ
  if { [ set_timeout ] != 0 } { return }       ;# ॢȤ
  if { [ setmode ]     != 0 } { return }       ;# ⡼ɤΥå
  if { [ clear_cache ] != 0 } { return }       ;# 񥭥åΥꥢ

  if { $sel_trans == "yes" } {                 ;# ϰϤ
      set origintxt [selection get]
      set sel_trans "no"
  } else {                                     ;# ʸڥƤ
      set origintxt [.pw.pane1.in.txt get 1.0 end]
  }
  #--- ʸ
  set   trimd_text ""
  set   split_text [split $origintxt \n]       ;# ʸ1ԤĤʬ䤹
  foreach work $split_text {
      append trimd_text [string trim $work] \n ;# Ƭȹζ
  }

  #--- ʸʬ (쥹ݥ󥹤ե˳Ǽ)
  trns_divide_sentence $trimd_text             ;# ʸʬ
  set   fname  [ file join $respdir respons.bin ]
  set   fileid [ open $fname w+ ]
  puts $fileid $response
  set  fsize  [expr [tell $fileid] -1]

  #--- 쥹ݥ󥹤Ƭ4Х(ʸο)򥹥å
  if { $fsize == 0 } { return }
  if { $fsize > 4 } {
      seek $fileid 4
      set alltext [string trim [read -nonewline $fileid] \x00 ]
  }
  close  $fileid

  set trimtext [split [string trim $alltext \n] \n]
  set endpoint [.pw.pane2.out.txt index end]

  foreach work $trimtext {
    set info1    [ string index $work 0 ]      ;# 1
    set info2    [ string index $work 1 ]      ;# 2
    set sentence [ string range $work 2 end ]  ;# ʸѥƥ

    if { ($info1 == "t") || ($info1 == "l") || ($info1 == "i") } {
      if { $style == "A" } then {
        if { $mode == "ej" } then {
            .pw.pane2.out.txt insert end "\[\] $sentence\n"
            .pw.pane2.out.txt insert end "\[\] $sentence\n"
        } else {
            .pw.pane2.out.txt insert end "\[\] $sentence\n"
            .pw.pane2.out.txt insert end "\[\] $sentence\n"
        }
      } else {
            .pw.pane2.out.txt insert end "$sentence\n"
      }
      continue
    }
    translate_one $sentence      ;# ʸ
    set output [get_output 2]    ;# 쥹ݥ󥹤Ѵ̤Ф
    if { $style == "A" } then {  ;# ɽ
        if { $mode == "ej" } then {
            .pw.pane2.out.txt insert end "\[\] $sentence\n"
            .pw.pane2.out.txt insert end "\[\] $output\n"
        } else {
            .pw.pane2.out.txt insert end "\[\] $sentence\n"
            .pw.pane2.out.txt insert end "\[\] $output\n"
        }
    } else {
            .pw.pane2.out.txt insert end "$output\n"
    }
    if { $tcl_platform(platform) != "windows" } {
        .pw.pane2.out.txt insert end "\n"
    }
  }
  .pw.pane2.out.txt see $endpoint
  host_close        ;# ѽλ
}
#--------------------------------------------------------------------------
#  (ᥤ)
#--------------------------------------------------------------------------
proc Re_trans { retrmode } {

  global  tcl_platform mode tagend

  set sentence [string trim [selection get]]  ;# ϰϤιƬȹζ
  if { $sentence == "" } { return }           ;# 

  set w(rt_in)  .rt.fr3.f3l.pw.pane1.in       ;# ʸڥ
  set w(rt_out) .rt.fr3.f3l.pw.pane2.out      ;# ʸڥ
  if { [winfo exists .rt] == 0 } {
      Create_Retrans $retrmode                ;# ̤
  } else {
    $w(rt_in).txt    delete 1.0 end
    $w(rt_out).txt   delete 1.0 end
    .rt.fr3.f3r.lab1 configure -text ""
    .rt.fr3.f3r.lab2 configure -text ""
    .rt.fr3.f3r.list delete 0 end
    if { $mode == "ej" } {
        .rt.fr2.hkg  configure -state normal -fg black
        .rt.fr2.hky  configure -state normal -fg black
        .rt.fr2.retr configure -state normal -fg black
    } else {
        .rt.fr2.hkg  configure -state disabled -fg gray70
        .rt.fr2.hky  configure -state disabled -fg gray70
        .rt.fr2.retr configure -state disabled -fg gray70
    }
  }
  $w(rt_in).txt insert end "$sentence\n"      ;# ʸɽ

  Connect_Server                              ;# Ф³
  if { [ init ]        != 0 } { return }      ;# ѳ
  if { [ setdictname ] != 0 } { return }      ;# Ѽ
  if { [ set_timeout ] != 0 } { return }      ;# ॢȤ
  if { [ setmode ]     != 0 } { return }      ;# ⡼ɤΥå
  if { [ clear_cache ] != 0 } { return }      ;# 񥭥åΥꥢ
  translate_one $sentence                     ;# ʸ
  set output [get_output 2]                   ;# 쥹ݥ󥹤Ѵ̤
  $w(rt_out).txt insert end $output           ;# ʸɽ
  set tagend [$w(rt_out).txt index "end -1 chars"]
  $w(rt_out).txt tag add firstyaku 1.0 $tagend            ;# ǽʸϡ
  $w(rt_out).txt tag configure firstyaku -foreground blue ;# Ĥɽ
}
#--------------------------------------------------------------------------
# ̤
#--------------------------------------------------------------------------
proc Create_Retrans { retrmode } {

  global  socketid response respdir mode gyflag startcol
  global  retr_helptext tcl_platform tagend retr_warning1 retr_warning2

  set retr_helptext \
        " ˡ \n\
           ѹ\n\n\
         1. ʸޤϺǽʸѹñΤɤ򥯥å(1)\n\
         2.(ʸ)(ʸ)פβ줫Υܥ򥯥å\n\
         3. 椫\n\
         4.Ǻפ򥯥å\n\n\
          ˡ \n\
           ʻѹ (2) \n\n\
         1. ʸޤϺǽʸѹñΤɤ򥯥å(1)\n\
         2.ʻ(ʸ)(ʸ)פβ줫Υܥ򥯥å\n\
         3. ʻ椫\n\
         4.ʻǺפ򥯥å\n\n\
         1ñľ򥯥åƺȥ顼ˤʤޤ\n\
         2ʻѹϱѴξΤ߲ǽǤ"
  set retr_warning1 "(ʸ)\n\
                     ʻ(ʸ)\n\n\
                     ǽʸ椫ѹñ򥯥åƲ"
  set retr_warning2 "Ѵξ硢ʻѹϤǤޤ"

  toplevel  .rt
  wm title  .rt "TkHonyaku Re-translation"

  frame   .rt.fr1
  frame   .rt.fr2
  frame   .rt.fr3
  frame   .rt.fr4
  pack    .rt.fr1 -side top -fill x -expand no
  pack    .rt.fr2 -side top -fill x -expand no
  pack    .rt.fr4 -side top -fill x -expand no
  pack    .rt.fr3 -side top -fill both -expand yes

  button  .rt.fr1.ykg -text (ʸ) -command {
             set gyflag g  ;# ʸ/ʸե饰=ʸ°פ̾ʤ(^_^)
             if { $mode == "ej" } {
               set work [split [.rt.fr3.f3l.pw.pane1.in.txt index insert] .]
               set startcol [lindex $work 1]
             } else {
               set work1 [.rt.fr3.f3l.pw.pane1.in.txt get 1.0 insert]
               set work2 [binary format "A*" $work1]
               set startcol [string length $work2]
             }
             .rt.fr3.f3r.list delete 0 end
             GetKouho "yakugo"    ;# ꥹȤ
          }
  button  .rt.fr1.yky -text (ʸ) -command {
             set outtext .rt.fr3.f3l.pw.pane2.out.txt
             if { [$outtext compare insert > $tagend] } {
                 tk_messageBox -parent . -title "TkHonyaku Re-trans Warning" \
                               -icon info -type ok  -message $retr_warning1
             } else {
               set gyflag y         ;# ʸ/ʸե饰=ʸ
               if { $mode == "ej" } {
                 set work1 [.rt.fr3.f3l.pw.pane2.out.txt get 1.0 insert]
                 set work2 [binary format "A*" $work1]
                 set startcol [string length $work2]
               } else {
                 set work [split [.rt.fr3.f3l.pw.pane2.out.txt index insert] .]
                 set startcol [lindex $work 1]
               }
               .rt.fr3.f3r.list delete 0 end
               GetKouho "yakugo"    ;# ꥹȤ
            }
          }
  button  .rt.fr1.retr -text Ǻ -command { RetrYakugo }
  button  .rt.fr1.help -text إ           -command {
          tk_messageBox -parent . -title "TkHonyaku Re-trans Help" \
                        -icon info -type ok  -message $retr_helptext
  }
  pack    .rt.fr1.ykg .rt.fr1.yky .rt.fr1.retr .rt.fr1.help -side left -fill y

  button  .rt.fr2.hkg -text ʻ(ʸ) -command {
             set gyflag g         ;# ʸ/ʸե饰=ʸ
             if { $mode == "ej" } {
               set work [split [.rt.fr3.f3l.pw.pane1.in.txt index insert] .]
               set startcol [lindex $work 1]
               set word [.rt.fr3.f3l.pw.pane1.in.txt \
                         get "insert wordstart" "insert wordend"]
               .rt.fr3.f3r.lab1 configure -text $word
               .rt.fr3.f3r.lab2 configure -text ""
               .rt.fr3.f3r.list delete 0 end
               GetKouho "hinshi"  ;# ʻꥹȤ
             } else {
               tk_messageBox -parent . -title "TkHonyaku Re-trans Warning" \
                             -icon info -type ok -message $retr_warning2
             }
          }
  if { $mode == "je" } {.rt.fr2.hkg configure -state disabled -fg gray70 }

  button  .rt.fr2.hky  -text ʻ(ʸ)   -command {
             set outtext .rt.fr3.f3l.pw.pane2.out.txt
             if { [$outtext compare insert > $tagend] } {
                 tk_messageBox -parent . -title "TkHonyaku Re-trans Warning" \
                               -icon info -type ok  -message $retr_warning1
             } else {
               set gyflag y  ;# ʸ/ʸե饰=ʸ
               if { $mode == "ej" } {
                 set work1 [.rt.fr3.f3l.pw.pane2.out.txt get 1.0 insert]
                 set work2 [binary format "A*" $work1]
                 set startcol [string length $work2]
                 set word [.rt.fr3.f3l.pw.pane2.out.txt \
                           get "insert wordstart" "insert wordend"]
                 .rt.fr3.f3r.lab1 configure -text $word
                 .rt.fr3.f3r.lab2 configure -text ""
                 .rt.fr3.f3r.list delete 0 end
                 GetKouho "hinshi"  ;# ʻꥹȤ
               } else {
                  tk_messageBox -parent . -title "TkHonyaku Re-trans Warning" \
                                -icon info -type ok -message $retr_warning2
               }
            }
        }
  if { $mode == "je" } {.rt.fr2.hky configure -state disabled -fg gray70 }

  button  .rt.fr2.retr -text ʻǺ -command { RetrHinshi }
  if { $mode == "je" } {.rt.fr2.retr configure -state disabled -fg gray70 }

  button  .rt.fr2.quit -text Ĥ -command { \
              if {$socketid != ""} {
                  close $socketid
                  set socketid   ""
#                  host_close
              }
              set use_server 0       ;# лե饰򥯥ꥢ
              destroy .rt
          }
  pack    .rt.fr2.hkg .rt.fr2.hky .rt.fr2.retr .rt.fr2.quit -side left -fill y

  label   .rt.fr4.lab -text \
          "  () β̤θʸӺǽʸϽʤǲ"
  pack    .rt.fr4.lab -side left -fill x -expand no

  frame   .rt.fr3.f3l
  frame   .rt.fr3.f3r
  pack    .rt.fr3.f3l .rt.fr3.f3r -side left -fill both -expand yes

  #=============================================
  # ڥ󼰥ɥ (ķ)
  #=============================================
  panedwindow_create .rt.fr3.f3l.pw 3.5i 3.5i
  pack   .rt.fr3.f3l.pw -expand yes -fill both

  set w(rt_in)  .rt.fr3.f3l.pw.pane1.in
  set w(rt_out) .rt.fr3.f3l.pw.pane2.out

  frame  $w(rt_in)
  frame  $w(rt_out)
  pack   $w(rt_in) $w(rt_out) -expand yes -fill both -padx 4 -pady 10

  scrollbar $w(rt_in).sbar  -command {.rt.fr3.f3l.pw.pane1.in.txt yview}
  pack      $w(rt_in).sbar  -side right -fill y
  text      $w(rt_in).txt   -width 80 -height 5 \
               -yscrollcommand {.rt.fr3.f3l.pw.pane1.in.sbar set}
  pack      $w(rt_in).txt   -side left -expand yes -fill both

  scrollbar $w(rt_out).sbar -command {.rt.fr3.f3l.pw.pane2.out.txt yview}
  pack      $w(rt_out).sbar -side right -fill y
  text      $w(rt_out).txt  -width 80 -height 8 \
               -yscrollcommand {.rt.fr3.f3l.pw.pane2.out.sbar set}
  pack      $w(rt_out).txt  -side left -expand yes -fill both

  #=============================================
  # 졿ʻꥹȤɽѥå
  #=============================================
  label     .rt.fr3.f3r.lab1 -text "" -relief sunken -fg red
  label     .rt.fr3.f3r.lab2 -text "" -relief sunken -fg blue
  pack      .rt.fr3.f3r.lab1 .rt.fr3.f3r.lab2 -side top -fill x -expand no

  listbox   .rt.fr3.f3r.list -width 35 -height 10 \
                             -xscrollcommand ".rt.fr3.f3r.xscr set" \
                             -yscrollcommand ".rt.fr3.f3r.yscr set"
  scrollbar .rt.fr3.f3r.xscr -orient horizontal \
                             -command ".rt.fr3.f3r.list xview"
  scrollbar .rt.fr3.f3r.yscr -orient vertical \
                             -command ".rt.fr3.f3r.list yview"
  pack      .rt.fr3.f3r.yscr -side right -fill y    -expand no
  pack      .rt.fr3.f3r.list -side top   -fill both -expand yes
  pack      .rt.fr3.f3r.xscr -side top   -fill x    -expand no

  #==== ꥹȥܥåΥХ
  bind .rt.fr3.f3r.list <ButtonRelease-1> { GetItem %x %y }

  # ---- ɥĤϡѽλ򤹤
  wm protocol .rt WM_DELETE_WINDOW { \
      if {$socketid != ""} {
#          close $socketid
#          set socketid   ""
          host_close
      }
      set use_server 0               ;# лե饰򥯥ꥢ
      destroy .rt
  }
}
#--------------------------------------------------------------------------
# 졿ʻꥹȤ򤵤줿Ƥɽ
#--------------------------------------------------------------------------
proc GetItem { x y } {

  global  listitem_num

  set listitem_num [.rt.fr3.f3r.list index @$x,$y]  ;# 򤷤ֹ
  set listitem     [.rt.fr3.f3r.list get @$x,$y]
  .rt.fr3.f3r.lab2 configure -text $listitem
}
#--------------------------------------------------------------------------
# ۥޥб
#--------------------------------------------------------------------------
proc mscroll {bindtag} {
    bind $bindtag <Button-5>         [list %W yview scroll  5 units]
    bind $bindtag <Button-4>         [list %W yview scroll -5 units]
    bind $bindtag <Shift-Button-5>   [list %W yview scroll  1 units]
    bind $bindtag <Shift-Button-4>   [list %W yview scroll -1 units]
    bind $bindtag <Control-Button-5> [list %W yview scroll  1 pages]
    bind $bindtag <Control-Button-4> [list %W yview scroll -1 pages]
}
#--------------------------------------------------------------------------
# Υإɽ
#--------------------------------------------------------------------------
proc DispDicHelp {} {

  global  tkh_version gifdir

  toplevel  .dih
  wm title  .dih "TkHonyaku Dictionary Help"

  text   .dih.txt -width 78 -height 22
  pack   .dih.txt -fill both -expand yes
  button .dih.but -text Ĥ -command { destroy .dih }
  pack   .dih.but -padx 10 -pady 2

  image create photo tkh_image -file [ file join $gifdir tkhonyaku.gif ]
  .dih.txt image create insert -image tkh_image -name tkhimage

  set text1 "أԣversion $tkh_version    ɥΥإ"
  set text2 {

[ư]         kterm, ǥ֥饦ʤ¾Υץꥱñ
               ɥå(ޥ֤ˤ)ǼưŪ˼롣

[¹]         ñץƥȥܥåϤ줿ñμ򤹤
               ˤ Enter Ϥ뤫Υܥ򥯥å롣

[]         [ư]ǤϤʤˡkterm, ǥ֥饦ʤ¾Υ
               ꥱñɥå(ޥ֤ˤ)
               Υܥ򲡤ȼ롣

[ñõ]     ñפΥƥȥܥåƤƾõ롣

[õ]       ʸΥƥȥܥåƤƾõ롣

[λ]         ɥĤ롣

}
  .dih.txt insert end [ format "%s\n%s" $text1 $text2 ]
  .dih.txt configure -state disabled

  tkwait visibility .dih    ;# .dih Ļ֤ˤʤΤԤ
}
#--------------------------------------------------------------------------
# ޥ򤷤ñưŪ˼
#--------------------------------------------------------------------------
proc mouse_dic {} {

  global  old_sel auto_dic

  if { ($auto_dic == "OFF") || ([winfo exists .dict] == 0) } {
      return
  }
  if [catch {selection get} new_sel] {
  } else {
      if { $new_sel != $old_sel } {
          Exec_dict $new_sel      ;# 
          set old_sel $new_sel
      }
  }
  after 3000 mouse_dic
}
#--------------------------------------------------------------------------
# 񻲾ȥɥ򳫤
#--------------------------------------------------------------------------
proc Use_dict {} {

  global  mode  auto_dic  old_sel

  if { [winfo exists .dict] == 0 } {
    toplevel  .dict
    wm title  .dict "TkHonyaku Dictionary"

    frame  .dict.fr1
    frame  .dict.fr2
    frame  .dict.fr3
    pack   .dict.fr1 .dict.fr2 -side top -anchor w
    pack   .dict.fr3 -side top -fill both -expand yes

    checkbutton .dict.fr1.auto  -text "ư" \
        -variable auto_dic -onvalue "ON" -offvalue "OFF" -command {
        if { $auto_dic == "ON" } {
            set old_sel ""
            mouse_dic
        } else {
            after cancel mouse_dic
        }
    }
    label  .dict.fr1.lab  -text "ñ"
    entry  .dict.fr1.word -width 30
    button .dict.fr1.exec -text "¡" -command {
           set word [.dict.fr1.word get]
           if { $word != "" } {
#              busy_eval { Exec_dict $word }    ;# 
               Exec_dict $word     ;# 
           }
    }
    button .dict.fr1.sele -text "" -command {
           set word [selection get]
           if { $word != "" } {
#              busy_eval { Exec_dict $word }    ;# 
               Exec_dict $word     ;# 
           }
    }

    pack   .dict.fr1.auto \
           .dict.fr1.lab  .dict.fr1.word \
           .dict.fr1.exec .dict.fr1.sele \
           -side left -fill x -expand yes -padx 4

    button .dict.fr2.clr1 -text ñõ \
                           -command { .dict.fr1.word delete 0 end }
    button .dict.fr2.clr2 -text õ -command {.dict.fr3.txt delete 1.0 end}
    button .dict.fr2.help -text إ -command { DispDicHelp }
    button .dict.fr2.quit -text Ĥ -command { destroy .dict }

    pack   .dict.fr2.clr1 .dict.fr2.clr2 .dict.fr2.help .dict.fr2.quit \
           -side left -fill x -expand yes -padx 4

    text      .dict.fr3.txt -height 15 -width 80 \
              -yscrollcommand ".dict.fr3.yscrl set"
    mscroll   .dict.fr3.txt
    scrollbar .dict.fr3.yscrl -command ".dict.fr3.txt yview"
    pack      .dict.fr3.yscrl -side right -fill y
    pack      .dict.fr3.txt -fill both -expand yes

    bind  .dict.fr1.word <KeyPress-Return> {
          set word [.dict.fr1.word get]
          if { $word != "" } {
#             busy_eval { Exec_dict $word }    ;# 
              Exec_dict $word       ;# 
          }    
    }
    focus .dict.fr1.word
    update idletasks                ;# ɽ򹹿
    if { $auto_dic == "ON" } {
        .dict.fr1.auto select       ;# ϰϤưŪ˼
        set old_sel ""
        mouse_dic
    }
  }
}
#--------------------------------------------------------------------------
# 񻲾Ȥ¹Ԥ
#--------------------------------------------------------------------------
proc Exec_dict { originalword } {

  global mode

  set trimword [string trim $originalword] ;# ʸζʸ
  .dict.fr1.word delete 0 end              ;# ñץƥȥܥåõ
  .dict.fr1.word insert 0 $trimword        ;# ñɽ

  if { [regexp {^[09a-zA-Z]} $trimword] } {
      set mode "ej"
  } else {
      set mode "je"
  }

  Connect_Server                          ;# Ф³
  if { [ init ] != 0 }        { return }  ;# ѳ
  if { [ setdictname ] != 0 } { return }  ;# Ѽ
  if { [ set_timeout ] != 0 } { return }  ;# ॢȤ
  if { [ setmode ] != 0 }     { return }  ;# ⡼ɤΥå
  if { [ browse_dict $trimword ] != 0 } { return } ;# 񻲾

  set output [get_output 0]               ;# 쥹ݥ󥹤Ѵ̤Ф
  set endpoint [.dict.fr3.txt index end]  ;# ߤνüˡ
  .dict.fr3.txt insert end \n$output      ;# ̤ɲäơ
  .dict.fr3.txt see $endpoint             ;# ɽ֤롣
  host_close                              ;# ѽλ
}
#--------------------------------------------------------------------------
# ʸʸΥɥ򡢽ġ¤٤ɽ
#--------------------------------------------------------------------------
proc Place { direction } {

  global  win_dir

  if { $direction == "V"} {
    set win_dir "V"
    .pw.grip configure -cursor sb_v_double_arrow
    pack  forget .pw
    place forget .pw.pane1
    place forget .pw.pane2
    place forget .pw.sash
    place forget .pw.grip
    place .pw.pane1 -relx 0.5 -rely 0   -anchor n -relwidth 1.0 -relheight 0.5
    place .pw.pane2 -relx 0.5 -rely 1.0 -anchor s -relwidth 1.0 -relheight 0.5
    place .pw.sash  -relx 0.5 -rely 0.5 -relwidth 1.0 -anchor c
    place .pw.grip  -relx 0.95 -rely 0.5 -anchor c
    pack  .pw -expand yes -fill both
  } else {
    set win_dir "H"
    .pw.grip configure -cursor sb_h_double_arrow
    pack  forget .pw
    place forget .pw.pane1
    place forget .pw.pane2
    place forget .pw.sash
    place forget .pw.grip
    place .pw.pane1 -relx 0.0 -rely 0.5 -anchor w -relwidth 0.5 -relheight 0.96
    place .pw.pane2 -relx 1.0 -rely 0.5 -anchor e -relwidth 0.5 -relheight 0.96
    place .pw.sash  -relx 0.5 -rely 0.98 -relwidth 1.0 -anchor c
    place .pw.grip  -relx 0.5 -rely 0.98 -anchor c
    pack  .pw -expand yes -fill both
  }
}
#--------------------------------------------------------------------------
# ܥطʿѹ
#--------------------------------------------------------------------------
proc ChgAllBut { borf color} {

  global  p1

  set buttons [ list .fr1.ej .fr1.ejsel .fr1.delin .fr1.yaku .fr1.dict \
      .fr1.setbut .fr2.je .fr2.ejsel .fr2.delout .fr2.hinsi .fr2.stsbut \
      .fr2.exit $p1.fr5.inbgbut $p1.fr5.infgbut $p1.fr6.outbgbut \
      $p1.fr6.outfgbut $p1.fr7.butbg $p1.fr7.butfg $p1.fr7.winbg \
      $p1.fr8.butbg .st.fr2.quit ]
  if { $borf == "BG" } {
      foreach i $buttons {
          $i configure -bg $color
      }
      .fr1.file configure -bg $color   ;# ե (ܥɤ)
      .fr2.help configure -bg $color   ;# إ   (ܥ)
  } else {
      foreach i $buttons {
          $i configure -fg $color
      }
      .fr1.file configure -fg $color   ;# ե (ܥ)
      .fr2.help configure -fg $color   ;# إ   (ˤܥ)
  }
}
#--------------------------------------------------------------------------
# 饸ܥȥåܥοѹ
#--------------------------------------------------------------------------
proc ChgRadBut { color} {

  global  p1 p2 p3 tcl_platform

  set buttons [ list \
      $p1.fr3.radtate $p1.fr3.radyoko $p1.fr4.radeandj $p1.fr4.radeorj \
      $p2.ejc $p2.ejo $p2.eji $p2.ejd $p2.ejk $p2.ejw $p2.ejp \
      $p3.jea $p3.jeb $p3.fr1.ssa1 $p3.fr1.ssa2 $p3.fr1.ssa3 $p3.fr2.sya1 \
      $p3.fr2.sya2 $p3.fr2.sya3 $p3.fr3.osk0 $p3.fr3.osk1 $p3.fr3.osk2 \
      $p3.fr3.osk3 $p3.fr3.osk4 $p3.fr3.osk5 $p3.fr3.osk6 $p3.fr4.osk7 \
      $p3.fr5.omk0 $p3.fr5.omk1 $p3.fr5.omk2 $p3.fr5.omk3 $p3.fr5.omk4 \
      $p3.fr5.omk5 $p3.fr6.omk6 $p3.fr6.omk7 $p3.fr6.omk8 $p3.fr6.omk9
  ]
  if { $tcl_platform(platform) == "unix" } {
      lappend buttons $p1.fr10.xim1 $p1.fr11.xim2 $p1.fr10.xim3 $p1.fr11.xim4
  }
  foreach i $buttons {
      $i configure -selectcolor $color
  }
}
#--------------------------------------------------------------------------
# ͤե˳Ǽ
#--------------------------------------------------------------------------
proc SaveConf {} {

  global  conf_file   disp_debug server port win_dir style 
  global  txtin_bg    txtin_fg   txtout_bg   txtout_fg
  global  window_bg   allbut_bg  allbut_fg   radbut_bg
  global  ximstyle    ejmode     jemode      my_osk    my_omk  auto_dic
  global  mainw_width mainw_height

  set valiable [ list disp_debug server    port      win_dir   style \
                      txtin_bg   txtin_fg  txtout_bg txtout_fg       \
                      window_bg  allbut_bg allbut_fg radbut_bg       \
                      ximstyle   ejmode    jemode    my_osk    my_omk auto_dic]
  set fileID [ open $conf_file w+ ]
  foreach i $valiable {
      puts $fileID "set $i \"[set $i]\""
  }
  puts  $fileID "set mainw_width  [ winfo width  . ]"
  puts  $fileID "set mainw_height [ winfo height . ]"
  close $fileID
}
#--------------------------------------------------------------------------
# ꥦɥ
#--------------------------------------------------------------------------
proc Setting {} {

  global  username server port win_dir style
  global  txtin_bg txtin_fg txtout_bg txtout_fg allbut_bg radbut_bg
  global  ximstyle ejmode jemode my_osk my_omk
  global  p1 p2 p3 cmd1 cmd2 cmd3 cmd4 tcl_platform disp_debug

  if { [winfo exists .st] == 1 } {return }
  toplevel  .st
  wm title  .st "TkHonyaku Setting"

  frame .st.fr1
  frame .st.fr2
  pack  .st.fr1 .st.fr2

  tabnotebook_create .st.fr1.tn    ;# ֥Ρȥ֥å
  pack .st.fr1.tn

  # ץΥǡ١˥꥽ɲ
  option add *Tabnotebook.tabs.background #666666  widgetDefault
  option add *Tabnotebook.margin          6        widgetDefault
  option add *Tabnotebook.tabColor        skyblue  widgetDefault
  option add *Tabnotebook.activeTabColor  yellow   widgetDefault

  set p1 [tabnotebook_page .st.fr1.tn ""]       ;# ڡ
  set p2 [tabnotebook_page .st.fr1.tn "Ѵ"]   ;# ڡ
  set p3 [tabnotebook_page .st.fr1.tn "Ѵ"]   ;# ڡ

  .st.fr1.tn.tabs configure -background #666666

  #----------------------------------------------------------------------
  # 
  #----------------------------------------------------------------------
  frame $p1.fr0
  frame $p1.fr1 ; frame $p1.fr2  ; frame $p1.fr3  ; frame $p1.fr4
  frame $p1.fr5 ; frame $p1.fr6  ; frame $p1.fr7  ; frame $p1.fr8
  frame $p1.fr9 ; frame $p1.fr10 ; frame $p1.fr11 ; frame $p1.fr12
  pack  $p1.fr0 $p1.fr1 $p1.fr2 $p1.fr3  $p1.fr4  $p1.fr5 $p1.fr6 \
        $p1.fr7 $p1.fr8 $p1.fr9 $p1.fr10 $p1.fr11 $p1.fr12 \
        -side top -fill both -expand yes -padx 10 -pady 4

  label $p1.fr0.lab0 -text "桼̾(ѿ)"
  entry $p1.fr0.ent0
  $p1.fr0.ent0 insert 1 $username
  pack  $p1.fr0.lab0 $p1.fr0.ent0 -side left -fill both -expand yes

  label $p1.fr1.lab1 -text ""
  entry $p1.fr1.ent1
  $p1.fr1.ent1 insert 1 $server
  pack  $p1.fr1.lab1 $p1.fr1.ent1 -side left -fill both -expand yes

  label $p1.fr2.lab2 -text "ݡֹ"
  entry $p1.fr2.ent2
  $p1.fr2.ent2 insert 1 $port
  pack  $p1.fr2.lab2 $p1.fr2.ent2 -side left -fill both -expand yes

  label $p1.fr3.lab3 -text "ɥ"
  radiobutton $p1.fr3.radtate -text "" -variable win_dir -value "V" \
              -command { Place "V" }
  radiobutton $p1.fr3.radyoko -text "" -variable win_dir -value "H" \
              -command { Place "H" }
  pack  $p1.fr3.lab3 $p1.fr3.radtate $p1.fr3.radyoko \
        -side left -fill both -expand yes
  if { $win_dir == "V" } { $p1.fr3.radtate select } \
  else { $p1.fr3.radyoko select }

  label       $p1.fr4.lab4 -text "ʸɽ"
  radiobutton $p1.fr4.radeandj -text ""     -variable style -value A
  radiobutton $p1.fr4.radeorj  -text "ʸΤ" -variable style -value O
  pack   $p1.fr4.lab4 $p1.fr4.radeandj $p1.fr4.radeorj \
         -side left -fill both -expand yes
  if { $style == "A" } { $p1.fr4.radeandj select } \
  else { $p1.fr4.radeorj select }

  button $p1.fr5.inbgbut -text ʸطʿ -command \
         { ;# set bgcol  [.pw.pane1.in.txt cget -bg]
#           set newcol [tk_chooseColor -initialcolor $bgcol]
           set newcol [ SelectColor ]
           if {$newcol != ""} then { .pw.pane1.in.txt configure -bg $newcol
                                     set txtin_bg $newcol }
         }
  button $p1.fr5.infgbut -text ʸʸ -command \
         { set newcol [ SelectColor ]
           if {$newcol != ""} then { .pw.pane1.in.txt configure -fg $newcol
                                     set txtin_fg $newcol }
         }
  button $p1.fr6.outbgbut -text ʸطʿ -command \
         { set newcol [ SelectColor ]
           if {$newcol != ""} then { .pw.pane2.out.txt configure -bg $newcol
                                     set txtout_bg $newcol }
         }
  button $p1.fr6.outfgbut -text ʸʸ -command \
         { set newcol [ SelectColor ]
           if {$newcol != ""} then { .pw.pane2.out.txt configure -fg $newcol
                                     set txtout_fg $newcol }
         }
  button $p1.fr7.winbg -text طʿ -command \
         { set newcol [ SelectColor ]
           if {$newcol != ""} then { set butbg [.fr1.ej cget -background]
                                     set butfg [.fr1.ej cget -foreground]
                                     tk_setPalette $newcol
                                     set window_bg $newcol
                                     ChgAllBut BG  $butbg
                                     ChgAllBut FG  $butfg }
         }
  button $p1.fr7.butbg -text ܥطʿ -command \
         { set newcol [ SelectColor ]
           if {$newcol != ""} then { option add *Button.background $newcol 100
                                     ChgAllBut BG  $newcol
                                     set allbut_bg $newcol }
         }
  button $p1.fr7.butfg -text ܥʸ -command \
         { set newcol [ SelectColor ]
           if {$newcol != ""} then { option add *Button.foreground $newcol 100
                                     ChgAllBut FG  $newcol
                                     set allbut_fg $newcol }
         }
  pack $p1.fr5.inbgbut  $p1.fr5.infgbut  -side left -fill both -expand yes
  pack $p1.fr6.outbgbut $p1.fr6.outfgbut -side left -fill both -expand yes
  pack $p1.fr7.winbg $p1.fr7.butbg $p1.fr7.butfg \
       -side left -fill both -expand yes

  button $p1.fr8.butbg -text 饸ܥο -command \
         { set newcol [ SelectColor ]
           if {$newcol != ""} then {option add *Radiobutton.selectColor $newcol
                                    option add *Checkbutton.selectColor $newcol
                                    ChgRadBut $newcol
                                    set radbut_bg $newcol }
         }
  pack $p1.fr8.butbg -side left -fill both -expand yes

  if { $tcl_platform(platform) == "unix" } {
      label  $p1.fr9.lab -text "Ѵ"
      pack   $p1.fr9.lab

      radiobutton $p1.fr10.xim1 -text "Over the Spot 1" \
      -anchor w -variable ximstyle -value 1 -command \
       {imconfigure .pw.pane1.in.txt  -style {PreeditPosition StatusArea}
        imconfigure .pw.pane2.out.txt -style {PreeditPosition StatusArea}}
      radiobutton $p1.fr11.xim2 -text "Over the Spot 2" \
      -anchor w -variable ximstyle -value 2 -command \
       {imconfigure .pw.pane1.in.txt  -style {PreeditPosition StatusNothing}
        imconfigure .pw.pane2.out.txt -style {PreeditPosition StatusNothing}}
      radiobutton $p1.fr10.xim3 -text "Off  the Spot" \
      -anchor w -variable ximstyle -value 3 -command \
       {imconfigure .pw.pane1.in.txt  -style {PreeditArea StatusArea}
        imconfigure .pw.pane2.out.txt -style {PreeditArea StatusArea}}
      radiobutton $p1.fr11.xim4 -text "root ɥ" \
      -anchor w -variable ximstyle -value 4 -command \
       {imconfigure .pw.pane1.in.txt  -style {PreeditNothing StatusNothing}
        imconfigure .pw.pane2.out.txt -style {PreeditNothing StatusNothing}}
      switch $ximstyle {
         2       { $p1.fr11.xim2 select }
         3       { $p1.fr10.xim3 select }
         4       { $p1.fr11.xim4 select }
         default { $p1.fr10.xim1 select }
      }
      pack $p1.fr10.xim1 $p1.fr10.xim3 -side left -fill both -expand yes
      pack $p1.fr11.xim2 $p1.fr11.xim4 -side left -fill both -expand yes
  }
  label $p1.fr12.lab12 -text "ǥХåѥɥ"
  radiobutton $p1.fr12.dbgyes -text "ɽ"   -variable disp_debug \
              -value "yes" -command { wm deiconify .deb }
  radiobutton $p1.fr12.dbgno  -text "ɽʤ" -variable disp_debug \
              -value "no"  -command {wm withdraw   .deb}
  pack  $p1.fr12.lab12 $p1.fr12.dbgyes $p1.fr12.dbgno \
        -side left -fill both -expand yes
  if { $disp_debug == "yes" } { $p1.fr12.dbgyes select } \
  else                        { $p1.fr12.dbgno  select }

  #----------------------------------------------------------------------
  # Ѵư
  #----------------------------------------------------------------------
  checkbutton $p2.ejc -text "ʸʸ̤ʤ" \
              -anchor w -variable ejmC -onvalue "C" -offvalue ""
  checkbutton $p2.ejo -text "ϤνǤ¸" \
              -anchor w -variable ejmO -onvalue "O" -offvalue ""
  checkbutton $p2.eji -text "̿ʿʸȤư" \
              -anchor w -variable ejmI -onvalue "I" -offvalue ""
  checkbutton $p2.ejd -text "֤ǤޤĴˤ" \
              -anchor w -variable ejmD -onvalue "D" -offvalue ""
  checkbutton $p2.ejk -text "ʸ֡פǤĤʤ" \
              -anchor w -variable ejmK -onvalue "K" -offvalue ""
  checkbutton $p2.ejw -text "ñؽԤ" \
              -anchor w -variable ejmW -onvalue "W" -offvalue ""
  checkbutton $p2.ejp -text "ʻؽԤ" \
              -anchor w -variable ejmP -onvalue "P" -offvalue ""

  pack  $p2.ejc $p2.ejo $p2.eji $p2.ejd $p2.ejk $p2.ejw $p2.ejp \
        -side top -fill both -expand yes -padx 10

  if { [string first "C" $ejmode] != -1 } { $p2.ejc select } \
                                     else { $p2.ejc deselect }
  if { [string first "O" $ejmode] != -1 } { $p2.ejo select } \
                                     else { $p2.ejo deselect }
  if { [string first "I" $ejmode] != -1 } { $p2.eji select } \
                                     else { $p2.eji deselect }
  if { [string first "D" $ejmode] != -1 } { $p2.ejd select } \
                                     else { $p2.ejd deselect }
  if { [string first "K" $ejmode] != -1 } { $p2.ejk select } \
                                     else { $p2.ejk deselect }
  if { [string first "W" $ejmode] != -1 } { $p2.ejw select } \
                                     else { $p2.ejw deselect }
  if { [string first "P" $ejmode] != -1 } { $p2.ejp select } \
                                     else { $p2.ejp deselect }

  #----------------------------------------------------------------------
  # Ѵư
  #----------------------------------------------------------------------
  checkbutton $p3.jea   -text "ؽѤ" \
              -anchor w -variable jemLD -onvalue "1" -offvalue "0"
  checkbutton $p3.jeb   -text "not û̷" \
              -anchor w -variable jemAB -onvalue "1" -offvalue "0"
  pack  $p3.jea $p3.jeb -side top -fill both -expand no -padx 10

  label $p3.lab3 -text "άΰ" -relief groove -anchor w
  pack  $p3.lab3 -side top -fill both -expand no -padx 10 -pady 6

  frame $p3.fr1
  pack  $p3.fr1
  radiobutton $p3.fr1.ssa1 -text "ꤵ줿䤦" \
                                                -variable jemsso -value 1
  radiobutton $p3.fr1.ssa2 -text "ư֤" -variable jemsso -value 2
  radiobutton $p3.fr1.ssa3 -text "̿" -variable jemsso -value 3
  pack  $p3.fr1.ssa1 $p3.fr1.ssa2 $p3.fr1.ssa3 \
         -side left -fill both -expand no -anchor w

  label $p3.lab4 -text "֡Ƥפ" -relief groove -anchor w
  pack  $p3.lab4 -side top -fill both -expand no -anchor w -padx 10 -pady 6

  frame $p3.fr2
  pack  $p3.fr2
  radiobutton $p3.fr2.sya1 -text "߷"     -variable jemsya -value 1
  radiobutton $p3.fr2.sya2 -text "߿ʹԷ" -variable jemsya -value 2
  radiobutton $p3.fr2.sya3 -text "λ"     -variable jemsya -value 3

  pack  $p3.fr2.sya1 $p3.fr2.sya2 $p3.fr2.sya3 \
        -side left -fill both -expand no -anchor w

  label $p3.lab5 -text "䤦θ" -relief groove -anchor w
  pack  $p3.lab5 -side top -fill both -expand no -anchor w -padx 10 -pady 6

  frame $p3.fr3 -relief sunken
  frame $p3.fr4 -relief sunken
  pack  $p3.fr3 $p3.fr4
  radiobutton $p3.fr3.osk0 -text "I"    -variable jemosk -value 0
  radiobutton $p3.fr3.osk1 -text "we"   -variable jemosk -value 1
  radiobutton $p3.fr3.osk2 -text "you"  -variable jemosk -value 2
  radiobutton $p3.fr3.osk3 -text "it"   -variable jemosk -value 3
  radiobutton $p3.fr3.osk4 -text "he"   -variable jemosk -value 4
  radiobutton $p3.fr3.osk5 -text "she"  -variable jemosk -value 5
  radiobutton $p3.fr3.osk6 -text "they" -variable jemosk -value 6
  radiobutton $p3.fr4.osk7 -text "¾" -variable jemosk -value 7
  entry       $p3.fr4.oskent -width 10
  $p3.fr4.oskent insert 0 $my_osk

  pack  $p3.fr3.osk0 $p3.fr3.osk1 $p3.fr3.osk2 $p3.fr3.osk3 \
        $p3.fr3.osk4 $p3.fr3.osk5 $p3.fr3.osk6 \
        -side left -expand no -anchor w
  pack  $p3.fr4.osk7 $p3.fr4.oskent -side left -expand no -anchor w

  label $p3.lab6 -text "䤦Ūθ" -relief groove -anchor w
  pack  $p3.lab6 -side top -fill both -expand no -anchor w -padx 10 -pady 6

  frame $p3.fr5
  frame $p3.fr6
  pack  $p3.fr5 $p3.fr6
  radiobutton $p3.fr5.omk0 -text ʤ -variable jemomk -value 0
  radiobutton $p3.fr5.omk1 -text "it"     -variable jemomk -value 1
  radiobutton $p3.fr5.omk2 -text "that"   -variable jemomk -value 2
  radiobutton $p3.fr5.omk3 -text "you"    -variable jemomk -value 3
  radiobutton $p3.fr5.omk4 -text "them"   -variable jemomk -value 4
  radiobutton $p3.fr5.omk5 -text "us"     -variable jemomk -value 5
  radiobutton $p3.fr6.omk6 -text "me"     -variable jemomk -value 6
  radiobutton $p3.fr6.omk7 -text "him"    -variable jemomk -value 7
  radiobutton $p3.fr6.omk8 -text "her"    -variable jemomk -value 8
  radiobutton $p3.fr6.omk9 -text "¾" -variable jemomk -value 9
  entry       $p3.fr6.omkent -width 10
  $p3.fr6.omkent insert 0 $my_omk

  pack $p3.fr5.omk0 $p3.fr5.omk1 $p3.fr5.omk2 $p3.fr5.omk3  \
       $p3.fr5.omk4 $p3.fr5.omk5 -side left -expand no -anchor w

  pack $p3.fr6.omk6 $p3.fr6.omk7 $p3.fr6.omk8 $p3.fr6.omk9 \
       $p3.fr6.omkent -side left -expand no -anchor w

  if { [string index $jemode 0] == 1 } { $p3.jea select } \
                                  else { $p3.jea deselect }
  if { [string index $jemode 1] == 1 } { $p3.jeb select } \
                                  else { $p3.jeb deselect }
  if { [string index $jemode 2] == 1 } { $p3.fr1.ssa1 select }
  if { [string index $jemode 2] == 2 } { $p3.fr1.ssa2 select }
  if { [string index $jemode 2] == 3 } { $p3.fr1.ssa3 select }
  if { [string index $jemode 3] == 1 } { $p3.fr2.sya1 select }
  if { [string index $jemode 3] == 2 } { $p3.fr2.sya2 select }
  if { [string index $jemode 3] == 3 } { $p3.fr2.sya3 select }
  if { [string index $jemode 4] == 0 } { $p3.fr3.osk0 select }
  if { [string index $jemode 4] == 1 } { $p3.fr3.osk1 select }
  if { [string index $jemode 4] == 2 } { $p3.fr3.osk2 select }
  if { [string index $jemode 4] == 3 } { $p3.fr3.osk3 select }
  if { [string index $jemode 4] == 4 } { $p3.fr3.osk4 select }
  if { [string index $jemode 4] == 5 } { $p3.fr3.osk5 select }
  if { [string index $jemode 4] == 6 } { $p3.fr3.osk6 select }
  if { [string index $jemode 4] == 7 } { $p3.fr4.osk7 select }

  set omkno [expr [string last ":" $jemode] -1 ]
  if { [string index $jemode $omkno] == 0 } { $p3.fr5.omk0 select }
  if { [string index $jemode $omkno] == 1 } { $p3.fr5.omk1 select }
  if { [string index $jemode $omkno] == 2 } { $p3.fr5.omk2 select }
  if { [string index $jemode $omkno] == 3 } { $p3.fr5.omk3 select }
  if { [string index $jemode $omkno] == 4 } { $p3.fr5.omk4 select }
  if { [string index $jemode $omkno] == 5 } { $p3.fr5.omk5 select }
  if { [string index $jemode $omkno] == 6 } { $p3.fr6.omk6 select }
  if { [string index $jemode $omkno] == 7 } { $p3.fr6.omk7 select }
  if { [string index $jemode $omkno] == 8 } { $p3.fr6.omk8 select }
  if { [string index $jemode $omkno] == 9 } { $p3.fr6.omk9 select }

  set cmd1 [ list $p1.fr1.ent1   get ]  ;# 
  set cmd2 [ list $p1.fr2.ent2   get ]  ;# ݡֹ
  set cmd3 [ list $p3.fr4.oskent get ]  ;# 䤦θ(¾)
  set cmd4 [ list $p3.fr6.omkent get ]  ;# 䤦Ūθ(¾)
  button .st.fr2.quit  -text "Ĥ" -command {
         set server [eval $cmd1]
         set port   [eval $cmd2]
         set my_osk [eval $cmd3]
         set my_omk [eval $cmd4]
         set    ejmode ""
         set    jemode ""
         append ejmode $ejmC $ejmO $ejmI $ejmD $ejmK $ejmW $ejmP
         append jemode $jemLD $jemAB $jemsso $jemsya \
                     $jemosk ":I,we,you,it,he,she,they," [eval $cmd3]: \
                     $jemomk ": ,it,that,you,them,us,me,him,her," [eval $cmd4]
         destroy .st
  }
  pack   .st.fr2.quit -side left -fill both -expand yes -padx 10 -pady 10

  # ɥĤ˽λ򤹤롣
  wm protocol .st WM_DELETE_WINDOW {
         set server [eval $cmd1]
         set port   [eval $cmd2]
         set my_osk [eval $cmd3]
         set my_omk [eval $cmd4]
         set    ejmode ""
         set    jemode ""
         append ejmode $ejmC $ejmO $ejmI $ejmD $ejmK $ejmW $ejmP
         append jemode $jemLD $jemAB $jemsso $jemsya \
                     $jemosk ":I,we,you,it,he,she,they," [eval $cmd3]: \
                     $jemomk ": ,it,that,you,them,us,me,him,her," [eval $cmd4]
  }
}
#--------------------------------------------------------------------------
# Υơɽɥ򳫤
#--------------------------------------------------------------------------
proc OpenStsWindow {} {

  if { [winfo exists .sts] == 0 } {
    toplevel  .sts
    wm title  .sts "TkHonyaku Status"

    frame  .sts.fr1
    frame  .sts.fr2
    frame  .sts.fr3
    frame  .sts.fr4
    pack   .sts.fr1 .sts.fr2 .sts.fr3 .sts.fr4 -side top -fill both -expand yes

    button .sts.fr1.stsbut -text ơ \
            -command { Connect_Server
                       DispStatus }
    button .sts.fr1.quit -text Ĥ -command { destroy .sts }
    grid   .sts.fr1.stsbut -column 0 -row 0
    grid   .sts.fr1.quit   -column 1 -row 0

    label .sts.fr2.lab1  -text "ơ"
    label .sts.fr2.lab2  -text "쥹ݥ󥹥Хȿ"
    label .sts.fr2.lab3  -text "³桼"
    label .sts.fr2.lab4  -text "桼̾"
    label .sts.fr2.lab5  -text "饤ȥۥ̾"
    label .sts.fr2.lab6  -text "饤ȥۥIP"

    label .sts.fr3.lab7  -text "ƥ༭"
    label .sts.fr3.lab8  -text "ej"
    label .sts.fr3.lab9  -text "je"

    label .sts.fr4.lab10 -text "ݡֹ"
    label .sts.fr4.lab11 -text "ej(1: 0:̤)"
    label .sts.fr4.lab12 -text "je(1: 0:̤)"
    label .sts.fr4.lab13 -text "ԥ塼ej"
    label .sts.fr4.lab14 -text "ԥ塼je"
    label .sts.fr4.lab15 -text "ؽ"
    label .sts.fr4.lab16 -text "å"
    label .sts.fr4.lab17 -text "ejФ read ѥ"
    label .sts.fr4.lab18 -text "ejФ  write ѥ"
    label .sts.fr4.lab19 -text "jeФ read ѥ"
    label .sts.fr4.lab20 -text "jeФ  write ѥ"
    label .sts.fr4.lab21 -text "ejФΥץֹ"
    label .sts.fr4.lab22 -text "jeФΥץֹ"

    for { set i 1 } { $i < 23 } { incr i } {
      if { $i <= 6 } then {
        grid  .sts.fr2.lab$i -column 0 -row $i -sticky e
        entry .sts.fr2.ent$i
        grid  .sts.fr2.ent$i -column 1 -row $i
      } elseif { ($i >= 7) &&  ($i <= 9) } {
        grid  .sts.fr3.lab$i -column 0 -row $i -sticky e
        entry .sts.fr3.ent$i -width 40
        grid  .sts.fr3.ent$i -column 1 -row $i
      } else {
        grid  .sts.fr4.lab$i -column 0 -row $i -sticky e
        entry .sts.fr4.ent$i
        grid  .sts.fr4.ent$i -column 1 -row $i
      }
    }
    tkwait visibility .sts
  }
}
#--------------------------------------------------------------------------
# ơɽɥͤɽ
#--------------------------------------------------------------------------
proc DispStatus {} {

  global  HEAD TRNS_GET_STATUS response status respdir

  #---- ơɽꥢõ ----
  for { set i 1 } { $i < 7 } { incr i } {
    .sts.fr2.ent$i delete 0 end
  }
  .sts.fr3.ent7 delete 0 end
  .sts.fr3.ent8 delete 0 end
  .sts.fr3.ent9 delete 0 end
  for { set i 10 } { $i < 23 } { incr i } {
    .sts.fr4.ent$i delete 0 end
  }

  #---- ơ׵ᥳޥɤ ----
  set  hdr_bin $HEAD
  set  len_bin [ binary format "I" 0x00000008 ]
  set  req_bin $TRNS_GET_STATUS
  set  para1   [ binary format "I" 0x00000001 ]

  append cmd $hdr_bin $len_bin $req_bin $para1
  set  response ""
  set  ret_sts  [ request_send $cmd ]
  set  resp_len [ string length $response ]

  switch $ret_sts {
    1 { .sts.fr2.ent1 insert 0 "Фʤ" }
    2 { .sts.fr2.ent1 insert 0 "إå 0xaabbccdd ǤϤʤ" }
    3 { .sts.fr2.ent1 insert end [ HonyakuError $status ] 
        .sts.fr2.ent2 insert 0 $resp_len }
    0 { .sts.fr2.ent1 insert end "0 ()"
        .sts.fr2.ent2 insert 0 $resp_len
        #--- 쥹ݥ󥹤ե˳Ǽ
        set   fname  [ file join $respdir respons.bin ]
        set   fileid [ open $fname w+ ]
        puts $fileid $response
        set  fsize  [expr [tell $fileid] -1]
        close $fileid

        #---- 쥹ݥ󥹤ͤե뤫ɤ߹ɽ ----
        set  fileid [ open      $fname r ]
        set  fsize  [ file size $fname ]
        if { $fsize == 0 } { return }
        if { $fsize >= 4 } {
          seek $fileid 0    ;# ³桼
          binary scan [read $fileid 4] I work3
          .sts.fr2.ent3 insert 0 $work3
        }
        if { $fsize >= 6 } {
          seek $fileid 4
          set strlist [list [ split [gets $fileid] \x00 ]]
          set joinlist [join $strlist]
          .sts.fr2.ent4 insert 0 [lindex $joinlist 0]  ;# 桼̾
          .sts.fr2.ent5 insert 0 [lindex $joinlist 1]  ;# 饤̾
          .sts.fr2.ent6 insert 0 [lindex $joinlist 2]  ;# 饤IP
          .sts.fr3.ent7 insert 0 [lindex $joinlist 3]  ;# ƥ༭
          .sts.fr3.ent8 insert 0 [lindex $joinlist 4]  ;# ej
          .sts.fr3.ent9 insert 0 [lindex $joinlist 5]  ;# je

          set  zindex 4
          for { set i 0 } { $i < 7 } { incr i } {
            seek $fileid $zindex
            set  fp [tell $fileid]
            set  zindex [expr $zindex + [string first \x00 [gets $fileid]] + 1]
          }
          for { set i 10 } { $i < 23 } { incr i } {
            seek $fileid $fp
            binary scan [read $fileid 4] I work
            .sts.fr4.ent$i insert 0 $work
            incr fp 4
          }
        }
        close $fileid
      }
  }
}
#--------------------------------------------------------------------------
# ʸե
#--------------------------------------------------------------------------
proc LoadOrigin {} {

  global  tcl_platform

  switch $tcl_platform(platform) {
    unix      -
    windows   { set ftypes {
                    {"ե"           *       }
                    {"ƥȥե"     {.txt}  }
                    {"C쥽ե"  {.c}    }
                    {"إåե"     {.h}    }
                    {"HTMLե"         {.html} }
                    {"SGMLե"         {.sgml} }
                }
              }
    macintosh { set ftypes {
                    {{All Files}      *            }
                    {{Text File}      {.txt}       }
                    {{Text File}      {}      TEXT }
                    {{C Source Files} {.c}         }
                    {{C Source Files} {}      TEXT }
                    {{Header File}    {.h}         }
                    {{HTML File}      {.html}      }
                    {{SGML File}      {.sgml}      }
                }
              }
  }
  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 [ .pw.pane1.in.txt index insert ]
          .pw.pane1.in.txt insert $idx $contents
      }
  }
}
#--------------------------------------------------------------------------
# ʸե¸
#--------------------------------------------------------------------------
proc SaveOrigin {} {

  global  tcl_platform

  switch $tcl_platform(platform) {
    unix      -
    windows   { set ftypes {
                    {"ե"       *      }
                    {"ƥȥե" {.txt} }
                }
              }
    macintosh { set ftypes {
                    {{All Files} *           }
                    {{Text File} {.txt}      }
                    {{Text File} {}     TEXT }
                }
              }
  }
  set fname [tk_getSaveFile -filetypes $ftypes -parent . \
             -initialfile NoTitle.txt -defaultextension .txt ]
  if { $fname == "" } {
      return
  } else {
      set   contents [ .pw.pane1.in.txt get 1.0 end ]
      set   fileid   [ open $fname "w" ]
      puts  -nonewline $fileid $contents
      close $fileid
  }
}
#--------------------------------------------------------------------------
# ʸե¸
#--------------------------------------------------------------------------
proc SaveTrans {} {

  global  tcl_platform

  switch $tcl_platform(platform) {
    unix      -
    windows   { set ftypes {
                    {"ե"       *      }
                    {"ƥȥե" {.txt} }
                }
              }
    macintosh { set ftypes {
                    {{All Files} *           }
                    {{Text File} {.txt}      }
                    {{Text File} {}     TEXT }
                }
              }
  }
  set fname [tk_getSaveFile -filetypes $ftypes -parent . \
             -initialfile NoTitle.txt -defaultextension .txt ]
  if { $fname == "" } {
      return
  } else {
      set   contents [ .pw.pane2.out.txt get 1.0 end ]
      set   fileid   [ open $fname "w" ]
      puts  -nonewline $fileid $contents
      close $fileid
  }
}
#--------------------------------------------------------------------------
# Сɽ
#--------------------------------------------------------------------------
proc DispVersion {} {

  global    tcl_version     tk_version     tkh_version  gifdir
  global    tcl_patchLevel  tk_patchLevel  tcl_platform

  toplevel  .ver
  wm title  .ver "TkHonyaku Version"

  text   .ver.txt -width 40
  pack   .ver.txt -fill both -expand yes
  button .ver.but -text Ĥ -command { destroy .ver }
  pack   .ver.but -padx 10 -pady 10

  image create photo tkh_image -file [ file join $gifdir tkhonyaku.gif ]
  .ver.txt image create insert -image tkh_image -name tkhimage

  set text1 " Tk version $tkh_version"
  set text2 "   ҡ <kawagisi@yk.rim.or.jp>"
  set text3 "   http://www.yk.rim.or.jp/~kawagisi/"
  set text4 " Platform: $tcl_platform(platform) , $tcl_platform(machine)"
  set text5 " OSμ: $tcl_platform(os)"
  set text6 " OS ΥС: $tcl_platform(osVersion)"
  set text7 " TclΥС: $tcl_patchLevel"
  set text8 " Tk ΥС: $tk_patchLevel"
  set week  {        }
  set current [ clock seconds ]
  set youbi   [ lindex $week [ clock format $current -format "%w" ]]
  set current [ clock seconds ]
  set text9   [ clock format $current \
                -format " %Yǯ%m%d($youbi) %H%Mʬ" ]
  .ver.txt insert end [ format "%s\n\n%s\n%s\n\n%s\n%s\n%s\n%s\n%s\n\n%s" \
           $text1 $text2 $text3 $text4 $text5 $text6 $text7 $text8 $text9]
  .ver.txt configure -height 15 -state disabled
}
#--------------------------------------------------------------------------
# إɽ
#--------------------------------------------------------------------------
proc DispHelp {} {

  global  gifdir helpdir

  if { [winfo exists .help] == 0 } {
    toplevel  .help
    wm title  .help "TkHonyaku Help"

    text .help.txt -height 15 -width 80 -yscrollcommand ".help.yscrl set"
    mscroll   .help.txt
    scrollbar .help.yscrl -command ".help.txt yview"
    pack      .help.yscrl -side right -fill y
    pack .help.txt -fill both -expand yes

    image create photo tkh_image -file [ file join $gifdir tkhonyaku.gif ]
    .help.txt image create insert -image tkh_image -name tkhimage
    .help.txt insert end \n

    set   helpfile [ open [ file join $helpdir tkhonyaku-readme ] r ]
    set   helpmsg  [ read -nonewline $helpfile ]
    close $helpfile
    .help.txt insert end $helpmsg
#    .help.txt configure -state disabled
# disabled  Windows ξɥåǤʤޥۥ뤬Ȥʤ
  }
}
#===========================================================================
# Υڥ󼰥ɥդΡȥ֥åӥ顼Ϣ
# ΥץϡMark Harrison ʪǤʰ¤Ƥޤˡ
# http://www.awl.com/cp/efftcl/efftcl.html ǸƤޤ
#
# ͡Ƕɤν Effective Tcl/Tk
#       Mark Harrison/Michael McLennan  ˮ: ISBN4-7561-3165-4
# ----------------------------------------------------------------------
#  panedwindow facility, notebook, tabnotebook, HSB color editor
# ----------------------------------------------------------------------
#  Effective Tcl/Tk Programming
#    Mark Harrison, DSC Communications Corp.
#    Michael McLennan, Bell Labs Innovations for Lucent Technologies
#    Addison-Wesley Professional Computing Series
# ======================================================================
#  Copyright (c) 1996-1997  Lucent Technologies Inc. and Mark Harrison
# ======================================================================

#===========================================================================
# ڥ󼰥ɥ (ķ)
#===========================================================================
proc panedwindow_create {win width height} {

    global pwInfo

    frame $win -class Panedwindow -width $width -height $height
    frame $win.pane1
    place $win.pane1 -relx 0.5 -rely 0 -anchor n -relwidth 1.0 -relheight 0.5
    frame $win.pane2
    place $win.pane2 -relx 0.5 -rely 1.0 -anchor s -relwidth 1.0 -relheight 0.5

    frame $win.sash -height 4 -borderwidth 2 -relief sunken
    place $win.sash -relx 0.5 -rely 0.5 -relwidth 1.0 -anchor c

    frame $win.grip -width 10 -height 10 -borderwidth 2 -relief raised
    place $win.grip -relx 0.95 -rely 0.5 -anchor c

    bind  $win.grip <ButtonPress-1>   "panedwindow_grab $win"
    bind  $win.grip <B1-Motion>       "panedwindow_drag $win %X %Y"
    bind  $win.grip <ButtonRelease-1> "panedwindow_drop $win %X %Y"

    return $win
}
#===========================================================================
# ڥ󼰥ɥΥåסʤĤޤߡˤؤ褦ɽ
#===========================================================================
proc panedwindow_grab {win} {
    $win.grip configure -relief sunken
}
#===========================================================================
# ڥ󼰥ɥΥåסʤĤޤߡˤɥå
#===========================================================================
proc panedwindow_drag {win x y} {

  global  win_dir

  if { $win_dir == "V" } {
      set realY [expr $y-[winfo rooty $win]]
      set Ymax  [winfo height $win]
      set frac  [expr double($realY)/$Ymax]
      if {$frac < 0.05} {
          set frac 0.05
      }
      if {$frac > 0.95} {
          set frac 0.95
      }
      place $win.sash -rely $frac
      place $win.grip -rely $frac
  } else {
      set realX [expr $x-[winfo rootx $win]]
      set Xmax  [winfo width $win]
      set frac  [expr double($realX)/$Xmax]
      if {$frac < 0.05} {
          set frac 0.05
      }
      if {$frac > 0.95} {
          set frac 0.95
      }
#      place $win.sash -relx $frac
      place $win.grip -relx $frac
  }
    return $frac
}
#===========================================================================
# ڥ󼰥ɥΥåסʤĤޤߡˤɥåפ
#===========================================================================
proc panedwindow_drop {win x y} {

  set frac [panedwindow_drag $win $x $y]
  panedwindow_divide $win $frac
  $win.grip configure -relief raised
}
#===========================================================================
# ڥΥĴ
#===========================================================================
proc panedwindow_divide {win frac} {

  global  win_dir

  if { $win_dir == "V" } {
      place $win.sash  -rely $frac
      place $win.grip  -rely $frac
      place $win.pane1 -relheight $frac
      place $win.pane2 -relheight [expr 1-$frac]
  } else {
#     place $win.sash  -relx $frac
      place $win.grip  -relx $frac
      place $win.pane1 -relwidth $frac
      place $win.pane2 -relwidth [expr 1-$frac]
  }
}
#===========================================================================
# Ρȥ֥å
#===========================================================================
proc notebook_create {win} {

  global nbInfo

  frame  $win -class Notebook
  pack   propagate $win 0

  set    nbInfo($win-count)   0    ;# ڡ
  set    nbInfo($win-pages)   ""   ;# ڡΥꥹ
  set    nbInfo($win-current) ""   ;# ߤΥڡ̾
  return $win                      ;# Ρȥ֥å֤̾
}
#===========================================================================
# Ρȥ֥å˿ڡä
#===========================================================================
proc notebook_page {win name} {

  global nbInfo

  set page "$win.page[incr nbInfo($win-count)]"
  lappend nbInfo($win-pages) $page
  set     nbInfo($win-page-$name) $page
  frame $page
  if {$nbInfo($win-count) == 1} {
      after idle [list notebook_display $win $name]
  }
  return $page
}
#===========================================================================
# Ρȥ֥åΥڡɽ
#===========================================================================
proc notebook_display {win name} {

  global nbInfo

  set page ""
  if {[info exists nbInfo($win-page-$name)]} {
        set page $nbInfo($win-page-$name)
  } elseif {[winfo exists $win.page$name]} {
        set page $win.page$name
  }
  if {$page == ""} {
    error "bad notebook page \"$name\""
  }

  notebook_fix_size $win

  if {$nbInfo($win-current) != ""} {
      pack forget $nbInfo($win-current)
  }
  pack $page -expand yes -fill both
  set nbInfo($win-current) $page
}
#===========================================================================
# Ρȥ֥åŬڤʥ
#===========================================================================
proc notebook_fix_size {win} {

  global nbInfo

  update idletasks

  set maxw 0
  set maxh 0
  foreach page $nbInfo($win-pages) {
      set w [winfo reqwidth $page]
      if {$w > $maxw} {
          set maxw $w
      }
      set h [winfo reqheight $page]
      if {$h > $maxh} {
          set maxh $h
      }
  }
  set bd   [$win cget -borderwidth]
  set maxw [expr $maxw+2*$bd]
  set maxh [expr $maxh+2*$bd]
  $win configure -width $maxw -height $maxh
}
#===========================================================================
# ֥Ρȥ֥å
#===========================================================================
proc tabnotebook_create {win} {

  global tnInfo

  frame  $win -class Tabnotebook
  canvas $win.tabs -highlightthickness 0
  pack   $win.tabs -fill x

  notebook_create $win.notebook
  pack   $win.notebook -expand yes -fill both

  set tnInfo($win-tabs)    ""    ;# ̾Υꥹ
  set tnInfo($win-current) ""    ;# ɽƤڡΥ̾
  set tnInfo($win-pending) ""    ;# 
  return $win
}
#===========================================================================
# ֥Ρȥ֥åΥڡ
#===========================================================================
proc tabnotebook_page {win name} {

  global tnInfo

  set page [notebook_page $win.notebook $name]
  lappend tnInfo($win-tabs) $name

  if {$tnInfo($win-pending) == ""} {
      set id [after idle [list tabnotebook_refresh $win]]
      set tnInfo($win-pending) $id
  }
  return $page
}
#===========================================================================
# ֤ΥåȤ褹
#===========================================================================
proc tabnotebook_refresh {win} {

  global tnInfo

  $win.tabs delete all
  set  margin [option get $win margin   Margin]
  set  color  [option get $win tabColor Color ]
  set  x      2
  set  maxh   0

  foreach name $tnInfo($win-tabs) {
      set id [$win.tabs create text \
             [expr $x+$margin+2] [expr -0.5*$margin] \
             -anchor sw -text $name -tags [list $name]]
      set bbox [$win.tabs bbox $id]
      set wd   [expr [lindex $bbox 2]-[lindex $bbox 0]]
      set ht   [expr [lindex $bbox 3]-[lindex $bbox 1]]
      if {$ht > $maxh} {
          set maxh $ht
      }

      $win.tabs create polygon 0 0 $x 0 \
          [expr $x+$margin]     [expr -$ht-$margin] \
          [expr $x+$margin+$wd] [expr -$ht-$margin] \
          [expr $x+$wd+2*$margin] 0 \
          2000 0 2000 10 0 10 \
          -outline black -fill $color \
          -tags [list $name tab tab-$name]

      $win.tabs raise $id

      $win.tabs bind $name <ButtonPress-1> \
       [list tabnotebook_display $win $name]

      set x [expr $x+$wd+2*$margin]
  }
  set height [expr $maxh+2*$margin]
  $win.tabs move all 0 $height

  $win.tabs configure -width $x -height [expr $height+4]

  if {$tnInfo($win-current) != ""} {
      tabnotebook_display $win $tnInfo($win-current)
  } else {
      tabnotebook_display $win [lindex $tnInfo($win-tabs) 0]
  }
  set tnInfo($win-pending) ""
}
#===========================================================================
# ڡ򤹤
#===========================================================================
proc tabnotebook_display {win name} {

  global tnInfo

  notebook_display $win.notebook $name

  set  normal [option get $win tabColor Color]
  $win.tabs itemconfigure tab -fill $normal

  set  active [option get $win activeTabColor Color]
  $win.tabs itemconfigure tab-$name -fill $active
  $win.tabs raise $name

  set tnInfo($win-current) $name
}
#--------------------------------------------------------------------------
# 
#--------------------------------------------------------------------------
proc SelectColor {} {

  global  newcolor

  toplevel .sc
  wm title .sc "TkHonyaku Color Selecter"

  colordial_create .sc.cd
  pack     .sc.cd
  button   .sc.ok   -text OK \
                    -command { set newcolor [ colordial_get .sc.cd ]
                               set x 1 }
  button   .sc.quit -text 󥻥 \
                    -command { set newcolor ""
                               set x 1 }
  pack     .sc.ok .sc.quit -side left -fill both -expand yes -padx 4  -pady 4

  vwait   x
  destroy .sc
  return  $newcolor
}
#===========================================================================
# 顼
#===========================================================================
proc colordial_create {win} {

#    global env cdInfo
    global gifdir cdInfo

    frame $win -class Colordial

    canvas $win.dial
    pack $win.dial -side bottom

    label $win.label -text "Color:"
    pack $win.label -side left

    frame $win.sample -width 15 -height 15
    pack $win.sample -expand yes -fill both -padx 4 -pady 4

#    set fname [file join $env(EFFTCL_LIBRARY) images colors.gif]
    set fname [file join $gifdir colors.gif]
    set imh [image create photo -file $fname]
    $win.dial create image 0 0 -anchor nw -image $imh
    $win.dial create oval 0 0 0 0 -fill black -tags hsval
    $win.dial create line 0 0 0 0 -width 4 -fill black -tags bval

    $win.dial configure -width [image width $imh] \
        -height [image height $imh]

    $win.dial bind hsval <B1-Motion> \
        "colordial_set_hs $win %x %y"
    $win.dial bind bval <B1-Motion> \
        "colordial_set_b $win %y"

    set cdInfo($win-hue) 0
    set cdInfo($win-saturation) 0
    set cdInfo($win-brightness) 1

    colordial_refresh $win
    return $win
}
#===========================================================================
# 顼 Brightness 
#===========================================================================
proc colordial_set_b {win y} {
    global cdInfo

    set bright [expr (145-$y)/140.0]
    if {$bright < 0} {
        set bright 0
    } elseif {$bright > 1} {
        set bright 1
    }
    set cdInfo($win-brightness) $bright
    colordial_refresh $win
}
#===========================================================================
# 顼 Hue, Saturation 
#===========================================================================
proc colordial_set_hs {win x y} {
    global cdInfo

    set hs [colordial_xy2hs $x $y]
    set hue [lindex $hs 0]
    set sat [lindex $hs 1]

    if {$sat > 1} {
        set sat 1
    }
    set cdInfo($win-hue) $hue
    set cdInfo($win-saturation) $sat

    colordial_refresh $win
}
#===========================================================================
# 顼 ɽ
#===========================================================================
proc colordial_refresh {win} {
    global cdInfo

    set angle $cdInfo($win-hue)
    set length $cdInfo($win-saturation)
    set x0 [expr 75 + cos($angle)*$length*70]
    set y0 [expr 75 - sin($angle)*$length*70]

    $win.dial coords hsval \
        [expr $x0-4] [expr $y0-4] \
        [expr $x0+4] [expr $y0+4]

    set bright $cdInfo($win-brightness)
    set y0 [expr 145-$bright*140]

    $win.dial coords bval 154 $y0 176 $y0

    $win.sample configure -background [colordial_get $win]
}
#===========================================================================
# 顼  䤤碌
#===========================================================================
proc colordial_get {win} {
    global cdInfo

    set h $cdInfo($win-hue)
    set s $cdInfo($win-saturation)
    set v $cdInfo($win-brightness)
    return [colordial_hsb2rgb $h $s $v]
}
#===========================================================================
# 顼  Hue, Saturation  (2)
#===========================================================================
proc colordial_xy2hs {x0 y0} {
    set wd 150
    set ht 150
    set margin 5

    set x [expr $x0 - $wd/2]
    set y [expr $ht/2 - $y0]
    if {$x != 0} {
        set angle [expr atan($y/double($x))]
        if {$x < 0} {
            set angle [expr $angle+3.14159]
        } elseif {$y < 0} {
            set angle [expr $angle+6.28318]
        }
        set dx [expr 0.5*cos($angle)*($wd-2*$margin)]
        set dy [expr 0.5*sin($angle)*($ht-2*$margin)]
        set len [expr sqrt($dx*$dx+$dy*$dy)]
        set hue [expr $angle/3.14159*180]
        set sat [expr sqrt($x*$x+$y*$y)/$len]
    } elseif {$y > 0} {
        set hue 90
        set len [expr 0.5*($ht-2*$margin)]
        set sat [expr $y/$len]
    } else {
        set hue 270
        set len [expr 0.5*($ht-2*$margin)]
        set sat [expr -$y/$len]
    }
    set hue [expr $hue*3.14159/180.0]

    return [list $hue $sat]
}
#===========================================================================
# 顼  HSB --> RGB Ѵ
#===========================================================================
proc colordial_hsb2rgb {h s v} {
    if {$s == 0} {
        set v [expr round(65535*$v)]
        set r $v
        set g $v
        set b $v
    } else {
        if {$h >= 6.28318} {set h [expr $h-6.28318]}
        set h [expr $h/1.0472]
        set f [expr $h-floor($h)]
        set p [expr round(65535*$v*(1.0-$s))]
        set q [expr round(65535*$v*(1.0-$s*$f))]
        set t [expr round(65535*$v*(1.0-$s*(1.0-$f)))]
        set v [expr round(65535*$v)]

        switch [expr int($h)] {
            0 {set r $v; set g $t; set b $p}
            1 {set r $q; set g $v; set b $p}
            2 {set r $p; set g $v; set b $t}
            3 {set r $p; set g $q; set b $v}
            4 {set r $t; set g $p; set b $v}
            5 {set r $v; set g $p; set b $q}
        }
    }
    return [format "#%.4x%.4x%.4x" $r $g $b]
}
#--------------------------------------------------------------------------
# 顼
#--------------------------------------------------------------------------
proc bgerror { msg } {

  global  errorInfo logdir

  set   fname  [ file join $logdir error.log ]
  set   fileid [ open $fname "w+" ]
  puts  $fileid $errorInfo
  close $fileid

  toplevel .err
  wm title .err "TkHonyaku ERROR!"

  frame .err.fr1 ; frame .err.fr2 ; frame .err.fr3 ; frame .err.fr4
  pack  .err.fr1
  pack  .err.fr2 -side top -fill both -expand yes
  pack  .err.fr3 .err.fr4

  label .err.fr1.lab -text "顼ȯޤ"
  pack  .err.fr1.lab

  text  .err.fr2.txt -width 40 -height 10 -wrap none \
                     -xscrollcommand ".err.fr2.xscrol set" \
                     -yscrollcommand ".err.fr2.yscrol set"
  mscroll   .err.fr2.txt
  scrollbar .err.fr2.xscrol -command ".err.fr2.txt xview" -orient horizontal
  scrollbar .err.fr2.yscrol -command ".err.fr2.txt yview"
  pack .err.fr2.xscrol -side bottom -fill x
  pack .err.fr2.yscrol -side right  -fill y
  pack .err.fr2.txt    -fill both -expand yes
  .err.fr2.txt insert 1.0 $errorInfo
  .err.fr2.txt configure -state disabled

  label  .err.fr3.lab -text "Ƥ $fname ¸ޤ"
  button .err.fr4.but -text "λ" -command "destroy .err"
  pack   .err.fr3.lab .err.fr4.but -side top
}
#----------------------------------------------------------------------------
# ޥɥ饤ѥ᡼å
#
#    ֤ = "GUI"   : GUI ǽ³ 
#           = "noGUI" : GUI ̵ե
#           = "QUIT"  : 򤻤˽λ
#----------------------------------------------------------------------------
proc Check_cmdline {} {

  global  argc argv mode style tkh_version input_file output_file helpdir
  global  cur_mode cur_style

  set help_msg1 "  ====== TkHonyaku version $tkh_version ======"
  set help_msg2 {
  tkhonyaku  [i inputfile] [o outputfile] [b buffer]
             [m ej] [m je] [f only] [f both] [d] [doc] [h] [help] [?]
  ------------------------------------------------------------------------
  (No option)           : TkHonyaku will start with GUI
  {i | in}  input_file  : Input (Original Text) file name
  {o | out} output_file : Output (Translated text) file name
                        : (default: ~/.tkhonyaku/honyaku.txt)
  {m | mode} {ej | je}  : translation Mode
                        :   "ej" (translate English to Japanese : default)
                        :   "je" (translate Japanese to English)
  {f | format} {both | only} : output file Format
                        : "both" (BOTH Original and Translated text : default)
                        : "only" (ONLY Translated text)
  {d | doc}             : open KTERM and print Document
  {h | help | ?}        : print Help (this Help message)
  ------------------------------------------------------------------------
  <For Example>
  1) $ tkhonyaku
           ; work with GUI (Graphical User Interface)
  2) $ tkhonyaku i english.txt o japan.txt
           ; No GUI. Translate english.txt (English) in current directory.
           ; and output into   japan.txt  (Japanese) in current directory.
           ; Output file format is BOTH original and translated text
  3) $ tkhonyaku i /tmp/english.txt
           ; No GUI. Translate /tmp/english.txt (English).
           ; and output into ~/.tkhonyaku/honyaku.txt (Japanese).
           ; Output file format is BOTH original and translated text.
  4) $ tkhonyaku m je f only i /tmp/japan.txt
           ; No GUI. Translate /tmp/japan.txt (Japanese).
           ; and output into ~/.tkhonyaku/honyaku.txt (English).
           ; Output file format is ONLY Translated text.
  5) $ tkhonyaku d &
           ; Open KTERM and display the full Document (using "less" command).
           ; To close KTERM window, press "Q" key.
  6) $ tkhonyaku help
     $ tkhonyaku h
     $ tkhonyaku ?
           ; print this Help message to KTERM
  }
  append help_msg $help_msg1 $help_msg2
  set ret_val     "noGUI"
  set opt_flag    ""
  set input_file  ""
  set output_file ""
  set cur_mode    "ej" ;# ⡼ ("ej" :  Ѵ, "je" : Ѵ)
  set cur_style   "A"  ;# ʸϥ ("A":, "O":ʸΤ)

  if { $argc > 0 } {
    foreach opt $argv {
      switch $opt {
          "i"      -
          "in"     { set opt_flag "I" }
          "o"      -
          "out"    { set opt_flag "O" }
          "b"      -
          "buffer" { set opt_flag "B" }
          "m"      -
          "mode"   { set opt_flag "M" }
          "f"      -
          "format" { set opt_flag "F" }
          "?"      -
          "h"      -
          "help"   { puts $help_msg ; set ret_val "QUIT" ; break }
          "d"      -
          "doc"    { exec kterm -e less [file join $helpdir tkhonyaku-readme]
                     set  ret_val "QUIT"
                     break }
          default {
            if {$opt_flag == "I" } {
                set opt_flag ""
                if { [file readable $opt] } {
                    set input_file $opt
                } else {
                    puts ":tkhonyaku: $opt: No such file or directory !
:tkhonyaku: ꤵ줿ե(ޤϥǥ쥯ȥ) $opt ¸ߤޤ
            (ޤϥʤ⤷ޤ)"
                    set  ret_val "QUIT"
                    break
                }
            } elseif {$opt_flag == "O" } {
                set opt_flag ""
                if { [file exists [file dirname $opt]] } {
                    set output_file $opt
                } else {
                    puts ":tkhonyaku: $opt: No such directory !
:tkhonyaku: ꤵ줿ǥ쥯ȥ $opt ¸ߤޤ
            (ޤϥʤ⤷ޤ)"
                    set  ret_val "QUIT"
                    break
                }
            } elseif { $opt_flag == "M" } {
                set opt_flag ""
                if { $opt == "je"} {
                    set cur_mode je
                } else {
                    set cur_mode ej
                }
            } elseif { $opt_flag == "F" } {
                set opt_flag ""
                if { $opt == "both"} {
                    set cur_style "A"
                } else {
                    set cur_style "O"
                }
            } else {
                 puts $help_msg
                 set  ret_val "QUIT"
                 break
            }
          }
      }
    }
  } else {
    set ret_val "GUI"
  }
  return $ret_val
}
#----------------------------------------------------------------------------
#  (GUI ̵ʸեϡʸե)
#----------------------------------------------------------------------------
proc Trans_FileToFile { input_file output_file } {

  global  server port mode style ejmode jemode response respdir conf_file
  global  cur_mode cur_style

  # 쥹ݥ󥹥ե(respons.bin)ѥǥ쥯ȥγǧ
  if { [file exists $respdir] == 0 } {
      file mkdir $respdir
  }
  if [ file exists $conf_file ]  {   ;# ե뤬ɤ߹
       source $conf_file
  } else {
      SetDefault   ;# ǥեͤꤹ
      SaveConf     ;# ͤե˳Ǽ
  }
  set mode  $cur_mode  ;# ޥɥ饤ޤϥǥեȤ⡼
  set style $cur_style ;# ޥɥ饤ޤϥǥեȤʸϥ

  Connect_Server                             ;# Ф³
  if { [ init ]        != 0 } { return }     ;# ѳ
  if { [ setdictname ] != 0 } { return }     ;# Ѽ
  if { [ set_timeout ] != 0 } { return }     ;# ॢȤ
  if { [ setmode ]     != 0 } { return }     ;# ⡼ɤΥå
  if { [ clear_cache ] != 0 } { return }     ;# 񥭥åΥꥢ
  if {$input_file != ""} {                   ;# ʸեϤ
      set   in_fileid [ open $input_file "r" ]
      set   original  [ read -nonewline $in_fileid ]
      close $in_fileid
  }
  #--- ʸ
  set   trimd_text ""
  set   split_text [split $original \n]        ;# ʸ1ԤĤʬ䤹
  foreach work $split_text {
      append trimd_text [string trim $work] \n ;# Ƭȹζ
  }
  trns_divide_sentence $trimd_text             ;# ʸʬ

  #--- ʸʬΥ쥹ݥ󥹤ե˳Ǽ
  set   fname  [ file join $respdir respons.bin ]
  set   fileid [ open $fname w+ ]
  puts $fileid $response
  set  fsize  [expr [tell $fileid] -1]

  #--- 쥹ݥ󥹤Ƭ4Х(ʸο)򥹥å
  if { $fsize == 0 } { return }
  if { $fsize > 4 } {
      seek $fileid 4
      set alltext [string trim [read -nonewline $fileid] \x00 ]
  }
  close  $fileid

  if {$output_file == ""} {                  ;# ʸϥե
      set output_file ~/.tkhonyaku/honyaku.txt
  }
  set  out_fileid [ open $output_file "a+" ]
  puts $out_fileid "\nʸե̾ $input_file \n"

  set  trimtext [ split [string trim $alltext \n] \n]  ;# ʸ
  foreach work $trimtext {
    set info1 [ string index $work 0]  ;# 1
    set info2 [ string index $work 1]  ;# 2
    set sentence [ string range $work 2 end]
    if { ($info1 == "t") || ($info1 == "l") || ($info1 == "i") } {
      if { $style == "A" } then {
        if { $mode == "ej" } then {
            puts $out_fileid "\[\] $sentence"
            puts $out_fileid "\[\] $sentence"
        } else {
            puts $out_fileid "\[\] $sentence"
            puts $out_fileid "\[\] $sentence"
        }
      } else {
            puts $out_fileid "$sentence"
      }
      continue
    }
    translate_one $sentence    ;# ʸ
    #--- ʸΥ쥹ݥ󥹤ե˳Ǽ
    set   fname  [ file join $respdir respons.bin ]
    set   fileid [ open $fname w+ ]
    puts $fileid $response
    set  fsize  [expr [tell $fileid] -1]

    #--- 쥹ݥ󥹤Ƭ 2ХȤ򥹥å
    if { $fsize == 0 } { return }
    if { $fsize >= 2 } {
        seek $fileid 2    ;# 
        regsub -all "\n" [read $fileid] "" output    ;# Ե
    }
    close  $fileid

    if { $style == "A" } then {
        if { $mode == "ej" } then {
            puts $out_fileid "\[\] $sentence"
            puts $out_fileid "\[\] $output"
        } else {
            puts $out_fileid "\[\] $sentence"
            puts $out_fileid "\[\] $output"
        }
    } else {
            puts $out_fileid "$output"
    }
  }
  close  $out_fileid
  host_close        ;# ѽλ
}
#--------------------------------------------------------------------------
# ᥤ (2)
#--------------------------------------------------------------------------
  switch [Check_cmdline] {  ;# ޥɥ饤ѥ᡼å
    "noGUI" { Trans_FileToFile $input_file $output_file ; exit }
    "QUIT"  { exit }
  }

  wm title    . "TkHonyaku  $tkh_version"
  wm geometry .
  .  configure -cursor hand2

  set disp_debug   "no"  ;# ǥХåѥɥɽʤ
  set mainw_width  ""    ;# ᥤ󥦥ɥ
  set mainw_height ""    ;# ᥤ󥦥ɥι⤵
  set socketid     ""    ;# åID
  set window_bg    ""    ;# ɥطʿ
  set allbut_bg    ""    ;# ܥطʿ
  set allbut_fg    ""    ;# ܥʸ
  set radbut_bg    ""    ;# 饸ܥο
  set sel_trans    "no"  ;# ϰϤ
  set win_dir      "V"   ;# ɥ=
  set use_server   0     ;# лե饰򥯥ꥢ

  switch $tcl_platform(platform) {
    unix      { set username $env(USER) }  ;# 桼̾
    windows   { set username winuser    }  ;# 桼̾
    macintosh { set username macuser    }  ;# 桼̾
  }
  # Tcl СȽѥե饰 (8.0.5 ξ "8.0")
  set tcl_ver_flag [string range $tcl_patchLevel 0 2]

  # 쥹ݥ󥹥ե(respons.bin)ѥǥ쥯ȥγǧ
  if { [file exists $respdir] == 0 } {
      file mkdir $respdir
  }
  if [ file exists $conf_file ]  {   ;# ե뤬ɤ߹
       source $conf_file
      if { $window_bg != ""  } {     ;# ɥطʿ
          tk_setPalette $window_bg
      }
      if { $allbut_bg != "" } {      ;# ܥطʿ
          option add *Button.background $allbut_bg
      }
      if { $allbut_fg != "" } {      ;# ܥʸ
          option add *Button.foreground $allbut_fg
      }
      if { $radbut_bg != "" } {      ;# 饸ܥ󡢥åܥο
          option add *Radiobutton.selectColor $radbut_bg
          option add *Checkbutton.selectColor $radbut_bg
      }
      # ΥС󤫤饢åץ졼ɤб
      if { ![info exists disp_debug] } { set disp_debug no  }
      if { ![info exists auto_dic  ] } { set auto_dic   OFF }
  } else {
      SetDefault   ;# ǥեͤꤹ
      SaveConf     ;# ͤե˳Ǽ
  }

  #=======================================================================
  # ǥХåѥɥ
  #=======================================================================
  toplevel  .deb
  wm title  .deb "TkHonyaku Debug"
  text      .deb.txt -width 80 -height 10 -wrap char \
                     -yscrollcommand ".deb.yscrol set"
  mscroll   .deb.txt
  scrollbar .deb.yscrol -command ".deb.txt yview"
  pack      .deb.yscrol -side right -fill y
  pack      .deb.txt    -fill both -expand yes
  set library  [info library]
  set script   [info script ]
  set hostname [info hostname]
#  set envilon  [parray env]
#  set username $env(USERNAME)    ;# () Win2KˤϤ뤬 Win95 ˤϤʤ
  .deb.txt insert end "library  = $library  \n"  ;# -- for DBUG ---
  .deb.txt insert end "script   = $script   \n"  ;# -- for DBUG ---
  .deb.txt insert end "hostname = $hostname \n"  ;# -- for DBUG ---
#  .deb.txt insert end "envilon  = $envilon  \n"  ;# -- for DBUG ---
  .deb.txt insert end "username = $username \n"  ;# -- for DBUG ---
  if {$disp_debug != "yes"} {
    wm withdraw .deb
  }
  # ǥХåѥɥĤ˽λ򤹤롣
  wm protocol .deb WM_DELETE_WINDOW { \
     set disp_debug "no"
     wm  withdraw .deb
  }

  frame  .fr1
  pack   .fr1 -side top -anchor w -padx 6 -pady 2

  button .fr1.ej     -text  -command { set  mode "ej"
                                           translation }
  button .fr1.ejsel  -text (ϰ) -command { 
                         if { [selection get] != ""} {
                             set  sel_trans yes
                             set  mode "ej"
                             translation
                         }
                     }
  button .fr1.delin  -text ʸõ -command {.pw.pane1.in.txt delete 1.0 end}
  button .fr1.yaku   -text (EJ) -command { Re_trans ej }
  button .fr1.dict   -text  -command { Use_dict }
  label  .fr1.file   -text ե -relief raised
  button .fr1.setbut -text      -command { Setting }
  pack   .fr1.ej .fr1.ejsel .fr1.delin .fr1.yaku .fr1.dict .fr1.file \
         .fr1.setbut  -side left -fill both -expand yes
  frame  .fr2
  pack   .fr2 -side top -anchor w -padx 6

  button .fr2.je     -text  -command { set  mode "je"
                                           translation }
  button .fr2.ejsel  -text (ϰ) -command {
                         if { [selection get] != ""} {
                             set  sel_trans yes
                             set  mode "je"
                             translation
                         }
                     }
  button .fr2.delout -text ʸõ -command {.pw.pane2.out.txt delete 1.0 end}
  button .fr2.hinsi  -text (JE) -command { Re_trans je }
  button .fr2.stsbut -text ơ -command { OpenStsWindow
                                                 Connect_Server
                                                 DispStatus }
  label  .fr2.help   -text إ -relief raised
  button .fr2.exit   -text λ   -command { \
         SaveConf
         if { [info exists socketid] && ($socketid != "") } {
                 host_close    ;# ѽλ
         }
         exit
  }
  pack   .fr2.je .fr2.ejsel .fr2.delout .fr2.hinsi .fr2.stsbut \
         .fr2.help .fr2.exit -side left -fill both -expand yes

  #=======================================================================
  # ڥ󼰥ɥ (ķ)
  #=======================================================================
  option add *Panedwindow.grip.cursor sb_v_double_arrow widgetDefault
  option add *Listbox.background white startupFile

  panedwindow_create .pw 3i 4i
  pack  .pw -expand yes -fill both

  frame .pw.pane1.in
  frame .pw.pane2.out
  pack  .pw.pane1.in .pw.pane2.out -expand yes -fill both -padx 4 -pady 10

  scrollbar .pw.pane1.in.sbar  -command {.pw.pane1.in.txt yview}
  pack      .pw.pane1.in.sbar  -side right -fill y

  text      .pw.pane1.in.txt   -bg $txtin_bg -fg $txtin_fg \
                               -width 80 -height 10 \
                               -yscrollcommand {.pw.pane1.in.sbar set}
  mscroll   .pw.pane1.in.txt
  pack      .pw.pane1.in.txt   -side left -expand yes -fill both

  scrollbar .pw.pane2.out.sbar -command {.pw.pane2.out.txt yview}
  pack      .pw.pane2.out.sbar -side right -fill y

  text      .pw.pane2.out.txt  -bg $txtout_bg -fg $txtout_fg \
                               -width 80 -height 10 \
                               -yscrollcommand {.pw.pane2.out.sbar set}
  mscroll   .pw.pane2.out.txt
  pack      .pw.pane2.out.txt  -side left -expand yes -fill both

  if { $allbut_bg != "" } {      ;# ܥطʿ
      .fr1.file configure -bg $allbut_bg  ;# ե (ܥɤ)
      .fr2.help configure -bg $allbut_bg  ;# إ   (ܥ)
  }
  if { $allbut_fg != "" } {      ;# ܥʸ
      .fr1.file configure -fg $allbut_fg  ;# ե (ܥɤ)
      .fr2.help configure -fg $allbut_fg  ;# إ   (ܥ)
  }
  if { $win_dir == "H"} {        ;# ɥ
      Place "H"
  }
  # ɥΥͤˤ
  if {($mainw_width != "") && ($mainw_height != "")} {
      set cur_rootx    [ winfo rootx . ]
      set cur_rooty    [ winfo rooty . ]
      set new_geometry [ format "%sx%s+%s+%s" \
                         $mainw_width $mainw_height $cur_rootx $cur_rooty ]
      wm geometry . $new_geometry
  }
  menu   .popupF -tearoff no
  .popupF add command -label "ʸե..." -command { LoadOrigin }
  .popupF add command -label "ʸե¸..." -command { SaveOrigin }
  .popupF add command -label "ʸե¸..." -command { SaveTrans }

  menu   .popupV -tearoff no
  .popupV add command -label "С" -command { DispVersion }
  .popupV add command -label "إ"     -command { DispHelp }

  bind   .fr1.file <Button-1> {
         set x [ expr [ winfo rootx %W ] + %x ]
         set y [ expr [ winfo rooty %W ] + %y ]
         tk_popup .popupF $x $y
  }
  bind   .fr2.help <Button-1> {
         set x [ expr [ winfo rootx %W ] + %x ]
         set y [ expr [ winfo rooty %W ] + %y ]
         tk_popup .popupV $x $y
  }
  # ᥤ󥦥ɥĤ˽λ򤹤롣
  wm protocol . WM_DELETE_WINDOW { \
     SaveConf
     if { [info exists socketid] && ($socketid != "") } {
             host_close    ;# ѽλ
     }
     exit
   }
#--- End of TkHonyaku -----------------------------------------------------
