Scid  4.6.5
spellchk.tcl
Go to the documentation of this file.
1 ### spellchk.tcl
2 ### Part of Scid.
3 ### Copyright (C) 2000-2003 Shane Hudson.
4 
5 set spellcheckType Player
6 
7 # Maximum nr of corrections to be scanned
8 # Set to zero to find them all
9 # Set to some positive number to limit
10 #
11 set spell_maxCorrections 0
12 
13 set spellcheckSurnames 1
14 set spellcheckAmbiguous 0
15 
16 # Remember what we are doing, being
17 # - "idle" - nothing special
18 # - "scanning" - finding corrections
19 # - "correcting" - making corrections
20 #
21 set spellstate idle
22 
23 # readSpellCheckFile:
24 # Presents a File Open dialog box for a Scid spellcheck file,
25 # then tries to read the file. If the parameter "message" is true
26 # (which is the default), a message box indicating the results
27 # is displayed.
28 #
29 proc readSpellCheckFile {{message 1}} {
30  global spellCheckFile
31  set ftype { { "Scid Spellcheck files" {".ssp" ".ssp.gz"} } }
32  set fullname [tk_getOpenFile -initialdir [pwd] -filetypes $ftype -title "Open Spellcheck file"]
33  if {![string compare $fullname ""]} { return 0}
34 
35  progressWindow "Scid - [tr Spellcheking]" "Loading $fullname ..."
36  set err [catch {sc_name read $fullname} result]
38  if {$err} {
39  if {$message} {
40  tk_messageBox -title "ERROR: Unable to read file" -type ok \
41  -icon error -message "Scid could not correctly read the spellcheck file you selected:\n\n$result"
42  }
43  return 0
44  }
45  set spellCheckFile $fullname
46  if {$message} {
47  tk_messageBox -title "Spellcheck file loaded." -type ok -icon info \
48  -message "Spellcheck file [file tail $fullname] loaded:\n[lindex $result 0] players, [lindex $result 1] events, [lindex $result 2] sites, [lindex $result 3] rounds.\n\nTo have this file automatically loaded every time you start Scid, select the \"Save Options\" from the Options menu before exiting."
49  }
50  return 1
51 }
52 
53 # Set the environment when the correction scan starts
54 #
55 proc startScanning {} {
56  global spellstate
57  global spellcheckType
58 
59  # Remember that we are scanning
60  #
61  set spellstate scanning
62 
63  # Disable all buttons except the cancel button that we
64  # transfer into a stop button
65  #
66  .spellcheckWin.buttons.ambig configure -state disabled
67  .spellcheckWin.buttons.ok configure -state disabled
68  .spellcheckWin.buttons.cancel configure -text "Stop"
69  bind .spellcheckWin <Alt-s> ".spellcheckWin.buttons.cancel invoke; break"
70  if {$spellcheckType == "Player"} {
71  .spellcheckWin.buttons.surnames configure -state disabled
72  }
73 }
74 
75 # Set the environment when the correction scan stops
76 #
77 proc stopScanning {} {
78  global spellstate
79  global spellcheckType
80 
81  # Remember that we are not scanning
82  #
83  set spellstate idle
84 
85  # Enable all buttons and set the cancel button back
86  #
87  .spellcheckWin.buttons.ambig configure -state enabled
88  .spellcheckWin.buttons.ok configure -state enabled
89  .spellcheckWin.buttons.cancel configure -text "Cancel"
90  bind .spellcheckWin <Alt-c> ".spellcheckWin.buttons.cancel invoke; break"
91  if {$spellcheckType == "Player"} {
92  .spellcheckWin.buttons.surnames configure -state enabled
93  }
94 }
95 
96 
97 # Set the environment when correction starts
98 #
99 proc startCorrecting {} {
100  global spellstate
101  global spellcheckType
102 
103  # Remember that we are correcting
104  #
105  set spellstate correcting
106 
107  # Disable all buttons
108  #
109  .spellcheckWin.buttons.ambig configure -state disabled
110  .spellcheckWin.buttons.ok configure -state disabled
111  .spellcheckWin.buttons.cancel configure -state disabled
112 
113  if {$spellcheckType == "Player"} {
114  .spellcheckWin.buttons.surnames configure -state disabled
115  }
116 }
117 
118 
119 # Start the correction scan and dump the results into the
120 # text window. After this the user may edit the correction
121 # 'script' and actually make the corrections.
122 #
123 # While the scan is running, all buttons except a stop button
124 # are disabled.
125 #
126 proc updateSpellCheckWin {type} {
127  global spellcheckType spell_maxCorrections spellcheckSurnames
128  global spellcheckAmbiguous
129 
130  .spellcheckWin.text.text delete 1.0 end
131  .spellcheckWin.text.text insert end "Scid is finding spelling corrections.\nPlease wait..."
132 
133  # Enable the progress bar
134  #
135 
137 
138  update idletasks
139  progressBarSet .spellcheckWin.progress 451 21
140  set err [catch {sc_name spellcheck -max $spell_maxCorrections \
141  -surnames $spellcheckSurnames \
142  -ambiguous $spellcheckAmbiguous $type} result]
144  if {$err} {
145  ERROR::MessageBox "" "Scid: Spellcheck results"
146  return
147  }
148 
149  .spellcheckWin.text.text delete 1.0 end
150  .spellcheckWin.text.text insert end $result
151 }
152 
153 
154 # Create the spell checking window with its event handlers
155 # and start the initial correction scan
156 #
157 proc openSpellCheckWin {type {parent .}} {
158  global spellcheckType spellcheckSurnames
159  global spellcheckAmbiguous
160  global spellstate
161 
162  set w .spellcheckWin
163 
164  if {[winfo exists $w]} {
165  tk_messageBox -type ok -icon info -title "Scid: Spellcheck error" \
166  -parent $parent \
167  -message "The spellcheck window is already open; close it first."
168  return
169  }
170 
171  if {[lindex [sc_name read] 0] == 0} {
172  # No spellcheck file loaded, so try to open one:
173  if {![readSpellCheckFile]} {
174  return
175  }
176  }
177  set spellcheckType $type
178 
179  toplevel $w
180  wm title $w "Scid: Spellcheck Results"
181  wm minsize $w 50 10
182 
183  bind $w <F1> { helpWindow Maintenance }
184  bind $w <Configure> "recordWinSize $w"
185 
186  # Create the button pad at the bottom of the window
187  #
188  set f [ttk::frame $w.buttons]
189  pack $f -side bottom -ipady 1 -fill x
190 
191  # Draw a canvas ("progress") to hold the progress bar
192  # and put it above the buttons at the bottom of the window
193  #
194  canvas $w.progress -width 450 -height 20 -bg white -relief solid -border 1
195  $w.progress create rectangle 0 0 0 0 -fill blue -outline blue -tags bar
196  $w.progress create text 445 10 -anchor e -font font_Regular -tags time \
197  -fill black -text "0:00 / 0:00"
198  pack $w.progress -side bottom
199 
200  # The ambiguous check mark
201  # Hitting it starts a new correction scan
202  ttk::checkbutton $f.ambig -variable spellcheckAmbiguous \
203  -text "Ambiguous" -command "updateSpellCheckWin $type"
204  pack $f.ambig -side left -padx 2 -ipady 2 -ipadx 3
205 
206  # When correcting player names, we add a surnames option
207  #
208  if {$type == "Player"} {
209  # The surnames check mark
210  # Hitting it starts a new correction scan
211  #
212  ttk::checkbutton $f.surnames -variable spellcheckSurnames \
213  -text "Surnames" -command "updateSpellCheckWin Player"
214  pack $f.surnames -side left -padx 2 -ipady 2 -ipadx 3
215  }
216 
217  # The button to start the correction making...
218  #
219  ttk::button $f.ok -text "Make Corrections" -underline 0 -command {
220  busyCursor .
221  set spelltext ""
222  catch {set spelltext [.spellcheckWin.text.text get 1.0 end-1c]}
223  .spellcheckWin.text.text delete 1.0 end
224  .spellcheckWin.text.text insert end \
225  "Scid is making the spelling corrections.\nPlease wait..."
226 
227  # Enable the progress bar
228  #
229  update idletasks
230  set spell_result ""
231  startCorrecting
232  progressBarSet .spellcheckWin.progress 451 21
233  set err [catch {sc_name correct $spellcheckType $spelltext} spell_result]
234  if ($err) {
235  ERROR::MessageBox
236  } else {
237  set msg "Number of names to be corrected: "
238  append msg "[lindex $spell_result 0] \n"
239  append msg "Number of names errors: "
240  append msg "[lindex $spell_result 1] \n\n"
241  append msg "Number of games corrected: "
242  append msg "[lindex $spell_result 2] \n"
243  append msg "NUmber of games _not_ corrected (date < birth or > death): "
244  append msg "[lindex $spell_result 3] \n"
245  tk_messageBox -type ok -parent .spellcheckWin \
246  -title "Scid: Spellcheck results" -message $msg
247  }
248  unbusyCursor .
249  focus .
250  destroy .spellcheckWin
251  sc_game tags reload
252  updateBoard -pgn
253  ::windows::gamelist::Refresh
254  }
255  bind $w <Alt-m> "$f.ok invoke; break"
256 
257  # The cancel button operates in an either/or context
258  # While some process is running, it simply stops it
259  # In other cases, spell checking is left
260  #
261  ttk::button $f.cancel -text "Cancel" -underline 0 -command {
262  if {$spellstate == "scanning" || $spellstate == "correcting"} {
263  progressBarCancel
264  } else {
265  focus .
266  destroy .spellcheckWin
267  }
268  }
269  bind $w <Alt-c> "$f.cancel invoke; break"
270  pack $f.cancel $f.ok -side right -padx 5
271 
272  # Prepare the text pad
273  #
274  set f [ttk::frame $w.text]
275  pack $w.text -expand yes -fill both
276  ttk::scrollbar $f.ybar -command "$f.text yview"
277  ttk::scrollbar $f.xbar -orient horizontal -command "$f.text xview"
278  text $f.text -yscrollcommand "$f.ybar set" -xscrollcommand "$f.xbar set" \
279  -setgrid 1 -width $::winWidth($w) -height $::winHeight($w) \
280  -background white -wrap none
281  $f.text configure -tabs \
282  [font measure font_Regular "xxxxxxxxxxxxxxxxxxxxxxxxx"]
283 
284  grid $f.text -row 0 -column 0 -sticky news
285  grid $f.ybar -row 0 -column 1 -sticky news
286  grid $f.xbar -row 1 -column 0 -sticky news
287 
288  grid rowconfig $w.text 0 -weight 1 -minsize 0
289  grid columnconfig $w.text 0 -weight 1 -minsize 0
290 
291  focus $f.text
292 
293  # Start the initial search for spelling corrections
294  #
295  updateSpellCheckWin $type
296 }
297 
298