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