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