source: trunk/expgui @ 262

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

# on 2000/08/14 16:28:34, toby did:
eliminate phase flag box if only 1 phase is present

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