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