Scid  4.7.0
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"} } }
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 $::tr(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 
180  wm title $w "Scid: $::tr(Spellchecking) $::tr(Result)"
181  wm minsize $w 0 15
182 
183  bind $w <F1> { helpWindow Maintenance }
184  bind $w <Configure> "recordWinSize $w"
185 
186  # Prepare the text pad
187  #
188  set f [ttk::frame $w.text]
189  pack $w.text -side top -expand true -fill both
190  ttk::scrollbar $f.ybar -command "$f.text yview"
191  ttk::scrollbar $f.xbar -orient horizontal -command "$f.text xview"
192  text $f.text -yscrollcommand "$f.ybar set" -xscrollcommand "$f.xbar set" \
193  -setgrid 1 -width $::winWidth($w) -height $::winHeight($w) \
194  -background white -wrap none
195  $f.text configure -tabs \
196  [font measure font_Regular "xxxxxxxxxxxxxxxxxxxxxxxxx"]
197 
198  grid $f.text -row 0 -column 0 -sticky nswe
199  grid $f.ybar -row 0 -column 1 -sticky nswe
200  grid $f.xbar -row 1 -column 0 -sticky nswe
201 
202  grid rowconfig $w.text 0 -weight 1 -minsize 0
203  grid columnconfig $w.text 0 -weight 1 -minsize 0
204 
205  focus $f.text
206  # Draw a canvas ("progress") to hold the progress bar
207  # and put it above the buttons at the bottom of the window
208  #
209  canvas $w.progress -width 450 -height 20 -bg white -relief solid -border 1
210  $w.progress create rectangle 0 0 0 0 -fill blue -outline blue -tags bar
211  $w.progress create text 445 10 -anchor e -font font_Regular -tags time \
212  -fill black -text "0:00 / 0:00"
213  pack $w.progress -side top -pady 5
214 
215  # Create the button pad at the bottom of the window
216  #
217  set f [ttk::frame $w.buttons]
218  pack $f -side bottom -fill x
219 
220  # The ambiguous check mark
221  # Hitting it starts a new correction scan
222  ttk::checkbutton $f.ambig -variable spellcheckAmbiguous \
223  -text $::tr(Ambiguous) -command "updateSpellCheckWin $type"
224  pack $f.ambig -side left -padx 2 -ipady 2 -ipadx 3
225 
226  # When correcting player names, we add a surnames option
227  #
228  if {$type == "Player"} {
229  # The surnames check mark
230  # Hitting it starts a new correction scan
231  #
232  ttk::checkbutton $f.surnames -variable spellcheckSurnames \
233  -text $::tr(Surnames) -command "updateSpellCheckWin Player"
234  pack $f.surnames -side left -padx 2 -ipady 2 -ipadx 3
235  }
236 
237  # The button to start the correction making...
238  #
239  ttk::button $f.ok -text $::tr(MakeCorrections) -underline 0 -command {
240  busyCursor .
241  set spelltext ""
242  catch {set spelltext [.spellcheckWin.text.text get 1.0 end-1c]}
243  .spellcheckWin.text.text delete 1.0 end
244  .spellcheckWin.text.text insert end \
245  "Scid is making the spelling corrections.\nPlease wait..."
246 
247  # Enable the progress bar
248  #
249  update idletasks
250  set spell_result ""
251  startCorrecting
252  progressBarSet .spellcheckWin.progress 451 21
253  set err [catch {sc_name correct $spellcheckType $spelltext} spell_result]
254  if ($err) {
255  ERROR::MessageBox
256  } else {
257  set msg "Number of names to be corrected: "
258  append msg "[lindex $spell_result 0] \n"
259  append msg "Number of names errors: "
260  append msg "[lindex $spell_result 1] \n\n"
261  append msg "Number of games corrected: "
262  append msg "[lindex $spell_result 2] \n"
263  append msg "Number of games NOT corrected (date<birth or >death): "
264  append msg "[lindex $spell_result 3]"
265  tk_messageBox -type ok -parent .spellcheckWin \
266  -title "Scid: $::tr(Spellchecking) $::tr(Result)" -message $msg
267  }
268  unbusyCursor .
269  focus .
270  destroy .spellcheckWin
271  sc_game tags reload
272  updateBoard -pgn
273  ::windows::gamelist::Refresh
274  }
275  bind $w <Alt-m> "$f.ok invoke; break"
276 
277  # The cancel button operates in an either/or context
278  # While some process is running, it simply stops it
279  # In other cases, spell checking is left
280  #
281  ttk::button $f.cancel -text $::tr(Cancel) -underline 0 -command {
282  if {$spellstate == "scanning" || $spellstate == "correcting"} {
283  progressBarCancel
284  } else {
285  focus .
286  destroy .spellcheckWin
287  }
288  }
289  bind $w <Alt-c> "$f.cancel invoke; break"
290  pack $f.cancel $f.ok -side right -padx 5 -fill x
291 
292 
293  # Start the initial search for spelling corrections
294  #
295  updateSpellCheckWin $type
296 }
297 
298