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