### Copyright (C) 1998 MOTOYAMA, Mashio<moto@ba.mbn.or.jp>
### This program is free software; you can redistribute it and/or modify
### it under the terms of the GNU General Public License as published by
### the Free Software Foundation; either version 2 of the License, or
### (at your option) any later version.
###
### This program is distributed in the hope that it will be useful,
### but WITHOUT ANY WARRANTY; without even the implied warranty of
### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
### GNU General Public License for more details.
###
### You should have received a copy of the GNU General Public License
### along with this program; if not, write to the Free Software

## key binding
proc keyBind {} {
    # キー・バインディング
    Desc "キー・バインディング"
    ShortDesc "キー・バインディング"
    ComboBox wKey\
	    -width 10\
	    -entryhelp {\
	    "\\C-" "Ctrl キー同時押し。後ろにキーを入れます"\
	    "\\S-" "Sift キー同時押し。後ろにキーを入れます"\
	    "\\Space" "Space キーです"\
	    "\\Escape" "ESC キーです"\
	    "\\Tab" "TAB キーです"\
	    "\\Nfer" "Nfer キーです"\
	    "\\Xfer" "Xfer キーです"\
	    "\\Backspace" "BackSpace キーです"\
	    "\\Delete" "Delete キーです"\
	    "\\Insert" "Insert キーです"\
	    "\\Rollup" "Rollup キーです"\
	    "\\Rolldown" "Rolldown キーです"\
	    "\\Up" "カーソル・キー(Up)です"\
	    "\\Left" "カーソル・キー(Left)です"\
	    "\\Right" "カーソル・キー(Right)です"\
	    "\\Down"  "カーソル・キー(Down)です"\
	    "\\Home"  "Home キーです"\
	    "\\Clear" "Clear キーです"\
	    "\\Help" "Help キーです"\
	    "\\Enter" "Enter キーです"\
	    "\\Return" "Return キーです"\
	    "\\F" "Function キーです。後ろに数字を入れます"\
	    "\\Pf" "Programable Function キーです。後ろに数字を付けたします"}\
	    -textafter "キーに"\
	    -help "機能を割り当てるキーを入力します"
    ComboBox wFunc\
	    -entryhelp {\
	    "alpha-mode" "英数字入力モード"\
	    "backward" "一文字戻る"\
	    "base-eisu" "I don\'t know this function, sorry"\
	    "base-hankaku" "I don\'t know this function, sorry"\
	    "base-henkan" "I don\'t know this function, sorry"\
	    "base-hiragana" "I don\'t know this function, sorry"\
	    "base-hiragana-katakana-toggle" "I don\'t know this function, sorry"\
	    "base-kakutei" "I don\'t know this function, sorry"\
	    "base-kakutei-henkan-toggle" "I don\'t know this function, sorry"\
	    "base-kana" "I don\'t know this function, sorry"\
	    "base-kana-eisu-toggle" "I don\'t know this function, sorry"\
	    "base-katakana" "I don\'t know this function, sorry"\
	    "base-rotate-backward" "I don\'t know this function, sorry"\
	    "base-rotate-forward" "I don\'t know this function, sorry"\
	    "base-zenkaku" "I don\'t know this function, sorry"\
	    "base-zenkaku-hankaku-toggle"  "I don\'t know this function, sorry"\
	    "beginning-of-line" "行頭へ移動"\
	    "bushu-mode" "部首入力モードへ移行"\
	    "capitalize" "I don\'t know this function, sorry"\
	    "convert-as-bushu" "I don\'t know this function, sorry"\
	    "convert-as-hex" "I don\'t know this function, sorry"\
	    "delete-dic-mode" "単語削除モードへ移行"\
	    "delete-next" "次の文字を削除"\
	    "delete-previous" "直前の文字を削除"\
	    "disconnect-server" "サーバとの接続を解除"\
	    "end-of-line" "行末へ移動"\
	    "extend" "I don\'t know this function, sorry"\
	    "extend-mode" "ユーティリティ・モードへ移行"\
	    "forward" "I don\'t know this function, sorry"\
	    "greek-mode" "ギリシャ文字入力モードへ移行"\
	    "hankaku" "I don\'t know this function, sorry"\
	    "henkan"  "入力されれた文字を変換"\
	    "henkan-nyuuryoku-mode" "I don\'t know this function, sorry"\
	    "henshu" "I don\'t know this function, sorry"\
	    "hex-mode" "十六進数入力モードへ移行"\
	    "hiragana" "I don\'t know this function, sorry"\
	    "japanese-mode" "I don\'t know this function, sorry"\
	    "jisho-ichiran" "辞書のマウント/アンマウント切替え"\
	    "kakutei" "変換文字の確定"\
	    "katakana" "I don\'t know this function, sorry"\
	    "kigo-mode" "記号入力モードへ移行します"\
	    "kill-to-end-of-line" "I don\'t know this function, sorry"\
	    "kouho-ichiran" "I don\'t know this function, sorry"\
	    "line-mode" "罫線入力モードへ移行します"\
	    "next" "次行または次のリストへ移動"\
	    "previous" "前行または前のリストへ移動"\
	    "quit" "I don\'t know this function, sorry"\
	    "quoted-insert" "I don\'t know this function, sorry"\
    	    "renbun-mode" "連文節変換へ切替えます"\
	    "romaji" "I don\'t know this function, sorry"\
	    "russian-mode" "ロシア文字入力モードへ移行します"\
	    "self-insert" "I don\'t know this function, sorry"\
	    "shinshuku-mode" "I don\'t know this function, sorry"\
	    "show-canna-file" "カスタマイズ・ファイルを表示します"\
	    "show-canna-version" "Canna のバージョンを表示します"\
	    "show-gakushu" "学習状態を表示します"\
	    "show-romkana-table" "ローマ字かな変換テーブルを表示します"\
	    "show-server-name" "サーバ名を表示します"\
	    "shrink"  "I don\'t know this function, sorry"\
	    "switch-server" "サーバを切替えます"\
	    "sync-dictionary" "辞書へ書き込みます"\
            "to-lower" "I don\'t know this function, sorry"\
	    "to-upper" "I don\'t know this function, sorry"\
	    "touroku" "I don\'t know this function, sorry"\
	    "touroku-mode" "単語登録モードへ移行します"\
	    "undefined" "I don\'t know this function, sorry"\
	    "zenkaku" "I don\'t know this function, sorry"}\
	    -textafter "の機能を割り当て"\
	    -help "このキーに割り当てる機能を選択します"
    Menu wMode\
	    -entryhelp {\
	    "グローバル" "全てのモードで有効になります"\
	    "単候補入力" "単候補モードでのみ有効になります"\
	    "読み入力" "読みモードでのみ有効になります"\
	    "部首変換" "部首変換モードでのみ有効になります"\
	    "単語登録" "単語登録モードでのみ有効になります"\
	    "単語削除" "単語削除モードでのみ有効になります"\
	    "記号入力" "記号入力モードでのみ有効になります"\
	    "ギリシャ文字入力" "ギリシャ文字入力モードでのみ有効になります"\
	    "罫線入力" "罫線入力モードでのみ有効になります"\
	    "十六進数入力" "十六進数入力モードでのみ有効になります"}\
	    -textafter "モードで有効にする"\
	    -help "設定するキー割り付けの適用範囲を選択します"
    ExtEntry defKey\
	    -entries {wKey wFunc wMode}\
	    -count 6\
	    -help "何らかのキーに割り当てる機能とその適用範囲を設定します"

    Save {
	
	set i 0
	set setAny 0
	
	forevery defKey {
	    if {$wKey != "" && $wFunc !=""} {
		set setAny 1
		switch -- $wMode(index) {
		    0 {
			set dPrefix "global-unbind-key-function"
			set aPrefix "global-set-key"
		    }
		    1 {
			set dPrefix "unbind-key-function \'tankouho-mode"
			set aPrefix "set-key \'tankouho-mode"
		    }
		    2 {
			set dPrefix "unbind-key-function \'yomi-mode"
			set aPrefix "set-key \'yomi-mode"
		    }
		    3 {
			set dPrefix "unbind-key-function \'bushu-mode"
			set aPrefix "set-key \'bushu-mode"
		    }
		    4 {
			set dPrefix "unbind-key-function \'touroku-mode"
			set aPrefix "set-key \'touroku-mode"
		    }
		    5 {
			set dPrefix "unbind-key-function \'delete-dic-mode"
			set aPrefix "set-key \'delete-dic-mode"
		    }
		    6 {
			set dPrefix "unbind-key-function \'kigou-mode"
			set aPrefix "set-key \'kigou-mode"
		    }
		    7 {
			set dPrefix "unbind-key-function \'greek-mode"
			set aPrefix "set-key \'greek-mode"
		    }
		    8 {
			set dPrefix "unbind-key-function \'line-mode"
			set aPrefix "set-key \'line-mode"
		    }
		    9 {
			set dPrefix "unbind-key-function \'hex-mode"
			set aPrefix "set-key \'hex-mode"
		    }
		    default {
			set dPrefix "global-unbind-key-function"
			set aPrefix "global-setkey"
		    }
		}
		set unbindList($i) "($dPrefix \'$wFunc)"
		set bindList($i) "($aPrefix \"$wKey\" \'$wFunc)"
		set i [expr $i + 1]
	    }	
	}

	if {$setAny} {
		foreach s [lsort [array names unbindList]] {
		    foreach t [lsort [array names unbindList]] {
			if {$s != $t} {
			    if {$unbindList($s) == $unbindList($t)} {
				set unbindList($t) ""
			    }
			}
		    }
		}

		foreach j [lsort [array names unbindList]] {
		    if {$unbindList($j) != ""} {
			print "$unbindList($j)"
		    }
		}

		unset unbindList

		foreach j [lsort [array names bindList]] {
		    if {$bindList($j) != ""} {
			print "$bindList($j)"
		    }
		}

		unset bindList
	}
    }
}
