source: branches/sandbox/expgui @ 1172

Last change on this file since 1172 was 1172, checked in by toby, 12 years ago

fix/enhance rigid bodies some

  • Property svn:keywords set to Author Date Revision Id
File size: 164.5 KB
Line 
1#!/bin/sh
2# the next line restarts this script using wish found in the path\
3exec wish "$0" "$@"
4# If this does not work, change the #!/usr/bin/wish line below
5# to reflect the actual wish location and delete all preceeding lines
6#
7# (delete here and above)
8#!/usr/bin/wish
9# $Id: expgui 1172 2011-09-26 22:37:47Z toby $
10set expgui(Revision) {$Revision: 1172 $ $Date: 2011-09-26 22:37:47 +0000 (Mon, 26 Sep 2011) $}
11
12package require Tk
13
14# to do:
15#
16# need to change heading and button label depending on where getExpFileName
17# is called from?
18#
19# global background editing & profile work differently: should both
20# start out blank with a "load from option"?
21#
22# idea:
23# a scroll list for all histogram refinement flags ; click on takes you to the
24# appropriate menu.
25#
26# idea:
27#   change cell parameters to labels and have a edit cell button
28#   that enforces metric symmetry
29#
30# to allow "global" access on phase page
31#   change buttons from radio to multiple
32#   -- or display all 9 cell flag/damps and all atoms
33#   make editMultipleRecords work with multiple phases, also cell flag/damp
34#   blank cell entries
35#   add phase to atom number in listing
36#   DisplayAllAtoms needs to loop over phases
37#
38# idea: load more than one bank from a multi-bank .RAW file
39#
40if {$tcl_version < 8.0} {
41    tk_dialog .expFileErrorMsg "Version Error" \
42            "EXPGUI requires Tcl/Tk version 8.0 or higher" error 0 "Exit"
43    exit
44}
45
46# the shell variable tells expgui to act as a shell for GSAS
47# as well as edit the EXP file. In no-shell mode, it can
48# only be used to edit the .EXP file as a callable program
49set expgui(shell) 1
50catch {if $env(EXPGUI_NOSHELL) {set expgui(shell) 0}}
51
52set expgui(expfile) {}
53if {$argv != ""} {
54    if {[string match *noshell* [string tolower $argv]]} {
55        # I doubt that noshell mode is used by anyone
56        set expgui(shell) 0
57        set expgui(expfile)  [lindex $argv 1]
58    } else {
59        set expgui(expfile)  [lindex $argv 0]
60    }
61}
62
63set expgui(curhist) {}
64set expmap(powderlist) {}
65set expgui(bkgcolor1) #fdf
66
67set expgui(debug) 0
68catch {if $env(DEBUG) {set expgui(debug) 1}}
69#set expgui(debug) 1
70
71# location for web pages, if not found locally
72set expgui(website) 11bm.xor.aps.anl.gov/expguidoc/
73# default for archive mode = on
74set expgui(archive) 1
75# default for autoexec load = off
76set expgui(autoexpload) 0
77# default for execprompt = on
78set expgui(execprompt) 1
79# default for autostart GRWND = off
80set expgui(autoGRWND) 0
81# by default expgui is iconified while GENLES, etc runs
82set expgui(autoiconify) 1
83# default for show EXPTOOL output = off
84set expgui(showexptool) 0
85# save the name of the wish executable
86set wishshell [info nameofexecutable] 
87# misc constants
88set txtvw(font) "Courier"
89set expgui(font) 14
90set liveplot(hst) 1
91set liveplot(legend) 1
92set expgui(filesort) 1
93set expgui(initstring) {}
94# use a separate window for DISAGL (default)
95set expgui(disaglSeparateBox) 1
96set expgui(DefaultPeakType) 0
97# default: keep current atoms when replacing a phase
98set expgui(DeleteAllAtoms) 0
99# flags for running POWPREF
100set expgui(needpowpref) 0
101set expgui(needpowpref_why) ""
102# on Mac associate a app with .EXP file (on by default)
103set expgui(MacAssignApp) 1
104#=============================================================================
105#----------------------------------------------------------------
106# where are we?
107set expgui(script) [info script]
108# translate links -- go six levels deep
109foreach i {1 2 3 4 5 6} {
110    if {[file type $expgui(script)] == "link"} {
111        set link [file readlink $expgui(script)]
112        if { [file  pathtype  $link] == "absolute" } {
113            set expgui(script) $link
114        } {
115            set expgui(script) [file dirname $expgui(script)]/$link
116        }
117    } else {
118        break
119    }
120}
121# fixup relative paths
122if {[file pathtype $expgui(script)] == "relative"} {
123    set expgui(script) [file join [pwd] $expgui(script)]
124}
125set expgui(script) [file normalize $expgui(script)]
126# save the package installation location; fix if possible, warn later (after
127# routines are sourced)
128set scriptdir [
129               set expgui(scriptdir) [file dirname $expgui(script) ]
130               ]
131
132if {[string first " " $expgui(scriptdir)] != -1} {
133    if {$tcl_platform(platform) == "windows"} {
134        # translate to the windows 8.3 version, if supported for this drive
135        set expgui(scriptdir) [file attributes $expgui(scriptdir) -shortname]
136    }
137}
138set expgui(gsasdir) [file dirname $expgui(scriptdir)]
139set expgui(gsasexe) [file join $expgui(gsasdir) exe]
140set expgui(docdir) [file join $expgui(scriptdir) doc]
141#----------------------------------------------------------------
142# use EXPGUI directory for packages
143lappend auto_path $expgui(scriptdir)
144#----------------------------------------------------------------
145source [file join $expgui(scriptdir) opts.tcl]
146# fetch EXP file processing routines
147source [file join $expgui(scriptdir) readexp.tcl]
148# commands for running GSAS programs
149source [file join $expgui(scriptdir) gsascmds.tcl]
150# contents of GSAS menus
151source [file join $expgui(scriptdir) gsasmenu.tcl]
152# commands for adding phases, histograms & atoms
153source [file join $expgui(scriptdir) addcmds.tcl]
154# commands for preferred orientation
155source [file join $expgui(scriptdir) orient.tcl]
156# setting data range/excluded regions
157source [file join $expgui(scriptdir) exclinit.tcl]
158# setup DISAGL viewer & editor
159source [file join $expgui(scriptdir) disagledit.tcl]
160source [file join $expgui(scriptdir) Geo_Viewer.tcl]
161#---------------------------------------------------------------------------
162# override options with locally defined values
163lappend filelist [file join $expgui(scriptdir) localconfig]
164if {$tcl_platform(platform) == "windows"} {
165    lappend filelist "c:/gsas.config"
166}
167lappend filelist [file join ~ .gsas_config]
168if {[catch {
169    foreach file $filelist {
170        if [file exists $file] {source $file}
171    }
172} errmsg]} {
173    set msg "Error reading file $file (aka [file nativename $file]): $errmsg"
174    MyMessageBox -parent . -title "Customize warning" \
175        -message $msg -icon warning -type Ignore -default ignore \
176        -helplink "expguierr.html Customizewarning"
177}
178# warn/stop on spaces in install location
179if {[string first " " $scriptdir] != -1} {
180    if {$tcl_platform(platform) == "windows"} {
181        # did the translation it work?
182        if {[string first " " $expgui(scriptdir)] != -1} {
183            update
184            MyMessageBox -parent . -title "Spaces in path" \
185                -message "You are using an install directory with a space in the name ($scriptdir) that on Windows that does not have a corresponding DOS name. Is this on a networked drive? This install location will not work for EXPGUI. Sorry." \
186                -icon warning -type Quit -default quit
187            exit
188        }
189    }
190    set warn 1
191    catch {set warn $expgui(warnonscriptspace)}
192    if $warn {
193        update
194        MyMessageBox -parent . -title "Good luck..." \
195            -message "You are using an install directory with a space in the name ($scriptdir) -- You may encounter bugs in EXPGUI that I am trying to fix. If so, please e-mail enough detail to Brian.Toby@ANL.gov so that I can try to reproduce it." \
196            -icon warning -type Continue -default continue
197        set expgui(warnonscriptspace) 0
198    }
199}
200
201if [catch {package require BWidget}] {
202    set msg "Error loading the BWidget package: This should not happen if you use the version of Tcl/Tk (tcl84+.exe) distributed with EXPGUI"
203    MyMessageBox -parent . -title "BWidget load error" \
204        -message $msg -icon error -type Error -default error \
205        -helplink "debug_tcltk.html"
206    destroy .
207}
208SetTkDefaultOptions $expgui(font)
209#---------------------------------------------------------------------------
210set expgui(resize) 0
211# platform-specific code
212if {$tcl_platform(platform) == "windows" \
213        && $tcl_platform(os) == "Windows 95"} {
214    if {[catch {package require winexec}] && \
215            [catch {package require winutils}]} {
216        MyMessageBox -parent . -title "WINEXEC Error" \
217            -message "Error -- Unable to load the WINEXEC or the WINUTILS package. This is needed in Win-95/-98/-me machines" \
218            -icon error -type Quit -default quit \
219            -helplink "expgui_Win_readme.html Winexec"
220        destroy .
221    }
222}
223if {$tcl_platform(platform) == "windows"} {
224    # check the path -- can DOS use it?
225    if {[string first {\\} $expgui(script) ] != -1} {
226        MyMessageBox -parent . -title "Networked Path" \
227                -message "Note -- You may have problems running EXPGUI/GSAS from a network drive. If you have errors, map $expgui(gsasdir) to a \"drive\" (like F:). (Use \"Map network drive\" to do this.)" \
228                -icon error -type {"Be brave"} -default "be brave" \
229                -helplink "expgui_Win_readme.html NetPath"
230    }
231    # test for error in COMSPEC environment variable setting
232    if {[catch {set env(COMSPEC)}]} { 
233        set env(COMSPEC) {c:\windows\system32\cmd.exe}
234    }
235    if {[auto_execok $env(COMSPEC)] == ""} {
236        set env(COMSPEC) {c:\windows\system32\cmd.exe}
237    }
238    if {[auto_execok $env(COMSPEC)] == ""} {
239        MyMessageBox -parent . -title "cmd.exe not found " \
240            -message "The Windows COMSPEC environment variable is not set correctly. It should point to a copy of cmd.exe (or command.com on -95/-98/-ME). EXPGUI also did not find the file in the most common location (c:\\windows\\system32\\cmd.exe). Please fix: EXPGUI cannot run GSAS programs without this." \
241            -icon "error" -type Sorry -default sorry
242#           -helplink "expguierr.html Customizewarning"
243        return
244    }
245    set expgui(exptool) [file join $expgui(gsasexe) exptool.exe]
246} else {
247    set expgui(exptool) [file join $expgui(gsasexe) exptool]
248    if {$tcl_platform(os) != "Darwin"} {
249        if [catch {set env(GSASBACKSPACE)}] {set env(GSASBACKSPACE) 1}
250    }
251}
252# find the pgplot directory
253if {[file exists [file join $expgui(gsasdir) pgl]]} {
254    set expgui(pgplotdir) [file join $expgui(gsasdir) pgl]
255} elseif {[file exists [file join $expgui(gsasdir) pgplot]]} {
256    set expgui(pgplotdir) [file join $expgui(gsasdir) pgplot]
257} else {
258    MyMessageBox -parent . -title "PGPLOT Error" \
259            -message "Warning -- Unable to find PGPLOT directory. GSAS graphics will not work without this. Is GSAS correctly installed?" \
260            -icon warning -type {"Limp Ahead"} -default "Limp Ahead" \
261            -helplink "expguierr.html NoPGPLOT"
262    set expgui(resize) 1
263    set expgui(pgplotdir) ""
264}
265
266# old code used to fix up bad distributions
267# # do we have a PGPLOT fonts file?
268# # if it is in the "wrong" place/name -- make it "right"
269# if {![file exists [file join $expgui(pgplotdir) grfont.dat]] && \
270#       [file exists [file join $expgui(gsasdir) fonts grfont.dat]]} {
271#     catch {file mkdir $expgui(pgplotdir)}
272#     file copy [file join $expgui(gsasdir) fonts grfont.dat] \
273#           [file join $expgui(pgplotdir)  grfont.dat]
274# }
275# # do we have a PGPLOT fonts file?
276# if {![file exists [file join $expgui(pgplotdir)  grfont.dat]] && \
277#       [file exists [file join $expgui(gsasdir) fonts pgfont.dat]]} {
278#     catch {file mkdir $expgui(pgplotdir)}
279#     file copy [file join $expgui(gsasdir) fonts pgfont.dat] \
280#           [file join $expgui(pgplotdir)  grfont.dat]
281# }
282# # do we have a PGPLOT fonts file?
283# if {![file exists [file join $expgui(pgplotdir)  grfont.dat]] && \
284#       [file exists [file join $expgui(pgplotdir)  pgfont.dat]]} {
285#     file copy [file join $expgui(pgplotdir)  pgfont.dat] \
286#           [file join $expgui(pgplotdir)  grfont.dat]
287# }
288
289if { $expgui(pgplotdir) != "" && \
290         ![file exists [file join $expgui(pgplotdir)  grfont.dat]]} {
291    MyMessageBox -parent . -title "PGPLOT Error" \
292            -message "Warning -- Unable to find file GRFONT.DAT in $expgui(pgplotdir). GSAS graphics will not work. Is GSAS correctly installed?" \
293            -icon warning -type {"Limp Ahead"} -default "Limp Ahead" \
294            -helplink "expguierr.html NoPGPLOT"
295    set expgui(resize) 1
296}
297#---------------------------------------------------------------------------
298if {$expgui(expfile) != ""} {
299    if {[string match -nocase {.o[0-9a-f][0-9a-f]} \
300             [file extension $expgui(expfile)]]} {
301        if {[file exists $expgui(expfile)]} {
302            # this is an archived file -- archive the current .EXP file
303            set expnam [file rootname $expgui(expfile)]
304            # get the last archived version
305            set lastf [lindex [lsort [glob -nocomplain $expnam.{O\[0-9A-F\]\[0-9A-F\]}]] end]
306            if {$lastf == ""} {
307                set num 01
308            } else {
309                regexp {.*\.O([0-9A-F][0-9A-F])$} $lastf a num
310                scan $num %x num
311                if {$num >= 255} {
312                    set num FF
313                } else {
314                    set num [string toupper [format %.2x [incr num]]]
315                }
316            }
317            catch {
318                set newfile $expnam.O$num
319                file rename -force $expnam.EXP $newfile
320                set fp [open $expnam.LST a+]
321                puts $fp "\n----------------------------------------------"
322                puts $fp "     Regressing to archive file [file tail $expgui(expfile)]"
323                puts $fp "     but first archiving [file tail $expnam.EXP] as [file tail $newfile]"
324                puts $fp "----------------------------------------------\n"
325                close $fp
326            }
327            file copy -force $expgui(expfile) $expnam.EXP
328            set expgui(expfile) $expnam.EXP
329            wm title . "EXPGUI interface to GSAS: [file tail $expgui(expfile)]"
330            set expgui(titleunchanged) 1
331        } else {
332            MyMessageBox -parent . -title "File not found" \
333                -message "Warning -- Archive file $expgui(expfile) was not found." \
334                -icon warning -type OK -default ok
335            set expgui(expfile) {}
336        }
337    } else {
338        set expgui(expfile) [SetEXPfile $expgui(expfile)]
339    }
340}
341if {$expgui(expfile) == ""} {
342    # place the parent window because the getExpFileName window will be centered above it.
343    set xpos 50
344    catch {set xpos $expgui(xpos)}
345    set ypos 50
346    catch {set ypos $expgui(ypos)}
347    set poscntr "nsew"
348    catch {set poscntr $expgui(poscenter)}
349    LocateWindow "." $xpos $ypos $poscntr
350    # windows needed this update before when using tk_getOpenFile.
351    # I am not sure it is still needed.
352    update
353    set expgui(expfile) [SetEXPfile [getExpFileName ""]]
354    set expgui(resize) 1
355}
356if {$expgui(expfile) == ""} exit
357
358#
359# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
360# <<<<<<<<<<    BEGINNING OF MAIN: GLOBAL AREA FOR DATA EXTRACTION >>>>>>>>>>>
361# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
362# load exp file and set up dialogs
363proc loadexp {expfile} {
364    global expgui expmap entryvar entrycmd tcl_platform
365    set expfile [SetEXPfile $expfile]
366    if {$expfile == ""} {
367        return
368    }
369    # change the icon and assign an app to this .EXP file
370    if {$tcl_platform(os) == "Darwin" && $expgui(MacAssignApp)} {
371        MacSetResourceFork $expfile
372    }
373    # read in the .EXP file
374    set fmt [expload $expfile]
375    set expgui(expfile) $expfile
376    # if the file was not in the correct format, force a rewrite before use
377    if {$fmt < 0} {
378        # read error
379        return
380    } elseif {$fmt == 1} {
381        set expgui(changed) 0
382    } else {
383        set expgui(changed) 1
384    }
385    mapexp
386    if {$expgui(MacroRunning)} {
387        set expgui(MacroStatus) "?"
388        catch {
389            set expgui(MacroStatus) \
390                "[expinfo cyclesrun] cycles; Chi2=$expgui(LastChi2); $expgui(LastnVars) vars"
391        }
392    }
393
394    set expgui(expModifiedLast) 0
395    catch {
396        set expgui(expModifiedLast) [file mtime $expgui(expfile)]
397    }
398    set expgui(last_History) [string range [string trim [lindex [exphistory last] 1]] 0 50 ]
399    # set the window/icon title
400    wm title . "EXPGUI interface to GSAS: [file tail $expgui(expfile)]"
401    set expgui(titleunchanged) 1
402    wm iconname . [file tail $expfile]
403
404    # load current information into EXPGUI pages
405    PaintEXPGUIpages
406
407    # start checking for external changes
408    afterawhile
409    # check if the file seems corrupt
410    set warnings [ScanEXPforError]
411    if {$warnings != ""} {
412        ExplainEXPerror . $warnings $expfile
413    }
414}
415
416# [re]load all screens with current state of EXPGUI file
417proc PaintEXPGUIpages {} {
418    global expgui expmap entryvar entrycmd
419    # reset the phase buttons
420    set expgui(curPhase) ""
421    # set the number of phases on the phase page
422    setphases
423
424    # disable the "global options" that don't make sense based on
425    # which histograms present
426    foreach num {1 2 3 4 5} {
427        set flag($num) 0
428    }
429    # save a list of the allowed modes, too
430    set expgui(AllowedHistSelectModes) {0 6}
431    foreach h $expmap(powderlist) {
432        if {[string range $expmap(htype_$h) 2 2] == "T"} {set flag(1) 1}
433        if {[string range $expmap(htype_$h) 1 2] == "NC"} {set flag(2) 1}
434        if {[string range $expmap(htype_$h) 1 2] == "XC" && \
435                [histinfo $h lam2] != 0.0} {set flag(3) 1}
436        if {[string range $expmap(htype_$h) 1 2] == "XC" && \
437                [histinfo $h lam2] == 0.0} {set flag(4) 1}
438        if {[string range $expmap(htype_$h) 1 2] == "XE"} {set flag(5) 1}
439    }
440    foreach num {1 2 3 4 5} \
441            lbl {TOF "CW Neutron" "Alpha12 Xray" "Monochromatic Xray" \
442            "Energy Disp Xray"} {
443        if $flag($num) {
444            $expgui(fm).option.menu.editmode entryconfigure $lbl -state normal
445            lappend expgui(AllowedHistSelectModes) $num
446        } else {
447            $expgui(fm).option.menu.editmode entryconfigure $lbl -state disabled
448        }
449    }
450    # disable traces on entryvar until we are ready
451    set entrycmd(trace) 0
452    trace vdelete entryvar w entvartrace
453
454    # propogate changes on the least squares page
455    set entryvar(cycles) [expinfo cycles]
456    set entrycmd(cycles) "expinfo cycles"
457    set entryvar(mbw) [expinfo mbw]
458    set entrycmd(mbw) "expinfo mbw"
459    # set expgui(globalmode) 0
460    set expgui(printopt) "Print Options ([expinfo print])"
461    set entryvar(title) [expinfo title]
462    global printopts
463    foreach num [array names printopts] {
464        set entrycmd(printopt$num) "printsetting $num"
465        set entryvar(printopt$num) [printsetting $num]
466    }
467    # enable traces on entryvar
468    set entrycmd(trace) 1
469    trace variable entryvar w entvartrace
470
471    # set fo extraction on LS page
472    SetupExtractHist
473    # set convergence criterion
474    InitLSvars
475
476    # update the histogram list & update the page
477    sethistlist
478}
479
480# called to reread the .EXP file
481proc rereadexp {expfile} {
482    global expgui
483    if $expgui(changed) {
484        set decision [tk_dialog .instrSaveData {Save .EXP changes} \
485                {You have made changes to the Experiment. Rereading will cause the changes to be lost. Select an option:} \
486                {} 0 "Save and reread" "Reread without Save" "Cancel reread command"]
487        switch $decision {
488            0 { savearchiveexp }
489            1 { }
490            2 { return }
491        }
492    }
493    loadexp $expgui(expfile)
494}
495
496proc SaveAsFile {} {
497    global expgui
498    global tcl_platform
499    set newexpfile [SetEXPfile [getExpFileName new] 1]
500    if {$newexpfile == ""} return
501    expwrite $newexpfile
502    set expgui(expfile) $newexpfile
503    # change the icon and assign an app to this .EXP file
504    if {$tcl_platform(os) == "Darwin" && $expgui(MacAssignApp)} {
505        MacSetResourceFork $expgui(expfile)
506    }
507    set expgui(changed) 0
508    set expgui(expModifiedLast) [file mtime $expgui(expfile)]
509    set expgui(last_History) [string range [string trim [lindex [exphistory last] 1]] 0 50 ]
510    # set the window/icon title
511    wm title . $expgui(expfile)
512    set expgui(titleunchanged) 1
513    wm iconname . [file tail $expgui(expfile)]
514    # set convergence criterion
515    InitLSvars
516    set expgui(needpowpref) 2
517    set expgui(needpowpref_why) "\tA new .EXP file was created\n" 
518}
519
520# called to read a different .EXP file
521proc readnewexp {"mode 0"} {
522    global expgui expmap
523    if $expgui(changed) {
524        set decision [tk_dialog .instrSaveData {Save .EXP changes} \
525                {You have made changes to the Experiment. Reading a different file without first saving will cause the changes to be lost. Select an option:} \
526                {} 0 "Save and read" "Read without Save" "Cancel read command"]
527        switch $decision {
528            0 { savearchiveexp }
529            1 {                }
530            2 { return }
531        }
532    }
533    if {$mode == 0} {
534        set newexpfile [getExpFileName old]
535    } else {
536        set newexpfile [RevertExpFile]
537    }
538    if {$newexpfile == ""} return
539
540    set expgui(globalmode) 0
541    loadexp $newexpfile
542
543    # switch to the appropriate 1st page
544    #StageTabUse
545    SetStartPanel
546
547    # reset the phase selection
548    set expgui(curPhase) {}
549
550    # select the first histogram in the list by default (if there are any)
551    if {[llength $expmap(histlistboxcontents)] > 0} {
552        set expgui(curhist) 0
553    } else {
554        set expgui(curhist) {}
555    }
556#    if {[CountHistory] > 100} {
557#       DeleteHistoryRecords "This .EXP file has [CountHistory] history records\nErasing most will speed EXPGUI"
558#    }
559}
560
561#------------- set up data read/write layer ----------------------
562# trace routine on entryvar
563proc entvartrace {array elem action} {
564    global expgui entrycmd entryvar entrybox
565    if !$entrycmd(trace) return
566   
567    catch {
568        if {$entrycmd($elem) == ""} return
569        incr expgui(changed)
570        if $expgui(debug) {puts "$entrycmd($elem)  set $entryvar($elem) "}
571        if {$entrycmd($elem) == ""} return
572        if [catch {
573            set result [eval $entrycmd($elem) set [list $entryvar($elem)]]
574            if {!$result} {
575                if $expgui(debug) {puts "error with $entrycmd($elem)"}
576                catch {$entrybox($elem) config -fg red}
577            } else {
578                catch {$entrybox($elem) config -fg black}
579                RecordMacroEntry "incr expgui(changed); $entrycmd($elem) set [list $entryvar($elem)]" 1
580            }
581            if {[string match "*atominfo" [lindex $entrycmd($elem) 0]]} {
582                after idle "UpdateAtomLine \
583                        [list [lindex $entrycmd($elem) 2]] \
584                        [lindex $entrycmd($elem) 1]"
585            }
586        } errmsg] {error $errmsg}       
587    }
588}
589
590# disable traces on entryvar until we are ready
591set entrycmd(trace) 0
592trace variable entryvar w entvartrace
593
594#
595#
596#
597##############################################################################
598#####                    #####################################################
599##### PROCEDURES SECTION #####################################################
600#####                    #####################################################
601##############################################################################
602
603# save some of the global options in ~/.gsas_config or ~/gsas.config in Windows
604proc SaveOptions {} {
605    global expgui env tcl_platform graph peakinfo
606    if {$tcl_platform(platform) == "windows"} {
607        set fp [open c:/gsas.config a]
608    } else {
609        set fp [open [file join ~ .gsas_config] a]
610    }
611
612    puts $fp "# EXPGUI saved options from [clock format [clock seconds]]"
613    set itemlist {archive asorttype hsorttype filesort disaglSeparateBox \
614        font autoexpload autoiconify autotick execprompt ShowGENLES}
615    if {$tcl_platform(os) == "Darwin"} {
616        lappend itemlist MacAssignApp
617    }
618    if {$tcl_platform(platform) == "windows" && \
619            $tcl_platform(os) == "Windows 95"} {
620        lappend itemlist autoGRWND
621    }
622    foreach item $itemlist {
623        puts $fp "set expgui($item) [list $expgui($item)]"
624    }
625    if {$tcl_platform(platform) != "windows"} {
626        puts $fp "set env(GSASBACKSPACE) [list $env(GSASBACKSPACE)]"
627    }
628    foreach v {printout legend outname outcmd autoraise color_excl \
629            color_obs color_calc} {
630        puts $fp "set graph($v) [list $graph($v)]"
631    }
632    foreach v {obssym obssize exclsym exclsize} {
633        puts $fp "set peakinfo($v) [list $peakinfo($v)]"
634    }
635    close $fp
636}
637
638# save some of the global options in ~/.gsas_config or ~/gsas.config in Windows
639proc SavePosition {} {
640    global expgui env tcl_platform graph peakinfo
641    if {$tcl_platform(platform) == "windows"} {
642        set fp [open c:/gsas.config a]
643    } else {
644        set fp [open [file join ~ .gsas_config] a]
645    }
646
647    puts $fp "# EXPGUI saved screen position on [clock format [clock seconds]]"
648    puts $fp "set expgui(xpos) [expr {[winfo x .]*100./[winfo screenwidth .]}]"
649    puts $fp "set expgui(ypos) [expr {[winfo y .]*100./[winfo screenheight .]}]"
650    puts $fp "set expgui(poscenter) NW"
651    close $fp
652}
653
654proc About { } {
655    global expgui expmap
656    set version "Revision [lindex $expgui(Revision) 1] (readexp.tcl [lindex $expmap(Revision) 1])"
657    catch {
658        set version "[GetSVNVersion $::expgui(gsasdir)]\ntrack: $::expgui(SVNversion)"
659    }
660    tk_dialog .about {About...} \
661"EXPGUI\n\
662Brian Toby\n\
663APS, Argonne National Laboratory\n\n\
664Not subject to copyright\n\n\
665$version\n\n\
666Cite: B. H. Toby, EXPGUI, a graphical\n\
667user interface for GSAS, J. Appl. Cryst.\n\
66834, 210-21 (2001).
669\n\n\
670Generalized Structure Analysis System\n(GSAS)\n\
671R. B. Von Dreele,\n
672APS, Argonne National Laboratory\n
673and A. C. Larson, Los Alamos (retired)\n\n\
674" \
675        info 0 OK
676}
677proc Cite { } {
678    global expgui expmap
679    tk_dialog .about {Citations...} \
680"If you use EXPGUI, please cite\n\n\
681B.H. Toby, EXPGUI, a graphical\n\
682user interface for GSAS, J. Appl. Cryst.\n\
68334, 210-21 (2001).\n\n\
684as well as\n\n\
685A.C. Larson and R.B. Von Dreele,\n\
686\"General Structure Analysis System (GSAS)\",\n\
687Los Alamos National Laboratory Report\n\
688LAUR 86-748 (2004)." \
689        info 0 OK
690}
691
692# this proc is no longer called, but I am leaving it here as it may
693# be of use in the future
694proc MakeAppleScript {} {
695    global wishshell expgui
696    # create a local script directory, if it does not exist
697    if {![file exists ~/Library/Scripts]} {file mkdir  ~/Library/Scripts}
698    set tmpfile [file nativename ~/tmpscriptfile]
699    set startdir [tk_chooseDirectory -initialdir [pwd] -mustexist 1 \
700                      -title "Choose GSAS starting directory"]
701    if {$startdir == ""} {set startdir "~"}
702    set dir [file nativename  ~/Library/Scripts]
703    if {[set xterm [auto_execok xterm]] == ""} {
704        MyMessageBox -parent . -title "xterm not found " \
705            -message "The AppleScript could not be created because the X11 xterm application was not found. Please correct your path and try again." \
706            -icon "error" -type Sorry -default sorry
707#           -helplink "expguierr.html Customizewarning"
708        return
709    }
710    set file [tk_getSaveFile -initialdir $dir -initialfile EXPGUI.scpt \
711                      -title "Choose location to save script"]
712    set path {$PATH:}
713    append path [file dirname $xterm]
714    set fp [open $tmpfile w]
715    # the applescript starts here
716    puts $fp {on run}
717    puts $fp {  tell application "Finder"}
718    puts $fp {     launch application "X11"}
719    puts $fp {   end tell}
720    puts $fp "  set results to do shell script \"cd $startdir; DISPLAY=:0.0 PATH=$path $wishshell $expgui(script)  > /dev/null 2>&1 &\""
721    puts $fp {end run}
722    # drag & drop
723    puts $fp {on open these_files}
724    puts $fp {  tell application "Finder"}
725    puts $fp {     launch application "X11"}
726    puts $fp {   end tell}
727    puts $fp {  repeat with this_file in these_files}
728    puts $fp "  set results to do shell script \"cd $startdir; DISPLAY=:0.0 PATH=$path $wishshell $expgui(script) \" & the quoted form of the POSIX path of this_file & \" > /dev/null 2>&1 &\""
729    puts $fp {  end repeat}
730    puts $fp {end open}
731    close $fp
732    if {[catch {
733        exec osacompile -l AppleScript -o $file  $tmpfile
734        file delete -force $tmpfile
735        MyMessageBox -parent . -title "AppleScript created" \
736            -message "Script $file created & compiled. You may wish to use the Script Editor to save this as an application" \
737            -icon "info" -type OK -default ok \
738            -helplink "osx.html CompileAppleScript"
739    } errmsg]} {
740        MyMessageBox -parent . -title "AppleScript warning" \
741        -message "An error occurred while attempting to create the script. Please report this bug, including these details:\n$errmsg"\
742            -icon warning -type Ignore -default ignore
743#       -helplink "expguierr.html Customizewarning"
744    }
745}
746
747# this proc gets called when the export coordinate button is pressed
748# it loads export
749proc BuildCoordExpMenu {menu} {
750    global expgui_cmdlist expgui
751    # do this only once
752    $menu config -postcommand {}
753    $menu delete 1 end
754    # add the cif export routine
755    set cmd gsas2cif
756    set action {}
757    catch {set action [lindex $expgui_cmdlist($cmd) 0]}
758    if {$action != "" && $action != "-"} {
759        $menu add command -label $cmd -command [subst $action]
760    }
761    # get a list of files to read
762    set filelist [glob -nocomplain [file join $expgui(scriptdir) export_*.tcl]]
763    foreach file $filelist {
764        source $file
765        $menu add command -label $label -command $action
766    }
767}
768
769# utility export routines for the export_*.tcl files:
770# make a box for export
771proc MakeExportBox {win title webref} {
772    global expmap expgui
773    catch {destroy $win}
774    toplevel $win
775    wm title $win "Export coordinates"
776    if {$webref != ""} {
777        bind $win <Key-F1> $webref
778    }
779    pack [label $win.lbl -text $title] -side top -anchor center
780    pack [frame $win.ps] -side top -anchor w
781    pack [label $win.ps.lbl -text "Select phase: "] -side left
782    foreach num $expmap(phaselist) {
783        pack [button $win.ps.$num -text $num \
784                    -command "SetExportPhase $num $win"] -side left
785    }
786    # leave a place for format-specific items
787    pack [frame $win.special] -side top
788    pack [frame $win.but] -side top -fill x -expand yes
789    pack [button $win.but.1 -text Write -command "destroy $win"] -side left
790    SetExportPhase [lindex $expmap(phaselist) 0] $win
791    pack [button $win.but.2 -text Cancel \
792            -command "set expgui(export_phase) 0;destroy $win"] -side left
793    pack [button $win.but.help -text Help -bg yellow \
794            -command "MakeWWWHelp expgui.html ExportMSI"] \
795            -side right
796}
797
798# set the phase in response to the button
799proc SetExportPhase {num win} {
800    global expmap expgui
801    foreach n $expmap(phaselist) { 
802        if {$n == $num} { 
803            $win.ps.$n config -relief sunken; 
804            set expgui(export_phase) $num 
805        } else { 
806            $win.ps.$n config -relief raised
807        }
808    }
809}
810
811# wait until idle
812proc afterawhile {} {
813    # cancel any other instances of this loop
814    after cancel afterawhile
815    after cancel whenidle
816    after cancel whenidle
817    after idle whenidle
818}
819
820# This is called every 2 seconds to check for changes to the .EXP file
821proc whenidle {} {
822    global expgui tcl_platform
823    if $expgui(titleunchanged) {
824        if {$expgui(changed) != 0} {
825            wm title . "EXPGUI interface to GSAS: $expgui(expfile) (modified)"
826            set expgui(titleunchanged) 0
827        }
828    }
829    if {$expgui(expModifiedLast) == 0} {
830        after 2000 afterawhile
831        return
832    }
833    if {![file exists $expgui(expfile)]} {
834        after 2000 afterawhile
835        return
836    }
837    if {[file mtime $expgui(expfile)] != $expgui(expModifiedLast)} {
838        # we are "locked". Note that whenidle loop will be restarted later
839        if {$tcl_platform(platform) == "windows" && [file exists expgui.lck]} {
840            return
841        }
842        set ans [ReloadExpMsg [file tail $expgui(expfile)] $expgui(changed)]
843
844        if {$ans == 0} {
845            loadexp $expgui(expfile)
846        } elseif {$ans == 1} {
847            # reset the time to the next version
848            set expgui(expModifiedLast) [file mtime $expgui(expfile)]
849        } elseif {$ans == 2} {
850            SaveAsFile
851        }
852    }
853    after 2000 afterawhile
854}
855
856# place a message about changes over the main window
857proc ReloadExpMsg {file changes} {
858    global expgui tcl_platform
859    set msg "File $file has been modified by another program"
860    if {$changes == 1} {
861        append msg " and you have made a change to this version.\n"
862    } elseif {$changes > 0} {
863        append msg " and you have made $changes changes to this version.\n"
864    } else {
865        append msg ".\n"
866    }
867    append msg "Do you want to use the newer (modified) version or continue with the older (previous) version of the file?"
868
869    set w .ask
870    catch {destroy $w}
871    toplevel $w -class Dialog
872    wm title $w "Reload?"
873    wm iconname $w "Reload?"
874    wm protocol $w WM_DELETE_WINDOW { }
875    wm transient $w .
876    bind $w <Key-F1> "MakeWWWHelp expguierr.html Overwrite"
877    pack [button $w.help -text Help -bg yellow \
878            -command "MakeWWWHelp expguierr.html Overwrite"] \
879            -side top -anchor e
880    frame $w.bot
881    pack $w.bot -side bottom
882    frame $w.top -class FixedFont
883    pack $w.top -side top -fill both -expand 1
884    label $w.top.msg -justify left \
885            -wraplength 5i -font {Times 18} \
886            -text $msg 
887    if {$tcl_platform(platform) == "windows"} {
888        $w.top.msg config -font {Times 14}
889    }
890    pack $w.top.msg  -side right -expand 1 -fill both -padx 3m -pady 3m
891    pack [button $w.bot.1 -text "Load new" \
892            -default active -command "set expgui(dialogbutton) 0" \
893            ] -side left -expand 1 -padx 3m -pady 2m
894    pack [button $w.bot.2 -text "Continue with old" \
895            -command "set expgui(dialogbutton) 1"] \
896            -side left -expand 1 -padx 3m -pady 2m
897    if {$changes > 0} {
898        pack [button $w.bot.3 -text "Save edited version" \
899            -command "set expgui(dialogbutton) 2"] \
900            -side left -expand 1 -padx 3m -pady 2m
901    }
902    # Create a binding for <Return> on the dialog
903    bind $w <Return> "$w.bot.1 invoke"
904    wm withdraw $w
905    update idletasks
906
907    # compute widths with a catch, since this has been showing an error
908    # at least for one Mac user
909    set askwid 40
910    catch {set askwid [winfo reqwidth $w]}
911    set askhgt 80
912    catch {set askhgt [winfo reqheight $w]}
913    catch {
914        # for now, always center the message over the main window
915        # center the new window in the middle of the parent
916        set x [expr [winfo x .] + [winfo width .]/2 - \
917                $askwid/2 - [winfo vrootx .]]
918        set y [expr [winfo y .] + [winfo height .]/2 - \
919                $askhgt/2 - [winfo vrooty .]]
920        wm geom $w +$x+$y
921    }
922    wm deiconify $w
923
924    # Grab the focus
925    set oldFocus [focus]
926    set oldGrab [grab current $w]
927    if {[string compare $oldGrab ""]} {
928        set grabStatus [grab status $oldGrab]
929    }
930    catch {grab $w}
931    focus $w.bot.1
932    # for windows rearrange window stacking
933    # -- Removed since this will normally happen after the GSAS program
934    # has finished
935    #if {$tcl_platform(platform) == "windows"} {
936        #lower .
937        #raise $w .
938    #}
939    update idletasks
940
941    tkwait variable expgui(dialogbutton)
942    catch {focus $oldFocus}
943    destroy $w
944    if {[string compare $oldGrab ""]} {
945        if {![string compare $grabStatus "global"]} {
946            catch {grab -global $oldGrab}
947        } else {
948            catch {grab $oldGrab}
949        }
950    }
951    # for windows rearrange window stacking
952    #if {$tcl_platform(platform) == "windows"} {
953        #raise .
954    #}
955    return $expgui(dialogbutton)
956}
957
958# --------  called to confirm before exiting
959proc catchQuit {} {
960    if {[confirmBeforeSave] == "Continue"} {
961        destroy .
962    }
963}
964# save the .EXP file before exiting?
965proc confirmBeforeSave {} {
966    global expgui
967    if !$expgui(changed) {
968        return "Continue"
969    }
970    set decision [tk_dialog .instrSaveData {Save .EXP changes} \
971            {You have made changes to the Experiment, but the changes are not saved. Select an option:} \
972            {} 0 "Save and Exit" "Exit without Save" "Cancel exit command"]
973    switch $decision {
974        0 { savearchiveexp;  return "Continue" }
975        1 {                  return "Continue" }
976        2 {                  return "Cancel"   }
977    }
978}
979
980# setup buttons for each phase on the phase page
981proc setphases {} {
982    global expgui expmap
983    eval destroy [winfo children $expgui(phaseFrame).top.ps]
984    pack [label $expgui(phaseFrame).top.ps.0 -text Phase:] -side left
985    foreach num $expmap(phaselist) {
986        pack [button $expgui(phaseFrame).top.ps.$num -text $num \
987                -command "SelectOnePhase $num" -padx 1.5m] -side left
988    }
989    if {[file executable $expgui(exptool)] && \
990            [llength $expmap(phaselist)]} {
991        pack [button $expgui(phaseFrame).top.ps.10 \
992                -text "Replace" -command MakeReplacePhaseBox \
993                ] -side left
994    }
995}
996
997# Procedure to respond to changes the phase.
998#  This loads the "phases" widgets with data corresponding to the selected phase.
999proc SelectOnePhase {num} {
1000    global entryvar entrycmd entrybox expmap expgui
1001    # if no phase has been selected, select the first one
1002    if {$num == ""} {set num [lindex $expmap(phaselist) 0]}
1003
1004    set crsPhase {}
1005    $expgui(atomxform) config -text "Modify Atoms" -state disabled
1006    foreach n $expmap(phaselist) type $expmap(phasetype) {
1007        if {$n == $num} {
1008            catch {$expgui(phaseFrame).top.ps.$num config -relief sunken}
1009            set crsPhase $num
1010            if {$type == 3} {
1011                set expgui(phasetype) "Magnetic\nOnly"
1012            } elseif {$type == 2} {
1013                set expgui(phasetype) "Magnetic\n& Nuclear"
1014            } elseif {$type == 4} {
1015                set expgui(phasetype) "Macromolecular"
1016            } elseif {$type == 10} {
1017                set expgui(phasetype) "Pawley"
1018            } else {
1019                set expgui(phasetype) ""
1020            }
1021        } else { 
1022            catch {$expgui(phaseFrame).top.ps.$n config -relief raised}
1023        }
1024    }
1025    # no phase is selected
1026    if {$crsPhase == "" || [llength $expmap(phaselist)] == 0} {
1027        # disable traces on entryvar
1028        set entrycmd(trace) 0
1029        set entrycmd(phasename) ""
1030        set entryvar(phasename) ""
1031        foreach ent {a b c alpha beta gamma} {
1032            set entryvar($ent) ""
1033        }
1034        foreach ent {cellref celldamp} {
1035            set entrycmd($ent) ""
1036            set entryvar($ent) ""
1037        }
1038        set expgui(curPhase) {}
1039        # enable traces on entryvar
1040        set entrycmd(trace) 1
1041        $expgui(EditingAtoms) config -text ""
1042        DisplayAtom 0 0
1043        DisplayU 0 0
1044        DisplayRefFlags 0 0
1045        $expgui(atomlistbox) delete 0 end
1046        $expgui(AddAtomBut)  config -state disabled
1047        return
1048    }
1049
1050    # don't reload the last displayed phase
1051    if {$expgui(curPhase) == $crsPhase} {
1052       DisplayAllAtoms $crsPhase
1053       return
1054    }
1055
1056    ##########################################################
1057    # load and display a phase
1058    ##########################################################
1059    # disable traces on entryvar while loading
1060    set entrycmd(trace) 0
1061    # phase title
1062    set entrycmd(phasename) "phaseinfo $crsPhase name"
1063    set entryvar(phasename) [phaseinfo $crsPhase name]
1064    # cell parameters & flags
1065    foreach ent {a b c alpha beta gamma} {
1066        set entryvar($ent) [phaseinfo $crsPhase $ent]
1067    }
1068    foreach ent {cellref celldamp} {
1069        set entrycmd($ent) "phaseinfo $crsPhase $ent"
1070        set entryvar($ent) [phaseinfo $crsPhase $ent]
1071    }
1072
1073    # initialize atoms display & disable
1074    DisplayAtom 0 0
1075    DisplayU 0 0
1076    DisplayRefFlags 0 0
1077    $expgui(EditingAtoms) config -text ""
1078    $expgui(AddAtomBut)  config -state normal
1079
1080    DisplayAllAtoms $crsPhase
1081
1082    # enable traces on entryvar now
1083    set entrycmd(trace) 1
1084}
1085
1086set expgui(noreenterDisplayAllAtoms) 0
1087# Populate expgui(atomlistbox) (a ScrolledListBox) with atoms
1088# from the selected phase.
1089proc DisplayAllAtoms {curPhase "mode reset"} {
1090    global entryvar entrycmd expmap expgui
1091    # make sure that atomlistboxcontents element exists
1092    if {[catch {set expmap(atomlistboxcontents)}]} {
1093        set expmap(atomlistboxcontents) {}
1094    }
1095    # if it does not show, we don't have a phase or we are already displaying
1096    # don't bother
1097    if {$expgui(pagenow) != "phaseFrame"} return
1098    if {$curPhase == ""} return
1099    if $expgui(noreenterDisplayAllAtoms) return
1100    # prevent reentry
1101    set expgui(noreenterDisplayAllAtoms) 1
1102    # set the current phase
1103    set expgui(curPhase) $curPhase
1104    if {$mode != "reset"} {
1105        # save the scrolled position
1106        set pos [lindex [$expgui(atomlistbox) yview] 0]
1107    } else {
1108        # for reset, do not keep the previously selected atoms
1109        set expgui(selectedatomlist) {}
1110    }
1111    $expgui(atomlistbox) delete 0 end
1112    # displaying a macromolecular phase?
1113    if {[lindex $expmap(phasetype) [expr {$expgui(curPhase) - 1}]] == 4} {
1114        set mm 1
1115        $expgui(phaseFrame).top.ps.10 config -state disabled
1116        $expgui(AddAtomBut)  config -state disabled
1117        pleasewait "loading atoms..."
1118    } else {
1119        set mm 0
1120        if {[file executable $expgui(exptool)]} {
1121            $expgui(phaseFrame).top.ps.10 config -state normal
1122            $expgui(AddAtomBut) config -state normal
1123        }
1124    }
1125
1126    # prepare header info
1127    set maxline I
1128    set phase $expgui(curPhase)
1129    set atomlist {}
1130    set typehead "type  "
1131    set namehead "  name  "
1132    set multhead "Mult"
1133    set coordhead "   "
1134    if {$mm} {
1135        set cmd mmatominfo
1136        set frachead "Occ."
1137    } else {
1138        set cmd atominfo
1139        set frachead "Occupancy"
1140    }
1141    set reshead "res/grp/#"
1142    # sort the atoms, as requested
1143    if  {$expgui(asorttype) == "type"} {
1144        # sort on atom type
1145        set typehead "type* "
1146        foreach atom $expmap(atomlist_$phase) {
1147            lappend atomlist "$atom [$cmd $phase $atom type] $phase"
1148        }
1149        set expmap(atomlistboxcontents) [lsort -ascii -index 1 $atomlist]
1150    } elseif {$expgui(asorttype) == "number"} {
1151        # sort on atom number
1152        set namehead "* name  "
1153        foreach atom $expmap(atomlist_$phase) {
1154            lappend atomlist "$atom $atom $phase"
1155        }
1156        set expmap(atomlistboxcontents) [lsort -integer -index 1 $atomlist]
1157    } elseif {$expgui(asorttype) == "mult"} {
1158        if {$mm} {
1159            set reshead "res*/grp/#"
1160            foreach atom $expmap(atomlist_$phase) {
1161                lappend atomlist "$atom [mmatominfo $phase $atom residue] $phase"
1162            }
1163            set expmap(atomlistboxcontents) [lsort -ascii -index 1 $atomlist]
1164        } else {
1165            # sort on atom number
1166            set multhead "Mlt*"
1167            foreach atom $expmap(atomlist_$phase) {
1168                lappend atomlist "$atom [atominfo $phase $atom mult] $phase"
1169            }
1170            set expmap(atomlistboxcontents) [lsort -integer -decreasing -index 1 $atomlist]
1171        }
1172    } elseif {$expgui(asorttype) == "occupancy"} {
1173        # sort on atom number
1174        if {$mm} {
1175            set frachead "  Occ* "
1176        } else {
1177            set frachead "  Occup* "
1178        }
1179        foreach atom $expmap(atomlist_$phase) {
1180            lappend atomlist "$atom [$cmd $phase $atom frac] $phase"
1181        }
1182        set expmap(atomlistboxcontents) [lsort -real -decreasing -index 1 $atomlist]
1183    } elseif {$expgui(asorttype) == "x"} {
1184        # sort on x
1185        set coordhead "(x*)"
1186        foreach atom $expmap(atomlist_$phase) {
1187            lappend atomlist "$atom [$cmd $phase $atom x] $phase"
1188        }
1189        set expmap(atomlistboxcontents) [lsort -real -index 1 $atomlist]
1190    } elseif {$expgui(asorttype) == "y"} {
1191        # sort on y
1192        set coordhead "(y*)"
1193        foreach atom $expmap(atomlist_$phase) {
1194            lappend atomlist "$atom [$cmd $phase $atom y] $phase"
1195        }
1196        set expmap(atomlistboxcontents) [lsort -real -index 1 $atomlist]
1197    } elseif {$expgui(asorttype) == "z"} {
1198        # sort on z
1199        set coordhead "(z*)"
1200        foreach atom $expmap(atomlist_$phase) {
1201            lappend atomlist "$atom [$cmd $phase $atom z] $phase"
1202        }
1203        set expmap(atomlistboxcontents) [lsort -real -index 1 $atomlist]
1204    } else {
1205        error "Bad expgui(asorttype) = $expgui(asorttype)"
1206    }
1207
1208    set expgui(atomlistboxline) {}
1209    # loop over atoms
1210    foreach tuple $expmap(atomlistboxcontents) {
1211        set atom [lindex $tuple 0]
1212        set phase [lindex $tuple 2]
1213        lappend expgui(atomlistboxline) $atom
1214        $expgui(atomlistbox) insert end \
1215                [FormatAtomLine $atom $phase maxline]
1216    }
1217    $expgui(atomtitle) delete 0 end
1218
1219    # create the header
1220    if {$mm} {
1221        $expgui(atomtitle) insert end [format "%12s %9s  %6s %8s%29s %4s  %s" \
1222                $namehead $reshead $typehead "ref/damp  " \
1223                "fractional coordinates$coordhead" \
1224                "$frachead" \
1225                " Uiso"]
1226        donewait
1227    } elseif {$maxline == "A"} {
1228        $expgui(atomtitle) insert end [format "%10s %6s %8s%29s %9s  %s" \
1229                $namehead $typehead "ref/damp  " \
1230                "fractional coordinates$coordhead" \
1231                "$multhead $frachead" \
1232                " Uiso/Uij                                            "]
1233    } else {
1234        $expgui(atomtitle) insert end [format "%10s %6s %8s%29s %9s  %s" \
1235                $namehead $typehead "ref/damp  " \
1236                "fractional coordinates$coordhead" \
1237                "$multhead $frachead" \
1238                " Uiso"]
1239    }
1240    if {$mode != "reset"} {
1241        # restore the selected items
1242        foreach i $expgui(selectedatomlist) {
1243            $expgui(atomlistbox) selection set $i
1244        }
1245        # restore the last scrolled position
1246        $expgui(atomlistbox) yview moveto $pos
1247    }
1248    # clear the reentry flag
1249    set expgui(noreenterDisplayAllAtoms) 0
1250}
1251
1252proc FormatAtomLine {atom phase maxline_var} {
1253    global expmap
1254    if {[lindex $expmap(phasetype) [expr {$phase - 1}]] == 4} {
1255        foreach type {x u f} {
1256            if {[mmatominfo $phase $atom ${type}ref]} {
1257                append refflag "[string toupper $type][mmatominfo $phase $atom ${type}damp] "
1258            } else {
1259                append refflag " [mmatominfo $phase $atom ${type}damp] "
1260            }
1261        }
1262        set line [format \
1263                "%5d %-6s %-3s%-2s%4d  %-6s %8s %9.5f%9.5f%9.5f%8.4f %7.4f" \
1264                $atom \
1265                [mmatominfo $phase $atom label] \
1266                [mmatominfo $phase $atom residue] \
1267                [mmatominfo $phase $atom group] \
1268                [mmatominfo $phase $atom resnum] \
1269                [mmatominfo $phase $atom type] \
1270                $refflag \
1271                [mmatominfo $phase $atom x] \
1272                [mmatominfo $phase $atom y] \
1273                [mmatominfo $phase $atom z] \
1274                [mmatominfo $phase $atom frac] \
1275                [mmatominfo $phase $atom Uiso]
1276        ]
1277    } elseif {[atominfo $phase $atom temptype] == "A"} {
1278        foreach type {x u f} {
1279            if {[atominfo $phase $atom ${type}ref]} {
1280                append refflag "[string toupper $type][atominfo $phase $atom ${type}damp] "
1281            } else {
1282                append refflag " [atominfo $phase $atom ${type}damp] "
1283            }
1284        }
1285        # want to set maxline in parent
1286        upvar $maxline_var maxline
1287        set maxline A
1288        # aniso
1289        set fmt "%3d %-6s %-6s %8s %s%s%s%4d%9.4f"
1290        set line [format $fmt \
1291                $atom \
1292                [atominfo $phase $atom label] \
1293                [atominfo $phase $atom type] \
1294                $refflag \
1295                [ShowFixAtom $phase $atom x] \
1296                [ShowFixAtom $phase $atom y] \
1297                [ShowFixAtom $phase $atom z] \
1298                [atominfo $phase $atom mult] \
1299                [atominfo $phase $atom frac]
1300        ]
1301        append line [format "  %9.5f%9.5f%9.5f%9.5f%9.5f%9.5f" \
1302                [atominfo $phase $atom U11] \
1303                [atominfo $phase $atom U22] \
1304                [atominfo $phase $atom U33] \
1305                [atominfo $phase $atom U12] \
1306                [atominfo $phase $atom U23] \
1307                [atominfo $phase $atom U13]
1308        ]
1309    } else {
1310        foreach type {x u f} {
1311            if {[atominfo $phase $atom ${type}ref]} {
1312                append refflag "[string toupper $type][atominfo $phase $atom ${type}damp] "
1313            } else {
1314                append refflag " [atominfo $phase $atom ${type}damp] "
1315            }
1316        }
1317        #set fmt "%3d %-6s %-6s %8s %10.6f%10.6f%10.6f%4d%9.4f  %9.5f"
1318        set fmt "%3d %-6s %-6s %8s %s%s%s%4d%9.4f  %9.5f"
1319        set line [format $fmt \
1320                $atom \
1321                [atominfo $phase $atom label] \
1322                [atominfo $phase $atom type] \
1323                $refflag \
1324                [ShowFixAtom $phase $atom x] \
1325                [ShowFixAtom $phase $atom y] \
1326                [ShowFixAtom $phase $atom z] \
1327                [atominfo $phase $atom mult] \
1328                [atominfo $phase $atom frac] \
1329                [atominfo $phase $atom Uiso]
1330        ]
1331    }
1332    return $line
1333}
1334
1335# format a coordinate from a non-mm phase as 10 chars; mark fixed coordinates differently
1336proc ShowFixAtom {phase atom var} {
1337  # is this fixed?
1338  set param [string toupper $var]
1339  if {[array name ::fix_param "$phase,$atom,$param"] != ""} {
1340    return [format "%9.5fF" [atominfo $phase $atom $var]]
1341  } else {
1342    return [format "%10.6f" [atominfo $phase $atom $var]]
1343  }
1344}
1345
1346# update the display of atom as they are changed
1347proc UpdateAtomLine {atomlist phase} {
1348    global expgui
1349    # for lots of atoms, it is faster to repaint the listbox
1350    if {[llength $atomlist] > 25} {
1351        DisplayAllAtoms $expgui(curPhase) noreset
1352        return
1353    }
1354    foreach atom $atomlist {
1355        set linenum [lsearch -exact $expgui(atomlistboxline) $atom]
1356        $expgui(atomlistbox) delete $linenum
1357        $expgui(atomlistbox) insert $linenum \
1358                [FormatAtomLine $atom $phase maxline]
1359    }
1360    # restore the selected items
1361    foreach i $expgui(selectedatomlist) {
1362        $expgui(atomlistbox) selection set $i
1363    }
1364}
1365
1366# Procedure to select all atoms in response to a right-click
1367proc SelectAllAtoms {} {
1368    global expgui
1369    $expgui(atomlistbox) selection set 0 end
1370    # call editRecord in case trace was called before the selection was made
1371    editRecord
1372}
1373
1374# Procedure to respond to left mouse release in the atoms Pane
1375proc editRecord { args } {
1376    global entrycmd expgui
1377    set expgui(selectedatomlist) [$expgui(atomlistbox) curselection]
1378    # disable traces on entryvar for right now
1379    set entrycmd(trace) 0
1380
1381    if {[llength $expgui(selectedatomlist)] == 0} {
1382        if $expgui(debug) {error "Attempt display non-existent atoms"}
1383    } elseif {[llength $expgui(selectedatomlist)] == 1} {
1384        editOneRecord $expgui(selectedatomlist)
1385    } else {
1386        editMultipleRecords $expgui(selectedatomlist)
1387    }
1388    # reenable traces on entryvar
1389    set entrycmd(trace) 1
1390    # repaint the atoms box in case anything was changed
1391    #    DisplayAllAtoms noreset
1392}
1393
1394proc editOneRecord { AtomIndex } {
1395    global expmap expgui
1396    # make sure that atomlistboxcontents element exists
1397    if {[catch {set expmap(atomlistboxcontents)}]} return
1398
1399    # get atom number & phase
1400    set tuple [lindex $expmap(atomlistboxcontents) $AtomIndex]
1401    set atomnum [lindex $tuple 0]
1402    set p [lindex $tuple 2]
1403    DisplayU $atomnum $p
1404    DisplayAtom $atomnum $p
1405    DisplayRefFlags $atomnum $p
1406    $expgui(EditingAtoms) config -text "Editing atom #$atomnum -- [atominfo $p $atomnum label]"
1407    $expgui(atomxform) config -text "Modify Atom" -state normal
1408}
1409
1410# this will not work for a multi-phase list of atoms (yet)
1411proc editMultipleRecords { AtomIndexList } {
1412    global expmap expgui
1413    # make sure that atomlistboxcontents element exists
1414    if {[catch {set expmap(atomlistboxcontents)}]} return
1415
1416    set numberList {}
1417    # current phase
1418    set p $expgui(curPhase)
1419    foreach AtomIndex $AtomIndexList {
1420        # get atom number & phase
1421        set tuple [lindex $expmap(atomlistboxcontents) $AtomIndex]
1422        lappend numberList [lindex $tuple 0]
1423#       set p [lindex $tuple 2]
1424    }
1425    # this needs to track by phase
1426    $expgui(EditingAtoms) config -text \
1427            "Set refinement options: atoms [CompressList $numberList]"
1428    DisplayU 0 0
1429    DisplayAtom 0 0
1430    # this needs to track by phase
1431    DisplayRefFlags $numberList $p
1432    $expgui(atomxform) config -text "Modify Atoms" -state normal
1433}
1434
1435# format a string of numbers to save space, e.g. "1 2 3 4 6 7 19 13 14 15"
1436# becomes "1-4,6,7,13-15,19"
1437proc CompressList {numberList "max 9999"} {
1438    # format the number list to save space
1439    set lastnum -99
1440    set flist {}
1441    set count 0
1442    set length 0
1443    if [catch {set sortlist [lsort -integer $numberList]}] {return $numberList}
1444    foreach num $sortlist {
1445        set next [expr $lastnum+1]
1446        if {$num != $next} {
1447            if {$count == 0 && $flist != ""} {
1448                if {[string length $flist] - $length > $max} {
1449                    set length [string length $flist]
1450                    append flist ",\n$num"
1451                } else {
1452                    append flist ",$num"
1453                }
1454            } elseif {$count == 1 && $flist != ""} {
1455                if {[string length $flist] - $length > $max} {
1456                    set length [string length $flist]
1457                    append flist ",$lastnum,\n$num"
1458                } else {
1459                    append flist ",$lastnum,$num"
1460                }
1461            } elseif {$flist != ""} {
1462                if {[string length $flist] - $length > $max} {
1463                    set length [string length $flist]
1464                    append flist "-$lastnum,\n$num"
1465                } else {
1466                    append flist "-$lastnum,$num"
1467                }
1468            } else {
1469                append flist "$num"
1470            }
1471            set lastnum $num
1472            set count 0
1473        } else {
1474            incr count
1475            incr lastnum
1476        }
1477    }
1478    if {$count == 1 && $flist != ""} {
1479        append flist ",$lastnum"
1480    } elseif {$flist != "" && $count > 1} {
1481        append flist "-$lastnum"
1482    }
1483    return $flist
1484}
1485
1486# Procedure to display Isotropic or Anisotropic temperature factors
1487#  Changes the display to one entry widget for Isotropic motion OR
1488#   6 entry widgets for Anisotropic motion in Frame3.
1489#   or disables the widet entirly if atom = 0
1490proc DisplayU { atomnum p} {
1491    global expgui entryvar entrycmd expmap
1492    set mm 0
1493    if {$atomnum == 0} {
1494        set iOrA disable
1495    } elseif {[lindex $expmap(phasetype) 0] == 4} {
1496        set mm 1
1497        set iOrA I
1498    } else {
1499        set iOrA [atominfo $p $atomnum temptype]
1500    }
1501
1502    set firstbox [lindex $expgui(anisolabels) 0]
1503    if { $iOrA == "A" } {
1504        $firstbox config -text "U11 "
1505        foreach item $expgui(anisolabels) {
1506            $item config -fg black
1507        }
1508        foreach item $expgui(anisoentry) var {U11 U22 U33 U12 U13 U23} {
1509            set entrycmd($var) "atominfo $p $atomnum $var"
1510            set entryvar($var) [eval $entrycmd($var)]
1511            $item config -fg black -state normal  -bg white
1512        }
1513    } elseif { $iOrA == "I" || $iOrA == "disable"} {
1514        foreach item $expgui(anisolabels) {
1515            $item config -fg $expgui(bkgcolor1)
1516        }
1517        foreach item [lrange $expgui(anisoentry) 1 end] \
1518                var {U22 U33 U12 U13 U23} {
1519            set entrycmd($var) ""
1520            set entryvar($var) ""
1521            $item config -fg $expgui(bkgcolor1) -bg $expgui(bkgcolor1) \
1522                    -state disabled
1523        }
1524        if { $iOrA == "disable"} {
1525            set entrycmd($var) ""
1526            set entryvar($var) ""
1527            [lindex $expgui(anisoentry) 0] config \
1528                    -fg $expgui(bkgcolor1) -bg $expgui(bkgcolor1) \
1529                    -state disabled
1530        } elseif {$mm} {
1531            set entrycmd(U11) "mmatominfo $p $atomnum Uiso"
1532            set entryvar(U11) [eval $entrycmd(U11)]
1533            $firstbox config -text Uiso -fg black
1534            [lindex $expgui(anisoentry) 0] config -fg black -bg white -state normal
1535        } else {
1536            set entrycmd(U11) "atominfo $p $atomnum Uiso"
1537            set entryvar(U11) [eval $entrycmd(U11)]
1538            $firstbox config -text Uiso -fg black
1539            [lindex $expgui(anisoentry) 0] config -fg black -bg white -state normal
1540        }
1541    }
1542}
1543
1544# need to think about multiple phases
1545
1546# Procedure to display refinement flags
1547proc DisplayRefFlags { atomnum p} {
1548    global expgui entryvar entrycmd expmap
1549    if {$atomnum == 0} {
1550        foreach label $expgui(atomreflbl) {
1551            $label config -fg $expgui(bkgcolor1)
1552        }
1553        foreach entry $expgui(atomref) {
1554            $entry config -state disabled \
1555                    -fg $expgui(bkgcolor1) -bg $expgui(bkgcolor1)
1556            # turn off checkbuttons
1557            catch {$entry deselect}
1558
1559        }
1560        return
1561    }
1562    foreach label $expgui(atomreflbl) {
1563        $label config -fg black
1564    }
1565    foreach entry $expgui(atomref) {
1566        $entry config -state normal -fg black -bg $expgui(bkgcolor1)
1567    }
1568    if {[lindex $expmap(phasetype) 0] == 4} {
1569        foreach var {xref uref fref xdamp udamp fdamp}  {
1570            set entrycmd($var) "mmatominfo $p [list $atomnum] $var"
1571            set entryvar($var) [eval $entrycmd($var)]
1572        }
1573    } else {
1574        foreach var {xref uref fref xdamp udamp fdamp}  {
1575            set entrycmd($var) "atominfo $p [list $atomnum] $var"
1576            set entryvar($var) [eval $entrycmd($var)]
1577        }
1578    }
1579}
1580
1581# Procedure to display an atom in the atom edit boxes
1582proc DisplayAtom { atomnum p} {
1583    global expgui entryvar entrycmd expmap
1584    if {$atomnum == 0} {
1585        foreach label $expgui(atomlabels) {
1586            $label config -fg $expgui(bkgcolor1)
1587        }
1588        foreach entry $expgui(atomentry) {
1589            $entry config -state disabled \
1590                    -fg $expgui(bkgcolor1) -bg $expgui(bkgcolor1)
1591        }
1592        return
1593    }
1594    foreach label $expgui(atomlabels) {
1595        $label config -fg black
1596    }
1597    foreach entry $expgui(atomentry) {
1598        $entry config -state normal -fg black -bg white
1599    }
1600    if {[lindex $expmap(phasetype) 0] == 4} {
1601        foreach var {x y z label frac } {
1602            set entrycmd($var) "mmatominfo $p $atomnum $var"
1603            set entryvar($var) [eval $entrycmd($var)]
1604        }
1605    } else {
1606        foreach var {x y z label frac } {
1607            set entrycmd($var) "atominfo $p $atomnum $var"
1608            set entryvar($var) [eval $entrycmd($var)]
1609        }
1610    }
1611}
1612
1613# make a histogram box; used in MakeHistPane,
1614proc MakeHistBox {frm} {
1615    global expgui
1616    grid [label $frm.mode -text "Select a Histogram" \
1617            -bg beige -anchor center -bd 2 -relief raised] \
1618            -row 0 -column 0 -columnspan 2 -sticky ew
1619    bind $frm.mode <Button-1> {
1620        set i [lsearch $expgui(AllowedHistSelectModes) $expgui(globalmode)]
1621        set expgui(globalmode) [lindex \
1622                "$expgui(AllowedHistSelectModes) \
1623                $expgui(AllowedHistSelectModes)" [incr i]]
1624        sethistlist
1625    }
1626    bind $frm.mode <Button-3> {set expgui(globalmode) 0; sethistlist}
1627    grid [listbox $frm.title -height 1 -relief flat \
1628            -exportselection 0 ] -row 1 -column 0 -sticky ew
1629    grid [listbox $frm.lbox -height 10 -width 25 \
1630            -exportselection 0 \
1631            -xscrollcommand "$frm.x set" \
1632            -yscrollcommand "$frm.y set" \
1633            ] -row 2 -column 0 -sticky news
1634    lappend expgui(HistSelectList) $frm
1635    grid [scrollbar $frm.x -orient horizontal \
1636            -command "move2boxesX \" $frm.title $frm.lbox \" " 
1637    ] -row 3 -column 0 -sticky ew
1638    grid [scrollbar $frm.y \
1639            -command "$frm.lbox yview"] \
1640            -row 2 -column 1 -sticky ns
1641    grid columnconfigure $frm 0 -weight 1
1642    grid rowconfigure $frm 2 -weight 1
1643}
1644
1645# update the histogram list
1646# to do: show histogram ref flags?
1647proc sethistlist {} {
1648    global expgui expmap
1649    array set lbl {
1650        1 "Select 1 or more\nTOF Histograms"
1651        2 "Select 1 or more\nCW Neutron Histograms"
1652        3 "Select 1 or more\nAlpha 1,2 X-ray Histograms"
1653        4 "Select 1 or more\nmonochromatic X-ray Histograms"
1654        5 "Select 1 or more Energy\nDispersive X-ray Histograms"
1655        6 "Select 1 or more of\n any type Histograms"
1656    }
1657    foreach lbox $expgui(HistSelectList) {
1658        $lbox.title delete 0 end
1659        $lbox.lbox delete 0 end
1660        if {$expgui(globalmode) != 0} {
1661            $lbox.lbox config -selectmode extended
1662            $lbox.mode config -text $lbl($expgui(globalmode)) -bg yellow
1663        } else {
1664            $lbox.lbox config -selectmode browse
1665            $lbox.mode config -text "Select a histogram" -bg beige
1666        }
1667    }
1668    StageTabUse
1669    # # disable the unallowed pages in all mode
1670    # if {$expgui(globalmode) == 6} {
1671    #   foreach pair $expgui(GlobalModeAllDisable) {
1672    #       if {$expgui(pagenow) == [lindex $pair 0]} {
1673    #           RaisePage lsFrame
1674    #       }
1675    #       eval [lindex $pair 1] -state disabled
1676    #   }
1677    # } else {
1678    #   foreach pair $expgui(GlobalModeAllDisable) {
1679    #       eval [lindex $pair 1] -state normal
1680    #   }
1681    # }
1682    set histlist {}
1683    if  {$expgui(hsorttype) == "type"} {
1684        # sort on histogram type
1685        foreach h [lsort -integer -increasing $expmap(powderlist)] {
1686            lappend histlist "$h [string range $expmap(htype_$h) 1 2]"
1687        }
1688        set expmap(histlistboxcontents) [lsort -ascii -index 1 $histlist]
1689    } elseif {$expgui(hsorttype) == "number"} {
1690        # sort on histogram number
1691        foreach h [lsort -integer -increasing $expmap(powderlist)] {
1692            lappend histlist "$h $h"
1693        }
1694        set expmap(histlistboxcontents) [lsort -integer -index 1 $histlist]
1695    } elseif {$expgui(hsorttype) == "bank"} {
1696        # sort on original bank number
1697        foreach h [lsort -integer -increasing $expmap(powderlist)] {
1698            lappend histlist "$h [histinfo $h bank]"
1699        }
1700        set expmap(histlistboxcontents) [lsort -integer -index 1 $histlist]
1701    } elseif {$expgui(hsorttype) == "angle"} {
1702        # sort on wavelength (CW) or angle (E disp.)
1703        foreach h [lsort -integer -increasing $expmap(powderlist)] {
1704            if {[string range $expmap(htype_$h) 2 2] == "T"} {
1705                set det [format %8.2f [histinfo $h tofangle]]
1706            } elseif {[string range $expmap(htype_$h) 2 2] == "C"} {
1707                set det [format %8.5f [histinfo $h lam1]]
1708            } elseif {[string range $expmap(htype_$h) 2 2] == "E"} {
1709                set det [format %8.2f [histinfo $h lam1]]
1710            } else {
1711                set det {}
1712            }
1713            lappend histlist "$h $det"
1714        }
1715        set expmap(histlistboxcontents) [lsort -real -index 1 $histlist]
1716    }
1717
1718    # title field needs to match longest title
1719    foreach lbox $expgui(HistSelectList) {
1720        $lbox.title insert end [format "%2s %s %4s %8s  %-67s" \
1721                "h#" \
1722                type \
1723                bank \
1724                "ang/wave" \
1725                "    title" \
1726                ]
1727    }
1728    foreach tuple $expmap(histlistboxcontents) {
1729        set h [lindex $tuple 0]
1730
1731        if {$expgui(globalmode) == 1} {
1732            if {[string range $expmap(htype_$h) 2 2] != "T"} continue
1733        } elseif {$expgui(globalmode) == 2} {
1734            if {[string range $expmap(htype_$h) 1 2] != "NC"} continue
1735        } elseif {$expgui(globalmode) == 3} {
1736            if {[string range $expmap(htype_$h) 1 2] != "XC" || \
1737                    [histinfo $h lam2] == 0.0} continue
1738        } elseif {$expgui(globalmode) == 4} {
1739            if {[string range $expmap(htype_$h) 1 2] != "XC" || \
1740                    [histinfo $h lam2] != 0.0} continue
1741        } elseif {$expgui(globalmode) == 5} {
1742            if {[string range $expmap(htype_$h) 1 2] != "XE"} continue
1743        }
1744
1745        if {[string range $expmap(htype_$h) 2 2] == "T"} {
1746            set det [format %8.2f [histinfo $h tofangle]]
1747        } elseif {[string range $expmap(htype_$h) 2 2] == "C"} {
1748            set det [format %8.5f [histinfo $h lam1]]
1749        } elseif {[string range $expmap(htype_$h) 2 2] == "E"} {
1750            set det [format %8.2f [histinfo $h lam1]]
1751        } else {
1752            set det {}
1753        }
1754        foreach lbox $expgui(HistSelectList) {
1755            $lbox.lbox insert end [format "%2d  %s %4d %8s  %-67s" \
1756                    $h \
1757                    [string range $expmap(htype_$h) 1 3] \
1758                    [histinfo $h bank] \
1759                    $det \
1760                    [string range [histinfo $h title] 0 66] \
1761                    ]
1762        }
1763    }
1764    UpdateCurrentPage
1765}
1766
1767proc UpdateCurrentPage {} {
1768    global expgui
1769    foreach set $expgui(frameactionlist) {
1770        if {$expgui(pagenow) == [lindex $set 0]} {catch [lindex $set 1]}
1771    }
1772}
1773
1774#-----------------------------------------------------------------------
1775# ----------- draw Histogram page
1776#-----------------------------------------------------------------------
1777proc DisplayHistogram {} {
1778    global expgui entrycmd entryvar entrybox expmap
1779
1780    # trap if more than one histogram is selected unless global mode
1781    if {$expgui(globalmode) == 0 && [llength $expgui(curhist)] > 1} {
1782        set expgui(curhist) [lindex $expgui(curhist) 0] 
1783    }
1784
1785    # disable the add histogram button if no phases are present
1786    catch {
1787        foreach c [winfo children $expgui(histFrame).bb] {
1788            if {[llength $expmap(phaselist)] == 0} {
1789                $c configure -state disabled
1790            } else {
1791                $c configure -state normal
1792            } 
1793        }
1794    }
1795
1796    # display the selected histograms
1797    $expgui(histFrame).hs.lbox selection clear 0 end
1798    foreach h $expgui(curhist) {
1799        $expgui(histFrame).hs.lbox selection set $h
1800    }
1801
1802    # disable traces on entryvar for right now
1803    set entrycmd(trace) 0
1804
1805    # get histogram list
1806    set histlist {}
1807    foreach item $expgui(curhist) {
1808        lappend histlist [lindex $expmap(powderlist) $item]
1809    }
1810    # must have at least one histogram selected here
1811    if {[llength $histlist] == 0} {
1812        set expgui(backtermlbl) ""
1813        set expgui(backtypelbl) ""
1814        set expgui(abstypelbl) "" 
1815        foreach var {bref bdamp absref absdamp} {
1816            set entrycmd($var) ""
1817            set entryvar($var) ""
1818        }
1819        $expgui(histFrame).top.txt config -text "No Selected Histograms"
1820        grid $expgui(histFrame).top -column 1 -row 0 -sticky nsew       
1821        set expgui(bkglbl) ""
1822        set expgui(abslbl) ""
1823        eval destroy [winfo children $expgui(diffBox)]
1824        set entrycmd(trace) 1
1825        return
1826    }
1827
1828    if {$expgui(globalmode) != 0} {
1829        set expgui(backtermlbl) ""
1830        set expgui(backtypelbl) ""
1831        set expgui(abstypelbl) "" 
1832        foreach var {bref bdamp absref absdamp} {
1833            set entrycmd($var) "histinfo [list $histlist] $var"
1834            set entryvar($var) [histinfo [lindex $histlist 0] $var]
1835        }
1836    } else {
1837        set hist $histlist
1838        set terms [histinfo $hist backterms]
1839        set expgui(backtermlbl) "($terms terms)"
1840        # background type 3 & 9 have gone away
1841        if {[histinfo $hist backtype] == 3 || [histinfo $hist backtype] == 9} {
1842            MyMessageBox -parent . -title "Background Change" \
1843                -type ok -default ok \
1844                -icon warning \
1845                -message "Background function #[histinfo $hist backtype] is no longer supported -- the function will now be changed to type #1 & the values reset"
1846            histinfo $histlist backtype set 1
1847            RecordMacroEntry "histinfo [list $histlist] backtype set 1" 0
1848            incr expgui(changed)
1849            for {set num 1 } { $num <= $terms } { incr num } {
1850                set var "bterm$num"
1851                histinfo $histlist $var set 0
1852                RecordMacroEntry "histinfo [list $histlist] $var set 0" 0
1853                incr expgui(changed)
1854            }
1855            RecordMacroEntry "incr expgui(changed)" 0
1856        }
1857        set expgui(backtypelbl) "Function type [histinfo $hist backtype]"
1858        foreach var {bref bdamp absref absdamp} {
1859            set entrycmd($var) "histinfo $hist $var"
1860            set entryvar($var) [eval $entrycmd($var)]
1861        }
1862        set abstype [histinfo $hist abstype]
1863        if {$abstype > 1} {
1864            set expgui(abstypelbl) "  Model #$abstype, values: [histinfo $hist abscor1], [histinfo $hist abscor2]"
1865        } else {
1866            set expgui(abstypelbl) "  Model #$abstype, value: [histinfo $hist abscor1]"
1867        }
1868    }
1869    # Top box
1870    catch {destroy $expgui(histFrame).pflag}
1871    if {$expgui(globalmode) != 0} {
1872        $expgui(histFrame).top.txt config \
1873                -text "Selected Histograms: [CompressList $histlist]"
1874        grid $expgui(histFrame).top -column 1 -row 0 -sticky nsew       
1875        set expgui(bkglbl) "Globally Edit Background"
1876        set expgui(abslbl) "Globally Edit Absorption"
1877    } else {
1878        grid forget $expgui(histFrame).top
1879        set expgui(bkglbl) "Edit Background"
1880        set expgui(abslbl) "Edit Abs./Refl."
1881        if {[llength $expmap(phaselist)] > 1} {
1882            TitleFrame $expgui(histFrame).pflag  \
1883                    -borderwidth 4 -side left -relief groove \
1884                    -text "Phase Flags"
1885            set expgui(pflag) [$expgui(histFrame).pflag getframe]
1886            grid $expgui(histFrame).pflag -column 1 -row 1 -sticky nsew
1887            grid rowconfigure $expgui(histFrame) 2 -minsize 35
1888            foreach p $expmap(phaselist) {
1889                pack [checkbutton $expgui(pflag).$p \
1890                        -command "GetPhaseFlags $hist" \
1891                        -variable expgui(pflag$p) -text $p] -side left
1892                if {[lsearch $expmap(phaselist_$hist) $p] == -1} {
1893                    set expgui(pflag$p) 0
1894                } else {
1895                    set expgui(pflag$p) 1
1896                }
1897            }
1898        }
1899    }
1900
1901    # diffractometer constants
1902    foreach var {lam1 lam2 kratio pola ipola ddamp zero \
1903            wref pref dcref daref ratref ttref zref } {
1904        set entrycmd($var) "histinfo [list $histlist] $var"
1905        set entryvar($var) [histinfo [lindex $histlist 0] $var]
1906    }
1907
1908    eval destroy [winfo children $expgui(diffBox)]
1909    if {$expgui(globalmode) == 0} {
1910        if {[string range $expmap(htype_$hist) 2 2] == "T"} {
1911        #------
1912        # TOF |
1913        #------
1914            grid [ label $expgui(diffBox).lDCrc -text "Refine DIFC" ] \
1915                    -column 1 -row 1
1916            grid [ checkbutton $expgui(diffBox).rfDCrc -variable entryvar(dcref) ] \
1917                    -column 2 -row 1
1918            grid [ label $expgui(diffBox).lDCdifc -text DIFC ] \
1919                    -column 3 -row 1 -sticky w
1920            grid [ entry $expgui(diffBox).eDCdifc -textvariable entryvar(lam1) \
1921                    -width 15 ] -column 4 -row 1
1922            set entrybox(lam1) $expgui(diffBox).eDCdifc
1923            #
1924            grid [ label $expgui(diffBox).lDCra -text "Refine DIFA" ] \
1925                    -column 1 -row 2
1926            grid [ checkbutton $expgui(diffBox).rfDCra -variable entryvar(daref) ] \
1927                    -column 2 -row 2
1928            grid [ label $expgui(diffBox).lDCdifa -text DIFA ] \
1929                    -column 3 -row 2
1930            grid [ entry $expgui(diffBox).eDCdifa -textvariable entryvar(lam2) \
1931                    -width 15 ] -column 4 -row 2
1932            set entrybox(lam2) $expgui(diffBox).eDCdifa
1933            #
1934            grid [ label $expgui(diffBox).lDCzero -text "Zero"] \
1935                    -column 3 -row 3
1936            grid [ entry $expgui(diffBox).eDCzero -textvariable entryvar(zero) \
1937                    -width 15 ] -column 4 -row 3
1938            set entrybox(zero) $expgui(diffBox).eDCzero
1939            grid [ label $expgui(diffBox).lDCzref -text "Refine zero" ] \
1940                    -column 1 -row 3 -sticky w
1941            grid [ checkbutton $expgui(diffBox).rfDCzref \
1942                    -variable entryvar(zref) ] -column 2 -row 3
1943        } elseif {[string range $expmap(htype_$hist) 1 2] == "NC"} { 
1944        #---------------
1945        # CW - neutron |
1946        #---------------
1947            grid [ label $expgui(diffBox).lDC1 -text "Refine wave" ] \
1948                    -column 1 -row 1
1949            grid [ checkbutton $expgui(diffBox).rfDC1 -variable entryvar(wref) ] \
1950                    -column 2 -row 1
1951            grid [ label $expgui(diffBox).lDCdifc -text wave ] \
1952                    -column 3 -row 1 -sticky w
1953            grid [ entry $expgui(diffBox).eDCdifc -textvariable entryvar(lam1) \
1954                    -width 15 ] -column 4 -row 1
1955            set entrybox(lam1) $expgui(diffBox).eDCdifc
1956            #
1957            grid [ label $expgui(diffBox).lDCzref -text "Refine zero" ] \
1958                    -column 1 -row 3 -sticky w
1959            grid [ checkbutton $expgui(diffBox).rfDCzref \
1960                    -variable entryvar(zref) ] -column 2 -row 3
1961            grid [ label $expgui(diffBox).lDCzero -text "Zero"] \
1962                    -column 3 -row 3
1963            grid [ entry $expgui(diffBox).eDCzero -textvariable entryvar(zero) \
1964                    -width 15 ] -column 4 -row 3
1965            set entrybox(zero) $expgui(diffBox).eDCzero
1966        } elseif {[string range $expmap(htype_$hist) 1 2] == "XC" && \
1967                [histinfo $hist lam2] == 0.0} {
1968        #--------------------------
1969        # CW - x-ray 1 wavelength |
1970        #--------------------------
1971            grid [ label $expgui(diffBox).lDC1 -text "Refine wave" ] \
1972                    -column 1 -row 1
1973            grid [ checkbutton $expgui(diffBox).rfDC1 -variable entryvar(wref) ] \
1974                    -column 2 -row 1
1975            grid [ label $expgui(diffBox).lDCdifc -text wave ] \
1976                    -column 3 -row 1 -sticky w
1977            grid [ entry $expgui(diffBox).eDCdifc -textvariable entryvar(lam1) \
1978                    -width 15 ] -column 4 -row 1
1979            set entrybox(lam1) $expgui(diffBox).eDCdifc
1980            #
1981            grid [ label $expgui(diffBox).lDCzref -text "Refine zero" ] \
1982                    -column 1 -row 3 -sticky w
1983            grid [ checkbutton $expgui(diffBox).rfDCzref \
1984                    -variable entryvar(zref) ] -column 2 -row 3
1985            grid [ label $expgui(diffBox).lDCzero -text "Zero"] \
1986                    -column 3 -row 3
1987            grid [ entry $expgui(diffBox).eDCzero -textvariable entryvar(zero) \
1988                    -width 15 ] -column 4 -row 3
1989            set entrybox(zero) $expgui(diffBox).eDCzero
1990            #
1991            grid [ label $expgui(diffBox).lDCpref -text "Refine POLA" ] \
1992                    -column 1 -row 4 -sticky w
1993            grid [ checkbutton $expgui(diffBox).rfDCpref \
1994                    -variable entryvar(pref) ] -column 2 -row 4
1995            grid [ label $expgui(diffBox).lDCpola -text POLA ] \
1996                    -column 3 -row 4
1997            grid [ entry $expgui(diffBox).eDCpola \
1998                    -textvariable entryvar(pola) -width 15 ] -column 4 -row 4
1999            set entrybox(pola) $expgui(diffBox).eDCpola
2000            grid [ label $expgui(diffBox).lDCipola -text "IPOLA" ] \
2001                    -column 5 -row 4
2002            grid [ entry $expgui(diffBox).eDCipola -width 2 \
2003                    -textvariable entryvar(ipola)] -column 6 -row 4
2004            set entrybox(ipola) $expgui(diffBox).eDCipola
2005        } elseif {[string range $expmap(htype_$hist) 1 2] == "XC"} {
2006        #---------------------------
2007        # CW - x-ray 2 wavelengths |
2008        #---------------------------
2009            grid [ label $expgui(diffBox).lDCdifc -text wavelengths ] \
2010                    -column 3 -row 1 -sticky w
2011            grid [ entry $expgui(diffBox).eDCdifc -textvariable entryvar(lam1) \
2012                    -width 15 ] -column 4 -row 1
2013            set entrybox(lam1) $expgui(diffBox).eDCdifc
2014            grid [ entry $expgui(diffBox).eDCdifa -textvariable entryvar(lam2) \
2015                    -width 15 ] -column 5 -row 1
2016            set entrybox(lam2) $expgui(diffBox).eDCdifa
2017            #
2018            grid [ label $expgui(diffBox).lDCrref -text "Refine ratio" ] \
2019                    -column 1 -row 2 -sticky w
2020            grid [ checkbutton $expgui(diffBox).rfDCrref \
2021                    -variable entryvar(ratref) ] -column 2 -row 2
2022            grid [ label $expgui(diffBox).lDCratio -text Ratio ] \
2023                    -column 3 -row 2
2024            grid [ entry $expgui(diffBox).eDCkratio \
2025                    -textvariable entryvar(kratio) \
2026                    -width 15 ] -column 4 -row 2
2027            set entrybox(kratio) $expgui(diffBox).eDCkratio
2028            #
2029            grid [ label $expgui(diffBox).lDCzero -text "Zero"] \
2030                    -column 3 -row 3
2031            grid [ entry $expgui(diffBox).eDCzero -textvariable entryvar(zero) \
2032                    -width 15 ] -column 4 -row 3
2033            grid [ label $expgui(diffBox).lDCzref -text "Refine zero" ] \
2034                    -column 1 -row 3 -sticky w
2035            set entrybox(zero) $expgui(diffBox).eDCzero
2036            grid [ checkbutton $expgui(diffBox).rfDCzref \
2037                    -variable entryvar(zref) ] -column 2 -row 3
2038            grid [ label $expgui(diffBox).lDCpref -text "Refine POLA" ] \
2039                    -column 1 -row 4 -sticky w
2040            grid [ checkbutton $expgui(diffBox).rfDCpref \
2041                    -variable entryvar(pref) ] -column 2 -row 4
2042            grid [ label $expgui(diffBox).lDCpola -text POLA ] \
2043                    -column 3 -row 4
2044            grid [ entry $expgui(diffBox).eDCpola \
2045                    -textvariable entryvar(pola) -width 15 ] -column 4 -row 4
2046            set entrybox(pola) $expgui(diffBox).eDCpola
2047            grid [ label $expgui(diffBox).lDCipola -text "IPOLA" ] \
2048                    -column 5 -row 4
2049            grid [ entry $expgui(diffBox).eDCipola -width 2 \
2050                    -textvariable entryvar(ipola)] -column 6 -row 4
2051            set entrybox(ipola) $expgui(diffBox).eDCipola
2052        } elseif {[string range $expmap(htype_$hist) 1 2] == "XE"} {
2053        #-------------
2054        # ED - x-ray |
2055        #-------------
2056            grid [ label $expgui(diffBox).lDC1 -text "Refine 2theta" ] \
2057                    -column 1 -row 1
2058            grid [ checkbutton $expgui(diffBox).rfDC1 -variable entryvar(ttref) ] \
2059                    -column 2 -row 1
2060            grid [ label $expgui(diffBox).lDCdifc -text 2Theta ] \
2061                    -column 3 -row 1 -sticky w
2062            grid [ entry $expgui(diffBox).eDCdifc -textvariable entryvar(lam1) \
2063                    -width 15 ] -column 4 -row 1
2064            set entrybox(lam1) $expgui(diffBox).eDCdifc
2065            #
2066            grid [ label $expgui(diffBox).lDCpref -text "Refine POLA" ] \
2067                    -column 1 -row 4 -sticky w
2068            grid [ checkbutton $expgui(diffBox).rfDCpref \
2069                    -variable entryvar(pref) ] -column 2 -row 4
2070            grid [ label $expgui(diffBox).lDCpola -text POLA ] \
2071                    -column 3 -row 4
2072            grid [ entry $expgui(diffBox).eDCpola \
2073                    -textvariable entryvar(pola) -width 15 ] -column 4 -row 4
2074            set entrybox(pola) $expgui(diffBox).eDCpola
2075            grid [ label $expgui(diffBox).lDCipola -text "IPOLA" ] \
2076                    -column 5 -row 4
2077            grid [ entry $expgui(diffBox).eDCipola -width 2 \
2078                    -textvariable entryvar(ipola)] -column 6 -row 4
2079            set entrybox(ipola) $expgui(diffBox).eDCipola
2080        }
2081    } elseif {$expgui(globalmode) == 1} {
2082        #-------------
2083        # Global TOF |
2084        #-------------
2085        grid [ label $expgui(diffBox).lDCrc -text "Refine DIFC" ] \
2086                -column 1 -row 1
2087        grid [ checkbutton $expgui(diffBox).rfDCrc -variable entryvar(dcref) ] \
2088                -column 2 -row 1
2089        grid [button $expgui(diffBox).bDCdifc -text "Set DIFC Globally" \
2090                -command "editglobalparm histinfo difc {DIFC}"] -column 3 -row 1
2091        #
2092        grid [ label $expgui(diffBox).lDCra -text "Refine DIFA" ] \
2093                -column 1 -row 2
2094        grid [ checkbutton $expgui(diffBox).rfDCra -variable entryvar(daref) ] \
2095                -column 2 -row 2
2096        grid [ button $expgui(diffBox).bDCdifa -text "Set DIFA Globally" \
2097                -command "editglobalparm histinfo difa {DIFA}"] -column 3 -row 2
2098        #
2099        grid [ label $expgui(diffBox).lDCzref -text "Refine zero" ] \
2100                -column 1 -row 3 -sticky w
2101        grid [ checkbutton $expgui(diffBox).rfDCzref \
2102                -variable entryvar(zref) ] -column 2 -row 3
2103        grid [ button $expgui(diffBox).bDCzero -text "Set ZERO Globally" \
2104                -command "editglobalparm histinfo zero {Zero}"] -column 3 -row 3
2105    } elseif {$expgui(globalmode) == 2} {
2106        #--------------------
2107        # Global CW neutron |
2108        #--------------------
2109        grid [ label $expgui(diffBox).lDC1 -text "Refine wave" ] \
2110                -column 1 -row 1
2111        grid [ checkbutton $expgui(diffBox).rfDC1 -variable entryvar(wref) ] \
2112                -column 2 -row 1
2113        grid [button $expgui(diffBox).bDCdifc -text "Set Wave Globally" \
2114                -command "editglobalparm histinfo lam1 Wavelength"] \
2115                -column 3 -row 1
2116        #
2117        grid [ label $expgui(diffBox).lDCzref -text "Refine zero" ] \
2118                -column 1 -row 3 -sticky w
2119        grid [ checkbutton $expgui(diffBox).rfDCzref \
2120                -variable entryvar(zref) ] -column 2 -row 3
2121        grid [button $expgui(diffBox).bDCzero -text "Set Zero Globally" \
2122                -command "editglobalparm histinfo zero Zero"] -column 3 -row 3
2123    } elseif {$expgui(globalmode) == 4} {
2124        #----------------------
2125        # Global CW mono xray |
2126        #----------------------
2127        grid [ label $expgui(diffBox).lDC1 -text "Refine wave" ] \
2128                -column 1 -row 1
2129        grid [ checkbutton $expgui(diffBox).rfDC1 -variable entryvar(wref) ] \
2130                -column 2 -row 1
2131        grid [button $expgui(diffBox).bDCdifc -text "Set Wave Globally" \
2132                -command "editglobalparm histinfo lam1 Wavelength"] \
2133                -column 3 -row 1
2134        #
2135        grid [ label $expgui(diffBox).lDCzref -text "Refine zero" ] \
2136                -column 1 -row 3 -sticky w
2137        grid [ checkbutton $expgui(diffBox).rfDCzref \
2138                -variable entryvar(zref) ] -column 2 -row 3
2139        grid [button $expgui(diffBox).bDCzero -text "Set Zero Globally" \
2140                -command "editglobalparm histinfo zero Zero"] -column 3 -row 3
2141        #
2142        grid [ label $expgui(diffBox).lDCpref -text "Refine POLA" ] \
2143                -column 1 -row 4 -sticky w
2144        grid [ checkbutton $expgui(diffBox).rfDCpref \
2145                -variable entryvar(pref) ] -column 2 -row 4
2146        grid [button $expgui(diffBox).bDCpola -text "Set POLA Globally" \
2147                -command "editglobalparm histinfo pola POLA"] -column 3 -row 4
2148        grid [button $expgui(diffBox).bDCipola -text "Set IPOLA Globally" \
2149                -command "editglobalparm histinfo ipola IPOLA"] -column 4 -row 4
2150    } elseif {$expgui(globalmode) == 3} {
2151        #------------------------
2152        # Global alpha 1,2 xray |
2153        #------------------------
2154        grid [button $expgui(diffBox).bDCl1 -text "Set Wave1 Globally" \
2155                -command "editglobalparm histinfo lam1 {Wavelength 1}"] \
2156                -column 3 -row 1
2157        grid [button $expgui(diffBox).bDCl2 -text "Set Wave2 Globally" \
2158                -command "editglobalparm histinfo lam2 {Wavelength 2}"] \
2159                -column 4 -row 1
2160        #
2161        grid [ label $expgui(diffBox).lDCratref -text "Refine Ratio" ] \
2162                -column 1 -row 2 -sticky w
2163        grid [ checkbutton $expgui(diffBox).rfDCratref \
2164                -variable entryvar(ratref) ] -column 2 -row 2
2165        grid [button $expgui(diffBox).bDCrrat -text "Set Ratio Globally" \
2166                -command "editglobalparm histinfo ratio {Wavelength Ratio}"] \
2167                -column 3 -row 2
2168        #
2169        grid [ label $expgui(diffBox).lDCzref -text "Refine zero" ] \
2170                -column 1 -row 3 -sticky w
2171        grid [ checkbutton $expgui(diffBox).rfDCzref \
2172                -variable entryvar(zref) ] -column 2 -row 3
2173        grid [button $expgui(diffBox).bDCzero -text "Set Zero Globally" \
2174                -command "editglobalparm histinfo zero Zero"] -column 3 -row 3
2175        #
2176        grid [ label $expgui(diffBox).lDCpref -text "Refine POLA" ] \
2177                -column 1 -row 4 -sticky w
2178        grid [ checkbutton $expgui(diffBox).rfDCpref \
2179                -variable entryvar(pref) ] -column 2 -row 4
2180        grid [button $expgui(diffBox).bDCpola -text "Set POLA Globally" \
2181                -command "editglobalparm histinfo pola POLA"] -column 3 -row 4
2182        grid [button $expgui(diffBox).bDCipola -text "Set IPOLA Globally" \
2183                -command "editglobalparm histinfo ipola IPOLA"] -column 4 -row 4
2184    } elseif {$expgui(globalmode) == 5} {
2185        #-----------------
2186        # Global ED xray |
2187        #-----------------
2188        grid [ label $expgui(diffBox).lDC1 -text "Refine 2theta" ] \
2189                -column 1 -row 1
2190        grid [ checkbutton $expgui(diffBox).rfDC1 -variable entryvar(ttref) ] \
2191                -column 2 -row 1
2192        grid [button $expgui(diffBox).bDCdifc -text "Set 2Theta Globally" \
2193                -command "editglobalparm histinfo ratio {Fixed 2Theta}"] \
2194                -column 3 -row 1
2195        #
2196        grid [ label $expgui(diffBox).lDCpref -text "Refine POLA" ] \
2197                -column 1 -row 4 -sticky w
2198        grid [ checkbutton $expgui(diffBox).rfDCpref \
2199                -variable entryvar(pref) ] -column 2 -row 4
2200        grid [button $expgui(diffBox).bDCpola -text "Set POLA Globally" \
2201                -command "editglobalparm histinfo pola POLA"] -column 3 -row 4
2202        grid [button $expgui(diffBox).bDCipola -text "Set IPOLA Globally" \
2203                -command "editglobalparm histinfo ipola IPOLA"] -column 4 -row 4
2204    }
2205    if {$expgui(globalmode) == 0} {
2206        grid [frame $expgui(diffBox).d] -column 5 -row 1 -rowspan 3 \
2207                -columnspan 2 -sticky e
2208    } else {
2209        grid [frame $expgui(diffBox).d] -column 4 -row 2 -rowspan 2 \
2210                -columnspan 2 -sticky e
2211    }
2212    grid [label $expgui(diffBox).d.lDamp -text "Damping  "] \
2213            -column 1 -row 1 
2214    tk_optionMenu $expgui(diffBox).d.om entryvar(ddamp) 0 1 2 3 4 5 6 7 8 9
2215    grid $expgui(diffBox).d.om -column 2 -row 1 
2216    grid columnconfigure $expgui(diffBox) 9  -weight 1
2217    grid columnconfigure $expgui(diffBox) 0  -weight 1
2218    update idletasks
2219    # enable traces on entryvar now
2220    set entrycmd(trace) 1
2221}
2222
2223# this gets the phase flags as set in the expgui(pflag*) elements
2224# (linked to phase flag checkbuttons) and the sets the "HST xx NPHAS" flags
2225# accordingly using SetPhaseFlag
2226proc GetPhaseFlags {hist} {
2227    global expmap expgui
2228    set plist {}
2229    foreach p $expmap(phaselist) {
2230        if {$expgui(pflag$p)} {lappend plist $p}
2231    }
2232    SetPhaseFlag $hist $plist
2233    RecordMacroEntry "SetPhaseFlag [list $hist] [list $plist]" 0
2234    incr expgui(changed)
2235    RecordMacroEntry "incr expgui(changed)" 0
2236    # set the powpref warning (1 = suggested)
2237    set expgui(needpowpref) 2
2238    set msg "Phase flags" 
2239    if {[string first $msg $expgui(needpowpref_why)] == -1} {
2240        append expgui(needpowpref_why) "\t$msg were changed\n"
2241    }
2242    mapexp
2243    # reset the phase selection
2244    set expgui(curPhase) {}
2245}
2246
2247#-----------------------------------------------------------------------
2248# populate the Scaling page
2249#-----------------------------------------------------------------------
2250proc DisplayFrac {} {
2251    global expgui entrycmd entryvar entrybox expmap
2252
2253    # trap if more than one histogram is selected unless global mode
2254    if {$expgui(globalmode) == 0 && [llength $expgui(curhist)] > 1} {
2255        set expgui(curhist) [lindex $expgui(curhist) 0] 
2256    }
2257
2258    # display the selected histograms
2259    $expgui(fracFrame).hs.lbox selection clear 0 end
2260    foreach h $expgui(curhist) {
2261        $expgui(fracFrame).hs.lbox selection set $h
2262    }
2263
2264    # disable traces on entryvar
2265    set entrycmd(trace) 0
2266
2267    # get histogram list
2268    set histlist {}
2269    foreach item $expgui(curhist) {
2270        lappend histlist [lindex $expmap(powderlist) $item]
2271    }
2272
2273    # must have at least one histogram selected here
2274    if {[llength $histlist] == 0} {
2275        foreach var {scale sref sdamp} {
2276            set entrycmd($var) ""
2277            set entryvar($var) ""
2278        }
2279        set parm [grid info $expgui(scaleBox).but1]
2280        if {$parm != ""} {
2281            grid forget  $expgui(scaleBox).but1
2282            eval grid $expgui(scaleBox).ent1 $parm
2283        }
2284        # destroy the contents of the frame
2285        set phaseFractf1 $expgui(FracBox).f
2286        eval destroy [winfo children $phaseFractf1]
2287        # reenable traces on entryvar
2288        set entrycmd(trace) 1
2289        return
2290    }
2291
2292    #--------------
2293    # Scale factor
2294    #--------------
2295    if {$expgui(globalmode) != 0} {
2296        foreach var {scale sref sdamp} {
2297            set entrycmd($var) "histinfo [list $histlist] $var"
2298            set entryvar($var) [histinfo [lindex $histlist 0] $var]
2299        }
2300        # reset scale to black
2301        catch {$entrybox(scale) config -fg black}
2302        set parm [grid info $expgui(scaleBox).ent1]
2303        if {$parm != ""} {
2304            grid forget  $expgui(scaleBox).ent1
2305            eval grid $expgui(scaleBox).but1 $parm
2306        }
2307    } else {
2308        set hist $histlist
2309        foreach var {scale sref sdamp} {
2310            set entrycmd($var) "histinfo $hist $var"
2311            set entryvar($var) [eval $entrycmd($var)]
2312        }
2313        # reset scale to black
2314        catch {$entrybox(scale) config -fg black}
2315        set parm [grid info $expgui(scaleBox).but1]
2316        if {$parm != ""} {
2317            grid forget  $expgui(scaleBox).but1
2318            eval grid $expgui(scaleBox).ent1 $parm
2319        }
2320    }
2321
2322    #----------------
2323    # Phase Fractions
2324    #----------------
2325    set phaseFractf1 $expgui(FracBox).f
2326    # destroy the contents of the frame
2327    eval destroy [winfo children $phaseFractf1]
2328    if {$expgui(globalmode) != 0} {
2329        set txt "Phase Fractions for Histograms: [CompressList $histlist]"
2330    } else {
2331        set txt "Phase Fractions"
2332    }
2333    $expgui(fracFrame).f1.phaseFrac configure -text $txt
2334    # Create the frame inside the canvas, One frame for each Phase.
2335    foreach i {1 2 3 4 5 6 7 8 9} {set phasehistlist($i) ""}
2336    foreach hist $histlist {
2337        foreach i $expmap(phaselist_$hist) {
2338            lappend phasehistlist($i) $hist
2339        }
2340    }
2341    foreach i {1 2 3 4 5 6 7 8 9} {
2342        if {[llength $phasehistlist($i)] == 0} continue
2343        set framePF [frame $phaseFractf1.pF$i -relief groove  -bd 4]
2344        grid $framePF -column 0 -row $i -sticky ew
2345        # Label Heading for each phase.
2346        if {$expgui(globalmode) != 0} {
2347            grid [label $framePF.l1 \
2348                    -text "Phase $i Hist: [CompressList $phasehistlist($i)]"] \
2349                    -column 0 -row 0 -sticky nws
2350            grid [button $framePF.but1 -text "Set Globally" \
2351                    -command "editglobalparm hapinfo frac \"Phase $i Fraction\" \
2352                    [list $phasehistlist($i)] $i" \
2353                    ] -column 1 -row 0
2354            set entrycmd(frref$i) "hapinfo [list $histlist] $i frref"
2355            set entryvar(frref$i) [hapinfo $hist $i frref]
2356            set entrycmd(frdamp$i) "hapinfo [list $histlist] $i frdamp"
2357        } else {
2358            grid [label $framePF.l1  -text "Phase $i"] \
2359                    -column 0 -row 0 -sticky nws
2360            grid [entry $framePF.ent -textvariable entryvar(frac$i) -width 15]\
2361                    -column 1 -row 0
2362            set entrybox(frac$i) $framePF.ent
2363            set entrycmd(frref$i) "hapinfo $hist $i frref"
2364            set entryvar(frref$i) [hapinfo $hist $i frref]
2365            set entrycmd(frdamp$i) "hapinfo $hist $i frdamp"
2366        }
2367        set entrycmd(frac$i) "hapinfo $hist $i frac"
2368        set entryvar(frac$i) [hapinfo $hist $i frac]
2369        grid [label $framePF.l2  -text "  Refine"] \
2370                -column 2 -row 0 -sticky nws
2371        grid [checkbutton $framePF.cb -variable entryvar(frref$i)] \
2372                -column 3 -row 0 -sticky nws
2373        grid [label $framePF.l3  -text "  Damping"] \
2374                -column 4 -row 0 -sticky nws
2375        tk_optionMenu $framePF.tkOptDamp entryvar(frdamp$i) \
2376                0 1 2 3 4 5 6 7 8 9     
2377        set entryvar(frdamp$i) [hapinfo $hist $i frdamp]
2378        grid $framePF.tkOptDamp -row 0 -sticky nsw -column 5
2379    }
2380    # resize the scroll window to match the actual
2381    update idletasks
2382    $expgui(FracBox) config -scrollregion [grid bbox $expgui(FracBox).f]
2383    $expgui(FracBox) config -width [lindex [grid bbox $expgui(FracBox).f] 2]
2384    update idletasks
2385    # enable traces on entryvar now
2386    set entrycmd(trace) 1
2387}
2388
2389#-----------------------------------------------------------------------
2390# display the profile page
2391#-----------------------------------------------------------------------
2392proc DisplayProfile {} {
2393    global expgui entrycmd entryvar entrybox expmap
2394
2395    # trap if more than one histogram is selected unless global mode
2396    if {$expgui(globalmode) == 0 && [llength $expgui(curhist)] > 1} {
2397        set expgui(curhist) [lindex $expgui(curhist) 0] 
2398    }
2399    # display the selected histograms
2400    $expgui(profFrame).hs.lbox selection clear 0 end
2401    foreach h $expgui(curhist) {
2402        $expgui(profFrame).hs.lbox selection set $h
2403    }
2404
2405    # destroy the contents of the frame
2406    eval destroy [winfo children $expgui(ProfileBox).f]
2407    # since the next steps can take a while, do a screen update
2408    update idletasks
2409
2410    if {$expgui(globalmode) == 0} {
2411        # must have at least one histogram selected here
2412        if {[llength $expgui(curhist)] == 0} return
2413        # disable traces on entryvar for right now
2414        set entrycmd(trace) 0
2415        set hist [lindex $expmap(powderlist) $expgui(curhist)]
2416        # no defined histograms?
2417        if {$hist == ""} return
2418        # Create one frame for each Phase.
2419        set ind -1
2420        set htype [string range $expmap(htype_$hist) 2 2]
2421        set zflag 0
2422        if {$htype == "C"} {
2423            set zflag [histinfo $hist zref]
2424        }
2425        foreach i $expmap(phaselist_$hist) {
2426            incr ind
2427            # Label Heading for each phase.
2428            set ptype [string trim [hapinfo $hist $i proftype]]
2429            grid [TitleFrame $expgui(ProfileBox).f.$i \
2430                      -text "Hist $hist -- Phase $i (type $ptype)" \
2431                      -relief groove -bd 2] \
2432                -column 0 -row $ind -sticky ew
2433            set ProfileFrame [$expgui(ProfileBox).f.$i getframe]
2434            grid [frame $ProfileFrame.1] \
2435                -column 0 -row 0 -columnspan 10
2436            pack [label $ProfileFrame.1.l  \
2437                      -text Damping]\
2438                -side left
2439            tk_optionMenu $ProfileFrame.1.tkOptDamp entryvar(pdamp_$i) \
2440                    0 1 2 3 4 5 6 7 8 9
2441            set entrycmd(pdamp_$i) "hapinfo $hist $i pdamp"
2442            set entryvar(pdamp_$i) [hapinfo $hist $i pdamp]
2443            pack $ProfileFrame.1.tkOptDamp -side left
2444            pack [label $ProfileFrame.1.l1 \
2445                    -text "  Peak cutoff"]\
2446                    -side left
2447            pack [entry $ProfileFrame.1.e1  \
2448                    -width 10 -textvariable entryvar(pcut_$i)]\
2449                    -side left
2450            set entrybox(pcut_$i) $ProfileFrame.1.e1
2451            set entrycmd(pcut_$i) "hapinfo $hist $i pcut"
2452            set entryvar(pcut_$i) [hapinfo $hist $i pcut]
2453
2454            pack [button $ProfileFrame.1.b1  \
2455                    -text "Change Type" \
2456                    -command "ChangeProfileType $hist $i"]\
2457                    -side left
2458           
2459            set col -1
2460            set row 1
2461            set nterms [hapinfo $hist $i profterms]
2462            set lbls "dummy [GetProfileTerms $i $hist [expr abs($ptype)]]"
2463            for { set num 1 } { $num <= $nterms } { incr num } {
2464                set term {}
2465                catch {set term [lindex $lbls $num]}
2466                if {$term == ""} {set term $num}
2467                incr col
2468                grid [label $ProfileFrame.l${num}_${i} -text "$term"] \
2469                        -row $row -column $col
2470                incr col
2471                grid [checkbutton $ProfileFrame.ref${num}_${i} \
2472                        -variable entryvar(pref${num}_$i)] -row $row -column $col
2473                set entrycmd(pref${num}_$i) "hapinfo $hist $i pref$num"
2474                set entryvar(pref${num}_$i) [hapinfo $hist $i pref$num]
2475                incr col
2476                grid [entry $ProfileFrame.ent${num}_${i} \
2477                        -textvariable entryvar(pterm${num}_$i)\
2478                        -width 12] -row $row -column $col
2479                set entrybox(pterm${num}_$i) $ProfileFrame.ent${num}_${i}
2480                set entrycmd(pterm${num}_$i) "hapinfo $hist $i pterm$num"
2481                set entryvar(pterm${num}_$i) [hapinfo $hist $i pterm$num]
2482                # disable trns & shft when zero is refined
2483                if {$zflag && ($term == "trns" || $term == "shft")} {
2484                    if {$entryvar(pref${num}_$i)} {
2485                        incr expgui(changed)
2486                        set entryvar(pref${num}_$i) 0
2487                    }
2488                    $ProfileFrame.l${num}_${i} config -fg gray
2489                    $ProfileFrame.ref${num}_${i} config -state disabled
2490                    $ProfileFrame.ent${num}_${i} config -fg gray
2491                }
2492                if {$col > 6} {set col -1; incr row}
2493            }
2494        }
2495        grid columnconfigure $expgui(ProfileBox).f 0 -weight 1
2496    } else {
2497        # get histogram list
2498        set histlist {}
2499        foreach item $expgui(curhist) {
2500            lappend histlist [lindex $expmap(powderlist) $item]
2501        }
2502        # must have at least one histogram selected here
2503        if {[llength $histlist] == 0} return
2504        # disable traces on entryvar for right now
2505        set entrycmd(trace) 0
2506        # loop through histograms & phases, set up an array by phase & profile type
2507        catch {unset prtyparray histarray phasearray}
2508        foreach hist $histlist {
2509            foreach phase $expmap(phaselist_$hist) {
2510                set prtyp [string trim [hapinfo $hist $phase proftype]]
2511                set key ${prtyp}_$phase
2512                lappend prtyparray($key) $hist
2513                lappend histarray($key) $hist
2514                lappend phasearray($key) $phase
2515            }
2516        }
2517       
2518        set ptype ""
2519        set i -1
2520        # loop over all combined phases and profile types, sorted 1st by profile number
2521        foreach key [lsort [array names prtyparray]] {
2522            # split key
2523            scan $key %d_%d prftyp p
2524
2525            if {$ptype != $prftyp || !$expgui(globalphasemode)} {
2526                set ptype $prftyp
2527                set curhistlist $histarray($key)
2528                set curphaslist $phasearray($key)
2529               
2530                set hist1 [lindex $curhistlist 0]
2531                set phase1 [lindex $curphaslist 0]
2532                set nterms [hapinfo $hist1 $phase1 profterms]
2533                set htype [string range $expmap(htype_$hist1) 2 2]
2534                set lbls "dummy [GetProfileTerms $phase1 $hist1 [expr abs($ptype)]]"
2535                # Create a frame for this type
2536                incr i
2537                set boxtitle "Phase $p, hist [CompressList $histarray($key)]"
2538                grid [TitleFrame $expgui(ProfileBox).f.$i \
2539                          -text "(type $ptype)" \
2540                          -relief groove -bd 2] \
2541                    -column 0 -row $i -sticky ew
2542                set ProfileFrame [$expgui(ProfileBox).f.$i getframe]
2543                grid [frame $ProfileFrame.0] \
2544                    -column 0 -row 0 -columnspan 20 -sticky ew
2545                grid [label $ProfileFrame.0.1  \
2546                        -anchor w] -row 0 -column 1
2547                grid [frame $ProfileFrame.1] \
2548                        -column 0 -row 1 -columnspan 20 -sticky ew
2549                grid [label $ProfileFrame.1.2  \
2550                        -text "Damping"] -row 0 -column 2
2551                tk_optionMenu $ProfileFrame.1.tkOptDamp \
2552                        entryvar(pdamp_$i) 0 1 2 3 4 5 6 7 8 9
2553                grid $ProfileFrame.1.tkOptDamp -row 0 -column 3
2554                grid [button $ProfileFrame.1.edit \
2555                        -text "Global Edit"] -row 0 -column 4 -sticky w
2556                set entryvar(pdamp_$i) [hapinfo $hist $phase pdamp]
2557                grid [button $ProfileFrame.1.b1 -text "Change Type"] \
2558                        -row 0 -column 5 -sticky w
2559                set col -1
2560                set row 2
2561                for { set num 1 } { $num <= $nterms } { incr num } {
2562                    set term {}
2563                    catch {set term [lindex $lbls $num]}
2564                    if {$term == ""} {set term $num}
2565                    incr col
2566                    grid [label $ProfileFrame.l${num}_${i} \
2567                            -text "$term"] -row $row -column $col
2568                    incr col
2569                    grid [checkbutton $ProfileFrame.ref${num}_${i} \
2570                            -variable entryvar(pref${num}_$i)] \
2571                            -row $row -column $col
2572                    set entryvar(pref${num}_$i) [hapinfo $hist $phase pref$num]
2573                    if {$col > 10} {set col -1; incr row}
2574                }
2575                grid columnconfigure $expgui(ProfileBox).f 0 -weight 1
2576            } else {
2577                # add to the current entry
2578                eval lappend curhistlist $histarray($key)
2579                eval lappend curphaslist $phasearray($key)
2580                append boxtitle "\nPhase $p, hist [CompressList $histarray($key)]"
2581            }
2582            $ProfileFrame.0.1 config -text $boxtitle
2583            $ProfileFrame.1.edit config -command "\
2584                    EditProfile \"\n$boxtitle\" \
2585                    [list $curhistlist] \
2586                    [list $curphaslist]"
2587            $ProfileFrame.1.b1 config -command "ChangeProfileType \
2588                    [list $curhistlist] [list $curphaslist]" 
2589            set entrycmd(pdamp_$i) "hapinfo \
2590                    [list $curhistlist] \
2591                    [list $curphaslist] pdamp"
2592            for { set num 1 } { $num <= $nterms } { incr num } {
2593                set entrycmd(pref${num}_$i) "hapinfo \
2594                        [list $curhistlist] \
2595                        [list $curphaslist] pref$num"
2596            }
2597        }
2598    }
2599   
2600    # resize the scroll window to match the actual
2601    update idletasks
2602    $expgui(ProfileBox) config -scrollregion [grid bbox $expgui(ProfileBox).f]
2603    $expgui(ProfileBox) config -width [lindex [grid bbox $expgui(ProfileBox).f] 2]
2604    update idletasks
2605    ResizeNotebook
2606    # enable traces on entryvar now
2607    set entrycmd(trace) 1
2608}
2609
2610# process the bit settings in the print options
2611#   bitnum -- the number of the bit to be tested/set starting at 0 for the LSBit
2612proc printsetting {bitnum "action get" "value {}"} {
2613    global entryvar expgui
2614    if {$action == "get"} {
2615        return [expr ([expinfo print] & int(pow(2,$bitnum))) != 0]
2616    } elseif $value {
2617        set newval [expr ([expinfo print] | int(pow(2,$bitnum)))]
2618    } else {
2619        set newval [expr ([expinfo print] & ~int(pow(2,$bitnum)))]
2620    }
2621    expinfo print set $newval
2622    RecordMacroEntry "incr expgui(changed); expinfo print set $newval" 1
2623    set expgui(printopt) "Print Options ([expinfo print])"
2624}
2625
2626# need to respond to mouse presses -- control variable associated with extract Fobs
2627# and set the LeBail extraction flags
2628proc SetupExtractHist {} {
2629    global expgui entrycmd entryvar expmap
2630
2631    # display the selected histograms
2632    $expgui(lsFrame).hs.lbox selection clear 0 end
2633    foreach h $expgui(curhist) {
2634        $expgui(lsFrame).hs.lbox selection set $h
2635    }
2636
2637    # get histogram list
2638    set histlist {}
2639    foreach item $expgui(curhist) {
2640        set hist [lindex $expmap(powderlist) $item]
2641        if {$hist != ""} {lappend histlist $hist}
2642    }
2643    set entrycmd(fobsextract) "histinfo [list $histlist] foextract"
2644    if {[llength $histlist] == 0 || [string trim $histlist] == ""} {
2645        set entrycmd(LBdamp) ""
2646        foreach phase {1 2 3 4 5 6 7 8 9} {
2647            $expgui(FobsExtractFrame).l$phase config -fg grey
2648            set expgui(Fextract$phase) {}
2649            foreach item $expgui(ExtractSettingsRadiobuttons) {
2650                ${item}$phase config -state disabled -bd 1
2651            }
2652        }
2653    } elseif {[llength $histlist] == 1} {
2654        # disable traces on entryvar
2655        set entrycmd(trace) 0
2656        set entryvar(fobsextract) [histinfo $histlist foextract]
2657        set entrycmd(LBdamp) "histinfo $histlist LBdamp"
2658        set entryvar(LBdamp) [histinfo $histlist LBdamp]
2659        foreach phase {1 2 3 4 5 6 7 8 9} {
2660            # is the phase present?
2661            if {[lsearch -exact $expmap(phaselist_$histlist) $phase] == -1} {
2662                $expgui(FobsExtractFrame).l$phase config -fg grey
2663                set expgui(Fextract$phase) {}
2664                foreach item $expgui(ExtractSettingsRadiobuttons) {
2665                    ${item}$phase config -state disabled -bd 1
2666                }
2667            } else {
2668                $expgui(FobsExtractFrame).l$phase config -fg black
2669                foreach item $expgui(ExtractSettingsRadiobuttons) {
2670                    ${item}$phase config -state normal -bd 2
2671                }
2672                set expgui(Fextract$phase) [hapinfo $histlist $phase extmeth]
2673            }
2674        }
2675    } elseif {[llength $histlist] > 1} {
2676        # disable traces on entryvar
2677        set entrycmd(LBdamp) "histinfo [list $histlist] LBdamp"
2678        set entryvar(LBdamp) [histinfo [lindex $histlist 0] LBdamp]
2679        set entrycmd(trace) 0
2680        # multiple histograms need phases in any histogram
2681        foreach phase {1 2 3 4 5 6 7 8 9} {
2682            set gotphase($phase) 0
2683        }           
2684        foreach hist $histlist {
2685            foreach phase $expmap(phaselist_$hist) {
2686                set gotphase($phase) 1
2687            }
2688        }
2689        foreach phase {1 2 3 4 5 6 7 8 9} {
2690            set expgui(Fextract$phase) {}
2691            if $gotphase($phase) {
2692                $expgui(FobsExtractFrame).l$phase config -fg black
2693                foreach item $expgui(ExtractSettingsRadiobuttons) {
2694                    ${item}$phase config -state normal -bd 2
2695                }
2696            } else {
2697                $expgui(FobsExtractFrame).l$phase config -fg grey
2698                foreach item $expgui(ExtractSettingsRadiobuttons) {
2699                    ${item}$phase config -state disabled -bd 1
2700                }
2701            }
2702        }
2703    }
2704    # reenable traces
2705    set entrycmd(trace) 1
2706}
2707# respond to a change in the fobs extraction method for a phase
2708# force the main extraction flag on, if fobs extraction is selected for any phase
2709proc HistExtractSet {phase} {
2710    global expgui entryvar expmap
2711    foreach item $expgui(curhist) {
2712        lappend histlist [lindex $expmap(powderlist) $item]
2713    }
2714    hapinfo $histlist $phase extmeth set $expgui(Fextract$phase)
2715    RecordMacroEntry "incr expgui(changed); hapinfo [list $histlist] $phase extmeth set $expgui(Fextract$phase)" 1
2716    incr expgui(changed)
2717    if {$expgui(Fextract$phase) != 0} {set entryvar(fobsextract) 1}
2718}
2719#---------------------------- Global Edit Functions ------------------------
2720proc editbackground {} {
2721    global expgui expmap entrycmd
2722    set histlist {}
2723    foreach n $expgui(curhist) {
2724        lappend histlist [lindex $expmap(powderlist) $n]
2725    }
2726    if {[llength $histlist] == 0} return
2727
2728    set w .back
2729    catch {destroy $w}
2730    toplevel $w -bg beige
2731    if {$expgui(globalmode) != 0} {
2732        wm title $w "Global Edit Background" 
2733    } else {
2734        wm title $w "Edit Background" 
2735    }
2736   
2737    pack [frame $w.0 -bd 6 -relief groove  -bg beige \
2738            ] -side top -expand yes -fill both
2739    if {[llength $histlist] > 1} {
2740        grid [label $w.0.a \
2741            -text "Setting background terms for histograms [CompressList $histlist]" \
2742            -bg beige] -row 0 -column 0 -columnspan 10
2743    } else {
2744        grid [label $w.0.a \
2745            -text "Setting background terms for histogram $histlist" \
2746            -bg beige] -row 0 -column 0 -columnspan 4
2747        grid [button $w.0.bkg -text "Fit Background\nGraphically" \
2748                -command "QuitEditBackground $w; bkgedit $histlist"] \
2749                -row 0 -column 4 -rowspan 2
2750        grid columnconfig $w.0 0 -weight 1
2751        grid columnconfig $w.0 4 -weight 1
2752    }
2753    set hist [lindex $histlist 0]
2754    grid [label $w.0.b -text "Function type" -bg beige]  -row 1 -column 0 -sticky e
2755
2756    # disable traces on  expgui(backtype) & expgui(backterms) now
2757    set entrycmd(trace) 0
2758
2759    # number of terms
2760    set expgui(backtype) [histinfo $hist backtype]
2761    set expgui(orig_backtype) $expgui(backtype)
2762    set expgui(prev_backtype) $expgui(backtype)
2763    set typemenu [tk_optionMenu $w.0.type expgui(backtype) null]
2764    $typemenu delete 0 end
2765    foreach item {
2766        "1 - Shifted Chebyschev"
2767        "2 - Cosine Fourier series"
2768        "4 - Power series in Q**2n/n!"
2769        "5 - Power series in n!/Q**2n"
2770        "6 - Power series in Q**2n/n! and n!/Q**2n"
2771        "7 - Linear interpolation function"
2772        "8 - Reciprocal interpolation function"
2773    } {
2774        set val [lindex $item 0]
2775        $typemenu insert end radiobutton -variable expgui(backtype) \
2776                -label $item -value $val
2777    }
2778# removed
2779#       "3 - Radial distribution peaks"
2780
2781    grid $w.0.type   -row 1 -column 1
2782    grid [label $w.0.c -text "  Number of terms"  -bg beige] -row 1 -column 2
2783
2784    # function type
2785    set expgui(backterms) [histinfo $hist backterms]
2786    set expgui(orig_backterms) $expgui(backterms) 
2787    set list {}; for {set i 1} {$i <= 36} {incr i} {lappend list $i}
2788    eval tk_optionMenu $w.0.terms expgui(backterms) $list
2789    grid $w.0.terms   -row 1 -column 3 
2790    # enable traces on  expgui(backtype) & expgui(backterms) now
2791    set entrycmd(trace) 1
2792
2793    #set background terms
2794    for {set num 1 } { $num <= 36 } { incr num } {
2795        set var "bterm$num"
2796        set expgui($var) {}
2797        set expgui(orig_$var) {}
2798    }
2799    if {[llength $histlist] == 1} {
2800        for {set num 1 } { $num <= $expgui(backterms) } { incr num } {
2801            set var "bterm$num"
2802            set expgui($var) [histinfo $histlist $var]
2803            set expgui(orig_$var) $expgui($var)
2804        }
2805    }
2806
2807    pack [frame $w.1 -bd 6 -relief groove  -bg beige] -side top \
2808            -expand yes -fill both
2809    ShowBackTerms $w.1
2810
2811    set expgui(temp) {}
2812    pack [frame $w.b -bg beige] -fill x -expand yes -side top
2813    grid [button $w.b.2 -text Continue -command "destroy $w"] -row 0 -column 1
2814    grid [button $w.b.3 -text Cancel \
2815            -command "QuitEditBackground $w"] -row 0 -column 2
2816    grid [button $w.b.help -text Help -bg yellow \
2817            -command "MakeWWWHelp expgui3.html EditBackground"] \
2818            -row 0 -column 4
2819    grid columnconfig $w.b 0 -weight 1
2820    grid columnconfig $w.b 3 -weight 1
2821    bind $w <Key-F1> "MakeWWWHelp expgui3.html EditBackground"
2822    bind $w <Return> "destroy $w"
2823
2824    # force the window to stay on top
2825    putontop $w
2826
2827    focus $w.b.2
2828    tkwait window $w
2829    afterputontop
2830
2831    if {$expgui(temp) != ""} return
2832
2833    if {$expgui(orig_backtype) != $expgui(backtype)} {
2834        histinfo $histlist backtype set $expgui(backtype)
2835        RecordMacroEntry "histinfo [list $histlist] backtype set $expgui(backtype)" 0
2836        incr expgui(changed)
2837    }
2838    if {$expgui(orig_backterms) != $expgui(backterms)} {
2839        histinfo $histlist backterms set $expgui(backterms)
2840        RecordMacroEntry "histinfo [list $histlist] backterms set $expgui(backterms)" 0
2841        incr expgui(changed)
2842    }
2843    for {set num 1 } { $num <= $expgui(backterms) } { incr num } {
2844        set var "bterm$num"
2845        if {$expgui(orig_$var) != $expgui($var)} {
2846            histinfo $histlist $var set $expgui($var)
2847            RecordMacroEntry "histinfo [list $histlist] $var set $expgui($var)" 0
2848            incr expgui(changed)
2849        }
2850    }
2851    RecordMacroEntry "incr expgui(changed)" 0
2852
2853    if {$expgui(globalmode) == 0} {
2854        set expgui(backtypelbl) "Function type [histinfo $hist backtype]"
2855        set expgui(backtermlbl) "([histinfo $hist backterms] terms)"
2856    }
2857}
2858
2859trace variable expgui(backterms) w ChangeBackTerms
2860proc ChangeBackTerms {a b c} {
2861    global entrycmd expgui
2862    if !$entrycmd(trace) return
2863    ShowBackTerms .back.1
2864}
2865
2866trace variable expgui(backtype) w ChangeBackType
2867# reset the terms to 1, 0, 0... when the number of terms increase
2868proc ChangeBackType {a b c} {
2869    global entrycmd expgui
2870    if !$entrycmd(trace) return
2871    if {$expgui(prev_backtype) == $expgui(backtype)} return
2872    set expgui(prev_backtype) $expgui(backtype)
2873    for {set num 1 } { $num <= $expgui(backterms) } { incr num } {
2874        set var "bterm$num"
2875        if {$num == 1} {
2876            set expgui($var) 1.0
2877        } else {
2878            set expgui($var) 0.0
2879        }
2880    }
2881}
2882
2883proc ShowBackTerms {w } {
2884    global expgui expmap
2885    # destroy the contents of the frame
2886    eval destroy [winfo children $w]
2887    set histlist {}
2888    foreach n $expgui(curhist) {
2889        lappend histlist [lindex $expmap(powderlist) $n]
2890    }
2891    set widgetsPerRow 4
2892    for {set rows 2; set num 1 } { $num <= $expgui(backterms) } { incr rows } {
2893        for {set cols 0} { (2*$widgetsPerRow > $cols) && ($num <= $expgui(backterms)) }  { incr num }  {
2894            set var "bterm$num"
2895            grid [label $w.l$num -text $num -bg beige]  \
2896                    -row $rows -column $cols -sticky nes
2897            incr cols
2898            grid [entry $w.e$num -width 15 -textvariable expgui($var) \
2899                    ] -row $rows  -column $cols  -sticky news
2900            incr cols
2901        }
2902    }
2903}
2904
2905proc QuitEditBackground {w} {
2906    global expgui
2907    # lets find out if anything changed
2908    set changed 0
2909    if {$expgui(orig_backtype) != $expgui(backtype)} {
2910        set changed 1
2911    }
2912    if {$expgui(orig_backterms) != $expgui(backterms)} {
2913        set changed 1
2914    }
2915    for {set num 1 } { $num <= $expgui(backterms) } { incr num } {
2916        set var "bterm$num"
2917        if {$expgui(orig_$var) != $expgui($var)} {
2918            set changed 1
2919            break
2920        }
2921    }
2922    if $changed {
2923        set decision [tk_dialog .changes "Abandon Changes" \
2924                "You have made changes to the background. Ok to abandon changes?" \
2925                warning 0 Abandon Keep]
2926        if !$decision {
2927            set expgui(temp) "Quit"
2928            destroy $w
2929        }
2930    } else {
2931        set expgui(temp) "Quit"
2932        destroy $w
2933    }
2934}
2935
2936# this probably needs work
2937proc editglobalparm {cmd variable title "histlist {}" "phase {}"} {
2938    global expgui expmap
2939    set w .global
2940    catch {destroy $w}
2941    toplevel $w -bg beige
2942    wm title $w "Edit Global Parameter"
2943    set expgui(temp) {}
2944    if {[llength $histlist] == 0} {
2945        set hist {}
2946        foreach n $expgui(curhist) {
2947            lappend hist [lindex $expmap(powderlist) $n]
2948        }
2949    } else {
2950        set hist $histlist
2951    }
2952    pack [frame $w.0 -bd 6 -relief groove -bg beige] \
2953            -side top -expand yes -fill both
2954    grid [label $w.0.a -text "Setting $title for histograms [CompressList $hist]"\
2955            -bg beige] \
2956            -row 0 -column 0 -columnspan 10
2957    grid [entry $w.0.b -textvariable expgui(temp)] \
2958            -row 1 -column 0 
2959
2960
2961    pack [frame $w.b -bg beige] -fill x -expand yes -side top
2962    pack [button $w.b.2 -text Continue -command "destroy $w"] -side left
2963    pack [button $w.b.3 -text Cancel -command "set expgui(temp) {}; destroy $w"] -side left
2964    pack [button $w.b.help -text Help -bg yellow \
2965            -command "MakeWWWHelp expgui3.html EditParm"] -side right
2966    bind $w <Key-F1> "MakeWWWHelp expgui3.html EditParm"
2967    bind $w <Return> "destroy $w"
2968
2969    # force the window to stay on top
2970    putontop $w
2971    focus $w.b.2
2972    tkwait window $w
2973    afterputontop
2974
2975    if {$expgui(temp) != ""} {
2976        foreach h $hist {
2977            if {$cmd == "histinfo"} {
2978                histinfo $h $variable set $expgui(temp)
2979                RecordMacroEntry "histinfo $h $variable set $expgui(temp)" 1
2980                incr expgui(changed)
2981                if $expgui(debug) {
2982                    puts "histinfo $h $variable set $expgui(temp)"
2983                }
2984            } elseif {$cmd == "hapinfo"} {
2985                hapinfo $h $phase $variable set $expgui(temp)
2986                RecordMacroEntry "hapinfo $h [list $phase] $variable set $expgui(temp)" 1
2987                incr expgui(changed)
2988                if $expgui(debug) {
2989                    puts "hapinfo $h $phase $variable set $expgui(temp)"
2990                }
2991            } else {
2992                error "$cmd unimplemented"
2993            }
2994        }
2995        RecordMacroEntry "incr expgui(changed)" 0
2996    }
2997}
2998
2999proc EditProfile {title histlist phaselist} {
3000    global expgui expmap entrycmd
3001    set w .back
3002    catch {destroy $w}
3003    toplevel $w -bg beige
3004    wm title $w "Global Edit Profile"
3005    set hist [lindex $histlist 0]
3006    set phase [lindex $phaselist 0]
3007    set ptype [string trim [hapinfo $hist $phase proftype]]
3008    set htype [string range $expmap(htype_$hist) 2 2]
3009    set nterms [hapinfo $hist $phase profterms]
3010   
3011    pack [frame $w.0 -bd 6 -relief groove  -bg beige \
3012            ] -side top -expand yes -fill both
3013    grid [label $w.0.a \
3014            -text "Setting profile terms: $title" \
3015            -bg beige] -row 0 -column 0 -columnspan 10
3016    grid [label $w.0.b -text "Function type $ptype"  -bg beige]  -row 1 -column 0
3017    grid [label $w.0.c -text "  Peak cutoff" -bg beige] -row 1 -column 3 
3018    grid [entry $w.0.d -width 10 ]  -row 1 -column 4
3019    set entrylist {}
3020    lappend entrylist "pcut $w.0.d"
3021
3022    set col -1
3023    set row 1
3024    set lbls "dummy [GetProfileTerms $phase $hist [expr abs($ptype)]]"
3025    pack [frame $w.1 -bd 6 -relief groove  -bg beige \
3026            ] -side top -expand yes -fill both
3027    for { set num 1 } { $num <= $nterms } { incr num } {
3028        set term {}
3029        catch {set term [lindex $lbls $num]}
3030        if {$term == ""} {set term $num}
3031        incr col
3032        grid [label $w.1.l${num} -text "$term" -bg beige] \
3033                -row $row -column $col
3034        incr col
3035        grid [entry $w.1.ent${num} \
3036                -width 14] -row $row -column $col
3037        lappend entrylist "pterm$num $w.1.ent${num}"   
3038        if {$col > 6} {set col -1; incr row}
3039    }
3040    pack [frame $w.b -bg beige] -fill x -expand yes -side top
3041    grid [button $w.b.2 -text Continue \
3042            -command "SetEditProfile [list $entrylist] [list $phaselist] \
3043            [list $histlist] $w"] -row 0 -column 1
3044    grid [button $w.b.3 -text Cancel \
3045            -command "QuitEditProfile $w [list $entrylist]"] -row 0 -column 2
3046    grid [button $w.b.help -text Help -bg yellow \
3047            -command "MakeWWWHelp expgui5.html GlobalEdit"] \
3048            -row 0 -column 4
3049    grid columnconfig $w.b 0 -weight 1
3050    grid columnconfig $w.b 3 -weight 1
3051    bind $w <Key-F1> "MakeWWWHelp expgui5.html GlobalEdit"
3052    bind $w <Return> "QuitEditProfile $w [list $entrylist]"
3053
3054    # force the window to stay on top
3055    putontop $w
3056    focus $w.b.2
3057    tkwait window $w
3058    afterputontop
3059}
3060
3061proc SetEditProfile {entrylist phaselist histlist w} {
3062    global expgui
3063    foreach item $entrylist {
3064        set value [ [lindex $item 1] get ]
3065        if {$value != ""} {
3066            hapinfo $histlist $phaselist [lindex $item 0] set $value
3067            RecordMacroEntry "hapinfo [list $histlist] [list $phaselist] [lindex $item 0] set $value" 1
3068            incr expgui(changed)
3069            RecordMacroEntry "incr expgui(changed)" 0
3070            if $expgui(debug) {
3071                puts "hapinfo [list $phaselist] [list $histlist] [lindex $item 0] set $value"
3072            }
3073        }
3074    }
3075    destroy $w
3076}
3077
3078proc QuitEditProfile {w entrylist} {
3079    global expgui
3080    # lets find out if anything changed
3081    set changed 0
3082    foreach item $entrylist {
3083        if {[ [lindex $item 1] get ] != ""} {set changed 1; break}
3084    }
3085    if $changed {
3086        set decision [tk_dialog .changes "Abandon Changes" \
3087                "You have made changes to the Profile. Ok to abandon changes?" \
3088                warning 0 Abandon Keep]
3089        if !$decision {destroy $w}
3090    } else {
3091        destroy $w
3092    }
3093}
3094
3095# this is called to change the absorption correction mode and to
3096# change the absorption correction model.
3097proc editabsorption {} {
3098    global expgui expmap
3099    set histlist {}
3100    foreach n $expgui(curhist) {
3101        lappend histlist [lindex $expmap(powderlist) $n]
3102    }
3103    if {[llength $histlist] == 0} return
3104
3105    set w .abs
3106    catch {destroy $w}
3107    toplevel $w -bg beige
3108    if {$expgui(globalmode) != 0} {
3109        wm title $w "Global Edit Absorption/Reflectivity" 
3110    } else {
3111        wm title $w "Edit Absorption/Reflectivity"
3112    }
3113   
3114    pack [frame $w.0 -bd 6 -relief groove  -bg beige \
3115            ] -side top -expand yes -fill both
3116    if {[llength $histlist] > 1} {
3117        grid [label $w.0.a \
3118            -text "Changing settings for histograms [CompressList $histlist]" \
3119            -bg beige] -row 0 -column 0 -columnspan 10
3120    } else {
3121        grid [label $w.0.a \
3122            -text "Changing settings for histogram $histlist" \
3123            -bg beige] -row 0 -column 0 -columnspan 4
3124        #grid columnconfig $w.0 4 -weight 1
3125    }
3126    grid rowconfig $w.0 1 -min 10
3127    set hist [lindex $histlist 0]
3128
3129    grid [label $w.0.lb1 -text "Absorption Coefficient(s)" -bg beige] \
3130            -row 2 -column 1  -columnspan 2
3131    grid [label $w.0.lb1a -text "1" -bg beige] -row 3 -column 1
3132    set expgui(abs2box1) $w.0.lb2a
3133    grid [label $w.0.lb2a -text "2" -bg beige] -row 3 -column 2
3134    grid [label $w.0.lb3 -text Absorption\nFunction -bg beige] \
3135            -row 2 -column 6 -rowspan 2 -columnspan 2
3136    grid [entry $w.0.ent1 -textvariable expgui(abscor1) -width 15] \
3137            -row 4 -column 1
3138    set expgui(abs2box2) $w.0.ent2
3139    grid [entry $w.0.ent2 -textvariable expgui(abscor2) -width 15] \
3140            -row 4 -column 2 
3141    trace vdelete expgui(abstype) w AbsSetoptmsg
3142    eval tk_optionMenu $w.0.m1 expgui(abstype) 0 1 2 3 4
3143    trace variable expgui(abstype) w AbsSetoptmsg
3144    grid $w.0.m1 -row 4 -column 6 -columnspan 2
3145    grid [label $w.0.lb8 -textvariable expgui(opttxt) -bg beige \
3146          -wrap 300 -justify left] -row 5 -column 1  -sticky ne -columnspan 7
3147    grid rowconfig $w.0 5 -min 100
3148    # set the values, note the trace on abstype
3149    foreach var {abscor1 abscor2 abstype} {
3150        set expgui($var) [histinfo $hist $var]
3151    }
3152
3153    pack [frame $w.b -bg beige] -fill x -expand yes -side top
3154    grid [button $w.b.2 -text Continue -command "AbsSaveEdit $w [list $histlist]"] \
3155            -row 0 -column 1
3156    grid [button $w.b.3 -text Cancel \
3157            -command "destroy $w"] -row 0 -column 2
3158    grid [button $w.b.help -text Help -bg yellow \
3159            -command "MakeWWWHelp expgui3.html EditAbsorption"] \
3160            -row 0 -column 4
3161    grid columnconfig $w.b 0 -weight 1
3162    grid columnconfig $w.b 3 -weight 1
3163    bind $w <Key-F1> "MakeWWWHelp expgui3.html EditAbsorption"
3164    bind $w <Return> "destroy $w"
3165
3166    # force the window to stay on top
3167    putontop $w
3168
3169    focus $w.b.2
3170    tkwait window $w
3171    afterputontop
3172}
3173
3174proc AbsSetoptmsg {args} {
3175    global expgui
3176    array set opttxt {
3177        0 "Correction for cylindrical samples [Lobanov & Alte da Veiga]. OK for all data types, but not for Bragg-Brentano flat-plate geometry. Set term 1 to mu*R/lambda (TOF: mu*R for lambda=1). For CW x-ray/neutron, do not refine!"
3178        1 "Wavelength-dependent correction for container penetration. Use with TOF & Energy Disp x-ray only."
3179        2 "Surface roughness correction [Pitschke, Hermann & Muttern]. Use with flat-plate reflection geometry (usually Bragg-Brentano) only."
3180        3 "Surface roughness correction, [Suortti]. Use with flat-plate reflection geometry (usually Bragg-Brentano) only."
3181        4 "Flat plate samples in transmission mode. OK for all data types, but not Bragg-Brentano geometry. Term 2 is angle w/r to beam (usually 0). For CW, do not refine."
3182    }
3183    set expgui(opttxt) ""
3184    catch {set expgui(opttxt) [set opttxt($expgui(abstype))]}
3185    switch $expgui(abstype) {
3186        0 -
3187        1 {
3188            $expgui(abs2box1) config -fg gray
3189            $expgui(abs2box2) config -state disabled -fg gray
3190        } 
3191        2 -
3192        3 -
3193        4 {
3194            $expgui(abs2box1) config -fg black
3195            $expgui(abs2box2) config -state normal -fg black
3196        }
3197        default {
3198            set expgui(opttxt) "Please select an absorption function"
3199        }
3200    }
3201}
3202proc AbsSaveEdit {top histlist} {
3203    global expgui expmap
3204    # sanity check: look at the histogram type
3205    set h [lindex $histlist 0]
3206    if {[string range $expmap(htype_$h) 2 2] == "T"} {set flag 1}
3207    if {[string range $expmap(htype_$h) 1 2] == "NC"} {set flag 2}
3208    if {[string range $expmap(htype_$h) 1 2] == "XC" && \
3209            [histinfo $h lam2] != 0.0} {set flag 3}
3210    if {[string range $expmap(htype_$h) 1 2] == "XC" && \
3211            [histinfo $h lam2] == 0.0} {set flag 4}
3212    if {[string range $expmap(htype_$h) 1 2] == "XE"} {set flag 5}
3213
3214    set msg {}
3215    if {$expgui(abstype) == 0 && ($flag == 3 || $flag == 4)} {
3216        set msg "Mode 0 is appropriate for cylindrical (Debye-Scherrer) geometry only"
3217    } elseif {$expgui(abstype) == 1 && ($flag != 1 && $flag != 5)} {
3218        set msg "Mode 1 is appropriate for wavelength-dispersive (TOF/E.D. X-ray) data only"
3219    } elseif {($expgui(abstype) == 2 || $expgui(abstype) == 3) \
3220            && $flag != 3 && $flag != 4} {
3221        set msg "Mode 1 is appropriate for reflection geometry flat-plate (typically Bragg-Brentano) data only"
3222    } elseif {$expgui(abstype) == 4 && $flag <= 3} {
3223        set msg "Mode 4 is appropriate for flat-plate samples in transmission"
3224    }
3225    if {$msg != ""} {
3226        set result [\
3227                MyMessageBox -parent $top -title "Sanity check" \
3228                -type okcancel -default cancel \
3229                -icon warning -helplink "expgui3.html AbsorptionSanity" \
3230                -message "$msg  -- are you sure you want to do this?"]
3231        if {$result == "cancel"} return
3232    }
3233
3234    # validate abscor1 & abscor2 (if needed)
3235    set msg {}
3236    if {![validreal expgui(abscor1) 15 8]} {
3237        set msg "Term 1 is invalid"
3238    }
3239    if {$expgui(abstype) > 1} {
3240        if {![validreal expgui(abscor2) 15 8]} {
3241            if {$msg != ""} {append msg "\n"}
3242            append msg "Term 2 is invalid"
3243        }
3244    }
3245    if {$msg != ""} {
3246        MyMessageBox -parent $top -title "Entry error" \
3247                -type ok -default ok \
3248                -icon warning -helplink "" \
3249                -message "Invalid data entered. Please correct.\n$msg"
3250        return
3251    }
3252   
3253    histinfo $histlist abstype set $expgui(abstype)
3254    RecordMacroEntry "histinfo [list $histlist] abstype set $expgui(abstype)" 0
3255    histinfo $histlist abscor1 set $expgui(abscor1)
3256    RecordMacroEntry "histinfo [list $histlist] abscor1 set $expgui(abscor1)" 0
3257    if {$expgui(abstype) > 1} {
3258        histinfo $histlist abscor2 set $expgui(abscor2)
3259        RecordMacroEntry "histinfo [list $histlist] abscor2 set $expgui(abscor2)" 0
3260    } else {
3261        histinfo $histlist abscor2 set 0.
3262        RecordMacroEntry "histinfo [list $histlist] abscor2 set 0." 0
3263    }
3264    # turn off refinement, just in case they didn't read
3265    if {($expgui(abstype) == 0 || $expgui(abstype) == 1 || $expgui(abstype) == 4) \
3266            && ($flag != 1 && $flag != 5)} {
3267        histinfo $histlist absref set 0
3268        RecordMacroEntry "histinfo [list $histlist] absref set 0" 0
3269    }
3270    incr expgui(changed)
3271    RecordMacroEntry "incr expgui(changed)" 0
3272    destroy $top
3273    DisplayHistogram
3274}
3275
3276##############################################################################
3277##                               #############################################
3278## END OF THE PROCEDURES SECTION #############################################
3279##                               #############################################
3280##############################################################################
3281
3282# <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
3283# <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<                          <<<<<<<<<<<<<<<<<<<
3284# <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<   BEGIN:  GUI SECTION    >>>>>>>>>>>>>>>>>>>
3285# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>                          >>>>>>>>>>>>>>>>>>>
3286# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
3287# A frame for menu items at top of display
3288set expgui(fm) [frame .fm -relief raised -borderwidth 2 -width 150 -height 40]
3289# Pack the menu frame.
3290pack $expgui(fm) -fill x -side top -anchor n
3291
3292# create a button bar
3293pack [frame .bar -relief raised -bd 2 -bg beige] -fill x -side top -anchor n
3294
3295# Creating the notebook and panes
3296
3297# create an array element describing each notebook page
3298# element 0 -- pane name
3299#         1 -- Label on frame
3300#         2 -- initialization command
3301#         3 -- update command
3302#         4 -- 0/1 Use 1 if pane should be disabled in when all histograms
3303#                are selected in global mode, 0 otherwise
3304#         5 -- Web page for pane
3305#         6 -- name anchor on Web page for pane
3306set expgui(notebookpagelist) {
3307    {lsFrame     "LS Controls" \
3308            "" \
3309            SetupExtractHist \
3310            0  expgui1.html ""}
3311    {phaseFrame   Phase        \
3312            "" \
3313            {SelectOnePhase $expgui(curPhase)} \
3314            0  expgui2.html ""}
3315    {histFrame    Powder \
3316            MakeHistPane \
3317            DisplayHistogram \
3318            1  expgui3.html ""}
3319    {fracFrame    Scaling \
3320            MakeScalingPane \
3321            DisplayFrac \
3322            0  expgui4.html ""}
3323    {profFrame    Profile \
3324            MakeProfilePane \
3325            DisplayProfile \
3326            1  expgui5.html ""}
3327    {consFrame    "Re/Constraints" \
3328            "source [file join $expgui(scriptdir) atomcons.tcl]; MakeConstraintsPane" \
3329            DisplayConstraintsPane \
3330            0  expgui6.html ""}
3331    {rbFrame   "Rigid Body" \
3332            "source  [file join $expgui(scriptdir) rigid.tcl]; MakeRBPane" \
3333            DisplayRB \
3334            1  expgui6A.html ""}
3335    {orientFrame  "MD Pref Orient" \
3336            MakeOrientPane \
3337            DisplayOrient \
3338            0  expgui7.html MD}
3339    {odfFrame  "SH Pref Orient" \
3340            "source [file join $expgui(scriptdir) odf.tcl]; MakeODFPane" \
3341            DisplayODFPane \
3342            0  expgui7.html ODF}
3343}
3344
3345# future expansion: single crystal panel goes here -- if present
3346catch {
3347   source  [file join $expgui(scriptdir) single.tcl]
3348   set expgui(notebookpagelist) [linsert $expgui(notebookpagelist) 3 \
3349       {singFrame   Single \
3350       "MakeSinglePane" \
3351       DisplaySingle \
3352       1  expguiSing.html ""}]
3353} err
3354
3355
3356pack [NoteBook .n -bd 2] -expand yes -fill both
3357# this should not be needed, but for some reason NoteBook is not
3358# using the optionDB
3359catch {.n configure -font [option get .n font Canvas]}
3360foreach item $expgui(notebookpagelist) {
3361    set frm [lindex $item 0]
3362    set expgui($frm) [\
3363            .n insert end $frm -text [lindex $item 1] \
3364            -createcmd "set expgui(pagenow) $frm; [lindex $item 2]" \
3365            -raisecmd "set expgui(pagenow) $frm; [lindex $item 3]"]
3366
3367    # at this time expgui(frameactionlist) is generated
3368    # from expgui(notebookpagelist), but in the future it might
3369    # make sense to use expgui(notebookpagelist) directly
3370    lappend expgui(frameactionlist) "$frm [list [lindex $item 3]]"
3371   
3372    # panes to disable in global "all" mode
3373    if {[lindex $item 4]} {
3374        lappend expgui(GlobalModeAllDisable) "$frm \{.n itemconfigure $frm\}"
3375    }
3376}
3377
3378set expgui(startpanel) 2
3379# procedure to disable tabs when phases or histograms are not defined
3380proc StageTabUse {args} {
3381    global expgui
3382    set expgui(startpanel) 2
3383    # reset everything
3384    foreach item [lrange $::expgui(notebookpagelist) 0 end] {
3385        set frm [lindex $item 0]
3386        .n itemconfigure $frm -state normal
3387    }
3388    # disable the unallowed pages in all mode
3389    if {$expgui(globalmode) == 6} {
3390        foreach pair $expgui(GlobalModeAllDisable) {
3391            if {$expgui(pagenow) == [lindex $pair 0]} {
3392                RaisePage lsFrame
3393            }
3394            eval [lindex $pair 1] -state disabled
3395        }
3396    } else {
3397        foreach pair $expgui(GlobalModeAllDisable) {
3398            eval [lindex $pair 1] -state normal
3399        }
3400    }
3401    # no phases are present, one must add a phase 1st
3402    if {[llength $::expmap(phaselist)] == 0} {
3403        foreach item [lrange $::expgui(notebookpagelist) 2 end] {
3404            set frm [lindex $item 0]
3405            .n itemconfigure $frm -state disabled
3406        }
3407        set expgui(startpanel) 0
3408        return
3409    }
3410    # do any of the phases have atoms?
3411    set flag 1
3412    foreach phase $::expmap(phaselist) {
3413        if {[array names ::expmap atomlist_$phase] != ""} {
3414            if {[llength $::expmap(atomlist_$phase)] > 0} {
3415                set flag 0
3416                break
3417            }
3418        }
3419    }
3420    # no atoms are present, one must add at least one before continuing
3421    if $flag {
3422        foreach item [lrange $::expgui(notebookpagelist) 2 end] {
3423            set frm [lindex $item 0]
3424            .n itemconfigure $frm -state disabled
3425        }
3426        set expgui(startpanel) 0
3427        return
3428    }
3429
3430    # no data is present, one must add a histogram next
3431    if {[llength $::expmap(nhst)] == 0} {
3432        foreach item [lrange $::expgui(notebookpagelist) 4 end] {
3433            set frm [lindex $item 0]
3434            .n itemconfigure $frm -state disabled
3435        }
3436        set expgui(startpanel) 1
3437        return
3438    }
3439}
3440# expgui(mapstat) is set by mapexp when it is called
3441# mapexp will be called when the .EXP file is changed (addition of phases, atoms or histograms)
3442trace variable expgui(mapstat) w StageTabUse
3443
3444# this is used to bring up the selected frame
3445proc RaisePage {nextpage} {
3446    global expgui
3447    set expgui(pagenow) $nextpage
3448    .n see $nextpage
3449    .n raise $nextpage
3450}
3451
3452proc SetStartPanel {} {
3453    global expgui
3454    if {$expgui(startpanel) == 0} {
3455        RaisePage phaseFrame
3456    } elseif {$expgui(startpanel) == 1} {
3457        RaisePage histFrame
3458    } else {
3459        RaisePage lsFrame
3460    }
3461}
3462
3463# resize the notebook to fit all the tabs and the largest page
3464proc ResizeNotebook {} {
3465    global expgui
3466    .n compute_size
3467}
3468
3469#----------------------------------------------------------------------------
3470proc MakePhasePane {} {
3471    #\/ \/ \/ \/ \/ \/ \/ BEGINNING OF PHASE PANE CODE \/ \/ \/ \/ \/ \/ \/
3472    global expgui entryvar entrybox entrycmd
3473    frame $expgui(phaseFrame).top
3474    set frameLatt [frame $expgui(phaseFrame).frameLatt]
3475    #  This is a big frame in the Phase notebook pane to hold atomic data.
3476    set fbig [frame $expgui(phaseFrame).fbig -width 180 \
3477            -relief raised -borderwidth 4 -class Coord]
3478    #  This is a frame just below the big frame: for edits
3479    set frame3 [frame $expgui(phaseFrame).frame3 -width 100 \
3480            -relief raised -borderwidth 4 -bg $expgui(bkgcolor1)]
3481
3482    grid $expgui(phaseFrame).top -sticky news -row 0 -column 0 
3483    grid $frameLatt -sticky news -row 2 -column 0 
3484    grid $fbig -sticky news -row 3 -column 0 
3485    # give extra space to the atoms box
3486    grid columnconfigure $expgui(phaseFrame) 0 -weight 1
3487    grid rowconfigure $expgui(phaseFrame) 3 -weight 1
3488    grid $frame3 -sticky news -row 4 -column 0 
3489    grid columnconfigure $expgui(phaseFrame) 0 -weight 1
3490    grid rowconfigure $expgui(phaseFrame) 3 -weight 1
3491    grid [frame  $expgui(phaseFrame).top.ps] -column 0 -row 0 -sticky w
3492    # this is where the buttons will go
3493    pack [label $expgui(phaseFrame).top.ps.0 -text "No Phases"] -side left
3494   
3495    grid [label $expgui(phaseFrame).top.lA -text title: \
3496            -fg blue ] -column 1 -row 0 -sticky e
3497    grid [entry $expgui(phaseFrame).top.lB -textvariable entryvar(phasename) \
3498            -fg blue -width 45] -column 2 -row 0 -sticky e
3499    grid columnconfigure $expgui(phaseFrame).top 1 -weight 1
3500    # ------------- Lattice Parameter Box ------------------
3501    set row 0
3502    foreach col {2 4 6} var {a b c} lbl {a b c} {
3503        grid [label $frameLatt.l$var -text $lbl] \
3504                -column $col -row $row -padx 5 -sticky e
3505        incr col
3506        grid [label $frameLatt.e$var -textvariable entryvar($var) \
3507                -relief groove -bd 2 -width 10] \
3508                -column $col -row $row -padx 5
3509#       grid [entry $frameLatt.e$var -textvariable entryvar($var) -width 10] \
3510#           -column $col -row $row -padx 5
3511#       set entrybox($var) $frameLatt.e$var
3512    }
3513    incr row
3514    foreach col {2 4 6} var {alpha beta gamma} lbl {a b g} {
3515        grid [label $frameLatt.l$var -text $lbl] \
3516                -column $col -row $row -padx 5 -sticky e
3517        set font [$frameLatt.l$var cget -font]
3518        $frameLatt.l$var config -font "Symbol [lrange $font 1 end]"
3519
3520        incr col
3521        grid [label $frameLatt.e$var -textvariable entryvar($var)\
3522                -relief groove -bd 2 -width 10] \
3523            -column $col -row $row -padx 5
3524#       grid [entry $frameLatt.e$var -textvariable entryvar($var) -width 10] \
3525#           -column $col -row $row -padx 5
3526#       set entrybox($var) $frameLatt.e$var
3527    }
3528   
3529    grid [button $frameLatt.edit -text "Edit\nCell" -command EditCellConstants] \
3530            -column 8 -row 0 -rowspan 2 -padx 5 -sticky e
3531    grid [label $frameLatt.lr -text "Refine Cell"] -column 9 -row 0 -padx 5 -sticky e
3532    grid [label $frameLatt.ld -text "Cell damping"] -column 9 -row 1 -padx 5 -sticky e
3533    set cFlag [checkbutton $frameLatt.c -text "" -variable entryvar(cellref)]
3534    grid $cFlag -column 10 -row 0 -padx 5 -sticky e
3535    tk_optionMenu $frameLatt.om entryvar(celldamp) 0 1 2 3 4 5 6 7 8 9
3536    grid $frameLatt.om -column 10 -row 1 -padx 5 -sticky e
3537    grid [label $frameLatt.phasetype -textvariable expgui(phasetype) -fg blue] \
3538            -column 1 -row 0 -rowspan 2
3539    if [file executable $expgui(exptool)] {
3540        grid [button $expgui(phaseFrame).frameLatt.newp \
3541                -text "Add\nPhase" -padx 1.5m -command MakeAddPhaseBox \
3542                ] -column 0 -row 0 -rowspan 2 -sticky w
3543    }
3544    grid columnconfig $frameLatt  1 -weight 1
3545    grid columnconfig $frameLatt  0 -weight 1
3546    #-------------- Begin Atom Coordinates Box  ------------------------------
3547    grid [listbox  $fbig.title -height 1 -relief flat \
3548            -exportselection 0 -bg lightgrey -fg black \
3549            -selectforeground black -selectbackground lightgrey] \
3550            -row 0 -column 0 -sticky ew
3551    set expgui(atomtitle) $fbig.title
3552    bind $expgui(atomtitle) <Button-1> {
3553        set i [lsearch {number type mult x y z occupancy} $expgui(asorttype)]
3554        incr i
3555        set expgui(asorttype) [lindex {number type mult x y z occupancy number} $i]
3556        DisplayAllAtoms $expgui(curPhase)
3557    }
3558    bind $expgui(atomtitle) <Button-3> {set expgui(asorttype) number; DisplayAllAtoms $expgui(curPhase)}
3559
3560    $expgui(atomtitle) configure -selectmode extended
3561    grid [listbox   $fbig.lbox -height 10 \
3562            -exportselection 0 \
3563            -xscrollcommand " $fbig.bscr set"\
3564            -yscrollcommand " $fbig.rscr set"\
3565            ] -row 1 -column 0 -sticky news
3566    set expgui(atomlistbox) $fbig.lbox
3567    $expgui(atomlistbox) configure -selectmode extended
3568    grid [scrollbar $fbig.bscr -orient horizontal \
3569            -command "move2boxesX \" $fbig.title $fbig.lbox \" " \
3570            ] -row 2 -column 0 -sticky ew
3571    grid [scrollbar $fbig.rscr  -command "$fbig.lbox yview" \
3572            ] -row 1 -column 1 -sticky ns
3573    # give extra space to the atoms box
3574    grid columnconfigure $fbig 0 -weight 1
3575    grid rowconfigure $fbig 1 -weight 1
3576   
3577    #   BIND mouse in editbox
3578    bind $expgui(atomlistbox) <ButtonRelease-1>   editRecord
3579    bind $expgui(atomlistbox) <Button-3>   SelectAllAtoms
3580   
3581    #-------------- End Atoms Section  ---------------------------------
3582
3583    # --------------------------- Begin Edit Box ------------------------
3584    grid [set expgui(EditingAtoms) [label $frame3.top -bg $expgui(bkgcolor1) -fg blue]] \
3585            -column 0 -row 0 -padx 2 -pady 3 -columnspan 10 -sticky w
3586    if [file executable $expgui(exptool)] {
3587        button $frame3.newa -text "Add New Atoms" \
3588                -bg $expgui(bkgcolor1) -highlightthickness 0 \
3589                -command {MakeAddAtomsBox $expgui(curPhase)}
3590        grid $frame3.newa -column 11 -row 0
3591        set expgui(AddAtomBut) $frame3.newa
3592    }
3593    button [set expgui(atomxform) $frame3.xa] \
3594            -bg $expgui(bkgcolor1) -highlightthickness 0 \
3595            -command {MakeXformAtomsBox $expgui(curPhase)}
3596    grid $expgui(atomxform) -column 11 -row 1 -sticky ew
3597
3598    set f3l1 [label $frame3.l1 -text "Refinement Flags:" -bg $expgui(bkgcolor1)]
3599    grid $f3l1 -column 0 -row 1 -padx 2 -sticky nsw -pady 3
3600    foreach lbl {X U F} var {xref uref fref} col {1 2 3} {
3601        grid [checkbutton $frame3.cf$col \
3602                -text $lbl -variable entryvar($var) \
3603                -bg $expgui(bkgcolor1) -highlightthickness 0 \
3604                -activebackground $expgui(bkgcolor1)] \
3605                -column $col -row 1 -padx 4 -pady 3 -sticky w
3606    }
3607    set f3l4 [label $frame3.l4 -text "  Damping:" -bg $expgui(bkgcolor1)]
3608    grid $f3l4 -column 4 -row 1 -padx 2 -sticky nsw -pady 3
3609   
3610    set col 4
3611    foreach var {xdamp udamp fdamp} num {2 3 4} lbl {X U F} {
3612        grid [label $frame3.lom$num -text $lbl \
3613                -bg $expgui(bkgcolor1)] \
3614                -column [incr col] -row 1 -padx 2 -pady 3 -sticky w
3615        tk_optionMenu $frame3.om$num entryvar($var) 0 1 2 3 4 5 6 7 8 9
3616        $frame3.om$num config -highlightthickness 0
3617        grid $frame3.om$num -column [incr col] -row 1 -padx 2 -pady 3 -sticky w
3618    }
3619    set expgui(atomreflbl) "$frame3.l1 $frame3.l4 $frame3.lom2 $frame3.lom3 $frame3.lom4 "
3620    set expgui(atomref) "$frame3.cf1 $frame3.cf2 $frame3.cf3 $frame3.om2 $frame3.om3 $frame3.om4"
3621   
3622    set coords [frame $frame3.coords  -width 100 -borderwidth 0  -bg $expgui(bkgcolor1)]
3623    grid $coords -column 0 -row 6 -columnspan 12 -sticky nsew
3624   
3625    set f3l1 [label $frame3.coords.l1 -text "Label" -bg $expgui(bkgcolor1)]
3626    grid $f3l1 -column 0 -row 4 -padx 2 -sticky nsw -pady 3
3627    set expgui(atomlabels) $f3l1
3628
3629    set f3e1 [entry  $frame3.coords.e1 -textvariable entryvar(label) -width 6]
3630    grid $f3e1 -column 1 -row 4 -padx 2 -sticky nsw -pady 3
3631    set expgui(atomentry) $f3e1
3632
3633    set f3l8 [label $frame3.coords.l8 -text "Coordinates" -bg $expgui(bkgcolor1)]
3634    grid $f3l8 -column 2 -row 4 -padx 2 -sticky nsw -pady 3
3635    lappend expgui(atomlabels) $f3l8
3636    set f3l11 [label $frame3.coords.l11 -text "Occupancy" -bg $expgui(bkgcolor1)]
3637    grid $f3l11 -column 6 -row 4 -padx 2 -sticky nsw -pady 3
3638    lappend expgui(atomlabels) $f3l11
3639
3640    foreach var {x y z frac} col {3 4 5 7} {
3641        set entrybox($var) [entry $frame3.coords.e$var \
3642                -textvariable entryvar($var) -width 10]
3643        grid $entrybox($var) -column $col -row 4 -padx 2 -sticky nsw -pady 3
3644        lappend expgui(atomentry) $entrybox($var)
3645    }
3646
3647
3648    set f3f31 [frame $frame3.f3f31  -width 100 -borderwidth 0 -bg $expgui(bkgcolor1)]
3649    grid $f3f31 -column 0 -row 7 -columnspan 12
3650    set expgui(anisolabels) {}
3651    foreach lbl {13 14 15 16 17 18} txt {Uiso U22 U33 U12 U13 U23} {
3652        lappend expgui(anisolabels)  [\
3653                label $f3f31.l$lbl -text $txt -bg $expgui(bkgcolor1)
3654        ]
3655    }
3656    set expgui(anisoentry) {}
3657    foreach i {e13 e14 e15 e16 e17 e18} var {U11 U22 U33 U12 U13 U23} { 
3658        lappend expgui(anisoentry) [\
3659                entry $f3f31.$i -textvariable entryvar($var) \
3660                -width 10]
3661        set entrybox($var) $f3f31.$i
3662    }
3663   
3664    set col 0
3665    foreach item1 $expgui(anisolabels) item2 $expgui(anisoentry) {
3666        grid $item1 -column $col -row 0 -sticky nsw -pady 3
3667        incr col
3668        grid $item2 -column $col -row 0 -sticky nsw -pady 3
3669        incr col
3670    }
3671    # --------------------------- End Edit Box -------------------------
3672   
3673    #/\ /\ /\ /\ /\ /\ /\ END OF PHASE PANE CODE /\ /\ /\ /\ /\ /\ /\ /\ /
3674    # resize in case the pane needs more space
3675    ResizeNotebook
3676}
3677
3678# called to create a window for editing unit cell constants
3679proc EditCellConstants {} {
3680    global expgui entrybox
3681    set spg [phaseinfo $expgui(curPhase) spacegroup]
3682    set laueaxis [GetLaue $spg]
3683    set vary ""
3684    set equivL ""
3685    set equivA ""
3686    switch -exact $laueaxis {
3687        1bar {set vary "a b c alpha beta gamma"}
3688        2/ma {set vary "a b c alpha"}
3689        2/mb {set vary "a b c beta"}
3690        2/mc {set vary "a b c gamma"}
3691        mmm  {set vary "a b c"}
3692        4/m  -
3693        4/mmm {set vary "a c"; set equivL "a b"} 
3694        3barR     -
3695        "3bar mR" {
3696            set vary "a alpha"
3697            set equivL "a b c"
3698            set equivA "alpha beta gamma"
3699        }
3700        3bar    -
3701        3barm1  -
3702        3bar1m  -
3703        6/m     -
3704        6/mmm  {set vary "a c";set equivL "a b"}
3705        "m 3"  -
3706        m3m    {set vary a;set equivL "a b c"}
3707        default {
3708            MyMessageBox -parent . -title "Laue problem" \
3709                    -message "Error processing Laue code: $laueaxis\nError in space group \"$spg\"?\nUnable to edit cell. Fix or use EXPEDT." \
3710                    -icon warning -type OK -default ok \
3711                    -helplink "expguierr.html BadLaue"
3712        }
3713    }
3714    set row 0
3715    set w .cell
3716    toplevel $w -bg beige
3717    wm title $w "Edit Cell Parameters" 
3718#    bind $w <Key-F1> "MakeWWWHelp expgui3.html EditBackground"
3719    bind $w <Return> "set expgui(temp) 1; destroy $w"
3720    pack [label $w.l1 -bg yellow -anchor center -justify center \
3721            -text "Edit unit cell parameters for phase #$expgui(curPhase)" \
3722            ] -side top -expand yes -fill both
3723    pack [label $w.l2 -bg beige -justify left \
3724            -text "title: [phaseinfo $expgui(curPhase) name]\nSpace group: $spg\nLaue class: $laueaxis" \
3725            ] -side top -expand yes -fill both
3726    pack [frame $w.0 -bd 6 -relief groove  -bg beige \
3727            ] -side top -expand yes -fill both
3728    pack [frame $w.b -bg beige] -fill x -expand yes -side top
3729    grid [button $w.b.2 -text Continue -command "set expgui(temp) 1; destroy $w"] -row 0 -column 1
3730    grid [button $w.b.3 -text Cancel \
3731            -command "set expgui(temp) 0; destroy $w"] -row 0 -column 2
3732#    grid [button $w.b.help -text Help -bg yellow \
3733#           -command "MakeWWWHelp expgui3.html EditBackground"] \
3734#           -row 0 -column 4
3735
3736    global tmpvar
3737    trace variable tmpvar w TestCellEdit
3738    foreach ent {a b c alpha beta gamma} {
3739        set tmpvar($ent) [phaseinfo $expgui(curPhase) $ent]
3740    }
3741
3742    set frameLatt $w.0
3743    foreach col {2 4 6} var {a b c} lbl {a b c} {
3744        grid [label $frameLatt.l$var -text $lbl -bg beige] \
3745                -column $col -row $row -padx 5 -sticky e
3746        incr col
3747        if {[lsearch $equivL $var] == -1} {
3748            set v $var
3749        } else {
3750            set v [lindex $equivL 0]
3751        }
3752        if {[lsearch $vary $var] == -1} {
3753            grid [label $frameLatt.e$var -textvariable tmpvar($v) \
3754                    -width 10 -bg beige] \
3755                    -column $col -row $row -padx 5
3756        } else {
3757            grid [entry $frameLatt.e$var -textvariable tmpvar($v) \
3758                    -width 10] -column $col -row $row -padx 5
3759            set entrybox($var) $frameLatt.e$var 
3760        }
3761    }
3762    incr row
3763    foreach col {2 4 6} var {alpha beta gamma} lbl {a b g} {
3764        grid [label $frameLatt.l$var -text $lbl -bg beige] \
3765                -column $col -row $row -padx 5 -sticky e
3766        set font [$frameLatt.l$var cget -font]
3767        $frameLatt.l$var config -font "Symbol [lrange $font 1 end]"
3768
3769        incr col
3770        if {[lsearch $equivA $var] == -1} {
3771            set v $var
3772        } else {
3773            set v [lindex $equivA 0]
3774        }
3775        if {[lsearch $vary $var] == -1} {
3776            grid [label $frameLatt.e$var -textvariable tmpvar($v)\
3777                    -width 10 -bg beige] \
3778                    -column $col -row $row -padx 5
3779        } else {
3780            grid [entry $frameLatt.e$var -textvariable tmpvar($v) \
3781            -width 10] -column $col -row $row -padx 5
3782            set entrybox($var) $frameLatt.e$var 
3783        }
3784    }
3785    putontop $w
3786    tkwait window $w
3787    afterputontop
3788    global entryvar
3789    set change 0
3790    if {$expgui(temp)} {
3791        foreach var {a b c} {
3792            if {[lsearch $equivL $var] == -1} {
3793                set v $var
3794            } else {
3795                set v [lindex $equivL 0]
3796            }
3797            catch {
3798                expr [set val $tmpvar($v)]
3799                if {[phaseinfo $expgui(curPhase) $var] != $val} {
3800                    phaseinfo $expgui(curPhase) $var set $val
3801                    RecordMacroEntry "phaseinfo $expgui(curPhase) $var set $val" 0
3802                    set entryvar($var) $val
3803                    incr expgui(changed)                   
3804                    set change 1
3805                }
3806            }
3807        }
3808        foreach var {alpha beta gamma} {
3809            if {[lsearch $equivA $var] == -1} {
3810                set v $var
3811            } else {
3812                set v [lindex $equivA 0]
3813            }
3814            catch {
3815                expr [set val $tmpvar($v)]
3816                if {[phaseinfo $expgui(curPhase) $var] != $val} {
3817                    phaseinfo $expgui(curPhase) $var set $val
3818                    RecordMacroEntry "phaseinfo $expgui(curPhase) $var set $val" 0
3819                    set entryvar($var) $val
3820                    incr expgui(changed)                   
3821                    set change 1
3822                }
3823            }
3824        }
3825        RecordMacroEntry "incr expgui(changed)" 0
3826        if {$change} {
3827            # set the powpref warning (1 = suggested)
3828            if {$expgui(needpowpref) == 0} {set expgui(needpowpref) 1}
3829            append expgui(needpowpref_why) "\tCell parameters were changed\n"
3830        }
3831    }
3832    unset tmpvar
3833}
3834
3835# highlight errors in unit cell constants
3836proc TestCellEdit {var elem mode} {
3837    global tmpvar entrybox
3838    if {[catch {expr $tmpvar($elem)} errmsg]} {
3839        catch {$entrybox($elem) config -fg red}
3840    } else {
3841        catch {$entrybox($elem) config -fg black}
3842    }
3843}
3844
3845#-----------------------------------------------------------------------------
3846proc MakeHistPane {} {
3847    #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
3848    global expgui
3849   
3850    grid columnconfigure $expgui(histFrame) 0 -weight 1
3851    grid rowconfigure $expgui(histFrame) 1 -weight 1
3852    grid rowconfigure $expgui(histFrame) 2 -weight 1
3853    grid rowconfigure $expgui(histFrame) 3 -weight 1
3854
3855    grid [frame $expgui(histFrame).hs -class HistList] \
3856            -column 0 -row 0 -rowspan 10 -sticky nsew
3857    MakeHistBox $expgui(histFrame).hs
3858    bind $expgui(histFrame).hs.lbox <ButtonRelease-1>  {
3859        set expgui(curhist) [$expgui(histFrame).hs.lbox curselection]
3860        DisplayHistogram
3861    }
3862    bind $expgui(histFrame).hs.lbox <Button-3>  {
3863        if $expgui(globalmode) {
3864            $expgui(histFrame).hs.lbox selection set 0 end
3865            set expgui(curhist) [$expgui(histFrame).hs.lbox curselection]
3866            DisplayHistogram
3867        }
3868    }
3869   
3870    frame $expgui(histFrame).top -borderwidth 4 -relief groove
3871    grid [label $expgui(histFrame).top.txt] -row 0 -column 0
3872    foreach item {backBox diffBox absBox} num {2 3 4} title {Background "Diffractometer Constants" "Absorption/Reflectivity Correction"} {
3873        TitleFrame $expgui(histFrame).$item  \
3874            -borderwidth 4 -side left -relief groove -text $title
3875        set expgui($item) [$expgui(histFrame).$item getframe]
3876        grid $expgui(histFrame).$item -column 1 -row $num -sticky nsew
3877        grid rowconfigure $expgui(histFrame) $num -minsize 100
3878    }
3879    grid [frame $expgui(histFrame).bb] -column 1 -row 6
3880    if [file executable $expgui(exptool)] {
3881        button $expgui(histFrame).bb.newh -text "Add New\nHistogram" \
3882                -command MakeAddHistBox
3883        grid $expgui(histFrame).bb.newh -column 0 -row 1
3884    }
3885    button $expgui(histFrame).bb.excl \
3886            -text "Set Data Limits &\nExcluded Regions" -command excledit
3887    grid $expgui(histFrame).bb.excl -column 1 -row 1
3888
3889    button $expgui(histFrame).bb.use -text "Set Histogram\nUse Flags" \
3890            -command SetHistUseFlags
3891    grid $expgui(histFrame).bb.use -column 2 -row 1
3892
3893    # BACKGROUND information.
3894    # <<<<<<<<<<<<<<<<<<<<<<<<< BACKGROUND  <<<<<<<<<<<<<<<<<<<<<
3895    grid [frame $expgui(backBox).frm1 ] -row 0 -column 0  -columnspan 11
3896    grid [label $expgui(backBox).frm1.lBGType \
3897            -textvariable expgui(backtypelbl)] \
3898            -row 1 -column 0 -sticky nws  -padx 2 -pady 3
3899    grid [label $expgui(backBox).frm1.lBGTerms \
3900            -textvariable expgui(backtermlbl)] \
3901            -row 1 -column 1 -sticky nws  -padx 2 -pady 3
3902    grid [button $expgui(backBox).frm1.edit -textvariable expgui(bkglbl) \
3903            -command editbackground] \
3904            -row 1 -column 2 -columnspan 3 -sticky w -padx 2 -pady 3
3905    grid [frame $expgui(backBox).frm2 ] \
3906            -row 1 -column 0 -columnspan 11 -sticky e
3907    grid [label $expgui(backBox).frm2.lfBG -text "  Refine background" ] \
3908            -row 2 -column 1 -sticky news -padx 4 -pady 3
3909    grid [checkbutton $expgui(backBox).frm2.rfBG -text "" \
3910            -variable  entryvar(bref) ] \
3911            -row 2 -column 2 -sticky news -padx 4 -pady 3
3912    grid [label $expgui(backBox).frm2.lBGDamp -text Damping ] \
3913            -row 2 -column 3 -sticky w    -padx 2 -pady 3
3914    tk_optionMenu $expgui(backBox).frm2.om  entryvar(bdamp) 0 1 2 3 4 5 6 7 8 9
3915    grid $expgui(backBox).frm2.om \
3916            -row 2 -column 4 -sticky news -padx 4 -pady 3 -sticky e
3917    # Absorption information.
3918    set expgui(abstypelbl) ""
3919    grid [label $expgui(absBox).lbl1 \
3920              -textvariable expgui(abstypelbl)] \
3921            -row 1 -column 1 -columnspan 5 -sticky nws
3922    grid [label $expgui(absBox).rf1 -text "  Refine Abs./Refl." ] \
3923            -row 2 -column 1 -sticky news -padx 2 -pady 3
3924    grid [checkbutton $expgui(absBox).rf2 -text "" \
3925            -variable  entryvar(absref) ] \
3926            -row 2 -column 2 -sticky news -padx 4 -pady 3
3927    grid [label $expgui(absBox).d1 -text Damping ] \
3928            -row 2 -column 3 -sticky w    -padx 2 -pady 3
3929    tk_optionMenu $expgui(absBox).d2  entryvar(absdamp) 0 1 2 3 4 5 6 7 8 9
3930    grid $expgui(absBox).d2 \
3931        -row 2 -column 4 -sticky news -padx 4 -pady 3 -sticky e
3932    grid [button $expgui(absBox).edit -textvariable expgui(abslbl) \
3933              -command editabsorption] \
3934        -row 2 -column 5 -sticky nsw -padx 2 -pady 3
3935
3936    #^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^END OF HISTOGRAM PANE CODE ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
3937    # insert the histograms & resize in case the pane needs more space   
3938    sethistlist
3939    ResizeNotebook
3940}
3941###############################################################################
3942proc MakeScalingPane {} {
3943    #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
3944    global expgui entryvar entrybox
3945
3946    pack [frame $expgui(fracFrame).hs -class HistList] \
3947            -side left -expand y -fill both
3948    MakeHistBox $expgui(fracFrame).hs
3949    bind $expgui(fracFrame).hs.lbox <ButtonRelease-1> {
3950        set expgui(curhist) [$expgui(fracFrame).hs.lbox curselection]
3951        DisplayFrac
3952    }
3953    bind $expgui(fracFrame).hs.lbox <Button-3>  {
3954        if $expgui(globalmode) {
3955            $expgui(fracFrame).hs.lbox selection set 0 end
3956            set expgui(curhist) [$expgui(fracFrame).hs.lbox curselection]
3957            DisplayFrac
3958        }
3959    }
3960
3961    pack [frame $expgui(fracFrame).f1] -fill both -expand true
3962    # Create a large canvas area containing a frame for each phase in the data set.
3963    # The canvas and vertical scrollbar are inside a frame called f1
3964    TitleFrame $expgui(fracFrame).f1.scaleBox \
3965        -borderwidth 4 -text "Scale Factor"
3966    #       -borderwidth 4 -width 600 -height 100 -label "Scale Factor"
3967    grid $expgui(fracFrame).f1.scaleBox -column 0 -row 0 -sticky nsew -columnspan 2
3968    set expgui(scaleBox)  [$expgui(fracFrame).f1.scaleBox getframe]
3969    grid [label $expgui(scaleBox).histSFLabel -text Scale] \
3970        -row 1 -column 0 -sticky nws  -padx 2 -pady 3
3971    grid [entry $expgui(scaleBox).ent1 -textvariable entryvar(scale) -width 15] \
3972            -row 1 -column 1 -sticky ew -padx 4 -pady 3
3973    set entrybox(scale) $expgui(scaleBox).ent1
3974
3975    button $expgui(scaleBox).but1 -text "Set Globally" \
3976            -command "editglobalparm histinfo scale {Scale Factor}"
3977
3978    grid [label $expgui(scaleBox).histSFRLabel -text " Refine"] \
3979            -row 1 -column 2 -sticky nws  -padx 2 -pady 3
3980    grid [checkbutton $expgui(scaleBox).rf -variable entryvar(sref)] \
3981            -row 1 -column 3 -sticky news -padx 4 -pady 3
3982    grid [label $expgui(scaleBox).lD1 -text "Damping"] \
3983            -row 1 -column 4 -sticky w    -padx 2 -pady 3
3984    tk_optionMenu $expgui(scaleBox).om entryvar(sdamp) 0 1 2 3 4 5 6 7 8 9
3985    grid $expgui(scaleBox).om \
3986            -row 1 -column 5 -sticky news -padx 4 -pady 3
3987    grid columnconfigure $expgui(scaleBox) 6  -weight 1
3988   
3989    grid [TitleFrame $expgui(fracFrame).f1.phaseFrac -bd 4 \
3990              -text "Phase Fractions" -relief groove] \
3991        -sticky news -row 1 -column 0 -columnspan 2
3992    set PhaseFractBox [$expgui(fracFrame).f1.phaseFrac getframe]
3993    grid columnconfigure $expgui(fracFrame).f1 0 -weight 1
3994    grid rowconfigure $expgui(fracFrame).f1 1 -weight 1
3995   
3996    grid [set expgui(FracBox) [canvas $PhaseFractBox.fracBox \
3997            -scrollregion {0 0 5000 500} \
3998            -yscrollcommand "$PhaseFractBox.yscroll set" \
3999            -width 500 -height 350 -bg lightgrey]] \
4000            -sticky  news -row 1 -column 0
4001    grid [scrollbar $PhaseFractBox.yscroll \
4002            -command "$expgui(FracBox) yview" \
4003            -orient vertical] \
4004            -sticky ns -row 1 -column 1
4005    frame $expgui(FracBox).f -bd 0
4006    $expgui(FracBox) create window 0 0 -anchor nw  -window $expgui(FracBox).f
4007
4008    # the rest of the page is created in DisplayFrac
4009
4010    # insert the histograms & resize in case the pane needs more space
4011    sethistlist
4012    ResizeNotebook
4013    # ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ END OF SCALING PANE CODE ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
4014}
4015###############################################################################
4016proc MakeProfilePane {} {
4017    global expgui
4018    # 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
4019    pack [frame $expgui(profFrame).hs -class HistList] \
4020            -side left -expand y -fill both
4021    MakeHistBox $expgui(profFrame).hs
4022    bind $expgui(profFrame).hs.lbox <ButtonRelease-1> {
4023        set expgui(curhist) [$expgui(profFrame).hs.lbox curselection]
4024        DisplayProfile
4025    }
4026    bind $expgui(profFrame).hs.lbox <Button-3>  {
4027        if $expgui(globalmode) {
4028            $expgui(profFrame).hs.lbox selection set 0 end
4029            set expgui(curhist) [$expgui(profFrame).hs.lbox curselection]
4030            DisplayProfile
4031        }
4032    }
4033
4034    # Create a large canvas area containing a frame for each phase in the data set.
4035    # The canvas and vertical scrollbar are inside a frame called f1
4036    pack [frame $expgui(profFrame).f1] -fill both -expand true
4037    grid [set expgui(ProfileBox) [canvas $expgui(profFrame).f1.profileBox \
4038            -scrollregion {0 0 5000 500} -width 500 -height 350 -bg lightgrey]] \
4039            -sticky  news -row 0 -column 0
4040    grid [scrollbar $expgui(profFrame).f1.yscroll -orient vertical] \
4041            -sticky ns -row 0 -column 1
4042   
4043    $expgui(ProfileBox) config -yscrollcommand "$expgui(profFrame).f1.yscroll set"
4044    $expgui(profFrame).f1.yscroll config -command { $expgui(ProfileBox) yview }
4045   
4046    grid columnconfigure $expgui(profFrame).f1 1 -weight 1
4047    grid rowconfigure $expgui(profFrame).f1 0 -weight 1
4048    frame $expgui(ProfileBox).f -bd 0
4049    $expgui(ProfileBox) create window 0 0 -anchor nw  -window $expgui(ProfileBox).f
4050   
4051    # insert the histograms & resize in case the pane needs more space
4052    sethistlist
4053    ResizeNotebook
4054    # ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ END OF PROFILE PANE CODE ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
4055}
4056
4057##############################################################################
4058# 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
4059array set printopts {
4060    0 "Print the reciprocal metric tensor changes"
4061    1 "Print the correlation matrix"
4062    2 "Print the Least-Squares matrices and vectors"
4063    4 "Print the linear constraint matrices"
4064    5 "Print the applied  shifts and shift factors"
4065    6 "Print the reciprocal metric tensor Var-Covar terms"
4066    7 "Print all parameters for each cycle"
4067    8 "Print summary shift/esd data after last cycle"
4068    9 "Print zero/unit pole figure constraint terms"
4069    10 "Output parameter name, value & esd to file"
4070}
4071pack [frame $expgui(lsFrame).hs -class HistList] \
4072        -side left -expand y -fill both
4073MakeHistBox $expgui(lsFrame).hs
4074bind $expgui(lsFrame).hs.lbox <ButtonRelease-1> {
4075    set expgui(curhist) [$expgui(lsFrame).hs.lbox curselection]
4076    SetupExtractHist
4077}
4078bind $expgui(lsFrame).hs.lbox <Button-3>  {
4079    if $expgui(globalmode) {
4080        $expgui(lsFrame).hs.lbox selection set 0 end
4081        set expgui(curhist) [$expgui(lsFrame).hs.lbox curselection]
4082        SetupExtractHist
4083    }
4084}
4085
4086pack [frame $expgui(lsFrame).f1] -fill both -expand true
4087set row 0
4088grid [label $expgui(lsFrame).f1.his1 -pady 6 -text "Last History:"] -row $row -column 0
4089grid [label $expgui(lsFrame).f1.his2 -relief groove -bd 2 -pady 6 \
4090        -textvariable expgui(last_History)] \
4091        -row $row -column 1 -columnspan 5 -sticky w
4092incr row
4093grid [label $expgui(lsFrame).f1.tit1 -pady 6 -text "Title:"] -row $row -column 0
4094grid [entry $expgui(lsFrame).f1.tit2 \
4095        -textvariable entryvar(title) -width 48] \
4096        -row $row -column 1 -columnspan 5 -sticky w
4097set entrycmd(title) "expinfo title"
4098
4099incr row
4100grid rowconfigure $expgui(lsFrame).f1 $row -weight 1
4101incr row
4102grid [frame $expgui(lsFrame).f1.b -bd 4 -relief groove] \
4103        -row $row -column 0 -columnspan 2 -pady 3  -sticky s
4104grid [label $expgui(lsFrame).f1.b.lcyc -text "Number of Cycles"] -row 0 -column 0
4105grid [entry $expgui(lsFrame).f1.b.ecyc -width 3 \
4106        -textvariable entryvar(cycles)] -row 0 -column 1
4107set entrybox(cycles) $expgui(lsFrame).f1.b.ecyc
4108
4109grid [frame $expgui(lsFrame).f1.cv -bd 4 -relief groove] \
4110        -row $row -column 2 -sticky ew
4111grid [label $expgui(lsFrame).f1.cv.l -text "Convergence Criterion"] \
4112        -row 0 -column 0 -columnspan 2
4113grid [label $expgui(lsFrame).f1.cv.v -textvariable expgui(convlbl)] -row 1 -column 0
4114grid [scale $expgui(lsFrame).f1.cv.s -orient horizontal \
4115        -from -200 -to 200 -showvalue 0 -command SetConv -resolution 10 \
4116        -variable expgui(convg)] -row 1 -column 1
4117
4118incr row
4119grid [menubutton $expgui(lsFrame).f1.lprint -textvariable expgui(printopt) \
4120        -menu $expgui(lsFrame).f1.lprint.menu -bd 4 -relief raised \
4121        ] -row $row -column 0 -columnspan 2 
4122menu $expgui(lsFrame).f1.lprint.menu
4123foreach num [lsort -integer [array names printopts]] {
4124    $expgui(lsFrame).f1.lprint.menu add checkbutton \
4125        -label "$printopts($num) ([expr int(pow(2,$num))])"\
4126        -variable entryvar(printopt$num)
4127}
4128
4129grid [frame $expgui(lsFrame).f1.marq -bd 4 -relief groove] \
4130        -row $row -column 2 -sticky ew
4131grid [label $expgui(lsFrame).f1.marq.l -text "Marquardt Damping"] \
4132        -row 0 -column 0 -columnspan 2
4133grid [label $expgui(lsFrame).f1.marq.v -textvariable expgui(marq)] \
4134        -row 1 -column 0
4135grid [scale $expgui(lsFrame).f1.marq.s -orient horizontal \
4136        -from 1.0 -to 9.99 -showvalue 0 -command SetMarq -resolution 0.01 \
4137        -variable expgui(marq)] -row 1 -column 1
4138
4139incr row
4140grid [frame $expgui(lsFrame).f1.d -bd 4 -relief groove] \
4141        -row $row -column 2 -sticky ew
4142grid [label $expgui(lsFrame).f1.d.lmbw -text "LS matrix bandwidth"] -row 0 -column 0
4143grid [entry $expgui(lsFrame).f1.d.embw -width 4 \
4144        -textvariable entryvar(mbw)] -row 0 -column 1
4145set entrybox(mbw) $expgui(lsFrame).f1.d.embw
4146
4147incr row
4148grid rowconfigure $expgui(lsFrame).f1 $row -weight 1
4149
4150incr row
4151grid [TitleFrame $expgui(lsFrame).f1.a -bd 4 -relief groove \
4152          -text "Reflection Intensity Extraction" \
4153         ] -row $row -column 0 -columnspan 6
4154set expgui(FobsExtractFrame) [$expgui(lsFrame).f1.a getframe]
4155
4156grid [frame $expgui(FobsExtractFrame).c -bd 4 -relief groove] \
4157        -row 0 -column 8 -columnspan 3 -sticky ens
4158grid [label $expgui(FobsExtractFrame).c.fol -text "Extract Fobs"] \
4159        -row 0 -column 2
4160grid [checkbutton $expgui(FobsExtractFrame).c.foc \
4161        -variable entryvar(fobsextract)] -row 0 -column 3
4162
4163grid [frame $expgui(FobsExtractFrame).d -bd 4 -relief groove] \
4164        -row 0 -column 3 -columnspan 5 -sticky ens
4165grid [label $expgui(FobsExtractFrame).d.fol -text "LeBail damping"] \
4166        -row 0 -column 2
4167tk_optionMenu $expgui(FobsExtractFrame).d.d entryvar(LBdamp) \
4168        0 1 2 3 4 5 6 7 8 9
4169grid $expgui(FobsExtractFrame).d.d -row 0 -column 3
4170incr row
4171grid rowconfigure $expgui(lsFrame).f1 $row -weight 1
4172
4173
4174foreach num {1 2 3 4 5 6 7 8 9} {
4175    grid [label $expgui(FobsExtractFrame).l$num -text $num] -row 1 -column $num
4176    grid [radiobutton $expgui(FobsExtractFrame).cc$num \
4177            -command "HistExtractSet $num" \
4178            -variable expgui(Fextract$num) -value 0] \
4179            -row 2 -column $num
4180    grid [radiobutton $expgui(FobsExtractFrame).ca$num \
4181            -command "HistExtractSet $num" \
4182            -variable expgui(Fextract$num) -value 1] \
4183            -row 3 -column $num
4184    grid [radiobutton $expgui(FobsExtractFrame).cb$num \
4185            -command "HistExtractSet $num" \
4186            -variable expgui(Fextract$num) -value 2] \
4187            -row 4 -column $num
4188}
4189set expgui(ExtractSettingsRadiobuttons) $expgui(FobsExtractFrame).cc
4190lappend expgui(ExtractSettingsRadiobuttons) $expgui(FobsExtractFrame).ca
4191lappend expgui(ExtractSettingsRadiobuttons) $expgui(FobsExtractFrame).cb
4192
4193grid [label $expgui(FobsExtractFrame).t \
4194        -text "Extraction\nMethod" -anchor c] \
4195        -column 0 -row 0 -sticky n
4196grid [label $expgui(FobsExtractFrame).t0 -text "(Phase #)" -anchor c] \
4197        -column 10 -row 1 -sticky w
4198grid [label $expgui(FobsExtractFrame).t1 -text "Rietveld" -anchor c] -column 0 -row 2
4199grid [label $expgui(FobsExtractFrame).t2 -text "F(calc) Weighted" -anchor c] -column 0 -row 3
4200grid [label $expgui(FobsExtractFrame).t3 -text "Equally Weighted" -anchor c] -column 0 -row 4
4201grid [label $expgui(FobsExtractFrame).t2a -text "(Model biased)" -anchor c] -column 10 -row 3
4202grid [label $expgui(FobsExtractFrame).t3a -text "(Le Bail method)" -anchor c] -column 10 -row 4
4203
4204proc InitLSvars {} {
4205    global expgui
4206    set expgui(convg) [set expgui(convinit) [expinfo convg]]
4207    set expgui(convlbl) [format %5.2f [expr pow(10,$expgui(convg)/100.)]]
4208    set expgui(marq) [set expgui(marqinit) [expinfo marq]]
4209    set expgui(mbw) [set expgui(mbwinit) [expinfo mbw]]
4210}
4211proc SetConv {x} {
4212    global expgui
4213    if {$x != $expgui(convinit) && $expgui(changed) <= 0} {
4214        incr expgui(changed)
4215        RecordMacroEntry "incr expgui(changed)" 0
4216    }
4217    if {$expgui(changed)} {
4218        expinfo convg set $x
4219        RecordMacroEntry "expinfo convg set $x" 0
4220    }
4221    set expgui(convlbl) [format %5.2f [expr {pow(10,$x/100.)}]]
4222}
4223proc SetMarq {x} {
4224    global expgui
4225    if {$x != $expgui(marqinit) && $expgui(changed) <= 0} {
4226        incr expgui(changed)
4227        RecordMacroEntry "incr expgui(changed)" 0
4228    }
4229    if {$expgui(changed)} {
4230        expinfo marq set $x
4231        RecordMacroEntry "expinfo marq set $x" 0
4232    }
4233}
4234# ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ END OF LS PANE CODE ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
4235#-------------------------------------------------------------------------
4236#-------------------------------------------------------------------------
4237#vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv THE MENU BAR vvvvvvvvvvvvvvvvvvvvvv
4238#---- file menu button
4239menubutton $expgui(fm).file -text File -menu $expgui(fm).file.menu
4240menu $expgui(fm).file.menu
4241if $expgui(debug) {
4242    $expgui(fm).file.menu add command -label "Reset" -command "reset"
4243}
4244if {$expgui(shell)} {
4245    $expgui(fm).file.menu add command -label "Open" -command readnewexp
4246    $expgui(fm).file.menu add command -label "expnam" -command readnewexp
4247}
4248$expgui(fm).file.menu add command -label "Save" -underline 0 \
4249        -command savearchiveexp
4250foreach c {s S} {bind . <Alt-$c> [list savearchiveexp]}
4251$expgui(fm).file.menu add command -label "Save As" \
4252        -command "SaveAsFile"
4253$expgui(fm).file.menu add command -label "Reread .EXP file" \
4254        -command {rereadexp $expgui(expfile)}
4255
4256#---- help menu button
4257menubutton $expgui(fm).help -text Help -menu $expgui(fm).help.menu
4258menu $expgui(fm).help.menu
4259$expgui(fm).help.menu add command -command showhelp -underline 0 \
4260        -label "Help Summary"
4261$expgui(fm).help.menu add command -command MakeWWWHelp  \
4262        -label "Help on current pane"
4263$expgui(fm).help.menu add command -command "MakeWWWHelp menu" \
4264        -label "Help on menu"
4265if {$tcl_platform(platform) == "windows"} {
4266    $expgui(fm).help.menu add command -label "Open console" \
4267        -command {console show}
4268}
4269if {![catch {package require tkcon} errmsg]} {
4270    $expgui(fm).help.menu add command -label "Open tkcon console" \
4271        -command {tkcon show}
4272} elseif {[file exists [file join $expgui(scriptdir) tkcon tkcon.tcl]]} {
4273    $expgui(fm).help.menu add command -label "Open tkcon console" \
4274        -command {source [file join $expgui(scriptdir) tkcon tkcon.tcl]}
4275} else {
4276    $expgui(fm).help.menu add command -label "Turn on debug puts" \
4277        -command {set expgui(debug) 1}
4278}
4279# add update commands to menu
4280if {[file exists [file join  $expgui(gsasdir) .svn]]} {
4281    $expgui(fm).file.menu add command -command CheckAndDoUpdate -label "Update GSAS/EXPGUI"
4282} else {
4283    $expgui(fm).file.menu add command  -state disabled -label "Self-updating not installed"
4284}
4285foreach c {h H} {bind . <Alt-$c> [list showhelp]}
4286# define help actions
4287bind . <Key-F1> MakeWWWHelp
4288$expgui(fm).help.menu add command -label "About..." -command About
4289$expgui(fm).help.menu add command -label "Cite..." -command Cite
4290
4291#---- options menu button
4292menubutton $expgui(fm).option -text Options \
4293        -menu $expgui(fm).option.menu
4294menu $expgui(fm).option.menu
4295
4296if {$expgui(shell)} {
4297    $expgui(fm).option.menu add checkbutton  -label "Archive EXP" \
4298            -variable expgui(archive)
4299    $expgui(fm).option.menu add checkbutton  -label "Use DISAGL window" \
4300            -variable expgui(disaglSeparateBox)
4301    $expgui(fm).option.menu  add checkbutton -label "Autoload EXP" \
4302            -variable expgui(autoexpload)
4303    $expgui(fm).option.menu  add checkbutton -label "Prompt after GSAS run" \
4304            -variable expgui(execprompt)
4305    if {$tcl_platform(platform) == "windows" && \
4306            $tcl_platform(osVersion) == 6.1} {
4307        set expgui(autoiconify) 0
4308        $expgui(fm).option.menu  add checkbutton -label "Iconify during GSAS" \
4309            -variable expgui(autoiconify) -state disabled
4310    } else {
4311        $expgui(fm).option.menu  add checkbutton -label "Iconify during GSAS" \
4312            -variable expgui(autoiconify)
4313    }
4314    if {$tcl_platform(platform) == "windows" && \
4315            $tcl_platform(os) == "Windows 95"} {
4316        $expgui(fm).option.menu  add checkbutton -label "Autostart GRWND" \
4317                -variable expgui(autoGRWND)
4318    }
4319}
4320$expgui(fm).option.menu add cascade -menu  $expgui(fm).option.menu.asort \
4321        -label "Sort atoms by"
4322
4323set expgui(asorttype) number
4324menu $expgui(fm).option.menu.asort
4325foreach opt {number type mult x y z occupancy} {
4326    $expgui(fm).option.menu.asort add radiobutton -command {DisplayAllAtoms $expgui(curPhase)}\
4327            -label $opt -value $opt -variable expgui(asorttype) 
4328}
4329
4330$expgui(fm).option.menu add cascade -menu  $expgui(fm).option.menu.hsort \
4331        -label "Sort histograms by"
4332
4333set expgui(hsorttype) number
4334menu $expgui(fm).option.menu.hsort
4335$expgui(fm).option.menu.hsort add radiobutton -command sethistlist \
4336        -label number -value number -variable expgui(hsorttype) 
4337$expgui(fm).option.menu.hsort add radiobutton -command sethistlist \
4338        -label "Histogram type" -value type -variable expgui(hsorttype) 
4339$expgui(fm).option.menu.hsort add radiobutton -command sethistlist \
4340        -label "Bank #" -value bank -variable expgui(hsorttype) 
4341$expgui(fm).option.menu.hsort add radiobutton -command sethistlist \
4342        -label "Angle/Wavelength" -value angle -variable expgui(hsorttype) 
4343
4344#---- Global mode menu button
4345$expgui(fm).option.menu add cascade -menu $expgui(fm).option.menu.editmode \
4346        -label "Multiple hist. selection"
4347menu $expgui(fm).option.menu.editmode
4348$expgui(fm).option.menu.editmode add radiobutton  -label "Off" \
4349        -variable expgui(globalmode) -value 0 \
4350        -command sethistlist
4351$expgui(fm).option.menu.editmode add radiobutton  -label "All" \
4352        -variable expgui(globalmode) -value 6 \
4353        -command sethistlist
4354$expgui(fm).option.menu.editmode add radiobutton  -label "TOF" \
4355        -variable expgui(globalmode) -value 1 \
4356        -command sethistlist
4357$expgui(fm).option.menu.editmode add radiobutton  -label "CW Neutron" \
4358        -variable expgui(globalmode) -value 2  \
4359        -command sethistlist
4360$expgui(fm).option.menu.editmode add radiobutton  -label "Alpha12 Xray" \
4361        -variable expgui(globalmode) -value 3 \
4362        -command sethistlist
4363$expgui(fm).option.menu.editmode add radiobutton  -label "Monochromatic Xray" \
4364        -variable expgui(globalmode) -value 4 \
4365        -command sethistlist
4366$expgui(fm).option.menu.editmode add radiobutton  -label "Energy Disp Xray" \
4367        -variable expgui(globalmode) -value 5 \
4368        -command sethistlist
4369$expgui(fm).option.menu.editmode add separator
4370$expgui(fm).option.menu.editmode add checkbutton \
4371        -label "Group phases together" \
4372        -variable expgui(globalphasemode) \
4373        -command sethistlist
4374
4375set expgui(globalmode) 0
4376set expgui(globalphasemode) 1
4377
4378if {$tcl_platform(platform) == "unix"} {
4379    $expgui(fm).option.menu  add checkbutton -label "Override backspace" \
4380            -variable env(GSASBACKSPACE)
4381}
4382
4383$expgui(fm).option.menu add cascade -menu  $expgui(fm).option.menu.font \
4384        -label "Screen font"
4385menu $expgui(fm).option.menu.font
4386foreach f {10 11 12 13 14 16 18 20 22} {
4387    $expgui(fm).option.menu.font add radiobutton \
4388            -command {SetTkDefaultOptions $expgui(font); ResizeFont .; ResizeNotebook} \
4389        -label $f -value $f -variable expgui(font) -font "Helvetica -$f"
4390}
4391
4392$expgui(fm).option.menu  add checkbutton -label "Show EXPTOOL output" \
4393        -variable expgui(showexptool)
4394$expgui(fm).option.menu add command -label "Save Options" \
4395        -command "SaveOptions"
4396$expgui(fm).option.menu add command -label "Save Position" \
4397        -command "SavePosition"
4398pack $expgui(fm).file $expgui(fm).option -side left  -in $expgui(fm)
4399
4400if {$expgui(shell)} {
4401    foreach menu $expgui(menunames) {
4402        set m [string tolower $menu]
4403        pack [menubutton $expgui(fm).$m -text $menu \
4404                -menu $expgui(fm).$m.menu] -side left
4405        menu $expgui(fm).$m.menu
4406    }
4407}
4408pack $expgui(fm).help  -side right -in $expgui(fm)
4409
4410if {$expgui(shell)} {
4411    # add an export command to the last menu that gets filled in later
4412    $expgui(fm).$m.menu add  cascade -label "Coord Export" \
4413            -menu $expgui(fm).$m.menu.coordexp
4414    menu $expgui(fm).$m.menu.coordexp \
4415            -postcommand "BuildCoordExpMenu $expgui(fm).$m.menu.coordexp"
4416    $expgui(fm).$m.menu.coordexp add command -label "Building menu" \
4417            -state disabled
4418    $expgui(fm).$m.menu.coordexp add command -label "Please wait..." \
4419            -state disabled
4420
4421    $expgui(fm).$m.menu add  cascade -label "CIF Export" \
4422            -menu $expgui(fm).$m.menu.cifexp
4423    menu $expgui(fm).$m.menu.cifexp
4424    $expgui(fm).$m.menu.cifexp add command -label gsas2cif \
4425            -command "runGSASwEXP gsas2cif"
4426    $expgui(fm).$m.menu.cifexp add command -label FillTemplate \
4427            -command "exec $wishshell [file join $expgui(scriptdir) fillcif.tcl] \[file root \[file tail \$expgui(expfile)]]"
4428    $expgui(fm).$m.menu.cifexp add command -label CIFselect \
4429            -command {
4430        if {[info procs CIFselect] == ""} {
4431            source [file join $expgui(scriptdir) cifselect.tcl]
4432        }
4433        CIFselect $expgui(expfile)
4434    }
4435    # add the commands in expgui_menulist
4436    foreach menu [array names expgui_menulist ] {
4437        foreach cmd $expgui_menulist($menu) {
4438            set action {}
4439            set opt {}
4440            catch {set action [lindex $expgui_cmdlist($cmd) 0]}
4441            catch {set opt [lindex $expgui_cmdlist($cmd) 2]}
4442            if {$expgui(debug) && $action == ""} {puts "blank command for $cmd"}
4443            if {$action != "" && $action != "-"} {
4444                eval $expgui(fm).$menu.menu add command \
4445                        -label $cmd $opt -command [list [subst $action]]
4446                if {[lindex $opt 0] == "-underline"} {
4447                    catch {
4448                        set num [lindex $opt 1]
4449                        set key [string range $cmd $num $num]
4450                        bind . <Alt-[string tolower $key]> [subst $action]
4451                        bind . <Alt-[string toupper $key]> [subst $action]
4452                    }
4453                }
4454            }
4455        }
4456    }
4457}
4458$expgui(fm).powder.menu add command -label "New InstParm file" -command {EditInstFile (new)}
4459
4460# Macro menu
4461$expgui(fm).macro.menu add checkbutton  -label "Record EXPGUI macro" \
4462    -variable expgui(RecordMacro)
4463$expgui(fm).macro.menu add command -label "Add comment to macro" \
4464    -command AddCommentMacroFile
4465$expgui(fm).macro.menu add command -label "Replay macro line-at-a-time" \
4466    -command {ReplayMacroFile 1}
4467$expgui(fm).macro.menu add command -label "Replay macro all at once" \
4468    -command {ReplayMacroFile 0}
4469$expgui(fm).macro.menu add checkbutton  -label "Show GSAS output in window" \
4470    -variable expgui(ShowGENLES)
4471bind . <Control-m> {set expgui(RecordMacro) [expr ! $expgui(RecordMacro)]}
4472set expgui(RecordMacro) 0
4473set expgui(MacroRunning) 0
4474set expgui(MacroStatus) "?"
4475set expgui(ShowGENLES) 1
4476trace variable expgui(RecordMacro) w SetRecordMacroOnOff
4477
4478# setup command help
4479foreach cmd [array names expgui_cmdlist] {
4480    set help {}
4481    catch {set help [lindex $expgui_cmdlist($cmd) 1]}
4482    if {$help == ""} {
4483        if {$expgui(debug)} {puts "no help for $cmd"}
4484    } else {
4485        # remove
4486        regsub -all \x09 $help " " help
4487        # preserve blank lines
4488        regsub -all \x0A\x0A $help "AAA1234567890AAA" help
4489        regsub -all \x0A $help " " help
4490        regsub -all "AAA1234567890AAA" $help \x0A\x0A help
4491        regsub -all " +" $help " " help
4492        set expgui_helplist($cmd) [string trim $help]
4493    }
4494}
4495if {$expgui(shell)} {
4496    # set up button bar
4497    foreach cmd $expgui(buttonlist) {
4498        set action {}
4499        catch {set action [lindex $expgui_cmdlist($cmd) 0]}
4500        if {$expgui(debug) && $action == ""} {puts "blank command for $cmd"}
4501        if {$action != ""} {
4502            pack [eval button .bar.$cmd -bg beige -activebackground yellow \
4503                    -padx 2m -pady 0 \
4504                    -text $cmd -command [list [subst $action]]] -side left
4505        }
4506    }
4507}
4508
4509if {$tcl_platform(os) == "Darwin"} {
4510#    $expgui(fm).file.menu add command -label "Create AppleScript" -command MakeAppleScript
4511    $expgui(fm).option.menu add checkbutton -label "Assign app to .EXP files" \
4512        -variable expgui(MacAssignApp)
4513}
4514$expgui(fm).file.menu add command -label "Exit"  -underline 1 -command catchQuit
4515foreach c {X x} {bind . <Alt-$c> [list catchQuit]}
4516#^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ END OF MENU DEFINITION ^^^^^^^^^^^^^^^^^^^
4517
4518# make the phase pane -- this must be done before setphases
4519# can be called (in loadexp)
4520MakePhasePane
4521
4522# handle indirect exits
4523wm protocol . WM_DELETE_WINDOW catchQuit
4524if {$tcl_platform(platform) != "windows"} {bind . <Control-c> catchQuit}
4525
4526set expgui(pagenow) ""
4527set expgui(curhist) {}
4528set expgui(selectedatomlist) {}
4529
4530loadexp $expgui(expfile)
4531
4532# reset the phase selection
4533set expgui(curPhase) {}
4534# select the first histogram in the list by default (if there are any)
4535if {[llength $expmap(histlistboxcontents)] > 0} {
4536    set expgui(curhist) 0
4537} else {
4538    set expgui(curhist) {}
4539}
4540
4541# execute any local commands for final initialization
4542eval $expgui(initstring)
4543
4544# resize the notebook to fit all the tabs and the largest page
4545ResizeNotebook
4546if {$expgui(resize)} {
4547    # this appears to be needed by OSX
4548    update
4549    #wm geom . [winfo reqwidth .]x[winfo reqheight .]
4550    wm geom . {}
4551}
4552# place the EXPGUI window
4553set xpos 50
4554catch {set xpos $expgui(xpos)}
4555set ypos 50
4556catch {set ypos $expgui(ypos)}
4557set poscntr "nsew"
4558catch {set poscntr $expgui(poscenter)}
4559LocateWindow "." $xpos $ypos $poscntr
4560update
4561SetStartPanel
4562#if {[CountHistory] > 200} {
4563#    DeleteHistoryRecords "This .EXP file has [CountHistory] history records\nErasing most will speed EXPGUI"
4564#}
Note: See TracBrowser for help on using the repository browser.