source: trunk/expgui @ 557

Last change on this file since 557 was 557, checked in by toby, 13 years ago

# on 2002/01/25 21:26:41, toby did:
Add absorption correction to histogram pane
move damping on histogram pane, to save space
bug fix: global ref. w/alpha1,2 the ratio param were in wrong spot

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