source: trunk/expgui @ 796

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

# on 2004/05/13 23:48:09, toby did:
add open console

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