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