source: trunk/expgui @ 419

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

# on 2001/09/04 22:08:10, toby did:
adjustable font support

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