Scid  4.7.0
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
eco.tcl
Go to the documentation of this file.
1 
2 ####################
3 # ECO Browser window
4 
5 set ::windows::eco::code ""
6 set ::windows::eco::count 0
7 set ::windows::eco::isOpen 0
8 
9 proc ::windows::eco::OpenClose {} {
10  if {[winfo exists .ecograph]} {
11  destroy .ecograph
12  } else {
14  }
15 }
16 
17 # ::windows::eco::Refresh
18 #
19 # Updates the ECO Browser window, opening it if necessary.
20 # If the ECO code "code" is "x", then the value of the
21 # variable ::windows::eco::code is used instead.
22 #
23 proc ::windows::eco::Refresh {{code "x"}} {
24  set w .ecograph
25  set graph $w.pane.graph
26  set text $w.pane.text
27  if {$code != "x"} { set ::windows::eco::code $code}
28  if {! [winfo exists $w]} {
29  set ::windows::eco::isOpen 1
31  wm minsize $w 200 100
32  bind $w <Escape> "destroy $w"
33  bind $w <F1> {helpWindow ECO}
34  bind $w <Destroy> {set ::windows::eco::isOpen 0}
35  text $w.title -relief flat -height 1 -width 1 -wrap word -font font_Bold
36  $w.title tag configure center -justify center
37  $w.title configure -cursor top_left_arrow
38  pack $w.title -side top -fill x
39  ttk::frame $w.b
40  pack $w.b -side bottom -fill x
41  ttk::button $w.b.classify -textvar ::tr(ReclassifyGames) -command classifyAllGames
42  dialogbutton $w.b.help -textvar ::tr(Help) -command {helpWindow ECO}
43  dialogbutton $w.b.close -textvar ::tr(Close) -command "destroy $w"
44  pack $w.b.classify -side left -padx 5 -pady 5
45  packbuttons right $w.b.close $w.b.help
46  set pane [::utils::pane::Create $w.pane graph text 500 400 0.5]
47  ::utils::pane::SetRange $w.pane 0.3 0.7
48  ::utils::pane::SetDrag $w.pane 0
49  pack $pane -side top -expand true -fill both
50 
51  canvas $graph.c -width 500 -height 250 -selectforeground [ttk::style lookup . -foreground] -background [ttk::style lookup . -background]
52  pack $graph.c -side top -fill both -expand yes
53  text $text.text -height 12 -width 75 -wrap word -font font_Regular \
54  -background white -selectbackground lightBlue \
55  -yscroll "$text.ybar set" -xscroll "$text.xbar set"
56  $text.text tag configure bold -font font_Bold
57  $text.text tag configure indent -lmargin2 20
58  $text.text configure -cursor top_left_arrow
59  ::htext::init $text.text
60  ttk::scrollbar $text.ybar -command "$text.text yview"
61  ttk::scrollbar $text.xbar -orient horizontal -command "$text.text xview"
62  grid $text.text -row 0 -column 0 -sticky nesw
63  grid $text.ybar -row 0 -column 1 -sticky nesw
64  #grid $text.xbar -row 1 -column 0 -sticky nesw
65  grid rowconfig $text 0 -weight 1 -minsize 0
66  grid columnconfig $text 0 -weight 1 -minsize 0
67 
68  foreach i {0 1 2 3 4 5 6 7 8 9 A B C D E a b c d e f g h i j k l m n o p
69  q r s t u v w x y z} {
70  bind $w <KeyPress-$i> "::windows::eco::KeyPress $i"
71  }
72 
73  foreach i {Left Delete less BackSpace} {
74  bind $w <KeyPress-$i> {::windows::eco::KeyPress "<"}
75  }
76 
77  bind $w <Home> {.ecograph.pane.text.text yview moveto 0}
78  bind $w <End> {.ecograph.pane.text.text yview moveto 1.0}
79  bind $w <Up> {.ecograph.pane.text.text yview scroll -1 units}
80  bind $w <Down> {.ecograph.pane.text.text yview scroll 1 units}
81  bind $w <Prior> {.ecograph.pane.text.text yview scroll -1 pages}
82  bind $w <Next> {.ecograph.pane.text.text yview scroll 1 pages}
83 
84  bind $graph.c <1> { ::windows::eco::Select %x }
85  bind $graph.c <$::MB3> { ::windows::eco::KeyPress "<" }
86 
87  bind $graph <Configure> {
88  ::utils::graph::configure eco -height [expr {[winfo height .ecograph.pane.graph.c] - 50} ]
89  ::utils::graph::configure eco -width [expr {[winfo width .ecograph.pane.graph.c] - 60} ]
90  ::utils::graph::redraw eco
91  }
92  bind $w <Configure> {
93  ::utils::graph::configure eco -height [expr {[winfo height .ecograph.pane.graph.c] - 50} ]
94  ::utils::graph::configure eco -width [expr {[winfo width .ecograph.pane.graph.c] - 60} ]
95  ::utils::graph::redraw eco
96  }
97  ::setTitle $w "Scid: [tr WindowsECO]"
98  ::utils::graph::create eco -width 1 -height 1 -xtop 50 -ytop 20 \
99  -xmin 0.5 -xtick 1 -ytick 5 -font font_Small -canvas $graph.c
100 
102  update
103  }
104 
106 }
107 
108 proc ::windows::eco::update {} {
109  set w .ecograph
110  set graph $w.pane.graph
111  set text $w.pane.text
112 
113  set height [expr {[winfo height $graph.c] - 50}]
114  set width [expr {[winfo width $graph.c] - 60}]
115 
116  set code $::windows::eco::code
117  # Collect data:
118  set len [string length $code]
119  set subcodes {}
120  if {$len == 0} {
121  set subcodes {A B C D E}
122  } elseif {$len == 1 || $len == 2} {
123  set subcodes {0 1 2 3 4 5 6 7 8 9}
124  } elseif {$len == 3} {
125  set subcodes {a b c d e f g h i j k l m n o p q r s t u v w x y z}
126  }
127 
128  set xlabels {}
129  set count 0
130  set data {}
131  set maxfreq 1
132  set wins {}
133  set draws {}
134 
135  set ::curr_db [sc_base current]
136  foreach i $subcodes {
137  set subcode "$code$i"
138  set stats [sc_base stats $::curr_db eco $subcode]
139  set freq [lindex $stats 0]
140  incr count
141  lappend data $count
142  lappend data $freq
143  lappend wins $count
144  lappend wins [lindex $stats 1]
145  lappend draws $count
146  lappend draws [expr {[lindex $stats 1] + [lindex $stats 2] + [lindex $stats 4]}]
147  if {$freq > $maxfreq} {set maxfreq $freq}
148  if {$len == 3} {
149  set subcode $i
150  }
151  lappend xlabels [list $count $subcode]
152  }
153  set hline 5
154  if {$maxfreq > 20} { set hline 10}
155  if {$maxfreq > 50} { set hline 25}
156  if {$maxfreq > 100} { set hline 50}
157  if {$maxfreq > 200} { set hline 100}
158  if {$maxfreq > 500} { set hline 250}
159  if {$maxfreq > 1000} { set hline 500}
160  if {$maxfreq > 2000} { set hline 1000}
161  if {$maxfreq > 5000} { set hline 2500}
162  if {$maxfreq > 10000} { set hline 5000}
163  if {$maxfreq > 20000} { set hline 10000}
164  if {$maxfreq > 50000} { set hline 25000}
165  if {$maxfreq > 100000} { set hline 50000}
166 
167  ::utils::graph::create eco -width $width -height $height -xtop 50 -ytop 20 \
168  -xmin 0.5 -xtick 1 -ytick $hline -font font_Small -canvas $graph.c
169  ::utils::graph::data eco data -color SteelBlue4 -points 0 -lines 0 -bars 1 \
170  -barwidth 0.8 -outline black -coords $data
171  ::utils::graph::data eco draws -color SteelBlue3 -points 0 -lines 0 -bars 1 \
172  -barwidth 0.8 -outline black -coords $draws
173  ::utils::graph::data eco wins -color SteelBlue1 -points 0 -lines 0 -bars 1 \
174  -barwidth 0.8 -outline black -coords $wins
175  ::utils::graph::data eco bounds -points 0 -lines 0 -bars 0 -coords {1 0 1 1}
176  ::utils::graph::configure eco -ymin 0 -xmin 0.4 -xmax [expr {$count + 0.6}] \
177  -xlabels $xlabels -hline [list [list gray80 1 each $hline]]
179  $text.text configure -state normal
180  $text.text delete 1.0 end
181  set stats [sc_base stats $::curr_db eco $code]
182  if {$len == 0} {
183  set section $::tr(ECOAllSections)
184  } elseif {$len < 3} {
185  set section "$::tr(ECOSection) \"$code\""
186  } else {
187  set section "$::tr(ECOCode) \"$code\""
188  }
189  set header "<center><b>$::tr(ECOSummary) $section</b><br>"
190  append header "[lindex $stats 0] $::tr(games): +[lindex $stats 1] =[lindex $stats 2] -[lindex $stats 3] ([lindex $stats 5]%)</center>\n\n"
191  ::htext::display $text.text "$header[sc_eco summary $code 1]"
192  $text.text configure -state disabled
193  $w.title configure -state normal
194  $w.title delete 1.0 end
195  $w.title insert end "$::tr(ECOFrequency) $section" center
196  $w.title configure -state disabled
197  set ::windows::eco::count $count
198 }
199 
200 proc ::windows::eco::Select {xc} {
201  variable count
202  variable code
203 
204  set x [::utils::graph::xunmap eco $xc]
205  set selection 0
206  for {set i 1} {$i <= $count} {incr i} {
207  if {$x >= [expr {$i - 0.4}] && $x <= [expr {$i + 0.4}]} {
208  set selection $i
209  }
210  }
211  if {$selection == 0} { return}
212  incr selection -1
213  set len [string length $code]
214  if {$len == 0} {
215  set code [lindex {A B C D E} $selection]
216  } elseif {$len == 1 || $len == 2} {
217  append code $selection
218  } elseif {$len == 3} {
219  append code [lindex {a b c d e f g h i j k l m n o p q r s t u v w x y z} $selection]
220  } else {
221  return
222  }
224 }
225 
226 # ::windows::eco::KeyPress
227 #
228 # Handles keyboard events in ECO browser window
229 #
230 proc ::windows::eco::KeyPress {key} {
231  set code $::windows::eco::code
232  set len [string length $code]
233  if {$key == "<"} {
234  set ::windows::eco::code [string range $code 0 [expr {$len - 2}]]
236  return
237  }
238  if {$key == "top"} {
239  set ::windows::eco::code ""
241  return
242  }
243 
244  if {$len == 0} {
245  set key [string toupper $key]
246  switch $key {
247  A - B - C - D - E {
248  # nothing
249  }
250  default { set key ""}
251  }
252  } elseif {$len == 1 || $len == 2} {
253  switch $key {
254  0 - 1 - 2 - 3 - 4 - 5 - 6 - 7 - 8 - 9 {
255  # nothing
256  }
257  default { set key ""}
258  }
259  } elseif {$len == 3} {
260  set key [string tolower $key]
261  switch $key {
262  a - b - c - d - e - f - g - h - i - j - k - l - m - n - o - p - q - r -
263  s - t - u - v - w - x - y - z {
264  # nothing
265  }
266  default { set key ""}
267  }
268  }
269 
270  if {$key != ""} {
271  set ::windows::eco::code "$code$key"
273  }
274 }
275