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