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