Annotation of capa/capa51/GUITools/quizzer.tcl, revision 1.14

1.1       albertel    1: ###########################################################
                      2: # quizzer.tcl - 
                      3: # Copyright Guy Albertelli II 1996
                      4: ###########################################################
                      5: set gTclVer 4.0
                      6: 
                      7: ###########################################################
                      8: # createControlWindow
                      9: ###########################################################
                     10: # Creates the menu window 
                     11: ###########################################################
                     12: # Arguments: none
                     13: # Returns: nothing
                     14: ###########################################################
                     15: proc createControlWindow {} {
                     16:     global gPrefs gDate gFind gChanged gWindowMenu gStudentSelection gFile \
                     17: 	   gUniqueNumber gXdviOpt gHeaderQCount gDir gHintVal gTryVal gProbVal \
                     18:            gPutLine gQuizTemp gCapaConfig gStopPrinting gFindList gFirstTime \
                     19:            gRefChanged gChangedLast gCreateImportLinks gFasterParsing
                     20:     
                     21:     after 500 { dateUpdate }
                     22:     after 1000 { cleanWindowList }
                     23: 
                     24:     set gFasterParsing 1
                     25:     set gFirstTime 1
                     26:     set gPrefs(info) "Problem"
                     27:     set gPrefs(TeXHeader) ""
                     28:     set gPrefs(TeXFooter) ""
                     29:     set gFind(find) ""
                     30:     set gFind(replace) ""
                     31:     set gFind(findOption) "-nocase"
                     32:     set gFind(scope) File
                     33:     set gChanged 0
                     34:     set gChangedLast 0
                     35:     trace variable gChanged w updateChangeStatus
                     36:     trace variable gRefChanged w updateChangeStatus
                     37:     set gStudentSelection(type) "Random"
                     38:     set gStudentSelection(random) "1"
                     39:     set gStudentSelection(studentNumber) ""
                     40:     set gStudentSelection(studentName) ""
                     41:     set gFile ""
                     42:     set gUniqueNumber 1
                     43:     set gXdviOpt "-geometry 800x650" 
                     44:     set gHeaderQCount 0
                     45:     set gPutLine 1
                     46:     set gTryVal 99
                     47:     set gHintVal 1
                     48:     set gProbVal 1
                     49:     set gStopPrinting 0
                     50:     set gDir(class) [pwd]
                     51:     set gDir(import) [pwd]
                     52:     set gDir(include) [pwd]
                     53:     set gDir(reference) [pwd]
                     54:     set gQuizTemp "true"
                     55:     set gFindList(files) ""
                     56:     set gFindList(refNum) ""
                     57:     set gCreateImportLinks 1
                     58:     set gCapaConfig(IMP_color) #0000ff
                     59:     set gCapaConfig(comment_color) #008400
                     60:     set gCapaConfig(Printer_selected) "0"
                     61:     set gCapaConfig(latex_command) "latex"
                     62:     set gCapaConfig(qzparse_command) "qzparse"
                     63:     set gCapaConfig(dvips_command) "dvips"
                     64:     set gCapaConfig(xdvi_command) "xdvi"
                     65:     set gCapaConfig(lprOneSided_command) "lpr "
                     66:     set gCapaConfig(lprTwoSided_command) ""
                     67:     set gCapaConfig(printer_option) ""
                     68:     set gCapaConfig(standardQuizzerHeader) "//CAPA system software is copyrighted by Michigan State University.\n//By using these materials, the User agrees to:\n//1) Protect the source code files  from unauthorized copying.\n//2) Limit  access  of the source material to teaching staff.\n//3) The User is free to mix, cut and paste, modify, adapt, delete,\n//   improve, etc. the problems and graphics for his/her own use.\n//\n/IMP \"../Tools/StdMacros\"\n/IMP \"../Tools/StdUnits\"\n/IMP \"../Tools/StdConst\"\n/IMP \"HWTop\"\n"
                     69:     
                     70: 
                     71:     wm withdraw .
                     72: 
                     73:     # there is code later on that depends upon .main existing and being visable
                     74:     set menuFrame [menu .main -tearoff 0 -type tearoff ]
                     75: 
                     76:     wm title $menuFrame "Quizzer"
                     77: 
                     78:     $menuFrame post 0 0
                     79: 
                     80:     wm geometry $menuFrame "+0+20"
                     81:     $menuFrame add command -label "Quizzer" -foreground grey85 -background \
                     82: 	    black -state disabled 
                     83:     $menuFrame add command -label "Info" -command { createInfoWindow }
                     84:     $menuFrame add cascade -label "File" -menu $menuFrame.file
                     85:     #$menuFrame add cascade -label "Edit .qz" -menu $menuFrame.edit
                     86:     #$menuFrame add cascade -label "Find" -menu $menuFrame.find
                     87:     $menuFrame add command -label "Prefs" -command { createPrefsWindow }
                     88:     $menuFrame add cascade -label "Windows" -menu $menuFrame.windows
                     89:     $menuFrame add command -label "Create .dvi" -command { 
                     90: 	studentSelectWindow createDvi } -accelerator "Alt+D"
                     91:     bind all <Alt-Shift-D> "studentSelectWindow createDvi"
                     92:     $menuFrame add command -label "Analyze Set" -command { 
                     93: 	analyzeSet } -accelerator "Alt+A"
                     94:     bind all <Alt-Shift-A> "analyzeSet"
                     95:     $menuFrame add command -label "Print" -command { printWindow }
                     96:     $menuFrame add command -label "Remap" -command { createRemapWindow }
                     97:     $menuFrame add command -label "Xdvi Options" -command { createXdviOpt }
                     98:     #$menuFrame add command -label "Change font" -command { changeFont }
                     99:     $menuFrame add command -label "Quit" -command { quit } \
                    100: 	    -accelerator "Alt+q"
                    101:     bind all <Alt-q> quit
                    102:     bind $menuFrame <Destroy> "quit 1"
                    103: 
                    104:     set file  [menu $menuFrame.file -tearoff 1  ]
                    105:     set edit    [menu $menuFrame.edit -tearoff 1 ]
                    106:     #set find    [menu $menuFrame.find -tearoff 1 ]
                    107:     set windows [menu $menuFrame.windows -tearoff 1 ]
                    108:     set gWindowMenu $windows
                    109:     
                    110:     $file add command -label "Set.qz File" -foreground grey50 -background \
                    111: 	    black -state disabled     
                    112:     $file add command -label "New" -command { 
                    113: 	createEditingWindow 
                    114: 	pickCapaConfig
                    115:     } -accelerator "Alt+n"
                    116:     bind all <Alt-n> { 
                    117: 	createEditingWindow 
                    118: 	pickCapaConfig
                    119:     }
                    120:     $file add command -label "Open" -command { openDocument } -accelerator "Alt+o"
                    121:     bind all <Alt-o> openDocument
                    122:     $file add command -label "Save" -command { saveDocument } -accelerator "Alt+s"
                    123:   #  binding moved to the creation of the editwindow
                    124:   #  bind $menuFrame <Alt-s> saveDocument
                    125:     $file add command -label "Save As..." -command { saveDocument 1 } \
                    126: 	-accelerator "Alt+S"
                    127:   #  binding moved to the creation of the editwindow
                    128:   #  bind $menuFrame <Alt-Shift-s> { saveDocument 1 }
                    129:     $file add command -label "Delete" -command { deleteFile 0 } 
                    130:     $file add command -label "Close" -command { closeDocument } -accelerator "Alt+w"
                    131:   #  binding moved to the creation of the editwindow
                    132:   #  bind .main <Alt-w> closeDocument
                    133:     $file add command -label "Reference File" -foreground grey90 -background \
                    134: 	    black -state disabled     
                    135:     $file add command -label "New Reference File..." \
                    136: 	-command { newReferenceFile } -accelerator "Alt+t"
                    137:     bind all <Alt-t> openReferenceFile
                    138:     $file add command -label "Open Reference File..." \
                    139: 	-command { openReferenceFile } -accelerator "Alt+r"
                    140:     bind all <Alt-r> openReferenceFile
                    141:     $file add command -label "Open capa.config" \
                    142: 	-command { openReferenceFile capa.config } 
                    143:     
                    144:     $edit add command -label "Cut" -command { cut } -accelerator "Alt+x"
                    145:   #  binding moved to the creation of the editwindow
                    146:   #  bind $menuFrame <Alt-x> cut 
                    147:     $edit add command -label "Copy" -command { copy } -accelerator "Alt+c"
                    148:   #  binding moved to the creation of the editwindow
                    149:   #  bind $menuFrame <Alt-c> copy
                    150:     $edit add command -label "Paste" -command { paste } -accelerator "Alt+v"
                    151:   #  binding moved to the creation of the editwindow
                    152:   #  bind .main <Alt-v> paste
                    153:     $edit add command -label "Select All " -command { selectAll } \
                    154: 	    -accelerator "Alt+a"
                    155:   #  binding moved to the creation of the editwindow
                    156:   #  bind $menuFrame <Alt-a> selectAll 
                    157:     $edit add separator
                    158:     $edit add command -label "Undo" -command "undo 0" \
                    159: 	    -accelerator "Alt+u"
                    160: #    $edit add command -label "Redo" -command "redo $num"
                    161:     $edit add separator
                    162:     $edit add command -label "Find" -command { createFindWindow } \
                    163: 	-accelerator "Alt+f"
                    164:     bind all <Alt-f> createFindWindow
                    165: 
                    166: #    $find add command -label "Find Panel.." -command { createFindWindow } \
                    167: 	    -accelerator "Alt+f"
                    168: #    bind all <Alt-f> createFindWindow
                    169: #    $find add command -label "Find Next" -command { next }
                    170: #    $find add command -label "Find Previous" -command { previous }
                    171: #    $find add command -label "Enter Selecton" -command { enterSelection }
                    172: #    $find add command -label "Jump to Selection" -command { jumpToSelection }
                    173: #    $find add command -label "Line Range..." -command { createLineWindow } \
                    174: 	    -accelerator "Alt+l"
                    175: #    bind all <Alt-l> createLineWindow
                    176: 
                    177:     bind all <Alt-0> printInfo
                    178:     bind all <Alt-KeyPress> { #unbind tkTraverseToMenu 
                    179:     } 
                    180:     bind all <Control-Tab> {tkTabToWindow [tk_focusPrev %W]}
                    181:     catch {bind all <ISO_Left_Tab> {tkTabToWindow [tk_focusPrev %W]}}
                    182:     catch {bind all <KP_Tab> {tkTabToWindow [tk_focusPrev %W]}}
                    183:     trace variable gQuizTemp w "changeMenuStatus $menuFrame"
                    184: }
                    185: 
                    186: ###########################################################
                    187: # changeMenuStatus
                    188: ###########################################################
                    189: # either enables or disable printing or creation of Dvi files
                    190: # based on the value of the gQuizTemp global
                    191: ###########################################################
                    192: # Argument: menuFrame (path name of the menu window)
                    193: #           name1 (name of traced varaiable, gQuizTemp)
                    194: #           name2 (empty argument from trace)
                    195: #           op (tracing on writes so this should be "w")
                    196: # Returns : nothing
                    197: # Globals : gQuizTemp (r)
                    198: ###########################################################
                    199: proc changeMenuStatus { menuFrame name1 name2 op } {
                    200:     global gQuizTemp
                    201:     if { $gQuizTemp } {
                    202: 	$menuFrame entryconfigure  5 -state normal
                    203: 	$menuFrame entryconfigure  6 -state normal 
                    204:     } else {
                    205: 	$menuFrame entryconfigure  5 -state disabled 
                    206: 	$menuFrame entryconfigure  6 -state disabled 
                    207:     }
                    208: }
                    209: 
                    210: ###########################################################
                    211: # printInfo
                    212: ###########################################################
                    213: # gets called by Alt-0, used to print out variable while
                    214: # still running
                    215: ###########################################################
                    216: # Argument: none
                    217: # Returns : nothing
                    218: # Globals : auto_path
                    219: ###########################################################
                    220: proc printInfo { } {
                    221:     global auto_path gUndo gRefText
                    222:     set num [lindex [array names gRefText] 0]
                    223:     set a "updateLocation 0"
                    224:     puts [list Main2: [time $a 20000]]
                    225:     set a "updateLocation $num"
                    226:     puts [list Ref  : [time $a 20000]]
                    227: }
                    228: 
                    229: ###########################################################
                    230: # createXdviOpt
                    231: ###########################################################
                    232: ###########################################################
                    233: ###########################################################
                    234: proc createXdviOpt {} {
                    235:     global gXdviOpt gWindowMenu
                    236: 
                    237:     if { [winfo exists .xdviOpt] } { 
                    238: 	capaRaise .xdviOpt 
                    239: 	return
                    240:     }
                    241: 
                    242:     set xdviOpt [toplevel .xdviOpt]
                    243:     $gWindowMenu add command -label "XdviOptions" -command "capaRaise $xdviOpt"
                    244:     wm title $xdviOpt "Options for Xdvi"
                    245: 
                    246:     set messageFrame [frame $xdviOpt.msg]
                    247:     message $xdviOpt.msg2 -text "Example: -geometry 800x700" -aspect 5000
                    248:     set buttonFrame [frame $xdviOpt.buttons -bd 10]
                    249:     pack $messageFrame $xdviOpt.msg2 $buttonFrame -side top -fill x
                    250: 
                    251:     message $messageFrame.msg -text "Options to xdvi:" -aspect 5000
                    252:     entry $messageFrame.entry -textvariable gXdviOpt
                    253: 
                    254:     pack $messageFrame.msg $messageFrame.entry -side left -fill x
                    255:     
                    256:     button $buttonFrame.ok -text Dismiss -command "destroy $xdviOpt
                    257:             removeWindowEntry XdviOptions" -underline 0
                    258:     pack $buttonFrame.ok -side left
                    259:     bind $xdviOpt <Destroy> "removeWindowEntry XdviOptions"
                    260: }
                    261: 
                    262: ###########################################################
                    263: # createInfoWindow
                    264: ###########################################################
                    265: # creates the Information window
                    266: ###########################################################
                    267: # Arguments: None
                    268: # Returns: Nothing
                    269: # Globals: gDate - the variable containg the current date 
                    270: #          gWindowMenu - used to register the new window in the
                    271: #                        windows menu
                    272: #          gVer - Stores the current version of Quizzer (set in 
                    273: #                 C init code
                    274: ###########################################################
                    275: proc createInfoWindow {} {
                    276:     global gDate gWindowMenu gVer gTclVer gCmd gCompileDate gUndoSize gUniqueNumber
                    277: 
                    278:     if { [winfo exists .about] } { 
                    279: 	capaRaise .about
                    280: 	return 
                    281:     }
                    282: 
                    283:     set about [toplevel .about]
                    284:     $gWindowMenu add command -label "About" -command "capaRaise $about"
                    285:     wm title $about "About" 
                    286:     
                    287:     label $about.l1  -font 12x24 -text "Quizzer $gVer" -pady 20
                    288:     label $about.l4  -font 8x13 -text "Quizzer.tcl version $gTclVer" -pady 20
                    289:     label $about.l6  -font 8x13 -text "$gCompileDate" 
                    290:     message $about.l2 -font 8x13 -text "Code by: Y. Tsai, G. Albertelli II Copyright Michigan State University Board of Trustees, 1992-1999, No Unauthorized Commercial Use" \
                    291: 	-pady 20 -aspect 300
                    292:     label $about.l3  -font 8x13 -textvariable gDate 
                    293:     label $about.l5  -font 8x13 -textvariable gCmd
                    294:     label $about.l7  -font 8x13 -textvariable gUndoSize
                    295:     label $about.l8  -font 8x13 -textvariable gUniqueNumber
                    296:    
                    297:     button $about.close -text "Close" -command "destroy $about
                    298:                                                 removeWindowEntry About"
                    299:     
                    300:     pack $about.l1 $about.l4 $about.l6 $about.l2 $about.l3 $about.l5 $about.l7 $about.l8\
                    301: 	    $about.close -side top 
                    302:     bind $about <Destroy> "removeWindowEntry About"
                    303:     Centre_Dialog $about default
                    304: }
                    305: 
                    306: ###########################################################
                    307: # enterSelection 
                    308: ###########################################################
                    309: ###########################################################
                    310: ###########################################################
                    311: proc enterSelection {} {
                    312:     global gTextWindow gFind
                    313:     if { [catch {set gTextWindow}] } { return }
                    314:     if { ![winfo exists $gTextWindow] } { return }
                    315: 
                    316:     createFindWindow
                    317:     catch {set gFind(find) [$gTextWindow get sel.first sel.last]}
                    318: }
                    319: 
                    320: ###########################################################
                    321: # jumpToSelection 
                    322: ###########################################################
                    323: ###########################################################
                    324: ###########################################################
                    325: proc jumpToSelection {} {
                    326:     global gTextWindow
                    327:     catch {$gTextWindow see sel.first}
                    328: }
                    329: 
                    330: proc cut {} {global gTextWindow;tk_textCut $gTextWindow}
                    331: proc copy {} {global gTextWindow;tk_textCopy $gTextWindow}
                    332: proc paste {} {global gTextWindow;tk_textPaste $gTextWindow}
                    333: 
                    334: ###########################################################
                    335: # selectAll 
                    336: ###########################################################
                    337: ###########################################################
                    338: ###########################################################
                    339: proc selectAll { { refNum 0 } } {
                    340:     global gTextWindow gRefText
                    341: 
                    342:     if { $refNum } { set window $gRefText($refNum) } else {
                    343: 	catch {set window $gTextWindow}
                    344:     } 
                    345:     if { ![winfo exists $window] } { return }
                    346:     $window tag add sel 0.0 end
                    347: }
                    348: 
                    349: ###########################################################
                    350: # creatEditingWindow
                    351: ###########################################################
                    352: ###########################################################
                    353: # Arguments: none
                    354: # Returns: a one if the Editing window existed and a zero
                    355: #          if it created a new window
                    356: # Globals:
                    357: ###########################################################
                    358: proc createEditingWindow { {setupUndo 1} } {
                    359:     global gEditWindow gPreviewMode gTextWindow gSetNumberText gFile \
                    360: 	gWindowMenu gNumberParsedText gLineNumberGoto gUndo \
                    361: 	gCharacterNumberGoto gLineNumberGoto gClosedDocument \
                    362: 	gPreviewButton gFirstTime
                    363: 
                    364:     if { [winfo exists .editwindow] } { 
                    365: 	capaRaise .editwindow
                    366: 	return 1 
                    367:     }
                    368:     set gFirstTime 1
                    369: 
                    370:     set gEditWindow [toplevel .editwindow]
                    371:     $gWindowMenu add command -label "$gFile" -command "capaRaise $gEditWindow"
                    372:     wm title $gEditWindow $gFile
                    373:     set gClosedDocument 0
                    374:     set editWindow [frame $gEditWindow.frame -borderwidth 10]
                    375:     
                    376:     pack $editWindow -expand 1 -fill both
                    377:    
                    378:     set editTop [frame $editWindow.editTop]
                    379:     set editBottom [frame $editWindow.editBottom]
                    380: 
                    381:     pack $editTop $editBottom -side top
                    382:     pack configure $editBottom -expand 1 -fill both
                    383: 
                    384:     set menuFrame [frame $editTop.menu -borderwidth 2 -relief raised]
                    385:     set assignInfo [frame $editTop.assignInfo -borderwidth 4 -relief groove]
                    386:     set mode [frame $editTop.mode -borderwidth 4 -relief groove]
                    387:     set buttonAndGoto [frame $editTop.buttonAndGoto ]
                    388:     pack $menuFrame $assignInfo $mode $buttonAndGoto -side left
                    389:     pack configure $menuFrame -anchor n
                    390: 
                    391:     menubutton $menuFrame.edit -text Edit -menu $menuFrame.edit.m
                    392:     pack $menuFrame.edit -side left
                    393: 
                    394:     set edit [ menu $menuFrame.edit.m ]
                    395:     $edit add command -label "Cut" -command { cut } -accelerator "Alt+x"
                    396:     $edit add command -label "Copy" -command { copy } -accelerator "Alt+c"
                    397:     $edit add command -label "Paste" -command { paste } -accelerator "Alt+v"
                    398:     $edit add command -label "Select All " -command { selectAll } -accelerator "Alt+a"
                    399:     $edit add separator
                    400:     $edit add command -label "Undo" -command "undo 0" -accelerator "Alt+u"
                    401:     $edit add separator
                    402:     $edit add command -label "Find" -command { createFindWindow } -accelerator "Alt+f"
                    403:     bind all <Alt-f> createFindWindow
                    404: 
                    405:     set buttons [frame $buttonAndGoto.buttons ]
                    406:     set gotoFrame [ frame $buttonAndGoto.gotoFrame ]
                    407:     pack $buttons $gotoFrame -side top
                    408: 
                    409:     set button1 [frame $buttons.button1]
                    410:     set button2 [frame $buttons.button2]
                    411:     pack $button1 $button2 -side top
                    412: 
                    413:     set gPreviewButton [button $button1.preview -text "Preview"  -command \
                    414: 			    { studentSelectWindow createPreviewWindow }]
                    415:     button $button1.dbHeader -text "DB Header"  -command createDBHeader
                    416:     button $button1.include -text "Include"  -command includeFile
                    417:     pack $button1.preview $button1.dbHeader $button1.include -side left
                    418:     
                    419:     button $button2.header -text "Std. Header" \
                    420:  	    -command insertStandardHeader
                    421:     button $button2.import -text "Import" -command importFile
                    422:     button $button2.end -text "Endline" -command insertEndline
                    423:     pack $button2.header $button2.import $button2.end -side left
                    424:         
                    425:     label $gotoFrame.msg -text "Current Line:"
                    426:     label $gotoFrame.current -textvariable gLineNumber
                    427:     entry $gotoFrame.line -width 8 -textvariable gLineNumberGoto
                    428:     bind $gotoFrame.line <KeyPress-Return> "gotoLine"
                    429:     button $gotoFrame.button -text "Goto" -command "gotoLine"
                    430:     pack $gotoFrame.msg $gotoFrame.current $gotoFrame.line \
                    431: 	    $gotoFrame.button -side left
                    432:     # variable used by gotoLine, needs to be set if it doesn't exist
                    433:     if { [ catch { set gCharacterNumberGoto } ] } {
                    434: 	set gCharacterNumberGoto ""
                    435:     }
                    436: 
                    437:     label $assignInfo.time -textvariable gDate 
                    438:     set setNum [frame $assignInfo.setNum -borderwidth 2]
                    439:     set probsParsed [frame $assignInfo.probsParsed -borderwidth 2]
                    440:     
                    441:     pack $assignInfo.time $setNum $probsParsed -side top
                    442:     pack configure $setNum $probsParsed -anchor e
                    443: 
                    444:     message $setNum.msg -text "Problem Set Number" -aspect 10000 
                    445:     set setNumberWindow [message $setNum.num  -relief sunken  -width 70 \
                    446: 	    -textvariable gSetNumberText]
                    447:     
                    448:     pack $setNum.num $setNum.msg -side right -anchor e
                    449: 
                    450:     label $probsParsed.msg -text "Number of Questions Parsed"  
                    451:     set numberParsedWindow [label $probsParsed.num -relief sunken  \
                    452: 	    -textvariable gNumberParsedText]
                    453:     
                    454:     pack $probsParsed.num $probsParsed.msg -side right -anchor e
                    455:     
                    456:     message $mode.msg -text "Mode" -aspect 10000 
                    457:     radiobutton $mode.enscript -text "Enscript" -value "Enscript" \
                    458: 	    -variable gPreviewMode 
                    459:     radiobutton $mode.tex -text "TeX" -value "TeX" -variable gPreviewMode
                    460:     radiobutton $mode.web -text "Web" -value "Web" -variable gPreviewMode
                    461:     
                    462:     pack $mode.msg $mode.enscript $mode.tex $mode.web -side top
                    463:     pack configure $mode.enscript $mode.tex $mode.web -anchor w 
                    464:     set gPreviewMode Enscript
                    465: 
                    466:     scrollbar $editBottom.scroll -orient vertical -command \
                    467: 	    "$editBottom.text yview"
                    468:     set gTextWindow [text $editBottom.text -yscrollcommand \
                    469: 	    "$editBottom.scroll set" -wrap char -height 40]
                    470: 
                    471:     pack $editBottom.scroll $editBottom.text -side left -expand 0
                    472:     pack configure $editBottom.scroll -expand 0 -fill y
                    473:     pack configure $editBottom.text -expand true -fill both
                    474: 
                    475:     if { $setupUndo} {
                    476: 	rename $gTextWindow .$gTextWindow
                    477: 	trackChanges $gTextWindow 0
                    478: 	set gUndo(0) 0
                    479: 	set gUndo(0.cur) 0
                    480:     }
                    481: 
                    482:     bind $gTextWindow <Alt-s> saveDocument
                    483:     bind $gTextWindow <Alt-Shift-s> { saveDocument 1 }
                    484:     bind $gEditWindow <Alt-w> closeDocument
                    485: #    bind $gEditWindow <Destroy> "closeDocument 1"
                    486:     wm protocol $gEditWindow WM_DELETE_WINDOW "closeDocument 1"
                    487:     bind $gTextWindow <Alt-x> "tk_textCut %W"
                    488:     bind $gTextWindow <Alt-c> "tk_textCopy %W"
                    489:     bind $gTextWindow <Alt-v> "tk_textPaste %W"
                    490:     bind $gTextWindow <Alt-a> selectAll 
                    491:     bind $gTextWindow <Alt-u> "undo 0" 
                    492:     addFindList
                    493:     Centre_Dialog $gEditWindow default
                    494: 
                    495:     createImportLinks 0 0.0 end
                    496:     return 0
                    497: }
                    498: 
                    499: ###########################################################
                    500: # includeFile 
                    501: ###########################################################
                    502: ###########################################################
                    503: ###########################################################
                    504: proc includeFile {} {
                    505:     global gTextWindow gDir
                    506:     
                    507:     if { [makeSure "Is the cursor in the correct position?"] == "Cancel" } {
                    508: 	return
                    509:     }
                    510:     
                    511: #    if { $gDir(include) == "." } { set gDir(include) [pwd] }
                    512:     set file [tk_getOpenFile -filetypes \
                    513: 	    { { {All Files} {"*"} } { {Quizzer} {"*.qz"} } } \
                    514: 	    -title "Select the proper file" \
                    515: 	    -initialdir "$gDir(include)" ]
                    516:     if { $file == "" } { return }
                    517:     set gDir(include) [file dirname $file]
                    518: 
                    519:     if { $file == "" } { return }
                    520: 
                    521:     set fileId [open $file "r"]
                    522:     
                    523:     $gTextWindow insert insert [read $fileId [file size $file]]
                    524: }
                    525: 
                    526: ###########################################################
                    527: # insertStandardHeader 
                    528: ###########################################################
                    529: ###########################################################
                    530: ###########################################################
                    531: proc insertStandardHeader {} {
                    532:     global gTextWindow gCapaConfig
                    533:     
                    534:     if { [makeSure "Is the cursor in the correct position?"] == "Cancel" } {
                    535:  	return
                    536:     }
                    537:     $gTextWindow insert insert $gCapaConfig(standardQuizzerHeader)
                    538: }
                    539: 
                    540: ###########################################################
                    541: # importFile 
                    542: ###########################################################
                    543: ###########################################################
                    544: ###########################################################
                    545: proc importFile {} {
                    546:     global gTextWindow gDir gProbVal gTryVal gHintVal gPutLine
                    547:     
                    548:     if { [makeSure "Is the cursor in the correct position?"] == "Cancel" } {
                    549:  	return
                    550:     }
                    551:     
                    552: #    if { $gDir(import) == "." } { set gDir(import) [pwd] }
                    553:     set file [tk_getOpenFile -filetypes \
                    554: 	    { { {All Files} {"*"} } { {Quizzer} {"*.qz"} } } \
                    555: 	    -title "Select the proper file" -initialdir "$gDir(import)" ]
                    556:     if { $file == "" } { return }
                    557:     set gDir(import) [file dirname $file]
                    558: 
                    559:     
                    560:     if { [getProbValTryVal] == "Cancel" } { return }
                    561:     
                    562:     $gTextWindow insert insert "//\n/BEG prob_val=$gProbVal\n/LET try_val=$gTryVal\n/LET hint_val=$gHintVal\n/DIS(\"$file\")\n/IMP \"$file\"\n"
                    563:     if { $gPutLine } {
                    564: 	$gTextWindow insert insert "/DIS(stdline)\n"
                    565:     } else {
                    566:  	$gTextWindow insert insert "/DIS(webonlyline)\n"
                    567:     }
                    568:     $gTextWindow see insert
                    569: }
                    570: 
                    571: ###########################################################
                    572: # insertEndline 
                    573: ###########################################################
                    574: ###########################################################
                    575: ###########################################################
                    576: proc insertEndline {} {
                    577:     global gTextWindow
                    578:     
                    579:     if { [makeSure "Is the cursor in the correct position?"] == "Cancel" } {
                    580:  	return
                    581:     }
                    582:     $gTextWindow insert insert "/END(stdendline)\n"
                    583: }
                    584: 
                    585: ###########################################################
                    586: # getProbValTryVal
                    587: ###########################################################
                    588: ###########################################################
                    589: ###########################################################
                    590: proc getProbValTryVal {} {
                    591:     global gPrompt gProbVal gTryVal gHintVal gPutLine
                    592:     
                    593:     set dialog [toplevel .getProbValTryVal -borderwidth 10]
                    594:     wm title $dialog "Getting Problem Value and Try Value"
                    595:     wm geo $dialog "+200+200"
                    596:     message $dialog.msg -text "Set the weight of the problem and the number of tries allowed." -aspect 700
                    597:     
                    598:     set gPrompt(result) ""
                    599:     scale $dialog.probVal -orient horizontal -variable gProbVal \
                    600:  	    -from 0 -to 9 -label "Problem Value" -length 150
                    601:     scale $dialog.tryVal -orient horizontal -variable gTryVal \
                    602:  	    -from 0 -to 99 -label "Try Value" -length 150
                    603:     scale $dialog.hintVal -orient horizontal -variable gHintVal \
                    604:  	    -from 1 -to 99 -label "Number of Tries Before Showing Hint" -length 150
                    605:     set gPrompt(stdline.1) "Put a stdline after this problem."
                    606:     set gPrompt(stdline.0) "Put a webonlyline after this problem."
                    607:     checkbutton $dialog.putLine -variable gPutLine \
                    608: 	    -width 38 \
                    609: 	    -textvariable gPrompt(displayLine) \
                    610: 	    -command { set gPrompt(displayLine) $gPrompt(stdline.$gPutLine) }
                    611:     set gPrompt(displayLine) $gPrompt(stdline.$gPutLine) 
                    612:     set buttonFrame [frame $dialog.buttons -bd 10]
                    613:     pack $dialog.msg $dialog.probVal $dialog.tryVal $dialog.hintVal $dialog.putLine \
                    614:  	    $buttonFrame -side top -fill x
                    615:     
                    616:     button $buttonFrame.yes -text Import -command {set gPrompt(yes) 1} \
                    617:  	    -underline 0
                    618:     button $buttonFrame.cancel -text Cancel -command { set gPrompt(yes) 0 } \
                    619:  	    -underline 0
                    620:     pack $buttonFrame.yes $buttonFrame.cancel -side left
                    621:     
                    622:     bind $dialog <Alt-Key> break
                    623:     bind $dialog <Destroy> "set gPrompt(yes) 0"
                    624:     Centre_Dialog $dialog default
                    625:     update
                    626:     
                    627:     focus $dialog
                    628:     capaRaise $dialog
                    629:     capaGrab $dialog
                    630:     vwait gPrompt(yes)
                    631:     capaGrab release $dialog
                    632:     bind $dialog <Destroy> ""
                    633:     destroy $dialog
                    634:     if {$gPrompt(yes)} {
                    635:  	return Done
                    636:     } else {
                    637:  	return Cancel
                    638:     }
                    639: }
                    640: 
                    641: ###########################################################
                    642: # updateDateBox 
                    643: ###########################################################
1.13      albertel  644: # sticks the date information from gControlDates into the date listbox
1.1       albertel  645: ###########################################################
                    646: ###########################################################
                    647: proc updateDateBox { listbox } {
                    648:     global gControlDates
                    649:     $listbox delete 0 end
                    650:     for {set i 0} {$i < [llength $gControlDates]} {incr i } {
                    651: 	set date [lindex $gControlDates $i]
                    652: 	if { $i != 0 } {
                    653: 	    $listbox insert end [eval format {"%4d %4d   %s %s %s"} $date]
                    654: 	} else {
                    655: 	    $listbox insert end [eval format {"  DEFAULT   %s %s %s"} [lrange $date 2 end]]
                    656: 	}
                    657:     }
                    658: }
                    659: 
                    660: ###########################################################
                    661: # loadDates 
                    662: ###########################################################
                    663: ###########################################################
                    664: ###########################################################
                    665: proc loadDates { listbox } {
                    666:     global gControlDates
                    667:     if { [catch {getHeaderInfo}]} { 
                    668: 	displayError "That set.db does not exist"
1.13      albertel  669:     } else { 
                    670: 	if { [llength $gControlDates] > 2 } {
                    671: 	    set gControlDates [linsert [lreverse [lrange $gControlDates 1 end]] 0 [lindex $gControlDates 0]] 
                    672: 	} 
                    673:     }
1.1       albertel  674:     updateDateBox $listbox
                    675: }
                    676: 
                    677: ###########################################################
                    678: # deleteDate 
                    679: ###########################################################
                    680: ###########################################################
                    681: ###########################################################
                    682: proc deleteDate { listbox } {
                    683:     global gControlDates
                    684:     if { [set a [$listbox index anchor]] != 0 } {
                    685: 	catch {$listbox delete anchor}
                    686: 	set gControlDates [lreplace $gControlDates $a $a]
                    687:     } else {
                    688: 	displayError "You can not delete the default setting, only change it."
                    689:     }
                    690: }
                    691: 
                    692: ###########################################################
                    693: # checkHeaderForDefault 
                    694: ###########################################################
                    695: ###########################################################
                    696: ###########################################################
                    697: proc checkHeaderForDefault { } {
                    698:     global gControlDates
                    699:     if { [lindex [lindex $gControlDates 0] 0] == 0 && \
                    700: 	     [lindex [lindex $gControlDates 0] 1] == 0 } {
                    701: 	return 1
                    702:     }
                    703:     return 0
                    704: }
                    705: 
                    706: ###########################################################
                    707: # dueAnswerOrder 
                    708: ###########################################################
                    709: ###########################################################
                    710: ###########################################################
                    711: proc dueAnswerOrder { } {
                    712:     global gDates
                    713:     set duetime [clock scan "$gDates(duemonth)/$gDates(dueday)/$gDates(dueyear) $gDates(duehour):$gDates(dueminute)"]
                    714:     set answertime [clock scan "$gDates(answermonth)/$gDates(answerday)/$gDates(answeryear) $gDates(answerhour):$gDates(answerminute)"]
                    715:     if { $duetime > $answertime } { return 1 } { return 0 }
                    716: }
                    717: 
                    718: ###########################################################
                    719: # checkDateFields 
                    720: ###########################################################
                    721: ###########################################################
                    722: ###########################################################
                    723: proc checkDateFields { } {
                    724:     global gDates
                    725: 
                    726:     set result 1
                    727:     foreach kind { open due answer } {
                    728: 	foreach type { year month day hour minute } {
                    729: 	    if { [validate $kind $type] != 1 } {
                    730: 		return $kind$type
                    731: 	    }
                    732: 	}
                    733:     }
                    734:     if { [validate "" durationhour] != 1 } { return durationhour }
                    735:     if { [validate  duration minute] != 1 } { return durationminute }
                    736:     if { [catch { expr $gDates(sectionstart) }] } { return sectionstart } else {
                    737: 	if {[string length $gDates(sectionstart)] <1} { return sectionstart } else {
                    738: 	    if { [catch { expr $gDates(sectionend) }] } { return sectionend } else {
                    739: 		if {[string length $gDates(sectionend)] <1} { return sectionend } 
                    740: 	    }
                    741: 	}
                    742:     }    
                    743:     return $result
                    744: }
                    745: 
                    746: ###########################################################
                    747: # validate
                    748: ###########################################################
                    749: ###########################################################
                    750: ###########################################################
                    751: proc validate { kind type {newvalue novalue} } {
                    752:     global gDates
                    753:     #tcl interprets all strings of digits with 0 as the first number as being 
                    754:     #in octal, need to prevent this from causing an error
                    755:     if { $newvalue == "novalue" } { 
                    756: 	set temp $gDates($kind$type)
                    757: 	set lowfail -1
                    758:     } else {
                    759: 	if { $newvalue == "" } { return 1 }
                    760: 	set temp $newvalue
                    761: 	set lowfail 1
                    762:     }
                    763:     if { [string length $temp] > 1  && [string index $temp 0] == 0 } {
                    764: 	set test [string range $temp 1 end]
                    765:     } else {
                    766: 	set test $temp
                    767:     }
                    768:     if { [catch { expr $test }] } { return 0 } else {
                    769: 	switch $type {
                    770: 	    year { 
                    771: 		if { $test < 1990 } { return $lowfail } 
                    772: 		if { $test > 9999 } { return 0 } 
                    773: 		if { [string length $temp] > 4 } { return 0 }
                    774: 	    }
                    775: 	    month { 
                    776: 		if { $test < 1 } { return $lowfail } 
                    777: 		if { $test > 12 } { return 0 } 
                    778: 		if { [string length $temp] > 2 } { return 0 }
                    779: 	    }
                    780: 	    day { 
                    781: 		if { $test < 1 } { return $lowfail } 
                    782: 		if { $test > 31 } { return 0 } 
                    783: 		if { [string length $temp] > 2 } { return 0 }
                    784: 	    }
                    785: 	    hour {
                    786: 		if { $test < 0 } { return 0 }
                    787: 		if { $test > 23 } { return 0 }
                    788: 		if { [string length $temp] > 2 } { return 0 }
                    789: 	    }
                    790: 	    minute {
                    791: 		if { $test < 0 } { return 0 }
                    792: 		if { $test > 59 } { return 0 }
                    793: 		if { [string length $temp] > 2 } { return 0 }
                    794: 	    }
                    795: 	    durationhour {
                    796: 		if { $test < 0 } { return 0 }
                    797: 		if { [string length $temp] > 4 } { return 0 }
                    798: 	    }
                    799: 	}
                    800:     }
                    801:     return 1
                    802: }
                    803: 
                    804: ###########################################################
                    805: # getday
                    806: ###########################################################
                    807: ###########################################################
                    808: ###########################################################
                    809: proc getday { kind } {
                    810:     global gDates
                    811:     if { [set day $gDates([set kind]day)] != ""} {
                    812: 	if { [set month $gDates([set kind]month)] != ""} {
                    813: 	    if { [set year $gDates([set kind]year)] != ""} {
                    814: 		if { [ catch { set gDates($kind.dayoweek) \
                    815: 				   [clock format [clock scan "$month/$day/$year"] \
                    816: 					-format %a] } error ] } {
                    817: 		    set gDates($kind.dayoweek) ""
                    818: 		}
                    819: 		return
                    820: 	    }
                    821: 	}
                    822:     }
                    823:     set gDates($kind.dayoweek) ""
                    824: }
                    825: 
                    826: ###########################################################
1.13      albertel  827: # moveUpDate
                    828: ###########################################################
                    829: ###########################################################
                    830: ###########################################################
                    831: proc  moveUpDate { listbox } {
                    832:     global gControlDates gDates
                    833:     if { ![winfo exists $listbox] } { return }
                    834:     if { [set which [$listbox curselection]] == "" } {
                    835: 	displayMessage "Please select a date to move."
                    836: 	return
                    837:     }
                    838:     if { $which > 1 } {
                    839: 	set element [lindex $gControlDates $which]
                    840: 	set gControlDates [lreplace $gControlDates $which $which]
                    841: 	set gControlDates [linsert $gControlDates [expr $which - 1] $element]
                    842: 	updateDateBox $listbox
                    843: 	$listbox selection set [expr {$which - 1}]
                    844: 	$listbox see [expr {$which -1}]
                    845:     }
                    846: }
                    847: 
                    848: ###########################################################
                    849: # moveDownDate
                    850: ###########################################################
                    851: ###########################################################
                    852: ###########################################################
                    853: proc  moveDownDate { listbox } {
                    854:     global gControlDates gDates
                    855:     if { ![winfo exists $listbox] } { return }
                    856:     if { [set which [$listbox curselection]] == "" } {
                    857: 	displayMessage "Please select a date to move."
                    858: 	return
                    859:     }
                    860:     if { ($which > 0) && ($which < ([llength $gControlDates]-1)) } {
                    861: 	set element [lindex $gControlDates $which]
                    862: 	set gControlDates [lreplace $gControlDates $which $which]
                    863: 	set gControlDates [linsert $gControlDates [expr $which + 1] $element]
                    864: 	updateDateBox $listbox
                    865: 	$listbox selection set [expr {$which + 1}]
                    866: 	$listbox see [expr {$which + 1}]
                    867:     }
                    868: }
                    869: 
                    870: ###########################################################
1.1       albertel  871: # enableDateValidation
                    872: ###########################################################
                    873: ###########################################################
                    874: ###########################################################
                    875: proc  enableDateValidation { toplevel } {
                    876:     global gDates
                    877:     set dateFrame $toplevel.dateFrame
                    878:     foreach type { open due answer } {
                    879: 	$dateFrame.[set type]date.year configure -validate key \
                    880: 	    -validatecommand "validate $type year %P"
                    881: 	$dateFrame.[set type]date.month configure -validate key \
                    882: 	    -validatecommand "validate $type month %P"
                    883: 	$dateFrame.[set type]date.day configure -validate key \
                    884: 	    -validatecommand "validate $type day %P"
                    885: 	$dateFrame.[set type]time.hour configure -validate key \
                    886: 	    -validatecommand "validate $type hour %P"
                    887: 	$dateFrame.[set type]time.minute configure -validate key \
                    888: 	    -validatecommand "validate $type minute %P"
                    889:     }
                    890:     $gDates(optFrame).duration.hour configure -validate key \
                    891: 	-validatecommand "validate {} durationhour %P"
                    892:     $gDates(optFrame).duration.minute configure -validate key \
                    893: 	-validatecommand "validate duration minute %P"
                    894: 
                    895: }
                    896: 
                    897: ###########################################################
                    898: # addDateOptions
                    899: ###########################################################
                    900: ###########################################################
                    901: ###########################################################
                    902: proc addDateOptions {} {
                    903:     global gDates
                    904:     pack $gDates(optFrame2)
                    905:     $gDates(optBut) configure -text "Less Options" -command "removeDateOptions"
                    906: }
                    907: 
                    908: ###########################################################
                    909: # removeDateOptions
                    910: ###########################################################
                    911: ###########################################################
                    912: ###########################################################
                    913: proc removeDateOptions {} {
                    914:     global gDates
                    915:     pack forget $gDates(optFrame2)
                    916:     $gDates(optBut) configure -text "More Options" -command "addDateOptions"
                    917: }
                    918: 
                    919: ###########################################################
                    920: # createDateDialog
                    921: ###########################################################
                    922: ###########################################################
                    923: ###########################################################
                    924: proc createDateDialog { toplevel makedefault } {
                    925:     global gDates gPrompt2
                    926: 
                    927:     catch [unset gDates]
                    928: 
                    929:     set infoFrame [frame $toplevel.infoFrame]
                    930:     set sectionFrame [frame $toplevel.sectionFrame -borderwidth 4]
                    931:     set dateFrame [frame $toplevel.dateFrame]
                    932:     set optionsFrame [frame $toplevel.optionsFrame]
                    933:     set buttonFrame [frame $toplevel.buttonFrame]
                    934:     pack $infoFrame $sectionFrame $dateFrame $buttonFrame -side top
                    935:     
                    936:     if { $makedefault } {
                    937: 	label $sectionFrame.sectionl -text "Default value for all sections:"
                    938: 	pack $sectionFrame.sectionl
                    939: 	set gDates(sectionstart) 0
                    940: 	set gDates(sectionend) 0
                    941:     } else {
                    942: 	grid [label $sectionFrame.sectionl -text "Dates and times are for"] \
                    943: 	    -column 0 -columnspan 4 -row 0
                    944: 	grid [label $sectionFrame.section12 -text "section:"] -column 0 -row 1
                    945: 	grid [entry $sectionFrame.start -width 3 -textvariable gDates(sectionstart)] \
                    946: 	    -column 1 -row 1
                    947: 	grid [label $sectionFrame.sectionl3 -text "through section: "] -column 2 -row 1
                    948: 	grid [entry $sectionFrame.end -width 3 -textvariable gDates(sectionend)] \
                    949: 	    -column 3 -row 1
                    950: 	button $buttonFrame.getdefaults -text "Get Defaults" -command \
                    951: 	    "setValues 0 0;enableDateValidation $toplevel"
                    952: 	pack $buttonFrame.getdefaults -side left
                    953:     }
                    954: 
                    955:     grid [label $dateFrame.datel -text "Date"] -column 1 -row 0
                    956:     grid [label $dateFrame.timel -text "Time"] -column 2 -row 0
                    957:     grid [label $dateFrame.helpd -text "yyyy/mm/dd"] -column 1 -row 1
                    958:     grid [label $dateFrame.helpt -text "hh:mm"] -column 2 -row 1
                    959:     grid [label $dateFrame.openl -text "Open"] -column 0 -row 2
                    960:     grid [set openDate [frame $dateFrame.opendate -borderwidth 2 -relief sunken]] -column 1 -row 2
                    961:     grid [set openTime [frame $dateFrame.opentime -borderwidth 2 -relief sunken]] -column 2 -row 2
                    962:     grid [label $dateFrame.openday -textvariable gDates(open.dayoweek)] -column 3 -row 2
                    963:     grid [label $dateFrame.duel -text "Due"] -column 0 -row 3
                    964:     grid [set dueDate [frame $dateFrame.duedate -borderwidth 2 -relief sunken]] -column 1 -row 3
                    965:     grid [set dueTime [frame $dateFrame.duetime -borderwidth 2 -relief sunken]] -column 2 -row 3
                    966:     grid [label $dateFrame.dueday -textvariable gDates(due.dayoweek)] -column 3 -row 3
                    967:     grid [label $dateFrame.answerl -text "Answer"] -column 0 -row 4
                    968:     grid [set answerDate [frame $dateFrame.answerdate -borderwidth 2 -relief sunken]] -column 1 -row 4
                    969:     grid [set answerTime [frame $dateFrame.answertime -borderwidth 2 -relief sunken]] -column 2 -row 4		
                    970:     grid [label $dateFrame.ansday -textvariable gDates(answer.dayoweek)] -column 3 -row 4
                    971:     
                    972:     
                    973:     foreach type { open due answer } {
                    974: 	entry [set [set type]Date].year -width 4 -textvariable gDates([set type]year) 
                    975: 	label [set [set type]Date].sl1 -text "/"
                    976: 	entry [set [set type]Date].month -width 2 -textvariable gDates([set type]month) 
                    977: 	label [set [set type]Date].sl2 -text "/"
                    978: 	entry [set [set type]Date].day -width 2 -textvariable gDates([set type]day) 
                    979: 	entry [set [set type]Time].hour -width 2 -textvariable gDates([set type]hour) 
                    980: 	label [set [set type]Time].colon -text ":"
                    981: 	entry [set [set type]Time].minute -width 2 -textvariable gDates([set type]minute)
                    982: 	pack [set [set type]Date].year [set [set type]Date].sl1 \
                    983: 	    [set [set type]Date].month [set [set type]Date].sl2 \
                    984: 	    [set [set type]Date].day -side left
                    985: 	pack [set [set type]Time].hour [set [set type]Time].colon \
                    986: 	    [set [set type]Time].minute -side left
                    987:     }
                    988: 
                    989:     set optionsFrame3 [frame $optionsFrame.options]
                    990:     set gDates(optFrame) [ set optionsFrame2 [frame $optionsFrame3.options ] ]
                    991:     pack $gDates(optFrame) $optionsFrame3
                    992:     set gDates(optFrame2) $optionsFrame
                    993: 
                    994:     set durationFrame [frame $optionsFrame2.duration]
                    995:     checkbutton $optionsFrame2.view -variable gDates(viewbetween) \
                    996: 	-text "Allow Viewing between Due and Answer dates" 
                    997:     checkbutton $optionsFrame2.response -variable gDates(inhibitresponse) \
                    998: 	-text "Inhibit Correct/Incorrect response \n(normally only for exams/quizzes)"
                    999:     pack $durationFrame $optionsFrame2.view $optionsFrame2.response -side top
1.7       albertel 1000:     set gDates(viewbetween) 0
1.1       albertel 1001:     set gDates(inhibitresponse) 0
                   1002: 
                   1003:     label $durationFrame.label -text "Duration"
                   1004:     entry $durationFrame.hour  -width 4 -textvariable gDates(durationhour)
                   1005:     label $durationFrame.colon -text ":"
                   1006:     entry $durationFrame.minute -width 2 -textvariable gDates(durationminute) 
                   1007:     pack $durationFrame.label $durationFrame.hour $durationFrame.colon \
                   1008: 	$durationFrame.minute -side left
                   1009:     set gDates(durationhour) 0
                   1010:     set gDates(durationminute) 0
                   1011: 
                   1012:     button $buttonFrame.help -text "Help" -command "showHelp dateEntry"
                   1013:     button $buttonFrame.set -text "Ok" -command "set gPrompt2(ok) 1"
                   1014:     button $buttonFrame.cancel -text "Cancel" -command "set gPrompt2(ok) 0"
                   1015:     set gDates(optBut) [ button $buttonFrame.options -text "More Options" \
                   1016: 			     -command "addDateOptions"]
                   1017:     pack $buttonFrame.help $buttonFrame.set $buttonFrame.cancel $buttonFrame.options \
                   1018: 	-side left
                   1019:     bind $toplevel <Destroy> "set gPrompt2(ok) 0"
                   1020:     bind $toplevel <KeyPress> "getday open;getday due;getday answer"
                   1021:     bind $toplevel <Return> {tkTabToWindow [tk_focusNext %W]}
                   1022: }
                   1023: 
                   1024: ###########################################################
                   1025: # addCurrentDates
                   1026: ###########################################################
                   1027: ###########################################################
                   1028: ###########################################################
                   1029: proc addCurrentDates { listbox makedefault {which -1} } {
                   1030:     global gControlDates gDates
                   1031:     foreach kind { open due answer } {
                   1032: 	foreach type { year month day hour minute } {
                   1033: 	    #tcl interprets all strings of digits with 0 as the first number as 
                   1034: 	    #being in octal, need to prevent this from causing an error
                   1035: 	    if { [string length $gDates($kind$type)] > 1  && \
                   1036: 		     [string index $gDates($kind$type) 0] == 0 } {
                   1037: 		set gDates($kind$type) [string range $gDates($kind$type) 1 end]
                   1038: 	    }
                   1039: 	}
                   1040:     }
                   1041:     set datestring [list $gDates(sectionstart) $gDates(sectionend) \
                   1042: 			[format "%04d/%02d/%02d %02d:%02d" $gDates(openyear) \
                   1043: 			     $gDates(openmonth) $gDates(openday) \
                   1044: 			     $gDates(openhour) $gDates(openminute) ] \
                   1045: 			[format "%04d/%02d/%02d %02d:%02d" $gDates(dueyear) \
                   1046: 			     $gDates(duemonth) $gDates(dueday) \
                   1047: 			     $gDates(duehour) $gDates(dueminute) ] \
                   1048: 			[format "%04d/%02d/%02d %02d:%02d" $gDates(answeryear) \
                   1049: 			     $gDates(answermonth) $gDates(answerday) \
                   1050: 			     $gDates(answerhour) $gDates(answerminute) ] \
                   1051: 		        [format "%d:%d" $gDates(durationhour) $gDates(durationminute)] \
                   1052: 			$gDates(inhibitresponse) $gDates(viewbetween) ]
                   1053:     if { $makedefault } {
                   1054: 	if { ([info globals gControlDates] == "") || ($gControlDates == "") } {
                   1055: 	    set gControlDates [list $datestring]
                   1056: 	} else { 
                   1057: 	    set gControlDates [lreplace $gControlDates 0 0 $datestring]
                   1058: 	}
                   1059:     } else {
                   1060: 	if { $which > -1 } {
                   1061: #	    puts "$gControlDates=$which=$which=$datestring"
                   1062: 	    set gControlDates [lreplace $gControlDates $which $which $datestring]
                   1063: 	} else {
                   1064: 	    lappend gControlDates $datestring
                   1065: 	}
                   1066:     }
                   1067:     updateDateBox $listbox
                   1068: }
                   1069: 
                   1070: ###########################################################
                   1071: # setValues
                   1072: ###########################################################
                   1073: ###########################################################
                   1074: ###########################################################
                   1075: proc setValues { which {doSections 1} } {
                   1076:     global gControlDates gDates
                   1077:     set datestring [lindex $gControlDates $which]
                   1078:     if { $doSections } {
                   1079: 	set gDates(sectionstart) [lindex $datestring 0]
                   1080: 	set gDates(sectionend) [lindex $datestring 1]
                   1081:     }
                   1082:     foreach type {open due answer} element {2 3 4} {
                   1083: 	set gDates([set type]year) [lindex [split [lindex $datestring $element] "/" ] 0]
                   1084: 	set gDates([set type]month) [lindex [split [lindex $datestring $element] "/" ] 1]
                   1085: 	set gDates([set type]day) [lindex [split [lindex [lindex $datestring $element] 0] "/" ] 2]
                   1086: 	set gDates([set type]hour) [lindex [split [lindex [lindex $datestring $element] 1] ":" ] 0]
                   1087: 	set gDates([set type]minute) [lindex [split [lindex [lindex $datestring $element] 1] ":" ] 1]
                   1088:     }
                   1089:     set gDates(durationhour) [lindex [split [lindex $datestring 5] ":" ] 0]
                   1090:     set gDates(durationminute) [lindex [split [lindex $datestring 5] ":" ] 1]
                   1091:     set gDates(inhibitresponse) [lindex $datestring 6]
                   1092:     set gDates(viewbetween) [lindex $datestring 7]
                   1093:     getday open
                   1094:     getday due
                   1095:     getday answer
                   1096: }
                   1097: 
                   1098: ###########################################################
                   1099: # changeDate
                   1100: ###########################################################
                   1101: ###########################################################
                   1102: ###########################################################
                   1103: proc changeDate { listbox } {
                   1104:     global gDates gPrompt2 gControlDates
                   1105: 
                   1106:     if { ![winfo exists $listbox] } { return }
                   1107:     if { [winfo exists .adddate] } { return }
                   1108:     if { [set which [$listbox index anchor]] == 0 } { set makedefault 1 } else { 
                   1109: 	set makedefault 0 }
                   1110:     if { $which == [$listbox index end] } {addDate $listbox;return}
                   1111:     set changeDate [toplevel .changeDate]
                   1112:     createDateDialog $changeDate $makedefault
                   1113:     setValues $which
                   1114:     enableDateValidation $changeDate
                   1115: 
                   1116:     if { $makedefault } {
                   1117: 	set gDates(sectionstart) 0
                   1118: 	set gDates(sectionend) 0
                   1119:     }
                   1120: 
                   1121:     Centre_Dialog $changeDate default
                   1122:     update
                   1123: 
                   1124:     focus $changeDate
                   1125:     capaRaise $changeDate
                   1126:     capaGrab $changeDate
                   1127:     set done 0
                   1128:     while { $done != 1 } {
                   1129: 	vwait gPrompt2(ok)
                   1130: 	if { $gPrompt2(ok) == 1 } {
                   1131: 	    if { 1 != [set done [checkDateFields]] } {
                   1132: 		displayError "Please correct field: $done"
                   1133: 		set done 0
                   1134: 	    } else {
                   1135: 		if { [dueAnswerOrder] } {
                   1136: 		    if { "Yes" == [makeSure "Right now answers are available before an assignment is due. Would you like to change this?."]} {
                   1137: 		    set done 0
                   1138: 		    } else {
                   1139: 			set done 1
                   1140: 		    }
                   1141: 		}
                   1142: 	    }
                   1143: 	} else {
                   1144: 	    set done 1
                   1145: 	}
                   1146:     }
                   1147:     capaGrab release $changeDate
                   1148:     bind $changeDate <Destroy> ""
                   1149:     destroy $changeDate
                   1150:     if {$gPrompt2(ok) == 1 } {
                   1151: 	addCurrentDates $listbox $makedefault $which
                   1152:     }    
                   1153:     return 
                   1154: }
                   1155: 
                   1156: ###########################################################
                   1157: # addDate 
                   1158: ###########################################################
                   1159: ###########################################################
                   1160: ###########################################################
                   1161: proc addDate { listbox } {
                   1162:     global gDates gPrompt2
                   1163: 
                   1164:     if { ![winfo exists $listbox ] } { return }
                   1165:     if { [winfo exists .adddate ] } { return }
                   1166:     if { [$listbox index end] == 0 } { set makedefault 1 } else { set makedefault 0 }
                   1167:     set addDate [toplevel .adddate]
                   1168: 
                   1169:     createDateDialog $addDate $makedefault
                   1170:     enableDateValidation $addDate
                   1171: 
                   1172:     Centre_Dialog $addDate default
                   1173:     update
                   1174: 
                   1175:     focus $addDate
                   1176:     capaRaise $addDate
                   1177:     capaGrab $addDate
                   1178:     set done 0
                   1179:     while { $done != 1 } {
                   1180: 	vwait gPrompt2(ok)
                   1181: 	if { $gPrompt2(ok) == 1 } {
                   1182: 	    if { 1 != [set done [checkDateFields]] } {
                   1183: 		displayError "Please correct field: $done"
                   1184: 		set done 0
                   1185: 	    } else {
                   1186: 		if { [dueAnswerOrder] } {
                   1187: 		    if { "Yes" == [makeSure "Right now answers are available before an assignment is due. Would you like to change this?."]} {
                   1188: 		    set done 0
                   1189: 		    } else {
                   1190: 			set done 1
                   1191: 		    }
                   1192: 		}
                   1193: 	    }
                   1194: 	} else {
                   1195: 	    set done 1
                   1196: 	}
                   1197:     }
                   1198:     capaGrab release $addDate
                   1199:     bind $addDate <Destroy> ""
                   1200:     destroy $addDate
                   1201:     if {$gPrompt2(ok) == 1 } {
                   1202: 	addCurrentDates $listbox $makedefault
                   1203:     }    
                   1204:     return 
                   1205: }
                   1206: 
                   1207: ###########################################################
                   1208: # createDBHeader
                   1209: ###########################################################
                   1210: ###########################################################
                   1211: ###########################################################
                   1212: proc createDBHeader {} {
                   1213:     global gNumberParsedText gPrompt gLoadHeaderSet gControlDates \
1.2       albertel 1214: 	    gSetNumberText  gHeaderQCount gEnableDiscussion gFile
1.1       albertel 1215: 
                   1216:     if { $gNumberParsedText == "" } {
                   1217: 	displayError "You must first preview the file before creating the \
                   1218: 		DB header."
                   1219: 	return
                   1220:     }
                   1221: 
                   1222:     if { [winfo exists .headerPrompt] } {
                   1223: 	capaRaise .headerPrompt
                   1224: 	return
                   1225:     }
                   1226: 
                   1227:     set dialog [toplevel .headerPrompt -borderwidth 10]
                   1228:     wm geo $dialog "+200+200"
                   1229:     wm title $dialog "Creating DB Header"
                   1230: 
                   1231:     message $dialog.msg -text "Header Information" -aspect 1000
                   1232:     set loadFrame [frame $dialog.loadFrame -borderwidth 4 -relief sunken]
                   1233:     set infoFrame [frame $dialog.infoFrame -borderwidth 4 -relief sunken]
1.13      albertel 1234:     label $dialog.message -text "Later entries will override earlier entries"
1.2       albertel 1235:     set optionFrame [frame $dialog.options]
1.1       albertel 1236:     set buttonFrame [frame $dialog.buttons -bd 10]
1.13      albertel 1237:     pack $dialog.msg $loadFrame $dialog.message $infoFrame $optionFrame \
                   1238: 	$buttonFrame -side top -fill x
1.1       albertel 1239: 
                   1240:     set legendFrame [frame $infoFrame.legendFrame]
                   1241:     set listFrame [frame $infoFrame.listFrame]
                   1242:     set commandFrame [frame $infoFrame.commandFrame]
                   1243:     pack $legendFrame $listFrame $commandFrame -side top
                   1244:  
                   1245:     label $legendFrame.legend1 -text " Section# |     Open       |     Due        |     Answer         "
                   1246:     label $legendFrame.legend2 -text "Start  End| Date      Time | Date     Time  | Date      Time     "
                   1247:     pack $legendFrame.legend1 $legendFrame.legend2 -side top
                   1248: 
                   1249:     set listbox [listbox $listFrame.list -width 63 -yscrollcommand "$listFrame.scroll set" ]
                   1250:     scrollbar $listFrame.scroll -command "$listbox yview"
                   1251:     pack $listFrame.list $listFrame.scroll -side left
1.13      albertel 1252:     pack configure $listFrame.scroll -fill y
1.1       albertel 1253:     updateDateBox $listbox
                   1254:     
                   1255:     button $commandFrame.add -text "Add" -command "addDate $listbox"
                   1256:     button $commandFrame.change -text "Change" -command "changeDate $listbox"
                   1257:     button $commandFrame.delete -text "Delete" -command "deleteDate $listbox"
1.13      albertel 1258:     button $commandFrame.moveup -text "MoveUp" -command "moveUpDate $listbox"
                   1259:     button $commandFrame.movedown -text "MoveDown" -command "moveDownDate $listbox"
                   1260:     pack $commandFrame.add $commandFrame.change $commandFrame.delete \
                   1261: 	$commandFrame.moveup $commandFrame.movedown -side left
1.1       albertel 1262:     bind $listbox <Double-ButtonPress-1> "changeDate $listbox"
                   1263: 
                   1264:     message $loadFrame.msg -text "Load header information from set:"  \
                   1265: 	    -aspect 1000
                   1266:     set gLoadHeaderSet $gSetNumberText
                   1267:     entry $loadFrame.entry -textvariable gLoadHeaderSet  -width 2
                   1268:     button $loadFrame.load -text "load" -command "loadDates $listbox"
                   1269:     pack $loadFrame.msg $loadFrame.entry $loadFrame.load -side left
                   1270: 
1.2       albertel 1271:     if { [file exists [file join [file dirname $gFile] discussion $gSetNumberText]] } {
                   1272: 	set gEnableDiscussion 1
                   1273:     } else {
                   1274: 	set gEnableDiscussion 0
                   1275:     }
                   1276:     checkbutton $optionFrame.discuss -text "Enable Discussion Forum" \
                   1277: 	-variable gEnableDiscussion
                   1278:     pack $optionFrame.discuss
                   1279: 
1.1       albertel 1280:     button $buttonFrame.ok -text Set -command { set gPrompt(ok) 1 } \
                   1281: 	    -underline 0 
                   1282:     button $buttonFrame.cancel -text Cancel -command { set gPrompt(ok) 0 } \
                   1283: 	    -underline 0 
                   1284:     pack $buttonFrame.ok $buttonFrame.cancel -side left
                   1285:     
                   1286:     bind $dialog <Destroy> "set gPrompt(ok) 0"
                   1287:     Centre_Dialog $dialog default
                   1288:     update
                   1289: 
                   1290:     focus $dialog
                   1291:     capaRaise $dialog
                   1292:     capaGrab $dialog
                   1293:     bind $dialog <Destroy> ""
                   1294:     set done 0
                   1295:     while { $done != 1 } {
                   1296: 	vwait gPrompt(ok)
                   1297: 	if { $gPrompt(ok) == 1 } {
                   1298: 	    set done [checkHeaderForDefault]
                   1299: 	    if { $done == 0 } {
                   1300: 		displayError "Must have a Default setting."
                   1301: 	    }
                   1302: 	} else {
                   1303: 	    set done 1
                   1304: 	}
                   1305:     }
                   1306:     capaGrab release $dialog
                   1307:     destroy $dialog
                   1308:     if {$gPrompt(ok) == 1 } {
1.2       albertel 1309: 	updateDiscussion
1.13      albertel 1310: 	if { [llength $gControlDates] > 2 } {
                   1311: 	    set gControlDates [linsert [lreverse [lrange $gControlDates 1 end]] 0 [lindex $gControlDates 0]] 
                   1312: 	} 
1.1       albertel 1313: 	eval updateHeader [ eval concat $gControlDates ]
                   1314:     }
                   1315:     
                   1316:     return    
                   1317: 
                   1318: }
                   1319: 
                   1320: ###########################################################
1.2       albertel 1321: # updateDiscussion
                   1322: ###########################################################
                   1323: ###########################################################
                   1324: ###########################################################
                   1325: proc updateDiscussion {} {
                   1326:     global gFile gSetNumberText gEnableDiscussion
                   1327:     set dir [file dirname $gFile]
                   1328:     set disDir [file join $dir discussion $gSetNumberText]
                   1329:     set logDir [file join $dir discussion logs]
                   1330:     if { $gEnableDiscussion } {
                   1331: 	if { ![file exists $disDir] } {
                   1332: 	    if { [file exists $disDir.unavailable] } {
                   1333: 		exec mv $disDir.unavailable $disDir
                   1334: 	    } else {
                   1335: 		file mkdir $disDir
                   1336: 		file attributes $disDir -permissions 0777
                   1337: 	    }
                   1338: 	}
                   1339: 	if { ![file exists $logDir] } {
                   1340: 	    file mkdir [file join $dir discussion logs]
                   1341: 	    file attributes [file join $dir discussion logs] -permissions 0777
                   1342: 	}
                   1343:     } else { 
                   1344: 	if { [file exists $disDir] } { exec mv $disDir $disDir.unavailable } 
                   1345:     }
                   1346: }
                   1347: 
                   1348: ###########################################################
1.1       albertel 1349: # allFieldsComplete2 
                   1350: ###########################################################
                   1351: ###########################################################
                   1352: ###########################################################
                   1353: proc allFieldsComplete2 {} {
1.2       albertel 1354:     global gLoadHeaderSet gControlDates 
1.1       albertel 1355:     
                   1356:     if { [string length $gOpenDate] != 8 } {
                   1357: 	return 0
                   1358:     } elseif { [string length $gOpenTime] != 5 } {
                   1359: 	return 0
                   1360:     } elseif { [string length $gDueDate] != 8 } {
                   1361: 	return 0
                   1362:     } elseif { [string length $gDueTime] != 5 } {
                   1363: 	return 0
                   1364:     } elseif { [string length $gAnswerDate] != 8 } {
                   1365: 	return 0
                   1366:     } elseif { [string length $gAnswerTime] != 5 } {
                   1367: 	return 0
                   1368:     } else {
                   1369: 	return 1
                   1370:     }
                   1371: }
                   1372: 
                   1373: ###########################################################
                   1374: # createFindWindow
                   1375: ###########################################################
                   1376: ###########################################################
                   1377: ###########################################################
                   1378: proc createFindWindow { {num 0} } {
                   1379:     global gFind gWindowMenu gFindListbox
                   1380: 
                   1381:     if { [winfo exists .find] } { 
                   1382: 	capaRaise .find
                   1383: 	pickFindFile $num
                   1384: 	return 
                   1385:     }
                   1386: 
                   1387:     set find [toplevel .find]
                   1388:     $gWindowMenu add command -label "Find" -command "capaRaise $find"
                   1389:     wm title $find "Find"
                   1390: 
                   1391:     set findFrame [frame $find.findFrame -width 5i]
                   1392:     set replaceFrame [frame $find.replaceFrame ]
                   1393:     set optionsFrame [frame $find.optionsFrame ]
                   1394:     set buttonFrame [frame $find.buttonsFrame ]
                   1395:     pack $findFrame $replaceFrame $optionsFrame $buttonFrame -side top \
                   1396: 	    -anchor e
                   1397:     pack configure $buttonFrame -anchor center
                   1398: 
                   1399:     message $findFrame.msg -text "Find:" -aspect 10000 
                   1400:     entry $findFrame.entry -width 50  -textvariable gFind(find)
                   1401:     pack $findFrame.msg $findFrame.entry -side left
                   1402: 
                   1403:     message $replaceFrame.msg -text "Replace with:" -aspect 10000 
                   1404:     entry $replaceFrame.entry -width 50  -textvariable gFind(replace)
                   1405:     pack $replaceFrame.msg $replaceFrame.entry -side left
                   1406: 
                   1407:     set fileFrame [frame $optionsFrame.file]
                   1408:     set scopeFrame [frame $optionsFrame.scope -relief groove -borderwidth 4]
                   1409:     set findOptionsFrame [frame $optionsFrame.findOptionsFrame -relief \
                   1410: 	    groove -borderwidth 4]
                   1411:     pack $fileFrame $scopeFrame $findOptionsFrame -side left
                   1412: 
                   1413:     set fileList [ frame $fileFrame.list ]
                   1414:     set fileScroll [frame $fileFrame.scroll ]
                   1415:     pack $fileList $fileScroll -side left
                   1416:     pack configure $fileScroll -fill y
                   1417: 
                   1418:     set gFindListbox [listbox $fileList.list -width 35 -height 4 \
                   1419: 	    -xscrollcommand "$fileList.scroll set" \
                   1420: 	    -yscrollcommand "$fileScroll.scroll set" \
                   1421: 	    -exportselection no]
                   1422:     scrollbar $fileList.scroll -orient h -command "$fileList.list xview"
                   1423:     pack $fileList.list $fileList.scroll -side top
                   1424:     pack configure $fileList.scroll -fill x
                   1425:     pack configure $fileList.list -fill both -expand 1
                   1426:     $fileList.list xview moveto 1
                   1427: 
                   1428:     scrollbar $fileScroll.scroll -orient v \
                   1429: 	    -command "$fileList.list yview"
                   1430:     pack $fileScroll.scroll -fill y -expand 1
                   1431: 
                   1432:     message $scopeFrame.msg -text "Replace All Scope" -aspect 10000 
                   1433:     radiobutton $scopeFrame.file -value "File" -variable gFind(scope) -text \
                   1434: 	    "Entire File" \
                   1435: 	    
                   1436:     radiobutton $scopeFrame.selection -value "Selection" -variable \
                   1437: 	    gFind(scope) -text "Selection" 
                   1438:     pack $scopeFrame.msg $scopeFrame.file $scopeFrame.selection 
                   1439:     pack configure $scopeFrame.file $scopeFrame.selection -anchor w
                   1440:     set gFind(scope) File
                   1441: 
                   1442:     message $findOptionsFrame.msg -text "Find Options" -aspect 10000 
                   1443:     radiobutton $findOptionsFrame.ignoreCase -variable gFind(findOption) \
                   1444: 	    -text "Ignore Case" -value "-nocase"
                   1445:     radiobutton $findOptionsFrame.exactCase -variable gFind(findOption) \
                   1446: 	    -text "Exact Case" -value "-exact"
                   1447:     radiobutton $findOptionsFrame.regexp -variable gFind(findOption) \
                   1448: 	    -text "Regular Expression" -value "-regexp"
                   1449:     pack $findOptionsFrame.msg $findOptionsFrame.ignoreCase \
                   1450: 	    $findOptionsFrame.exactCase $findOptionsFrame.regexp 
                   1451:     pack $findOptionsFrame.ignoreCase $findOptionsFrame.exactCase \
                   1452: 	    $findOptionsFrame.regexp -anchor w
                   1453:     set gFind(findOption) "-nocase"
                   1454: 
                   1455:     button $buttonFrame.replaceAll -text "Replace All" -command "replaceAll" 
                   1456:     button $buttonFrame.replace -text "Replace" -command "replace" 
                   1457:     button $buttonFrame.replaceFind -text "Replace and Find" -command \
                   1458: 	    "replaceFind" 
                   1459:     button $buttonFrame.previous -text "Previous" -command "previous" 
                   1460:     button $buttonFrame.next -text "Next <Return>" -command "next" 
                   1461:     bind $find <KeyPress-Return> next
                   1462:     button $buttonFrame.close -text "Close" -command "removeWindowEntry Find
                   1463:                                                       destroy $find"
                   1464:     bind $find <Destroy> "removeWindowEntry Find"
                   1465:     pack $buttonFrame.replaceAll $buttonFrame.replace \
                   1466: 	    $buttonFrame.replaceFind $buttonFrame.previous \
                   1467: 	    $buttonFrame.next $buttonFrame.close -side left
                   1468: 
                   1469:     Centre_Dialog $find default
                   1470:     updateFindList
                   1471:     pickFindFile $num
                   1472: }
                   1473: 
                   1474: ###########################################################
                   1475: # pickFindFile
                   1476: ###########################################################
                   1477: ###########################################################
                   1478: ###########################################################
                   1479: proc pickFindFile { num } {
                   1480:     global gFindListbox gFindList gRefFile gFile
                   1481:     if { [catch {set gFindListbox}] } { return }
                   1482:     if { ![winfo exists $gFindListbox] } { return }
                   1483:     if { $num == 0 } {
                   1484: 	set newfile $gFile
                   1485:     } else {
                   1486: 	set newfile $gRefFile($num)
                   1487:     }
                   1488:     for {set i 0} {$i<[llength $gFindList(files)]} {incr i} {
                   1489: 	set file [lindex $gFindList(files) $i]
                   1490: 	if { $file == $newfile } { break }
                   1491:     }
                   1492:     if { $i < [llength $gFindList(files)] } { 
                   1493: 	$gFindListbox selection clear 0 end 
                   1494: 	$gFindListbox selection set $i 
                   1495:     }
                   1496: }
                   1497: 
                   1498: ###########################################################
                   1499: # updateFindList 
                   1500: ###########################################################
                   1501: ###########################################################
                   1502: ###########################################################
                   1503: proc updateFindList {} {
                   1504:     global gFindListbox gFindList
                   1505:     if { [catch {set gFindListbox}] } { return }
                   1506:     if { ![winfo exists $gFindListbox] } { return }
                   1507:     $gFindListbox delete 0 end
                   1508:     eval "$gFindListbox insert end $gFindList(files)"
                   1509:     $gFindListbox xview moveto 1
                   1510: }
                   1511: 
                   1512: ###########################################################
                   1513: # whichFile 
                   1514: ###########################################################
                   1515: ###########################################################
                   1516: ###########################################################
                   1517: proc whichFile { refNum } {
                   1518:     global gRefFile gFile
                   1519:     if { $refNum > 0 } {
                   1520: 	return $gRefFile($refNum)
                   1521:     } else {
                   1522: 	if { $refNum < 0 } {
                   1523: 	    switch -- $refNum {
                   1524: 		-1 { return "Preview Window" }
                   1525: 		-2 { return "Parse Errors Window" }
                   1526: 		-3 { return "LaTeX Output Window" }
                   1527: 	    }
                   1528: 	} else {
                   1529: 	    return $gFile
                   1530: 	}
                   1531:     }
                   1532: }
                   1533: ###########################################################
                   1534: # addFindList 
                   1535: ###########################################################
                   1536: ###########################################################
                   1537: ###########################################################
                   1538: proc addFindList { {refNum 0} } {
                   1539:     global gFindList gRefFile gFile
                   1540: 
                   1541:     set file [whichFile $refNum]
                   1542:     lappend gFindList(files) $file
                   1543:     lappend gFindList(refNum) $refNum
                   1544:     updateFindList
                   1545: }
                   1546: 
                   1547: ###########################################################
                   1548: # removeFindList 
                   1549: ###########################################################
                   1550: ###########################################################
                   1551: ###########################################################
                   1552: proc removeFindList { {refNum 0} } {
                   1553:     global gFindList gRefFile gFile
                   1554: 
                   1555:     set file [whichFile $refNum]
                   1556:     set k [llength $gFindList(refNum)]
                   1557:     for {set i 0} {$i < $k } { incr i } {
                   1558: 	if { $refNum == [lindex $gFindList(refNum) $i] } { break } 
                   1559:     }
                   1560:     if { $i != $k } {
                   1561: 	set gFindList(refNum) [lreplace $gFindList(refNum) $i $i]
                   1562: 	set gFindList(files) [lreplace $gFindList(files) $i $i]
                   1563:     } 
                   1564:     updateFindList
                   1565: }
                   1566: 
                   1567: ###########################################################
                   1568: # getFindWindow 
                   1569: ###########################################################
                   1570: ###########################################################
                   1571: ###########################################################
                   1572: proc getFindWindow { {refNumVar none} } {
                   1573:     global gFindListbox gFindList gRefText gTextWindow \
                   1574: 	gPreviewText gParseErrorsText gCreateDviText
                   1575: 
                   1576:     set current [$gFindListbox curselection]
                   1577:     if { $current == "" } { set current 0 }
                   1578:     if { [set refNum [lindex $gFindList(refNum) $current] ] } {
                   1579: 	if { $refNum < 0 } {
                   1580: 	    switch -- $refNum {
                   1581: 		-1 { set window $gPreviewText }
                   1582: 		-2 { set window $gParseErrorsText }
                   1583: 		-3 { set window $gCreateDviText }
                   1584: 	    }
                   1585: 	} else {
                   1586: 	    set window $gRefText($refNum)
                   1587: 	}
                   1588:     } else {
                   1589: 	set window $gTextWindow
                   1590:     }
                   1591:     if { $refNumVar != "none" } {
                   1592: 	upvar $refNumVar refNumUp
                   1593: 	set refNumUp $refNum
                   1594:     }
                   1595:     return $window
                   1596: }
                   1597: 
                   1598: ###########################################################
                   1599: # replaceAll 
                   1600: ###########################################################
                   1601: ###########################################################
                   1602: ###########################################################
                   1603: proc replaceAll {} {
                   1604:     global gFind gCreateImportLinks 
                   1605:     
                   1606:     set window [getFindWindow]
                   1607:     if { ![winfo exists $window] } { return }
                   1608: 
                   1609:     set gCreateImportLinks 0
                   1610:     set num 0
                   1611:     switch $gFind(scope) {
                   1612: 	File 
                   1613: 	{
                   1614: 	    $window mark set insert 0.0
                   1615: 	    set begin 0.0
                   1616: 	    while { [nextRegion $begin end] != "" } {
                   1617: 		incr num
                   1618: 		replace
                   1619: 		if { ! ($num%10) } { update idletasks }
                   1620: 		set begin sel.last
                   1621: 	    }
                   1622: 	}
                   1623: 	Selection
                   1624: 	{
                   1625: 	    set error [ catch {$window mark set replace sel.first}]
                   1626: 	    if { $error != 0 } { return }
                   1627: 	    $window mark set capaBegin sel.first
                   1628: 	    $window mark set capaEnd   sel.last
                   1629: 	    while { [set begin [nextRegion capaBegin capaEnd]] != "" } {
                   1630: 		incr num
                   1631: 		replace
                   1632: 		$window mark set capaBegin $begin
                   1633: 	    }
                   1634: 	}
                   1635:     }
                   1636:     if { $num == 1 } { set s {} } { set s s }    
                   1637:     update idletasks
                   1638:     set gCreateImportLinks 1
                   1639:     getFindWindow refNum
                   1640:     if { $refNum >= 0 } { registerCreateImportLinks $refNum 0.0 end }
1.12      albertel 1641:     displayMessage "Replaced $num occurrence$s"
1.1       albertel 1642: }
                   1643: 
                   1644: ###########################################################
                   1645: ###########################################################
                   1646: ###########################################################
                   1647: ###########################################################
                   1648: proc replace {} {
                   1649:     global gFind 
                   1650: 
                   1651:     set refNum 0
                   1652:     set window [getFindWindow refNum]
                   1653:     if { ![winfo exist $window] } { return }
                   1654: 
                   1655:     set error [ catch {$window mark set replace sel.first}]
                   1656:     if { $error == 0 } {
                   1657: 	$window delete sel.first sel.last
                   1658:     } else {
                   1659: 	$window mark set replace insert
                   1660:     }
                   1661: 
                   1662:     $window insert replace "$gFind(replace)"
                   1663:  
                   1664:     catch {$window tag remove sel sel.first sel.last}
                   1665: 
                   1666:     $window tag add sel "replace - [string length "$gFind(replace)"] \
                   1667: 	    chars " replace
                   1668:     $window see replace
                   1669: 
                   1670:     $window mark unset replace
                   1671: }
                   1672: 
                   1673: ###########################################################
                   1674: ###########################################################
                   1675: ###########################################################
                   1676: ###########################################################
                   1677: proc replaceFind {} {
                   1678:     set window [getFindWindow]
                   1679:     if { ![winfo exists $window] } { return }
                   1680:     replace
                   1681:     next
                   1682: }
                   1683: 
                   1684: ###########################################################
                   1685: ###########################################################
                   1686: ###########################################################
                   1687: ###########################################################
                   1688: proc searchBody { found } {
                   1689:     global gFind 
                   1690: 
                   1691:     set window [getFindWindow refNum]
                   1692:     if { ![winfo exists $window] } { return }
                   1693: 
                   1694:     catch {$window tag remove sel sel.first sel.last}
                   1695:     $window tag add sel $found "$found + [string length $gFind(find)] \
                   1696: 	    chars"
                   1697:     $window see $found
                   1698:     $window mark set insert "$found + [string length $gFind(find)] chars"
                   1699: }
                   1700: 
                   1701: ###########################################################
                   1702: ###########################################################
                   1703: ###########################################################
                   1704: ###########################################################
                   1705: proc previous {} {
                   1706:     global gFind
                   1707: 
                   1708:     set window [getFindWindow]
                   1709:     if { ![winfo exists $window] } { return }
                   1710: 
                   1711:     if { [catch { set found [$window search $gFind(findOption) -backwards -- \
                   1712: 	    $gFind(find) "sel.first - 1 c" ] } ] } {
                   1713: 	set found [ $window search $gFind(findOption) -backwards -- \
                   1714: 		$gFind(find) "insert - 1 c" ]
                   1715:     }
                   1716:     if { $found != "" } { searchBody $found }
                   1717:     return $found    
                   1718: }
                   1719: 
                   1720: ###########################################################
                   1721: ###########################################################
                   1722: ###########################################################
                   1723: ###########################################################
                   1724: proc next {} {
                   1725:     global gFind 
                   1726: 
                   1727:     set window [getFindWindow]
                   1728:     if { ![winfo exists $window] } { return }
                   1729: 
                   1730:     set found [ $window search $gFind(findOption) -forwards -- \
                   1731: 	    $gFind(find) "insert + 1 c" ]
                   1732:     if { $found != "" } { 
                   1733: 	searchBody $found 
                   1734:     } else { 
                   1735: 	displayMessage "Search String Not Found" 
                   1736:     }
                   1737:     return $found    
                   1738: }
                   1739: 
                   1740: ###########################################################
                   1741: ###########################################################
                   1742: ###########################################################
                   1743: ###########################################################
                   1744: proc nextRegion { begin end } {
                   1745:     global gFind 
                   1746: 
                   1747:     set window [getFindWindow]
                   1748:     if { ![winfo exists $window] } { return }
                   1749: 
                   1750:     set error [ catch {set found [ $window search $gFind(findOption) \
                   1751: 	    -forwards -- $gFind(find) $begin $end ] } ]
                   1752:     if { $error != 0 } { set found "" }
                   1753:     if { $found != "" } { 
                   1754: 	searchBody $found 
                   1755: 	set found "$found + [string length $gFind(find)] chars"
                   1756:     }
                   1757:     return $found
                   1758: }
                   1759: 
                   1760: ###########################################################
                   1761: ###########################################################
                   1762: ###########################################################
                   1763: ###########################################################
                   1764: proc createLineWindow {} {
                   1765:     global gLineNumber gCharacterNumber gLineNumberGoto gCharacterNumberGoto
                   1766:     global gWindowMenu
                   1767: 
                   1768:     if { [winfo exists .lineWindow] } { 
                   1769: 	capaRaise .lineWindow
                   1770: 	return 
                   1771:     }
                   1772: 
                   1773:     set lineWindow [toplevel .lineWindow]
                   1774:     $gWindowMenu add command -label "LineSelect" -command \
                   1775: 	    "capaRaise $lineWindow"
                   1776:     wm title $lineWindow "Select Line"
                   1777: 
                   1778:     label $lineWindow.line -text "Line:"
                   1779:     grid $lineWindow.line -column 1 -row 0
                   1780:     label $lineWindow.character -text "Character:"
                   1781:     grid $lineWindow.character -column 2 -row 0
                   1782:     label $lineWindow.current -text "Current:"
                   1783:     grid $lineWindow.current -column 0 -row 1
                   1784:     label $lineWindow.lineNumber -textvariable gLineNumber
                   1785:     grid $lineWindow.lineNumber -column 1 -row 1
                   1786:     label $lineWindow.characterNumber -textvariable gCharacterNumber
                   1787:     grid $lineWindow.characterNumber -column 2 -row 1
                   1788:     label $lineWindow.goto -text "Goto:"
                   1789:     grid $lineWindow.goto -column 0 -row 2
                   1790:     entry $lineWindow.lineEntry -textvariable gLineNumberGoto
                   1791:     grid $lineWindow.lineEntry -column 1 -row 2
                   1792:     set gLineNumberGoto ""
                   1793:     entry $lineWindow.characterEntry -textvariable gCharacterNumberGoto
                   1794:     grid $lineWindow.characterEntry -column 2 -row 2
                   1795:     set gCharacterNumberGoto ""
                   1796:     button $lineWindow.close -text "Close" -command "destroy $lineWindow 
                   1797: 	                                       removeWindowEntry LineSelect"
                   1798:     bind $lineWindow <Destroy> "removeWindowEntry LineSelect"
                   1799:     grid $lineWindow.close -column 1 -row 3
                   1800:     button $lineWindow.gotoButton -text "Goto<Return>" -command "gotoLine"
                   1801:     grid $lineWindow.gotoButton -column 2 -row 3    
                   1802:     
                   1803:     bind $lineWindow <KeyPress-Return> gotoLine
                   1804: 
                   1805:     Centre_Dialog $lineWindow default
                   1806: }
                   1807: 
                   1808: ###########################################################
                   1809: ###########################################################
                   1810: ###########################################################
                   1811: ###########################################################
                   1812: proc gotoLine {} {
                   1813:     global gTextWindow gLineNumberGoto gCharacterNumberGoto
                   1814:     if { [catch {set gTextWindow}] } { return }
                   1815:     if { ![winfo exists $gTextWindow] } { return }
                   1816: 
                   1817:     if { $gCharacterNumberGoto == "" } {
                   1818: 	if { $gLineNumberGoto == "" } {
                   1819: 	    return
                   1820: 	} else {
                   1821: 	    $gTextWindow mark set insert $gLineNumberGoto.0
                   1822: 	    catch {$gTextWindow tag remove sel sel.first sel.last}
                   1823: 	    $gTextWindow tag add sel "insert linestart" "insert lineend"
                   1824: 	    $gTextWindow see insert
                   1825: 	} 
                   1826:     } else {
                   1827: 	if { $gLineNumberGoto == "" } {
                   1828: 	    $gTextWindow mark set insert "insert linestart + \
                   1829: 		    $gCharacterNumberGoto chars"
                   1830: 	    catch {$gTextWindow tag remove sel sel.first sel.last}
                   1831: 	    $gTextWindow tag add sel "insert - 1 chars " insert
                   1832: 	    $gTextWindow see insert
                   1833: 	} else {
                   1834: 	    $gTextWindow mark set insert $gLineNumberGoto.$gCharacterNumberGoto
                   1835: 	    catch {$gTextWindow tag remove sel sel.first sel.last}
                   1836: 	    $gTextWindow tag add sel "$gLineNumberGoto.$gCharacterNumberGoto \
                   1837: 		    - 1 chars " "$gLineNumberGoto.$gCharacterNumberGoto"
                   1838: 	    $gTextWindow see insert
                   1839: 	} 
                   1840:     }
                   1841: }
                   1842: 
                   1843: proc faster {} {
                   1844:     global gFasterParsing
                   1845:     puts $gFasterParsing
                   1846: }
                   1847: 
                   1848: ###########################################################
                   1849: # createPrefsWindow
                   1850: ###########################################################
                   1851: ###########################################################
                   1852: ###########################################################
                   1853: proc createPrefsWindow {} {
                   1854:     global gPrefs gWindowMenu gEditWindow gFile gWhichFile gPrefsEditWindow \
                   1855: 	gFasterParsing
                   1856:     if { [catch {set gEditWindow}] } { return }
                   1857:     if { ![winfo exists $gEditWindow] } { return }
                   1858:     if { [winfo exists .prefs] } { capaRaise .prefs; return }
                   1859: 
                   1860:     set prefs [toplevel .prefs]
1.13      albertel 1861:     $gWindowMenu add command -label "Prefernces" -command "capaRaise $prefs"
1.1       albertel 1862:     wm title $prefs "Preferences"
                   1863: 
                   1864:     set frameAll [frame $prefs.frameAll -relief groove -borderwidth 4]
                   1865:     pack $frameAll -expand true -fill both
                   1866: 
                   1867:     set frameFile [frame $frameAll.file]
                   1868:     set frameInfo [frame $frameAll.info -relief groove -borderwidth 4 ]
                   1869:     set frameButton [frame $frameAll.button ]
                   1870:     pack $frameButton $frameInfo $frameFile -side top -expand false
                   1871:     pack configure $frameButton -expand false -anchor center
                   1872: 
                   1873:     message $frameInfo.msg -text "Print Out"
                   1874:     radiobutton $frameInfo.problem -text "Problems Only" -value "Problem" \
                   1875: 	    -variable gPrefs(info) 
                   1876:     radiobutton $frameInfo.problemandanswer -text "Problems and Answers" \
                   1877: 	    -value "ProblemAnswer" -variable gPrefs(info) 
                   1878:     radiobutton $frameInfo.answer -text "Answers Only" -value "Answer" \
                   1879: 	    -variable gPrefs(info) 
                   1880:     pack $frameInfo.msg $frameInfo.problem $frameInfo.problemandanswer \
                   1881: 	    $frameInfo.answer -side left -expand false -anchor w
                   1882: 
                   1883:     
                   1884:     set selectMenu [tk_optionMenu $frameFile.menu gWhichFile HTMLheader HTMLfooter \
                   1885: 			TeXheader TeXfooter]
                   1886:     set frameEdit [frame $frameFile.edit]
                   1887:     pack $frameFile.menu $frameEdit
                   1888:     pack configure $frameEdit -expand true -fill both
                   1889:     trace variable gWhichFile w changePrefFile
                   1890: 
                   1891:     scrollbar $frameEdit.scroll -orient vertical -command "$frameEdit.text yview"
                   1892:     set gPrefsEditWindow [text $frameEdit.text -yscrollcommand \
                   1893: 	    "$frameEdit.scroll set" -wrap char -height 20 -width 80]
                   1894:     pack $frameEdit.scroll $frameEdit.text -side left
                   1895:     pack configure $frameEdit.scroll -expand false -fill y
                   1896:     pack configure $frameEdit.text -expand true -fill both
                   1897: 
                   1898:     checkbutton $frameButton.faster -text "Faster Parsing" -command faster \
                   1899: 	-variable gFasterParsing
                   1900:     button $frameButton.impcolor -text "/IMP color" -command "getColor IMP_color"
                   1901:     button $frameButton.commentcolor -text "// color" -command "getColor comment_color"
                   1902:     button $frameButton.config -text "Reread capa.config" -command "rereadCapaConfig"
                   1903:     button $frameButton.ok -text "Dismiss" -command "destroy $prefs
                   1904:                                                 trace vdelete gWhichFile w changePrefFile
1.13      albertel 1905:                                                 removeWindowEntry Prefernces"
1.1       albertel 1906:     bind $prefs <Destroy> "removeWindowEntry Preferences"
                   1907:     button $frameButton.save -text "Save All" -command "savePrefs"
                   1908:     pack $frameButton.impcolor $frameButton.commentcolor $frameButton.config \
                   1909: 	$frameButton.ok $frameButton.save $frameButton.faster -side left
                   1910: 
                   1911:     foreach file {HTMLheader HTMLfooter TeXheader TeXfooter} {
                   1912: 	if { [ catch {
                   1913: 	    set filename [file join [file dirname $gFile] $file ]
                   1914: 	    set fileId [open $filename r]
                   1915: 	    set gPrefs($file) [read $fileId [file size $filename ]]
                   1916: 	    close $fileId } errors ] } { 
                   1917: 	    set gPrefs($file) ""
                   1918: 	}
                   1919:     }
                   1920:     set gPrefs(currentFile) ""
                   1921:     set gWhichFile HTMLheader
                   1922: 
                   1923: 
                   1924:     Centre_Dialog $prefs default
                   1925: }
                   1926: 
                   1927: ###########################################################
                   1928: # getColor
                   1929: ###########################################################
                   1930: ###########################################################
                   1931: ###########################################################
                   1932: proc getColor { whatfor } {
                   1933:     global gCapaConfig gUniqueNumber gRefText
                   1934:     set color [tk_chooseColor -initialcolor $gCapaConfig($whatfor)]
                   1935:     set gCapaConfig($whatfor) $color 
                   1936:     if { $color != "" } { updateColors }
                   1937:     displayMessage "To keep this color, put \"$whatfor = $color\" in the capa.config file."
                   1938: }
                   1939: 
                   1940: ###########################################################
                   1941: # updateColors
                   1942: ###########################################################
                   1943: ###########################################################
                   1944: ###########################################################
                   1945: proc updateColors {} {
                   1946:     global gCapaConfig gUniqueNumber gRefText
                   1947:     set todo [array names gRefText]
                   1948:     lappend todo 0
                   1949:     displayStatus "Updating Colors . . ." both
                   1950:     set num 0
                   1951:     foreach win $todo {
                   1952: 	createImportLinks $win 0.0 end
                   1953: 	incr num
                   1954: 	updateStatusBar [expr $num/double([llength $todo])]
                   1955:     }
                   1956:     removeStatus
                   1957: }
                   1958: 
                   1959: ###########################################################
                   1960: # changePrefFile
                   1961: ###########################################################
                   1962: ###########################################################
                   1963: ###########################################################
                   1964: proc changePrefFile { var1 var2 op } {
                   1965:     global gPrefs gPrefsEditWindow gFile gWhichFile
                   1966: 
                   1967:     if { $gPrefs(currentFile) != "" } {
                   1968: 	set  gPrefs($gPrefs(currentFile)) [$gPrefsEditWindow get 0.0 end-1c]
                   1969:     }
                   1970:     set gPrefs(currentFile) $gWhichFile
                   1971:     $gPrefsEditWindow delete 0.0 end
                   1972:     $gPrefsEditWindow insert 0.0 $gPrefs($gWhichFile)
                   1973: }
                   1974: 
                   1975: ###########################################################
                   1976: # updatePrefsWindow 
                   1977: ###########################################################
                   1978: ###########################################################
                   1979: ###########################################################
                   1980: proc updatePrefsWindow {} {
                   1981:     global gPrefs gPrefsEditWindow gFile gWhichFile
                   1982:     if { [catch {set gPrefsEditWindow}] } { return }
                   1983:     if { ![winfo exists $gPrefsEditWindow] } { return }
                   1984: 
                   1985:     foreach file {HTMLheader HTMLfooter TeXheader TeXfooter} {
                   1986: 	if { [ catch {
                   1987: 	    set filename [file join [file dirname $gFile] $file ]
                   1988: 	    set fileId [open $filename r]
                   1989: 	    set gPrefs($file) [read $fileId [file size $filename]]
                   1990: 	    close $fileId } ] } { 
                   1991: 	    set gPrefs($file) ""
                   1992: 	}
                   1993:     }
                   1994:     $gPrefsEditWindow delete 0.0 end
                   1995:     $gPrefsEditWindow insert 0.0 $gPrefs($gWhichFile)
                   1996: }
                   1997: 
                   1998: ###########################################################
                   1999: ###########################################################
                   2000: ###########################################################
                   2001: ###########################################################
                   2002: proc savePrefs {} {
                   2003:     global gPrefsEditWindow gFile gPrefs 
                   2004:     if { [catch {set gPrefsEditWindow}] } { return }
                   2005:     if { ![winfo exists $gPrefsEditWindow] } { return }
                   2006:     if { $gPrefs(currentFile) != "" } {
                   2007: 	set  gPrefs($gPrefs(currentFile)) [$gPrefsEditWindow get 0.0 end-1c]
                   2008:     }
                   2009:     foreach file {HTMLheader HTMLfooter TeXheader TeXfooter} {
                   2010: 	if { $gPrefs($file) != "" } {
                   2011: 	    set fileId [open [file join [file dirname $gFile] $file ] w]
                   2012: 	    puts -nonewline $fileId $gPrefs($file)
                   2013: 	    close $fileId
                   2014: 	} else {
                   2015: 	    exec rm -f [file join [file dirname $gFile] $file ]
                   2016: 	}
                   2017:     }
                   2018: }
                   2019: 
                   2020: ###########################################################
                   2021: # checkHeader
                   2022: ###########################################################
                   2023: ###########################################################
                   2024: ###########################################################
                   2025: proc checkHeader { numberParsed } {
                   2026:     global gWeightsDiffer gPartialDiffer gSetNumberText gHeaderQCount \
                   2027: 	gControlDates gLoadHeaderSet gFirstTime
                   2028: 
                   2029: #    if { $gFirstTime } { set gFirstTime 0; return }
                   2030:     set gLoadHeaderSet $gSetNumberText
                   2031:     set error [catch {getHeaderInfo}]
1.13      albertel 2032:     if { [llength $gControlDates] > 2 } {
                   2033: 	set gControlDates [linsert [lreverse [lrange $gControlDates 1 end]] 0 [lindex $gControlDates 0]] 
                   2034:     } 
1.1       albertel 2035:     if { $error == 1 } {
                   2036: 	set gHeaderQCount "0"
                   2037: 	set gControlDates ""
                   2038: 	displayError "The db file for this set does not yet exist."
                   2039:     } else {
                   2040: 	set errortext ""
                   2041: 	if { ( $numberParsed != $gHeaderQCount ) } {
                   2042: 	    set error 1
                   2043: 	    append errortext "Number of questions ($numberParsed) is different from the number in setX.db ($gHeaderQCount). "    
                   2044: 	} 
                   2045: 	if { $gWeightsDiffer } {
                   2046: 	    set error 1
                   2047: 	    append errortext "The problem weights specified in the QZ file are different from the ones in the DB file. "
                   2048: 	} 
                   2049: 	if { $gPartialDiffer } {
                   2050: 	    set error 1
                   2051: 	    append errortext "Whether or not a problem is hand graded as specified in the QZ file is different from the DB file. "
                   2052: 	}
                   2053: 	if { $error } {
                   2054: 	    displayError "The curent DB Header does not match what the set file says it should be: $errortext. Set the DB Header!" red
                   2055: 	}
                   2056:     }
                   2057:     return $error
                   2058: }
                   2059: 
                   2060: ###########################################################
                   2061: # fillInStudentName
                   2062: ###########################################################
                   2063: ###########################################################
                   2064: ###########################################################
                   2065: #proc notherefillInStudentName { v } {
                   2066: #    global $v
                   2067: 
                   2068: #    set student [capaGetStudent [set ${v}(studentNumber)]]
                   2069: #    set ${v}(studentName) [lindex $student [expr [llength $student] - 1] ]
                   2070: #}
                   2071: 
                   2072: ###########################################################
                   2073: # studentSelectWindow
                   2074: ###########################################################
                   2075: ###########################################################
                   2076: ###########################################################
                   2077: proc studentSelectWindow { followupCommand } {
                   2078:     global gStudentSelection gChanged gWindowMenu gEditWindow
                   2079:     if { [catch {set gEditWindow}] } { return }
                   2080:     if { ![winfo exists $gEditWindow] } { return }
                   2081:     if { $gChanged } { if { [askToSave 0 0] == "Cancel" } { return } }
                   2082: 
                   2083:     if { [winfo exists .studentSelect] } { 
                   2084: 	capaRaise .studentSelect
                   2085: 	return
                   2086:     }
                   2087:     set student [toplevel .studentSelect]
                   2088:     $gWindowMenu add command -label "SelectStudent" \
                   2089: 	    -command "capaRaise $student"
                   2090:     wm title $student "Select Student"
                   2091: 
                   2092:     message $student.msg -text "Please specify a student to preview" \
                   2093: 	    -aspect 10000
                   2094:     set infoFrame [frame $student.frame -relief groove -borderwidth 4]
                   2095:     set buttonFrame [frame $student.buttonFrame ]
                   2096:     pack $student.msg $infoFrame $buttonFrame -side top 
                   2097: 
                   2098:     button $buttonFrame.ok -text "Preview" -command \
                   2099: 	"selectStudentPreview $student $followupCommand"
                   2100:     button $buttonFrame.cancel -text "Cancel" -command \
                   2101: 	    "destroy $student
                   2102:              trace vdelete gStudentSelection(studentNumber) w \"global gStudentSelection; set gStudentSelection(type) Specific ;#\"
                   2103:              trace vdelete gStudentSelection(studentName) w \"global gStudentSelection; set gStudentSelection(type) Specific ;#\"
                   2104:              removeWindowEntry SelectStudent"
                   2105:     bind $student <Destroy> \
                   2106: 	    "trace vdelete gStudentSelection(studentNumber) w \"global gStudentSelection; set gStudentSelection(type) Specific ;#\"
                   2107:              trace vdelete gStudentSelection(studentName) w \"global gStudentSelection; set gStudentSelection(type) Specific ;#\"
                   2108:              removeWindowEntry SelectStudent"
                   2109:     pack $buttonFrame.ok $buttonFrame.cancel -side left
                   2110: 
                   2111:     set randomAnyFrame [frame $infoFrame.randomany]
                   2112:     set randomFrame [frame $infoFrame.random]
                   2113:     set specificFrame [frame $infoFrame.specific]
                   2114:     set sectionFrame [frame $infoFrame.section]
                   2115: #    pack $randomAnyFrame  $randomFrame $specificFrame $sectionFrame -side top
                   2116:     pack $randomFrame $specificFrame -side top
                   2117:     pack configure $specificFrame -expand true -fill both
                   2118: 
                   2119:     radiobutton $randomAnyFrame.random -text "Randomly select a student" \
                   2120: 	-value "RandomAny" -variable gStudentSelection(type) 
                   2121:     pack $randomAnyFrame.random
                   2122: 
                   2123:     radiobutton $randomFrame.random -text "Randomly select one student \
                   2124: 	    from section:" -value "Random" -variable gStudentSelection(type) 
                   2125:     entry $randomFrame.entry -textvariable gStudentSelection(random)  -width 3
                   2126:     pack $randomFrame.random $randomFrame.entry -side left 
                   2127: 
                   2128:     radiobutton $specificFrame.specific -text "Specify the student by:" \
                   2129: 	    -value "Specific" -variable gStudentSelection(type) 
                   2130:     set studentNumber [frame $specificFrame.studentNumber]
                   2131:     set fullName [frame $specificFrame.fullName]
                   2132:     pack $specificFrame.specific $studentNumber $fullName -side top
                   2133:     pack configure $specificFrame.specific -anchor w
                   2134:     pack configure $studentNumber $fullName -anchor e
                   2135: 
                   2136:     radiobutton $sectionFrame.section 
                   2137:     message $studentNumber.msg -text "Student Number: "  -aspect 10000
                   2138:     entry $studentNumber.entry -textvariable gStudentSelection(studentNumber) \
                   2139: 	    -width 9 -validate key -validatecommand "limitEntry %W 9 any %P"
                   2140:     pack $studentNumber.msg $studentNumber.entry -side left
                   2141: 
                   2142:     message $fullName.msg -text "Student Name: "  -aspect 10000 
                   2143:     entry $fullName.msg2 -textvariable gStudentSelection(studentName) -width 35 \
                   2144: 	-validate key -validatecommand "limitEntry %W 35 any %P"
                   2145:     pack $fullName.msg $fullName.msg2 -side left
                   2146:     
                   2147:     trace variable gStudentSelection(studentNumber) w \
                   2148: 	"global gStudentSelection; set gStudentSelection(type) Specific ;#"
                   2149:     trace variable gStudentSelection(studentName) w \
                   2150: 	"global gStudentSelection; set gStudentSelection(type) Specific ;#"
                   2151: 
                   2152:     bind $studentNumber.entry <KeyPress-Return> \
                   2153: 	"fillInStudent gStudentSelection(studentName) gStudentSelection(studentNumber) 0"
                   2154:     bind $fullName.msg2 <KeyPress-Return> \
                   2155: 	"fillInStudent gStudentSelection(studentName) gStudentSelection(studentNumber) 1"
                   2156: #    $specificFrame.specific configure -command \
                   2157: 	"$studentNumber.entry configure -state normal"
                   2158: #    $randomFrame.random configure -command \
                   2159: 	"$studentNumber.entry configure -state disabled"
                   2160: 
                   2161:     Centre_Dialog $student default
                   2162: }
                   2163: 
                   2164: ###########################################################
                   2165: # selectStudentPreview
                   2166: ###########################################################
                   2167: ###########################################################
                   2168: ###########################################################
                   2169: proc selectStudentPreview { student followupCommand} {
                   2170:     global gStudentSelection
                   2171:     destroy $student
                   2172:     if { $gStudentSelection(type) == "Specific" } { 
                   2173: 	if {$gStudentSelection(studentNumber) == ""} {
                   2174: 	    fillInStudent gStudentSelection(studentName) \
                   2175: 		gStudentSelection(studentNumber) 1
                   2176: 	} else {
                   2177: 	    fillInStudent gStudentSelection(studentName) \
                   2178: 		gStudentSelection(studentNumber) 0
                   2179: 	}
                   2180: 	if {$gStudentSelection(studentNumber) == ""} { return }
                   2181:     }
                   2182:     removeWindowEntry SelectStudent
                   2183:     $followupCommand
                   2184: }
                   2185: 
                   2186: ###########################################################
                   2187: # createPreviewWindow
                   2188: ###########################################################
                   2189: ###########################################################
                   2190: ###########################################################
                   2191: proc createPreviewWindow {} {
                   2192:     global gPreviewMode gPreviewText gPrefs gSetNumberText gStudentSelection
                   2193:     global gWindowMenu gNumberParsedText    gNumber    
                   2194:     global gLoadHeaderSet gHeaderQCount gControlDates
                   2195:     
                   2196:     if { ![winfo exists .preview] } {
                   2197: 	
                   2198: 	set previewWindow [toplevel .preview]
                   2199:         $gWindowMenu add command -label "Preview" -command \
                   2200: 		"capaRaise $previewWindow"
                   2201: 	wm title $previewWindow "Preview"
                   2202: 	addFindList -1
                   2203: 
                   2204: 	set windowFrame [frame $previewWindow.windowFrame]
                   2205: 	set buttonFrame [frame $previewWindow.buttonFrame]
                   2206: 	
                   2207: 	pack $windowFrame $buttonFrame -side bottom 
                   2208: 	pack configure $windowFrame -expand true -fill both
                   2209: 	pack configure $buttonFrame -anchor e
                   2210: 
                   2211: 	scrollbar $windowFrame.scroll -orient vertical -command \
                   2212: 		"$windowFrame.text yview"
                   2213: 	set gPreviewText [text $windowFrame.text -yscrollcommand \
                   2214: 		"$windowFrame.scroll set" -wrap char -height 40]
                   2215: 	
                   2216: 	pack $windowFrame.scroll $gPreviewText -side left -expand 0
                   2217: 	pack configure $windowFrame.scroll -expand 0 -fill y
                   2218: 	pack configure $gPreviewText -expand true -fill both
                   2219: 	
                   2220: 	button $buttonFrame.ok -text Dismiss -command "destroy $previewWindow
                   2221: 	                                          removeWindowEntry Preview
                   2222:                                                   removeFindList -1"
                   2223: 	bind $previewWindow <Destroy> "removeWindowEntry Preview
                   2224:                                        removeFindList -1"
                   2225: 	button $buttonFrame.save -text "Save Output" -command "saveText $gPreviewText"
                   2226: 	button $buttonFrame.stop -text "Stop Parser" -command "stopParser"
                   2227: 	button $buttonFrame.print -text "Print Output" -command "printText $gPreviewText"
                   2228: 	pack $buttonFrame.print $buttonFrame.save $buttonFrame.stop \
                   2229: 	    $buttonFrame.ok -side left
                   2230: 	
                   2231: 	Centre_Dialog $previewWindow default
                   2232: 	wm withdraw $previewWindow
                   2233: 	update idletasks
                   2234: 	set win_width [winfo reqwidth $previewWindow]
                   2235: 	set win_height [winfo reqheight $previewWindow]
                   2236: 	wm geometry $previewWindow +[expr [winfo rootx $previewWindow] - \
                   2237: 		100]+[winfo rooty $previewWindow]
                   2238: 	wm deiconify $previewWindow
                   2239: 	update
                   2240:     } else {
                   2241: 	set previewWindow .preview
                   2242: 	$gPreviewText delete 0.0 end
                   2243: 	update
                   2244:     }
                   2245: 
                   2246:     switch $gPrefs(info) {
                   2247: 	Problem { set type 0 }
                   2248: 	ProblemAnswer { set type 1 }
                   2249: 	Answer { set type 2 }
                   2250:     }
                   2251:     grab .preview
                   2252:     if { [catch {
                   2253: 	switch $gPreviewMode {
                   2254: 	    Enscript
                   2255: 	    {
                   2256: 		set numberParsed [enscriptParse $type $gSetNumberText \
                   2257: 				  $gStudentSelection(type) $gStudentSelection(random) \
                   2258: 				  $gStudentSelection(studentNumber) \
                   2259: 				  $gStudentSelection(studentName) gPreviewText]
                   2260: 	    }
                   2261: 	    TeX
                   2262: 	    {
                   2263: 		set numberParsed [texParse $type $gSetNumberText \
                   2264: 				  $gStudentSelection(type) $gStudentSelection(random) \
                   2265: 				  $gStudentSelection(studentNumber) \
                   2266: 				  $gStudentSelection(studentName) gPreviewText]
                   2267: 	    }
                   2268: 	    Web
                   2269: 	    {
                   2270: 		set numberParsed [webParse $type $gSetNumberText \
                   2271: 				  $gStudentSelection(type) $gStudentSelection(random) \
                   2272: 				  $gStudentSelection(studentNumber) \
                   2273: 				  $gStudentSelection(studentName) gPreviewText]
                   2274: 	    }
                   2275: 	} }]} { return }
                   2276:     grab release .preview
                   2277:     if { $numberParsed == -1 } {
                   2278: 	destroy $previewWindow
                   2279: 	removeWindowEntry Preview
                   2280: 	return
                   2281:     }
                   2282:     checkHeader $numberParsed
                   2283:     $gPreviewText tag configure problem 
                   2284:     $gPreviewText tag configure answer 
                   2285:     capaRaise $previewWindow
                   2286: 
                   2287:     set gNumberParsedText $numberParsed
                   2288:     showParseErrors 
                   2289: }
                   2290: 
                   2291: ###########################################################
1.6       albertel 2292: # openError
                   2293: ###########################################################
                   2294: ###########################################################
                   2295: ###########################################################
                   2296: proc openError { file line type} {
                   2297:     global gRefLine gLineNumberGoto gTextWindow
                   2298:     if { $type == 2 } {
                   2299: 	set gLineNumberGoto $line
                   2300: 	gotoLine
                   2301: 	capaRaise [winfo toplevel $gTextWindow]
                   2302:     } else {
                   2303: 	if {[set num [openReferenceFile $file]]} {
                   2304: 	    update idletasks
                   2305: 	    set gRefLine($num) $line
                   2306: 	    gotoRefLine $num
                   2307: 	}
                   2308:     }
                   2309: }
                   2310: 
                   2311: ###########################################################
                   2312: # showParseErrors
1.1       albertel 2313: ###########################################################
                   2314: ###########################################################
                   2315: ###########################################################
                   2316: proc showParseErrors {} {
1.6       albertel 2317:     global gParseErrorsText gWindowMenu gUniqueNumber gCapaConfig
1.1       albertel 2318: 
                   2319:     set parseErrors [getParseErrors]
                   2320:     
                   2321:     if { $parseErrors != "" } {
                   2322: 
                   2323: 	if { ![winfo exists .parseErrors] } {
                   2324: 	    
                   2325: 	    set parseErrorsWindow [toplevel .parseErrors]
                   2326: 	    $gWindowMenu add command -label "ParseErrors" -command "capaRaise \
                   2327: 		    $parseErrorsWindow"
                   2328: 	    wm title $parseErrorsWindow "Parse Errors"
                   2329: 	    addFindList -2
                   2330: 
                   2331: 	    set windowFrame [frame $parseErrorsWindow.windowFrame]
                   2332: 	    set buttonFrame [frame $parseErrorsWindow.buttonFrame]
                   2333: 	    
                   2334: 	    pack $windowFrame $buttonFrame -side bottom 
                   2335: 	    pack configure $windowFrame -expand true -fill both
                   2336: 	    pack configure $buttonFrame -anchor e
                   2337: 
                   2338: 	    scrollbar $windowFrame.scroll -orient vertical -command \
                   2339: 		    "$windowFrame.text yview"
                   2340: 	    set gParseErrorsText [text $windowFrame.text -yscrollcommand \
                   2341: 		    "$windowFrame.scroll set" -wrap char -height 40]
                   2342: 	    
                   2343: 	    pack $windowFrame.scroll $gParseErrorsText -side left -expand 0
                   2344: 	    pack configure $windowFrame.scroll -expand 0 -fill y
                   2345: 	    pack configure $gParseErrorsText -expand true -fill both
                   2346: 	    
                   2347: 	    button $buttonFrame.ok -text Dismiss -command \
                   2348: 		    "destroy $parseErrorsWindow
                   2349: 	             removeWindowEntry ParseErrors
                   2350:                      removeFindList -2"
                   2351: 	    bind $parseErrorsWindow <Destroy> "removeWindowEntry ParseErrors
                   2352:                                                removeFindList -2"
                   2353: 	    button $buttonFrame.save -text "Save Output" \
                   2354: 		    -command "saveText $gParseErrorsText"
                   2355: 	    button $buttonFrame.print -text "Print Output" \
                   2356: 		    -command "printText $gParseErrorsText"
                   2357: 	    pack $buttonFrame.print $buttonFrame.save \
                   2358: 		    $buttonFrame.ok -side left
                   2359: 	
                   2360: 	    Centre_Dialog $parseErrorsWindow default
                   2361: 	    update
                   2362: 	    capaRaise $parseErrorsWindow
                   2363: 	} else {
                   2364: 	    $gParseErrorsText delete 0.0 end
                   2365: 	    capaRaise .parseErrors
                   2366: 	}
1.5       albertel 2367: 	foreach line [split $parseErrors "\n"] {
1.6       albertel 2368: 	    set tag 0
                   2369: 	    if { [regexp -indices {File:.+->(.+), Line ([0-9]+): ERROR:} $line result file linenum]} {
                   2370: 		set tag 1
1.5       albertel 2371: 	    } else {
1.6       albertel 2372: 		if { [regexp -indices {File:(.+), Line ([0-9]+): ERROR:} $line result file linenum]} {
                   2373: 		    set tag 2
1.5       albertel 2374: 		}
                   2375: 	    }
1.6       albertel 2376: 	    if { $tag } {
                   2377: 		set tagnum [incr gUniqueNumber]
                   2378: 		set linenum [eval [list string range $line] $linenum]
                   2379: 		set filename [eval [list string range $line] $file]
                   2380: 		set i [expr [lindex [split [$gParseErrorsText index end] .] 0] - 1]
                   2381: 	    }
                   2382: 	    $gParseErrorsText insert end "$line\n"
                   2383: 	    if { $tag } {
                   2384: 		$gParseErrorsText tag add error.$tagnum $i.[lindex $file 0] $i.[expr [lindex $file 1] + 1]
                   2385: 		$gParseErrorsText tag configure error.$tagnum -foreground $gCapaConfig(IMP_color)
                   2386: 		$gParseErrorsText tag bind error.$tagnum <Double-ButtonPress> \
                   2387: 		    "eval openError $filename $linenum $tag"
                   2388: 	    }
1.5       albertel 2389: 	}
1.1       albertel 2390:     } else {
                   2391: 	if { [winfo exists .parseErrors] } { $gParseErrorsText delete 0.0 end }
                   2392:     }
                   2393: 
                   2394:     return $parseErrors
                   2395: }
                   2396: 
                   2397: ###########################################################
                   2398: # printText
                   2399: ###########################################################
                   2400: # prints the contents of the text window, creates a temp file named
                   2401: # quiztemp.txt
                   2402: ###########################################################
                   2403: # Arguments: window (name of text window to print the contents of.
                   2404: # Returns  : nothing
                   2405: # Globals  :
                   2406: ###########################################################
                   2407: proc printText { window } {
                   2408: 
                   2409:     if { ![winfo exists $window] } {
                   2410: 	return
                   2411:     }
                   2412:     set lprCommand [getLprCommand quiztemp.txt]
                   2413: 
                   2414:     if {$lprCommand == "Cancel"} { 
                   2415: 	return
                   2416:     }
                   2417:   
                   2418:     set fileId [open "quiztemp.txt" w]
                   2419:     puts -nonewline $fileId "[ $window get 0.0 end ]"
                   2420:     close $fileId
                   2421: 
                   2422:     set errorMsg ""
                   2423:     set error [catch {set output [ eval "exec $lprCommand" ] } errorMsg ]
                   2424:     
                   2425:     if { $error == 1 } {
                   2426:         displayError "An error occurred while printing: $errorMsg"
                   2427:     } else {
                   2428: 	displayMessage "Print job sent to the printer.\n $output"
                   2429:     }
                   2430:     exec rm -f quiztemp.txt
                   2431: }
                   2432: 
                   2433: ###########################################################
                   2434: # saveText
                   2435: ###########################################################
                   2436: # saves the contents of the text window
                   2437: ###########################################################
                   2438: # Arguments: window (name of text window to save the contents of.
                   2439: #            saveAs (whether to ask to save)
                   2440: #            refNum (if supplied reference file unique number
                   2441: #                    that is being saved)
                   2442: # Returns  : name of the file saved
                   2443: # Globals  :
                   2444: ###########################################################
                   2445: proc saveText { window {saveAs 1} {refNum 0} } {
                   2446:     global gWindowMenu gRefFile gRefChanged gCapaConfig gFile
                   2447: 
                   2448:     if { ![winfo exists $window] } {return}
                   2449:     if { $refNum } {
                   2450: 	if {$saveAs} { 
                   2451: 	    set dir [file dirname $gRefFile($refNum)]
                   2452: 	    set file ""
                   2453: 	} else {
                   2454: 	    set dir [file dirname $gRefFile($refNum)]
                   2455: 	    set file [file tail $gRefFile($refNum)]
                   2456: 	}
                   2457:     } else { set dir [ set file "" ] }
                   2458: #    if { $dir == "" || $dir == "."} { set dir [pwd] }
                   2459:     if { $file == "" } {
                   2460: 	set file [tk_getSaveFile -title "Enter the name to Save As" \
                   2461: 		-initialdir "$dir" ]
                   2462: 	if { $file == "" } {
                   2463: 	    displayError "File not saved"
                   2464: 	    return
                   2465: 	}
                   2466:     } else {
                   2467: 	set file [file join $dir $file]
                   2468:     }
                   2469: 
                   2470:     if { $refNum } { 
                   2471: 	catch {removeWindowEntry  "Reference $gRefFile($refNum)*" }
                   2472: 	catch {removeFindList $refNum}
                   2473: 	set gRefFile($refNum) $file
                   2474: 	addFindList $refNum
                   2475: 	wm title [winfo toplevel $window] $file 
                   2476: 	$gWindowMenu add command -label "Reference $file" \
                   2477: 		-command "capaRaise [winfo toplevel $window]"
                   2478: 	if { !$saveAs } {
                   2479: 	    if { ([array name gCapaConfig quizzerBackupRef] == "") ||
                   2480: 		 ($gCapaConfig(quizzerBackupRef)!="off") } {
                   2481: 		if { [catch {file copy -force $file $file.bak} ] } {
                   2482: 		    displayError "Unable to create backup for $file"
                   2483: 		}
                   2484: 	    }
                   2485: 	}
                   2486:     }
                   2487:     set fileId [open $file w]
                   2488:     puts -nonewline $fileId [$window get 0.0 end]
                   2489:     close $fileId
                   2490:     if { $refNum } { set gRefChanged($refNum) 0 }
                   2491:     if { ([file tail $file] == "capa.config") && ($dir == [file dirname $gFile]) } {
                   2492: 	if { [makeSure "Reread capa.config settings into Quizzer?"] != "Cancel" } {
                   2493: 	    rereadCapaConfig
                   2494: 	}
                   2495:     }
                   2496: 
                   2497:     return $file
                   2498: }
                   2499: 
                   2500: ###########################################################
                   2501: # deleteFile
                   2502: ###########################################################
                   2503: ###########################################################
                   2504: ###########################################################
                   2505: proc deleteFile { which } {
                   2506:     global gFile gRefFile
                   2507:     if { $which } { set file $gRefFile($which) } else { set file $gFile }
                   2508:     if { [makeSure "Do you wish to Delete $file"] == "Cancel" } { return } 
                   2509:     if { $which } { closeRefFile $which 1 0 } else { closeDocument 1 0 }
                   2510:     file delete -- $file
                   2511: }
                   2512: 
                   2513: ###########################################################
                   2514: ###########################################################
                   2515: ###########################################################
                   2516: ###########################################################
                   2517: proc rereadCapaConfig { } {
                   2518:     global gCapaConfig
                   2519:     set printer_option $gCapaConfig(printer_option)
                   2520:     unset gCapaConfig(printer_option)
                   2521:     set error [parseCapaConfig]
                   2522:     if { $error != "OK" } { 
                   2523: 	displayError "Invalid capa.config file" 
                   2524: 	set gCapaConfig(printer_option) $printer_option
                   2525:     } 
1.5       albertel 2526:     setDefaultValues
1.1       albertel 2527:     updateColors
                   2528: }
                   2529: 
                   2530: ###########################################################
                   2531: ###########################################################
                   2532: ###########################################################
                   2533: ###########################################################
                   2534: proc pickCapaConfig { } {
                   2535:     set error NOTOK
                   2536:     while { $error != "OK" } {
                   2537: 	set file [tk_getOpenFile -title "Pick a capa.config file" \
                   2538: 		      -filetypes { { {CAPA configuration}  {capa.config} } \
                   2539: 				       { {All Files} {*} } }]
                   2540: 	if { $file == "" } { break }
                   2541: 	set oldDir [pwd]
                   2542: 	cd [file dirname $file]	
                   2543: 	set error [parseCapaConfig]
                   2544: 	if { $error != "OK" } { displayError "Invalid capa.config file"; cd $oldDir } 
1.5       albertel 2545: 	setDefaultValues
1.1       albertel 2546:     } 
                   2547: }
                   2548: 
1.5       albertel 2549: proc setDefaultValues {} {
                   2550:     global gProbVal gTryVal gHintVal gCapaConfig
                   2551:     catch {set gProbVal $gCapaConfig(default_prob_val)}
                   2552:     catch {set gTryVal $gCapaConfig(default_try_val)}
                   2553:     catch {set gHintVal $gCapaConfig(default_hint_val)}
                   2554: }
                   2555: 
1.1       albertel 2556: ###########################################################
                   2557: # openDocument
                   2558: ###########################################################
                   2559: ###########################################################
                   2560: ###########################################################
                   2561: proc openDocument {} {
1.5       albertel 2562:     global gFile gTextWindow gSetNumberText gPrefs gChanged gQuizTemp gUndo 
1.1       albertel 2563: 
                   2564:     if { $gChanged } { if { [askToSave 0 0] == "Cancel" } { return } }
                   2565:     if { ![catch {set gTextWindow}] } { 
                   2566: 	if { [winfo exists $gTextWindow] } { return }
                   2567:     }
                   2568: 
                   2569:     # the event generation is because of a bug in tk_getOpenFile
                   2570:     # After double cliking the mouse Button one is thought to be left down.
                   2571:     # this only seems to happen when a toplevel window is created
                   2572:     # after getting the file
                   2573:     set gFile [tk_getOpenFile -filetypes \
                   2574: 	    { { {Quizzer} {"*.qz"} } { {All Files} {"*"} } } \
                   2575: 	    -title "Select the proper file" -initialdir "[pwd]" ]
                   2576: #    event generate .main <ButtonRelease-1>
                   2577:     if { $gFile == "" } { return }
                   2578:     if { [file isdirectory $gFile] } {
                   2579: 	displayError "You attempted to open $gFile which is a directory not a file."
                   2580: 	return
                   2581:     }
                   2582: 
                   2583:     set error [ catch {set fileId [open $gFile r] } ]
                   2584: 
                   2585:     if { $error } {
                   2586: 	displayError "Unable to read $gFile"
                   2587: 	return
                   2588:     } 
                   2589:     
                   2590:     set oldDir [pwd]
                   2591:     
                   2592:     cd [file dirname $gFile]
                   2593: 
                   2594:     set tempfiles ""
                   2595:     catch {set tempfiles [glob quiztemp.*]}
                   2596:     if { $tempfiles == "" } { 
                   2597: 	set gQuizTemp true 
                   2598:     }     
                   2599:     foreach quiztempFile $tempfiles {
                   2600: 	if { ! ( [file isfile $quiztempFile] && 
                   2601: 	[file writable $quiztempFile] ) } {
                   2602: 	    if { [makeSure "There are old quiztemp files in this directory that can not be overwritten. If you continue editing you will be unable to print or Create .dvi. You can still preview and save."] == "Cancel" } {
                   2603:                 cd $oldDir
                   2604:                 set gQuizTemp true
                   2605:                 return
                   2606: 	    } else {
                   2607:                 set gQuizTemp false
                   2608: 		break
                   2609: 	    }
                   2610: 	}
                   2611:     }
                   2612: 
                   2613:     if { $tempfiles == "" } {
                   2614: 	if { ! [file writable $gFile] } {
                   2615: 	    if { [makeSure "You do not have permission to write to this directory. If you continue editing you will be unable to save, print, or create .dvi"] == "Cancel" } {
                   2616: 		cd $oldDir
                   2617:                 set gQuizTemp true
                   2618:                 return
                   2619: 	    } else {
                   2620:                 set gQuizTemp false
                   2621: 	    }
                   2622: 	}
                   2623:     }
                   2624: 
                   2625:     set error [parseCapaConfig]
                   2626:     
                   2627:     if { $error != "OK" } {
                   2628: 	cd $oldDir
                   2629: 	set gQuizTemp true
                   2630:         return
                   2631:     }
1.5       albertel 2632:     setDefaultValues
                   2633: 
1.1       albertel 2634:     createEditingWindow 0
                   2635:     $gTextWindow delete 0.0 end
                   2636:     $gTextWindow insert 0.0 [read $fileId [file size $gFile]]
                   2637:     $gTextWindow delete end-1c
                   2638: 
                   2639:     rename $gTextWindow .$gTextWindow
                   2640:     trackChanges $gTextWindow 0
                   2641:     set gUndo(0) 0
                   2642:     set gUndo(0.cur) 0
                   2643: 
                   2644:     createImportLinks 0 0.0 end
                   2645:     focus -force $gTextWindow 
                   2646:     set coord [$gTextWindow bbox 0.0]
                   2647:     event generate $gTextWindow <1> -x [lindex $coord 0] -y [lindex $coord 1]
                   2648:     event generate $gTextWindow <ButtonRelease-1>
                   2649:     update
                   2650:     close $fileId
                   2651: 
                   2652:     updatePrefsWindow 
                   2653: 
                   2654:     set gSetNumberText [string range [file rootname [file tail $gFile]] 3 end ]
                   2655:     checkIfValidFilename
                   2656:     set gChanged 0
                   2657: }
                   2658: 
                   2659: ###########################################################
                   2660: # includeReferenceFile
                   2661: ###########################################################
                   2662: ###########################################################
                   2663: ###########################################################
                   2664: proc includeReferenceFile { file num window } {
                   2665:     set index [$window index "file.$num.first linestart"]
                   2666:     if { ![file readable $file] } {
                   2667: 	displayError "Unable to read file $file"
                   2668: 	return
                   2669:     }
                   2670:     set impline [$window get $index "$index lineend"]
                   2671:     set fileId [open $file r]
                   2672:     $window insert $index /
                   2673:     update idletasks
                   2674:     if { [$window index end] == [$window index "$index + 1 lines"] } {
                   2675: 	$window insert "$index + 1 lines" "\n[read $fileId [file size $file]]"
                   2676:     } else {
                   2677: 	$window insert "$index + 1 lines" [read $fileId [file size $file]]
                   2678:     }
                   2679:     close $fileId
                   2680: }
                   2681: 
                   2682: ###########################################################
                   2683: # impMenuSaveAs
                   2684: ###########################################################
                   2685: ###########################################################
                   2686: ###########################################################
                   2687: proc impMenuSaveAs { file num window } {
                   2688:     set index [$window index "file.$num.first linestart"]
                   2689:     if { ![file readable $file] } {
                   2690: 	displayError "Unable to read file $file"
                   2691: 	return
                   2692:     }
                   2693:     set dir [file dirname $file]
                   2694:     if { $dir != "." } {
                   2695: 	set dest [tk_getSaveFile -title "Enter the name to Save As" -initialdir $dir]
                   2696:     } else {
                   2697: 	set dest [tk_getSaveFile -title "Enter the name to Save As" -initialdir [pwd]]
                   2698:     }
                   2699:     if { $dest == "" } {
                   2700: 	displayError "File not saved"
                   2701: 	return
                   2702:     }
                   2703:     if { [catch {file copy -force -- $file $dest} errorMsg ] } {
                   2704: 	displayError "Unable to copy $file to $dest: $errorMsg"
                   2705: 	return
                   2706:     }
                   2707:     $window delete $index "$index lineend"
                   2708:     if { $dir != "." } {
                   2709: 	$window insert $index "/IMP \"$dest\""
                   2710:     } else {
                   2711: 	$window insert $index "/IMP \"[file tail $dest]\""
                   2712:     }
                   2713: }
                   2714: 
                   2715: ###########################################################
                   2716: # createImpMenu
                   2717: ###########################################################
                   2718: ###########################################################
                   2719: ###########################################################
                   2720: proc createImpMenu { file num window } {
                   2721:     set menuFrame [menu .impmenu -tearoff 0 -type tearoff ]
                   2722: 
                   2723: #    wm title $menuFrame "Quizzer"
                   2724:     wm overrideredirect $menuFrame 1
                   2725:     wm positionfrom $menuFrame program
                   2726:     $menuFrame post [winfo pointerx .] [winfo pointery .]
                   2727:     $menuFrame add command -label "Open" -command "openReferenceFile $file"
                   2728:     $menuFrame add command -label "Include" -command \
                   2729: 	"includeReferenceFile $file $num $window"
                   2730:     $menuFrame add command -label "saveAs" -command "impMenuSaveAs $file $num $window"
                   2731:     grab $menuFrame
                   2732:     bind all <ButtonRelease> "grab release $menuFrame;destroy $menuFrame"
                   2733: }
                   2734: 
                   2735: ###########################################################
                   2736: # registerCreateImportLinks
                   2737: ###########################################################
                   2738: ###########################################################
                   2739: ###########################################################
                   2740: proc registerCreateImportLinks { num start end} {
                   2741:     global gDoCreateImportLinks gCreateImportLinks
                   2742: 
                   2743:     if { $gCreateImportLinks && !$gDoCreateImportLinks($num) } {
                   2744: 	after idle "createImportLinks $num $start $end"
                   2745: 	set gDoCreateImportLinks($num) 1
                   2746:     }
                   2747: }
                   2748: 
                   2749: ###########################################################
                   2750: # createImportLinks
                   2751: ###########################################################
                   2752: ###########################################################
                   2753: ###########################################################
                   2754: proc createImportLinks { num start end } {
                   2755:     global gTextWindow gUniqueNumber gDoCreateImportLinks gRefText gCapaConfig 
                   2756:     
                   2757:     set gDoCreateImportLinks($num) 0
                   2758:     if { $num } { set window $gRefText($num) } else { set window $gTextWindow }
                   2759:     if { ![winfo exists $window] } { return }
                   2760:     set end [$window index $end]
                   2761:     set start [$window index $start]
                   2762:     set lastline [lindex [split $end .] 0]
                   2763:     set startline [lindex [split $start .] 0]
                   2764:     foreach tag [$window tag names] {
                   2765: 	if { [regexp {file\..+} $tag ] } {
                   2766: 	    if { [$window tag nextrange $tag "$start linestart" "$end lineend"] != "" } {
                   2767: 		$window tag delete $tag
                   2768: 	    }
                   2769: 	}
                   2770:     }
                   2771:     for { set i $startline } { $i <= $lastline } { incr i } {
                   2772: 	set aline [$window get $i.0 "$i.0 lineend"]
                   2773: 	if { [regexp -nocase {(^[ 	]*)(//.*)} $aline matchVar match1 match2] } {
                   2774: 	    set tagnum [incr gUniqueNumber]
                   2775: 	    set start [string length $match1]
                   2776: 	    set end [expr $start + [string length $match2]]
                   2777: 	    $window tag add file.$tagnum $i.$start $i.$end
                   2778: 	    $window tag configure file.$tagnum -foreground $gCapaConfig(comment_color)
                   2779: 	} elseif { [regexp -nocase {(^.*/let.*[ 	]+)(//.*)} $aline matchVar \
                   2780: 			match1 match2] } {
                   2781: 	    set tagnum [incr gUniqueNumber]
                   2782: 	    set start [string length $match1]
                   2783: 	    set end [expr $start + [string length $match2]]
                   2784: 	    $window tag add file.$tagnum $i.$start $i.$end
                   2785: 	    $window tag configure file.$tagnum -foreground $gCapaConfig(comment_color)
                   2786: 	} 
                   2787: 	if { [regexp -nocase "(.*/imp +)(\"\[^\"\]+\")(.*)" $aline matchVar \
                   2788: 		  match1 match2 match3] } {
                   2789: 	    set tagnum [incr gUniqueNumber]
                   2790: 	    set start [string length $match1]
                   2791: 	    set end [expr $start + [string length $match2]]
                   2792: 	    $window tag add file.$tagnum $i.$start $i.$end
                   2793: 	    $window tag configure file.$tagnum -foreground $gCapaConfig(IMP_color)
                   2794: 	    $window tag bind file.$tagnum <Double-ButtonPress> \
                   2795: 		"eval openReferenceFile $match2"
                   2796: 	    $window tag bind file.$tagnum <ButtonPress-3> \
                   2797: 		"eval createImpMenu $match2 $tagnum $window"
                   2798: 	} 
                   2799:     }
                   2800: }
                   2801: 
                   2802: ###########################################################
                   2803: # isReferenceFileOpen
                   2804: ###########################################################
                   2805: ###########################################################
                   2806: ###########################################################
                   2807: proc isReferenceFileOpen { file } {
                   2808:     global gWindowMenu
                   2809: #    if { [catch {set index [$gWindowMenu index "Reference $file"]} ] } { return "" }
                   2810:     set last [$gWindowMenu index end]
                   2811: #    puts $last
                   2812:     for { set index 1 } { $index <= $last } { incr index } {
                   2813: #	puts $index
                   2814: 	catch {set entry [$gWindowMenu entrycget $index -label]}
                   2815: 	if { "Reference" == [set entryfile [lindex $entry 0]] } {
                   2816: 	    set entryfile [lindex $entry 1]
                   2817: 	}
                   2818: #	puts $entryfile
1.6       albertel 2819: 	if { [catch {file stat $entryfile a1}] } { continue }
1.1       albertel 2820: 	file stat $file a2
                   2821: #	puts "$a2(ino) == $a1(ino)"
                   2822: 	if { $a2(ino) == $a1(ino) } {
                   2823: #	    puts "seems right"
                   2824: 	    return [lindex [$gWindowMenu entrycget $index -command] 1]
                   2825: 	}
                   2826:     }
                   2827: #    puts "failed $index"
                   2828:     return ""
                   2829: #    puts $index
                   2830: #    puts [$gWindowMenu entrycget $index -command]
                   2831: #    return [lindex [$gWindowMenu entrycget $index -command] 1]
                   2832: }
                   2833: 
                   2834: ###########################################################
                   2835: # newReferenceFile
                   2836: ###########################################################
                   2837: ###########################################################
                   2838: ###########################################################
                   2839: proc newReferenceFile { } {
                   2840:     global gDir
                   2841: #    if { $gDir(reference) == "." } { set gDir(reference) [pwd] }
                   2842:     set file [tk_getSaveFile -title "Enter the name of the New reference file" \
                   2843: 		  -initialdir "$gDir(reference)" ]
                   2844:     event generate .main <ButtonRelease-1>
                   2845:     if { $file == "" } { return }
                   2846:     set gDir(reference) [file dirname $file]
                   2847:     if { [file isdirectory $file] } {
                   2848: 	displayError "You attempted to create $file which is already a directory."
                   2849: 	return
                   2850:     }
                   2851:     openReferenceFile $file 1
                   2852: }
                   2853: 
                   2854: ###########################################################
                   2855: # openReferenceFile
                   2856: ###########################################################
                   2857: ###########################################################
                   2858: ###########################################################
                   2859: proc openReferenceFile { {file ""} {new 0}} {
                   2860:     global gUniqueNumber gWindowMenu gDir
                   2861: 
                   2862:     set num [incr gUniqueNumber]
                   2863:     global gRefCurLine gRefLine gRefText gRefChanged gRefFile gRefClosed \
                   2864: 	gUndo gRefChangedLast
                   2865:     # the event generation is because of a bug in tk_getOpenFile
                   2866:     # After double cliking the mouse Button one is thought to be left down.
                   2867:     # this only seems to happen when a toplevel window is created
                   2868:     # after getting the file
                   2869:     if { $file == "" } {
                   2870: #	if { $gDir(reference) == "." } { set gDir(reference) [pwd] }
                   2871: 	set file [tk_getOpenFile -filetypes \
                   2872: 		      { { {All Files} {"*"} } { {Quizzer} {"*.qz"} } } \
                   2873: 		      -title "Select the proper file" \
                   2874: 		      -initialdir "$gDir(reference)" ]
                   2875: 	event generate .main <ButtonRelease-1>
1.6       albertel 2876: 	if { $file == "" } { return 0 }
1.1       albertel 2877: 	set gDir(reference) [file dirname $file]
                   2878: 	
                   2879: 	if { [file isdirectory $file] } {
                   2880: 	    displayError "You attempted to open $file which is a directory not a file."
1.6       albertel 2881: 	    return 0
1.1       albertel 2882: 	}
                   2883:     } else {
                   2884: 	if { !$new } {
                   2885: 	    if { [set window [isReferenceFileOpen $file] ] != "" } { 
1.6       albertel 2886: 		set num [lindex [split [lindex [split $window .] 1] e] end]
1.1       albertel 2887: 		capaRaise $window
1.6       albertel 2888: 		return $num
1.1       albertel 2889: 	    }
                   2890: 	    # specifically opening the capa.config file
                   2891: 	    if { $file == "capa.config" } {
                   2892: 		global gTextWindow gFile
                   2893: 		if { [catch {set gTextWindow}] } { 
                   2894: 		    set file [tk_getOpenFile -filetypes \
                   2895: 				  {{{Capa.config file} {"capa.config"}} 
                   2896: 				      { {All Files} {"*"} } } \
                   2897: 				  -title "Select the proper file" \
                   2898: 				  -initialdir "$gDir(reference)" ]
1.6       albertel 2899: 		    if { $file == "" } { return 0 }
1.1       albertel 2900: 		} else {
                   2901: 		    set file [file join [file dirname $gFile] capa.config]
                   2902: 		}
                   2903: 	    } else {
                   2904: 		if { ![file isfile $file] && ![file readable $file] } {
                   2905: 		    displayError "Unable to find $file"
1.6       albertel 2906: 		    return 0
1.1       albertel 2907: 		}
                   2908: 		if { [file isdirectory $file] } {
                   2909: 		    displayError "You attempted to open $file which is a directory not a file."
1.6       albertel 2910: 		    return 0
1.1       albertel 2911: 		}
                   2912: 	    }
                   2913: 	}
                   2914:     }
                   2915: 
                   2916:     set gRefFile($num) $file
                   2917:     set referenceFile [toplevel .reference$num]
                   2918:     wm title $referenceFile "$file"
                   2919: 
                   2920:     $gWindowMenu add command -label "Reference $file" \
                   2921: 	-command "capaRaise $referenceFile"
                   2922: 
                   2923:     set menuFrame [frame $referenceFile.menu -borderwidth 3 -relief raised]
                   2924:     set lineFrame [frame $referenceFile.lineFrame]
                   2925:     set windowFrame [frame $referenceFile.windowFrame]
                   2926:     pack $menuFrame $lineFrame $windowFrame
                   2927:     pack configure $windowFrame -expand 1 -fill both
                   2928:     pack configure $menuFrame -fill x
                   2929: 
                   2930:     label $lineFrame.msg -text "Current Line:"
                   2931:     label $lineFrame.current -textvariable gRefCurLine($num)
                   2932:     entry $lineFrame.line -width 8 -textvariable gRefLine($num)
                   2933:     bind $lineFrame.line <KeyPress-Return> "+gotoRefLine $num"
                   2934:     button $lineFrame.button -text "Goto" -command \
                   2935: 	    "gotoRefLine $num"
                   2936:     pack $lineFrame.msg $lineFrame.current $lineFrame.line \
                   2937: 	    $lineFrame.button -side left
                   2938:     
                   2939:     set infoFrame [frame $windowFrame.infoFrame]
                   2940:     set textFrame [frame $windowFrame.textFrame]
                   2941:     pack $infoFrame $textFrame -side top
                   2942:     pack configure $textFrame -expand 1 -fill both
                   2943: 
                   2944:     scrollbar $textFrame.scroll -orient vertical -command \
                   2945: 	    "$textFrame.text yview"
                   2946:     set textWindow [text $textFrame.text -yscrollcommand \
                   2947: 	    "$textFrame.scroll set" -wrap char]
                   2948:     pack $textFrame.scroll $textWindow -side left -expand 0
                   2949:     pack configure $textFrame.scroll -expand 0 -fill y
                   2950:     pack configure $textWindow -expand true -fill both
                   2951: 
                   2952: #    label $infoFrame.label -textvariable gRefFile($num)
                   2953: #    pack $infoFrame.label -side left
                   2954: 
                   2955:     menubutton $menuFrame.file -text File -menu $menuFrame.file.m
                   2956:     menubutton $menuFrame.edit -text Edit -menu $menuFrame.edit.m
                   2957:     pack $menuFrame.file $menuFrame.edit -side left
                   2958:     
                   2959:     set fileMenu [ menu $menuFrame.file.m ]
                   2960:     set editMenu [ menu $menuFrame.edit.m ]
                   2961: 
                   2962:     $fileMenu add command -label Save -command \
                   2963: 	    "saveText $textWindow 0 $num" -accelerator "Alt+s"
                   2964:     bind $referenceFile <Alt-s> \
                   2965: 	    "saveText $textWindow 0 $num"
                   2966:     $fileMenu add command -label "Save As" -command \
                   2967: 	    "saveText $textWindow 1 $num" -accelerator "Alt+S"
                   2968:     bind $referenceFile <Alt-Shift-s> "saveText $textWindow 1 $num" 
                   2969:     $fileMenu add command -label Delete -command "deleteFile $num"
                   2970:     $fileMenu add command -label Print -command "printText $textWindow"
                   2971:     $fileMenu add command -label Close -command "closeRefFile $num" \
                   2972: 	    -accelerator "Alt+w"
                   2973:     bind $referenceFile <Alt-w> "closeRefFile $num"
                   2974: #    bind $referenceFile <Destroy> "closeRefFile $num 1"
                   2975:     wm protocol $referenceFile WM_DELETE_WINDOW "closeRefFile $num 1"
                   2976:     $editMenu add command -label "Cut" -command "tk_textCut $textWindow" \
                   2977: 	    -accelerator "Alt+x"
                   2978:     bind $referenceFile <Alt-x> "tk_textCut $textWindow"
                   2979:     $editMenu add command -label "Copy" -command "tk_textCopy $textWindow" \
                   2980: 	    -accelerator "Alt+c"
                   2981:     bind $referenceFile <Alt-c> "tk_textCopy $textWindow"
                   2982:     $editMenu add command -label "Paste" -command "tk_textPaste $textWindow" \
                   2983: 	    -accelerator "Alt+v"
                   2984:     bind $referenceFile <Alt-v> "tk_textPaste $textWindow"
                   2985:     $editMenu add command -label "Select All " -command \
                   2986: 	    "selectAll $num "  -accelerator "Alt+a"
                   2987:     bind $referenceFile <Alt-a> "selectAll $num" 
                   2988:     $editMenu add separator
                   2989:     $editMenu add command -label "Undo" -command "undo $num" \
                   2990: 	    -accelerator "Alt+u"
                   2991:     bind $referenceFile <Alt-u> "undo $num" 
                   2992: #    $editMenu add command -label "Redo" -command "redo $num"
                   2993:     $editMenu add separator
                   2994:     $editMenu add command -label "Find" -command "createFindWindow $num" \
                   2995: 	-accelerator "Alt+f"
                   2996: 
                   2997:     
                   2998:     if { !$new } {
                   2999: 	set fileId [open $file r]
                   3000: 	$textWindow insert 0.0 [read $fileId [file size $file]]
                   3001: 	$textWindow delete end-1c
                   3002: 	close $fileId
                   3003:     }
                   3004:     
                   3005:     set gRefText($num) $textWindow
                   3006:     rename $textWindow .$textWindow
                   3007:     trackChanges $textWindow $num
                   3008:     set gUndo($num) 0
                   3009:     set gUndo($num.cur) 0
                   3010: 
                   3011:     createImportLinks $num 0.0 end
                   3012:     focus -force $textWindow 
                   3013: #    update
                   3014:     set coord [$textWindow bbox 0.0]
                   3015:     event generate $textWindow <1> -x [lindex $coord 0] -y [lindex $coord 1]
                   3016: #    update
                   3017:     event generate $textWindow <ButtonRelease-1>
                   3018: #    update
                   3019: #    capaRaise $referenceFile
                   3020:     after 1 "catch \{focus $textWindow;raise $referenceFile\}"
                   3021:     selection clear
                   3022: #order is important here since gRefChanged has a trace on it the references 
                   3023: #gRefChangedLast
                   3024:     set gRefChangedLast($num) 0
                   3025:     set gRefChanged($num) 0
                   3026:     set gRefClosed($num) 0
                   3027:     addFindList $num
1.6       albertel 3028:     return $num
1.1       albertel 3029: }
                   3030: 
                   3031: ###########################################################
                   3032: ###########################################################
                   3033: ###########################################################
                   3034: ###########################################################
                   3035: proc trackChanges { procName num } {
                   3036:     eval "proc $procName args {
                   3037: 	global gUndo gRefChanged gChanged
                   3038: 	if {\[regexp {^(ins|del).*} \[lindex \$args 0\]\]} { 
                   3039: 	    #puts \"\$args\"
                   3040: 	    if { \$gUndo($num.cur) != \$gUndo($num) } {
                   3041: 		set i \[expr \$gUndo($num.cur) + 1 \]
                   3042: 		while { \[ info exists gUndo($num.\$i) \] } {
                   3043: 		    unset gUndo($num.\$i)
                   3044: 		    incr i
                   3045: 		}
                   3046: 		set gUndo($num) \$gUndo($num.cur)
                   3047: 	    }
                   3048: 	    set gUndo($num.cur) \$gUndo($num)
                   3049: 	    set insertindex \[.$procName index \[lindex \$args 1 \] \]
                   3050: 	    set numChange \[set gUndo($num.cur) \[incr gUndo($num) \] \]
                   3051: 	    if { $num == 0 } { set gChanged 1 } else { set gRefChanged($num) 1 }
                   3052: 	}
                   3053: 	if {\[regexp {^(ins).*} \[lindex \$args 0\]\]} {
                   3054: 	    set index2 \$insertindex+\[string length \[lindex \$args 2 \] \]chars
                   3055: 	    set gUndo($num.\$numChange) \"delete \$insertindex \$index2 \"
                   3056:             if {\[regexp {.*\[\"/\].*} \$args\] || \
                   3057:                 \[regexp {.*\[\"/\].*} \[.$procName get \"\$insertindex linestart\" \"\$index2 lineend\"\]\]} { 
                   3058:                 registerCreateImportLinks $num \$insertindex \$index2
                   3059:             }
                   3060: 	} elseif {\[regexp {^(del).*} \[lindex \$args 0\]\]} { 
                   3061: 	    if { \[catch { set insertindex2 \[.$procName index \
                   3062: 		    \[lindex \$args 2 \] \] } \] } {
                   3063: 		set chars \[ .$procName get \$insertindex \]
                   3064:                 set insertindex2 \$insertindex+1c
                   3065: 	    } else {
                   3066: 		set chars \[ .$procName get \$insertindex \$insertindex2 \]
                   3067: 	    }
                   3068: 	    set gUndo($num.\$numChange) \"insert \$insertindex \[list \$chars\] \"
1.11      albertel 3069:             if { \[string length \$chars\] > 100 } {
                   3070:                    registerCreateImportLinks $num \$insertindex-1line \$insertindex2+1line
                   3071:             } else {
                   3072:                 if {\[regexp \{.*\[\"/\].*\} \$chars\] || \
                   3073:                     \[regexp \{.*\[\"/\].*\} \[.$procName get \"\$insertindex linestart\" \"\$insertindex2 lineend\"\]\]} { 
                   3074:                    registerCreateImportLinks $num \$insertindex \$insertindex2
                   3075:                 }
1.1       albertel 3076:             }
1.11      albertel 3077:         }
1.1       albertel 3078: 	set result \[uplevel .$procName \$args\]
                   3079:         updateLocation $num
                   3080:         return \$result
                   3081:     }"
                   3082: }
                   3083: 
                   3084: ###########################################################
                   3085: ###########################################################
                   3086: ###########################################################
                   3087: ###########################################################
                   3088: proc undo { num } {
1.6       albertel 3089:     global gUndo gRefText gTextWindow gChanged gRefChanged
1.1       albertel 3090:     if { $gUndo($num.cur) == 0 } { return }
                   3091:     set undoInfo $gUndo($num.$gUndo($num.cur))
                   3092:     if { [regexp {.*[\"/].*} $undoInfo] } {
                   3093: 	registerCreateImportLinks $num [lindex $undoInfo 1] end
                   3094:     }
                   3095:     if { [regexp {.*delete.*} $undoInfo] } {
                   3096: 	registerCreateImportLinks $num [lindex $undoInfo 1] [lindex $undoInfo 2]
                   3097:     }
                   3098:     if { $num == 0 } {
                   3099: 	if {[catch {eval ".$gTextWindow $gUndo($num.$gUndo($num.cur))"}]} { return }
1.6       albertel 3100: 	set gChanged 1
1.1       albertel 3101:     } else {
                   3102: 	if {[catch {eval ".$gRefText($num) $gUndo($num.$gUndo($num.cur))"}]} { return }
1.6       albertel 3103: 	set gRefChanged($num) 1
1.1       albertel 3104:     }
                   3105:     incr gUndo($num.cur) -1
                   3106: }
                   3107: 
                   3108: ###########################################################
                   3109: ###########################################################
                   3110: ###########################################################
                   3111: ###########################################################
                   3112: proc redo { num } {
                   3113:     global gUndo gRefText
                   3114: }
                   3115: 
                   3116: ###########################################################
                   3117: ###########################################################
                   3118: ###########################################################
                   3119: ###########################################################
                   3120: proc gotoRefLine { number } {
                   3121:     global gRefLine gRefText
                   3122:     if { [catch {set gRefText($number)}] } { return }
                   3123:     if { ![winfo exists $gRefText($number)] } { return }
                   3124: 
                   3125:     if { $gRefLine($number) == "" } {
                   3126: 	return
                   3127:     } else {
                   3128: 	$gRefText($number) mark set insert $gRefLine($number).0
                   3129: 	catch {$gRefText($number) tag remove sel sel.first sel.last}
                   3130: 	$gRefText($number) tag add sel "insert linestart" "insert lineend"
                   3131: 	$gRefText($number) see insert
                   3132:     } 
                   3133: }
                   3134: 
                   3135: ###########################################################
                   3136: # updateLocation
                   3137: ###########################################################
                   3138: ###########################################################
                   3139: ###########################################################
                   3140: proc updateLocation { number } {
                   3141:     global gRefCurLine gRefText gTextWindow gLineNumber gCharacterNumber
                   3142: 
                   3143:     if {$number} {set window $gRefText($number)} {set window $gTextWindow} 
                   3144: #    if {![winfo exists $gRefText($number)]} {return};#do I need this
                   3145:     set spot [split [.$window index insert] "."]
                   3146:     if { $number } {
                   3147: 	set gRefCurLine($number) [lindex $spot 0 ]
                   3148:     } else {
                   3149: 	set gLineNumber [lindex $spot 0]
                   3150: 	set gCharacterNumber [lindex $spot 0]
                   3151:     }
                   3152: }
                   3153: 
                   3154: ###########################################################
                   3155: ###########################################################
                   3156: ###########################################################
                   3157: ###########################################################
                   3158: proc askToSave { refNum mustClose } {
                   3159:     global gPrompt
                   3160:     
                   3161:     set dialog [toplevel .askToSavePrompt -borderwidth 10]
                   3162:     wm title $dialog "Do you wish to Save"
                   3163:     wm geo $dialog "+200+200"
                   3164:     if { $refNum } {
                   3165: 	global gRefFile
                   3166: 	set msg "Reference File: $gRefFile($refNum) has changed. Do you wish to save?"
                   3167:     } else {
                   3168: 	set msg "Source has changed do you wish to save?"
                   3169:     }
                   3170:     message $dialog.msg -text $msg -aspect 800
                   3171:     
                   3172:     set gPrompt(result) ""
                   3173:     set buttonFrame [frame $dialog.buttons -bd 10]
                   3174:     pack $dialog.msg $buttonFrame -side top -fill x
                   3175:     
                   3176:     bind $dialog <Destroy> { 
                   3177: 	set gPrompt(result) Cancel
                   3178: 	set gPrompt(yes) 0
                   3179:     }
                   3180: 
                   3181:     button $buttonFrame.yes -text Yes -underline 0 -command {
                   3182: 	set gPrompt(yes) 1
                   3183:     }
                   3184:     button $buttonFrame.no -text No -underline 0 -command {
                   3185: 	set gPrompt(yes) 0
                   3186:     } 
                   3187:     if { !$mustClose } {
                   3188: 	button $buttonFrame.cancel -text Cancel  -underline 0 -command { 
                   3189: 	    set gPrompt(yes) 0 
                   3190: 	    set gPrompt(result) Cancel
                   3191: 	}
                   3192: 	pack $buttonFrame.yes $buttonFrame.no $buttonFrame.cancel -side left
                   3193:     } else {
                   3194: 	pack $buttonFrame.yes $buttonFrame.no -side left
                   3195:     }
                   3196:     bind $dialog <Alt-Key> break
                   3197: 
                   3198:     Centre_Dialog $dialog default
                   3199:     update
                   3200: 
                   3201:     focus $dialog
                   3202:     capaRaise $dialog
                   3203:     capaGrab $dialog
                   3204:     vwait gPrompt(yes)
                   3205:     capaGrab release $dialog
                   3206:     bind $dialog <Destroy> ""
                   3207:     destroy $dialog
                   3208:     if {$gPrompt(yes)} {
                   3209: 	if { $refNum } {
                   3210: 	    global gRefText 
                   3211: 	    saveText $gRefText($refNum) 0 $refNum
                   3212: 	} else {
                   3213: 	    saveDocument
                   3214: 	}
                   3215:     } else {
                   3216: 	return $gPrompt(result)
                   3217:     }
                   3218: }    
                   3219: 
                   3220: ###########################################################
                   3221: ###########################################################
                   3222: ###########################################################
                   3223: ###########################################################
                   3224: proc saveDocument { {saveAs 0} } {
                   3225:     global gFile gTextWindow gSetNumberText gChanged gEditWindow gWindowMenu \
                   3226: 	gCapaConfig
                   3227:     if { [catch {set gTextWindow}] } { return }
                   3228:     if { ![winfo exists $gTextWindow] } { return }
                   3229:     if { $gFile == "" } { set saveAs 1 }
                   3230:     if {$saveAs == 1} {
                   3231: 	set temp [tk_getSaveFile -title "Enter the name to Save As" \
                   3232: 		-initialdir "[pwd]" ]
                   3233: 	if { $temp == "" } {
                   3234: 	    displayError "File not saved"
                   3235: 	    return
                   3236: 	}
                   3237: 	
                   3238: 	catch {removeWindowEntry "$gFile*"}
                   3239: 	catch {removeFindList}	
                   3240: 	set gFile $temp
                   3241: 	addFindList
                   3242: 	cd [file dirname $gFile]
                   3243: 	set gSetNumberText [string range [file rootname [file tail $gFile]] \
                   3244: 		3 end ]
                   3245: 	checkIfValidFilename
                   3246: 	wm title [winfo toplevel $gEditWindow] $gFile 
                   3247: 
                   3248: 	$gWindowMenu add command -label "$gFile" -command \
                   3249: 		"capaRaise $gEditWindow"
                   3250:     } else {
                   3251: 	if { ([array name gCapaConfig quizzerBackupQZ] == "") ||
                   3252: 	     ($gCapaConfig(quizzerBackupQZ)!="off") } {
                   3253: 	    if { [catch {file copy -force $gFile $gFile.bak} ] } {
                   3254: 		displayError "Unable to create backup for $gFile"
                   3255: 	    }
                   3256: 	}
                   3257:     }
                   3258:     
                   3259:     set fileId [open $gFile w]
                   3260: 
                   3261:     puts -nonewline $fileId [$gTextWindow get 0.0 end]
                   3262: 
                   3263:     close $fileId
                   3264: 
                   3265:     savePrefs
                   3266:     set gChanged 0
                   3267: }
                   3268:  
                   3269: ###########################################################
                   3270: ###########################################################
                   3271: ###########################################################
                   3272: ###########################################################
                   3273: proc closeDocument { { mustClose 0 } { save 1 } } {
                   3274:     global gFile gEditWindow gChanged gPrefs \
                   3275: 	    gPutLine gTryVal gProbVal gHintVal gQuizTemp \
                   3276: 	    gNumberParsedText gSetNumberText gClosedDocument gTextWindow
                   3277:     if { [catch {set gEditWindow}] } { return }
                   3278:     if { ![winfo exists $gEditWindow] } { return }
                   3279:     if { $gClosedDocument } { return }
                   3280:     if { $save && $gChanged } {
                   3281: 	if { [askToSave 0 $mustClose] == "Cancel"  && (! $mustClose ) } { return }
                   3282:     }
                   3283:     if {(!$mustClose)&&[makeSure "Are you sure you wish to stop editing?"]=="Cancel"} {
                   3284: 	return 
                   3285:     }
                   3286:     set gClosedDocument 1
                   3287:     removeFindList
                   3288:     destroy $gEditWindow
                   3289:     removeWindowEntry "$gFile*"
                   3290:     set gFile ""
                   3291:     set gChanged 0
                   3292:     set gPrefs(info) "Problem"
                   3293:     set gPrefs(TeXHeader) ""
                   3294:     set gPrefs(TeXFooter) ""
                   3295:     set gPutLine 1
                   3296:     set gTryVal 99
                   3297:     set gHintVal 1
                   3298:     set gProbVal 1
                   3299:     set gQuizTemp "true"
                   3300:     set gNumberParsedText ""
                   3301:     set gSetNumberText ""
                   3302: }
                   3303: 
                   3304: ###########################################################
                   3305: ###########################################################
                   3306: ###########################################################
                   3307: ###########################################################
                   3308: proc closeRefFile { refNum  { mustClose 0 } { save 1 } } {
                   3309:     global gRefChanged gRefText gRefFile gRefClosed gRefCurLine gRefLine gRefChangedLast
                   3310:     if { [catch {set gRefText($refNum)}] } { return }
                   3311:     if { ![winfo exists $gRefText($refNum)] } { return }
                   3312:     if { $gRefClosed($refNum) } { return }
                   3313:     if { $save && $gRefChanged($refNum) } { 
                   3314: 	if { [askToSave $refNum $mustClose] == "Cancel" && ( ! $mustClose ) } { return }
                   3315:     }
                   3316: 
                   3317:     if { ( ! $mustClose ) && ( [makeSure "Are you sure you wish to stop editing $gRefFile($refNum)?"] == "Cancel" ) } {
                   3318: 	return 
                   3319:     }
                   3320:     set gRefClosed($refNum) 1
                   3321:     removeFindList $refNum
                   3322:     destroy [winfo toplevel $gRefText($refNum)]
                   3323:     removeWindowEntry "Reference $gRefFile($refNum)*"
                   3324:     unset gRefText($refNum) gRefChanged($refNum) gRefClosed($refNum) gRefFile($refNum) \
                   3325: 	gRefCurLine($refNum) gRefLine($refNum) gRefChangedLast($refNum)
                   3326: }
                   3327: 
                   3328: ###########################################################
                   3329: # quit
                   3330: ###########################################################
                   3331: # called when the quit option is selected on the menu, unmaps
                   3332: # all keys.
                   3333: ###########################################################
                   3334: # Arguments: None
                   3335: # Returns: Nothing
                   3336: # Globals: gChanged - whether or not the file has been modified
                   3337: ###########################################################
                   3338: proc quit { { mustClose 0 } } {
                   3339:     global gChanged gRefChanged gRefText
                   3340: 
                   3341:     if { (! $mustClose ) && [makeSure "Are you sure you wish to quit?"] == "Cancel" } {
                   3342: 	return 
                   3343:     }
                   3344: 
                   3345:     if { $gChanged } {
                   3346:  	if { [askToSave 0 $mustClose] == "Cancel" && ( ! $mustClose ) } {
                   3347: 	    return
                   3348: 	}
                   3349:     }
                   3350:     
                   3351:     foreach refNum [array names gRefChanged] {
                   3352: 	if { $gRefChanged($refNum) == 1 } {
                   3353: 	    if { [winfo exists $gRefText($refNum)] } {
                   3354: 		if { [askToSave $refNum $mustClose ] == "Cancel" && (! $mustClose ) } {
                   3355: 		    return
                   3356: 		}
                   3357: 	    }
                   3358: 	}
                   3359:     }
                   3360: 
                   3361:     exec /bin/rm -f quiztemp.ps
                   3362:     exec /bin/rm -f quiztemp.dvi
                   3363:     exec /bin/rm -f quiztemp.tex
                   3364:     exec /bin/rm -f quiztemp.log
                   3365:     exec /bin/rm -f quiztemp.aux
                   3366:     exec /bin/rm -f quiztemp.txt
                   3367: 
                   3368:     unmapAllKeys
                   3369: 
                   3370:     exit
                   3371: }
                   3372: 
                   3373: ###########################################################
                   3374: # createStopButton
                   3375: ###########################################################
                   3376: ###########################################################
                   3377: ###########################################################
                   3378: proc createStopButton {} {
                   3379:     global gStopStatus
                   3380:     if {[winfo exists .stopbutton]} {destroy .stopbutton}
                   3381:     set top [toplevel .stopbutton]
                   3382:     button $top.stop -text "Stop Parser" -command "stopParser"
1.4       albertel 3383:     label $top.status -textvariable gStopStatus -width 35 -anchor w
1.1       albertel 3384:     pack $top.stop $top.status
                   3385:     set gStopStatus ""
                   3386:     grab $top
                   3387:     Centre_Dialog $top default
                   3388:     update
                   3389: }
                   3390: 
                   3391: ###########################################################
                   3392: # destroyStopButton
                   3393: ###########################################################
                   3394: ###########################################################
                   3395: ###########################################################
                   3396: proc destroyStopButton {} {
                   3397:     grab release .stopbutton
                   3398:     destroy .stopbutton
                   3399: }
                   3400: 
                   3401: ###########################################################
                   3402: # createDvi
                   3403: ###########################################################
                   3404: ###########################################################
                   3405: ###########################################################
                   3406: proc createDvi { {showXdvi 1} {showStopButton 0} {useqzparse 0}} {
                   3407:     global gPreviewMode gCreateDviText gPrefs gSetNumberText \
                   3408: 	    gStudentSelection gFile gWindowMenu gLatexId gParseErrorsText \
                   3409: 	    gEditWindow gCreateDviTextTemp gXdviOpt  \
                   3410:             gLoadHeaderSet gCapaConfig gControlDates gNumberParsedText \
                   3411: 	    gDonePrinting
                   3412: 
                   3413:     if { [catch {set gEditWindow}] } { return }
                   3414:     if { ![winfo exists $gEditWindow] } { return }
                   3415:     
                   3416:     catch { destroy .createDviText }
                   3417:     set gCreateDviTextTemp [text .createDviText]
                   3418:     
                   3419:     switch $gPrefs(info) {
                   3420: 	Problem { set type 0 }
                   3421: 	ProblemAnswer { set type 1 }
                   3422: 	Answer { set type 2 }
                   3423:     }
                   3424: 
                   3425:     if { $useqzparse } {
                   3426: 	set gDonePrinting 0
                   3427: 	switch $gPrefs(info) {
                   3428: 	    Problem	{ set type "-T" }
                   3429: 	    Answer	{ set type "-Ta" }
                   3430: 	    ProblemAnswer { set type "-Tb" }
                   3431: 	    default	{ set type "-T"	}
                   3432: 	}
                   3433: 	createCreateDviWin
                   3434: 	grab .createDvi
                   3435: 	$gCreateDviText delete 0.0 end
                   3436: 	if { [setupSetsToPrint set start end 0] == 1 } { return 1 }
                   3437: 	if { [set gStopPrinting [expr 2 == [runLatex \
                   3438:             "echo [pwd] | $gCapaConfig(qzparse_command) \
                   3439: 	    -stu $gStudentSelection(studentNumber) -set $set \
                   3440: 	    -d [pwd] -c [pwd] $type " gCreateDviText] ] ] } {
                   3441: 	    exec rm -f $gStudentSelection(studentNumber).tex quiztemp.tex
                   3442: 	    if {$showStopping} {
                   3443: 		displayMessage "Printing has been stopped."
                   3444: 		set gDonePrinting 1
                   3445: 		set gStopPrinting 0
                   3446: 	    }
                   3447: 	    return 1
                   3448: 	}
                   3449: 	exec mv $gStudentSelection(studentNumber).tex quiztemp.tex
                   3450: 	exec /bin/rm -f quiztemp.dvi 
                   3451:     } else {
                   3452: 	createStopButton
                   3453: 	if { [catch { 
                   3454: 	    set numberParsed [ texParse $type $gSetNumberText \
                   3455: 				   $gStudentSelection(type) $gStudentSelection(random) \
                   3456: 				   $gStudentSelection(studentNumber) \
                   3457: 				   $gStudentSelection(studentName) \
                   3458: 				   gCreateDviTextTemp 1 ] }]} {
                   3459: 	    return
                   3460: 	}
                   3461: 	destroyStopButton
                   3462: 	checkHeader $numberParsed
                   3463: 	
                   3464: 	if { [showParseErrors] != "" } {
                   3465: 	    if { [makeSure "There were errors when parsing the .qz file, \
                   3466: 		continue to create the .dvi?"] =="Cancel" } {
                   3467: 		destroy $gCreateDviTextTemp
                   3468: 		return
                   3469: 	    }   
                   3470: 	}
                   3471: 	
                   3472: 	set error [catch { set fileId [open quiztemp.tex w] } ]
                   3473: 	if { $error } {
                   3474: 	    displayError "Unable to create neccessary temp files, delete all the\
                   3475:  quiztemp file from the class directory."
                   3476: 	    return
                   3477: 	}
                   3478: 
                   3479: 	set filename [file join [file dirname $gFile] TeXheader ]
                   3480: 	set texfileId [open $filename r]
                   3481: 	puts -nonewline $fileId [read $texfileId [file size $filename]]
                   3482: 	close $texfileId
                   3483:     
                   3484: 	puts -nonewline  $fileId "[$gCreateDviTextTemp get 0.0 end]"
                   3485: 	
                   3486: 	set filename [file join [file dirname $gFile] TeXfooter ]
                   3487: 	set texfileId [open $filename r]
                   3488: 	puts $fileId [read $texfileId [file size $filename]]
                   3489: 	close $texfileId
                   3490: 	
                   3491: 	close $fileId
                   3492:     }
                   3493: 
                   3494:     destroy $gCreateDviTextTemp
                   3495: 
                   3496:     if { ![winfo exists .createDvi] } {
                   3497: 	set createDviWindow [toplevel .createDvi]
                   3498: 	$gWindowMenu add command -label "CreateDvi" -command \
                   3499: 		"capaRaise $createDviWindow"
                   3500: 	wm title $createDviWindow "LaTeX Output"
                   3501: 	addFindList -3
                   3502: 
                   3503: 	set windowFrame [frame $createDviWindow.windowFrame]
                   3504: 	set buttonFrame [frame $createDviWindow.buttonFrame]
                   3505: 	
                   3506: 	pack $windowFrame $buttonFrame -side bottom
                   3507: 	pack configure $windowFrame -expand true -fill both
                   3508: 	pack configure $buttonFrame -anchor e
                   3509: 
                   3510: 	scrollbar $windowFrame.scroll -orient vertical -command \
                   3511: 		"$windowFrame.text yview"
                   3512: 	set gCreateDviText [text $windowFrame.text -yscrollcommand \
                   3513: 		"$windowFrame.scroll set" -wrap char -height 40]
                   3514: 	
                   3515: 	pack $windowFrame.scroll $gCreateDviText -side left -expand 0
                   3516: 	pack configure $windowFrame.scroll -expand 0 -fill y
                   3517: 	pack configure $gCreateDviText -expand true -fill both
                   3518: 	
                   3519: 	set appearingFrame [frame $buttonFrame.appearingFrame]
                   3520: 	button $buttonFrame.ok -text Dismiss -command \
                   3521: 		"trace vdelete gFile w updateCreateDvi
                   3522:                  destroy $createDviWindow
                   3523: 	         removeWindowEntry CreateDvi
                   3524:                  removeFindList -3"
                   3525: 	bind $createDviWindow <Destroy> \
                   3526: 		"trace vdelete gFile w updateCreateDvi
                   3527: 	         removeWindowEntry CreateDvi
                   3528:                  removeFindList -3"
                   3529: 	pack $appearingFrame $buttonFrame.ok -side left
                   3530: 
                   3531: 	button $appearingFrame.stop -text "Stop Creating Print Jobs"\
                   3532: 		-command "stopPrinting"
                   3533: 	set name [file rootname [file tail $gFile ] ].dvi
                   3534: 	button $appearingFrame.print -text \
                   3535: 		"Save.dvi file to $name" \
                   3536: 		-command saveDvi
                   3537: 	trace variable gFile w updateCreateDvi
                   3538: 
                   3539: 	if { $showStopButton } {
                   3540: 	    pack $appearingFrame.stop $appearingFrame.print -side left
                   3541: 	    pack forget $appearingFrame.print
                   3542: 	} else {
                   3543: 	    pack $appearingFrame.stop $appearingFrame.print -side left
                   3544: 	    pack forget $appearingFrame.stop
                   3545: 	}
                   3546: 
                   3547: 	Centre_Dialog $createDviWindow default
                   3548: 	update
                   3549:     } else {
                   3550: 	if { $showStopButton } {
                   3551: 	    pack forget .createDvi.buttonFrame.appearingFrame.print
                   3552: 	    pack .createDvi.buttonFrame.appearingFrame.stop
                   3553: 	} else {
                   3554: 	    pack forget .createDvi.buttonFrame.appearingFrame.stop
                   3555: 	    pack .createDvi.buttonFrame.appearingFrame.print
                   3556: 	}
                   3557: 	if { !$useqzparse } { $gCreateDviText delete 0.0 end }
                   3558:     }
                   3559:         
                   3560:     exec /bin/rm -f quiztemp.dvi 
                   3561:     $gCreateDviText insert end      "$gCapaConfig(latex_command)\n"
                   3562:     $gCreateDviText see end
                   3563:     set createdDvi [ runLatex "pwd ; $gCapaConfig(latex_command) quiztemp.tex < \
                   3564: 	    [file join / dev null ]" gCreateDviText]
                   3565: 
                   3566:     if { ($showXdvi == 1)  && ( $createdDvi == 1 ) } { 
                   3567: 	eval "exec $gCapaConfig(xdvi_command) $gXdviOpt quiztemp.dvi >& /dev/null & "
                   3568:     }
                   3569: 
                   3570:     catch { capaRaise $gParseErrorsText }
                   3571:     set gDonePrinting 1
                   3572:     return $createdDvi
                   3573: }
                   3574: 
                   3575: ###########################################################
                   3576: ###########################################################
                   3577: ###########################################################
                   3578: ###########################################################
                   3579: proc stopPrinting {} {
                   3580:     global gStopPrinting
                   3581:     set gStopPrinting 1
                   3582: }
                   3583: 
                   3584: ###########################################################
                   3585: ###########################################################
                   3586: ###########################################################
                   3587: ###########################################################
                   3588: proc saveDvi { } {
                   3589:     global gFile
                   3590: 
                   3591:     set name [file rootname [ file tail $gFile]].dvi
                   3592:     catch { exec rm -f $name }
                   3593: 
                   3594:     if { [ catch { exec cp quiztemp.dvi $name } ] } {
                   3595: 	displayMessage "Unable to create $name "
                   3596:     } else {
                   3597: 	displayMessage "Created $name "
                   3598:     }
                   3599: }
                   3600: 
                   3601: 
                   3602: ###########################################################
                   3603: ###########################################################
                   3604: ###########################################################
                   3605: ###########################################################
                   3606: proc updateCreateDvi { name1 name2 op } {
                   3607:     global gFile
                   3608: 
                   3609:     set name [file rootname [file tail $gFile ] ].dvi
                   3610:     catch { .createDvi.buttonFrame.appearingFrame.print configure \
                   3611: 	    -text "Save.dvi file to $name" }
                   3612: }
                   3613: ###########################################################
                   3614: ###########################################################
                   3615: ###########################################################
                   3616: ###########################################################
                   3617: proc printWindow {} {
                   3618:     global gPrintSelection gWindowMenu gEditWindow gStopPrinting\
                   3619: 	gSetNumberText gMaxSet gFile gChanged
                   3620: 
                   3621:     set gStopPrinting 0
                   3622:     if { [catch {set gEditWindow}] } { return }
                   3623:     if { ![winfo exists $gEditWindow] } { return }
                   3624: 
                   3625:     if { [winfo exists .print] } { 
                   3626: 	capaRaise .print
                   3627: 	return 
                   3628:     }
                   3629:     if { $gChanged } { if { [askToSave 0 0] == "Cancel" } { return } }
                   3630: 
                   3631:     set print [toplevel .print]
                   3632:     $gWindowMenu add command -label "Print" -command "capaRaise $print"
                   3633:     wm title $print "Select a Print Method"
                   3634:     message $print.msg -text "Please specify a print method." -aspect 10000
                   3635:     set oneSetFrame [frame $print.frame1 -relief groove -borderwidth 4]
                   3636:     set moreSetFrame [frame $print.frame2 -relief groove -borderwidth 4]
                   3637:     set buttonFrame [frame $print.buttons]
                   3638:     pack $print.msg $oneSetFrame $moreSetFrame $buttonFrame -side top 
                   3639:     pack configure $oneSetFrame $moreSetFrame -anchor w -fill x
                   3640:    
1.14    ! albertel 3641:     set msg2Frame [frame $moreSetFrame.msg2 -relief solid -borderwidth 2]
1.1       albertel 3642:     set infoFrame [frame $moreSetFrame.frame1]
1.14    ! albertel 3643:     set msg3Frame [frame $moreSetFrame.msg3 -relief solid -borderwidth 2]
        !          3644:     set setFrame [frame $moreSetFrame.frame2] 
        !          3645:     pack $msg2Frame $setFrame $msg3Frame $infoFrame -anchor w
1.1       albertel 3646:     
                   3647:     if {[catch {set gPrintSelection(sets)}]} {set gPrintSelection(sets) printCur}
                   3648:     if {[catch {set gPrintSelection(type)}]} {set gPrintSelection(type) printSpecific}
                   3649:     if {[catch {set gPrintSelection(setend)}]} {set gPrintSelection(setend) $gSetNumberText}
                   3650:     radiobutton $setFrame.specific -text "Print Current Set ($gSetNumberText)" \
                   3651: 	    -value "printCur" -variable gPrintSelection(sets) 
                   3652:     set scaleFrame [frame $setFrame.scales]
                   3653:     pack $setFrame.specific $scaleFrame -anchor w
                   3654: 
                   3655:     for { set i 1 } { $i <= $gMaxSet } { incr i } {
                   3656: 	if { ! [file exists [file join [file dirname $gFile] records "set$i.db"]] } { break }
                   3657:     }
                   3658:     incr i -1
                   3659:     set gPrintSelection(setend) $gSetNumberText
                   3660:     radiobutton $scaleFrame.range -text "Print Set Range:" \
                   3661: 	    -value "printRange" -variable gPrintSelection(sets) 
                   3662:     scale $scaleFrame.start -from 1 -to $i -variable gPrintSelection(setstart) \
                   3663: 	-orient h 
                   3664:     label $scaleFrame.msg -text "to"
                   3665:     scale $scaleFrame.end -from 1 -to $i -variable gPrintSelection(setend) \
                   3666: 	-orient h 
                   3667:     pack $scaleFrame.range $scaleFrame.start $scaleFrame.msg \
                   3668: 	$scaleFrame.end -side left
                   3669: 
                   3670:     button $buttonFrame.ok -text "Select" -command selectedPrintMethod
                   3671:     button $buttonFrame.cancel -text "Cancel" -command \
                   3672: 	    "destroy .print
                   3673:              removeWindowEntry Print"
                   3674:     bind $print <Destroy> "removeWindowEntry Print"
                   3675:     pack $buttonFrame.ok $buttonFrame.cancel -side left
                   3676:     
1.14    ! albertel 3677:     set msgFrame [frame $oneSetFrame.msg -relief solid -borderwidth 2]
1.1       albertel 3678:     set currentDviFrame [frame $oneSetFrame.currentDvi]
                   3679:     set currentPreviewFrame [frame $oneSetFrame.currentPreview]
                   3680:     set randomFrame [frame $oneSetFrame.random]
                   3681:     set specificFrame [frame $infoFrame.specific]
                   3682:     set sectionFrame [frame $infoFrame.section]
                   3683:     set multSectionFrame [frame $infoFrame.multsection]
                   3684:     set wholeClassFrame [frame $infoFrame.wholeClass]
1.14    ! albertel 3685:     pack $msgFrame $currentDviFrame $currentPreviewFrame $randomFrame $specificFrame \
1.1       albertel 3686: 	    $sectionFrame $multSectionFrame $wholeClassFrame -anchor w \
                   3687: 	    -side top
                   3688:     pack configure $specificFrame -expand true -fill both
1.14    ! albertel 3689: 
        !          3690:     label $msgFrame.msg -text "Select:"
        !          3691:     pack $msgFrame.msg -anchor w
        !          3692:     label $msg2Frame.msg -text "Or Select:"
        !          3693:     pack $msg2Frame.msg -anchor w
        !          3694:     label $msg3Frame.msg -text "For:"
        !          3695:     pack $msg3Frame.msg -anchor w
1.1       albertel 3696: 
                   3697:     radiobutton $currentDviFrame.currentDvi -text "Print current .dvi" \
                   3698: 	    -value "printCurrentDvi" -variable gPrintSelection(type) 
                   3699:     pack $currentDviFrame.currentDvi -side left 
                   3700: 
                   3701:     radiobutton $randomFrame.random -text \
                   3702: 	    "Randomly select one student from section:" \
                   3703: 	    -value "printRandom" -variable gPrintSelection(type) 
                   3704:     entry $randomFrame.entry -textvariable gPrintSelection(random)  -width 3 \
                   3705: 	-validate key -validatecommand "limitEntry %W 3 number %P"
                   3706:     pack $randomFrame.random $randomFrame.entry -side left 
                   3707: 
                   3708:     radiobutton $specificFrame.specific -text "Specify the student by:" \
                   3709: 	-value "printSpecific" -variable gPrintSelection(type) 
                   3710:     set studentNumber [frame $specificFrame.studentNumber]
                   3711:     set fullName [frame $specificFrame.fullName]
                   3712:     pack $specificFrame.specific $studentNumber $fullName -side top
                   3713:     pack configure $specificFrame.specific -anchor w
                   3714:     pack configure $studentNumber $fullName -anchor e
                   3715: 
                   3716:     radiobutton $sectionFrame.section -text "Print section" \
                   3717: 	-value "printSection" -variable gPrintSelection(type) 
                   3718:     entry $sectionFrame.entry -textvariable gPrintSelection(section)  -width 3 \
                   3719: 	-validate key -validatecommand "limitEntry %W 3 number %P"
                   3720:     pack $sectionFrame.section $sectionFrame.entry -side left
                   3721: 
                   3722:     radiobutton $multSectionFrame.section -text "Print multiple sections" \
                   3723: 	-value "printMultipleSections" -variable gPrintSelection(type) 
                   3724:     pack $multSectionFrame.section -side left
                   3725: 
                   3726:     radiobutton $wholeClassFrame.wholeClass -text "Print whole class." \
                   3727: 	-value "printWholeClass" -variable gPrintSelection(type) 
                   3728:     pack $wholeClassFrame.wholeClass -side left
                   3729:     
1.10      albertel 3730:     message $studentNumber.msg -text "Student Number: "  -aspect 10000
1.1       albertel 3731:     entry $studentNumber.entry -textvariable gPrintSelection(studentNumber) -width 9 \
                   3732: 	-validate key -validatecommand "limitEntry %W 9 any %P"
                   3733:     pack $studentNumber.msg $studentNumber.entry -side left
                   3734: 
                   3735:     message $fullName.msg -text "Student Name: "  -aspect 10000 
                   3736:     entry $fullName.entry -textvariable gPrintSelection(studentName) -width 30 \
                   3737: 	-validate key -validatecommand "limitEntry %W 30 any %P"
                   3738:     pack $fullName.msg $fullName.entry -side left
                   3739: 
                   3740:     trace variable gPrintSelection(studentNumber) w \
                   3741: 	"global gPrintSelection; set gPrintSelection(type) printSpecific ;#"
                   3742:     trace variable gPrintSelection(studentName) w \
                   3743: 	"global gPrintSelection; set gPrintSelection(type) printSpecific ;#"
                   3744: 
                   3745: #    puts "trace info:[trace vinfo gPrintSelection(studentNumber)]"
                   3746: 
                   3747:     bind $studentNumber.entry <KeyPress-Return> \
                   3748: 	"fillInStudent gPrintSelection(studentName) gPrintSelection(studentNumber) 0"
                   3749:     bind $fullName.entry <KeyPress-Return> \
                   3750: 	"fillInStudent gPrintSelection(studentName) gPrintSelection(studentNumber) 1"
                   3751: 
                   3752: 
                   3753:     #Disable the entry boxes that are not selected, and enable the 
                   3754:     #ones that are
                   3755: #    $specificFrame.specific configure -command "
                   3756: #           $studentNumber.entry configure -state normal
                   3757: #           $fullName.entry configure -state normal"
                   3758: #    $randomFrame.random configure -command "
                   3759: #           $studentNumber.entry configure -state disabled
                   3760: #           $fullName.entry configure -state disabled"    
                   3761: 
                   3762:     #If the window had been called up before we need to check the state
                   3763:     #of the variable and disable/enable the correct enry boxes
                   3764: #    if { $gPrintSelection(type) == "printSpecific" } {
                   3765: #	   $studentNumber.entry configure -state normal
                   3766: #           $fullName.entry configure -state normal
                   3767: #    } 
                   3768: #    if { $gPrintSelection(type) == "printRandom" } {
                   3769: #           $studentNumber.entry configure -state disabled
                   3770: #           $fullName.entry configure -state disabled
                   3771: #    } 
                   3772:     
                   3773:     Centre_Dialog $print default
                   3774: }
                   3775: 
                   3776: proc selectedPrintMethod {} {
                   3777:     global gStopPrinting gPrintSelection gStudentSelection
                   3778:     
                   3779:     switch $gPrintSelection(type) {
                   3780: 	printSpecific {
                   3781: 	    if { $gPrintSelection(studentNumber) == "" } {
                   3782: 		displayError "You must specify a student number."
                   3783: 		return
                   3784: 	    }
                   3785: 	}
                   3786: 	printSection {
                   3787: 	    if { $gPrintSelection(section)== "" } {
                   3788: 		displayError "You must specify a section."
                   3789: 		return
                   3790: 	    }
                   3791: 	}
                   3792: 	default {}
                   3793:     }
                   3794: 	
                   3795:     destroy .print
                   3796:     removeWindowEntry Print
                   3797:     [set gPrintSelection(type)]
                   3798:     set gStopPrinting 0 
                   3799: }
                   3800: 
                   3801: ###########################################################
                   3802: # createCreateDviWin
                   3803: ###########################################################
                   3804: ###########################################################
                   3805: ###########################################################
                   3806: proc createCreateDviWin {} { 
                   3807:     global gWindowMenu gFile gCreateDviText
                   3808:     if { ![winfo exists .createDvi] } {
                   3809: 	set createDviWindow [toplevel .createDvi]
                   3810: 	$gWindowMenu add command -label "CreateDvi" -command \
                   3811: 		"capaRaise $createDviWindow"
                   3812: 	wm title $createDviWindow "LaTeX Output"
                   3813: 
                   3814: 	set windowFrame [frame $createDviWindow.windowFrame]
                   3815: 	set buttonFrame [frame $createDviWindow.buttonFrame]
                   3816: 	
                   3817: 	pack $windowFrame $buttonFrame -side bottom
                   3818: 	pack configure $windowFrame -expand true -fill both
                   3819: 	pack configure $buttonFrame -anchor e
                   3820: 
                   3821: 	scrollbar $windowFrame.scroll -orient vertical -command \
                   3822: 		"$windowFrame.text yview"
                   3823: 	set gCreateDviText [text $windowFrame.text -yscrollcommand \
                   3824: 		"$windowFrame.scroll set" -wrap char -height 40]
                   3825: 	
                   3826: 	pack $windowFrame.scroll $gCreateDviText -side left -expand 0
                   3827: 	pack configure $windowFrame.scroll -expand 0 -fill y
                   3828: 	pack configure $gCreateDviText -expand true -fill both
                   3829: 	
                   3830: 	set appearingFrame [frame $buttonFrame.appearingFrame]
                   3831: 	button $buttonFrame.ok -text Dismiss -command \
                   3832: 	    "checkDestroyPrint $createDviWindow"
                   3833: 	wm protocol $createDviWindow WM_DELETE_WINDOW \
                   3834: 	    "checkDestroyPrint $createDviWindow"
                   3835: 	pack $appearingFrame $buttonFrame.ok -side left
                   3836: 
                   3837: 	button $appearingFrame.stop -text "Stop Creating Print Jobs"\
                   3838: 		-command "stopPrinting"
                   3839: 	set name [file rootname [file tail $gFile ] ].dvi
                   3840: 	button $appearingFrame.print -text \
                   3841: 		"Save.dvi file to $name" \
                   3842: 		-command saveDvi
                   3843: 	trace variable gFile w updateCreateDvi
                   3844: 
                   3845: 	pack $appearingFrame.stop $appearingFrame.print -side left
                   3846: 	pack forget $appearingFrame.print
                   3847: 
                   3848: 	Centre_Dialog $createDviWindow default
                   3849: 	update
                   3850:     } else {
                   3851: 	pack forget .createDvi.buttonFrame.appearingFrame.print
                   3852: 	pack .createDvi.buttonFrame.appearingFrame.stop
                   3853:     }
                   3854: }
                   3855: 
                   3856: ###########################################################
                   3857: # printBody
                   3858: ###########################################################
                   3859: # sends the file quiztemp.ps to the printer through lpr using
                   3860: # the option foud in gLprCommand
                   3861: ###########################################################
1.10      albertel 3862: # Arguments: lprCommand - actual command to be run to print
                   3863: #            showCompletionMessage - (defaults to 1 true)
                   3864: #                      controls whether the print complete 
                   3865: #                      message gets shown
1.1       albertel 3866: # Returns: Nothing
                   3867: # Globals: gCapaConfig -
                   3868: #          gStopPrinting -
                   3869: # Files: quiztemp.ps - file containg info to print (removed)
                   3870: ###########################################################
1.10      albertel 3871: proc printBody { lprCommand { showCompletionMessage 1 } } {
1.1       albertel 3872:     global gCapaConfig gStopPrinting gDonePrinting
                   3873: 
                   3874:     set errorMsg ""
                   3875:     set error [ catch {exec $gCapaConfig(dvips_command) quiztemp.dvi \
                   3876: 	    -o quiztemp.ps >& /dev/null} errorMsg ]
                   3877:     if { $error } {
                   3878: 	displayError \
1.12      albertel 3879: 		"When attempting to run dvips an error occurred : $errorMsg"
1.1       albertel 3880: 	return 1
                   3881:     }
                   3882: 
                   3883:     if { $gStopPrinting } {
                   3884: 	displayMessage "Printing has been stopped."
                   3885: 	set gStopPrinting 0
                   3886: 	set gDonePrinting 1
                   3887: 	return 1
                   3888:     }
                   3889: 
                   3890:     set errorMsg
                   3891:     set error [catch {set returnMessage [eval "exec $lprCommand"] } errorMsg ]
                   3892:     
                   3893:     if { $error == 1 } {
1.12      albertel 3894:         displayError "When attempting to print an error occurred : $errorMsg"
1.1       albertel 3895: 	return 1
                   3896:     } else {
                   3897: 	if { $showCompletionMessage } {
                   3898: 	    displayMessage "Print job sent to the printer.\n $returnMessage"
                   3899: 	}
                   3900:     }
                   3901:     
                   3902:     return 0
                   3903: }
                   3904: 
                   3905: 
                   3906: 
                   3907: ###########################################################
                   3908: ###########################################################
                   3909: ###########################################################
                   3910: ###########################################################
                   3911: proc printCurrentDvi {} {
                   3912: 
                   3913:     set lprCommand [getLprCommand quiztemp.ps]
                   3914: 
                   3915:     if {$lprCommand == ""} { 
                   3916: 	displayError "You must at least specify a print queue for lpr. \
                   3917: 		Nothing printed."
                   3918: 	return 
                   3919:     }
                   3920: 
                   3921:     if {$lprCommand == "Cancel"} { 
                   3922: 	return
                   3923:     }
                   3924:     
                   3925:     printBody $lprCommand
                   3926:     
                   3927:     return 0
                   3928: }
                   3929: 
                   3930: ###########################################################
                   3931: ###########################################################
                   3932: ###########################################################
                   3933: ###########################################################
                   3934: proc printCurrentPreview {} {
                   3935: 
                   3936:     set lprCommand [getLprCommand quiztemp.ps]
                   3937: 
                   3938:     if {$lprCommand == ""} { 
                   3939: 	displayError "You must at least specify a print queue for lpr. \
                   3940: 		Nothing printed."
                   3941: 	return 
                   3942:     }
                   3943: 
                   3944:     if {$lprCommand == "Cancel"} { 
                   3945: 	return
                   3946:     }
                   3947: 
                   3948:     if { [createDvi 0] == 2 } {
                   3949: 	displayMessage "Printing has been stopped"
                   3950:     }
                   3951: 
                   3952:     printBody $lprCommand
                   3953: 
                   3954:     return 0
                   3955: }
                   3956: 
                   3957: ###########################################################
                   3958: ###########################################################
                   3959: ###########################################################
                   3960: ###########################################################
                   3961: proc printRandom {} {
                   3962:     global gStudentSelection gPrintSelection
                   3963:     
                   3964:     set lprCommand [getLprCommand quiztemp.ps]
                   3965: 
                   3966:     if {$lprCommand == ""} { 
                   3967: 	displayError "You must at least specify a print queue for lpr. \
                   3968: 		Nothing printed."
                   3969: 	return 
                   3970:     }
                   3971: 
                   3972:     if {$lprCommand == "Cancel"} { 
                   3973: 	return
                   3974:     }
                   3975: 
                   3976:     set type $gStudentSelection(type)
                   3977:     set random $gStudentSelection(random)
                   3978: 
                   3979:     set gStudentSelection(type) Random
                   3980:     set gStudentSelection(random) $gPrintSelection(random)
                   3981: 
                   3982:     if { [createDvi 0 1] == 2 } {
                   3983: 	displayMessage "Printing has been stopped"
                   3984:     }
                   3985: 
                   3986:     printBody $lprCommand
                   3987: 
                   3988:     set gStudentSelection(type) $type
                   3989:     set gStudentSelection(random) $random
                   3990:     
                   3991:     return 0
                   3992: }
                   3993: 
                   3994: ###########################################################
                   3995: ###########################################################
                   3996: ###########################################################
                   3997: ###########################################################
                   3998: proc printSpecific {} {
                   3999:     global gStudentSelection gPrintSelection
                   4000:     
                   4001:     set lprCommand [getLprCommand quiztemp.ps]
                   4002: 
                   4003:     if {$lprCommand == ""} { 
                   4004: 	displayError "You must at least specify a print queue for lpr. \
                   4005: 		Nothing printed."
                   4006: 	return 
                   4007:     }
                   4008: 
                   4009:     if {$lprCommand == "Cancel"} { 
                   4010: 	return
                   4011:     }
                   4012: 
                   4013:     set type $gStudentSelection(type)
                   4014:     set studentNumber $gStudentSelection(studentNumber)
                   4015: 
                   4016:     set gStudentSelection(type) Specific
                   4017:     set gStudentSelection(studentNumber) $gPrintSelection(studentNumber)
                   4018: 
                   4019:     if { [createDvi 0 1 1] == 2 } {
                   4020: 	displayMessage "Printing has been stopped"
                   4021:     }
                   4022: 
                   4023:     printBody $lprCommand
                   4024: 
                   4025:     set gStudentSelection(type) $type
                   4026:     set gStudentSelection(studentNumber) $studentNumber
                   4027:     
                   4028:     return 0
                   4029: }
                   4030: 
                   4031: ###########################################################
                   4032: ###########################################################
                   4033: ###########################################################
                   4034: ###########################################################
                   4035: proc printStudent { studentNumber } {
                   4036:     global gStudentSelection gStopPrinting gDonePrinting
                   4037:     
                   4038:     set type $gStudentSelection(type)
                   4039:     set studentNumberOld $gStudentSelection(studentNumber)
                   4040: 
                   4041:     set gStudentSelection(type) Specific
                   4042:     set gStudentSelection(studentNumber) $studentNumber
                   4043: 
                   4044:     set createdDvi [createDvi 0 1]
                   4045: 
                   4046:     if { $createdDvi == 1 } { printBody } else { 
                   4047: 	displayMessage "Printing has been stopped"
                   4048: 	set gStopPrinting 0
                   4049: 	set gDonePrinting 1
                   4050:     }
                   4051:     
                   4052:     set gStudentSelection(type) $type
                   4053:     set gStudentSelection(studentNumber) $studentNumberOld
                   4054: 
                   4055:     return 0
                   4056: }
                   4057: 
                   4058: proc checkDestroyPrint { createDviWindow } {
                   4059:     global gDonePrinting
                   4060:     if { !$gDonePrinting } {
                   4061: 	if { [makeSure "Do you really wish to stop printing?"] == "Yes" } {
                   4062: 	    global gStopPrinting 
                   4063: 	    set gStopPrinting 1
                   4064: 	    after 1000 "destroyPrint $createDviWindow"
                   4065: 	}
                   4066: 	return
                   4067:     }
                   4068:     destroyPrint $createDviWindow
                   4069: }
                   4070: 
                   4071: proc destroyPrint { createDviWindow } {
                   4072:     trace vdelete gFile w updateCreateDvi
                   4073:     destroy $createDviWindow
                   4074:     removeWindowEntry CreateDvi
                   4075: }
                   4076: 
                   4077: ###########################################################
                   4078: ###########################################################
                   4079: ###########################################################
                   4080: ###########################################################
                   4081: proc setupSetsToPrint { setVar startVar endVar {checkForHeader 1}} {
                   4082:     global gPrintSelection gSetNumberText gLoadHeaderSet 
                   4083:     upvar $setVar set $startVar start $endVar end
                   4084:     if { $gPrintSelection(sets) == "printRange" } {
                   4085: 	set start $gPrintSelection(setstart)
                   4086: 	set end $gPrintSelection(setend)
                   4087: 	set set "$start:$end"
                   4088: 	set errors ""
                   4089: 	for {set i $start}  {$i <= $end} {incr i} {
                   4090: 	    set gLoadHeaderSet $i
                   4091: 	    if {[catch {getHeaderInfo}]} { append errors ", $i" }
                   4092: 	}
                   4093: 	if { $checkForHeader && $errors != "" } {
                   4094: 	    set errors [string range $errors 1 end]
                   4095: 	    if { [llength $errors] > 1 } { 
                   4096: 		set errors "s$errors" 
                   4097: 		set errors [linsert $errors [expr [llength $errors] - 1] and]
                   4098: 	    }
                   4099: 	    displayError "DB header has not yet been set for set$errors. Please set the DB Header before printing Sections."
                   4100: 	    return 1
                   4101: 	}
                   4102:     } else {
                   4103: 	set start [set end [set set $gSetNumberText]]
                   4104: 	set gLoadHeaderSet $gSetNumberText
                   4105: 	if {$checkForHeader && [catch {getHeaderInfo}]} {
                   4106: 	    displayError "DB header has not yet been set. Please set the DB Header before printing Sections."
                   4107: 	    return 1
                   4108: 	}
                   4109:     }
                   4110: }
                   4111: 
                   4112: ###########################################################
                   4113: ###########################################################
                   4114: ###########################################################
                   4115: ###########################################################
                   4116: proc printSection { { lprCommand "" } } {
                   4117:     global gPrintSelection gCapaConfig gSetNumberText gWindowMenu \
                   4118: 	gCreateDviText gStopPrinting gPrefs gFile gDonePrinting \
                   4119: 	gLoadHeaderSet
                   4120: 
                   4121:     set gDonePrinting 0
                   4122:     set showStopping 0
                   4123: 
                   4124:     if { [setupSetsToPrint set start end] == 1 } { return 1}
                   4125:     if { $lprCommand == "" } {
                   4126: 	set showStopping 1
                   4127: 	set lprCommand [getLprCommand quiztemp.ps]
                   4128: 	if { $lprCommand == "" } {
                   4129: 	    displayError "Print command was empty, unable to print."
                   4130: 	    return 1
                   4131: 	}
                   4132: 	if {$lprCommand == "Cancel" } {
                   4133: 	    return 1
                   4134: 	}
                   4135:     }
                   4136: 
                   4137:     createCreateDviWin
                   4138:     
                   4139:     grab .createDvi
                   4140: 
                   4141:     $gCreateDviText delete 0.0 end
                   4142: 
                   4143:     switch $gPrefs(info) {
                   4144: 	Problem	{ set type "-T" }
                   4145: 	Answer	{ set type "-Ta" }
                   4146: 	ProblemAnswer { set type "-Tb" }
                   4147: 	default	{ set type "-T"	}
                   4148:     }
                   4149: 
1.8       albertel 4150:     set prSection [string trimleft $gPrintSelection(section) 0]
                   4151: 
1.1       albertel 4152:     if { [set gStopPrinting [expr 2 == [runLatex \
                   4153: 	    "echo [pwd] | $gCapaConfig(qzparse_command) \
1.8       albertel 4154: 	    -sec $prSection -set $set \
1.1       albertel 4155: 	    -d [pwd] -c [pwd] $type " gCreateDviText] ] ] } {
                   4156: 	for {set i $start} { $i <= $end} { incr i } {
1.8       albertel 4157: 	    exec rm -f section$prSection-set$i.tex 
1.1       albertel 4158: 	}
                   4159: 	if {$showStopping} {
                   4160: 	    displayMessage "Printing has been stopped."
                   4161: 	    set gDonePrinting 1
                   4162: 	    set gStopPrinting 0
                   4163: 	}
                   4164: 	return 1
                   4165:     }
                   4166:     if { $gStopPrinting } {
                   4167: 	displayMessage "Printing has been stopped."
                   4168: 	set gDonePrinting 1
                   4169: 	set gStopPrinting 0
                   4170: 	return 1
                   4171:     }
                   4172: 
                   4173:     for { set i $start} { $i <= $end } { incr i } {
1.8       albertel 4174: 	if { ! [file exists section$prSection-set$i.tex] } {
1.1       albertel 4175: 	    if {$showStopping} {
                   4176: 		displayError "The qzparse command: $gCapaConfig(qzparse_command), was unable to produce the expected output. Printing stopped"
                   4177: 		set gStopPrinting 0
                   4178: 		set gDonePrinting 1
                   4179: 	    }
                   4180: 	    return 2
                   4181: 	}
                   4182: 	
1.8       albertel 4183: 	exec mv section$prSection-set$i.tex quiztemp.tex
1.1       albertel 4184: 	exec /bin/rm -f quiztemp.dvi 
                   4185: 
                   4186: 	$gCreateDviText insert end "$gCapaConfig(latex_command)\n"
                   4187: 	$gCreateDviText see end 
                   4188: 	
                   4189: 	if { [set gStopPrinting [ expr 2 == [runLatex \
                   4190: 	    "pwd ; $gCapaConfig(latex_command) \
                   4191: 	    quiztemp.tex < [file join / dev null ]" gCreateDviText ] ] ] } {
                   4192: 	    if {$showStopping} {
                   4193: 		displayError "The LaTeX command: $gCapaConfig(latex_command), was unable to produce the expected output. Printing stopped"
                   4194: 		set gStopPrinting 0
                   4195: 		set gDonePrinting 1
                   4196: 	    }
                   4197: 	    return 1
                   4198: 	}
                   4199: 
                   4200: 	if { $gStopPrinting } {
                   4201: 	    displayMessage "Printing has been stopped."
                   4202: 	    set gDonePrinting 1
                   4203: 	    set gStopPrinting 0
                   4204: 	    return 1
                   4205: 	}
                   4206: 
                   4207: 	set a [expr ($showStopping) && ($end == $i)]
                   4208: 	if { [set gStopPrinting [printBody $lprCommand $a ] ] } {
                   4209: 	    if {$showStopping} {
                   4210: 		displayMessage "Printing has been stopped."
                   4211: 		set gDonePrinting 1
                   4212: 		set gStopPrinting 0
                   4213: 	    }
                   4214: 	    return 1
                   4215: 	}
                   4216: 	if { $gStopPrinting } {
                   4217: 	    displayMessage "Printing has been stopped."
                   4218: 	    set gDonePrinting 1
                   4219: 	    set gStopPrinting 0
                   4220: 	    return 1
                   4221: 	}
                   4222:     }
                   4223: 
                   4224:     set gDonePrinting 1
                   4225:     return 0
                   4226: }
                   4227: 
                   4228: ###########################################################
                   4229: ###########################################################
                   4230: ###########################################################
                   4231: ###########################################################
                   4232: proc printMultipleSections { } {
                   4233:     global gPrintSelection gCapaConfig gSetNumberText gWindowMenu \
                   4234: 	    gCreateDviText gStopPrinting gPrefs gDonePrinting
                   4235:     
                   4236:     #checks if the DB Header is set
                   4237:     if { [setupSetsToPrint set start end] == 1 } { return 1}
                   4238: 
                   4239:     set sectionList [ getExistingSections ]
                   4240:     set sectionsToPrint [ pickSections $sectionList "Select Sections to Print:" ]
                   4241: 
                   4242:     if { $sectionsToPrint == "" } {
                   4243: 	displayMessage "No sections selected, therefore nothing was printed."
                   4244: 	return 1
                   4245:     }
                   4246:     if { $sectionsToPrint == "Cancel" } {
                   4247: 	return 1
                   4248:     }
                   4249: 
                   4250:     set lprCommand [getLprCommand quiztemp.ps]
                   4251:     if { $lprCommand == "" } {
                   4252: 	displayError "Print command was empty, unable to print."
                   4253: 	return 1
                   4254:     }
                   4255:     if {$lprCommand == "Cancel" } {
                   4256: 	return 1
                   4257:     }
                   4258: 
                   4259:     if { [makeSure "You have selected to print $gPrefs(info)s for sections: [string trim $sectionsToPrint], using the print command \"$lprCommand\", continue?"] == "Cancel" } {
                   4260: 	return 1
                   4261:     }
                   4262: 
                   4263:     foreach section $sectionsToPrint {
                   4264: 	set gDonePrinting 0
                   4265: 	set gPrintSelection(section) $section
                   4266: 	if { [set gStopPrinting [printSection $lprCommand] ] } {
                   4267: 	    set gDonePrinting 0
                   4268: 	    if { $gStopPrinting == 2 } {
                   4269: 		displayError "The qzparse command: $gCapaConfig(qzparse_command), was unable to produce the expected output. Printing stopped"
                   4270: 	    } else {
                   4271: 		displayMessage "Printing has been stopped."
                   4272: 	    }
                   4273: 	    set gDonePrinting 1
                   4274: 	    return 1
                   4275: 	}
                   4276: 	if { $gStopPrinting } {
                   4277: 	    displayMessage "Printing has been stopped."
                   4278: 	    set gStopPrinting 0
                   4279: 	    set gDonePrinting 1
                   4280: 	    return 1
                   4281: 	}
                   4282:     }
                   4283:     set gDonePrinting 1
                   4284:     displayMessage "Print jobs sent to the printer."
                   4285:     return 0
                   4286: }
                   4287: 
                   4288: ###########################################################
                   4289: ###########################################################
                   4290: ###########################################################
                   4291: ###########################################################
                   4292: proc printWholeClass { } {
                   4293:     global gPrintSelection gCapaConfig gSetNumberText gWindowMenu \
                   4294: 	    gCreateDviText gStopPrinting gPrefs gDonePrinting
                   4295:     
                   4296:     if {[catch {getHeaderInfo}]} {
                   4297: 	displayError "DB header has not yet been set, Please set DB Header before printing Sections."
                   4298: 	return 1
                   4299:     }
                   4300:     set sectionsToPrint [ getExistingSections ]
                   4301: 
                   4302:     if { $sectionsToPrint == "" } {
                   4303: 	displayMessage "No sections exist, therefore nothing was printed."
                   4304: 	return 1
                   4305:     }
                   4306: 
                   4307:     set lprCommand [getLprCommand quiztemp.ps]
                   4308: 
                   4309:     if { $lprCommand == "" } {
                   4310: 	displayError "Print command was empty, unable to print."
                   4311: 	return 1
                   4312:     }
                   4313:     if {$lprCommand == "Cancel" } {
                   4314: 	return 1
                   4315:     }
                   4316: 
                   4317:     if { [makeSure "You have selected to print $gPrefs(info)s for the entire class of [llength $sectionsToPrint] sections, using the print command $lprCommand, continue?"] == "Cancel" } {
                   4318: 	return 1
                   4319:     }
                   4320: 
                   4321:     foreach section $sectionsToPrint {
                   4322: 	set section [lindex $section 0]
                   4323: 	set gPrintSelection(section) $section
                   4324: 	set gStopPrinting [printSection $lprCommand]
                   4325: 	if { $gStopPrinting } {
                   4326: 	    if { $gStopPrinting == 2 } {
                   4327: 		displayError "$gCapaConfig(qzparse_command) was unable to produce the expected output. Printing stopped"
                   4328: 	    } else {
                   4329: 		displayMessage "Printing has been stopped."
                   4330: 	    }
                   4331: 	    set gDonePrinting 1
                   4332: 	    set gStopPrinting 0
                   4333: 	    return 1
                   4334: 	}
                   4335:     }
                   4336:     set gDonePrinting 1
                   4337:     displayMessage "Print jobs sent to the printer."
                   4338:     return 0
                   4339: }
                   4340: 
                   4341: ###########################################################
                   4342: # analyzeSet
                   4343: ###########################################################
                   4344: ###########################################################
                   4345: ###########################################################
                   4346: proc analyzeSet {} {
                   4347:     global gChanged gWindowMenu gEditWindow gAnalyze gSetNumberText gNumberParsedText
                   4348: 
                   4349:     if { [catch {winfo exists $gEditWindow}] } { return }
                   4350:     if { ![ winfo exists $gEditWindow ] } { return }
                   4351:     if { $gChanged } { if { [askToSave 0 0] == "Cancel" } { return } }
                   4352:     if { [winfo exists .analyzeSet] } { 
                   4353: 	capaRaise .analyzeSet
                   4354: 	return
                   4355:     }
                   4356: 
                   4357:     set analyze [toplevel .analyzeSet]
                   4358:     $gWindowMenu add command -label "AnalyzeSet" \
                   4359: 	    -command "capaRaise $analyze"
                   4360:     wm title $analyze "Analyze Set"
                   4361:     
                   4362:     set settingsFrame [frame $analyze.settingsFrame]
                   4363:     set dataFrame [frame $analyze.dataFrame]
                   4364:     pack $settingsFrame $dataFrame -side top
                   4365: 
                   4366:     set classFrame [frame $settingsFrame.classFrame]
                   4367:     set setFrame [frame $settingsFrame.setFrame]
                   4368:     set probFrame [frame $settingsFrame.probFrame]
                   4369:     set statusFrame [frame $settingsFrame.statusFrame]
                   4370:     set statusBar [frame $settingsFrame.statusBar]
                   4371:     set buttonFrame [frame $settingsFrame.buttonFrame]
                   4372:     pack $classFrame $setFrame $probFrame $statusFrame $statusBar $buttonFrame \
                   4373: 	-side top
                   4374:     
                   4375:     set canvasFrame [frame $dataFrame.canvasFrame]
                   4376:     set numberFrame [frame $dataFrame.numberFrame]
                   4377:     pack $canvasFrame $numberFrame -side top
                   4378: 
                   4379:     set gAnalyze(class) [pwd]
                   4380:     label $classFrame.label -textvariable gAnalyze(class)
                   4381:     pack $classFrame.label
                   4382:     
                   4383:     set gAnalyze(set) $gSetNumberText
                   4384:     label $setFrame.lbl -text "Set number:"
                   4385:     label $setFrame.set -textvariable gAnalyze(set)
                   4386: #    button $setFrame.change -text "Change" -command analyzeChangeSet
                   4387:     pack $setFrame.lbl $setFrame.set -side left
                   4388: 
                   4389:     if { [set gAnalyze(maxprob) $gNumberParsedText] == "" } { set gAnalyze(maxprob) 1 }
                   4390:     
                   4391:     label $probFrame.label -text "Problem Number :"
                   4392:     set gAnalyze(scale) [scale $probFrame.problem \
                   4393: 			     -from 1 -to $gAnalyze(maxprob) \
                   4394: 			     -variable gAnalyze(prob) -command analyzeUpdate \
                   4395: 			     -orient h -length 150 -tickinterval 1]
                   4396:     pack $probFrame.label $probFrame.problem -side left
                   4397: 
                   4398:     set gAnalyze(status) ""
                   4399:     label $statusFrame.label -text "Status:" 
                   4400:     label $statusFrame.status -textvariable gAnalyze(status)
                   4401:     pack $statusFrame.label $statusFrame.status -side left
                   4402: 
                   4403:     set gAnalyze(statcanvas) [canvas $statusBar.canvas -width 200 -height 20]
                   4404:     pack $statusBar.canvas
                   4405:     $gAnalyze(statcanvas) create rectangle 1 1 199 19 -outline black
                   4406:     set gAnalyze(bar) [$gAnalyze(statcanvas) create rectangle 1 1 1 19 -fill red -outline black]
                   4407: 
                   4408:     button $buttonFrame.class -text "Run Class" -command "analyzeClass 1"
                   4409:     button $buttonFrame.random -text "Run Random" -command analyzeRandom
                   4410:     button $buttonFrame.stop -text "Stop" -command analyzeStop
                   4411:     button $buttonFrame.close -text "Dismiss" -comman analyzeClose
                   4412:     pack $buttonFrame.class $buttonFrame.random $buttonFrame.stop \
                   4413: 	$buttonFrame.close -side left
                   4414:     
                   4415:     set gAnalyze(canvaswidth) 600
                   4416:     set gAnalyze(canvas) [canvas $canvasFrame.canvas -width $gAnalyze(canvaswidth) \
                   4417: 			      -height 100]
                   4418:     pack $gAnalyze(canvas)
                   4419:     
                   4420:     set hiFrame [frame $numberFrame.hiFrame]
                   4421:     set lowFrame [frame $numberFrame.lowFrame]
                   4422:     set uniqFrame [frame $numberFrame.uniqFrame]
                   4423:     pack $lowFrame $hiFrame $uniqFrame -side left
                   4424:     pack configure $hiFrame -anchor e
                   4425:     pack configure $lowFrame -anchor w
                   4426: 
                   4427:     label $hiFrame.label -text "High End:"
                   4428:     label $hiFrame.num -textvariable gAnalyze(highnum)
                   4429:     pack $hiFrame.label $hiFrame.num -side left
                   4430: 
                   4431:     label $lowFrame.label -text "Low End:"
                   4432:     label $lowFrame.num -textvariable gAnalyze(lownum)
                   4433:     pack $lowFrame.label $lowFrame.num -side left
                   4434: 
                   4435:     label $uniqFrame.label -text "Num. Unique:"
                   4436:     label $uniqFrame.num -textvariable gAnalyze(numuniq)
                   4437:     pack $uniqFrame.label $uniqFrame.num -side left
                   4438: 
                   4439:     set gAnalyze(studentNumbers) [getStudentNumbers]
                   4440:     set gAnalyze(exit) 0
                   4441: }
                   4442: 
                   4443: ###########################################################
                   4444: # analyzeClass
                   4445: ###########################################################
                   4446: ###########################################################
                   4447: ###########################################################
                   4448: proc analyzeClass { {start 1} } {
                   4449:     global gAnalyze gCapaConfig
                   4450:     if { $gAnalyze(studentNumbers)=="" } { return }
                   4451:     if { $start } { 
                   4452: 	set gAnalyze(toprocess) $gAnalyze(studentNumbers) 
                   4453: 	set gAnalyze(stop) 0
                   4454: 	set gAnalyze(update) 1
                   4455: 	set gAnalyze(done) 0
                   4456: 	set gAnalyze(total) [expr [llength $gAnalyze(toprocess)]/3]
                   4457: 	foreach name [array names gAnalyze *.\[alhu\]*] { unset gAnalyze($name) }
                   4458:     }
                   4459:     set number [lindex $gAnalyze(toprocess) 0]
                   4460:     set name [lindex $gAnalyze(toprocess) 1]
                   4461:     set section [lindex $gAnalyze(toprocess) 2]
                   4462:     set gAnalyze(toprocess) [lrange $gAnalyze(toprocess) 3 end]
1.11      albertel 4463:     set command "$gCapaConfig(answers_command) $number \"$name\" 0 $gAnalyze(set)"
1.1       albertel 4464:     set fileId [open "|$command" "r"]
                   4465:     set gAnalyze(pid) [pid $fileId]
                   4466:     fconfigure $fileId -blocking 0
                   4467:     fileevent $fileId readable "analyzeLine $fileId"
                   4468:     set gAnalyze(status) "Processing $number"
                   4469:     incr gAnalyze(done)
                   4470:     $gAnalyze(statcanvas) coords $gAnalyze(bar) 1 1 [expr 200*($gAnalyze(done)/double($gAnalyze(total)))] 19
                   4471:     update idletasks
                   4472: }
                   4473: 
                   4474: ###########################################################
                   4475: # analyzeEatQuestion
                   4476: ###########################################################
                   4477: ###########################################################
                   4478: ###########################################################
                   4479: proc analyzeEatQuestion { fileId } {
                   4480:     global gAnalyze 
                   4481:     if { $gAnalyze(exit) } { 
                   4482: 	fileevent $fileId readable ""
                   4483: 	catch {close $fileId}
                   4484: 	return
                   4485:     }
                   4486:     set aline [gets $fileId]
                   4487:     if { $aline != "" } {
                   4488: 	switch -- [lindex [split $aline :] 0] {
                   4489: 	    EQES { fileevent $fileId readable "analyzeLine $fileId" }
                   4490: 	}
                   4491:     }
                   4492:     if { [eof $fileId] } { analyzeEnd $fileId }
                   4493: }
                   4494: 
                   4495: ###########################################################
                   4496: # analyzeLine
                   4497: ###########################################################
                   4498: ###########################################################
                   4499: ###########################################################
                   4500: proc analyzeLine { fileId } {
                   4501:     global gAnalyze 
                   4502:     
                   4503:     if { $gAnalyze(exit) } { 
                   4504: 	fileevent $fileId readable ""
                   4505: 	catch {close $fileId}
                   4506: 	return
                   4507:     }
                   4508:     set aline [gets $fileId]
                   4509:     if { $aline != "" } {
                   4510: 	switch [lindex [split $aline :] 0] {
                   4511: 	    ANS { 
                   4512: 		incr gAnalyze(problemNum)
                   4513: 		set ans [string range $aline 4 end]
                   4514: 		set length [llength $ans]
                   4515: 		lappend gAnalyze($gAnalyze(problemNum).ans) \
                   4516: 		    [lindex $ans 0]
                   4517: 		if { ($length == 2) || ($length == 4)} {
                   4518: 		    lappend gAnalyze($gAnalyze(problemNum).unit) \
                   4519: 			[lindex $ans end]
                   4520: 		} 
                   4521: 		if { ($length == 3) || ($length == 4) } {
                   4522: 		    lappend gAnalyze($gAnalyze(problemNum).low) \
                   4523: 			[lindex $ans 1]
                   4524: 		    lappend gAnalyze($gAnalyze(problemNum).high) \
                   4525: 			[lindex $ans 2]
                   4526: 		}
                   4527: 	    }
                   4528: 	    SET { set gAnalyze(problemNum) 0 }
                   4529: 	    DONE { 
                   4530: 		set gAnalyze(maxprob) $gAnalyze(problemNum)
                   4531: 		$gAnalyze(scale) configure -to $gAnalyze(maxprob)
                   4532: 	    }
                   4533: 	    ERROR {
                   4534:  		fileevent $fileId readable ""
                   4535: 		displayError "Answers returned invalid message: $aline" 
                   4536: 		fileevent $fileId readable "analyzeLine $fileId"
                   4537: 	    }
                   4538: 	    BQES { fileevent $fileId readable "analyzeEatQuestion $fileId" }
                   4539: 	    default {
                   4540: 	    }
                   4541: 	}
                   4542:     }
                   4543:     if { [eof $fileId] } { analyzeEnd $fileId }
                   4544: }
                   4545: 
                   4546: ###########################################################
                   4547: # analyzeEnd
                   4548: ###########################################################
                   4549: ###########################################################
                   4550: ###########################################################
                   4551: proc analyzeEnd { fileId } {
                   4552:     global gAnalyze
                   4553:     if { [eof $fileId] } {
                   4554: 	fileevent $fileId readable ""
                   4555: 	catch {close $fileId}
                   4556: 	if { $gAnalyze(stop) } { return }
                   4557: 	if { [llength $gAnalyze(toprocess)] > 0 } { 
                   4558: 	    analyzeClass 0 
                   4559: 	} else {
                   4560: 	    analyzeUpdate
                   4561: 	    set gAnalyze(status) "Done"
                   4562: 	}
                   4563: 	if { !$gAnalyze(update) } {
                   4564: 	    incr gAnalyze(update) 
                   4565: 	    analyzeUpdate
                   4566: 	} else {
                   4567:  	    incr gAnalyze(update) 
                   4568: 	    if { $gAnalyze(update) == 10 } {
                   4569: 		set gAnalyze(update) 0
                   4570: 	    }
                   4571: 	}
                   4572:     }
                   4573: }
                   4574: 
                   4575: ###########################################################
                   4576: # analyzeRandom
                   4577: ###########################################################
                   4578: ###########################################################
                   4579: ###########################################################
                   4580: proc analyzeRandom { } {
                   4581:     global gAnalyze
                   4582:     set numToRun [getString . "How many random students should be run?"]
                   4583:     if { $numToRun == "" } { return }
                   4584:     if { [catch {incr numToRun} ] } {
                   4585: 	displayMessage "Invalid number."
                   4586:     }
                   4587:     incr numToRun -1
                   4588:     set gAnalyze(total) $numToRun
                   4589:     catch {unset gAnalyze(toprocess)}
                   4590:     for { set i 0 } { $i < $numToRun } { incr i } {
                   4591: 	append gAnalyze(toprocess) "[format "%09d" $i] Random 999 "
                   4592:     }
                   4593:     set gAnalyze(stop) 0
                   4594:     set gAnalyze(update) 1
                   4595:     set gAnalyze(done) 0
                   4596:     foreach name [array names gAnalyze *.\[alhu\]*] { unset gAnalyze($name) }    
                   4597:     analyzeClass 0
                   4598: }
                   4599: 
                   4600: ###########################################################
1.4       albertel 4601: # analyzeStrings
                   4602: ###########################################################
                   4603: ###########################################################
                   4604: ###########################################################
                   4605: proc analyzeStrings { prob window create} {
                   4606:     global gAnalyze
                   4607: 
                   4608:     if { ![winfo exists $window.analyzestrings] } { if {!$create} { return } }
                   4609:     if { ![catch {set setWin [toplevel $window.analyzestrings]}] } {
                   4610: 	set msgFrame [frame $setWin.msgFrame]
                   4611: 	set valFrame [frame $setWin.valFrame]
                   4612: 	set buttonFrame [frame $setWin.buttonFrame]
                   4613: 	pack $msgFrame $valFrame $buttonFrame
                   4614: 	pack configure $valFrame -expand 1 -fill both
                   4615: 	
                   4616: 	message $msgFrame.msg -text "Correct Answers" -aspect 3000
                   4617: 	pack $msgFrame.msg
                   4618: 	
                   4619: 	set maxWidth 1
                   4620: 	foreach choice $gAnalyze($prob.ans) {
                   4621: 	    if {[string length $choice]>$maxWidth} {set maxWidth [string length $choice]}
                   4622: 	}
                   4623: 	set maxStringWidth $maxWidth
                   4624: 	incr maxWidth 6
                   4625: 
                   4626: 	set selectMode none
                   4627: 	listbox $valFrame.val -width [expr $maxWidth + 2] \
                   4628: 	    -yscrollcommand "$valFrame.scroll set" -selectmode $selectMode
                   4629: 	scrollbar $valFrame.scroll -command "$valFrame.val yview"
                   4630: 	pack $valFrame.val $valFrame.scroll -side left
                   4631: 	pack configure $valFrame.val -expand 1 -fill both 
                   4632: 	pack configure $valFrame.scroll -expand 0 -fill y
                   4633: 	button $buttonFrame.cancel -text "Dismiss" -command "destroy $setWin"
                   4634: 	pack $buttonFrame.cancel
                   4635:     } else {
                   4636: 	set maxWidth 1
                   4637: 	set valFrame $window.analyzestrings.valFrame
                   4638: 	$valFrame.val delete 0 end
                   4639: 	foreach choice $gAnalyze($prob.ans) {
                   4640: 	    if {[string length $choice]>$maxWidth} {set maxWidth [string length $choice]}
                   4641: 	}
                   4642: 	set maxStringWidth $maxWidth
                   4643: 	incr maxWidth 6
                   4644:     }
                   4645:     set lastchoice [lindex $gAnalyze($gAnalyze(prob).ans) 0]
                   4646:     set num 1
                   4647:     foreach choice [lsort $gAnalyze($gAnalyze(prob).ans)] {    
                   4648: 	if { $lastchoice != $choice } {
                   4649: 	    $valFrame.val insert end \
                   4650: 		"[format %-[set maxStringWidth]s $lastchoice] [format %5d $num]"
                   4651: 	    set lastchoice $choice
                   4652: 	    set num 1
                   4653: 	} else {
                   4654: 	    incr num
                   4655: 	}
                   4656:     }	
                   4657:     $valFrame.val insert end \
                   4658: 	"[format %-[set maxStringWidth]s $lastchoice] [format %5d $num]"
                   4659: }
                   4660: 
                   4661: ###########################################################
1.1       albertel 4662: # analyzeUpdate
                   4663: ###########################################################
                   4664: ###########################################################
                   4665: ###########################################################
                   4666: proc analyzeUpdate { {newProbNumber 0} } {
                   4667:     global gAnalyze
                   4668: 
                   4669:     if {[catch {set gAnalyze($gAnalyze(prob).ans)}]} { return }
                   4670:     foreach problem [array names gAnalyze *.\[alh\]*] {
                   4671: 	if { [catch {set gAnalyze($problem) [lsort -real $gAnalyze($problem)]}]} {
                   4672: 	    set gAnalyze($problem) [lsort $gAnalyze($problem)]
                   4673: 	}
                   4674:     }
                   4675: 
                   4676:     set c $gAnalyze(canvas)
                   4677:     $c delete all
                   4678:     set gAnalyze(lownum) [set low [lindex $gAnalyze($gAnalyze(prob).ans) 0]]
                   4679:     set gAnalyze(highnum) [set high [lindex $gAnalyze($gAnalyze(prob).ans) end]]
                   4680:     set gAnalyze(numuniq) [llength [lunique $gAnalyze($gAnalyze(prob).ans)]]
                   4681: #don't draw anything if the answers aren't numbers
1.4       albertel 4682:     if { [catch {expr $low + 1}]} { 
                   4683: 	catch {destroy $c.button}
                   4684: 	update idletask
                   4685: 	button $c.button -text "List of strings" -command \
                   4686: 	    "analyzeStrings $gAnalyze(prob) $c 1"
                   4687: 	$c create window [expr $gAnalyze(canvaswidth)/2.0] 40 -window $c.button
                   4688: 	analyzeStrings $gAnalyze(prob) $c 0
                   4689: 	return 
                   4690:     }
1.1       albertel 4691: 
                   4692:     $c create line 25 50 [expr $gAnalyze(canvaswidth) - 25] 50
                   4693:     set diff [expr double($high-$low)]
                   4694:     if { $diff == 0 } {
                   4695: 	set center [expr $gAnalyze(canvaswidth)/2.0]
1.4       albertel 4696: 	$c create rectangle [expr $center - 2] 48 [expr $center + 2] 52 -fill green
1.1       albertel 4697: 	update idletasks
                   4698: 	return
                   4699:     }
                   4700:     set delta [format "%1.e" [expr ($diff)/15.0]]
                   4701:     set start [expr double(int($low/$delta)+1)*$delta]
                   4702:     while { $start < $high } {
                   4703: 	set center [expr ($gAnalyze(canvaswidth)-50)*(($start-$low)/$diff)]
                   4704: 	set center [expr $center+25]
                   4705: 	$c create line $center 40 $center 60
                   4706: 	set start [expr $start + $delta]
                   4707:     }
                   4708:     if { ($low < 0) && ($high > 0) } {
                   4709: 	set center [expr ($gAnalyze(canvaswidth)-50)*((0-$low)/$diff)]
                   4710: 	set center [expr $center+25]
                   4711: 	$c create rectangle [expr $center - 1] 40 [expr $center + 1] 60
                   4712:     }
1.3       albertel 4713:     set lastpoint [lindex $gAnalyze($gAnalyze(prob).ans) 0]
                   4714:     set num 0
1.1       albertel 4715:     foreach point $gAnalyze($gAnalyze(prob).ans) {    
1.3       albertel 4716: 	if { $lastpoint != $point } {
                   4717: 	    set center [expr ($gAnalyze(canvaswidth)-50)*(($lastpoint-$low)/$diff)]
                   4718: 	    set center [expr $center+25]
1.4       albertel 4719: 	    $c create rectangle [expr $center - 2] [expr 48-$num] \
                   4720: 		[expr $center + 2] [expr 52+$num] -fill green
1.3       albertel 4721: 	    set lastpoint $point
                   4722: 	    set num 0
                   4723: 	} else {
                   4724: 	    incr num
                   4725: 	}
1.1       albertel 4726:     }	
1.4       albertel 4727:     set center [expr ($gAnalyze(canvaswidth)-50)*(($lastpoint-$low)/$diff)]
                   4728:     set center [expr $center+25]
                   4729:     $c create rectangle [expr $center - 2] [expr 48-$num] \
                   4730: 	[expr $center + 2] [expr 52+$num] -fill green
1.1       albertel 4731:     
                   4732:     update idletasks
                   4733: }
                   4734: 
                   4735: ###########################################################
                   4736: # analyzeStop
                   4737: ###########################################################
                   4738: ###########################################################
                   4739: ###########################################################
                   4740: proc analyzeStop {} {
                   4741:     global gAnalyze
                   4742:     set gAnalyze(stop) 1
                   4743:     set gAnalyze(status) "Stopped"
1.9       albertel 4744:     catch {exec kill -SIGKILL $gAnalyze(pid)}
1.1       albertel 4745: }
                   4746: 
                   4747: ###########################################################
                   4748: # analyzeClose
                   4749: ###########################################################
                   4750: ###########################################################
                   4751: ###########################################################
                   4752: proc analyzeClose { } {
                   4753:     global gAnalyze
                   4754:     destroy [winfo toplevel $gAnalyze(canvas)]
                   4755:     unset gAnalyze
                   4756:     global gAnalyze
                   4757:     set gAnalyze(exit) 1
                   4758: }
                   4759: 
                   4760: ###########################################################
                   4761: # checkIfValidFilename 
                   4762: ###########################################################
                   4763: ###########################################################
                   4764: ###########################################################
                   4765: proc checkIfValidFilename {} {
                   4766:     global gSetNumberText gPreviewButton
                   4767:     if { [regexp \[^0-9\]+ $gSetNumberText] } {
                   4768: 	displayError "This file is not properly named. \n\nA main assignment file must be named setX.qz, where  X is replaced by a number between 1 and 99 inclusive. \n\nPlease do a \"Save .qz As\". \n\nUntil the file is properly named you will not be able to Preview, Create .dvi, or Print."
                   4769: 	$gPreviewButton configure -state disabled
                   4770: 	.main entryconfigure 5 -state disabled
                   4771: 	.main entryconfigure 7 -state disabled
                   4772: 	return 0
                   4773:     }
                   4774:     $gPreviewButton configure -state normal
                   4775:     .main entryconfigure 5 -state normal
                   4776:     .main entryconfigure 7 -state normal
                   4777:     return 1
                   4778: }
                   4779: 
                   4780: ###########################################################
                   4781: # updateChangeStatus
                   4782: ###########################################################
                   4783: ###########################################################
                   4784: ###########################################################
                   4785: proc updateChangeStatus { name1 name2 op } {
                   4786:     global gChanged gRefChanged gRefText gChangedLast gRefChangedLast \
                   4787: 	gEditWindow gWindowMenu gRefFile
                   4788:     if { $name1 == "gChanged" } {
                   4789: 	if { $gChanged != $gChangedLast } {
                   4790: 	    set gChangedLast $gChanged
                   4791: 	    global gFile
                   4792: 	    if { [catch {set gEditWindow}] } { return }
                   4793: 	    if { ![winfo exists $gEditWindow] } { return }
                   4794: 	    if { $gChanged } {
                   4795: 		catch {removeWindowEntry "$gFile*"}
                   4796: 		wm title [winfo toplevel $gEditWindow] "$gFile (Modified)"
                   4797: 		$gWindowMenu add command -label "$gFile (Modified)" -command \
                   4798: 		    "capaRaise $gEditWindow"
                   4799: 	    } else {
                   4800: 		catch {removeWindowEntry "$gFile*"}
                   4801: 		wm title [winfo toplevel $gEditWindow] "$gFile"
                   4802: 		$gWindowMenu add command -label "$gFile" -command \
                   4803: 		    "capaRaise $gEditWindow"
                   4804: 	    }
                   4805: 	}
                   4806:     } else {
                   4807: 	if { $gRefChanged($name2) != $gRefChangedLast($name2) } {
                   4808: 	    if { [catch {set gRefText($name2)}] } { return }
                   4809: 	    if { ![winfo exists $gRefText($name2)] } { return }
                   4810: 	    if { $gRefChanged($name2) } {
                   4811: 		catch {removeWindowEntry  "Reference $gRefFile($name2)*" }
                   4812: 	       wm title [winfo toplevel $gRefText($name2)] "$gRefFile($name2) (Modified)"
                   4813: 		$gWindowMenu add command -label "Reference $gRefFile($name2) (Modified)" \
                   4814: 		    -command "capaRaise [winfo toplevel $gRefText($name2)]"
                   4815: 	    } else {
                   4816: 		catch {removeWindowEntry  "Reference $gRefFile($name2)*" }
                   4817: 		wm title [winfo toplevel $gRefText($name2)] "$gRefFile($name2)"
                   4818: 		$gWindowMenu add command -label "Reference $gRefFile($name2)" \
                   4819: 		    -command "capaRaise [winfo toplevel $gRefText($name2)]"
                   4820: 	    }
                   4821: 	}
                   4822:     }
                   4823: }
                   4824: ###########################################################
                   4825: # main
                   4826: ###########################################################
                   4827: # sets up the auto_path variable, some globals and adds some
                   4828: # options then calls createControlWindow to give the user something
                   4829: # to do
                   4830: ###########################################################
                   4831: # Arguments: None
                   4832: # Returns: Nothing
                   4833: # Globals:
                   4834: ###########################################################
                   4835: 
                   4836: source quizzer.templates.tcl
                   4837: 
                   4838: if { [lindex $auto_path 0] == "./lib/tcl7.5" } {
                   4839:     set auto_path ""
                   4840:     lappend auto_path [pwd]/lib/tcl7.5
                   4841:     lappend auto_path [pwd]/lib/tk4.1
                   4842: }
                   4843: 
                   4844: lappend auto_path /usr/local/lib/CAPA45/Quizzer
                   4845: lappend auto_path [pwd]
                   4846: 
                   4847: set font 8x13bold
                   4848: catch {
                   4849:     if { $argc > 0 } {
                   4850: 	switch -glob -- [lindex $argv 0] {
                   4851: 	    {-[Ll]*} { set font 9x15bold }
                   4852: 	    {-[Mm]*} -
                   4853: 	    {-[Nn]*} { set font 8x13bold }
                   4854: 	    {-[Ss]*} { set font fixed }
                   4855: 	}
                   4856:     }
                   4857: }
                   4858: option add *font $font
                   4859: createControlWindow

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>