source: trunk/expgui @ 181

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

# on 2000/06/01 02:48:14, toby did:
define default txtview(font)

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