source: trunk/expgui @ 255

Last change on this file since 255 was 255, checked in by toby, 11 years ago

# on 2000/08/04 18:29:16, toby did:
Change phase buttons, add "Add Phase" after last phase
change old "Add Phase" to "Replace Phase" (not yet implemented)
Add xform atoms button w/dialog
implement sort on occupancy
change error message when a non-existing atom is selected (happens with empty phase)
implement phase flags -- when more than one phase is present
move SetPhaseFlag? to readexp.tcl
/

  • Property rcs:author set to toby
  • Property rcs:date set to 2000/08/04 18:29:16
  • Property rcs:lines set to +41 -35
  • Property rcs:rev set to 1.31
  • Property rcs:state set to Exp
  • Property svn:keywords set to Author Date Revision Id
File size: 116.9 KB
Line 
1#!/usr/local/bin/wish
2# $Id: expgui 255 2009-12-04 23:03:00Z toby $
3set expgui(Revision) {$Revision: 255 $ $Date: 2009-12-04 23:03:00 +0000 (Fri, 04 Dec 2009) $}
4
5# to do:
6#
7# need to change heading and button label depending on where getExpFileName
8# is called from?
9#
10# global background editing & profile work differently: should both
11# start out blank with a "load from option"?
12#
13# idea:
14# a scroll list for all histogram refinement flags ; click on takes you to the
15# appropriate menu.
16#
17# idea:
18#   change cell parameters to labels and have a edit cell button
19#   that enforces metric symmetry
20#
21# to allow "global" access on phase page
22#   change buttons from radio to multiple
23#   -- or display all 9 cell flag/damps and all atoms
24#   make editMultipleRecords work with multiple phases, also cell flag/damp
25#   blank cell entries
26#   add phase to atom number in listing
27#   DisplayAllAtoms needs to loop over phases: expgui(curPhase)
28#
29# idea: load more than one bank from a multi-bank .RAW file
30#
31if {$tcl_version < 8.0} {
32    tk_dialog .expFileErrorMsg "Version Error" \
33            "The program requires Tcl/Tk version 8.0 or higher" error 0 "Exit"
34    exit
35}
36
37if {$argv != ""} {
38    set expgui(expfile) $argv
39    if {[string toupper [file extension $expgui(expfile)]] != ".EXP"} {
40        append expgui(expfile) ".EXP"
41    }
42} else {
43    set expgui(expfile) {}
44}
45
46set expgui(curhist) {}
47set expmap(powderlist) {}
48
49set expgui(debug) 0
50catch {if $env(DEBUG) {set expgui(debug) 1}}
51#set expgui(debug) 1
52
53# default for archive mode = on
54set expgui(archive) 1
55# default for autoexec load = off
56set expgui(autoexpload) 0
57# save the name of the wish executable
58set wishshell [info nameofexecutable]
59# misc constants
60set txtvw(font) "Courier"
61set expgui(coordfont) "-*-courier-bold-r-normal--12-*"
62set expgui(histfont) "-*-courier-bold-r-normal--12-*"
63set liveplot(hst) 1
64set liveplot(legend) 1
65set expgui(filesort) 1
66set expgui(initstring) {}
67# use a separate window for DISAGL (default)
68set expgui(disaglSeparateBox) 1
69set expgui(DefaultPeakType) 0
70#=============================================================================
71#----------------------------------------------------------------
72# where are we?
73set expgui(script) [info script]
74# translate links -- go six levels deep
75foreach i {1 2 3 4 5 6} {
76    if {[file type $expgui(script)] == "link"} {
77        set link [file readlink $expgui(script)]
78        if { [file  pathtype  $link] == "absolute" } {
79            set expgui(script) $link
80        } {
81            set expgui(script) [file dirname $expgui(script)]/$link
82        }
83    } else {
84        break
85    }
86}
87# fixup relative paths
88if {[file pathtype $expgui(script)] == "relative"} {
89    set expgui(script) [file join [pwd] $expgui(script)]
90}
91set expgui(scriptdir) [file dirname $expgui(script) ]
92set expgui(gsasdir) [file dirname $expgui(scriptdir)]
93set expgui(gsasexe) [file join $expgui(gsasdir) exe]
94#----------------------------------------------------------------
95lappend auto_path $expgui(scriptdir)
96set expgui(havetix) 0
97set expgui(haveBW) 1
98# for debugging non-BWidget version set environment variable NOBWIDGET
99catch {if $env(NOBWIDGET) {set expgui(haveBW) 0}}
100if $expgui(haveBW) {
101    if [catch {package require BWidget}] {set expgui(haveBW) 0}
102}
103# get the notebook widget if not using BWidgets
104if {!$expgui(haveBW)} {source [file join $expgui(scriptdir) notebook.tcl]}
105#----------------------------------------------------------------
106if [file exists [file join $expgui(scriptdir) opts.tcl]] {
107    source [file join $expgui(scriptdir) opts.tcl]
108}
109# fetch EXP file processing routines
110source [file join $expgui(scriptdir) readexp.tcl]
111# commands for running GSAS programs
112source [file join $expgui(scriptdir) gsascmds.tcl]
113# contents of GSAS menus
114source [file join $expgui(scriptdir) gsasmenu.tcl]
115# commands for adding phases, histograms & atoms
116source [file join $expgui(scriptdir) addcmds.tcl]
117# commands for preferred orientation
118source [file join $expgui(scriptdir) orient.tcl]
119#---------------------------------------------------------------------------
120# override options with locally defined values
121if [file exists [file join $expgui(scriptdir) localconfig]] {
122    source [file join $expgui(scriptdir) localconfig]
123}
124if [file exists [file join ~ .gsas_config]] {
125    source [file join ~ .gsas_config]
126}
127if {$tcl_platform(platform) == "windows"} {
128    set expgui(exptool) [file join $expgui(gsasexe) exptool.exe]
129} else {
130    set expgui(exptool) [file join $expgui(gsasexe) exptool]
131}
132#---------------------------------------------------------------------------
133if {$expgui(expfile) != ""} {
134    if ![file exists $expgui(expfile)] {
135        update
136        set ans [tk_dialog .expFileErrorMsg "File Open Error" \
137                "File [file tail $expgui(expfile)] does not exist in [file dirname $expgui(expfile)]" \
138                error 0 "Create" "Open other"]
139        if $ans {set expgui(expfile) {}}
140    }
141}
142if {$expgui(expfile) == ""} {
143    # center the parent window because the getExpFileName window
144    # will be centered above it.
145    wm withdraw .
146    set x [expr [winfo screenwidth .]/2 - [winfo reqwidth .]/2 ]
147    set y [expr [winfo screenheight .]/2 - [winfo reqheight .]/2]
148    wm geom . +$x+$y
149    wm deiconify .
150    # windows needed this update before when using tk_getOpenFile.
151    # I am not sure it is still needed.
152    update
153    #
154    set expgui(expfile) [getExpFileName old]
155}
156if {$expgui(expfile) == ""} exit
157# you've been warned this .EXP does not exist!
158if ![file exists $expgui(expfile)] {
159    # create an "empty" exp file
160    createexp $expgui(expfile) \
161            [getstring "title for experiment $expgui(expfile)" 60 0]
162}
163catch {cd [string trim [file dirname $expgui(expfile)]]}
164
165#
166# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
167# <<<<<<<<<<    BEGINNING OF MAIN: GLOBAL AREA FOR DATA EXTRACTION >>>>>>>>>>>
168# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
169# load exp file and set up dialogs
170proc loadexp {expfile} {
171    global expgui expmap entryvar entrycmd tcl_platform
172    set exploadtime [time {set fmt [expload $expfile]}]
173    if $expgui(debug) {puts "expload $exploadtime"}
174    # if the file was not in the correct format, force a rewrite before use
175    if {$fmt < 0} {
176        # read error
177        return
178    } elseif {$fmt == 0 && $tcl_platform(platform) == "windows"} {
179        set expgui(changed) 1
180    } elseif {$fmt == 1 && $tcl_platform(platform) == "unix"} {
181        set expgui(changed) 1
182    } else {
183        set expgui(changed) 0
184    }
185
186    # force exp files to be upper case, force save if name changes
187    set filetail [file tail $expfile]
188    set filetailcaps [string toupper $filetail]
189    if {$tcl_platform(platform) == "unix" && $filetail != $filetailcaps} {
190        set expgui(changed) 1
191    }
192    if {[file dirname $expfile] == "."} {
193        set expgui(expfile) $filetailcaps
194    } else {
195        set expgui(expfile) [file join \
196                [file dirname $expfile] $filetailcaps]
197    }
198
199    mapexp
200    set expgui(expModifiedLast) 0
201    catch {
202        set expgui(expModifiedLast) [file mtime $expgui(expfile)]
203    }
204    set expgui(last_History) [string range [string trim [lindex [exphistory last] 1]] 0 50 ]
205    # set the window/icon title
206    wm title . "EXPGUI $expfile"
207    set expgui(titleunchanged) 1
208    wm iconname . [file tail $expfile]
209
210    # set the number of phases on the phase page
211    setphases
212
213    # disable the "global options" that don't make sense based on
214    # which histograms present
215    foreach num {1 2 3 4 5} {
216        set flag($num) 0
217    }
218    # save a list of the allowed modes, too
219    set expgui(AllowedHistSelectModes) {0 6}
220    foreach h $expmap(powderlist) {
221        if {[string range $expmap(htype_$h) 2 2] == "T"} {set flag(1) 1}
222        if {[string range $expmap(htype_$h) 1 2] == "NC"} {set flag(2) 1}
223        if {[string range $expmap(htype_$h) 1 2] == "XC" && \
224                [histinfo $h lam2] != 0.0} {set flag(3) 1}
225        if {[string range $expmap(htype_$h) 1 2] == "XC" && \
226                [histinfo $h lam2] == 0.0} {set flag(4) 1}
227        if {[string range $expmap(htype_$h) 1 2] == "XE"} {set flag(5) 1}
228    }
229    foreach num {1 2 3 4 5} \
230            lbl {TOF "CW Neutron" "Alpha12 Xray" "Monochromatic Xray" \
231            "Energy Disp Xray"} {
232        if $flag($num) {
233            $expgui(fm).option.menu.editmode entryconfigure $lbl -state normal
234            lappend expgui(AllowedHistSelectModes) $num
235        } else {
236            $expgui(fm).option.menu.editmode entryconfigure $lbl -state disabled
237        }
238    }
239    # disable traces on entryvar until we are ready
240    set entrycmd(trace) 0
241    trace vdelete entryvar w entvartrace
242
243    # propogate changes on the least squares page
244    set entryvar(cycles) [expinfo cycles]
245    set entrycmd(cycles) "expinfo cycles"
246    # set expgui(globalmode) 0
247    set expgui(printopt) "Print Options ([expinfo print])"
248    set entryvar(title) [expinfo title]
249    global printopts
250    foreach num [array names printopts] {
251        set entrycmd(printopt$num) "printsetting $num"
252        set entryvar(printopt$num) [printsetting $num]
253    }
254    # enable traces on entryvar
255    set entrycmd(trace) 1
256    trace variable entryvar w entvartrace
257
258    # set fo extraction on LS page
259    SetupExtractHist
260
261    # update the histogram list & update the page
262    sethistlist
263
264    # start checking for external changes
265    afterawhile
266}
267
268# called to reread the .EXP file
269proc rereadexp {expfile} {
270    global expgui
271    if $expgui(changed) {
272        set decision [tk_dialog .instrSaveData {Save .EXP changes} \
273                {You have made changes to the Experiment. Rereading will cause the changes to be lost. Select an option:} \
274                {} 0 "Save and reread" "Reread without Save" "Cancel reread command"]
275        switch $decision {
276            0 { savearchiveexp }
277            1 { }
278            2 { return }
279        }
280    }
281    loadexp $expgui(expfile)
282}
283
284proc SaveAsFile {} {
285    global expgui
286    set newexpfile [getExpFileName new]
287    if {$newexpfile == ""} return
288    expwrite $newexpfile
289    set expgui(expfile) $newexpfile
290    catch {cd [string trim [file dirname $expgui(expfile)]]}
291    set expgui(changed) 0
292    set expgui(expModifiedLast) [file mtime $expgui(expfile)]
293    set expgui(last_History) [string range [string trim [lindex [exphistory last] 1]] 0 50 ]
294    # set the window/icon title
295    wm title . $expgui(expfile)
296    set expgui(titleunchanged) 1
297    wm iconname . [file tail $expgui(expfile)]
298}
299
300# called to read a different .EXP file
301proc readnewexp {} {
302    global expgui expmap
303    if $expgui(changed) {
304        set decision [tk_dialog .instrSaveData {Save .EXP changes} \
305                {You have made changes to the Experiment. Reading a different file will cause the changes to be lost. Select an option:} \
306                {} 0 "Save and read" "Read without Save" "Cancel read command"]
307        switch $decision {
308            0 { savearchiveexp }
309            1 {                }
310            2 { return }
311        }
312    }
313    set newexpfile [getExpFileName old]
314    if {$newexpfile == ""} return
315
316    # switch to the 1st page
317    RaisePage lsFrame
318
319    if ![file exists $newexpfile] {
320        # you've been warned this .EXP does not exist!
321        # create an "empty" exp file
322        createexp $newexpfile \
323                [getstring "title for experiment $newexpfile" 60 0]
324    }
325    set expgui(expfile) $newexpfile
326    catch {cd [string trim [file dirname $expgui(expfile)]]}
327    set expgui(globalmode) 0
328    loadexp $expgui(expfile)
329
330    # select the 1st phase
331    SelectOnePhase [lindex $expmap(phaselist) 0]
332    # select the first histogram in the list by default (if there are any)
333    if {[llength $expmap(histlistboxcontents)] > 0} {
334        set expgui(curhist) 0
335    } else {
336        set expgui(curhist) {}
337    }
338    if {[CountHistory] > 100} {
339        DeleteHistoryRecords "This .EXP file has [CountHistory] history records\nErasing most will speed EXPGUI"
340    }
341}
342
343#------------- set up data read/write layer ----------------------
344# trace routine on entryvar
345proc entvartrace {array elem action} {
346    global expgui entrycmd entryvar
347    if !$entrycmd(trace) return
348   
349    catch {
350        if {$entrycmd($elem) == ""} return
351        incr expgui(changed)
352        if $expgui(debug) {puts "$entrycmd($elem)  set $entryvar($elem) "}
353        if {$entrycmd($elem) == ""} return
354        if [catch {
355            eval $entrycmd($elem) set [list $entryvar($elem)]
356            if {[lindex $entrycmd($elem) 0] == "atominfo"} {
357                after idle {DisplayAllAtoms noreset}
358            }
359        } errmsg] {puts "entvartrace error: $errmsg"}   
360    }
361}
362
363# disable traces on entryvar until we are ready
364set entrycmd(trace) 0
365trace variable entryvar w entvartrace
366
367#
368#
369#
370##############################################################################
371#####                    #####################################################
372##### PROCEDURES SECTION #####################################################
373#####                    #####################################################
374##############################################################################
375# reset routine is used for debugging
376proc reset {} {
377    global expgui script argv
378    set script $expgui(script)
379    set argv $expgui(expfile)
380    # remove traces
381    global entryvar
382    foreach cmd [trace vinfo entryvar] {
383        eval trace vdelete entryvar $cmd
384    }
385    global expgui
386    foreach cmd [trace vinfo expgui(backterms)] {
387        eval trace vdelete entryvar $cmd
388    }
389    foreach cmd [trace vinfo expgui(backtype)] {
390        eval trace vdelete entryvar $cmd
391    }
392    foreach a {
393        expmap expgui entryvar entrycmd 
394        expgui_menulist expgui_cmdlist expgui_helplist
395    } {
396        global $a
397        catch {unset  $a}
398    }
399    foreach w [winfo children .] {
400        destroy $w
401    }
402
403    uplevel #0 {source $script}
404}
405
406# save some of the global options in ~/.gsas_config
407proc SaveOptions {} {
408    global expgui env tcl_platform
409    set fp [open [file join ~ .gsas_config] a]
410    foreach item {archive asorttype hsorttype filesort disaglSeparateBox} {
411        puts $fp "set expgui($item) $expgui($item)"
412    }
413    if {$tcl_platform(platform) != "windows"} {
414        puts $fp "set env(GSASBACKSPACE) $env(GSASBACKSPACE)"
415        puts $fp "set expgui(autoexpload) $expgui(autoexpload)"
416    }
417    close $fp
418}
419
420proc About { } {
421    global expgui expmap
422    tk_dialog .about {About...} \
423"EXPGUI\n\
424Jonathan Wasserman and Brian Toby\n\
425NIST Center for Neutron Research\n\n\
4262000, Not subject to copyright\n\n\
427Revision [lindex $expgui(Revision) 1] (readexp.tcl [lindex $expmap(Revision) 1])\n\n\
428Generalized Structure Analysis System (GSAS)\n\
429A. C. Larson and\n R. B. Von Dreele,\n LANSCE, Los Alamos\n\n\
430" \
431        info 0 OK
432}
433
434# wait until idle
435proc afterawhile {} {
436    # cancel any other instances of this loop
437    after cancel afterawhile
438    after cancel whenidle
439    after cancel whenidle
440    after idle whenidle
441}
442
443# This is called every 2 seconds to check for changes to the .EXP file
444proc whenidle {} {
445    global expgui
446    if $expgui(titleunchanged) {
447        if {$expgui(changed) != 0} {
448            wm title . "$expgui(expfile) (modified)"
449            set expgui(titleunchanged) 0
450        }
451    }
452    if {$expgui(expModifiedLast) == 0} {
453        after 2000 afterawhile
454        return
455    }
456    if {[file mtime $expgui(expfile)] != $expgui(expModifiedLast)} {
457        set ans [ReloadExpMsg [file tail $expgui(expfile)] $expgui(changed)]
458        if {$ans == 0} {
459            loadexp $expgui(expfile)
460        } elseif {$ans == 1} {
461            # reset the time to the next version
462            set expgui(expModifiedLast) [file mtime $expgui(expfile)]
463        } elseif {$ans == 2} {
464            savearchiveexp
465        }
466    }
467    after 2000 afterawhile
468}
469
470# place a message about changes over the main window
471proc ReloadExpMsg {file changes} {
472    global expgui tcl_platform
473    set msg "File $file has been modified by another program"
474    if {$changes == 1} {
475        append msg " and you have made a change to this version.\n"
476    } elseif {$changes > 0} {
477        append msg " and you have made $changes changes to this version.\n"
478    } else {
479        append msg ".\n"
480    }
481    append msg "Do you want to use the newer (modified) version or continue with the older (previous) version of the file?"
482
483    set w .ask
484    catch {destroy $w}
485    toplevel $w -class Dialog
486    wm title $w "Reload?"
487    wm iconname $w "Reload?"
488    wm protocol $w WM_DELETE_WINDOW { }
489    wm transient $w .
490    frame $w.bot
491    pack $w.bot -side bottom
492    frame $w.top
493    pack $w.top -side top -fill both -expand 1
494    label $w.top.msg -justify left \
495            -wraplength 5i -font {Times 18} \
496            -text $msg
497    if {$tcl_platform(platform) == "windows"} {
498        $w.top.msg config -font {Times 14}
499    }
500    pack $w.top.msg  -side right -expand 1 -fill both -padx 3m -pady 3m
501    pack [button $w.bot.1 -text "Load new" \
502            -default active -command "set expgui(dialogbutton) 0" \
503            ] -side left -expand 1 -padx 3m -pady 2m
504    pack [button $w.bot.2 -text "Continue with old" \
505            -command "set expgui(dialogbutton) 1"] \
506            -side left -expand 1 -padx 3m -pady 2m
507    if {$changes > 0} {
508        pack [button $w.bot.3 -text "Save edited version" \
509            -command "set expgui(dialogbutton) 2"] \
510            -side left -expand 1 -padx 3m -pady 2m
511    }
512    # Create a binding for <Return> on the dialog
513    bind $w <Return> "tkButtonInvoke $w.bot.1"
514    wm withdraw $w
515    update idletasks
516
517    # for windows put the box in the upper left, for
518    # unix center it over the parent (in unix it appears later)
519    #if {$tcl_platform(platform) == "windows"} {
520        #wm geom $w +0+0
521    #} else {
522
523        # for now, always center the message over the main window
524        # center the new window in the middle of the parent
525        set x [expr [winfo x .] + [winfo width .]/2 - \
526                [winfo reqwidth $w]/2 - [winfo vrootx .]]
527        set y [expr [winfo y .] + [winfo height .]/2 - \
528                [winfo reqheight $w]/2 - [winfo vrooty .]]
529        wm geom $w +$x+$y
530    #}
531    wm deiconify $w
532
533    # Grab the focus
534    set oldFocus [focus]
535    set oldGrab [grab current $w]
536    if {[string compare $oldGrab ""]} {
537        set grabStatus [grab status $oldGrab]
538    }
539    grab $w
540    focus $w.bot.1
541    # for windows rearrange window stacking
542    if {$tcl_platform(platform) == "windows"} {
543        lower .
544        raise $w .
545    }
546    update idletasks
547
548    tkwait variable expgui(dialogbutton)
549    catch {focus $oldFocus}
550    destroy $w
551    if {[string compare $oldGrab ""]} {
552      if {![string compare $grabStatus "global"]} {
553            grab -global $oldGrab
554        } else {
555            grab $oldGrab
556        }
557    }
558    raise .
559    return $expgui(dialogbutton)
560}
561
562# --------  called to confirm before exiting
563proc catchQuit {} {
564    if {[confirmBeforeSave] == "Continue"} {
565        destroy .
566    }
567}
568# save the .EXP file before exiting?
569proc confirmBeforeSave {} {
570    global expgui
571    if !$expgui(changed) {
572        return "Continue"
573    }
574    set decision [tk_dialog .instrSaveData {Save .EXP changes} \
575            {You have made changes to the Experiment, but the changes are not saved. Select an option:} \
576            {} 0 "Save and Exit" "Exit without Save" "Cancel exit command"]
577    switch $decision {
578        0 { savearchiveexp;  return "Continue" }
579        1 {                  return "Continue" }
580        2 {                  return "Cancel"   }
581    }
582}
583
584proc archiveexp {} {
585    global expgui tcl_platform
586    # is there a file to archive
587    if {![file exists $expgui(expfile)]} return
588    catch {
589        set expnam [file rootname $expgui(expfile)]
590        if {$tcl_platform(platform) == "windows"} {
591            set version -1
592            catch {source $expnam.version}
593            incr version
594            if ![file executable [file join $expgui(scriptdir) pkzip.exe]] {
595                # archive w/o pkzip
596                set file $expnam![format "%3.3d" $version].EXP
597                file copy -force $expnam.EXP $file
598                set fp [open $expnam.lst a]
599                puts $fp "\n--------------------------------------------------------------"
600                puts $fp "Archiving $expnam.EXP as $file"
601                puts $fp "--------------------------------------------------------------\n"
602                close $fp
603            } else {
604                # archive with PKZIP           
605                # need to limit expnam to 8 characters
606                set sexp [string toupper [string range [file root [file tail $expnam] ] 0 7]]
607                # PKZIP can't handle long dir names either
608                cd [set dir [file dirname $expnam]]
609                set file $sexp.[format "%3.3d" $version]
610                file copy -force $expnam.EXP $file
611                exec [file join $expgui(scriptdir) pkzip.exe] -m $sexp $file > zip.out &
612                set fp [open $expnam.lst a]
613                puts $fp "\n--------------------------------------------------------------"
614                puts $fp "Archiving $expnam.EXP as $file in [file join $dir $sexp.ZIP]"
615                puts $fp "--------------------------------------------------------------\n"
616                close $fp
617            }
618            set fp [open $expnam.version w]
619            puts $fp "set version $version"
620            close $fp
621        } else {
622            set files [glob -nocomplain $expnam.EXP.*]
623            if {$files == ""} {
624                set file $expnam.EXP.000
625            } else {
626                set file [lindex [lsort -decreasing $files] 0]
627                regexp {.*\.EXP.0?0?([0-9]*).*} $file junk number
628                incr number
629                set file $expnam.EXP.[format "%3.3d" $number]
630            }
631            file copy $expgui(expfile) $file
632            if [catch {exec gzip $file}] {
633                exec echo "\n----------------------------------------------" >> $expnam.LST
634                exec echo "     Archiving $expnam.EXP as $file " >> $expnam.LST
635                exec echo "----------------------------------------------\n" >> $expnam.LST
636            } else {
637                exec echo "\n----------------------------------------------" >> $expnam.LST
638                exec echo "     Archiving $expnam.EXP as $file.gz " >> $expnam.LST
639                exec echo "----------------------------------------------\n" >> $expnam.LST
640            }
641        }
642    } errmsg
643    if {$errmsg != ""} {
644        tk_dialog .warn Confirm "Error in archive: $errmsg" warning 0 OK
645    }
646}
647
648# save and optionally archive the expfile
649proc savearchiveexp {} {
650    global expgui expmap
651    if !$expgui(changed) return
652    if $expgui(archive) archiveexp
653    # add a history record
654    exphistory add " EXPGUI [lindex $expgui(Revision) 1] [lindex $expmap(Revision) 1] ($expgui(changed) changes) -- [clock format [clock seconds]]"
655    # now save the file
656    expwrite $expgui(expfile)
657    set expgui(changed) 0
658    set expgui(expModifiedLast) [file mtime $expgui(expfile)]
659    set expgui(last_History) [string range [string trim [lindex [exphistory last] 1]] 0 50 ]
660    wm title . $expgui(expfile)
661    set expgui(titleunchanged) 1
662}
663
664# setup buttons for each phase on the phase page
665proc setphases {} {
666    global expgui expmap
667    eval destroy [winfo children $expgui(phaseFrame).top.ps]
668    pack [label $expgui(phaseFrame).top.ps.0 -text Phase:] -side left
669    foreach num $expmap(phaselist) type $expmap(phasetype) {
670        pack [button $expgui(phaseFrame).top.ps.$num -text $num \
671                -command "SelectOnePhase $num" -padx 1.5m] -side left
672        if {$type > 3} {
673            $expgui(phaseFrame).top.ps.$num config -state disabled
674        }
675    }
676    if {[file executable $expgui(exptool)] && \
677            [llength $expmap(phaselist)]} {
678        pack [button $expgui(phaseFrame).top.ps.10 -text "Add Phase" \
679                -padx 1.5m -command MakeAddPhaseBox] -side left
680    }
681}
682
683# Procedure to respond to changes the phase.
684#  This loads the "phases" widgets with data corresponding to the selected phase.
685proc SelectOnePhase {num} {
686    global entryvar entrycmd expmap expgui
687    set crsPhase {}
688    $expgui(atomxform) config -text "Xform Atoms" -state disabled
689    foreach n $expmap(phaselist) type $expmap(phasetype) {
690        if {$n == $num && $type <= 3} {
691            catch {$expgui(phaseFrame).top.ps.$num config -relief sunken}
692            set crsPhase $num
693            if {$type == 3} {
694                set expgui(phasetype) "Magnetic\nOnly"
695            } elseif {$type == 2} {
696                set expgui(phasetype) "Magnetic\n& Nuclear"
697            } elseif {$type == 4} {
698                # this is not used at present
699                set expgui(phasetype) "Macromolecular"
700            } else {
701                set expgui(phasetype) ""
702            }
703        } else {
704            catch {$expgui(phaseFrame).top.ps.$n config -relief raised}
705        }
706    }
707    # no phase is selected
708    if {$crsPhase == "" || [llength $expmap(phaselist)] == 0} {
709        # disable traces on entryvar
710        set entrycmd(trace) 0
711        set entrycmd(phasename) ""
712        set entryvar(phasename) ""
713        foreach ent {a b c alpha beta gamma cellref celldamp} {
714            set entrycmd($ent) ""
715            set entryvar($ent) ""
716        }
717        set expgui(curPhase) {}
718        # enable traces on entryvar
719        set entrycmd(trace) 1
720        $expgui(EditingAtoms) config -text ""
721        DisplayAtom 0 0
722        DisplayU 0 0
723        DisplayRefFlags 0 0
724        $expgui(atomlistbox) delete 0 end
725        return
726    }
727
728    set expgui(curPhase) $crsPhase
729    # we have a phase
730
731    # disable traces on entryvar for right now
732    set entrycmd(trace) 0
733
734    ##########################################################
735    ######   SECTION: ASSIGNMENT OF DATA VARIABLES  ##########
736    ##########################################################
737    # phase title
738    set entrycmd(phasename) "phaseinfo $crsPhase name"
739    set entryvar(phasename) [phaseinfo $crsPhase name]
740    # cell parameters & flags
741    foreach ent {a b c alpha beta gamma cellref celldamp} {
742        set entrycmd($ent) "phaseinfo $crsPhase $ent"
743        set entryvar($ent) [phaseinfo $crsPhase $ent]
744    }
745
746    #Procedure call: DisplayU -- Display Anisotropic/Isotropic widget or disable
747    # initialize to diasbled
748    DisplayAtom 0 0
749    DisplayU 0 0
750    DisplayRefFlags 0 0
751    $expgui(EditingAtoms) config -text ""
752
753    DisplayAllAtoms
754
755    # enable traces on entryvar now
756    set entrycmd(trace) 1
757}
758
759set expgui(noreenterDisplayAllAtoms) 0
760# Populate expgui(atomlistbox) (ScrolledListBox) with atoms from selected phase.
761proc DisplayAllAtoms {"mode reset"} {
762    global entryvar entrycmd expmap expgui
763    # if it does not show, dont bother
764    if {$expgui(pagenow) != "phaseFrame"} return
765    if {$expgui(curPhase) == ""} return
766    if $expgui(noreenterDisplayAllAtoms) return
767   
768    set expgui(noreenterDisplayAllAtoms) 1
769    if {$mode != "reset"} {
770        # save the scrolled position
771        set pos [lindex [$expgui(atomlistbox) yview] 0]
772    } else {
773        # this is a reset -- clear the selected atoms list
774        set expgui(selectedatomlist) {}
775    }
776    $expgui(atomlistbox) delete 0 end
777    # loop over atoms
778    set maxline I
779    set phase $expgui(curPhase)
780    set atomlist {}
781    set typehead "type  "
782    set namehead "  name  "
783    set multhead "Mult"
784    set coordhead "   "
785    set frachead "Occupancy"
786    if  {$expgui(asorttype) == "type"} {
787        # sort on atom type
788        set typehead "type* "
789        foreach atom $expmap(atomlist_$phase) {
790            lappend atomlist "$atom [atominfo $phase $atom type] $phase"
791        }
792        set expmap(atomlistboxcontents) [lsort -ascii -index 1 $atomlist]
793    } elseif {$expgui(asorttype) == "number"} {
794        # sort on atom number
795        set namehead "* name  "
796        foreach atom $expmap(atomlist_$phase) {
797            lappend atomlist "$atom $atom $phase"
798        }
799        set expmap(atomlistboxcontents) [lsort -integer -index 1 $atomlist]
800    } elseif {$expgui(asorttype) == "mult"} {
801        # sort on atom number
802        set multhead "Mlt*"
803        foreach atom $expmap(atomlist_$phase) {
804            lappend atomlist "$atom [atominfo $phase $atom mult] $phase"
805        }
806        set expmap(atomlistboxcontents) [lsort -integer -decreasing -index 1 $atomlist]
807    } elseif {$expgui(asorttype) == "occupancy"} {
808        # sort on atom number
809        set frachead "  Occup* "
810        foreach atom $expmap(atomlist_$phase) {
811            lappend atomlist "$atom [atominfo $phase $atom frac] $phase"
812        }
813        set expmap(atomlistboxcontents) [lsort -real -decreasing -index 1 $atomlist]
814    } elseif {$expgui(asorttype) == "x"} {
815        # sort on x
816        set coordhead "(x*)"
817        foreach atom $expmap(atomlist_$phase) {
818            lappend atomlist "$atom [atominfo $phase $atom x] $phase"
819        }
820        set expmap(atomlistboxcontents) [lsort -real -index 1 $atomlist]
821    } elseif {$expgui(asorttype) == "y"} {
822        # sort on y
823        set coordhead "(y*)"
824        foreach atom $expmap(atomlist_$phase) {
825            lappend atomlist "$atom [atominfo $phase $atom y] $phase"
826        }
827        set expmap(atomlistboxcontents) [lsort -real -index 1 $atomlist]
828    } elseif {$expgui(asorttype) == "z"} {
829        # sort on z
830        set coordhead "(z*)"
831        foreach atom $expmap(atomlist_$phase) {
832            lappend atomlist "$atom [atominfo $phase $atom z] $phase"
833        }
834        set expmap(atomlistboxcontents) [lsort -real -index 1 $atomlist]
835    } else {
836        error "Bad expgui(asorttype) = $expgui(asorttype)"
837    }
838
839    foreach tuple $expmap(atomlistboxcontents) {
840        set atom [lindex $tuple 0]
841        set phase [lindex $tuple 2]
842        set refflag {}
843        foreach type {x u f} {
844            if {[atominfo $phase $atom ${type}ref]} {
845                append refflag "[string toupper $type][atominfo $phase $atom ${type}damp] "
846            } else {
847                append refflag " [atominfo $phase $atom ${type}damp] "
848            }   
849        }
850        set line [format "%3d %-6s %-6s %8s %10.6f%10.6f%10.6f%4d%9.4f" \
851                $atom \
852                [atominfo $phase $atom label] \
853                [atominfo $phase $atom type] \
854                $refflag \
855                [atominfo $phase $atom x] \
856                [atominfo $phase $atom y] \
857                [atominfo $phase $atom z] \
858                [atominfo $phase $atom mult] \
859                [atominfo $phase $atom frac]
860        ]
861
862        # add temperature factors (iso/anoiso)
863        if {[atominfo $phase $atom temptype] == "A"} {
864            set maxline A
865            append line [format "  %9.5f%9.5f%9.5f%9.5f%9.5f%9.5f" \
866                    [atominfo $phase $atom U11] \
867                    [atominfo $phase $atom U22] \
868                    [atominfo $phase $atom U33] \
869                    [atominfo $phase $atom U12] \
870                    [atominfo $phase $atom U23] \
871                    [atominfo $phase $atom U13]
872            ]
873        } else {
874            append line [format "  %9.5f" \
875                    [atominfo $phase $atom Uiso]
876            ]
877        }
878        $expgui(atomlistbox) insert end $line
879    }
880    $expgui(atomtitle) delete 0 end
881    if {$maxline == "A"} {
882        $expgui(atomtitle) insert end [format "%10s %6s %8s%29s %9s  %s" \
883                $namehead $typehead "ref/damp  " \
884                "fractional coordinates$coordhead" \
885                "$multhead $frachead" \
886                " Uiso/Uij                                            "]
887    } else {
888        $expgui(atomtitle) insert end [format "%10s %6s %8s%29s %9s  %s" \
889                $namehead $typehead "ref/damp  " \
890                "fractional coordinates$coordhead" \
891                "$multhead $frachead" \
892                " Uiso"]
893    }
894    if {$mode != "reset"} {
895        # restore the selected items
896        foreach i $expgui(selectedatomlist) {
897            $expgui(atomlistbox) selection set $i
898        }
899        # restore the last scrolled position
900        $expgui(atomlistbox) yview moveto $pos
901    }
902    set expgui(noreenterDisplayAllAtoms) 0
903}
904
905# Procedure to select all atoms in response to a right-click
906proc SelectAllAtoms {} {
907    global expgui
908    $expgui(atomlistbox) selection set 0 end
909    # call editRecord in case trace was called before the selection was made
910    editRecord
911}
912
913# Procedure to respond to left mouse release in the atoms Pane
914proc editRecord { args } {
915    global entrycmd expgui
916    set expgui(selectedatomlist) [$expgui(atomlistbox) curselection]
917    # disable traces on entryvar for right now
918    set entrycmd(trace) 0
919
920    if {[llength $expgui(selectedatomlist)] == 0} {
921        if $expgui(debug) {error "Attempt display non-existent atoms"}
922    } elseif {[llength $expgui(selectedatomlist)] == 1} {
923        editOneRecord $expgui(selectedatomlist)
924    } else {
925        editMultipleRecords $expgui(selectedatomlist)
926    }
927    # reenable traces on entryvar
928    set entrycmd(trace) 1
929    # repaint the atoms box in case anything was changed
930    #    DisplayAllAtoms noreset
931}
932
933proc editOneRecord { AtomIndex } {
934    global expmap expgui
935    # get atom number & phase
936    set tuple [lindex $expmap(atomlistboxcontents) $AtomIndex]
937    set atomnum [lindex $tuple 0]
938    set p [lindex $tuple 2]
939    DisplayU $atomnum $p
940    DisplayAtom $atomnum $p
941    DisplayRefFlags $atomnum $p
942    $expgui(EditingAtoms) config -text "Editing atom #$atomnum -- [atominfo $p $atomnum label]"
943    $expgui(atomxform) config -text "Xform Atom" -state normal
944}
945
946# this will not work for a multi-phase list of atoms (yet)
947proc editMultipleRecords { AtomIndexList } {
948    global expmap expgui
949    set numberList {}
950    # current phase
951    set p $expgui(curPhase)
952    foreach AtomIndex $AtomIndexList {
953        # get atom number & phase
954        set tuple [lindex $expmap(atomlistboxcontents) $AtomIndex]
955        lappend numberList [lindex $tuple 0]
956#       set p [lindex $tuple 2]
957    }
958    # this needs to track by phase
959    $expgui(EditingAtoms) config -text \
960            "Set refinement options: atoms [CompressList $numberList]"
961    DisplayU 0 0
962    DisplayAtom 0 0
963    # this needs to track by phase
964    DisplayRefFlags $numberList $p
965    $expgui(atomxform) config -text "Xform Atoms" -state normal
966}
967
968# format a string of numbers to save space, e.g. "1 2 3 4 6 7 19 13 14 15"
969# becomes "1-4,6,7,13-15,19"
970proc CompressList {numberList} {
971    # format the number list to save space
972    set lastnum -99
973    set flist {}
974    set count 0
975    if [catch {set sortlist [lsort -integer $numberList]}] {return $numberList}
976    foreach num $sortlist {
977            set next [expr $lastnum+1]
978            if {$num != $next} {
979                if {$count == 0 && $flist != ""} {
980                    append flist ",$num"
981                } elseif {$count == 1 && $flist != ""} {
982                    append flist ",$lastnum,$num"
983                } elseif {$flist != ""} {
984                    append flist "-$lastnum,$num"
985            } else {
986                append flist "$num"
987            }
988            set lastnum $num
989            set count 0
990        } else {
991            incr count
992            incr lastnum
993        }
994    }
995    if {$count == 1 && $flist != ""} {
996        append flist ",$lastnum"
997    } elseif {$flist != "" && $count > 1} {
998        append flist "-$lastnum"
999    }
1000    return $flist
1001}
1002
1003# Procedure to display Isotropic or Anisotropic temperature factors
1004#  Changes the display to one entry widget for Isotropic motion OR
1005#   6 entry widgets for Anisotropic motion in Frame3.
1006#   or disables the widet entirly if atom = 0
1007proc DisplayU { atomnum p} {
1008    global expgui entryvar entrycmd
1009    if {$atomnum == 0} {
1010        set iOrA disable
1011    } else {
1012        set iOrA [atominfo $p $atomnum temptype]
1013    }
1014
1015    set firstbox [lindex $expgui(anisolabels) 0]
1016    if { $iOrA == "A" } {
1017        $firstbox config -text "U11 "
1018        foreach item $expgui(anisolabels) {
1019            $item config -fg black
1020        }
1021        foreach item $expgui(anisoentry) var {U11 U22 U33 U12 U13 U23} {
1022            set entrycmd($var) "atominfo $p $atomnum $var"
1023            set entryvar($var) [eval $entrycmd($var)]
1024            $item config -fg black -state normal  -bg white
1025        }
1026    } elseif { $iOrA == "I" || $iOrA == "disable"} {
1027        foreach item $expgui(anisolabels) {
1028#           $item config -fg grey
1029            $item config -fg beige
1030        }
1031        foreach item [lrange $expgui(anisoentry) 1 end] \
1032                var {U22 U33 U12 U13 U23} {
1033            set entrycmd($var) ""
1034            set entryvar($var) ""
1035            $item config -fg beige -bg beige  -state disabled
1036        }
1037        if { $iOrA == "disable"} {
1038            set entrycmd($var) ""
1039            set entryvar($var) ""
1040#           [lindex $expgui(anisoentry) 0] config -fg white -state disabled
1041            [lindex $expgui(anisoentry) 0] config -fg beige -bg beige -state disabled
1042        } else {
1043            set entrycmd(U11) "atominfo $p $atomnum Uiso"
1044            set entryvar(U11) [eval $entrycmd(U11)]
1045            $firstbox config -text Uiso -fg black
1046            [lindex $expgui(anisoentry) 0] config -fg black -bg white -state normal
1047        }
1048    }
1049}
1050
1051# need to think about multiple phases
1052
1053# Procedure to display refinement flags
1054proc DisplayRefFlags { atomnum p} {
1055    global expgui entryvar entrycmd
1056    if {$atomnum == 0} {
1057        foreach label $expgui(atomreflbl) {
1058            $label config -fg beige
1059        }
1060        foreach entry $expgui(atomref) {
1061            $entry config -state disabled -fg beige -bg beige
1062        }
1063        return
1064    }
1065    foreach label $expgui(atomreflbl) {
1066        $label config -fg black
1067    }
1068    foreach entry $expgui(atomref) {
1069        $entry config -state normal -fg black -bg beige
1070    }
1071    foreach var {xref uref fref xdamp udamp fdamp}  {
1072        set entrycmd($var) "atominfo $p [list $atomnum] $var"
1073        set entryvar($var) [eval $entrycmd($var)]
1074    }
1075}
1076
1077# Procedure to display an atom in the atom edit boxes
1078proc DisplayAtom { atomnum p} {
1079    global expgui entryvar entrycmd
1080    if {$atomnum == 0} {
1081        foreach label $expgui(atomlabels) {
1082            $label config -fg beige
1083        }
1084        foreach entry $expgui(atomentry) {
1085            $entry config -state disabled -fg beige -bg beige
1086        }
1087        return
1088    }
1089    foreach label $expgui(atomlabels) {
1090        $label config -fg black
1091    }
1092    foreach entry $expgui(atomentry) {
1093        $entry config -state normal -fg black -bg white
1094    }
1095    foreach var {x y z label frac } {
1096        set entrycmd($var) "atominfo $p $atomnum $var"
1097        set entryvar($var) [eval $entrycmd($var)]
1098    }
1099}
1100
1101# make a histogram box; used in MakeHistPane,
1102proc MakeHistBox {frm} {
1103    global expgui
1104    grid [label $frm.mode -text "Select a Histogram" \
1105            -bg beige -anchor center] \
1106            -row 0 -column 0 -columnspan 2 -sticky ew
1107    bind $frm.mode <Button-1> {
1108        set i [lsearch $expgui(AllowedHistSelectModes) $expgui(globalmode)]
1109        set expgui(globalmode) [lindex \
1110                "$expgui(AllowedHistSelectModes) \
1111                $expgui(AllowedHistSelectModes)" [incr i]]
1112        sethistlist
1113    }
1114    bind $frm.mode <Button-3> {set expgui(globalmode) 0; sethistlist}
1115    grid [listbox $frm.title -height 1 -relief flat \
1116            -exportselection 0 \
1117            -font $expgui(histfont) ] -row 1 -column 0 -sticky ew
1118    grid [listbox $frm.lbox -height 10 -width 25 \
1119            -exportselection 0 \
1120            -font $expgui(histfont) \
1121            -xscrollcommand "$frm.x set" \
1122            -yscrollcommand "$frm.y set" \
1123            ] -row 2 -column 0 -sticky news
1124    lappend expgui(HistSelectList) $frm
1125    grid [scrollbar $frm.x -orient horizontal \
1126            -command "move2boxes \" $frm.title $frm.lbox \" "
1127    ] -row 3 -column 0 -sticky ew
1128    grid [scrollbar $frm.y \
1129            -command "$frm.lbox yview"] \
1130            -row 2 -column 1 -sticky ns
1131    grid columnconfigure $frm 0 -weight 1
1132    grid rowconfigure $frm 2 -weight 1
1133}
1134
1135# update the histogram list
1136# to do: show histogram ref flags?
1137proc sethistlist {} {
1138    global expgui expmap
1139    array set lbl {
1140        1 "Select 1 or more\nTOF Histograms"
1141        2 "Select 1 or more\nCW Neutron Histograms"
1142        3 "Select 1 or more\nAlpha 1,2 X-ray Histograms"
1143        4 "Select 1 or more\nmonochromatic X-ray Histograms"
1144        5 "Select 1 or more Energy\nDisperive X-ray Histograms"
1145        6 "Select 1 or more of\n any type Histograms"
1146    }
1147    foreach lbox $expgui(HistSelectList) {
1148        $lbox.title delete 0 end
1149        $lbox.lbox delete 0 end
1150        if {$expgui(globalmode) != 0} {
1151            $lbox.lbox config -selectmode extended
1152            $lbox.mode config -text $lbl($expgui(globalmode)) -bg yellow
1153        } else {
1154            $lbox.lbox config -selectmode browse
1155            $lbox.mode config -text "Select a histogram" -bg beige
1156        }
1157    }
1158    # disable the unallowed pages in all mode
1159    if {$expgui(globalmode) == 6} {
1160        foreach pair $expgui(GlobalModeAllDisable) {
1161            if {$expgui(pagenow) == [lindex $pair 0]} {
1162                RaisePage lsFrame
1163            }
1164            eval [lindex $pair 1] -state disabled
1165        }
1166    } else {
1167        foreach pair $expgui(GlobalModeAllDisable) {
1168            eval [lindex $pair 1] -state normal
1169        }
1170    }
1171    set histlist {}
1172    if  {$expgui(hsorttype) == "type"} {
1173        # sort on histogram type
1174        foreach h [lsort -integer -increasing $expmap(powderlist)] {
1175            lappend histlist "$h [string range $expmap(htype_$h) 1 2]"
1176        }
1177        set expmap(histlistboxcontents) [lsort -ascii -index 1 $histlist]
1178    } elseif {$expgui(hsorttype) == "number"} {
1179        # sort on histogram number
1180        foreach h [lsort -integer -increasing $expmap(powderlist)] {
1181            lappend histlist "$h $h"
1182        }
1183        set expmap(histlistboxcontents) [lsort -integer -index 1 $histlist]
1184    } elseif {$expgui(hsorttype) == "bank"} {
1185        # sort on original bank number
1186        foreach h [lsort -integer -increasing $expmap(powderlist)] {
1187            lappend histlist "$h [histinfo $h bank]"
1188        }
1189        set expmap(histlistboxcontents) [lsort -integer -index 1 $histlist]
1190    } elseif {$expgui(hsorttype) == "angle"} {
1191        # sort on wavelength (CW) or angle (E disp.)
1192        foreach h [lsort -integer -increasing $expmap(powderlist)] {
1193            if {[string range $expmap(htype_$h) 2 2] == "T"} {
1194                set det [format %8.2f [histinfo $h tofangle]]
1195            } elseif {[string range $expmap(htype_$h) 2 2] == "C"} {
1196                set det [format %8.5f [histinfo $h lam1]]
1197            } elseif {[string range $expmap(htype_$h) 2 2] == "E"} {
1198                set det [format %8.2f [histinfo $h lam1]]
1199            } else {
1200                set det {}
1201            }
1202            lappend histlist "$h $det"
1203        }
1204        set expmap(histlistboxcontents) [lsort -real -index 1 $histlist]
1205    }
1206
1207    # title field needs to match longest title
1208    foreach lbox $expgui(HistSelectList) {
1209        $lbox.title insert end [format "%2s %s %4s %8s  %-67s" \
1210                "h#" \
1211                type \
1212                bank \
1213                "ang/wave" \
1214                "    title" \
1215                ]
1216    }
1217    foreach tuple $expmap(histlistboxcontents) {
1218        set h [lindex $tuple 0]
1219
1220        if {$expgui(globalmode) == 1} {
1221            if {[string range $expmap(htype_$h) 2 2] != "T"} continue
1222        } elseif {$expgui(globalmode) == 2} {
1223            if {[string range $expmap(htype_$h) 1 2] != "NC"} continue
1224        } elseif {$expgui(globalmode) == 3} {
1225            if {[string range $expmap(htype_$h) 1 2] != "XC" || \
1226                    [histinfo $h lam2] == 0.0} continue
1227        } elseif {$expgui(globalmode) == 4} {
1228            if {[string range $expmap(htype_$h) 1 2] != "XC" || \
1229                    [histinfo $h lam2] != 0.0} continue
1230        } elseif {$expgui(globalmode) == 5} {
1231            if {[string range $expmap(htype_$h) 1 2] != "XE"} continue
1232        }
1233
1234        if {[string range $expmap(htype_$h) 2 2] == "T"} {
1235            set det [format %8.2f [histinfo $h tofangle]]
1236        } elseif {[string range $expmap(htype_$h) 2 2] == "C"} {
1237            set det [format %8.5f [histinfo $h lam1]]
1238        } elseif {[string range $expmap(htype_$h) 2 2] == "E"} {
1239            set det [format %8.2f [histinfo $h lam1]]
1240        } else {
1241            set det {}
1242        }
1243        foreach lbox $expgui(HistSelectList) {
1244            $lbox.lbox insert end [format "%2d  %s  %4d %8s  %-67s" \
1245                    $h \
1246                    [string range $expmap(htype_$h) 1 2] \
1247                    [histinfo $h bank] \
1248                    $det \
1249                    [string range [histinfo $h title] 0 66] \
1250                    ]
1251        }
1252    }
1253    UpdateCurrentPage
1254}
1255
1256proc UpdateCurrentPage {} {
1257    global expgui
1258    foreach set $expgui(frameactionlist) {
1259        if {$expgui(pagenow) == [lindex $set 0]} {catch [lindex $set 1]}
1260    }
1261}
1262
1263#-----------------------------------------------------------------------
1264# ----------- draw Histogram page
1265#-----------------------------------------------------------------------
1266proc DisplayHistogram {} {
1267    global expgui entrycmd entryvar expmap
1268
1269    # trap if more than one histogram is selected unless global mode
1270    if {$expgui(globalmode) == 0 && [llength $expgui(curhist)] > 1} {
1271        set expgui(curhist) [lindex $expgui(curhist) 0]
1272    }
1273
1274    # display the selected histograms
1275    $expgui(histFrame).hs.lbox selection clear 0 end
1276    foreach h $expgui(curhist) {
1277        $expgui(histFrame).hs.lbox selection set $h
1278    }
1279
1280    # disable traces on entryvar for right now
1281    set entrycmd(trace) 0
1282
1283    # get histogram list
1284    set histlist {}
1285    foreach item $expgui(curhist) {
1286        lappend histlist [lindex $expmap(powderlist) $item]
1287    }
1288    # must have at least one histogram selected here
1289    if {[llength $histlist] == 0} {
1290        set expgui(backtermlbl) ""
1291        set expgui(backtypelbl) ""
1292        foreach var {bref bdamp} {
1293            set entrycmd($var) ""
1294            set entryvar($var) ""
1295        }
1296        $expgui(histFrame).top.txt config -text "No Selected Histograms"
1297        grid $expgui(histFrame).top -column 1 -row 0 -sticky nsew       
1298        set expgui(bkglbl) ""
1299        eval destroy [winfo children $expgui(diffBox)]
1300        set entrycmd(trace) 1
1301        return
1302    }
1303
1304    if {$expgui(globalmode) != 0} {
1305        set expgui(backtermlbl) ""
1306        set expgui(backtypelbl) ""
1307        foreach var {bref bdamp} {
1308            set entrycmd($var) "histinfo [list $histlist] $var"
1309            set entryvar($var) [histinfo [lindex $histlist 0] $var]
1310        }
1311    } else {
1312        set hist $histlist
1313        set terms [histinfo $hist backterms]
1314        set expgui(backtermlbl) "($terms terms)"
1315        set expgui(backtypelbl) "Function type [histinfo $hist backtype]"
1316        foreach var {bref bdamp} {
1317            set entrycmd($var) "histinfo $hist $var"
1318            set entryvar($var) [eval $entrycmd($var)]
1319        }
1320    }
1321    # Top box
1322    if {$expgui(globalmode) != 0} {
1323        if $expgui(haveBW) {
1324            catch {destroy $expgui(histFrame).pflag}
1325        }
1326        $expgui(histFrame).top.txt config \
1327                -text "Selected Histograms: [CompressList $histlist]"
1328        grid $expgui(histFrame).top -column 1 -row 0 -sticky nsew       
1329        set expgui(bkglbl) "Globally Edit Background"
1330    } else {
1331        grid forget $expgui(histFrame).top
1332        set expgui(bkglbl) "Edit Background"
1333        if $expgui(haveBW) {
1334            catch {destroy $expgui(histFrame).pflag}
1335            TitleFrame $expgui(histFrame).pflag  \
1336                    -borderwidth 4 -side left -relief groove \
1337                    -text "Phase Flags"
1338            set expgui(pflag) [$expgui(histFrame).pflag getframe]
1339            grid $expgui(histFrame).pflag -column 1 -row 1 -sticky nsew
1340            grid rowconfigure $expgui(histFrame) 2 -minsize 35
1341            foreach p $expmap(phaselist) {
1342                pack [checkbutton $expgui(pflag).$p \
1343                        -command "GetPhaseFlags $hist" \
1344                        -variable expgui(pflag$p) -text $p] -side left
1345                if {[lsearch $expmap(phaselist_$hist) $p] == -1} {
1346                    set expgui(pflag$p) 0
1347                } else {
1348                    set expgui(pflag$p) 1
1349                }
1350            }
1351        }
1352    }
1353
1354    # diffractometer constants
1355    foreach var {lam1 lam2 kratio pola ipola ddamp zero \
1356            wref pref dcref daref ratref ttref zref } {
1357        set entrycmd($var) "histinfo [list $histlist] $var"
1358        set entryvar($var) [histinfo [lindex $histlist 0] $var]
1359    }
1360
1361    eval destroy [winfo children $expgui(diffBox)]
1362    if {$expgui(globalmode) == 0} {
1363        if {[string range $expmap(htype_$hist) 2 2] == "T"} {
1364        #------
1365        # TOF |
1366        #------
1367            grid [ label $expgui(diffBox).lDCrc -text "Refine DIFC" ] \
1368                    -column 1 -row 1
1369            grid [ checkbutton $expgui(diffBox).rfDCrc -variable entryvar(dcref) ] \
1370                    -column 2 -row 1
1371            grid [ label $expgui(diffBox).lDCdifc -text DIFC ] \
1372                    -column 3 -row 1 -sticky w
1373            grid [ entry $expgui(diffBox).eDCdifc -textvariable entryvar(lam1) \
1374                    -width 15 ] -column 4 -row 1
1375            #
1376            grid [ label $expgui(diffBox).lDCra -text "Refine DIFA" ] \
1377                    -column 1 -row 2
1378            grid [ checkbutton $expgui(diffBox).rfDCra -variable entryvar(daref) ] \
1379                    -column 2 -row 2
1380            grid [ label $expgui(diffBox).lDCdifa -text DIFA ] \
1381                    -column 3 -row 2
1382            grid [ entry $expgui(diffBox).eDCdifa -textvariable entryvar(lam2) \
1383                    -width 15 ] -column 4 -row 2
1384            #
1385            grid [ label $expgui(diffBox).lDCzero -text "Zero"] \
1386                    -column 3 -row 3
1387            grid [ entry $expgui(diffBox).eDCzero -textvariable entryvar(zero) \
1388                    -width 15 ] -column 4 -row 3
1389            grid [ label $expgui(diffBox).lDCzref -text "Refine zero" ] \
1390                    -column 1 -row 3 -sticky w
1391            grid [ checkbutton $expgui(diffBox).rfDCzref \
1392                    -variable entryvar(zref) ] -column 2 -row 3
1393        } elseif {[string range $expmap(htype_$hist) 1 2] == "NC"} {
1394        #---------------
1395        # CW - neutron |
1396        #---------------
1397            grid [ label $expgui(diffBox).lDC1 -text "Refine wave" ] \
1398                    -column 1 -row 1
1399            grid [ checkbutton $expgui(diffBox).rfDC1 -variable entryvar(wref) ] \
1400                    -column 2 -row 1
1401            grid [ label $expgui(diffBox).lDCdifc -text wave ] \
1402                    -column 3 -row 1 -sticky w
1403            grid [ entry $expgui(diffBox).eDCdifc -textvariable entryvar(lam1) \
1404                    -width 15 ] -column 4 -row 1
1405            #
1406            grid [ label $expgui(diffBox).lDCzref -text "Refine zero" ] \
1407                    -column 1 -row 3 -sticky w
1408            grid [ checkbutton $expgui(diffBox).rfDCzref \
1409                    -variable entryvar(zref) ] -column 2 -row 3
1410            grid [ label $expgui(diffBox).lDCzero -text "Zero"] \
1411                    -column 3 -row 3
1412            grid [ entry $expgui(diffBox).eDCzero -textvariable entryvar(zero) \
1413                    -width 15 ] -column 4 -row 3
1414        } elseif {[string range $expmap(htype_$hist) 1 2] == "XC" && \
1415                [histinfo $hist lam2] == 0.0} {
1416        #--------------------------
1417        # CW - x-ray 1 wavelength |
1418        #--------------------------
1419            grid [ label $expgui(diffBox).lDC1 -text "Refine wave" ] \
1420                    -column 1 -row 1
1421            grid [ checkbutton $expgui(diffBox).rfDC1 -variable entryvar(wref) ] \
1422                    -column 2 -row 1
1423            grid [ label $expgui(diffBox).lDCdifc -text wave ] \
1424                    -column 3 -row 1 -sticky w
1425            grid [ entry $expgui(diffBox).eDCdifc -textvariable entryvar(lam1) \
1426                    -width 15 ] -column 4 -row 1
1427            #
1428            grid [ label $expgui(diffBox).lDCzref -text "Refine zero" ] \
1429                    -column 1 -row 3 -sticky w
1430            grid [ checkbutton $expgui(diffBox).rfDCzref \
1431                    -variable entryvar(zref) ] -column 2 -row 3
1432            grid [ label $expgui(diffBox).lDCzero -text "Zero"] \
1433                    -column 3 -row 3
1434            grid [ entry $expgui(diffBox).eDCzero -textvariable entryvar(zero) \
1435                    -width 15 ] -column 4 -row 3
1436            #
1437            grid [ label $expgui(diffBox).lDCpref -text "Refine POLA" ] \
1438                    -column 1 -row 4 -sticky w
1439            grid [ checkbutton $expgui(diffBox).rfDCpref \
1440                    -variable entryvar(pref) ] -column 2 -row 4
1441            grid [ label $expgui(diffBox).lDCpola -text POLA ] \
1442                    -column 3 -row 4
1443            grid [ entry $expgui(diffBox).eDCpola \
1444                    -textvariable entryvar(pola) -width 15 ] -column 4 -row 4
1445            grid [ label $expgui(diffBox).lDCipola -text "IPOLA" ] \
1446                    -column 5 -row 4
1447            grid [ entry $expgui(diffBox).eDCipola -width 2 \
1448                    -textvariable entryvar(ipola)] -column 6 -row 4
1449        } elseif {[string range $expmap(htype_$hist) 1 2] == "XC"} {
1450        #---------------------------
1451        # CW - x-ray 2 wavelengths |
1452        #---------------------------
1453            grid [ label $expgui(diffBox).lDCdifc -text wavelengths ] \
1454                    -column 3 -row 1 -sticky w
1455            grid [ entry $expgui(diffBox).eDCdifc -textvariable entryvar(lam1) \
1456                    -width 15 ] -column 4 -row 1
1457            grid [ entry $expgui(diffBox).eDCdifa -textvariable entryvar(lam2) \
1458                    -width 15 ] -column 5 -row 1
1459            #
1460            grid [ label $expgui(diffBox).lDCrref -text "Refine ratio" ] \
1461                    -column 1 -row 2 -sticky w
1462            grid [ checkbutton $expgui(diffBox).rfDCrref \
1463                    -variable entryvar(ratref) ] -column 2 -row 2
1464            grid [ label $expgui(diffBox).lDCratio -text Ratio ] \
1465                    -column 3 -row 2
1466            grid [ entry $expgui(diffBox).eDCkratio \
1467                    -textvariable entryvar(kratio) \
1468                    -width 15 ] -column 4 -row 2
1469            #
1470            grid [ label $expgui(diffBox).lDCzero -text "Zero"] \
1471                    -column 3 -row 3
1472            grid [ entry $expgui(diffBox).eDCzero -textvariable entryvar(zero) \
1473                    -width 15 ] -column 4 -row 3
1474            grid [ label $expgui(diffBox).lDCzref -text "Refine zero" ] \
1475                    -column 1 -row 3 -sticky w
1476            grid [ checkbutton $expgui(diffBox).rfDCzref \
1477                    -variable entryvar(zref) ] -column 2 -row 3
1478            grid [ label $expgui(diffBox).lDCpref -text "Refine POLA" ] \
1479                    -column 1 -row 4 -sticky w
1480            grid [ checkbutton $expgui(diffBox).rfDCpref \
1481                    -variable entryvar(pref) ] -column 2 -row 4
1482            grid [ label $expgui(diffBox).lDCpola -text POLA ] \
1483                    -column 3 -row 4
1484            grid [ entry $expgui(diffBox).eDCpola \
1485                    -textvariable entryvar(pola) -width 15 ] -column 4 -row 4
1486            grid [ label $expgui(diffBox).lDCipola -text "IPOLA" ] \
1487                    -column 5 -row 4
1488            grid [ entry $expgui(diffBox).eDCipola -width 2 \
1489                    -textvariable entryvar(ipola)] -column 6 -row 4
1490        } elseif {[string range $expmap(htype_$hist) 1 2] == "XE"} {
1491        #-------------
1492        # ED - x-ray |
1493        #-------------
1494            grid [ label $expgui(diffBox).lDC1 -text "Refine 2theta" ] \
1495                    -column 1 -row 1
1496            grid [ checkbutton $expgui(diffBox).rfDC1 -variable entryvar(ttref) ] \
1497                    -column 2 -row 1
1498            grid [ label $expgui(diffBox).lDCdifc -text 2Theta ] \
1499                    -column 3 -row 1 -sticky w
1500            grid [ entry $expgui(diffBox).eDCdifc -textvariable entryvar(lam1) \
1501                    -width 15 ] -column 4 -row 1
1502            #
1503            grid [ label $expgui(diffBox).lDCpref -text "Refine POLA" ] \
1504                    -column 1 -row 4 -sticky w
1505            grid [ checkbutton $expgui(diffBox).rfDCpref \
1506                    -variable entryvar(pref) ] -column 2 -row 4
1507            grid [ label $expgui(diffBox).lDCpola -text POLA ] \
1508                    -column 3 -row 4
1509            grid [ entry $expgui(diffBox).eDCpola \
1510                    -textvariable entryvar(pola) -width 15 ] -column 4 -row 4
1511            grid [ label $expgui(diffBox).lDCipola -text "IPOLA" ] \
1512                    -column 5 -row 4
1513            grid [ entry $expgui(diffBox).eDCipola -width 2 \
1514                    -textvariable entryvar(ipola)] -column 6 -row 4
1515        }
1516    } elseif {$expgui(globalmode) == 1} {
1517        #-------------
1518        # Global TOF |
1519        #-------------
1520        grid [ label $expgui(diffBox).lDCrc -text "Refine DIFC" ] \
1521                -column 1 -row 1
1522        grid [ checkbutton $expgui(diffBox).rfDCrc -variable entryvar(dcref) ] \
1523                -column 2 -row 1
1524        grid [button $expgui(diffBox).bDCdifc -text "Set DIFC Globally" \
1525                -command "editglobalparm histinfo difc {DIFC}"] -column 3 -row 1
1526        #
1527        grid [ label $expgui(diffBox).lDCra -text "Refine DIFA" ] \
1528                -column 1 -row 2
1529        grid [ checkbutton $expgui(diffBox).rfDCra -variable entryvar(daref) ] \
1530                -column 2 -row 2
1531        grid [ button $expgui(diffBox).bDCdifa -text "Set DIFA Globally" \
1532                -command "editglobalparm histinfo difa {DIFA}"] -column 3 -row 2
1533        #
1534        grid [ label $expgui(diffBox).lDCzref -text "Refine zero" ] \
1535                -column 1 -row 3 -sticky w
1536        grid [ checkbutton $expgui(diffBox).rfDCzref \
1537                -variable entryvar(zref) ] -column 2 -row 3
1538        grid [ button $expgui(diffBox).bDCzero -text "Set ZERO Globally" \
1539                -command "editglobalparm histinfo zero {Zero}"] -column 3 -row 3
1540    } elseif {$expgui(globalmode) == 2} {
1541        #--------------------
1542        # Global CW neutron |
1543        #--------------------
1544        grid [ label $expgui(diffBox).lDC1 -text "Refine wave" ] \
1545                -column 1 -row 1
1546        grid [ checkbutton $expgui(diffBox).rfDC1 -variable entryvar(wref) ] \
1547                -column 2 -row 1
1548        grid [button $expgui(diffBox).bDCdifc -text "Set Wave Globally" \
1549                -command "editglobalparm histinfo lam1 Wavelength"] \
1550                -column 3 -row 1
1551        #
1552        grid [ label $expgui(diffBox).lDCzref -text "Refine zero" ] \
1553                -column 1 -row 3 -sticky w
1554        grid [ checkbutton $expgui(diffBox).rfDCzref \
1555                -variable entryvar(zref) ] -column 2 -row 3
1556        grid [button $expgui(diffBox).bDCzero -text "Set Zero Globally" \
1557                -command "editglobalparm histinfo zero Zero"] -column 3 -row 3
1558    } elseif {$expgui(globalmode) == 4} {
1559        #----------------------
1560        # Global CW mono xray |
1561        #----------------------
1562        grid [ label $expgui(diffBox).lDC1 -text "Refine wave" ] \
1563                -column 1 -row 1
1564        grid [ checkbutton $expgui(diffBox).rfDC1 -variable entryvar(wref) ] \
1565                -column 2 -row 1
1566        grid [button $expgui(diffBox).bDCdifc -text "Set Wave Globally" \
1567                -command "editglobalparm histinfo lam1 Wavelength"] \
1568                -column 3 -row 1
1569        #
1570        grid [ label $expgui(diffBox).lDCzref -text "Refine zero" ] \
1571                -column 1 -row 3 -sticky w
1572        grid [ checkbutton $expgui(diffBox).rfDCzref \
1573                -variable entryvar(zref) ] -column 2 -row 3
1574        grid [button $expgui(diffBox).bDCzero -text "Set Zero Globally" \
1575                -command "editglobalparm histinfo zero Zero"] -column 3 -row 3
1576        #
1577        grid [ label $expgui(diffBox).lDCpref -text "Refine POLA" ] \
1578                -column 1 -row 4 -sticky w
1579        grid [ checkbutton $expgui(diffBox).rfDCpref \
1580                -variable entryvar(pref) ] -column 2 -row 4
1581        grid [button $expgui(diffBox).bDCpola -text "Set POLA Globally" \
1582                -command "editglobalparm histinfo pola POLA"] -column 3 -row 4
1583        grid [button $expgui(diffBox).bDCipola -text "Set IPOLA Globally" \
1584                -command "editglobalparm histinfo ipola IPOLA"] -column 4 -row 4
1585    } elseif {$expgui(globalmode) == 3} {
1586        #------------------------
1587        # Global alpha 1,2 xray |
1588        #------------------------
1589        grid [button $expgui(diffBox).bDCl1 -text "Set Wave1 Globally" \
1590                -command "editglobalparm histinfo lam1 {Wavelength 1}"] \
1591                -column 3 -row 1
1592        grid [button $expgui(diffBox).bDCl2 -text "Set Wave2 Globally" \
1593                -command "editglobalparm histinfo lam2 {Wavelength 2}"] \
1594                -column 4 -row 1
1595        #
1596        grid [ label $expgui(diffBox).lDCratref -text "Refine Ratio" ] \
1597                -column 1 -row 3 -sticky w
1598        grid [ checkbutton $expgui(diffBox).rfDCratref \
1599                -variable entryvar(ratref) ] -column 2 -row 3
1600        grid [button $expgui(diffBox).bDCrrat -text "Set Ratio Globally" \
1601                -command "editglobalparm histinfo ratio {Wavelength Ratio}"] \
1602                -column 3 -row 3
1603        #
1604        grid [ label $expgui(diffBox).lDCzref -text "Refine zero" ] \
1605                -column 1 -row 3 -sticky w
1606        grid [ checkbutton $expgui(diffBox).rfDCzref \
1607                -variable entryvar(zref) ] -column 2 -row 3
1608        grid [button $expgui(diffBox).bDCzero -text "Set Zero Globally" \
1609                -command "editglobalparm histinfo zero Zero"] -column 3 -row 3
1610        #
1611        grid [ label $expgui(diffBox).lDCpref -text "Refine POLA" ] \
1612                -column 1 -row 4 -sticky w
1613        grid [ checkbutton $expgui(diffBox).rfDCpref \
1614                -variable entryvar(pref) ] -column 2 -row 4
1615        grid [button $expgui(diffBox).bDCpola -text "Set POLA Globally" \
1616                -command "editglobalparm histinfo pola POLA"] -column 3 -row 4
1617        grid [button $expgui(diffBox).bDCipola -text "Set IPOLA Globally" \
1618                -command "editglobalparm histinfo ipola IPOLA"] -column 4 -row 4
1619    } elseif {$expgui(globalmode) == 5} {
1620        #-----------------
1621        # Global ED xray |
1622        #-----------------
1623        grid [ label $expgui(diffBox).lDC1 -text "Refine 2theta" ] \
1624                -column 1 -row 1
1625        grid [ checkbutton $expgui(diffBox).rfDC1 -variable entryvar(ttref) ] \
1626                -column 2 -row 1
1627        grid [button $expgui(diffBox).bDCdifc -text "Set 2Theta Globally" \
1628                -command "editglobalparm histinfo ratio {Fixed 2Theta}"] \
1629                -column 3 -row 1
1630        #
1631        grid [ label $expgui(diffBox).lDCpref -text "Refine POLA" ] \
1632                -column 1 -row 4 -sticky w
1633        grid [ checkbutton $expgui(diffBox).rfDCpref \
1634                -variable entryvar(pref) ] -column 2 -row 4
1635        grid [button $expgui(diffBox).bDCpola -text "Set POLA Globally" \
1636                -command "editglobalparm histinfo pola POLA"] -column 3 -row 4
1637        grid [button $expgui(diffBox).bDCipola -text "Set IPOLA Globally" \
1638                -command "editglobalparm histinfo ipola IPOLA"] -column 4 -row 4
1639    }
1640    if {$expgui(globalmode) == 0} {
1641        grid [frame $expgui(diffBox).d] -column 5 -row 5 \
1642                -columnspan 2 -sticky e
1643    } else {
1644        grid [frame $expgui(diffBox).d] -column 4 -row 5 \
1645                -columnspan 2 -sticky e
1646    }
1647    grid [label $expgui(diffBox).d.lDamp -text "Damping  "] \
1648            -column 1 -row 1
1649    tk_optionMenu $expgui(diffBox).d.om entryvar(ddamp) 0 1 2 3 4 5 6 7 8 9
1650    grid $expgui(diffBox).d.om -column 2 -row 1
1651    grid columnconfigure $expgui(diffBox) 9  -weight 1
1652    grid columnconfigure $expgui(diffBox) 0  -weight 1
1653    update idletasks
1654    # enable traces on entryvar now
1655    set entrycmd(trace) 1
1656}
1657
1658# this gets the phase flags as set in the expgui(pflag*) elements
1659# (linked to phase flag checkbuttons) and the sets the "HST xx NPHAS" flags
1660# accordingly using SetPhaseFlag
1661proc GetPhaseFlags {hist} {
1662    global expmap expgui
1663    set plist {}
1664    foreach p $expmap(phaselist) {
1665        if {$expgui(pflag$p)} {lappend plist $p}
1666    }
1667    SetPhaseFlag $hist $plist
1668    incr expgui(changed)
1669    mapexp
1670}
1671
1672#-----------------------------------------------------------------------
1673# populate the Scaling page
1674#-----------------------------------------------------------------------
1675proc DisplayFrac {} {
1676    global expgui entrycmd entryvar expmap
1677
1678    # trap if more than one histogram is selected unless global mode
1679    if {$expgui(globalmode) == 0 && [llength $expgui(curhist)] > 1} {
1680        set expgui(curhist) [lindex $expgui(curhist) 0]
1681    }
1682
1683    # display the selected histograms
1684    $expgui(fracFrame).hs.lbox selection clear 0 end
1685    foreach h $expgui(curhist) {
1686        $expgui(fracFrame).hs.lbox selection set $h
1687    }
1688
1689    # disable traces on entryvar
1690    set entrycmd(trace) 0
1691
1692    # get histogram list
1693    set histlist {}
1694    foreach item $expgui(curhist) {
1695        lappend histlist [lindex $expmap(powderlist) $item]
1696    }
1697
1698    # must have at least one histogram selected here
1699    if {[llength $histlist] == 0} {
1700        foreach var {scale sref sdamp} {
1701            set entrycmd($var) ""
1702            set entryvar($var) ""
1703        }
1704        set parm [grid info $expgui(scaleBox).but1]
1705        if {$parm != ""} {
1706            grid forget  $expgui(scaleBox).but1
1707            eval grid $expgui(scaleBox).ent1 $parm
1708        }
1709        # destroy the contents of the frame
1710        set phaseFractf1 $expgui(FracBox).f
1711        eval destroy [winfo children $phaseFractf1]
1712        # reenable traces on entryvar
1713        set entrycmd(trace) 1
1714        return
1715    }
1716
1717    #--------------
1718    # Scale factor
1719    #--------------
1720    if {$expgui(globalmode) != 0} {
1721        foreach var {scale sref sdamp} {
1722            set entrycmd($var) "histinfo [list $histlist] $var"
1723            set entryvar($var) [histinfo [lindex $histlist 0] $var]
1724        }
1725        set parm [grid info $expgui(scaleBox).ent1]
1726        if {$parm != ""} {
1727            grid forget  $expgui(scaleBox).ent1
1728            eval grid $expgui(scaleBox).but1 $parm
1729        }
1730    } else {
1731        set hist $histlist
1732        foreach var {scale sref sdamp} {
1733            set entrycmd($var) "histinfo $hist $var"
1734            set entryvar($var) [eval $entrycmd($var)]
1735        }
1736        set parm [grid info $expgui(scaleBox).but1]
1737        if {$parm != ""} {
1738            grid forget  $expgui(scaleBox).but1
1739            eval grid $expgui(scaleBox).ent1 $parm
1740        }
1741    }
1742
1743    #----------------
1744    # Phase Fractions
1745    #----------------
1746    set phaseFractf1 $expgui(FracBox).f
1747    # destroy the contents of the frame
1748    eval destroy [winfo children $phaseFractf1]
1749    if {$expgui(globalmode) != 0} {
1750        set txt "Phase Fractions for Histograms: [CompressList $histlist]"
1751    } else {
1752        set txt "Phase Fractions"
1753    }
1754    if $expgui(haveBW) {
1755        $expgui(fracFrame).f1.phaseFrac configure -text $txt
1756    } else {
1757        grid [label $phaseFractf1.txt -anchor center -text $txt] \
1758                -column 0 -row 0 -sticky news
1759    }
1760    # Create the frame inside the canvas, One frame for each Phase.
1761    foreach i {1 2 3 4 5 6 7 8 9} {set phasehistlist($i) ""}
1762    foreach hist $histlist {
1763        foreach i $expmap(phaselist_$hist) {
1764            lappend phasehistlist($i) $hist
1765        }
1766    }
1767    foreach i {1 2 3 4 5 6 7 8 9} {
1768        if {[llength $phasehistlist($i)] == 0} continue
1769        set framePF [frame $phaseFractf1.pF$i -relief groove  -bd 4]
1770        grid $framePF -column 0 -row $i -sticky ew
1771        # Label Heading for each phase.
1772        if {$expgui(globalmode) != 0} {
1773            grid [label $framePF.l1 \
1774                    -text "Phase $i Hist: [CompressList $phasehistlist($i)]"] \
1775                    -column 0 -row 0 -sticky nws
1776            grid [button $framePF.but1 -text "Set Globally" \
1777                    -command "editglobalparm hapinfo frac \"Phase $i Fraction\" \
1778                    [list $phasehistlist($i)] $i" \
1779                    ] -column 1 -row 0
1780        } else {
1781            grid [label $framePF.l1  -text "Phase $i"] \
1782                    -column 0 -row 0 -sticky nws
1783            grid [entry $framePF.ent -textvariable entryvar(frac$i) -width 15]\
1784                    -column 1 -row 0
1785        }
1786        set entrycmd(frac$i) "hapinfo $hist $i frac"
1787        set entryvar(frac$i) [hapinfo $hist $i frac]
1788        grid [label $framePF.l2  -text "  Refine"] \
1789                -column 2 -row 0 -sticky nws
1790        grid [checkbutton $framePF.cb -variable entryvar(frref$i)] \
1791                -column 3 -row 0 -sticky nws
1792        set entrycmd(frref$i) "hapinfo $hist $i frref"
1793        set entryvar(frref$i) [hapinfo $hist $i frref]
1794        grid [label $framePF.l3  -text "  Damping"] \
1795                -column 4 -row 0 -sticky nws
1796        tk_optionMenu $framePF.tkOptDamp entryvar(frdamp$i) \
1797                0 1 2 3 4 5 6 7 8 9     
1798        set entrycmd(frdamp$i) "hapinfo $hist $i frdamp"
1799        set entryvar(frdamp$i) [hapinfo $hist $i frdamp]
1800        grid $framePF.tkOptDamp -row 0 -sticky nsw -column 5
1801    }
1802    # resize the scroll window to match the actual
1803    update idletasks
1804    $expgui(FracBox) config -scrollregion [grid bbox $expgui(FracBox).f]
1805    $expgui(FracBox) config -width [lindex [grid bbox $expgui(FracBox).f] 2]
1806    update idletasks
1807    # enable traces on entryvar now
1808    set entrycmd(trace) 1
1809}
1810
1811#-----------------------------------------------------------------------
1812# display the profile page
1813#-----------------------------------------------------------------------
1814proc DisplayProfile {} {
1815    global expgui entrycmd entryvar expmap
1816
1817    # trap if more than one histogram is selected unless global mode
1818    if {$expgui(globalmode) == 0 && [llength $expgui(curhist)] > 1} {
1819        set expgui(curhist) [lindex $expgui(curhist) 0]
1820    }
1821    # display the selected histograms
1822    $expgui(profFrame).hs.lbox selection clear 0 end
1823    foreach h $expgui(curhist) {
1824        $expgui(profFrame).hs.lbox selection set $h
1825    }
1826
1827    # destroy the contents of the frame
1828    eval destroy [winfo children $expgui(ProfileBox).f]
1829
1830    if {$expgui(globalmode) == 0} {
1831        # must have at least one histogram selected here
1832        if {[llength $expgui(curhist)] == 0} return
1833        # disable traces on entryvar for right now
1834        set entrycmd(trace) 0
1835        set hist [lindex $expmap(powderlist) $expgui(curhist)]
1836        # no defined histograms?
1837        if {$hist == ""} return
1838        # Create one frame for each Phase.
1839        set ind -1
1840        set htype [string range $expmap(htype_$hist) 2 2]
1841        foreach i $expmap(phaselist_$hist) {
1842            incr ind
1843            # Label Heading for each phase.
1844            set ptype [string trim [hapinfo $hist $i proftype]]
1845            if {$expgui(haveBW)} {
1846                grid [TitleFrame $expgui(ProfileBox).f.$i \
1847                        -text "Hist $hist -- Phase $i (type $ptype)" \
1848                        -relief groove -bd 2] \
1849                        -column 0 -row $ind -sticky ew
1850                set ProfileFrame [$expgui(ProfileBox).f.$i getframe]
1851                grid [frame $ProfileFrame.1] \
1852                        -column 0 -row 0 -columnspan 10
1853                pack [label $ProfileFrame.1.l  \
1854                        -text Damping]\
1855                        -side left
1856            } else {
1857                grid [frame $expgui(ProfileBox).f.$i -relief groove -bd 4] \
1858                        -column 0 -row $ind -sticky ew
1859                set ProfileFrame $expgui(ProfileBox).f.$i
1860                grid [frame $ProfileFrame.1] \
1861                        -column 0 -row 0 -columnspan 10 -sticky ew
1862                pack [label $ProfileFrame.1.l  \
1863                        -text "Phase $i (type $ptype)    Damping"]\
1864                        -side left
1865            }
1866            tk_optionMenu $ProfileFrame.1.tkOptDamp entryvar(pdamp_$i) \
1867                    0 1 2 3 4 5 6 7 8 9
1868            set entrycmd(pdamp_$i) "hapinfo $hist $i pdamp"
1869            set entryvar(pdamp_$i) [hapinfo $hist $i pdamp]
1870            pack $ProfileFrame.1.tkOptDamp -side left
1871            pack [label $ProfileFrame.1.l1 \
1872                    -text "  Peak cutoff"]\
1873                    -side left
1874            pack [entry $ProfileFrame.1.e1  \
1875                    -width 10 -textvariable entryvar(pcut_$i)]\
1876                    -side left
1877            set entrycmd(pcut_$i) "hapinfo $hist $i pcut"
1878            set entryvar(pcut_$i) [hapinfo $hist $i pcut]
1879
1880            pack [button $ProfileFrame.1.b1  \
1881                    -text "Change Type" \
1882                    -command "ChangeProfileType $hist $i"]\
1883                    -side left
1884           
1885            set col -1
1886            set row 1
1887            set nterms [hapinfo $hist $i profterms]
1888            set lbls "dummy [GetProfileTerms $i $hist $ptype]"
1889            for { set num 1 } { $num <= $nterms } { incr num } {
1890                set term {}
1891                catch {set term [lindex $lbls $num]}
1892                if {$term == ""} {set term $num}
1893                incr col
1894                grid [label $ProfileFrame.l${num}_${i} -text "$term"] \
1895                        -row $row -column $col
1896                incr col
1897                grid [checkbutton $ProfileFrame.ref${num}_${i} \
1898                        -variable entryvar(pref${num}_$i)] -row $row -column $col
1899                set entrycmd(pref${num}_$i) "hapinfo $hist $i pref$num"
1900                set entryvar(pref${num}_$i) [hapinfo $hist $i pref$num]
1901                incr col
1902                grid [entry $ProfileFrame.ent${num}_${i} \
1903                        -textvariable entryvar(pterm${num}_$i)\
1904                        -width 12] -row $row -column $col
1905                set entrycmd(pterm${num}_$i) "hapinfo $hist $i pterm$num"
1906                set entryvar(pterm${num}_$i) [hapinfo $hist $i pterm$num]
1907                if {$col > 6} {set col -1; incr row}
1908            }
1909        }
1910        grid columnconfigure $expgui(ProfileBox).f 0 -weight 1
1911    } else {
1912        # get histogram list
1913        set histlist {}
1914        foreach item $expgui(curhist) {
1915            lappend histlist [lindex $expmap(powderlist) $item]
1916        }
1917        # must have at least one histogram selected here
1918        if {[llength $histlist] == 0} return
1919        # disable traces on entryvar for right now
1920        set entrycmd(trace) 0
1921        # loop through histograms & phases, set up an array by phase & profile type
1922        catch {unset prtyparray histarray phasearray}
1923        foreach hist $histlist {
1924            foreach phase $expmap(phaselist_$hist) {
1925                set prtyp [string trim [hapinfo $hist $phase proftype]]
1926                set key ${prtyp}_$phase
1927                lappend prtyparray($key) $hist
1928                lappend histarray($key) $hist
1929                lappend phasearray($key) $phase
1930            }
1931        }
1932       
1933        set ptype ""
1934        set i -1
1935        # loop over all combined phases and profile types, sorted 1st by profile number
1936        foreach key [lsort [array names prtyparray]] {
1937            # split key
1938            scan $key %d_%d prftyp p
1939
1940            if {$ptype != $prftyp || !$expgui(globalphasemode)} {
1941                set ptype $prftyp
1942                set curhistlist $histarray($key)
1943                set curphaslist $phasearray($key)
1944               
1945                set hist1 [lindex $curhistlist 0]
1946                set phase1 [lindex $curphaslist 0]
1947                set nterms [hapinfo $hist1 $phase1 profterms]
1948                set htype [string range $expmap(htype_$hist1) 2 2]
1949                set lbls "dummy [GetProfileTerms $phase1 $hist1 $ptype]"
1950                # Create a frame for this type
1951                incr i
1952                set boxtitle "Phase $p, hist [CompressList $histarray($key)]"
1953                if {$expgui(haveBW)} {
1954                    grid [TitleFrame $expgui(ProfileBox).f.$i \
1955                            -text "(type $ptype)" \
1956                            -relief groove -bd 2] \
1957                            -column 0 -row $i -sticky ew
1958                    set ProfileFrame [$expgui(ProfileBox).f.$i getframe]
1959                    grid [frame $ProfileFrame.0] \
1960                            -column 0 -row 0 -columnspan 20 -sticky ew
1961                } else {
1962                    grid [frame $expgui(ProfileBox).f.$i \
1963                            -relief groove -bd 4] \
1964                            -column 0 -row $i -sticky ew
1965                    set ProfileFrame $expgui(ProfileBox).f.$i
1966                    grid [frame $ProfileFrame.0] \
1967                            -column 0 -row 0 -columnspan 20 -sticky ew
1968                    grid [label $ProfileFrame.0.0  \
1969                            -text "Profile Type $ptype   "] -row 0 -column 0
1970                }
1971                grid [label $ProfileFrame.0.1  \
1972                        -anchor w] -row 0 -column 1
1973                grid [frame $ProfileFrame.1] \
1974                        -column 0 -row 1 -columnspan 20 -sticky ew
1975                grid [label $ProfileFrame.1.2  \
1976                        -text "Damping"] -row 0 -column 2
1977                tk_optionMenu $ProfileFrame.1.tkOptDamp \
1978                        entryvar(pdamp_$i) 0 1 2 3 4 5 6 7 8 9
1979                grid $ProfileFrame.1.tkOptDamp -row 0 -column 3
1980                grid [button $ProfileFrame.1.edit \
1981                        -text "Global Edit"] -row 0 -column 4 -sticky w
1982                set entryvar(pdamp_$i) [hapinfo $hist $phase pdamp]
1983                grid [button $ProfileFrame.1.b1 -text "Change Type"] \
1984                        -row 0 -column 5 -sticky w
1985                set col -1
1986                set row 2
1987                for { set num 1 } { $num <= $nterms } { incr num } {
1988                    set term {}
1989                    catch {set term [lindex $lbls $num]}
1990                    if {$term == ""} {set term $num}
1991                    incr col
1992                    grid [label $ProfileFrame.l${num}_${i} \
1993                            -text "$term"] -row $row -column $col
1994                    incr col
1995                    grid [checkbutton $ProfileFrame.ref${num}_${i} \
1996                            -variable entryvar(pref${num}_$i)] \
1997                            -row $row -column $col
1998                    set entryvar(pref${num}_$i) [hapinfo $hist $phase pref$num]
1999                    if {$col > 10} {set col -1; incr row}
2000                }
2001                grid columnconfigure $expgui(ProfileBox).f 0 -weight 1
2002            } else {
2003                # add to the current entry
2004                eval lappend curhistlist $histarray($key)
2005                eval lappend curphaslist $phasearray($key)
2006                append boxtitle "\nPhase $p, hist [CompressList $histarray($key)]"
2007            }
2008            $ProfileFrame.0.1 config -text $boxtitle
2009            $ProfileFrame.1.edit config -command "\
2010                    EditProfile \"\n$boxtitle\" \
2011                    [list $curhistlist] \
2012                    [list $curphaslist]"
2013            $ProfileFrame.1.b1 config -command "ChangeProfileType \
2014                    [list $curhistlist] [list $curphaslist]"
2015            set entrycmd(pdamp_$i) "hapinfo \
2016                    [list $curhistlist] \
2017                    [list $curphaslist] pdamp"
2018            for { set num 1 } { $num <= $nterms } { incr num } {
2019                set entrycmd(pref${num}_$i) "hapinfo \
2020                        [list $curhistlist] \
2021                        [list $curphaslist] pref$num"
2022            }
2023        }
2024    }
2025   
2026    # resize the scroll window to match the actual
2027    update idletasks
2028    $expgui(ProfileBox) config -scrollregion [grid bbox $expgui(ProfileBox).f]
2029    $expgui(ProfileBox) config -width [lindex [grid bbox $expgui(ProfileBox).f] 2]
2030    update idletasks
2031    ResizeNotebook
2032    # enable traces on entryvar now
2033    set entrycmd(trace) 1
2034}
2035
2036# process the bit settings in the print options
2037#   bitnum -- the number of the bit to be tested/set starting at 0 for the LSBit
2038proc printsetting {bitnum "action get" "value {}"} {
2039    global entryvar expgui
2040    if {$action == "get"} {
2041        return [expr ([expinfo print] & int(pow(2,$bitnum))) != 0]
2042    } elseif $value {
2043        set newval [expr ([expinfo print] | int(pow(2,$bitnum)))]
2044    } else {
2045        set newval [expr ([expinfo print] & ~int(pow(2,$bitnum)))]
2046    }
2047    expinfo print set $newval
2048    set expgui(printopt) "Print Options ([expinfo print])"
2049}
2050
2051# need to respond to mouse presses -- control variable associated with extract Fobs
2052# and set the LeBail extraction flags
2053proc SetupExtractHist {} {
2054    global expgui entrycmd entryvar expmap
2055
2056    # display the selected histograms
2057    $expgui(lsFrame).hs.lbox selection clear 0 end
2058    foreach h $expgui(curhist) {
2059        $expgui(lsFrame).hs.lbox selection set $h
2060    }
2061
2062    # get histogram list
2063    set histlist {}
2064    foreach item $expgui(curhist) {
2065        set hist [lindex $expmap(powderlist) $item]
2066        if {$hist != ""} {lappend histlist $hist}
2067    }
2068    set entrycmd(fobsextract) "histinfo [list $histlist] foextract"
2069    if {[llength $histlist] == 0 || [string trim $histlist] == ""} {
2070        foreach phase {1 2 3 4 5 6 7 8 9} {
2071            $expgui(FobsExtractFrame).l$phase config -fg grey
2072            set expgui(Fextract$phase) {}
2073            foreach item $expgui(ExtractSettingsRadiobuttons) {
2074                ${item}$phase config -state disabled -bd 1
2075            }
2076        }
2077    } elseif {[llength $histlist] == 1} {
2078        # disable traces on entryvar
2079        set entrycmd(trace) 0
2080        set entryvar(fobsextract) [histinfo $histlist foextract]
2081        foreach phase {1 2 3 4 5 6 7 8 9} {
2082            # is the phase present?
2083            if {[lsearch -exact $expmap(phaselist_$histlist) $phase] == -1} {
2084                $expgui(FobsExtractFrame).l$phase config -fg grey
2085                set expgui(Fextract$phase) {}
2086                foreach item $expgui(ExtractSettingsRadiobuttons) {
2087                    ${item}$phase config -state disabled -bd 1
2088                }
2089            } else {
2090                $expgui(FobsExtractFrame).l$phase config -fg black
2091                foreach item $expgui(ExtractSettingsRadiobuttons) {
2092                    ${item}$phase config -state normal -bd 2
2093                }
2094                set expgui(Fextract$phase) [hapinfo $histlist $phase extmeth]
2095            }
2096        }
2097    } elseif {[llength $histlist] > 1} {
2098        # disable traces on entryvar
2099        set entrycmd(trace) 0
2100        # multiple histograms need phases in any histogram
2101        foreach phase {1 2 3 4 5 6 7 8 9} {
2102            set gotphase($phase) 0
2103        }           
2104        foreach hist $histlist {
2105            foreach phase $expmap(phaselist_$hist) {
2106                set gotphase($phase) 1
2107            }
2108        }
2109        foreach phase {1 2 3 4 5 6 7 8 9} {
2110            set expgui(Fextract$phase) {}
2111            if $gotphase($phase) {
2112                $expgui(FobsExtractFrame).l$phase config -fg black
2113                foreach item $expgui(ExtractSettingsRadiobuttons) {
2114                    ${item}$phase config -state normal -bd 2
2115                }
2116            } else {
2117                $expgui(FobsExtractFrame).l$phase config -fg grey
2118                foreach item $expgui(ExtractSettingsRadiobuttons) {
2119                    ${item}$phase config -state disabled -bd 1
2120                }
2121            }
2122        }
2123    }
2124    # reenable traces
2125    set entrycmd(trace) 1
2126}
2127# respond to a change in the fobs extraction method for a phase
2128# force the main extraction flag on, if fobs extraction is selected for any phase
2129proc HistExtractSet {phase} {
2130    global expgui entryvar expmap
2131    foreach item $expgui(curhist) {
2132        lappend histlist [lindex $expmap(powderlist) $item]
2133    }
2134    hapinfo $histlist $phase extmeth set $expgui(Fextract$phase)
2135    incr expgui(changed)
2136    if {$expgui(Fextract$phase) != 0} {set entryvar(fobsextract) 1}
2137}
2138#---------------------------- Global Edit Functions ------------------------
2139proc editbackground {} {
2140    global expgui expmap entrycmd
2141    set histlist {}
2142    foreach n $expgui(curhist) {
2143        lappend histlist [lindex $expmap(powderlist) $n]
2144    }
2145    if {[llength $histlist] == 0} return
2146
2147    set w .back
2148    catch {destroy $w}
2149    toplevel $w -bg beige
2150    if {$expgui(globalmode) != 0} {
2151        wm title $w "Global Edit Background"
2152    } else {
2153        wm title $w "Edit Background"
2154    }
2155   
2156    pack [frame $w.0 -bd 6 -relief groove  -bg beige \
2157            ] -side top -expand yes -fill both
2158    if {[llength $histlist] > 1} {
2159        grid [label $w.0.a \
2160            -text "Setting background terms for histograms [CompressList $histlist]" \
2161            -bg beige] -row 0 -column 0 -columnspan 10
2162    } else {
2163        grid [label $w.0.a \
2164            -text "Setting background terms for histogram $histlist" \
2165            -bg beige] -row 0 -column 0 -columnspan 10
2166    }
2167    set hist [lindex $histlist 0]
2168    grid [label $w.0.b -text "Function type"  -bg beige]  -row 1 -column 0
2169
2170    # disable traces on  expgui(backtype) & expgui(backterms) now
2171    set entrycmd(trace) 0
2172
2173    # number of terms
2174    set expgui(backtype) [histinfo $hist backtype]
2175    set expgui(orig_backtype) $expgui(backtype)
2176    set expgui(prev_backtype) $expgui(backtype)
2177    set typemenu [tk_optionMenu $w.0.type expgui(backtype) null]
2178    $typemenu delete 0 end
2179    foreach item {
2180        "1 - Shifted Chebyshev"
2181        "2 - Cosine Fourier series"
2182        "3 - Radial distribution peaks"
2183        "4 - Power series in Q**2n/n!"
2184        "5 - Power series in n!/Q**2n"
2185        "6 - Power series in Q**2n/n! and n!/Q**2n"
2186        "7 - Linear interpolation function"
2187        "8 - Reciprocal interpolation function"
2188    } {
2189        set val [lindex $item 0]
2190        $typemenu insert end radiobutton -variable expgui(backtype) \
2191                -label $item -value $val
2192    }
2193
2194    grid $w.0.type   -row 1 -column 1
2195    grid [label $w.0.c -text "  Number of terms"  -bg beige] -row 1 -column 2
2196
2197    # function type
2198    set expgui(backterms) [histinfo $hist backterms]
2199    set expgui(orig_backterms) $expgui(backterms)
2200    set list {}; for {set i 1} {$i <= 36} {incr i} {lappend list $i}
2201    eval tk_optionMenu $w.0.terms expgui(backterms) $list
2202    grid $w.0.terms   -row 1 -column 3
2203    # enable traces on  expgui(backtype) & expgui(backterms) now
2204    set entrycmd(trace) 1
2205
2206    #set background terms
2207    for {set num 1 } { $num <= 36 } { incr num } {
2208        set var "bterm$num"
2209        set expgui($var) {}
2210        set expgui(orig_$var) {}
2211    }
2212    if {[llength $histlist] == 1} {
2213        for {set num 1 } { $num <= $expgui(backterms) } { incr num } {
2214            set var "bterm$num"
2215            set expgui($var) [histinfo $histlist $var]
2216            set expgui(orig_$var) $expgui($var)
2217        }
2218    }
2219
2220    pack [frame $w.1 -bd 6 -relief groove  -bg beige] -side top \
2221            -expand yes -fill both
2222    ShowBackTerms $w.1
2223
2224    set expgui(temp) {}
2225    pack [frame $w.b] -side top
2226    pack [button $w.b.2 -text Set -command "destroy $w"] -side left
2227    pack [button $w.b.3 -text Quit \
2228            -command "QuitEditBackground $w"] -side left
2229    bind $w <Return> "destroy $w"
2230
2231    # force the window to stay on top
2232    putontop $w
2233
2234    focus $w.b.2
2235    tkwait window $w
2236    afterputontop
2237
2238    if {$expgui(temp) != ""} return
2239
2240    if {$expgui(orig_backtype) != $expgui(backtype)} {
2241        histinfo $histlist backtype set $expgui(backtype)
2242        incr expgui(changed)
2243    }
2244    if {$expgui(orig_backterms) != $expgui(backterms)} {
2245        histinfo $histlist backterms set $expgui(backterms)
2246        incr expgui(changed)
2247    }
2248    for {set num 1 } { $num <= $expgui(backterms) } { incr num } {
2249        set var "bterm$num"
2250        if {$expgui(orig_$var) != $expgui($var)} {
2251            histinfo $histlist $var set $expgui($var)
2252            incr expgui(changed)
2253        }
2254    }
2255
2256    if {$expgui(globalmode) == 0} {
2257        set expgui(backtypelbl) "Function type [histinfo $hist backtype]"
2258        set expgui(backtermlbl) "([histinfo $hist backterms] terms)"
2259    }
2260}
2261
2262trace variable expgui(backterms) w ChangeBackTerms
2263proc ChangeBackTerms {a b c} {
2264    global entrycmd expgui
2265    if !$entrycmd(trace) return
2266    ShowBackTerms .back.1
2267}
2268
2269trace variable expgui(backtype) w ChangeBackType
2270# reset the terms to 1, 0, 0... when the number of terms increase
2271proc ChangeBackType {a b c} {
2272    global entrycmd expgui
2273    if !$entrycmd(trace) return
2274    if {$expgui(prev_backtype) == $expgui(backtype)} return
2275    set expgui(prev_backtype) $expgui(backtype)
2276    for {set num 1 } { $num <= $expgui(backterms) } { incr num } {
2277        set var "bterm$num"
2278        if {$num == 1} {
2279            set expgui($var) 1.0
2280        } else {
2281            set expgui($var) 0.0
2282        }
2283    }
2284}
2285
2286proc ShowBackTerms {w } {
2287    global expgui expmap
2288    # destroy the contents of the frame
2289    eval destroy [winfo children $w]
2290    set histlist {}
2291    foreach n $expgui(curhist) {
2292        lappend histlist [lindex $expmap(powderlist) $n]
2293    }
2294    set widgetsPerRow 4
2295    for {set rows 2; set num 1 } { $num <= $expgui(backterms) } { incr rows } {
2296        for {set cols 0} { (2*$widgetsPerRow > $cols) && ($num <= $expgui(backterms)) }  { incr num }  {
2297            set var "bterm$num"
2298            grid [label $w.l$num -text $num -bg beige]  \
2299                    -row $rows -column $cols -sticky nes
2300            incr cols
2301            grid [entry $w.e$num -width 15 -textvariable expgui($var) \
2302                    ] -row $rows  -column $cols  -sticky news
2303            incr cols
2304        }
2305    }
2306}
2307
2308proc QuitEditBackground {w} {
2309    global expgui
2310    # lets find out if anything changed
2311    set changed 0
2312    if {$expgui(orig_backtype) != $expgui(backtype)} {
2313        set changed 1
2314    }
2315    if {$expgui(orig_backterms) != $expgui(backterms)} {
2316        set changed 1
2317    }
2318    for {set num 1 } { $num <= $expgui(backterms) } { incr num } {
2319        set var "bterm$num"
2320        if {$expgui(orig_$var) != $expgui($var)} {
2321            set changed 1
2322            break
2323        }
2324    }
2325    if $changed {
2326        set decision [tk_dialog .changes "Abandon Changes" \
2327                "You have made changes to the background. Ok to abandon changes?" \
2328                warning 0 Abandon Keep]
2329        if !$decision {
2330            set expgui(temp) "Quit"
2331            destroy $w
2332        }
2333    } else {
2334        set expgui(temp) "Quit"
2335        destroy $w
2336    }
2337}
2338
2339# this probably needs work
2340proc editglobalparm {cmd variable title "histlist {}" "phase {}"} {
2341    global expgui expmap
2342    set w .global
2343    catch {destroy $w}
2344    toplevel $w -bg beige
2345    wm title $w "Edit Global Parameter"
2346    set expgui(temp) {}
2347    if {[llength $histlist] == 0} {
2348        set hist {}
2349        foreach n $expgui(curhist) {
2350            lappend hist [lindex $expmap(powderlist) $n]
2351        }
2352    } else {
2353        set hist $histlist
2354    }
2355    pack [frame $w.0 -bd 6 -relief groove -bg beige] \
2356            -side top -expand yes -fill both
2357    grid [label $w.0.a -text "Setting $title for histograms [CompressList $hist]"\
2358            -bg beige] \
2359            -row 0 -column 0 -columnspan 10
2360    grid [entry $w.0.b -textvariable expgui(temp)] \
2361            -row 1 -column 0
2362
2363
2364    pack [frame $w.b] -side top
2365    pack [button $w.b.2 -text Set -command "destroy $w"] -side left
2366    pack [button $w.b.3 -text Quit -command "set expgui(temp) {}; destroy $w"] -side left
2367    bind $w <Return> "destroy $w"
2368
2369    # force the window to stay on top
2370    putontop $w
2371    focus $w.b.2
2372    tkwait window $w
2373    afterputontop
2374
2375    if {$expgui(temp) != ""} {
2376        foreach h $hist {
2377            if {$cmd == "histinfo"} {
2378                histinfo $h $variable set $expgui(temp)
2379                incr expgui(changed)
2380                if $expgui(debug) {
2381                    puts "histinfo $h $variable set $expgui(temp)"
2382                }
2383            } elseif {$cmd == "hapinfo"} {
2384                hapinfo $h $phase $variable set $expgui(temp)
2385                incr expgui(changed)
2386                if $expgui(debug) {
2387                    puts "hapinfo $phase $h $variable set $expgui(temp)"
2388                }
2389            } else {
2390                error "$cmd unimplemented"
2391            }
2392        }
2393    }
2394}
2395
2396proc EditProfile {title histlist phaselist} {
2397    global expgui expmap entrycmd
2398    set w .back
2399    catch {destroy $w}
2400    toplevel $w -bg beige
2401    wm title $w "Global Edit Profile"
2402    set hist [lindex $histlist 0]
2403    set phase [lindex $phaselist 0]
2404    set ptype [string trim [hapinfo $hist $phase proftype]]
2405    set htype [string range $expmap(htype_$hist) 2 2]
2406    set nterms [hapinfo $hist $phase profterms]
2407   
2408    pack [frame $w.0 -bd 6 -relief groove  -bg beige \
2409            ] -side top -expand yes -fill both
2410    grid [label $w.0.a \
2411            -text "Setting profile terms: $title" \
2412            -bg beige] -row 0 -column 0 -columnspan 10
2413    grid [label $w.0.b -text "Function type $ptype"  -bg beige]  -row 1 -column 0
2414    grid [label $w.0.c -text "  Peak cutoff" -bg beige] -row 1 -column 3
2415    grid [entry $w.0.d -width 10 ]  -row 1 -column 4
2416    set entrylist {}
2417    lappend entrylist "pcut $w.0.d"
2418
2419    set col -1
2420    set row 1
2421    set lbls "dummy [GetProfileTerms $phase $hist $ptype]"
2422    pack [frame $w.1 -bd 6 -relief groove  -bg beige \
2423            ] -side top -expand yes -fill both
2424    for { set num 1 } { $num <= $nterms } { incr num } {
2425        set term {}
2426        catch {set term [lindex $lbls $num]}
2427        if {$term == ""} {set term $num}
2428        incr col
2429        grid [label $w.1.l${num} -text "$term" -bg beige] \
2430                -row $row -column $col
2431        incr col
2432        grid [entry $w.1.ent${num} \
2433                -width 14] -row $row -column $col
2434        lappend entrylist "pterm$num $w.1.ent${num}"   
2435        if {$col > 6} {set col -1; incr row}
2436    }
2437    pack [frame $w.b] -side top
2438    pack [button $w.b.2 -text Set \
2439            -command "SetEditProfile [list $entrylist] [list $phaselist] \
2440            [list $histlist] $w"] -side left
2441    pack [button $w.b.3 -text Quit \
2442            -command "QuitEditProfile $w [list $entrylist]"] -side left
2443    bind $w <Return> "destroy $w"
2444
2445    # force the window to stay on top
2446    putontop $w
2447    focus $w.b.2
2448    tkwait window $w
2449    afterputontop
2450}
2451
2452proc SetEditProfile {entrylist phaselist histlist w} {
2453    global expgui
2454    foreach item $entrylist {
2455        set value [ [lindex $item 1] get ]
2456        if {$value != ""} {
2457            hapinfo $histlist $phaselist [lindex $item 0] set $value
2458            incr expgui(changed)
2459            if $expgui(debug) {
2460                puts "hapinfo [list $phaselist] [list $histlist] [lindex $item 0] set $value"
2461            }
2462        }
2463    }
2464    destroy $w
2465}
2466
2467proc QuitEditProfile {w entrylist} {
2468    global expgui
2469    # lets find out if anything changed
2470    set changed 0
2471    foreach item $entrylist {
2472        if {[ [lindex $item 1] get ] != ""} {set changed 1; break}
2473    }
2474    if $changed {
2475        set decision [tk_dialog .changes "Abandon Changes" \
2476                "You have made changes to the Profile. Ok to abandon changes?" \
2477                warning 0 Abandon Keep]
2478        if !$decision {destroy $w}
2479    } else {
2480        destroy $w
2481    }
2482}
2483
2484##############################################################################
2485##                               #############################################
2486## END OF THE PROCEDURES SECTION #############################################
2487##                               #############################################
2488##############################################################################
2489
2490# <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
2491# <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<                          <<<<<<<<<<<<<<<<<<<
2492# <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<   BEGIN:  GUI SECTION    >>>>>>>>>>>>>>>>>>>
2493# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>                          >>>>>>>>>>>>>>>>>>>
2494# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
2495# A frame for menu items at top of display
2496set expgui(fm) [frame .fm -relief raised -borderwidth 2 -width 150 -height 40]
2497# Pack the menu frame.
2498pack $expgui(fm) -fill x -side top -anchor n
2499
2500# create a button bar
2501pack [frame .bar -relief raised -bd 2 -bg beige] -fill x -side top -anchor n
2502
2503# Creating the notebook with 5 panes: Phase, Histogram, Scaling, Profile
2504# & LS controls
2505# 0 name,
2506# 1 title
2507# 2 create command
2508# 3 raise command
2509# 4 disable page in global mode (0/1)
2510
2511if $expgui(haveBW) {
2512    pack [NoteBook .n -bd 2] -expand yes -fill both
2513    # create an array element describing each notebook page
2514    # element 0 -- pane name
2515    #         1 -- Label on frame
2516    #         2 -- initialization command
2517    #         3 -- update command
2518    #         4 -- 0/1 Use 1 if pane should be disabled in when all histograms
2519    #                are selected in global mode, 0 otherwise
2520    set expgui(notebookpagelist) {
2521        {lsFrame     "LS Controls" \
2522                "" \
2523                SetupExtractHist \
2524                0}
2525        {phaseFrame   Phase        \
2526                "" \
2527                "SelectOnePhase \$expgui(curPhase); DisplayAllAtoms noreset" \
2528                0}
2529        {histFrame    Histogram \
2530                MakeHistPane \
2531                DisplayHistogram \
2532                1}
2533        {fracFrame    Scaling \
2534                MakeScalingPane \
2535                DisplayFrac \
2536                0}
2537        {profFrame    Profile \
2538                MakeProfilePane \
2539                DisplayProfile \
2540                1}
2541        {consFrame    Constraints \
2542                "source [file join $expgui(scriptdir) atomcons.tcl]; MakeConstraintsPane" \
2543                DisplayConstraintsPane \
2544                0}
2545        {orientFrame  "MD Pref Orient" \
2546                MakeOrientPane \
2547                DisplayOrient \
2548                0}
2549        {odfFrame  "SH Pref Orient" \
2550                "source [file join $expgui(scriptdir) odf.tcl]; MakeODFPane" \
2551                DisplayODFPane \
2552                0}
2553    }
2554    foreach item $expgui(notebookpagelist) {
2555        set frm [lindex $item 0]
2556        set expgui($frm) [\
2557            .n insert end $frm -text [lindex $item 1] \
2558            -createcmd [lindex $item 2] \
2559            -raisecmd "set expgui(pagenow) $frm; [lindex $item 3]"]
2560
2561        # at this time expgui(frameactionlist) is generated
2562        # from expgui(notebookpagelist), but in the future it might
2563        # make sense to use expgui(notebookpagelist) directly
2564        lappend expgui(frameactionlist) "$frm [list [lindex $item 3]]"
2565
2566        # panes to disable in global "all" mode
2567        if {[lindex $item 4]} {
2568            lappend expgui(GlobalModeAllDisable) \
2569                    "$frm \{.n itemconfigure $frm\}"
2570        }
2571    }
2572} else {
2573    Notebook:create .n \
2574            -pages {lsFrame phaseFrame histFrame fracFrame profFrame}
2575    pack .n -anchor w -fill both -expand yes
2576    foreach item {lsFrame phaseFrame histFrame fracFrame profFrame \
2577            orientFrame} \
2578            page {"LS Controls" Phase Histogram Scaling Profile \
2579            "MD Pref Orient"} {
2580        set expgui($item) [Notebook:frame .n $item]
2581        Notebook:pageconfig .n $item -command "InitPage $item" -title $page
2582    }
2583    lappend expgui(frameactionlist) "lsFrame SetupExtractHist"
2584    lappend expgui(frameactionlist) "phaseFrame {DisplayAllAtoms noreset}"
2585    lappend expgui(frameactionlist) "histFrame DisplayHistogram"
2586    lappend expgui(frameactionlist) "fracFrame DisplayFrac"
2587    lappend expgui(frameactionlist) "profFrame DisplayProfile"
2588    lappend expgui(frameactionlist) "orientFrame DisplayOrient"
2589    set expgui(GlobalModeAllDisable) {}
2590    lappend expgui(GlobalModeAllDisable) "histFrame {Notebook:pageconfig .n histFrame}"
2591    lappend expgui(GlobalModeAllDisable) "profFrame {Notebook:pageconfig .n profFrame}"
2592}
2593
2594# this is used to bring up the selected frame
2595proc RaisePage {nextpage} {
2596    global expgui
2597    if $expgui(haveBW) {
2598        .n see $nextpage
2599        .n raise $nextpage
2600        set expgui(pagenow) $nextpage
2601    } else {
2602        Notebook:raise .n $nextpage
2603        InitPage $nextpage
2604    }
2605}
2606# this is only called when BWidget is not in use
2607proc InitPage {nextpage} {
2608    global expgui
2609    set expgui(pagenow) $nextpage
2610    UpdateCurrentPage
2611}
2612# resize the notebook to fit all the tabs and the largest page
2613proc ResizeNotebook {} {
2614    global expgui
2615    if {$expgui(haveBW)} {
2616        .n compute_size
2617    } else {
2618        Notebook:resize .n
2619    }
2620}
2621
2622#----------------------------------------------------------------------------
2623proc MakePhasePane {} {
2624    #\/ \/ \/ \/ \/ \/ \/ BEGINNING OF PHASE PANE CODE \/ \/ \/ \/ \/ \/ \/
2625    global expgui
2626    frame $expgui(phaseFrame).top
2627    set frameLatt [frame $expgui(phaseFrame).frameLatt]
2628    #  This is a big frame in the Phase notebook pane to hold atomic data.
2629    set fbig [frame $expgui(phaseFrame).fbig -width 180 \
2630            -relief raised -borderwidth 4 -bg beige]
2631    #  This is a frame just below the big frame: for edits
2632    set frame3 [frame $expgui(phaseFrame).frame3 -width 100 \
2633            -relief raised -borderwidth 4 -bg beige]
2634
2635    grid $expgui(phaseFrame).top -sticky news -row 0 -column 0
2636    grid $frameLatt -sticky news -row 2 -column 0
2637    grid $fbig -sticky news -row 3 -column 0
2638    # give extra space to the atoms box
2639    grid columnconfigure $expgui(phaseFrame) 0 -weight 1
2640    grid rowconfigure $expgui(phaseFrame) 3 -weight 1
2641    grid $frame3 -sticky news -row 4 -column 0
2642    grid columnconfigure $expgui(phaseFrame) 0 -weight 1
2643    grid rowconfigure $expgui(phaseFrame) 3 -weight 1
2644    grid [frame  $expgui(phaseFrame).top.ps] -column 0 -row 0 -sticky w
2645    # this is where the buttons will go
2646    pack [label $expgui(phaseFrame).top.ps.0 -text "No Phases"] -side left
2647   
2648    grid [label $expgui(phaseFrame).top.lA -text title: \
2649            -fg blue ] -column 1 -row 0 -sticky e
2650    grid [entry $expgui(phaseFrame).top.lB -textvariable entryvar(phasename) \
2651            -fg blue -width 45] -column 2 -row 0 -sticky e
2652    grid columnconfigure $expgui(phaseFrame).top 1 -weight 1
2653    # ------------- Lattice Parameter Box ------------------
2654    set row 0
2655    foreach col {2 4 6} var {a b c} lbl {a b c} {
2656        grid [label $frameLatt.l$var -text $lbl] \
2657                -column $col -row $row -padx 5 -sticky e
2658        incr col
2659        grid [entry $frameLatt.e$var -textvariable entryvar($var) -width 10] \
2660            -column $col -row $row -padx 5
2661    }
2662    incr row
2663    foreach col {2 4 6} var {alpha beta gamma} lbl {a b g} {
2664        grid [label $frameLatt.l$var -text $lbl -font symbol] \
2665                -column $col -row $row -padx 5 -sticky e
2666        incr col
2667        grid [entry $frameLatt.e$var -textvariable entryvar($var) -width 10] \
2668            -column $col -row $row -padx 5
2669    }
2670   
2671    grid [label $frameLatt.lr -text "Refine Cell"] -column 8 -row 0 -padx 5 -sticky e
2672    grid [label $frameLatt.ld -text "Cell damping"] -column 8 -row 1 -padx 5 -sticky e
2673    set cFlag [checkbutton $frameLatt.c -text "" -variable entryvar(cellref)]
2674    grid $cFlag -column 9 -row 0 -padx 5 -sticky e
2675    tk_optionMenu $frameLatt.om entryvar(celldamp) 0 1 2 3 4 5 6 7 8 9
2676    grid $frameLatt.om -column 9 -row 1 -padx 5 -sticky e
2677    grid [label $frameLatt.phasetype -textvariable expgui(phasetype) -fg blue] \
2678            -column 1 -row 0 -rowspan 2
2679    if [file executable $expgui(exptool)] {
2680        button $frameLatt.newp -text Replace\nPhase \
2681                -command MakeReplacePhaseBox -state disabled
2682        grid $frameLatt.newp -column 0 -row 0 -rowspan 2 -sticky w
2683    }
2684    grid columnconfig $frameLatt  1 -weight 1
2685    grid columnconfig $frameLatt  0 -weight 1
2686    #-------------- Begin Atom Coordinates Box  ------------------------------
2687    grid [listbox  $fbig.title -height 1 -relief flat \
2688            -exportselection 0 -bg lightgrey -fg black \
2689            -selectforeground black -selectbackground lightgrey] \
2690            -row 0 -column 0 -sticky ew
2691    set expgui(atomtitle) $fbig.title
2692    bind $expgui(atomtitle) <Button-1> {
2693        set i [lsearch {number type mult x y z occupancy} $expgui(asorttype)]
2694        incr i
2695        set expgui(asorttype) [lindex {number type mult x y z occupancy number} $i]
2696        DisplayAllAtoms
2697    }
2698    bind $expgui(atomtitle) <Button-3> {set expgui(asorttype) number; DisplayAllAtoms}
2699
2700    $expgui(atomtitle) configure -font $expgui(coordfont) -selectmode extended
2701    grid [listbox   $fbig.lbox -height 10 \
2702            -exportselection 0 \
2703            -xscrollcommand " $fbig.bscr set"\
2704            -yscrollcommand " $fbig.rscr set"\
2705            ] -row 1 -column 0 -sticky news
2706    set expgui(atomlistbox) $fbig.lbox
2707    $expgui(atomlistbox) configure -font $expgui(coordfont) -selectmode extended
2708    grid [scrollbar $fbig.bscr -orient horizontal \
2709            -command "move2boxes \" $fbig.title $fbig.lbox \" " \
2710            ] -row 2 -column 0 -sticky ew
2711    grid [scrollbar $fbig.rscr  -command "$fbig.lbox yview" \
2712            ] -row 1 -column 1 -sticky ns
2713    # give extra space to the atoms box
2714    grid columnconfigure $fbig 0 -weight 1
2715    grid rowconfigure $fbig 1 -weight 1
2716   
2717    #   BIND mouse in editbox
2718    bind $expgui(atomlistbox) <ButtonRelease-1>   editRecord
2719    bind $expgui(atomlistbox) <Button-3>   SelectAllAtoms
2720   
2721    #-------------- End Atoms Section  ---------------------------------
2722
2723    # --------------------------- Begin Edit Box ------------------------
2724    grid [set expgui(EditingAtoms) [label $frame3.top -bg beige -fg blue]] \
2725            -column 0 -row 0 -padx 2 -pady 3 -columnspan 10 -sticky w
2726    if [file executable $expgui(exptool)] {
2727        button $frame3.newa -text "Add New Atom" \
2728                -command {MakeAddAtomsBox $expgui(curPhase)}
2729        grid $frame3.newa -column 11 -row 0
2730    }
2731    button [set expgui(atomxform) $frame3.xa] \
2732            -command {MakeXformAtomsBox $expgui(curPhase)}
2733    grid $expgui(atomxform) -column 11 -row 1 -sticky ew
2734
2735    set f3l1 [label $frame3.l1 -text "Refinement Flags " -bg beige]
2736    grid $f3l1 -column 0 -row 1 -padx 2 -sticky nsw -pady 3
2737   
2738    set f3cFlag1 [checkbutton $frame3.cf1 -text "X" -variable entryvar(xref) -bg beige]
2739    set f3cFlag2 [checkbutton $frame3.cf2 -text "U" -variable entryvar(uref) -bg beige]
2740    set f3cFlag3 [checkbutton $frame3.cf3 -text "F" -variable entryvar(fref) -bg beige]
2741    grid $f3cFlag1 -column 1 -row 1 -padx 2 -pady 3 -sticky w
2742    grid $f3cFlag2 -column 2 -row 1 -padx 2 -pady 3 -sticky w
2743    grid $f3cFlag3 -column 3 -row 1 -padx 2 -pady 3 -sticky w
2744   
2745    set f3l4 [label $frame3.l4 -text "Damping Factors " -bg beige]
2746    grid $f3l4 -column 4 -row 1 -padx 2 -sticky nsw -pady 3
2747   
2748    tk_optionMenu $frame3.om2 entryvar(xdamp) 0 1 2 3 4 5 6 7 8 9
2749    tk_optionMenu $frame3.om3 entryvar(udamp) 0 1 2 3 4 5 6 7 8 9
2750    tk_optionMenu $frame3.om4 entryvar(fdamp) 0 1 2 3 4 5 6 7 8 9
2751    grid [label $frame3.lom2 -text X -bg beige] -column 5 -row 1 -padx 2 -pady 3 -sticky w
2752    grid $frame3.om2 -column 6 -row 1 -padx 2 -pady 3 -sticky w
2753    grid [label $frame3.lom3 -text U -bg beige] -column 7 -row 1 -padx 2 -pady 3 -sticky w
2754    grid $frame3.om3 -column 8 -row 1 -padx 2 -pady 3 -sticky w
2755    grid [label $frame3.lom4 -text F -bg beige] -column 9 -row 1 -padx 2 -pady 3 -sticky w
2756    grid $frame3.om4 -column 10 -row 1 -padx 2 -pady 3 -sticky w
2757
2758    set expgui(atomreflbl) "$frame3.l1 $frame3.l4 $frame3.lom2 $frame3.lom3 $frame3.lom4 "
2759    set expgui(atomref) "$frame3.cf1 $frame3.cf2 $frame3.cf3 $frame3.om2 $frame3.om3 $frame3.om4"
2760   
2761    set coords [frame $frame3.coords  -width 100 -borderwidth 0  -bg beige]
2762    grid $coords -column 0 -row 6 -columnspan 12 -sticky nsew
2763   
2764    set f3l1 [label $frame3.coords.l1 -text "Label" -bg beige]
2765    set f3e1  [entry  $frame3.coords.e1 -textvariable entryvar(label) -width 6]
2766    set f3l8 [label $frame3.coords.l8 -text "Coordinates" -bg beige]
2767    set f3e8  [entry  $frame3.coords.e8 -textvariable entryvar(x) -width 10]
2768    set f3e9  [entry  $frame3.coords.e9 -textvariable entryvar(y) -width 10]
2769    set f3e10 [entry $frame3.coords.e10 -textvariable entryvar(z) -width 10]
2770    set f3l11 [label $frame3.coords.l11 -text "Occupancy" -bg beige]
2771    set f3e11 [entry $frame3.coords.e11 -textvariable entryvar(frac) -width 10]
2772    set expgui(atomlabels) "$frame3.coords.l1 $frame3.coords.l8 $frame3.coords.l11"
2773    set expgui(atomentry)  "$frame3.coords.e1 $frame3.coords.e8 $frame3.coords.e9 $frame3.coords.e10 $frame3.coords.e11"
2774   
2775    grid $f3l1 -column 0 -row 4 -padx 2 -sticky nsw -pady 3
2776    grid $f3e1 -column 1 -row 4 -padx 2 -sticky nsw -pady 3
2777    grid $f3l8 -column 2 -row 4 -padx 2 -sticky nsw -pady 3
2778    grid $f3e8 -column 3 -row 4 -padx 2 -sticky nsw -pady 3
2779    grid $f3e9 -column 4 -row 4 -padx 2 -sticky nsw -pady 3
2780    grid $f3e10 -column 5 -row 4 -padx 2 -sticky nsw -pady 3
2781    grid $f3l11 -column 6 -row 4 -padx 2 -sticky nsw -pady 3
2782    grid $f3e11 -column 7 -row 4 -padx 2 -sticky nsw -pady 3
2783
2784    set f3f31 [frame $frame3.f3f31  -width 100 -borderwidth 0 -bg beige]
2785    grid $f3f31 -column 0 -row 7 -columnspan 12
2786    set expgui(anisolabels) {}
2787    lappend expgui(anisolabels)  [label $f3f31.l13 -text "Uiso" -bg beige]
2788    lappend expgui(anisolabels)  [label $f3f31.l14 -text "U22" -bg beige]
2789    lappend expgui(anisolabels)  [label $f3f31.l15 -text "U33" -bg beige]
2790    lappend expgui(anisolabels)  [label $f3f31.l16 -text "U12" -bg beige]
2791    lappend expgui(anisolabels)  [label $f3f31.l17 -text "U13" -bg beige]
2792    lappend expgui(anisolabels)  [label $f3f31.l18 -text "U23" -bg beige]
2793
2794    set expgui(anisoentry) {}
2795    lappend expgui(anisoentry) [entry $f3f31.e13 -textvariable entryvar(U11) -width 10]
2796    lappend expgui(anisoentry) [entry $f3f31.e14 -textvariable entryvar(U22) -width 10]
2797    lappend expgui(anisoentry) [entry $f3f31.e15 -textvariable entryvar(U33) -width 10]
2798    lappend expgui(anisoentry) [entry $f3f31.e16 -textvariable entryvar(U12) -width 10]
2799    lappend expgui(anisoentry) [entry $f3f31.e17 -textvariable entryvar(U13) -width 10]
2800    lappend expgui(anisoentry) [entry $f3f31.e18 -textvariable entryvar(U23) -width 10]
2801   
2802    set col 0
2803    foreach item1 $expgui(anisolabels) item2 $expgui(anisoentry) {
2804        grid $item1 -column $col -row 0 -sticky nsw -pady 3
2805        incr col
2806        grid $item2 -column $col -row 0 -sticky nsw -pady 3
2807        incr col
2808    }
2809    # --------------------------- End Edit Box -------------------------
2810   
2811    #/\ /\ /\ /\ /\ /\ /\ END OF PHASE PANE CODE /\ /\ /\ /\ /\ /\ /\ /\ /
2812    # resize in case the pane needs more space
2813    ResizeNotebook
2814}
2815
2816proc move2boxes {boxlist args} {
2817    foreach listbox $boxlist {
2818        eval $listbox xview $args
2819    }
2820}
2821#-----------------------------------------------------------------------------
2822proc MakeHistPane {} {
2823    #v v v v v v v v v v  BEGINNING OF HISTOGRAM PANE CODE v v v v v v v v v v
2824    global expgui
2825   
2826    grid columnconfigure $expgui(histFrame) 0 -weight 1
2827    grid rowconfigure $expgui(histFrame) 1 -weight 1
2828    grid rowconfigure $expgui(histFrame) 2 -weight 1
2829    grid rowconfigure $expgui(histFrame) 3 -weight 1
2830
2831    grid [frame $expgui(histFrame).hs] \
2832            -column 0 -row 0 -rowspan 10 -sticky nsew
2833    MakeHistBox $expgui(histFrame).hs
2834    bind $expgui(histFrame).hs.lbox <ButtonRelease-1>  {
2835        set expgui(curhist) [$expgui(histFrame).hs.lbox curselection]
2836        DisplayHistogram
2837    }
2838    bind $expgui(histFrame).hs.lbox <Button-3>  {
2839        if $expgui(globalmode) {
2840            $expgui(histFrame).hs.lbox selection set 0 end
2841            set expgui(curhist) [$expgui(histFrame).hs.lbox curselection]
2842            DisplayHistogram
2843        }
2844    }
2845   
2846    frame $expgui(histFrame).top -borderwidth 4 -relief groove
2847    grid [label $expgui(histFrame).top.txt] -row 0 -column 0
2848    if $expgui(haveBW) {
2849        foreach item {backBox diffBox} num {2 3} \
2850                title {Background "Diffractometer Constants"} {
2851            TitleFrame $expgui(histFrame).$item  \
2852                    -borderwidth 4 -side left -relief groove -text $title
2853            set expgui($item) [$expgui(histFrame).$item getframe]
2854            grid $expgui(histFrame).$item -column 1 -row $num -sticky nsew
2855            grid rowconfigure $expgui(histFrame) $num -minsize 100
2856        }
2857    } else {
2858        foreach item {backBox diffBox} num {1 2} \
2859                title {Background "Diffractometer Constants"} {
2860            frame $expgui(histFrame).$item  -borderwidth 4 -relief groove
2861            grid $expgui(histFrame).$item -column 1 -row $num -sticky nsew
2862            set expgui($item)  $expgui(histFrame).$item
2863            grid [label $expgui(histFrame).$item.title -text $title] \
2864                    -row 0 -column 0 -columnspan 10 -sticky nw
2865        }
2866    }
2867    if [file executable $expgui(exptool)] {
2868        button $expgui(histFrame).newh -text "Add New Histogram" -command MakeAddHistBox
2869        grid $expgui(histFrame).newh -column 1 -row 6
2870    }
2871
2872    # BACKGROUND information.
2873    # <<<<<<<<<<<<<<<<<<<<<<<<< BACKGROUND  <<<<<<<<<<<<<<<<<<<<<
2874    grid [frame $expgui(backBox).frm1 ] -row 0 -column 0  -columnspan 11
2875    grid [label $expgui(backBox).frm1.lBGType \
2876            -textvariable expgui(backtypelbl)] \
2877            -row 1 -column 0 -sticky nws  -padx 2 -pady 3
2878    grid [label $expgui(backBox).frm1.lBGTerms \
2879            -textvariable expgui(backtermlbl)] \
2880            -row 1 -column 1 -sticky nws  -padx 2 -pady 3
2881    grid [button $expgui(backBox).frm1.edit -textvariable expgui(bkglbl) \
2882            -command editbackground] \
2883            -row 1 -column 2 -columnspan 3 -sticky w -padx 2 -pady 3
2884    grid [frame $expgui(backBox).frm2 ] \
2885            -row 1 -column 0 -columnspan 11 -sticky e
2886    grid [label $expgui(backBox).frm2.lfBG -text "  Refine background" ] \
2887            -row 2 -column 1 -sticky news -padx 4 -pady 3
2888    grid [checkbutton $expgui(backBox).frm2.rfBG -text "" \
2889            -variable  entryvar(bref) ] \
2890            -row 2 -column 2 -sticky news -padx 4 -pady 3
2891    grid [label $expgui(backBox).frm2.lBGDamp -text Damping ] \
2892            -row 2 -column 3 -sticky w    -padx 2 -pady 3
2893    tk_optionMenu $expgui(backBox).frm2.om  entryvar(bdamp) 0 1 2 3 4 5 6 7 8 9
2894    grid $expgui(backBox).frm2.om \
2895            -row 2 -column 4 -sticky news -padx 4 -pady 3 -sticky e
2896    #^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^END OF HISTOGRAM PANE CODE ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
2897    # insert the histograms & resize in case the pane needs more space
2898    sethistlist
2899    ResizeNotebook
2900}
2901###############################################################################
2902proc MakeScalingPane {} {
2903    #v v v v v v v v v v  BEGINNING OF SCALING PANE CODE v v v v v v v v v v
2904    global expgui
2905
2906    pack [frame $expgui(fracFrame).hs] -side left -expand y -fill both
2907    MakeHistBox $expgui(fracFrame).hs
2908    bind $expgui(fracFrame).hs.lbox <ButtonRelease-1> {
2909        set expgui(curhist) [$expgui(fracFrame).hs.lbox curselection]
2910        DisplayFrac
2911    }
2912    bind $expgui(fracFrame).hs.lbox <Button-3>  {
2913        if $expgui(globalmode) {
2914            $expgui(fracFrame).hs.lbox selection set 0 end
2915            set expgui(curhist) [$expgui(fracFrame).hs.lbox curselection]
2916            DisplayFrac
2917        }
2918    }
2919
2920    pack [frame $expgui(fracFrame).f1] -fill both -expand true
2921    # Create a large canvas area containing a frame for each phase in the data set.
2922    # The canvas and vertical scrollbar are inside a frame called f1
2923    if $expgui(haveBW) {
2924        TitleFrame $expgui(fracFrame).f1.scaleBox \
2925                -borderwidth 4 -text "Scale Factor"
2926        #           -borderwidth 4 -width 600 -height 100 -label "Scale Factor"
2927        grid $expgui(fracFrame).f1.scaleBox -column 0 -row 0 -sticky nsew -columnspan 2
2928        set expgui(scaleBox)  [$expgui(fracFrame).f1.scaleBox getframe]
2929        grid [label $expgui(scaleBox).histSFLabel -text Scale] \
2930                -row 1 -column 0 -sticky nws  -padx 2 -pady 3
2931    } else {
2932        frame $expgui(fracFrame).f1.scaleBox  -borderwidth 4 -relief groove
2933        grid $expgui(fracFrame).f1.scaleBox -column 0 -row 0 -sticky nsew -columnspan 2
2934        set expgui(scaleBox)  $expgui(fracFrame).f1.scaleBox
2935        grid [label $expgui(scaleBox).histSFLabel -text "Scale Factor"] \
2936                -row 1 -column 0 -sticky nws  -padx 2 -pady 3
2937    }
2938    grid [entry $expgui(scaleBox).ent1 -textvariable entryvar(scale) -width 15] \
2939            -row 1 -column 1 -sticky ew -padx 4 -pady 3
2940    button $expgui(scaleBox).but1 -text "Set Globally" \
2941            -command "editglobalparm histinfo scale {Scale Factor}"
2942
2943    grid [label $expgui(scaleBox).histSFRLabel -text " Refine"] \
2944            -row 1 -column 2 -sticky nws  -padx 2 -pady 3
2945    grid [checkbutton $expgui(scaleBox).rf -variable entryvar(sref)] \
2946            -row 1 -column 3 -sticky news -padx 4 -pady 3
2947    grid [label $expgui(scaleBox).lD1 -text "Damping"] \
2948            -row 1 -column 4 -sticky w    -padx 2 -pady 3
2949    tk_optionMenu $expgui(scaleBox).om entryvar(sdamp) 0 1 2 3 4 5 6 7 8 9
2950    grid $expgui(scaleBox).om \
2951            -row 1 -column 5 -sticky news -padx 4 -pady 3
2952    grid columnconfigure $expgui(scaleBox) 6  -weight 1
2953   
2954    if $expgui(haveBW) {
2955        grid [TitleFrame $expgui(fracFrame).f1.phaseFrac -bd 4 \
2956                -text "Phase Fractions" -relief groove] \
2957                -sticky news -row 1 -column 0 -columnspan 2
2958        set PhaseFractBox [$expgui(fracFrame).f1.phaseFrac getframe]
2959    } else {
2960        set PhaseFractBox $expgui(fracFrame).f1
2961    }
2962    grid columnconfigure $expgui(fracFrame).f1 0 -weight 1
2963    grid rowconfigure $expgui(fracFrame).f1 1 -weight 1
2964   
2965    grid [set expgui(FracBox) [canvas $PhaseFractBox.fracBox \
2966            -scrollregion {0 0 5000 500} \
2967            -yscrollcommand "$PhaseFractBox.yscroll set" \
2968            -width 500 -height 350 -bg lightgrey]] \
2969            -sticky  news -row 1 -column 0
2970    grid [scrollbar $PhaseFractBox.yscroll \
2971            -command "$expgui(FracBox) yview" \
2972            -orient vertical] \
2973            -sticky ns -row 1 -column 1
2974    frame $expgui(FracBox).f -bd 0
2975    $expgui(FracBox) create window 0 0 -anchor nw  -window $expgui(FracBox).f
2976
2977    # the rest of the page is created in DisplayFrac
2978
2979    # insert the histograms & resize in case the pane needs more space
2980    sethistlist
2981    ResizeNotebook
2982    # ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ END OF SCALING PANE CODE ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
2983}
2984###############################################################################
2985proc MakeProfilePane {} {
2986    global expgui
2987    # v v v v v v v v v v BEGINNING OF PROFILE PANE CODE v v v v v v v v v v v
2988    pack [frame $expgui(profFrame).hs] -side left -expand y -fill both
2989    MakeHistBox $expgui(profFrame).hs
2990    bind $expgui(profFrame).hs.lbox <ButtonRelease-1> {
2991        set expgui(curhist) [$expgui(profFrame).hs.lbox curselection]
2992        DisplayProfile
2993    }
2994    bind $expgui(profFrame).hs.lbox <Button-3>  {
2995        if $expgui(globalmode) {
2996            $expgui(profFrame).hs.lbox selection set 0 end
2997            set expgui(curhist) [$expgui(profFrame).hs.lbox curselection]
2998            DisplayProfile
2999        }
3000    }
3001
3002    # Create a large canvas area containing a frame for each phase in the data set.
3003    # The canvas and vertical scrollbar are inside a frame called f1
3004    pack [frame $expgui(profFrame).f1] -fill both -expand true
3005    grid [set expgui(ProfileBox) [canvas $expgui(profFrame).f1.profileBox \
3006            -scrollregion {0 0 5000 500} -width 500 -height 350 -bg lightgrey]] \
3007            -sticky  news -row 0 -column 0
3008    grid [scrollbar $expgui(profFrame).f1.yscroll -orient vertical] \
3009            -sticky ns -row 0 -column 1
3010   
3011    $expgui(ProfileBox) config -yscrollcommand "$expgui(profFrame).f1.yscroll set"
3012    $expgui(profFrame).f1.yscroll config -command { $expgui(ProfileBox) yview }
3013   
3014    grid columnconfigure $expgui(profFrame).f1 1 -weight 1
3015    grid rowconfigure $expgui(profFrame).f1 0 -weight 1
3016    frame $expgui(ProfileBox).f -bd 0
3017    $expgui(ProfileBox) create window 0 0 -anchor nw  -window $expgui(ProfileBox).f
3018   
3019    # insert the histograms & resize in case the pane needs more space
3020    sethistlist
3021    ResizeNotebook
3022    # ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ END OF PROFILE PANE CODE ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
3023}
3024
3025##############################################################################
3026# v v v v v v v v v v BEGINNING OF LS PANE CODE v v v v v v v v v v v v v
3027array set printopts {
3028    0 "Print the reciprocal metric tensor changes"
3029    1 "Print the correlation matrix"
3030    2 "Print the Least-Squares matrices and vectors"
3031    4 "Print the linear constraint matrices"
3032    5 "Print the applied  shifts and shift factors"
3033    6 "Print the reciprocal metric tensor Var-Covar terms"
3034    7 "Print all parameters for each cycle"
3035    8 "Print summary shift/esd data after last cycle"
3036    9 "Print zero/unit pole figure constraint terms"
3037}
3038pack [frame $expgui(lsFrame).hs] -side left -expand y -fill both
3039MakeHistBox $expgui(lsFrame).hs
3040bind $expgui(lsFrame).hs.lbox <ButtonRelease-1> {
3041    set expgui(curhist) [$expgui(lsFrame).hs.lbox curselection]
3042    SetupExtractHist
3043}
3044bind $expgui(lsFrame).hs.lbox <Button-3>  {
3045    if $expgui(globalmode) {
3046        $expgui(lsFrame).hs.lbox selection set 0 end
3047        set expgui(curhist) [$expgui(lsFrame).hs.lbox curselection]
3048        SetupExtractHist
3049    }
3050}
3051
3052pack [frame $expgui(lsFrame).f1] -fill both -expand true
3053grid rowconfigure $expgui(lsFrame).f1  4 -weight 1
3054set row 0
3055grid [label $expgui(lsFrame).f1.his1 -pady 6 -text "Last History:"] -row $row -column 0
3056grid [label $expgui(lsFrame).f1.his2 -relief raised -bd 2 -pady 6 \
3057        -textvariable expgui(last_History)] \
3058        -row $row -column 1 -columnspan 5 -sticky w
3059incr row
3060grid [label $expgui(lsFrame).f1.tit1 -pady 6 -text "Title:"] -row $row -column 0
3061grid [entry $expgui(lsFrame).f1.tit2 \
3062        -textvariable entryvar(title) -width 48] \
3063        -row $row -column 1 -columnspan 5 -sticky w
3064set entrycmd(title) "expinfo title"
3065
3066incr row
3067grid [frame $expgui(lsFrame).f1.b -bd 4 -relief groove] \
3068        -row $row -column 0 -columnspan 2 -pady 3
3069grid [label $expgui(lsFrame).f1.b.lcyc -text "Number of Cycles"] -row 0 -column 0
3070grid [entry $expgui(lsFrame).f1.b.ecyc -width 3 \
3071        -textvariable entryvar(cycles)] -row 0 -column 1
3072grid [menubutton $expgui(lsFrame).f1.lprint -textvariable expgui(printopt) \
3073        -menu $expgui(lsFrame).f1.lprint.menu -bd 4 -relief raised \
3074        ] -row $row -column 2
3075menu $expgui(lsFrame).f1.lprint.menu
3076foreach num [lsort [array names printopts]] {
3077    $expgui(lsFrame).f1.lprint.menu add checkbutton \
3078            -label "$printopts($num) ([expr int(pow(2,$num))])"\
3079        -variable entryvar(printopt$num)
3080}
3081#grid [frame $expgui(lsFrame).f1.c -bd 4 -relief groove] -row $row -column 3
3082#grid [label $expgui(lsFrame).f1.c.fol -text "Extract Fobs"] -row 0 -column 2
3083#grid [checkbutton $expgui(lsFrame).f1.c.foc -variable entryvar(fobsextract)] -row 0 -column 3
3084incr row
3085if {$expgui(haveBW)} {
3086    grid [TitleFrame $expgui(lsFrame).f1.a -bd 4 -relief groove \
3087            -text "Reflection Intensity Extraction" \
3088            ] -row $row -column 0 -columnspan 6
3089    set expgui(FobsExtractFrame) [$expgui(lsFrame).f1.a getframe]
3090} else {
3091    grid [frame $expgui(lsFrame).f1.a -bd 4 -relief groove \
3092            ] -row $row -column 0 -columnspan 6
3093    set expgui(FobsExtractFrame) $expgui(lsFrame).f1.a
3094}
3095grid rowconfigure $expgui(lsFrame).f1 $row -pad 16
3096grid [frame $expgui(FobsExtractFrame).c -bd 4 -relief groove] \
3097        -row 0 -column 8 -columnspan 3 -sticky e
3098grid [label $expgui(FobsExtractFrame).c.fol -text "Extract Fobs"] \
3099        -row 0 -column 2
3100grid [checkbutton $expgui(FobsExtractFrame).c.foc \
3101        -variable entryvar(fobsextract)] -row 0 -column 3
3102foreach num {1 2 3 4 5 6 7 8 9} {
3103    grid [label $expgui(FobsExtractFrame).l$num -text $num] -row 1 -column $num
3104    grid [radiobutton $expgui(FobsExtractFrame).cc$num \
3105            -command "HistExtractSet $num" \
3106            -variable expgui(Fextract$num) -value 0] \
3107            -row 2 -column $num
3108    grid [radiobutton $expgui(FobsExtractFrame).ca$num \
3109            -command "HistExtractSet $num" \
3110            -variable expgui(Fextract$num) -value 1] \
3111            -row 3 -column $num
3112    grid [radiobutton $expgui(FobsExtractFrame).cb$num \
3113            -command "HistExtractSet $num" \
3114            -variable expgui(Fextract$num) -value 2] \
3115            -row 4 -column $num
3116}
3117set expgui(ExtractSettingsRadiobuttons) $expgui(FobsExtractFrame).cc
3118lappend expgui(ExtractSettingsRadiobuttons) $expgui(FobsExtractFrame).ca
3119lappend expgui(ExtractSettingsRadiobuttons) $expgui(FobsExtractFrame).cb
3120
3121grid [label $expgui(FobsExtractFrame).t \
3122        -text "Extraction\nMethod" -anchor c] \
3123        -column 0 -row 0 -rowspan 2 -sticky s
3124grid [label $expgui(FobsExtractFrame).t0 -text "Phase #" -anchor sw] \
3125        -column 1 -row 0 -columnspan 7 -sticky sw
3126grid [label $expgui(FobsExtractFrame).t1 -text "Rietveld" -anchor c] -column 0 -row 2
3127grid [label $expgui(FobsExtractFrame).t2 -text "F(calc) Weighted" -anchor c] -column 0 -row 3
3128grid [label $expgui(FobsExtractFrame).t3 -text "Equally Weighted" -anchor c] -column 0 -row 4
3129grid [label $expgui(FobsExtractFrame).t2a -text "(Model biased)" -anchor c] -column 10 -row 3
3130grid [label $expgui(FobsExtractFrame).t3a -text "(Le Bail method)" -anchor c] -column 10 -row 4
3131# ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ END OF LS PANE CODE ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
3132#-------------------------------------------------------------------------
3133#-------------------------------------------------------------------------
3134#vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv THE MENU BAR vvvvvvvvvvvvvvvvvvvvvv
3135
3136#---- file menu button
3137menubutton $expgui(fm).file -text File -menu $expgui(fm).file.menu
3138menu $expgui(fm).file.menu
3139if $expgui(debug) {
3140    $expgui(fm).file.menu add command -label "Reset" -command "reset"
3141}
3142if {$tcl_platform(platform) != "macintosh"} {
3143    $expgui(fm).file.menu add command -label "expnam" -command readnewexp
3144}
3145$expgui(fm).file.menu add command -label "Save" -underline 0 \
3146        -command savearchiveexp
3147foreach c {s S} {bind . <Alt-$c> [list savearchiveexp]}
3148$expgui(fm).file.menu add command -label "Save As" \
3149        -command "SaveAsFile"
3150$expgui(fm).file.menu add command -label "Reread .EXP file" \
3151        -command {rereadexp $expgui(expfile)}
3152
3153#---- help menu button
3154menubutton $expgui(fm).help -text Help -menu $expgui(fm).help.menu
3155menu $expgui(fm).help.menu
3156$expgui(fm).help.menu add command -command showhelp -underline 0 \
3157        -label "Help on Command"
3158foreach c {h H} {bind . <Alt-$c> [list showhelp]}
3159$expgui(fm).help.menu add command -label "About..." -command About
3160
3161#---- options menu button
3162menubutton $expgui(fm).option -text Options \
3163        -menu $expgui(fm).option.menu
3164menu $expgui(fm).option.menu
3165
3166if {$tcl_platform(platform) != "macintosh"} {
3167    $expgui(fm).option.menu add checkbutton  -label "Archive EXP" \
3168            -variable expgui(archive)
3169    $expgui(fm).option.menu add checkbutton  -label "Use DISAGL window" \
3170            -variable expgui(disaglSeparateBox)
3171}
3172$expgui(fm).option.menu add cascade -menu  $expgui(fm).option.menu.asort \
3173        -label "Sort atoms by"
3174
3175set expgui(asorttype) number
3176menu $expgui(fm).option.menu.asort
3177foreach opt {number type mult x y z occupancy} {
3178    $expgui(fm).option.menu.asort add radiobutton -command DisplayAllAtoms \
3179            -label $opt -value $opt -variable expgui(asorttype)
3180}
3181
3182$expgui(fm).option.menu add cascade -menu  $expgui(fm).option.menu.hsort \
3183        -label "Sort histograms by"
3184
3185set expgui(hsorttype) number
3186menu $expgui(fm).option.menu.hsort
3187$expgui(fm).option.menu.hsort add radiobutton -command sethistlist \
3188        -label number -value number -variable expgui(hsorttype)
3189$expgui(fm).option.menu.hsort add radiobutton -command sethistlist \
3190        -label "Histogram type" -value type -variable expgui(hsorttype)
3191$expgui(fm).option.menu.hsort add radiobutton -command sethistlist \
3192        -label "Bank #" -value bank -variable expgui(hsorttype)
3193$expgui(fm).option.menu.hsort add radiobutton -command sethistlist \
3194        -label "Angle/Wavelength" -value angle -variable expgui(hsorttype)
3195
3196#---- Global mode menu button
3197$expgui(fm).option.menu add cascade -menu $expgui(fm).option.menu.editmode \
3198        -label "Multiple hist. selection"
3199menu $expgui(fm).option.menu.editmode
3200$expgui(fm).option.menu.editmode add radiobutton  -label "Off" \
3201        -variable expgui(globalmode) -value 0 \
3202        -command sethistlist
3203$expgui(fm).option.menu.editmode add radiobutton  -label "All" \
3204        -variable expgui(globalmode) -value 6 \
3205        -command sethistlist
3206$expgui(fm).option.menu.editmode add radiobutton  -label "TOF" \
3207        -variable expgui(globalmode) -value 1 \
3208        -command sethistlist
3209$expgui(fm).option.menu.editmode add radiobutton  -label "CW Neutron" \
3210        -variable expgui(globalmode) -value 2  \
3211        -command sethistlist
3212$expgui(fm).option.menu.editmode add radiobutton  -label "Alpha12 Xray" \
3213        -variable expgui(globalmode) -value 3 \
3214        -command sethistlist
3215$expgui(fm).option.menu.editmode add radiobutton  -label "Monochromatic Xray" \
3216        -variable expgui(globalmode) -value 4 \
3217        -command sethistlist
3218$expgui(fm).option.menu.editmode add radiobutton  -label "Energy Disp Xray" \
3219        -variable expgui(globalmode) -value 5 \
3220        -command sethistlist
3221$expgui(fm).option.menu.editmode add separator
3222$expgui(fm).option.menu.editmode add checkbutton \
3223        -label "Group phases together" \
3224        -variable expgui(globalphasemode) \
3225        -command sethistlist
3226
3227set expgui(globalmode) 0
3228set expgui(globalphasemode) 1
3229
3230if {$tcl_platform(platform) == "unix"} {
3231    $expgui(fm).option.menu  add checkbutton -label "Override backspace" \
3232            -variable env(GSASBACKSPACE)
3233    $expgui(fm).option.menu  add checkbutton -label "Autoload EXP" \
3234            -variable expgui(autoexpload)
3235}
3236
3237$expgui(fm).option.menu add command -label "Save Options" \
3238        -command "SaveOptions"
3239
3240pack $expgui(fm).file $expgui(fm).option -side left  -in $expgui(fm)
3241
3242if {$tcl_platform(platform) != "macintosh"} {
3243    foreach menu $expgui(menunames) {
3244        set m [string tolower $menu]
3245        pack [menubutton $expgui(fm).$m -text $menu \
3246                -menu $expgui(fm).$m.menu] -side left
3247        menu $expgui(fm).$m.menu
3248    }
3249}
3250pack $expgui(fm).help  -side right -in $expgui(fm)
3251
3252if {$tcl_platform(platform) != "macintosh"} {
3253    # add the commands in expgui_menulist
3254    foreach menu [array names expgui_menulist ] {
3255        foreach cmd $expgui_menulist($menu) {
3256            set action {}
3257            set opt {}
3258            catch {set action [lindex $expgui_cmdlist($cmd) 0]}
3259            catch {set opt [lindex $expgui_cmdlist($cmd) 2]}
3260            if {$expgui(debug) && $action == ""} {puts "blank command for $cmd"}
3261            if {$action != "" && $action != "-"} {
3262                eval $expgui(fm).$menu.menu add command \
3263                        -label $cmd $opt -command [list [subst $action]]
3264                if {[lindex $opt 0] == "-underline"} {
3265                    catch {
3266                        set num [lindex $opt 1]
3267                        set key [string range $cmd $num $num]
3268                        bind . <Alt-[string tolower $key]> [subst $action]
3269                        bind . <Alt-[string toupper $key]> [subst $action]
3270                    }
3271                }
3272            }
3273        }
3274    }
3275}
3276# setup command help
3277foreach cmd [array names expgui_cmdlist] {
3278    set help {}
3279    catch {set help [lindex $expgui_cmdlist($cmd) 1]}
3280    if {$help == ""} {
3281        if {$expgui(debug)} {puts "no help for $cmd"}
3282    } else {
3283        # remove
3284        regsub -all \x09 $help " " help
3285        # preserve blank lines
3286        regsub -all \x0A\x0A $help "AAA1234567890AAA" help
3287        regsub -all \x0A $help " " help
3288        regsub -all "AAA1234567890AAA" $help \x0A\x0A help
3289        regsub -all " +" $help " " help
3290        set expgui_helplist($cmd) [string trim $help]
3291    }
3292}
3293if {$tcl_platform(platform) != "macintosh"} {
3294    # set up button bar
3295    foreach cmd $expgui(buttonlist) {
3296        set action {}
3297        catch {set action [lindex $expgui_cmdlist($cmd) 0]}
3298        if {$expgui(debug) && $action == ""} {puts "blank command for $cmd"}
3299        if {$action != ""} {
3300            pack [eval button .bar.$cmd -bg beige -activebackground yellow \
3301                    -text $cmd -command [list [subst $action]]] -side left
3302        }
3303    }
3304}
3305
3306$expgui(fm).file.menu add command -label "Exit"  -underline 1 -command catchQuit
3307foreach c {X x} {bind . <Alt-$c> [list catchQuit]}
3308#^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ END OF MENU DEFINITION ^^^^^^^^^^^^^^^^^^^
3309
3310# make the phase pane
3311MakePhasePane
3312# and the rest of the windows w/o BWidget
3313if !$expgui(haveBW) {
3314    MakeHistPane
3315    MakeScalingPane
3316    MakeProfilePane
3317}
3318
3319# handle indirect exits
3320wm protocol . WM_DELETE_WINDOW catchQuit
3321bind . <Control-c> catchQuit
3322
3323set expgui(curPhase) ""
3324set expgui(pagenow) ""
3325set expgui(curhist) {}
3326set expgui(selectedatomlist) {}
3327
3328loadexp $expgui(expfile)
3329
3330# select the 1st phase
3331SelectOnePhase [lindex $expmap(phaselist) 0]
3332# select the first histogram in the list by default (if there are any)
3333if {[llength $expmap(histlistboxcontents)] > 0} {
3334    set expgui(curhist) 0
3335} else {
3336    set expgui(curhist) {}
3337}
3338
3339# execute any local commands for final initialization
3340eval $expgui(initstring)
3341
3342# resize the notebook to fit all the tabs and the largest page
3343ResizeNotebook
3344
3345RaisePage lsFrame
3346if {[CountHistory] > 100} {
3347    DeleteHistoryRecords "This .EXP file has [CountHistory] history records\nErasing most will speed EXPGUI"
3348}
Note: See TracBrowser for help on using the repository browser.