source: trunk/expgui @ 757

Last change on this file since 757 was 757, checked in by toby, 14 years ago

# on 2003/11/22 00:50:56, toby did:
advance EXPGUI version number to reflect bug fixes in other files

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