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