Adapted patch to 5.3.83
[sgc2.git] / Config.praat
blob5206e755b3e297d26f3f9bf996d4b59708574924
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 DrawLogging .color$ .x .y .size
77     .size /= 2
78         .y += .size
79     if .color$ = "Blue"
80         .color$ = "{0.5,0.5,1}"
81      else
82         .color$ = "Blue"
83      endif
84     demo Paint circle... '.color$' '.x' '.y' '.size'
85 endproc
87 ###############################################################
89 # Obligatory button Drawing Routines
90
91 # These MUST be defined
93 ###############################################################
95 procedure DrawReturn .color$ .x .y .size
96     call DrawConfig '.color$' '.x' '.y' '.size'
97 endproc
99 # Set the correct button states after redrawing the window
100 procedure setConfigMainPage
101         call testLoadTable SummaryToneEvaluation
102         if testLoadTable.table <= 0
103                 call Draw_button 'config$' PerfSummary 1
104         endif
105     # Handle logging buttons with forced button draw
106     if fileReadable("'preferencesLogDir$'/logPerformance.txt")
107         call Draw_button 'config$' +!Logging 'config.logPerformance'
108     endif
109 endproc
111 ###############################################################
113 # Button Processing Routines
115 ###############################################################
117 procedure processConfigShuffleLists .clickX .clickY .pressed$
118         .table$ = "Config"
119         .label$ = "ShuffleLists"
120         config.shuffleLists = not config.shuffleLists
121         .displayButton = 2*config.shuffleLists
122     call Draw_button '.table$' '.label$' '.displayButton'
123 endproc
125 procedure processConfigUseSoundExample .clickX .clickY .pressed$
126         .table$ = "Config"
127         .label$ = "UseSoundExample"
128         config.useSoundExample = not config.useSoundExample
129         .displayButton = 2*config.useSoundExample
130     call Draw_button '.table$' '.label$' '.displayButton'
131 endproc
132         
133 procedure processConfigSynthesis .tts$ .clickX .clickY .pressed$
134         .table$ = "Config"
135         .label$ = "Synthesis_'.tts$'"
136         if sgc2.synthesizer > 0 or (speakCommandFile$ <> "" and fileReadable(speakCommandFile$))
137                 if config.synthesis$ = ""
138                         config.synthesis$ = .tts$
139                         .displayButton = 2
140                 else
141                         config.synthesis$ = ""
142                         .displayButton = 0
143                 endif
144         else
145                 config.synthesis$ = "_DISABLED_"
146                 .displayButton = -1
147         endif
148     call Draw_button '.table$' '.label$' '.displayButton'
149 endproc
150         
151 procedure processConfigStrict .clickX .clickY .pressed$
152         .table$ = "Config"
153         .label$ = "Strict"
154         config.strict = not config.strict
155         if config.strict > 0
156                 .displayButton = 2
157         else
158                 .displayButton = 0
159         endif
160         # Change TTS for Strict!
161         call set_up_TTS
162     call Draw_button '.table$' '.label$' '.displayButton'
163 endproc
165 procedure processConfigDisplayPinyin .clickX .clickY .pressed$
166         .table$ = "Config"
167         .label$ = "DisplayPinyin"
168         config.displayPinyin = not config.displayPinyin
169         .displayButton = 2*config.displayPinyin
170     call Draw_button '.table$' '.label$' '.displayButton'
171 endproc
172                 
173 procedure processConfigDisplayChar .clickX .clickY .pressed$
174         .table$ = "Config"
175         .label$ = "DisplayChar"
176         config.displayChar = not config.displayChar
177         .displayButton = 2*config.displayChar
178     call Draw_button 'table$' '.label$' '.displayButton'
179 endproc
181 procedure processConfigDisplayTrans .clickX .clickY .pressed$
182         .table$ = "Config"
183         .label$ = "DisplayTrans"
184         config.displayTrans = not config.displayTrans
185         .displayButton = 2*config.displayTrans
186     call Draw_button 'table$' '.label$' '.displayButton'
187 endproc
189 procedure processConfigDisplayNumbers .clickX .clickY .pressed$
190         .table$ = "Config"
191         .label$ = "DisplayNumbers"
192         config.displayNumbers = not config.displayNumbers
193         .displayButton = 2*config.displayNumbers
194     call Draw_button 'table$' '.label$' '.displayButton'
195 endproc
197 procedure processConfigLanguage .language$ .clickX .clickY .pressed$
198         .table$ = "Config"
199         .label$ = "Language_'.language$'"
200         call processLanguageCodes '.table$' '.label$'
201 endproc
202         
203 procedure processConfigShowBackground .clickX .clickY .pressed$
204         .table$ = "Config"
205         .label$ = "ShowBackground"
206         config.showBackground = not config.showBackground
207         .displayButton = 2*config.showBackground
208     call Draw_button 'table$' '.label$' '.displayButton'
209 endproc
211 procedure processConfigInput .input$ .clickX .clickY .pressed$
212         .table$ = "Config"
213         .label$ = "Input_'.input$'"
214     call Draw_button '.table$' Input_'config.input$' 0
215         config.input$ = .input$
216     call Draw_button '.table$' Input_'config.input$' 2
217 endproc
219 procedure processConfigRegister .register .clickX .clickY .pressed$
220         .table$ = "Config"
221         .label$ = "Register_'.register'"
222         call setRegisterFromLabel '.table$' '.label$'
223 endproc
224         
225 procedure setRegisterFromLabel .table$ .label$
226     call Draw_button '.table$' Register_'config.register' 0
227     call Draw_button '.table$' '.label$' 2
228     # Someone might have to use more than 3 chars for the config.register code
229     .numChars = length(.label$) - length("Register_")
230         .registerText$ = right$(.label$, .numChars)
231         config.register = '.registerText$'
232 endproc
234 procedure processConfigDeleteWordlist .clickX .clickY .pressed$
235         .table$ = "Config"
236         .label$ = "DeleteWordlist"
238     # Do not process undeletable word lists, only those stored in the 
239     # preferencesDirectory$ can be deleted
240     if fileReadable("'sgc2wordlists$'/'wordlistName$'") or fileReadable("'sgc2wordlists$'/'wordlistName$'/wordlist.txt") or     fileReadable("'sgc2wordlists$'/'wordlistName$'/wordlist.Table")
241                 call findLabel '.table$' !'.label$'
242                 if findLabel.row > 0
243                 select Table '.table$'
244                 .alertText$ = Get value... 'findLabel.row' Text
245                 .confirmKey$ = Get value... 'findLabel.row' Key
246                 .popupText$ = Get value... 'findLabel.row' Helptext
247         else
248             exit Cannot find delete directive: '.table$' !'.label$'
249         endif
250         call write_text_popup 'defaultFont$' 14 '.popupText$'
251         call Draw_button '.table$' '.label$' 2
252         alertText$ = .alertText$
253         call Draw_button '.table$' '.label$' 3
254         alertText$ = ""
255         
256                 # Wait for confirmation
257                 demoWaitForInput()
258         if demoInput(.confirmKey$)
259                 .deleteWordListDir$ = wordlistName$
260                 call load_word_list "'localWordlistDir$'" 1
261                 call removeWordlist '.deleteWordListDir$'
262                         call load_word_list "'localWordlistDir$'" 0
263         endif
264                 call Draw_button '.table$' '.label$' 0
265                 call Draw_config_page
266     endif
267 endproc
269 wordlistTag$ = "Wordlist name"        
270 procedure processConfigInstallWordlist .clickX .clickY .pressed$
271         .table$ = "Config"
272         .label$ = "InstallWordlist"
273     call Draw_button '.table$' '.label$' 1
274         if windows
275                 # Do not use the automatic sgc list option, ask for a wordlist NAME
276                 # Get help text
277                 call findLabel '.table$' '.label$'
278                 .row = findLabel.row
279                 select Table '.table$'
280                 .openDialogue$ = Get value... '.row' Helptext
281                 call convert_praat_to_latin1 '.openDialogue$'
282                 .openDialogue$ = convert_praat_to_latin1.text$
283                 .wordlistButton$ = replace$(wordlistTag$, " ", "_", 0)
284                 .wordlistButton$ = replace_regex$(.wordlistButton$, "^.", "\l&", 0)
285                 beginPause(.openDialogue$)
286                         sentence (wordlistTag$, "")
287                 clicked = endPause ("Cancel", "Open", 2)
288                 .wordlist_Name$ = ""
289                 if clicked = 2
290                         .wordlist_Name$ = '.wordlistButton$'$
291                 endif
292                 call install_wordlists_by_name '.wordlist_Name$'
293         else
294         call sgc2wordlist 'homeDirectory$'
295         call sgc2wordlist 'homeDirectory$'/Downloads
296         call sgc2wordlist 'homeDirectory$'/Documents
297         call sgc2wordlist 'homeDirectory$'/My Documents
298         call sgc2wordlist 'homeDirectory$'/My Documents/Downloads
299         call sgc2wordlist 'homeDirectory$'/Desktop
300         call sgc2wordlist 'preferencesAppDir$'
301         endif
302         call load_word_list "'localWordlistDir$'" 0
303     call Draw_button '.table$' '.label$' 0
304         call Draw_config_page
305 endproc
307 procedure processConfigOpenWordlist .clickX .clickY .pressed$
308         .table$ = "Config"
309         .label$ = "OpenWordlist"
310     call Draw_button '.table$' '.label$' 1
312         # Get help text
313         call findLabel '.table$' '.label$'
314         .row = findLabel.row
315         select Table '.table$'
316         .openDialogue$ = Get value... '.row' Helptext
317         call convert_praat_to_latin1 '.openDialogue$'
318         .openDialogue$ = convert_praat_to_latin1.text$
320         .wordlist_Name$ = chooseReadFile$ (.openDialogue$)
321         call install_wordlists_by_name '.wordlist_Name$'
322         call load_word_list "'localWordlistDir$'" 0
323     call Draw_button '.table$' '.label$' 0
324         call Draw_config_page
325 endproc
327 procedure processConfigPerfSummary .clickX .clickY .pressed$
328         .table$ = "Config"
329         .label$ = "PerfSummary"
330         
331         call testLoadTable SummaryToneEvaluation
332         if testLoadTable.table > 0
333                 call Draw_button '.table$' '.label$' 1
334                 call loadTable SummaryToneEvaluation
335                 call write_tabbed_table SummaryToneEvaluation Evaluation_'config.language$'
336                 demoWaitForInput()
337                 select Table SummaryToneEvaluation
338                 Remove
340                 call Draw_button '.table$' '.label$' 0
341                 call Draw_config_page
342         endif
343 endproc
345 procedure processConfigListPerf .clickX .clickY .pressed$
346         .table$ = "Config"
347         .label$ = "ListPerf"
348     call Draw_button '.table$' '.label$' 1
349         
350         call write_tabbed_table 'initialiseSGC2.toneevaluation_table$' Evaluation_'config.language$'
351         demoWaitForInput()
352         
353     call Draw_button '.table$' '.label$' 0
354         call Draw_config_page
355 endproc
357 procedure processConfigSavePerf .clickX .clickY .pressed$
358         .table$ = "Config"
359         .label$ = "SavePerf"
360     call Draw_button '.table$' '.label$' 1
361         
362         # Get help text
363         call findLabel '.table$' '.label$'
364         .row = findLabel.row
365         select Table '.table$'
366         .openDialogue$ = Get value... '.row' Helptext
367         call convert_praat_to_latin1 '.openDialogue$'
368         .openDialogue$ = convert_praat_to_latin1.text$
370         .wordlist_Name$ = chooseWriteFile$ (.openDialogue$, "Performance.Table")
371         if .wordlist_Name$ <> ""
372                 select Table 'initialiseSGC2.toneevaluation_table$'
373                 Write to table file... '.wordlist_Name$'
374         endif
375         
376     call Draw_button '.table$' '.label$' 0
377         call Draw_config_page
378 endproc
380 procedure processConfigManual .clickX .clickY .pressed$
381         .table$ = "Config"
382         .label$ = "Manual"
383         call Draw_button '.table$' '.label$' 1
384         if fileReadable("ManPages/Demo_Application.man")
385                 Read from file... ManPages/Demo_Application.man
386         else
387                 Go to manual page... Demo Application
388         endif
389         # Wait until the manual is put to the background
390         demoWaitForInput()
391     call Draw_button '.table$' '.label$' 0
392     demo Erase all
393     call Draw_config_page
394 endproc
396 ###############################################################
398 # Obligatory button Processing Routines
400 # These MUST be defined
402 ###############################################################
404 procedure processConfigReturn .clickX .clickY .pressed$
405         .table$ = "Config"
406         .label$ = "Return"
407         call Draw_button '.table$' '.label$' 1
408         call write_preferences ""
409 endproc
411 procedure processConfigRefresh .clickX .clickY .pressed$
412         .table$ = "Config"
413         .label$ = "Refresh"
414         call Draw_config_page
415 endproc
417 procedure processConfigCredits .clickX .clickY .pressed$
418         .table$ = "Config"
419         .label$ = "Credits"
420         call Draw_button '.table$' '.label$' 1
421         call write_text_table Credits_'config.language$'
422         demoWaitForInput()
423     call Draw_button '.table$' '.label$' 0
424     demo Erase all
425     call Draw_config_page
426 endproc
428 procedure processConfigHelp .clickX .clickY .pressed$
429         .table$ = "Config"
430         .label$ = "Help"
431         call help_loop  '.table$' Draw_config_page
432 endproc
434 ###############################################################
436 # Miscelaneous supporting code
438 ###############################################################
439 procedure install_wordlists_by_name .wordlist_Name$
440         if .wordlist_Name$ <> ""
441                 if index(.wordlist_Name$, "wordlist.")
442                         .wordlist_Name$ = replace_regex$(.wordlist_Name$, "[/\\]wordlist\.([^/\\]+)$", "", 0)
443                 elsif index_regex(.wordlist_Name$, "(?i\.(wav|flac|iaf[fc]|mp3))")
444                         .wordlist_Name$ = replace_regex$(.wordlist_Name$, "[/\\][^/\\]+$", "", 0)
445                 endif
446                 if index_regex(.wordlist_Name$, "[/\\]")
447                         .sourceDir$ = left$(.wordlist_Name$, rindex_regex(.wordlist_Name$, "[/\\]") -1)
448                         .file$ = right$(.wordlist_Name$, length(.wordlist_Name$) - rindex_regex(.wordlist_Name$, "[/\\]"))
449                         call readWordlist "'.sourceDir$'" '.file$'
450                 else
451                         .start = 1
452                         if index(.wordlist_Name$, ".")
453                                 .start = 4
454                         endif
455                         .extension1$ = ".sgc"
456                         .extension2$ = ".Table"
457                         .extension3$ = ".txt"
458                         .extension4$ = ""
459                         for .e from .start to 4
460                                 .currentExtension$ = .extension'.e'$
461                         call readWordlist "'homeDirectory$'" '.wordlist_Name$''.currentExtension$'
462                         call readWordlist "'homeDirectory$'/Downloads" '.wordlist_Name$''.currentExtension$'
463                         call readWordlist "'homeDirectory$'/Documents" '.wordlist_Name$''.currentExtension$'
464                         call readWordlist "'homeDirectory$'/My Documents/Downloads" '.wordlist_Name$''.currentExtension$'
465                         call readWordlist "'homeDirectory$'/My Documents" '.wordlist_Name$''.currentExtension$'
466                         call readWordlist "'homeDirectory$'/Desktop" '.wordlist_Name$''.currentExtension$'
467                         call readWordlist "'preferencesAppDir$'" '.wordlist_Name$''.currentExtension$'
468                         endfor
469                 endif
470         endif
471 endproc
473 # Word lists: It is a VERY good idea to make sure that word-lists
474 # have unique names.
475 procedure load_word_list .localdir$ .relnumber
476         call clean_up_sound
477     
478     # Remove old word list
479     if wordlist$ <> ""
480                 select Table 'wordlist$'
481                 Remove
482                 call wipeArea 'wipeWordlistArea$'
483                 wordlist$ = ""
484     endif
485     
486         # Create Table that will recieve the wordlists and directories
487         Create Table with column names... AllWordLists 0 Name Directory
488         .te.currentWordlistRow = 0
489         
490         # Start with the word lists in the distribution, unless the local directory exists!
491         call CreateCreateWordlists
492         select Table CreateWordlists
493     .numLists = Get number of rows
494         for .i to .numLists
495                 select Table CreateWordlists
496        .currentName$ = Get value... '.i' Name
497            if not (fileReadable("'.localdir$'/'.currentName$'") or fileReadable("'.localdir$'/'.currentName$'/wordlist.Table") or fileReadable("'.localdir$'/'.currentName$'/wordlist.txt"))
498                    .procedureName$ = replace_regex$(.currentName$, "[^\w\-\.]", "_", 0)
499                         select Table AllWordLists
500                         Append row
501                         .te.currentWordlistRow = Get number of rows
502                         Set string value... '.te.currentWordlistRow' Name '.currentName$'
503                     .currentDirectory$ = "*call Create'.procedureName$'"
504                         Set string value... '.te.currentWordlistRow' Directory '.currentDirectory$'
505                 endif
506         endfor
507         select Table CreateWordlists
508         Remove
510         # First the global word lists
511         if fileReadable(globalwordlists$) or fileReadable("'globalwordlists$'/directory.txt")
512         Create Strings as directory list... WordList 'globalwordlists$'
513         .numLists = Get number of strings
514         for .i to .numLists
515             select Strings WordList
516             .currentName$ = Get string... '.i'
517                         if .currentName$ <> "directory.txt"
518                                 select Table AllWordLists
519                                 .listExist = Search column: "Name", .currentName$
520                                 if not .listExist
521                                         Append row
522                                         .te.currentWordlistRow = Get number of rows
523                                         Set string value... '.te.currentWordlistRow' Name '.currentName$'
524                                 .currentDirectory$ = globalwordlists$+"/"+.currentName$
525                                         Set string value... '.te.currentWordlistRow' Directory '.currentDirectory$'
526                                 endif
527                         endif
528         endfor
529         select Strings WordList
530         Remove
531         endif
532         
533         # Now the preferences word lists
534         if fileReadable(sgc2wordlists$) or fileReadable("'sgc2wordlists$'/directory.txt")
535         Create Strings as directory list... WordList 'sgc2wordlists$'
536         .numLists = Get number of strings
537         for .i to .numLists
538             select Strings WordList
539             .currentName$ = Get string... '.i'
540                         if .currentName$ <> "directory.txt"
541                                 select Table AllWordLists
542                                 .listExist = Search column: "Name", .currentName$
543                                 if not .listExist
544                                         Append row
545                                         .te.currentWordlistRow = Get number of rows
546                                         Set string value... '.te.currentWordlistRow' Name '.currentName$'
547                                 .currentDirectory$ = sgc2wordlists$+"/"+.currentName$
548                                         Set string value... '.te.currentWordlistRow' Directory '.currentDirectory$'
549                                 endif
550                         endif
551         endfor
552         select Strings WordList
553         Remove
554         endif
555         
556         # Finally, the local word lists
557         if fileReadable(.localdir$) or fileReadable("'.localdir$'/directory.txt")
558         Create Strings as directory list... WordList '.localdir$'
559         .numLists = Get number of strings
560         for .i to .numLists
561             select Strings WordList
562             .currentName$ = Get string... '.i'
563                         if .currentName$ <> "directory.txt"
564                                 select Table AllWordLists
565                                 .listExist = Search column: "Name", .currentName$
566                                 if not .listExist
567                                         Append row
568                                         .te.currentWordlistRow = Get number of rows
569                                         Set string value... '.te.currentWordlistRow' Name '.currentName$'
570                                 .currentDirectory$ = .localdir$+"/"+.currentName$
571                                         Set string value... '.te.currentWordlistRow' Directory '.currentDirectory$'
572                                 endif
573                         endif
574         endfor
575         select Strings WordList
576         Remove
577     endif
579         # Get the position of the current word list
580         select Table AllWordLists
581         .currentNumber = 1
582     .numLists = Get number of rows
584         if wordlistName$ <> ""
585                 select Table AllWordLists
586                 .currentNumber = Search column... Name 'wordlistName$'
587         endif
589     wordlistNum = .currentNumber + .relnumber
590     if wordlistNum > .numLists
591         wordlistNum = 1
592         elsif wordlistNum < 1 and .numLists > 0
593                 wordlistNum = .numLists
594     endif
595     select Table AllWordLists
596     wordlistName$ = Get value... 'wordlistNum' Name
597         .dirWordlistName$ = Get value... 'wordlistNum' Directory
598     .dirString$ = replace_regex$(.dirWordlistName$, "[ ]", "&", 0)
599         
600         # Read in full tables
601         if fileReadable("'.dirString$'/wordlist.Table")
602         Read from file... '.dirString$'/wordlist.Table
603         Rename... 'wordlistName$'
604         # Praat wil change the name if it feels like it
605         wordlist$ = selected$("Table")
606         # Handle (legacy) simple word lists
607         elsif fileReadable("'.dirString$'/wordlist.txt")
608                 Create Table with column names... "'wordlistName$'" 0 Pinyin Sound
609         wordlist$ = selected$("Table")
610                 Read Strings from raw text file... '.dirString$'/wordlist.txt
611                 Rename... RawWordList
612                 .numWordStrings = Get number of strings
613                 for .i to .numWordStrings
614                         select Strings RawWordList
615                         .currentLine$ = Get string... '.i'
616                         .currentPinyin$ = extractWord$(.currentLine$, "")
617                         if length(.currentLine$) > length(.currentPinyin$)+1
618                                 .currentFile$ = right$(.currentLine$, length(.currentPinyin$)+1)
619                         elsif fileReadable("'.dirString$'/'.currentPinyin$'.spx")
620                                 .currentFile$ = .currentPinyin$+".spx"
621                         elsif fileReadable("'.dirString$'/'.currentPinyin$'.flac")
622                                 .currentFile$ = .currentPinyin$+".flac"
623                         elsif fileReadable("'.dirString$'/'.currentPinyin$'.wav")
624                                 .currentFile$ = .currentPinyin$+".wav"
625                         elsif fileReadable("'.dirString$'/'.currentPinyin$'.mp3")
626                                 .currentFile$ = .currentPinyin$+".mp3"
627                         else
628                                 .currentFile$ = "-"
629                         endif
630                         select Table 'wordlist$'
631                         Append row
632                         Set string value... '.i' Pinyin '.currentPinyin$'
633                         Set string value... '.i' Sound '.currentFile$'
634                 endfor
635                 select Strings RawWordList
636                 Remove
637                 
638                 select Table 'wordlist$'
639         elsif fileReadable(.dirString$) or fileReadable("'.dirString$'/directory.txt")
640                 Create Table with column names... "'wordlistName$'" 0 Pinyin Sound
641         wordlist$ = selected$("Table")
642                 Create Strings as file list... RawWordList '.dirString$'/*
643                 .numWordStrings = Get number of strings
644                 .i = 0
645                 for .j to .numWordStrings
646                         select Strings RawWordList
647                         .currentLine$ = Get string... '.j'
648                         .currentFile$ = extractWord$(.currentLine$, "")
649                         if index_regex(.currentFile$, "\.(spx|flac|wav|mp3)")
650                                 .currentPinyin$ = left$(.currentFile$, index(.currentFile$, ".")-1)
651                                 select Table 'wordlist$'
652                                 Append row
653                                 .i += 1
654                                 Set string value... '.i' Pinyin '.currentPinyin$'
655                                 Set string value... '.i' Sound '.currentFile$'
656                         endif
657                 endfor
658                 select Strings RawWordList
659                 Remove
660                 
661                 select Table 'wordlist$'
662         elsif startsWith(.dirString$, "*call ")
663                 .callProcedure$ = right$(.dirString$, length(.dirString$)-1)
664                 '.callProcedure$'
665                 wordlist$ = selected$("Table")
666         endif
668         # Can this wordlist be deleted?
669         if fileReadable("'sgc2wordlists$'/'wordlistName$'") or fileReadable("'sgc2wordlists$'/'wordlistName$'/wordlist.txt") or fileReadable("'sgc2wordlists$'/'wordlistName$'/wordlist.Table")
670                 config.deleteWordlist = 0
671         else
672                 config.deleteWordlist = -1              
673         endif
675         # Add a Character and Translation column if missing
676         if wordlist$ <> ""
677                 select Table 'wordlist$'
678         te.numberOfWords = Get number of rows
679                 .characterColumn = Get column index... Character
680                 if not .characterColumn
681                         Append column... Character
682                         for .i to te.numberOfWords
683                                 Set string value... '.i' Character -
684                         endfor
685                 endif
686                 .translationColumn = Get column index... Translation
687                 if not .translationColumn
688                         Append column... Translation
689                         for .i to te.numberOfWords
690                                 Set string value... '.i' Translation -
691                         endfor
692                 endif
693         endif
694         
695         # Remove all rows without Pinyin
696         .numRows = Get number of rows
697         for i to .numRows
698                 .rowNum = .numRows - i + 1
699                 .pinyinValue$ = Get value... '.rowNum' Pinyin
700                 if not index_regex(.pinyinValue$, "[a-zA-Z0-9]")
701                         Remove row... '.rowNum'
702                 endif
703         endfor
704         te.numberOfWords = Get number of rows
705         
706         # Shuffle words if requested
707     if config.shuffleLists
708         Randomize rows
709     endif
711         # Clean up
712     select Table AllWordLists
713     Remove
714         
715     # There were no Word Lists
716     label NOWORDLISTS
717     if .numLists <= 0
718         wordlistName$ = "No Word Lists available"
719         wordlistNum = 1
720                 Create Table with column names... "'wordlistName$'" 0 Pinyin Character Translation Sound
721         wordlist$ = selected$("Table")
722         .i = 0
723                 Append row
724                 .i += 1
725                 Set string value... '.i' Pinyin ni3hao3
726                 Set string value... '.i' Character -
727                 Set string value... '.i' Translation -
728                 Set string value... '.i' Sound -
729                 Append row
730                 .i += 1
731                 Set string value... '.i' Pinyin xie4xie0
732                 Set string value... '.i' Character -
733                 Set string value... '.i' Translation -
734                 Set string value... '.i' Sound -
735                 Append row
736                 .i += 1
737                 Set string value... '.i' Pinyin zai4jian4
738                 Set string value... '.i' Character -
739                 Set string value... '.i' Translation -
740                 Set string value... '.i' Sound -
741         te.numberOfWords = Get number of rows
742     endif
744         call set_window_title 'buttons$' 'wordlistName$'
745 endproc
747 procedure display_word_list_name
748     .xtext = 50
749     .ytext = 12
750    call reset_viewport
751     .displayWordList$ = replace_regex$(wordlistName$, "[_]", " ", 0)
752     call wipeArea 'wipeWordlistArea$'
753         call adjustFontSizeOnHeight 'defaultFont$' 'defaultFontSize' 5
754         .currentFontSize = adjustFontSizeOnHeight.newFontSize
756     demo Blue
757         demo Text special... '.xtext' Centre '.ytext' Bottom Helvetica '.currentFontSize' 0 '.displayWordList$'
758     demo Black
759         demoShow()
760         call set_font_size 'defaultFontSize'
761 endproc
763 procedure write_word_list
764         # Write current Pinyin text
765         call display_text Black
766         
767         # Write the current word list name
768         call display_word_list_name
769 endproc
771 procedure start_logging
772         if fileReadable("'preferencesLogDir$'/logPerformance.txt")
773                 .logDirectory$ < 'preferencesLogDir$'/logPerformance.txt
774                 .logDirectory$ = extractWord$(.logDirectory$, "")
775                 if .logDirectory$ = "" or not (fileReadable(.logDirectory$) or  fileReadable("'.logDirectory$'/directory.txt")
776                         .logDirectory$ = "'preferencesLogDir$'"
777                 endif
778                 currentLogDirectory$ = "'.logDirectory$'/log'logtimeStamp$'"
779                 createDirectory(currentLogDirectory$)
780                 if not fileReadable("'currentLogDirectory$'/wordlist.Table")
781                         .headerLine$ = "Pinyin'tab$'Character'tab$'Sound'newline$'"
782                         .headerLine$ > 'currentLogDirectory$'/wordlist.Table
783                 endif
784                         # Flip switch
785                         config.logPerformance = 1
786         endif
787 endproc
789 procedure log_command .logtext$
790         if logging
791                 fileappend "'currentLogDirectory$'/logFile.txt" '.logtext$''newline$'
792         endif
793 endproc
795 procedure log_performance .recordedSound$ .register .proficiency .pinyin$ .choice$
796         # Log files
797         .currentDate$ = date$()
798         .timeStamp$ = replace_regex$(.currentDate$, "[^a-zA-Z0-9\-_]", "-", 0)
799         .choiceText$ = replace_regex$(.choice$, "[^a-zA-Z0-9\-_]", "-", 0)
801         if config.logPerformance and fileReadable("'preferencesLogDir$'/logPerformance.txt")
802            .outfilename$ = .pinyin$+"_"+.choice$+"_'.register'_"+.timeStamp$+".wav"
803            .logtext$ = "# #+.pinyin$+tab$+.choice$+tab$+"'.register'Hz"+tab$+.currentDate$+tab$+.outfilename$+newline$
804        # Plain log text
805            fileappend 'currentLogDirectory$'/logFile.txt '.logtext$'
806            
807        # A wordlist.Table
808        fileappend 'currentLogDirectory$'/wordlist.Table '.pinyin$''tab$''.choice$''tab$''.outfilename$''newline$'
809        # The recorded sound
810            select Sound 'recordedSound$'
811            Write to WAV file... 'currentLogDirectory$'/'.outfilename$'
812         endif
813 endproc
815 procedure paint_logging_light
816     select Table Config
817     .row = Search column... Label !Logging
818         if .row < 1
819                 exit Button Table Config does not have a row with label !Logging
820         endif
821         # Get button values
822     .leftX = Get value... '.row' LeftX
823     .rightX = Get value... '.row' RightX
824     .lowY = Get value... '.row' LowY
825     .highY = Get value... '.row' HighY
826     .buttonColor$ = Get value... '.row' Color
827     .centerX = (.leftX + .rightX)/2
828     .centerY = (.lowY + .highY)/2
829     .radius = (.highY - .lowY )/(4*2)
830     .wipeRadius = 1.1*.radius
831     if config.logPerformance and fileReadable("'preferencesLogDir$'/logPerformance.txt")
832         demo Paint circle... White '.centerX' '.centerY' '.wipeRadius'
833         demo Paint circle... '.buttonColor$' '.centerX' '.centerY' '.radius'
834         demoShow()
835     else
836         demo Paint circle... White '.centerX' '.centerY' '.wipeRadius'
837         demoShow()
838     endif
839 endproc
841 # Uninstall word lists
842 procedure removeWordlist .deletedWordlistName$
843     .targetDir$ = ""
844     if fileReadable("'sgc2wordlists$'/'.deletedWordlistName$'") or fileReadable("'sgc2wordlists$'/'.deletedWordlistName$'/wordlist.txt") or fileReadable("'sgc2wordlists$'/'.deletedWordlistName$'/wordlist.Table")
845         .targetDir$ = "'sgc2wordlists$'/'.deletedWordlistName$'"
846     endif
847         if .targetDir$ <> ""
848         Create Strings as file list... DeleteList '.targetDir$'/*
849         .numdeleteFiles = Get number of strings
850         for .i to .numdeleteFiles
851                 .file$ = Get string... '.i'
852                 deleteFile("'.targetDir$'/'.file$'")
853         endfor
854         filedelete '.targetDir$'
855                 if windows
856                         system rmdir "'.targetDir$'" /s /q
857                 endif
858                 # Remove deleted word list
859                 select Strings DeleteList
860                 plus Table 'wordlist$'
861                 Remove
862                 wordlist$ = ""
863         endif
864 endproc
866 # Install word lists
867 procedure sgc2wordlist .sourceDir$
868         if startsWith(.sourceDir$, "preferencesDirectory$")
869                 .sourceDir$ = replace$(.sourceDir$, "preferencesDirectory$", preferencesDirectory$)
870         endif
872         .targetDirectory$ = "'sgc2wordlists$'"
873         if  fileReadable(.sourceDir$) or fileReadable("'.sourceDir$'/directory.txt")
874                 Create Strings as file list... PackageList '.sourceDir$'/*.sgc
875                 .numFiles = Get number of strings
876                 for .i to .numFiles
877                         select Strings PackageList
878                         .file$ = Get string... '.i'
879                         call readWordlist '.sourceDir$' '.file$'
880                 endfor
882                 select Strings PackageList
883                 Remove
884         endif
885 endproc
887 # Debuggin remarks!!!
888 # fileReadable(<directory>) does not work in Windows
889 # The file paths / -> \\ must be performed on the filenames in Windows before the system_nocheck is given
890 # And yet only the 7z decompression has been implemented
891 windowsUnzipCommand$ = """C:\Program Files\7-Zip\7z"" x"
892 procedure readWordlist .sourceDir$ .file$
893         # No use doing anything if the source does not exist
894         if fileReadable("'.sourceDir$'/'.file$'") or fileReadable("'.sourceDir$'/'.file$'/wordlist.txt") or fileReadable("'.sourceDir$'/'.file$'/wordlist.Table")
895                 # What will be the target wordlist directory?
896                 .targetDirectory$ = "'sgc2wordlists$'"
897                 .dirname$ = left$(.file$, rindex(.file$, ".")-1)
898                 if .dirname$ = ""
899                         .dirname$ = .file$
900                 endif
901                 .wordlistDirectory$ = .targetDirectory$+"/"+.dirname$
902                 # Wordlist directory does not exist, neither locally nor in the preferences
903                 if not (fileReadable(.dirname$) or fileReadable("'.dirname$'/directory.txt") or fileReadable(.wordlistDirectory$) or fileReadable("'.wordlistDirectory$'/directory.txt"))
904                         .wasWordList = 0
905                         # Move source to destination
906                         if index(.file$, ".sgc") or index(.file$, ".zip")
907                                 # Create wordlist directory
908                                 createDirectory(.wordlistDirectory$)
909                                 .wasWordList = 1
910                                 if macintosh or unix
911                                         system bash -rc -- 'cp "'.sourceDir$'/'.file$'" "'.wordlistDirectory$'/"'
912                                         system cd ''.wordlistDirectory$'';bash -rc -- 'unzip "'.file$'"'
913                                 elsif windows
914                                         .winWordListDirectory$ = replace_regex$(.wordlistDirectory$, "/", "\\", 0)
915                                         .winSourceDirectory$ = replace_regex$("'.sourceDir$'\'.file$'", "/", "\\", 0)
916                                         system copy "'.winSourceDirectory$'" "'.winWordListDirectory$'" /q
917                                         system chdir "'.winWordListDirectory$'" && 'windowsUnzipCommand$' "'.file$'"
918                                 endif
919                                 deleteFile("'.wordlistDirectory$'/'.file$'")
920                         elsif index_regex(.file$, "\.(Table|txt)")
921                                 # Create wordlist directory
922                                 createDirectory(.wordlistDirectory$)
923                                 .wasWordList = 1
924                                 .extension$ = replace_regex$(.file$, "^.+\.(Table|txt)$", "\1", 0)
925                                 if macintosh or unix
926                                         system bash -rc -- 'cp "'.sourceDir$'/'.file$'" "'.wordlistDirectory$'/wordlist.'.extension$'"'
927                                 elsif windows
928                                         .winWordListDirectory$ = replace_regex$(.wordlistDirectory$, "/", "\\", 0)
929                                         .winSourceDirectory$ = replace_regex$("'.sourceDir$'\'.file$'", "/", "\\", 0)
930                                         system copy "'.winSourceDirectory$'" "'.winWordListDirectory$'\wordlist.'.extension$'" /q
931                                 endif
932                         elsif fileReadable("'.sourceDir$'/'.file$'/wordlist.Table") or fileReadable("'.sourceDir$'/'.file$'/wordlist.txt") or fileReadable("'.sourceDir$'/'.file$'/LICENSE.txt")
933                                 # Copy wordlist directory
934                                 .wasWordList = 1
935                                 if macintosh or unix
936                                         system bash -rc -- 'cp -r "'.sourceDir$'/'.file$'" "'.wordlistDirectory$'"'
937                                 elsif windows
938                                         createDirectory(.wordlistDirectory$)
939                                         .winWordListDirectory$ = replace_regex$(.wordlistDirectory$, "/", "\\", 0)
940                                         .winSourceDirectory$ = replace_regex$("'.sourceDir$'\'.file$'", "/", "\\", 0)
941                                         system xcopy "'.winSourceDirectory$'" "'.winWordListDirectory$'" /s /e /q
942                                 endif
943                         endif
944                         
945                         # Set current word list to read list
946                         if .wasWordList
947                                 wordlistName$ = .dirname$
948                         else
949                                 .table$ = "Config"
950                                 .label$ = "!NotAWordlist"
952                                 # Get help text
953                                 call findLabel '.table$' '.label$'
954                                 .row = findLabel.row
955                                 select Table '.table$'
956                                 .helpText$ = Get value... '.row' Helptext
957                                 .filetext$ = replace_regex$("'.sourceDir$'/'.file$'", "_", "\\_ ", 0)
958                         call write_text_popup 'defaultFont$' 14 '.helpText$' "'.filetext$'"
959                                 # Wait for confirmation
960                                 demoWaitForInput()
961                         endif
962                 endif
963         endif
964 endproc