1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161
|
###
### misc.tcl: part of Scid.
### Copyright (C) 2001 Shane Hudson.
### Copyright (C) 2007 Pascal Georges
### Copyright (C) 2015 Fulvio Benini
###
### Miscellaneous routines called by other Tcl functions
################################################################################
# vwait but will timeout after a delay. Var must be fully qualified (::)
################################################################################
proc vwaitTimed { var {delay 0} {warn "warnuser"} } {
proc trigger {var warn} {
if {$warn == "warnuser"} {
tk_messageBox -type ok -icon error -parent . -title "Protocol error" -message "vwait timeout for $var"
}
set $var 1
}
if { $delay != 0 } {
set timerId [after $delay "trigger $var $warn"]
}
vwait $var
if [info exists timerId] { after cancel $timerId }
}
################################################################################
# bindFocusColors:
# Configures a text or entry widget so it turns lightYellow when it
# gets the focus, and turns white again when it loses focus.
#
# THIS IS CURRENTLY DISABLED since it works fine with regular entry widgets
# but causes problems with our combobox widgets, not sure why!
#
proc bindFocusColors {w {inColor lightYellow} {outColor white}} {
$w configure -background $outColor
#bind $w <FocusIn> "+$w configure -background $inColor"
#bind $w <FocusOut> "+$w configure -background $outColor"
}
## FROM TK 8.5.9
## ttk::bindMouseWheel $bindtag $command...
# Adds basic mousewheel support to $bindtag.
# $command will be passed one additional argument
# specifying the mousewheel direction (-1: up, +1: down).
#
proc bindMouseWheel {bindtag callback} {
switch -- [tk windowingsystem] {
x11 {
bind $bindtag <ButtonPress-4> "$callback -1; break"
bind $bindtag <ButtonPress-5> "$callback +1; break"
}
win32 {
bind $bindtag <<MWheel>> "[append callback { [expr {-(%d/120)}]}]; break"
}
aqua {
bind $bindtag <MouseWheel> "[append callback { [expr {-(%D)}]} ]; break"
}
}
}
# dialogbuttonframe:
# Creates a frame that will be shown at the bottom of a
# dialog window. It takes two parameters: the frame widget
# name to create, and a list of button args. Each element
# should contain a widget name, and button arguments.
#
proc dialogbuttonframe {frame buttonlist} {
ttk::frame $frame
set bnames {}
set maxlength 0
foreach buttonargs $buttonlist {
set bname $frame.[lindex $buttonargs 0]
set bargs [lrange $buttonargs 1 end]
eval ttk::button $bname $bargs
set bnames [linsert $bnames 0 $bname]
set length [string length [$bname cget -text]]
if {$length > $maxlength} { set length $maxlength}
}
if {$maxlength < 7} { set maxlength 7 }
foreach b $bnames {
$b configure -width $maxlength -padx 4
pack $b -side right -padx 4 -pady 4
}
}
# packbuttons
# Packs a row of dialog buttons to the left/right of their frame
# with a standard amount of padding.
#
proc packbuttons {side args} {
eval pack $args -side $side -padx 5 -pady 3
}
proc packdlgbuttons {args} {
pack {*}$args -side right -padx 5 -pady "15 5"
}
# dialogbutton:
# Creates a button that will be shown in a dialog box, so it
# is given a minimum width.
#
proc dialogbutton {w args} {
set retval [eval ttk::button $w $args] ;# -style TButton
set length [string length [$w cget -text]]
if {$length < 7} { set length 7 }
$w configure -width $length
return retval
}
proc dialogbuttonsmall {w args {style "Small.TButton"} } {
set retval [eval ttk::button $w -style $style $args]
set length [string length [$w cget -text]]
if {$length < 7} { set length 7 }
$w configure -width $length
return retval
}
# autoscrollframe
# Creates and returns a frame containing a widget which is gridded
# with scrollbars that automatically hide themselves when they are
# not needed.
# The frame and widget may already exist; they are created if needed.
# FBF 2011.03.05:
# $frame and $w aspects are not changed if they already exists
# scrollbars are created on time 0, otherwise they are not hidden
#
# Usage:
# autoscrollframe [-bars none|x|y|both] frame type w args
#
proc autoscrollframe {args} {
set bars both
if {[lindex $args 0] == "-bars"} {
set bars [lindex $args 1]
if {$bars != "x" && $bars != "y" && $bars != "none" && $bars != "both"} {
return -code error "Invalid parameter: -bars $bars"
}
set args [lrange $args 2 end]
}
if {[llength $args] < 3} {
return -code error "Insufficient number of parameters"
}
set frame [lindex $args 0]
set type [lindex $args 1]
set w [lindex $args 2]
set args [lrange $args 3 end]
set retval $frame
if {! [winfo exists $frame]} {
ttk::frame $frame
$frame configure -relief sunken -borderwidth 2
}
if {! [winfo exists $w]} {
$type $w
if {[llength $args] > 0} {
eval $w configure $args
}
$w configure -relief flat -borderwidth 0
}
autoscrollBars $bars $frame $w
return $retval
}
proc autoscrollBars {bars frame w} {
global _autoscroll
grid $w -in $frame -row 0 -column 0 -sticky news
if {$bars == "y" || $bars == "both"} {
ttk::scrollbar $frame.ybar -command [list $w yview] -takefocus 0
$w configure -yscrollcommand [list _autoscroll $frame.ybar]
grid $frame.ybar -row 0 -column 1 -sticky ns
set _autoscroll($frame.ybar) 1
set _autoscroll(time:$frame.ybar) 0
bindMouseWheel $w "_autoscrollMouseWheel $w $frame.ybar"
}
if {$bars == "x" || $bars == "both"} {
ttk::scrollbar $frame.xbar -command [list $w xview] -takefocus 0 -orient horizontal
$w configure -xscrollcommand [list _autoscroll $frame.xbar]
grid $frame.xbar -row 1 -column 0 -sticky we
set _autoscroll($frame.xbar) 1
set _autoscroll(time:$frame.xbar) 0
}
grid rowconfigure $frame 0 -weight 1
grid columnconfigure $frame 0 -weight 1
grid rowconfigure $frame 1 -weight 0
grid columnconfigure $frame 1 -weight 0
}
proc _autoscrollMouseWheel {{w} {bar} {direction}} {
if {$::_autoscroll($bar) == 0} return
$w yview scroll $direction units
}
array set _autoscroll {}
# _autoscroll
# This is the "set" command called for auto-scrollbars.
# If the bar is shown but should not be, it is hidden.
# If the bar is hidden but should be shown, it is redrawn.
# Note that once a bar is shown, it will not be removed again for
# at least a few milliseconds; this is to overcome problematic
# interactions between the x and y scrollbars where hiding one
# causes the other to be shown etc. This usually happens because
# the stupid Tcl/Tk text widget doesn't handle scrollbars well.
#
proc _autoscroll {bar args} {
global _autoscroll
if {[llength $args] == 2} {
set min [lindex $args 0]
set max [lindex $args 1]
if {$min > 0.0 || $max < 1.0} {
if {! $_autoscroll($bar)} {
grid configure $bar
set _autoscroll($bar) 1
set _autoscroll(time:$bar) [clock clicks -milli]
}
} else {
if {[clock clicks -milli] > [expr {$_autoscroll(time:$bar) + 100}]} {
grid remove $bar
set _autoscroll($bar) 0
}
}
}
eval $bar set $args
}
proc _autoscrollMap {frame} {
# wm geometry [winfo toplevel $frame] [wm geometry [winfo toplevel $frame]]
}
# busyCursor, unbusyCursor:
# Sets all cursors to watch (indicating busy) or back to their normal
# setting again.
array set scid_busycursor {}
set scid_busycursorState 0
proc doBusyCursor {w flag} {
global scid_busycursor
if {! [winfo exists $w]} { return }
if {[winfo class $w] == "Menu"} { return }
if {$flag} {
if { [ catch { set scid_busycursor($w) [$w cget -cursor] } ] } {
return
}
catch {$w configure -cursor watch}
} else {
catch {$w configure -cursor $scid_busycursor($w)} err
}
foreach i [winfo children $w] { doBusyCursor $i $flag }
}
proc busyCursor {w {flag 1}} {
global scid_busycursor scid_busycursorState
if {$scid_busycursorState == $flag} { return }
set scid_busycursorState $flag
doBusyCursor $w $flag
}
proc unbusyCursor {w} {busyCursor $w 0}
# addHorizontalRule, addVerticalRule
# Add a horizontal/vertical rule frame to a window.
# The optional parameters [x/y]padding and sunken allow the spacing and
# appearance of the rule to be specified.
#
set horizRuleCounter 0
set vertRuleCounter 0
proc addHorizontalRule {w {ypadding 5} {relief sunken} {height 2} } {
global horizRuleCounter
ttk::separator $w.line$horizRuleCounter -orient horizontal
pack $w.line$horizRuleCounter -fill x ;# -pady $ypadding
# set f [ ttk::frame $w.line$horizRuleCounter -height $height -borderwidth 2 -relief $relief ]
# pack $f -fill x -pady $ypadding
incr horizRuleCounter
}
proc addVerticalRule {w {xpadding 5} {relief sunken}} {
global vertRuleCounter
ttk::separator $w.line$vertRuleCounter -orient vertical
pack $w.line$vertRuleCounter -fill y -side left ;# -padx $xpadding
# set f [ ttk::frame $w.line$vertRuleCounter -width 2 -borderwidth 2 -relief $relief ]
# pack $f -fill y -padx $xpadding -side left
incr vertRuleCounter
}
# progressWindow:
# Creates a window with a label, progress bar, and (if specified),
# a cancel button and cancellation command.
#
proc progressWindow { title text {button ""} {command "progressBarCancel"} } {
set w .progressWin
if {[winfo exists $w]} { return }
set ::progressWin_focus [focus]
win::createDialog $w 6
wm resizable $w 0 0
wm title $w $title
ttk::frame $w.f
ttk::label $w.f.t -text $text
autoscrollframe -bars y $w.f.cmsg text $w.f.cmsg.text -width 70 -height 14 -wrap word -font font_Regular
canvas $w.f.c -width 400 -height 20 -bg white -relief solid -border 1 -highlightthickness 0
$w.f.c create rectangle 0 0 0 0 -fill DodgerBlue3 -outline DodgerBlue3 -tags bar
$w.f.c create text 395 10 -anchor e -font font_Regular -tags time -fill black -text "0:00 / 0:00"
ttk::button $w.f.cancel -text $button -command "$command"
grid $w.f.t -row 0 -columnspan 2 -pady 4 -sticky news
grid $w.f.cmsg -row 1 -columnspan 2 -pady 4 -sticky news
grid $w.f.c -row 2 -column 0 -pady 4 -stick w
grid $w.f.cancel -row 2 -column 1 -padx "10 0"
grid $w.f -sticky news
grid rowconfigure $w.f 1 -weight 1
grid columnconfigure $w.f 0 -weight 1
grid remove $w.f.cmsg
if {$button == ""} { grid remove $w.f.cancel }
# Set up geometry for middle of screen:
set x [expr ([winfo screenwidth $w] - 400) / 2]
set y [expr ([winfo screenheight $w] - 40) / 2]
wm geometry $w +$x+$y
grab $w
wm withdraw $w
progressBarSet $w.f.c 401 21
set ::progressCanvas(show) "catch {wm deiconify $w}"
}
proc progressBarSet { canvasname width height } {
update idletasks
set ::progressCanvas(name) $canvasname
set ::progressCanvas(w) $width
set ::progressCanvas(h) $height
set ::progressCanvas(cancel) 0
set ::progressCanvas(init) 1
set ::progressCanvas(show) {}
set ::progressCanvas(time) [clock milliseconds]
after idle { unset ::progressCanvas(init) }
}
proc progressBarCancel { } {
set ::progressCanvas(cancel) 1
}
proc progressCallBack {done {msg ""}} {
if {$done == "init"} {
if {[info exists ::progressCanvas(init)]} {
return $::progressCanvas(init)
}
# No progress bar
return -code break
}
if {! [winfo exists $::progressCanvas(name)] || $::progressCanvas(cancel)} {
#Interrupted
return -code break
}
set elapsed [expr { [clock milliseconds] - $::progressCanvas(time) }]
if {$done != 0} {
set estimated [expr { int($elapsed / double($done) / 1000) }]
set elapsed [expr { $elapsed / 1000 }]
} else {
set elapsed [expr { $elapsed / 1000 }]
set estimated $elapsed
}
if {$::progressCanvas(show) != ""} {
if {$elapsed == 0 && $estimated < 2 && $msg == ""} { return }
eval $::progressCanvas(show)
set ::progressCanvas(show) {}
}
set width [expr { int(double($::progressCanvas(w)) * double($done)) }]
$::progressCanvas(name) coords bar 0 0 $width $::progressCanvas(h)
set t [format "%d:%02d / %d:%02d" \
[expr {$elapsed / 60}] [expr {$elapsed % 60}] \
[expr {$estimated / 60}] [expr {$estimated % 60}]]
$::progressCanvas(name) itemconfigure time -text $t
if {$msg != ""} {
catch {
set widget "$::progressCanvas(name)msg"
grid $widget
append widget ".text"
$widget insert end "$msg\n"
}
}
update
if {! [winfo exists $::progressCanvas(name)] || $::progressCanvas(cancel)} {
#Interrupted
return -code break
}
}
proc changeProgressWindow {newtext} {
set w .progressWin
if {[winfo exists $w]} {
$w.f.t configure -text $newtext
update idletasks
}
}
proc updateProgressWindow {done total} {
set w .progressWin
if {! [winfo exists $w]} { return }
if {$total != 0} {
set done [expr { double($done) / double($total) }]
} else {
set done 1
}
::progressCallBack $done
}
proc closeProgressWindow {{force false}} {
set w .progressWin
if {! [winfo exists $w]} { return }
if {!$force && [$w.f.cmsg.text index end] != "2.0" } {
$w.f.cancel configure -text "$::tr(Close)"
$w.f.cancel configure -command "closeProgressWindow true"
grid forget $w.f.c
grid $w.f.cancel
$w.f.cmsg.text configure -state disabled
return
}
grab release $w
destroy $w
update idletasks
catch {focus $::progressWin_focus}
}
proc CreateSelectDBWidget {{w} {varname} {ref_base ""} {readOnly 1}} {
set listbases {}
if {$ref_base == ""} { set ref_base [sc_base current] }
set tr_database [tr Database]
set tr_prefix_len [expr {[string length $tr_database] + 1}]
set selected 0
foreach i [sc_base list] {
if {$readOnly || ![sc_base isReadOnly $i]} {
set fname [file tail [sc_base filename $i]]
if {$i == $ref_base} { set selected [llength $listbases] }
lappend listbases "$tr_database $i: $fname"
}
}
ttk::combobox $w.lb -values $listbases -state readonly
grid $w.lb -sticky news
grid columnconfigure $w 0 -weight 1
bind $w.lb <<ComboboxSelected>> "
set $varname \[ string index \[$w.lb get\] $tr_prefix_len \]
"
$w.lb current $selected
event generate $w.lb <<ComboboxSelected>>
}
proc storeEmtComment { h m s } {
set time "[format "%d" $h]:[format "%02d" $m]:[format "%02d" $s]"
#Replace %emt if present, otherwise prepend it
if {[regsub {\[%emt\s*.*?\]} [sc_pos getComment] "\[%emt $time\]" comment]} {
sc_pos setComment "$comment"
} else {
sc_pos setComment "\[%emt $time\]$comment"
}
}
proc storeEvalComment { value } {
#Replace %eval if present, otherwise prepend it
if {[regsub {\[%eval\s*.*?\]} [sc_pos getComment] "\[%eval $value\]" comment]} {
sc_pos setComment "$comment"
} else {
sc_pos setComment "\[%eval $value\]$comment"
}
}
################################################################################
# clock widget
################################################################################
namespace eval gameclock {
array set data {}
################################################################################
proc new { parent n { size 100 } {showfall 0} } {
global ::gameclock::data
set data(showfallen$n) $showfall
set data(id$n) ""
if {$parent != ""} {
set data(id$n) $parent.clock$n
canvas $data(id$n) -height $size -width $size
pack $data(id$n) -side top -anchor center
for {set i 1} {$i<13} {incr i} {
set a [expr {$i/6.*acos(-1)}]
set x [expr { ($size/2 + (($size-15)/2)*sin($a) ) }]
set y [expr { ($size/2 - (($size-15)/2)*cos($a) ) }]
$data(id$n) create text $x $y -text $i -tag clock$n
}
bind $data(id$n) <Button-1> "::gameclock::toggleClock $n"
}
set data(fg$n) "black"
set data(running$n) 0
set data(digital$n) 1
::gameclock::reset $n
::gameclock::draw $n
}
################################################################################
proc draw { n } {
global ::gameclock::data
#TODO: Hack. For the moment we assume that:
# -clock 1 is the white clock on the main board
# -clock 2 is the black clock on the main board
set sec $data(counter$n)
set h [format "%d" [expr abs($sec) / 60 / 60] ]
set m [format "%02d" [expr (abs($sec) / 60) % 60] ]
set s [format "%02d" [expr abs($sec) % 60] ]
if {$n == 1} { set ::gamePlayers(clockW) "$h:$m:$s" }
if {$n == 2} { set ::gamePlayers(clockB) "$h:$m:$s" }
if {! [winfo exists $data(id$n)]} { return }
$data(id$n) delete aig$n
set w [$data(id$n) cget -width ]
set h [$data(id$n) cget -height ]
set cx [ expr $w / 2 ]
set cy [ expr $h / 2 ]
if {$w < $h} {
set size [ expr $w - 15 ]
} else {
set size [ expr $h - 15 ]
}
if { $sec > 0 && $data(showfallen$n) } {
set color "red"
} else {
set color $::gameclock::data(fg$n)
}
if {$color == "white"} {set fg "black"} else {set fg "white"}
foreach divisor {30 1800 21600} length "[expr $size/2 * 0.8] [expr $size/2 * 0.7] [expr $size/2 * 0.4]" \
width {1 2 3} {
set angle [expr {$sec * acos(-1) / $divisor}]
set x [expr {$cx + $length * sin($angle)}]
set y [expr {$cy - $length * cos($angle)}]
$data(id$n) create line $cx $cy $x $y -width $width -tags aig$n -fill $color
}
# draw a digital clock
if {$data(digital$n)} {
set m [format "%02d" [expr abs($sec) / 60] ]
set s [format "%02d" [expr abs($sec) % 60] ]
$data(id$n) create text $cx [expr $cy + $size/4 ] -text "$m:$s" -anchor center -fill $color -tag aig$n
}
}
################################################################################
proc every {ms body n} {
incr ::gameclock::data(counter$n)
eval $body
if {$::gameclock::data(id$n) == "" ||
[winfo exists $::gameclock::data(id$n)]} {
after $ms [info level 0]
}
}
################################################################################
proc getSec { n } {
return [expr 0 - $::gameclock::data(counter$n)]
}
################################################################################
proc setSec { n value } {
set ::gameclock::data(counter$n) $value
::gameclock::draw $n
}
################################################################################
proc add { n value } {
set ::gameclock::data(counter$n) [ expr $::gameclock::data(counter$n) - $value ]
::gameclock::draw $n
}
################################################################################
proc reset { n } {
::gameclock::stop $n
set ::gameclock::data(counter$n) 0
}
################################################################################
proc start { n } {
if {$::gameclock::data(running$n)} { return }
set ::gameclock::data(running$n) 1
::gameclock::every 1000 "draw $n" $n
}
################################################################################
proc stop { n } {
if {! $::gameclock::data(running$n)} { return 0 }
set ::gameclock::data(running$n) 0
after cancel "::gameclock::every 1000 \{draw $n\} $n"
return 1
}
################################################################################
proc storeTimeComment { color } {
set sec [::gameclock::getSec $color]
set h [format "%d" [expr abs($sec) / 60 / 60] ]
set m [format "%02d" [expr (abs($sec) / 60) % 60] ]
set s [format "%02d" [expr abs($sec) % 60] ]
set time "$h:$m:$s"
#Replace %clk if present, otherwise prepend it
if {[regsub {\[%clk\s*.*?\]} [sc_pos getComment] "\[%clk $time\]" comment]} {
sc_pos setComment "$comment"
} else {
sc_pos setComment "\[%clk $time\]$comment"
}
}
################################################################################
proc toggleClock { n } {
if { $::gameclock::data(running$n) } {
stop $n
} else {
start $n
}
}
################################################################################
proc setColor { n color } {
if {$color == "white"} {
set fg "black"
set bg "white"
} else {
set fg "white"
set bg "black"
}
set ::gameclock::data(fg$n) $fg
$::gameclock::data(id$n) configure -background $bg
$::gameclock::data(id$n) itemconfigure clock$n -fill $fg
$::gameclock::data(id$n) itemconfigure aig$n -fill $fg
}
################################################################################
proc isRunning { } {
global ::gameclock::data
catch {
if {$data(running1) || $data(running2)} { return 1 }
}
return 0
}
}
################################################################################
# html generation
################################################################################
namespace eval html {
set data {}
set idx 0
################################################################################
proc exportCurrentFilter {} {
# Check that we have some games to export:
if {[sc_filter count] == 0} {
tk_messageBox -title "Scid: Filter empty" -type ok -icon info \
-message "The filter contains no games."
return
}
set ftype {
{ "HTML files" {".html" ".htm"} }
{ "All files" {"*"} }
}
set idir $::initialDir(html)
set fName [tk_getSaveFile -initialdir $idir -filetypes $ftype -defaultextension ".html" -title "Create an HTML file"]
if {$fName == ""} { return }
if {[file extension $fName] != ".html" } {
append fName ".html"
}
set prefix [file rootname [file tail $fName] ]
set dirtarget [file dirname $fName]
set sourcedir [file join $::scidExeDir html]
catch {file copy -force [file join $sourcedir bitmaps] $dirtarget}
catch {file copy -force [file join $sourcedir scid.js] $dirtarget}
catch {file copy -force [file join $sourcedir scid.css] $dirtarget}
# writeIndex "[file join $dirtarget $prefix].html" $prefix
progressWindow "Scid" "Exporting games..."
set savedGameNum [sc_game number]
set gn [sc_filter first]
set players {}
set ::html::cancelHTML 0
set total [sc_filter count]
# build the list of matches
set idx 1
while {$gn != 0 && ! $::html::cancelHTML} {
updateProgressWindow $idx $total
sc_game load $gn
set pl "[sc_game tags get White] - [sc_game tags get Black]"
lappend players $pl
set gn [sc_filter next]
incr idx
}
set idx 1
set gn [sc_filter first]
while {$gn != 0 && ! $::html::cancelHTML} {
updateProgressWindow $idx $total
sc_game load $gn
fillData
set pl "[sc_game tags get White] - [sc_game tags get Black]"
toHtml $::html::data $idx $dirtarget $prefix $players $pl [sc_game tags get "Event"] [sc_game tags get "ECO"] [sc_game info result] [sc_game tags get "Date"]
set gn [sc_filter next]
incr idx
}
closeProgressWindow
exportPGN "[file join $dirtarget $prefix].pgn" "filter"
sc_game load $savedGameNum
}
################################################################################
proc sc_progressBar {} {
set ::html::cancelHTML 1
}
################################################################################
proc exportCurrentGame {} {
set ftype {
{ "HTML files" {".html" ".htm"} }
{ "All files" {"*"} }
}
set idir $::initialDir(html)
set fName [tk_getSaveFile -initialdir $idir -filetypes $ftype -defaultextension ".html" -title "Create an HTML file"]
if {[file extension $fName] != ".html" && [file extension $fName] != ".htm" } {
append fName ".html"
}
if {$fName == ""} { return }
set prefix [file rootname [file tail $fName] ]
set dirtarget [file dirname $fName]
set sourcedir [file join $::scidExeDir html]
catch { file copy -force [file join $sourcedir bitmaps] $dirtarget }
catch { file copy -force [file join $sourcedir scid.js] $dirtarget }
catch { file copy -force [file join $sourcedir scid.css] $dirtarget }
fillData
set players [list "[sc_game tags get White] - [sc_game tags get Black]"]
toHtml $::html::data -1 $dirtarget $prefix $players [lindex $players 0] \
[sc_game tags get "Event"] [sc_game tags get "ECO"] \
[sc_game info result] [sc_game tags get "Date"]
exportPGN "[file join $dirtarget $prefix].pgn" "current"
}
################################################################################
# Dictionary mapping from special characters to their entities. (from tcllib)
variable entities {
\xa0 \xa1 ¡ \xa2 ¢ \xa3 £ \xa4 ¤
\xa5 ¥ \xa6 ¦ \xa7 § \xa8 ¨ \xa9 ©
\xaa ª \xab « \xac ¬ \xad ­ \xae ®
\xaf ¯ \xb0 ° \xb1 ± \xb2 ² \xb3 ³
\xb4 ´ \xb5 µ \xb6 ¶ \xb7 · \xb8 ¸
\xb9 ¹ \xba º \xbb » \xbc ¼ \xbd ½
\xbe ¾ \xbf ¿ \xc0 À \xc1 Á \xc2 Â
\xc3 Ã \xc4 Ä \xc5 Å \xc6 Æ \xc7 Ç
\xc8 È \xc9 É \xca Ê \xcb Ë \xcc Ì
\xcd Í \xce Î \xcf Ï \xd0 Ð \xd1 Ñ
\xd2 Ò \xd3 Ó \xd4 Ô \xd5 Õ \xd6 Ö
\xd7 × \xd8 Ø \xd9 Ù \xda Ú \xdb Û
\xdc Ü \xdd Ý \xde Þ \xdf ß \xe0 à
\xe1 á \xe2 â \xe3 ã \xe4 ä \xe5 å
\xe6 æ \xe7 ç \xe8 è \xe9 é \xea ê
\xeb ë \xec ì \xed í \xee î \xef ï
\xf0 ð \xf1 ñ \xf2 ò \xf3 ó \xf4 ô
\xf5 õ \xf6 ö \xf7 ÷ \xf8 ø \xf9 ù
\xfa ú \xfb û \xfc ü \xfd ý \xfe þ
\xff ÿ \u192 ƒ \u391 Α \u392 Β \u393 Γ
\u394 Δ \u395 Ε \u396 Ζ \u397 Η \u398 Θ
\u399 Ι \u39A Κ \u39B Λ \u39C Μ \u39D Ν
\u39E Ξ \u39F Ο \u3A0 Π \u3A1 Ρ \u3A3 Σ
\u3A4 Τ \u3A5 Υ \u3A6 Φ \u3A7 Χ \u3A8 Ψ
\u3A9 Ω \u3B1 α \u3B2 β \u3B3 γ \u3B4 δ
\u3B5 ε \u3B6 ζ \u3B7 η \u3B8 θ \u3B9 ι
\u3BA κ \u3BB λ \u3BC μ \u3BD ν \u3BE ξ
\u3BF ο \u3C0 π \u3C1 ρ \u3C2 ς \u3C3 σ
\u3C4 τ \u3C5 υ \u3C6 φ \u3C7 χ \u3C8 ψ
\u3C9 ω \u3D1 ϑ \u3D2 ϒ \u3D6 ϖ
\u2022 • \u2026 … \u2032 ′ \u2033 ″
\u203E ‾ \u2044 ⁄ \u2118 ℘ \u2111 ℑ
\u211C ℜ \u2122 ™ \u2135 ℵ \u2190 ←
\u2191 ↑ \u2192 → \u2193 ↓ \u2194 ↔ \u21B5 ↵
\u21D0 ⇐ \u21D1 ⇑ \u21D2 ⇒ \u21D3 ⇓ \u21D4 ⇔
\u2200 ∀ \u2202 ∂ \u2203 ∃ \u2205 ∅
\u2207 ∇ \u2208 ∈ \u2209 ∉ \u220B ∋ \u220F ∏
\u2211 ∑ \u2212 − \u2217 ∗ \u221A √
\u221D ∝ \u221E ∞ \u2220 ∠ \u2227 ∧ \u2228 ∨
\u2229 ∩ \u222A ∪ \u222B ∫ \u2234 ∴ \u223C ∼
\u2245 ≅ \u2248 ≈ \u2260 ≠ \u2261 ≡ \u2264 ≤
\u2265 ≥ \u2282 ⊂ \u2283 ⊃ \u2284 ⊄ \u2286 ⊆
\u2287 ⊇ \u2295 ⊕ \u2297 ⊗ \u22A5 ⊥
\u22C5 ⋅ \u2308 ⌈ \u2309 ⌉ \u230A ⌊
\u230B ⌋ \u2329 ⟨ \u232A ⟩ \u25CA ◊
\u2660 ♠ \u2663 ♣ \u2665 ♥ \u2666 ♦
\x22 " \x26 & \x3C < \x3E > \u152 Œ
\u153 œ \u160 Š \u161 š \u178 Ÿ
\u2C6 ˆ \u2DC ˜ \u2002   \u2003   \u2009  
\u200C ‌ \u200D ‍ \u200E ‎ \u200F ‏ \u2013 –
\u2014 — \u2018 ‘ \u2019 ’ \u201A ‚
\u201C “ \u201D ” \u201E „ \u2020 †
\u2021 ‡ \u2030 ‰ \u2039 ‹ \u203A ›
\u20AC €
}
proc html_entities {s} {
variable entities
return [string map $entities $s]
}
################################################################################
proc toHtml { dt game dirtarget prefix {players ""} {this_players ""} {event ""} {eco "ECO"} {result "*"} {date ""} } {
if { $game != -1 } {
set f [open "[file join $dirtarget $prefix]_${game}.html" w]
} else {
set f [open "[file join $dirtarget $prefix].html" w]
}
# header
puts $f "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">"
puts $f "<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" lang=\"en\">"
puts $f "<head>"
puts $f "<meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\" />"
puts $f "<link rel=\"stylesheet\" type=\"text/css\" href=\"scid.css\" />"
puts $f "<script src=\"scid.js\" type=\"text/javascript\"></script>"
puts $f "<script type=\"text/javascript\">"
puts $f "// <!\[CDATA\["
puts $f "movesArray = new Array("
for {set i 0} {$i<[llength $dt]} {incr i} {
array set elt [lindex $dt $i]
puts -nonewline $f "\"$elt(fen) $elt(prev) $elt(next)\""
if {$i < [expr [llength $dt] -1]} { puts $f "," }
}
puts $f ");"
puts $f "var current = 0;"
puts $f "var prefix = \"$prefix\";"
puts $f "// \]\]>"
puts $f "</script>"
puts $f "<title>Scid</title>"
puts $f "<meta content=\"Scid\" name=\"author\" />"
puts $f "</head>"
puts $f "<body onload=\"doinit()\" onkeydown=\"handlekey(event)\">"
puts $f "<div id=\"framecontent\">"
puts $f "<div class=\"innertube\">"
# diagram
puts $f "<div id=\"diagram\"><!-- diagram goes here --></div>"
# navigation
puts $f "<div id=\"nav\" style=\"text-align: center\"><!-- navigation goes here -->"
puts $f "<form action=\"#\">"
puts $f "<p>"
puts $f "<input type='button' value=' ↓↑ ' onclick='rotate()' /> <input type='button' value=' |< ' onclick='jump(0)' /> <input type='button' value=' < ' onclick='moveForward(0)' /> <input type='button' value=' > ' onclick='moveForward(1)' /> <input type='button' value=' >| ' onclick='jump(1)' /> "
puts $f "</p><p>"
# other games navigation
puts $f "<select name=\"gameselect\" id=\"gameselect\" size=\"1\" onchange=\"gotogame()\">"
set i 1
foreach l $players {
if { $game == $i } {
puts $f "<option selected=\"selected\">$i. [html_entities $l]</option>"
} else {
puts $f "<option>$i. [html_entities $l]</option>"
}
incr i
}
puts $f "</select>"
puts $f "</p><p>"
puts $f "<input type=\"button\" value=\"<--\" onclick=\"gotoprevgame()\" /> <input type=\"button\" value=\"-->\" onclick=\"gotonextgame()\" />"
puts $f "</p><p>"
puts $f "<a href=\"${prefix}.pgn\">${prefix}.pgn</a>"
puts $f "</p>"
puts $f "</form>"
puts $f "</div>"
puts $f "</div>"
puts $f "</div>"
puts $f "<div id=\"maincontent\">"
puts $f "<div class=\"innertube\">"
puts $f "<div id=\"moves\"><!-- moves go here -->"
# game header
puts $f "<span class=\"hPlayers\"> [html_entities $this_players]</span>"
puts $f "<span class=\"hEvent\"><br /> [html_entities $event]</span>"
puts $f "<span class=\"hEvent\"><br />\[$date\]</span>"
puts $f "<span class=\"hAnnot\"><br />\[$eco\]</span>"
puts $f "<p>"
# link moves
set prevdepth 0
set prevvarnumber 0
for {set i 0} {$i<[llength $dt]} {incr i} {
array set elt [lindex $dt $i]
if {$elt(depth) == 0} {
set class "V0"
} elseif {$elt(depth) == 1} {
set class "V1"
} else {
set class "V2"
}
if { $prevdepth == $elt(depth) && $prevvarnumber != $elt(var) } {
puts $f "<span class=\"VC\">\]</span></div>"
puts $f "<div class=\"var\"><span class=\"VC\">\[</span>"
} else {
while { $prevdepth > $elt(depth) } {
puts $f "<span class=\"VC\">\]</span></div>"
set prevdepth [expr $prevdepth - 1]
}
while { $prevdepth < $elt(depth) } {
puts $f "<div class=\"var\"><span class=\"VC\">\[</span>"
set prevdepth [expr $prevdepth + 1]
}
}
set prevvarnumber $elt(var)
# id = "mv1" not "id=1" now
set nag [html_entities $elt(nag)]
set comment [html_entities $elt(comment)]
puts $f "<a href=\"javascript:gotoMove($elt(idx))\" id=\"mv$elt(idx)\" class=\"$class\">$elt(move)$nag</a>"
if {$elt(diag)} {
insertMiniDiag $elt(fen) $f
}
if {$comment != ""} {
puts $f "<span class=\"VC\">$comment</span>"
}
}
while { $prevdepth > 0 } {
puts $f "<span class=\"VC\">\]</span></div>"
set prevdepth [expr $prevdepth - 1]
}
puts $f "<br /><span class=\"VH\">$result</span>"
puts $f "<p>"
puts $f "<a href=\"http://scid.sourceforge.net/\" style=\"font-size: 0.8em\">Created with Scid</a>"
puts $f "</div>"
puts $f "</div>"
puts $f "</div>"
puts $f "</body>"
puts $f "</html>"
close $f
}
################################################################################
proc colorSq {sq} {
if { [expr $sq % 2] == 1 && [expr int($sq / 8) %2 ] == 0 || [expr $sq % 2] == 0 && [expr int($sq / 8) %2 ] == 1 } {
return "bs"
} else {
return "ws"
}
}
################################################################################
proc piece2gif {piece} {
if {$piece == "K"} { return "wk" }
if {$piece == "k"} { return "bk" }
if {$piece == "Q"} { return "wq" }
if {$piece == "q"} { return "bq" }
if {$piece == "R"} { return "wr" }
if {$piece == "r"} { return "br" }
if {$piece == "B"} { return "wb" }
if {$piece == "b"} { return "bb" }
if {$piece == "N"} { return "wn" }
if {$piece == "n"} { return "bn" }
if {$piece == "P"} { return "wp" }
if {$piece == "p"} { return "bp" }
if {$piece == " "} { return "sq" }
}
################################################################################
proc insertMiniDiag {fen f} {
set square 0
set space " "
puts $f "<table Border=0 CellSpacing=0 CellPadding=0><tr>"
for {set i 0} {$i < [string length $fen]} {incr i} {
set l [string range $fen $i $i ]
set res [scan $l "%d" c]
if {$res == 1} {
if { $c >= 1 && $c <= 8 } {
for { set j 0} {$j < $c} {incr j} {
puts $f "<td class=\"[colorSq $square]\"><img border=0 align=\"left\" src=\"bitmaps/mini/[piece2gif $space].gif\"></td>"
incr square
}
}
} elseif {$l == "/"} {
puts $f "</tr><tr>"
} else {
puts $f "<td class=\"[colorSq $square]\"><img border=0 align=\"left\" src=\"bitmaps/mini/[piece2gif $l].gif\"></td>"
incr square
}
}
puts $f "</tr></table>"
}
################################################################################
# fill data with { idx FEN prev next move nag comment depth }
proc fillData {} {
set ::html::data {}
set ::html::idx -1
sc_move start
parseGame
}
################################################################################
proc parseGame { {prev -2} } {
global ::html::data ::html::idx
set already_written 0
set dots 0
while {1} {
if { ! $already_written } {
recordElt $dots $prev
set dots 0
set prev -2
} else {
set dots 1
}
set already_written 0
# handle variants
if {[sc_var count]>0} {
# First write the move in the current line for which variations exist
#
if { ![sc_pos isAt vend]} {
sc_move forward
recordElt $dots $prev
sc_move back
set lastIdx $idx
set already_written 1
}
for {set v 0} {$v<[sc_var count]} {incr v} {
sc_var enter $v
# in order to get the comment before first move
sc_move back
parseGame -1
sc_var exit
}
#update the "next" token
array set elt [lindex $data $lastIdx]
set elt(next) [expr $idx + 1]
lset data $lastIdx [array get elt]
#update the "previous" token
set prev $lastIdx
}
if {[sc_pos isAt vend]} { break }
sc_move forward
}
}
################################################################################
proc recordElt { dots {prev -2} } {
global ::html::data ::html::idx
array set elt {}
incr idx
set elt(idx) $idx
set elt(fen) [lindex [split [sc_pos fen]] 0]
if {$prev != -2} {
set elt(prev) $prev
} else {
set elt(prev) [expr $idx-1]
}
set nag [sc_pos getNags]
if {$nag == "0"} { set nag "" }
if {[string match "*D *" $nag] || [string match "*# *" $nag]} {
set elt(diag) 1
} else {
set elt(diag) 0
}
set nag [regsub -all "D " $nag "" ]
set nag [regsub -all "# " $nag "" ]
set elt(nag) $nag
set comment [sc_pos getComment]
set comment [regsub -all "\[\x5B\]%draw (.)+\[\x5D\]" $comment ""]
set elt(comment) $comment
set elt(depth) [sc_var level]
set elt(var) [sc_var number]
if {![sc_pos isAt vend]} {
set elt(next) [expr $idx +1 ]
} else {
set elt(next) -1
}
set m [sc_game info previousMove]
set mn [sc_pos moveNumber]
set elt(move) ""
if {[sc_pos side] == "black" && $m != ""} {
set elt(move) "$mn.$m"
} else {
if {! [sc_pos isAt vstart] } {
sc_move back
set pnag [sc_pos getNags]
if {$pnag == "0"} { set pnag "" }
if {[string match "*D *" $pnag] || [string match "*# *" $pnag]} {
set pdiag 1
} else {
set pdiag 0
}
if { [sc_pos isAt vstart] || [sc_pos getComment] != "" || $pdiag > 0 } {
set dots 1
}
sc_move forward
}
if {$dots && $m != ""} {
set elt(move) "[expr $mn -1]. ... $m"
} else {
set elt(move) $m
}
}
lappend ::html::data [array get elt]
}
################################################################################
# proc writeIndex {fn prefix} {
# set f [open $fn w]
# puts $f "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">"
# puts $f "<html>"
# puts $f "<head>"
# puts $f "<meta content=\"text/html; charset=ISO-8859-1\" http-equiv=\"content-type\">"
# puts $f "<title>Scid</title>"
# puts $f "<meta content=\"Scid\" name=\"author\">"
# puts $f "</head>"
# puts $f "<frameset BORDER=\"0\" FRAMEBORDER=\"0\" FRAMESPACING=\"0\" COLS=\"380,*\">"
# puts $f "<frameset BORDER=\"0\" FRAMEBORDER=\"0\" FRAMESPACING=\"0\" ROWS=\"380,*\">"
# puts $f "<frame NAME=\"diagram\" SCROLLING=\"Auto\">"
# puts $f "<frame NAME=\"nav\" SRC=\"${prefix}_nav.html\" SCROLLING=\"Auto\">"
# puts $f "</frameset>"
# puts $f "<frame NAME=\"moves\" SRC=\"${prefix}_1.html\" SCROLLING=\"Auto\">"
# puts $f "</frameset>"
# puts $f "</html>"
# close $f
# }
################################################################################
proc exportPGN { fName selection } {
if {$selection == "filter"} {
progressWindow "Scid" "Exporting games..." $::tr(Cancel)
}
sc_base export $selection "PGN" $fName -append 0 -starttext "" -endtext "" -comments 1 -variations 1 \
-space 1 -symbols 1 -indentC 0 -indentV 0 -column 0 -noMarkCodes 1 -convertNullMoves 1
if {$selection == "filter"} {
closeProgressWindow
}
}
}
################################################################################
#
################################################################################
# end of misc.tcl
|