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