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