Ran tables2scripts.praat
[sgc2.git] / Config.praat
blob47bf667a6af6e0085673072d95e42794667a33a5
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
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 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 '.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 DrawLogging .color$ .x .y .size
77     .size /= 2
78         .y += .size
79     if .color$ = "Blue"
80         .color$ = "{0.5,0.5,1}"
81      else
82         .color$ = "Blue"
83      endif
84     demo Paint circle... '.color$' '.x' '.y' '.size'
85 endproc
87 ###############################################################
89 # Obligatory button Drawing Routines
90
91 # These MUST be defined
93 ###############################################################
95 procedure DrawReturn .color$ .x .y .size
96     call DrawConfig '.color$' '.x' '.y' '.size'
97 endproc
99 # Set the correct button states after redrawing the window
100 procedure setConfigButtons
101     # Handle logging buttons with forced button draw
102     if fileReadable("'preferencesLogDir$'/logPerformance.txt")
103         call Draw_button 'config$' +!Logging 'config.logPerformance'
104     endif
105 endproc
107 ###############################################################
109 # Button Processing Routines
111 ###############################################################
113 procedure processConfigShuffleLists .clickX .clickY .pressed$
114         .table$ = "Config"
115         .label$ = "ShuffleLists"
116         config.shuffleLists = not config.shuffleLists
117         .displayButton = 2*config.shuffleLists
118     call Draw_button '.table$' '.label$' '.displayButton'
119 endproc
121 procedure processConfigUseSoundExample .clickX .clickY .pressed$
122         .table$ = "Config"
123         .label$ = "UseSoundExample"
124         config.useSoundExample = not config.useSoundExample
125         .displayButton = 2*config.useSoundExample
126     call Draw_button '.table$' '.label$' '.displayButton'
127 endproc
128         
129 procedure processConfigSynthesis .tts$ .clickX .clickY .pressed$
130         .table$ = "Config"
131         .label$ = "Synthesis_'.tts$'"
132         if speakCommandFile$ <> "" and fileReadable(speakCommandFile$)
133                 if config.synthesis$ = ""
134                         config.synthesis$ = .tts$
135                         .displayButton = 2
136                 else
137                         config.synthesis$ = ""
138                         .displayButton = 0
139                 endif
140         else
141                 config.synthesis$ = "_DISABLED_"
142                 .displayButton = 1
143         endif
144     call Draw_button '.table$' '.label$' '.displayButton'
145 endproc
146         
147 procedure processConfigStrict .clickX .clickY .pressed$
148         .table$ = "Config"
149         .label$ = "Strict"
150         config.strict = not config.strict
151         if config.strict > 0
152                 precision = 3
153                 .displayButton = 2
154         else
155                 precision = 1.5
156                 .displayButton = 0
157         endif
158     call Draw_button '.table$' '.label$' '.displayButton'
159 endproc
161 procedure processConfigDisplayPinyin .clickX .clickY .pressed$
162         .table$ = "Config"
163         .label$ = "DisplayPinyin"
164         config.displayPinyin = not config.displayPinyin
165         .displayButton = 2*config.displayPinyin
166     call Draw_button '.table$' '.label$' '.displayButton'
167 endproc
168                 
169 procedure processConfigDisplayChar .clickX .clickY .pressed$
170         .table$ = "Config"
171         .label$ = "DisplayChar"
172         config.displayChar = not config.displayChar
173         .displayButton = 2*config.displayChar
174     call Draw_button 'table$' '.label$' '.displayButton'
175 endproc
177 procedure processConfigDisplayTrans .clickX .clickY .pressed$
178         .table$ = "Config"
179         .label$ = "DisplayTrans"
180         config.displayTrans = not config.displayTrans
181         .displayButton = 2*config.displayTrans
182     call Draw_button 'table$' '.label$' '.displayButton'
183 endproc
185 procedure processConfigDisplayNumbers .clickX .clickY .pressed$
186         .table$ = "Config"
187         .label$ = "DisplayNumbers"
188         config.displayNumbers = not config.displayNumbers
189         .displayButton = 2*config.displayNumbers
190     call Draw_button 'table$' '.label$' '.displayButton'
191 endproc
193 procedure processConfigLanguage .language$ .clickX .clickY .pressed$
194         .table$ = "Config"
195         .label$ = "Language_'.language$'"
196         call processLanguageCodes '.table$' '.label$'
197 endproc
198         
199 procedure processConfigShowBackground .clickX .clickY .pressed$
200         .table$ = "Config"
201         .label$ = "ShowBackground"
202         config.showBackground = not config.showBackground
203         .displayButton = 2*config.showBackground
204     call Draw_button 'table$' '.label$' '.displayButton'
205 endproc
207 procedure processConfigInput .input$ .clickX .clickY .pressed$
208         .table$ = "Config"
209         .label$ = "Input_'.input$'"
210     call Draw_button '.table$' Input_'config.input$' 0
211         config.input$ = .input$
212     call Draw_button '.table$' Input_'config.input$' 2
213 endproc
215 procedure processConfigRegister .register .clickX .clickY .pressed$
216         .table$ = "Config"
217         .label$ = "Register_'.register'"
218         call setRegisterFromLabel '.table$' '.label$'
219 endproc
220         
221 procedure setRegisterFromLabel .table$ .label$
222     call Draw_button '.table$' Register_'config.register' 0
223     call Draw_button '.table$' '.label$' 2
224     # Someone might have to use more than 3 chars for the config.register code
225     .numChars = length(.label$) - length("Register_")
226         .registerText$ = right$(.label$, .numChars)
227         config.register = '.registerText$'
228 endproc
230 procedure processConfigDeleteWordlist .clickX .clickY .pressed$
231         .table$ = "Config"
232         .label$ = "DeleteWordlist"
234     # Do not process undeletable word lists, only those stored in the 
235     # preferencesDirectory$ can be deleted
236     if fileReadable("'preferencesDirectory$'/sgc2/wordlists/'wordlistName$'")
237         call findLabel '.table$' !'.label$'
238         if findLabel.row > 0
239         select Table '.table$'
240         .alertText$ = Get value... 'findLabel.row' Text
241         .confirmKey$ = Get value... 'findLabel.row' Key
242         .popupText$ = Get value... 'findLabel.row' Helptext
243         else
244             exit Cannot find delete directive: '.table$' !'.label$'
245         endif
246         call write_text_popup 'defaultFont$' 14 '.popupText$'
247         call Draw_button '.table$' '.label$' 2
248         alertText$ = .alertText$
249         call Draw_button '.table$' '.label$' 3
250         alertText$ = ""
251         
252                 # Wait for confirmation
253                 demoWaitForInput()
254         if demoInput(.confirmKey$)
255                 .deleteWordListDir$ = wordlistName$
256                 call load_word_list "'localWordlistDir$'" 1
257                 call removeWordlist '.deleteWordListDir$'
258         endif
259                 call Draw_button '.table$' '.label$' 0
260                 call Draw_config_page
261     endif
262 endproc
264 wordlistTag$ = "Wordlist name"        
265 procedure processConfigInstallWordlist .clickX .clickY .pressed$
266         .table$ = "Config"
267         .label$ = "InstallWordlist"
268     call Draw_button '.table$' '.label$' 1
269         if windows
270                 # Do not use the automatic sgc list option, ask for a wordlist NAME
271                 # Get help text
272                 call findLabel '.table$' '.label$'
273                 .row = findLabel.row
274                 select Table '.table$'
275                 .openDialogue$ = Get value... '.row' Helptext
276                 call convert_praat_to_utf8 '.openDialogue$'
277                 .openDialogue$ = convert_praat_to_utf8.text$
278                 .wordlistButton$ = replace$(wordlistTag$, " ", "_", 0)
279                 .wordlistButton$ = replace_regex$(.wordlistButton$, "^.", "\l&", 0)
280                 beginPause(.openDialogue$)
281                         sentence (wordlistTag$, "")
282                 clicked = endPause ("Cancel", "Open", 2)
283                 .wordlist_Name$ = ""
284                 if clicked = 2
285                         .wordlist_Name$ = '.wordlistButton$'$
286                 endif
287                 call install_wordlists_by_name '.wordlist_Name$'
288         else
289         call sgc2wordlist 'homeDirectory$'
290         call sgc2wordlist 'homeDirectory$'/Downloads
291         call sgc2wordlist 'homeDirectory$'/Documents
292         call sgc2wordlist 'homeDirectory$'/My Documents
293         call sgc2wordlist 'homeDirectory$'/My Documents/Downloads
294         call sgc2wordlist 'homeDirectory$'/Desktop
295         call sgc2wordlist 'preferencesDirectory$'/sgc2
296         endif
297         call load_word_list "'localWordlistDir$'" 0
298     call Draw_button '.table$' '.label$' 0
299         call Draw_config_page
300 endproc
302 procedure processConfigOpenWordlist .clickX .clickY .pressed$
303         .table$ = "Config"
304         .label$ = "OpenWordlist"
305     call Draw_button '.table$' '.label$' 1
307         # Get help text
308         call findLabel '.table$' '.label$'
309         .row = findLabel.row
310         select Table '.table$'
311         .openDialogue$ = Get value... '.row' Helptext
312         call convert_praat_to_utf8 '.openDialogue$'
313         .openDialogue$ = convert_praat_to_utf8.text$
315         .wordlist_Name$ = chooseReadFile$ (.openDialogue$)
316         call install_wordlists_by_name '.wordlist_Name$'
317         call load_word_list "'localWordlistDir$'" 0
318     call Draw_button '.table$' '.label$' 0
319         call Draw_config_page
320 endproc
322 ###############################################################
324 # Obligatory button Processing Routines
326 # These MUST be defined
328 ###############################################################
330 procedure processConfigReturn .clickX .clickY .pressed$
331         .table$ = "Config"
332         .label$ = "Return"
333         call Draw_button '.table$' '.label$' 1
334         call write_preferences ""
335 endproc
337 procedure processConfigRefresh .clickX .clickY .pressed$
338         .table$ = "Config"
339         .label$ = "Refresh"
340         call Draw_config_page
341 endproc
343 procedure processConfigCredits .clickX .clickY .pressed$
344         .table$ = "Config"
345         .label$ = "Credits"
346         call Draw_button '.table$' '.label$' 1
347         call write_text_table Credits_'config.language$'
348         demoWaitForInput()
349     call Draw_button '.table$' '.label$' 0
350     demo Erase all
351     call Draw_config_page
352 endproc
354 procedure processConfigHelp .clickX .clickY .pressed$
355         .table$ = "Config"
356         .label$ = "Help"
357         call help_loop  '.table$' Draw_config_page
358 endproc
360 ###############################################################
362 # Miscelaneous supporting code
364 ###############################################################
365 procedure install_wordlists_by_name .wordlist_Name$
366         if .wordlist_Name$ <> ""
367 printline '.wordlist_Name$'
368                 if index(.wordlist_Name$, "/")
369                         .sourceDir$ = left$(.wordlist_Name$, rindex(.wordlist_Name$, "/") -1)
370                         .file$ = right$(.wordlist_Name$, length(.wordlist_Name$) - rindex(.wordlist_Name$, "/"))
371                         call readWordlist "'.sourceDir$'" '.file$'
372                 else
373                         .start = 1
374                         if index(.wordlist_Name$, ".")
375                                 .start = 4
376                         endif
377                         .extension1$ = ".sgc"
378                         .extension2$ = ".Table"
379                         .extension3$ = ".txt"
380                         .extension4$ = ""
381                         for .e from .start to 4
382                                 .currentExtension$ = .extension'.e'$
383                         call readWordlist "'homeDirectory$'" '.wordlist_Name$''.currentExtension$'
384                         call readWordlist "'homeDirectory$'/Downloads" '.wordlist_Name$''.currentExtension$'
385                         call readWordlist "'homeDirectory$'/Documents" '.wordlist_Name$''.currentExtension$'
386                         call readWordlist "'homeDirectory$'/My Documents/Downloads" '.wordlist_Name$''.currentExtension$'
387                         call readWordlist "'homeDirectory$'/My Documents" '.wordlist_Name$''.currentExtension$'
388                         call readWordlist "'homeDirectory$'/Desktop" '.wordlist_Name$''.currentExtension$'
389                         call readWordlist "'preferencesDirectory$'/sgc2" '.wordlist_Name$''.currentExtension$'
390                         endfor
391                 endif
392         endif
393 endproc
395 # Word lists: It is a VERY good idea to make sure that word-lists
396 # have unique names.
397 procedure load_word_list .localdir$ .relnumber
398     # Remove old word list
399     if wordlist$ <> ""
400                 select Table 'wordlist$'
401                 Remove
402                 call wipeArea 'wipeWordlistArea$'
403                 wordlist$ = ""
404     endif
405     
406         # Create Table that will recieve the wordlists and directories
407         Create Table with column names... AllWordLists 0 Name Directory
408         .currentWordlistRow = 0
409         
410         # Start with the word lists in the distribution, unless the local directory exists!
411         if not fileReadable(.localdir$)
412                 call CreateCreateWordlists
413                 select Table CreateWordlists
414         .numLists = Get number of rows
415                 for .i to .numLists
416                         select Table CreateWordlists
417            .currentName$ = Get value... '.i' Name
418                    .currentDirectory$ = "*call Create'.currentName$'"
419                         select Table AllWordLists
420                         Append row
421                         .currentWordlistRow = Get number of rows
422                         Set string value... '.currentWordlistRow' Name '.currentName$'
423                     .currentDirectory$ = "*call Create'.currentName$'"
424                         Set string value... '.currentWordlistRow' Directory '.currentDirectory$'
425                 endfor
426                 select Table CreateWordlists
427                 Remove
428         endif
429         
430         # First the global word lists
431         if fileReadable(globalwordlists$)
432         Create Strings as directory list... WordList 'globalwordlists$'
433         .numLists = Get number of strings
434         for .i to .numLists
435             select Strings WordList
436             .currentName$ = Get string... '.i'
437                         select Table AllWordLists
438                         Append row
439                         .currentWordlistRow = Get number of rows
440                         Set string value... '.currentWordlistRow' Name '.currentName$'
441                     .currentDirectory$ = globalwordlists$+"/"+.currentName$
442                         Set string value... '.currentWordlistRow' Directory '.currentDirectory$'
443         endfor
444         select Strings WordList
445         Remove
446         endif
447         
448         # Now the preferences word lists
449         if fileReadable(sgc2wordlists$)
450         Create Strings as directory list... WordList 'sgc2wordlists$'
451         .numLists = Get number of strings
452         for .i to .numLists
453             select Strings WordList
454             .currentName$ = Get string... '.i'
455                         select Table AllWordLists
456                         Append row
457                         .currentWordlistRow = Get number of rows
458                         Set string value... '.currentWordlistRow' Name '.currentName$'
459                     .currentDirectory$ = sgc2wordlists$+"/"+.currentName$
460                         Set string value... '.currentWordlistRow' Directory '.currentDirectory$'
461         endfor
462         select Strings WordList
463         Remove
464         endif
465         
466         # Finally, the local word lists
467         if fileReadable(.localdir$)
468         Create Strings as directory list... WordList '.localdir$'
469         .numLists = Get number of strings
470         for .i to .numLists
471             select Strings WordList
472             .currentName$ = Get string... '.i'
473                         select Table AllWordLists
474                         Append row
475                         .currentWordlistRow = Get number of rows
476                         Set string value... '.currentWordlistRow' Name '.currentName$'
477                     .currentDirectory$ = .localdir$+"/"+.currentName$
478                         Set string value... '.currentWordlistRow' Directory '.currentDirectory$'
479         endfor
480         select Strings WordList
481         Remove
482     endif
484         # Get the position of the current word list
485         select Table AllWordLists
486         .currentNumber = 1
487     .numLists = Get number of rows
488         if wordlistName$ <> ""
489                 select Table AllWordLists
490                 .currentNumber = Search column... Name 'wordlistName$'
491         endif
492     wordlistNum = .currentNumber + .relnumber
493     if wordlistNum > .numLists
494         wordlistNum = 1
495         elsif wordlistNum < 1
496                 wordlistNum = .numLists
497     endif
498     select Table AllWordLists
499     wordlistName$ = Get value... 'wordlistNum' Name
500         .dirWordlistName$ = Get value... 'wordlistNum' Directory
501     .dirString$ = replace_regex$(.dirWordlistName$, "[ ]", "&", 0)
502         
503         # Read in full tables
504         if fileReadable("'.dirString$'/wordlist.Table")
505         Read from file... '.dirString$'/wordlist.Table
506         Rename... 'wordlistName$'
507         # Praat wil change the name if it feels like it
508         wordlist$ = selected$("Table")
509         # Handle (legacy) simple word lists
510         elsif fileReadable("'.dirString$'/wordlist.txt")
511                 Create Table with column names... "'wordlistName$'" 0 Pinyin Sound
512         wordlist$ = selected$("Table")
513                 Read Strings from raw text file... '.dirString$'/wordlist.txt
514                 Rename... RawWordList
515                 .numWordStrings = Get number of strings
516                 for .i to .numWordStrings
517                         select Strings RawWordList
518                         .currentLine$ = Get string... '.i'
519                         .currentPinyin$ = extractWord$(.currentLine$, "")
520                         if length(.currentLine$) > length(.currentPinyin$)+1
521                                 .currentFile$ = right$(.currentLine$, length(.currentPinyin$)+1)
522                         elsif fileReadable("'.dirString$'/'.currentPinyin$'.spx")
523                                 .currentFile$ = .currentPinyin$+".spx"
524                         elsif fileReadable("'.dirString$'/'.currentPinyin$'.flac")
525                                 .currentFile$ = .currentPinyin$+".flac"
526                         elsif fileReadable("'.dirString$'/'.currentPinyin$'.wav")
527                                 .currentFile$ = .currentPinyin$+".wav"
528                         elsif fileReadable("'.dirString$'/'.currentPinyin$'.mp3")
529                                 .currentFile$ = .currentPinyin$+".mp3"
530                         else
531                                 .currentFile$ = "-"
532                         endif
533                         select Table 'wordlist$'
534                         Append row
535                         Set string value... '.i' Pinyin '.currentPinyin$'
536                         Set string value... '.i' Sound '.currentFile$'
537                 endfor
538                 select Strings RawWordList
539                 Remove
540                 
541                 select Table 'wordlist$'
542         elsif fileReadable(.dirString$)
543                 Create Table with column names... "'wordlistName$'" 0 Pinyin Sound
544         wordlist$ = selected$("Table")
545                 Create Strings as file list... RawWordList '.dirString$'/*
546                 .numWordStrings = Get number of strings
547                 .i = 0
548                 for .j to .numWordStrings
549                         select Strings RawWordList
550                         .currentLine$ = Get string... '.j'
551                         .currentFile$ = extractWord$(.currentLine$, "")
552                         if index_regex(.currentFile$, "\.(spx|flac|wav|mp3)")
553                                 .currentPinyin$ = left$(.currentFile$, index(.currentFile$, ".")-1)
554                                 select Table 'wordlist$'
555                                 Append row
556                                 .i += 1
557                                 Set string value... '.i' Pinyin '.currentPinyin$'
558                                 Set string value... '.i' Sound '.currentFile$'
559                         endif
560                 endfor
561                 select Strings RawWordList
562                 Remove
563                 
564                 select Table 'wordlist$'
565         elsif startsWith(.dirString$, "*call ")
566                 .callProcedure$ = right$(.dirString$, length(.dirString$)-1)
567                 '.callProcedure$'
568                 wordlist$ = selected$("Table")
569         endif
570         
571         # Can this wordlist be deleted?
572         if fileReadable("'preferencesDirectory$'/sgc2/wordlists/'wordlistName$'")
573                 config.deleteWordlist = 0
574         else
575                 config.deleteWordlist = -1              
576         endif
577         
578         # Add a Character and Translation column if missing
579         select Table 'wordlist$'
580     numberOfWords = Get number of rows
581         .characterColumn = Get column index... Character
582         if not .characterColumn
583                 Append column... Character
584                 for .i to numberOfWords
585                         Set string value... '.i' Character -
586                 endfor
587         endif
588         .translationColumn = Get column index... Translation
589         if not .translationColumn
590                 Append column... Translation
591                 for .i to numberOfWords
592                         Set string value... '.i' Translation -
593                 endfor
594         endif
595         
596         # Remove all rows without Pinyin
597         .numRows = Get number of rows
598         for i to .numRows
599                 .rowNum = .numRows - i + 1
600                 .pinyinValue$ = Get value... '.rowNum' Pinyin
601                 if not index_regex(.pinyinValue$, "[a-zA-Z0-9]")
602                         Remove row... '.rowNum'
603                 endif
604         endfor
605         numberOfWords = Get number of rows
606         
607         # Shuffle words if requested
608     if config.shuffleLists
609         Randomize rows
610     endif
612         # Clean up
613     select Table AllWordLists
614     Remove
615     
616     # There were no Word Lists
617     label NOWORDLISTS
618     if .numLists <= 0
619         wordlistName$ = "No Word Lists available"
620         wordlistNum = 1
621                 Create Table with column names... "'wordlistName$'" 0 Pinyin Character Translation Sound
622         wordlist$ = selected$("Table")
623         .i = 0
624                 Append row
625                 .i += 1
626                 Set string value... '.i' Pinyin ni3hao3
627                 Set string value... '.i' Character -
628                 Set string value... '.i' Translation -
629                 Set string value... '.i' Sound -
630                 Append row
631                 .i += 1
632                 Set string value... '.i' Pinyin xie4xie0
633                 Set string value... '.i' Character -
634                 Set string value... '.i' Translation -
635                 Set string value... '.i' Sound -
636                 Append row
637                 .i += 1
638                 Set string value... '.i' Pinyin zai4jian4
639                 Set string value... '.i' Character -
640                 Set string value... '.i' Translation -
641                 Set string value... '.i' Sound -
642         numberOfWords = Get number of rows
643     endif
645         call set_window_title 'buttons$' 'wordlistName$'
646 endproc
648 procedure display_word_list_name
649     .xtext = 50
650     .ytext = 12
651    call reset_viewport
652     .displayWordList$ = replace_regex$(wordlistName$, "[_]", " ", 0)
653     call wipeArea 'wipeWordlistArea$'
654         call adjustFontSizeOnHeight 'defaultFontSize' 5
655         .currentFontSize = adjustFontSizeOnHeight.newFontSize
657     demo Blue
658         demo Text special... '.xtext' Centre '.ytext' Bottom Helvetica '.currentFontSize' 0 '.displayWordList$'
659     demo Black
660         demoShow()
661         call set_font_size 'defaultFontSize'
662 endproc
664 procedure write_word_list
665         # Draw the contour
666         call draw_tone_contour
668         # Write current Pinyin text
669         call display_text Black
670         
671         # Write the current word list name
672         call display_word_list_name
673 endproc
675 procedure start_logging
676         if fileReadable("'preferencesLogDir$'/logPerformance.txt")
677                 .logDirectory$ < 'preferencesLogDir$'/logPerformance.txt
678                 .logDirectory$ = extractWord$(.logDirectory$, "")
679                 if .logDirectory$ = "" or not fileReadable(.logDirectory$)
680                         .logDirectory$ = "'preferencesDirectory$'/sgc2/log"
681                 endif
682                 createDirectory("'.logDirectory$'/sgc2log")
683                 currentLogDirectory$ = "'.logDirectory$'/sgc2log/log'logtimeStamp$'"
684                 createDirectory(currentLogDirectory$)
685                 if not fileReadable("'currentLogDirectory$'/wordlist.Table")
686                         .headerLine$ = "Pinyin'tab$'Character'tab$'Sound'newline$'"
687                         .headerLine$ > 'currentLogDirectory$'/wordlist.Table
688                 endif
689                         # Flip switch
690                         config.logPerformance = 1
691         endif
692 endproc
694 procedure log_performance .recordedSound$ .register .precision .pinyin$ .choice$
695         # Log files
696         .currentDate$ = date$()
697         .timeStamp$ = replace_regex$(.currentDate$, "[^a-zA-Z0-9\-_]", "-", 0)
698         .choiceText$ = replace_regex$(.choice$, "[^a-zA-Z0-9\-_]", "-", 0)
700         if config.logPerformance and fileReadable("'preferencesLogDir$'/logPerformance.txt")
701            .outfilename$ = .pinyin$+"_"+.choice$+"_'.register'_"+.timeStamp$+".wav"
702            .logtext$ = .pinyin$+tab$+.choice$+tab$+"'.register'Hz"+tab$+.currentDate$+tab$+.outfilename$+newline$
703            # Plain log text
704            fileappend 'currentLogDirectory$'/logFile.txt '.logtext$'
705            # A wordlist.Table
706            fileappend 'currentLogDirectory$'/wordlist.Table '.pinyin$''tab$''.choice$''tab$''.outfilename$''newline$'
707                 # The recorded sound
708            select Sound 'recordedSound$'
709            Write to WAV file... 'currentLogDirectory$'/'.outfilename$'
710         endif
711 endproc
713 procedure paint_logging_light
714     select Table Config
715     .row = Search column... Label !Logging
716         if .row < 1
717                 exit Button Table Config does not have a row with label !Logging
718         endif
719         # Get button values
720     .leftX = Get value... '.row' LeftX
721     .rightX = Get value... '.row' RightX
722     .lowY = Get value... '.row' LowY
723     .highY = Get value... '.row' HighY
724     .buttonColor$ = Get value... '.row' Color
725     .centerX = (.leftX + .rightX)/2
726     .centerY = (.lowY + .highY)/2
727     .radius = (.highY - .lowY )/(4*2)
728     .wipeRadius = 1.1*.radius
729     if config.logPerformance and fileReadable("'preferencesLogDir$'/logPerformance.txt")
730         demo Paint circle... White '.centerX' '.centerY' '.wipeRadius'
731         demo Paint circle... '.buttonColor$' '.centerX' '.centerY' '.radius'
732         demoShow()
733     else
734         demo Paint circle... White '.centerX' '.centerY' '.wipeRadius'
735         demoShow()
736     endif
737 endproc
739 # Uninstall word lists
740 procedure removeWordlist .deletedWordlistName$
741     .targetDir$ = ""
742     if fileReadable("'preferencesDirectory$'/sgc2/wordlists/'.deletedWordlistName$'")
743         .targetDir$ = "'preferencesDirectory$'/sgc2/wordlists/'.deletedWordlistName$'"
744     endif
745     Create Strings as file list... DeleteList '.targetDir$'/*
746     .numdeleteFiles = Get number of strings
747     for .i to .numdeleteFiles
748         .file$ = Get string... '.i'
749         deleteFile("'.targetDir$'/'.file$'")
750     endfor
751     deleteFile("'.targetDir$'")
752         select Strings DeleteList
753         Remove
754 endproc
756 # Install word lists
757 procedure sgc2wordlist .sourceDir$
758         if startsWith(.sourceDir$, "preferencesDirectory$")
759                 .sourceDir$ = replace$(.sourceDir$, "preferencesDirectory$", preferencesDirectory$)
760         endif
762         .targetDirectory$ = "'preferencesDirectory$'/sgc2/wordlists"
763         if  fileReadable(.sourceDir$)
764                 Create Strings as file list... PackageList '.sourceDir$'/*.sgc
765                 .numFiles = Get number of strings
766                 for .i to .numFiles
767                         select Strings PackageList
768                         .file$ = Get string... '.i'
769                         call readWordlist '.sourceDir$' '.file$'
770                 endfor
772                 select Strings PackageList
773                 Remove
774         endif
775 endproc
777 # Debuggin remarks!!!
778 # fileReadable(<directory>) werkt niet in Windows
779 # De file paths / -> \\ must be performed on the filenames in Windows before the system_nocheck is given
780 # And yet only the 7z decompression has been implemented
781 windowsUnzipCommand$ = """C:\Program Files\7-Zip\7z"" x"
782 procedure readWordlist .sourceDir$ .file$
783         # No use doing anything if the source does not exist
784         if fileReadable("'.sourceDir$'/'.file$'")
785                 # What will be the target wordlist directory?
786                 .targetDirectory$ = "'preferencesDirectory$'/sgc2/wordlists"
787                 .dirname$ = left$(.file$, rindex(.file$, ".")-1)
788                 if .dirname$ = ""
789                         .dirname$ = .file$
790                 endif
791                 .wordlistDirectory$ = .targetDirectory$+"/"+.dirname$
792                 # Wordlist directory does not exist, neither locally nor in the preferences
793                 if not (fileReadable(.dirname$) or fileReadable(.wordlistDirectory$))
794                         .wasWordList = 0
795                         # Move source to destination
796                         if index(.file$, ".sgc")
797                                 # Create wordlist directory
798                                 createDirectory(.wordlistDirectory$)
799                                 .wasWordList = 1
800                                 if macintosh or unix
801                                         system bash -rc -- 'cp "'.sourceDir$'/'.file$'" "'.wordlistDirectory$'/"'
802                                         system cd ''.wordlistDirectory$'';bash -rc -- 'unzip "'.file$'"'
803                                 elsif windows
804                                         system copy "'.sourceDir$'/'.file$'" "'.wordlistDirectory$'/"
805                                         system chdir "'.wordlistDirectory$'" && 'windowsUnzipCommand$' "'.file$'"
806                                 endif
807                                 deleteFile("'.wordlistDirectory$'/'.file$'")
808                         elsif index_regex(.file$, "\.(Table|txt)")
809                                 # Create wordlist directory
810                                 createDirectory(.wordlistDirectory$)
811                                 .wasWordList = 1
812                                 .extension$ = replace_regex$(.file$, "^.+\.(Table|txt)$", "\1", 0)
813                                 if macintosh or unix
814                                         system bash -rc -- 'cp "'.sourceDir$'/'.file$'" "'.wordlistDirectory$'/wordlist.'.extension$'"'
815                                 elsif windows
816                                         system copy "'.sourceDir$'/'.file$'" "'.wordlistDirectory$'/wordlist.'.extension$'"
817                                 endif
818                         elsif fileReadable("'.sourceDir$'/'.file$'/wordlist.Table") or fileReadable("'.sourceDir$'/'.file$'/wordlist.txt")
819                                 # Create wordlist directory
820                                 createDirectory(.wordlistDirectory$)
821                                 .wasWordList = 1
822                                 .sourceFiles = Create Strings as file list... SourceStrings '.sourceDir$'/'.file$'/
823                                 .numFiles = Get number of strings
824                                 for .i to .numFiles
825                                         select '.sourceFiles'
826                                         .currentFile$ = Get string... '.i'
827                                         if macintosh or unix
828                                                 system bash -rc -- 'cp "'.sourceDir$'/'.file$'/'.currentFile$'" "'.wordlistDirectory$'/"'
829                                         elsif windows
830                                                 system copy "'.sourceDir$'/'.file$'/'.currentFile$'" "'.wordlistDirectory$'/"
831                                         endif
832                                 endfor
833                                 select '.sourceFiles'
834                                 Remove
835                         endif
836                         
837                         # Set current word list to read list
838                         if .wasWordList
839                                 wordlistName$ = .dirname$
840                         else
841                                 .table$ = "Config"
842                                 .label$ = "!NotAWordlist"
844                                 # Get help text
845                                 call findLabel '.table$' '.label$'
846                                 .row = findLabel.row
847                                 select Table '.table$'
848                                 .helpText$ = Get value... '.row' Helptext
849                                 .filetext$ = replace_regex$("'.sourceDir$'/'.file$'", "_", "\\_ ", 0)
850                         call write_text_popup 'defaultFont$' 14 '.helpText$' "'.filetext$'"
851                                 # Wait for confirmation
852                                 demoWaitForInput()
853                         endif
854                 endif
855         endif
856 endproc