source: trunk/expgui @ 466

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

# on 2001/10/18 23:22:57, toby did:
update archiving

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