Worked on exporting word lists, added path to example sounds
[sgc2.git] / Config.praat
blob444a9cc671a4716194a7986117cd33c7d0636c39
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 DrawSaveAudio .color$ .x .y .size
90     .size /= 2
91         .y += .size
92     if .color$ = "White"
93         .color$ = "White"
94     elsif .color$ = "Blue"
95         .color$ = "{0.5,0.5,1}"
96     else
97         .color$ = "Blue"
98     endif
99     demo Paint circle... '.color$' '.x' '.y' '.size'
100 endproc
102 ###############################################################
104 # Obligatory button Drawing Routines
106 # These MUST be defined
108 ###############################################################
110 procedure DrawReturn .color$ .x .y .size
111     call DrawConfig '.color$' '.x' '.y' '.size'
112 endproc
114 # Set the correct button states after redrawing the window
115 procedure setConfigMainPage
116     call Draw_button 'config$' +SaveAudio 'sgc.saveAudioOn'
117 endproc
119 ###############################################################
121 # Button Processing Routines
123 ###############################################################
125 procedure processConfigShuffleLists .clickX .clickY .pressed$
126         .table$ = "Config"
127         .label$ = "ShuffleLists"
128         config.shuffleLists = not config.shuffleLists
129         .displayButton = 2*config.shuffleLists
130     call Draw_button '.table$' '.label$' '.displayButton'
131 endproc
133 procedure processConfigAdaptiveLists .clickX .clickY .pressed$
134         .table$ = "Config"
135         .label$ = "AdaptiveLists"
136         config.adaptiveLists = not config.adaptiveLists
137         .displayButton = 2*config.adaptiveLists
138     call Draw_button '.table$' '.label$' '.displayButton'
139 endproc
141 procedure processConfigUseSoundExample .clickX .clickY .pressed$
142         .table$ = "Config"
143         .label$ = "UseSoundExample"
144         config.useSoundExample = not config.useSoundExample
145         .displayButton = 2*config.useSoundExample
146     call Draw_button '.table$' '.label$' '.displayButton'
147 endproc
148         
149 procedure processConfigSynthesis .tts$ .clickX .clickY .pressed$
150         .table$ = "Config"
151         .label$ = "Synthesis_'.tts$'"
152         if sgc2.synthesizer > 0 or (speakCommandFile$ <> "" and fileReadable(speakCommandFile$))
153                 if config.synthesis$ = ""
154                         config.synthesis$ = .tts$
155                         .displayButton = 2
156                 else
157                         config.synthesis$ = ""
158                         .displayButton = 0
159                 endif
160         else
161                 config.synthesis$ = "_DISABLED_"
162                 .displayButton = -1
163         endif
164     call Draw_button '.table$' '.label$' '.displayButton'
165 endproc
166         
167 procedure processConfigStrict .clickX .clickY .pressed$
168         .table$ = "Config"
169         .label$ = "Strict"
170         .tmp = 'config.strict$'
171         .tmp += 1
172         if .tmp > sgc.highestLevel
173                 .tmp = 0
174         endif
175         config.strict$ = "'.tmp'"
176         if .tmp > 0
177                 .displayButton = 2
178         else
179                 .displayButton = 0
180         endif
181         # Change TTS for Strict!
182         call set_up_TTS
183     call Draw_button '.table$' '.label$' '.displayButton'
184 endproc
186 procedure processConfigDisplayPinyin .clickX .clickY .pressed$
187         .table$ = "Config"
188         .label$ = "DisplayPinyin"
189         config.displayPinyin = not config.displayPinyin
190         .displayButton = 2*config.displayPinyin
191     call Draw_button '.table$' '.label$' '.displayButton'
192 endproc
193                 
194 procedure processConfigDisplayChar .clickX .clickY .pressed$
195         .table$ = "Config"
196         .label$ = "DisplayChar"
197         config.displayChar = not config.displayChar
198         .displayButton = 2*config.displayChar
199     call Draw_button 'table$' '.label$' '.displayButton'
200 endproc
202 procedure processConfigDisplayTrans .clickX .clickY .pressed$
203         .table$ = "Config"
204         .label$ = "DisplayTrans"
205         config.displayTrans = not config.displayTrans
206         .displayButton = 2*config.displayTrans
207     call Draw_button 'table$' '.label$' '.displayButton'
208 endproc
210 procedure processConfigDisplayNumbers .clickX .clickY .pressed$
211         .table$ = "Config"
212         .label$ = "DisplayNumbers"
213         config.displayNumbers = not config.displayNumbers
214         .displayButton = 2*config.displayNumbers
215     call Draw_button 'table$' '.label$' '.displayButton'
216 endproc
218 procedure processConfigLanguage .language$ .clickX .clickY .pressed$
219         .table$ = "Config"
220         .label$ = "Language_'.language$'"
221         call processLanguageCodes '.table$' '.label$'
222 endproc
223         
224 procedure processConfigShowBackground .clickX .clickY .pressed$
225         .table$ = "Config"
226         .label$ = "ShowBackground"
227         config.showBackground = not config.showBackground
228         .displayButton = 2*config.showBackground
229     call Draw_button 'table$' '.label$' '.displayButton'
230 endproc
232 procedure processConfigInput .input$ .clickX .clickY .pressed$
233         .table$ = "Config"
234         .label$ = "Input_'.input$'"
235     call Draw_button '.table$' Input_'config.input$' 0
236         config.input$ = .input$
237     call Draw_button '.table$' Input_'config.input$' 2
238 endproc
240 procedure processConfigRegister .register .clickX .clickY .pressed$
241         .table$ = "Config"
242         .label$ = "Register_'.register'"
243         call setRegisterFromLabel '.table$' '.label$'
244 endproc
245         
246 procedure setRegisterFromLabel .table$ .label$
247     call Draw_button '.table$' Register_'config.register' 0
248     call Draw_button '.table$' '.label$' 2
249     # Someone might have to use more than 3 chars for the config.register code
250     .numChars = length(.label$) - length("Register_")
251         .registerText$ = right$(.label$, .numChars)
252         config.register = '.registerText$'
253 endproc
255 procedure processConfigDeleteWordlist .clickX .clickY .pressed$
256         .table$ = "Config"
257         .label$ = "DeleteWordlist"
259     # Do not process undeletable word lists, only those stored in the 
260     # preferencesDirectory$ can be deleted
261     if fileReadable("'sgc2wordlists$'/'wordlistName$'") or fileReadable("'sgc2wordlists$'/'wordlistName$'/wordlist.txt") or     fileReadable("'sgc2wordlists$'/'wordlistName$'/wordlist.Table")
262                 call findLabel '.table$' !'.label$'
263                 if findLabel.row > 0
264                 select Table '.table$'
265                 .alertText$ = Get value... 'findLabel.row' Text
266                 .confirmKey$ = Get value... 'findLabel.row' Key
267                 .popupText$ = Get value... 'findLabel.row' Helptext
268         else
269             exit Cannot find delete directive: '.table$' !'.label$'
270         endif
271         call write_text_popup 'defaultFont$' 14 '.popupText$'
272         call Draw_button '.table$' '.label$' 2
273         alertText$ = .alertText$
274         call Draw_button '.table$' '.label$' 3
275         alertText$ = ""
276         
277                 # Wait for confirmation
278                 demoWaitForInput()
279         if demoInput(.confirmKey$)
280                 .deleteWordListDir$ = wordlistName$
281                 call load_word_list "'localWordlistDir$'" 1
282                 call removeWordlist '.deleteWordListDir$'
283                         call load_word_list "'localWordlistDir$'" 0
284         endif
285                 call Draw_button '.table$' '.label$' 0
286                 call Draw_config_page
287     endif
288 endproc
290 wordlistTag$ = "Wordlist name"        
291 procedure processConfigInstallWordlist .clickX .clickY .pressed$
292         .table$ = "Config"
293         .label$ = "InstallWordlist"
294     call Draw_button '.table$' '.label$' 1
295         if windows
296                 # Do not use the automatic sgc list option, ask for a wordlist NAME
297                 # Get help text
298                 call findLabel '.table$' '.label$'
299                 .row = findLabel.row
300                 select Table '.table$'
301                 .openDialogue$ = Get value... '.row' Helptext
302                 call convert_praat_to_latin1 '.openDialogue$'
303                 .openDialogue$ = convert_praat_to_latin1.text$
304                 .wordlistButton$ = replace$(wordlistTag$, " ", "_", 0)
305                 .wordlistButton$ = replace_regex$(.wordlistButton$, "^.", "\l&", 0)
306                 beginPause(.openDialogue$)
307                         sentence (wordlistTag$, "")
308                 clicked = endPause ("Cancel", "Open", 2, 1)
309                 .wordlist_Name$ = ""
310                 if clicked = 2
311                         .wordlist_Name$ = '.wordlistButton$'$
312                 endif
313                 call install_wordlists_by_name '.wordlist_Name$'
314         else
315         call sgc2wordlist 'homeDirectory$'
316         call sgc2wordlist 'homeDirectory$'/Downloads
317         call sgc2wordlist 'homeDirectory$'/Documents
318         call sgc2wordlist 'homeDirectory$'/My Documents
319         call sgc2wordlist 'homeDirectory$'/My Documents/Downloads
320         call sgc2wordlist 'homeDirectory$'/Desktop
321         call sgc2wordlist 'preferencesAppDir$'
322         endif
323         call load_word_list "'localWordlistDir$'" 0
324     call Draw_button '.table$' '.label$' 0
325         call Draw_config_page
326 endproc
328 procedure processConfigOpenWordlist .clickX .clickY .pressed$
329         .table$ = "Config"
330         .label$ = "OpenWordlist"
331     call Draw_button '.table$' '.label$' 1
333         # Get help text
334         call findLabel '.table$' '.label$'
335         .row = findLabel.row
336         select Table '.table$'
337         .openDialogue$ = Get value... '.row' Helptext
338         call convert_praat_to_latin1 '.openDialogue$'
339         .openDialogue$ = convert_praat_to_latin1.text$
341         .wordlist_Name$ = chooseReadFile$ (.openDialogue$)
342         call install_wordlists_by_name '.wordlist_Name$'
343         call load_word_list "'localWordlistDir$'" 0
344     call Draw_button '.table$' '.label$' 0
345         call Draw_config_page
346 endproc
348 procedure processConfigExportWordlist .clickX .clickY .pressed$
349         .table$ = "Config"
350         .label$ = "ExportWordlist"
351         
352     call Draw_button '.table$' '.label$' 1
353         
354         # Get help text
355         call findLabel '.table$' '.label$'
356         .row = findLabel.row
357         select Table '.table$'
358         .openDialogue$ = Get value... '.row' Helptext
359         call convert_praat_to_latin1 '.openDialogue$'
360         .openDialogue$ = convert_praat_to_latin1.text$
361         
362         .defaultName$= "'wordlist$'_SGC2.tsv"
364         .newWordlistName$ = chooseWriteFile$ (.openDialogue$, .defaultName$)
365         # Ensure the right extension: ".tsv"
366         while .newWordlistName$ <> "" and not endsWith(.newWordlistName$, ".tsv")
367                 .newWordlistName$ = .newWordlistName$ + ".tsv"
368                 .newWordlistName$ = chooseWriteFile$ (.openDialogue$, .newWordlistName$)
369         endwhile
370         if .newWordlistName$ <> ""
371                 select sgc.currentWordlist
372                 .tmpSaveWordlist = Copy: "SaveWordlist"
373                 # Remove all words that are deselected
374                 .showCol = Get column index: "Show"
375                 if .showCol > 0
376                         .numRows = Get number of rows
377                         .row = .numRows
378                         while .row > 0
379                                 .show$ = Get value: .row, "Show"
380                                 .sound$ = Get value: .row, "Sound"
381                                 if .show$ = "-"
382                                         Remove row: .row
383                                 elsif .sound$ <> "" and .sound$ <> "-" and .sound$ <> "?"
384                                         # Put path before sound examples
385                                         # If there is no path, add the current wordlist path
386                                         if index_regex(.sound$, "^(/|~/|[A-Z]:\\)") <= 0
387                                                 .sound$ = localWordlistDir$+"/"+wordlistName$+"/"+.sound$
388                                                 Set string value: .row, "Sound", .sound$
389                                         endif
390                                 endif
391                                 .row -= 1
392                         endwhile
393                         Remove column: "Show"
394                 endif
395                 Write to table file... '.newWordlistName$'
396                 Remove
397         endif
398         
399     call Draw_button '.table$' '.label$' 0
400         call Draw_config_page
401 endproc
403 # Select the words to practise. This is quite a complex piece of code
404 procedure processConfigSelectWordsOLD .clickX .clickY .pressed$
405         .table$ = "Config"
406         .label$ = "SelectWords"
407     call Draw_button '.table$' '.label$' 1
409         # Get help text
410         call findLabel '.table$' '.label$'
411         .row = findLabel.row
412         select Table '.table$'
413         .helpText$ = Get value... '.row' Helptext
414         call convert_praat_to_latin1 '.helpText$'
415         .helpText$ = convert_praat_to_latin1.text$
416    
417     # Implement Cancel
418     select sgc.currentWordlist
419     .tmpOriginalWordlist = Copy: "Original_'wordlist$'"
421     
422     # Set current word
423         select sgc.currentWordlist
424         sgc.numberOfWords = Get number of rows
425     .currentWord = sgc.currentWord
426     if .currentWord <= 0 or .currentWord > sgc.numberOfWords or config.shuffleLists
427                 .currentWord = 1
428         endif
429     
430         # The texts
431         call get_feedback_text 'config.language$' Part
432         call convert_praat_to_latin1 'get_feedback_text.text$'
433         .partText$ = convert_praat_to_latin1.text$
434         call get_feedback_text 'config.language$' Tones
435         call convert_praat_to_latin1 'get_feedback_text.text$'
436         .toneText$ = convert_praat_to_latin1.text$
437         call get_feedback_text 'config.language$' Cancel
438         call convert_praat_to_latin1 'get_feedback_text.text$'
439         .cancelText$ = convert_praat_to_latin1.text$
440         call get_feedback_text 'config.language$' Clear
441         call convert_praat_to_latin1 'get_feedback_text.text$'
442         .clearText$ = convert_praat_to_latin1.text$
443         call get_feedback_text 'config.language$' All
444         call convert_praat_to_latin1 'get_feedback_text.text$'
445         .allText$ = convert_praat_to_latin1.text$
446         call get_feedback_text 'config.language$' Previous
447         call convert_praat_to_latin1 'get_feedback_text.text$'
448         .prevWord$ = convert_praat_to_latin1.text$
449         call get_feedback_text 'config.language$' Next
450         call convert_praat_to_latin1 'get_feedback_text.text$'
451         .nextWord$ = convert_praat_to_latin1.text$
452         call get_feedback_text 'config.language$' Continue
453         call convert_praat_to_latin1 'get_feedback_text.text$'
454         .continueText$ = convert_praat_to_latin1.text$
455         call get_feedback_text 'config.language$' Show
456         call convert_praat_to_latin1 'get_feedback_text.text$'
457         .showText$ = convert_praat_to_latin1.text$
459         .pinyinText$ = "Pinyin"
460         .characterText$ = "Character"
461         
462         # Get lesson names
463         select sgc.currentWordlist
464         .lessonCol = Get column index: "Lesson"
465         .lessonList$ = tab$
466         .numLessons = 0
467         # All shown keeps track whether all words are shown
468         # If so, selecting a lesson is preceded by a Clear All
469         .allShown = 0
470         # Sort words for consistent selection interface
471         if config.shuffleLists
472                 # Allow subdivision in lessons
473                 if .lessonCol > 0
474                         Sort rows... Lesson Pinyin
475                 else
476                         Sort rows... Pinyin
477                 endif
478         endif           
479         if .lessonCol > 0
480                 for .i to sgc.numberOfWords
481                         .currentLesson$ = Get value: .i, "Lesson"
482                         if .currentLesson$ <> "" and .currentLesson$ <> "-" and index_regex(.lessonList$, "\t'.currentLesson$'\t") <= 0
483                                 .lessonList$ = .lessonList$ + .currentLesson$ + tab$
484                                 .numLessons += 1
485                                 .lessonName$['.numLessons'] = .currentLesson$
486                         endif
487                         .shown$ = Get value: .i, "Show"
488                         if .shown$ = "-"
489                                 .allShown = 0
490                         endif
491                 endfor
492         endif
493         
494         clicked = -1
495         .numWordsPerScreen = 15
496         while clicked <> 6 and clicked <> 1
497                 .lessonSelected = -1
498                 .toneSelected = -1
499                 .firstShown = -1
500                 
501                 select sgc.currentWordlist
502                 .lessonCol = Get column index: "Lesson"
503                 # Sort words for consistent selection interface
504         if config.shuffleLists
505                         # Allow subdivision in lessons
506                         if .lessonCol > 0
507                                 Sort rows... Lesson Pinyin
508                         else
509                                 Sort rows... Pinyin
510                         endif
511         endif           
512                 .max = .numWordsPerScreen - 1
513                 if .currentWord + .max > sgc.numberOfWords
514                         .max = sgc.numberOfWords - .currentWord
515                 endif
516                 for .i from 0 to .numWordsPerScreen - 1
517                         if .i <= .max
518                                 .currentPinyin$ = Get value: .currentWord+.i, "Pinyin"
519                                 if index_regex(.currentPinyin$, "[0-9]") <= 0
520                                         call pinyin2numbers '.currentPinyin$'
521                                         .currentPinyin$ = replace_regex$(pinyin2numbers.intermediatePinyin$, ".+", "\l&", 0)
522                                         .currentPinyin$ = replace_regex$(.currentPinyin$, "[^a-zA-Z0-9]", "", 0)
523                                 endif
524                                 .pinyin$[.i] = .currentPinyin$
526                                 .character$[.i] = Get value: .currentWord+.i, "Character"
527                                 .lessonNum$[.i] = ""
528                                 if .lessonCol > 0
529                                         .lessonNum$[.i] = Get value: .currentWord+.i, "Lesson"
530                                         .lessonNum$[.i] = " : " + .lessonNum$[.i]
531                                 endif
532                                 .showText$[.i] = .pinyin$['.i']
533                                 if .character$['.i'] <> "-"
534                                         .showText$[.i] = .pinyin$['.i']+" ("+.character$['.i']+.lessonNum$['.i']+")"
535                                 elsif .lessonNum$['.i'] <> ""
536                                         .showText$[.i] = .pinyin$['.i']+" ( -"+.lessonNum$['.i']+")"
537                                 endif
538                                 .showValueText$[.i] = Get value: .currentWord+.i, "Show"
539                                 if .showValueText$[.i] = "-"
540                                         .showValue[.i] = 0
541                                 else
542                                         .showValue[.i] = 1
543                                 endif
544                                 .showVariable$[.i] = .pinyin$[.i]
545                                 .tmp$ = .showVariable$[.i]
546                                 '.tmp$' = .showValue[.i]
547                         else
548                                 .showText$[.i] = "-"
549                                 .showValue[.i] = 0
550                         endif
551                 endfor
553                 # The user text input window (beginPause .... endPause)
554                 beginPause(.helpText$)
555                         boolean (.showText$[0], .showValue[0])
556                         boolean (.showText$[1], .showValue[1])
557                         boolean (.showText$[2], .showValue[2])
558                         boolean (.showText$[3], .showValue[3])
559                         boolean (.showText$[4], .showValue[4])
560                         boolean (.showText$[5], .showValue[5])
561                         boolean (.showText$[6], .showValue[6])
562                         boolean (.showText$[7], .showValue[7])
563                         boolean (.showText$[8], .showValue[8])
564                         boolean (.showText$[9], .showValue[9])
565                         boolean (.showText$[10], .showValue[10])
566                         boolean (.showText$[11], .showValue[11])
567                         boolean (.showText$[12], .showValue[12])
568                         boolean (.showText$[13], .showValue[13])
569                         boolean (.showText$[14], .showValue[14])
570                         if .numLessons > 0
571                                 optionMenu: .partText$, 1
572                                         option: "---"
573                                         .j = 0
574                                         for .j to .numLessons
575                                                 option: .lessonName$['.j']
576                                         endfor
577                         endif
578                         optionMenu: .toneText$, 1
579                                 option: "---"
580                                 option: "0"
581                                 option: "1"
582                                 option: "2"
583                                 option: "3"
584                                 option: "4"
585                 clicked = endPause ("'.cancelText$'", "'.clearText$'", "'.allText$'", "'.prevWord$'", "'.nextWord$'", "'.continueText$'", 6, 1)
586                 
587                 if clicked = 2
588                         for .i to sgc.numberOfWords
589                                 Set string value: .i, "Show", "-"
590                         endfor
591                         .allShown = 0
592                 elsif clicked = 3
593                         for .i to sgc.numberOfWords
594                                 Set string value: .i, "Show", "+"
595                         endfor
596                         .allShown = 1
597                 elsif clicked != 1
598                         # Get selected Part BEFORE we do anything else
599                         if .numLessons > 0
600                                 .tmp$ = replace_regex$(.partText$, "^(.)", "\l\1", 0)
601                                 .lessonSelected = '.tmp$' - 1
602                         endif
603                         if .lessonSelected > 0
604                                 .allShown = 1
605                                 .firstShown = -1
606                                 select sgc.currentWordlist
607                                 for .i to sgc.numberOfWords
608                                         # Keep track of whether all are shown
609                                         .shown$ = Get value: .i, "Show"
610                                         if .shown$ = "-"
611                                                 .allShown = 0
612                                         endif
613                                 endfor
614                                 for .i to sgc.numberOfWords
615                                         # Lessons
616                                         .currentLesson$ = Get value: .i, "Lesson"
617                                         if .currentLesson$ = .lessonName$['.lessonSelected']
618                                                 Set string value: .i, "Show", "+"
619                                         elsif .allShown = 1
620                                                 Set string value: .i, "Show", "-"
621                                         endif
622                                 endfor
623                                 for .i to sgc.numberOfWords
624                                         # Keep track of whether all are shown
625                                         .shown$ = Get value: .i, "Show"
626                                         if .firstShown <=0 and .shown$ <> "-"
627                                                 .firstShown = .i
628                                         endif
629                                 endfor
630                         else
631                                 for .i from 0 to .max
632                                         .tmp$ = .showVariable$['.i']
633                                         .showValue['.i'] = '.tmp$'
634                                         .showWord$['.i'] = "-"
635                                         if .showValue['.i'] <> 0
636                                                 .showWord$['.i'] = "+"
637                                         endif
638                                         Set string value: .currentWord+.i, "Show", .showWord$['.i']
639                                 endfor
640                         endif
641                         .tmp$ = replace_regex$(.toneText$, "^(.)", "\l\1", 0)
642                         .toneSelected = '.tmp$' - 2
643                         if .toneSelected >= 0
644                                 select sgc.currentWordlist
645                                 for .i to sgc.numberOfWords
646                                         .currentPinyin$ = Get value: .i, "Pinyin"
647                                         if index_regex(.currentPinyin$, "[0-9]") <= 0
648                                                 call pinyin2numbers '.currentPinyin$'
649                                                 .currentPinyin$ = replace_regex$(pinyin2numbers.intermediatePinyin$, ".+", "\l&", 0)
650                                                 .currentPinyin$ = replace_regex$(.currentPinyin$, "[^a-zA-Z0-9]", "", 0)
651                                         endif
652                                         .tmp$ = Get value: .i, "Show"
653                                         if .tmp$ <> "-" and index(.currentPinyin$, "'.toneSelected'") > 0
654                                                 Set string value: .i, "Show", "+"
655                                         else
656                                                 Set string value: .i, "Show", "-"
657                                                 .allShown = 0
658                                         endif
659                                 endfor
660                         endif
661                         
662                         if clicked = 4
663                                 if .firstShown > 0
664                                         .currentWord = (.firstShown div .numWordsPerScreen) * .numWordsPerScreen + 1
665                                 elsif .toneSelected < 0
666                                         .currentWord -= .numWordsPerScreen
667                                 endif
668                                 if .currentWord <= 0
669                                         .currentWord = (sgc.numberOfWords div .numWordsPerScreen) * .numWordsPerScreen + 1
670                                 endif
671                         elsif clicked = 5
672                                 if .firstShown > 0
673                                         .currentWord = (.firstShown div .numWordsPerScreen) * .numWordsPerScreen + 1
674                                 elsif .toneSelected < 0
675                                         .currentWord += .numWordsPerScreen
676                                 endif
677                                 if .currentWord > sgc.numberOfWords
678                                         .currentWord = 1
679                                 endif
680                         endif
681                 endif
682                 # Reset and go to the first selected word (can shuffle list)
683                 if clicked = 6
684                         sgc.currentWord = -1
685                         call next_word
686                 endif
687         endwhile
688         
689     # Implement Cancel
690     if clicked = 1
691                 select sgc.currentWordlist
692                 Remove
693                 select .tmpOriginalWordlist
694                 Copy: wordlist$
695         endif
696         select .tmpOriginalWordlist
697         Remove
698     call Draw_button '.table$' '.label$' 0
699 endproc
701 procedure processConfigPerfSummary .clickX .clickY .pressed$
702         .table$ = "Config"
703         .label$ = "PerfSummary"
704         
705         call testLoadTable SummaryToneEvaluation
706         if testLoadTable.table > 0
707                 call Draw_button '.table$' '.label$' 1
708                 call loadTable SummaryToneEvaluation
709                 call write_tabbed_table SummaryToneEvaluation Evaluation_'config.language$'
710                 demoWaitForInput()
711                 select Table SummaryToneEvaluation
712                 Remove
714                 call Draw_button '.table$' '.label$' 0
715                 call Draw_config_page
716         endif
717 endproc
719 # Wipe current performance table and initialize a new one
720 procedure processConfigClearSummary .clickX .clickY .pressed$
721         .table$ = "Config"
722         .label$ = "ClearSummary"
723         
724     call Draw_button '.table$' '.label$' 1
725         
726         if not sgc.saveAudioOn
727                 select Table '.table$'
728                 call findLabel '.table$' !'.label$'
729                 if findLabel.row > 0
730                         .alertText$ = Get value... 'findLabel.row' Text
731                         .confirmKey$ = Get value... 'findLabel.row' Key
732                         .popupText$ = Get value... 'findLabel.row' Helptext
733                         
734                 call write_text_popup 'defaultFont$' 14 '.popupText$'
735                 call Draw_button '.table$' '.label$' 2
736                 alertText$ = .alertText$
737                 call Draw_button '.table$' '.label$' 3
738                 alertText$ = ""
739                 
740                         # Wait for confirmation
741                         demoWaitForInput()
742                 if demoInput(.confirmKey$)
743                                 sgc.savePerf$ = ""
744                                 call initialize_toneevaluation_tables
745                         endif
746                 endif
747         endif
748         
749     call Draw_button '.table$' '.label$' 'sgc.saveAudioOn'
750         call Draw_config_page
751         
752 endproc
754 procedure processConfigListPerf .clickX .clickY .pressed$
755         .table$ = "Config"
756         .label$ = "ListPerf"
757     call Draw_button '.table$' '.label$' 1
758         
759         select sgc2.performanceTable
760         View & Edit
761         demoWaitForInput()
762         
763     call Draw_button '.table$' '.label$' 0
764         call Draw_config_page
765 endproc
767 procedure processConfigOpenPerf .clickX .clickY .pressed$
768         .table$ = "Config"
769         .label$ = "OpenPerf"
770     call Draw_button '.table$' '.label$' 1
771         
772         if not sgc.saveAudioOn 
773                 # Get help text
774                 call findLabel '.table$' !'.label$'
775                 .row = findLabel.row
776                 select Table '.table$'
777                 .openDialogue$ = Get value... '.row' Helptext
778                 call convert_praat_to_latin1 '.openDialogue$'
779                 .openDialogue$ = convert_praat_to_latin1.text$
780                 if variableExists("eval.performance$")
781                         .defaultName$= "'eval.performance$'.tsv"
782                 else
783                         .defaultName$= "Performance.tsv"
784                 endif
785                 if sgc.savePerf$ <> ""
786                         .defaultName$= sgc.savePerf$
787                 endif
788                 .performance_Name$ = chooseReadFile$ (.openDialogue$)
789                 while .performance_Name$ <> "" and index_regex(.performance_Name$, "\.(tsv|TSV)$") <= 0
790                         .performance_Name$ = chooseReadFile$ (.openDialogue$)                   
791                 endwhile
792                 if .performance_Name$ <> "" and fileReadable(.performance_Name$)
793                         sgc.savePerf$ = .performance_Name$
794                         call initialize_toneevaluation_tables
795                         
796                         # Set SaveAudio directory if it is not currently "in use"
797                         if not sgc.saveAudioOn
798                                 sgc.saveAudio$ = replace_regex$(sgc.savePerf$, "[/\\][^/\\]+$", "", 0)
799                                 # We are not sure yet that this is actually an audio directory
800                                 config.audioName$ = ""
801                         endif
802                 endif
803         endif
804         
805     call Draw_button '.table$' '.label$' 'sgc.saveAudioOn'
806         call Draw_config_page
807 endproc
809 procedure processConfigSavePerf .clickX .clickY .pressed$
810         .table$ = "Config"
811         .label$ = "SavePerf"
812     call Draw_button '.table$' '.label$' 1
813         
814         if not sgc.saveAudioOn 
815                 # Get help text
816                 call findLabel '.table$' '.label$'
817                 .row = findLabel.row
818                 select Table '.table$'
819                 .openDialogue$ = Get value... '.row' Helptext
820                 call convert_praat_to_latin1 '.openDialogue$'
821                 .openDialogue$ = convert_praat_to_latin1.text$
822                 if variableExists("eval.performance$")
823                         .defaultName$= "'eval.performance$'.tsv"
824                 else
825                         .defaultName$= "Performance.tsv"
826                 endif
827                 if sgc.savePerf$ <> ""
828                         .defaultName$= sgc.savePerf$
829                 endif
830                 .performance_Name$ = chooseWriteFile$ (.openDialogue$, .defaultName$)
831                 # Ensure the right extension: ".tsv"
832                 while .performance_Name$ <> "" and not endsWith(.performance_Name$, ".tsv")
833                         .performance_Name$ = .performance_Name$ + ".tsv"
834                         .performance_Name$ = chooseWriteFile$ (.openDialogue$, .performance_Name$)
835                 endwhile
836                 if .performance_Name$ <> ""
837                         select sgc2.performanceTable
838                         Write to table file... '.performance_Name$'
839                         sgc.savePerf$ = .performance_Name$
840                 endif
841         endif
842         
843     call Draw_button '.table$' '.label$' 'sgc.saveAudioOn'
844         call Draw_config_page
845 endproc
847 procedure processConfigManual .clickX .clickY .pressed$
848         .table$ = "Config"
849         .label$ = "Manual"
850         call Draw_button '.table$' '.label$' 1
851         if fileReadable("ManPages/SpeakGoodChinese_2.man")
852                 Read from file... ManPages/SpeakGoodChinese_2.man
853         else
854                 Go to manual page... SpeakGoodChinese 2
855         endif
856         # Wait until the manual is put to the background
857         demoWaitForInput()
858     call Draw_button '.table$' '.label$' 0
859     demo Erase all
860     call Draw_config_page
861 endproc
863 procedure processConfigSaveAudio .clickX .clickY .pressed$
864         .table$ = "Config"
865         .label$ = "SaveAudio"
866     call Draw_button '.table$' '.label$' 1
867         
868         if sgc.saveAudioOn = 0
869                 # Get help text
870                 call findLabel '.table$' '.label$'
871                 .row = findLabel.row
872                 select Table '.table$'
873                 .openDialogue$ = Get value... '.row' Helptext
874                 call convert_praat_to_latin1 '.openDialogue$'
875                 .openDialogue$ = convert_praat_to_latin1.text$
876                 .defaultName$= ""
877                 sgc.saveAudio$ = chooseDirectory$ (.openDialogue$)
878                 if sgc.saveAudio$ <> ""
879                         sgc.saveAudioOn = 1
880                         .currentPathName$ = replace_regex$(sgc.saveAudio$, "[^/\\]*[/\\]?$", "", 0)
881                         .currentDirName$ = replace$(sgc.saveAudio$, .currentPathName$, "", 0)
882                         config.savePerf = -1
883                         config.openPerf = -1
884                         config.clearSummary = -1
885                         config.audioName$ = .currentDirName$
887                         # Clear performance table and open/create sgc.savePerf$
888                         sgc.savePerf$ = "'sgc.saveAudio$'/'.currentDirName$'.tsv"
889                         call initialize_toneevaluation_tables
890                         # Write empty table
891                         call update_toneevaluation_file
892                         if sgc.savePerf$ <> "" and initialiseSGC2.toneevaluation_table$ <> ""
893                                 select sgc2.performanceTable
894                                 Write to table file: sgc.savePerf$
895                         endif
896                 else
897                         config.audioName$ = ""
898                 endif
899         else
900                 sgc.saveAudioOn = 0
901                 config.savePerf = 0
902                 config.openPerf = 0
903                 config.clearSummary = 0
904                 # Store current performance table
905                 call update_toneevaluation_file
906         endif
907         
908     call Draw_button '.table$' '.label$' 'sgc.saveAudioOn'
909         call Draw_config_page
910 endproc
912 ###############################################################
914 # Obligatory button Processing Routines
916 # These MUST be defined
918 ###############################################################
920 procedure processConfigReturn .clickX .clickY .pressed$
921         .table$ = "Config"
922         .label$ = "Return"
923         call Draw_button '.table$' '.label$' 1
924         call write_preferences ""
925 endproc
927 procedure processConfigRefresh .clickX .clickY .pressed$
928         .table$ = "Config"
929         .label$ = "Refresh"
930         call Draw_config_page
931 endproc
933 procedure processConfigCredits .clickX .clickY .pressed$
934         .table$ = "Config"
935         .label$ = "Credits"
936         call Draw_button '.table$' '.label$' 1
937         call write_text_table Credits_'config.language$'
938         demoWaitForInput()
939     call Draw_button '.table$' '.label$' 0
940     demo Erase all
941     call Draw_config_page
942 endproc
944 procedure processConfigHelp .clickX .clickY .pressed$
945         .table$ = "Config"
946         .label$ = "Help"
947         call help_loop  '.table$' Draw_config_page
948 endproc
950 ###############################################################
952 # Miscelaneous supporting code
954 ###############################################################
955 procedure install_wordlists_by_name .wordlist_Name$
956         if .wordlist_Name$ <> ""
957                 if index_regex(.wordlist_Name$, "(?iwordlist|LICENSE)\.")
958                         .wordlist_Name$ = replace_regex$(.wordlist_Name$, "[/\\](?iwordlist|LICENSE)\.([^/\\]+)$", "", 0)
959                 elsif index_regex(.wordlist_Name$, "(?i\.(wav|flac|iaf[fc]|mp3))")
960                         .wordlist_Name$ = replace_regex$(.wordlist_Name$, "[/\\][^/\\]+$", "", 0)
961                 endif
962                 if index_regex(.wordlist_Name$, "[/\\]")
963                         .sourceDir$ = left$(.wordlist_Name$, rindex_regex(.wordlist_Name$, "[/\\]") -1)
964                         .file$ = right$(.wordlist_Name$, length(.wordlist_Name$) - rindex_regex(.wordlist_Name$, "[/\\]"))
965                         call readWordlist "'.sourceDir$'" '.file$'
966                 else
967                         .start = 1
968                         if index(.wordlist_Name$, ".")
969                                 .start = 4
970                         endif
971                         .extension1$ = ".sgc"
972                         .extension2$ = ".Table"
973                         .extension3$ = ".txt"
974                         .extension4$ = ".tsv"
975                         .extension5$ = ""
976                         for .e from .start to 5
977                                 .currentExtension$ = .extension'.e'$
978                         call readWordlist "'homeDirectory$'" '.wordlist_Name$''.currentExtension$'
979                         call readWordlist "'homeDirectory$'/Downloads" '.wordlist_Name$''.currentExtension$'
980                         call readWordlist "'homeDirectory$'/Documents" '.wordlist_Name$''.currentExtension$'
981                         call readWordlist "'homeDirectory$'/My Documents/Downloads" '.wordlist_Name$''.currentExtension$'
982                         call readWordlist "'homeDirectory$'/My Documents" '.wordlist_Name$''.currentExtension$'
983                         call readWordlist "'homeDirectory$'/Desktop" '.wordlist_Name$''.currentExtension$'
984                         call readWordlist "'preferencesAppDir$'" '.wordlist_Name$''.currentExtension$'
985                         endfor
986                 endif
987         endif
988 endproc
990 # Word lists: It is a VERY good idea to make sure that word-lists
991 # have unique names.
992 procedure load_word_list .localdir$ .relnumber
993         call clean_up_sound
994         if sgc.allWordLists > 0
995                 select sgc.allWordLists
996                 Remove
997                 sgc.allWordLists = -1
998         endif
999     
1000     # Remove old word list
1001     if wordlist$ <> ""
1002                 select sgc.currentWordlist
1003                 Remove
1004                 call wipeArea 'wipeWordlistArea$'
1005                 wordlist$ = ""
1006                 sgc.currentWordlist = -1
1007     endif
1008     
1009         # Create Table that will recieve the wordlists and directories
1010         sgc.allWordLists = Create Table with column names... AllWordLists 0 Name Directory
1011         .sgc.currentWordlistRow = 0
1012         
1013         # First the global word lists
1014         if fileReadable(globalwordlists$) or fileReadable("'globalwordlists$'/directory.txt")
1015         Create Strings as directory list... WordList 'globalwordlists$'
1016         .numLists = Get number of strings
1017         for .i to .numLists
1018             select Strings WordList
1019             .currentName$ = Get string... '.i'
1020                         if .currentName$ <> "directory.txt"
1021                                 select sgc.allWordLists
1022                                 .listExist = Search column: "Name", .currentName$
1023                                 if not .listExist
1024                                         Append row
1025                                         .sgc.currentWordlistRow = Get number of rows
1026                                         Set string value... '.sgc.currentWordlistRow' Name '.currentName$'
1027                                 .currentDirectory$ = globalwordlists$+"/"+.currentName$
1028                                         Set string value... '.sgc.currentWordlistRow' Directory '.currentDirectory$'
1029                                 endif
1030                         endif
1031         endfor
1032         select Strings WordList
1033         Remove
1034         endif
1035         
1036         # Now the preferences word lists
1037         if fileReadable(sgc2wordlists$) or fileReadable("'sgc2wordlists$'/directory.txt")
1038         Create Strings as directory list... WordList 'sgc2wordlists$'
1039         .numLists = Get number of strings
1040         for .i to .numLists
1041                         select Strings WordList
1042             .currentName$ = Get string... '.i'
1043                         if .currentName$ <> "directory.txt"
1044                                 select sgc.allWordLists
1045                                 .listExist = Search column: "Name", .currentName$
1046                                 if not .listExist
1047                                         Append row
1048                                         .sgc.currentWordlistRow = Get number of rows
1049                                         Set string value... '.sgc.currentWordlistRow' Name '.currentName$'
1050                                 .currentDirectory$ = sgc2wordlists$+"/"+.currentName$
1051                                         Set string value... '.sgc.currentWordlistRow' Directory '.currentDirectory$'
1052                                 endif
1053                         endif
1054         endfor
1055         select Strings WordList
1056         Remove
1057         endif
1059         # End with the word lists in the distribution
1060         call CreateCreateWordlists
1061         select Table CreateWordlists
1062     .numLists = Get number of rows
1063         for .i to .numLists
1064                 select Table CreateWordlists
1065                 .currentName$ = Get value... '.i' Name
1066                 if .currentName$ <> "directory.txt"
1067                         select sgc.allWordLists
1068                         .listExist = Search column: "Name", .currentName$
1069                         if not .listExist
1070                                 .procedureName$ = replace_regex$(.currentName$, "[^\w\-\.]", "_", 0)
1071                                 select sgc.allWordLists
1072                                 Append row
1073                                 .sgc.currentWordlistRow = Get number of rows
1074                                 Set string value... '.sgc.currentWordlistRow' Name '.currentName$'
1075                                 .currentDirectory$ = "*call Create'.procedureName$'"
1076                                 Set string value... '.sgc.currentWordlistRow' Directory '.currentDirectory$'
1077                         endif
1078                 endif
1079         endfor
1080         select Table CreateWordlists
1081         Remove
1082         
1083         # Finally, the local word lists
1084         if fileReadable(.localdir$) or fileReadable("'.localdir$'/directory.txt")
1085         Create Strings as directory list... WordList '.localdir$'
1086         .numLists = Get number of strings
1087         for .i to .numLists
1088             select Strings WordList
1089             .currentName$ = Get string... '.i'
1090                         if .currentName$ <> "directory.txt"
1091                                 select sgc.allWordLists
1092                                 .listExist = Search column: "Name", .currentName$
1093                                 if not .listExist
1094                                         Append row
1095                                         .sgc.currentWordlistRow = Get number of rows
1096                                         Set string value... '.sgc.currentWordlistRow' Name '.currentName$'
1097                                 .currentDirectory$ = .localdir$+"/"+.currentName$
1098                                         Set string value... '.sgc.currentWordlistRow' Directory '.currentDirectory$'
1099                                 endif
1100                         endif
1101         endfor
1102         select Strings WordList
1103         Remove
1104     endif
1106         # Get the position of the current word list
1107         select sgc.allWordLists
1108         .currentNumber = 1
1109     .numLists = Get number of rows
1111         if wordlistName$ <> ""
1112                 select sgc.allWordLists
1113                 .currentNumber = Search column... Name 'wordlistName$'
1114         endif
1116     wordlistNum = .currentNumber + .relnumber
1117     if wordlistNum > .numLists
1118         wordlistNum = 1
1119         elsif wordlistNum < 1 and .numLists > 0
1120                 wordlistNum = .numLists
1121     endif
1122     select sgc.allWordLists
1123     wordlistName$ = Get value... 'wordlistNum' Name
1124         .dirWordlistName$ = Get value... 'wordlistNum' Directory
1125     .dirString$ = replace_regex$(.dirWordlistName$, "[ ]", "&", 0)
1126         
1127         # Read in full tables
1128         if fileReadable("'.dirString$'/wordlist.Table")
1129                 call readTable '.dirString$'/wordlist.Table
1130         if readTable.tableID > 0
1131                         Rename... 'wordlistName$'
1132                         # Praat wil change the name if it feels like it
1133                         wordlist$ = selected$("Table")
1134                         # Add a Sound column if it is not present
1135                         .soundIndex = Get column index: "Sound"
1136                         if .soundIndex <= 0
1137                                 Append column: "Sound"
1138                         endif
1139                 else
1140                         .numLists = 0
1141                         goto EMERGENCYEXIT
1142                 endif
1143         # Handle (legacy) simple word lists
1144         elsif fileReadable("'.dirString$'/wordlist.txt")
1145                 Create Table with column names... "'wordlistName$'" 0 Pinyin Character Sound Translation
1146         wordlist$ = selected$("Table")
1147                 Read Strings from raw text file... '.dirString$'/wordlist.txt
1148                 Rename... RawWordList
1149                 .numWordStrings = Get number of strings
1150                 for .i to .numWordStrings
1151                         select Strings RawWordList
1152                         .currentFile$ = "-"
1153                         .currentChar$ = "-"
1154                         .currentTrans$ = "-"
1155                         .currentLine$ = Get string... '.i'
1156                         # Remove leading whitespace
1157                         .currentLine$ = replace_regex$(.currentLine$, "^[ \t]+", "", 0)
1158                         .separatorIndex = index_regex(.currentLine$, "[ \t;\-]")
1159                         if .separatorIndex <= 0
1160                                 .separatorIndex = length(.currentLine$) + 1
1161                         endif
1162                         .currentPinyin$ = left$(.currentLine$, .separatorIndex-1)
1163                         .currentLine$ = right$(.currentLine$, length(.currentLine$) - .separatorIndex)
1164                         # There is more on the line, but we do not know what
1165                         if length(.currentLine$) > 0
1166                                 while length(.currentLine$) > 0
1167                                         .separatorIndex = index_regex(.currentLine$, "[\t;]")
1168                                         if .separatorIndex <= 0
1169                                                 .separatorIndex = length(.currentLine$)+1
1170                                         endif
1171                                         .currentItem$ = left$(.currentLine$, .separatorIndex-1)
1172                                         .currentLine$ = right$(.currentLine$, length(.currentLine$) - .separatorIndex )
1173                                         if index_regex(.currentItem$, ".(spx|flac|wav|mp3)")
1174                                                 # Audio
1175                                                 .currentFile$ = .currentItem$
1176                                         elsif index_regex(.currentItem$, "[a-zA-Z0-9]") > 0
1177                                                 # Translation
1178                                                 .currentTrans$ = .currentItem$
1179                                         elsif index_regex(.currentItem$, "[^ \t\r\l]") > 0 && index_regex(.currentItem$, "[a-zA-Z0-9\-]") <= 0
1180                                                 # Characters
1181                                                 .currentChar$ = .currentItem$
1182                                         endif
1183                                 endwhile
1184                         endif
1185                         if .currentFile$ = "-"
1186                                 if fileReadable("'.dirString$'/'.currentPinyin$'.spx")
1187                                         .currentFile$ = .currentPinyin$+".spx"
1188                                 elsif fileReadable("'.dirString$'/'.currentPinyin$'.flac")
1189                                         .currentFile$ = .currentPinyin$+".flac"
1190                                 elsif fileReadable("'.dirString$'/'.currentPinyin$'.wav")
1191                                         .currentFile$ = .currentPinyin$+".wav"
1192                                 elsif fileReadable("'.dirString$'/'.currentPinyin$'.mp3")
1193                                         .currentFile$ = .currentPinyin$+".mp3"
1194                                 endif
1195                         endif
1196                         select Table 'wordlist$'
1197                         Append row
1198                         Set string value... '.i' Pinyin '.currentPinyin$'
1199                         Set string value... '.i' Sound '.currentFile$'
1200                         Set string value... '.i' Character '.currentChar$'
1201                         Set string value... '.i' Translation '.currentTrans$'
1202                 endfor
1203                 select Strings RawWordList
1204                 Remove
1205                 
1206                 select Table 'wordlist$'
1207         elsif fileReadable(.dirString$) or fileReadable("'.dirString$'/directory.txt")
1208                 Create Table with column names... "'wordlistName$'" 0 Pinyin Sound
1209         wordlist$ = selected$("Table")
1210                 Create Strings as file list... RawWordList '.dirString$'/*
1211                 .numWordStrings = Get number of strings
1212                 .i = 0
1213                 for .j to .numWordStrings
1214                         select Strings RawWordList
1215                         .currentLine$ = Get string... '.j'
1216                         .currentFile$ = extractWord$(.currentLine$, "")
1217                         if index_regex(.currentFile$, "\.(spx|flac|wav|mp3)")
1218                                 .currentPinyin$ = left$(.currentFile$, index(.currentFile$, ".")-1)
1219                                 select Table 'wordlist$'
1220                                 Append row
1221                                 .i += 1
1222                                 Set string value... '.i' Pinyin '.currentPinyin$'
1223                                 Set string value... '.i' Sound '.currentFile$'
1224                         endif
1225                 endfor
1226                 select Strings RawWordList
1227                 Remove
1228                 
1229                 select Table 'wordlist$'
1230         elsif startsWith(.dirString$, "*call ")
1231                 .callProcedure$ = right$(.dirString$, length(.dirString$)-1)
1232                 '.callProcedure$'
1233                 wordlist$ = selected$("Table")
1234         else
1235                 # Nothing, get out
1236                 .numLists = 0
1237                 goto EMERGENCYEXIT
1238         endif
1240         # Can this wordlist be deleted?
1241         if fileReadable("'sgc2wordlists$'/'wordlistName$'") or fileReadable("'sgc2wordlists$'/'wordlistName$'/wordlist.txt") or fileReadable("'sgc2wordlists$'/'wordlistName$'/wordlist.Table")
1242                 config.deleteWordlist = 0
1243         else
1244                 config.deleteWordlist = -1              
1245         endif
1247         # Check first column name and add Character, Translation and Show columns if missing
1248         if wordlist$ <> ""
1249                 select Table 'wordlist$'
1250                 # Hack to correct odd behavior of tables with unicode characters
1251                 .firstColumn$ = Get column label: 1
1252                 if index_regex(.firstColumn$, "^[^!-~]") > 0
1253                         .firstColumn$ = replace_regex$(.firstColumn$, "^[^!-~]", "", 0)
1254                         Set column label (index): 1, .firstColumn$
1255                 endif
1256                 
1257         sgc.numberOfWords = Get number of rows
1258                 .characterColumn = Get column index... Character
1259                 if not .characterColumn
1260                         Append column... Character
1261                         for .i to sgc.numberOfWords
1262                                 Set string value... '.i' Character -
1263                         endfor
1264                 endif
1265                 .translationColumn = Get column index... Translation
1266                 if not .translationColumn
1267                         Append column... Translation
1268                         for .i to sgc.numberOfWords
1269                                 Set string value... '.i' Translation -
1270                         endfor
1271                 endif
1272                 .showColumn = Get column index... Show
1273                 if not .showColumn
1274                         Append column... Show
1275                         for .i to sgc.numberOfWords
1276                                 Set string value... '.i' Show +
1277                         endfor
1278                 endif
1279         endif
1280         
1281         # Remove all rows without Pinyin
1282         .numRows = Get number of rows
1283         for i to .numRows
1284                 .rowNum = .numRows - i + 1
1285                 .pinyinValue$ = Get value... '.rowNum' Pinyin
1286                 if not index_regex(.pinyinValue$, "[a-zA-Z0-9]")
1287                         Remove row... '.rowNum'
1288                 endif
1289         endfor
1290         sgc.numberOfWords = Get number of rows
1291         
1292         # Shuffle words if requested
1293     if config.shuffleLists
1294         Randomize rows
1295     endif
1297         # Clean up
1298         label EMERGENCYEXIT
1299     #select sgc.allWordLists
1300     #Remove
1301         
1302     # There were no Word Lists
1303     label NOWORDLISTS
1304     if .numLists <= 0
1305         wordlistName$ = wordlistName$+" No Word Lists available"
1306         wordlistNum = 1
1307                 Create Table with column names... "'wordlistName$'" 0 Pinyin Character Translation Sound
1308         wordlist$ = selected$("Table")
1309         .i = 0
1310                 Append row
1311                 .i += 1
1312                 Set string value... '.i' Pinyin ni3hao3
1313                 Set string value... '.i' Character 你好
1314                 Set string value... '.i' Translation hello
1315                 Set string value... '.i' Sound -
1316                 Append row
1317                 .i += 1
1318                 Set string value... '.i' Pinyin xie4xie0
1319                 Set string value... '.i' Character 谢谢
1320                 Set string value... '.i' Translation thanks
1321                 Set string value... '.i' Sound -
1322                 Append row
1323                 .i += 1
1324                 Set string value... '.i' Pinyin zai4jian4
1325                 Set string value... '.i' Character 再见
1326                 Set string value... '.i' Translation goodbye
1327                 Set string value... '.i' Sound -
1328         sgc.numberOfWords = Get number of rows
1329     endif
1330         
1331         select Table 'wordlist$'
1332         sgc.currentWordlist = selected()
1333         call set_window_title 'buttons$' 'wordlistName$'
1334 endproc
1336 procedure read_wordlist .wordlistName$ .dirString$
1337         # Read in full tables
1338         if fileReadable("'.dirString$'/wordlist.Table")
1339                 call readTable '.dirString$'/wordlist.Table
1340                 .wordlistID = selected ()
1341         if .wordlistID > 0
1342                         Rename... '.wordlistName$'
1343                         # Praat wil change the name if it feels like it
1344                         .wordlist$ = selected$("Table")
1345                         # Add a Sound column if it is not present
1346                         .soundIndex = Get column index: "Sound"
1347                         if .soundIndex <= 0
1348                                 Append column: "Sound"
1349                         endif
1350                 else
1351                         goto EMERGENCYEXITWL
1352                 endif
1353         # Handle (legacy) simple word lists
1354         elsif fileReadable("'.dirString$'/wordlist.txt")
1355                 .wordlistID = Create Table with column names... "'.wordlistName$'" 0 Pinyin Character Sound Translation
1356                 .wordlist$ = selected$("Table")
1357                 Read Strings from raw text file... '.dirString$'/wordlist.txt
1358                 Rename... RawWordList
1359                 .numWordStrings = Get number of strings
1360                 for .i to .numWordStrings
1361                         select Strings RawWordList
1362                         .currentFile$ = "-"
1363                         .currentChar$ = "-"
1364                         .currentTrans$ = "-"
1365                         .currentLine$ = Get string... '.i'
1366                         # Remove leading whitespace
1367                         .currentLine$ = replace_regex$(.currentLine$, "^[ \t]+", "", 0)
1368                         .separatorIndex = index_regex(.currentLine$, "[ \t;\-]")
1369                         if .separatorIndex <= 0
1370                                 .separatorIndex = length(.currentLine$) + 1
1371                         endif
1372                         .currentPinyin$ = left$(.currentLine$, .separatorIndex-1)
1373                         .currentLine$ = right$(.currentLine$, length(.currentLine$) - .separatorIndex)
1374                         # There is more on the line, but we do not know what
1375                         if length(.currentLine$) > 0
1376                                 while length(.currentLine$) > 0
1377                                         .separatorIndex = index_regex(.currentLine$, "[\t;]")
1378                                         if .separatorIndex <= 0
1379                                                 .separatorIndex = length(.currentLine$)+1
1380                                         endif
1381                                         .currentItem$ = left$(.currentLine$, .separatorIndex-1)
1382                                         .currentLine$ = right$(.currentLine$, length(.currentLine$) - .separatorIndex )
1383                                         if index_regex(.currentItem$, ".(spx|flac|wav|mp3)")
1384                                                 # Audio
1385                                                 .currentFile$ = .currentItem$
1386                                         elsif index_regex(.currentItem$, "[a-zA-Z0-9]") > 0
1387                                                 # Translation
1388                                                 .currentTrans$ = .currentItem$
1389                                         elsif index_regex(.currentItem$, "[^ \t\r\l]") > 0 && index_regex(.currentItem$, "[a-zA-Z0-9\-]") <= 0
1390                                                 # Characters
1391                                                 .currentChar$ = .currentItem$
1392                                         endif
1393                                 endwhile
1394                         endif
1395                         if .currentFile$ = "-"
1396                                 if fileReadable("'.dirString$'/'.currentPinyin$'.spx")
1397                                         .currentFile$ = .currentPinyin$+".spx"
1398                                 elsif fileReadable("'.dirString$'/'.currentPinyin$'.flac")
1399                                         .currentFile$ = .currentPinyin$+".flac"
1400                                 elsif fileReadable("'.dirString$'/'.currentPinyin$'.wav")
1401                                         .currentFile$ = .currentPinyin$+".wav"
1402                                 elsif fileReadable("'.dirString$'/'.currentPinyin$'.mp3")
1403                                         .currentFile$ = .currentPinyin$+".mp3"
1404                                 endif
1405                         endif
1406                         select .wordlistID
1407                         Append row
1408                         Set string value... '.i' Pinyin '.currentPinyin$'
1409                         Set string value... '.i' Sound '.currentFile$'
1410                         Set string value... '.i' Character '.currentChar$'
1411                         Set string value... '.i' Translation '.currentTrans$'
1412                 endfor
1413                 select Strings RawWordList
1414                 Remove
1415                 
1416                 select .wordlistID
1417         elsif fileReadable(.dirString$) or fileReadable("'.dirString$'/directory.txt")
1418                 .wordlistID = Create Table with column names... "'.wordlistName$'" 0 Pinyin Sound
1419         .wordlist$ = selected$("Table")
1420                 .tmp = Create Strings as file list... RawWordList '.dirString$'/*
1421                 .numWordStrings = Get number of strings
1422                 .i = 0
1423                 for .j to .numWordStrings
1424                         select .tmp
1425                         .currentLine$ = Get string... '.j'
1426                         .currentFile$ = extractWord$(.currentLine$, "")
1427                         if index_regex(.currentFile$, "\.(spx|flac|wav|mp3)")
1428                                 .currentPinyin$ = left$(.currentFile$, index(.currentFile$, ".")-1)
1429                                 select sgc.currentWordlist
1430                                 Append row
1431                                 .i += 1
1432                                 Set string value... '.i' Pinyin '.currentPinyin$'
1433                                 Set string value... '.i' Sound '.currentFile$'
1434                         endif
1435                 endfor
1436                 select .tmp
1437                 Remove
1438                 
1439                 select sgc.currentWordlist
1440         elsif startsWith(.dirString$, "*call ")
1441                 .callProcedure$ = right$(.dirString$, length(.dirString$)-1)
1442                 '.callProcedure$'
1443                 .wordlistID = selected()
1444                 .wordlist$ = selected$("Table")
1445         else
1446                 # Nothing, get out
1447                 goto EMERGENCYEXITWL
1448         endif
1449         
1450         # Add the path to the file Sound file names
1451         select .wordlistID
1452         .numRows = Get number of rows
1453         for .w to .numRows
1454                 .soundfile$ = Get value: .w, "Sound"
1455                 if index_regex(.soundfile$, "[/\\]") <= 0
1456                         if index_regex(.dirString$, "[/\\]$")
1457                                 .soundfile$ = .dirString$+.soundfile$
1458                         elsif index_regex(.dirString$, "[\\]")
1459                                 .soundfile$ = .dirString$+"\\"+.soundfile$
1460                         else
1461                                 .soundfile$ = .dirString$+"/"+.soundfile$
1462                         endif
1463                         Set string value: .w, "Sound", .soundfile$ 
1464                 endif
1465         endfor
1466         
1467         label EMERGENCYEXITWL
1469 endproc
1471 procedure merge_into_wordlist .newTableID .lessonPostfix$
1472         select sgc.currentWordlist
1473         .oldLessonColumn = Get column index: "Lesson"
1475         select .newTableID
1476         .numRows = Get number of rows
1477         .lessonColumn = Get column index: "Lesson"
1478         .characterColumn = Get column index: "Character"
1479         .soundColumn = Get column index: "Sound"
1480         .translationColumn = Get column index: "Translation"
1481         for .w to .numRows
1482                 .currentFile$ = "-"
1483                 .currentChar$ = "-"
1484                 .currentTrans$ = "-"
1485                 .currentLesson$ = "-"
1486                 
1487                 select .newTableID
1488                 .currentPinyin$ = Get value: .w, "Pinyin"
1489                 if .soundColumn > 0
1490                         .currentFile$ = Get value: .w, "Sound"
1491                 endif
1492                 if .characterColumn > 0
1493                         .currentChar$ = Get value: .w, "Character"
1494                 endif
1495                 if .translationColumn > 0
1496                         .currentTrans$ = Get value: .w, "Translation"
1497                 endif
1498                 if .lessonColumn > 0
1499                         .currentLesson$ = Get value: .w, "Lesson"
1500                 endif
1501                 select sgc.currentWordlist
1502                 Append row
1503                 .n = Get number of rows
1504                 Set string value: .n, "Pinyin", .currentPinyin$
1505                 Set string value: .n, "Sound", .currentFile$
1506                 Set string value: .n, "Character", .currentChar$
1507                 Set string value: .n, "Translation", .currentTrans$
1508                 if .oldLessonColumn > 0
1509                         if .currentLesson$ = "-" or .currentLesson$ = "" or .currentLesson$ = "?"
1510                                 Set string value: .n, "Lesson", .lessonPostfix$
1511                         else
1512                                 Set string value: .n, "Lesson", .currentLesson$+" "+.lessonPostfix$
1513                         endif
1514                 endif
1515         endfor
1516 endproc
1518 procedure next_word
1519         if wordlist$ <> ""
1520                 select sgc.currentWordlist
1521                 .showCurrent$ = "-"
1522                 if sgc.currentWord < 0 or sgc.currentWord > sgc.numberOfWords
1523                 if config.shuffleLists
1524                             Randomize rows
1525                 endif
1526                         sgc.currentWord = 0
1527                 endif
1528                 while .showCurrent$ = "-" and sgc.currentWord <= sgc.numberOfWords
1529                         sgc.currentWord += 1
1530                         if sgc.currentWord <= sgc.numberOfWords
1531                                 .showCurrent$ = Get value... 'sgc.currentWord' Show
1532                         endif
1533                 endwhile
1534         endif
1535 endproc
1537 procedure previous_word
1538         if wordlist$ <> ""
1539                 select sgc.currentWordlist
1540                 .showCurrent$ = "-"
1541                 if sgc.currentWord <= 0
1542                 if config.shuffleLists
1543                             Randomize rows
1544                 endif
1545                         sgc.currentWord = sgc.numberOfWords + 1
1546                 endif
1547                 while .showCurrent$ = "-" and sgc.currentWord > 0
1548                         sgc.currentWord -= 1
1549                         if sgc.currentWord > 0
1550                                 .showCurrent$ = Get value... 'sgc.currentWord' Show
1551                         endif
1552                 endwhile
1553         endif
1554 endproc
1556 procedure display_word_list_name
1557     .xtext = 50
1558     .ytext = 12
1559    call reset_viewport
1560     .displayWordList$ = replace_regex$(wordlistName$, "[_]", " ", 0)
1561     call wipeArea 'wipeWordlistArea$'
1562         call adjustFontSizeOnHeight 'defaultFont$' 'defaultFontSize' 5
1563         .currentFontSize = adjustFontSizeOnHeight.newFontSize
1565     demo Blue
1566         demo Text special... '.xtext' Centre '.ytext' Bottom 'defaultFont$' '.currentFontSize' 0 '.displayWordList$'
1567     demo Black
1568         demoShow()
1569         call set_font_size 'defaultFontSize'
1570 endproc
1572 procedure write_word_list
1573         # Write current Pinyin text
1574         call display_text Black
1575         
1576         # Write the current word list name
1577         call display_word_list_name
1578 endproc
1580 procedure paint_saveAudio_light
1581     select Table 'config$'
1582     .row = Search column... Label SaveAudio
1583         if .row < 1
1584                 exit Button Table Config does not have a row with label SaveAudio
1585         endif
1586         # Get button values
1587     .leftX = Get value... '.row' LeftX
1588     .rightX = Get value... '.row' RightX
1589     .lowY = Get value... '.row' LowY
1590     .highY = Get value... '.row' HighY
1591     .buttonColor$ = Get value... '.row' Color
1592     # The button text and symbol
1593         .horWC = demo Horizontal mm to wc... 10.0
1594         .verWC = demo Vertical mm to wc... 10.0
1595         if .verWC > 0
1596                 .verCoeff = .horWC / .verWC
1597         else
1598                 .verCoeff = 1
1599         endif
1601     .centerX = (.leftX + .rightX)/2
1602     .centerY = .lowY + 0.6*(.highY-.lowY)
1603     .radius = min(.verCoeff * (.highY - .lowY ), (.rightX - .leftX))/3
1604     .wipeRadius = 1.1*.radius
1605     call DrawSaveAudio White '.centerX' '.centerY' '.wipeRadius'
1607     if sgc.saveAudioOn and sgc.saveAudio$ <> ""
1608                 call DrawSaveAudio "DarkBlue" '.centerX' '.centerY' '.radius'
1609     endif
1610     demoShow()
1611 endproc
1613 # Uninstall word lists
1614 procedure removeWordlist .deletedWordlistName$
1615     .targetDir$ = ""
1616     if fileReadable("'sgc2wordlists$'/'.deletedWordlistName$'") or fileReadable("'sgc2wordlists$'/'.deletedWordlistName$'/wordlist.txt") or fileReadable("'sgc2wordlists$'/'.deletedWordlistName$'/wordlist.Table")
1617         .targetDir$ = "'sgc2wordlists$'/'.deletedWordlistName$'"
1618     endif
1619         if .targetDir$ <> ""
1620         Create Strings as file list... DeleteList '.targetDir$'
1621         .numdeleteFiles = Get number of strings
1622         for .i to .numdeleteFiles
1623                 .file$ = Get string... '.i'
1624                 deleteFile("'.targetDir$'/'.file$'")
1625         endfor
1626         filedelete '.targetDir$'
1627         # Check whether the directory was actually deleted. Make sure this is a valid directory name!
1628         # That is, it does not contain funny characters, nor funny names
1629         if index_regex(.deletedWordlistName$, "[^a-zA-Z0-9_ .\-]") <= 0 and index(.deletedWordlistName$, "..") <= 0 and index_regex(.deletedWordlistName$, "[a-zA-Z]") > 0
1630                         if windows
1631                                 nocheck system rmdir "'.targetDir$'" /s /q
1632                         elsif fileReadable(.targetDir$)
1633                                 system bash -rc -- 'rm -r -- "'.targetDir$'"'
1634                         endif
1635                 endif
1636                 # Remove deleted word list
1637                 select Strings DeleteList
1638                 plus Table 'wordlist$'
1639                 Remove
1640                 wordlist$ = ""
1641         endif
1642 endproc
1644 # Install word lists
1645 procedure sgc2wordlist .sourceDir$
1646         if startsWith(.sourceDir$, "preferencesDirectory$")
1647                 .sourceDir$ = replace$(.sourceDir$, "preferencesDirectory$", preferencesDirectory$)
1648         endif
1650         .targetDirectory$ = "'sgc2wordlists$'"
1651         if  fileReadable(.sourceDir$) or fileReadable("'.sourceDir$'/directory.txt")
1652                 Create Strings as file list... PackageList '.sourceDir$'/*.sgc
1653                 .numFiles = Get number of strings
1654                 for .i to .numFiles
1655                         select Strings PackageList
1656                         .file$ = Get string... '.i'
1657                         call readWordlist '.sourceDir$' '.file$'
1658                 endfor
1660                 select Strings PackageList
1661                 Remove
1662         endif
1663 endproc
1665 # Debuggin remarks!!!
1666 # fileReadable(<directory>) does not work in Windows
1667 # The file paths / -> \\ must be performed on the filenames in Windows before the system_nocheck is given
1668 # And yet only the 7z decompression has been implemented
1669 windowsUnzipCommand$ = ""
1670 if fileReadable("C:\Program Files\7-Zip\7z.exe")
1671         windowsUnzipCommand$ = """C:\Program Files\7-Zip\7z.exe"" -y e"
1672         windowsUnzipDestDir$ = " -o"
1673 elsif fileReadable("C:\Program Files\WinZip\winzip32.exe")
1674         # !!! Find a way to include the output folder !!!
1675         windowsUnzipCommand$ = "C:\Program Files\WinZip\winzip32.exe"" -min -e -o -j "
1676         windowsUnzipDestDir$ = ""
1677 endif
1678 procedure readWordlist .sourceDir$ .file$
1679         # No use doing anything if the source does not exist
1680         if fileReadable("'.sourceDir$'/'.file$'") or fileReadable("'.sourceDir$'/'.file$'/wordlist.txt") or fileReadable("'.sourceDir$'/'.file$'/wordlist.Table")
1681                 # What will be the target wordlist directory?
1682                 .targetDirectory$ = "'sgc2wordlists$'"
1683                 .dirname$ = left$(.file$, rindex(.file$, ".")-1)
1684                 if .dirname$ = ""
1685                         .dirname$ = .file$
1686                 endif
1687                 .wordlistDirectory$ = .targetDirectory$+"/"+.dirname$
1688                 # Wordlist directory does not exist, neither locally nor in the preferences
1689                 .wordListExists = 0
1690                 .tmpDirs = nocheck Create Strings as directory list... TMPWORDLISTS '.targetDirectory$'/
1691                 if .tmpDirs != undefined and .tmpDirs > 0
1692                         .numDirs = Get number of strings
1693                         for .d to .numDirs
1694                                 select .tmpDirs
1695                                 .currentString$ = Get string... '.d'
1696                                 if .currentString$ = .dirname$
1697                                         .wordListExists = 1
1698                                 endif
1699                         endfor
1700                         Remove
1701                 endif
1702                 if not (.wordListExists or fileReadable(.dirname$) or fileReadable("'.dirname$'/directory.txt") or fileReadable(.wordlistDirectory$) or fileReadable("'.wordlistDirectory$'/directory.txt"))
1703                         .wasWordList = 0
1704                         # Move source to destination
1705                         # Use this if you want to allow direct loading of ZIP files. Could lead to problems.
1706                         # if index(.file$, ".sgc") or index(.file$, ".zip")
1707                         # 
1708                         if index(.file$, ".sgc")
1709                                 if index_regex(.wordlistDirectory$, "[^a-zA-Z0-9_\.\- /~\:]") <= 0 and index_regex(.dirname$, "[^a-zA-Z0-9_\.\- ]") <= 0 and not (windows and windowsUnzipCommand$ = "")
1710                                         # Create wordlist directory
1711                                         createDirectory(.wordlistDirectory$)
1712                                         .wasWordList = 1
1713                                         if macintosh or unix
1714                                                 system bash -rc -- 'unzip -j "'.sourceDir$'/'.file$'" -d "'.wordlistDirectory$'"'
1715                                         elsif windows and windowsUnzipCommand$ <> ""
1716                                                 .winWordListDirectory$ = replace_regex$(.wordlistDirectory$, "/", "\\", 0)
1717                                                 .winSourceDirectory$ = replace_regex$("'.sourceDir$'\'.file$'", "/", "\\", 0)
1718                                                 system mkdir "'.winWordListDirectory$'" & 'windowsUnzipCommand$' "'.winSourceDirectory$'" 'windowsUnzipDestDir$'"'.winWordListDirectory$'"
1719                                         endif
1720                                 elsif (windows and windowsUnzipCommand$ = "")
1721                                         # Warn to install 7Zip
1722                                         call get_feedback_text 'config.language$' InstallUnzip
1723                                         call convert_praat_to_latin1 'get_feedback_text.text$'
1724                                         .zipText$ = convert_praat_to_latin1.text$
1725                                         call write_text_popup 'defaultFont$' 14 '.zipText$'
1726                                         # Wait for confirmation
1727                                         demoWaitForInput()
1728                                         call Draw_config_page
1729                                 endif
1730                                 # Remove if not valid!
1731                                 if fileReadable("'.wordlistDirectory$'/wordlist.Table") or fileReadable("'.wordlistDirectory$'/wordlist.txt") or fileReadable("'.wordlistDirectory$'/LICENSE.txt")
1732                                         if fileReadable("'.wordlistDirectory$'/wordlist.Table")
1733                                                 call readTable '.sourceDir$'/'.file$'
1734                                                 if readTable.tableID > 0
1735                                                         select readTable.tableID
1736                                                         # Hack around odd behavior of column index
1737                                                         .pinyinCol = 0
1738                                                         .firstColumn$ = Get column label: 1
1739                                                         .firstColumn$ = replace_regex$(.firstColumn$, "^[^!-~]", "", 0)
1740                                                         if .firstColumn$ = "Pinyin"
1741                                                                 .pinyinCol = 1
1742                                                         else
1743                                                                 .pinyinCol = Get column index... Pinyin
1744                                                         endif
1745                                                         Remove
1746                                                         # No Pinyin in table
1747                                                         if .pinyinCol <= 0
1748                                                                 .wasWordList = 0
1749                                                         endif
1750                                                 else
1751                                                         .wasWordList = 0
1752                                                 endif
1753                                         endif
1754                                 else
1755                                         # None of wordlist.Table, wordlist.txt, nor LICENSE.txt
1756                                         .wasWordList = 0
1757                                 endif
1758                                 ### REALLY DANGEROUS STUFF, SHOULD BE HANDLED BETTER
1759                                 if .wasWordList = 0
1760                                         # Remove newly created directory
1761                                         if index_regex(.wordlistDirectory$, "[^a-zA-Z0-9_\.\- /~\:]") <= 0 and index_regex(.dirname$, "[^a-zA-Z0-9_\.\- ]") <= 0 and index(.dirname$, "..") <= 0
1762                                                 if macintosh or unix
1763                                                         system bash -rc -- 'rm -r "'.wordlistDirectory$'/"'
1764                                                 elsif windows
1765                                                         system rmdir /Q /S "'.winWordListDirectory$'/"
1766                                                 endif
1767                                         endif
1768                                 endif
1769                         elsif index_regex(.file$, "\.(Table|txt|tsv)")
1770                                 # Check whether this is a valid table
1771                                 call readTable '.sourceDir$'/'.file$'
1772                                 if readTable.tableID > 0
1773                                         select readTable.tableID
1774                                         # Hack around odd behavior of column index
1775                                         .pinyinCol = 0
1776                                         .firstColumn$ = Get column label: 1
1777                                         .firstColumn$ = replace_regex$(.firstColumn$, "^[^!-~]", "", 0)
1778                                         if .firstColumn$ = "Pinyin"
1779                                                 .pinyinCol = 1
1780                                         else
1781                                                 .pinyinCol = Get column index... Pinyin
1782                                         endif
1783                                         
1784                                         if .pinyinCol > 0
1785                                                 select readTable.tableID
1786                                                 # Create wordlist directory
1787                                                 createDirectory(.wordlistDirectory$)
1788                                                 .wasWordList = 1
1789                                                 .extension$ = "Table"
1790                                                 .wordlistFilePath$ = "'.wordlistDirectory$'/wordlist.'.extension$'"
1791                                                 if windows
1792                                                         .wordlistFilePath$ = replace_regex$(.wordlistFilePath$, "/", "\\", 0)
1793                                                 endif
1794                                                 # Save
1795                                                 select readTable.tableID
1796                                                 Save as tab-separated file: .wordlistFilePath$
1797                                                 if not fileReadable(.wordlistFilePath$)
1798                                                         .wasWordList = 0
1799                                                 endif
1800                                         endif
1801                                         select readTable.tableID
1802                                         Remove
1803                                 endif
1804                         elsif fileReadable("'.sourceDir$'/'.file$'/wordlist.Table") or fileReadable("'.sourceDir$'/'.file$'/wordlist.txt") or fileReadable("'.sourceDir$'/'.file$'/LICENSE.txt")
1805                                 # Copy wordlist directory
1806                                 if index_regex(.file$, "[^a-zA-Z0-9_\.\- ]") <= 0
1807                                         .wasWordList = 1
1808                                         if macintosh or unix
1809                                                 system bash -rc -- 'cp -r "'.sourceDir$'/'.file$'" "'.wordlistDirectory$'"'
1810                                         elsif windows
1811                                                 createDirectory(.wordlistDirectory$)
1812                                                 .winWordListDirectory$ = replace_regex$(.wordlistDirectory$, "/", "\\", 0)
1813                                                 .winSourceDirectory$ = replace_regex$("'.sourceDir$'\'.file$'", "/", "\\", 0)
1814                                                 system xcopy "'.winSourceDirectory$'" "'.winWordListDirectory$'" /s /e /q
1815                                         endif
1816                                 endif
1817                         endif
1818                         
1819                         # Set current word list to read list
1820                         if .wasWordList
1821                                 wordlistName$ = .dirname$
1822                         else
1823                                 .table$ = "Config"
1824                                 .label$ = "!NotAWordlist"
1826                                 # Get help text
1827                                 call findLabel '.table$' '.label$'
1828                                 .row = findLabel.row
1829                                 select Table '.table$'
1830                                 .helpText$ = Get value... '.row' Helptext
1831                                 .printablePath$ = "'.sourceDir$'/'.file$'"
1832                                 if windows
1833                                         .printablePath$ = replace$("'.sourceDir$'\'.file$'", "\", "\bs", 0)
1834                                 endif
1835                                 .filetext$ = replace_regex$(.printablePath$, "_", "\\_ ", 0)
1836                         call write_text_popup 'defaultFont$' 14 '.helpText$' "'.filetext$'"
1837                                 # Wait for confirmation
1838                                 demoWaitForInput()
1839                         endif
1840                 endif
1841         endif
1842 endproc