source: trunk/expgui @ 809

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

# on 2004/09/28 22:31:01, toby did:
use winutils or winexec
catch expgui when called with .Oxx (archived .EXP) files

to eventually offer rename choices

catch Mac Reload EXP? bug

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