source: trunk/expgui @ 323

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

# on 2000/10/12 21:40:40, toby did:
implement optional EXPTOOL output
Change action for unsaved changes in whenidle to Save As
Add help pointers
Exchange locations of Replace Phase & Add Phase buttons so that "Add Phass"
is always displayed
new WWW help commands

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