source: trunk/expgui @ 457

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

# on 2001/10/14 05:34:23, toby did:
bug fix: fix so that new histogram buttons are disabled when
no phases are present

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