File:  [LON-CAPA] / capa / capa51 / GUITools / quizzer.tcl
Revision 1.15: download - view: text, annotated - select for diffs
Tue Feb 8 22:18:11 2000 UTC (24 years, 6 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
-Some quick updates
-group email coming along

    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: ###########################################################
  644: # sticks the date information from gControlDates into the date listbox
  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"
  669:     } else { 
  670: 	if { [llength $gControlDates] > 2 } {
  671: 	    set gControlDates [linsert [lreverse [lrange $gControlDates 1 end]] 0 [lindex $gControlDates 0]] 
  672: 	} 
  673:     }
  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: ###########################################################
  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: ###########################################################
  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
 1000:     set gDates(viewbetween) 0
 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 \
 1214: 	    gSetNumberText  gHeaderQCount gEnableDiscussion gFile
 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]
 1234:     label $dialog.message -text "Later entries will override earlier entries"
 1235:     set optionFrame [frame $dialog.options]
 1236:     set buttonFrame [frame $dialog.buttons -bd 10]
 1237:     pack $dialog.msg $loadFrame $dialog.message $infoFrame $optionFrame \
 1238: 	$buttonFrame -side top -fill x
 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
 1252:     pack configure $listFrame.scroll -fill y
 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"
 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
 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: 
 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: 
 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 } {
 1309: 	updateDiscussion
 1310: 	if { [llength $gControlDates] > 2 } {
 1311: 	    set gControlDates [linsert [lreverse [lrange $gControlDates 1 end]] 0 [lindex $gControlDates 0]] 
 1312: 	} 
 1313: 	eval updateHeader [ eval concat $gControlDates ]
 1314:     }
 1315:     
 1316:     return    
 1317: 
 1318: }
 1319: 
 1320: ###########################################################
 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: ###########################################################
 1349: # allFieldsComplete2 
 1350: ###########################################################
 1351: ###########################################################
 1352: ###########################################################
 1353: proc allFieldsComplete2 {} {
 1354:     global gLoadHeaderSet gControlDates 
 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 }
 1641:     displayMessage "Replaced $num occurrence$s"
 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]
 1861:     $gWindowMenu add command -label "Prefernces" -command "capaRaise $prefs"
 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
 1905:                                                 removeWindowEntry Prefernces"
 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}]
 2032:     catch { 
 2033: 	if { [llength $gControlDates] > 2 } {
 2034: 	    set gControlDates [linsert [lreverse [lrange $gControlDates 1 end]] 0 [lindex $gControlDates 0]] 
 2035: 	} 
 2036:     }
 2037:     if { $error == 1 } {
 2038: 	set gHeaderQCount "0"
 2039: 	set gControlDates ""
 2040: 	displayError "The db file for this set does not yet exist."
 2041:     } else {
 2042: 	set errortext ""
 2043: 	if { ( $numberParsed != $gHeaderQCount ) } {
 2044: 	    set error 1
 2045: 	    append errortext "Number of questions ($numberParsed) is different from the number in setX.db ($gHeaderQCount). "    
 2046: 	} 
 2047: 	if { $gWeightsDiffer } {
 2048: 	    set error 1
 2049: 	    append errortext "The problem weights specified in the QZ file are different from the ones in the DB file. "
 2050: 	} 
 2051: 	if { $gPartialDiffer } {
 2052: 	    set error 1
 2053: 	    append errortext "Whether or not a problem is hand graded as specified in the QZ file is different from the DB file. "
 2054: 	}
 2055: 	if { $error } {
 2056: 	    displayError "The curent DB Header does not match what the set file says it should be: $errortext. Set the DB Header!" red
 2057: 	}
 2058:     }
 2059:     return $error
 2060: }
 2061: 
 2062: ###########################################################
 2063: # fillInStudentName
 2064: ###########################################################
 2065: ###########################################################
 2066: ###########################################################
 2067: #proc notherefillInStudentName { v } {
 2068: #    global $v
 2069: 
 2070: #    set student [capaGetStudent [set ${v}(studentNumber)]]
 2071: #    set ${v}(studentName) [lindex $student [expr [llength $student] - 1] ]
 2072: #}
 2073: 
 2074: ###########################################################
 2075: # studentSelectWindow
 2076: ###########################################################
 2077: ###########################################################
 2078: ###########################################################
 2079: proc studentSelectWindow { followupCommand } {
 2080:     global gStudentSelection gChanged gWindowMenu gEditWindow
 2081:     if { [catch {set gEditWindow}] } { return }
 2082:     if { ![winfo exists $gEditWindow] } { return }
 2083:     if { $gChanged } { if { [askToSave 0 0] == "Cancel" } { return } }
 2084: 
 2085:     if { [winfo exists .studentSelect] } { 
 2086: 	capaRaise .studentSelect
 2087: 	return
 2088:     }
 2089:     set student [toplevel .studentSelect]
 2090:     $gWindowMenu add command -label "SelectStudent" \
 2091: 	    -command "capaRaise $student"
 2092:     wm title $student "Select Student"
 2093: 
 2094:     message $student.msg -text "Please specify a student to preview" \
 2095: 	    -aspect 10000
 2096:     set infoFrame [frame $student.frame -relief groove -borderwidth 4]
 2097:     set buttonFrame [frame $student.buttonFrame ]
 2098:     pack $student.msg $infoFrame $buttonFrame -side top 
 2099: 
 2100:     button $buttonFrame.ok -text "Preview" -command \
 2101: 	"selectStudentPreview $student $followupCommand"
 2102:     button $buttonFrame.cancel -text "Cancel" -command \
 2103: 	    "destroy $student
 2104:              trace vdelete gStudentSelection(studentNumber) w \"global gStudentSelection; set gStudentSelection(type) Specific ;#\"
 2105:              trace vdelete gStudentSelection(studentName) w \"global gStudentSelection; set gStudentSelection(type) Specific ;#\"
 2106:              removeWindowEntry SelectStudent"
 2107:     bind $student <Destroy> \
 2108: 	    "trace vdelete gStudentSelection(studentNumber) w \"global gStudentSelection; set gStudentSelection(type) Specific ;#\"
 2109:              trace vdelete gStudentSelection(studentName) w \"global gStudentSelection; set gStudentSelection(type) Specific ;#\"
 2110:              removeWindowEntry SelectStudent"
 2111:     pack $buttonFrame.ok $buttonFrame.cancel -side left
 2112: 
 2113:     set randomAnyFrame [frame $infoFrame.randomany]
 2114:     set randomFrame [frame $infoFrame.random]
 2115:     set specificFrame [frame $infoFrame.specific]
 2116:     set sectionFrame [frame $infoFrame.section]
 2117: #    pack $randomAnyFrame  $randomFrame $specificFrame $sectionFrame -side top
 2118:     pack $randomFrame $specificFrame -side top
 2119:     pack configure $specificFrame -expand true -fill both
 2120: 
 2121:     radiobutton $randomAnyFrame.random -text "Randomly select a student" \
 2122: 	-value "RandomAny" -variable gStudentSelection(type) 
 2123:     pack $randomAnyFrame.random
 2124: 
 2125:     radiobutton $randomFrame.random -text "Randomly select one student \
 2126: 	    from section:" -value "Random" -variable gStudentSelection(type) 
 2127:     entry $randomFrame.entry -textvariable gStudentSelection(random)  -width 3
 2128:     pack $randomFrame.random $randomFrame.entry -side left 
 2129: 
 2130:     radiobutton $specificFrame.specific -text "Specify the student by:" \
 2131: 	    -value "Specific" -variable gStudentSelection(type) 
 2132:     set studentNumber [frame $specificFrame.studentNumber]
 2133:     set fullName [frame $specificFrame.fullName]
 2134:     pack $specificFrame.specific $studentNumber $fullName -side top
 2135:     pack configure $specificFrame.specific -anchor w
 2136:     pack configure $studentNumber $fullName -anchor e
 2137: 
 2138:     radiobutton $sectionFrame.section 
 2139:     message $studentNumber.msg -text "Student Number: "  -aspect 10000
 2140:     entry $studentNumber.entry -textvariable gStudentSelection(studentNumber) \
 2141: 	    -width 9 -validate key -validatecommand "limitEntry %W 9 any %P"
 2142:     pack $studentNumber.msg $studentNumber.entry -side left
 2143: 
 2144:     message $fullName.msg -text "Student Name: "  -aspect 10000 
 2145:     entry $fullName.msg2 -textvariable gStudentSelection(studentName) -width 35 \
 2146: 	-validate key -validatecommand "limitEntry %W 35 any %P"
 2147:     pack $fullName.msg $fullName.msg2 -side left
 2148:     
 2149:     trace variable gStudentSelection(studentNumber) w \
 2150: 	"global gStudentSelection; set gStudentSelection(type) Specific ;#"
 2151:     trace variable gStudentSelection(studentName) w \
 2152: 	"global gStudentSelection; set gStudentSelection(type) Specific ;#"
 2153: 
 2154:     bind $studentNumber.entry <KeyPress-Return> \
 2155: 	"fillInStudent gStudentSelection(studentName) gStudentSelection(studentNumber) 0"
 2156:     bind $fullName.msg2 <KeyPress-Return> \
 2157: 	"fillInStudent gStudentSelection(studentName) gStudentSelection(studentNumber) 1"
 2158: #    $specificFrame.specific configure -command \
 2159: 	"$studentNumber.entry configure -state normal"
 2160: #    $randomFrame.random configure -command \
 2161: 	"$studentNumber.entry configure -state disabled"
 2162: 
 2163:     Centre_Dialog $student default
 2164: }
 2165: 
 2166: ###########################################################
 2167: # selectStudentPreview
 2168: ###########################################################
 2169: ###########################################################
 2170: ###########################################################
 2171: proc selectStudentPreview { student followupCommand} {
 2172:     global gStudentSelection
 2173:     destroy $student
 2174:     if { $gStudentSelection(type) == "Specific" } { 
 2175: 	if {$gStudentSelection(studentNumber) == ""} {
 2176: 	    fillInStudent gStudentSelection(studentName) \
 2177: 		gStudentSelection(studentNumber) 1
 2178: 	} else {
 2179: 	    fillInStudent gStudentSelection(studentName) \
 2180: 		gStudentSelection(studentNumber) 0
 2181: 	}
 2182: 	if {$gStudentSelection(studentNumber) == ""} { return }
 2183:     }
 2184:     removeWindowEntry SelectStudent
 2185:     $followupCommand
 2186: }
 2187: 
 2188: ###########################################################
 2189: # createPreviewWindow
 2190: ###########################################################
 2191: ###########################################################
 2192: ###########################################################
 2193: proc createPreviewWindow {} {
 2194:     global gPreviewMode gPreviewText gPrefs gSetNumberText gStudentSelection
 2195:     global gWindowMenu gNumberParsedText    gNumber    
 2196:     global gLoadHeaderSet gHeaderQCount gControlDates
 2197:     
 2198:     if { ![winfo exists .preview] } {
 2199: 	
 2200: 	set previewWindow [toplevel .preview]
 2201:         $gWindowMenu add command -label "Preview" -command \
 2202: 		"capaRaise $previewWindow"
 2203: 	wm title $previewWindow "Preview"
 2204: 	addFindList -1
 2205: 
 2206: 	set windowFrame [frame $previewWindow.windowFrame]
 2207: 	set buttonFrame [frame $previewWindow.buttonFrame]
 2208: 	
 2209: 	pack $windowFrame $buttonFrame -side bottom 
 2210: 	pack configure $windowFrame -expand true -fill both
 2211: 	pack configure $buttonFrame -anchor e
 2212: 
 2213: 	scrollbar $windowFrame.scroll -orient vertical -command \
 2214: 		"$windowFrame.text yview"
 2215: 	set gPreviewText [text $windowFrame.text -yscrollcommand \
 2216: 		"$windowFrame.scroll set" -wrap char -height 40]
 2217: 	
 2218: 	pack $windowFrame.scroll $gPreviewText -side left -expand 0
 2219: 	pack configure $windowFrame.scroll -expand 0 -fill y
 2220: 	pack configure $gPreviewText -expand true -fill both
 2221: 	
 2222: 	button $buttonFrame.ok -text Dismiss -command "destroy $previewWindow
 2223: 	                                          removeWindowEntry Preview
 2224:                                                   removeFindList -1"
 2225: 	bind $previewWindow <Destroy> "removeWindowEntry Preview
 2226:                                        removeFindList -1"
 2227: 	button $buttonFrame.save -text "Save Output" -command "saveText $gPreviewText"
 2228: 	button $buttonFrame.stop -text "Stop Parser" -command "stopParser"
 2229: 	button $buttonFrame.print -text "Print Output" -command "printText $gPreviewText"
 2230: 	pack $buttonFrame.print $buttonFrame.save $buttonFrame.stop \
 2231: 	    $buttonFrame.ok -side left
 2232: 	
 2233: 	Centre_Dialog $previewWindow default
 2234: 	wm withdraw $previewWindow
 2235: 	update idletasks
 2236: 	set win_width [winfo reqwidth $previewWindow]
 2237: 	set win_height [winfo reqheight $previewWindow]
 2238: 	wm geometry $previewWindow +[expr [winfo rootx $previewWindow] - \
 2239: 		100]+[winfo rooty $previewWindow]
 2240: 	wm deiconify $previewWindow
 2241: 	update
 2242:     } else {
 2243: 	set previewWindow .preview
 2244: 	$gPreviewText delete 0.0 end
 2245: 	update
 2246:     }
 2247: 
 2248:     switch $gPrefs(info) {
 2249: 	Problem { set type 0 }
 2250: 	ProblemAnswer { set type 1 }
 2251: 	Answer { set type 2 }
 2252:     }
 2253:     grab .preview
 2254:     if { [catch {
 2255: 	switch $gPreviewMode {
 2256: 	    Enscript
 2257: 	    {
 2258: 		set numberParsed [enscriptParse $type $gSetNumberText \
 2259: 				  $gStudentSelection(type) $gStudentSelection(random) \
 2260: 				  $gStudentSelection(studentNumber) \
 2261: 				  $gStudentSelection(studentName) gPreviewText]
 2262: 	    }
 2263: 	    TeX
 2264: 	    {
 2265: 		set numberParsed [texParse $type $gSetNumberText \
 2266: 				  $gStudentSelection(type) $gStudentSelection(random) \
 2267: 				  $gStudentSelection(studentNumber) \
 2268: 				  $gStudentSelection(studentName) gPreviewText]
 2269: 	    }
 2270: 	    Web
 2271: 	    {
 2272: 		set numberParsed [webParse $type $gSetNumberText \
 2273: 				  $gStudentSelection(type) $gStudentSelection(random) \
 2274: 				  $gStudentSelection(studentNumber) \
 2275: 				  $gStudentSelection(studentName) gPreviewText]
 2276: 	    }
 2277: 	} }]} { return }
 2278:     grab release .preview
 2279:     if { $numberParsed == -1 } {
 2280: 	destroy $previewWindow
 2281: 	removeWindowEntry Preview
 2282: 	return
 2283:     }
 2284:     checkHeader $numberParsed
 2285:     $gPreviewText tag configure problem 
 2286:     $gPreviewText tag configure answer 
 2287:     capaRaise $previewWindow
 2288: 
 2289:     set gNumberParsedText $numberParsed
 2290:     showParseErrors 
 2291: }
 2292: 
 2293: ###########################################################
 2294: # openError
 2295: ###########################################################
 2296: ###########################################################
 2297: ###########################################################
 2298: proc openError { file line type} {
 2299:     global gRefLine gLineNumberGoto gTextWindow
 2300:     if { $type == 2 } {
 2301: 	set gLineNumberGoto $line
 2302: 	gotoLine
 2303: 	capaRaise [winfo toplevel $gTextWindow]
 2304:     } else {
 2305: 	if {[set num [openReferenceFile $file]]} {
 2306: 	    update idletasks
 2307: 	    set gRefLine($num) $line
 2308: 	    gotoRefLine $num
 2309: 	}
 2310:     }
 2311: }
 2312: 
 2313: ###########################################################
 2314: # showParseErrors
 2315: ###########################################################
 2316: ###########################################################
 2317: ###########################################################
 2318: proc showParseErrors {} {
 2319:     global gParseErrorsText gWindowMenu gUniqueNumber gCapaConfig
 2320: 
 2321:     set parseErrors [getParseErrors]
 2322:     
 2323:     if { $parseErrors != "" } {
 2324: 
 2325: 	if { ![winfo exists .parseErrors] } {
 2326: 	    
 2327: 	    set parseErrorsWindow [toplevel .parseErrors]
 2328: 	    $gWindowMenu add command -label "ParseErrors" -command "capaRaise \
 2329: 		    $parseErrorsWindow"
 2330: 	    wm title $parseErrorsWindow "Parse Errors"
 2331: 	    addFindList -2
 2332: 
 2333: 	    set windowFrame [frame $parseErrorsWindow.windowFrame]
 2334: 	    set buttonFrame [frame $parseErrorsWindow.buttonFrame]
 2335: 	    
 2336: 	    pack $windowFrame $buttonFrame -side bottom 
 2337: 	    pack configure $windowFrame -expand true -fill both
 2338: 	    pack configure $buttonFrame -anchor e
 2339: 
 2340: 	    scrollbar $windowFrame.scroll -orient vertical -command \
 2341: 		    "$windowFrame.text yview"
 2342: 	    set gParseErrorsText [text $windowFrame.text -yscrollcommand \
 2343: 		    "$windowFrame.scroll set" -wrap char -height 40]
 2344: 	    
 2345: 	    pack $windowFrame.scroll $gParseErrorsText -side left -expand 0
 2346: 	    pack configure $windowFrame.scroll -expand 0 -fill y
 2347: 	    pack configure $gParseErrorsText -expand true -fill both
 2348: 	    
 2349: 	    button $buttonFrame.ok -text Dismiss -command \
 2350: 		    "destroy $parseErrorsWindow
 2351: 	             removeWindowEntry ParseErrors
 2352:                      removeFindList -2"
 2353: 	    bind $parseErrorsWindow <Destroy> "removeWindowEntry ParseErrors
 2354:                                                removeFindList -2"
 2355: 	    button $buttonFrame.save -text "Save Output" \
 2356: 		    -command "saveText $gParseErrorsText"
 2357: 	    button $buttonFrame.print -text "Print Output" \
 2358: 		    -command "printText $gParseErrorsText"
 2359: 	    pack $buttonFrame.print $buttonFrame.save \
 2360: 		    $buttonFrame.ok -side left
 2361: 	
 2362: 	    Centre_Dialog $parseErrorsWindow default
 2363: 	    update
 2364: 	    capaRaise $parseErrorsWindow
 2365: 	} else {
 2366: 	    $gParseErrorsText delete 0.0 end
 2367: 	    capaRaise .parseErrors
 2368: 	}
 2369: 	foreach line [split $parseErrors "\n"] {
 2370: 	    set tag 0
 2371: 	    if { [regexp -indices {File:.+->(.+), Line ([0-9]+): ERROR:} $line result file linenum]} {
 2372: 		set tag 1
 2373: 	    } else {
 2374: 		if { [regexp -indices {File:(.+), Line ([0-9]+): ERROR:} $line result file linenum]} {
 2375: 		    set tag 2
 2376: 		}
 2377: 	    }
 2378: 	    if { $tag } {
 2379: 		set tagnum [incr gUniqueNumber]
 2380: 		set linenum [eval [list string range $line] $linenum]
 2381: 		set filename [eval [list string range $line] $file]
 2382: 		set i [expr [lindex [split [$gParseErrorsText index end] .] 0] - 1]
 2383: 	    }
 2384: 	    $gParseErrorsText insert end "$line\n"
 2385: 	    if { $tag } {
 2386: 		$gParseErrorsText tag add error.$tagnum $i.[lindex $file 0] $i.[expr [lindex $file 1] + 1]
 2387: 		$gParseErrorsText tag configure error.$tagnum -foreground $gCapaConfig(IMP_color)
 2388: 		$gParseErrorsText tag bind error.$tagnum <Double-ButtonPress> \
 2389: 		    "eval openError $filename $linenum $tag"
 2390: 	    }
 2391: 	}
 2392:     } else {
 2393: 	if { [winfo exists .parseErrors] } { $gParseErrorsText delete 0.0 end }
 2394:     }
 2395: 
 2396:     return $parseErrors
 2397: }
 2398: 
 2399: ###########################################################
 2400: # printText
 2401: ###########################################################
 2402: # prints the contents of the text window, creates a temp file named
 2403: # quiztemp.txt
 2404: ###########################################################
 2405: # Arguments: window (name of text window to print the contents of.
 2406: # Returns  : nothing
 2407: # Globals  :
 2408: ###########################################################
 2409: proc printText { window } {
 2410: 
 2411:     if { ![winfo exists $window] } {
 2412: 	return
 2413:     }
 2414:     set lprCommand [getLprCommand quiztemp.txt]
 2415: 
 2416:     if {$lprCommand == "Cancel"} { 
 2417: 	return
 2418:     }
 2419:   
 2420:     set fileId [open "quiztemp.txt" w]
 2421:     puts -nonewline $fileId "[ $window get 0.0 end ]"
 2422:     close $fileId
 2423: 
 2424:     set errorMsg ""
 2425:     set error [catch {set output [ eval "exec $lprCommand" ] } errorMsg ]
 2426:     
 2427:     if { $error == 1 } {
 2428:         displayError "An error occurred while printing: $errorMsg"
 2429:     } else {
 2430: 	displayMessage "Print job sent to the printer.\n $output"
 2431:     }
 2432:     exec rm -f quiztemp.txt
 2433: }
 2434: 
 2435: ###########################################################
 2436: # saveText
 2437: ###########################################################
 2438: # saves the contents of the text window
 2439: ###########################################################
 2440: # Arguments: window (name of text window to save the contents of.
 2441: #            saveAs (whether to ask to save)
 2442: #            refNum (if supplied reference file unique number
 2443: #                    that is being saved)
 2444: # Returns  : name of the file saved
 2445: # Globals  :
 2446: ###########################################################
 2447: proc saveText { window {saveAs 1} {refNum 0} } {
 2448:     global gWindowMenu gRefFile gRefChanged gCapaConfig gFile
 2449: 
 2450:     if { ![winfo exists $window] } {return}
 2451:     if { $refNum } {
 2452: 	if {$saveAs} { 
 2453: 	    set dir [file dirname $gRefFile($refNum)]
 2454: 	    set file ""
 2455: 	} else {
 2456: 	    set dir [file dirname $gRefFile($refNum)]
 2457: 	    set file [file tail $gRefFile($refNum)]
 2458: 	}
 2459:     } else { set dir [ set file "" ] }
 2460: #    if { $dir == "" || $dir == "."} { set dir [pwd] }
 2461:     if { $file == "" } {
 2462: 	set file [tk_getSaveFile -title "Enter the name to Save As" \
 2463: 		-initialdir "$dir" ]
 2464: 	if { $file == "" } {
 2465: 	    displayError "File not saved"
 2466: 	    return
 2467: 	}
 2468:     } else {
 2469: 	set file [file join $dir $file]
 2470:     }
 2471: 
 2472:     if { $refNum } { 
 2473: 	catch {removeWindowEntry  "Reference $gRefFile($refNum)*" }
 2474: 	catch {removeFindList $refNum}
 2475: 	set gRefFile($refNum) $file
 2476: 	addFindList $refNum
 2477: 	wm title [winfo toplevel $window] $file 
 2478: 	$gWindowMenu add command -label "Reference $file" \
 2479: 		-command "capaRaise [winfo toplevel $window]"
 2480: 	if { !$saveAs } {
 2481: 	    if { ([array name gCapaConfig quizzerBackupRef] == "") ||
 2482: 		 ($gCapaConfig(quizzerBackupRef)!="off") } {
 2483: 		if { [catch {file copy -force $file $file.bak} ] } {
 2484: 		    displayError "Unable to create backup for $file"
 2485: 		}
 2486: 	    }
 2487: 	}
 2488:     }
 2489:     set fileId [open $file w]
 2490:     puts -nonewline $fileId [$window get 0.0 end]
 2491:     close $fileId
 2492:     if { $refNum } { set gRefChanged($refNum) 0 }
 2493:     if { ([file tail $file] == "capa.config") && ($dir == [file dirname $gFile]) } {
 2494: 	if { [makeSure "Reread capa.config settings into Quizzer?"] != "Cancel" } {
 2495: 	    rereadCapaConfig
 2496: 	}
 2497:     }
 2498: 
 2499:     return $file
 2500: }
 2501: 
 2502: ###########################################################
 2503: # deleteFile
 2504: ###########################################################
 2505: ###########################################################
 2506: ###########################################################
 2507: proc deleteFile { which } {
 2508:     global gFile gRefFile
 2509:     if { $which } { set file $gRefFile($which) } else { set file $gFile }
 2510:     if { [makeSure "Do you wish to Delete $file"] == "Cancel" } { return } 
 2511:     if { $which } { closeRefFile $which 1 0 } else { closeDocument 1 0 }
 2512:     file delete -- $file
 2513: }
 2514: 
 2515: ###########################################################
 2516: ###########################################################
 2517: ###########################################################
 2518: ###########################################################
 2519: proc rereadCapaConfig { } {
 2520:     global gCapaConfig
 2521:     set printer_option $gCapaConfig(printer_option)
 2522:     unset gCapaConfig(printer_option)
 2523:     set error [parseCapaConfig]
 2524:     if { $error != "OK" } { 
 2525: 	displayError "Invalid capa.config file" 
 2526: 	set gCapaConfig(printer_option) $printer_option
 2527:     } 
 2528:     setDefaultValues
 2529:     updateColors
 2530: }
 2531: 
 2532: ###########################################################
 2533: ###########################################################
 2534: ###########################################################
 2535: ###########################################################
 2536: proc pickCapaConfig { } {
 2537:     set error NOTOK
 2538:     while { $error != "OK" } {
 2539: 	set file [tk_getOpenFile -title "Pick a capa.config file" \
 2540: 		      -filetypes { { {CAPA configuration}  {capa.config} } \
 2541: 				       { {All Files} {*} } }]
 2542: 	if { $file == "" } { break }
 2543: 	set oldDir [pwd]
 2544: 	cd [file dirname $file]	
 2545: 	set error [parseCapaConfig]
 2546: 	if { $error != "OK" } { displayError "Invalid capa.config file"; cd $oldDir } 
 2547: 	setDefaultValues
 2548:     } 
 2549: }
 2550: 
 2551: proc setDefaultValues {} {
 2552:     global gProbVal gTryVal gHintVal gCapaConfig
 2553:     catch {set gProbVal $gCapaConfig(default_prob_val)}
 2554:     catch {set gTryVal $gCapaConfig(default_try_val)}
 2555:     catch {set gHintVal $gCapaConfig(default_hint_val)}
 2556: }
 2557: 
 2558: ###########################################################
 2559: # openDocument
 2560: ###########################################################
 2561: ###########################################################
 2562: ###########################################################
 2563: proc openDocument {} {
 2564:     global gFile gTextWindow gSetNumberText gPrefs gChanged gQuizTemp gUndo 
 2565: 
 2566:     if { $gChanged } { if { [askToSave 0 0] == "Cancel" } { return } }
 2567:     if { ![catch {set gTextWindow}] } { 
 2568: 	if { [winfo exists $gTextWindow] } { return }
 2569:     }
 2570: 
 2571:     # the event generation is because of a bug in tk_getOpenFile
 2572:     # After double cliking the mouse Button one is thought to be left down.
 2573:     # this only seems to happen when a toplevel window is created
 2574:     # after getting the file
 2575:     set gFile [tk_getOpenFile -filetypes \
 2576: 	    { { {Quizzer} {"*.qz"} } { {All Files} {"*"} } } \
 2577: 	    -title "Select the proper file" -initialdir "[pwd]" ]
 2578: #    event generate .main <ButtonRelease-1>
 2579:     if { $gFile == "" } { return }
 2580:     if { [file isdirectory $gFile] } {
 2581: 	displayError "You attempted to open $gFile which is a directory not a file."
 2582: 	return
 2583:     }
 2584: 
 2585:     set error [ catch {set fileId [open $gFile r] } ]
 2586: 
 2587:     if { $error } {
 2588: 	displayError "Unable to read $gFile"
 2589: 	return
 2590:     } 
 2591:     
 2592:     set oldDir [pwd]
 2593:     
 2594:     cd [file dirname $gFile]
 2595: 
 2596:     set tempfiles ""
 2597:     catch {set tempfiles [glob quiztemp.*]}
 2598:     if { $tempfiles == "" } { 
 2599: 	set gQuizTemp true 
 2600:     }     
 2601:     foreach quiztempFile $tempfiles {
 2602: 	if { ! ( [file isfile $quiztempFile] && 
 2603: 	[file writable $quiztempFile] ) } {
 2604: 	    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" } {
 2605:                 cd $oldDir
 2606:                 set gQuizTemp true
 2607:                 return
 2608: 	    } else {
 2609:                 set gQuizTemp false
 2610: 		break
 2611: 	    }
 2612: 	}
 2613:     }
 2614: 
 2615:     if { $tempfiles == "" } {
 2616: 	if { ! [file writable $gFile] } {
 2617: 	    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" } {
 2618: 		cd $oldDir
 2619:                 set gQuizTemp true
 2620:                 return
 2621: 	    } else {
 2622:                 set gQuizTemp false
 2623: 	    }
 2624: 	}
 2625:     }
 2626: 
 2627:     set error [parseCapaConfig]
 2628:     
 2629:     if { $error != "OK" } {
 2630: 	cd $oldDir
 2631: 	set gQuizTemp true
 2632:         return
 2633:     }
 2634:     setDefaultValues
 2635: 
 2636:     createEditingWindow 0
 2637:     $gTextWindow delete 0.0 end
 2638:     $gTextWindow insert 0.0 [read $fileId [file size $gFile]]
 2639:     $gTextWindow delete end-1c
 2640: 
 2641:     rename $gTextWindow .$gTextWindow
 2642:     trackChanges $gTextWindow 0
 2643:     set gUndo(0) 0
 2644:     set gUndo(0.cur) 0
 2645: 
 2646:     createImportLinks 0 0.0 end
 2647:     focus -force $gTextWindow 
 2648:     set coord [$gTextWindow bbox 0.0]
 2649:     event generate $gTextWindow <1> -x [lindex $coord 0] -y [lindex $coord 1]
 2650:     event generate $gTextWindow <ButtonRelease-1>
 2651:     update
 2652:     close $fileId
 2653: 
 2654:     updatePrefsWindow 
 2655: 
 2656:     set gSetNumberText [string range [file rootname [file tail $gFile]] 3 end ]
 2657:     checkIfValidFilename
 2658:     set gChanged 0
 2659: }
 2660: 
 2661: ###########################################################
 2662: # includeReferenceFile
 2663: ###########################################################
 2664: ###########################################################
 2665: ###########################################################
 2666: proc includeReferenceFile { file num window } {
 2667:     set index [$window index "file.$num.first linestart"]
 2668:     if { ![file readable $file] } {
 2669: 	displayError "Unable to read file $file"
 2670: 	return
 2671:     }
 2672:     set impline [$window get $index "$index lineend"]
 2673:     set fileId [open $file r]
 2674:     $window insert $index /
 2675:     update idletasks
 2676:     if { [$window index end] == [$window index "$index + 1 lines"] } {
 2677: 	$window insert "$index + 1 lines" "\n[read $fileId [file size $file]]"
 2678:     } else {
 2679: 	$window insert "$index + 1 lines" [read $fileId [file size $file]]
 2680:     }
 2681:     close $fileId
 2682: }
 2683: 
 2684: ###########################################################
 2685: # impMenuSaveAs
 2686: ###########################################################
 2687: ###########################################################
 2688: ###########################################################
 2689: proc impMenuSaveAs { file num window } {
 2690:     set index [$window index "file.$num.first linestart"]
 2691:     if { ![file readable $file] } {
 2692: 	displayError "Unable to read file $file"
 2693: 	return
 2694:     }
 2695:     set dir [file dirname $file]
 2696:     if { $dir != "." } {
 2697: 	set dest [tk_getSaveFile -title "Enter the name to Save As" -initialdir $dir]
 2698:     } else {
 2699: 	set dest [tk_getSaveFile -title "Enter the name to Save As" -initialdir [pwd]]
 2700:     }
 2701:     if { $dest == "" } {
 2702: 	displayError "File not saved"
 2703: 	return
 2704:     }
 2705:     if { [catch {file copy -force -- $file $dest} errorMsg ] } {
 2706: 	displayError "Unable to copy $file to $dest: $errorMsg"
 2707: 	return
 2708:     }
 2709:     $window delete $index "$index lineend"
 2710:     if { $dir != "." } {
 2711: 	$window insert $index "/IMP \"$dest\""
 2712:     } else {
 2713: 	$window insert $index "/IMP \"[file tail $dest]\""
 2714:     }
 2715: }
 2716: 
 2717: ###########################################################
 2718: # createImpMenu
 2719: ###########################################################
 2720: ###########################################################
 2721: ###########################################################
 2722: proc createImpMenu { file num window } {
 2723:     set menuFrame [menu .impmenu -tearoff 0 -type tearoff ]
 2724: 
 2725: #    wm title $menuFrame "Quizzer"
 2726:     wm overrideredirect $menuFrame 1
 2727:     wm positionfrom $menuFrame program
 2728:     $menuFrame post [winfo pointerx .] [winfo pointery .]
 2729:     $menuFrame add command -label "Open" -command "openReferenceFile $file"
 2730:     $menuFrame add command -label "Include" -command \
 2731: 	"includeReferenceFile $file $num $window"
 2732:     $menuFrame add command -label "saveAs" -command "impMenuSaveAs $file $num $window"
 2733:     grab $menuFrame
 2734:     bind all <ButtonRelease> "grab release $menuFrame;destroy $menuFrame"
 2735: }
 2736: 
 2737: ###########################################################
 2738: # registerCreateImportLinks
 2739: ###########################################################
 2740: ###########################################################
 2741: ###########################################################
 2742: proc registerCreateImportLinks { num start end} {
 2743:     global gDoCreateImportLinks gCreateImportLinks
 2744: 
 2745:     if { $gCreateImportLinks && !$gDoCreateImportLinks($num) } {
 2746: 	after idle "createImportLinks $num $start $end"
 2747: 	set gDoCreateImportLinks($num) 1
 2748:     }
 2749: }
 2750: 
 2751: ###########################################################
 2752: # createImportLinks
 2753: ###########################################################
 2754: ###########################################################
 2755: ###########################################################
 2756: proc createImportLinks { num start end } {
 2757:     global gTextWindow gUniqueNumber gDoCreateImportLinks gRefText gCapaConfig 
 2758:     
 2759:     set gDoCreateImportLinks($num) 0
 2760:     if { $num } { set window $gRefText($num) } else { set window $gTextWindow }
 2761:     if { ![winfo exists $window] } { return }
 2762:     set end [$window index $end]
 2763:     set start [$window index $start]
 2764:     set lastline [lindex [split $end .] 0]
 2765:     set startline [lindex [split $start .] 0]
 2766:     foreach tag [$window tag names] {
 2767: 	if { [regexp {file\..+} $tag ] } {
 2768: 	    if { [$window tag nextrange $tag "$start linestart" "$end lineend"] != "" } {
 2769: 		$window tag delete $tag
 2770: 	    }
 2771: 	}
 2772:     }
 2773:     for { set i $startline } { $i <= $lastline } { incr i } {
 2774: 	set aline [$window get $i.0 "$i.0 lineend"]
 2775: 	if { [regexp -nocase {(^[ 	]*)(//.*)} $aline matchVar match1 match2] } {
 2776: 	    set tagnum [incr gUniqueNumber]
 2777: 	    set start [string length $match1]
 2778: 	    set end [expr $start + [string length $match2]]
 2779: 	    $window tag add file.$tagnum $i.$start $i.$end
 2780: 	    $window tag configure file.$tagnum -foreground $gCapaConfig(comment_color)
 2781: 	} elseif { [regexp -nocase {(^.*/let.*[ 	]+)(//.*)} $aline matchVar \
 2782: 			match1 match2] } {
 2783: 	    set tagnum [incr gUniqueNumber]
 2784: 	    set start [string length $match1]
 2785: 	    set end [expr $start + [string length $match2]]
 2786: 	    $window tag add file.$tagnum $i.$start $i.$end
 2787: 	    $window tag configure file.$tagnum -foreground $gCapaConfig(comment_color)
 2788: 	} 
 2789: 	if { [regexp -nocase "(.*/imp +)(\"\[^\"\]+\")(.*)" $aline matchVar \
 2790: 		  match1 match2 match3] } {
 2791: 	    set tagnum [incr gUniqueNumber]
 2792: 	    set start [string length $match1]
 2793: 	    set end [expr $start + [string length $match2]]
 2794: 	    $window tag add file.$tagnum $i.$start $i.$end
 2795: 	    $window tag configure file.$tagnum -foreground $gCapaConfig(IMP_color)
 2796: 	    $window tag bind file.$tagnum <Double-ButtonPress> \
 2797: 		"eval openReferenceFile $match2"
 2798: 	    $window tag bind file.$tagnum <ButtonPress-3> \
 2799: 		"eval createImpMenu $match2 $tagnum $window"
 2800: 	} 
 2801:     }
 2802: }
 2803: 
 2804: ###########################################################
 2805: # isReferenceFileOpen
 2806: ###########################################################
 2807: ###########################################################
 2808: ###########################################################
 2809: proc isReferenceFileOpen { file } {
 2810:     global gWindowMenu
 2811: #    if { [catch {set index [$gWindowMenu index "Reference $file"]} ] } { return "" }
 2812:     set last [$gWindowMenu index end]
 2813: #    puts $last
 2814:     for { set index 1 } { $index <= $last } { incr index } {
 2815: #	puts $index
 2816: 	catch {set entry [$gWindowMenu entrycget $index -label]}
 2817: 	if { "Reference" == [set entryfile [lindex $entry 0]] } {
 2818: 	    set entryfile [lindex $entry 1]
 2819: 	}
 2820: #	puts $entryfile
 2821: 	if { [catch {file stat $entryfile a1}] } { continue }
 2822: 	file stat $file a2
 2823: #	puts "$a2(ino) == $a1(ino)"
 2824: 	if { $a2(ino) == $a1(ino) } {
 2825: #	    puts "seems right"
 2826: 	    return [lindex [$gWindowMenu entrycget $index -command] 1]
 2827: 	}
 2828:     }
 2829: #    puts "failed $index"
 2830:     return ""
 2831: #    puts $index
 2832: #    puts [$gWindowMenu entrycget $index -command]
 2833: #    return [lindex [$gWindowMenu entrycget $index -command] 1]
 2834: }
 2835: 
 2836: ###########################################################
 2837: # newReferenceFile
 2838: ###########################################################
 2839: ###########################################################
 2840: ###########################################################
 2841: proc newReferenceFile { } {
 2842:     global gDir
 2843: #    if { $gDir(reference) == "." } { set gDir(reference) [pwd] }
 2844:     set file [tk_getSaveFile -title "Enter the name of the New reference file" \
 2845: 		  -initialdir "$gDir(reference)" ]
 2846:     event generate .main <ButtonRelease-1>
 2847:     if { $file == "" } { return }
 2848:     set gDir(reference) [file dirname $file]
 2849:     if { [file isdirectory $file] } {
 2850: 	displayError "You attempted to create $file which is already a directory."
 2851: 	return
 2852:     }
 2853:     openReferenceFile $file 1
 2854: }
 2855: 
 2856: ###########################################################
 2857: # openReferenceFile
 2858: ###########################################################
 2859: ###########################################################
 2860: ###########################################################
 2861: proc openReferenceFile { {file ""} {new 0}} {
 2862:     global gUniqueNumber gWindowMenu gDir
 2863: 
 2864:     set num [incr gUniqueNumber]
 2865:     global gRefCurLine gRefLine gRefText gRefChanged gRefFile gRefClosed \
 2866: 	gUndo gRefChangedLast
 2867:     # the event generation is because of a bug in tk_getOpenFile
 2868:     # After double cliking the mouse Button one is thought to be left down.
 2869:     # this only seems to happen when a toplevel window is created
 2870:     # after getting the file
 2871:     if { $file == "" } {
 2872: #	if { $gDir(reference) == "." } { set gDir(reference) [pwd] }
 2873: 	set file [tk_getOpenFile -filetypes \
 2874: 		      { { {All Files} {"*"} } { {Quizzer} {"*.qz"} } } \
 2875: 		      -title "Select the proper file" \
 2876: 		      -initialdir "$gDir(reference)" ]
 2877: 	event generate .main <ButtonRelease-1>
 2878: 	if { $file == "" } { return 0 }
 2879: 	set gDir(reference) [file dirname $file]
 2880: 	
 2881: 	if { [file isdirectory $file] } {
 2882: 	    displayError "You attempted to open $file which is a directory not a file."
 2883: 	    return 0
 2884: 	}
 2885:     } else {
 2886: 	if { !$new } {
 2887: 	    if { [set window [isReferenceFileOpen $file] ] != "" } { 
 2888: 		set num [lindex [split [lindex [split $window .] 1] e] end]
 2889: 		capaRaise $window
 2890: 		return $num
 2891: 	    }
 2892: 	    # specifically opening the capa.config file
 2893: 	    if { $file == "capa.config" } {
 2894: 		global gTextWindow gFile
 2895: 		if { [catch {set gTextWindow}] } { 
 2896: 		    set file [tk_getOpenFile -filetypes \
 2897: 				  {{{Capa.config file} {"capa.config"}} 
 2898: 				      { {All Files} {"*"} } } \
 2899: 				  -title "Select the proper file" \
 2900: 				  -initialdir "$gDir(reference)" ]
 2901: 		    if { $file == "" } { return 0 }
 2902: 		} else {
 2903: 		    set file [file join [file dirname $gFile] capa.config]
 2904: 		}
 2905: 	    } else {
 2906: 		if { ![file isfile $file] && ![file readable $file] } {
 2907: 		    displayError "Unable to find $file"
 2908: 		    return 0
 2909: 		}
 2910: 		if { [file isdirectory $file] } {
 2911: 		    displayError "You attempted to open $file which is a directory not a file."
 2912: 		    return 0
 2913: 		}
 2914: 	    }
 2915: 	}
 2916:     }
 2917: 
 2918:     set gRefFile($num) $file
 2919:     set referenceFile [toplevel .reference$num]
 2920:     wm title $referenceFile "$file"
 2921: 
 2922:     $gWindowMenu add command -label "Reference $file" \
 2923: 	-command "capaRaise $referenceFile"
 2924: 
 2925:     set menuFrame [frame $referenceFile.menu -borderwidth 3 -relief raised]
 2926:     set lineFrame [frame $referenceFile.lineFrame]
 2927:     set windowFrame [frame $referenceFile.windowFrame]
 2928:     pack $menuFrame $lineFrame $windowFrame
 2929:     pack configure $windowFrame -expand 1 -fill both
 2930:     pack configure $menuFrame -fill x
 2931: 
 2932:     label $lineFrame.msg -text "Current Line:"
 2933:     label $lineFrame.current -textvariable gRefCurLine($num)
 2934:     entry $lineFrame.line -width 8 -textvariable gRefLine($num)
 2935:     bind $lineFrame.line <KeyPress-Return> "+gotoRefLine $num"
 2936:     button $lineFrame.button -text "Goto" -command \
 2937: 	    "gotoRefLine $num"
 2938:     pack $lineFrame.msg $lineFrame.current $lineFrame.line \
 2939: 	    $lineFrame.button -side left
 2940:     
 2941:     set infoFrame [frame $windowFrame.infoFrame]
 2942:     set textFrame [frame $windowFrame.textFrame]
 2943:     pack $infoFrame $textFrame -side top
 2944:     pack configure $textFrame -expand 1 -fill both
 2945: 
 2946:     scrollbar $textFrame.scroll -orient vertical -command \
 2947: 	    "$textFrame.text yview"
 2948:     set textWindow [text $textFrame.text -yscrollcommand \
 2949: 	    "$textFrame.scroll set" -wrap char]
 2950:     pack $textFrame.scroll $textWindow -side left -expand 0
 2951:     pack configure $textFrame.scroll -expand 0 -fill y
 2952:     pack configure $textWindow -expand true -fill both
 2953: 
 2954: #    label $infoFrame.label -textvariable gRefFile($num)
 2955: #    pack $infoFrame.label -side left
 2956: 
 2957:     menubutton $menuFrame.file -text File -menu $menuFrame.file.m
 2958:     menubutton $menuFrame.edit -text Edit -menu $menuFrame.edit.m
 2959:     pack $menuFrame.file $menuFrame.edit -side left
 2960:     
 2961:     set fileMenu [ menu $menuFrame.file.m ]
 2962:     set editMenu [ menu $menuFrame.edit.m ]
 2963: 
 2964:     $fileMenu add command -label Save -command \
 2965: 	    "saveText $textWindow 0 $num" -accelerator "Alt+s"
 2966:     bind $referenceFile <Alt-s> \
 2967: 	    "saveText $textWindow 0 $num"
 2968:     $fileMenu add command -label "Save As" -command \
 2969: 	    "saveText $textWindow 1 $num" -accelerator "Alt+S"
 2970:     bind $referenceFile <Alt-Shift-s> "saveText $textWindow 1 $num" 
 2971:     $fileMenu add command -label Delete -command "deleteFile $num"
 2972:     $fileMenu add command -label Print -command "printText $textWindow"
 2973:     $fileMenu add command -label Close -command "closeRefFile $num" \
 2974: 	    -accelerator "Alt+w"
 2975:     bind $referenceFile <Alt-w> "closeRefFile $num"
 2976: #    bind $referenceFile <Destroy> "closeRefFile $num 1"
 2977:     wm protocol $referenceFile WM_DELETE_WINDOW "closeRefFile $num 1"
 2978:     $editMenu add command -label "Cut" -command "tk_textCut $textWindow" \
 2979: 	    -accelerator "Alt+x"
 2980:     bind $referenceFile <Alt-x> "tk_textCut $textWindow"
 2981:     $editMenu add command -label "Copy" -command "tk_textCopy $textWindow" \
 2982: 	    -accelerator "Alt+c"
 2983:     bind $referenceFile <Alt-c> "tk_textCopy $textWindow"
 2984:     $editMenu add command -label "Paste" -command "tk_textPaste $textWindow" \
 2985: 	    -accelerator "Alt+v"
 2986:     bind $referenceFile <Alt-v> "tk_textPaste $textWindow"
 2987:     $editMenu add command -label "Select All " -command \
 2988: 	    "selectAll $num "  -accelerator "Alt+a"
 2989:     bind $referenceFile <Alt-a> "selectAll $num" 
 2990:     $editMenu add separator
 2991:     $editMenu add command -label "Undo" -command "undo $num" \
 2992: 	    -accelerator "Alt+u"
 2993:     bind $referenceFile <Alt-u> "undo $num" 
 2994: #    $editMenu add command -label "Redo" -command "redo $num"
 2995:     $editMenu add separator
 2996:     $editMenu add command -label "Find" -command "createFindWindow $num" \
 2997: 	-accelerator "Alt+f"
 2998: 
 2999:     
 3000:     if { !$new } {
 3001: 	set fileId [open $file r]
 3002: 	$textWindow insert 0.0 [read $fileId [file size $file]]
 3003: 	$textWindow delete end-1c
 3004: 	close $fileId
 3005:     }
 3006:     
 3007:     set gRefText($num) $textWindow
 3008:     rename $textWindow .$textWindow
 3009:     trackChanges $textWindow $num
 3010:     set gUndo($num) 0
 3011:     set gUndo($num.cur) 0
 3012: 
 3013:     createImportLinks $num 0.0 end
 3014:     focus -force $textWindow 
 3015: #    update
 3016:     set coord [$textWindow bbox 0.0]
 3017:     event generate $textWindow <1> -x [lindex $coord 0] -y [lindex $coord 1]
 3018: #    update
 3019:     event generate $textWindow <ButtonRelease-1>
 3020: #    update
 3021: #    capaRaise $referenceFile
 3022:     after 1 "catch \{focus $textWindow;raise $referenceFile\}"
 3023:     selection clear
 3024: #order is important here since gRefChanged has a trace on it the references 
 3025: #gRefChangedLast
 3026:     set gRefChangedLast($num) 0
 3027:     set gRefChanged($num) 0
 3028:     set gRefClosed($num) 0
 3029:     addFindList $num
 3030:     return $num
 3031: }
 3032: 
 3033: ###########################################################
 3034: ###########################################################
 3035: ###########################################################
 3036: ###########################################################
 3037: proc trackChanges { procName num } {
 3038:     eval "proc $procName args {
 3039: 	global gUndo gRefChanged gChanged
 3040: 	if {\[regexp {^(ins|del).*} \[lindex \$args 0\]\]} { 
 3041: 	    #puts \"\$args\"
 3042: 	    if { \$gUndo($num.cur) != \$gUndo($num) } {
 3043: 		set i \[expr \$gUndo($num.cur) + 1 \]
 3044: 		while { \[ info exists gUndo($num.\$i) \] } {
 3045: 		    unset gUndo($num.\$i)
 3046: 		    incr i
 3047: 		}
 3048: 		set gUndo($num) \$gUndo($num.cur)
 3049: 	    }
 3050: 	    set gUndo($num.cur) \$gUndo($num)
 3051: 	    set insertindex \[.$procName index \[lindex \$args 1 \] \]
 3052: 	    set numChange \[set gUndo($num.cur) \[incr gUndo($num) \] \]
 3053: 	    if { $num == 0 } { set gChanged 1 } else { set gRefChanged($num) 1 }
 3054: 	}
 3055: 	if {\[regexp {^(ins).*} \[lindex \$args 0\]\]} {
 3056: 	    set index2 \$insertindex+\[string length \[lindex \$args 2 \] \]chars
 3057: 	    set gUndo($num.\$numChange) \"delete \$insertindex \$index2 \"
 3058:             if {\[regexp {.*\[\"/\].*} \$args\] || \
 3059:                 \[regexp {.*\[\"/\].*} \[.$procName get \"\$insertindex linestart\" \"\$index2 lineend\"\]\]} { 
 3060:                 registerCreateImportLinks $num \$insertindex \$index2
 3061:             }
 3062: 	} elseif {\[regexp {^(del).*} \[lindex \$args 0\]\]} { 
 3063: 	    if { \[catch { set insertindex2 \[.$procName index \
 3064: 		    \[lindex \$args 2 \] \] } \] } {
 3065: 		set chars \[ .$procName get \$insertindex \]
 3066:                 set insertindex2 \$insertindex+1c
 3067: 	    } else {
 3068: 		set chars \[ .$procName get \$insertindex \$insertindex2 \]
 3069: 	    }
 3070: 	    set gUndo($num.\$numChange) \"insert \$insertindex \[list \$chars\] \"
 3071:             if { \[string length \$chars\] > 100 } {
 3072:                    registerCreateImportLinks $num \$insertindex-1line \$insertindex2+1line
 3073:             } else {
 3074:                 if {\[regexp \{.*\[\"/\].*\} \$chars\] || \
 3075:                     \[regexp \{.*\[\"/\].*\} \[.$procName get \"\$insertindex linestart\" \"\$insertindex2 lineend\"\]\]} { 
 3076:                    registerCreateImportLinks $num \$insertindex \$insertindex2
 3077:                 }
 3078:             }
 3079:         }
 3080: 	set result \[uplevel .$procName \$args\]
 3081:         updateLocation $num
 3082:         return \$result
 3083:     }"
 3084: }
 3085: 
 3086: ###########################################################
 3087: ###########################################################
 3088: ###########################################################
 3089: ###########################################################
 3090: proc undo { num } {
 3091:     global gUndo gRefText gTextWindow gChanged gRefChanged
 3092:     if { $gUndo($num.cur) == 0 } { return }
 3093:     set undoInfo $gUndo($num.$gUndo($num.cur))
 3094:     if { [regexp {.*[\"/].*} $undoInfo] } {
 3095: 	registerCreateImportLinks $num [lindex $undoInfo 1] end
 3096:     }
 3097:     if { [regexp {.*delete.*} $undoInfo] } {
 3098: 	registerCreateImportLinks $num [lindex $undoInfo 1] [lindex $undoInfo 2]
 3099:     }
 3100:     if { $num == 0 } {
 3101: 	if {[catch {eval ".$gTextWindow $gUndo($num.$gUndo($num.cur))"}]} { return }
 3102: 	set gChanged 1
 3103:     } else {
 3104: 	if {[catch {eval ".$gRefText($num) $gUndo($num.$gUndo($num.cur))"}]} { return }
 3105: 	set gRefChanged($num) 1
 3106:     }
 3107:     incr gUndo($num.cur) -1
 3108: }
 3109: 
 3110: ###########################################################
 3111: ###########################################################
 3112: ###########################################################
 3113: ###########################################################
 3114: proc redo { num } {
 3115:     global gUndo gRefText
 3116: }
 3117: 
 3118: ###########################################################
 3119: ###########################################################
 3120: ###########################################################
 3121: ###########################################################
 3122: proc gotoRefLine { number } {
 3123:     global gRefLine gRefText
 3124:     if { [catch {set gRefText($number)}] } { return }
 3125:     if { ![winfo exists $gRefText($number)] } { return }
 3126: 
 3127:     if { $gRefLine($number) == "" } {
 3128: 	return
 3129:     } else {
 3130: 	$gRefText($number) mark set insert $gRefLine($number).0
 3131: 	catch {$gRefText($number) tag remove sel sel.first sel.last}
 3132: 	$gRefText($number) tag add sel "insert linestart" "insert lineend"
 3133: 	$gRefText($number) see insert
 3134:     } 
 3135: }
 3136: 
 3137: ###########################################################
 3138: # updateLocation
 3139: ###########################################################
 3140: ###########################################################
 3141: ###########################################################
 3142: proc updateLocation { number } {
 3143:     global gRefCurLine gRefText gTextWindow gLineNumber gCharacterNumber
 3144: 
 3145:     if {$number} {set window $gRefText($number)} {set window $gTextWindow} 
 3146: #    if {![winfo exists $gRefText($number)]} {return};#do I need this
 3147:     set spot [split [.$window index insert] "."]
 3148:     if { $number } {
 3149: 	set gRefCurLine($number) [lindex $spot 0 ]
 3150:     } else {
 3151: 	set gLineNumber [lindex $spot 0]
 3152: 	set gCharacterNumber [lindex $spot 0]
 3153:     }
 3154: }
 3155: 
 3156: ###########################################################
 3157: ###########################################################
 3158: ###########################################################
 3159: ###########################################################
 3160: proc askToSave { refNum mustClose } {
 3161:     global gPrompt
 3162:     
 3163:     set dialog [toplevel .askToSavePrompt -borderwidth 10]
 3164:     wm title $dialog "Do you wish to Save"
 3165:     wm geo $dialog "+200+200"
 3166:     if { $refNum } {
 3167: 	global gRefFile
 3168: 	set msg "Reference File: $gRefFile($refNum) has changed. Do you wish to save?"
 3169:     } else {
 3170: 	set msg "Source has changed do you wish to save?"
 3171:     }
 3172:     message $dialog.msg -text $msg -aspect 800
 3173:     
 3174:     set gPrompt(result) ""
 3175:     set buttonFrame [frame $dialog.buttons -bd 10]
 3176:     pack $dialog.msg $buttonFrame -side top -fill x
 3177:     
 3178:     bind $dialog <Destroy> { 
 3179: 	set gPrompt(result) Cancel
 3180: 	set gPrompt(yes) 0
 3181:     }
 3182: 
 3183:     button $buttonFrame.yes -text Yes -underline 0 -command {
 3184: 	set gPrompt(yes) 1
 3185:     }
 3186:     button $buttonFrame.no -text No -underline 0 -command {
 3187: 	set gPrompt(yes) 0
 3188:     } 
 3189:     if { !$mustClose } {
 3190: 	button $buttonFrame.cancel -text Cancel  -underline 0 -command { 
 3191: 	    set gPrompt(yes) 0 
 3192: 	    set gPrompt(result) Cancel
 3193: 	}
 3194: 	pack $buttonFrame.yes $buttonFrame.no $buttonFrame.cancel -side left
 3195:     } else {
 3196: 	pack $buttonFrame.yes $buttonFrame.no -side left
 3197:     }
 3198:     bind $dialog <Alt-Key> break
 3199: 
 3200:     Centre_Dialog $dialog default
 3201:     update
 3202: 
 3203:     focus $dialog
 3204:     capaRaise $dialog
 3205:     capaGrab $dialog
 3206:     vwait gPrompt(yes)
 3207:     capaGrab release $dialog
 3208:     bind $dialog <Destroy> ""
 3209:     destroy $dialog
 3210:     if {$gPrompt(yes)} {
 3211: 	if { $refNum } {
 3212: 	    global gRefText 
 3213: 	    saveText $gRefText($refNum) 0 $refNum
 3214: 	} else {
 3215: 	    saveDocument
 3216: 	}
 3217:     } else {
 3218: 	return $gPrompt(result)
 3219:     }
 3220: }    
 3221: 
 3222: ###########################################################
 3223: ###########################################################
 3224: ###########################################################
 3225: ###########################################################
 3226: proc saveDocument { {saveAs 0} } {
 3227:     global gFile gTextWindow gSetNumberText gChanged gEditWindow gWindowMenu \
 3228: 	gCapaConfig
 3229:     if { [catch {set gTextWindow}] } { return }
 3230:     if { ![winfo exists $gTextWindow] } { return }
 3231:     if { $gFile == "" } { set saveAs 1 }
 3232:     if {$saveAs == 1} {
 3233: 	set temp [tk_getSaveFile -title "Enter the name to Save As" \
 3234: 		-initialdir "[pwd]" ]
 3235: 	if { $temp == "" } {
 3236: 	    displayError "File not saved"
 3237: 	    return
 3238: 	}
 3239: 	
 3240: 	catch {removeWindowEntry "$gFile*"}
 3241: 	catch {removeFindList}	
 3242: 	set gFile $temp
 3243: 	addFindList
 3244: 	cd [file dirname $gFile]
 3245: 	set gSetNumberText [string range [file rootname [file tail $gFile]] \
 3246: 		3 end ]
 3247: 	checkIfValidFilename
 3248: 	wm title [winfo toplevel $gEditWindow] $gFile 
 3249: 
 3250: 	$gWindowMenu add command -label "$gFile" -command \
 3251: 		"capaRaise $gEditWindow"
 3252:     } else {
 3253: 	if { ([array name gCapaConfig quizzerBackupQZ] == "") ||
 3254: 	     ($gCapaConfig(quizzerBackupQZ)!="off") } {
 3255: 	    if { [catch {file copy -force $gFile $gFile.bak} ] } {
 3256: 		displayError "Unable to create backup for $gFile"
 3257: 	    }
 3258: 	}
 3259:     }
 3260:     
 3261:     set fileId [open $gFile w]
 3262: 
 3263:     puts -nonewline $fileId [$gTextWindow get 0.0 end]
 3264: 
 3265:     close $fileId
 3266: 
 3267:     savePrefs
 3268:     set gChanged 0
 3269: }
 3270:  
 3271: ###########################################################
 3272: ###########################################################
 3273: ###########################################################
 3274: ###########################################################
 3275: proc closeDocument { { mustClose 0 } { save 1 } } {
 3276:     global gFile gEditWindow gChanged gPrefs \
 3277: 	    gPutLine gTryVal gProbVal gHintVal gQuizTemp \
 3278: 	    gNumberParsedText gSetNumberText gClosedDocument gTextWindow
 3279:     if { [catch {set gEditWindow}] } { return }
 3280:     if { ![winfo exists $gEditWindow] } { return }
 3281:     if { $gClosedDocument } { return }
 3282:     if { $save && $gChanged } {
 3283: 	if { [askToSave 0 $mustClose] == "Cancel"  && (! $mustClose ) } { return }
 3284:     }
 3285:     if {(!$mustClose)&&[makeSure "Are you sure you wish to stop editing?"]=="Cancel"} {
 3286: 	return 
 3287:     }
 3288:     set gClosedDocument 1
 3289:     removeFindList
 3290:     destroy $gEditWindow
 3291:     removeWindowEntry "$gFile*"
 3292:     set gFile ""
 3293:     set gChanged 0
 3294:     set gPrefs(info) "Problem"
 3295:     set gPrefs(TeXHeader) ""
 3296:     set gPrefs(TeXFooter) ""
 3297:     set gPutLine 1
 3298:     set gTryVal 99
 3299:     set gHintVal 1
 3300:     set gProbVal 1
 3301:     set gQuizTemp "true"
 3302:     set gNumberParsedText ""
 3303:     set gSetNumberText ""
 3304: }
 3305: 
 3306: ###########################################################
 3307: ###########################################################
 3308: ###########################################################
 3309: ###########################################################
 3310: proc closeRefFile { refNum  { mustClose 0 } { save 1 } } {
 3311:     global gRefChanged gRefText gRefFile gRefClosed gRefCurLine gRefLine gRefChangedLast
 3312:     if { [catch {set gRefText($refNum)}] } { return }
 3313:     if { ![winfo exists $gRefText($refNum)] } { return }
 3314:     if { $gRefClosed($refNum) } { return }
 3315:     if { $save && $gRefChanged($refNum) } { 
 3316: 	if { [askToSave $refNum $mustClose] == "Cancel" && ( ! $mustClose ) } { return }
 3317:     }
 3318: 
 3319:     if { ( ! $mustClose ) && ( [makeSure "Are you sure you wish to stop editing $gRefFile($refNum)?"] == "Cancel" ) } {
 3320: 	return 
 3321:     }
 3322:     set gRefClosed($refNum) 1
 3323:     removeFindList $refNum
 3324:     destroy [winfo toplevel $gRefText($refNum)]
 3325:     removeWindowEntry "Reference $gRefFile($refNum)*"
 3326:     unset gRefText($refNum) gRefChanged($refNum) gRefClosed($refNum) gRefFile($refNum) \
 3327: 	gRefCurLine($refNum) gRefLine($refNum) gRefChangedLast($refNum)
 3328: }
 3329: 
 3330: ###########################################################
 3331: # quit
 3332: ###########################################################
 3333: # called when the quit option is selected on the menu, unmaps
 3334: # all keys.
 3335: ###########################################################
 3336: # Arguments: None
 3337: # Returns: Nothing
 3338: # Globals: gChanged - whether or not the file has been modified
 3339: ###########################################################
 3340: proc quit { { mustClose 0 } } {
 3341:     global gChanged gRefChanged gRefText
 3342: 
 3343:     if { (! $mustClose ) && [makeSure "Are you sure you wish to quit?"] == "Cancel" } {
 3344: 	return 
 3345:     }
 3346: 
 3347:     if { $gChanged } {
 3348:  	if { [askToSave 0 $mustClose] == "Cancel" && ( ! $mustClose ) } {
 3349: 	    return
 3350: 	}
 3351:     }
 3352:     
 3353:     foreach refNum [array names gRefChanged] {
 3354: 	if { $gRefChanged($refNum) == 1 } {
 3355: 	    if { [winfo exists $gRefText($refNum)] } {
 3356: 		if { [askToSave $refNum $mustClose ] == "Cancel" && (! $mustClose ) } {
 3357: 		    return
 3358: 		}
 3359: 	    }
 3360: 	}
 3361:     }
 3362: 
 3363:     exec /bin/rm -f quiztemp.ps
 3364:     exec /bin/rm -f quiztemp.dvi
 3365:     exec /bin/rm -f quiztemp.tex
 3366:     exec /bin/rm -f quiztemp.log
 3367:     exec /bin/rm -f quiztemp.aux
 3368:     exec /bin/rm -f quiztemp.txt
 3369: 
 3370:     unmapAllKeys
 3371: 
 3372:     exit
 3373: }
 3374: 
 3375: ###########################################################
 3376: # createStopButton
 3377: ###########################################################
 3378: ###########################################################
 3379: ###########################################################
 3380: proc createStopButton {} {
 3381:     global gStopStatus
 3382:     if {[winfo exists .stopbutton]} {destroy .stopbutton}
 3383:     set top [toplevel .stopbutton]
 3384:     button $top.stop -text "Stop Parser" -command "stopParser"
 3385:     label $top.status -textvariable gStopStatus -width 35 -anchor w
 3386:     pack $top.stop $top.status
 3387:     set gStopStatus ""
 3388:     grab $top
 3389:     Centre_Dialog $top default
 3390:     update
 3391: }
 3392: 
 3393: ###########################################################
 3394: # destroyStopButton
 3395: ###########################################################
 3396: ###########################################################
 3397: ###########################################################
 3398: proc destroyStopButton {} {
 3399:     grab release .stopbutton
 3400:     destroy .stopbutton
 3401: }
 3402: 
 3403: ###########################################################
 3404: # createDvi
 3405: ###########################################################
 3406: ###########################################################
 3407: ###########################################################
 3408: proc createDvi { {showXdvi 1} {showStopButton 0} {useqzparse 0}} {
 3409:     global gPreviewMode gCreateDviText gPrefs gSetNumberText \
 3410: 	    gStudentSelection gFile gWindowMenu gLatexId gParseErrorsText \
 3411: 	    gEditWindow gCreateDviTextTemp gXdviOpt  \
 3412:             gLoadHeaderSet gCapaConfig gControlDates gNumberParsedText \
 3413: 	    gDonePrinting
 3414: 
 3415:     if { [catch {set gEditWindow}] } { return }
 3416:     if { ![winfo exists $gEditWindow] } { return }
 3417:     
 3418:     catch { destroy .createDviText }
 3419:     set gCreateDviTextTemp [text .createDviText]
 3420:     
 3421:     switch $gPrefs(info) {
 3422: 	Problem { set type 0 }
 3423: 	ProblemAnswer { set type 1 }
 3424: 	Answer { set type 2 }
 3425:     }
 3426: 
 3427:     if { $useqzparse } {
 3428: 	set gDonePrinting 0
 3429: 	switch $gPrefs(info) {
 3430: 	    Problem	{ set type "-T" }
 3431: 	    Answer	{ set type "-Ta" }
 3432: 	    ProblemAnswer { set type "-Tb" }
 3433: 	    default	{ set type "-T"	}
 3434: 	}
 3435: 	createCreateDviWin
 3436: 	grab .createDvi
 3437: 	$gCreateDviText delete 0.0 end
 3438: 	if { [setupSetsToPrint set start end 0] == 1 } { return 1 }
 3439: 	if { [set gStopPrinting [expr 2 == [runLatex \
 3440:             "echo [pwd] | $gCapaConfig(qzparse_command) \
 3441: 	    -stu $gStudentSelection(studentNumber) -set $set \
 3442: 	    -d [pwd] -c [pwd] $type " gCreateDviText] ] ] } {
 3443: 	    exec rm -f $gStudentSelection(studentNumber).tex quiztemp.tex
 3444: 	    if {$showStopping} {
 3445: 		displayMessage "Printing has been stopped."
 3446: 		set gDonePrinting 1
 3447: 		set gStopPrinting 0
 3448: 	    }
 3449: 	    return 1
 3450: 	}
 3451: 	exec mv $gStudentSelection(studentNumber).tex quiztemp.tex
 3452: 	exec /bin/rm -f quiztemp.dvi 
 3453:     } else {
 3454: 	createStopButton
 3455: 	if { [catch { 
 3456: 	    set numberParsed [ texParse $type $gSetNumberText \
 3457: 				   $gStudentSelection(type) $gStudentSelection(random) \
 3458: 				   $gStudentSelection(studentNumber) \
 3459: 				   $gStudentSelection(studentName) \
 3460: 				   gCreateDviTextTemp 1 ] }]} {
 3461: 	    return
 3462: 	}
 3463: 	destroyStopButton
 3464: 	checkHeader $numberParsed
 3465: 	
 3466: 	if { [showParseErrors] != "" } {
 3467: 	    if { [makeSure "There were errors when parsing the .qz file, \
 3468: 		continue to create the .dvi?"] =="Cancel" } {
 3469: 		destroy $gCreateDviTextTemp
 3470: 		return
 3471: 	    }   
 3472: 	}
 3473: 	
 3474: 	set error [catch { set fileId [open quiztemp.tex w] } ]
 3475: 	if { $error } {
 3476: 	    displayError "Unable to create neccessary temp files, delete all the\
 3477:  quiztemp file from the class directory."
 3478: 	    return
 3479: 	}
 3480: 
 3481: 	set filename [file join [file dirname $gFile] TeXheader ]
 3482: 	set texfileId [open $filename r]
 3483: 	puts -nonewline $fileId [read $texfileId [file size $filename]]
 3484: 	close $texfileId
 3485:     
 3486: 	puts -nonewline  $fileId "[$gCreateDviTextTemp get 0.0 end]"
 3487: 	
 3488: 	set filename [file join [file dirname $gFile] TeXfooter ]
 3489: 	set texfileId [open $filename r]
 3490: 	puts $fileId [read $texfileId [file size $filename]]
 3491: 	close $texfileId
 3492: 	
 3493: 	close $fileId
 3494:     }
 3495: 
 3496:     destroy $gCreateDviTextTemp
 3497: 
 3498:     if { ![winfo exists .createDvi] } {
 3499: 	set createDviWindow [toplevel .createDvi]
 3500: 	$gWindowMenu add command -label "CreateDvi" -command \
 3501: 		"capaRaise $createDviWindow"
 3502: 	wm title $createDviWindow "LaTeX Output"
 3503: 	addFindList -3
 3504: 
 3505: 	set windowFrame [frame $createDviWindow.windowFrame]
 3506: 	set buttonFrame [frame $createDviWindow.buttonFrame]
 3507: 	
 3508: 	pack $windowFrame $buttonFrame -side bottom
 3509: 	pack configure $windowFrame -expand true -fill both
 3510: 	pack configure $buttonFrame -anchor e
 3511: 
 3512: 	scrollbar $windowFrame.scroll -orient vertical -command \
 3513: 		"$windowFrame.text yview"
 3514: 	set gCreateDviText [text $windowFrame.text -yscrollcommand \
 3515: 		"$windowFrame.scroll set" -wrap char -height 40]
 3516: 	
 3517: 	pack $windowFrame.scroll $gCreateDviText -side left -expand 0
 3518: 	pack configure $windowFrame.scroll -expand 0 -fill y
 3519: 	pack configure $gCreateDviText -expand true -fill both
 3520: 	
 3521: 	set appearingFrame [frame $buttonFrame.appearingFrame]
 3522: 	button $buttonFrame.ok -text Dismiss -command \
 3523: 		"trace vdelete gFile w updateCreateDvi
 3524:                  destroy $createDviWindow
 3525: 	         removeWindowEntry CreateDvi
 3526:                  removeFindList -3"
 3527: 	bind $createDviWindow <Destroy> \
 3528: 		"trace vdelete gFile w updateCreateDvi
 3529: 	         removeWindowEntry CreateDvi
 3530:                  removeFindList -3"
 3531: 	pack $appearingFrame $buttonFrame.ok -side left
 3532: 
 3533: 	button $appearingFrame.stop -text "Stop Creating Print Jobs"\
 3534: 		-command "stopPrinting"
 3535: 	set name [file rootname [file tail $gFile ] ].dvi
 3536: 	button $appearingFrame.print -text \
 3537: 		"Save.dvi file to $name" \
 3538: 		-command saveDvi
 3539: 	trace variable gFile w updateCreateDvi
 3540: 
 3541: 	if { $showStopButton } {
 3542: 	    pack $appearingFrame.stop $appearingFrame.print -side left
 3543: 	    pack forget $appearingFrame.print
 3544: 	} else {
 3545: 	    pack $appearingFrame.stop $appearingFrame.print -side left
 3546: 	    pack forget $appearingFrame.stop
 3547: 	}
 3548: 
 3549: 	Centre_Dialog $createDviWindow default
 3550: 	update
 3551:     } else {
 3552: 	if { $showStopButton } {
 3553: 	    pack forget .createDvi.buttonFrame.appearingFrame.print
 3554: 	    pack .createDvi.buttonFrame.appearingFrame.stop
 3555: 	} else {
 3556: 	    pack forget .createDvi.buttonFrame.appearingFrame.stop
 3557: 	    pack .createDvi.buttonFrame.appearingFrame.print
 3558: 	}
 3559: 	if { !$useqzparse } { $gCreateDviText delete 0.0 end }
 3560:     }
 3561:         
 3562:     exec /bin/rm -f quiztemp.dvi 
 3563:     $gCreateDviText insert end      "$gCapaConfig(latex_command)\n"
 3564:     $gCreateDviText see end
 3565:     set createdDvi [ runLatex "pwd ; $gCapaConfig(latex_command) quiztemp.tex < \
 3566: 	    [file join / dev null ]" gCreateDviText]
 3567: 
 3568:     if { ($showXdvi == 1)  && ( $createdDvi == 1 ) } { 
 3569: 	eval "exec $gCapaConfig(xdvi_command) $gXdviOpt quiztemp.dvi >& /dev/null & "
 3570:     }
 3571: 
 3572:     catch { capaRaise $gParseErrorsText }
 3573:     set gDonePrinting 1
 3574:     return $createdDvi
 3575: }
 3576: 
 3577: ###########################################################
 3578: ###########################################################
 3579: ###########################################################
 3580: ###########################################################
 3581: proc stopPrinting {} {
 3582:     global gStopPrinting
 3583:     set gStopPrinting 1
 3584: }
 3585: 
 3586: ###########################################################
 3587: ###########################################################
 3588: ###########################################################
 3589: ###########################################################
 3590: proc saveDvi { } {
 3591:     global gFile
 3592: 
 3593:     set name [file rootname [ file tail $gFile]].dvi
 3594:     catch { exec rm -f $name }
 3595: 
 3596:     if { [ catch { exec cp quiztemp.dvi $name } ] } {
 3597: 	displayMessage "Unable to create $name "
 3598:     } else {
 3599: 	displayMessage "Created $name "
 3600:     }
 3601: }
 3602: 
 3603: 
 3604: ###########################################################
 3605: ###########################################################
 3606: ###########################################################
 3607: ###########################################################
 3608: proc updateCreateDvi { name1 name2 op } {
 3609:     global gFile
 3610: 
 3611:     set name [file rootname [file tail $gFile ] ].dvi
 3612:     catch { .createDvi.buttonFrame.appearingFrame.print configure \
 3613: 	    -text "Save.dvi file to $name" }
 3614: }
 3615: ###########################################################
 3616: ###########################################################
 3617: ###########################################################
 3618: ###########################################################
 3619: proc printWindow {} {
 3620:     global gPrintSelection gWindowMenu gEditWindow gStopPrinting\
 3621: 	gSetNumberText gMaxSet gFile gChanged
 3622: 
 3623:     set gStopPrinting 0
 3624:     if { [catch {set gEditWindow}] } { return }
 3625:     if { ![winfo exists $gEditWindow] } { return }
 3626: 
 3627:     if { [winfo exists .print] } { 
 3628: 	capaRaise .print
 3629: 	return 
 3630:     }
 3631:     if { $gChanged } { if { [askToSave 0 0] == "Cancel" } { return } }
 3632: 
 3633:     set print [toplevel .print]
 3634:     $gWindowMenu add command -label "Print" -command "capaRaise $print"
 3635:     wm title $print "Select a Print Method"
 3636:     message $print.msg -text "Please specify a print method." -aspect 10000
 3637:     set oneSetFrame [frame $print.frame1 -relief groove -borderwidth 4]
 3638:     set moreSetFrame [frame $print.frame2 -relief groove -borderwidth 4]
 3639:     set buttonFrame [frame $print.buttons]
 3640:     pack $print.msg $oneSetFrame $moreSetFrame $buttonFrame -side top 
 3641:     pack configure $oneSetFrame $moreSetFrame -anchor w -fill x
 3642:    
 3643:     set msg2Frame [frame $moreSetFrame.msg2 -relief solid -borderwidth 2]
 3644:     set infoFrame [frame $moreSetFrame.frame1]
 3645:     set msg3Frame [frame $moreSetFrame.msg3 -relief solid -borderwidth 2]
 3646:     set setFrame [frame $moreSetFrame.frame2] 
 3647:     pack $msg2Frame $setFrame $msg3Frame $infoFrame -anchor w
 3648:     
 3649:     if {[catch {set gPrintSelection(sets)}]} {set gPrintSelection(sets) printCur}
 3650:     if {[catch {set gPrintSelection(type)}]} {set gPrintSelection(type) printSpecific}
 3651:     if {[catch {set gPrintSelection(setend)}]} {set gPrintSelection(setend) $gSetNumberText}
 3652:     radiobutton $setFrame.specific -text "Print Current Set ($gSetNumberText)" \
 3653: 	    -value "printCur" -variable gPrintSelection(sets) 
 3654:     set scaleFrame [frame $setFrame.scales]
 3655:     pack $setFrame.specific $scaleFrame -anchor w
 3656: 
 3657:     for { set i 1 } { $i <= $gMaxSet } { incr i } {
 3658: 	if { ! [file exists [file join [file dirname $gFile] records "set$i.db"]] } { break }
 3659:     }
 3660:     incr i -1
 3661:     set gPrintSelection(setend) $gSetNumberText
 3662:     radiobutton $scaleFrame.range -text "Print Set Range:" \
 3663: 	    -value "printRange" -variable gPrintSelection(sets) 
 3664:     scale $scaleFrame.start -from 1 -to $i -variable gPrintSelection(setstart) \
 3665: 	-orient h 
 3666:     label $scaleFrame.msg -text "to"
 3667:     scale $scaleFrame.end -from 1 -to $i -variable gPrintSelection(setend) \
 3668: 	-orient h 
 3669:     pack $scaleFrame.range $scaleFrame.start $scaleFrame.msg \
 3670: 	$scaleFrame.end -side left
 3671: 
 3672:     button $buttonFrame.ok -text "Select" -command selectedPrintMethod
 3673:     button $buttonFrame.cancel -text "Cancel" -command \
 3674: 	    "destroy .print
 3675:              removeWindowEntry Print"
 3676:     bind $print <Destroy> "removeWindowEntry Print"
 3677:     pack $buttonFrame.ok $buttonFrame.cancel -side left
 3678:     
 3679:     set msgFrame [frame $oneSetFrame.msg -relief solid -borderwidth 2]
 3680:     set currentDviFrame [frame $oneSetFrame.currentDvi]
 3681:     set currentPreviewFrame [frame $oneSetFrame.currentPreview]
 3682:     set randomFrame [frame $oneSetFrame.random]
 3683:     set specificFrame [frame $infoFrame.specific]
 3684:     set sectionFrame [frame $infoFrame.section]
 3685:     set multSectionFrame [frame $infoFrame.multsection]
 3686:     set wholeClassFrame [frame $infoFrame.wholeClass]
 3687:     pack $msgFrame $currentDviFrame $currentPreviewFrame $randomFrame $specificFrame \
 3688: 	    $sectionFrame $multSectionFrame $wholeClassFrame -anchor w \
 3689: 	    -side top
 3690:     pack configure $specificFrame -expand true -fill both
 3691: 
 3692:     label $msgFrame.msg -text "Select:"
 3693:     pack $msgFrame.msg -anchor w
 3694:     label $msg2Frame.msg -text "Or Select:"
 3695:     pack $msg2Frame.msg -anchor w
 3696:     label $msg3Frame.msg -text "For:"
 3697:     pack $msg3Frame.msg -anchor w
 3698: 
 3699:     radiobutton $currentDviFrame.currentDvi -text "Print current .dvi" \
 3700: 	    -value "printCurrentDvi" -variable gPrintSelection(type) 
 3701:     pack $currentDviFrame.currentDvi -side left 
 3702: 
 3703:     radiobutton $randomFrame.random -text \
 3704: 	    "Randomly select one student from section:" \
 3705: 	    -value "printRandom" -variable gPrintSelection(type) 
 3706:     entry $randomFrame.entry -textvariable gPrintSelection(random)  -width 3 \
 3707: 	-validate key -validatecommand "limitEntry %W 3 number %P"
 3708:     pack $randomFrame.random $randomFrame.entry -side left 
 3709: 
 3710:     radiobutton $specificFrame.specific -text "Specify the student by:" \
 3711: 	-value "printSpecific" -variable gPrintSelection(type) 
 3712:     set studentNumber [frame $specificFrame.studentNumber]
 3713:     set fullName [frame $specificFrame.fullName]
 3714:     pack $specificFrame.specific $studentNumber $fullName -side top
 3715:     pack configure $specificFrame.specific -anchor w
 3716:     pack configure $studentNumber $fullName -anchor e
 3717: 
 3718:     radiobutton $sectionFrame.section -text "Print section" \
 3719: 	-value "printSection" -variable gPrintSelection(type) 
 3720:     entry $sectionFrame.entry -textvariable gPrintSelection(section)  -width 3 \
 3721: 	-validate key -validatecommand "limitEntry %W 3 number %P"
 3722:     pack $sectionFrame.section $sectionFrame.entry -side left
 3723: 
 3724:     radiobutton $multSectionFrame.section -text "Print multiple sections" \
 3725: 	-value "printMultipleSections" -variable gPrintSelection(type) 
 3726:     pack $multSectionFrame.section -side left
 3727: 
 3728:     radiobutton $wholeClassFrame.wholeClass -text "Print whole class." \
 3729: 	-value "printWholeClass" -variable gPrintSelection(type) 
 3730:     pack $wholeClassFrame.wholeClass -side left
 3731:     
 3732:     message $studentNumber.msg -text "Student Number: "  -aspect 10000
 3733:     entry $studentNumber.entry -textvariable gPrintSelection(studentNumber) -width 9 \
 3734: 	-validate key -validatecommand "limitEntry %W 9 any %P"
 3735:     pack $studentNumber.msg $studentNumber.entry -side left
 3736: 
 3737:     message $fullName.msg -text "Student Name: "  -aspect 10000 
 3738:     entry $fullName.entry -textvariable gPrintSelection(studentName) -width 30 \
 3739: 	-validate key -validatecommand "limitEntry %W 30 any %P"
 3740:     pack $fullName.msg $fullName.entry -side left
 3741: 
 3742:     trace variable gPrintSelection(studentNumber) w \
 3743: 	"global gPrintSelection; set gPrintSelection(type) printSpecific ;#"
 3744:     trace variable gPrintSelection(studentName) w \
 3745: 	"global gPrintSelection; set gPrintSelection(type) printSpecific ;#"
 3746: 
 3747: #    puts "trace info:[trace vinfo gPrintSelection(studentNumber)]"
 3748: 
 3749:     bind $studentNumber.entry <KeyPress-Return> \
 3750: 	"fillInStudent gPrintSelection(studentName) gPrintSelection(studentNumber) 0"
 3751:     bind $fullName.entry <KeyPress-Return> \
 3752: 	"fillInStudent gPrintSelection(studentName) gPrintSelection(studentNumber) 1"
 3753: 
 3754: 
 3755:     #Disable the entry boxes that are not selected, and enable the 
 3756:     #ones that are
 3757: #    $specificFrame.specific configure -command "
 3758: #           $studentNumber.entry configure -state normal
 3759: #           $fullName.entry configure -state normal"
 3760: #    $randomFrame.random configure -command "
 3761: #           $studentNumber.entry configure -state disabled
 3762: #           $fullName.entry configure -state disabled"    
 3763: 
 3764:     #If the window had been called up before we need to check the state
 3765:     #of the variable and disable/enable the correct enry boxes
 3766: #    if { $gPrintSelection(type) == "printSpecific" } {
 3767: #	   $studentNumber.entry configure -state normal
 3768: #           $fullName.entry configure -state normal
 3769: #    } 
 3770: #    if { $gPrintSelection(type) == "printRandom" } {
 3771: #           $studentNumber.entry configure -state disabled
 3772: #           $fullName.entry configure -state disabled
 3773: #    } 
 3774:     
 3775:     Centre_Dialog $print default
 3776: }
 3777: 
 3778: proc selectedPrintMethod {} {
 3779:     global gStopPrinting gPrintSelection gStudentSelection
 3780:     
 3781:     switch $gPrintSelection(type) {
 3782: 	printSpecific {
 3783: 	    if { $gPrintSelection(studentNumber) == "" } {
 3784: 		displayError "You must specify a student number."
 3785: 		return
 3786: 	    }
 3787: 	}
 3788: 	printSection {
 3789: 	    if { $gPrintSelection(section)== "" } {
 3790: 		displayError "You must specify a section."
 3791: 		return
 3792: 	    }
 3793: 	}
 3794: 	default {}
 3795:     }
 3796: 	
 3797:     destroy .print
 3798:     removeWindowEntry Print
 3799:     [set gPrintSelection(type)]
 3800:     set gStopPrinting 0 
 3801: }
 3802: 
 3803: ###########################################################
 3804: # createCreateDviWin
 3805: ###########################################################
 3806: ###########################################################
 3807: ###########################################################
 3808: proc createCreateDviWin {} { 
 3809:     global gWindowMenu gFile gCreateDviText
 3810:     if { ![winfo exists .createDvi] } {
 3811: 	set createDviWindow [toplevel .createDvi]
 3812: 	$gWindowMenu add command -label "CreateDvi" -command \
 3813: 		"capaRaise $createDviWindow"
 3814: 	wm title $createDviWindow "LaTeX Output"
 3815: 
 3816: 	set windowFrame [frame $createDviWindow.windowFrame]
 3817: 	set buttonFrame [frame $createDviWindow.buttonFrame]
 3818: 	
 3819: 	pack $windowFrame $buttonFrame -side bottom
 3820: 	pack configure $windowFrame -expand true -fill both
 3821: 	pack configure $buttonFrame -anchor e
 3822: 
 3823: 	scrollbar $windowFrame.scroll -orient vertical -command \
 3824: 		"$windowFrame.text yview"
 3825: 	set gCreateDviText [text $windowFrame.text -yscrollcommand \
 3826: 		"$windowFrame.scroll set" -wrap char -height 40]
 3827: 	
 3828: 	pack $windowFrame.scroll $gCreateDviText -side left -expand 0
 3829: 	pack configure $windowFrame.scroll -expand 0 -fill y
 3830: 	pack configure $gCreateDviText -expand true -fill both
 3831: 	
 3832: 	set appearingFrame [frame $buttonFrame.appearingFrame]
 3833: 	button $buttonFrame.ok -text Dismiss -command \
 3834: 	    "checkDestroyPrint $createDviWindow"
 3835: 	wm protocol $createDviWindow WM_DELETE_WINDOW \
 3836: 	    "checkDestroyPrint $createDviWindow"
 3837: 	pack $appearingFrame $buttonFrame.ok -side left
 3838: 
 3839: 	button $appearingFrame.stop -text "Stop Creating Print Jobs"\
 3840: 		-command "stopPrinting"
 3841: 	set name [file rootname [file tail $gFile ] ].dvi
 3842: 	button $appearingFrame.print -text \
 3843: 		"Save.dvi file to $name" \
 3844: 		-command saveDvi
 3845: 	trace variable gFile w updateCreateDvi
 3846: 
 3847: 	pack $appearingFrame.stop $appearingFrame.print -side left
 3848: 	pack forget $appearingFrame.print
 3849: 
 3850: 	Centre_Dialog $createDviWindow default
 3851: 	update
 3852:     } else {
 3853: 	pack forget .createDvi.buttonFrame.appearingFrame.print
 3854: 	pack .createDvi.buttonFrame.appearingFrame.stop
 3855:     }
 3856: }
 3857: 
 3858: ###########################################################
 3859: # printBody
 3860: ###########################################################
 3861: # sends the file quiztemp.ps to the printer through lpr using
 3862: # the option foud in gLprCommand
 3863: ###########################################################
 3864: # Arguments: lprCommand - actual command to be run to print
 3865: #            showCompletionMessage - (defaults to 1 true)
 3866: #                      controls whether the print complete 
 3867: #                      message gets shown
 3868: # Returns: Nothing
 3869: # Globals: gCapaConfig -
 3870: #          gStopPrinting -
 3871: # Files: quiztemp.ps - file containg info to print (removed)
 3872: ###########################################################
 3873: proc printBody { lprCommand { showCompletionMessage 1 } } {
 3874:     global gCapaConfig gStopPrinting gDonePrinting
 3875: 
 3876:     set errorMsg ""
 3877:     set error [ catch {eval exec $gCapaConfig(dvips_command) quiztemp.dvi \
 3878: 	    -o quiztemp.ps >& /dev/null} errorMsg ]
 3879:     if { $error } {
 3880: 	displayError \
 3881: 		"When attempting to run dvips an error occurred : $errorMsg"
 3882: 	return 1
 3883:     }
 3884: 
 3885:     if { $gStopPrinting } {
 3886: 	displayMessage "Printing has been stopped."
 3887: 	set gStopPrinting 0
 3888: 	set gDonePrinting 1
 3889: 	return 1
 3890:     }
 3891: 
 3892:     set errorMsg
 3893:     set error [catch {set returnMessage [eval "exec $lprCommand"] } errorMsg ]
 3894:     
 3895:     if { $error == 1 } {
 3896:         displayError "When attempting to print an error occurred : $errorMsg"
 3897: 	return 1
 3898:     } else {
 3899: 	if { $showCompletionMessage } {
 3900: 	    displayMessage "Print job sent to the printer.\n $returnMessage"
 3901: 	}
 3902:     }
 3903:     
 3904:     return 0
 3905: }
 3906: 
 3907: 
 3908: 
 3909: ###########################################################
 3910: ###########################################################
 3911: ###########################################################
 3912: ###########################################################
 3913: proc printCurrentDvi {} {
 3914: 
 3915:     set lprCommand [getLprCommand quiztemp.ps]
 3916: 
 3917:     if {$lprCommand == ""} { 
 3918: 	displayError "You must at least specify a print queue for lpr. \
 3919: 		Nothing printed."
 3920: 	return 
 3921:     }
 3922: 
 3923:     if {$lprCommand == "Cancel"} { 
 3924: 	return
 3925:     }
 3926:     
 3927:     printBody $lprCommand
 3928:     
 3929:     return 0
 3930: }
 3931: 
 3932: ###########################################################
 3933: ###########################################################
 3934: ###########################################################
 3935: ###########################################################
 3936: proc printCurrentPreview {} {
 3937: 
 3938:     set lprCommand [getLprCommand quiztemp.ps]
 3939: 
 3940:     if {$lprCommand == ""} { 
 3941: 	displayError "You must at least specify a print queue for lpr. \
 3942: 		Nothing printed."
 3943: 	return 
 3944:     }
 3945: 
 3946:     if {$lprCommand == "Cancel"} { 
 3947: 	return
 3948:     }
 3949: 
 3950:     if { [createDvi 0] == 2 } {
 3951: 	displayMessage "Printing has been stopped"
 3952:     }
 3953: 
 3954:     printBody $lprCommand
 3955: 
 3956:     return 0
 3957: }
 3958: 
 3959: ###########################################################
 3960: ###########################################################
 3961: ###########################################################
 3962: ###########################################################
 3963: proc printRandom {} {
 3964:     global gStudentSelection gPrintSelection
 3965:     
 3966:     set lprCommand [getLprCommand quiztemp.ps]
 3967: 
 3968:     if {$lprCommand == ""} { 
 3969: 	displayError "You must at least specify a print queue for lpr. \
 3970: 		Nothing printed."
 3971: 	return 
 3972:     }
 3973: 
 3974:     if {$lprCommand == "Cancel"} { 
 3975: 	return
 3976:     }
 3977: 
 3978:     set type $gStudentSelection(type)
 3979:     set random $gStudentSelection(random)
 3980: 
 3981:     set gStudentSelection(type) Random
 3982:     set gStudentSelection(random) $gPrintSelection(random)
 3983: 
 3984:     if { [createDvi 0 1] == 2 } {
 3985: 	displayMessage "Printing has been stopped"
 3986:     }
 3987: 
 3988:     printBody $lprCommand
 3989: 
 3990:     set gStudentSelection(type) $type
 3991:     set gStudentSelection(random) $random
 3992:     
 3993:     return 0
 3994: }
 3995: 
 3996: ###########################################################
 3997: ###########################################################
 3998: ###########################################################
 3999: ###########################################################
 4000: proc printSpecific {} {
 4001:     global gStudentSelection gPrintSelection
 4002:     
 4003:     set lprCommand [getLprCommand quiztemp.ps]
 4004: 
 4005:     if {$lprCommand == ""} { 
 4006: 	displayError "You must at least specify a print queue for lpr. \
 4007: 		Nothing printed."
 4008: 	return 
 4009:     }
 4010: 
 4011:     if {$lprCommand == "Cancel"} { 
 4012: 	return
 4013:     }
 4014: 
 4015:     set type $gStudentSelection(type)
 4016:     set studentNumber $gStudentSelection(studentNumber)
 4017: 
 4018:     set gStudentSelection(type) Specific
 4019:     set gStudentSelection(studentNumber) $gPrintSelection(studentNumber)
 4020: 
 4021:     if { [createDvi 0 1 1] == 2 } {
 4022: 	displayMessage "Printing has been stopped"
 4023:     }
 4024: 
 4025:     printBody $lprCommand
 4026: 
 4027:     set gStudentSelection(type) $type
 4028:     set gStudentSelection(studentNumber) $studentNumber
 4029:     
 4030:     return 0
 4031: }
 4032: 
 4033: ###########################################################
 4034: ###########################################################
 4035: ###########################################################
 4036: ###########################################################
 4037: proc printStudent { studentNumber } {
 4038:     global gStudentSelection gStopPrinting gDonePrinting
 4039:     
 4040:     set type $gStudentSelection(type)
 4041:     set studentNumberOld $gStudentSelection(studentNumber)
 4042: 
 4043:     set gStudentSelection(type) Specific
 4044:     set gStudentSelection(studentNumber) $studentNumber
 4045: 
 4046:     set createdDvi [createDvi 0 1]
 4047: 
 4048:     if { $createdDvi == 1 } { printBody } else { 
 4049: 	displayMessage "Printing has been stopped"
 4050: 	set gStopPrinting 0
 4051: 	set gDonePrinting 1
 4052:     }
 4053:     
 4054:     set gStudentSelection(type) $type
 4055:     set gStudentSelection(studentNumber) $studentNumberOld
 4056: 
 4057:     return 0
 4058: }
 4059: 
 4060: proc checkDestroyPrint { createDviWindow } {
 4061:     global gDonePrinting
 4062:     if { !$gDonePrinting } {
 4063: 	if { [makeSure "Do you really wish to stop printing?"] == "Yes" } {
 4064: 	    global gStopPrinting 
 4065: 	    set gStopPrinting 1
 4066: 	    after 1000 "destroyPrint $createDviWindow"
 4067: 	}
 4068: 	return
 4069:     }
 4070:     destroyPrint $createDviWindow
 4071: }
 4072: 
 4073: proc destroyPrint { createDviWindow } {
 4074:     trace vdelete gFile w updateCreateDvi
 4075:     destroy $createDviWindow
 4076:     removeWindowEntry CreateDvi
 4077: }
 4078: 
 4079: ###########################################################
 4080: ###########################################################
 4081: ###########################################################
 4082: ###########################################################
 4083: proc setupSetsToPrint { setVar startVar endVar {checkForHeader 1}} {
 4084:     global gPrintSelection gSetNumberText gLoadHeaderSet 
 4085:     upvar $setVar set $startVar start $endVar end
 4086:     if { $gPrintSelection(sets) == "printRange" } {
 4087: 	set start $gPrintSelection(setstart)
 4088: 	set end $gPrintSelection(setend)
 4089: 	set set "$start:$end"
 4090: 	set errors ""
 4091: 	for {set i $start}  {$i <= $end} {incr i} {
 4092: 	    set gLoadHeaderSet $i
 4093: 	    if {[catch {getHeaderInfo}]} { append errors ", $i" }
 4094: 	}
 4095: 	if { $checkForHeader && $errors != "" } {
 4096: 	    set errors [string range $errors 1 end]
 4097: 	    if { [llength $errors] > 1 } { 
 4098: 		set errors "s$errors" 
 4099: 		set errors [linsert $errors [expr [llength $errors] - 1] and]
 4100: 	    }
 4101: 	    displayError "DB header has not yet been set for set$errors. Please set the DB Header before printing Sections."
 4102: 	    return 1
 4103: 	}
 4104:     } else {
 4105: 	set start [set end [set set $gSetNumberText]]
 4106: 	set gLoadHeaderSet $gSetNumberText
 4107: 	if {$checkForHeader && [catch {getHeaderInfo}]} {
 4108: 	    displayError "DB header has not yet been set. Please set the DB Header before printing Sections."
 4109: 	    return 1
 4110: 	}
 4111:     }
 4112: }
 4113: 
 4114: ###########################################################
 4115: ###########################################################
 4116: ###########################################################
 4117: ###########################################################
 4118: proc printSection { { lprCommand "" } } {
 4119:     global gPrintSelection gCapaConfig gSetNumberText gWindowMenu \
 4120: 	gCreateDviText gStopPrinting gPrefs gFile gDonePrinting \
 4121: 	gLoadHeaderSet
 4122: 
 4123:     set gDonePrinting 0
 4124:     set showStopping 0
 4125: 
 4126:     if { [setupSetsToPrint set start end] == 1 } { return 1}
 4127:     if { $lprCommand == "" } {
 4128: 	set showStopping 1
 4129: 	set lprCommand [getLprCommand quiztemp.ps]
 4130: 	if { $lprCommand == "" } {
 4131: 	    displayError "Print command was empty, unable to print."
 4132: 	    return 1
 4133: 	}
 4134: 	if {$lprCommand == "Cancel" } {
 4135: 	    return 1
 4136: 	}
 4137:     }
 4138: 
 4139:     createCreateDviWin
 4140:     
 4141:     grab .createDvi
 4142: 
 4143:     $gCreateDviText delete 0.0 end
 4144: 
 4145:     switch $gPrefs(info) {
 4146: 	Problem	{ set type "-T" }
 4147: 	Answer	{ set type "-Ta" }
 4148: 	ProblemAnswer { set type "-Tb" }
 4149: 	default	{ set type "-T"	}
 4150:     }
 4151: 
 4152:     set prSection [string trimleft $gPrintSelection(section) 0]
 4153: 
 4154:     if { [set gStopPrinting [expr 2 == [runLatex \
 4155: 	    "echo [pwd] | $gCapaConfig(qzparse_command) \
 4156: 	    -sec $prSection -set $set \
 4157: 	    -d [pwd] -c [pwd] $type " gCreateDviText] ] ] } {
 4158: 	for {set i $start} { $i <= $end} { incr i } {
 4159: 	    exec rm -f section$prSection-set$i.tex 
 4160: 	}
 4161: 	if {$showStopping} {
 4162: 	    displayMessage "Printing has been stopped."
 4163: 	    set gDonePrinting 1
 4164: 	    set gStopPrinting 0
 4165: 	}
 4166: 	return 1
 4167:     }
 4168:     if { $gStopPrinting } {
 4169: 	displayMessage "Printing has been stopped."
 4170: 	set gDonePrinting 1
 4171: 	set gStopPrinting 0
 4172: 	return 1
 4173:     }
 4174: 
 4175:     for { set i $start} { $i <= $end } { incr i } {
 4176: 	if { ! [file exists section$prSection-set$i.tex] } {
 4177: 	    if {$showStopping} {
 4178: 		displayError "The qzparse command: $gCapaConfig(qzparse_command), was unable to produce the expected output. Printing stopped"
 4179: 		set gStopPrinting 0
 4180: 		set gDonePrinting 1
 4181: 	    }
 4182: 	    return 2
 4183: 	}
 4184: 	
 4185: 	exec mv section$prSection-set$i.tex quiztemp.tex
 4186: 	exec /bin/rm -f quiztemp.dvi 
 4187: 
 4188: 	$gCreateDviText insert end "$gCapaConfig(latex_command)\n"
 4189: 	$gCreateDviText see end 
 4190: 	
 4191: 	if { [set gStopPrinting [ expr 2 == [runLatex \
 4192: 	    "pwd ; $gCapaConfig(latex_command) \
 4193: 	    quiztemp.tex < [file join / dev null ]" gCreateDviText ] ] ] } {
 4194: 	    if {$showStopping} {
 4195: 		displayError "The LaTeX command: $gCapaConfig(latex_command), was unable to produce the expected output. Printing stopped"
 4196: 		set gStopPrinting 0
 4197: 		set gDonePrinting 1
 4198: 	    }
 4199: 	    return 1
 4200: 	}
 4201: 
 4202: 	if { $gStopPrinting } {
 4203: 	    displayMessage "Printing has been stopped."
 4204: 	    set gDonePrinting 1
 4205: 	    set gStopPrinting 0
 4206: 	    return 1
 4207: 	}
 4208: 
 4209: 	set a [expr ($showStopping) && ($end == $i)]
 4210: 	if { [set gStopPrinting [printBody $lprCommand $a ] ] } {
 4211: 	    if {$showStopping} {
 4212: 		displayMessage "Printing has been stopped."
 4213: 		set gDonePrinting 1
 4214: 		set gStopPrinting 0
 4215: 	    }
 4216: 	    return 1
 4217: 	}
 4218: 	if { $gStopPrinting } {
 4219: 	    displayMessage "Printing has been stopped."
 4220: 	    set gDonePrinting 1
 4221: 	    set gStopPrinting 0
 4222: 	    return 1
 4223: 	}
 4224:     }
 4225: 
 4226:     set gDonePrinting 1
 4227:     return 0
 4228: }
 4229: 
 4230: ###########################################################
 4231: ###########################################################
 4232: ###########################################################
 4233: ###########################################################
 4234: proc printMultipleSections { } {
 4235:     global gPrintSelection gCapaConfig gSetNumberText gWindowMenu \
 4236: 	    gCreateDviText gStopPrinting gPrefs gDonePrinting
 4237:     
 4238:     #checks if the DB Header is set
 4239:     if { [setupSetsToPrint set start end] == 1 } { return 1}
 4240: 
 4241:     set sectionList [ getExistingSections ]
 4242:     set sectionsToPrint [ pickSections $sectionList "Select Sections to Print:" ]
 4243: 
 4244:     if { $sectionsToPrint == "" } {
 4245: 	displayMessage "No sections selected, therefore nothing was printed."
 4246: 	return 1
 4247:     }
 4248:     if { $sectionsToPrint == "Cancel" } {
 4249: 	return 1
 4250:     }
 4251: 
 4252:     set lprCommand [getLprCommand quiztemp.ps]
 4253:     if { $lprCommand == "" } {
 4254: 	displayError "Print command was empty, unable to print."
 4255: 	return 1
 4256:     }
 4257:     if {$lprCommand == "Cancel" } {
 4258: 	return 1
 4259:     }
 4260: 
 4261:     if { [makeSure "You have selected to print $gPrefs(info)s for sections: [string trim $sectionsToPrint], using the print command \"$lprCommand\", continue?"] == "Cancel" } {
 4262: 	return 1
 4263:     }
 4264: 
 4265:     foreach section $sectionsToPrint {
 4266: 	set gDonePrinting 0
 4267: 	set gPrintSelection(section) $section
 4268: 	if { [set gStopPrinting [printSection $lprCommand] ] } {
 4269: 	    set gDonePrinting 0
 4270: 	    if { $gStopPrinting == 2 } {
 4271: 		displayError "The qzparse command: $gCapaConfig(qzparse_command), was unable to produce the expected output. Printing stopped"
 4272: 	    } else {
 4273: 		displayMessage "Printing has been stopped."
 4274: 	    }
 4275: 	    set gDonePrinting 1
 4276: 	    return 1
 4277: 	}
 4278: 	if { $gStopPrinting } {
 4279: 	    displayMessage "Printing has been stopped."
 4280: 	    set gStopPrinting 0
 4281: 	    set gDonePrinting 1
 4282: 	    return 1
 4283: 	}
 4284:     }
 4285:     set gDonePrinting 1
 4286:     displayMessage "Print jobs sent to the printer."
 4287:     return 0
 4288: }
 4289: 
 4290: ###########################################################
 4291: ###########################################################
 4292: ###########################################################
 4293: ###########################################################
 4294: proc printWholeClass { } {
 4295:     global gPrintSelection gCapaConfig gSetNumberText gWindowMenu \
 4296: 	    gCreateDviText gStopPrinting gPrefs gDonePrinting
 4297:     
 4298:     if {[catch {getHeaderInfo}]} {
 4299: 	displayError "DB header has not yet been set, Please set DB Header before printing Sections."
 4300: 	return 1
 4301:     }
 4302:     set sectionsToPrint [ getExistingSections ]
 4303: 
 4304:     if { $sectionsToPrint == "" } {
 4305: 	displayMessage "No sections exist, therefore nothing was printed."
 4306: 	return 1
 4307:     }
 4308: 
 4309:     set lprCommand [getLprCommand quiztemp.ps]
 4310: 
 4311:     if { $lprCommand == "" } {
 4312: 	displayError "Print command was empty, unable to print."
 4313: 	return 1
 4314:     }
 4315:     if {$lprCommand == "Cancel" } {
 4316: 	return 1
 4317:     }
 4318: 
 4319:     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" } {
 4320: 	return 1
 4321:     }
 4322: 
 4323:     foreach section $sectionsToPrint {
 4324: 	set section [lindex $section 0]
 4325: 	set gPrintSelection(section) $section
 4326: 	set gStopPrinting [printSection $lprCommand]
 4327: 	if { $gStopPrinting } {
 4328: 	    if { $gStopPrinting == 2 } {
 4329: 		displayError "$gCapaConfig(qzparse_command) was unable to produce the expected output. Printing stopped"
 4330: 	    } else {
 4331: 		displayMessage "Printing has been stopped."
 4332: 	    }
 4333: 	    set gDonePrinting 1
 4334: 	    set gStopPrinting 0
 4335: 	    return 1
 4336: 	}
 4337:     }
 4338:     set gDonePrinting 1
 4339:     displayMessage "Print jobs sent to the printer."
 4340:     return 0
 4341: }
 4342: 
 4343: ###########################################################
 4344: # analyzeSet
 4345: ###########################################################
 4346: ###########################################################
 4347: ###########################################################
 4348: proc analyzeSet {} {
 4349:     global gChanged gWindowMenu gEditWindow gAnalyze gSetNumberText gNumberParsedText
 4350: 
 4351:     if { [catch {winfo exists $gEditWindow}] } { return }
 4352:     if { ![ winfo exists $gEditWindow ] } { return }
 4353:     if { $gChanged } { if { [askToSave 0 0] == "Cancel" } { return } }
 4354:     if { [winfo exists .analyzeSet] } { 
 4355: 	capaRaise .analyzeSet
 4356: 	return
 4357:     }
 4358: 
 4359:     set analyze [toplevel .analyzeSet]
 4360:     $gWindowMenu add command -label "AnalyzeSet" \
 4361: 	    -command "capaRaise $analyze"
 4362:     wm title $analyze "Analyze Set"
 4363:     
 4364:     set settingsFrame [frame $analyze.settingsFrame]
 4365:     set dataFrame [frame $analyze.dataFrame]
 4366:     pack $settingsFrame $dataFrame -side top
 4367: 
 4368:     set classFrame [frame $settingsFrame.classFrame]
 4369:     set setFrame [frame $settingsFrame.setFrame]
 4370:     set probFrame [frame $settingsFrame.probFrame]
 4371:     set statusFrame [frame $settingsFrame.statusFrame]
 4372:     set statusBar [frame $settingsFrame.statusBar]
 4373:     set buttonFrame [frame $settingsFrame.buttonFrame]
 4374:     pack $classFrame $setFrame $probFrame $statusFrame $statusBar $buttonFrame \
 4375: 	-side top
 4376:     
 4377:     set canvasFrame [frame $dataFrame.canvasFrame]
 4378:     set numberFrame [frame $dataFrame.numberFrame]
 4379:     pack $canvasFrame $numberFrame -side top
 4380: 
 4381:     set gAnalyze(class) [pwd]
 4382:     label $classFrame.label -textvariable gAnalyze(class)
 4383:     pack $classFrame.label
 4384:     
 4385:     set gAnalyze(set) $gSetNumberText
 4386:     label $setFrame.lbl -text "Set number:"
 4387:     label $setFrame.set -textvariable gAnalyze(set)
 4388: #    button $setFrame.change -text "Change" -command analyzeChangeSet
 4389:     pack $setFrame.lbl $setFrame.set -side left
 4390: 
 4391:     if { [set gAnalyze(maxprob) $gNumberParsedText] == "" } { set gAnalyze(maxprob) 1 }
 4392:     
 4393:     label $probFrame.label -text "Problem Number :"
 4394:     set gAnalyze(scale) [scale $probFrame.problem \
 4395: 			     -from 1 -to $gAnalyze(maxprob) \
 4396: 			     -variable gAnalyze(prob) -command analyzeUpdate \
 4397: 			     -orient h -length 150 -tickinterval 1]
 4398:     pack $probFrame.label $probFrame.problem -side left
 4399: 
 4400:     set gAnalyze(status) ""
 4401:     label $statusFrame.label -text "Status:" 
 4402:     label $statusFrame.status -textvariable gAnalyze(status)
 4403:     pack $statusFrame.label $statusFrame.status -side left
 4404: 
 4405:     set gAnalyze(statcanvas) [canvas $statusBar.canvas -width 200 -height 20]
 4406:     pack $statusBar.canvas
 4407:     $gAnalyze(statcanvas) create rectangle 1 1 199 19 -outline black
 4408:     set gAnalyze(bar) [$gAnalyze(statcanvas) create rectangle 1 1 1 19 -fill red -outline black]
 4409: 
 4410:     button $buttonFrame.class -text "Run Class" -command "analyzeClass 1"
 4411:     button $buttonFrame.random -text "Run Random" -command analyzeRandom
 4412:     button $buttonFrame.stop -text "Stop" -command analyzeStop
 4413:     button $buttonFrame.close -text "Dismiss" -comman analyzeClose
 4414:     pack $buttonFrame.class $buttonFrame.random $buttonFrame.stop \
 4415: 	$buttonFrame.close -side left
 4416:     
 4417:     set gAnalyze(canvaswidth) 600
 4418:     set gAnalyze(canvas) [canvas $canvasFrame.canvas -width $gAnalyze(canvaswidth) \
 4419: 			      -height 100]
 4420:     pack $gAnalyze(canvas)
 4421:     
 4422:     set hiFrame [frame $numberFrame.hiFrame]
 4423:     set lowFrame [frame $numberFrame.lowFrame]
 4424:     set uniqFrame [frame $numberFrame.uniqFrame]
 4425:     pack $lowFrame $hiFrame $uniqFrame -side left
 4426:     pack configure $hiFrame -anchor e
 4427:     pack configure $lowFrame -anchor w
 4428: 
 4429:     label $hiFrame.label -text "High End:"
 4430:     label $hiFrame.num -textvariable gAnalyze(highnum)
 4431:     pack $hiFrame.label $hiFrame.num -side left
 4432: 
 4433:     label $lowFrame.label -text "Low End:"
 4434:     label $lowFrame.num -textvariable gAnalyze(lownum)
 4435:     pack $lowFrame.label $lowFrame.num -side left
 4436: 
 4437:     label $uniqFrame.label -text "Num. Unique:"
 4438:     label $uniqFrame.num -textvariable gAnalyze(numuniq)
 4439:     pack $uniqFrame.label $uniqFrame.num -side left
 4440: 
 4441:     set gAnalyze(studentNumbers) [getStudentNumbers]
 4442:     set gAnalyze(exit) 0
 4443: }
 4444: 
 4445: ###########################################################
 4446: # analyzeClass
 4447: ###########################################################
 4448: ###########################################################
 4449: ###########################################################
 4450: proc analyzeClass { {start 1} } {
 4451:     global gAnalyze gCapaConfig
 4452:     if { $gAnalyze(studentNumbers)=="" } { return }
 4453:     if { $start } { 
 4454: 	set gAnalyze(toprocess) $gAnalyze(studentNumbers) 
 4455: 	set gAnalyze(stop) 0
 4456: 	set gAnalyze(update) 1
 4457: 	set gAnalyze(done) 0
 4458: 	set gAnalyze(total) [expr [llength $gAnalyze(toprocess)]/3]
 4459: 	foreach name [array names gAnalyze *.\[alhu\]*] { unset gAnalyze($name) }
 4460:     }
 4461:     set number [lindex $gAnalyze(toprocess) 0]
 4462:     set name [lindex $gAnalyze(toprocess) 1]
 4463:     set section [lindex $gAnalyze(toprocess) 2]
 4464:     set gAnalyze(toprocess) [lrange $gAnalyze(toprocess) 3 end]
 4465:     set command "$gCapaConfig(answers_command) $number \"$name\" 0 $gAnalyze(set)"
 4466:     set fileId [open "|$command" "r"]
 4467:     set gAnalyze(pid) [pid $fileId]
 4468:     fconfigure $fileId -blocking 0
 4469:     fileevent $fileId readable "analyzeLine $fileId"
 4470:     set gAnalyze(status) "Processing $number"
 4471:     incr gAnalyze(done)
 4472:     $gAnalyze(statcanvas) coords $gAnalyze(bar) 1 1 [expr 200*($gAnalyze(done)/double($gAnalyze(total)))] 19
 4473:     update idletasks
 4474: }
 4475: 
 4476: ###########################################################
 4477: # analyzeEatQuestion
 4478: ###########################################################
 4479: ###########################################################
 4480: ###########################################################
 4481: proc analyzeEatQuestion { fileId } {
 4482:     global gAnalyze 
 4483:     if { $gAnalyze(exit) } { 
 4484: 	fileevent $fileId readable ""
 4485: 	catch {close $fileId}
 4486: 	return
 4487:     }
 4488:     set aline [gets $fileId]
 4489:     if { $aline != "" } {
 4490: 	switch -- [lindex [split $aline :] 0] {
 4491: 	    EQES { fileevent $fileId readable "analyzeLine $fileId" }
 4492: 	}
 4493:     }
 4494:     if { [eof $fileId] } { analyzeEnd $fileId }
 4495: }
 4496: 
 4497: ###########################################################
 4498: # analyzeLine
 4499: ###########################################################
 4500: ###########################################################
 4501: ###########################################################
 4502: proc analyzeLine { fileId } {
 4503:     global gAnalyze 
 4504:     
 4505:     if { $gAnalyze(exit) } { 
 4506: 	fileevent $fileId readable ""
 4507: 	catch {close $fileId}
 4508: 	return
 4509:     }
 4510:     set aline [gets $fileId]
 4511:     if { $aline != "" } {
 4512: 	switch [lindex [split $aline :] 0] {
 4513: 	    ANS { 
 4514: 		incr gAnalyze(problemNum)
 4515: 		set ans [string range $aline 4 end]
 4516: 		set length [llength $ans]
 4517: 		lappend gAnalyze($gAnalyze(problemNum).ans) \
 4518: 		    [lindex $ans 0]
 4519: 		if { ($length == 2) || ($length == 4)} {
 4520: 		    lappend gAnalyze($gAnalyze(problemNum).unit) \
 4521: 			[lindex $ans end]
 4522: 		} 
 4523: 		if { ($length == 3) || ($length == 4) } {
 4524: 		    lappend gAnalyze($gAnalyze(problemNum).low) \
 4525: 			[lindex $ans 1]
 4526: 		    lappend gAnalyze($gAnalyze(problemNum).high) \
 4527: 			[lindex $ans 2]
 4528: 		}
 4529: 	    }
 4530: 	    SET { set gAnalyze(problemNum) 0 }
 4531: 	    DONE { 
 4532: 		set gAnalyze(maxprob) $gAnalyze(problemNum)
 4533: 		$gAnalyze(scale) configure -to $gAnalyze(maxprob)
 4534: 	    }
 4535: 	    ERROR {
 4536:  		fileevent $fileId readable ""
 4537: 		displayError "Answers returned invalid message: $aline" 
 4538: 		fileevent $fileId readable "analyzeLine $fileId"
 4539: 	    }
 4540: 	    BQES { fileevent $fileId readable "analyzeEatQuestion $fileId" }
 4541: 	    default {
 4542: 	    }
 4543: 	}
 4544:     }
 4545:     if { [eof $fileId] } { analyzeEnd $fileId }
 4546: }
 4547: 
 4548: ###########################################################
 4549: # analyzeEnd
 4550: ###########################################################
 4551: ###########################################################
 4552: ###########################################################
 4553: proc analyzeEnd { fileId } {
 4554:     global gAnalyze
 4555:     if { [eof $fileId] } {
 4556: 	fileevent $fileId readable ""
 4557: 	catch {close $fileId}
 4558: 	if { $gAnalyze(stop) } { return }
 4559: 	if { [llength $gAnalyze(toprocess)] > 0 } { 
 4560: 	    analyzeClass 0 
 4561: 	} else {
 4562: 	    analyzeUpdate
 4563: 	    set gAnalyze(status) "Done"
 4564: 	}
 4565: 	if { !$gAnalyze(update) } {
 4566: 	    incr gAnalyze(update) 
 4567: 	    analyzeUpdate
 4568: 	} else {
 4569:  	    incr gAnalyze(update) 
 4570: 	    if { $gAnalyze(update) == 10 } {
 4571: 		set gAnalyze(update) 0
 4572: 	    }
 4573: 	}
 4574:     }
 4575: }
 4576: 
 4577: ###########################################################
 4578: # analyzeRandom
 4579: ###########################################################
 4580: ###########################################################
 4581: ###########################################################
 4582: proc analyzeRandom { } {
 4583:     global gAnalyze
 4584:     set numToRun [getString . "How many random students should be run?"]
 4585:     if { $numToRun == "" } { return }
 4586:     if { [catch {incr numToRun} ] } {
 4587: 	displayMessage "Invalid number."
 4588:     }
 4589:     incr numToRun -1
 4590:     set gAnalyze(total) $numToRun
 4591:     catch {unset gAnalyze(toprocess)}
 4592:     for { set i 0 } { $i < $numToRun } { incr i } {
 4593: 	append gAnalyze(toprocess) "[format "%09d" $i] Random 999 "
 4594:     }
 4595:     set gAnalyze(stop) 0
 4596:     set gAnalyze(update) 1
 4597:     set gAnalyze(done) 0
 4598:     foreach name [array names gAnalyze *.\[alhu\]*] { unset gAnalyze($name) }    
 4599:     analyzeClass 0
 4600: }
 4601: 
 4602: ###########################################################
 4603: # analyzeStrings
 4604: ###########################################################
 4605: ###########################################################
 4606: ###########################################################
 4607: proc analyzeStrings { prob window create} {
 4608:     global gAnalyze
 4609: 
 4610:     if { ![winfo exists $window.analyzestrings] } { if {!$create} { return } }
 4611:     if { ![catch {set setWin [toplevel $window.analyzestrings]}] } {
 4612: 	set msgFrame [frame $setWin.msgFrame]
 4613: 	set valFrame [frame $setWin.valFrame]
 4614: 	set buttonFrame [frame $setWin.buttonFrame]
 4615: 	pack $msgFrame $valFrame $buttonFrame
 4616: 	pack configure $valFrame -expand 1 -fill both
 4617: 	
 4618: 	message $msgFrame.msg -text "Correct Answers" -aspect 3000
 4619: 	pack $msgFrame.msg
 4620: 	
 4621: 	set maxWidth 1
 4622: 	foreach choice $gAnalyze($prob.ans) {
 4623: 	    if {[string length $choice]>$maxWidth} {set maxWidth [string length $choice]}
 4624: 	}
 4625: 	set maxStringWidth $maxWidth
 4626: 	incr maxWidth 6
 4627: 
 4628: 	set selectMode none
 4629: 	listbox $valFrame.val -width [expr $maxWidth + 2] \
 4630: 	    -yscrollcommand "$valFrame.scroll set" -selectmode $selectMode
 4631: 	scrollbar $valFrame.scroll -command "$valFrame.val yview"
 4632: 	pack $valFrame.val $valFrame.scroll -side left
 4633: 	pack configure $valFrame.val -expand 1 -fill both 
 4634: 	pack configure $valFrame.scroll -expand 0 -fill y
 4635: 	button $buttonFrame.cancel -text "Dismiss" -command "destroy $setWin"
 4636: 	pack $buttonFrame.cancel
 4637:     } else {
 4638: 	set maxWidth 1
 4639: 	set valFrame $window.analyzestrings.valFrame
 4640: 	$valFrame.val delete 0 end
 4641: 	foreach choice $gAnalyze($prob.ans) {
 4642: 	    if {[string length $choice]>$maxWidth} {set maxWidth [string length $choice]}
 4643: 	}
 4644: 	set maxStringWidth $maxWidth
 4645: 	incr maxWidth 6
 4646:     }
 4647:     set lastchoice [lindex $gAnalyze($gAnalyze(prob).ans) 0]
 4648:     set num 1
 4649:     foreach choice [lsort $gAnalyze($gAnalyze(prob).ans)] {    
 4650: 	if { $lastchoice != $choice } {
 4651: 	    $valFrame.val insert end \
 4652: 		"[format %-[set maxStringWidth]s $lastchoice] [format %5d $num]"
 4653: 	    set lastchoice $choice
 4654: 	    set num 1
 4655: 	} else {
 4656: 	    incr num
 4657: 	}
 4658:     }	
 4659:     $valFrame.val insert end \
 4660: 	"[format %-[set maxStringWidth]s $lastchoice] [format %5d $num]"
 4661: }
 4662: 
 4663: ###########################################################
 4664: # analyzeUpdate
 4665: ###########################################################
 4666: ###########################################################
 4667: ###########################################################
 4668: proc analyzeUpdate { {newProbNumber 0} } {
 4669:     global gAnalyze
 4670: 
 4671:     if {[catch {set gAnalyze($gAnalyze(prob).ans)}]} { return }
 4672:     foreach problem [array names gAnalyze *.\[alh\]*] {
 4673: 	if { [catch {set gAnalyze($problem) [lsort -real $gAnalyze($problem)]}]} {
 4674: 	    set gAnalyze($problem) [lsort $gAnalyze($problem)]
 4675: 	}
 4676:     }
 4677: 
 4678:     set c $gAnalyze(canvas)
 4679:     $c delete all
 4680:     set gAnalyze(lownum) [set low [lindex $gAnalyze($gAnalyze(prob).ans) 0]]
 4681:     set gAnalyze(highnum) [set high [lindex $gAnalyze($gAnalyze(prob).ans) end]]
 4682:     set gAnalyze(numuniq) [llength [lunique $gAnalyze($gAnalyze(prob).ans)]]
 4683: #don't draw anything if the answers aren't numbers
 4684:     if { [catch {expr $low + 1}]} { 
 4685: 	catch {destroy $c.button}
 4686: 	update idletask
 4687: 	button $c.button -text "List of strings" -command \
 4688: 	    "analyzeStrings $gAnalyze(prob) $c 1"
 4689: 	$c create window [expr $gAnalyze(canvaswidth)/2.0] 40 -window $c.button
 4690: 	analyzeStrings $gAnalyze(prob) $c 0
 4691: 	return 
 4692:     }
 4693: 
 4694:     $c create line 25 50 [expr $gAnalyze(canvaswidth) - 25] 50
 4695:     set diff [expr double($high-$low)]
 4696:     if { $diff == 0 } {
 4697: 	set center [expr $gAnalyze(canvaswidth)/2.0]
 4698: 	$c create rectangle [expr $center - 2] 48 [expr $center + 2] 52 -fill green
 4699: 	update idletasks
 4700: 	return
 4701:     }
 4702:     set delta [format "%1.e" [expr ($diff)/15.0]]
 4703:     set start [expr double(int($low/$delta)+1)*$delta]
 4704:     while { $start < $high } {
 4705: 	set center [expr ($gAnalyze(canvaswidth)-50)*(($start-$low)/$diff)]
 4706: 	set center [expr $center+25]
 4707: 	$c create line $center 40 $center 60
 4708: 	set start [expr $start + $delta]
 4709:     }
 4710:     if { ($low < 0) && ($high > 0) } {
 4711: 	set center [expr ($gAnalyze(canvaswidth)-50)*((0-$low)/$diff)]
 4712: 	set center [expr $center+25]
 4713: 	$c create rectangle [expr $center - 1] 40 [expr $center + 1] 60
 4714:     }
 4715:     set lastpoint [lindex $gAnalyze($gAnalyze(prob).ans) 0]
 4716:     set num 0
 4717:     foreach point $gAnalyze($gAnalyze(prob).ans) {    
 4718: 	if { $lastpoint != $point } {
 4719: 	    set center [expr ($gAnalyze(canvaswidth)-50)*(($lastpoint-$low)/$diff)]
 4720: 	    set center [expr $center+25]
 4721: 	    $c create rectangle [expr $center - 2] [expr 48-$num] \
 4722: 		[expr $center + 2] [expr 52+$num] -fill green
 4723: 	    set lastpoint $point
 4724: 	    set num 0
 4725: 	} else {
 4726: 	    incr num
 4727: 	}
 4728:     }	
 4729:     set center [expr ($gAnalyze(canvaswidth)-50)*(($lastpoint-$low)/$diff)]
 4730:     set center [expr $center+25]
 4731:     $c create rectangle [expr $center - 2] [expr 48-$num] \
 4732: 	[expr $center + 2] [expr 52+$num] -fill green
 4733:     
 4734:     update idletasks
 4735: }
 4736: 
 4737: ###########################################################
 4738: # analyzeStop
 4739: ###########################################################
 4740: ###########################################################
 4741: ###########################################################
 4742: proc analyzeStop {} {
 4743:     global gAnalyze
 4744:     set gAnalyze(stop) 1
 4745:     set gAnalyze(status) "Stopped"
 4746:     catch {exec kill -SIGKILL $gAnalyze(pid)}
 4747: }
 4748: 
 4749: ###########################################################
 4750: # analyzeClose
 4751: ###########################################################
 4752: ###########################################################
 4753: ###########################################################
 4754: proc analyzeClose { } {
 4755:     global gAnalyze
 4756:     destroy [winfo toplevel $gAnalyze(canvas)]
 4757:     unset gAnalyze
 4758:     global gAnalyze
 4759:     set gAnalyze(exit) 1
 4760: }
 4761: 
 4762: ###########################################################
 4763: # checkIfValidFilename 
 4764: ###########################################################
 4765: ###########################################################
 4766: ###########################################################
 4767: proc checkIfValidFilename {} {
 4768:     global gSetNumberText gPreviewButton
 4769:     if { [regexp \[^0-9\]+ $gSetNumberText] } {
 4770: 	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."
 4771: 	$gPreviewButton configure -state disabled
 4772: 	.main entryconfigure 5 -state disabled
 4773: 	.main entryconfigure 7 -state disabled
 4774: 	return 0
 4775:     }
 4776:     $gPreviewButton configure -state normal
 4777:     .main entryconfigure 5 -state normal
 4778:     .main entryconfigure 7 -state normal
 4779:     return 1
 4780: }
 4781: 
 4782: ###########################################################
 4783: # updateChangeStatus
 4784: ###########################################################
 4785: ###########################################################
 4786: ###########################################################
 4787: proc updateChangeStatus { name1 name2 op } {
 4788:     global gChanged gRefChanged gRefText gChangedLast gRefChangedLast \
 4789: 	gEditWindow gWindowMenu gRefFile
 4790:     if { $name1 == "gChanged" } {
 4791: 	if { $gChanged != $gChangedLast } {
 4792: 	    set gChangedLast $gChanged
 4793: 	    global gFile
 4794: 	    if { [catch {set gEditWindow}] } { return }
 4795: 	    if { ![winfo exists $gEditWindow] } { return }
 4796: 	    if { $gChanged } {
 4797: 		catch {removeWindowEntry "$gFile*"}
 4798: 		wm title [winfo toplevel $gEditWindow] "$gFile (Modified)"
 4799: 		$gWindowMenu add command -label "$gFile (Modified)" -command \
 4800: 		    "capaRaise $gEditWindow"
 4801: 	    } else {
 4802: 		catch {removeWindowEntry "$gFile*"}
 4803: 		wm title [winfo toplevel $gEditWindow] "$gFile"
 4804: 		$gWindowMenu add command -label "$gFile" -command \
 4805: 		    "capaRaise $gEditWindow"
 4806: 	    }
 4807: 	}
 4808:     } else {
 4809: 	if { $gRefChanged($name2) != $gRefChangedLast($name2) } {
 4810: 	    if { [catch {set gRefText($name2)}] } { return }
 4811: 	    if { ![winfo exists $gRefText($name2)] } { return }
 4812: 	    if { $gRefChanged($name2) } {
 4813: 		catch {removeWindowEntry  "Reference $gRefFile($name2)*" }
 4814: 	       wm title [winfo toplevel $gRefText($name2)] "$gRefFile($name2) (Modified)"
 4815: 		$gWindowMenu add command -label "Reference $gRefFile($name2) (Modified)" \
 4816: 		    -command "capaRaise [winfo toplevel $gRefText($name2)]"
 4817: 	    } else {
 4818: 		catch {removeWindowEntry  "Reference $gRefFile($name2)*" }
 4819: 		wm title [winfo toplevel $gRefText($name2)] "$gRefFile($name2)"
 4820: 		$gWindowMenu add command -label "Reference $gRefFile($name2)" \
 4821: 		    -command "capaRaise [winfo toplevel $gRefText($name2)]"
 4822: 	    }
 4823: 	}
 4824:     }
 4825: }
 4826: ###########################################################
 4827: # main
 4828: ###########################################################
 4829: # sets up the auto_path variable, some globals and adds some
 4830: # options then calls createControlWindow to give the user something
 4831: # to do
 4832: ###########################################################
 4833: # Arguments: None
 4834: # Returns: Nothing
 4835: # Globals:
 4836: ###########################################################
 4837: 
 4838: source quizzer.templates.tcl
 4839: 
 4840: if { [lindex $auto_path 0] == "./lib/tcl7.5" } {
 4841:     set auto_path ""
 4842:     lappend auto_path [pwd]/lib/tcl7.5
 4843:     lappend auto_path [pwd]/lib/tk4.1
 4844: }
 4845: 
 4846: lappend auto_path /usr/local/lib/CAPA45/Quizzer
 4847: lappend auto_path [pwd]
 4848: 
 4849: set font 8x13bold
 4850: catch {
 4851:     if { $argc > 0 } {
 4852: 	switch -glob -- [lindex $argv 0] {
 4853: 	    {-[Ll]*} { set font 9x15bold }
 4854: 	    {-[Mm]*} -
 4855: 	    {-[Nn]*} { set font 8x13bold }
 4856: 	    {-[Ss]*} { set font fixed }
 4857: 	}
 4858:     }
 4859: }
 4860: option add *font $font
 4861: createControlWindow

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