package require Tix
package require fileutil
package require textutil
wm title . "Shiten"
# タブ区切りテキストファイルを読み、
# 中身を行、列2次元のリストに格納して返します。
proc readDataFile dataVar {
upvar $dataVar data
set content [::fileutil::cat "./shiten.txt" -encoding cp932]
set rows [::textutil::split::splitx $content {[\r\n]+} ]
set data {}
foreach rowStr $rows {
set rowArr [::textutil::split::splitx $rowStr "\\t+"]
lappend data $rowArr
}
}
# 各セルをフォーマットするコールバックプロシージャ
proc cbFormatCmd {w area x1 y1 x2 y2} {
if {$area eq "main"} {
$w format grid $x1 $y1 $x2 $y2 \
-bd 2 -bordercolor #606060 -relief raised \
-bg #eeeeee -fill 1
}
if {$area eq "x-margin"} {
$w format grid $x1 $y1 $x2 $y2 \
-bd 2 -bordercolor #606060 -relief raised \
-bg #dddddd -fill 1
}
}
# 編集を開始しようとする際に呼ばれるコールバックプロシージャ
set OriginalCopy {}
proc cbEditNotifyCmd {w x y} {
global OriginalCopy
if {$y == 0} {
return 0
} else {
set curval [$w entrycget $x $y -text]
if {"$curval" == "-"} {
tk_messageBox -message "値が-のセルは編集できません。"
return 0
} else {
set OriginalCopy $curval
return 1
}
}
}
# 編集が完了する際に呼ばれるコールバックプロシージャ
proc cbEditDoneCmd {w x y} {
global OriginalCopy
set curval [$w entrycget $x $y -text]
if {$x == 2 || $x == 4} {
if {! [string is integer $curval]} {
tk_messageBox -message "整数で入力してください。エラー:${curval}";
$w entryconfigure $x $y -text $OriginalCopy
}
}
}
set f [frame .f]
set g [tixGrid $f.g -width 5 -height 6 \
-font {Helvetica 12 normal} \
-format "cbFormatCmd $f.g" \
-leftmargin 0 -selectmode extended \
-xscrollcommand "$f.scrh set" -yscrollcommand "$f.scrv set" \
-editnotify "cbEditNotifyCmd $f.g" \
-editdone "cbEditDoneCmd $f.g" ]
set scrv [scrollbar $f.scrv -ori v -com "$g yview"]
set scrh [scrollbar $f.scrh -ori h -com "$g xview"]
grid $g -row 0 -column 0
grid $scrv -row 0 -column 1 -sticky ns
grid $scrh -row 1 -column 0 -sticky ew
$g size col 0 -size 10char
$g size col 1 -size 10char
$g size col 2 -size 4char
$g size col 3 -size 32char
$g size col 4 -size 10char
set collabels {支店コード 支店名称 地域 所在地 従業員数}
set x 0
foreach e $collabels {
$g set $x 0 -itemtype text -text $e ; incr x
}
readDataFile data
set y 1
foreach e $data {
set x 0
foreach c $e {
$g set $x $y -itemtype text -text $c; incr x
}
incr y
}
button .b -text 終了 -command exit
pack $f .b -side top -anc e
|