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