Rewriting performance evaluation
[sgc2.git] / Config.praat
blob02defcd5fc1bff3c648b6e21273bb76832ebf1d6
2 # SpeakGoodChinese 2.0
3
4 # Praat script handling configuration page
6 #     SpeakGoodChinese: Config.praat loads the code needed for the 
7 #     settings and the Settings page of SpeakGoodChinese.
8 #     
9 #     Copyright (C) 2007-2010  R.J.J.H. van Son and 2010 the Netherlands Cancer Institute
10 #     The SpeakGoodChinese team are:
11 #     Guangqin Chen, Zhonyan Chen, Stefan de Koning, Eveline van Hagen, 
12 #     Rob van Son, Dennis Vierkant, David Weenink
13
14 #     This program is free software; you can redistribute it and/or modify
15 #     it under the terms of the GNU General Public License as published by
16 #     the Free Software Foundation; either version 2 of the License, or
17 #     (at your option) any later version.
18
19 #     This program is distributed in the hope that it will be useful,
20 #     but WITHOUT ANY WARRANTY; without even the implied warranty of
21 #     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22 #     GNU General Public License for more details.
23
24 #     You should have received a copy of the GNU General Public License
25 #     along with this program; if not, write to the Free Software
26 #     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
27
29 ###############################################################
31 # Button Drawing Routines
33 ###############################################################
35 procedure DrawCredits .color$ .x .y .size
36         .size *= 0.71
37         .lineheight = 2*.size
38         call adjustFontSizeOnHeight 'defaultFont$' 24 '.lineheight'
39         .currentFontSize = adjustFontSizeOnHeight.newFontSize
40     demo Paint circle... {0.2,0.2,0.8} '.x' '.y' '.size'
41         demo Colour... White
42         demo Text special... '.x' centre '.y' half Times '.currentFontSize' 0 i
43         demoShow()
44         demo Colour... Black
45         call set_font_size 'defaultFontSize'    
46 endproc
48 procedure DrawDeleteList .color$ .x .y .size
49     .y += 2*.size
50     .leftX = .x - 10
51     .rightX = .x + 10
52     .botY = .y+1
53     .topY = .botY + 5
54         call set_font_size 12
55     demo Red
56     .displayWordList$ = replace_regex$(wordlistName$, "[_]", " ", 0)
58         # Adapt wide of text
59         .maxWidth = (.rightX - .leftX)
60     .currentFontSize = defaultFontSize
61         call adjustFontSizeOnWidth 'defaultFont$' '.currentFontSize' '.maxWidth' '.displayWordList$'
62         .currentFontSize = adjustFontSizeOnWidth.newFontSize
63         if adjustFontSizeOnWidth.diff > 0
64                 .rightX += adjustFontSizeOnWidth.diff/2
65                 .leftX -= adjustFontSizeOnWidth.diff/2
66         endif
67         call set_font_size '.currentFontSize'
68     demo Paint rectangle... White '.leftX' '.rightX' '.botY' '.topY'
70         demo Text... '.x' Centre '.botY' Bottom '.displayWordList$'
71     demo Black
72         demoShow()
73         call set_font_size 'defaultFontSize'
74 endproc
76 procedure DrawManual .color$ .x .y .size
77         .size *= 0.71
78         .lineheight = 2*.size
79         call adjustFontSizeOnHeight 'defaultFont$' 24 '.lineheight'
80         .currentFontSize = adjustFontSizeOnHeight.newFontSize
81     demo Paint circle... {0.2,0.2,0.8} '.x' '.y' '.size'
82         demo Colour... White
83         demo Text special... '.x' centre '.y' half Times '.currentFontSize' 0 M
84         demoShow()
85         demo Colour... Black
86         call set_font_size 'defaultFontSize'    
87 endproc
89 procedure DrawLogging .color$ .x .y .size
90     .size /= 2
91         .y += .size
92     if .color$ = "Blue"
93         .color$ = "{0.5,0.5,1}"
94      else
95         .color$ = "Blue"
96      endif
97     demo Paint circle... '.color$' '.x' '.y' '.size'
98 endproc
100 ###############################################################
102 # Obligatory button Drawing Routines
104 # These MUST be defined
106 ###############################################################
108 procedure DrawReturn .color$ .x .y .size
109     call DrawConfig '.color$' '.x' '.y' '.size'
110 endproc
112 # Set the correct button states after redrawing the window
113 procedure setConfigMainPage
114         call testLoadTable SummaryToneEvaluation
115         if testLoadTable.table <= 0
116                 call Draw_button 'config$' PerfSummary 1
117         endif
118     # Handle logging buttons with forced button draw
119     if fileReadable("'preferencesLogDir$'/logPerformance.txt")
120         call Draw_button 'config$' +!Logging 'config.logPerformance'
121     endif
122 endproc
124 ###############################################################
126 # Button Processing Routines
128 ###############################################################
130 procedure processConfigShuffleLists .clickX .clickY .pressed$
131         .table$ = "Config"
132         .label$ = "ShuffleLists"
133         config.shuffleLists = not config.shuffleLists
134         .displayButton = 2*config.shuffleLists
135     call Draw_button '.table$' '.label$' '.displayButton'
136 endproc
138 procedure processConfigUseSoundExample .clickX .clickY .pressed$
139         .table$ = "Config"
140         .label$ = "UseSoundExample"
141         config.useSoundExample = not config.useSoundExample
142         .displayButton = 2*config.useSoundExample
143     call Draw_button '.table$' '.label$' '.displayButton'
144 endproc
145         
146 procedure processConfigSynthesis .tts$ .clickX .clickY .pressed$
147         .table$ = "Config"
148         .label$ = "Synthesis_'.tts$'"
149         if sgc2.synthesizer > 0 or (speakCommandFile$ <> "" and fileReadable(speakCommandFile$))
150                 if config.synthesis$ = ""
151                         config.synthesis$ = .tts$
152                         .displayButton = 2
153                 else
154                         config.synthesis$ = ""
155                         .displayButton = 0
156                 endif
157         else
158                 config.synthesis$ = "_DISABLED_"
159                 .displayButton = -1
160         endif
161     call Draw_button '.table$' '.label$' '.displayButton'
162 endproc
163         
164 procedure processConfigStrict .clickX .clickY .pressed$
165         .table$ = "Config"
166         .label$ = "Strict"
167         config.strict = not config.strict
168         if config.strict > 0
169                 .displayButton = 2
170         else
171                 .displayButton = 0
172         endif
173         # Change TTS for Strict!
174         call set_up_TTS
175     call Draw_button '.table$' '.label$' '.displayButton'
176 endproc
178 procedure processConfigDisplayPinyin .clickX .clickY .pressed$
179         .table$ = "Config"
180         .label$ = "DisplayPinyin"
181         config.displayPinyin = not config.displayPinyin
182         .displayButton = 2*config.displayPinyin
183     call Draw_button '.table$' '.label$' '.displayButton'
184 endproc
185                 
186 procedure processConfigDisplayChar .clickX .clickY .pressed$
187         .table$ = "Config"
188         .label$ = "DisplayChar"
189         config.displayChar = not config.displayChar
190         .displayButton = 2*config.displayChar
191     call Draw_button 'table$' '.label$' '.displayButton'
192 endproc
194 procedure processConfigDisplayTrans .clickX .clickY .pressed$
195         .table$ = "Config"
196         .label$ = "DisplayTrans"
197         config.displayTrans = not config.displayTrans
198         .displayButton = 2*config.displayTrans
199     call Draw_button 'table$' '.label$' '.displayButton'
200 endproc
202 procedure processConfigDisplayNumbers .clickX .clickY .pressed$
203         .table$ = "Config"
204         .label$ = "DisplayNumbers"
205         config.displayNumbers = not config.displayNumbers
206         .displayButton = 2*config.displayNumbers
207     call Draw_button 'table$' '.label$' '.displayButton'
208 endproc
210 procedure processConfigLanguage .language$ .clickX .clickY .pressed$
211         .table$ = "Config"
212         .label$ = "Language_'.language$'"
213         call processLanguageCodes '.table$' '.label$'
214 endproc
215         
216 procedure processConfigShowBackground .clickX .clickY .pressed$
217         .table$ = "Config"
218         .label$ = "ShowBackground"
219         config.showBackground = not config.showBackground
220         .displayButton = 2*config.showBackground
221     call Draw_button 'table$' '.label$' '.displayButton'
222 endproc
224 procedure processConfigInput .input$ .clickX .clickY .pressed$
225         .table$ = "Config"
226         .label$ = "Input_'.input$'"
227     call Draw_button '.table$' Input_'config.input$' 0
228         config.input$ = .input$
229     call Draw_button '.table$' Input_'config.input$' 2
230 endproc
232 procedure processConfigRegister .register .clickX .clickY .pressed$
233         .table$ = "Config"
234         .label$ = "Register_'.register'"
235         call setRegisterFromLabel '.table$' '.label$'
236 endproc
237         
238 procedure setRegisterFromLabel .table$ .label$
239     call Draw_button '.table$' Register_'config.register' 0
240     call Draw_button '.table$' '.label$' 2
241     # Someone might have to use more than 3 chars for the config.register code
242     .numChars = length(.label$) - length("Register_")
243         .registerText$ = right$(.label$, .numChars)
244         config.register = '.registerText$'
245 endproc
247 procedure processConfigDeleteWordlist .clickX .clickY .pressed$
248         .table$ = "Config"
249         .label$ = "DeleteWordlist"
251     # Do not process undeletable word lists, only those stored in the 
252     # preferencesDirectory$ can be deleted
253     if fileReadable("'sgc2wordlists$'/'wordlistName$'") or fileReadable("'sgc2wordlists$'/'wordlistName$'/wordlist.txt") or     fileReadable("'sgc2wordlists$'/'wordlistName$'/wordlist.Table")
254                 call findLabel '.table$' !'.label$'
255                 if findLabel.row > 0
256                 select Table '.table$'
257                 .alertText$ = Get value... 'findLabel.row' Text
258                 .confirmKey$ = Get value... 'findLabel.row' Key
259                 .popupText$ = Get value... 'findLabel.row' Helptext
260         else
261             exit Cannot find delete directive: '.table$' !'.label$'
262         endif
263         call write_text_popup 'defaultFont$' 14 '.popupText$'
264         call Draw_button '.table$' '.label$' 2
265         alertText$ = .alertText$
266         call Draw_button '.table$' '.label$' 3
267         alertText$ = ""
268         
269                 # Wait for confirmation
270                 demoWaitForInput()
271         if demoInput(.confirmKey$)
272                 .deleteWordListDir$ = wordlistName$
273                 call load_word_list "'localWordlistDir$'" 1
274                 call removeWordlist '.deleteWordListDir$'
275                         call load_word_list "'localWordlistDir$'" 0
276         endif
277                 call Draw_button '.table$' '.label$' 0
278                 call Draw_config_page
279     endif
280 endproc
282 wordlistTag$ = "Wordlist name"        
283 procedure processConfigInstallWordlist .clickX .clickY .pressed$
284         .table$ = "Config"
285         .label$ = "InstallWordlist"
286     call Draw_button '.table$' '.label$' 1
287         if windows
288                 # Do not use the automatic sgc list option, ask for a wordlist NAME
289                 # Get help text
290                 call findLabel '.table$' '.label$'
291                 .row = findLabel.row
292                 select Table '.table$'
293                 .openDialogue$ = Get value... '.row' Helptext
294                 call convert_praat_to_latin1 '.openDialogue$'
295                 .openDialogue$ = convert_praat_to_latin1.text$
296                 .wordlistButton$ = replace$(wordlistTag$, " ", "_", 0)
297                 .wordlistButton$ = replace_regex$(.wordlistButton$, "^.", "\l&", 0)
298                 beginPause(.openDialogue$)
299                         sentence (wordlistTag$, "")
300                 clicked = endPause ("Cancel", "Open", 2, 1)
301                 .wordlist_Name$ = ""
302                 if clicked = 2
303                         .wordlist_Name$ = '.wordlistButton$'$
304                 endif
305                 call install_wordlists_by_name '.wordlist_Name$'
306         else
307         call sgc2wordlist 'homeDirectory$'
308         call sgc2wordlist 'homeDirectory$'/Downloads
309         call sgc2wordlist 'homeDirectory$'/Documents
310         call sgc2wordlist 'homeDirectory$'/My Documents
311         call sgc2wordlist 'homeDirectory$'/My Documents/Downloads
312         call sgc2wordlist 'homeDirectory$'/Desktop
313         call sgc2wordlist 'preferencesAppDir$'
314         endif
315         call load_word_list "'localWordlistDir$'" 0
316     call Draw_button '.table$' '.label$' 0
317         call Draw_config_page
318 endproc
320 procedure processConfigOpenWordlist .clickX .clickY .pressed$
321         .table$ = "Config"
322         .label$ = "OpenWordlist"
323     call Draw_button '.table$' '.label$' 1
325         # Get help text
326         call findLabel '.table$' '.label$'
327         .row = findLabel.row
328         select Table '.table$'
329         .openDialogue$ = Get value... '.row' Helptext
330         call convert_praat_to_latin1 '.openDialogue$'
331         .openDialogue$ = convert_praat_to_latin1.text$
333         .wordlist_Name$ = chooseReadFile$ (.openDialogue$)
334         call install_wordlists_by_name '.wordlist_Name$'
335         call load_word_list "'localWordlistDir$'" 0
336     call Draw_button '.table$' '.label$' 0
337         call Draw_config_page
338 endproc
340 # Select the words to practise. This is quite a complex piece of code
341 procedure processConfigSelectWords .clickX .clickY .pressed$
342         .table$ = "Config"
343         .label$ = "SelectWords"
344     call Draw_button '.table$' '.label$' 1
346         # Get help text
347         call findLabel '.table$' '.label$'
348         .row = findLabel.row
349         select Table '.table$'
350         .helpText$ = Get value... '.row' Helptext
351         call convert_praat_to_latin1 '.helpText$'
352         .helpText$ = convert_praat_to_latin1.text$
353    
354     # Implement Cancel
355     select Table 'wordlist$'
356     .tmpOriginalWordlist = Copy: "Original_'wordlist$'"
358     
359     # Set current word
360         select Table 'wordlist$'
361         te.numberOfWords = Get number of rows
362     .currentWord = te.currentWord
363     if .currentWord <= 0 or .currentWord > te.numberOfWords or config.shuffleLists
364                 .currentWord = 1
365         endif
366     
367         # The texts
368         call get_feedback_text 'config.language$' Part
369         call convert_praat_to_latin1 'get_feedback_text.text$'
370         .partText$ = convert_praat_to_latin1.text$
371         call get_feedback_text 'config.language$' Tones
372         call convert_praat_to_latin1 'get_feedback_text.text$'
373         .toneText$ = convert_praat_to_latin1.text$
374         call get_feedback_text 'config.language$' Cancel
375         call convert_praat_to_latin1 'get_feedback_text.text$'
376         .cancelText$ = convert_praat_to_latin1.text$
377         call get_feedback_text 'config.language$' Clear
378         call convert_praat_to_latin1 'get_feedback_text.text$'
379         .clearText$ = convert_praat_to_latin1.text$
380         call get_feedback_text 'config.language$' All
381         call convert_praat_to_latin1 'get_feedback_text.text$'
382         .allText$ = convert_praat_to_latin1.text$
383         call get_feedback_text 'config.language$' Previous
384         call convert_praat_to_latin1 'get_feedback_text.text$'
385         .prevWord$ = convert_praat_to_latin1.text$
386         call get_feedback_text 'config.language$' Next
387         call convert_praat_to_latin1 'get_feedback_text.text$'
388         .nextWord$ = convert_praat_to_latin1.text$
389         call get_feedback_text 'config.language$' Continue
390         call convert_praat_to_latin1 'get_feedback_text.text$'
391         .continueText$ = convert_praat_to_latin1.text$
392         call get_feedback_text 'config.language$' Show
393         call convert_praat_to_latin1 'get_feedback_text.text$'
394         .showText$ = convert_praat_to_latin1.text$
396         .pinyinText$ = "Pinyin"
397         .characterText$ = "Character"
398         
399         # Get lesson names
400         select Table 'wordlist$'
401         .lessonCol = Get column index: "Lesson"
402         .lessonList$ = tab$
403         .numLessons = 0
404         # All shown keeps track whether all words are shown
405         # If so, selecting a lesson is preceded by a Clear All
406         .allShown = 0
407         # Sort words for consistent selection interface
408         if config.shuffleLists
409                 # Allow subdivision in lessons
410                 if .lessonCol > 0
411                         Sort rows... Lesson Pinyin
412                 else
413                         Sort rows... Pinyin
414                 endif
415         endif           
416         if .lessonCol > 0
417                 for .i to te.numberOfWords
418                         .currentLesson$ = Get value: .i, "Lesson"
419                         if .currentLesson$ <> "" and .currentLesson$ <> "-" and index_regex(.lessonList$, "\t'.currentLesson$'\t") <= 0
420                                 .lessonList$ = .lessonList$ + .currentLesson$ + tab$
421                                 .numLessons += 1
422                                 .lessonName$['.numLessons'] = .currentLesson$
423                         endif
424                         .shown$ = Get value: .i, "Show"
425                         if .shown$ = "-"
426                                 .allShown = 0
427                         endif
428                 endfor
429         endif
430         
431         clicked = -1
432         .numWordsPerScreen = 15
433         while clicked <> 6 and clicked <> 1
434                 .lessonSelected = -1
435                 .toneSelected = -1
436                 .firstShown = -1
437                 
438                 select Table 'wordlist$'
439                 .lessonCol = Get column index: "Lesson"
440                 # Sort words for consistent selection interface
441         if config.shuffleLists
442                         # Allow subdivision in lessons
443                         if .lessonCol > 0
444                                 Sort rows... Lesson Pinyin
445                         else
446                                 Sort rows... Pinyin
447                         endif
448         endif           
449                 .max = .numWordsPerScreen - 1
450                 if .currentWord + .max > te.numberOfWords
451                         .max = te.numberOfWords - .currentWord
452                 endif
453                 for .i from 0 to .numWordsPerScreen - 1
454                         if .i <= .max
455                                 .pinyin$[.i] = Get value: .currentWord+.i, "Pinyin"
456                                 .character$[.i] = Get value: .currentWord+.i, "Character"
457                                 .lessonNum$[.i] = ""
458                                 if .lessonCol > 0
459                                         .lessonNum$[.i] = Get value: .currentWord+.i, "Lesson"
460                                         .lessonNum$[.i] = " : " + .lessonNum$[.i]
461                                 endif
462                                 .showText$[.i] = .pinyin$['.i']
463                                 if .character$['.i'] <> "-"
464                                         .showText$[.i] = .pinyin$['.i']+" ("+.character$['.i']+.lessonNum$['.i']+")"
465                                 elsif .lessonNum$['.i'] <> ""
466                                         .showText$[.i] = .pinyin$['.i']+" ( -"+.lessonNum$['.i']+")"
467                                 endif
468                                 .showValueText$[.i] = Get value: .currentWord+.i, "Show"
469                                 if .showValueText$[.i] = "-"
470                                         .showValue[.i] = 0
471                                 else
472                                         .showValue[.i] = 1
473                                 endif
474                                 .showVariable$[.i] = replace_regex$(.pinyin$[.i], ".+", "\l&", 0)
475                                 .showVariable$[.i] = replace_regex$(.showVariable$[.i], " ", "_", 0)
476                                 .tmp$ = .showVariable$['.i']
477                                 '.tmp$' = .showValue[.i]
478                         else
479                                 .showText$[.i] = "-"
480                                 .showValue[.i] = 0
481                         endif
482                 endfor
484                 # The user text input window (beginPause .... endPause)
485                 beginPause(.helpText$)
486                         boolean (.showText$[0], .showValue[0])
487                         boolean (.showText$[1], .showValue[1])
488                         boolean (.showText$[2], .showValue[2])
489                         boolean (.showText$[3], .showValue[3])
490                         boolean (.showText$[4], .showValue[4])
491                         boolean (.showText$[5], .showValue[5])
492                         boolean (.showText$[6], .showValue[6])
493                         boolean (.showText$[7], .showValue[7])
494                         boolean (.showText$[8], .showValue[8])
495                         boolean (.showText$[9], .showValue[9])
496                         boolean (.showText$[10], .showValue[10])
497                         boolean (.showText$[11], .showValue[11])
498                         boolean (.showText$[12], .showValue[12])
499                         boolean (.showText$[13], .showValue[13])
500                         boolean (.showText$[14], .showValue[14])
501                         if .numLessons > 0
502                                 optionMenu: .partText$, 1
503                                         option: "---"
504                                         .j = 0
505                                         for .j to .numLessons
506                                                 option: .lessonName$['.j']
507                                         endfor
508                         endif
509                         optionMenu: .toneText$, 1
510                                 option: "---"
511                                 option: "0"
512                                 option: "1"
513                                 option: "2"
514                                 option: "3"
515                                 option: "4"
516                 clicked = endPause ("'.cancelText$'", "'.clearText$'", "'.allText$'", "'.prevWord$'", "'.nextWord$'", "'.continueText$'", 6, 1)
517                 
518                 if clicked = 2
519                         for .i to te.numberOfWords
520                                 Set string value: .i, "Show", "-"
521                         endfor
522                         .allShown = 0
523                 elsif clicked = 3
524                         for .i to te.numberOfWords
525                                 Set string value: .i, "Show", "+"
526                         endfor
527                         .allShown = 1
528                 elsif clicked != 1
529                         # Get selected Part BEFORE we do anything else
530                         if .numLessons > 0
531                                 .tmp$ = replace_regex$(.partText$, "^(.)", "\l\1", 0)
532                                 .lessonSelected = '.tmp$' - 1
533                         endif
534                         if .lessonSelected > 0
535                                 .allShown = 1
536                                 .firstShown = -1
537                                 select Table 'wordlist$'
538                                 for .i to te.numberOfWords
539                                         # Keep track of whether all are shown
540                                         .shown$ = Get value: .i, "Show"
541                                         if .shown$ = "-"
542                                                 .allShown = 0
543                                         endif
544                                 endfor
545                                 for .i to te.numberOfWords
546                                         # Lessons
547                                         .currentLesson$ = Get value: .i, "Lesson"
548                                         if .currentLesson$ = .lessonName$['.lessonSelected']
549                                                 Set string value: .i, "Show", "+"
550                                         elsif .allShown = 1
551                                                 Set string value: .i, "Show", "-"
552                                         endif
553                                 endfor
554                                 for .i to te.numberOfWords
555                                         # Keep track of whether all are shown
556                                         .shown$ = Get value: .i, "Show"
557                                         if .firstShown <=0 and .shown$ <> "-"
558                                                 .firstShown = .i
559                                         endif
560                                 endfor
561                         else
562                                 for .i from 0 to .max
563                                         .tmp$ = .showVariable$['.i']
564                                         .showValue['.i'] = '.tmp$'
565                                         .showWord$['.i'] = "-"
566                                         if .showValue['.i'] <> 0
567                                                 .showWord$['.i'] = "+"
568                                         endif
569                                         Set string value: .currentWord+.i, "Show", .showWord$['.i']
570                                 endfor
571                         endif
572                         .tmp$ = replace_regex$(.toneText$, "^(.)", "\l\1", 0)
573                         .toneSelected = '.tmp$' - 2
574                         if .toneSelected >= 0
575                                 select Table 'wordlist$'
576                                 for .i to te.numberOfWords
577                                         .currentPinyin$ = Get value: .i, "Pinyin"
578                                         .tmp$ = Get value: .i, "Show"
579                                         if .tmp$ <> "-" and index(.currentPinyin$, "'.toneSelected'") > 0
580                                                 Set string value: .i, "Show", "+"
581                                         else
582                                                 Set string value: .i, "Show", "-"
583                                                 .allShown = 0
584                                         endif
585                                 endfor
586                         endif
587                         
588                         if clicked = 4
589                                 if .firstShown > 0
590                                         .currentWord = (.firstShown div .numWordsPerScreen) * .numWordsPerScreen + 1
591                                 elsif .toneSelected < 0
592                                         .currentWord -= .numWordsPerScreen
593                                 endif
594                                 if .currentWord <= 0
595                                         .currentWord = (te.numberOfWords div .numWordsPerScreen) * .numWordsPerScreen + 1
596                                 endif
597                         elsif clicked = 5
598                                 if .firstShown > 0
599                                         .currentWord = (.firstShown div .numWordsPerScreen) * .numWordsPerScreen + 1
600                                 elsif .toneSelected < 0
601                                         .currentWord += .numWordsPerScreen
602                                 endif
603                                 if .currentWord > te.numberOfWords
604                                         .currentWord = 1
605                                 endif
606                         endif
607                 endif
608                 # Reset and go to the first selected word (can shuffle list)
609                 if clicked = 6
610                         te.currentWord = -1
611                         call next_word
612                 endif
613         endwhile
614         
615     # Implement Cancel
616     if clicked = 1
617                 select Table 'wordlist$'
618                 Remove
619                 select .tmpOriginalWordlist
620                 Copy: wordlist$
621         endif
622         select .tmpOriginalWordlist
623         Remove
624     call Draw_button '.table$' '.label$' 0
625 endproc
627 procedure processConfigPerfSummary .clickX .clickY .pressed$
628         .table$ = "Config"
629         .label$ = "PerfSummary"
630         
631         call testLoadTable SummaryToneEvaluation
632         if testLoadTable.table > 0
633                 call Draw_button '.table$' '.label$' 1
634                 call loadTable SummaryToneEvaluation
635                 call write_tabbed_table SummaryToneEvaluation Evaluation_'config.language$'
636                 demoWaitForInput()
637                 select Table SummaryToneEvaluation
638                 Remove
640                 call Draw_button '.table$' '.label$' 0
641                 call Draw_config_page
642         endif
643 endproc
645 # Wipe current performance table and initialize a new one
646 procedure processConfigClearSummary .clickX .clickY .pressed$
647         .table$ = "Config"
648         .label$ = "ClearSummary"
649         
650     call Draw_button '.table$' '.label$' 1
651         
652         
653         config.savePerf$ = ""
654         call initialize_toneevaluation_tables
655         
656     call Draw_button '.table$' '.label$' 0
657         call Draw_config_page
658         
659 endproc
661 procedure processConfigListPerf .clickX .clickY .pressed$
662         .table$ = "Config"
663         .label$ = "ListPerf"
664     call Draw_button '.table$' '.label$' 1
665         
666         select Table 'initialiseSGC2.toneevaluation_table$'
667         View & Edit
668         demoWaitForInput()
669         
670     call Draw_button '.table$' '.label$' 0
671         call Draw_config_page
672 endproc
674 procedure processConfigSavePerf .clickX .clickY .pressed$
675         .table$ = "Config"
676         .label$ = "SavePerf"
677     call Draw_button '.table$' '.label$' 1
678         
679         # Get help text
680         call findLabel '.table$' '.label$'
681         .row = findLabel.row
682         select Table '.table$'
683         .openDialogue$ = Get value... '.row' Helptext
684         call convert_praat_to_latin1 '.openDialogue$'
685         .openDialogue$ = convert_praat_to_latin1.text$
686         .defaultName$= "Performance.tsv"
687         if config.savePerf$ <> ""
688                 .defaultName$= config.savePerf$
689         endif
690         .performance_Name$ = chooseWriteFile$ (.openDialogue$, .defaultName$)
691         if .performance_Name$ <> ""
692                 select Table 'initialiseSGC2.toneevaluation_table$'
693                 Write to table file... '.performance_Name$'
694                 config.savePerf$ = .performance_Name$
695         endif
696         
697     call Draw_button '.table$' '.label$' 0
698         call Draw_config_page
699 endproc
701 procedure processConfigManual .clickX .clickY .pressed$
702         .table$ = "Config"
703         .label$ = "Manual"
704         call Draw_button '.table$' '.label$' 1
705         if fileReadable("ManPages/SpeakGoodChinese_2.man")
706                 Read from file... ManPages/SpeakGoodChinese_2.man
707         else
708                 Go to manual page... SpeakGoodChinese 2
709         endif
710         # Wait until the manual is put to the background
711         demoWaitForInput()
712     call Draw_button '.table$' '.label$' 0
713     demo Erase all
714     call Draw_config_page
715 endproc
717 ###############################################################
719 # Obligatory button Processing Routines
721 # These MUST be defined
723 ###############################################################
725 procedure processConfigReturn .clickX .clickY .pressed$
726         .table$ = "Config"
727         .label$ = "Return"
728         call Draw_button '.table$' '.label$' 1
729         call write_preferences ""
730 endproc
732 procedure processConfigRefresh .clickX .clickY .pressed$
733         .table$ = "Config"
734         .label$ = "Refresh"
735         call Draw_config_page
736 endproc
738 procedure processConfigCredits .clickX .clickY .pressed$
739         .table$ = "Config"
740         .label$ = "Credits"
741         call Draw_button '.table$' '.label$' 1
742         call write_text_table Credits_'config.language$'
743         demoWaitForInput()
744     call Draw_button '.table$' '.label$' 0
745     demo Erase all
746     call Draw_config_page
747 endproc
749 procedure processConfigHelp .clickX .clickY .pressed$
750         .table$ = "Config"
751         .label$ = "Help"
752         call help_loop  '.table$' Draw_config_page
753 endproc
755 ###############################################################
757 # Miscelaneous supporting code
759 ###############################################################
760 procedure install_wordlists_by_name .wordlist_Name$
761         if .wordlist_Name$ <> ""
762                 if index(.wordlist_Name$, "wordlist.")
763                         .wordlist_Name$ = replace_regex$(.wordlist_Name$, "[/\\]wordlist\.([^/\\]+)$", "", 0)
764                 elsif index_regex(.wordlist_Name$, "(?i\.(wav|flac|iaf[fc]|mp3))")
765                         .wordlist_Name$ = replace_regex$(.wordlist_Name$, "[/\\][^/\\]+$", "", 0)
766                 endif
767                 if index_regex(.wordlist_Name$, "[/\\]")
768                         .sourceDir$ = left$(.wordlist_Name$, rindex_regex(.wordlist_Name$, "[/\\]") -1)
769                         .file$ = right$(.wordlist_Name$, length(.wordlist_Name$) - rindex_regex(.wordlist_Name$, "[/\\]"))
770                         call readWordlist "'.sourceDir$'" '.file$'
771                 else
772                         .start = 1
773                         if index(.wordlist_Name$, ".")
774                                 .start = 4
775                         endif
776                         .extension1$ = ".sgc"
777                         .extension2$ = ".Table"
778                         .extension3$ = ".txt"
779                         .extension4$ = ".tsv"
780                         .extension5$ = ""
781                         for .e from .start to 5
782                                 .currentExtension$ = .extension'.e'$
783                         call readWordlist "'homeDirectory$'" '.wordlist_Name$''.currentExtension$'
784                         call readWordlist "'homeDirectory$'/Downloads" '.wordlist_Name$''.currentExtension$'
785                         call readWordlist "'homeDirectory$'/Documents" '.wordlist_Name$''.currentExtension$'
786                         call readWordlist "'homeDirectory$'/My Documents/Downloads" '.wordlist_Name$''.currentExtension$'
787                         call readWordlist "'homeDirectory$'/My Documents" '.wordlist_Name$''.currentExtension$'
788                         call readWordlist "'homeDirectory$'/Desktop" '.wordlist_Name$''.currentExtension$'
789                         call readWordlist "'preferencesAppDir$'" '.wordlist_Name$''.currentExtension$'
790                         endfor
791                 endif
792         endif
793 endproc
795 # Word lists: It is a VERY good idea to make sure that word-lists
796 # have unique names.
797 procedure load_word_list .localdir$ .relnumber
798         call clean_up_sound
799     
800     # Remove old word list
801     if wordlist$ <> ""
802                 select Table 'wordlist$'
803                 Remove
804                 call wipeArea 'wipeWordlistArea$'
805                 wordlist$ = ""
806     endif
807     
808         # Create Table that will recieve the wordlists and directories
809         Create Table with column names... AllWordLists 0 Name Directory
810         .te.currentWordlistRow = 0
811         
812         # First the global word lists
813         if fileReadable(globalwordlists$) or fileReadable("'globalwordlists$'/directory.txt")
814         Create Strings as directory list... WordList 'globalwordlists$'
815         .numLists = Get number of strings
816         for .i to .numLists
817             select Strings WordList
818             .currentName$ = Get string... '.i'
819                         if .currentName$ <> "directory.txt"
820                                 select Table AllWordLists
821                                 .listExist = Search column: "Name", .currentName$
822                                 if not .listExist
823                                         Append row
824                                         .te.currentWordlistRow = Get number of rows
825                                         Set string value... '.te.currentWordlistRow' Name '.currentName$'
826                                 .currentDirectory$ = globalwordlists$+"/"+.currentName$
827                                         Set string value... '.te.currentWordlistRow' Directory '.currentDirectory$'
828                                 endif
829                         endif
830         endfor
831         select Strings WordList
832         Remove
833         endif
834         
835         # Now the preferences word lists
836         if fileReadable(sgc2wordlists$) or fileReadable("'sgc2wordlists$'/directory.txt")
837         Create Strings as directory list... WordList 'sgc2wordlists$'
838         .numLists = Get number of strings
839         for .i to .numLists
840                         select Strings WordList
841             .currentName$ = Get string... '.i'
842                         if .currentName$ <> "directory.txt"
843                                 select Table AllWordLists
844                                 .listExist = Search column: "Name", .currentName$
845                                 if not .listExist
846                                         Append row
847                                         .te.currentWordlistRow = Get number of rows
848                                         Set string value... '.te.currentWordlistRow' Name '.currentName$'
849                                 .currentDirectory$ = sgc2wordlists$+"/"+.currentName$
850                                         Set string value... '.te.currentWordlistRow' Directory '.currentDirectory$'
851                                 endif
852                         endif
853         endfor
854         select Strings WordList
855         Remove
856         endif
858         # End with the word lists in the distribution
859         call CreateCreateWordlists
860         select Table CreateWordlists
861     .numLists = Get number of rows
862         for .i to .numLists
863                 select Table CreateWordlists
864                 .currentName$ = Get value... '.i' Name
865                 if .currentName$ <> "directory.txt"
866                         select Table AllWordLists
867                         .listExist = Search column: "Name", .currentName$
868                         if not .listExist
869                                 .procedureName$ = replace_regex$(.currentName$, "[^\w\-\.]", "_", 0)
870                                 select Table AllWordLists
871                                 Append row
872                                 .te.currentWordlistRow = Get number of rows
873                                 Set string value... '.te.currentWordlistRow' Name '.currentName$'
874                                 .currentDirectory$ = "*call Create'.procedureName$'"
875                                 Set string value... '.te.currentWordlistRow' Directory '.currentDirectory$'
876                         endif
877                 endif
878         endfor
879         select Table CreateWordlists
880         Remove
881         
882         # Finally, the local word lists
883         if fileReadable(.localdir$) or fileReadable("'.localdir$'/directory.txt")
884         Create Strings as directory list... WordList '.localdir$'
885         .numLists = Get number of strings
886         for .i to .numLists
887             select Strings WordList
888             .currentName$ = Get string... '.i'
889                         if .currentName$ <> "directory.txt"
890                                 select Table AllWordLists
891                                 .listExist = Search column: "Name", .currentName$
892                                 if not .listExist
893                                         Append row
894                                         .te.currentWordlistRow = Get number of rows
895                                         Set string value... '.te.currentWordlistRow' Name '.currentName$'
896                                 .currentDirectory$ = .localdir$+"/"+.currentName$
897                                         Set string value... '.te.currentWordlistRow' Directory '.currentDirectory$'
898                                 endif
899                         endif
900         endfor
901         select Strings WordList
902         Remove
903     endif
905         # Get the position of the current word list
906         select Table AllWordLists
907         .currentNumber = 1
908     .numLists = Get number of rows
910         if wordlistName$ <> ""
911                 select Table AllWordLists
912                 .currentNumber = Search column... Name 'wordlistName$'
913         endif
915     wordlistNum = .currentNumber + .relnumber
916     if wordlistNum > .numLists
917         wordlistNum = 1
918         elsif wordlistNum < 1 and .numLists > 0
919                 wordlistNum = .numLists
920     endif
921     select Table AllWordLists
922     wordlistName$ = Get value... 'wordlistNum' Name
923         .dirWordlistName$ = Get value... 'wordlistNum' Directory
924     .dirString$ = replace_regex$(.dirWordlistName$, "[ ]", "&", 0)
925         
926         # Read in full tables
927         if fileReadable("'.dirString$'/wordlist.Table")
928                 call readTable '.dirString$'/wordlist.Table
929         if readTable.tableID > 0
930                         Rename... 'wordlistName$'
931                         # Praat wil change the name if it feels like it
932                         wordlist$ = selected$("Table")
933                         # Add a Sound column if it is not present
934                         .soundIndex = Get column index: "Sound"
935                         if .soundIndex <= 0
936                                 Append column: "Sound"
937                         endif
938                 else
939                         .numLists = 0
940                         goto EMERGENCYEXIT
941                 endif
942         # Handle (legacy) simple word lists
943         elsif fileReadable("'.dirString$'/wordlist.txt")
944                 Create Table with column names... "'wordlistName$'" 0 Pinyin Character Sound Translation
945         wordlist$ = selected$("Table")
946                 Read Strings from raw text file... '.dirString$'/wordlist.txt
947                 Rename... RawWordList
948                 .numWordStrings = Get number of strings
949                 for .i to .numWordStrings
950                         select Strings RawWordList
951                         .currentFile$ = "-"
952                         .currentChar$ = "-"
953                         .currentTrans$ = "-"
954                         .currentLine$ = Get string... '.i'
955                         # Remove leading whitespace
956                         .currentLine$ = replace_regex$(.currentLine$, "^[ \t]+", "", 0)
957                         .separatorIndex = index_regex(.currentLine$, "[ \t;\-]")
958                         if .separatorIndex <= 0
959                                 .separatorIndex = length(.currentLine$) + 1
960                         endif
961                         .currentPinyin$ = left$(.currentLine$, .separatorIndex-1)
962                         .currentLine$ = right$(.currentLine$, length(.currentLine$) - .separatorIndex)
963                         # There is more on the line, but we do not know what
964                         if length(.currentLine$) > 0
965                                 while length(.currentLine$) > 0
966                                         .separatorIndex = index_regex(.currentLine$, "[\t;]")
967                                         if .separatorIndex <= 0
968                                                 .separatorIndex = length(.currentLine$)+1
969                                         endif
970                                         .currentItem$ = left$(.currentLine$, .separatorIndex-1)
971                                         .currentLine$ = right$(.currentLine$, length(.currentLine$) - .separatorIndex )
972                                         if index_regex(.currentItem$, ".(spx|flac|wav|mp3)")
973                                                 # Audio
974                                                 .currentFile$ = .currentItem$
975                                         elsif index_regex(.currentItem$, "[a-zA-Z0-9]") > 0
976                                                 # Translation
977                                                 .currentTrans$ = .currentItem$
978                                         elsif index_regex(.currentItem$, "[^ \t\r\l]") > 0 && index_regex(.currentItem$, "[a-zA-Z0-9\-]") <= 0
979                                                 # Characters
980                                                 .currentChar$ = .currentItem$
981                                         endif
982                                 endwhile
983                         endif
984                         if .currentFile$ = "-"
985                                 if fileReadable("'.dirString$'/'.currentPinyin$'.spx")
986                                         .currentFile$ = .currentPinyin$+".spx"
987                                 elsif fileReadable("'.dirString$'/'.currentPinyin$'.flac")
988                                         .currentFile$ = .currentPinyin$+".flac"
989                                 elsif fileReadable("'.dirString$'/'.currentPinyin$'.wav")
990                                         .currentFile$ = .currentPinyin$+".wav"
991                                 elsif fileReadable("'.dirString$'/'.currentPinyin$'.mp3")
992                                         .currentFile$ = .currentPinyin$+".mp3"
993                                 endif
994                         endif
995                         select Table 'wordlist$'
996                         Append row
997                         Set string value... '.i' Pinyin '.currentPinyin$'
998                         Set string value... '.i' Sound '.currentFile$'
999                         Set string value... '.i' Character '.currentChar$'
1000                         Set string value... '.i' Translation '.currentTrans$'
1001                 endfor
1002                 select Strings RawWordList
1003                 Remove
1004                 
1005                 select Table 'wordlist$'
1006         elsif fileReadable(.dirString$) or fileReadable("'.dirString$'/directory.txt")
1007                 Create Table with column names... "'wordlistName$'" 0 Pinyin Sound
1008         wordlist$ = selected$("Table")
1009                 Create Strings as file list... RawWordList '.dirString$'/*
1010                 .numWordStrings = Get number of strings
1011                 .i = 0
1012                 for .j to .numWordStrings
1013                         select Strings RawWordList
1014                         .currentLine$ = Get string... '.j'
1015                         .currentFile$ = extractWord$(.currentLine$, "")
1016                         if index_regex(.currentFile$, "\.(spx|flac|wav|mp3)")
1017                                 .currentPinyin$ = left$(.currentFile$, index(.currentFile$, ".")-1)
1018                                 select Table 'wordlist$'
1019                                 Append row
1020                                 .i += 1
1021                                 Set string value... '.i' Pinyin '.currentPinyin$'
1022                                 Set string value... '.i' Sound '.currentFile$'
1023                         endif
1024                 endfor
1025                 select Strings RawWordList
1026                 Remove
1027                 
1028                 select Table 'wordlist$'
1029         elsif startsWith(.dirString$, "*call ")
1030                 .callProcedure$ = right$(.dirString$, length(.dirString$)-1)
1031                 '.callProcedure$'
1032                 wordlist$ = selected$("Table")
1033         else
1034                 # Nothing, get out
1035                 .numLists = 0
1036                 goto EMERGENCYEXIT              
1037         endif
1039         # Can this wordlist be deleted?
1040         if fileReadable("'sgc2wordlists$'/'wordlistName$'") or fileReadable("'sgc2wordlists$'/'wordlistName$'/wordlist.txt") or fileReadable("'sgc2wordlists$'/'wordlistName$'/wordlist.Table")
1041                 config.deleteWordlist = 0
1042         else
1043                 config.deleteWordlist = -1              
1044         endif
1046         # Check first column name and add Character, Translation and Show columns if missing
1047         if wordlist$ <> ""
1048                 select Table 'wordlist$'
1049                 # HAck to correct odd behavior of tables with unicode characters
1050                 .firstColumn$ = Get column label: 1
1051                 if index_regex(.firstColumn$, "^[^!-~]") > 0
1052                         .firstColumn$ = replace_regex$(.firstColumn$, "^[^!-~]", "", 0)
1053                         Set column label (index): 1, .firstColumn$
1054                 endif
1055                 
1056         te.numberOfWords = Get number of rows
1057                 .characterColumn = Get column index... Character
1058                 if not .characterColumn
1059                         Append column... Character
1060                         for .i to te.numberOfWords
1061                                 Set string value... '.i' Character -
1062                         endfor
1063                 endif
1064                 .translationColumn = Get column index... Translation
1065                 if not .translationColumn
1066                         Append column... Translation
1067                         for .i to te.numberOfWords
1068                                 Set string value... '.i' Translation -
1069                         endfor
1070                 endif
1071                 .showColumn = Get column index... Show
1072                 if not .showColumn
1073                         Append column... Show
1074                         for .i to te.numberOfWords
1075                                 Set string value... '.i' Show +
1076                         endfor
1077                 endif
1078         endif
1079         
1080         # Remove all rows without Pinyin
1081         .numRows = Get number of rows
1082         for i to .numRows
1083                 .rowNum = .numRows - i + 1
1084                 .pinyinValue$ = Get value... '.rowNum' Pinyin
1085                 if not index_regex(.pinyinValue$, "[a-zA-Z0-9]")
1086                         Remove row... '.rowNum'
1087                 endif
1088         endfor
1089         te.numberOfWords = Get number of rows
1090         
1091         # Shuffle words if requested
1092     if config.shuffleLists
1093         Randomize rows
1094     endif
1096         # Clean up
1097         label EMERGENCYEXIT
1098     select Table AllWordLists
1099     Remove
1100         
1101     # There were no Word Lists
1102     label NOWORDLISTS
1103     if .numLists <= 0
1104         wordlistName$ = wordlistName$+" No Word Lists available"
1105         wordlistNum = 1
1106                 Create Table with column names... "'wordlistName$'" 0 Pinyin Character Translation Sound
1107         wordlist$ = selected$("Table")
1108         .i = 0
1109                 Append row
1110                 .i += 1
1111                 Set string value... '.i' Pinyin ni3hao3
1112                 Set string value... '.i' Character 你好
1113                 Set string value... '.i' Translation hello
1114                 Set string value... '.i' Sound -
1115                 Append row
1116                 .i += 1
1117                 Set string value... '.i' Pinyin xie4xie0
1118                 Set string value... '.i' Character 谢谢
1119                 Set string value... '.i' Translation thanks
1120                 Set string value... '.i' Sound -
1121                 Append row
1122                 .i += 1
1123                 Set string value... '.i' Pinyin zai4jian4
1124                 Set string value... '.i' Character 再见
1125                 Set string value... '.i' Translation goodbye
1126                 Set string value... '.i' Sound -
1127         te.numberOfWords = Get number of rows
1128     endif
1130         call set_window_title 'buttons$' 'wordlistName$'
1131 endproc
1133 procedure next_word
1134         if wordlist$ <> ""
1135                 select Table 'wordlist$'
1136                 .showCurrent$ = "-"
1137                 if te.currentWord < 0 or te.currentWord > te.numberOfWords
1138                 if config.shuffleLists
1139                             Randomize rows
1140                 endif
1141                         te.currentWord = 0
1142                 endif
1143                 while .showCurrent$ = "-" and te.currentWord <= te.numberOfWords
1144                         te.currentWord += 1
1145                         if te.currentWord <= te.numberOfWords
1146                                 .showCurrent$ = Get value... 'te.currentWord' Show
1147                         endif
1148                 endwhile
1149         endif
1150 endproc
1152 procedure previous_word
1153         if wordlist$ <> ""
1154                 select Table 'wordlist$'
1155                 .showCurrent$ = "-"
1156                 if te.currentWord <= 0
1157                 if config.shuffleLists
1158                             Randomize rows
1159                 endif
1160                         te.currentWord = te.numberOfWords + 1
1161                 endif
1162                 while .showCurrent$ = "-" and te.currentWord > 0
1163                         te.currentWord -= 1
1164                         if te.currentWord > 0
1165                                 .showCurrent$ = Get value... 'te.currentWord' Show
1166                         endif
1167                 endwhile
1168         endif
1169 endproc
1171 procedure display_word_list_name
1172     .xtext = 50
1173     .ytext = 12
1174    call reset_viewport
1175     .displayWordList$ = replace_regex$(wordlistName$, "[_]", " ", 0)
1176     call wipeArea 'wipeWordlistArea$'
1177         call adjustFontSizeOnHeight 'defaultFont$' 'defaultFontSize' 5
1178         .currentFontSize = adjustFontSizeOnHeight.newFontSize
1180     demo Blue
1181         demo Text special... '.xtext' Centre '.ytext' Bottom 'defaultFont$' '.currentFontSize' 0 '.displayWordList$'
1182     demo Black
1183         demoShow()
1184         call set_font_size 'defaultFontSize'
1185 endproc
1187 procedure write_word_list
1188         # Write current Pinyin text
1189         call display_text Black
1190         
1191         # Write the current word list name
1192         call display_word_list_name
1193 endproc
1195 procedure start_logging
1196         if fileReadable("'preferencesLogDir$'/logPerformance.txt")
1197                 .logDirectory$ < 'preferencesLogDir$'/logPerformance.txt
1198                 .logDirectory$ = extractWord$(.logDirectory$, "")
1199                 if .logDirectory$ = "" or not (fileReadable(.logDirectory$) or  fileReadable("'.logDirectory$'/directory.txt")
1200                         .logDirectory$ = "'preferencesLogDir$'"
1201                 endif
1202                 currentLogDirectory$ = "'.logDirectory$'/log'logtimeStamp$'"
1203                 createDirectory(currentLogDirectory$)
1204                 if not fileReadable("'currentLogDirectory$'/wordlist.Table")
1205                         .headerLine$ = "Pinyin'tab$'Character'tab$'Sound'newline$'"
1206                         .headerLine$ > 'currentLogDirectory$'/wordlist.Table
1207                 endif
1208                         # Flip switch
1209                         config.logPerformance = 1
1210         endif
1211 endproc
1213 procedure log_command .logtext$
1214         if logging
1215                 fileappend "'currentLogDirectory$'/logFile.txt" '.logtext$''newline$'
1216         endif
1217 endproc
1219 procedure log_performance .recordedSound$ .register .proficiency .pinyin$ .choice$
1220         # Log files
1221         .currentDate$ = date$()
1222         .timeStamp$ = replace_regex$(.currentDate$, "[^a-zA-Z0-9\-_]", "-", 0)
1223         .choiceText$ = replace_regex$(.choice$, "[^a-zA-Z0-9\-_]", "-", 0)
1225         if config.logPerformance and fileReadable("'preferencesLogDir$'/logPerformance.txt")
1226            .outfilename$ = .pinyin$+"_"+.choice$+"_'.register'_"+.timeStamp$+".wav"
1227            .logtext$ = "# #+.pinyin$+tab$+.choice$+tab$+"'.register'Hz"+tab$+.currentDate$+tab$+.outfilename$+newline$
1228        # Plain log text
1229            fileappend 'currentLogDirectory$'/logFile.txt '.logtext$'
1230            
1231        # A wordlist.Table
1232        fileappend 'currentLogDirectory$'/wordlist.Table '.pinyin$''tab$''.choice$''tab$''.outfilename$''newline$'
1233        # The recorded sound
1234            select Sound 'recordedSound$'
1235            Write to WAV file... 'currentLogDirectory$'/'.outfilename$'
1236         endif
1237 endproc
1239 procedure paint_logging_light
1240     select Table Config
1241     .row = Search column... Label !Logging
1242         if .row < 1
1243                 exit Button Table Config does not have a row with label !Logging
1244         endif
1245         # Get button values
1246     .leftX = Get value... '.row' LeftX
1247     .rightX = Get value... '.row' RightX
1248     .lowY = Get value... '.row' LowY
1249     .highY = Get value... '.row' HighY
1250     .buttonColor$ = Get value... '.row' Color
1251     .centerX = (.leftX + .rightX)/2
1252     .centerY = (.lowY + .highY)/2
1253     .radius = (.highY - .lowY )/(4*2)
1254     .wipeRadius = 1.1*.radius
1255     if config.logPerformance and fileReadable("'preferencesLogDir$'/logPerformance.txt")
1256         demo Paint circle... White '.centerX' '.centerY' '.wipeRadius'
1257         demo Paint circle... '.buttonColor$' '.centerX' '.centerY' '.radius'
1258         demoShow()
1259     else
1260         demo Paint circle... White '.centerX' '.centerY' '.wipeRadius'
1261         demoShow()
1262     endif
1263 endproc
1265 # Uninstall word lists
1266 procedure removeWordlist .deletedWordlistName$
1267     .targetDir$ = ""
1268     if fileReadable("'sgc2wordlists$'/'.deletedWordlistName$'") or fileReadable("'sgc2wordlists$'/'.deletedWordlistName$'/wordlist.txt") or fileReadable("'sgc2wordlists$'/'.deletedWordlistName$'/wordlist.Table")
1269         .targetDir$ = "'sgc2wordlists$'/'.deletedWordlistName$'"
1270     endif
1271         if .targetDir$ <> ""
1272         Create Strings as file list... DeleteList '.targetDir$'
1273         .numdeleteFiles = Get number of strings
1274         for .i to .numdeleteFiles
1275                 .file$ = Get string... '.i'
1276                 deleteFile("'.targetDir$'/'.file$'")
1277         endfor
1278         filedelete '.targetDir$'
1279         # Check whether the directory was actually deleted. Make sure this is a valid directory name!
1280         # That is, it does not contain funny characters, nor funny names
1281         if index_regex(.deletedWordlistName$, "[^a-zA-Z0-9_ .\-]") <= 0 and index(.deletedWordlistName$, "..") <= 0 and index_regex(.deletedWordlistName$, "[a-zA-Z]") > 0
1282                         if windows
1283                                 nocheck system rmdir "'.targetDir$'" /s /q
1284                         elsif fileReadable(.targetDir$)
1285                                 system bash -rc -- 'rm -r -- "'.targetDir$'"'
1286                         endif
1287                 endif
1288                 # Remove deleted word list
1289                 select Strings DeleteList
1290                 plus Table 'wordlist$'
1291                 Remove
1292                 wordlist$ = ""
1293         endif
1294 endproc
1296 # Install word lists
1297 procedure sgc2wordlist .sourceDir$
1298         if startsWith(.sourceDir$, "preferencesDirectory$")
1299                 .sourceDir$ = replace$(.sourceDir$, "preferencesDirectory$", preferencesDirectory$)
1300         endif
1302         .targetDirectory$ = "'sgc2wordlists$'"
1303         if  fileReadable(.sourceDir$) or fileReadable("'.sourceDir$'/directory.txt")
1304                 Create Strings as file list... PackageList '.sourceDir$'/*.sgc
1305                 .numFiles = Get number of strings
1306                 for .i to .numFiles
1307                         select Strings PackageList
1308                         .file$ = Get string... '.i'
1309                         call readWordlist '.sourceDir$' '.file$'
1310                 endfor
1312                 select Strings PackageList
1313                 Remove
1314         endif
1315 endproc
1317 # Debuggin remarks!!!
1318 # fileReadable(<directory>) does not work in Windows
1319 # The file paths / -> \\ must be performed on the filenames in Windows before the system_nocheck is given
1320 # And yet only the 7z decompression has been implemented
1321 windowsUnzipCommand$ = ""
1322 if fileReadable("C:\Program Files\7-Zip\7z.exe")
1323         windowsUnzipCommand$ = """C:\Program Files\7-Zip\7z.exe"" e"
1324 elsif fileReadable("C:\Program Files\WinZip\winzip32.exe")
1325         # !!! Find a way to include the output folder !!!
1326         windowsUnzipCommand$ = "C:\Program Files\WinZip\winzip32.exe"" -min -e "
1327 endif
1328 procedure readWordlist .sourceDir$ .file$
1329         # No use doing anything if the source does not exist
1330         if fileReadable("'.sourceDir$'/'.file$'") or fileReadable("'.sourceDir$'/'.file$'/wordlist.txt") or fileReadable("'.sourceDir$'/'.file$'/wordlist.Table")
1331                 # What will be the target wordlist directory?
1332                 .targetDirectory$ = "'sgc2wordlists$'"
1333                 .dirname$ = left$(.file$, rindex(.file$, ".")-1)
1334                 if .dirname$ = ""
1335                         .dirname$ = .file$
1336                 endif
1337                 .wordlistDirectory$ = .targetDirectory$+"/"+.dirname$
1338                 # Wordlist directory does not exist, neither locally nor in the preferences
1339                 .wordListExists = 0
1340                 .tmpDirs = nocheck Create Strings as directory list... TMPWORDLISTS '.targetDirectory$'/
1341                 if .tmpDirs != undefined and .tmpDirs > 0
1342                         .numDirs = Get number of strings
1343                         for .d to .numDirs
1344                                 select .tmpDirs
1345                                 .currentString$ = Get string... '.d'
1346                                 if .currentString$ = .dirname$
1347                                         .wordListExists = 1
1348                                 endif
1349                         endfor
1350                         Remove
1351                 endif
1352                 if not (.wordListExists or fileReadable(.dirname$) or fileReadable("'.dirname$'/directory.txt") or fileReadable(.wordlistDirectory$) or fileReadable("'.wordlistDirectory$'/directory.txt"))
1353                         .wasWordList = 0
1354                         # Move source to destination
1355                         if index(.file$, ".sgc") or index(.file$, ".zip")
1356                                 if index_regex(.wordlistDirectory$, "[^a-zA-Z0-9_\.\- /~\:]") <= 0 and index_regex(.dirname$, "[^a-zA-Z0-9_\.\- ]") <= 0 and not (windows and windowsUnzipCommand$ = "")
1357                                         # Create wordlist directory
1358                                         createDirectory(.wordlistDirectory$)
1359                                         .wasWordList = 1
1360                                         if macintosh or unix
1361                                                 system bash -rc -- 'cp "'.sourceDir$'/'.file$'" "'.wordlistDirectory$'/"'
1362                                                 system cd ''.wordlistDirectory$'';bash -rc -- 'unzip "'.file$'"'
1363                                         elsif windows and windowsUnzipCommand$ <> ""
1364                                                 .winWordListDirectory$ = replace_regex$(.wordlistDirectory$, "/", "\\", 0)
1365                                                 .winSourceDirectory$ = replace_regex$("'.sourceDir$'\'.file$'", "/", "\\", 0)
1366                                                 system copy "'.winSourceDirectory$'" "'.winWordListDirectory$'" /Y
1367                                                 system chdir /d "'.winWordListDirectory$'" && 'windowsUnzipCommand$' "'.file$'"
1368                                         endif
1369                                 endif
1370                                 deleteFile("'.wordlistDirectory$'/'.file$'")
1371                                 # Remove if not valid!
1372                                 if fileReadable("'.wordlistDirectory$'/wordlist.Table") or fileReadable("'.wordlistDirectory$'/wordlist.txt") or fileReadable("'.wordlistDirectory$'/LICENSE.txt")
1373                                         if fileReadable("'.wordlistDirectory$'/wordlist.Table")
1374                                                 call readTable '.sourceDir$'/'.file$'
1375                                                 if readTable.tableID > 0
1376                                                         select readTable.tableID
1377                                                         # Hack around odd behavior of column index
1378                                                         .pinyinCol = 0
1379                                                         .firstColumn$ = Get column label: 1
1380                                                         .firstColumn$ = replace_regex$(.firstColumn$, "^[^!-~]", "", 0)
1381                                                         if .firstColumn$ = "Pinyin"
1382                                                                 .pinyinCol = 1
1383                                                         else
1384                                                                 .pinyinCol = Get column index... Pinyin
1385                                                         endif
1386                                                         Remove
1387                                                         # No Pinyin in table
1388                                                         if .pinyinCol <= 0
1389                                                                 .wasWordList = 0
1390                                                         endif
1391                                                 else
1392                                                         .wasWordList = 0
1393                                                 endif
1394                                         endif
1395                                 else
1396                                         # None of wordlist.Table, wordlist.txt, nor LICENSE.txt
1397                                         .wasWordList = 0
1398                                 endif
1399                                 ### REALLY DANGEROUS STUFF, SHOULD BE HANDLED BETTER
1400                                 if .wasWordList = 0
1401                                         # Remove newly created directory
1402                                         if index_regex(.wordlistDirectory$, "[^a-zA-Z0-9_\.\- /~\:]") <= 0 and index_regex(.dirname$, "[^a-zA-Z0-9_\.\- ]") <= 0 and index(.dirname$, "..") <= 0
1403                                                 if macintosh or unix
1404                                                         system bash -rc -- 'rm -r "'.wordlistDirectory$'/"'
1405                                                 elsif windows
1406                                                         system rmdir /Q /S "'.winWordListDirectory$'/"
1407                                                 endif
1408                                         endif
1409                                 endif
1410                         elsif index_regex(.file$, "\.(Table|txt|tsv)")
1411                                 # Check whether this is a valid table
1412                                 call readTable '.sourceDir$'/'.file$'
1413                                 if readTable.tableID > 0
1414                                         select readTable.tableID
1415                                         # Hack around odd behavior of column index
1416                                         .pinyinCol = 0
1417                                         .firstColumn$ = Get column label: 1
1418                                         .firstColumn$ = replace_regex$(.firstColumn$, "^[^!-~]", "", 0)
1419                                         if .firstColumn$ = "Pinyin"
1420                                                 .pinyinCol = 1
1421                                         else
1422                                                 .pinyinCol = Get column index... Pinyin
1423                                         endif
1424                                         Remove
1425                                         
1426                                         if .pinyinCol > 0
1427                                                 # Create wordlist directory
1428                                                 createDirectory(.wordlistDirectory$)
1429                                                 .wasWordList = 1
1430                                                 .extension$ = replace_regex$(.file$, "^.+\.(Table|txt|tsv)$", "\1", 0)
1431                                                 if .extension$ = "tsv"
1432                                                         .extension$ = "Table"
1433                                                 endif
1434                                                 if macintosh or unix
1435                                                         system bash -rc -- 'cp "'.sourceDir$'/'.file$'" "'.wordlistDirectory$'/wordlist.'.extension$'"'
1436                                                 elsif windows
1437                                                         .winWordListDirectory$ = replace_regex$(.wordlistDirectory$, "/", "\\", 0)
1438                                                         .winSourceDirectory$ = replace_regex$("'.sourceDir$'\'.file$'", "/", "\\", 0)
1439                                                         system copy "'.winSourceDirectory$'" "'.winWordListDirectory$'\wordlist.'.extension$'" /q
1440                                                 endif
1441                                         endif
1442                                 endif
1443                         elsif fileReadable("'.sourceDir$'/'.file$'/wordlist.Table") or fileReadable("'.sourceDir$'/'.file$'/wordlist.txt") or fileReadable("'.sourceDir$'/'.file$'/LICENSE.txt")
1444                                 # Copy wordlist directory
1445                                 if index_regex(.file$, "[^a-zA-Z0-9_\.\- ]") <= 0
1446                                         .wasWordList = 1
1447                                         if macintosh or unix
1448                                                 system bash -rc -- 'cp -r "'.sourceDir$'/'.file$'" "'.wordlistDirectory$'"'
1449                                         elsif windows
1450                                                 createDirectory(.wordlistDirectory$)
1451                                                 .winWordListDirectory$ = replace_regex$(.wordlistDirectory$, "/", "\\", 0)
1452                                                 .winSourceDirectory$ = replace_regex$("'.sourceDir$'\'.file$'", "/", "\\", 0)
1453                                                 system xcopy "'.winSourceDirectory$'" "'.winWordListDirectory$'" /s /e /q
1454                                         endif
1455                                 endif
1456                         endif
1457                         
1458                         # Set current word list to read list
1459                         if .wasWordList
1460                                 wordlistName$ = .dirname$
1461                         else
1462                                 .table$ = "Config"
1463                                 .label$ = "!NotAWordlist"
1465                                 # Get help text
1466                                 call findLabel '.table$' '.label$'
1467                                 .row = findLabel.row
1468                                 select Table '.table$'
1469                                 .helpText$ = Get value... '.row' Helptext
1470                                 .filetext$ = replace_regex$("'.sourceDir$'/'.file$'", "_", "\\_ ", 0)
1471                         call write_text_popup 'defaultFont$' 14 '.helpText$' "'.filetext$'"
1472                                 # Wait for confirmation
1473                                 demoWaitForInput()
1474                         endif
1475                 endif
1476         endif
1477 endproc