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