source: trunk/expgui @ 660

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

# on 2002/10/31 17:29:18, toby did:
do better job reporting gsas_config errors & write to file better

  • Property rcs:author set to toby
  • Property rcs:date set to 2002/10/31 17:29:18
  • Property rcs:lines set to +13 -14
  • Property rcs:rev set to 1.59
  • Property rcs:state set to Exp
  • Property svn:keywords set to Author Date Revision Id
File size: 140.6 KB
Line 
1#!/usr/local/bin/wish
2# $Id: expgui 660 2009-12-04 23:09:53Z toby $
3set expgui(Revision) {$Revision: 660 $ $Date: 2009-12-04 23:09:53 +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
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            "EXPGUI requires Tcl/Tk version 8.0 or higher" error 0 "Exit"
34    exit
35}
36
37# the shell variable tells expgui to act as a shell for GSAS
38# as well as edit the EXP file. In no-shell mode, it can
39# only be used to edit the .EXP file as a callable program
40set expgui(shell) 1
41catch {if $env(EXPGUI_NOSHELL) {set expgui(shell) 0}}
42
43if {$argv != ""} {
44    if {[string match *noshell* [string tolower $argv]]} {
45        #puts noshell
46        set expgui(shell) 0
47        set expgui(expfile) [lindex $argv 1]
48    } else {
49        set expgui(expfile) [lindex $argv 0]
50    }
51    if {[string toupper [file extension $expgui(expfile)]] != ".EXP"} {
52        append expgui(expfile) ".EXP"
53    }
54} else {
55    set expgui(expfile) {}
56}
57
58set expgui(curhist) {}
59set expmap(powderlist) {}
60set expgui(bkgcolor1) #fdf
61
62set expgui(debug) 0
63catch {if $env(DEBUG) {set expgui(debug) 1}}
64#set expgui(debug) 1
65
66# location for web pages, if not found locally
67set expgui(website) www.ncnr.nist.gov/xtal/software/expgui
68# default for archive mode = on
69set expgui(archive) 1
70# default for autoexec load = off
71set expgui(autoexpload) 0
72# by default expgui is iconified while GENLES, etc runs
73set expgui(autoiconify) 1
74# default for show EXPTOOL output = off
75set expgui(showexptool) 0
76# save the name of the wish executable
77set wishshell [info nameofexecutable]
78# misc constants
79set txtvw(font) "Courier"
80set expgui(font) 14
81set liveplot(hst) 1
82set liveplot(legend) 1
83set expgui(filesort) 1
84set expgui(initstring) {}
85# use a separate window for DISAGL (default)
86set expgui(disaglSeparateBox) 1
87set expgui(DefaultPeakType) 0
88# default: keep current atoms when replacing a phase
89set expgui(DeleteAllAtoms) 0
90#=============================================================================
91#----------------------------------------------------------------
92# where are we?
93set expgui(script) [info script]
94# translate links -- go six levels deep
95foreach i {1 2 3 4 5 6} {
96    if {[file type $expgui(script)] == "link"} {
97        set link [file readlink $expgui(script)]
98        if { [file  pathtype  $link] == "absolute" } {
99            set expgui(script) $link
100        } {
101            set expgui(script) [file dirname $expgui(script)]/$link
102        }
103    } else {
104        break
105    }
106}
107# fixup relative paths
108if {[file pathtype $expgui(script)] == "relative"} {
109    set expgui(script) [file join [pwd] $expgui(script)]
110}
111set expgui(scriptdir) [file dirname $expgui(script) ]
112set expgui(gsasdir) [file dirname $expgui(scriptdir)]
113set expgui(gsasexe) [file join $expgui(gsasdir) exe]
114set expgui(docdir) [file join $expgui(scriptdir) doc]
115#----------------------------------------------------------------
116lappend auto_path $expgui(scriptdir)
117set expgui(havetix) 0
118set expgui(haveBW) 1
119# for debugging non-BWidget version set environment variable NOBWIDGET
120catch {if $env(NOBWIDGET) {set expgui(haveBW) 0}}
121if $expgui(haveBW) {
122    if [catch {package require BWidget}] {set expgui(haveBW) 0}
123}
124# get the notebook widget if not using BWidgets
125if {!$expgui(haveBW)} {source [file join $expgui(scriptdir) notebook.tcl]}
126#----------------------------------------------------------------
127source [file join $expgui(scriptdir) opts.tcl]
128# fetch EXP file processing routines
129source [file join $expgui(scriptdir) readexp.tcl]
130# commands for running GSAS programs
131source [file join $expgui(scriptdir) gsascmds.tcl]
132# contents of GSAS menus
133source [file join $expgui(scriptdir) gsasmenu.tcl]
134# commands for adding phases, histograms & atoms
135source [file join $expgui(scriptdir) addcmds.tcl]
136# commands for preferred orientation
137source [file join $expgui(scriptdir) orient.tcl]
138# setting data range/excluded regions
139source [file join $expgui(scriptdir) exclinit.tcl]
140#---------------------------------------------------------------------------
141# override options with locally defined values
142if {[catch {
143    foreach file [list \
144                      [file join $expgui(scriptdir) localconfig] \
145                      [file join ~ .gsas_config]] {
146        if [file exists $file] {source $file}
147    }
148} errmsg]} {
149    set msg "Error reading file $file (aka [file nativename $file]): $errmsg"
150    MyMessageBox -parent . -title "Customize warning" \
151        -message $msg -icon warning -type Ignore -default ignore \
152        -helplink "expguierr.html Customizewarning"
153}
154SetTkDefaultOptions $expgui(font)
155#---------------------------------------------------------------------------
156# platform-specific code
157if {$tcl_platform(platform) == "windows" \
158        && $tcl_platform(os) == "Windows 95"} {
159    if [catch {package require winexec}] {
160        MyMessageBox -parent . -title "WINEXEC Error" \
161                -message "Error -- Unable to load the WINEXEC package. This is needed in Win95 machines" \
162                -icon error -type Quit -default quit \
163                -helplink "expgui_Win_readme.html Winexec"
164        destroy .
165    }
166}
167if {$tcl_platform(platform) == "windows"} {
168    # check the path -- can DOS use it?
169    if {[string first {\\} $expgui(script) ] != -1} {
170        MyMessageBox -parent . -title "Networked Path" \
171                -message "Note -- You may have problems running EXPGUI/GSAS from a network drive. If you have errors, map $expgui(gsasdir) to a \"drive\" (like F:). (Use \"Map network drive\" to do this.)" \
172                -icon error -type {"Be brave"} -default "be brave" \
173                -helplink "expgui_Win_readme.html NetPath"
174    }
175    #
176    set expgui(exptool) [file join $expgui(gsasexe) exptool.exe]
177} else {
178    set expgui(exptool) [file join $expgui(gsasexe) exptool]
179    if [catch {set env(GSASBACKSPACE)}] {set env(GSASBACKSPACE) 1}
180}
181# do we have a PGPLOT fonts file?
182# if it is in the "wrong" place/name -- make it "right"
183if {![file exists [file join $expgui(gsasdir) pgl grfont.dat]] && \
184        [file exists [file join $expgui(gsasdir) fonts grfont.dat]]} {
185    catch {file mkdir [file join $expgui(gsasdir) pgl]}
186    file copy [file join $expgui(gsasdir) fonts grfont.dat] \
187            [file join $expgui(gsasdir) pgl grfont.dat]
188}
189# do we have a PGPLOT fonts file?
190if {![file exists [file join $expgui(gsasdir) pgl grfont.dat]] && \
191        [file exists [file join $expgui(gsasdir) fonts pgfont.dat]]} {
192    catch {file mkdir [file join $expgui(gsasdir) pgl]}
193    file copy [file join $expgui(gsasdir) fonts pgfont.dat] \
194            [file join $expgui(gsasdir) pgl grfont.dat]
195}
196# do we have a PGPLOT fonts file?
197if {![file exists [file join $expgui(gsasdir) pgl grfont.dat]] && \
198        [file exists [file join $expgui(gsasdir) pgl pgfont.dat]]} {
199    file copy [file join $expgui(gsasdir) pgl pgfont.dat] \
200            [file join $expgui(gsasdir) pgl grfont.dat]
201}
202if ![file exists [file join $expgui(gsasdir) pgl grfont.dat]] {
203    MyMessageBox -parent . -title "PGPLOT Error" \
204            -message "Warning -- Unable to find file GRFONT.DAT in [file join $expgui(gsasdir) pgl]. GSAS graphics will not work. Is GSAS correctly installed?" \
205            -icon warning -type {"Limp Ahead"} -default "Limp Ahead" \
206            -helplink "expguierr.html NoPGPLOT"
207}
208#---------------------------------------------------------------------------
209if {$expgui(expfile) != ""} {
210   # is there a space in the name?
211    if {[string first " " $expgui(expfile)] != -1} {
212        update
213        MyMessageBox -parent . -title "File Name Error" \
214                -message "File name $expgui(expfile) is invalid -- EXPGUI cannot process experiment files with spaces in the name" \
215                -icon warning -type Continue -default continue
216#               -helplink "expguierr.html OpenErr"
217        set expgui(expfile) {}
218    } elseif ![file exists $expgui(expfile)] {
219        update
220        set ans [
221        MyMessageBox -parent . -title "File Open Error" \
222                -message "File [file tail $expgui(expfile)] does not exist in [file dirname $expgui(expfile)]. OK to create?" \
223                -icon question -type {"Select other" "Create"} -default "select other" \
224                -helplink "expguierr.html OpenErr"
225        ]
226        if {[string tolower $ans] != "create"} {set expgui(expfile) {}}
227    }
228}
229if {$expgui(expfile) == ""} {
230    # center the parent window because the getExpFileName window
231    # will be centered above it.
232    wm withdraw .
233    set x [expr [winfo screenwidth .]/2 - [winfo reqwidth .]/2 ]
234    set y [expr [winfo screenheight .]/2 - [winfo reqheight .]/2]
235    wm geom . +$x+$y
236    wm deiconify .
237    # windows needed this update before when using tk_getOpenFile.
238    # I am not sure it is still needed.
239    update
240    #
241    set expgui(expfile) [getExpFileName old]
242}
243if {$expgui(expfile) == ""} exit
244# you've been warned this .EXP does not exist!
245if ![file exists $expgui(expfile)] {
246    # create an "empty" exp file
247    createexp $expgui(expfile) \
248            [getstring "title for experiment $expgui(expfile)" 60 0]
249}
250catch {cd [string trim [file dirname $expgui(expfile)]]}
251
252#
253# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
254# <<<<<<<<<<    BEGINNING OF MAIN: GLOBAL AREA FOR DATA EXTRACTION >>>>>>>>>>>
255# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
256# load exp file and set up dialogs
257proc loadexp {expfile} {
258    global expgui expmap entryvar entrycmd tcl_platform
259    # is this a compressed archive file?
260    if {[string match {*.O[0-9A-F][0-9A-F]} $expfile]} {
261        set expgui(expfile) {}
262        set expnam [file rootname $expfile]
263        set ans [MyMessageBox -parent . -title "Load Archived File" \
264                -message "Loading archived version of $expnam. Do you want to continue using the same experiment name or work with the archived version under a new name?" \
265                -icon question -type "{Use New Name} {Continue with current}" \
266                -default {Use New Name} \
267                -helplink "expguierr.html LoadArchived"
268        ]
269        # archive the current .EXP file
270        if {$ans != "use new name" && [file exists $expfile]} {
271            # get the last archived version
272            set lastf [lindex [lsort [glob -nocomplain $expnam.{O\[0-9A-F\]\[0-9A-F\]}]] end]
273            if {$lastf == ""} {
274                set num 01
275            } else {
276                regexp {.*\.O([0-9A-F][0-9A-F])$} $lastf a num
277                scan $num %x num
278                if {$num >= 255} {
279                    set num FF
280                } else {
281                    set num [string toupper [format %.2x [incr num]]]
282                }
283            }
284            catch {
285                set newfile $expnam.O$num
286                file rename -force $expnam.EXP $newfile
287                set fp [open $expnam.LST a+]
288                puts $fp "\n----------------------------------------------"
289                puts $fp "     Regressing to archive file [file tail $expfile]"
290                puts $fp "     but first archiving [file tail $expnam.EXP] as [file tail $newfile]"
291                puts $fp "----------------------------------------------\n"
292                close $fp
293            }
294        }
295        file copy -force $expfile $expnam.EXP
296        set fmt [expload $expfile]
297        # if the file was not in the correct format, force a rewrite before use
298        if {$fmt < 0} {
299            # read error
300            return
301        } elseif {$fmt == 1} {
302            set expgui(changed) 0
303        } else {
304            set expgui(changed) 1
305        }
306        set expfile $expnam.EXP
307        if {$ans == "use new name"} {
308            SaveAsFile
309            set expfile $expgui(expfile)
310        }
311    } else {
312        set exploadtime [time {set fmt [expload $expfile]}]
313        if $expgui(debug) {puts "expload $exploadtime"}
314        # if the file was not in the correct format, force a rewrite before use
315        if {$fmt < 0} {
316            # read error
317            return
318        } elseif {$fmt == 1} {
319            set expgui(changed) 0
320        } else {
321            set expgui(changed) 1
322        }
323    }
324    # force exp files to be upper case, force save if name changes
325    set filetail [file tail $expfile]
326    set filetailcaps [string toupper $filetail]
327    if {$tcl_platform(platform) == "unix" && $filetail != $filetailcaps} {
328        set expgui(changed) 1
329    }
330    if {[file dirname $expfile] == "."} {
331        set expgui(expfile) $filetailcaps
332    } else {
333        set expgui(expfile) [file join \
334                [file dirname $expfile] $filetailcaps]
335    }
336
337    mapexp
338    set expgui(expModifiedLast) 0
339    catch {
340        set expgui(expModifiedLast) [file mtime $expgui(expfile)]
341    }
342    set expgui(last_History) [string range [string trim [lindex [exphistory last] 1]] 0 50 ]
343    # set the window/icon title
344    wm title . "EXPGUI $expfile"
345    set expgui(titleunchanged) 1
346    wm iconname . [file tail $expfile]
347
348    # reset the phase buttons
349    set expgui(curPhase) ""
350    # set the number of phases on the phase page
351    setphases
352
353    # disable the "global options" that don't make sense based on
354    # which histograms present
355    foreach num {1 2 3 4 5} {
356        set flag($num) 0
357    }
358    # save a list of the allowed modes, too
359    set expgui(AllowedHistSelectModes) {0 6}
360    foreach h $expmap(powderlist) {
361        if {[string range $expmap(htype_$h) 2 2] == "T"} {set flag(1) 1}
362        if {[string range $expmap(htype_$h) 1 2] == "NC"} {set flag(2) 1}
363        if {[string range $expmap(htype_$h) 1 2] == "XC" && \
364                [histinfo $h lam2] != 0.0} {set flag(3) 1}
365        if {[string range $expmap(htype_$h) 1 2] == "XC" && \
366                [histinfo $h lam2] == 0.0} {set flag(4) 1}
367        if {[string range $expmap(htype_$h) 1 2] == "XE"} {set flag(5) 1}
368    }
369    foreach num {1 2 3 4 5} \
370            lbl {TOF "CW Neutron" "Alpha12 Xray" "Monochromatic Xray" \
371            "Energy Disp Xray"} {
372        if $flag($num) {
373            $expgui(fm).option.menu.editmode entryconfigure $lbl -state normal
374            lappend expgui(AllowedHistSelectModes) $num
375        } else {
376            $expgui(fm).option.menu.editmode entryconfigure $lbl -state disabled
377        }
378    }
379    # disable traces on entryvar until we are ready
380    set entrycmd(trace) 0
381    trace vdelete entryvar w entvartrace
382
383    # propogate changes on the least squares page
384    set entryvar(cycles) [expinfo cycles]
385    set entrycmd(cycles) "expinfo cycles"
386    # set expgui(globalmode) 0
387    set expgui(printopt) "Print Options ([expinfo print])"
388    set entryvar(title) [expinfo title]
389    global printopts
390    foreach num [array names printopts] {
391        set entrycmd(printopt$num) "printsetting $num"
392        set entryvar(printopt$num) [printsetting $num]
393    }
394    # enable traces on entryvar
395    set entrycmd(trace) 1
396    trace variable entryvar w entvartrace
397
398    # set fo extraction on LS page
399    SetupExtractHist
400    # set convergence criterion
401    InitLSvars
402
403    # update the histogram list & update the page
404    sethistlist
405
406    # start checking for external changes
407    afterawhile
408}
409
410# called to reread the .EXP file
411proc rereadexp {expfile} {
412    global expgui
413    if $expgui(changed) {
414        set decision [tk_dialog .instrSaveData {Save .EXP changes} \
415                {You have made changes to the Experiment. Rereading will cause the changes to be lost. Select an option:} \
416                {} 0 "Save and reread" "Reread without Save" "Cancel reread command"]
417        switch $decision {
418            0 { savearchiveexp }
419            1 { }
420            2 { return }
421        }
422    }
423    loadexp $expgui(expfile)
424}
425
426proc SaveAsFile {} {
427    global expgui
428    set newexpfile [getExpFileName new]
429    if {$newexpfile == ""} return
430    expwrite $newexpfile
431    set expgui(expfile) $newexpfile
432    catch {cd [string trim [file dirname $expgui(expfile)]]}
433    set expgui(changed) 0
434    set expgui(expModifiedLast) [file mtime $expgui(expfile)]
435    set expgui(last_History) [string range [string trim [lindex [exphistory last] 1]] 0 50 ]
436    # set the window/icon title
437    wm title . $expgui(expfile)
438    set expgui(titleunchanged) 1
439    wm iconname . [file tail $expgui(expfile)]
440    # set convergence criterion
441    InitLSvars
442}
443
444# called to read a different .EXP file
445proc readnewexp {} {
446    global expgui expmap
447    if $expgui(changed) {
448        set decision [tk_dialog .instrSaveData {Save .EXP changes} \
449                {You have made changes to the Experiment. Reading a different file without first saving will cause the changes to be lost. Select an option:} \
450                {} 0 "Save and read" "Read without Save" "Cancel read command"]
451        switch $decision {
452            0 { savearchiveexp }
453            1 {                }
454            2 { return }
455        }
456    }
457    set newexpfile [getExpFileName old]
458    if {$newexpfile == ""} return
459
460    # switch to the 1st page
461    RaisePage lsFrame
462
463    if ![file exists $newexpfile] {
464        # you've been warned this .EXP does not exist!
465        # create an "empty" exp file
466        createexp $newexpfile \
467                [getstring "title for experiment $newexpfile" 60 0]
468    }
469    set expgui(expfile) $newexpfile
470    catch {cd [string trim [file dirname $expgui(expfile)]]}
471    set expgui(globalmode) 0
472    loadexp $expgui(expfile)
473
474    # reset the phase selection
475    set expgui(curPhase) {}
476
477    # select the first histogram in the list by default (if there are any)
478    if {[llength $expmap(histlistboxcontents)] > 0} {
479        set expgui(curhist) 0
480    } else {
481        set expgui(curhist) {}
482    }
483    if {[CountHistory] > 100} {
484        DeleteHistoryRecords "This .EXP file has [CountHistory] history records\nErasing most will speed EXPGUI"
485    }
486}
487
488#------------- set up data read/write layer ----------------------
489# trace routine on entryvar
490proc entvartrace {array elem action} {
491    global expgui entrycmd entryvar entrybox
492    if !$entrycmd(trace) return
493   
494    catch {
495        if {$entrycmd($elem) == ""} return
496        incr expgui(changed)
497        if $expgui(debug) {puts "$entrycmd($elem)  set $entryvar($elem) "}
498        if {$entrycmd($elem) == ""} return
499        if [catch {
500            set result [eval $entrycmd($elem) set [list $entryvar($elem)]]
501            if {!$result} {
502                if $expgui(debug) {puts "error with $entrycmd($elem)"}
503                catch {$entrybox($elem) config -fg red}
504            } else {
505                catch {$entrybox($elem) config -fg black}
506            }
507            if {[string match "*atominfo" [lindex $entrycmd($elem) 0]]} {
508                after idle "UpdateAtomLine \
509                        [list [lindex $entrycmd($elem) 2]] \
510                        [lindex $entrycmd($elem) 1]"
511            }
512        } errmsg] {error $errmsg}       
513    }
514}
515
516# disable traces on entryvar until we are ready
517set entrycmd(trace) 0
518trace variable entryvar w entvartrace
519
520#
521#
522#
523##############################################################################
524#####                    #####################################################
525##### PROCEDURES SECTION #####################################################
526#####                    #####################################################
527##############################################################################
528
529# save some of the global options in ~/.gsas_config
530proc SaveOptions {} {
531    global expgui env tcl_platform graph peakinfo
532    set fp [open [file join ~ .gsas_config] a]
533    foreach item {archive asorttype hsorttype filesort disaglSeparateBox \
534            font autoexpload autoiconify autotick} {
535        puts $fp "set expgui($item) [list $expgui($item)]"
536    }
537    if {$tcl_platform(platform) != "windows"} {
538        puts $fp "set env(GSASBACKSPACE) [list $env(GSASBACKSPACE)]"
539    }
540    foreach v {printout legend outname outcmd autoraise color_excl \
541            color_obs color_calc} {
542        puts $fp "set graph($v) [list $graph($v)]"
543    }
544    foreach v {obssym obssize exclsym exclsize} {
545        puts $fp "set peakinfo($v) [list $peakinfo($v)]"
546    }
547    close $fp
548}
549
550proc About { } {
551    global expgui expmap
552    tk_dialog .about {About...} \
553"EXPGUI\n\
554Jonathan Wasserman and Brian Toby\n\
555NIST Center for Neutron Research\n\n\
5562000, Not subject to copyright\n\n\
557Revision [lindex $expgui(Revision) 1] (readexp.tcl [lindex $expmap(Revision) 1])\n\n\
558Cite: B. H. Toby, EXPGUI, a graphical\n\
559user interface for GSAS, J. Appl. Cryst.\n\
56034, 210-21 (2001).
561\n\n\
562Generalized Structure Analysis System\n(GSAS)\n\
563A. C. Larson and\n R. B. Von Dreele,\n LANSCE, Los Alamos\n\n\
564" \
565        info 0 OK
566}
567proc Cite { } {
568    global expgui expmap
569    tk_dialog .about {Citations...} \
570"If you use EXPGUI, please cite\n\n\
571B.H. Toby, EXPGUI, a graphical\n\
572user interface for GSAS, J. Appl. Cryst.\n\
57334, 210-21 (2001).\n\n\
574as well as\n\n\
575A.C. Larson and R.B. Von Dreele,\n\
576\"General Structure Analysis System (GSAS)\",\n\
577Los Alamos National Laboratory Report\n\
578LAUR 86-748 (2000)." \
579        info 0 OK
580}
581
582# this proc gets called when the export coordinate button is pressed
583# it loads export
584proc BuildCoordExpMenu {menu} {
585    global expgui_cmdlist expgui
586    # do this only once
587    $menu config -postcommand {}
588    $menu delete 1 end
589    # add the cif export routine
590    set cmd gsas2cif
591    set action {}
592    catch {set action [lindex $expgui_cmdlist($cmd) 0]}
593    if {$action != "" && $action != "-"} {
594        $menu add command -label $cmd -command [subst $action]
595    }
596    # get a list of files to read
597    set filelist [glob -nocomplain [file join $expgui(scriptdir) export_*.tcl]]
598    foreach file $filelist {
599        source $file
600        $menu add command -label $label -command $action
601    }
602}
603
604# utility export routines for the export_*.tcl files:
605# make a box for export
606proc MakeExportBox {win title webref} {
607    global expmap expgui
608    catch {destroy $win}
609    toplevel $win
610    wm title $win "Export coordinates"
611    if {$webref != ""} {
612        bind $win <Key-F1> $webref
613    }
614    pack [label $win.lbl -text $title] -side top -anchor center
615    pack [frame $win.ps] -side top -anchor w
616    pack [label $win.ps.lbl -text "Select phase: "] -side left
617    foreach num $expmap(phaselist) {
618        pack [button $win.ps.$num -text $num \
619                    -command "SetExportPhase $num $win"] -side left
620    }
621    # leave a place for format-specific items
622    pack [frame $win.special] -side top
623    pack [frame $win.but] -side top -fill x -expand yes
624    pack [button $win.but.1 -text Write -command "destroy $win"] -side left
625    SetExportPhase [lindex $expmap(phaselist) 0] $win
626    pack [button $win.but.2 -text Quit \
627            -command "set expgui(export_phase) 0;destroy $win"] -side left
628    pack [button $win.but.help -text Help -bg yellow \
629            -command "MakeWWWHelp expgui.html ExportMSI"] \
630            -side right
631}
632
633# set the phase in response to the button
634proc SetExportPhase {num win} {
635    global expmap expgui
636    foreach n $expmap(phaselist) {
637        if {$n == $num} {
638            $win.ps.$n config -relief sunken;
639            set expgui(export_phase) $num
640        } else {
641            $win.ps.$n config -relief raised
642        }
643    }
644}
645
646# wait until idle
647proc afterawhile {} {
648    # cancel any other instances of this loop
649    after cancel afterawhile
650    after cancel whenidle
651    after cancel whenidle
652    after idle whenidle
653}
654
655# This is called every 2 seconds to check for changes to the .EXP file
656proc whenidle {} {
657    global expgui tcl_platform
658    if $expgui(titleunchanged) {
659        if {$expgui(changed) != 0} {
660            wm title . "$expgui(expfile) (modified)"
661            set expgui(titleunchanged) 0
662        }
663    }
664    if {$expgui(expModifiedLast) == 0} {
665        after 2000 afterawhile
666        return
667    }
668    if {![file exists $expgui(expfile)]} {
669        after 2000 afterawhile
670        return
671    }
672    if {[file mtime $expgui(expfile)] != $expgui(expModifiedLast)} {
673        # we are "locked". Note that whenidle loop will be restarted later
674        if {$tcl_platform(platform) == "windows" && [file exists expgui.lck]} {
675            return
676        }
677        set ans [ReloadExpMsg [file tail $expgui(expfile)] $expgui(changed)]
678
679        if {$ans == 0} {
680            loadexp $expgui(expfile)
681        } elseif {$ans == 1} {
682            # reset the time to the next version
683            set expgui(expModifiedLast) [file mtime $expgui(expfile)]
684        } elseif {$ans == 2} {
685            SaveAsFile
686        }
687    }
688    after 2000 afterawhile
689}
690
691# place a message about changes over the main window
692proc ReloadExpMsg {file changes} {
693    global expgui tcl_platform
694    set msg "File $file has been modified by another program"
695    if {$changes == 1} {
696        append msg " and you have made a change to this version.\n"
697    } elseif {$changes > 0} {
698        append msg " and you have made $changes changes to this version.\n"
699    } else {
700        append msg ".\n"
701    }
702    append msg "Do you want to use the newer (modified) version or continue with the older (previous) version of the file?"
703
704    set w .ask
705    catch {destroy $w}
706    toplevel $w -class Dialog
707    wm title $w "Reload?"
708    wm iconname $w "Reload?"
709    wm protocol $w WM_DELETE_WINDOW { }
710    wm transient $w .
711    bind $w <Key-F1> "MakeWWWHelp expguierr.html Overwrite"
712    pack [button $w.help -text Help -bg yellow \
713            -command "MakeWWWHelp expguierr.html Overwrite"] \
714            -side top -anchor e
715    frame $w.bot
716    pack $w.bot -side bottom
717    frame $w.top -class FixedFont
718    pack $w.top -side top -fill both -expand 1
719    label $w.top.msg -justify left \
720            -wraplength 5i -font {Times 18} \
721            -text $msg
722    if {$tcl_platform(platform) == "windows"} {
723        $w.top.msg config -font {Times 14}
724    }
725    pack $w.top.msg  -side right -expand 1 -fill both -padx 3m -pady 3m
726    pack [button $w.bot.1 -text "Load new" \
727            -default active -command "set expgui(dialogbutton) 0" \
728            ] -side left -expand 1 -padx 3m -pady 2m
729    pack [button $w.bot.2 -text "Continue with old" \
730            -command "set expgui(dialogbutton) 1"] \
731            -side left -expand 1 -padx 3m -pady 2m
732    if {$changes > 0} {
733        pack [button $w.bot.3 -text "Save edited version" \
734            -command "set expgui(dialogbutton) 2"] \
735            -side left -expand 1 -padx 3m -pady 2m
736    }
737    # Create a binding for <Return> on the dialog
738    bind $w <Return> "tkButtonInvoke $w.bot.1"
739    wm withdraw $w
740    update idletasks
741
742    # for windows put the box in the upper left, for
743    # unix center it over the parent (in unix it appears later)
744    #if {$tcl_platform(platform) == "windows"} {
745        #wm geom $w +0+0
746    #} else {
747
748        # for now, always center the message over the main window
749        # center the new window in the middle of the parent
750        set x [expr [winfo x .] + [winfo width .]/2 - \
751                [winfo reqwidth $w]/2 - [winfo vrootx .]]
752        set y [expr [winfo y .] + [winfo height .]/2 - \
753                [winfo reqheight $w]/2 - [winfo vrooty .]]
754        wm geom $w +$x+$y
755    #}
756    wm deiconify $w
757
758    # Grab the focus
759    set oldFocus [focus]
760    set oldGrab [grab current $w]
761    if {[string compare $oldGrab ""]} {
762        set grabStatus [grab status $oldGrab]
763    }
764    catch {grab $w}
765    focus $w.bot.1
766    # for windows rearrange window stacking
767    # -- Removed since this will normally happen after the GSAS program
768    # has finished
769    #if {$tcl_platform(platform) == "windows"} {
770        #lower .
771        #raise $w .
772    #}
773    update idletasks
774
775    tkwait variable expgui(dialogbutton)
776    catch {focus $oldFocus}
777    destroy $w
778    if {[string compare $oldGrab ""]} {
779        if {![string compare $grabStatus "global"]} {
780            catch {grab -global $oldGrab}
781        } else {
782            catch {grab $oldGrab}
783        }
784    }
785    # for windows rearrange window stacking
786    #if {$tcl_platform(platform) == "windows"} {
787        #raise .
788    #}
789    return $expgui(dialogbutton)
790}
791
792# --------  called to confirm before exiting
793proc catchQuit {} {
794    if {[confirmBeforeSave] == "Continue"} {
795        destroy .
796    }
797}
798# save the .EXP file before exiting?
799proc confirmBeforeSave {} {
800    global expgui
801    if !$expgui(changed) {
802        return "Continue"
803    }
804    set decision [tk_dialog .instrSaveData {Save .EXP changes} \
805            {You have made changes to the Experiment, but the changes are not saved. Select an option:} \
806            {} 0 "Save and Exit" "Exit without Save" "Cancel exit command"]
807    switch $decision {
808        0 { savearchiveexp;  return "Continue" }
809        1 {                  return "Continue" }
810        2 {                  return "Cancel"   }
811    }
812}
813
814# setup buttons for each phase on the phase page
815proc setphases {} {
816    global expgui expmap
817    eval destroy [winfo children $expgui(phaseFrame).top.ps]
818    pack [label $expgui(phaseFrame).top.ps.0 -text Phase:] -side left
819    foreach num $expmap(phaselist) {
820        pack [button $expgui(phaseFrame).top.ps.$num -text $num \
821                -command "SelectOnePhase $num" -padx 1.5m] -side left
822    }
823    if {[file executable $expgui(exptool)] && \
824            [llength $expmap(phaselist)]} {
825        pack [button $expgui(phaseFrame).top.ps.10 \
826                -text "Replace" -command MakeReplacePhaseBox \
827                ] -side left
828    }
829}
830
831# Procedure to respond to changes the phase.
832#  This loads the "phases" widgets with data corresponding to the selected phase.
833proc SelectOnePhase {num} {
834    global entryvar entrycmd entrybox expmap expgui
835    # if no phase has been selected, select the first one
836    if {$num == ""} {set num [lindex $expmap(phaselist) 0]}
837
838    set crsPhase {}
839    $expgui(atomxform) config -text "Xform Atoms" -state disabled
840    foreach n $expmap(phaselist) type $expmap(phasetype) {
841        if {$n == $num} {
842            catch {$expgui(phaseFrame).top.ps.$num config -relief sunken}
843            set crsPhase $num
844            if {$type == 3} {
845                set expgui(phasetype) "Magnetic\nOnly"
846            } elseif {$type == 2} {
847                set expgui(phasetype) "Magnetic\n& Nuclear"
848            } elseif {$type == 4} {
849                set expgui(phasetype) "Macromolecular"
850            } else {
851                set expgui(phasetype) ""
852            }
853        } else {
854            catch {$expgui(phaseFrame).top.ps.$n config -relief raised}
855        }
856    }
857    # no phase is selected
858    if {$crsPhase == "" || [llength $expmap(phaselist)] == 0} {
859        # disable traces on entryvar
860        set entrycmd(trace) 0
861        set entrycmd(phasename) ""
862        set entryvar(phasename) ""
863        foreach ent {a b c alpha beta gamma cellref celldamp} {
864            set entrycmd($ent) ""
865            set entryvar($ent) ""
866        }
867        set expgui(curPhase) {}
868        # enable traces on entryvar
869        set entrycmd(trace) 1
870        $expgui(EditingAtoms) config -text ""
871        DisplayAtom 0 0
872        DisplayU 0 0
873        DisplayRefFlags 0 0
874        $expgui(atomlistbox) delete 0 end
875        return
876    }
877
878    # don't reload the last displayed phase
879    if {$expgui(curPhase) == $crsPhase} return
880
881    ##########################################################
882    # load and display a phase
883    ##########################################################
884    # disable traces on entryvar while loading
885    set entrycmd(trace) 0
886    # phase title
887    set entrycmd(phasename) "phaseinfo $crsPhase name"
888    set entryvar(phasename) [phaseinfo $crsPhase name]
889    # cell parameters & flags
890    foreach ent {a b c alpha beta gamma cellref celldamp} {
891        set entrycmd($ent) "phaseinfo $crsPhase $ent"
892        set entryvar($ent) [phaseinfo $crsPhase $ent]
893        # reset item to black
894        catch {$entrybox($ent) config -fg black}
895    }
896
897    # initialize atoms display & disable
898    DisplayAtom 0 0
899    DisplayU 0 0
900    DisplayRefFlags 0 0
901    $expgui(EditingAtoms) config -text ""
902
903    DisplayAllAtoms $crsPhase
904
905    # enable traces on entryvar now
906    set entrycmd(trace) 1
907}
908
909set expgui(noreenterDisplayAllAtoms) 0
910# Populate expgui(atomlistbox) (a ScrolledListBox) with atoms
911# from the selected phase.
912proc DisplayAllAtoms {curPhase "mode reset"} {
913    global entryvar entrycmd expmap expgui
914    # if it does not show, we don't have a phase or we are already displaying
915    # don't bother
916    if {$expgui(pagenow) != "phaseFrame"} return
917    if {$curPhase == ""} return
918    if $expgui(noreenterDisplayAllAtoms) return
919    # prevent reentry
920    set expgui(noreenterDisplayAllAtoms) 1
921    # set the current phase
922    set expgui(curPhase) $curPhase
923    if {$mode != "reset"} {
924        # save the scrolled position
925        set pos [lindex [$expgui(atomlistbox) yview] 0]
926    } else {
927        # for reset, do not keep the previously selected atoms
928        set expgui(selectedatomlist) {}
929    }
930    $expgui(atomlistbox) delete 0 end
931    # displaying a macromolecular phase?
932    if {[lindex $expmap(phasetype) [expr {$expgui(curPhase) - 1}]] == 4} {
933        set mm 1
934        $expgui(phaseFrame).top.ps.10 config -state disabled
935        $expgui(AddAtomBut)  config -state disabled
936        pleasewait "loading atoms..."
937    } else {
938        set mm 0
939        if {[file executable $expgui(exptool)]} {
940            $expgui(phaseFrame).top.ps.10 config -state normal
941            $expgui(AddAtomBut) config -state normal
942        }
943    }
944
945    # prepare header info
946    set maxline I
947    set phase $expgui(curPhase)
948    set atomlist {}
949    set typehead "type  "
950    set namehead "  name  "
951    set multhead "Mult"
952    set coordhead "   "
953    if {$mm} {
954        set cmd mmatominfo
955        set frachead "Occ."
956    } else {
957        set cmd atominfo
958        set frachead "Occupancy"
959    }
960    set reshead "res/grp/#"
961    # sort the atoms, as requested
962    if  {$expgui(asorttype) == "type"} {
963        # sort on atom type
964        set typehead "type* "
965        foreach atom $expmap(atomlist_$phase) {
966            lappend atomlist "$atom [$cmd $phase $atom type] $phase"
967        }
968        set expmap(atomlistboxcontents) [lsort -ascii -index 1 $atomlist]
969    } elseif {$expgui(asorttype) == "number"} {
970        # sort on atom number
971        set namehead "* name  "
972        foreach atom $expmap(atomlist_$phase) {
973            lappend atomlist "$atom $atom $phase"
974        }
975        set expmap(atomlistboxcontents) [lsort -integer -index 1 $atomlist]
976    } elseif {$expgui(asorttype) == "mult"} {
977        if {$mm} {
978            set reshead "res*/grp/#"
979            foreach atom $expmap(atomlist_$phase) {
980                lappend atomlist "$atom [mmatominfo $phase $atom residue] $phase"
981            }
982            set expmap(atomlistboxcontents) [lsort -ascii -index 1 $atomlist]
983        } else {
984            # sort on atom number
985            set multhead "Mlt*"
986            foreach atom $expmap(atomlist_$phase) {
987                lappend atomlist "$atom [atominfo $phase $atom mult] $phase"
988            }
989            set expmap(atomlistboxcontents) [lsort -integer -decreasing -index 1 $atomlist]
990        }
991    } elseif {$expgui(asorttype) == "occupancy"} {
992        # sort on atom number
993        if {$mm} {
994            set frachead "  Occ* "
995        } else {
996            set frachead "  Occup* "
997        }
998        foreach atom $expmap(atomlist_$phase) {
999            lappend atomlist "$atom [$cmd $phase $atom frac] $phase"
1000        }
1001        set expmap(atomlistboxcontents) [lsort -real -decreasing -index 1 $atomlist]
1002    } elseif {$expgui(asorttype) == "x"} {
1003        # sort on x
1004        set coordhead "(x*)"
1005        foreach atom $expmap(atomlist_$phase) {
1006            lappend atomlist "$atom [$cmd $phase $atom x] $phase"
1007        }
1008        set expmap(atomlistboxcontents) [lsort -real -index 1 $atomlist]
1009    } elseif {$expgui(asorttype) == "y"} {
1010        # sort on y
1011        set coordhead "(y*)"
1012        foreach atom $expmap(atomlist_$phase) {
1013            lappend atomlist "$atom [$cmd $phase $atom y] $phase"
1014        }
1015        set expmap(atomlistboxcontents) [lsort -real -index 1 $atomlist]
1016    } elseif {$expgui(asorttype) == "z"} {
1017        # sort on z
1018        set coordhead "(z*)"
1019        foreach atom $expmap(atomlist_$phase) {
1020            lappend atomlist "$atom [$cmd $phase $atom z] $phase"
1021        }
1022        set expmap(atomlistboxcontents) [lsort -real -index 1 $atomlist]
1023    } else {
1024        error "Bad expgui(asorttype) = $expgui(asorttype)"
1025    }
1026
1027    set expgui(atomlistboxline) {}
1028    # loop over atoms
1029    foreach tuple $expmap(atomlistboxcontents) {
1030        set atom [lindex $tuple 0]
1031        set phase [lindex $tuple 2]
1032        lappend expgui(atomlistboxline) $atom
1033        $expgui(atomlistbox) insert end \
1034                [FormatAtomLine $atom $phase maxline]
1035    }
1036    $expgui(atomtitle) delete 0 end
1037
1038    # create the header
1039    if {$mm} {
1040        $expgui(atomtitle) insert end [format "%12s %9s  %6s %8s%29s %4s  %s" \
1041                $namehead $reshead $typehead "ref/damp  " \
1042                "fractional coordinates$coordhead" \
1043                "$frachead" \
1044                " Uiso"]
1045        donewait
1046    } elseif {$maxline == "A"} {
1047        $expgui(atomtitle) insert end [format "%10s %6s %8s%29s %9s  %s" \
1048                $namehead $typehead "ref/damp  " \
1049                "fractional coordinates$coordhead" \
1050                "$multhead $frachead" \
1051                " Uiso/Uij                                            "]
1052    } else {
1053        $expgui(atomtitle) insert end [format "%10s %6s %8s%29s %9s  %s" \
1054                $namehead $typehead "ref/damp  " \
1055                "fractional coordinates$coordhead" \
1056                "$multhead $frachead" \
1057                " Uiso"]
1058    }
1059    if {$mode != "reset"} {
1060        # restore the selected items
1061        foreach i $expgui(selectedatomlist) {
1062            $expgui(atomlistbox) selection set $i
1063        }
1064        # restore the last scrolled position
1065        $expgui(atomlistbox) yview moveto $pos
1066    }
1067    # clear the reentry flag
1068    set expgui(noreenterDisplayAllAtoms) 0
1069}
1070
1071proc FormatAtomLine {atom phase maxline_var} {
1072    global expmap
1073    if {[lindex $expmap(phasetype) [expr {$phase - 1}]] == 4} {
1074        foreach type {x u f} {
1075            if {[mmatominfo $phase $atom ${type}ref]} {
1076                append refflag "[string toupper $type][mmatominfo $phase $atom ${type}damp] "
1077            } else {
1078                append refflag " [mmatominfo $phase $atom ${type}damp] "
1079            }   
1080        }
1081        set line [format \
1082                "%5d %-6s %-3s%-2s%4d  %-6s %8s %9.5f%9.5f%9.5f%8.4f %7.4f" \
1083                $atom \
1084                [mmatominfo $phase $atom label] \
1085                [mmatominfo $phase $atom residue] \
1086                [mmatominfo $phase $atom group] \
1087                [mmatominfo $phase $atom resnum] \
1088                [mmatominfo $phase $atom type] \
1089                $refflag \
1090                [mmatominfo $phase $atom x] \
1091                [mmatominfo $phase $atom y] \
1092                [mmatominfo $phase $atom z] \
1093                [mmatominfo $phase $atom frac] \
1094                [mmatominfo $phase $atom Uiso]
1095        ]
1096    } elseif {[atominfo $phase $atom temptype] == "A"} {
1097        foreach type {x u f} {
1098            if {[atominfo $phase $atom ${type}ref]} {
1099                append refflag "[string toupper $type][atominfo $phase $atom ${type}damp] "
1100            } else {
1101                append refflag " [atominfo $phase $atom ${type}damp] "
1102            }
1103        }
1104        # want to set maxline in parent
1105        upvar $maxline_var maxline
1106        set maxline A
1107        # aniso
1108        set line [format "%3d %-6s %-6s %8s %10.6f%10.6f%10.6f%4d%9.4f" \
1109                $atom \
1110                [atominfo $phase $atom label] \
1111                [atominfo $phase $atom type] \
1112                $refflag \
1113                [atominfo $phase $atom x] \
1114                [atominfo $phase $atom y] \
1115                [atominfo $phase $atom z] \
1116                [atominfo $phase $atom mult] \
1117                [atominfo $phase $atom frac]
1118        ]
1119        append line [format "  %9.5f%9.5f%9.5f%9.5f%9.5f%9.5f" \
1120                [atominfo $phase $atom U11] \
1121                [atominfo $phase $atom U22] \
1122                [atominfo $phase $atom U33] \
1123                [atominfo $phase $atom U12] \
1124                [atominfo $phase $atom U23] \
1125                [atominfo $phase $atom U13]
1126        ]
1127    } else {
1128        foreach type {x u f} {
1129            if {[atominfo $phase $atom ${type}ref]} {
1130                append refflag "[string toupper $type][atominfo $phase $atom ${type}damp] "
1131            } else {
1132                append refflag " [atominfo $phase $atom ${type}damp] "
1133            }   
1134        }
1135        set line [format \
1136                "%3d %-6s %-6s %8s %10.6f%10.6f%10.6f%4d%9.4f  %9.5f" \
1137                $atom \
1138                [atominfo $phase $atom label] \
1139                [atominfo $phase $atom type] \
1140                $refflag \
1141                [atominfo $phase $atom x] \
1142                [atominfo $phase $atom y] \
1143                [atominfo $phase $atom z] \
1144                [atominfo $phase $atom mult] \
1145                [atominfo $phase $atom frac] \
1146                [atominfo $phase $atom Uiso]
1147        ]
1148    }
1149    return $line
1150}
1151
1152# update the display of atom as they are changed
1153proc UpdateAtomLine {atomlist phase} {
1154    global expgui
1155    # for lots of atoms, it is faster to repaint the listbox
1156    if {[llength $atomlist] > 25} {
1157        DisplayAllAtoms $expgui(curPhase) noreset
1158        return
1159    }
1160    foreach atom $atomlist {
1161        set linenum [lsearch -exact $expgui(atomlistboxline) $atom]
1162        $expgui(atomlistbox) delete $linenum
1163        $expgui(atomlistbox) insert $linenum \
1164                [FormatAtomLine $atom $phase maxline]
1165    }
1166    # restore the selected items
1167    foreach i $expgui(selectedatomlist) {
1168        $expgui(atomlistbox) selection set $i
1169    }
1170}
1171
1172# Procedure to select all atoms in response to a right-click
1173proc SelectAllAtoms {} {
1174    global expgui
1175    $expgui(atomlistbox) selection set 0 end
1176    # call editRecord in case trace was called before the selection was made
1177    editRecord
1178}
1179
1180# Procedure to respond to left mouse release in the atoms Pane
1181proc editRecord { args } {
1182    global entrycmd expgui
1183    set expgui(selectedatomlist) [$expgui(atomlistbox) curselection]
1184    # disable traces on entryvar for right now
1185    set entrycmd(trace) 0
1186
1187    if {[llength $expgui(selectedatomlist)] == 0} {
1188        if $expgui(debug) {error "Attempt display non-existent atoms"}
1189    } elseif {[llength $expgui(selectedatomlist)] == 1} {
1190        editOneRecord $expgui(selectedatomlist)
1191    } else {
1192        editMultipleRecords $expgui(selectedatomlist)
1193    }
1194    # reenable traces on entryvar
1195    set entrycmd(trace) 1
1196    # repaint the atoms box in case anything was changed
1197    #    DisplayAllAtoms noreset
1198}
1199
1200proc editOneRecord { AtomIndex } {
1201    global expmap expgui
1202    # get atom number & phase
1203    set tuple [lindex $expmap(atomlistboxcontents) $AtomIndex]
1204    set atomnum [lindex $tuple 0]
1205    set p [lindex $tuple 2]
1206    DisplayU $atomnum $p
1207    DisplayAtom $atomnum $p
1208    DisplayRefFlags $atomnum $p
1209    $expgui(EditingAtoms) config -text "Editing atom #$atomnum -- [atominfo $p $atomnum label]"
1210    $expgui(atomxform) config -text "Xform Atom" -state normal
1211}
1212
1213# this will not work for a multi-phase list of atoms (yet)
1214proc editMultipleRecords { AtomIndexList } {
1215    global expmap expgui
1216    set numberList {}
1217    # current phase
1218    set p $expgui(curPhase)
1219    foreach AtomIndex $AtomIndexList {
1220        # get atom number & phase
1221        set tuple [lindex $expmap(atomlistboxcontents) $AtomIndex]
1222        lappend numberList [lindex $tuple 0]
1223#       set p [lindex $tuple 2]
1224    }
1225    # this needs to track by phase
1226    $expgui(EditingAtoms) config -text \
1227            "Set refinement options: atoms [CompressList $numberList]"
1228    DisplayU 0 0
1229    DisplayAtom 0 0
1230    # this needs to track by phase
1231    DisplayRefFlags $numberList $p
1232    $expgui(atomxform) config -text "Xform Atoms" -state normal
1233}
1234
1235# format a string of numbers to save space, e.g. "1 2 3 4 6 7 19 13 14 15"
1236# becomes "1-4,6,7,13-15,19"
1237proc CompressList {numberList "max 9999"} {
1238    # format the number list to save space
1239    set lastnum -99
1240    set flist {}
1241    set count 0
1242    set length 0
1243    if [catch {set sortlist [lsort -integer $numberList]}] {return $numberList}
1244    foreach num $sortlist {
1245        set next [expr $lastnum+1]
1246        if {$num != $next} {
1247            if {$count == 0 && $flist != ""} {
1248                if {[string length $flist] - $length > $max} {
1249                    set length [string length $flist]
1250                    append flist ",\n$num"
1251                } else {
1252                    append flist ",$num"
1253                }
1254            } elseif {$count == 1 && $flist != ""} {
1255                if {[string length $flist] - $length > $max} {
1256                    set length [string length $flist]
1257                    append flist ",$lastnum,\n$num"
1258                } else {
1259                    append flist ",$lastnum,$num"
1260                }
1261            } elseif {$flist != ""} {
1262                if {[string length $flist] - $length > $max} {
1263                    set length [string length $flist]
1264                    append flist "-$lastnum,\n$num"
1265                } else {
1266                    append flist "-$lastnum,$num"
1267                }
1268            } else {
1269                append flist "$num"
1270            }
1271            set lastnum $num
1272            set count 0
1273        } else {
1274            incr count
1275            incr lastnum
1276        }
1277    }
1278    if {$count == 1 && $flist != ""} {
1279        append flist ",$lastnum"
1280    } elseif {$flist != "" && $count > 1} {
1281        append flist "-$lastnum"
1282    }
1283    return $flist
1284}
1285
1286# Procedure to display Isotropic or Anisotropic temperature factors
1287#  Changes the display to one entry widget for Isotropic motion OR
1288#   6 entry widgets for Anisotropic motion in Frame3.
1289#   or disables the widet entirly if atom = 0
1290proc DisplayU { atomnum p} {
1291    global expgui entryvar entrycmd expmap
1292    set mm 0
1293    if {$atomnum == 0} {
1294        set iOrA disable
1295    } elseif {[lindex $expmap(phasetype) 0] == 4} {
1296        set mm 1
1297        set iOrA I
1298    } else {
1299        set iOrA [atominfo $p $atomnum temptype]
1300    }
1301
1302    set firstbox [lindex $expgui(anisolabels) 0]
1303    if { $iOrA == "A" } {
1304        $firstbox config -text "U11 "
1305        foreach item $expgui(anisolabels) {
1306            $item config -fg black
1307        }
1308        foreach item $expgui(anisoentry) var {U11 U22 U33 U12 U13 U23} {
1309            set entrycmd($var) "atominfo $p $atomnum $var"
1310            set entryvar($var) [eval $entrycmd($var)]
1311            $item config -fg black -state normal  -bg white
1312        }
1313    } elseif { $iOrA == "I" || $iOrA == "disable"} {
1314        foreach item $expgui(anisolabels) {
1315            $item config -fg $expgui(bkgcolor1)
1316        }
1317        foreach item [lrange $expgui(anisoentry) 1 end] \
1318                var {U22 U33 U12 U13 U23} {
1319            set entrycmd($var) ""
1320            set entryvar($var) ""
1321            $item config -fg $expgui(bkgcolor1) -bg $expgui(bkgcolor1) \
1322                    -state disabled
1323        }
1324        if { $iOrA == "disable"} {
1325            set entrycmd($var) ""
1326            set entryvar($var) ""
1327            [lindex $expgui(anisoentry) 0] config \
1328                    -fg $expgui(bkgcolor1) -bg $expgui(bkgcolor1) \
1329                    -state disabled
1330        } elseif {$mm} {
1331            set entrycmd(U11) "mmatominfo $p $atomnum Uiso"
1332            set entryvar(U11) [eval $entrycmd(U11)]
1333            $firstbox config -text Uiso -fg black
1334            [lindex $expgui(anisoentry) 0] config -fg black -bg white -state normal
1335        } else {
1336            set entrycmd(U11) "atominfo $p $atomnum Uiso"
1337            set entryvar(U11) [eval $entrycmd(U11)]
1338            $firstbox config -text Uiso -fg black
1339            [lindex $expgui(anisoentry) 0] config -fg black -bg white -state normal
1340        }
1341    }
1342}
1343
1344# need to think about multiple phases
1345
1346# Procedure to display refinement flags
1347proc DisplayRefFlags { atomnum p} {
1348    global expgui entryvar entrycmd expmap
1349    if {$atomnum == 0} {
1350        foreach label $expgui(atomreflbl) {
1351            $label config -fg $expgui(bkgcolor1)
1352        }
1353        foreach entry $expgui(atomref) {
1354            $entry config -state disabled \
1355                    -fg $expgui(bkgcolor1) -bg $expgui(bkgcolor1)
1356            # turn off checkbuttons
1357            catch {$entry deselect}
1358
1359        }
1360        return
1361    }
1362    foreach label $expgui(atomreflbl) {
1363        $label config -fg black
1364    }
1365    foreach entry $expgui(atomref) {
1366        $entry config -state normal -fg black -bg $expgui(bkgcolor1)
1367    }
1368    if {[lindex $expmap(phasetype) 0] == 4} {
1369        foreach var {xref uref fref xdamp udamp fdamp}  {
1370            set entrycmd($var) "mmatominfo $p [list $atomnum] $var"
1371            set entryvar($var) [eval $entrycmd($var)]
1372        }
1373    } else {
1374        foreach var {xref uref fref xdamp udamp fdamp}  {
1375            set entrycmd($var) "atominfo $p [list $atomnum] $var"
1376            set entryvar($var) [eval $entrycmd($var)]
1377        }
1378    }
1379}
1380
1381# Procedure to display an atom in the atom edit boxes
1382proc DisplayAtom { atomnum p} {
1383    global expgui entryvar entrycmd expmap
1384    if {$atomnum == 0} {
1385        foreach label $expgui(atomlabels) {
1386            $label config -fg $expgui(bkgcolor1)
1387        }
1388        foreach entry $expgui(atomentry) {
1389            $entry config -state disabled \
1390                    -fg $expgui(bkgcolor1) -bg $expgui(bkgcolor1)
1391        }
1392        return
1393    }
1394    foreach label $expgui(atomlabels) {
1395        $label config -fg black
1396    }
1397    foreach entry $expgui(atomentry) {
1398        $entry config -state normal -fg black -bg white
1399    }
1400    if {[lindex $expmap(phasetype) 0] == 4} {
1401        foreach var {x y z label frac } {
1402            set entrycmd($var) "mmatominfo $p $atomnum $var"
1403            set entryvar($var) [eval $entrycmd($var)]
1404        }
1405    } else {
1406        foreach var {x y z label frac } {
1407            set entrycmd($var) "atominfo $p $atomnum $var"
1408            set entryvar($var) [eval $entrycmd($var)]
1409        }
1410    }
1411}
1412
1413# make a histogram box; used in MakeHistPane,
1414proc MakeHistBox {frm} {
1415    global expgui
1416    grid [label $frm.mode -text "Select a Histogram" \
1417            -bg beige -anchor center] \
1418            -row 0 -column 0 -columnspan 2 -sticky ew
1419    bind $frm.mode <Button-1> {
1420        set i [lsearch $expgui(AllowedHistSelectModes) $expgui(globalmode)]
1421        set expgui(globalmode) [lindex \
1422                "$expgui(AllowedHistSelectModes) \
1423                $expgui(AllowedHistSelectModes)" [incr i]]
1424        sethistlist
1425    }
1426    bind $frm.mode <Button-3> {set expgui(globalmode) 0; sethistlist}
1427    grid [listbox $frm.title -height 1 -relief flat \
1428            -exportselection 0 ] -row 1 -column 0 -sticky ew
1429    grid [listbox $frm.lbox -height 10 -width 25 \
1430            -exportselection 0 \
1431            -xscrollcommand "$frm.x set" \
1432            -yscrollcommand "$frm.y set" \
1433            ] -row 2 -column 0 -sticky news
1434    lappend expgui(HistSelectList) $frm
1435    grid [scrollbar $frm.x -orient horizontal \
1436            -command "move2boxesX \" $frm.title $frm.lbox \" "
1437    ] -row 3 -column 0 -sticky ew
1438    grid [scrollbar $frm.y \
1439            -command "$frm.lbox yview"] \
1440            -row 2 -column 1 -sticky ns
1441    grid columnconfigure $frm 0 -weight 1
1442    grid rowconfigure $frm 2 -weight 1
1443}
1444
1445# update the histogram list
1446# to do: show histogram ref flags?
1447proc sethistlist {} {
1448    global expgui expmap
1449    array set lbl {
1450        1 "Select 1 or more\nTOF Histograms"
1451        2 "Select 1 or more\nCW Neutron Histograms"
1452        3 "Select 1 or more\nAlpha 1,2 X-ray Histograms"
1453        4 "Select 1 or more\nmonochromatic X-ray Histograms"
1454        5 "Select 1 or more Energy\nDispersive X-ray Histograms"
1455        6 "Select 1 or more of\n any type Histograms"
1456    }
1457    foreach lbox $expgui(HistSelectList) {
1458        $lbox.title delete 0 end
1459        $lbox.lbox delete 0 end
1460        if {$expgui(globalmode) != 0} {
1461            $lbox.lbox config -selectmode extended
1462            $lbox.mode config -text $lbl($expgui(globalmode)) -bg yellow
1463        } else {
1464            $lbox.lbox config -selectmode browse
1465            $lbox.mode config -text "Select a histogram" -bg beige
1466        }
1467    }
1468    # disable the unallowed pages in all mode
1469    if {$expgui(globalmode) == 6} {
1470        foreach pair $expgui(GlobalModeAllDisable) {
1471            if {$expgui(pagenow) == [lindex $pair 0]} {
1472                RaisePage lsFrame
1473            }
1474            eval [lindex $pair 1] -state disabled
1475        }
1476    } else {
1477        foreach pair $expgui(GlobalModeAllDisable) {
1478            eval [lindex $pair 1] -state normal
1479        }
1480    }
1481    set histlist {}
1482    if  {$expgui(hsorttype) == "type"} {
1483        # sort on histogram type
1484        foreach h [lsort -integer -increasing $expmap(powderlist)] {
1485            lappend histlist "$h [string range $expmap(htype_$h) 1 2]"
1486        }
1487        set expmap(histlistboxcontents) [lsort -ascii -index 1 $histlist]
1488    } elseif {$expgui(hsorttype) == "number"} {
1489        # sort on histogram number
1490        foreach h [lsort -integer -increasing $expmap(powderlist)] {
1491            lappend histlist "$h $h"
1492        }
1493        set expmap(histlistboxcontents) [lsort -integer -index 1 $histlist]
1494    } elseif {$expgui(hsorttype) == "bank"} {
1495        # sort on original bank number
1496        foreach h [lsort -integer -increasing $expmap(powderlist)] {
1497            lappend histlist "$h [histinfo $h bank]"
1498        }
1499        set expmap(histlistboxcontents) [lsort -integer -index 1 $histlist]
1500    } elseif {$expgui(hsorttype) == "angle"} {
1501        # sort on wavelength (CW) or angle (E disp.)
1502        foreach h [lsort -integer -increasing $expmap(powderlist)] {
1503            if {[string range $expmap(htype_$h) 2 2] == "T"} {
1504                set det [format %8.2f [histinfo $h tofangle]]
1505            } elseif {[string range $expmap(htype_$h) 2 2] == "C"} {
1506                set det [format %8.5f [histinfo $h lam1]]
1507            } elseif {[string range $expmap(htype_$h) 2 2] == "E"} {
1508                set det [format %8.2f [histinfo $h lam1]]
1509            } else {
1510                set det {}
1511            }
1512            lappend histlist "$h $det"
1513        }
1514        set expmap(histlistboxcontents) [lsort -real -index 1 $histlist]
1515    }
1516
1517    # title field needs to match longest title
1518    foreach lbox $expgui(HistSelectList) {
1519        $lbox.title insert end [format "%2s %s %4s %8s  %-67s" \
1520                "h#" \
1521                type \
1522                bank \
1523                "ang/wave" \
1524                "    title" \
1525                ]
1526    }
1527    foreach tuple $expmap(histlistboxcontents) {
1528        set h [lindex $tuple 0]
1529
1530        if {$expgui(globalmode) == 1} {
1531            if {[string range $expmap(htype_$h) 2 2] != "T"} continue
1532        } elseif {$expgui(globalmode) == 2} {
1533            if {[string range $expmap(htype_$h) 1 2] != "NC"} continue
1534        } elseif {$expgui(globalmode) == 3} {
1535            if {[string range $expmap(htype_$h) 1 2] != "XC" || \
1536                    [histinfo $h lam2] == 0.0} continue
1537        } elseif {$expgui(globalmode) == 4} {
1538            if {[string range $expmap(htype_$h) 1 2] != "XC" || \
1539                    [histinfo $h lam2] != 0.0} continue
1540        } elseif {$expgui(globalmode) == 5} {
1541            if {[string range $expmap(htype_$h) 1 2] != "XE"} continue
1542        }
1543
1544        if {[string range $expmap(htype_$h) 2 2] == "T"} {
1545            set det [format %8.2f [histinfo $h tofangle]]
1546        } elseif {[string range $expmap(htype_$h) 2 2] == "C"} {
1547            set det [format %8.5f [histinfo $h lam1]]
1548        } elseif {[string range $expmap(htype_$h) 2 2] == "E"} {
1549            set det [format %8.2f [histinfo $h lam1]]
1550        } else {
1551            set det {}
1552        }
1553        foreach lbox $expgui(HistSelectList) {
1554            $lbox.lbox insert end [format "%2d  %s %4d %8s  %-67s" \
1555                    $h \
1556                    [string range $expmap(htype_$h) 1 3] \
1557                    [histinfo $h bank] \
1558                    $det \
1559                    [string range [histinfo $h title] 0 66] \
1560                    ]
1561        }
1562    }
1563    UpdateCurrentPage
1564}
1565
1566proc UpdateCurrentPage {} {
1567    global expgui
1568    foreach set $expgui(frameactionlist) {
1569        if {$expgui(pagenow) == [lindex $set 0]} {catch [lindex $set 1]}
1570    }
1571}
1572
1573#-----------------------------------------------------------------------
1574# ----------- draw Histogram page
1575#-----------------------------------------------------------------------
1576proc DisplayHistogram {} {
1577    global expgui entrycmd entryvar entrybox expmap
1578
1579    # trap if more than one histogram is selected unless global mode
1580    if {$expgui(globalmode) == 0 && [llength $expgui(curhist)] > 1} {
1581        set expgui(curhist) [lindex $expgui(curhist) 0]
1582    }
1583
1584    # disable the add histogram button if no phases are present
1585    catch {
1586        foreach c [winfo children $expgui(histFrame).bb] {
1587            if {[llength $expmap(phaselist)] == 0} {
1588                $c configure -state disabled
1589            } else {
1590                $c configure -state normal
1591            } 
1592        }
1593    }
1594
1595    # display the selected histograms
1596    $expgui(histFrame).hs.lbox selection clear 0 end
1597    foreach h $expgui(curhist) {
1598        $expgui(histFrame).hs.lbox selection set $h
1599    }
1600
1601    # disable traces on entryvar for right now
1602    set entrycmd(trace) 0
1603
1604    # get histogram list
1605    set histlist {}
1606    foreach item $expgui(curhist) {
1607        lappend histlist [lindex $expmap(powderlist) $item]
1608    }
1609    # must have at least one histogram selected here
1610    if {[llength $histlist] == 0} {
1611        set expgui(backtermlbl) ""
1612        set expgui(backtypelbl) ""
1613        foreach var {bref bdamp absref absdamp} {
1614            set entrycmd($var) ""
1615            set entryvar($var) ""
1616        }
1617        $expgui(histFrame).top.txt config -text "No Selected Histograms"
1618        grid $expgui(histFrame).top -column 1 -row 0 -sticky nsew       
1619        set expgui(bkglbl) ""
1620        set expgui(abslbl) ""
1621        eval destroy [winfo children $expgui(diffBox)]
1622        set entrycmd(trace) 1
1623        return
1624    }
1625
1626    if {$expgui(globalmode) != 0} {
1627        set expgui(backtermlbl) ""
1628        set expgui(backtypelbl) ""
1629        foreach var {bref bdamp absref absdamp} {
1630            set entrycmd($var) "histinfo [list $histlist] $var"
1631            set entryvar($var) [histinfo [lindex $histlist 0] $var]
1632        }
1633    } else {
1634        set hist $histlist
1635        set terms [histinfo $hist backterms]
1636        set expgui(backtermlbl) "($terms terms)"
1637        set expgui(backtypelbl) "Function type [histinfo $hist backtype]"
1638        foreach var {bref bdamp absref absdamp} {
1639            set entrycmd($var) "histinfo $hist $var"
1640            set entryvar($var) [eval $entrycmd($var)]
1641        }
1642    }
1643    # Top box
1644    if $expgui(haveBW) {
1645        catch {destroy $expgui(histFrame).pflag}
1646    }
1647    if {$expgui(globalmode) != 0} {
1648        $expgui(histFrame).top.txt config \
1649                -text "Selected Histograms: [CompressList $histlist]"
1650        grid $expgui(histFrame).top -column 1 -row 0 -sticky nsew       
1651        set expgui(bkglbl) "Globally Edit Background"
1652        set expgui(abslbl) "Globally Edit Absorption"
1653    } else {
1654        grid forget $expgui(histFrame).top
1655        set expgui(bkglbl) "Edit Background"
1656        set expgui(abslbl) "Edit Abs./Refl."
1657        if {$expgui(haveBW) && [llength $expmap(phaselist)] > 1} {
1658            TitleFrame $expgui(histFrame).pflag  \
1659                    -borderwidth 4 -side left -relief groove \
1660                    -text "Phase Flags"
1661            set expgui(pflag) [$expgui(histFrame).pflag getframe]
1662            grid $expgui(histFrame).pflag -column 1 -row 1 -sticky nsew
1663            grid rowconfigure $expgui(histFrame) 2 -minsize 35
1664            foreach p $expmap(phaselist) {
1665                pack [checkbutton $expgui(pflag).$p \
1666                        -command "GetPhaseFlags $hist" \
1667                        -variable expgui(pflag$p) -text $p] -side left
1668                if {[lsearch $expmap(phaselist_$hist) $p] == -1} {
1669                    set expgui(pflag$p) 0
1670                } else {
1671                    set expgui(pflag$p) 1
1672                }
1673            }
1674        }
1675    }
1676
1677    # diffractometer constants
1678    foreach var {lam1 lam2 kratio pola ipola ddamp zero \
1679            wref pref dcref daref ratref ttref zref } {
1680        set entrycmd($var) "histinfo [list $histlist] $var"
1681        set entryvar($var) [histinfo [lindex $histlist 0] $var]
1682    }
1683
1684    eval destroy [winfo children $expgui(diffBox)]
1685    if {$expgui(globalmode) == 0} {
1686        if {[string range $expmap(htype_$hist) 2 2] == "T"} {
1687        #------
1688        # TOF |
1689        #------
1690            grid [ label $expgui(diffBox).lDCrc -text "Refine DIFC" ] \
1691                    -column 1 -row 1
1692            grid [ checkbutton $expgui(diffBox).rfDCrc -variable entryvar(dcref) ] \
1693                    -column 2 -row 1
1694            grid [ label $expgui(diffBox).lDCdifc -text DIFC ] \
1695                    -column 3 -row 1 -sticky w
1696            grid [ entry $expgui(diffBox).eDCdifc -textvariable entryvar(lam1) \
1697                    -width 15 ] -column 4 -row 1
1698            set entrybox(lam1) $expgui(diffBox).eDCdifc
1699            #
1700            grid [ label $expgui(diffBox).lDCra -text "Refine DIFA" ] \
1701                    -column 1 -row 2
1702            grid [ checkbutton $expgui(diffBox).rfDCra -variable entryvar(daref) ] \
1703                    -column 2 -row 2
1704            grid [ label $expgui(diffBox).lDCdifa -text DIFA ] \
1705                    -column 3 -row 2
1706            grid [ entry $expgui(diffBox).eDCdifa -textvariable entryvar(lam2) \
1707                    -width 15 ] -column 4 -row 2
1708            set entrybox(lam2) $expgui(diffBox).eDCdifa
1709            #
1710            grid [ label $expgui(diffBox).lDCzero -text "Zero"] \
1711                    -column 3 -row 3
1712            grid [ entry $expgui(diffBox).eDCzero -textvariable entryvar(zero) \
1713                    -width 15 ] -column 4 -row 3
1714            set entrybox(zero) $expgui(diffBox).eDCzero
1715            grid [ label $expgui(diffBox).lDCzref -text "Refine zero" ] \
1716                    -column 1 -row 3 -sticky w
1717            grid [ checkbutton $expgui(diffBox).rfDCzref \
1718                    -variable entryvar(zref) ] -column 2 -row 3
1719        } elseif {[string range $expmap(htype_$hist) 1 2] == "NC"} {
1720        #---------------
1721        # CW - neutron |
1722        #---------------
1723            grid [ label $expgui(diffBox).lDC1 -text "Refine wave" ] \
1724                    -column 1 -row 1
1725            grid [ checkbutton $expgui(diffBox).rfDC1 -variable entryvar(wref) ] \
1726                    -column 2 -row 1
1727            grid [ label $expgui(diffBox).lDCdifc -text wave ] \
1728                    -column 3 -row 1 -sticky w
1729            grid [ entry $expgui(diffBox).eDCdifc -textvariable entryvar(lam1) \
1730                    -width 15 ] -column 4 -row 1
1731            set entrybox(lam1) $expgui(diffBox).eDCdifc
1732            #
1733            grid [ label $expgui(diffBox).lDCzref -text "Refine zero" ] \
1734                    -column 1 -row 3 -sticky w
1735            grid [ checkbutton $expgui(diffBox).rfDCzref \
1736                    -variable entryvar(zref) ] -column 2 -row 3
1737            grid [ label $expgui(diffBox).lDCzero -text "Zero"] \
1738                    -column 3 -row 3
1739            grid [ entry $expgui(diffBox).eDCzero -textvariable entryvar(zero) \
1740                    -width 15 ] -column 4 -row 3
1741            set entrybox(zero) $expgui(diffBox).eDCzero
1742        } elseif {[string range $expmap(htype_$hist) 1 2] == "XC" && \
1743                [histinfo $hist lam2] == 0.0} {
1744        #--------------------------
1745        # CW - x-ray 1 wavelength |
1746        #--------------------------
1747            grid [ label $expgui(diffBox).lDC1 -text "Refine wave" ] \
1748                    -column 1 -row 1
1749            grid [ checkbutton $expgui(diffBox).rfDC1 -variable entryvar(wref) ] \
1750                    -column 2 -row 1
1751            grid [ label $expgui(diffBox).lDCdifc -text wave ] \
1752                    -column 3 -row 1 -sticky w
1753            grid [ entry $expgui(diffBox).eDCdifc -textvariable entryvar(lam1) \
1754                    -width 15 ] -column 4 -row 1
1755            set entrybox(lam1) $expgui(diffBox).eDCdifc
1756            #
1757            grid [ label $expgui(diffBox).lDCzref -text "Refine zero" ] \
1758                    -column 1 -row 3 -sticky w
1759            grid [ checkbutton $expgui(diffBox).rfDCzref \
1760                    -variable entryvar(zref) ] -column 2 -row 3
1761            grid [ label $expgui(diffBox).lDCzero -text "Zero"] \
1762                    -column 3 -row 3
1763            grid [ entry $expgui(diffBox).eDCzero -textvariable entryvar(zero) \
1764                    -width 15 ] -column 4 -row 3
1765            set entrybox(zero) $expgui(diffBox).eDCzero
1766            #
1767            grid [ label $expgui(diffBox).lDCpref -text "Refine POLA" ] \
1768                    -column 1 -row 4 -sticky w
1769            grid [ checkbutton $expgui(diffBox).rfDCpref \
1770                    -variable entryvar(pref) ] -column 2 -row 4
1771            grid [ label $expgui(diffBox).lDCpola -text POLA ] \
1772                    -column 3 -row 4
1773            grid [ entry $expgui(diffBox).eDCpola \
1774                    -textvariable entryvar(pola) -width 15 ] -column 4 -row 4
1775            set entrybox(pola) $expgui(diffBox).eDCpola
1776            grid [ label $expgui(diffBox).lDCipola -text "IPOLA" ] \
1777                    -column 5 -row 4
1778            grid [ entry $expgui(diffBox).eDCipola -width 2 \
1779                    -textvariable entryvar(ipola)] -column 6 -row 4
1780            set entrybox(ipola) $expgui(diffBox).eDCipola
1781        } elseif {[string range $expmap(htype_$hist) 1 2] == "XC"} {
1782        #---------------------------
1783        # CW - x-ray 2 wavelengths |
1784        #---------------------------
1785            grid [ label $expgui(diffBox).lDCdifc -text wavelengths ] \
1786                    -column 3 -row 1 -sticky w
1787            grid [ entry $expgui(diffBox).eDCdifc -textvariable entryvar(lam1) \
1788                    -width 15 ] -column 4 -row 1
1789            set entrybox(lam1) $expgui(diffBox).eDCdifc
1790            grid [ entry $expgui(diffBox).eDCdifa -textvariable entryvar(lam2) \
1791                    -width 15 ] -column 5 -row 1
1792            set entrybox(lam2) $expgui(diffBox).eDCdifa
1793            #
1794            grid [ label $expgui(diffBox).lDCrref -text "Refine ratio" ] \
1795                    -column 1 -row 2 -sticky w
1796            grid [ checkbutton $expgui(diffBox).rfDCrref \
1797                    -variable entryvar(ratref) ] -column 2 -row 2
1798            grid [ label $expgui(diffBox).lDCratio -text Ratio ] \
1799                    -column 3 -row 2
1800            grid [ entry $expgui(diffBox).eDCkratio \
1801                    -textvariable entryvar(kratio) \
1802                    -width 15 ] -column 4 -row 2
1803            set entrybox(kratio) $expgui(diffBox).eDCkratio
1804            #
1805            grid [ label $expgui(diffBox).lDCzero -text "Zero"] \
1806                    -column 3 -row 3
1807            grid [ entry $expgui(diffBox).eDCzero -textvariable entryvar(zero) \
1808                    -width 15 ] -column 4 -row 3
1809            grid [ label $expgui(diffBox).lDCzref -text "Refine zero" ] \
1810                    -column 1 -row 3 -sticky w
1811            set entrybox(zero) $expgui(diffBox).eDCzero
1812            grid [ checkbutton $expgui(diffBox).rfDCzref \
1813                    -variable entryvar(zref) ] -column 2 -row 3
1814            grid [ label $expgui(diffBox).lDCpref -text "Refine POLA" ] \
1815                    -column 1 -row 4 -sticky w
1816            grid [ checkbutton $expgui(diffBox).rfDCpref \
1817                    -variable entryvar(pref) ] -column 2 -row 4
1818            grid [ label $expgui(diffBox).lDCpola -text POLA ] \
1819                    -column 3 -row 4
1820            grid [ entry $expgui(diffBox).eDCpola \
1821                    -textvariable entryvar(pola) -width 15 ] -column 4 -row 4
1822            set entrybox(pola) $expgui(diffBox).eDCpola
1823            grid [ label $expgui(diffBox).lDCipola -text "IPOLA" ] \
1824                    -column 5 -row 4
1825            grid [ entry $expgui(diffBox).eDCipola -width 2 \
1826                    -textvariable entryvar(ipola)] -column 6 -row 4
1827            set entrybox(ipola) $expgui(diffBox).eDCipola
1828        } elseif {[string range $expmap(htype_$hist) 1 2] == "XE"} {
1829        #-------------
1830        # ED - x-ray |
1831        #-------------
1832            grid [ label $expgui(diffBox).lDC1 -text "Refine 2theta" ] \
1833                    -column 1 -row 1
1834            grid [ checkbutton $expgui(diffBox).rfDC1 -variable entryvar(ttref) ] \
1835                    -column 2 -row 1
1836            grid [ label $expgui(diffBox).lDCdifc -text 2Theta ] \
1837                    -column 3 -row 1 -sticky w
1838            grid [ entry $expgui(diffBox).eDCdifc -textvariable entryvar(lam1) \
1839                    -width 15 ] -column 4 -row 1
1840            set entrybox(lam1) $expgui(diffBox).eDCdifc
1841            #
1842            grid [ label $expgui(diffBox).lDCpref -text "Refine POLA" ] \
1843                    -column 1 -row 4 -sticky w
1844            grid [ checkbutton $expgui(diffBox).rfDCpref \
1845                    -variable entryvar(pref) ] -column 2 -row 4
1846            grid [ label $expgui(diffBox).lDCpola -text POLA ] \
1847                    -column 3 -row 4
1848            grid [ entry $expgui(diffBox).eDCpola \
1849                    -textvariable entryvar(pola) -width 15 ] -column 4 -row 4
1850            set entrybox(pola) $expgui(diffBox).eDCpola
1851            grid [ label $expgui(diffBox).lDCipola -text "IPOLA" ] \
1852                    -column 5 -row 4
1853            grid [ entry $expgui(diffBox).eDCipola -width 2 \
1854                    -textvariable entryvar(ipola)] -column 6 -row 4
1855            set entrybox(ipola) $expgui(diffBox).eDCipola
1856        }
1857    } elseif {$expgui(globalmode) == 1} {
1858        #-------------
1859        # Global TOF |
1860        #-------------
1861        grid [ label $expgui(diffBox).lDCrc -text "Refine DIFC" ] \
1862                -column 1 -row 1
1863        grid [ checkbutton $expgui(diffBox).rfDCrc -variable entryvar(dcref) ] \
1864                -column 2 -row 1
1865        grid [button $expgui(diffBox).bDCdifc -text "Set DIFC Globally" \
1866                -command "editglobalparm histinfo difc {DIFC}"] -column 3 -row 1
1867        #
1868        grid [ label $expgui(diffBox).lDCra -text "Refine DIFA" ] \
1869                -column 1 -row 2
1870        grid [ checkbutton $expgui(diffBox).rfDCra -variable entryvar(daref) ] \
1871                -column 2 -row 2
1872        grid [ button $expgui(diffBox).bDCdifa -text "Set DIFA Globally" \
1873                -command "editglobalparm histinfo difa {DIFA}"] -column 3 -row 2
1874        #
1875        grid [ label $expgui(diffBox).lDCzref -text "Refine zero" ] \
1876                -column 1 -row 3 -sticky w
1877        grid [ checkbutton $expgui(diffBox).rfDCzref \
1878                -variable entryvar(zref) ] -column 2 -row 3
1879        grid [ button $expgui(diffBox).bDCzero -text "Set ZERO Globally" \
1880                -command "editglobalparm histinfo zero {Zero}"] -column 3 -row 3
1881    } elseif {$expgui(globalmode) == 2} {
1882        #--------------------
1883        # Global CW neutron |
1884        #--------------------
1885        grid [ label $expgui(diffBox).lDC1 -text "Refine wave" ] \
1886                -column 1 -row 1
1887        grid [ checkbutton $expgui(diffBox).rfDC1 -variable entryvar(wref) ] \
1888                -column 2 -row 1
1889        grid [button $expgui(diffBox).bDCdifc -text "Set Wave Globally" \
1890                -command "editglobalparm histinfo lam1 Wavelength"] \
1891                -column 3 -row 1
1892        #
1893        grid [ label $expgui(diffBox).lDCzref -text "Refine zero" ] \
1894                -column 1 -row 3 -sticky w
1895        grid [ checkbutton $expgui(diffBox).rfDCzref \
1896                -variable entryvar(zref) ] -column 2 -row 3
1897        grid [button $expgui(diffBox).bDCzero -text "Set Zero Globally" \
1898                -command "editglobalparm histinfo zero Zero"] -column 3 -row 3
1899    } elseif {$expgui(globalmode) == 4} {
1900        #----------------------
1901        # Global CW mono xray |
1902        #----------------------
1903        grid [ label $expgui(diffBox).lDC1 -text "Refine wave" ] \
1904                -column 1 -row 1
1905        grid [ checkbutton $expgui(diffBox).rfDC1 -variable entryvar(wref) ] \
1906                -column 2 -row 1
1907        grid [button $expgui(diffBox).bDCdifc -text "Set Wave Globally" \
1908                -command "editglobalparm histinfo lam1 Wavelength"] \
1909                -column 3 -row 1
1910        #
1911        grid [ label $expgui(diffBox).lDCzref -text "Refine zero" ] \
1912                -column 1 -row 3 -sticky w
1913        grid [ checkbutton $expgui(diffBox).rfDCzref \
1914                -variable entryvar(zref) ] -column 2 -row 3
1915        grid [button $expgui(diffBox).bDCzero -text "Set Zero Globally" \
1916                -command "editglobalparm histinfo zero Zero"] -column 3 -row 3
1917        #
1918        grid [ label $expgui(diffBox).lDCpref -text "Refine POLA" ] \
1919                -column 1 -row 4 -sticky w
1920        grid [ checkbutton $expgui(diffBox).rfDCpref \
1921                -variable entryvar(pref) ] -column 2 -row 4
1922        grid [button $expgui(diffBox).bDCpola -text "Set POLA Globally" \
1923                -command "editglobalparm histinfo pola POLA"] -column 3 -row 4
1924        grid [button $expgui(diffBox).bDCipola -text "Set IPOLA Globally" \
1925                -command "editglobalparm histinfo ipola IPOLA"] -column 4 -row 4
1926    } elseif {$expgui(globalmode) == 3} {
1927        #------------------------
1928        # Global alpha 1,2 xray |
1929        #------------------------
1930        grid [button $expgui(diffBox).bDCl1 -text "Set Wave1 Globally" \
1931                -command "editglobalparm histinfo lam1 {Wavelength 1}"] \
1932                -column 3 -row 1
1933        grid [button $expgui(diffBox).bDCl2 -text "Set Wave2 Globally" \
1934                -command "editglobalparm histinfo lam2 {Wavelength 2}"] \
1935                -column 4 -row 1
1936        #
1937        grid [ label $expgui(diffBox).lDCratref -text "Refine Ratio" ] \
1938                -column 1 -row 2 -sticky w
1939        grid [ checkbutton $expgui(diffBox).rfDCratref \
1940                -variable entryvar(ratref) ] -column 2 -row 2
1941        grid [button $expgui(diffBox).bDCrrat -text "Set Ratio Globally" \
1942                -command "editglobalparm histinfo ratio {Wavelength Ratio}"] \
1943                -column 3 -row 2
1944        #
1945        grid [ label $expgui(diffBox).lDCzref -text "Refine zero" ] \
1946                -column 1 -row 3 -sticky w
1947        grid [ checkbutton $expgui(diffBox).rfDCzref \
1948                -variable entryvar(zref) ] -column 2 -row 3
1949        grid [button $expgui(diffBox).bDCzero -text "Set Zero Globally" \
1950                -command "editglobalparm histinfo zero Zero"] -column 3 -row 3
1951        #
1952        grid [ label $expgui(diffBox).lDCpref -text "Refine POLA" ] \
1953                -column 1 -row 4 -sticky w
1954        grid [ checkbutton $expgui(diffBox).rfDCpref \
1955                -variable entryvar(pref) ] -column 2 -row 4
1956        grid [button $expgui(diffBox).bDCpola -text "Set POLA Globally" \
1957                -command "editglobalparm histinfo pola POLA"] -column 3 -row 4
1958        grid [button $expgui(diffBox).bDCipola -text "Set IPOLA Globally" \
1959                -command "editglobalparm histinfo ipola IPOLA"] -column 4 -row 4
1960    } elseif {$expgui(globalmode) == 5} {
1961        #-----------------
1962        # Global ED xray |
1963        #-----------------
1964        grid [ label $expgui(diffBox).lDC1 -text "Refine 2theta" ] \
1965                -column 1 -row 1
1966        grid [ checkbutton $expgui(diffBox).rfDC1 -variable entryvar(ttref) ] \
1967                -column 2 -row 1
1968        grid [button $expgui(diffBox).bDCdifc -text "Set 2Theta Globally" \
1969                -command "editglobalparm histinfo ratio {Fixed 2Theta}"] \
1970                -column 3 -row 1
1971        #
1972        grid [ label $expgui(diffBox).lDCpref -text "Refine POLA" ] \
1973                -column 1 -row 4 -sticky w
1974        grid [ checkbutton $expgui(diffBox).rfDCpref \
1975                -variable entryvar(pref) ] -column 2 -row 4
1976        grid [button $expgui(diffBox).bDCpola -text "Set POLA Globally" \
1977                -command "editglobalparm histinfo pola POLA"] -column 3 -row 4
1978        grid [button $expgui(diffBox).bDCipola -text "Set IPOLA Globally" \
1979                -command "editglobalparm histinfo ipola IPOLA"] -column 4 -row 4
1980    }
1981    if {$expgui(globalmode) == 0} {
1982        grid [frame $expgui(diffBox).d] -column 5 -row 1 -rowspan 3 \
1983                -columnspan 2 -sticky e
1984    } else {
1985        grid [frame $expgui(diffBox).d] -column 4 -row 2 -rowspan 2 \
1986                -columnspan 2 -sticky e
1987    }
1988    grid [label $expgui(diffBox).d.lDamp -text "Damping  "] \
1989            -column 1 -row 1
1990    tk_optionMenu $expgui(diffBox).d.om entryvar(ddamp) 0 1 2 3 4 5 6 7 8 9
1991    grid $expgui(diffBox).d.om -column 2 -row 1
1992    grid columnconfigure $expgui(diffBox) 9  -weight 1
1993    grid columnconfigure $expgui(diffBox) 0  -weight 1
1994    update idletasks
1995    # enable traces on entryvar now
1996    set entrycmd(trace) 1
1997}
1998
1999# this gets the phase flags as set in the expgui(pflag*) elements
2000# (linked to phase flag checkbuttons) and the sets the "HST xx NPHAS" flags
2001# accordingly using SetPhaseFlag
2002proc GetPhaseFlags {hist} {
2003    global expmap expgui
2004    set plist {}
2005    foreach p $expmap(phaselist) {
2006        if {$expgui(pflag$p)} {lappend plist $p}
2007    }
2008    SetPhaseFlag $hist $plist
2009    incr expgui(changed)
2010    mapexp
2011}
2012
2013#-----------------------------------------------------------------------
2014# populate the Scaling page
2015#-----------------------------------------------------------------------
2016proc DisplayFrac {} {
2017    global expgui entrycmd entryvar entrybox expmap
2018
2019    # trap if more than one histogram is selected unless global mode
2020    if {$expgui(globalmode) == 0 && [llength $expgui(curhist)] > 1} {
2021        set expgui(curhist) [lindex $expgui(curhist) 0]
2022    }
2023
2024    # display the selected histograms
2025    $expgui(fracFrame).hs.lbox selection clear 0 end
2026    foreach h $expgui(curhist) {
2027        $expgui(fracFrame).hs.lbox selection set $h
2028    }
2029
2030    # disable traces on entryvar
2031    set entrycmd(trace) 0
2032
2033    # get histogram list
2034    set histlist {}
2035    foreach item $expgui(curhist) {
2036        lappend histlist [lindex $expmap(powderlist) $item]
2037    }
2038
2039    # must have at least one histogram selected here
2040    if {[llength $histlist] == 0} {
2041        foreach var {scale sref sdamp} {
2042            set entrycmd($var) ""
2043            set entryvar($var) ""
2044        }
2045        set parm [grid info $expgui(scaleBox).but1]
2046        if {$parm != ""} {
2047            grid forget  $expgui(scaleBox).but1
2048            eval grid $expgui(scaleBox).ent1 $parm
2049        }
2050        # destroy the contents of the frame
2051        set phaseFractf1 $expgui(FracBox).f
2052        eval destroy [winfo children $phaseFractf1]
2053        # reenable traces on entryvar
2054        set entrycmd(trace) 1
2055        return
2056    }
2057
2058    #--------------
2059    # Scale factor
2060    #--------------
2061    if {$expgui(globalmode) != 0} {
2062        foreach var {scale sref sdamp} {
2063            set entrycmd($var) "histinfo [list $histlist] $var"
2064            set entryvar($var) [histinfo [lindex $histlist 0] $var]
2065        }
2066        # reset scale to black
2067        catch {$entrybox(scale) config -fg black}
2068        set parm [grid info $expgui(scaleBox).ent1]
2069        if {$parm != ""} {
2070            grid forget  $expgui(scaleBox).ent1
2071            eval grid $expgui(scaleBox).but1 $parm
2072        }
2073    } else {
2074        set hist $histlist
2075        foreach var {scale sref sdamp} {
2076            set entrycmd($var) "histinfo $hist $var"
2077            set entryvar($var) [eval $entrycmd($var)]
2078        }
2079        # reset scale to black
2080        catch {$entrybox(scale) config -fg black}
2081        set parm [grid info $expgui(scaleBox).but1]
2082        if {$parm != ""} {
2083            grid forget  $expgui(scaleBox).but1
2084            eval grid $expgui(scaleBox).ent1 $parm
2085        }
2086    }
2087
2088    #----------------
2089    # Phase Fractions
2090    #----------------
2091    set phaseFractf1 $expgui(FracBox).f
2092    # destroy the contents of the frame
2093    eval destroy [winfo children $phaseFractf1]
2094    if {$expgui(globalmode) != 0} {
2095        set txt "Phase Fractions for Histograms: [CompressList $histlist]"
2096    } else {
2097        set txt "Phase Fractions"
2098    }
2099    if $expgui(haveBW) {
2100        $expgui(fracFrame).f1.phaseFrac configure -text $txt
2101    } else {
2102        grid [label $phaseFractf1.txt -anchor center -text $txt] \
2103                -column 0 -row 0 -sticky news
2104    }
2105    # Create the frame inside the canvas, One frame for each Phase.
2106    foreach i {1 2 3 4 5 6 7 8 9} {set phasehistlist($i) ""}
2107    foreach hist $histlist {
2108        foreach i $expmap(phaselist_$hist) {
2109            lappend phasehistlist($i) $hist
2110        }
2111    }
2112    foreach i {1 2 3 4 5 6 7 8 9} {
2113        if {[llength $phasehistlist($i)] == 0} continue
2114        set framePF [frame $phaseFractf1.pF$i -relief groove  -bd 4]
2115        grid $framePF -column 0 -row $i -sticky ew
2116        # Label Heading for each phase.
2117        if {$expgui(globalmode) != 0} {
2118            grid [label $framePF.l1 \
2119                    -text "Phase $i Hist: [CompressList $phasehistlist($i)]"] \
2120                    -column 0 -row 0 -sticky nws
2121            grid [button $framePF.but1 -text "Set Globally" \
2122                    -command "editglobalparm hapinfo frac \"Phase $i Fraction\" \
2123                    [list $phasehistlist($i)] $i" \
2124                    ] -column 1 -row 0
2125        } else {
2126            grid [label $framePF.l1  -text "Phase $i"] \
2127                    -column 0 -row 0 -sticky nws
2128            grid [entry $framePF.ent -textvariable entryvar(frac$i) -width 15]\
2129                    -column 1 -row 0
2130            set entrybox(frac$i) $framePF.ent
2131        }
2132        set entrycmd(frac$i) "hapinfo $hist $i frac"
2133        set entryvar(frac$i) [hapinfo $hist $i frac]
2134        grid [label $framePF.l2  -text "  Refine"] \
2135                -column 2 -row 0 -sticky nws
2136        grid [checkbutton $framePF.cb -variable entryvar(frref$i)] \
2137                -column 3 -row 0 -sticky nws
2138        set entrycmd(frref$i) "hapinfo $hist $i frref"
2139        set entryvar(frref$i) [hapinfo $hist $i frref]
2140        grid [label $framePF.l3  -text "  Damping"] \
2141                -column 4 -row 0 -sticky nws
2142        tk_optionMenu $framePF.tkOptDamp entryvar(frdamp$i) \
2143                0 1 2 3 4 5 6 7 8 9     
2144        set entrycmd(frdamp$i) "hapinfo $hist $i frdamp"
2145        set entryvar(frdamp$i) [hapinfo $hist $i frdamp]
2146        grid $framePF.tkOptDamp -row 0 -sticky nsw -column 5
2147    }
2148    # resize the scroll window to match the actual
2149    update idletasks
2150    $expgui(FracBox) config -scrollregion [grid bbox $expgui(FracBox).f]
2151    $expgui(FracBox) config -width [lindex [grid bbox $expgui(FracBox).f] 2]
2152    update idletasks
2153    # enable traces on entryvar now
2154    set entrycmd(trace) 1
2155}
2156
2157#-----------------------------------------------------------------------
2158# display the profile page
2159#-----------------------------------------------------------------------
2160proc DisplayProfile {} {
2161    global expgui entrycmd entryvar entrybox expmap
2162
2163    # trap if more than one histogram is selected unless global mode
2164    if {$expgui(globalmode) == 0 && [llength $expgui(curhist)] > 1} {
2165        set expgui(curhist) [lindex $expgui(curhist) 0]
2166    }
2167    # display the selected histograms
2168    $expgui(profFrame).hs.lbox selection clear 0 end
2169    foreach h $expgui(curhist) {
2170        $expgui(profFrame).hs.lbox selection set $h
2171    }
2172
2173    # destroy the contents of the frame
2174    eval destroy [winfo children $expgui(ProfileBox).f]
2175    # since the next steps can take a while, do a screen update
2176    update idletasks
2177
2178    if {$expgui(globalmode) == 0} {
2179        # must have at least one histogram selected here
2180        if {[llength $expgui(curhist)] == 0} return
2181        # disable traces on entryvar for right now
2182        set entrycmd(trace) 0
2183        set hist [lindex $expmap(powderlist) $expgui(curhist)]
2184        # no defined histograms?
2185        if {$hist == ""} return
2186        # Create one frame for each Phase.
2187        set ind -1
2188        set htype [string range $expmap(htype_$hist) 2 2]
2189        foreach i $expmap(phaselist_$hist) {
2190            incr ind
2191            # Label Heading for each phase.
2192            set ptype [string trim [hapinfo $hist $i proftype]]
2193            if {$expgui(haveBW)} {
2194                grid [TitleFrame $expgui(ProfileBox).f.$i \
2195                        -text "Hist $hist -- Phase $i (type $ptype)" \
2196                        -relief groove -bd 2] \
2197                        -column 0 -row $ind -sticky ew
2198                set ProfileFrame [$expgui(ProfileBox).f.$i getframe]
2199                grid [frame $ProfileFrame.1] \
2200                        -column 0 -row 0 -columnspan 10
2201                pack [label $ProfileFrame.1.l  \
2202                        -text Damping]\
2203                        -side left
2204            } else {
2205                grid [frame $expgui(ProfileBox).f.$i -relief groove -bd 4] \
2206                        -column 0 -row $ind -sticky ew
2207                set ProfileFrame $expgui(ProfileBox).f.$i
2208                grid [frame $ProfileFrame.1] \
2209                        -column 0 -row 0 -columnspan 10 -sticky ew
2210                pack [label $ProfileFrame.1.l  \
2211                        -text "Phase $i (type $ptype)    Damping"]\
2212                        -side left
2213            }
2214            tk_optionMenu $ProfileFrame.1.tkOptDamp entryvar(pdamp_$i) \
2215                    0 1 2 3 4 5 6 7 8 9
2216            set entrycmd(pdamp_$i) "hapinfo $hist $i pdamp"
2217            set entryvar(pdamp_$i) [hapinfo $hist $i pdamp]
2218            pack $ProfileFrame.1.tkOptDamp -side left
2219            pack [label $ProfileFrame.1.l1 \
2220                    -text "  Peak cutoff"]\
2221                    -side left
2222            pack [entry $ProfileFrame.1.e1  \
2223                    -width 10 -textvariable entryvar(pcut_$i)]\
2224                    -side left
2225            set entrybox(pcut_$i) $ProfileFrame.1.e1
2226            set entrycmd(pcut_$i) "hapinfo $hist $i pcut"
2227            set entryvar(pcut_$i) [hapinfo $hist $i pcut]
2228
2229            pack [button $ProfileFrame.1.b1  \
2230                    -text "Change Type" \
2231                    -command "ChangeProfileType $hist $i"]\
2232                    -side left
2233           
2234            set col -1
2235            set row 1
2236            set nterms [hapinfo $hist $i profterms]
2237            set lbls "dummy [GetProfileTerms $i $hist $ptype]"
2238            for { set num 1 } { $num <= $nterms } { incr num } {
2239                set term {}
2240                catch {set term [lindex $lbls $num]}
2241                if {$term == ""} {set term $num}
2242                incr col
2243                grid [label $ProfileFrame.l${num}_${i} -text "$term"] \
2244                        -row $row -column $col
2245                incr col
2246                grid [checkbutton $ProfileFrame.ref${num}_${i} \
2247                        -variable entryvar(pref${num}_$i)] -row $row -column $col
2248                set entrycmd(pref${num}_$i) "hapinfo $hist $i pref$num"
2249                set entryvar(pref${num}_$i) [hapinfo $hist $i pref$num]
2250                incr col
2251                grid [entry $ProfileFrame.ent${num}_${i} \
2252                        -textvariable entryvar(pterm${num}_$i)\
2253                        -width 12] -row $row -column $col
2254                set entrybox(pterm${num}_$i) $ProfileFrame.ent${num}_${i}
2255                set entrycmd(pterm${num}_$i) "hapinfo $hist $i pterm$num"
2256                set entryvar(pterm${num}_$i) [hapinfo $hist $i pterm$num]
2257                if {$col > 6} {set col -1; incr row}
2258            }
2259        }
2260        grid columnconfigure $expgui(ProfileBox).f 0 -weight 1
2261    } else {
2262        # get histogram list
2263        set histlist {}
2264        foreach item $expgui(curhist) {
2265            lappend histlist [lindex $expmap(powderlist) $item]
2266        }
2267        # must have at least one histogram selected here
2268        if {[llength $histlist] == 0} return
2269        # disable traces on entryvar for right now
2270        set entrycmd(trace) 0
2271        # loop through histograms & phases, set up an array by phase & profile type
2272        catch {unset prtyparray histarray phasearray}
2273        foreach hist $histlist {
2274            foreach phase $expmap(phaselist_$hist) {
2275                set prtyp [string trim [hapinfo $hist $phase proftype]]
2276                set key ${prtyp}_$phase
2277                lappend prtyparray($key) $hist
2278                lappend histarray($key) $hist
2279                lappend phasearray($key) $phase
2280            }
2281        }
2282       
2283        set ptype ""
2284        set i -1
2285        # loop over all combined phases and profile types, sorted 1st by profile number
2286        foreach key [lsort [array names prtyparray]] {
2287            # split key
2288            scan $key %d_%d prftyp p
2289
2290            if {$ptype != $prftyp || !$expgui(globalphasemode)} {
2291                set ptype $prftyp
2292                set curhistlist $histarray($key)
2293                set curphaslist $phasearray($key)
2294               
2295                set hist1 [lindex $curhistlist 0]
2296                set phase1 [lindex $curphaslist 0]
2297                set nterms [hapinfo $hist1 $phase1 profterms]
2298                set htype [string range $expmap(htype_$hist1) 2 2]
2299                set lbls "dummy [GetProfileTerms $phase1 $hist1 $ptype]"
2300                # Create a frame for this type
2301                incr i
2302                set boxtitle "Phase $p, hist [CompressList $histarray($key)]"
2303                if {$expgui(haveBW)} {
2304                    grid [TitleFrame $expgui(ProfileBox).f.$i \
2305                            -text "(type $ptype)" \
2306                            -relief groove -bd 2] \
2307                            -column 0 -row $i -sticky ew
2308                    set ProfileFrame [$expgui(ProfileBox).f.$i getframe]
2309                    grid [frame $ProfileFrame.0] \
2310                            -column 0 -row 0 -columnspan 20 -sticky ew
2311                } else {
2312                    grid [frame $expgui(ProfileBox).f.$i \
2313                            -relief groove -bd 4] \
2314                            -column 0 -row $i -sticky ew
2315                    set ProfileFrame $expgui(ProfileBox).f.$i
2316                    grid [frame $ProfileFrame.0] \
2317                            -column 0 -row 0 -columnspan 20 -sticky ew
2318                    grid [label $ProfileFrame.0.0  \
2319                            -text "Profile Type $ptype   "] -row 0 -column 0
2320                }
2321                grid [label $ProfileFrame.0.1  \
2322                        -anchor w] -row 0 -column 1
2323                grid [frame $ProfileFrame.1] \
2324                        -column 0 -row 1 -columnspan 20 -sticky ew
2325                grid [label $ProfileFrame.1.2  \
2326                        -text "Damping"] -row 0 -column 2
2327                tk_optionMenu $ProfileFrame.1.tkOptDamp \
2328                        entryvar(pdamp_$i) 0 1 2 3 4 5 6 7 8 9
2329                grid $ProfileFrame.1.tkOptDamp -row 0 -column 3
2330                grid [button $ProfileFrame.1.edit \
2331                        -text "Global Edit"] -row 0 -column 4 -sticky w
2332                set entryvar(pdamp_$i) [hapinfo $hist $phase pdamp]
2333                grid [button $ProfileFrame.1.b1 -text "Change Type"] \
2334                        -row 0 -column 5 -sticky w
2335                set col -1
2336                set row 2
2337                for { set num 1 } { $num <= $nterms } { incr num } {
2338                    set term {}
2339                    catch {set term [lindex $lbls $num]}
2340                    if {$term == ""} {set term $num}
2341                    incr col
2342                    grid [label $ProfileFrame.l${num}_${i} \
2343                            -text "$term"] -row $row -column $col
2344                    incr col
2345                    grid [checkbutton $ProfileFrame.ref${num}_${i} \
2346                            -variable entryvar(pref${num}_$i)] \
2347                            -row $row -column $col
2348                    set entryvar(pref${num}_$i) [hapinfo $hist $phase pref$num]
2349                    if {$col > 10} {set col -1; incr row}
2350                }
2351                grid columnconfigure $expgui(ProfileBox).f 0 -weight 1
2352            } else {
2353                # add to the current entry
2354                eval lappend curhistlist $histarray($key)
2355                eval lappend curphaslist $phasearray($key)
2356                append boxtitle "\nPhase $p, hist [CompressList $histarray($key)]"
2357            }
2358            $ProfileFrame.0.1 config -text $boxtitle
2359            $ProfileFrame.1.edit config -command "\
2360                    EditProfile \"\n$boxtitle\" \
2361                    [list $curhistlist] \
2362                    [list $curphaslist]"
2363            $ProfileFrame.1.b1 config -command "ChangeProfileType \
2364                    [list $curhistlist] [list $curphaslist]"
2365            set entrycmd(pdamp_$i) "hapinfo \
2366                    [list $curhistlist] \
2367                    [list $curphaslist] pdamp"
2368            for { set num 1 } { $num <= $nterms } { incr num } {
2369                set entrycmd(pref${num}_$i) "hapinfo \
2370                        [list $curhistlist] \
2371                        [list $curphaslist] pref$num"
2372            }
2373        }
2374    }
2375   
2376    # resize the scroll window to match the actual
2377    update idletasks
2378    $expgui(ProfileBox) config -scrollregion [grid bbox $expgui(ProfileBox).f]
2379    $expgui(ProfileBox) config -width [lindex [grid bbox $expgui(ProfileBox).f] 2]
2380    update idletasks
2381    ResizeNotebook
2382    # enable traces on entryvar now
2383    set entrycmd(trace) 1
2384}
2385
2386# process the bit settings in the print options
2387#   bitnum -- the number of the bit to be tested/set starting at 0 for the LSBit
2388proc printsetting {bitnum "action get" "value {}"} {
2389    global entryvar expgui
2390    if {$action == "get"} {
2391        return [expr ([expinfo print] & int(pow(2,$bitnum))) != 0]
2392    } elseif $value {
2393        set newval [expr ([expinfo print] | int(pow(2,$bitnum)))]
2394    } else {
2395        set newval [expr ([expinfo print] & ~int(pow(2,$bitnum)))]
2396    }
2397    expinfo print set $newval
2398    set expgui(printopt) "Print Options ([expinfo print])"
2399}
2400
2401# need to respond to mouse presses -- control variable associated with extract Fobs
2402# and set the LeBail extraction flags
2403proc SetupExtractHist {} {
2404    global expgui entrycmd entryvar expmap
2405
2406    # display the selected histograms
2407    $expgui(lsFrame).hs.lbox selection clear 0 end
2408    foreach h $expgui(curhist) {
2409        $expgui(lsFrame).hs.lbox selection set $h
2410    }
2411
2412    # get histogram list
2413    set histlist {}
2414    foreach item $expgui(curhist) {
2415        set hist [lindex $expmap(powderlist) $item]
2416        if {$hist != ""} {lappend histlist $hist}
2417    }
2418    set entrycmd(fobsextract) "histinfo [list $histlist] foextract"
2419    if {[llength $histlist] == 0 || [string trim $histlist] == ""} {
2420        set entrycmd(LBdamp) ""
2421        foreach phase {1 2 3 4 5 6 7 8 9} {
2422            $expgui(FobsExtractFrame).l$phase config -fg grey
2423            set expgui(Fextract$phase) {}
2424            foreach item $expgui(ExtractSettingsRadiobuttons) {
2425                ${item}$phase config -state disabled -bd 1
2426            }
2427        }
2428    } elseif {[llength $histlist] == 1} {
2429        # disable traces on entryvar
2430        set entrycmd(trace) 0
2431        set entryvar(fobsextract) [histinfo $histlist foextract]
2432        set entrycmd(LBdamp) "histinfo $histlist LBdamp"
2433        set entryvar(LBdamp) [histinfo $histlist LBdamp]
2434        foreach phase {1 2 3 4 5 6 7 8 9} {
2435            # is the phase present?
2436            if {[lsearch -exact $expmap(phaselist_$histlist) $phase] == -1} {
2437                $expgui(FobsExtractFrame).l$phase config -fg grey
2438                set expgui(Fextract$phase) {}
2439                foreach item $expgui(ExtractSettingsRadiobuttons) {
2440                    ${item}$phase config -state disabled -bd 1
2441                }
2442            } else {
2443                $expgui(FobsExtractFrame).l$phase config -fg black
2444                foreach item $expgui(ExtractSettingsRadiobuttons) {
2445                    ${item}$phase config -state normal -bd 2
2446                }
2447                set expgui(Fextract$phase) [hapinfo $histlist $phase extmeth]
2448            }
2449        }
2450    } elseif {[llength $histlist] > 1} {
2451        # disable traces on entryvar
2452        set entrycmd(LBdamp) "histinfo [list $histlist] LBdamp"
2453        set entryvar(LBdamp) [histinfo [lindex $histlist 0] LBdamp]
2454        set entrycmd(trace) 0
2455        # multiple histograms need phases in any histogram
2456        foreach phase {1 2 3 4 5 6 7 8 9} {
2457            set gotphase($phase) 0
2458        }           
2459        foreach hist $histlist {
2460            foreach phase $expmap(phaselist_$hist) {
2461                set gotphase($phase) 1
2462            }
2463        }
2464        foreach phase {1 2 3 4 5 6 7 8 9} {
2465            set expgui(Fextract$phase) {}
2466            if $gotphase($phase) {
2467                $expgui(FobsExtractFrame).l$phase config -fg black
2468                foreach item $expgui(ExtractSettingsRadiobuttons) {
2469                    ${item}$phase config -state normal -bd 2
2470                }
2471            } else {
2472                $expgui(FobsExtractFrame).l$phase config -fg grey
2473                foreach item $expgui(ExtractSettingsRadiobuttons) {
2474                    ${item}$phase config -state disabled -bd 1
2475                }
2476            }
2477        }
2478    }
2479    # reenable traces
2480    set entrycmd(trace) 1
2481}
2482# respond to a change in the fobs extraction method for a phase
2483# force the main extraction flag on, if fobs extraction is selected for any phase
2484proc HistExtractSet {phase} {
2485    global expgui entryvar expmap
2486    foreach item $expgui(curhist) {
2487        lappend histlist [lindex $expmap(powderlist) $item]
2488    }
2489    hapinfo $histlist $phase extmeth set $expgui(Fextract$phase)
2490    incr expgui(changed)
2491    if {$expgui(Fextract$phase) != 0} {set entryvar(fobsextract) 1}
2492}
2493#---------------------------- Global Edit Functions ------------------------
2494proc editbackground {} {
2495    global expgui expmap entrycmd
2496    set histlist {}
2497    foreach n $expgui(curhist) {
2498        lappend histlist [lindex $expmap(powderlist) $n]
2499    }
2500    if {[llength $histlist] == 0} return
2501
2502    set w .back
2503    catch {destroy $w}
2504    toplevel $w -bg beige
2505    if {$expgui(globalmode) != 0} {
2506        wm title $w "Global Edit Background"
2507    } else {
2508        wm title $w "Edit Background"
2509    }
2510   
2511    pack [frame $w.0 -bd 6 -relief groove  -bg beige \
2512            ] -side top -expand yes -fill both
2513    if {[llength $histlist] > 1} {
2514        grid [label $w.0.a \
2515            -text "Setting background terms for histograms [CompressList $histlist]" \
2516            -bg beige] -row 0 -column 0 -columnspan 10
2517    } else {
2518        grid [label $w.0.a \
2519            -text "Setting background terms for histogram $histlist" \
2520            -bg beige] -row 0 -column 0 -columnspan 4
2521        grid [button $w.0.bkg -text "Fit Background\nGraphically" \
2522                -command "QuitEditBackground $w; bkgedit $histlist"] \
2523                -row 0 -column 4 -rowspan 2
2524        grid columnconfig $w.0 0 -weight 1
2525        grid columnconfig $w.0 4 -weight 1
2526    }
2527    set hist [lindex $histlist 0]
2528    grid [label $w.0.b -text "Function type" -bg beige]  -row 1 -column 0 -sticky e
2529
2530    # disable traces on  expgui(backtype) & expgui(backterms) now
2531    set entrycmd(trace) 0
2532
2533    # number of terms
2534    set expgui(backtype) [histinfo $hist backtype]
2535    set expgui(orig_backtype) $expgui(backtype)
2536    set expgui(prev_backtype) $expgui(backtype)
2537    set typemenu [tk_optionMenu $w.0.type expgui(backtype) null]
2538    $typemenu delete 0 end
2539    foreach item {
2540        "1 - Shifted Chebyschev"
2541        "2 - Cosine Fourier series"
2542        "3 - Radial distribution peaks"
2543        "4 - Power series in Q**2n/n!"
2544        "5 - Power series in n!/Q**2n"
2545        "6 - Power series in Q**2n/n! and n!/Q**2n"
2546        "7 - Linear interpolation function"
2547        "8 - Reciprocal interpolation function"
2548    } {
2549        set val [lindex $item 0]
2550        $typemenu insert end radiobutton -variable expgui(backtype) \
2551                -label $item -value $val
2552    }
2553
2554    grid $w.0.type   -row 1 -column 1
2555    grid [label $w.0.c -text "  Number of terms"  -bg beige] -row 1 -column 2
2556
2557    # function type
2558    set expgui(backterms) [histinfo $hist backterms]
2559    set expgui(orig_backterms) $expgui(backterms)
2560    set list {}; for {set i 1} {$i <= 36} {incr i} {lappend list $i}
2561    eval tk_optionMenu $w.0.terms expgui(backterms) $list
2562    grid $w.0.terms   -row 1 -column 3
2563    # enable traces on  expgui(backtype) & expgui(backterms) now
2564    set entrycmd(trace) 1
2565
2566    #set background terms
2567    for {set num 1 } { $num <= 36 } { incr num } {
2568        set var "bterm$num"
2569        set expgui($var) {}
2570        set expgui(orig_$var) {}
2571    }
2572    if {[llength $histlist] == 1} {
2573        for {set num 1 } { $num <= $expgui(backterms) } { incr num } {
2574            set var "bterm$num"
2575            set expgui($var) [histinfo $histlist $var]
2576            set expgui(orig_$var) $expgui($var)
2577        }
2578    }
2579
2580    pack [frame $w.1 -bd 6 -relief groove  -bg beige] -side top \
2581            -expand yes -fill both
2582    ShowBackTerms $w.1
2583
2584    set expgui(temp) {}
2585    pack [frame $w.b -bg beige] -fill x -expand yes -side top
2586    grid [button $w.b.2 -text Set -command "destroy $w"] -row 0 -column 1
2587    grid [button $w.b.3 -text Quit \
2588            -command "QuitEditBackground $w"] -row 0 -column 2
2589    grid [button $w.b.help -text Help -bg yellow \
2590            -command "MakeWWWHelp expgui3.html EditBackground"] \
2591            -row 0 -column 4
2592    grid columnconfig $w.b 0 -weight 1
2593    grid columnconfig $w.b 3 -weight 1
2594    bind $w <Key-F1> "MakeWWWHelp expgui3.html EditBackground"
2595    bind $w <Return> "destroy $w"
2596
2597    # force the window to stay on top
2598    putontop $w
2599
2600    focus $w.b.2
2601    tkwait window $w
2602    afterputontop
2603
2604    if {$expgui(temp) != ""} return
2605
2606    if {$expgui(orig_backtype) != $expgui(backtype)} {
2607        histinfo $histlist backtype set $expgui(backtype)
2608        incr expgui(changed)
2609    }
2610    if {$expgui(orig_backterms) != $expgui(backterms)} {
2611        histinfo $histlist backterms set $expgui(backterms)
2612        incr expgui(changed)
2613    }
2614    for {set num 1 } { $num <= $expgui(backterms) } { incr num } {
2615        set var "bterm$num"
2616        if {$expgui(orig_$var) != $expgui($var)} {
2617            histinfo $histlist $var set $expgui($var)
2618            incr expgui(changed)
2619        }
2620    }
2621
2622    if {$expgui(globalmode) == 0} {
2623        set expgui(backtypelbl) "Function type [histinfo $hist backtype]"
2624        set expgui(backtermlbl) "([histinfo $hist backterms] terms)"
2625    }
2626}
2627
2628trace variable expgui(backterms) w ChangeBackTerms
2629proc ChangeBackTerms {a b c} {
2630    global entrycmd expgui
2631    if !$entrycmd(trace) return
2632    ShowBackTerms .back.1
2633}
2634
2635trace variable expgui(backtype) w ChangeBackType
2636# reset the terms to 1, 0, 0... when the number of terms increase
2637proc ChangeBackType {a b c} {
2638    global entrycmd expgui
2639    if !$entrycmd(trace) return
2640    if {$expgui(prev_backtype) == $expgui(backtype)} return
2641    set expgui(prev_backtype) $expgui(backtype)
2642    for {set num 1 } { $num <= $expgui(backterms) } { incr num } {
2643        set var "bterm$num"
2644        if {$num == 1} {
2645            set expgui($var) 1.0
2646        } else {
2647            set expgui($var) 0.0
2648        }
2649    }
2650}
2651
2652proc ShowBackTerms {w } {
2653    global expgui expmap
2654    # destroy the contents of the frame
2655    eval destroy [winfo children $w]
2656    set histlist {}
2657    foreach n $expgui(curhist) {
2658        lappend histlist [lindex $expmap(powderlist) $n]
2659    }
2660    set widgetsPerRow 4
2661    for {set rows 2; set num 1 } { $num <= $expgui(backterms) } { incr rows } {
2662        for {set cols 0} { (2*$widgetsPerRow > $cols) && ($num <= $expgui(backterms)) }  { incr num }  {
2663            set var "bterm$num"
2664            grid [label $w.l$num -text $num -bg beige]  \
2665                    -row $rows -column $cols -sticky nes
2666            incr cols
2667            grid [entry $w.e$num -width 15 -textvariable expgui($var) \
2668                    ] -row $rows  -column $cols  -sticky news
2669            incr cols
2670        }
2671    }
2672}
2673
2674proc QuitEditBackground {w} {
2675    global expgui
2676    # lets find out if anything changed
2677    set changed 0
2678    if {$expgui(orig_backtype) != $expgui(backtype)} {
2679        set changed 1
2680    }
2681    if {$expgui(orig_backterms) != $expgui(backterms)} {
2682        set changed 1
2683    }
2684    for {set num 1 } { $num <= $expgui(backterms) } { incr num } {
2685        set var "bterm$num"
2686        if {$expgui(orig_$var) != $expgui($var)} {
2687            set changed 1
2688            break
2689        }
2690    }
2691    if $changed {
2692        set decision [tk_dialog .changes "Abandon Changes" \
2693                "You have made changes to the background. Ok to abandon changes?" \
2694                warning 0 Abandon Keep]
2695        if !$decision {
2696            set expgui(temp) "Quit"
2697            destroy $w
2698        }
2699    } else {
2700        set expgui(temp) "Quit"
2701        destroy $w
2702    }
2703}
2704
2705# this probably needs work
2706proc editglobalparm {cmd variable title "histlist {}" "phase {}"} {
2707    global expgui expmap
2708    set w .global
2709    catch {destroy $w}
2710    toplevel $w -bg beige
2711    wm title $w "Edit Global Parameter"
2712    set expgui(temp) {}
2713    if {[llength $histlist] == 0} {
2714        set hist {}
2715        foreach n $expgui(curhist) {
2716            lappend hist [lindex $expmap(powderlist) $n]
2717        }
2718    } else {
2719        set hist $histlist
2720    }
2721    pack [frame $w.0 -bd 6 -relief groove -bg beige] \
2722            -side top -expand yes -fill both
2723    grid [label $w.0.a -text "Setting $title for histograms [CompressList $hist]"\
2724            -bg beige] \
2725            -row 0 -column 0 -columnspan 10
2726    grid [entry $w.0.b -textvariable expgui(temp)] \
2727            -row 1 -column 0
2728
2729
2730    pack [frame $w.b -bg beige] -fill x -expand yes -side top
2731    pack [button $w.b.2 -text Set -command "destroy $w"] -side left
2732    pack [button $w.b.3 -text Quit -command "set expgui(temp) {}; destroy $w"] -side left
2733    pack [button $w.b.help -text Help -bg yellow \
2734            -command "MakeWWWHelp expgui3.html EditParm"] -side right
2735    bind $w <Key-F1> "MakeWWWHelp expgui3.html EditParm"
2736    bind $w <Return> "destroy $w"
2737
2738    # force the window to stay on top
2739    putontop $w
2740    focus $w.b.2
2741    tkwait window $w
2742    afterputontop
2743
2744    if {$expgui(temp) != ""} {
2745        foreach h $hist {
2746            if {$cmd == "histinfo"} {
2747                histinfo $h $variable set $expgui(temp)
2748                incr expgui(changed)
2749                if $expgui(debug) {
2750                    puts "histinfo $h $variable set $expgui(temp)"
2751                }
2752            } elseif {$cmd == "hapinfo"} {
2753                hapinfo $h $phase $variable set $expgui(temp)
2754                incr expgui(changed)
2755                if $expgui(debug) {
2756                    puts "hapinfo $phase $h $variable set $expgui(temp)"
2757                }
2758            } else {
2759                error "$cmd unimplemented"
2760            }
2761        }
2762    }
2763}
2764
2765proc EditProfile {title histlist phaselist} {
2766    global expgui expmap entrycmd
2767    set w .back
2768    catch {destroy $w}
2769    toplevel $w -bg beige
2770    wm title $w "Global Edit Profile"
2771    set hist [lindex $histlist 0]
2772    set phase [lindex $phaselist 0]
2773    set ptype [string trim [hapinfo $hist $phase proftype]]
2774    set htype [string range $expmap(htype_$hist) 2 2]
2775    set nterms [hapinfo $hist $phase profterms]
2776   
2777    pack [frame $w.0 -bd 6 -relief groove  -bg beige \
2778            ] -side top -expand yes -fill both
2779    grid [label $w.0.a \
2780            -text "Setting profile terms: $title" \
2781            -bg beige] -row 0 -column 0 -columnspan 10
2782    grid [label $w.0.b -text "Function type $ptype"  -bg beige]  -row 1 -column 0
2783    grid [label $w.0.c -text "  Peak cutoff" -bg beige] -row 1 -column 3
2784    grid [entry $w.0.d -width 10 ]  -row 1 -column 4
2785    set entrylist {}
2786    lappend entrylist "pcut $w.0.d"
2787
2788    set col -1
2789    set row 1
2790    set lbls "dummy [GetProfileTerms $phase $hist $ptype]"
2791    pack [frame $w.1 -bd 6 -relief groove  -bg beige \
2792            ] -side top -expand yes -fill both
2793    for { set num 1 } { $num <= $nterms } { incr num } {
2794        set term {}
2795        catch {set term [lindex $lbls $num]}
2796        if {$term == ""} {set term $num}
2797        incr col
2798        grid [label $w.1.l${num} -text "$term" -bg beige] \
2799                -row $row -column $col
2800        incr col
2801        grid [entry $w.1.ent${num} \
2802                -width 14] -row $row -column $col
2803        lappend entrylist "pterm$num $w.1.ent${num}"   
2804        if {$col > 6} {set col -1; incr row}
2805    }
2806    pack [frame $w.b -bg beige] -fill x -expand yes -side top
2807    grid [button $w.b.2 -text Set \
2808            -command "SetEditProfile [list $entrylist] [list $phaselist] \
2809            [list $histlist] $w"] -row 0 -column 1
2810    grid [button $w.b.3 -text Quit \
2811            -command "QuitEditProfile $w [list $entrylist]"] -row 0 -column 2
2812    grid [button $w.b.help -text Help -bg yellow \
2813            -command "MakeWWWHelp expgui5.html GlobalEdit"] \
2814            -row 0 -column 4
2815    grid columnconfig $w.b 0 -weight 1
2816    grid columnconfig $w.b 3 -weight 1
2817    bind $w <Key-F1> "MakeWWWHelp expgui5.html GlobalEdit"
2818    bind $w <Return> "QuitEditProfile $w [list $entrylist]"
2819
2820    # force the window to stay on top
2821    putontop $w
2822    focus $w.b.2
2823    tkwait window $w
2824    afterputontop
2825}
2826
2827proc SetEditProfile {entrylist phaselist histlist w} {
2828    global expgui
2829    foreach item $entrylist {
2830        set value [ [lindex $item 1] get ]
2831        if {$value != ""} {
2832            hapinfo $histlist $phaselist [lindex $item 0] set $value
2833            incr expgui(changed)
2834            if $expgui(debug) {
2835                puts "hapinfo [list $phaselist] [list $histlist] [lindex $item 0] set $value"
2836            }
2837        }
2838    }
2839    destroy $w
2840}
2841
2842proc QuitEditProfile {w entrylist} {
2843    global expgui
2844    # lets find out if anything changed
2845    set changed 0
2846    foreach item $entrylist {
2847        if {[ [lindex $item 1] get ] != ""} {set changed 1; break}
2848    }
2849    if $changed {
2850        set decision [tk_dialog .changes "Abandon Changes" \
2851                "You have made changes to the Profile. Ok to abandon changes?" \
2852                warning 0 Abandon Keep]
2853        if !$decision {destroy $w}
2854    } else {
2855        destroy $w
2856    }
2857}
2858
2859# this is called to change the absorption correction mode and to
2860# change the absorption correction model.
2861proc editabsorption {} {
2862    global expgui expmap
2863    set histlist {}
2864    foreach n $expgui(curhist) {
2865        lappend histlist [lindex $expmap(powderlist) $n]
2866    }
2867    if {[llength $histlist] == 0} return
2868
2869    set w .abs
2870    catch {destroy $w}
2871    toplevel $w -bg beige
2872    if {$expgui(globalmode) != 0} {
2873        wm title $w "Global Edit Absorption/Reflectivity"
2874    } else {
2875        wm title $w "Edit Absorption/Reflectivity"
2876    }
2877   
2878    pack [frame $w.0 -bd 6 -relief groove  -bg beige \
2879            ] -side top -expand yes -fill both
2880    if {[llength $histlist] > 1} {
2881        grid [label $w.0.a \
2882            -text "Changing settings for histograms [CompressList $histlist]" \
2883            -bg beige] -row 0 -column 0 -columnspan 10
2884    } else {
2885        grid [label $w.0.a \
2886            -text "Changing settings for histogram $histlist" \
2887            -bg beige] -row 0 -column 0 -columnspan 4
2888        #grid columnconfig $w.0 4 -weight 1
2889    }
2890    grid rowconfig $w.0 1 -min 10
2891    set hist [lindex $histlist 0]
2892
2893    grid [label $w.0.lb1 -text "Absorption Coefficient(s)" -bg beige] \
2894            -row 2 -column 1  -columnspan 2
2895    grid [label $w.0.lb1a -text "1" -bg beige] -row 3 -column 1
2896    set expgui(abs2box1) $w.0.lb2a
2897    grid [label $w.0.lb2a -text "2" -bg beige] -row 3 -column 2
2898    grid [label $w.0.lb3 -text Absorption\nFunction -bg beige] \
2899            -row 2 -column 6 -rowspan 2 -columnspan 2
2900    grid [entry $w.0.ent1 -textvariable expgui(abscor1) -width 15] \
2901            -row 4 -column 1
2902    set expgui(abs2box2) $w.0.ent2
2903    grid [entry $w.0.ent2 -textvariable expgui(abscor2) -width 15] \
2904            -row 4 -column 2
2905    trace vdelete expgui(abstype) w AbsSetoptmsg
2906    eval tk_optionMenu $w.0.m1 expgui(abstype) 0 1 2 3 4
2907    trace variable expgui(abstype) w AbsSetoptmsg
2908    grid $w.0.m1 -row 4 -column 6 -columnspan 2
2909    grid [label $w.0.lb8 -textvariable expgui(opttxt) -bg beige \
2910          -wrap 300 -justify left] -row 5 -column 1  -sticky ne -columnspan 7
2911    grid rowconfig $w.0 5 -min 100
2912    # set the values, note the trace on abstype
2913    foreach var {abscor1 abscor2 abstype} {
2914        set expgui($var) [histinfo $hist $var]
2915    }
2916
2917    pack [frame $w.b -bg beige] -fill x -expand yes -side top
2918    grid [button $w.b.2 -text Set -command "AbsSaveEdit $w [list $histlist]"] \
2919            -row 0 -column 1
2920    grid [button $w.b.3 -text Quit \
2921            -command "destroy $w"] -row 0 -column 2
2922    grid [button $w.b.help -text Help -bg yellow \
2923            -command "MakeWWWHelp expgui3.html EditAbsorption"] \
2924            -row 0 -column 4
2925    grid columnconfig $w.b 0 -weight 1
2926    grid columnconfig $w.b 3 -weight 1
2927    bind $w <Key-F1> "MakeWWWHelp expgui3.html EditAbsorption"
2928    bind $w <Return> "destroy $w"
2929
2930    # force the window to stay on top
2931    putontop $w
2932
2933    focus $w.b.2
2934    tkwait window $w
2935    afterputontop
2936}
2937
2938proc AbsSetoptmsg {args} {
2939    global expgui
2940    array set opttxt {
2941        0 "Correction for cylindrical samples [Lobanov & Alte da Veiga]. OK for all data types, but not for Bragg-Brentano flat-plate geometry. Set term 1 to mu*R/lambda (TOF: mu*R for lambda=1). For CW x-ray/neutron, do not refine!"
2942        1 "Wavelength-dependent correction for container penetration. Use with TOF & Energy Disp x-ray only."
2943        2 "Surface roughness correction [Pitschke, Hermann & Muttern]. Use with flat-plate reflection geometry (usually Bragg-Brentano) only."
2944        3 "Surface roughness correction, [Suortti]. Use with flat-plate reflection geometry (usually Bragg-Brentano) only."
2945        4 "Flat plate samples in transmission mode. OK for all data types, but not Bragg-Brentano geometry. Term 2 is angle w/r to beam (usually 0). For CW, do not refine."
2946    }
2947    set expgui(opttxt) ""
2948    catch {set expgui(opttxt) [set opttxt($expgui(abstype))]}
2949    switch $expgui(abstype) {
2950        0 -
2951        1 {
2952            $expgui(abs2box1) config -fg gray
2953            $expgui(abs2box2) config -state disabled -fg gray
2954        }
2955        2 -
2956        3 -
2957        4 {
2958            $expgui(abs2box1) config -fg black
2959            $expgui(abs2box2) config -state normal -fg black
2960        }
2961        default {
2962            set expgui(opttxt) "Please select an absorption function"
2963        }
2964    }
2965}
2966proc AbsSaveEdit {top histlist} {
2967    global expgui expmap
2968    # sanity check: look at the histogram type
2969    set h [lindex $histlist 0]
2970    if {[string range $expmap(htype_$h) 2 2] == "T"} {set flag 1}
2971    if {[string range $expmap(htype_$h) 1 2] == "NC"} {set flag 2}
2972    if {[string range $expmap(htype_$h) 1 2] == "XC" && \
2973            [histinfo $h lam2] != 0.0} {set flag 3}
2974    if {[string range $expmap(htype_$h) 1 2] == "XC" && \
2975            [histinfo $h lam2] == 0.0} {set flag 4}
2976    if {[string range $expmap(htype_$h) 1 2] == "XE"} {set flag 5}
2977
2978    set msg {}
2979    if {$expgui(abstype) == 0 && ($flag == 3 || $flag == 4)} {
2980        set msg "Mode 0 is appropriate for cylindrical (Debye-Scherrer) geometry only"
2981    } elseif {$expgui(abstype) == 1 && ($flag != 1 && $flag != 5)} {
2982        set msg "Mode 1 is appropriate for wavelength-dispersive (TOF/E.D. X-ray) data only"
2983    } elseif {($expgui(abstype) == 2 || $expgui(abstype) == 3) \
2984            && $flag != 3 && $flag != 4} {
2985        set msg "Mode 1 is appropriate for reflection geometry flat-plate (typically Bragg-Brentano) data only"
2986    } elseif {$expgui(abstype) == 4 && $flag <= 3} {
2987        set msg "Mode 4 is appropriate for flat-plate samples in transmission"
2988    }
2989    if {$msg != ""} {
2990        set result [\
2991                MyMessageBox -parent $top -title "Sanity check" \
2992                -type okcancel -default cancel \
2993                -icon warning -helplink "expgui3.html AbsorptionSanity" \
2994                -message "$msg  -- are you sure you want to do this?"]
2995        if {$result == "cancel"} return
2996    }
2997
2998    # validate abscor1 & abscor2 (if needed)
2999    set msg {}
3000    if {![validreal expgui(abscor1) 15 8]} {
3001        set msg "Term 1 is invalid"
3002    }
3003    if {$expgui(abstype) > 1} {
3004        if {![validreal expgui(abscor2) 15 8]} {
3005            if {$msg != ""} {append msg "\n"}
3006            append msg "Term 2 is invalid"
3007        }
3008    }
3009    if {$msg != ""} {
3010        MyMessageBox -parent $top -title "Entry error" \
3011                -type ok -default ok \
3012                -icon warning -helplink "" \
3013                -message "Invalid data entered. Please correct.\n$msg"
3014        return
3015    }
3016   
3017    histinfo $histlist abstype set $expgui(abstype)
3018    histinfo $histlist abscor1 set $expgui(abscor1)
3019    if {$expgui(abstype) > 1} {
3020        histinfo $histlist abscor2 set $expgui(abscor2)
3021    } else {
3022        histinfo $histlist abscor2 set 0.
3023    }
3024    # turn off refinement, just in case they didn't read
3025    if {($expgui(abstype) == 0 || $expgui(abstype) == 1 || $expgui(abstype) == 4) \
3026            && ($flag != 1 && $flag != 5)} {
3027        histinfo $histlist absref set 0
3028    }
3029    incr expgui(changed)
3030    destroy $top
3031}
3032
3033##############################################################################
3034##                               #############################################
3035## END OF THE PROCEDURES SECTION #############################################
3036##                               #############################################
3037##############################################################################
3038
3039# <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
3040# <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<                          <<<<<<<<<<<<<<<<<<<
3041# <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<   BEGIN:  GUI SECTION    >>>>>>>>>>>>>>>>>>>
3042# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>                          >>>>>>>>>>>>>>>>>>>
3043# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
3044# A frame for menu items at top of display
3045set expgui(fm) [frame .fm -relief raised -borderwidth 2 -width 150 -height 40]
3046# Pack the menu frame.
3047pack $expgui(fm) -fill x -side top -anchor n
3048
3049# create a button bar
3050pack [frame .bar -relief raised -bd 2 -bg beige] -fill x -side top -anchor n
3051
3052# Creating the notebook and panes
3053
3054# create an array element describing each notebook page (used with BW only)
3055# element 0 -- pane name
3056#         1 -- Label on frame
3057#         2 -- initialization command
3058#         3 -- update command
3059#         4 -- 0/1 Use 1 if pane should be disabled in when all histograms
3060#                are selected in global mode, 0 otherwise
3061#         5 -- Web page for pane
3062#         6 -- name anchor on Web page for pane
3063set expgui(notebookpagelist) {
3064    {lsFrame     "LS Controls" \
3065            "" \
3066            SetupExtractHist \
3067            0  expgui1.html ""}
3068    {phaseFrame   Phase        \
3069            "" \
3070            {SelectOnePhase $expgui(curPhase)} \
3071            0  expgui2.html ""}
3072    {histFrame    Histogram \
3073            MakeHistPane \
3074            DisplayHistogram \
3075            1  expgui3.html ""}
3076    {fracFrame    Scaling \
3077            MakeScalingPane \
3078            DisplayFrac \
3079            0  expgui4.html ""}
3080    {profFrame    Profile \
3081            MakeProfilePane \
3082            DisplayProfile \
3083            1  expgui5.html ""}
3084    {consFrame    Constraints \
3085            "source [file join $expgui(scriptdir) atomcons.tcl]; MakeConstraintsPane" \
3086            DisplayConstraintsPane \
3087            0  expgui6.html ""}
3088    {orientFrame  "MD Pref Orient" \
3089            MakeOrientPane \
3090            DisplayOrient \
3091            0  expgui7.html MD}
3092    {odfFrame  "SH Pref Orient" \
3093            "source [file join $expgui(scriptdir) odf.tcl]; MakeODFPane" \
3094            DisplayODFPane \
3095            0  expgui7.html ODF}
3096}
3097
3098if $expgui(haveBW) {
3099    pack [NoteBook .n -bd 2] -expand yes -fill both
3100    # this should not be needed, but for some reason NoteBook is not
3101    # using the optionDB
3102    catch {.n configure -font [option get .n font Canvas]}
3103    foreach item $expgui(notebookpagelist) {
3104        set frm [lindex $item 0]
3105        set expgui($frm) [\
3106            .n insert end $frm -text [lindex $item 1] \
3107            -createcmd "set expgui(pagenow) $frm; [lindex $item 2]" \
3108            -raisecmd "set expgui(pagenow) $frm; [lindex $item 3]"]
3109
3110        # at this time expgui(frameactionlist) is generated
3111        # from expgui(notebookpagelist), but in the future it might
3112        # make sense to use expgui(notebookpagelist) directly
3113        lappend expgui(frameactionlist) "$frm [list [lindex $item 3]]"
3114
3115        # panes to disable in global "all" mode
3116        if {[lindex $item 4]} {
3117            lappend expgui(GlobalModeAllDisable) \
3118                    "$frm \{.n itemconfigure $frm\}"
3119        }
3120    }
3121} else {
3122    Notebook:create .n \
3123            -pages {lsFrame phaseFrame histFrame fracFrame profFrame}
3124    pack .n -anchor w -fill both -expand yes
3125    foreach item {lsFrame phaseFrame histFrame fracFrame profFrame \
3126            orientFrame} \
3127            page {"LS Controls" Phase Histogram Scaling Profile \
3128            "MD Pref Orient"} {
3129        set expgui($item) [Notebook:frame .n $item]
3130        Notebook:pageconfig .n $item -command "InitPage $item" -title $page
3131    }
3132    lappend expgui(frameactionlist) "lsFrame SetupExtractHist"
3133    lappend expgui(frameactionlist) "phaseFrame {DisplayAllAtoms $expgui(curPhase) noreset}"
3134    lappend expgui(frameactionlist) "histFrame DisplayHistogram"
3135    lappend expgui(frameactionlist) "fracFrame DisplayFrac"
3136    lappend expgui(frameactionlist) "profFrame DisplayProfile"
3137    lappend expgui(frameactionlist) "orientFrame DisplayOrient"
3138    set expgui(GlobalModeAllDisable) {}
3139    lappend expgui(GlobalModeAllDisable) "histFrame {Notebook:pageconfig .n histFrame}"
3140    lappend expgui(GlobalModeAllDisable) "profFrame {Notebook:pageconfig .n profFrame}"
3141}
3142
3143# this is used to bring up the selected frame
3144proc RaisePage {nextpage} {
3145    global expgui
3146    if $expgui(haveBW) {
3147        set expgui(pagenow) $nextpage
3148        .n see $nextpage
3149        .n raise $nextpage
3150    } else {
3151        Notebook:raise .n $nextpage
3152        InitPage $nextpage
3153    }
3154}
3155# this is only called when BWidget is not in use
3156proc InitPage {nextpage} {
3157    global expgui
3158    set expgui(pagenow) $nextpage
3159    UpdateCurrentPage
3160}
3161# resize the notebook to fit all the tabs and the largest page
3162proc ResizeNotebook {} {
3163    global expgui
3164    if {$expgui(haveBW)} {
3165        .n compute_size
3166    } else {
3167        Notebook:resize .n
3168    }
3169}
3170
3171#----------------------------------------------------------------------------
3172proc MakePhasePane {} {
3173    #\/ \/ \/ \/ \/ \/ \/ BEGINNING OF PHASE PANE CODE \/ \/ \/ \/ \/ \/ \/
3174    global expgui entryvar entrybox entrycmd
3175    frame $expgui(phaseFrame).top
3176    set frameLatt [frame $expgui(phaseFrame).frameLatt]
3177    #  This is a big frame in the Phase notebook pane to hold atomic data.
3178    set fbig [frame $expgui(phaseFrame).fbig -width 180 \
3179            -relief raised -borderwidth 4 -class Coord]
3180    #  This is a frame just below the big frame: for edits
3181    set frame3 [frame $expgui(phaseFrame).frame3 -width 100 \
3182            -relief raised -borderwidth 4 -bg $expgui(bkgcolor1)]
3183
3184    grid $expgui(phaseFrame).top -sticky news -row 0 -column 0
3185    grid $frameLatt -sticky news -row 2 -column 0
3186    grid $fbig -sticky news -row 3 -column 0
3187    # give extra space to the atoms box
3188    grid columnconfigure $expgui(phaseFrame) 0 -weight 1
3189    grid rowconfigure $expgui(phaseFrame) 3 -weight 1
3190    grid $frame3 -sticky news -row 4 -column 0
3191    grid columnconfigure $expgui(phaseFrame) 0 -weight 1
3192    grid rowconfigure $expgui(phaseFrame) 3 -weight 1
3193    grid [frame  $expgui(phaseFrame).top.ps] -column 0 -row 0 -sticky w
3194    # this is where the buttons will go
3195    pack [label $expgui(phaseFrame).top.ps.0 -text "No Phases"] -side left
3196   
3197    grid [label $expgui(phaseFrame).top.lA -text title: \
3198            -fg blue ] -column 1 -row 0 -sticky e
3199    grid [entry $expgui(phaseFrame).top.lB -textvariable entryvar(phasename) \
3200            -fg blue -width 45] -column 2 -row 0 -sticky e
3201    grid columnconfigure $expgui(phaseFrame).top 1 -weight 1
3202    # ------------- Lattice Parameter Box ------------------
3203    set row 0
3204    foreach col {2 4 6} var {a b c} lbl {a b c} {
3205        grid [label $frameLatt.l$var -text $lbl] \
3206                -column $col -row $row -padx 5 -sticky e
3207        incr col
3208        grid [entry $frameLatt.e$var -textvariable entryvar($var) -width 10] \
3209            -column $col -row $row -padx 5
3210        set entrybox($var) $frameLatt.e$var
3211    }
3212    incr row
3213    foreach col {2 4 6} var {alpha beta gamma} lbl {a b g} {
3214        grid [label $frameLatt.l$var -text $lbl] \
3215                -column $col -row $row -padx 5 -sticky e
3216        set font [$frameLatt.l$var cget -font]
3217        $frameLatt.l$var config -font "Symbol [lrange $font 1 end]"
3218
3219        incr col
3220        grid [entry $frameLatt.e$var -textvariable entryvar($var) -width 10] \
3221            -column $col -row $row -padx 5
3222        set entrybox($var) $frameLatt.e$var
3223    }
3224   
3225    grid [label $frameLatt.lr -text "Refine Cell"] -column 8 -row 0 -padx 5 -sticky e
3226    grid [label $frameLatt.ld -text "Cell damping"] -column 8 -row 1 -padx 5 -sticky e
3227    set cFlag [checkbutton $frameLatt.c -text "" -variable entryvar(cellref)]
3228    grid $cFlag -column 9 -row 0 -padx 5 -sticky e
3229    tk_optionMenu $frameLatt.om entryvar(celldamp) 0 1 2 3 4 5 6 7 8 9
3230    grid $frameLatt.om -column 9 -row 1 -padx 5 -sticky e
3231    grid [label $frameLatt.phasetype -textvariable expgui(phasetype) -fg blue] \
3232            -column 1 -row 0 -rowspan 2
3233    if [file executable $expgui(exptool)] {
3234        grid [button $expgui(phaseFrame).frameLatt.newp \
3235                -text "Add\nPhase" -padx 1.5m -command MakeAddPhaseBox \
3236                ] -column 0 -row 0 -rowspan 2 -sticky w
3237    }
3238    grid columnconfig $frameLatt  1 -weight 1
3239    grid columnconfig $frameLatt  0 -weight 1
3240    #-------------- Begin Atom Coordinates Box  ------------------------------
3241    grid [listbox  $fbig.title -height 1 -relief flat \
3242            -exportselection 0 -bg lightgrey -fg black \
3243            -selectforeground black -selectbackground lightgrey] \
3244            -row 0 -column 0 -sticky ew
3245    set expgui(atomtitle) $fbig.title
3246    bind $expgui(atomtitle) <Button-1> {
3247        set i [lsearch {number type mult x y z occupancy} $expgui(asorttype)]
3248        incr i
3249        set expgui(asorttype) [lindex {number type mult x y z occupancy number} $i]
3250        DisplayAllAtoms $expgui(curPhase)
3251    }
3252    bind $expgui(atomtitle) <Button-3> {set expgui(asorttype) number; DisplayAllAtoms $expgui(curPhase)}
3253
3254    $expgui(atomtitle) configure -selectmode extended
3255    grid [listbox   $fbig.lbox -height 10 \
3256            -exportselection 0 \
3257            -xscrollcommand " $fbig.bscr set"\
3258            -yscrollcommand " $fbig.rscr set"\
3259            ] -row 1 -column 0 -sticky news
3260    set expgui(atomlistbox) $fbig.lbox
3261    $expgui(atomlistbox) configure -selectmode extended
3262    grid [scrollbar $fbig.bscr -orient horizontal \
3263            -command "move2boxesX \" $fbig.title $fbig.lbox \" " \
3264            ] -row 2 -column 0 -sticky ew
3265    grid [scrollbar $fbig.rscr  -command "$fbig.lbox yview" \
3266            ] -row 1 -column 1 -sticky ns
3267    # give extra space to the atoms box
3268    grid columnconfigure $fbig 0 -weight 1
3269    grid rowconfigure $fbig 1 -weight 1
3270   
3271    #   BIND mouse in editbox
3272    bind $expgui(atomlistbox) <ButtonRelease-1>   editRecord
3273    bind $expgui(atomlistbox) <Button-3>   SelectAllAtoms
3274   
3275    #-------------- End Atoms Section  ---------------------------------
3276
3277    # --------------------------- Begin Edit Box ------------------------
3278    grid [set expgui(EditingAtoms) [label $frame3.top -bg $expgui(bkgcolor1) -fg blue]] \
3279            -column 0 -row 0 -padx 2 -pady 3 -columnspan 10 -sticky w
3280    if [file executable $expgui(exptool)] {
3281        button $frame3.newa -text "Add New Atoms" \
3282                -bg $expgui(bkgcolor1) -highlightthickness 0 \
3283                -command {MakeAddAtomsBox $expgui(curPhase)}
3284        grid $frame3.newa -column 11 -row 0
3285        set expgui(AddAtomBut) $frame3.newa
3286    }
3287    button [set expgui(atomxform) $frame3.xa] \
3288            -bg $expgui(bkgcolor1) -highlightthickness 0 \
3289            -command {MakeXformAtomsBox $expgui(curPhase)}
3290    grid $expgui(atomxform) -column 11 -row 1 -sticky ew
3291
3292    set f3l1 [label $frame3.l1 -text "Refinement Flags:" -bg $expgui(bkgcolor1)]
3293    grid $f3l1 -column 0 -row 1 -padx 2 -sticky nsw -pady 3
3294    foreach lbl {X U F} var {xref uref fref} col {1 2 3} {
3295        grid [checkbutton $frame3.cf$col \
3296                -text $lbl -variable entryvar($var) \
3297                -bg $expgui(bkgcolor1) -highlightthickness 0 \
3298                -activebackground $expgui(bkgcolor1)] \
3299                -column $col -row 1 -padx 4 -pady 3 -sticky w
3300    }
3301    set f3l4 [label $frame3.l4 -text "  Damping:" -bg $expgui(bkgcolor1)]
3302    grid $f3l4 -column 4 -row 1 -padx 2 -sticky nsw -pady 3
3303   
3304    set col 4
3305    foreach var {xdamp udamp fdamp} num {2 3 4} lbl {X U F} {
3306        grid [label $frame3.lom$num -text $lbl \
3307                -bg $expgui(bkgcolor1)] \
3308                -column [incr col] -row 1 -padx 2 -pady 3 -sticky w
3309        tk_optionMenu $frame3.om$num entryvar($var) 0 1 2 3 4 5 6 7 8 9
3310        $frame3.om$num config -highlightthickness 0
3311        grid $frame3.om$num -column [incr col] -row 1 -padx 2 -pady 3 -sticky w
3312    }
3313    set expgui(atomreflbl) "$frame3.l1 $frame3.l4 $frame3.lom2 $frame3.lom3 $frame3.lom4 "
3314    set expgui(atomref) "$frame3.cf1 $frame3.cf2 $frame3.cf3 $frame3.om2 $frame3.om3 $frame3.om4"
3315   
3316    set coords [frame $frame3.coords  -width 100 -borderwidth 0  -bg $expgui(bkgcolor1)]
3317    grid $coords -column 0 -row 6 -columnspan 12 -sticky nsew
3318   
3319    set f3l1 [label $frame3.coords.l1 -text "Label" -bg $expgui(bkgcolor1)]
3320    grid $f3l1 -column 0 -row 4 -padx 2 -sticky nsw -pady 3
3321    set expgui(atomlabels) $f3l1
3322
3323    set f3e1 [entry  $frame3.coords.e1 -textvariable entryvar(label) -width 6]
3324    grid $f3e1 -column 1 -row 4 -padx 2 -sticky nsw -pady 3
3325    set expgui(atomentry) $f3e1
3326
3327    set f3l8 [label $frame3.coords.l8 -text "Coordinates" -bg $expgui(bkgcolor1)]
3328    grid $f3l8 -column 2 -row 4 -padx 2 -sticky nsw -pady 3
3329    lappend expgui(atomlabels) $f3l8
3330    set f3l11 [label $frame3.coords.l11 -text "Occupancy" -bg $expgui(bkgcolor1)]
3331    grid $f3l11 -column 6 -row 4 -padx 2 -sticky nsw -pady 3
3332    lappend expgui(atomlabels) $f3l11
3333
3334    foreach var {x y z frac} col {3 4 5 7} {
3335        set entrybox($var) [entry $frame3.coords.e$var \
3336                -textvariable entryvar($var) -width 10]
3337        grid $entrybox($var) -column $col -row 4 -padx 2 -sticky nsw -pady 3
3338        lappend expgui(atomentry) $entrybox($var)
3339    }
3340
3341
3342    set f3f31 [frame $frame3.f3f31  -width 100 -borderwidth 0 -bg $expgui(bkgcolor1)]
3343    grid $f3f31 -column 0 -row 7 -columnspan 12
3344    set expgui(anisolabels) {}
3345    foreach lbl {13 14 15 16 17 18} txt {Uiso U22 U33 U12 U13 U23} {
3346        lappend expgui(anisolabels)  [\
3347                label $f3f31.l$lbl -text $txt -bg $expgui(bkgcolor1)
3348        ]
3349    }
3350    set expgui(anisoentry) {}
3351    foreach i {e13 e14 e15 e16 e17 e18} var {U11 U22 U33 U12 U13 U23} {
3352        lappend expgui(anisoentry) [\
3353                entry $f3f31.$i -textvariable entryvar($var) \
3354                -width 10]
3355        set entrybox($var) $f3f31.$i
3356    }
3357   
3358    set col 0
3359    foreach item1 $expgui(anisolabels) item2 $expgui(anisoentry) {
3360        grid $item1 -column $col -row 0 -sticky nsw -pady 3
3361        incr col
3362        grid $item2 -column $col -row 0 -sticky nsw -pady 3
3363        incr col
3364    }
3365    # --------------------------- End Edit Box -------------------------
3366   
3367    #/\ /\ /\ /\ /\ /\ /\ END OF PHASE PANE CODE /\ /\ /\ /\ /\ /\ /\ /\ /
3368    # resize in case the pane needs more space
3369    ResizeNotebook
3370}
3371
3372#-----------------------------------------------------------------------------
3373proc MakeHistPane {} {
3374    #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
3375    global expgui
3376   
3377    grid columnconfigure $expgui(histFrame) 0 -weight 1
3378    grid rowconfigure $expgui(histFrame) 1 -weight 1
3379    grid rowconfigure $expgui(histFrame) 2 -weight 1
3380    grid rowconfigure $expgui(histFrame) 3 -weight 1
3381
3382    grid [frame $expgui(histFrame).hs -class HistList] \
3383            -column 0 -row 0 -rowspan 10 -sticky nsew
3384    MakeHistBox $expgui(histFrame).hs
3385    bind $expgui(histFrame).hs.lbox <ButtonRelease-1>  {
3386        set expgui(curhist) [$expgui(histFrame).hs.lbox curselection]
3387        DisplayHistogram
3388    }
3389    bind $expgui(histFrame).hs.lbox <Button-3>  {
3390        if $expgui(globalmode) {
3391            $expgui(histFrame).hs.lbox selection set 0 end
3392            set expgui(curhist) [$expgui(histFrame).hs.lbox curselection]
3393            DisplayHistogram
3394        }
3395    }
3396   
3397    frame $expgui(histFrame).top -borderwidth 4 -relief groove
3398    grid [label $expgui(histFrame).top.txt] -row 0 -column 0
3399    if $expgui(haveBW) {
3400        foreach item {backBox diffBox absBox} num {2 3 4} \
3401                title {Background "Diffractometer Constants" \
3402                "Absorption/Reflectivity Correction"} {
3403            TitleFrame $expgui(histFrame).$item  \
3404                    -borderwidth 4 -side left -relief groove -text $title
3405            set expgui($item) [$expgui(histFrame).$item getframe]
3406            grid $expgui(histFrame).$item -column 1 -row $num -sticky nsew
3407            grid rowconfigure $expgui(histFrame) $num -minsize 100
3408        }
3409    } else {
3410        foreach item {backBox diffBox absBox} num {1 2 3} \
3411                title {Background "Diffractometer Constants" \
3412                "Absorp./Reflect. Corr"} {
3413            frame $expgui(histFrame).$item  -borderwidth 4 -relief groove
3414            grid $expgui(histFrame).$item -column 1 -row $num -sticky nsew
3415            set expgui($item)  $expgui(histFrame).$item
3416            grid [label $expgui(histFrame).$item.title -text $title] \
3417                    -row 0 -column 0 -columnspan 10 -sticky nw
3418        }
3419    }
3420
3421    grid [frame $expgui(histFrame).bb] -column 1 -row 6
3422    if [file executable $expgui(exptool)] {
3423        button $expgui(histFrame).bb.newh -text "Add New\nHistogram" \
3424                -command MakeAddHistBox
3425        grid $expgui(histFrame).bb.newh -column 0 -row 1
3426    }
3427    button $expgui(histFrame).bb.excl \
3428            -text "Set Data Limits &\nExcluded Regions" -command excledit
3429    grid $expgui(histFrame).bb.excl -column 1 -row 1
3430
3431    button $expgui(histFrame).bb.use -text "Set Histogram\nUse Flags" \
3432            -command SetHistUseFlags
3433    grid $expgui(histFrame).bb.use -column 2 -row 1
3434
3435    # BACKGROUND information.
3436    # <<<<<<<<<<<<<<<<<<<<<<<<< BACKGROUND  <<<<<<<<<<<<<<<<<<<<<
3437    grid [frame $expgui(backBox).frm1 ] -row 0 -column 0  -columnspan 11
3438    grid [label $expgui(backBox).frm1.lBGType \
3439            -textvariable expgui(backtypelbl)] \
3440            -row 1 -column 0 -sticky nws  -padx 2 -pady 3
3441    grid [label $expgui(backBox).frm1.lBGTerms \
3442            -textvariable expgui(backtermlbl)] \
3443            -row 1 -column 1 -sticky nws  -padx 2 -pady 3
3444    grid [button $expgui(backBox).frm1.edit -textvariable expgui(bkglbl) \
3445            -command editbackground] \
3446            -row 1 -column 2 -columnspan 3 -sticky w -padx 2 -pady 3
3447    grid [frame $expgui(backBox).frm2 ] \
3448            -row 1 -column 0 -columnspan 11 -sticky e
3449    grid [label $expgui(backBox).frm2.lfBG -text "  Refine background" ] \
3450            -row 2 -column 1 -sticky news -padx 4 -pady 3
3451    grid [checkbutton $expgui(backBox).frm2.rfBG -text "" \
3452            -variable  entryvar(bref) ] \
3453            -row 2 -column 2 -sticky news -padx 4 -pady 3
3454    grid [label $expgui(backBox).frm2.lBGDamp -text Damping ] \
3455            -row 2 -column 3 -sticky w    -padx 2 -pady 3
3456    tk_optionMenu $expgui(backBox).frm2.om  entryvar(bdamp) 0 1 2 3 4 5 6 7 8 9
3457    grid $expgui(backBox).frm2.om \
3458            -row 2 -column 4 -sticky news -padx 4 -pady 3 -sticky e
3459    # Absorption information.
3460    grid [label $expgui(absBox).rf1 -text "  Refine Abs./Refl." ] \
3461            -row 2 -column 1 -sticky news -padx 4 -pady 3
3462    grid [checkbutton $expgui(absBox).rf2 -text "" \
3463            -variable  entryvar(absref) ] \
3464            -row 2 -column 2 -sticky news -padx 4 -pady 3
3465    grid [label $expgui(absBox).d1 -text Damping ] \
3466            -row 2 -column 3 -sticky w    -padx 2 -pady 3
3467    tk_optionMenu $expgui(absBox).d2  entryvar(absdamp) 0 1 2 3 4 5 6 7 8 9
3468    grid $expgui(absBox).d2 \
3469            -row 2 -column 4 -sticky news -padx 4 -pady 3 -sticky e
3470    grid [button $expgui(absBox).edit -textvariable expgui(abslbl) \
3471            -command editabsorption] \
3472            -row 2 -column 5 -sticky w -padx 2 -pady 3
3473
3474    #^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^END OF HISTOGRAM PANE CODE ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
3475    # insert the histograms & resize in case the pane needs more space   
3476    sethistlist
3477    ResizeNotebook
3478}
3479###############################################################################
3480proc MakeScalingPane {} {
3481    #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
3482    global expgui entryvar entrybox
3483
3484    pack [frame $expgui(fracFrame).hs -class HistList] \
3485            -side left -expand y -fill both
3486    MakeHistBox $expgui(fracFrame).hs
3487    bind $expgui(fracFrame).hs.lbox <ButtonRelease-1> {
3488        set expgui(curhist) [$expgui(fracFrame).hs.lbox curselection]
3489        DisplayFrac
3490    }
3491    bind $expgui(fracFrame).hs.lbox <Button-3>  {
3492        if $expgui(globalmode) {
3493            $expgui(fracFrame).hs.lbox selection set 0 end
3494            set expgui(curhist) [$expgui(fracFrame).hs.lbox curselection]
3495            DisplayFrac
3496        }
3497    }
3498
3499    pack [frame $expgui(fracFrame).f1] -fill both -expand true
3500    # Create a large canvas area containing a frame for each phase in the data set.
3501    # The canvas and vertical scrollbar are inside a frame called f1
3502    if $expgui(haveBW) {
3503        TitleFrame $expgui(fracFrame).f1.scaleBox \
3504                -borderwidth 4 -text "Scale Factor"
3505        #           -borderwidth 4 -width 600 -height 100 -label "Scale Factor"
3506        grid $expgui(fracFrame).f1.scaleBox -column 0 -row 0 -sticky nsew -columnspan 2
3507        set expgui(scaleBox)  [$expgui(fracFrame).f1.scaleBox getframe]
3508        grid [label $expgui(scaleBox).histSFLabel -text Scale] \
3509                -row 1 -column 0 -sticky nws  -padx 2 -pady 3
3510    } else {
3511        frame $expgui(fracFrame).f1.scaleBox  -borderwidth 4 -relief groove
3512        grid $expgui(fracFrame).f1.scaleBox -column 0 -row 0 -sticky nsew -columnspan 2
3513        set expgui(scaleBox)  $expgui(fracFrame).f1.scaleBox
3514        grid [label $expgui(scaleBox).histSFLabel -text "Scale Factor"] \
3515                -row 1 -column 0 -sticky nws  -padx 2 -pady 3
3516    }
3517    grid [entry $expgui(scaleBox).ent1 -textvariable entryvar(scale) -width 15] \
3518            -row 1 -column 1 -sticky ew -padx 4 -pady 3
3519    set entrybox(scale) $expgui(scaleBox).ent1
3520
3521    button $expgui(scaleBox).but1 -text "Set Globally" \
3522            -command "editglobalparm histinfo scale {Scale Factor}"
3523
3524    grid [label $expgui(scaleBox).histSFRLabel -text " Refine"] \
3525            -row 1 -column 2 -sticky nws  -padx 2 -pady 3
3526    grid [checkbutton $expgui(scaleBox).rf -variable entryvar(sref)] \
3527            -row 1 -column 3 -sticky news -padx 4 -pady 3
3528    grid [label $expgui(scaleBox).lD1 -text "Damping"] \
3529            -row 1 -column 4 -sticky w    -padx 2 -pady 3
3530    tk_optionMenu $expgui(scaleBox).om entryvar(sdamp) 0 1 2 3 4 5 6 7 8 9
3531    grid $expgui(scaleBox).om \
3532            -row 1 -column 5 -sticky news -padx 4 -pady 3
3533    grid columnconfigure $expgui(scaleBox) 6  -weight 1
3534   
3535    if $expgui(haveBW) {
3536        grid [TitleFrame $expgui(fracFrame).f1.phaseFrac -bd 4 \
3537                -text "Phase Fractions" -relief groove] \
3538                -sticky news -row 1 -column 0 -columnspan 2
3539        set PhaseFractBox [$expgui(fracFrame).f1.phaseFrac getframe]
3540    } else {
3541        set PhaseFractBox $expgui(fracFrame).f1
3542    }
3543    grid columnconfigure $expgui(fracFrame).f1 0 -weight 1
3544    grid rowconfigure $expgui(fracFrame).f1 1 -weight 1
3545   
3546    grid [set expgui(FracBox) [canvas $PhaseFractBox.fracBox \
3547            -scrollregion {0 0 5000 500} \
3548            -yscrollcommand "$PhaseFractBox.yscroll set" \
3549            -width 500 -height 350 -bg lightgrey]] \
3550            -sticky  news -row 1 -column 0
3551    grid [scrollbar $PhaseFractBox.yscroll \
3552            -command "$expgui(FracBox) yview" \
3553            -orient vertical] \
3554            -sticky ns -row 1 -column 1
3555    frame $expgui(FracBox).f -bd 0
3556    $expgui(FracBox) create window 0 0 -anchor nw  -window $expgui(FracBox).f
3557
3558    # the rest of the page is created in DisplayFrac
3559
3560    # insert the histograms & resize in case the pane needs more space
3561    sethistlist
3562    ResizeNotebook
3563    # ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ END OF SCALING PANE CODE ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
3564}
3565###############################################################################
3566proc MakeProfilePane {} {
3567    global expgui
3568    # 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
3569    pack [frame $expgui(profFrame).hs -class HistList] \
3570            -side left -expand y -fill both
3571    MakeHistBox $expgui(profFrame).hs
3572    bind $expgui(profFrame).hs.lbox <ButtonRelease-1> {
3573        set expgui(curhist) [$expgui(profFrame).hs.lbox curselection]
3574        DisplayProfile
3575    }
3576    bind $expgui(profFrame).hs.lbox <Button-3>  {
3577        if $expgui(globalmode) {
3578            $expgui(profFrame).hs.lbox selection set 0 end
3579            set expgui(curhist) [$expgui(profFrame).hs.lbox curselection]
3580            DisplayProfile
3581        }
3582    }
3583
3584    # Create a large canvas area containing a frame for each phase in the data set.
3585    # The canvas and vertical scrollbar are inside a frame called f1
3586    pack [frame $expgui(profFrame).f1] -fill both -expand true
3587    grid [set expgui(ProfileBox) [canvas $expgui(profFrame).f1.profileBox \
3588            -scrollregion {0 0 5000 500} -width 500 -height 350 -bg lightgrey]] \
3589            -sticky  news -row 0 -column 0
3590    grid [scrollbar $expgui(profFrame).f1.yscroll -orient vertical] \
3591            -sticky ns -row 0 -column 1
3592   
3593    $expgui(ProfileBox) config -yscrollcommand "$expgui(profFrame).f1.yscroll set"
3594    $expgui(profFrame).f1.yscroll config -command { $expgui(ProfileBox) yview }
3595   
3596    grid columnconfigure $expgui(profFrame).f1 1 -weight 1
3597    grid rowconfigure $expgui(profFrame).f1 0 -weight 1
3598    frame $expgui(ProfileBox).f -bd 0
3599    $expgui(ProfileBox) create window 0 0 -anchor nw  -window $expgui(ProfileBox).f
3600   
3601    # insert the histograms & resize in case the pane needs more space
3602    sethistlist
3603    ResizeNotebook
3604    # ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ END OF PROFILE PANE CODE ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
3605}
3606
3607##############################################################################
3608# 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
3609array set printopts {
3610    0 "Print the reciprocal metric tensor changes"
3611    1 "Print the correlation matrix"
3612    2 "Print the Least-Squares matrices and vectors"
3613    4 "Print the linear constraint matrices"
3614    5 "Print the applied  shifts and shift factors"
3615    6 "Print the reciprocal metric tensor Var-Covar terms"
3616    7 "Print all parameters for each cycle"
3617    8 "Print summary shift/esd data after last cycle"
3618    9 "Print zero/unit pole figure constraint terms"
3619}
3620pack [frame $expgui(lsFrame).hs -class HistList] \
3621        -side left -expand y -fill both
3622MakeHistBox $expgui(lsFrame).hs
3623bind $expgui(lsFrame).hs.lbox <ButtonRelease-1> {
3624    set expgui(curhist) [$expgui(lsFrame).hs.lbox curselection]
3625    SetupExtractHist
3626}
3627bind $expgui(lsFrame).hs.lbox <Button-3>  {
3628    if $expgui(globalmode) {
3629        $expgui(lsFrame).hs.lbox selection set 0 end
3630        set expgui(curhist) [$expgui(lsFrame).hs.lbox curselection]
3631        SetupExtractHist
3632    }
3633}
3634
3635pack [frame $expgui(lsFrame).f1] -fill both -expand true
3636set row 0
3637grid [label $expgui(lsFrame).f1.his1 -pady 6 -text "Last History:"] -row $row -column 0
3638grid [label $expgui(lsFrame).f1.his2 -relief raised -bd 2 -pady 6 \
3639        -textvariable expgui(last_History)] \
3640        -row $row -column 1 -columnspan 5 -sticky w
3641incr row
3642grid [label $expgui(lsFrame).f1.tit1 -pady 6 -text "Title:"] -row $row -column 0
3643grid [entry $expgui(lsFrame).f1.tit2 \
3644        -textvariable entryvar(title) -width 48] \
3645        -row $row -column 1 -columnspan 5 -sticky w
3646set entrycmd(title) "expinfo title"
3647
3648incr row
3649grid rowconfigure $expgui(lsFrame).f1 $row -weight 1
3650incr row
3651grid [frame $expgui(lsFrame).f1.b -bd 4 -relief groove] \
3652        -row $row -column 0 -columnspan 2 -pady 3  -sticky s
3653grid [label $expgui(lsFrame).f1.b.lcyc -text "Number of Cycles"] -row 0 -column 0
3654grid [entry $expgui(lsFrame).f1.b.ecyc -width 3 \
3655        -textvariable entryvar(cycles)] -row 0 -column 1
3656set entrybox(cycles) $expgui(lsFrame).f1.b.ecyc
3657
3658grid [frame $expgui(lsFrame).f1.cv -bd 4 -relief groove] \
3659        -row $row -column 2 -sticky ew
3660grid [label $expgui(lsFrame).f1.cv.l -text "Convgerence Criterion"] \
3661        -row 0 -column 0 -columnspan 2
3662grid [label $expgui(lsFrame).f1.cv.v -textvariable expgui(convlbl)] -row 1 -column 0
3663grid [scale $expgui(lsFrame).f1.cv.s -orient horizontal \
3664        -from -200 -to 200 -showvalue 0 -command SetConv -resolution 10 \
3665        -variable expgui(convg)] -row 1 -column 1
3666
3667incr row
3668grid [menubutton $expgui(lsFrame).f1.lprint -textvariable expgui(printopt) \
3669        -menu $expgui(lsFrame).f1.lprint.menu -bd 4 -relief raised \
3670        ] -row $row -column 0 -columnspan 2
3671menu $expgui(lsFrame).f1.lprint.menu
3672foreach num [lsort [array names printopts]] {
3673    $expgui(lsFrame).f1.lprint.menu add checkbutton \
3674            -label "$printopts($num) ([expr int(pow(2,$num))])"\
3675        -variable entryvar(printopt$num)
3676}
3677
3678grid [frame $expgui(lsFrame).f1.marq -bd 4 -relief groove] \
3679        -row $row -column 2 -sticky ew
3680grid [label $expgui(lsFrame).f1.marq.l -text "Marquardt Damping"] \
3681        -row 0 -column 0 -columnspan 2
3682grid [label $expgui(lsFrame).f1.marq.v -textvariable expgui(marq)] \
3683        -row 1 -column 0
3684grid [scale $expgui(lsFrame).f1.marq.s -orient horizontal \
3685        -from 1.0 -to 9.99 -showvalue 0 -command SetMarq -resolution 0.01 \
3686        -variable expgui(marq)] -row 1 -column 1
3687
3688incr row
3689grid rowconfigure $expgui(lsFrame).f1 $row -weight 1
3690
3691incr row
3692if {$expgui(haveBW)} {
3693    grid [TitleFrame $expgui(lsFrame).f1.a -bd 4 -relief groove \
3694            -text "Reflection Intensity Extraction" \
3695            ] -row $row -column 0 -columnspan 6
3696    set expgui(FobsExtractFrame) [$expgui(lsFrame).f1.a getframe]
3697} else {
3698    grid [frame $expgui(lsFrame).f1.a -bd 4 -relief groove \
3699            ] -row $row -column 0 -columnspan 6
3700    set expgui(FobsExtractFrame) $expgui(lsFrame).f1.a
3701}
3702
3703grid [frame $expgui(FobsExtractFrame).c -bd 4 -relief groove] \
3704        -row 0 -column 8 -columnspan 3 -sticky ens
3705grid [label $expgui(FobsExtractFrame).c.fol -text "Extract Fobs"] \
3706        -row 0 -column 2
3707grid [checkbutton $expgui(FobsExtractFrame).c.foc \
3708        -variable entryvar(fobsextract)] -row 0 -column 3
3709
3710grid [frame $expgui(FobsExtractFrame).d -bd 4 -relief groove] \
3711        -row 0 -column 3 -columnspan 5 -sticky ens
3712grid [label $expgui(FobsExtractFrame).d.fol -text "LeBail damping"] \
3713        -row 0 -column 2
3714tk_optionMenu $expgui(FobsExtractFrame).d.d entryvar(LBdamp) \
3715        0 1 2 3 4 5 6 7 8 9
3716grid $expgui(FobsExtractFrame).d.d -row 0 -column 3
3717incr row
3718grid rowconfigure $expgui(lsFrame).f1 $row -weight 1
3719
3720
3721
3722foreach num {1 2 3 4 5 6 7 8 9} {
3723    grid [label $expgui(FobsExtractFrame).l$num -text $num] -row 1 -column $num
3724    grid [radiobutton $expgui(FobsExtractFrame).cc$num \
3725            -command "HistExtractSet $num" \
3726            -variable expgui(Fextract$num) -value 0] \
3727            -row 2 -column $num
3728    grid [radiobutton $expgui(FobsExtractFrame).ca$num \
3729            -command "HistExtractSet $num" \
3730            -variable expgui(Fextract$num) -value 1] \
3731            -row 3 -column $num
3732    grid [radiobutton $expgui(FobsExtractFrame).cb$num \
3733            -command "HistExtractSet $num" \
3734            -variable expgui(Fextract$num) -value 2] \
3735            -row 4 -column $num
3736}
3737set expgui(ExtractSettingsRadiobuttons) $expgui(FobsExtractFrame).cc
3738lappend expgui(ExtractSettingsRadiobuttons) $expgui(FobsExtractFrame).ca
3739lappend expgui(ExtractSettingsRadiobuttons) $expgui(FobsExtractFrame).cb
3740
3741grid [label $expgui(FobsExtractFrame).t \
3742        -text "Extraction\nMethod" -anchor c] \
3743        -column 0 -row 0 -sticky n
3744grid [label $expgui(FobsExtractFrame).t0 -text "(Phase #)" -anchor c] \
3745        -column 10 -row 1 -sticky w
3746grid [label $expgui(FobsExtractFrame).t1 -text "Rietveld" -anchor c] -column 0 -row 2
3747grid [label $expgui(FobsExtractFrame).t2 -text "F(calc) Weighted" -anchor c] -column 0 -row 3
3748grid [label $expgui(FobsExtractFrame).t3 -text "Equally Weighted" -anchor c] -column 0 -row 4
3749grid [label $expgui(FobsExtractFrame).t2a -text "(Model biased)" -anchor c] -column 10 -row 3
3750grid [label $expgui(FobsExtractFrame).t3a -text "(Le Bail method)" -anchor c] -column 10 -row 4
3751
3752proc InitLSvars {} {
3753    global expgui
3754    set expgui(convg) [set expgui(convinit) [expinfo convg]]
3755    set expgui(convlbl) [format %5.2f [expr pow(10,$expgui(convg)/100.)]]
3756    set expgui(marq) [set expgui(marqinit) [expinfo marq]]
3757}
3758proc SetConv {x} {
3759    global expgui
3760    if {$x != $expgui(convinit) && $expgui(changed) <= 0} {
3761        incr expgui(changed)
3762    }
3763    if {$expgui(changed)} {expinfo convg set $x}
3764    set expgui(convlbl) [format %5.2f [expr {pow(10,$x/100.)}]]
3765}
3766proc SetMarq {x} {
3767    global expgui
3768    if {$x != $expgui(marqinit) && $expgui(changed) <= 0} {
3769        incr expgui(changed)
3770    }
3771    if {$expgui(changed)} {expinfo marq set $x}
3772}
3773# ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ END OF LS PANE CODE ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
3774#-------------------------------------------------------------------------
3775#-------------------------------------------------------------------------
3776#vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv THE MENU BAR vvvvvvvvvvvvvvvvvvvvvv
3777
3778#---- file menu button
3779menubutton $expgui(fm).file -text File -menu $expgui(fm).file.menu
3780menu $expgui(fm).file.menu
3781if $expgui(debug) {
3782    $expgui(fm).file.menu add command -label "Reset" -command "reset"
3783}
3784if {$expgui(shell)} {
3785    $expgui(fm).file.menu add command -label "Open" -command readnewexp
3786    $expgui(fm).file.menu add command -label "expnam" -command readnewexp
3787}
3788$expgui(fm).file.menu add command -label "Save" -underline 0 \
3789        -command savearchiveexp
3790foreach c {s S} {bind . <Alt-$c> [list savearchiveexp]}
3791$expgui(fm).file.menu add command -label "Save As" \
3792        -command "SaveAsFile"
3793$expgui(fm).file.menu add command -label "Reread .EXP file" \
3794        -command {rereadexp $expgui(expfile)}
3795
3796#---- help menu button
3797menubutton $expgui(fm).help -text Help -menu $expgui(fm).help.menu
3798menu $expgui(fm).help.menu
3799$expgui(fm).help.menu add command -command showhelp -underline 0 \
3800        -label "Help Summary"
3801$expgui(fm).help.menu add command -command MakeWWWHelp  \
3802        -label "Help on current pane"
3803$expgui(fm).help.menu add command -command "MakeWWWHelp menu" \
3804        -label "Help on menu"
3805foreach c {h H} {bind . <Alt-$c> [list showhelp]}
3806# define help actions
3807bind . <Key-F1> MakeWWWHelp
3808$expgui(fm).help.menu add command -label "About..." -command About
3809$expgui(fm).help.menu add command -label "Cite..." -command Cite
3810
3811#---- options menu button
3812menubutton $expgui(fm).option -text Options \
3813        -menu $expgui(fm).option.menu
3814menu $expgui(fm).option.menu
3815
3816if {$expgui(shell)} {
3817    $expgui(fm).option.menu add checkbutton  -label "Archive EXP" \
3818            -variable expgui(archive)
3819    $expgui(fm).option.menu add checkbutton  -label "Use DISAGL window" \
3820            -variable expgui(disaglSeparateBox)
3821    $expgui(fm).option.menu  add checkbutton -label "Autoload EXP" \
3822            -variable expgui(autoexpload)
3823    $expgui(fm).option.menu  add checkbutton -label "Iconify during GSAS" \
3824            -variable expgui(autoiconify)
3825}
3826$expgui(fm).option.menu add cascade -menu  $expgui(fm).option.menu.asort \
3827        -label "Sort atoms by"
3828
3829set expgui(asorttype) number
3830menu $expgui(fm).option.menu.asort
3831foreach opt {number type mult x y z occupancy} {
3832    $expgui(fm).option.menu.asort add radiobutton -command {DisplayAllAtoms $expgui(curPhase)}\
3833            -label $opt -value $opt -variable expgui(asorttype)
3834}
3835
3836$expgui(fm).option.menu add cascade -menu  $expgui(fm).option.menu.hsort \
3837        -label "Sort histograms by"
3838
3839set expgui(hsorttype) number
3840menu $expgui(fm).option.menu.hsort
3841$expgui(fm).option.menu.hsort add radiobutton -command sethistlist \
3842        -label number -value number -variable expgui(hsorttype)
3843$expgui(fm).option.menu.hsort add radiobutton -command sethistlist \
3844        -label "Histogram type" -value type -variable expgui(hsorttype)
3845$expgui(fm).option.menu.hsort add radiobutton -command sethistlist \
3846        -label "Bank #" -value bank -variable expgui(hsorttype)
3847$expgui(fm).option.menu.hsort add radiobutton -command sethistlist \
3848        -label "Angle/Wavelength" -value angle -variable expgui(hsorttype)
3849
3850#---- Global mode menu button
3851$expgui(fm).option.menu add cascade -menu $expgui(fm).option.menu.editmode \
3852        -label "Multiple hist. selection"
3853menu $expgui(fm).option.menu.editmode
3854$expgui(fm).option.menu.editmode add radiobutton  -label "Off" \
3855        -variable expgui(globalmode) -value 0 \
3856        -command sethistlist
3857$expgui(fm).option.menu.editmode add radiobutton  -label "All" \
3858        -variable expgui(globalmode) -value 6 \
3859        -command sethistlist
3860$expgui(fm).option.menu.editmode add radiobutton  -label "TOF" \
3861        -variable expgui(globalmode) -value 1 \
3862        -command sethistlist
3863$expgui(fm).option.menu.editmode add radiobutton  -label "CW Neutron" \
3864        -variable expgui(globalmode) -value 2  \
3865        -command sethistlist
3866$expgui(fm).option.menu.editmode add radiobutton  -label "Alpha12 Xray" \
3867        -variable expgui(globalmode) -value 3 \
3868        -command sethistlist
3869$expgui(fm).option.menu.editmode add radiobutton  -label "Monochromatic Xray" \
3870        -variable expgui(globalmode) -value 4 \
3871        -command sethistlist
3872$expgui(fm).option.menu.editmode add radiobutton  -label "Energy Disp Xray" \
3873        -variable expgui(globalmode) -value 5 \
3874        -command sethistlist
3875$expgui(fm).option.menu.editmode add separator
3876$expgui(fm).option.menu.editmode add checkbutton \
3877        -label "Group phases together" \
3878        -variable expgui(globalphasemode) \
3879        -command sethistlist
3880
3881set expgui(globalmode) 0
3882set expgui(globalphasemode) 1
3883
3884if {$tcl_platform(platform) == "unix"} {
3885    $expgui(fm).option.menu  add checkbutton -label "Override backspace" \
3886            -variable env(GSASBACKSPACE)
3887}
3888
3889$expgui(fm).option.menu add cascade -menu  $expgui(fm).option.menu.font \
3890        -label "Screen font"
3891menu $expgui(fm).option.menu.font
3892foreach f {10 11 12 13 14 16 18 20 22} {
3893    $expgui(fm).option.menu.font add radiobutton \
3894            -command {SetTkDefaultOptions $expgui(font); ResizeFont .; ResizeNotebook} \
3895        -label $f -value $f -variable expgui(font) -font "Helvetica -$f"
3896}
3897
3898$expgui(fm).option.menu  add checkbutton -label "Show EXPTOOL output" \
3899        -variable expgui(showexptool)
3900$expgui(fm).option.menu add command -label "Save Options" \
3901        -command "SaveOptions"
3902
3903pack $expgui(fm).file $expgui(fm).option -side left  -in $expgui(fm)
3904
3905if {$expgui(shell)} {
3906    foreach menu $expgui(menunames) {
3907        set m [string tolower $menu]
3908        pack [menubutton $expgui(fm).$m -text $menu \
3909                -menu $expgui(fm).$m.menu] -side left
3910        menu $expgui(fm).$m.menu
3911    }
3912}
3913pack $expgui(fm).help  -side right -in $expgui(fm)
3914
3915if {$expgui(shell)} {
3916    # add an export command to the last menu that gets filled in later
3917    $expgui(fm).$m.menu add  cascade -label "Coord Export" \
3918            -menu $expgui(fm).$m.menu.coordexp
3919    menu $expgui(fm).$m.menu.coordexp \
3920            -postcommand "BuildCoordExpMenu $expgui(fm).$m.menu.coordexp"
3921    $expgui(fm).$m.menu.coordexp add command -label "Building menu" \
3922            -state disabled
3923    $expgui(fm).$m.menu.coordexp add command -label "Please wait..." \
3924            -state disabled
3925
3926    $expgui(fm).$m.menu add  cascade -label "CIF Export" \
3927            -menu $expgui(fm).$m.menu.cifexp
3928    menu $expgui(fm).$m.menu.cifexp
3929    $expgui(fm).$m.menu.cifexp add command -label gsas2cif \
3930            -command "runGSASwEXP gsas2cif"
3931    $expgui(fm).$m.menu.cifexp add command -label FillTemplate \
3932            -command "exec $wishshell [file join $expgui(scriptdir) fillcif.tcl] \[file root \[file tail \$expgui(expfile)]]"
3933    $expgui(fm).$m.menu.cifexp add command -label CIFselect \
3934            -command {
3935        if {[info procs CIFselect] == ""} {
3936            source [file join $expgui(scriptdir) cifselect.tcl]
3937        }
3938        CIFselect $expgui(expfile)
3939    }
3940    # add the commands in expgui_menulist
3941    foreach menu [array names expgui_menulist ] {
3942        foreach cmd $expgui_menulist($menu) {
3943            set action {}
3944            set opt {}
3945            catch {set action [lindex $expgui_cmdlist($cmd) 0]}
3946            catch {set opt [lindex $expgui_cmdlist($cmd) 2]}
3947            if {$expgui(debug) && $action == ""} {puts "blank command for $cmd"}
3948            if {$action != "" && $action != "-"} {
3949                eval $expgui(fm).$menu.menu add command \
3950                        -label $cmd $opt -command [list [subst $action]]
3951                if {[lindex $opt 0] == "-underline"} {
3952                    catch {
3953                        set num [lindex $opt 1]
3954                        set key [string range $cmd $num $num]
3955                        bind . <Alt-[string tolower $key]> [subst $action]
3956                        bind . <Alt-[string toupper $key]> [subst $action]
3957                    }
3958                }
3959            }
3960        }
3961    }
3962}
3963# setup command help
3964foreach cmd [array names expgui_cmdlist] {
3965    set help {}
3966    catch {set help [lindex $expgui_cmdlist($cmd) 1]}
3967    if {$help == ""} {
3968        if {$expgui(debug)} {puts "no help for $cmd"}
3969    } else {
3970        # remove
3971        regsub -all \x09 $help " " help
3972        # preserve blank lines
3973        regsub -all \x0A\x0A $help "AAA1234567890AAA" help
3974        regsub -all \x0A $help " " help
3975        regsub -all "AAA1234567890AAA" $help \x0A\x0A help
3976        regsub -all " +" $help " " help
3977        set expgui_helplist($cmd) [string trim $help]
3978    }
3979}
3980if {$expgui(shell)} {
3981    # set up button bar
3982    foreach cmd $expgui(buttonlist) {
3983        set action {}
3984        catch {set action [lindex $expgui_cmdlist($cmd) 0]}
3985        if {$expgui(debug) && $action == ""} {puts "blank command for $cmd"}
3986        if {$action != ""} {
3987            pack [eval button .bar.$cmd -bg beige -activebackground yellow \
3988                    -padx 2m -pady 0 \
3989                    -text $cmd -command [list [subst $action]]] -side left
3990        }
3991    }
3992}
3993
3994$expgui(fm).file.menu add command -label "Exit"  -underline 1 -command catchQuit
3995foreach c {X x} {bind . <Alt-$c> [list catchQuit]}
3996#^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ END OF MENU DEFINITION ^^^^^^^^^^^^^^^^^^^
3997
3998# make the phase pane -- this must be done before setphases
3999# can be called (in loadexp)
4000MakePhasePane
4001# and the rest of the windows w/o BWidget
4002if {!$expgui(haveBW)} {
4003    MakeHistPane
4004    MakeScalingPane
4005    MakeProfilePane
4006}
4007
4008# handle indirect exits
4009wm protocol . WM_DELETE_WINDOW catchQuit
4010bind . <Control-c> catchQuit
4011
4012set expgui(pagenow) ""
4013set expgui(curhist) {}
4014set expgui(selectedatomlist) {}
4015
4016loadexp $expgui(expfile)
4017
4018# reset the phase selection
4019set expgui(curPhase) {}
4020# select the first histogram in the list by default (if there are any)
4021if {[llength $expmap(histlistboxcontents)] > 0} {
4022    set expgui(curhist) 0
4023} else {
4024    set expgui(curhist) {}
4025}
4026
4027# execute any local commands for final initialization
4028eval $expgui(initstring)
4029
4030# resize the notebook to fit all the tabs and the largest page
4031ResizeNotebook
4032
4033RaisePage lsFrame
4034if {[CountHistory] > 100} {
4035    DeleteHistoryRecords "This .EXP file has [CountHistory] history records\nErasing most will speed EXPGUI"
4036}
Note: See TracBrowser for help on using the repository browser.