source: trunk/expgui @ 631

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

# on 2002/07/18 20:50:45, toby did:
GSAS2CIF & fillCIF changes

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