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