source: trunk/expgui @ 645

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

# on 2002/09/05 18:22:20, toby did:
trap error in localconfig and .gsas_config
add CIFselect

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