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