source: trunk/expgui @ 449

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

# on 2001/09/25 23:30:11, toby did:
Add citations, as suggested by Lachlan
Add Set Histogram flag button

  • Property rcs:author set to toby
  • Property rcs:date set to 2001/09/25 23:30:11
  • Property rcs:lines set to +36 -17
  • Property rcs:rev set to 1.47
  • 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 449 2009-12-04 23:06:21Z toby $
3set expgui(Revision) {$Revision: 449 $ $Date: 2009-12-04 23:06:21 +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        if {[llength $expmap(phaselist)] == 0} {
1426            $expgui(histFrame).newh configure -state disabled
1427        } else {
1428            $expgui(histFrame).newh configure -state normal
1429        }
1430    }
1431
1432    # display the selected histograms
1433    $expgui(histFrame).hs.lbox selection clear 0 end
1434    foreach h $expgui(curhist) {
1435        $expgui(histFrame).hs.lbox selection set $h
1436    }
1437
1438    # disable traces on entryvar for right now
1439    set entrycmd(trace) 0
1440
1441    # get histogram list
1442    set histlist {}
1443    foreach item $expgui(curhist) {
1444        lappend histlist [lindex $expmap(powderlist) $item]
1445    }
1446    # must have at least one histogram selected here
1447    if {[llength $histlist] == 0} {
1448        set expgui(backtermlbl) ""
1449        set expgui(backtypelbl) ""
1450        foreach var {bref bdamp} {
1451            set entrycmd($var) ""
1452            set entryvar($var) ""
1453        }
1454        $expgui(histFrame).top.txt config -text "No Selected Histograms"
1455        grid $expgui(histFrame).top -column 1 -row 0 -sticky nsew       
1456        set expgui(bkglbl) ""
1457        eval destroy [winfo children $expgui(diffBox)]
1458        set entrycmd(trace) 1
1459        return
1460    }
1461
1462    if {$expgui(globalmode) != 0} {
1463        set expgui(backtermlbl) ""
1464        set expgui(backtypelbl) ""
1465        foreach var {bref bdamp} {
1466            set entrycmd($var) "histinfo [list $histlist] $var"
1467            set entryvar($var) [histinfo [lindex $histlist 0] $var]
1468        }
1469    } else {
1470        set hist $histlist
1471        set terms [histinfo $hist backterms]
1472        set expgui(backtermlbl) "($terms terms)"
1473        set expgui(backtypelbl) "Function type [histinfo $hist backtype]"
1474        foreach var {bref bdamp} {
1475            set entrycmd($var) "histinfo $hist $var"
1476            set entryvar($var) [eval $entrycmd($var)]
1477        }
1478    }
1479    # Top box
1480    if $expgui(haveBW) {
1481        catch {destroy $expgui(histFrame).pflag}
1482    }
1483    if {$expgui(globalmode) != 0} {
1484        $expgui(histFrame).top.txt config \
1485                -text "Selected Histograms: [CompressList $histlist]"
1486        grid $expgui(histFrame).top -column 1 -row 0 -sticky nsew       
1487        set expgui(bkglbl) "Globally Edit Background"
1488    } else {
1489        grid forget $expgui(histFrame).top
1490        set expgui(bkglbl) "Edit Background"
1491        if {$expgui(haveBW) && [llength $expmap(phaselist)] > 1} {
1492            TitleFrame $expgui(histFrame).pflag  \
1493                    -borderwidth 4 -side left -relief groove \
1494                    -text "Phase Flags"
1495            set expgui(pflag) [$expgui(histFrame).pflag getframe]
1496            grid $expgui(histFrame).pflag -column 1 -row 1 -sticky nsew
1497            grid rowconfigure $expgui(histFrame) 2 -minsize 35
1498            foreach p $expmap(phaselist) {
1499                pack [checkbutton $expgui(pflag).$p \
1500                        -command "GetPhaseFlags $hist" \
1501                        -variable expgui(pflag$p) -text $p] -side left
1502                if {[lsearch $expmap(phaselist_$hist) $p] == -1} {
1503                    set expgui(pflag$p) 0
1504                } else {
1505                    set expgui(pflag$p) 1
1506                }
1507            }
1508        }
1509    }
1510
1511    # diffractometer constants
1512    foreach var {lam1 lam2 kratio pola ipola ddamp zero \
1513            wref pref dcref daref ratref ttref zref } {
1514        set entrycmd($var) "histinfo [list $histlist] $var"
1515        set entryvar($var) [histinfo [lindex $histlist 0] $var]
1516    }
1517
1518    eval destroy [winfo children $expgui(diffBox)]
1519    if {$expgui(globalmode) == 0} {
1520        if {[string range $expmap(htype_$hist) 2 2] == "T"} {
1521        #------
1522        # TOF |
1523        #------
1524            grid [ label $expgui(diffBox).lDCrc -text "Refine DIFC" ] \
1525                    -column 1 -row 1
1526            grid [ checkbutton $expgui(diffBox).rfDCrc -variable entryvar(dcref) ] \
1527                    -column 2 -row 1
1528            grid [ label $expgui(diffBox).lDCdifc -text DIFC ] \
1529                    -column 3 -row 1 -sticky w
1530            grid [ entry $expgui(diffBox).eDCdifc -textvariable entryvar(lam1) \
1531                    -width 15 ] -column 4 -row 1
1532            set entrybox(lam1) $expgui(diffBox).eDCdifc
1533            #
1534            grid [ label $expgui(diffBox).lDCra -text "Refine DIFA" ] \
1535                    -column 1 -row 2
1536            grid [ checkbutton $expgui(diffBox).rfDCra -variable entryvar(daref) ] \
1537                    -column 2 -row 2
1538            grid [ label $expgui(diffBox).lDCdifa -text DIFA ] \
1539                    -column 3 -row 2
1540            grid [ entry $expgui(diffBox).eDCdifa -textvariable entryvar(lam2) \
1541                    -width 15 ] -column 4 -row 2
1542            set entrybox(lam2) $expgui(diffBox).eDCdifa
1543            #
1544            grid [ label $expgui(diffBox).lDCzero -text "Zero"] \
1545                    -column 3 -row 3
1546            grid [ entry $expgui(diffBox).eDCzero -textvariable entryvar(zero) \
1547                    -width 15 ] -column 4 -row 3
1548            set entrybox(zero) $expgui(diffBox).eDCzero
1549            grid [ label $expgui(diffBox).lDCzref -text "Refine zero" ] \
1550                    -column 1 -row 3 -sticky w
1551            grid [ checkbutton $expgui(diffBox).rfDCzref \
1552                    -variable entryvar(zref) ] -column 2 -row 3
1553        } elseif {[string range $expmap(htype_$hist) 1 2] == "NC"} {
1554        #---------------
1555        # CW - neutron |
1556        #---------------
1557            grid [ label $expgui(diffBox).lDC1 -text "Refine wave" ] \
1558                    -column 1 -row 1
1559            grid [ checkbutton $expgui(diffBox).rfDC1 -variable entryvar(wref) ] \
1560                    -column 2 -row 1
1561            grid [ label $expgui(diffBox).lDCdifc -text wave ] \
1562                    -column 3 -row 1 -sticky w
1563            grid [ entry $expgui(diffBox).eDCdifc -textvariable entryvar(lam1) \
1564                    -width 15 ] -column 4 -row 1
1565            set entrybox(lam1) $expgui(diffBox).eDCdifc
1566            #
1567            grid [ label $expgui(diffBox).lDCzref -text "Refine zero" ] \
1568                    -column 1 -row 3 -sticky w
1569            grid [ checkbutton $expgui(diffBox).rfDCzref \
1570                    -variable entryvar(zref) ] -column 2 -row 3
1571            grid [ label $expgui(diffBox).lDCzero -text "Zero"] \
1572                    -column 3 -row 3
1573            grid [ entry $expgui(diffBox).eDCzero -textvariable entryvar(zero) \
1574                    -width 15 ] -column 4 -row 3
1575            set entrybox(zero) $expgui(diffBox).eDCzero
1576        } elseif {[string range $expmap(htype_$hist) 1 2] == "XC" && \
1577                [histinfo $hist lam2] == 0.0} {
1578        #--------------------------
1579        # CW - x-ray 1 wavelength |
1580        #--------------------------
1581            grid [ label $expgui(diffBox).lDC1 -text "Refine wave" ] \
1582                    -column 1 -row 1
1583            grid [ checkbutton $expgui(diffBox).rfDC1 -variable entryvar(wref) ] \
1584                    -column 2 -row 1
1585            grid [ label $expgui(diffBox).lDCdifc -text wave ] \
1586                    -column 3 -row 1 -sticky w
1587            grid [ entry $expgui(diffBox).eDCdifc -textvariable entryvar(lam1) \
1588                    -width 15 ] -column 4 -row 1
1589            set entrybox(lam1) $expgui(diffBox).eDCdifc
1590            #
1591            grid [ label $expgui(diffBox).lDCzref -text "Refine zero" ] \
1592                    -column 1 -row 3 -sticky w
1593            grid [ checkbutton $expgui(diffBox).rfDCzref \
1594                    -variable entryvar(zref) ] -column 2 -row 3
1595            grid [ label $expgui(diffBox).lDCzero -text "Zero"] \
1596                    -column 3 -row 3
1597            grid [ entry $expgui(diffBox).eDCzero -textvariable entryvar(zero) \
1598                    -width 15 ] -column 4 -row 3
1599            set entrybox(zero) $expgui(diffBox).eDCzero
1600            #
1601            grid [ label $expgui(diffBox).lDCpref -text "Refine POLA" ] \
1602                    -column 1 -row 4 -sticky w
1603            grid [ checkbutton $expgui(diffBox).rfDCpref \
1604                    -variable entryvar(pref) ] -column 2 -row 4
1605            grid [ label $expgui(diffBox).lDCpola -text POLA ] \
1606                    -column 3 -row 4
1607            grid [ entry $expgui(diffBox).eDCpola \
1608                    -textvariable entryvar(pola) -width 15 ] -column 4 -row 4
1609            set entrybox(pola) $expgui(diffBox).eDCpola
1610            grid [ label $expgui(diffBox).lDCipola -text "IPOLA" ] \
1611                    -column 5 -row 4
1612            grid [ entry $expgui(diffBox).eDCipola -width 2 \
1613                    -textvariable entryvar(ipola)] -column 6 -row 4
1614            set entrybox(ipola) $expgui(diffBox).eDCipola
1615        } elseif {[string range $expmap(htype_$hist) 1 2] == "XC"} {
1616        #---------------------------
1617        # CW - x-ray 2 wavelengths |
1618        #---------------------------
1619            grid [ label $expgui(diffBox).lDCdifc -text wavelengths ] \
1620                    -column 3 -row 1 -sticky w
1621            grid [ entry $expgui(diffBox).eDCdifc -textvariable entryvar(lam1) \
1622                    -width 15 ] -column 4 -row 1
1623            set entrybox(lam1) $expgui(diffBox).eDCdifc
1624            grid [ entry $expgui(diffBox).eDCdifa -textvariable entryvar(lam2) \
1625                    -width 15 ] -column 5 -row 1
1626            set entrybox(lam2) $expgui(diffBox).eDCdifa
1627            #
1628            grid [ label $expgui(diffBox).lDCrref -text "Refine ratio" ] \
1629                    -column 1 -row 2 -sticky w
1630            grid [ checkbutton $expgui(diffBox).rfDCrref \
1631                    -variable entryvar(ratref) ] -column 2 -row 2
1632            grid [ label $expgui(diffBox).lDCratio -text Ratio ] \
1633                    -column 3 -row 2
1634            grid [ entry $expgui(diffBox).eDCkratio \
1635                    -textvariable entryvar(kratio) \
1636                    -width 15 ] -column 4 -row 2
1637            set entrybox(kratio) $expgui(diffBox).eDCkratio
1638            #
1639            grid [ label $expgui(diffBox).lDCzero -text "Zero"] \
1640                    -column 3 -row 3
1641            grid [ entry $expgui(diffBox).eDCzero -textvariable entryvar(zero) \
1642                    -width 15 ] -column 4 -row 3
1643            grid [ label $expgui(diffBox).lDCzref -text "Refine zero" ] \
1644                    -column 1 -row 3 -sticky w
1645            set entrybox(zero) $expgui(diffBox).eDCzero
1646            grid [ checkbutton $expgui(diffBox).rfDCzref \
1647                    -variable entryvar(zref) ] -column 2 -row 3
1648            grid [ label $expgui(diffBox).lDCpref -text "Refine POLA" ] \
1649                    -column 1 -row 4 -sticky w
1650            grid [ checkbutton $expgui(diffBox).rfDCpref \
1651                    -variable entryvar(pref) ] -column 2 -row 4
1652            grid [ label $expgui(diffBox).lDCpola -text POLA ] \
1653                    -column 3 -row 4
1654            grid [ entry $expgui(diffBox).eDCpola \
1655                    -textvariable entryvar(pola) -width 15 ] -column 4 -row 4
1656            set entrybox(pola) $expgui(diffBox).eDCpola
1657            grid [ label $expgui(diffBox).lDCipola -text "IPOLA" ] \
1658                    -column 5 -row 4
1659            grid [ entry $expgui(diffBox).eDCipola -width 2 \
1660                    -textvariable entryvar(ipola)] -column 6 -row 4
1661            set entrybox(ipola) $expgui(diffBox).eDCipola
1662        } elseif {[string range $expmap(htype_$hist) 1 2] == "XE"} {
1663        #-------------
1664        # ED - x-ray |
1665        #-------------
1666            grid [ label $expgui(diffBox).lDC1 -text "Refine 2theta" ] \
1667                    -column 1 -row 1
1668            grid [ checkbutton $expgui(diffBox).rfDC1 -variable entryvar(ttref) ] \
1669                    -column 2 -row 1
1670            grid [ label $expgui(diffBox).lDCdifc -text 2Theta ] \
1671                    -column 3 -row 1 -sticky w
1672            grid [ entry $expgui(diffBox).eDCdifc -textvariable entryvar(lam1) \
1673                    -width 15 ] -column 4 -row 1
1674            set entrybox(lam1) $expgui(diffBox).eDCdifc
1675            #
1676            grid [ label $expgui(diffBox).lDCpref -text "Refine POLA" ] \
1677                    -column 1 -row 4 -sticky w
1678            grid [ checkbutton $expgui(diffBox).rfDCpref \
1679                    -variable entryvar(pref) ] -column 2 -row 4
1680            grid [ label $expgui(diffBox).lDCpola -text POLA ] \
1681                    -column 3 -row 4
1682            grid [ entry $expgui(diffBox).eDCpola \
1683                    -textvariable entryvar(pola) -width 15 ] -column 4 -row 4
1684            set entrybox(pola) $expgui(diffBox).eDCpola
1685            grid [ label $expgui(diffBox).lDCipola -text "IPOLA" ] \
1686                    -column 5 -row 4
1687            grid [ entry $expgui(diffBox).eDCipola -width 2 \
1688                    -textvariable entryvar(ipola)] -column 6 -row 4
1689            set entrybox(ipola) $expgui(diffBox).eDCipola
1690        }
1691    } elseif {$expgui(globalmode) == 1} {
1692        #-------------
1693        # Global TOF |
1694        #-------------
1695        grid [ label $expgui(diffBox).lDCrc -text "Refine DIFC" ] \
1696                -column 1 -row 1
1697        grid [ checkbutton $expgui(diffBox).rfDCrc -variable entryvar(dcref) ] \
1698                -column 2 -row 1
1699        grid [button $expgui(diffBox).bDCdifc -text "Set DIFC Globally" \
1700                -command "editglobalparm histinfo difc {DIFC}"] -column 3 -row 1
1701        #
1702        grid [ label $expgui(diffBox).lDCra -text "Refine DIFA" ] \
1703                -column 1 -row 2
1704        grid [ checkbutton $expgui(diffBox).rfDCra -variable entryvar(daref) ] \
1705                -column 2 -row 2
1706        grid [ button $expgui(diffBox).bDCdifa -text "Set DIFA Globally" \
1707                -command "editglobalparm histinfo difa {DIFA}"] -column 3 -row 2
1708        #
1709        grid [ label $expgui(diffBox).lDCzref -text "Refine zero" ] \
1710                -column 1 -row 3 -sticky w
1711        grid [ checkbutton $expgui(diffBox).rfDCzref \
1712                -variable entryvar(zref) ] -column 2 -row 3
1713        grid [ button $expgui(diffBox).bDCzero -text "Set ZERO Globally" \
1714                -command "editglobalparm histinfo zero {Zero}"] -column 3 -row 3
1715    } elseif {$expgui(globalmode) == 2} {
1716        #--------------------
1717        # Global CW neutron |
1718        #--------------------
1719        grid [ label $expgui(diffBox).lDC1 -text "Refine wave" ] \
1720                -column 1 -row 1
1721        grid [ checkbutton $expgui(diffBox).rfDC1 -variable entryvar(wref) ] \
1722                -column 2 -row 1
1723        grid [button $expgui(diffBox).bDCdifc -text "Set Wave Globally" \
1724                -command "editglobalparm histinfo lam1 Wavelength"] \
1725                -column 3 -row 1
1726        #
1727        grid [ label $expgui(diffBox).lDCzref -text "Refine zero" ] \
1728                -column 1 -row 3 -sticky w
1729        grid [ checkbutton $expgui(diffBox).rfDCzref \
1730                -variable entryvar(zref) ] -column 2 -row 3
1731        grid [button $expgui(diffBox).bDCzero -text "Set Zero Globally" \
1732                -command "editglobalparm histinfo zero Zero"] -column 3 -row 3
1733    } elseif {$expgui(globalmode) == 4} {
1734        #----------------------
1735        # Global CW mono xray |
1736        #----------------------
1737        grid [ label $expgui(diffBox).lDC1 -text "Refine wave" ] \
1738                -column 1 -row 1
1739        grid [ checkbutton $expgui(diffBox).rfDC1 -variable entryvar(wref) ] \
1740                -column 2 -row 1
1741        grid [button $expgui(diffBox).bDCdifc -text "Set Wave Globally" \
1742                -command "editglobalparm histinfo lam1 Wavelength"] \
1743                -column 3 -row 1
1744        #
1745        grid [ label $expgui(diffBox).lDCzref -text "Refine zero" ] \
1746                -column 1 -row 3 -sticky w
1747        grid [ checkbutton $expgui(diffBox).rfDCzref \
1748                -variable entryvar(zref) ] -column 2 -row 3
1749        grid [button $expgui(diffBox).bDCzero -text "Set Zero Globally" \
1750                -command "editglobalparm histinfo zero Zero"] -column 3 -row 3
1751        #
1752        grid [ label $expgui(diffBox).lDCpref -text "Refine POLA" ] \
1753                -column 1 -row 4 -sticky w
1754        grid [ checkbutton $expgui(diffBox).rfDCpref \
1755                -variable entryvar(pref) ] -column 2 -row 4
1756        grid [button $expgui(diffBox).bDCpola -text "Set POLA Globally" \
1757                -command "editglobalparm histinfo pola POLA"] -column 3 -row 4
1758        grid [button $expgui(diffBox).bDCipola -text "Set IPOLA Globally" \
1759                -command "editglobalparm histinfo ipola IPOLA"] -column 4 -row 4
1760    } elseif {$expgui(globalmode) == 3} {
1761        #------------------------
1762        # Global alpha 1,2 xray |
1763        #------------------------
1764        grid [button $expgui(diffBox).bDCl1 -text "Set Wave1 Globally" \
1765                -command "editglobalparm histinfo lam1 {Wavelength 1}"] \
1766                -column 3 -row 1
1767        grid [button $expgui(diffBox).bDCl2 -text "Set Wave2 Globally" \
1768                -command "editglobalparm histinfo lam2 {Wavelength 2}"] \
1769                -column 4 -row 1
1770        #
1771        grid [ label $expgui(diffBox).lDCratref -text "Refine Ratio" ] \
1772                -column 1 -row 3 -sticky w
1773        grid [ checkbutton $expgui(diffBox).rfDCratref \
1774                -variable entryvar(ratref) ] -column 2 -row 3
1775        grid [button $expgui(diffBox).bDCrrat -text "Set Ratio Globally" \
1776                -command "editglobalparm histinfo ratio {Wavelength Ratio}"] \
1777                -column 3 -row 3
1778        #
1779        grid [ label $expgui(diffBox).lDCzref -text "Refine zero" ] \
1780                -column 1 -row 3 -sticky w
1781        grid [ checkbutton $expgui(diffBox).rfDCzref \
1782                -variable entryvar(zref) ] -column 2 -row 3
1783        grid [button $expgui(diffBox).bDCzero -text "Set Zero Globally" \
1784                -command "editglobalparm histinfo zero Zero"] -column 3 -row 3
1785        #
1786        grid [ label $expgui(diffBox).lDCpref -text "Refine POLA" ] \
1787                -column 1 -row 4 -sticky w
1788        grid [ checkbutton $expgui(diffBox).rfDCpref \
1789                -variable entryvar(pref) ] -column 2 -row 4
1790        grid [button $expgui(diffBox).bDCpola -text "Set POLA Globally" \
1791                -command "editglobalparm histinfo pola POLA"] -column 3 -row 4
1792        grid [button $expgui(diffBox).bDCipola -text "Set IPOLA Globally" \
1793                -command "editglobalparm histinfo ipola IPOLA"] -column 4 -row 4
1794    } elseif {$expgui(globalmode) == 5} {
1795        #-----------------
1796        # Global ED xray |
1797        #-----------------
1798        grid [ label $expgui(diffBox).lDC1 -text "Refine 2theta" ] \
1799                -column 1 -row 1
1800        grid [ checkbutton $expgui(diffBox).rfDC1 -variable entryvar(ttref) ] \
1801                -column 2 -row 1
1802        grid [button $expgui(diffBox).bDCdifc -text "Set 2Theta Globally" \
1803                -command "editglobalparm histinfo ratio {Fixed 2Theta}"] \
1804                -column 3 -row 1
1805        #
1806        grid [ label $expgui(diffBox).lDCpref -text "Refine POLA" ] \
1807                -column 1 -row 4 -sticky w
1808        grid [ checkbutton $expgui(diffBox).rfDCpref \
1809                -variable entryvar(pref) ] -column 2 -row 4
1810        grid [button $expgui(diffBox).bDCpola -text "Set POLA Globally" \
1811                -command "editglobalparm histinfo pola POLA"] -column 3 -row 4
1812        grid [button $expgui(diffBox).bDCipola -text "Set IPOLA Globally" \
1813                -command "editglobalparm histinfo ipola IPOLA"] -column 4 -row 4
1814    }
1815    if {$expgui(globalmode) == 0} {
1816        grid [frame $expgui(diffBox).d] -column 5 -row 5 \
1817                -columnspan 2 -sticky e
1818    } else {
1819        grid [frame $expgui(diffBox).d] -column 4 -row 5 \
1820                -columnspan 2 -sticky e
1821    }
1822    grid [label $expgui(diffBox).d.lDamp -text "Damping  "] \
1823            -column 1 -row 1
1824    tk_optionMenu $expgui(diffBox).d.om entryvar(ddamp) 0 1 2 3 4 5 6 7 8 9
1825    grid $expgui(diffBox).d.om -column 2 -row 1
1826    grid columnconfigure $expgui(diffBox) 9  -weight 1
1827    grid columnconfigure $expgui(diffBox) 0  -weight 1
1828    update idletasks
1829    # enable traces on entryvar now
1830    set entrycmd(trace) 1
1831}
1832
1833# this gets the phase flags as set in the expgui(pflag*) elements
1834# (linked to phase flag checkbuttons) and the sets the "HST xx NPHAS" flags
1835# accordingly using SetPhaseFlag
1836proc GetPhaseFlags {hist} {
1837    global expmap expgui
1838    set plist {}
1839    foreach p $expmap(phaselist) {
1840        if {$expgui(pflag$p)} {lappend plist $p}
1841    }
1842    SetPhaseFlag $hist $plist
1843    incr expgui(changed)
1844    mapexp
1845}
1846
1847#-----------------------------------------------------------------------
1848# populate the Scaling page
1849#-----------------------------------------------------------------------
1850proc DisplayFrac {} {
1851    global expgui entrycmd entryvar entrybox expmap
1852
1853    # trap if more than one histogram is selected unless global mode
1854    if {$expgui(globalmode) == 0 && [llength $expgui(curhist)] > 1} {
1855        set expgui(curhist) [lindex $expgui(curhist) 0]
1856    }
1857
1858    # display the selected histograms
1859    $expgui(fracFrame).hs.lbox selection clear 0 end
1860    foreach h $expgui(curhist) {
1861        $expgui(fracFrame).hs.lbox selection set $h
1862    }
1863
1864    # disable traces on entryvar
1865    set entrycmd(trace) 0
1866
1867    # get histogram list
1868    set histlist {}
1869    foreach item $expgui(curhist) {
1870        lappend histlist [lindex $expmap(powderlist) $item]
1871    }
1872
1873    # must have at least one histogram selected here
1874    if {[llength $histlist] == 0} {
1875        foreach var {scale sref sdamp} {
1876            set entrycmd($var) ""
1877            set entryvar($var) ""
1878        }
1879        set parm [grid info $expgui(scaleBox).but1]
1880        if {$parm != ""} {
1881            grid forget  $expgui(scaleBox).but1
1882            eval grid $expgui(scaleBox).ent1 $parm
1883        }
1884        # destroy the contents of the frame
1885        set phaseFractf1 $expgui(FracBox).f
1886        eval destroy [winfo children $phaseFractf1]
1887        # reenable traces on entryvar
1888        set entrycmd(trace) 1
1889        return
1890    }
1891
1892    #--------------
1893    # Scale factor
1894    #--------------
1895    if {$expgui(globalmode) != 0} {
1896        foreach var {scale sref sdamp} {
1897            set entrycmd($var) "histinfo [list $histlist] $var"
1898            set entryvar($var) [histinfo [lindex $histlist 0] $var]
1899        }
1900        # reset scale to black
1901        catch {$entrybox(scale) config -fg black}
1902        set parm [grid info $expgui(scaleBox).ent1]
1903        if {$parm != ""} {
1904            grid forget  $expgui(scaleBox).ent1
1905            eval grid $expgui(scaleBox).but1 $parm
1906        }
1907    } else {
1908        set hist $histlist
1909        foreach var {scale sref sdamp} {
1910            set entrycmd($var) "histinfo $hist $var"
1911            set entryvar($var) [eval $entrycmd($var)]
1912        }
1913        # reset scale to black
1914        catch {$entrybox(scale) config -fg black}
1915        set parm [grid info $expgui(scaleBox).but1]
1916        if {$parm != ""} {
1917            grid forget  $expgui(scaleBox).but1
1918            eval grid $expgui(scaleBox).ent1 $parm
1919        }
1920    }
1921
1922    #----------------
1923    # Phase Fractions
1924    #----------------
1925    set phaseFractf1 $expgui(FracBox).f
1926    # destroy the contents of the frame
1927    eval destroy [winfo children $phaseFractf1]
1928    if {$expgui(globalmode) != 0} {
1929        set txt "Phase Fractions for Histograms: [CompressList $histlist]"
1930    } else {
1931        set txt "Phase Fractions"
1932    }
1933    if $expgui(haveBW) {
1934        $expgui(fracFrame).f1.phaseFrac configure -text $txt
1935    } else {
1936        grid [label $phaseFractf1.txt -anchor center -text $txt] \
1937                -column 0 -row 0 -sticky news
1938    }
1939    # Create the frame inside the canvas, One frame for each Phase.
1940    foreach i {1 2 3 4 5 6 7 8 9} {set phasehistlist($i) ""}
1941    foreach hist $histlist {
1942        foreach i $expmap(phaselist_$hist) {
1943            lappend phasehistlist($i) $hist
1944        }
1945    }
1946    foreach i {1 2 3 4 5 6 7 8 9} {
1947        if {[llength $phasehistlist($i)] == 0} continue
1948        set framePF [frame $phaseFractf1.pF$i -relief groove  -bd 4]
1949        grid $framePF -column 0 -row $i -sticky ew
1950        # Label Heading for each phase.
1951        if {$expgui(globalmode) != 0} {
1952            grid [label $framePF.l1 \
1953                    -text "Phase $i Hist: [CompressList $phasehistlist($i)]"] \
1954                    -column 0 -row 0 -sticky nws
1955            grid [button $framePF.but1 -text "Set Globally" \
1956                    -command "editglobalparm hapinfo frac \"Phase $i Fraction\" \
1957                    [list $phasehistlist($i)] $i" \
1958                    ] -column 1 -row 0
1959        } else {
1960            grid [label $framePF.l1  -text "Phase $i"] \
1961                    -column 0 -row 0 -sticky nws
1962            grid [entry $framePF.ent -textvariable entryvar(frac$i) -width 15]\
1963                    -column 1 -row 0
1964            set entrybox(frac$i) $framePF.ent
1965        }
1966        set entrycmd(frac$i) "hapinfo $hist $i frac"
1967        set entryvar(frac$i) [hapinfo $hist $i frac]
1968        grid [label $framePF.l2  -text "  Refine"] \
1969                -column 2 -row 0 -sticky nws
1970        grid [checkbutton $framePF.cb -variable entryvar(frref$i)] \
1971                -column 3 -row 0 -sticky nws
1972        set entrycmd(frref$i) "hapinfo $hist $i frref"
1973        set entryvar(frref$i) [hapinfo $hist $i frref]
1974        grid [label $framePF.l3  -text "  Damping"] \
1975                -column 4 -row 0 -sticky nws
1976        tk_optionMenu $framePF.tkOptDamp entryvar(frdamp$i) \
1977                0 1 2 3 4 5 6 7 8 9     
1978        set entrycmd(frdamp$i) "hapinfo $hist $i frdamp"
1979        set entryvar(frdamp$i) [hapinfo $hist $i frdamp]
1980        grid $framePF.tkOptDamp -row 0 -sticky nsw -column 5
1981    }
1982    # resize the scroll window to match the actual
1983    update idletasks
1984    $expgui(FracBox) config -scrollregion [grid bbox $expgui(FracBox).f]
1985    $expgui(FracBox) config -width [lindex [grid bbox $expgui(FracBox).f] 2]
1986    update idletasks
1987    # enable traces on entryvar now
1988    set entrycmd(trace) 1
1989}
1990
1991#-----------------------------------------------------------------------
1992# display the profile page
1993#-----------------------------------------------------------------------
1994proc DisplayProfile {} {
1995    global expgui entrycmd entryvar entrybox expmap
1996
1997    # trap if more than one histogram is selected unless global mode
1998    if {$expgui(globalmode) == 0 && [llength $expgui(curhist)] > 1} {
1999        set expgui(curhist) [lindex $expgui(curhist) 0]
2000    }
2001    # display the selected histograms
2002    $expgui(profFrame).hs.lbox selection clear 0 end
2003    foreach h $expgui(curhist) {
2004        $expgui(profFrame).hs.lbox selection set $h
2005    }
2006
2007    # destroy the contents of the frame
2008    eval destroy [winfo children $expgui(ProfileBox).f]
2009    # since the next steps can take a while, do a screen update
2010    update idletasks
2011
2012    if {$expgui(globalmode) == 0} {
2013        # must have at least one histogram selected here
2014        if {[llength $expgui(curhist)] == 0} return
2015        # disable traces on entryvar for right now
2016        set entrycmd(trace) 0
2017        set hist [lindex $expmap(powderlist) $expgui(curhist)]
2018        # no defined histograms?
2019        if {$hist == ""} return
2020        # Create one frame for each Phase.
2021        set ind -1
2022        set htype [string range $expmap(htype_$hist) 2 2]
2023        foreach i $expmap(phaselist_$hist) {
2024            incr ind
2025            # Label Heading for each phase.
2026            set ptype [string trim [hapinfo $hist $i proftype]]
2027            if {$expgui(haveBW)} {
2028                grid [TitleFrame $expgui(ProfileBox).f.$i \
2029                        -text "Hist $hist -- Phase $i (type $ptype)" \
2030                        -relief groove -bd 2] \
2031                        -column 0 -row $ind -sticky ew
2032                set ProfileFrame [$expgui(ProfileBox).f.$i getframe]
2033                grid [frame $ProfileFrame.1] \
2034                        -column 0 -row 0 -columnspan 10
2035                pack [label $ProfileFrame.1.l  \
2036                        -text Damping]\
2037                        -side left
2038            } else {
2039                grid [frame $expgui(ProfileBox).f.$i -relief groove -bd 4] \
2040                        -column 0 -row $ind -sticky ew
2041                set ProfileFrame $expgui(ProfileBox).f.$i
2042                grid [frame $ProfileFrame.1] \
2043                        -column 0 -row 0 -columnspan 10 -sticky ew
2044                pack [label $ProfileFrame.1.l  \
2045                        -text "Phase $i (type $ptype)    Damping"]\
2046                        -side left
2047            }
2048            tk_optionMenu $ProfileFrame.1.tkOptDamp entryvar(pdamp_$i) \
2049                    0 1 2 3 4 5 6 7 8 9
2050            set entrycmd(pdamp_$i) "hapinfo $hist $i pdamp"
2051            set entryvar(pdamp_$i) [hapinfo $hist $i pdamp]
2052            pack $ProfileFrame.1.tkOptDamp -side left
2053            pack [label $ProfileFrame.1.l1 \
2054                    -text "  Peak cutoff"]\
2055                    -side left
2056            pack [entry $ProfileFrame.1.e1  \
2057                    -width 10 -textvariable entryvar(pcut_$i)]\
2058                    -side left
2059            set entrybox(pcut_$i) $ProfileFrame.1.e1
2060            set entrycmd(pcut_$i) "hapinfo $hist $i pcut"
2061            set entryvar(pcut_$i) [hapinfo $hist $i pcut]
2062
2063            pack [button $ProfileFrame.1.b1  \
2064                    -text "Change Type" \
2065                    -command "ChangeProfileType $hist $i"]\
2066                    -side left
2067           
2068            set col -1
2069            set row 1
2070            set nterms [hapinfo $hist $i profterms]
2071            set lbls "dummy [GetProfileTerms $i $hist $ptype]"
2072            for { set num 1 } { $num <= $nterms } { incr num } {
2073                set term {}
2074                catch {set term [lindex $lbls $num]}
2075                if {$term == ""} {set term $num}
2076                incr col
2077                grid [label $ProfileFrame.l${num}_${i} -text "$term"] \
2078                        -row $row -column $col
2079                incr col
2080                grid [checkbutton $ProfileFrame.ref${num}_${i} \
2081                        -variable entryvar(pref${num}_$i)] -row $row -column $col
2082                set entrycmd(pref${num}_$i) "hapinfo $hist $i pref$num"
2083                set entryvar(pref${num}_$i) [hapinfo $hist $i pref$num]
2084                incr col
2085                grid [entry $ProfileFrame.ent${num}_${i} \
2086                        -textvariable entryvar(pterm${num}_$i)\
2087                        -width 12] -row $row -column $col
2088                set entrybox(pterm${num}_$i) $ProfileFrame.ent${num}_${i}
2089                set entrycmd(pterm${num}_$i) "hapinfo $hist $i pterm$num"
2090                set entryvar(pterm${num}_$i) [hapinfo $hist $i pterm$num]
2091                if {$col > 6} {set col -1; incr row}
2092            }
2093        }
2094        grid columnconfigure $expgui(ProfileBox).f 0 -weight 1
2095    } else {
2096        # get histogram list
2097        set histlist {}
2098        foreach item $expgui(curhist) {
2099            lappend histlist [lindex $expmap(powderlist) $item]
2100        }
2101        # must have at least one histogram selected here
2102        if {[llength $histlist] == 0} return
2103        # disable traces on entryvar for right now
2104        set entrycmd(trace) 0
2105        # loop through histograms & phases, set up an array by phase & profile type
2106        catch {unset prtyparray histarray phasearray}
2107        foreach hist $histlist {
2108            foreach phase $expmap(phaselist_$hist) {
2109                set prtyp [string trim [hapinfo $hist $phase proftype]]
2110                set key ${prtyp}_$phase
2111                lappend prtyparray($key) $hist
2112                lappend histarray($key) $hist
2113                lappend phasearray($key) $phase
2114            }
2115        }
2116       
2117        set ptype ""
2118        set i -1
2119        # loop over all combined phases and profile types, sorted 1st by profile number
2120        foreach key [lsort [array names prtyparray]] {
2121            # split key
2122            scan $key %d_%d prftyp p
2123
2124            if {$ptype != $prftyp || !$expgui(globalphasemode)} {
2125                set ptype $prftyp
2126                set curhistlist $histarray($key)
2127                set curphaslist $phasearray($key)
2128               
2129                set hist1 [lindex $curhistlist 0]
2130                set phase1 [lindex $curphaslist 0]
2131                set nterms [hapinfo $hist1 $phase1 profterms]
2132                set htype [string range $expmap(htype_$hist1) 2 2]
2133                set lbls "dummy [GetProfileTerms $phase1 $hist1 $ptype]"
2134                # Create a frame for this type
2135                incr i
2136                set boxtitle "Phase $p, hist [CompressList $histarray($key)]"
2137                if {$expgui(haveBW)} {
2138                    grid [TitleFrame $expgui(ProfileBox).f.$i \
2139                            -text "(type $ptype)" \
2140                            -relief groove -bd 2] \
2141                            -column 0 -row $i -sticky ew
2142                    set ProfileFrame [$expgui(ProfileBox).f.$i getframe]
2143                    grid [frame $ProfileFrame.0] \
2144                            -column 0 -row 0 -columnspan 20 -sticky ew
2145                } else {
2146                    grid [frame $expgui(ProfileBox).f.$i \
2147                            -relief groove -bd 4] \
2148                            -column 0 -row $i -sticky ew
2149                    set ProfileFrame $expgui(ProfileBox).f.$i
2150                    grid [frame $ProfileFrame.0] \
2151                            -column 0 -row 0 -columnspan 20 -sticky ew
2152                    grid [label $ProfileFrame.0.0  \
2153                            -text "Profile Type $ptype   "] -row 0 -column 0
2154                }
2155                grid [label $ProfileFrame.0.1  \
2156                        -anchor w] -row 0 -column 1
2157                grid [frame $ProfileFrame.1] \
2158                        -column 0 -row 1 -columnspan 20 -sticky ew
2159                grid [label $ProfileFrame.1.2  \
2160                        -text "Damping"] -row 0 -column 2
2161                tk_optionMenu $ProfileFrame.1.tkOptDamp \
2162                        entryvar(pdamp_$i) 0 1 2 3 4 5 6 7 8 9
2163                grid $ProfileFrame.1.tkOptDamp -row 0 -column 3
2164                grid [button $ProfileFrame.1.edit \
2165                        -text "Global Edit"] -row 0 -column 4 -sticky w
2166                set entryvar(pdamp_$i) [hapinfo $hist $phase pdamp]
2167                grid [button $ProfileFrame.1.b1 -text "Change Type"] \
2168                        -row 0 -column 5 -sticky w
2169                set col -1
2170                set row 2
2171                for { set num 1 } { $num <= $nterms } { incr num } {
2172                    set term {}
2173                    catch {set term [lindex $lbls $num]}
2174                    if {$term == ""} {set term $num}
2175                    incr col
2176                    grid [label $ProfileFrame.l${num}_${i} \
2177                            -text "$term"] -row $row -column $col
2178                    incr col
2179                    grid [checkbutton $ProfileFrame.ref${num}_${i} \
2180                            -variable entryvar(pref${num}_$i)] \
2181                            -row $row -column $col
2182                    set entryvar(pref${num}_$i) [hapinfo $hist $phase pref$num]
2183                    if {$col > 10} {set col -1; incr row}
2184                }
2185                grid columnconfigure $expgui(ProfileBox).f 0 -weight 1
2186            } else {
2187                # add to the current entry
2188                eval lappend curhistlist $histarray($key)
2189                eval lappend curphaslist $phasearray($key)
2190                append boxtitle "\nPhase $p, hist [CompressList $histarray($key)]"
2191            }
2192            $ProfileFrame.0.1 config -text $boxtitle
2193            $ProfileFrame.1.edit config -command "\
2194                    EditProfile \"\n$boxtitle\" \
2195                    [list $curhistlist] \
2196                    [list $curphaslist]"
2197            $ProfileFrame.1.b1 config -command "ChangeProfileType \
2198                    [list $curhistlist] [list $curphaslist]"
2199            set entrycmd(pdamp_$i) "hapinfo \
2200                    [list $curhistlist] \
2201                    [list $curphaslist] pdamp"
2202            for { set num 1 } { $num <= $nterms } { incr num } {
2203                set entrycmd(pref${num}_$i) "hapinfo \
2204                        [list $curhistlist] \
2205                        [list $curphaslist] pref$num"
2206            }
2207        }
2208    }
2209   
2210    # resize the scroll window to match the actual
2211    update idletasks
2212    $expgui(ProfileBox) config -scrollregion [grid bbox $expgui(ProfileBox).f]
2213    $expgui(ProfileBox) config -width [lindex [grid bbox $expgui(ProfileBox).f] 2]
2214    update idletasks
2215    ResizeNotebook
2216    # enable traces on entryvar now
2217    set entrycmd(trace) 1
2218}
2219
2220# process the bit settings in the print options
2221#   bitnum -- the number of the bit to be tested/set starting at 0 for the LSBit
2222proc printsetting {bitnum "action get" "value {}"} {
2223    global entryvar expgui
2224    if {$action == "get"} {
2225        return [expr ([expinfo print] & int(pow(2,$bitnum))) != 0]
2226    } elseif $value {
2227        set newval [expr ([expinfo print] | int(pow(2,$bitnum)))]
2228    } else {
2229        set newval [expr ([expinfo print] & ~int(pow(2,$bitnum)))]
2230    }
2231    expinfo print set $newval
2232    set expgui(printopt) "Print Options ([expinfo print])"
2233}
2234
2235# need to respond to mouse presses -- control variable associated with extract Fobs
2236# and set the LeBail extraction flags
2237proc SetupExtractHist {} {
2238    global expgui entrycmd entryvar expmap
2239
2240    # display the selected histograms
2241    $expgui(lsFrame).hs.lbox selection clear 0 end
2242    foreach h $expgui(curhist) {
2243        $expgui(lsFrame).hs.lbox selection set $h
2244    }
2245
2246    # get histogram list
2247    set histlist {}
2248    foreach item $expgui(curhist) {
2249        set hist [lindex $expmap(powderlist) $item]
2250        if {$hist != ""} {lappend histlist $hist}
2251    }
2252    set entrycmd(fobsextract) "histinfo [list $histlist] foextract"
2253    if {[llength $histlist] == 0 || [string trim $histlist] == ""} {
2254        set entrycmd(LBdamp) ""
2255        foreach phase {1 2 3 4 5 6 7 8 9} {
2256            $expgui(FobsExtractFrame).l$phase config -fg grey
2257            set expgui(Fextract$phase) {}
2258            foreach item $expgui(ExtractSettingsRadiobuttons) {
2259                ${item}$phase config -state disabled -bd 1
2260            }
2261        }
2262    } elseif {[llength $histlist] == 1} {
2263        # disable traces on entryvar
2264        set entrycmd(trace) 0
2265        set entryvar(fobsextract) [histinfo $histlist foextract]
2266        set entrycmd(LBdamp) "histinfo $histlist LBdamp"
2267        set entryvar(LBdamp) [histinfo $histlist LBdamp]
2268        foreach phase {1 2 3 4 5 6 7 8 9} {
2269            # is the phase present?
2270            if {[lsearch -exact $expmap(phaselist_$histlist) $phase] == -1} {
2271                $expgui(FobsExtractFrame).l$phase config -fg grey
2272                set expgui(Fextract$phase) {}
2273                foreach item $expgui(ExtractSettingsRadiobuttons) {
2274                    ${item}$phase config -state disabled -bd 1
2275                }
2276            } else {
2277                $expgui(FobsExtractFrame).l$phase config -fg black
2278                foreach item $expgui(ExtractSettingsRadiobuttons) {
2279                    ${item}$phase config -state normal -bd 2
2280                }
2281                set expgui(Fextract$phase) [hapinfo $histlist $phase extmeth]
2282            }
2283        }
2284    } elseif {[llength $histlist] > 1} {
2285        # disable traces on entryvar
2286        set entrycmd(LBdamp) "histinfo [list $histlist] LBdamp"
2287        set entryvar(LBdamp) [histinfo [lindex $histlist 0] LBdamp]
2288        set entrycmd(trace) 0
2289        # multiple histograms need phases in any histogram
2290        foreach phase {1 2 3 4 5 6 7 8 9} {
2291            set gotphase($phase) 0
2292        }           
2293        foreach hist $histlist {
2294            foreach phase $expmap(phaselist_$hist) {
2295                set gotphase($phase) 1
2296            }
2297        }
2298        foreach phase {1 2 3 4 5 6 7 8 9} {
2299            set expgui(Fextract$phase) {}
2300            if $gotphase($phase) {
2301                $expgui(FobsExtractFrame).l$phase config -fg black
2302                foreach item $expgui(ExtractSettingsRadiobuttons) {
2303                    ${item}$phase config -state normal -bd 2
2304                }
2305            } else {
2306                $expgui(FobsExtractFrame).l$phase config -fg grey
2307                foreach item $expgui(ExtractSettingsRadiobuttons) {
2308                    ${item}$phase config -state disabled -bd 1
2309                }
2310            }
2311        }
2312    }
2313    # reenable traces
2314    set entrycmd(trace) 1
2315}
2316# respond to a change in the fobs extraction method for a phase
2317# force the main extraction flag on, if fobs extraction is selected for any phase
2318proc HistExtractSet {phase} {
2319    global expgui entryvar expmap
2320    foreach item $expgui(curhist) {
2321        lappend histlist [lindex $expmap(powderlist) $item]
2322    }
2323    hapinfo $histlist $phase extmeth set $expgui(Fextract$phase)
2324    incr expgui(changed)
2325    if {$expgui(Fextract$phase) != 0} {set entryvar(fobsextract) 1}
2326}
2327#---------------------------- Global Edit Functions ------------------------
2328proc editbackground {} {
2329    global expgui expmap entrycmd
2330    set histlist {}
2331    foreach n $expgui(curhist) {
2332        lappend histlist [lindex $expmap(powderlist) $n]
2333    }
2334    if {[llength $histlist] == 0} return
2335
2336    set w .back
2337    catch {destroy $w}
2338    toplevel $w -bg beige
2339    if {$expgui(globalmode) != 0} {
2340        wm title $w "Global Edit Background"
2341    } else {
2342        wm title $w "Edit Background"
2343    }
2344   
2345    pack [frame $w.0 -bd 6 -relief groove  -bg beige \
2346            ] -side top -expand yes -fill both
2347    if {[llength $histlist] > 1} {
2348        grid [label $w.0.a \
2349            -text "Setting background terms for histograms [CompressList $histlist]" \
2350            -bg beige] -row 0 -column 0 -columnspan 10
2351    } else {
2352        grid [label $w.0.a \
2353            -text "Setting background terms for histogram $histlist" \
2354            -bg beige] -row 0 -column 0 -columnspan 4
2355        grid [button $w.0.bkg -text "Fit Background\nGraphically" \
2356                -command "QuitEditBackground $w; bkgedit $histlist"] \
2357                -row 0 -column 4 -rowspan 2
2358        grid columnconfig $w.0 0 -weight 1
2359        grid columnconfig $w.0 4 -weight 1
2360    }
2361    set hist [lindex $histlist 0]
2362    grid [label $w.0.b -text "Function type" -bg beige]  -row 1 -column 0 -sticky e
2363
2364    # disable traces on  expgui(backtype) & expgui(backterms) now
2365    set entrycmd(trace) 0
2366
2367    # number of terms
2368    set expgui(backtype) [histinfo $hist backtype]
2369    set expgui(orig_backtype) $expgui(backtype)
2370    set expgui(prev_backtype) $expgui(backtype)
2371    set typemenu [tk_optionMenu $w.0.type expgui(backtype) null]
2372    $typemenu delete 0 end
2373    foreach item {
2374        "1 - Shifted Chebyschev"
2375        "2 - Cosine Fourier series"
2376        "3 - Radial distribution peaks"
2377        "4 - Power series in Q**2n/n!"
2378        "5 - Power series in n!/Q**2n"
2379        "6 - Power series in Q**2n/n! and n!/Q**2n"
2380        "7 - Linear interpolation function"
2381        "8 - Reciprocal interpolation function"
2382    } {
2383        set val [lindex $item 0]
2384        $typemenu insert end radiobutton -variable expgui(backtype) \
2385                -label $item -value $val
2386    }
2387
2388    grid $w.0.type   -row 1 -column 1
2389    grid [label $w.0.c -text "  Number of terms"  -bg beige] -row 1 -column 2
2390
2391    # function type
2392    set expgui(backterms) [histinfo $hist backterms]
2393    set expgui(orig_backterms) $expgui(backterms)
2394    set list {}; for {set i 1} {$i <= 36} {incr i} {lappend list $i}
2395    eval tk_optionMenu $w.0.terms expgui(backterms) $list
2396    grid $w.0.terms   -row 1 -column 3
2397    # enable traces on  expgui(backtype) & expgui(backterms) now
2398    set entrycmd(trace) 1
2399
2400    #set background terms
2401    for {set num 1 } { $num <= 36 } { incr num } {
2402        set var "bterm$num"
2403        set expgui($var) {}
2404        set expgui(orig_$var) {}
2405    }
2406    if {[llength $histlist] == 1} {
2407        for {set num 1 } { $num <= $expgui(backterms) } { incr num } {
2408            set var "bterm$num"
2409            set expgui($var) [histinfo $histlist $var]
2410            set expgui(orig_$var) $expgui($var)
2411        }
2412    }
2413
2414    pack [frame $w.1 -bd 6 -relief groove  -bg beige] -side top \
2415            -expand yes -fill both
2416    ShowBackTerms $w.1
2417
2418    set expgui(temp) {}
2419    pack [frame $w.b -bg beige] -fill x -expand yes -side top
2420    grid [button $w.b.2 -text Set -command "destroy $w"] -row 0 -column 1
2421    grid [button $w.b.3 -text Quit \
2422            -command "QuitEditBackground $w"] -row 0 -column 2
2423    grid [button $w.b.help -text Help -bg yellow \
2424            -command "MakeWWWHelp expgui3.html EditBackground"] \
2425            -row 0 -column 4
2426    grid columnconfig $w.b 0 -weight 1
2427    grid columnconfig $w.b 3 -weight 1
2428    bind $w <Key-F1> "MakeWWWHelp expgui3.html EditBackground"
2429    bind $w <Return> "destroy $w"
2430
2431    # force the window to stay on top
2432    putontop $w
2433
2434    focus $w.b.2
2435    tkwait window $w
2436    afterputontop
2437
2438    if {$expgui(temp) != ""} return
2439
2440    if {$expgui(orig_backtype) != $expgui(backtype)} {
2441        histinfo $histlist backtype set $expgui(backtype)
2442        incr expgui(changed)
2443    }
2444    if {$expgui(orig_backterms) != $expgui(backterms)} {
2445        histinfo $histlist backterms set $expgui(backterms)
2446        incr expgui(changed)
2447    }
2448    for {set num 1 } { $num <= $expgui(backterms) } { incr num } {
2449        set var "bterm$num"
2450        if {$expgui(orig_$var) != $expgui($var)} {
2451            histinfo $histlist $var set $expgui($var)
2452            incr expgui(changed)
2453        }
2454    }
2455
2456    if {$expgui(globalmode) == 0} {
2457        set expgui(backtypelbl) "Function type [histinfo $hist backtype]"
2458        set expgui(backtermlbl) "([histinfo $hist backterms] terms)"
2459    }
2460}
2461
2462trace variable expgui(backterms) w ChangeBackTerms
2463proc ChangeBackTerms {a b c} {
2464    global entrycmd expgui
2465    if !$entrycmd(trace) return
2466    ShowBackTerms .back.1
2467}
2468
2469trace variable expgui(backtype) w ChangeBackType
2470# reset the terms to 1, 0, 0... when the number of terms increase
2471proc ChangeBackType {a b c} {
2472    global entrycmd expgui
2473    if !$entrycmd(trace) return
2474    if {$expgui(prev_backtype) == $expgui(backtype)} return
2475    set expgui(prev_backtype) $expgui(backtype)
2476    for {set num 1 } { $num <= $expgui(backterms) } { incr num } {
2477        set var "bterm$num"
2478        if {$num == 1} {
2479            set expgui($var) 1.0
2480        } else {
2481            set expgui($var) 0.0
2482        }
2483    }
2484}
2485
2486proc ShowBackTerms {w } {
2487    global expgui expmap
2488    # destroy the contents of the frame
2489    eval destroy [winfo children $w]
2490    set histlist {}
2491    foreach n $expgui(curhist) {
2492        lappend histlist [lindex $expmap(powderlist) $n]
2493    }
2494    set widgetsPerRow 4
2495    for {set rows 2; set num 1 } { $num <= $expgui(backterms) } { incr rows } {
2496        for {set cols 0} { (2*$widgetsPerRow > $cols) && ($num <= $expgui(backterms)) }  { incr num }  {
2497            set var "bterm$num"
2498            grid [label $w.l$num -text $num -bg beige]  \
2499                    -row $rows -column $cols -sticky nes
2500            incr cols
2501            grid [entry $w.e$num -width 15 -textvariable expgui($var) \
2502                    ] -row $rows  -column $cols  -sticky news
2503            incr cols
2504        }
2505    }
2506}
2507
2508proc QuitEditBackground {w} {
2509    global expgui
2510    # lets find out if anything changed
2511    set changed 0
2512    if {$expgui(orig_backtype) != $expgui(backtype)} {
2513        set changed 1
2514    }
2515    if {$expgui(orig_backterms) != $expgui(backterms)} {
2516        set changed 1
2517    }
2518    for {set num 1 } { $num <= $expgui(backterms) } { incr num } {
2519        set var "bterm$num"
2520        if {$expgui(orig_$var) != $expgui($var)} {
2521            set changed 1
2522            break
2523        }
2524    }
2525    if $changed {
2526        set decision [tk_dialog .changes "Abandon Changes" \
2527                "You have made changes to the background. Ok to abandon changes?" \
2528                warning 0 Abandon Keep]
2529        if !$decision {
2530            set expgui(temp) "Quit"
2531            destroy $w
2532        }
2533    } else {
2534        set expgui(temp) "Quit"
2535        destroy $w
2536    }
2537}
2538
2539# this probably needs work
2540proc editglobalparm {cmd variable title "histlist {}" "phase {}"} {
2541    global expgui expmap
2542    set w .global
2543    catch {destroy $w}
2544    toplevel $w -bg beige
2545    wm title $w "Edit Global Parameter"
2546    set expgui(temp) {}
2547    if {[llength $histlist] == 0} {
2548        set hist {}
2549        foreach n $expgui(curhist) {
2550            lappend hist [lindex $expmap(powderlist) $n]
2551        }
2552    } else {
2553        set hist $histlist
2554    }
2555    pack [frame $w.0 -bd 6 -relief groove -bg beige] \
2556            -side top -expand yes -fill both
2557    grid [label $w.0.a -text "Setting $title for histograms [CompressList $hist]"\
2558            -bg beige] \
2559            -row 0 -column 0 -columnspan 10
2560    grid [entry $w.0.b -textvariable expgui(temp)] \
2561            -row 1 -column 0
2562
2563
2564    pack [frame $w.b -bg beige] -fill x -expand yes -side top
2565    pack [button $w.b.2 -text Set -command "destroy $w"] -side left
2566    pack [button $w.b.3 -text Quit -command "set expgui(temp) {}; destroy $w"] -side left
2567    pack [button $w.b.help -text Help -bg yellow \
2568            -command "MakeWWWHelp expgui3.html EditParm"] -side right
2569    bind $w <Key-F1> "MakeWWWHelp expgui3.html EditParm"
2570    bind $w <Return> "destroy $w"
2571
2572    # force the window to stay on top
2573    putontop $w
2574    focus $w.b.2
2575    tkwait window $w
2576    afterputontop
2577
2578    if {$expgui(temp) != ""} {
2579        foreach h $hist {
2580            if {$cmd == "histinfo"} {
2581                histinfo $h $variable set $expgui(temp)
2582                incr expgui(changed)
2583                if $expgui(debug) {
2584                    puts "histinfo $h $variable set $expgui(temp)"
2585                }
2586            } elseif {$cmd == "hapinfo"} {
2587                hapinfo $h $phase $variable set $expgui(temp)
2588                incr expgui(changed)
2589                if $expgui(debug) {
2590                    puts "hapinfo $phase $h $variable set $expgui(temp)"
2591                }
2592            } else {
2593                error "$cmd unimplemented"
2594            }
2595        }
2596    }
2597}
2598
2599proc EditProfile {title histlist phaselist} {
2600    global expgui expmap entrycmd
2601    set w .back
2602    catch {destroy $w}
2603    toplevel $w -bg beige
2604    wm title $w "Global Edit Profile"
2605    set hist [lindex $histlist 0]
2606    set phase [lindex $phaselist 0]
2607    set ptype [string trim [hapinfo $hist $phase proftype]]
2608    set htype [string range $expmap(htype_$hist) 2 2]
2609    set nterms [hapinfo $hist $phase profterms]
2610   
2611    pack [frame $w.0 -bd 6 -relief groove  -bg beige \
2612            ] -side top -expand yes -fill both
2613    grid [label $w.0.a \
2614            -text "Setting profile terms: $title" \
2615            -bg beige] -row 0 -column 0 -columnspan 10
2616    grid [label $w.0.b -text "Function type $ptype"  -bg beige]  -row 1 -column 0
2617    grid [label $w.0.c -text "  Peak cutoff" -bg beige] -row 1 -column 3
2618    grid [entry $w.0.d -width 10 ]  -row 1 -column 4
2619    set entrylist {}
2620    lappend entrylist "pcut $w.0.d"
2621
2622    set col -1
2623    set row 1
2624    set lbls "dummy [GetProfileTerms $phase $hist $ptype]"
2625    pack [frame $w.1 -bd 6 -relief groove  -bg beige \
2626            ] -side top -expand yes -fill both
2627    for { set num 1 } { $num <= $nterms } { incr num } {
2628        set term {}
2629        catch {set term [lindex $lbls $num]}
2630        if {$term == ""} {set term $num}
2631        incr col
2632        grid [label $w.1.l${num} -text "$term" -bg beige] \
2633                -row $row -column $col
2634        incr col
2635        grid [entry $w.1.ent${num} \
2636                -width 14] -row $row -column $col
2637        lappend entrylist "pterm$num $w.1.ent${num}"   
2638        if {$col > 6} {set col -1; incr row}
2639    }
2640    pack [frame $w.b -bg beige] -fill x -expand yes -side top
2641    grid [button $w.b.2 -text Set \
2642            -command "SetEditProfile [list $entrylist] [list $phaselist] \
2643            [list $histlist] $w"] -row 0 -column 1
2644    grid [button $w.b.3 -text Quit \
2645            -command "QuitEditProfile $w [list $entrylist]"] -row 0 -column 2
2646    grid [button $w.b.help -text Help -bg yellow \
2647            -command "MakeWWWHelp expgui5.html GlobalEdit"] \
2648            -row 0 -column 4
2649    grid columnconfig $w.b 0 -weight 1
2650    grid columnconfig $w.b 3 -weight 1
2651    bind $w <Key-F1> "MakeWWWHelp expgui5.html GlobalEdit"
2652    bind $w <Return> "QuitEditProfile $w [list $entrylist]"
2653
2654    # force the window to stay on top
2655    putontop $w
2656    focus $w.b.2
2657    tkwait window $w
2658    afterputontop
2659}
2660
2661proc SetEditProfile {entrylist phaselist histlist w} {
2662    global expgui
2663    foreach item $entrylist {
2664        set value [ [lindex $item 1] get ]
2665        if {$value != ""} {
2666            hapinfo $histlist $phaselist [lindex $item 0] set $value
2667            incr expgui(changed)
2668            if $expgui(debug) {
2669                puts "hapinfo [list $phaselist] [list $histlist] [lindex $item 0] set $value"
2670            }
2671        }
2672    }
2673    destroy $w
2674}
2675
2676proc QuitEditProfile {w entrylist} {
2677    global expgui
2678    # lets find out if anything changed
2679    set changed 0
2680    foreach item $entrylist {
2681        if {[ [lindex $item 1] get ] != ""} {set changed 1; break}
2682    }
2683    if $changed {
2684        set decision [tk_dialog .changes "Abandon Changes" \
2685                "You have made changes to the Profile. Ok to abandon changes?" \
2686                warning 0 Abandon Keep]
2687        if !$decision {destroy $w}
2688    } else {
2689        destroy $w
2690    }
2691}
2692
2693##############################################################################
2694##                               #############################################
2695## END OF THE PROCEDURES SECTION #############################################
2696##                               #############################################
2697##############################################################################
2698
2699# <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
2700# <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<                          <<<<<<<<<<<<<<<<<<<
2701# <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<   BEGIN:  GUI SECTION    >>>>>>>>>>>>>>>>>>>
2702# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>                          >>>>>>>>>>>>>>>>>>>
2703# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
2704# A frame for menu items at top of display
2705set expgui(fm) [frame .fm -relief raised -borderwidth 2 -width 150 -height 40]
2706# Pack the menu frame.
2707pack $expgui(fm) -fill x -side top -anchor n
2708
2709# create a button bar
2710pack [frame .bar -relief raised -bd 2 -bg beige] -fill x -side top -anchor n
2711
2712# Creating the notebook and panes
2713
2714# create an array element describing each notebook page (used with BW only)
2715# element 0 -- pane name
2716#         1 -- Label on frame
2717#         2 -- initialization command
2718#         3 -- update command
2719#         4 -- 0/1 Use 1 if pane should be disabled in when all histograms
2720#                are selected in global mode, 0 otherwise
2721#         5 -- Web page for pane
2722#         6 -- name anchor on Web page for pane
2723set expgui(notebookpagelist) {
2724    {lsFrame     "LS Controls" \
2725            "" \
2726            SetupExtractHist \
2727            0  expgui1.html ""}
2728    {phaseFrame   Phase        \
2729            "" \
2730            "SelectOnePhase \$expgui(curPhase); DisplayAllAtoms noreset" \
2731            0  expgui2.html ""}
2732    {histFrame    Histogram \
2733            MakeHistPane \
2734            DisplayHistogram \
2735            1  expgui3.html ""}
2736    {fracFrame    Scaling \
2737            MakeScalingPane \
2738            DisplayFrac \
2739            0  expgui4.html ""}
2740    {profFrame    Profile \
2741            MakeProfilePane \
2742            DisplayProfile \
2743            1  expgui5.html ""}
2744    {consFrame    Constraints \
2745            "source [file join $expgui(scriptdir) atomcons.tcl]; MakeConstraintsPane" \
2746            DisplayConstraintsPane \
2747            0  expgui6.html ""}
2748    {orientFrame  "MD Pref Orient" \
2749            MakeOrientPane \
2750            DisplayOrient \
2751            0  expgui7.html MD}
2752    {odfFrame  "SH Pref Orient" \
2753            "source [file join $expgui(scriptdir) odf.tcl]; MakeODFPane" \
2754            DisplayODFPane \
2755            0  expgui7.html ODF}
2756}
2757
2758if $expgui(haveBW) {
2759    pack [NoteBook .n -bd 2] -expand yes -fill both
2760    # this should not be needed, but for some reason NoteBook is not
2761    # using the optionDB
2762    catch {.n configure -font [option get .n font Canvas]}
2763    foreach item $expgui(notebookpagelist) {
2764        set frm [lindex $item 0]
2765        set expgui($frm) [\
2766            .n insert end $frm -text [lindex $item 1] \
2767            -createcmd [lindex $item 2] \
2768            -raisecmd "set expgui(pagenow) $frm; [lindex $item 3]"]
2769
2770        # at this time expgui(frameactionlist) is generated
2771        # from expgui(notebookpagelist), but in the future it might
2772        # make sense to use expgui(notebookpagelist) directly
2773        lappend expgui(frameactionlist) "$frm [list [lindex $item 3]]"
2774
2775        # panes to disable in global "all" mode
2776        if {[lindex $item 4]} {
2777            lappend expgui(GlobalModeAllDisable) \
2778                    "$frm \{.n itemconfigure $frm\}"
2779        }
2780    }
2781} else {
2782    Notebook:create .n \
2783            -pages {lsFrame phaseFrame histFrame fracFrame profFrame}
2784    pack .n -anchor w -fill both -expand yes
2785    foreach item {lsFrame phaseFrame histFrame fracFrame profFrame \
2786            orientFrame} \
2787            page {"LS Controls" Phase Histogram Scaling Profile \
2788            "MD Pref Orient"} {
2789        set expgui($item) [Notebook:frame .n $item]
2790        Notebook:pageconfig .n $item -command "InitPage $item" -title $page
2791    }
2792    lappend expgui(frameactionlist) "lsFrame SetupExtractHist"
2793    lappend expgui(frameactionlist) "phaseFrame {DisplayAllAtoms noreset}"
2794    lappend expgui(frameactionlist) "histFrame DisplayHistogram"
2795    lappend expgui(frameactionlist) "fracFrame DisplayFrac"
2796    lappend expgui(frameactionlist) "profFrame DisplayProfile"
2797    lappend expgui(frameactionlist) "orientFrame DisplayOrient"
2798    set expgui(GlobalModeAllDisable) {}
2799    lappend expgui(GlobalModeAllDisable) "histFrame {Notebook:pageconfig .n histFrame}"
2800    lappend expgui(GlobalModeAllDisable) "profFrame {Notebook:pageconfig .n profFrame}"
2801}
2802
2803# this is used to bring up the selected frame
2804proc RaisePage {nextpage} {
2805    global expgui
2806    if $expgui(haveBW) {
2807        .n see $nextpage
2808        .n raise $nextpage
2809        set expgui(pagenow) $nextpage
2810    } else {
2811        Notebook:raise .n $nextpage
2812        InitPage $nextpage
2813    }
2814}
2815# this is only called when BWidget is not in use
2816proc InitPage {nextpage} {
2817    global expgui
2818    set expgui(pagenow) $nextpage
2819    UpdateCurrentPage
2820}
2821# resize the notebook to fit all the tabs and the largest page
2822proc ResizeNotebook {} {
2823    global expgui
2824    if {$expgui(haveBW)} {
2825        .n compute_size
2826    } else {
2827        Notebook:resize .n
2828    }
2829}
2830
2831#----------------------------------------------------------------------------
2832proc MakePhasePane {} {
2833    #\/ \/ \/ \/ \/ \/ \/ BEGINNING OF PHASE PANE CODE \/ \/ \/ \/ \/ \/ \/
2834    global expgui entryvar entrybox entrycmd
2835    frame $expgui(phaseFrame).top
2836    set frameLatt [frame $expgui(phaseFrame).frameLatt]
2837    #  This is a big frame in the Phase notebook pane to hold atomic data.
2838    set fbig [frame $expgui(phaseFrame).fbig -width 180 \
2839            -relief raised -borderwidth 4 -class Coord]
2840    #  This is a frame just below the big frame: for edits
2841    set frame3 [frame $expgui(phaseFrame).frame3 -width 100 \
2842            -relief raised -borderwidth 4 -bg $expgui(bkgcolor1)]
2843
2844    grid $expgui(phaseFrame).top -sticky news -row 0 -column 0
2845    grid $frameLatt -sticky news -row 2 -column 0
2846    grid $fbig -sticky news -row 3 -column 0
2847    # give extra space to the atoms box
2848    grid columnconfigure $expgui(phaseFrame) 0 -weight 1
2849    grid rowconfigure $expgui(phaseFrame) 3 -weight 1
2850    grid $frame3 -sticky news -row 4 -column 0
2851    grid columnconfigure $expgui(phaseFrame) 0 -weight 1
2852    grid rowconfigure $expgui(phaseFrame) 3 -weight 1
2853    grid [frame  $expgui(phaseFrame).top.ps] -column 0 -row 0 -sticky w
2854    # this is where the buttons will go
2855    pack [label $expgui(phaseFrame).top.ps.0 -text "No Phases"] -side left
2856   
2857    grid [label $expgui(phaseFrame).top.lA -text title: \
2858            -fg blue ] -column 1 -row 0 -sticky e
2859    grid [entry $expgui(phaseFrame).top.lB -textvariable entryvar(phasename) \
2860            -fg blue -width 45] -column 2 -row 0 -sticky e
2861    grid columnconfigure $expgui(phaseFrame).top 1 -weight 1
2862    # ------------- Lattice Parameter Box ------------------
2863    set row 0
2864    foreach col {2 4 6} var {a b c} lbl {a b c} {
2865        grid [label $frameLatt.l$var -text $lbl] \
2866                -column $col -row $row -padx 5 -sticky e
2867        incr col
2868        grid [entry $frameLatt.e$var -textvariable entryvar($var) -width 10] \
2869            -column $col -row $row -padx 5
2870        set entrybox($var) $frameLatt.e$var
2871    }
2872    incr row
2873    foreach col {2 4 6} var {alpha beta gamma} lbl {a b g} {
2874        grid [label $frameLatt.l$var -text $lbl] \
2875                -column $col -row $row -padx 5 -sticky e
2876        set font [$frameLatt.l$var cget -font]
2877        $frameLatt.l$var config -font "Symbol [lrange $font 1 end]"
2878
2879        incr col
2880        grid [entry $frameLatt.e$var -textvariable entryvar($var) -width 10] \
2881            -column $col -row $row -padx 5
2882        set entrybox($var) $frameLatt.e$var
2883    }
2884   
2885    grid [label $frameLatt.lr -text "Refine Cell"] -column 8 -row 0 -padx 5 -sticky e
2886    grid [label $frameLatt.ld -text "Cell damping"] -column 8 -row 1 -padx 5 -sticky e
2887    set cFlag [checkbutton $frameLatt.c -text "" -variable entryvar(cellref)]
2888    grid $cFlag -column 9 -row 0 -padx 5 -sticky e
2889    tk_optionMenu $frameLatt.om entryvar(celldamp) 0 1 2 3 4 5 6 7 8 9
2890    grid $frameLatt.om -column 9 -row 1 -padx 5 -sticky e
2891    grid [label $frameLatt.phasetype -textvariable expgui(phasetype) -fg blue] \
2892            -column 1 -row 0 -rowspan 2
2893    if [file executable $expgui(exptool)] {
2894        grid [button $expgui(phaseFrame).frameLatt.newp \
2895                -text "Add\nPhase" -padx 1.5m -command MakeAddPhaseBox \
2896                ] -column 0 -row 0 -rowspan 2 -sticky w
2897    }
2898    grid columnconfig $frameLatt  1 -weight 1
2899    grid columnconfig $frameLatt  0 -weight 1
2900    #-------------- Begin Atom Coordinates Box  ------------------------------
2901    grid [listbox  $fbig.title -height 1 -relief flat \
2902            -exportselection 0 -bg lightgrey -fg black \
2903            -selectforeground black -selectbackground lightgrey] \
2904            -row 0 -column 0 -sticky ew
2905    set expgui(atomtitle) $fbig.title
2906    bind $expgui(atomtitle) <Button-1> {
2907        set i [lsearch {number type mult x y z occupancy} $expgui(asorttype)]
2908        incr i
2909        set expgui(asorttype) [lindex {number type mult x y z occupancy number} $i]
2910        DisplayAllAtoms
2911    }
2912    bind $expgui(atomtitle) <Button-3> {set expgui(asorttype) number; DisplayAllAtoms}
2913
2914    $expgui(atomtitle) configure -selectmode extended
2915    grid [listbox   $fbig.lbox -height 10 \
2916            -exportselection 0 \
2917            -xscrollcommand " $fbig.bscr set"\
2918            -yscrollcommand " $fbig.rscr set"\
2919            ] -row 1 -column 0 -sticky news
2920    set expgui(atomlistbox) $fbig.lbox
2921    $expgui(atomlistbox) configure -selectmode extended
2922    grid [scrollbar $fbig.bscr -orient horizontal \
2923            -command "move2boxesX \" $fbig.title $fbig.lbox \" " \
2924            ] -row 2 -column 0 -sticky ew
2925    grid [scrollbar $fbig.rscr  -command "$fbig.lbox yview" \
2926            ] -row 1 -column 1 -sticky ns
2927    # give extra space to the atoms box
2928    grid columnconfigure $fbig 0 -weight 1
2929    grid rowconfigure $fbig 1 -weight 1
2930   
2931    #   BIND mouse in editbox
2932    bind $expgui(atomlistbox) <ButtonRelease-1>   editRecord
2933    bind $expgui(atomlistbox) <Button-3>   SelectAllAtoms
2934   
2935    #-------------- End Atoms Section  ---------------------------------
2936
2937    # --------------------------- Begin Edit Box ------------------------
2938    grid [set expgui(EditingAtoms) [label $frame3.top -bg $expgui(bkgcolor1) -fg blue]] \
2939            -column 0 -row 0 -padx 2 -pady 3 -columnspan 10 -sticky w
2940    if [file executable $expgui(exptool)] {
2941        button $frame3.newa -text "Add New Atoms" \
2942                -bg $expgui(bkgcolor1) -highlightthickness 0 \
2943                -command {MakeAddAtomsBox $expgui(curPhase)}
2944        grid $frame3.newa -column 11 -row 0
2945    }
2946    button [set expgui(atomxform) $frame3.xa] \
2947            -bg $expgui(bkgcolor1) -highlightthickness 0 \
2948            -command {MakeXformAtomsBox $expgui(curPhase)}
2949    grid $expgui(atomxform) -column 11 -row 1 -sticky ew
2950
2951    set f3l1 [label $frame3.l1 -text "Refinement Flags:" -bg $expgui(bkgcolor1)]
2952    grid $f3l1 -column 0 -row 1 -padx 2 -sticky nsw -pady 3
2953    foreach lbl {X U F} var {xref uref fref} col {1 2 3} {
2954        grid [checkbutton $frame3.cf$col \
2955                -text $lbl -variable entryvar($var) \
2956                -bg $expgui(bkgcolor1) -highlightthickness 0 \
2957                -activebackground $expgui(bkgcolor1)] \
2958                -column $col -row 1 -padx 4 -pady 3 -sticky w
2959    }
2960    set f3l4 [label $frame3.l4 -text "  Damping:" -bg $expgui(bkgcolor1)]
2961    grid $f3l4 -column 4 -row 1 -padx 2 -sticky nsw -pady 3
2962   
2963    set col 4
2964    foreach var {xdamp udamp fdamp} num {2 3 4} lbl {X U F} {
2965        grid [label $frame3.lom$num -text $lbl \
2966                -bg $expgui(bkgcolor1)] \
2967                -column [incr col] -row 1 -padx 2 -pady 3 -sticky w
2968        tk_optionMenu $frame3.om$num entryvar($var) 0 1 2 3 4 5 6 7 8 9
2969        $frame3.om$num config -highlightthickness 0
2970        grid $frame3.om$num -column [incr col] -row 1 -padx 2 -pady 3 -sticky w
2971    }
2972    set expgui(atomreflbl) "$frame3.l1 $frame3.l4 $frame3.lom2 $frame3.lom3 $frame3.lom4 "
2973    set expgui(atomref) "$frame3.cf1 $frame3.cf2 $frame3.cf3 $frame3.om2 $frame3.om3 $frame3.om4"
2974   
2975    set coords [frame $frame3.coords  -width 100 -borderwidth 0  -bg $expgui(bkgcolor1)]
2976    grid $coords -column 0 -row 6 -columnspan 12 -sticky nsew
2977   
2978    set f3l1 [label $frame3.coords.l1 -text "Label" -bg $expgui(bkgcolor1)]
2979    grid $f3l1 -column 0 -row 4 -padx 2 -sticky nsw -pady 3
2980    set expgui(atomlabels) $f3l1
2981
2982    set f3e1 [entry  $frame3.coords.e1 -textvariable entryvar(label) -width 6]
2983    grid $f3e1 -column 1 -row 4 -padx 2 -sticky nsw -pady 3
2984    set expgui(atomentry) $f3e1
2985
2986    set f3l8 [label $frame3.coords.l8 -text "Coordinates" -bg $expgui(bkgcolor1)]
2987    grid $f3l8 -column 2 -row 4 -padx 2 -sticky nsw -pady 3
2988    lappend expgui(atomlabels) $f3l8
2989    set f3l11 [label $frame3.coords.l11 -text "Occupancy" -bg $expgui(bkgcolor1)]
2990    grid $f3l11 -column 6 -row 4 -padx 2 -sticky nsw -pady 3
2991    lappend expgui(atomlabels) $f3l11
2992
2993    foreach var {x y z frac} col {3 4 5 7} {
2994        set entrybox($var) [entry $frame3.coords.e$var \
2995                -textvariable entryvar($var) -width 10]
2996        grid $entrybox($var) -column $col -row 4 -padx 2 -sticky nsw -pady 3
2997        lappend expgui(atomentry) $entrybox($var)
2998    }
2999
3000
3001    set f3f31 [frame $frame3.f3f31  -width 100 -borderwidth 0 -bg $expgui(bkgcolor1)]
3002    grid $f3f31 -column 0 -row 7 -columnspan 12
3003    set expgui(anisolabels) {}
3004    foreach lbl {13 14 15 16 17 18} txt {Uiso U22 U33 U12 U13 U23} {
3005        lappend expgui(anisolabels)  [\
3006                label $f3f31.l$lbl -text $txt -bg $expgui(bkgcolor1)
3007        ]
3008    }
3009    set expgui(anisoentry) {}
3010    foreach i {e13 e14 e15 e16 e17 e18} var {U11 U22 U33 U12 U13 U23} {
3011        lappend expgui(anisoentry) [\
3012                entry $f3f31.$i -textvariable entryvar($var) \
3013                -width 10]
3014        set entrybox($var) $f3f31.$i
3015    }
3016   
3017    set col 0
3018    foreach item1 $expgui(anisolabels) item2 $expgui(anisoentry) {
3019        grid $item1 -column $col -row 0 -sticky nsw -pady 3
3020        incr col
3021        grid $item2 -column $col -row 0 -sticky nsw -pady 3
3022        incr col
3023    }
3024    # --------------------------- End Edit Box -------------------------
3025   
3026    #/\ /\ /\ /\ /\ /\ /\ END OF PHASE PANE CODE /\ /\ /\ /\ /\ /\ /\ /\ /
3027    # resize in case the pane needs more space
3028    ResizeNotebook
3029}
3030
3031#-----------------------------------------------------------------------------
3032proc MakeHistPane {} {
3033    #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
3034    global expgui
3035   
3036    grid columnconfigure $expgui(histFrame) 0 -weight 1
3037    grid rowconfigure $expgui(histFrame) 1 -weight 1
3038    grid rowconfigure $expgui(histFrame) 2 -weight 1
3039    grid rowconfigure $expgui(histFrame) 3 -weight 1
3040
3041    grid [frame $expgui(histFrame).hs -class HistList] \
3042            -column 0 -row 0 -rowspan 10 -sticky nsew
3043    MakeHistBox $expgui(histFrame).hs
3044    bind $expgui(histFrame).hs.lbox <ButtonRelease-1>  {
3045        set expgui(curhist) [$expgui(histFrame).hs.lbox curselection]
3046        DisplayHistogram
3047    }
3048    bind $expgui(histFrame).hs.lbox <Button-3>  {
3049        if $expgui(globalmode) {
3050            $expgui(histFrame).hs.lbox selection set 0 end
3051            set expgui(curhist) [$expgui(histFrame).hs.lbox curselection]
3052            DisplayHistogram
3053        }
3054    }
3055   
3056    frame $expgui(histFrame).top -borderwidth 4 -relief groove
3057    grid [label $expgui(histFrame).top.txt] -row 0 -column 0
3058    if $expgui(haveBW) {
3059        foreach item {backBox diffBox} num {2 3} \
3060                title {Background "Diffractometer Constants"} {
3061            TitleFrame $expgui(histFrame).$item  \
3062                    -borderwidth 4 -side left -relief groove -text $title
3063            set expgui($item) [$expgui(histFrame).$item getframe]
3064            grid $expgui(histFrame).$item -column 1 -row $num -sticky nsew
3065            grid rowconfigure $expgui(histFrame) $num -minsize 100
3066        }
3067    } else {
3068        foreach item {backBox diffBox} num {1 2} \
3069                title {Background "Diffractometer Constants"} {
3070            frame $expgui(histFrame).$item  -borderwidth 4 -relief groove
3071            grid $expgui(histFrame).$item -column 1 -row $num -sticky nsew
3072            set expgui($item)  $expgui(histFrame).$item
3073            grid [label $expgui(histFrame).$item.title -text $title] \
3074                    -row 0 -column 0 -columnspan 10 -sticky nw
3075        }
3076    }
3077    grid [frame $expgui(histFrame).bb] -column 1 -row 6
3078    if [file executable $expgui(exptool)] {
3079        button $expgui(histFrame).bb.newh -text "Add New\nHistogram" \
3080                -command MakeAddHistBox
3081        grid $expgui(histFrame).bb.newh -column 0 -row 1
3082    }
3083    if [file exists [file join $expgui(scriptdir) excledt]] {
3084        button $expgui(histFrame).bb.excl \
3085                -text "Set Data Limits &\nExcluded Regions" -command excledit
3086        grid $expgui(histFrame).bb.excl -column 1 -row 1
3087    }
3088    button $expgui(histFrame).bb.use -text "Set Histogram\nUse Flags" \
3089            -command SetHistUseFlags
3090    grid $expgui(histFrame).bb.use -column 2 -row 1
3091
3092    # BACKGROUND information.
3093    # <<<<<<<<<<<<<<<<<<<<<<<<< BACKGROUND  <<<<<<<<<<<<<<<<<<<<<
3094    grid [frame $expgui(backBox).frm1 ] -row 0 -column 0  -columnspan 11
3095    grid [label $expgui(backBox).frm1.lBGType \
3096            -textvariable expgui(backtypelbl)] \
3097            -row 1 -column 0 -sticky nws  -padx 2 -pady 3
3098    grid [label $expgui(backBox).frm1.lBGTerms \
3099            -textvariable expgui(backtermlbl)] \
3100            -row 1 -column 1 -sticky nws  -padx 2 -pady 3
3101    grid [button $expgui(backBox).frm1.edit -textvariable expgui(bkglbl) \
3102            -command editbackground] \
3103            -row 1 -column 2 -columnspan 3 -sticky w -padx 2 -pady 3
3104    grid [frame $expgui(backBox).frm2 ] \
3105            -row 1 -column 0 -columnspan 11 -sticky e
3106    grid [label $expgui(backBox).frm2.lfBG -text "  Refine background" ] \
3107            -row 2 -column 1 -sticky news -padx 4 -pady 3
3108    grid [checkbutton $expgui(backBox).frm2.rfBG -text "" \
3109            -variable  entryvar(bref) ] \
3110            -row 2 -column 2 -sticky news -padx 4 -pady 3
3111    grid [label $expgui(backBox).frm2.lBGDamp -text Damping ] \
3112            -row 2 -column 3 -sticky w    -padx 2 -pady 3
3113    tk_optionMenu $expgui(backBox).frm2.om  entryvar(bdamp) 0 1 2 3 4 5 6 7 8 9
3114    grid $expgui(backBox).frm2.om \
3115            -row 2 -column 4 -sticky news -padx 4 -pady 3 -sticky e
3116    #^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^END OF HISTOGRAM PANE CODE ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
3117    # insert the histograms & resize in case the pane needs more space
3118    sethistlist
3119    ResizeNotebook
3120}
3121###############################################################################
3122proc MakeScalingPane {} {
3123    #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
3124    global expgui entryvar entrybox
3125
3126    pack [frame $expgui(fracFrame).hs -class HistList] \
3127            -side left -expand y -fill both
3128    MakeHistBox $expgui(fracFrame).hs
3129    bind $expgui(fracFrame).hs.lbox <ButtonRelease-1> {
3130        set expgui(curhist) [$expgui(fracFrame).hs.lbox curselection]
3131        DisplayFrac
3132    }
3133    bind $expgui(fracFrame).hs.lbox <Button-3>  {
3134        if $expgui(globalmode) {
3135            $expgui(fracFrame).hs.lbox selection set 0 end
3136            set expgui(curhist) [$expgui(fracFrame).hs.lbox curselection]
3137            DisplayFrac
3138        }
3139    }
3140
3141    pack [frame $expgui(fracFrame).f1] -fill both -expand true
3142    # Create a large canvas area containing a frame for each phase in the data set.
3143    # The canvas and vertical scrollbar are inside a frame called f1
3144    if $expgui(haveBW) {
3145        TitleFrame $expgui(fracFrame).f1.scaleBox \
3146                -borderwidth 4 -text "Scale Factor"
3147        #           -borderwidth 4 -width 600 -height 100 -label "Scale Factor"
3148        grid $expgui(fracFrame).f1.scaleBox -column 0 -row 0 -sticky nsew -columnspan 2
3149        set expgui(scaleBox)  [$expgui(fracFrame).f1.scaleBox getframe]
3150        grid [label $expgui(scaleBox).histSFLabel -text Scale] \
3151                -row 1 -column 0 -sticky nws  -padx 2 -pady 3
3152    } else {
3153        frame $expgui(fracFrame).f1.scaleBox  -borderwidth 4 -relief groove
3154        grid $expgui(fracFrame).f1.scaleBox -column 0 -row 0 -sticky nsew -columnspan 2
3155        set expgui(scaleBox)  $expgui(fracFrame).f1.scaleBox
3156        grid [label $expgui(scaleBox).histSFLabel -text "Scale Factor"] \
3157                -row 1 -column 0 -sticky nws  -padx 2 -pady 3
3158    }
3159    grid [entry $expgui(scaleBox).ent1 -textvariable entryvar(scale) -width 15] \
3160            -row 1 -column 1 -sticky ew -padx 4 -pady 3
3161    set entrybox(scale) $expgui(scaleBox).ent1
3162
3163    button $expgui(scaleBox).but1 -text "Set Globally" \
3164            -command "editglobalparm histinfo scale {Scale Factor}"
3165
3166    grid [label $expgui(scaleBox).histSFRLabel -text " Refine"] \
3167            -row 1 -column 2 -sticky nws  -padx 2 -pady 3
3168    grid [checkbutton $expgui(scaleBox).rf -variable entryvar(sref)] \
3169            -row 1 -column 3 -sticky news -padx 4 -pady 3
3170    grid [label $expgui(scaleBox).lD1 -text "Damping"] \
3171            -row 1 -column 4 -sticky w    -padx 2 -pady 3
3172    tk_optionMenu $expgui(scaleBox).om entryvar(sdamp) 0 1 2 3 4 5 6 7 8 9
3173    grid $expgui(scaleBox).om \
3174            -row 1 -column 5 -sticky news -padx 4 -pady 3
3175    grid columnconfigure $expgui(scaleBox) 6  -weight 1
3176   
3177    if $expgui(haveBW) {
3178        grid [TitleFrame $expgui(fracFrame).f1.phaseFrac -bd 4 \
3179                -text "Phase Fractions" -relief groove] \
3180                -sticky news -row 1 -column 0 -columnspan 2
3181        set PhaseFractBox [$expgui(fracFrame).f1.phaseFrac getframe]
3182    } else {
3183        set PhaseFractBox $expgui(fracFrame).f1
3184    }
3185    grid columnconfigure $expgui(fracFrame).f1 0 -weight 1
3186    grid rowconfigure $expgui(fracFrame).f1 1 -weight 1
3187   
3188    grid [set expgui(FracBox) [canvas $PhaseFractBox.fracBox \
3189            -scrollregion {0 0 5000 500} \
3190            -yscrollcommand "$PhaseFractBox.yscroll set" \
3191            -width 500 -height 350 -bg lightgrey]] \
3192            -sticky  news -row 1 -column 0
3193    grid [scrollbar $PhaseFractBox.yscroll \
3194            -command "$expgui(FracBox) yview" \
3195            -orient vertical] \
3196            -sticky ns -row 1 -column 1
3197    frame $expgui(FracBox).f -bd 0
3198    $expgui(FracBox) create window 0 0 -anchor nw  -window $expgui(FracBox).f
3199
3200    # the rest of the page is created in DisplayFrac
3201
3202    # insert the histograms & resize in case the pane needs more space
3203    sethistlist
3204    ResizeNotebook
3205    # ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ END OF SCALING PANE CODE ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
3206}
3207###############################################################################
3208proc MakeProfilePane {} {
3209    global expgui
3210    # 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
3211    pack [frame $expgui(profFrame).hs -class HistList] \
3212            -side left -expand y -fill both
3213    MakeHistBox $expgui(profFrame).hs
3214    bind $expgui(profFrame).hs.lbox <ButtonRelease-1> {
3215        set expgui(curhist) [$expgui(profFrame).hs.lbox curselection]
3216        DisplayProfile
3217    }
3218    bind $expgui(profFrame).hs.lbox <Button-3>  {
3219        if $expgui(globalmode) {
3220            $expgui(profFrame).hs.lbox selection set 0 end
3221            set expgui(curhist) [$expgui(profFrame).hs.lbox curselection]
3222            DisplayProfile
3223        }
3224    }
3225
3226    # Create a large canvas area containing a frame for each phase in the data set.
3227    # The canvas and vertical scrollbar are inside a frame called f1
3228    pack [frame $expgui(profFrame).f1] -fill both -expand true
3229    grid [set expgui(ProfileBox) [canvas $expgui(profFrame).f1.profileBox \
3230            -scrollregion {0 0 5000 500} -width 500 -height 350 -bg lightgrey]] \
3231            -sticky  news -row 0 -column 0
3232    grid [scrollbar $expgui(profFrame).f1.yscroll -orient vertical] \
3233            -sticky ns -row 0 -column 1
3234   
3235    $expgui(ProfileBox) config -yscrollcommand "$expgui(profFrame).f1.yscroll set"
3236    $expgui(profFrame).f1.yscroll config -command { $expgui(ProfileBox) yview }
3237   
3238    grid columnconfigure $expgui(profFrame).f1 1 -weight 1
3239    grid rowconfigure $expgui(profFrame).f1 0 -weight 1
3240    frame $expgui(ProfileBox).f -bd 0
3241    $expgui(ProfileBox) create window 0 0 -anchor nw  -window $expgui(ProfileBox).f
3242   
3243    # insert the histograms & resize in case the pane needs more space
3244    sethistlist
3245    ResizeNotebook
3246    # ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ END OF PROFILE PANE CODE ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
3247}
3248
3249##############################################################################
3250# 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
3251array set printopts {
3252    0 "Print the reciprocal metric tensor changes"
3253    1 "Print the correlation matrix"
3254    2 "Print the Least-Squares matrices and vectors"
3255    4 "Print the linear constraint matrices"
3256    5 "Print the applied  shifts and shift factors"
3257    6 "Print the reciprocal metric tensor Var-Covar terms"
3258    7 "Print all parameters for each cycle"
3259    8 "Print summary shift/esd data after last cycle"
3260    9 "Print zero/unit pole figure constraint terms"
3261}
3262pack [frame $expgui(lsFrame).hs -class HistList] \
3263        -side left -expand y -fill both
3264MakeHistBox $expgui(lsFrame).hs
3265bind $expgui(lsFrame).hs.lbox <ButtonRelease-1> {
3266    set expgui(curhist) [$expgui(lsFrame).hs.lbox curselection]
3267    SetupExtractHist
3268}
3269bind $expgui(lsFrame).hs.lbox <Button-3>  {
3270    if $expgui(globalmode) {
3271        $expgui(lsFrame).hs.lbox selection set 0 end
3272        set expgui(curhist) [$expgui(lsFrame).hs.lbox curselection]
3273        SetupExtractHist
3274    }
3275}
3276
3277pack [frame $expgui(lsFrame).f1] -fill both -expand true
3278set row 0
3279grid [label $expgui(lsFrame).f1.his1 -pady 6 -text "Last History:"] -row $row -column 0
3280grid [label $expgui(lsFrame).f1.his2 -relief raised -bd 2 -pady 6 \
3281        -textvariable expgui(last_History)] \
3282        -row $row -column 1 -columnspan 5 -sticky w
3283incr row
3284grid [label $expgui(lsFrame).f1.tit1 -pady 6 -text "Title:"] -row $row -column 0
3285grid [entry $expgui(lsFrame).f1.tit2 \
3286        -textvariable entryvar(title) -width 48] \
3287        -row $row -column 1 -columnspan 5 -sticky w
3288set entrycmd(title) "expinfo title"
3289
3290incr row
3291grid rowconfigure $expgui(lsFrame).f1 $row -weight 1
3292incr row
3293grid [frame $expgui(lsFrame).f1.b -bd 4 -relief groove] \
3294        -row $row -column 0 -columnspan 2 -pady 3  -sticky s
3295grid [label $expgui(lsFrame).f1.b.lcyc -text "Number of Cycles"] -row 0 -column 0
3296grid [entry $expgui(lsFrame).f1.b.ecyc -width 3 \
3297        -textvariable entryvar(cycles)] -row 0 -column 1
3298set entrybox(cycles) $expgui(lsFrame).f1.b.ecyc
3299
3300grid [frame $expgui(lsFrame).f1.cv -bd 4 -relief groove] \
3301        -row $row -column 2 -sticky ew
3302grid [label $expgui(lsFrame).f1.cv.l -text "Convgerence Criterion"] \
3303        -row 0 -column 0 -columnspan 2
3304grid [label $expgui(lsFrame).f1.cv.v -textvariable expgui(convlbl)] -row 1 -column 0
3305grid [scale $expgui(lsFrame).f1.cv.s -orient horizontal \
3306        -from -200 -to 200 -showvalue 0 -command SetConv -resolution 10 \
3307        -variable expgui(convg)] -row 1 -column 1
3308
3309incr row
3310grid [menubutton $expgui(lsFrame).f1.lprint -textvariable expgui(printopt) \
3311        -menu $expgui(lsFrame).f1.lprint.menu -bd 4 -relief raised \
3312        ] -row $row -column 0 -columnspan 2
3313menu $expgui(lsFrame).f1.lprint.menu
3314foreach num [lsort [array names printopts]] {
3315    $expgui(lsFrame).f1.lprint.menu add checkbutton \
3316            -label "$printopts($num) ([expr int(pow(2,$num))])"\
3317        -variable entryvar(printopt$num)
3318}
3319
3320grid [frame $expgui(lsFrame).f1.marq -bd 4 -relief groove] \
3321        -row $row -column 2 -sticky ew
3322grid [label $expgui(lsFrame).f1.marq.l -text "Marquardt Damping"] \
3323        -row 0 -column 0 -columnspan 2
3324grid [label $expgui(lsFrame).f1.marq.v -textvariable expgui(marq)] \
3325        -row 1 -column 0
3326grid [scale $expgui(lsFrame).f1.marq.s -orient horizontal \
3327        -from 1.0 -to 9.99 -showvalue 0 -command SetMarq -resolution 0.01 \
3328        -variable expgui(marq)] -row 1 -column 1
3329
3330incr row
3331grid rowconfigure $expgui(lsFrame).f1 $row -weight 1
3332
3333incr row
3334if {$expgui(haveBW)} {
3335    grid [TitleFrame $expgui(lsFrame).f1.a -bd 4 -relief groove \
3336            -text "Reflection Intensity Extraction" \
3337            ] -row $row -column 0 -columnspan 6
3338    set expgui(FobsExtractFrame) [$expgui(lsFrame).f1.a getframe]
3339} else {
3340    grid [frame $expgui(lsFrame).f1.a -bd 4 -relief groove \
3341            ] -row $row -column 0 -columnspan 6
3342    set expgui(FobsExtractFrame) $expgui(lsFrame).f1.a
3343}
3344
3345grid [frame $expgui(FobsExtractFrame).c -bd 4 -relief groove] \
3346        -row 0 -column 8 -columnspan 3 -sticky ens
3347grid [label $expgui(FobsExtractFrame).c.fol -text "Extract Fobs"] \
3348        -row 0 -column 2
3349grid [checkbutton $expgui(FobsExtractFrame).c.foc \
3350        -variable entryvar(fobsextract)] -row 0 -column 3
3351
3352grid [frame $expgui(FobsExtractFrame).d -bd 4 -relief groove] \
3353        -row 0 -column 3 -columnspan 5 -sticky ens
3354grid [label $expgui(FobsExtractFrame).d.fol -text "LeBail damping"] \
3355        -row 0 -column 2
3356tk_optionMenu $expgui(FobsExtractFrame).d.d entryvar(LBdamp) \
3357        0 1 2 3 4 5 6 7 8 9
3358grid $expgui(FobsExtractFrame).d.d -row 0 -column 3
3359incr row
3360grid rowconfigure $expgui(lsFrame).f1 $row -weight 1
3361
3362
3363
3364foreach num {1 2 3 4 5 6 7 8 9} {
3365    grid [label $expgui(FobsExtractFrame).l$num -text $num] -row 1 -column $num
3366    grid [radiobutton $expgui(FobsExtractFrame).cc$num \
3367            -command "HistExtractSet $num" \
3368            -variable expgui(Fextract$num) -value 0] \
3369            -row 2 -column $num
3370    grid [radiobutton $expgui(FobsExtractFrame).ca$num \
3371            -command "HistExtractSet $num" \
3372            -variable expgui(Fextract$num) -value 1] \
3373            -row 3 -column $num
3374    grid [radiobutton $expgui(FobsExtractFrame).cb$num \
3375            -command "HistExtractSet $num" \
3376            -variable expgui(Fextract$num) -value 2] \
3377            -row 4 -column $num
3378}
3379set expgui(ExtractSettingsRadiobuttons) $expgui(FobsExtractFrame).cc
3380lappend expgui(ExtractSettingsRadiobuttons) $expgui(FobsExtractFrame).ca
3381lappend expgui(ExtractSettingsRadiobuttons) $expgui(FobsExtractFrame).cb
3382
3383grid [label $expgui(FobsExtractFrame).t \
3384        -text "Extraction\nMethod" -anchor c] \
3385        -column 0 -row 0 -sticky n
3386grid [label $expgui(FobsExtractFrame).t0 -text "(Phase #)" -anchor c] \
3387        -column 10 -row 1 -sticky w
3388grid [label $expgui(FobsExtractFrame).t1 -text "Rietveld" -anchor c] -column 0 -row 2
3389grid [label $expgui(FobsExtractFrame).t2 -text "F(calc) Weighted" -anchor c] -column 0 -row 3
3390grid [label $expgui(FobsExtractFrame).t3 -text "Equally Weighted" -anchor c] -column 0 -row 4
3391grid [label $expgui(FobsExtractFrame).t2a -text "(Model biased)" -anchor c] -column 10 -row 3
3392grid [label $expgui(FobsExtractFrame).t3a -text "(Le Bail method)" -anchor c] -column 10 -row 4
3393
3394proc InitLSvars {} {
3395    global expgui
3396    set expgui(convg) [set expgui(convinit) [expinfo convg]]
3397    set expgui(convlbl) [format %5.2f [expr pow(10,$expgui(convg)/100.)]]
3398    set expgui(marq) [set expgui(marqinit) [expinfo marq]]
3399}
3400proc SetConv {x} {
3401    global expgui
3402    if {$x != $expgui(convinit) && $expgui(changed) <= 0} {
3403        incr expgui(changed)
3404    }
3405    if {$expgui(changed)} {expinfo convg set $x}
3406    set expgui(convlbl) [format %5.2f [expr {pow(10,$x/100.)}]]
3407}
3408proc SetMarq {x} {
3409    global expgui
3410    if {$x != $expgui(marqinit) && $expgui(changed) <= 0} {
3411        incr expgui(changed)
3412    }
3413    if {$expgui(changed)} {expinfo marq set $x}
3414}
3415# ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ END OF LS PANE CODE ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
3416#-------------------------------------------------------------------------
3417#-------------------------------------------------------------------------
3418#vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv THE MENU BAR vvvvvvvvvvvvvvvvvvvvvv
3419
3420#---- file menu button
3421menubutton $expgui(fm).file -text File -menu $expgui(fm).file.menu
3422menu $expgui(fm).file.menu
3423if $expgui(debug) {
3424    $expgui(fm).file.menu add command -label "Reset" -command "reset"
3425}
3426if {$expgui(shell)} {
3427    $expgui(fm).file.menu add command -label "Open" -command readnewexp
3428    $expgui(fm).file.menu add command -label "expnam" -command readnewexp
3429}
3430$expgui(fm).file.menu add command -label "Save" -underline 0 \
3431        -command savearchiveexp
3432foreach c {s S} {bind . <Alt-$c> [list savearchiveexp]}
3433$expgui(fm).file.menu add command -label "Save As" \
3434        -command "SaveAsFile"
3435$expgui(fm).file.menu add command -label "Reread .EXP file" \
3436        -command {rereadexp $expgui(expfile)}
3437
3438#---- help menu button
3439menubutton $expgui(fm).help -text Help -menu $expgui(fm).help.menu
3440menu $expgui(fm).help.menu
3441$expgui(fm).help.menu add command -command showhelp -underline 0 \
3442        -label "Help Summary"
3443$expgui(fm).help.menu add command -command MakeWWWHelp  \
3444        -label "Help on current pane"
3445$expgui(fm).help.menu add command -command "MakeWWWHelp menu" \
3446        -label "Help on menu"
3447foreach c {h H} {bind . <Alt-$c> [list showhelp]}
3448# define help actions
3449bind . <Key-F1> MakeWWWHelp
3450$expgui(fm).help.menu add command -label "About..." -command About
3451$expgui(fm).help.menu add command -label "Cite..." -command Cite
3452
3453#---- options menu button
3454menubutton $expgui(fm).option -text Options \
3455        -menu $expgui(fm).option.menu
3456menu $expgui(fm).option.menu
3457
3458if {$expgui(shell)} {
3459    $expgui(fm).option.menu add checkbutton  -label "Archive EXP" \
3460            -variable expgui(archive)
3461    $expgui(fm).option.menu add checkbutton  -label "Use DISAGL window" \
3462            -variable expgui(disaglSeparateBox)
3463    $expgui(fm).option.menu  add checkbutton -label "Autoload EXP" \
3464            -variable expgui(autoexpload)
3465    $expgui(fm).option.menu  add checkbutton -label "Iconify during GSAS" \
3466            -variable expgui(autoiconify)
3467}
3468$expgui(fm).option.menu add cascade -menu  $expgui(fm).option.menu.asort \
3469        -label "Sort atoms by"
3470
3471set expgui(asorttype) number
3472menu $expgui(fm).option.menu.asort
3473foreach opt {number type mult x y z occupancy} {
3474    $expgui(fm).option.menu.asort add radiobutton -command DisplayAllAtoms \
3475            -label $opt -value $opt -variable expgui(asorttype)
3476}
3477
3478$expgui(fm).option.menu add cascade -menu  $expgui(fm).option.menu.hsort \
3479        -label "Sort histograms by"
3480
3481set expgui(hsorttype) number
3482menu $expgui(fm).option.menu.hsort
3483$expgui(fm).option.menu.hsort add radiobutton -command sethistlist \
3484        -label number -value number -variable expgui(hsorttype)
3485$expgui(fm).option.menu.hsort add radiobutton -command sethistlist \
3486        -label "Histogram type" -value type -variable expgui(hsorttype)
3487$expgui(fm).option.menu.hsort add radiobutton -command sethistlist \
3488        -label "Bank #" -value bank -variable expgui(hsorttype)
3489$expgui(fm).option.menu.hsort add radiobutton -command sethistlist \
3490        -label "Angle/Wavelength" -value angle -variable expgui(hsorttype)
3491
3492#---- Global mode menu button
3493$expgui(fm).option.menu add cascade -menu $expgui(fm).option.menu.editmode \
3494        -label "Multiple hist. selection"
3495menu $expgui(fm).option.menu.editmode
3496$expgui(fm).option.menu.editmode add radiobutton  -label "Off" \
3497        -variable expgui(globalmode) -value 0 \
3498        -command sethistlist
3499$expgui(fm).option.menu.editmode add radiobutton  -label "All" \
3500        -variable expgui(globalmode) -value 6 \
3501        -command sethistlist
3502$expgui(fm).option.menu.editmode add radiobutton  -label "TOF" \
3503        -variable expgui(globalmode) -value 1 \
3504        -command sethistlist
3505$expgui(fm).option.menu.editmode add radiobutton  -label "CW Neutron" \
3506        -variable expgui(globalmode) -value 2  \
3507        -command sethistlist
3508$expgui(fm).option.menu.editmode add radiobutton  -label "Alpha12 Xray" \
3509        -variable expgui(globalmode) -value 3 \
3510        -command sethistlist
3511$expgui(fm).option.menu.editmode add radiobutton  -label "Monochromatic Xray" \
3512        -variable expgui(globalmode) -value 4 \
3513        -command sethistlist
3514$expgui(fm).option.menu.editmode add radiobutton  -label "Energy Disp Xray" \
3515        -variable expgui(globalmode) -value 5 \
3516        -command sethistlist
3517$expgui(fm).option.menu.editmode add separator
3518$expgui(fm).option.menu.editmode add checkbutton \
3519        -label "Group phases together" \
3520        -variable expgui(globalphasemode) \
3521        -command sethistlist
3522
3523set expgui(globalmode) 0
3524set expgui(globalphasemode) 1
3525
3526if {$tcl_platform(platform) == "unix"} {
3527    $expgui(fm).option.menu  add checkbutton -label "Override backspace" \
3528            -variable env(GSASBACKSPACE)
3529}
3530
3531$expgui(fm).option.menu add cascade -menu  $expgui(fm).option.menu.font \
3532        -label "Screen font"
3533menu $expgui(fm).option.menu.font
3534foreach f {10 11 12 13 14 16 18 20 22} {
3535    $expgui(fm).option.menu.font add radiobutton \
3536            -command {SetTkDefaultOptions $expgui(font); ResizeFont .; ResizeNotebook} \
3537        -label $f -value $f -variable expgui(font) -font "Helvetica -$f"
3538}
3539
3540$expgui(fm).option.menu  add checkbutton -label "Show EXPTOOL output" \
3541        -variable expgui(showexptool)
3542$expgui(fm).option.menu add command -label "Save Options" \
3543        -command "SaveOptions"
3544
3545pack $expgui(fm).file $expgui(fm).option -side left  -in $expgui(fm)
3546
3547if {$expgui(shell)} {
3548    foreach menu $expgui(menunames) {
3549        set m [string tolower $menu]
3550        pack [menubutton $expgui(fm).$m -text $menu \
3551                -menu $expgui(fm).$m.menu] -side left
3552        menu $expgui(fm).$m.menu
3553    }
3554}
3555pack $expgui(fm).help  -side right -in $expgui(fm)
3556
3557if {$expgui(shell)} {
3558    # add the commands in expgui_menulist
3559    foreach menu [array names expgui_menulist ] {
3560        foreach cmd $expgui_menulist($menu) {
3561            set action {}
3562            set opt {}
3563            catch {set action [lindex $expgui_cmdlist($cmd) 0]}
3564            catch {set opt [lindex $expgui_cmdlist($cmd) 2]}
3565            if {$expgui(debug) && $action == ""} {puts "blank command for $cmd"}
3566            if {$action != "" && $action != "-"} {
3567                eval $expgui(fm).$menu.menu add command \
3568                        -label $cmd $opt -command [list [subst $action]]
3569                if {[lindex $opt 0] == "-underline"} {
3570                    catch {
3571                        set num [lindex $opt 1]
3572                        set key [string range $cmd $num $num]
3573                        bind . <Alt-[string tolower $key]> [subst $action]
3574                        bind . <Alt-[string toupper $key]> [subst $action]
3575                    }
3576                }
3577            }
3578        }
3579    }
3580}
3581# setup command help
3582foreach cmd [array names expgui_cmdlist] {
3583    set help {}
3584    catch {set help [lindex $expgui_cmdlist($cmd) 1]}
3585    if {$help == ""} {
3586        if {$expgui(debug)} {puts "no help for $cmd"}
3587    } else {
3588        # remove
3589        regsub -all \x09 $help " " help
3590        # preserve blank lines
3591        regsub -all \x0A\x0A $help "AAA1234567890AAA" help
3592        regsub -all \x0A $help " " help
3593        regsub -all "AAA1234567890AAA" $help \x0A\x0A help
3594        regsub -all " +" $help " " help
3595        set expgui_helplist($cmd) [string trim $help]
3596    }
3597}
3598if {$expgui(shell)} {
3599    # set up button bar
3600    foreach cmd $expgui(buttonlist) {
3601        set action {}
3602        catch {set action [lindex $expgui_cmdlist($cmd) 0]}
3603        if {$expgui(debug) && $action == ""} {puts "blank command for $cmd"}
3604        if {$action != ""} {
3605            pack [eval button .bar.$cmd -bg beige -activebackground yellow \
3606                    -text $cmd -command [list [subst $action]]] -side left
3607        }
3608    }
3609}
3610
3611$expgui(fm).file.menu add command -label "Exit"  -underline 1 -command catchQuit
3612foreach c {X x} {bind . <Alt-$c> [list catchQuit]}
3613#^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ END OF MENU DEFINITION ^^^^^^^^^^^^^^^^^^^
3614
3615# make the phase pane
3616MakePhasePane
3617# and the rest of the windows w/o BWidget
3618if !$expgui(haveBW) {
3619    MakeHistPane
3620    MakeScalingPane
3621    MakeProfilePane
3622}
3623
3624# handle indirect exits
3625wm protocol . WM_DELETE_WINDOW catchQuit
3626bind . <Control-c> catchQuit
3627
3628set expgui(curPhase) ""
3629set expgui(pagenow) ""
3630set expgui(curhist) {}
3631set expgui(selectedatomlist) {}
3632
3633loadexp $expgui(expfile)
3634
3635# select the 1st phase
3636SelectOnePhase [lindex $expmap(phaselist) 0]
3637# select the first histogram in the list by default (if there are any)
3638if {[llength $expmap(histlistboxcontents)] > 0} {
3639    set expgui(curhist) 0
3640} else {
3641    set expgui(curhist) {}
3642}
3643
3644# execute any local commands for final initialization
3645eval $expgui(initstring)
3646
3647# resize the notebook to fit all the tabs and the largest page
3648ResizeNotebook
3649
3650RaisePage lsFrame
3651if {[CountHistory] > 100} {
3652    DeleteHistoryRecords "This .EXP file has [CountHistory] history records\nErasing most will speed EXPGUI"
3653}
Note: See TracBrowser for help on using the repository browser.