Added full log and replay functionality in the SCRIPT version (removed from binary).
[sgc2.git] / Config.praat
blobf6433b0574e660cec9c8eb4081e186db9c3ecf61
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         .newWordlistName$ = "'wordlist$'_SGC2"
364         # Ensure the right extension: ".tsv"
365         while .newWordlistName$ <> "" and not endsWith(.newWordlistName$, ".tsv")
366                 .newWordlistName$ = .newWordlistName$ + ".tsv"
367                 .newWordlistName$ = chooseWriteFile$ (.openDialogue$, .newWordlistName$)
368                 
369                 .tableName$ = replace_regex$(.newWordlistName$, "^.*[/\\:]([^/\\:]+)\.(tsv|Table)$", "\1", 0)
370                 call testLoadTable '.tableName$'
371                 if testLoadTable.table > 0
372                         call loadTable '.tableName$'
373                         .tmpTable = selected()
374                         Save as tab-separated file: .newWordlistName$
375                         Remove
376                         .newWordlistName$ = ""
377                 endif
378                 
379         endwhile
380         if .newWordlistName$ <> ""
381                 select sgc.currentWordlist
382                 .tmpSaveWordlist = Copy: "SaveWordlist"
383                 # Get the table sorted
384                 .lessonCol = Get column index: "Lesson"
385                 if .lessonCol > 0
386                         Sort rows... Lesson Pinyin
387                 else
388                         Sort rows... Pinyin
389                 endif
390                 
391                 # Remove all words that are deselected
392                 .showCol = Get column index: "Show"
393                 if .showCol > 0
394                         .numRows = Get number of rows
395                         .row = .numRows
396                         while .row > 0
397                                 .show$ = Get value: .row, "Show"
398                                 .sound$ = Get value: .row, "Sound"
399                                 if .show$ = "-"
400                                         .numRows = Get number of rows
401                                         # Do NOT remove all the rows
402                                         if .numRows > 1
403                                                 Remove row: .row
404                                         endif
405                                 elsif .sound$ <> "" and .sound$ <> "-" and .sound$ <> "?"
406                                         # Put path before sound examples
407                                         # If there is no path, add the current wordlist path
408                                         if index_regex(.sound$, "^(/|~/|[A-Z]:\\)") <= 0
409                                                 .sound$ = localWordlistDir$+"/"+wordlistName$+"/"+.sound$
410                                                 Set string value: .row, "Sound", .sound$
411                                         endif
412                                 endif
413                                 .row -= 1
414                         endwhile
415                         Remove column: "Show"
416                 endif
417                 Write to table file... '.newWordlistName$'
418                 Remove
419         endif
420         
421     call Draw_button '.table$' '.label$' 0
422         call Draw_config_page
423 endproc
425 procedure processConfigPerfSummary .clickX .clickY .pressed$
426         .table$ = "Config"
427         .label$ = "PerfSummary"
428         
429         call testLoadTable SummaryToneEvaluation
430         if testLoadTable.table > 0
431                 call Draw_button '.table$' '.label$' 1
432                 call loadTable SummaryToneEvaluation
433                 call write_tabbed_table SummaryToneEvaluation Evaluation_'config.language$'
434                 demoWaitForInput()
435                 select Table SummaryToneEvaluation
436                 Remove
438                 call Draw_button '.table$' '.label$' 0
439                 call Draw_config_page
440         endif
441 endproc
443 # Wipe current performance table and initialize a new one
444 procedure processConfigClearSummary .clickX .clickY .pressed$
445         .table$ = "Config"
446         .label$ = "ClearSummary"
447         
448     call Draw_button '.table$' '.label$' 1
449         
450         if not sgc.saveAudioOn
451                 select Table '.table$'
452                 call findLabel '.table$' !'.label$'
453                 if findLabel.row > 0
454                         .alertText$ = Get value... 'findLabel.row' Text
455                         .confirmKey$ = Get value... 'findLabel.row' Key
456                         .popupText$ = Get value... 'findLabel.row' Helptext
457                         
458                 call write_text_popup 'defaultFont$' 14 '.popupText$'
459                 call Draw_button '.table$' '.label$' 2
460                 alertText$ = .alertText$
461                 call Draw_button '.table$' '.label$' 3
462                 alertText$ = ""
463                 
464                         # Wait for confirmation
465                         demoWaitForInput()
466                 if demoInput(.confirmKey$)
467                                 sgc.savePerf$ = ""
468                                 call initialize_toneevaluation_tables
469                         endif
470                 endif
471         endif
472         
473     call Draw_button '.table$' '.label$' 'sgc.saveAudioOn'
474         call Draw_config_page
475         
476 endproc
478 procedure processConfigListPerf .clickX .clickY .pressed$
479         .table$ = "Config"
480         .label$ = "ListPerf"
481     call Draw_button '.table$' '.label$' 1
482         
483         select sgc2.performanceTable
484         View & Edit
485         demoWaitForInput()
486         
487     call Draw_button '.table$' '.label$' 0
488         call Draw_config_page
489 endproc
491 procedure processConfigOpenPerf .clickX .clickY .pressed$
492         .table$ = "Config"
493         .label$ = "OpenPerf"
494     call Draw_button '.table$' '.label$' 1
495         
496         if not sgc.saveAudioOn 
497                 # Get help text
498                 call findLabel '.table$' !'.label$'
499                 .row = findLabel.row
500                 select Table '.table$'
501                 .openDialogue$ = Get value... '.row' Helptext
502                 call convert_praat_to_latin1 '.openDialogue$'
503                 .openDialogue$ = convert_praat_to_latin1.text$
504                 if variableExists("eval.performance$")
505                         .defaultName$= "'eval.performance$'.tsv"
506                 else
507                         .defaultName$= "Performance.tsv"
508                 endif
509                 if sgc.savePerf$ <> ""
510                         .defaultName$= sgc.savePerf$
511                 endif
512                 .performance_Name$ = chooseReadFile$ (.openDialogue$)
513                 while .performance_Name$ <> "" and index_regex(.performance_Name$, "\.(tsv|TSV)$") <= 0
514                         .performance_Name$ = chooseReadFile$ (.openDialogue$)                   
515                 endwhile
516                 if .performance_Name$ <> "" and fileReadable(.performance_Name$)
517                         sgc.savePerf$ = .performance_Name$
518                         call initialize_toneevaluation_tables
519                         
520                         # Set SaveAudio directory if it is not currently "in use"
521                         if not sgc.saveAudioOn
522                                 sgc.saveAudio$ = replace_regex$(sgc.savePerf$, "[/\\][^/\\]+$", "", 0)
523                                 # We are not sure yet that this is actually an audio directory
524                                 config.audioName$ = ""
525                         endif
526                 endif
527         endif
528         
529     call Draw_button '.table$' '.label$' 'sgc.saveAudioOn'
530         call Draw_config_page
531 endproc
533 procedure processConfigSavePerf .clickX .clickY .pressed$
534         .table$ = "Config"
535         .label$ = "SavePerf"
536     call Draw_button '.table$' '.label$' 1
537         
538         if not sgc.saveAudioOn 
539                 # Get help text
540                 call findLabel '.table$' '.label$'
541                 .row = findLabel.row
542                 select Table '.table$'
543                 .openDialogue$ = Get value... '.row' Helptext
544                 call convert_praat_to_latin1 '.openDialogue$'
545                 .openDialogue$ = convert_praat_to_latin1.text$
546                 if variableExists("eval.performance$")
547                         .defaultName$= "'eval.performance$'.tsv"
548                 else
549                         .defaultName$= "Performance.tsv"
550                 endif
551                 if sgc.savePerf$ <> ""
552                         .defaultName$= sgc.savePerf$
553                 endif
554                 .performance_Name$ = chooseWriteFile$ (.openDialogue$, .defaultName$)
555                 # Ensure the right extension: ".tsv"
556                 while .performance_Name$ <> "" and not endsWith(.performance_Name$, ".tsv")
557                         .performance_Name$ = .performance_Name$ + ".tsv"
558                         .performance_Name$ = chooseWriteFile$ (.openDialogue$, .performance_Name$)
559                 endwhile
560                 if .performance_Name$ <> ""
561                         select sgc2.performanceTable
562                         Write to table file... '.performance_Name$'
563                         sgc.savePerf$ = .performance_Name$
564                 endif
565         endif
566         
567     call Draw_button '.table$' '.label$' 'sgc.saveAudioOn'
568         call Draw_config_page
569 endproc
571 procedure processConfigManual .clickX .clickY .pressed$
572         .table$ = "Config"
573         .label$ = "Manual"
574         call Draw_button '.table$' '.label$' 1
575         if fileReadable("ManPages/SpeakGoodChinese_2.man")
576                 Read from file... ManPages/SpeakGoodChinese_2.man
577         else
578                 Go to manual page... SpeakGoodChinese 2
579         endif
580         # Wait until the manual is put to the background
581         demoWaitForInput()
582     call Draw_button '.table$' '.label$' 0
583     demo Erase all
584     call Draw_config_page
585 endproc
587 procedure processConfigSaveAudio .clickX .clickY .pressed$
588         .table$ = "Config"
589         .label$ = "SaveAudio"
590     call Draw_button '.table$' '.label$' 1
591         
592         if sgc.saveAudioOn = 0
593                 # Get help text
594                 call findLabel '.table$' '.label$'
595                 .row = findLabel.row
596                 select Table '.table$'
597                 .openDialogue$ = Get value... '.row' Helptext
598                 call convert_praat_to_latin1 '.openDialogue$'
599                 .openDialogue$ = convert_praat_to_latin1.text$
600                 .defaultName$= ""
601                 sgc.saveAudio$ = chooseDirectory$ (.openDialogue$)
602                 if sgc.saveAudio$ <> ""
603                         sgc.saveAudioOn = 1
604                         .currentPathName$ = replace_regex$(sgc.saveAudio$, "[^/\\]*[/\\]?$", "", 0)
605                         .currentDirName$ = replace$(sgc.saveAudio$, .currentPathName$, "", 0)
606                         config.savePerf = -1
607                         config.openPerf = -1
608                         config.clearSummary = -1
609                         config.audioName$ = .currentDirName$
611                         # Clear performance table and open/create sgc.savePerf$
612                         sgc.savePerf$ = "'sgc.saveAudio$'/'.currentDirName$'.tsv"
613                         call initialize_toneevaluation_tables
614                         # Write empty table
615                         call update_toneevaluation_file
616                         if sgc.savePerf$ <> "" and initialiseSGC2.toneevaluation_table$ <> ""
617                                 select sgc2.performanceTable
618                                 Write to table file: sgc.savePerf$
619                         endif
620                 else
621                         config.audioName$ = ""
622                 endif
623         else
624                 sgc.saveAudioOn = 0
625                 config.savePerf = 0
626                 config.openPerf = 0
627                 config.clearSummary = 0
628                 # Store current performance table
629                 call update_toneevaluation_file
630         endif
631         
632     call Draw_button '.table$' '.label$' 'sgc.saveAudioOn'
633         call Draw_config_page
634 endproc
636 ###############################################################
638 # Obligatory button Processing Routines
640 # These MUST be defined
642 ###############################################################
644 procedure processConfigReturn .clickX .clickY .pressed$
645         .table$ = "Config"
646         .label$ = "Return"
647         call Draw_button '.table$' '.label$' 1
648         call write_preferences ""
649 endproc
651 procedure processConfigRefresh .clickX .clickY .pressed$
652         .table$ = "Config"
653         .label$ = "Refresh"
654         call Draw_config_page
655 endproc
657 procedure processConfigCredits .clickX .clickY .pressed$
658         .table$ = "Config"
659         .label$ = "Credits"
660         call Draw_button '.table$' '.label$' 1
661         call write_text_table Credits_'config.language$'
662         demoWaitForInput()
663     call Draw_button '.table$' '.label$' 0
664     demo Erase all
665     call Draw_config_page
666 endproc
668 procedure processConfigHelp .clickX .clickY .pressed$
669         .table$ = "Config"
670         .label$ = "Help"
671         call help_loop  '.table$' Draw_config_page
672 endproc
674 ###############################################################
676 # Miscelaneous supporting code
678 ###############################################################
679 procedure install_wordlists_by_name .wordlist_Name$
680         if .wordlist_Name$ <> ""
681                 if index_regex(.wordlist_Name$, "(?iwordlist|LICENSE)\.")
682                         .wordlist_Name$ = replace_regex$(.wordlist_Name$, "[/\\](?iwordlist|LICENSE)\.([^/\\]+)$", "", 0)
683                 elsif index_regex(.wordlist_Name$, "(?i\.(wav|flac|iaf[fc]|mp3))")
684                         .wordlist_Name$ = replace_regex$(.wordlist_Name$, "[/\\][^/\\]+$", "", 0)
685                 endif
686                 if index_regex(.wordlist_Name$, "[/\\]")
687                         .sourceDir$ = left$(.wordlist_Name$, rindex_regex(.wordlist_Name$, "[/\\]") -1)
688                         .file$ = right$(.wordlist_Name$, length(.wordlist_Name$) - rindex_regex(.wordlist_Name$, "[/\\]"))
689                         call readWordlist "'.sourceDir$'" '.file$'
690                 else
691                         .start = 1
692                         if index(.wordlist_Name$, ".")
693                                 .start = 4
694                         endif
695                         .extension1$ = ".sgc"
696                         .extension2$ = ".Table"
697                         .extension3$ = ".txt"
698                         .extension4$ = ".tsv"
699                         .extension5$ = ""
700                         for .e from .start to 5
701                                 .currentExtension$ = .extension'.e'$
702                         call readWordlist "'homeDirectory$'" '.wordlist_Name$''.currentExtension$'
703                         call readWordlist "'homeDirectory$'/Downloads" '.wordlist_Name$''.currentExtension$'
704                         call readWordlist "'homeDirectory$'/Documents" '.wordlist_Name$''.currentExtension$'
705                         call readWordlist "'homeDirectory$'/My Documents/Downloads" '.wordlist_Name$''.currentExtension$'
706                         call readWordlist "'homeDirectory$'/My Documents" '.wordlist_Name$''.currentExtension$'
707                         call readWordlist "'homeDirectory$'/Desktop" '.wordlist_Name$''.currentExtension$'
708                         call readWordlist "'preferencesAppDir$'" '.wordlist_Name$''.currentExtension$'
709                         endfor
710                 endif
711         endif
712 endproc
714 # Word lists: It is a VERY good idea to make sure that word-lists
715 # have unique names.
716 procedure load_word_list .localdir$ .relnumber
717         call clean_up_sound
718         if sgc.allWordLists > 0
719                 select sgc.allWordLists
720                 Remove
721                 sgc.allWordLists = -1
722         endif
723     
724     # Remove old word list
725     if wordlist$ <> ""
726                 select sgc.currentWordlist
727                 Remove
728                 call wipeArea 'wipeWordlistArea$'
729                 wordlist$ = ""
730                 sgc.currentWordlist = -1
731                 sgc.currentWord = 1
732     endif
733     
734         # Create Table that will recieve the wordlists and directories
735         sgc.allWordLists = Create Table with column names... AllWordLists 0 Name Directory
736         .sgc.currentWordlistRow = 0
737         
738         # First the global word lists
739         if fileReadable(globalwordlists$) or fileReadable("'globalwordlists$'/directory.txt")
740         Create Strings as directory list... WordList 'globalwordlists$'
741         .numLists = Get number of strings
742         for .i to .numLists
743             select Strings WordList
744             .currentName$ = Get string... '.i'
745                         if .currentName$ <> "directory.txt"
746                                 select sgc.allWordLists
747                                 .listExist = Search column: "Name", .currentName$
748                                 if not .listExist
749                                         Append row
750                                         .sgc.currentWordlistRow = Get number of rows
751                                         Set string value... '.sgc.currentWordlistRow' Name '.currentName$'
752                                 .currentDirectory$ = globalwordlists$+"/"+.currentName$
753                                         Set string value... '.sgc.currentWordlistRow' Directory '.currentDirectory$'
754                                 endif
755                         endif
756         endfor
757         select Strings WordList
758         Remove
759         endif
760         
761         # Now the preferences word lists
762         if fileReadable(sgc2wordlists$) or fileReadable("'sgc2wordlists$'/directory.txt")
763         Create Strings as directory list... WordList 'sgc2wordlists$'
764         .numLists = Get number of strings
765         for .i to .numLists
766                         select Strings WordList
767             .currentName$ = Get string... '.i'
768                         if .currentName$ <> "directory.txt"
769                                 select sgc.allWordLists
770                                 .listExist = Search column: "Name", .currentName$
771                                 if not .listExist
772                                         Append row
773                                         .sgc.currentWordlistRow = Get number of rows
774                                         Set string value... '.sgc.currentWordlistRow' Name '.currentName$'
775                                 .currentDirectory$ = sgc2wordlists$+"/"+.currentName$
776                                         Set string value... '.sgc.currentWordlistRow' Directory '.currentDirectory$'
777                                 endif
778                         endif
779         endfor
780         select Strings WordList
781         Remove
782         endif
784         # End with the word lists in the distribution
785         call CreateCreateWordlists
786         select Table CreateWordlists
787     .numLists = Get number of rows
788         for .i to .numLists
789                 select Table CreateWordlists
790                 .currentName$ = Get value... '.i' Name
791                 if .currentName$ <> "directory.txt"
792                         select sgc.allWordLists
793                         .listExist = Search column: "Name", .currentName$
794                         if not .listExist
795                                 .procedureName$ = replace_regex$(.currentName$, "[^\w\-\.]", "_", 0)
796                                 select sgc.allWordLists
797                                 Append row
798                                 .sgc.currentWordlistRow = Get number of rows
799                                 Set string value... '.sgc.currentWordlistRow' Name '.currentName$'
800                                 .currentDirectory$ = "*call Create'.procedureName$'"
801                                 Set string value... '.sgc.currentWordlistRow' Directory '.currentDirectory$'
802                         endif
803                 endif
804         endfor
805         select Table CreateWordlists
806         Remove
807         
808         # Finally, the local word lists
809         if fileReadable(.localdir$) or fileReadable("'.localdir$'/directory.txt")
810         Create Strings as directory list... WordList '.localdir$'
811         .numLists = Get number of strings
812         for .i to .numLists
813             select Strings WordList
814             .currentName$ = Get string... '.i'
815                         if .currentName$ <> "directory.txt"
816                                 select sgc.allWordLists
817                                 .listExist = Search column: "Name", .currentName$
818                                 if not .listExist
819                                         Append row
820                                         .sgc.currentWordlistRow = Get number of rows
821                                         Set string value... '.sgc.currentWordlistRow' Name '.currentName$'
822                                 .currentDirectory$ = .localdir$+"/"+.currentName$
823                                         Set string value... '.sgc.currentWordlistRow' Directory '.currentDirectory$'
824                                 endif
825                         endif
826         endfor
827         select Strings WordList
828         Remove
829     endif
831         # Get the position of the current word list
832         select sgc.allWordLists
833         .currentNumber = 1
834     .numLists = Get number of rows
836         if wordlistName$ <> ""
837                 select sgc.allWordLists
838                 .currentNumber = Search column... Name 'wordlistName$'
839         endif
841     wordlistNum = .currentNumber + .relnumber
842     if wordlistNum > .numLists
843         wordlistNum = 1
844         elsif wordlistNum < 1 and .numLists > 0
845                 wordlistNum = .numLists
846     endif
847     select sgc.allWordLists
848     wordlistName$ = Get value... 'wordlistNum' Name
849         .dirWordlistName$ = Get value... 'wordlistNum' Directory
850     .dirString$ = replace_regex$(.dirWordlistName$, "[ ]", "&", 0)
851         
852         # Read in full tables
853         if fileReadable("'.dirString$'/wordlist.Table")
854                 call readTable '.dirString$'/wordlist.Table
855         if readTable.tableID > 0
856                         Rename... 'wordlistName$'
857                         # Praat wil change the name if it feels like it
858                         wordlist$ = selected$("Table")
859                         # Add a Sound column if it is not present
860                         .soundIndex = Get column index: "Sound"
861                         if .soundIndex <= 0
862                                 Append column: "Sound"
863                         endif
864                 else
865                         .numLists = 0
866                         goto EMERGENCYEXIT
867                 endif
868         # Handle (legacy) simple word lists
869         elsif fileReadable("'.dirString$'/wordlist.txt")
870                 Create Table with column names... "'wordlistName$'" 0 Pinyin Character Sound Translation
871         wordlist$ = selected$("Table")
872                 Read Strings from raw text file... '.dirString$'/wordlist.txt
873                 Rename... RawWordList
874                 .numWordStrings = Get number of strings
875                 for .i to .numWordStrings
876                         select Strings RawWordList
877                         .currentFile$ = "-"
878                         .currentChar$ = "-"
879                         .currentTrans$ = "-"
880                         .currentLine$ = Get string... '.i'
881                         # Remove leading whitespace
882                         .currentLine$ = replace_regex$(.currentLine$, "^[ \t]+", "", 0)
883                         .separatorIndex = index_regex(.currentLine$, "[ \t;\-]")
884                         if .separatorIndex <= 0
885                                 .separatorIndex = length(.currentLine$) + 1
886                         endif
887                         .currentPinyin$ = left$(.currentLine$, .separatorIndex-1)
888                         .currentLine$ = right$(.currentLine$, length(.currentLine$) - .separatorIndex)
889                         # There is more on the line, but we do not know what
890                         if length(.currentLine$) > 0
891                                 while length(.currentLine$) > 0
892                                         .separatorIndex = index_regex(.currentLine$, "[\t;]")
893                                         if .separatorIndex <= 0
894                                                 .separatorIndex = length(.currentLine$)+1
895                                         endif
896                                         .currentItem$ = left$(.currentLine$, .separatorIndex-1)
897                                         .currentLine$ = right$(.currentLine$, length(.currentLine$) - .separatorIndex )
898                                         if index_regex(.currentItem$, ".(spx|flac|wav|mp3)")
899                                                 # Audio
900                                                 .currentFile$ = .currentItem$
901                                         elsif index_regex(.currentItem$, "[a-zA-Z0-9]") > 0
902                                                 # Translation
903                                                 .currentTrans$ = .currentItem$
904                                         elsif index_regex(.currentItem$, "[^ \t\r\l]") > 0 && index_regex(.currentItem$, "[a-zA-Z0-9\-]") <= 0
905                                                 # Characters
906                                                 .currentChar$ = .currentItem$
907                                         endif
908                                 endwhile
909                         endif
910                         if .currentFile$ = "-"
911                                 if fileReadable("'.dirString$'/'.currentPinyin$'.spx")
912                                         .currentFile$ = .currentPinyin$+".spx"
913                                 elsif fileReadable("'.dirString$'/'.currentPinyin$'.flac")
914                                         .currentFile$ = .currentPinyin$+".flac"
915                                 elsif fileReadable("'.dirString$'/'.currentPinyin$'.wav")
916                                         .currentFile$ = .currentPinyin$+".wav"
917                                 elsif fileReadable("'.dirString$'/'.currentPinyin$'.mp3")
918                                         .currentFile$ = .currentPinyin$+".mp3"
919                                 endif
920                         endif
921                         select Table 'wordlist$'
922                         Append row
923                         Set string value... '.i' Pinyin '.currentPinyin$'
924                         Set string value... '.i' Sound '.currentFile$'
925                         Set string value... '.i' Character '.currentChar$'
926                         Set string value... '.i' Translation '.currentTrans$'
927                 endfor
928                 select Strings RawWordList
929                 Remove
930                 
931                 select Table 'wordlist$'
932         elsif fileReadable(.dirString$) or fileReadable("'.dirString$'/directory.txt")
933                 Create Table with column names... "'wordlistName$'" 0 Pinyin Sound
934         wordlist$ = selected$("Table")
935                 Create Strings as file list... RawWordList '.dirString$'/*
936                 .numWordStrings = Get number of strings
937                 .i = 0
938                 for .j to .numWordStrings
939                         select Strings RawWordList
940                         .currentLine$ = Get string... '.j'
941                         .currentFile$ = extractWord$(.currentLine$, "")
942                         if index_regex(.currentFile$, "\.(spx|flac|wav|mp3)")
943                                 .currentPinyin$ = left$(.currentFile$, index(.currentFile$, ".")-1)
944                                 select Table 'wordlist$'
945                                 Append row
946                                 .i += 1
947                                 Set string value... '.i' Pinyin '.currentPinyin$'
948                                 Set string value... '.i' Sound '.currentFile$'
949                         endif
950                 endfor
951                 select Strings RawWordList
952                 Remove
953                 
954                 select Table 'wordlist$'
955         elsif startsWith(.dirString$, "*call ")
956                 .callProcedure$ = right$(.dirString$, length(.dirString$)-1)
957                 '.callProcedure$'
958                 wordlist$ = selected$("Table")
959         else
960                 # Nothing, get out
961                 .numLists = 0
962                 goto EMERGENCYEXIT
963         endif
965         # Can this wordlist be deleted?
966         if fileReadable("'sgc2wordlists$'/'wordlistName$'") or fileReadable("'sgc2wordlists$'/'wordlistName$'/wordlist.txt") or fileReadable("'sgc2wordlists$'/'wordlistName$'/wordlist.Table")
967                 config.deleteWordlist = 0
968         else
969                 config.deleteWordlist = -1              
970         endif
972         # Check first column name and add Character, Translation, Sound and Show columns if missing
973         if wordlist$ <> ""
974                 select Table 'wordlist$'
975                 # Hack to correct odd behavior of tables with unicode characters
976                 .firstColumn$ = Get column label: 1
977                 if index_regex(.firstColumn$, "^[^!-~]") > 0
978                         .firstColumn$ = replace_regex$(.firstColumn$, "^[^!-~]", "", 0)
979                         Set column label (index): 1, .firstColumn$
980                 endif
981                 
982         sgc.numberOfWords = Get number of rows
983                 .characterColumn = Get column index... Character
984                 if not .characterColumn
985                         Append column... Character
986                         for .i to sgc.numberOfWords
987                                 Set string value... '.i' Character -
988                         endfor
989                 endif
990                 .translationColumn = Get column index... Translation
991                 if not .translationColumn
992                         Append column... Translation
993                         for .i to sgc.numberOfWords
994                                 Set string value... '.i' Translation -
995                         endfor
996                 endif
997                 .translationColumn = Get column index... Sound
998                 if not .translationColumn
999                         Append column... Sound
1000                         for .i to sgc.numberOfWords
1001                                 Set string value... '.i' Sound -
1002                         endfor
1003                 endif
1004                 .showColumn = Get column index... Show
1005                 if not .showColumn
1006                         Append column... Show
1007                         for .i to sgc.numberOfWords
1008                                 Set string value... '.i' Show +
1009                         endfor
1010                 endif
1011         endif
1012         
1013         # Remove all rows without Pinyin
1014         .numRows = Get number of rows
1015         for i to .numRows
1016                 .rowNum = .numRows - i + 1
1017                 .pinyinValue$ = Get value... '.rowNum' Pinyin
1018                 .currLength = Get number of rows
1019                 if not index_regex(.pinyinValue$, "^[a-zA-Z]")
1020                         if .currLength > 1
1021                                 Remove row: .rowNum
1022                         else
1023                                 Set string value: .rowNum, "Pinyin", "bu4"
1024                         endif
1025                 endif
1026         endfor
1027         .numRows = Get number of rows
1028         sgc.numberOfWords = Get number of rows
1029         
1030         # Shuffle words if requested
1031     if config.shuffleLists
1032         Randomize rows
1033     endif
1035         # Determine number of words actually shown
1036         sgc.currentWord = 0
1037         sgc.currentWordNum = 1
1038         sgc.numberOfDisplayedWords = 0  
1039         for .i to .numRows
1040                 .show$ = Get value: .i, "Show"
1041                 if .show$ = "+"
1042                         if sgc.currentWord < 1
1043                                 sgc.currentWord = .i
1044                         endif
1045                         sgc.numberOfDisplayedWords += 1
1046                 endif
1047         endfor
1048         
1049         # Clean up
1050         label EMERGENCYEXIT
1051     #select sgc.allWordLists
1052     #Remove
1053         
1054     # There were no Word Lists
1055     label NOWORDLISTS
1056     if .numLists <= 0
1057         wordlistName$ = wordlistName$+" No Word Lists available"
1058         wordlistNum = 1
1059                 Create Table with column names... "'wordlistName$'" 1 Pinyin Character Translation Sound
1060         wordlist$ = selected$("Table")
1061         .i = 0
1062                 Append row
1063                 .i += 1
1064                 Set string value... '.i' Pinyin ni3hao3
1065                 Set string value... '.i' Character 你好
1066                 Set string value... '.i' Translation hello
1067                 Set string value... '.i' Sound -
1068                 Append row
1069                 .i += 1
1070                 Set string value... '.i' Pinyin xie4xie0
1071                 Set string value... '.i' Character 谢谢
1072                 Set string value... '.i' Translation thanks
1073                 Set string value... '.i' Sound -
1074                 Append row
1075                 .i += 1
1076                 Set string value... '.i' Pinyin zai4jian4
1077                 Set string value... '.i' Character 再见
1078                 Set string value... '.i' Translation goodbye
1079                 Set string value... '.i' Sound -
1080                 # Get rid of empty first row
1081                 Remove row: 1
1082                 
1083                 # Set default values
1084         sgc.numberOfWords = Get number of rows
1085                 sgc.currentWord = 1
1086                 sgc.numberOfDisplayedWords = Get number of rows
1087     endif
1088         
1089         select Table 'wordlist$'
1090         sgc.currentWordlist = selected()
1091         call set_window_title 'buttons$' 'wordlistName$'
1092 endproc
1094 procedure read_wordlist .wordlistName$ .dirString$
1095         # Read in full tables
1096         if fileReadable("'.dirString$'/wordlist.Table")
1097                 call readTable '.dirString$'/wordlist.Table
1098                 .wordlistID = selected ()
1099         if .wordlistID > 0
1100                         Rename... '.wordlistName$'
1101                         # Praat wil change the name if it feels like it
1102                         .wordlist$ = selected$("Table")
1103                         # Add a Sound column if it is not present
1104                         .soundIndex = Get column index: "Sound"
1105                         if .soundIndex <= 0
1106                                 Append column: "Sound"
1107                         endif
1108                 else
1109                         goto EMERGENCYEXITWL
1110                 endif
1111         # Handle (legacy) simple word lists
1112         elsif fileReadable("'.dirString$'/wordlist.txt")
1113                 .wordlistID = Create Table with column names... "'.wordlistName$'" 0 Pinyin Character Sound Translation
1114                 .wordlist$ = selected$("Table")
1115                 Read Strings from raw text file... '.dirString$'/wordlist.txt
1116                 Rename... RawWordList
1117                 .numWordStrings = Get number of strings
1118                 for .i to .numWordStrings
1119                         select Strings RawWordList
1120                         .currentFile$ = "-"
1121                         .currentChar$ = "-"
1122                         .currentTrans$ = "-"
1123                         .currentLine$ = Get string... '.i'
1124                         # Remove leading whitespace
1125                         .currentLine$ = replace_regex$(.currentLine$, "^[ \t]+", "", 0)
1126                         .separatorIndex = index_regex(.currentLine$, "[ \t;\-]")
1127                         if .separatorIndex <= 0
1128                                 .separatorIndex = length(.currentLine$) + 1
1129                         endif
1130                         .currentPinyin$ = left$(.currentLine$, .separatorIndex-1)
1131                         .currentLine$ = right$(.currentLine$, length(.currentLine$) - .separatorIndex)
1132                         # There is more on the line, but we do not know what
1133                         if length(.currentLine$) > 0
1134                                 while length(.currentLine$) > 0
1135                                         .separatorIndex = index_regex(.currentLine$, "[\t;]")
1136                                         if .separatorIndex <= 0
1137                                                 .separatorIndex = length(.currentLine$)+1
1138                                         endif
1139                                         .currentItem$ = left$(.currentLine$, .separatorIndex-1)
1140                                         .currentLine$ = right$(.currentLine$, length(.currentLine$) - .separatorIndex )
1141                                         if index_regex(.currentItem$, ".(spx|flac|wav|mp3)")
1142                                                 # Audio
1143                                                 .currentFile$ = .currentItem$
1144                                         elsif index_regex(.currentItem$, "[a-zA-Z0-9]") > 0
1145                                                 # Translation
1146                                                 .currentTrans$ = .currentItem$
1147                                         elsif index_regex(.currentItem$, "[^ \t\r\l]") > 0 && index_regex(.currentItem$, "[a-zA-Z0-9\-]") <= 0
1148                                                 # Characters
1149                                                 .currentChar$ = .currentItem$
1150                                         endif
1151                                 endwhile
1152                         endif
1153                         if .currentFile$ = "-"
1154                                 if fileReadable("'.dirString$'/'.currentPinyin$'.spx")
1155                                         .currentFile$ = .currentPinyin$+".spx"
1156                                 elsif fileReadable("'.dirString$'/'.currentPinyin$'.flac")
1157                                         .currentFile$ = .currentPinyin$+".flac"
1158                                 elsif fileReadable("'.dirString$'/'.currentPinyin$'.wav")
1159                                         .currentFile$ = .currentPinyin$+".wav"
1160                                 elsif fileReadable("'.dirString$'/'.currentPinyin$'.mp3")
1161                                         .currentFile$ = .currentPinyin$+".mp3"
1162                                 endif
1163                         endif
1164                         select .wordlistID
1165                         Append row
1166                         Set string value... '.i' Pinyin '.currentPinyin$'
1167                         Set string value... '.i' Sound '.currentFile$'
1168                         Set string value... '.i' Character '.currentChar$'
1169                         Set string value... '.i' Translation '.currentTrans$'
1170                 endfor
1171                 select Strings RawWordList
1172                 Remove
1173                 
1174                 select .wordlistID
1175         elsif fileReadable(.dirString$) or fileReadable("'.dirString$'/directory.txt")
1176                 .wordlistID = Create Table with column names... "'.wordlistName$'" 0 Pinyin Sound
1177         .wordlist$ = selected$("Table")
1178                 .tmp = Create Strings as file list... RawWordList '.dirString$'/*
1179                 .numWordStrings = Get number of strings
1180                 .i = 0
1181                 for .j to .numWordStrings
1182                         select .tmp
1183                         .currentLine$ = Get string... '.j'
1184                         .currentFile$ = extractWord$(.currentLine$, "")
1185                         if index_regex(.currentFile$, "\.(spx|flac|wav|mp3)")
1186                                 .currentPinyin$ = left$(.currentFile$, index(.currentFile$, ".")-1)
1187                                 select sgc.currentWordlist
1188                                 Append row
1189                                 .i += 1
1190                                 Set string value... '.i' Pinyin '.currentPinyin$'
1191                                 Set string value... '.i' Sound '.currentFile$'
1192                         endif
1193                 endfor
1194                 select .tmp
1195                 Remove
1196                 
1197                 select sgc.currentWordlist
1198         elsif startsWith(.dirString$, "*call ")
1199                 .callProcedure$ = right$(.dirString$, length(.dirString$)-1)
1200                 '.callProcedure$'
1201                 .wordlistID = selected()
1202                 .wordlist$ = selected$("Table")
1203         else
1204                 # Nothing, get out
1205                 goto EMERGENCYEXITWL
1206         endif
1207         
1208         # Add the path to the file Sound file names
1209         select .wordlistID
1210         .numRows = Get number of rows
1211         for .w to .numRows
1212                 .soundfile$ = Get value: .w, "Sound"
1213                 if index_regex(.soundfile$, "[/\\]") <= 0
1214                         if index_regex(.dirString$, "[/\\]$")
1215                                 .soundfile$ = .dirString$+.soundfile$
1216                         elsif index_regex(.dirString$, "[\\]")
1217                                 .soundfile$ = .dirString$+"\\"+.soundfile$
1218                         else
1219                                 .soundfile$ = .dirString$+"/"+.soundfile$
1220                         endif
1221                         Set string value: .w, "Sound", .soundfile$ 
1222                 endif
1223         endfor
1224         
1225         label EMERGENCYEXITWL
1227 endproc
1229 procedure merge_into_wordlist .newTableID .lessonPostfix$
1230         select sgc.currentWordlist
1231         .oldLessonColumn = Get column index: "Lesson"
1233         select .newTableID
1234         .numRows = Get number of rows
1235         .lessonColumn = Get column index: "Lesson"
1236         .characterColumn = Get column index: "Character"
1237         .soundColumn = Get column index: "Sound"
1238         .translationColumn = Get column index: "Translation"
1239         for .w to .numRows
1240                 .currentFile$ = "-"
1241                 .currentChar$ = "-"
1242                 .currentTrans$ = "-"
1243                 .currentLesson$ = "-"
1244                 
1245                 select .newTableID
1246                 .currentPinyin$ = Get value: .w, "Pinyin"
1247                 if .soundColumn > 0
1248                         .currentFile$ = Get value: .w, "Sound"
1249                 endif
1250                 if .characterColumn > 0
1251                         .currentChar$ = Get value: .w, "Character"
1252                 endif
1253                 if .translationColumn > 0
1254                         .currentTrans$ = Get value: .w, "Translation"
1255                 endif
1256                 if .lessonColumn > 0
1257                         .currentLesson$ = Get value: .w, "Lesson"
1258                 endif
1259                 select sgc.currentWordlist
1260                 Append row
1261                 .n = Get number of rows
1262                 Set string value: .n, "Pinyin", .currentPinyin$
1263                 Set string value: .n, "Sound", .currentFile$
1264                 Set string value: .n, "Character", .currentChar$
1265                 Set string value: .n, "Translation", .currentTrans$
1266                 if .oldLessonColumn > 0
1267                         if .currentLesson$ = "-" or .currentLesson$ = "" or .currentLesson$ = "?"
1268                                 Set string value: .n, "Lesson", .lessonPostfix$
1269                         else
1270                                 Set string value: .n, "Lesson", .currentLesson$+" "+.lessonPostfix$
1271                         endif
1272                 endif
1273         endfor
1274 endproc
1276 procedure next_word
1277         if wordlist$ <> ""
1278                 select sgc.currentWordlist
1279                 .showCurrent$ = "-"
1280                 if sgc.currentWord < 0 or sgc.currentWord > sgc.numberOfWords
1281                 if config.shuffleLists
1282                             Randomize rows
1283                 endif
1284                         sgc.currentWord = 0
1285                         sgc.currentWordNum = 0
1286                 endif
1287                 while .showCurrent$ = "-" and sgc.currentWord <= sgc.numberOfWords
1288                         sgc.currentWord += 1
1289                         if sgc.currentWord <= sgc.numberOfWords
1290                                 .showCurrent$ = Get value... 'sgc.currentWord' Show
1291                         endif
1292                 endwhile
1293                 if sgc.currentWord > 0
1294                         sgc.currentWordNum += 1
1295                 else
1296                         sgc.currentWordNum = 0
1297                 endif
1298         endif
1299 endproc
1301 procedure previous_word
1302         if wordlist$ <> ""
1303                 select sgc.currentWordlist
1304                 .showCurrent$ = "-"
1305                 if sgc.currentWord <= 0
1306                 if config.shuffleLists
1307                             Randomize rows
1308                 endif
1309                         sgc.currentWord = sgc.numberOfWords + 1
1310                         sgc.currentWordNum = sgc.numberOfDisplayedWords + 1
1311                 endif
1312                 while .showCurrent$ = "-" and sgc.currentWord > 0
1313                         sgc.currentWord -= 1
1314                         if sgc.currentWord > 0
1315                                 .showCurrent$ = Get value... 'sgc.currentWord' Show
1316                         endif
1317                 endwhile
1318                 if sgc.currentWord > 0
1319                         sgc.currentWordNum -= 1
1320                 else
1321                         sgc.currentWordNum = 0
1322                 endif
1323         endif
1324 endproc
1326 procedure display_word_list_name
1327     .xtext = 50
1328     .ytext = 12
1329    call reset_viewport
1330     .displayWordList$ = replace_regex$(wordlistName$, "[_]", " ", 0)
1331     call wipeArea 'wipeWordlistArea$'
1332         call adjustFontSizeOnHeight 'defaultFont$' 'defaultFontSize' 5
1333         .currentFontSize = adjustFontSizeOnHeight.newFontSize
1335     demo Blue
1336         demo Text special... '.xtext' Centre '.ytext' Bottom 'defaultFont$' '.currentFontSize' 0 '.displayWordList$'
1337     demo Black
1338         demoShow()
1339         call set_font_size 'defaultFontSize'
1340 endproc
1342 procedure write_word_list
1343         # Write current Pinyin text
1344         call display_text Black
1345         
1346         # Write the current word list name
1347         call display_word_list_name
1348 endproc
1350 procedure paint_saveAudio_light
1351     select Table 'config$'
1352     .row = Search column... Label SaveAudio
1353         if .row < 1
1354                 exit Button Table Config does not have a row with label SaveAudio
1355         endif
1356         # Get button values
1357     .leftX = Get value... '.row' LeftX
1358     .rightX = Get value... '.row' RightX
1359     .lowY = Get value... '.row' LowY
1360     .highY = Get value... '.row' HighY
1361     .buttonColor$ = Get value... '.row' Color
1362     # The button text and symbol
1363         .horWC = demo Horizontal mm to wc... 10.0
1364         .verWC = demo Vertical mm to wc... 10.0
1365         if .verWC > 0
1366                 .verCoeff = .horWC / .verWC
1367         else
1368                 .verCoeff = 1
1369         endif
1371     .centerX = (.leftX + .rightX)/2
1372     .centerY = .lowY + 0.6*(.highY-.lowY)
1373     .radius = min(.verCoeff * (.highY - .lowY ), (.rightX - .leftX))/3
1374     .wipeRadius = 1.1*.radius
1375     call DrawSaveAudio White '.centerX' '.centerY' '.wipeRadius'
1377     if sgc.saveAudioOn and sgc.saveAudio$ <> ""
1378                 call DrawSaveAudio "DarkBlue" '.centerX' '.centerY' '.radius'
1379     endif
1380     demoShow()
1381 endproc
1383 # Uninstall word lists
1384 procedure removeWordlist .deletedWordlistName$
1385     .targetDir$ = ""
1386     if fileReadable("'sgc2wordlists$'/'.deletedWordlistName$'") or fileReadable("'sgc2wordlists$'/'.deletedWordlistName$'/wordlist.txt") or fileReadable("'sgc2wordlists$'/'.deletedWordlistName$'/wordlist.Table")
1387         .targetDir$ = "'sgc2wordlists$'/'.deletedWordlistName$'"
1388     endif
1389         if .targetDir$ <> ""
1390         Create Strings as file list... DeleteList '.targetDir$'
1391         .numdeleteFiles = Get number of strings
1392         for .i to .numdeleteFiles
1393                 .file$ = Get string... '.i'
1394                 deleteFile("'.targetDir$'/'.file$'")
1395         endfor
1396         filedelete '.targetDir$'
1397         # Check whether the directory was actually deleted. Make sure this is a valid directory name!
1398         # That is, it does not contain funny characters, nor funny names
1399         if index_regex(.deletedWordlistName$, "[^a-zA-Z0-9_ .\-]") <= 0 and index(.deletedWordlistName$, "..") <= 0 and index_regex(.deletedWordlistName$, "[a-zA-Z]") > 0
1400                         if windows
1401                                 nocheck system rmdir "'.targetDir$'" /s /q
1402                         elsif fileReadable(.targetDir$)
1403                                 system bash -rc -- 'rm -r -- "'.targetDir$'"'
1404                         endif
1405                 endif
1406                 # Remove deleted word list
1407                 select Strings DeleteList
1408                 plus Table 'wordlist$'
1409                 Remove
1410                 wordlist$ = ""
1411         endif
1412 endproc
1414 # Install word lists
1415 procedure sgc2wordlist .sourceDir$
1416         if startsWith(.sourceDir$, "preferencesDirectory$")
1417                 .sourceDir$ = replace$(.sourceDir$, "preferencesDirectory$", preferencesDirectory$)
1418         endif
1420         .targetDirectory$ = "'sgc2wordlists$'"
1421         if  fileReadable(.sourceDir$) or fileReadable("'.sourceDir$'/directory.txt")
1422                 Create Strings as file list... PackageList '.sourceDir$'/*.sgc
1423                 .numFiles = Get number of strings
1424                 for .i to .numFiles
1425                         select Strings PackageList
1426                         .file$ = Get string... '.i'
1427                         call readWordlist '.sourceDir$' '.file$'
1428                 endfor
1430                 select Strings PackageList
1431                 Remove
1432         endif
1433 endproc
1435 # Debuggin remarks!!!
1436 # fileReadable(<directory>) does not work in Windows
1437 # The file paths / -> \\ must be performed on the filenames in Windows before the system_nocheck is given
1438 # And yet only the 7z decompression has been implemented
1439 windowsUnzipCommand$ = ""
1440 if fileReadable("C:\Program Files\7-Zip\7z.exe")
1441         windowsUnzipCommand$ = """C:\Program Files\7-Zip\7z.exe"" -y e"
1442         windowsUnzipDestDir$ = " -o"
1443 elsif fileReadable("C:\Program Files\WinZip\winzip32.exe")
1444         # !!! Find a way to include the output folder !!!
1445         windowsUnzipCommand$ = "C:\Program Files\WinZip\winzip32.exe"" -min -e -o -j "
1446         windowsUnzipDestDir$ = ""
1447 endif
1448 procedure readWordlist .sourceDir$ .file$
1449         # No use doing anything if the source does not exist
1450         if fileReadable("'.sourceDir$'/'.file$'") or fileReadable("'.sourceDir$'/'.file$'/wordlist.txt") or fileReadable("'.sourceDir$'/'.file$'/wordlist.Table")
1451                 # What will be the target wordlist directory?
1452                 .targetDirectory$ = "'sgc2wordlists$'"
1453                 .dirname$ = left$(.file$, rindex(.file$, ".")-1)
1454                 if .dirname$ = ""
1455                         .dirname$ = .file$
1456                 endif
1457                 .wordlistDirectory$ = .targetDirectory$+"/"+.dirname$
1458                 # Wordlist directory does not exist, neither locally nor in the preferences
1459                 .wordListExists = 0
1460                 .tmpDirs = nocheck Create Strings as directory list... TMPWORDLISTS '.targetDirectory$'/
1461                 if .tmpDirs != undefined and .tmpDirs > 0
1462                         .numDirs = Get number of strings
1463                         for .d to .numDirs
1464                                 select .tmpDirs
1465                                 .currentString$ = Get string... '.d'
1466                                 if .currentString$ = .dirname$
1467                                         .wordListExists = 1
1468                                 endif
1469                         endfor
1470                         Remove
1471                 endif
1472                 if not (.wordListExists or fileReadable(.dirname$) or fileReadable("'.dirname$'/directory.txt") or fileReadable(.wordlistDirectory$) or fileReadable("'.wordlistDirectory$'/directory.txt"))
1473                         .wasWordList = 0
1474                         # Move source to destination
1475                         # Use this if you want to allow direct loading of ZIP files. Could lead to problems.
1476                         # if index(.file$, ".sgc") or index(.file$, ".zip")
1477                         # 
1478                         if index(.file$, ".sgc")
1479                                 if index_regex(.wordlistDirectory$, "[^a-zA-Z0-9_\.\- /~\:]") <= 0 and index_regex(.dirname$, "[^a-zA-Z0-9_\.\- ]") <= 0 and not (windows and windowsUnzipCommand$ = "")
1480                                         # Create wordlist directory
1481                                         createDirectory(.wordlistDirectory$)
1482                                         .wasWordList = 1
1483                                         if macintosh or unix
1484                                                 system bash -rc -- 'unzip -j "'.sourceDir$'/'.file$'" -d "'.wordlistDirectory$'"'
1485                                         elsif windows and windowsUnzipCommand$ <> ""
1486                                                 .winWordListDirectory$ = replace_regex$(.wordlistDirectory$, "/", "\\", 0)
1487                                                 .winSourceDirectory$ = replace_regex$("'.sourceDir$'\'.file$'", "/", "\\", 0)
1488                                                 system mkdir "'.winWordListDirectory$'" & 'windowsUnzipCommand$' "'.winSourceDirectory$'" 'windowsUnzipDestDir$'"'.winWordListDirectory$'"
1489                                         endif
1490                                 elsif (windows and windowsUnzipCommand$ = "")
1491                                         # Warn to install 7Zip
1492                                         call get_feedback_text 'config.language$' InstallUnzip
1493                                         call convert_praat_to_latin1 'get_feedback_text.text$'
1494                                         .zipText$ = convert_praat_to_latin1.text$
1495                                         call write_text_popup 'defaultFont$' 14 '.zipText$'
1496                                         # Wait for confirmation
1497                                         demoWaitForInput()
1498                                         call Draw_config_page
1499                                 endif
1500                                 # Remove if not valid!
1501                                 if fileReadable("'.wordlistDirectory$'/wordlist.Table") or fileReadable("'.wordlistDirectory$'/wordlist.txt") or fileReadable("'.wordlistDirectory$'/LICENSE.txt")
1502                                         if fileReadable("'.wordlistDirectory$'/wordlist.Table")
1503                                                 call readTable '.sourceDir$'/'.file$'
1504                                                 if readTable.tableID > 0
1505                                                         select readTable.tableID
1506                                                         # Hack around odd behavior of column index
1507                                                         .pinyinCol = 0
1508                                                         .firstColumn$ = Get column label: 1
1509                                                         .firstColumn$ = replace_regex$(.firstColumn$, "^[^!-~]", "", 0)
1510                                                         if .firstColumn$ = "Pinyin"
1511                                                                 .pinyinCol = 1
1512                                                         else
1513                                                                 .pinyinCol = Get column index... Pinyin
1514                                                         endif
1515                                                         Remove
1516                                                         # No Pinyin in table
1517                                                         if .pinyinCol <= 0
1518                                                                 .wasWordList = 0
1519                                                         endif
1520                                                 else
1521                                                         .wasWordList = 0
1522                                                 endif
1523                                         endif
1524                                 else
1525                                         # None of wordlist.Table, wordlist.txt, nor LICENSE.txt
1526                                         .wasWordList = 0
1527                                 endif
1528                                 ### REALLY DANGEROUS STUFF, SHOULD BE HANDLED BETTER
1529                                 if .wasWordList = 0
1530                                         # Remove newly created directory
1531                                         if index_regex(.wordlistDirectory$, "[^a-zA-Z0-9_\.\- /~\:]") <= 0 and index_regex(.dirname$, "[^a-zA-Z0-9_\.\- ]") <= 0 and index(.dirname$, "..") <= 0
1532                                                 if macintosh or unix
1533                                                         system bash -rc -- 'rm -r "'.wordlistDirectory$'/"'
1534                                                 elsif windows
1535                                                         system rmdir /Q /S "'.winWordListDirectory$'/"
1536                                                 endif
1537                                         endif
1538                                 endif
1539                         elsif index_regex(.file$, "\.(Table|tsv|csv)")
1540                                 # Check whether this is a valid table
1541                                 call readTable '.sourceDir$'/'.file$'
1542                                 if readTable.tableID > 0
1543                                         select readTable.tableID
1544                                         # Hack around odd behavior of column index
1545                                         .pinyinCol = 0
1546                                         .firstColumn$ = Get column label: 1
1547                                         .firstColumn$ = replace_regex$(.firstColumn$, "^[^!-~]", "", 0)
1548                                         if .firstColumn$ = "Pinyin"
1549                                                 .pinyinCol = 1
1550                                         else
1551                                                 .pinyinCol = Get column index... Pinyin
1552                                         endif
1553                                         
1554                                         if .pinyinCol > 0
1555                                                 select readTable.tableID
1556                                                 # Create wordlist directory
1557                                                 createDirectory(.wordlistDirectory$)
1558                                                 .wasWordList = 1
1559                                                 .extension$ = "Table"
1560                                                 .wordlistFilePath$ = "'.wordlistDirectory$'/wordlist.'.extension$'"
1561                                                 if windows
1562                                                         .wordlistFilePath$ = replace_regex$(.wordlistFilePath$, "/", "\\", 0)
1563                                                 endif
1564                                                 # Save
1565                                                 select readTable.tableID
1566                                                 Save as tab-separated file: .wordlistFilePath$
1567                                                 if not fileReadable(.wordlistFilePath$)
1568                                                         .wasWordList = 0
1569                                                 endif
1570                                         endif
1571                                         select readTable.tableID
1572                                         Remove
1573                                 endif
1574                         elsif index_regex(.file$, "\.(?itxt)")
1575                                 # Check whether this is a valid table
1576                                 readTable.tableID = nocheck Read Strings from raw text file: "'.sourceDir$'/'.file$'"
1577                                 if readTable.tableID > 0
1578                                         .wasWordList = 1
1579                                         # Create wordlist directory
1580                                         createDirectory(.wordlistDirectory$)
1581                                         .extension$ = "txt"
1582                                         .wordlistFilePath$ = "'.wordlistDirectory$'/wordlist.'.extension$'"
1583                                         if windows
1584                                                 .wordlistFilePath$ = replace_regex$(.wordlistFilePath$, "/", "\\", 0)
1585                                         endif
1586                                         select readTable.tableID
1587                                         Save as raw text file: .wordlistFilePath$
1588                                         select readTable.tableID
1589                                         Remove
1590                                 endif
1591                         elsif fileReadable("'.sourceDir$'/'.file$'/wordlist.Table") or fileReadable("'.sourceDir$'/'.file$'/wordlist.txt") or fileReadable("'.sourceDir$'/'.file$'/LICENSE.txt")
1592                                 # Copy wordlist directory
1593                                 if index_regex(.file$, "[^a-zA-Z0-9_\.\- ]") <= 0
1594                                         .wasWordList = 1
1595                                         if macintosh or unix
1596                                                 system bash -rc -- 'cp -r "'.sourceDir$'/'.file$'" "'.wordlistDirectory$'"'
1597                                         elsif windows
1598                                                 createDirectory(.wordlistDirectory$)
1599                                                 .winWordListDirectory$ = replace_regex$(.wordlistDirectory$, "/", "\\", 0)
1600                                                 .winSourceDirectory$ = replace_regex$("'.sourceDir$'\'.file$'", "/", "\\", 0)
1601                                                 system xcopy "'.winSourceDirectory$'" "'.winWordListDirectory$'" /s /e /q
1602                                         endif
1603                                 endif
1604                         endif
1605                         
1606                         # Set current word list to read list
1607                         if .wasWordList
1608                                 wordlistName$ = .dirname$
1609                         else
1610                                 .table$ = "Config"
1611                                 .label$ = "!NotAWordlist"
1613                                 # Get help text
1614                                 call findLabel '.table$' '.label$'
1615                                 .row = findLabel.row
1616                                 select Table '.table$'
1617                                 .helpText$ = Get value... '.row' Helptext
1618                                 .printablePath$ = "'.sourceDir$'/'.file$'"
1619                                 if windows
1620                                         .printablePath$ = replace$("'.sourceDir$'\'.file$'", "\", "\bs", 0)
1621                                 endif
1622                                 .filetext$ = replace_regex$(.printablePath$, "_", "\\_ ", 0)
1623                         call write_text_popup 'defaultFont$' 14 '.helpText$' "'.filetext$'"
1624                                 # Wait for confirmation
1625                                 demoWaitForInput()
1626                         endif
1627                 endif
1628         endif
1629 endproc