Changeset 328 for trunk/gsascmds.tcl


Ignore:
Timestamp:
Dec 4, 2009 5:04:13 PM (11 years ago)
Author:
toby
Message:

# on 2000/10/18 00:03:58, toby did:
Major change in order of routines!
Switch tk_dialog to MyMessageBox? and add WWW references for some error mgs
fix bug in GetProfileTerms? that prevented use of cached labels
remove platform-specific runtime code to expgui so that it gets run later

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/gsascmds.tcl

    • Property rcs:date changed from 2000/10/17 15:20:45 to 2000/10/18 00:03:58
    • Property rcs:lines changed from +82 -49 to +1187 -1173
    • Property rcs:rev changed from 1.26 to 1.27
    r325 r328  
    11# $Id$
    2 # platform-specific code
    3 if {$tcl_platform(platform) == "windows"} {
    4     if [catch {package require winexec}] {
    5         MyMessageBox -parent . -title "WINEXEC Error" \
    6                 -message "Error -- Unable to load the WINEXEC package. This is needed in Win95 machines" \
    7                 -icon error -type Quit -default quit \
    8                 -helplink "expgui_Win_readme.html Winexec"
    9         destroy .
    10     }
    11     if ![file exists [file join $expgui(gsasdir) fonts grfont.dat]] {
    12         MyMessageBox -parent . -title "PGPLOT Error" \
    13                 -message "Warning -- Unable to find file GRFONT.DAT. GSAS graphics will not work. Is GSAS correctly installed?" \
    14                 -icon warning -type {"Limp Ahead"} -default "Limp Ahead" \
    15                 -helplink "expguierr.html NoPGPLOT"
    16     }
    17 
    18     if {$tcl_platform(os) == "Windows 95"} {
    19         # this creates a DOS box to run a program in
    20         proc forknewterm {title command "background 0" "scrollbar 1" "wait 1"} {
    21             global env expgui
    22             # Windows environment variables
    23             # -95 does not seem to be able to use these
    24             set env(GSAS) [file nativename $expgui(gsasdir)]
    25             # PGPLOT_FONT is needed by PGPLOT
    26             set env(PGPLOT_FONT) [file nativename [file join $expgui(gsasdir) fonts grfont.dat]]
    27             # this is the number of lines/page in the .LST (etc.) file
    28             set env(LENPAGE) 60
    29             set pwd [file nativename [pwd]]
    30 
    31             # check the path -- can DOS use it?
    32             if {[string first // [pwd]] != -1} {
    33                 MyMessageBox -parent . -title "Invalid Path" \
    34                 -message {Error -- Use "Map network drive" to access this directory with a letter (e.g. F:) GSAS can't directly access a network drive} \
    35                 -icon error -type ok -default ok \
    36                 -helplink "expgui_Win_readme.html NetPath"
    37                 return
    38             }
    39             # all winexec commands are background commands
    40             #   if $background
    41 
    42             # pause is hard coded in the .BAT file
    43             #if $wait  {
    44             #   append command " pause"
    45             #}
    46 
    47             # replace the forward slashes with backward
    48             regsub -all / $command \\ command
    49             # Win95 does not seem to inherit the environment from Tcl env vars
    50             # so define it in the .BAT file
    51             winexec -d [file nativename [pwd]] \
    52                 [file join $expgui(scriptdir) gsastcl.bat] \
    53                 "[file nativename $expgui(gsasdir)] $command"
    54         }
    55     } else {
    56         # now for - brain-dead Windows-NT
    57         # this creates a DOS box to run a program in
    58         proc forknewterm {title command "background 0" "scrollbar 1" "wait 1"} {
    59             global env expgui
    60             # Windows environment variables
    61             set env(GSAS) [file nativename $expgui(gsasdir)]
    62             # PGPLOT_FONT is needed by PGPLOT
    63             set env(PGPLOT_FONT) [file nativename [file join $expgui(gsasdir) fonts grfont.dat]]
    64             # this is the number of lines/page in the .LST (etc.) file
    65             set env(LENPAGE) 60
    66             # all winexec commands are background commands -- ignore background arg
    67             # can't get pause to work! -- ignore wait
    68 
    69             set prevcmd {}
    70             foreach cmd $command {
    71                 if {$prevcmd != ""} {
    72                     tk_dialog .done_yet Confirm "Press OK to start command $cmd" "" 0 OK
    73                 }
    74                 # replace the forward slashes with backward
    75                 regsub -all / $cmd \\ cmd
    76                 # cmd.exe must be in the path -- lets hope that at least works!
    77                 winexec -d [file nativename [pwd]] cmd.exe "/c $cmd"
    78                 set prevcmd $cmd
    79             }
    80         }
    81     }
    82 } else {
    83     if ![file exists [file join $expgui(gsasdir) pgl grfont.dat]] {
    84         MyMessageBox -parent . -title "PGPLOT Error" \
    85                 -message "Warning -- Unable to find file grfont.dat. GSAS graphics will not work. Is GSAS correctly installed?" \
    86                 -icon warning -type {"Limp Ahead"} -default "Limp Ahead" \
    87                 -helplink "expguierr.html NoPGPLOT"
    88     }
    89     if [catch {set env(GSASBACKSPACE)}] {set env(GSASBACKSPACE) 1}
    90 
    91     # this creates a xterm window to run a program in
    92     proc forknewterm {title command "background 0" "scrollbar 1" "wait 1"} {
    93         global env expgui
    94         # UNIX environment variables
    95         set env(GSASEXE) $expgui(gsasexe)
    96         set env(ATOMDATA) [file join $expgui(gsasdir) data atmdata.dat]
    97         set env(ATMXSECT) [file join $expgui(gsasdir) data atmxsect.dat]
    98         # PGPLOT_DIR is needed by PGPLOT
    99         set env(PGPLOT_DIR) [file join $expgui(gsasdir) pgl]
    100         # this is the number of lines/page in the .LST (etc.) file
    101         set env(LENPAGE) 60
    102         set termopts {}
    103         if $env(GSASBACKSPACE) {
    104             append termopts \
    105                     {-xrm "xterm*VT100.Translations: #override\\n <KeyPress>BackSpace: string(\\177)"}
    106         }
    107         if $scrollbar {
    108             append termopts " -sb"
     2#------------------------------------------------------------------------------
     3# display routines
     4#------------------------------------------------------------------------------
     5#       Message box code that centers the message box over the parent.
     6#          or along the edge, if too close,
     7#          but leave a border along +x & +y for reasons I don't remember
     8#       It also allows the button names to be defined using
     9#            -type $list  -- where $list has a list of button names
     10#       larger messages are placed in a scrolled text widget
     11#       capitalization is now ignored for -default
     12#       The command returns the name button in all lower case letters
     13#       otherwise see  tk_messageBox for a description
     14#
     15#       This is a modification of tkMessageBox (msgbox.tcl v1.5)
     16#
     17proc MyMessageBox {args} {
     18    global tkPriv tcl_platform
     19
     20    set w tkPrivMsgBox
     21    upvar #0 $w data
     22
     23    #
     24    # The default value of the title is space (" ") not the empty string
     25    # because for some window managers, a
     26    #           wm title .foo ""
     27    # causes the window title to be "foo" instead of the empty string.
     28    #
     29    set specs {
     30        {-default "" "" ""}
     31        {-icon "" "" "info"}
     32        {-message "" "" ""}
     33        {-parent "" "" .}
     34        {-title "" "" " "}
     35        {-type "" "" "ok"}
     36        {-helplink "" "" ""}
     37    }
     38
     39    tclParseConfigSpec $w $specs "" $args
     40
     41    if {[lsearch {info warning error question} $data(-icon)] == -1} {
     42        error "bad -icon value \"$data(-icon)\": must be error, info, question, or warning"
     43    }
     44    if {![string compare $tcl_platform(platform) "macintosh"]} {
     45      switch -- $data(-icon) {
     46          "error"     {set data(-icon) "stop"}
     47          "warning"   {set data(-icon) "caution"}
     48          "info"      {set data(-icon) "note"}
     49        }
     50    }
     51
     52    if {![winfo exists $data(-parent)]} {
     53        error "bad window path name \"$data(-parent)\""
     54    }
     55
     56    switch -- $data(-type) {
     57        abortretryignore {
     58            set buttons {
     59                {abort  -width 6 -text Abort -under 0}
     60                {retry  -width 6 -text Retry -under 0}
     61                {ignore -width 6 -text Ignore -under 0}
     62            }
     63        }
     64        ok {
     65            set buttons {
     66                {ok -width 6 -text OK -under 0}
     67            }
     68          if {![string compare $data(-default) ""]} {
     69                set data(-default) "ok"
     70            }
     71        }
     72        okcancel {
     73            set buttons {
     74                {ok     -width 6 -text OK     -under 0}
     75                {cancel -width 6 -text Cancel -under 0}
     76            }
     77        }
     78        retrycancel {
     79            set buttons {
     80                {retry  -width 6 -text Retry  -under 0}
     81                {cancel -width 6 -text Cancel -under 0}
     82            }
     83        }
     84        yesno {
     85            set buttons {
     86                {yes    -width 6 -text Yes -under 0}
     87                {no     -width 6 -text No  -under 0}
     88            }
     89        }
     90        yesnocancel {
     91            set buttons {
     92                {yes    -width 6 -text Yes -under 0}
     93                {no     -width 6 -text No  -under 0}
     94                {cancel -width 6 -text Cancel -under 0}
     95            }
     96        }
     97        default {
     98#           error "bad -type value \"$data(-type)\": must be abortretryignore, ok, okcancel, retrycancel, yesno, or yesnocancel"
     99            foreach item $data(-type) {
     100                lappend buttons [list [string tolower $item] -text $item -under 0]
     101            }
     102        }
     103    }
     104
     105    if {[string compare $data(-default) ""]} {
     106        set valid 0
     107        foreach btn $buttons {
     108            if {![string compare [lindex $btn 0] [string tolower $data(-default)]]} {
     109                set valid 1
     110                break
     111            }
     112        }
     113        if {!$valid} {
     114            error "invalid default button \"$data(-default)\""
     115        }
     116    }
     117
     118    # 2. Set the dialog to be a child window of $parent
     119    #
     120    #
     121    if {[string compare $data(-parent) .]} {
     122        set w $data(-parent).__tk__messagebox
     123    } else {
     124        set w .__tk__messagebox
     125    }
     126
     127    # 3. Create the top-level window and divide it into top
     128    # and bottom parts.
     129
     130    catch {destroy $w}
     131    toplevel $w -class Dialog
     132    wm title $w $data(-title)
     133    wm iconname $w Dialog
     134    wm protocol $w WM_DELETE_WINDOW { }
     135    wm transient $w $data(-parent)
     136    if {![string compare $tcl_platform(platform) "macintosh"]} {
     137        unsupported1 style $w dBoxProc
     138    }
     139
     140    frame $w.bot
     141    pack $w.bot -side bottom -fill both
     142    frame $w.top
     143    pack $w.top -side top -fill both -expand 1
     144    if {$data(-helplink) != ""} {
     145#       frame $w.help
     146#       pack $w.help -side top -fill both
     147        pack [button $w.top.1 -text Help -bg yellow \
     148                -command "MakeWWWHelp $data(-helplink)"] \
     149                -side right -anchor ne
     150        bind $w <Key-F1> "MakeWWWHelp $data(-helplink)"
     151    }
     152    if {[string compare $tcl_platform(platform) "macintosh"]} {
     153        $w.bot configure -relief raised -bd 1
     154        $w.top configure -relief raised -bd 1
     155    }
     156
     157    # 4. Fill the top part with bitmap and message (use the option
     158    # database for -wraplength and -font so that they can be
     159    # overridden by the caller).
     160
     161    option add *Dialog.msg.wrapLength 3i widgetDefault
     162
     163    if {[string length $data(-message)] > 300} {
     164        if {![string compare $tcl_platform(platform) "macintosh"]} {
     165            option add *Dialog.msg.t.font system widgetDefault
    109166        } else {
    110             append termopts " +sb"
    111         }
    112         if $background {
    113             set suffix {&}
     167            option add *Dialog.msg.t.font {Times 18} widgetDefault
     168        }
     169        frame $w.msg
     170        grid [text  $w.msg.t  \
     171                -height 20 -width 55 -relief flat -wrap word \
     172                -yscrollcommand "$w.msg.rscr set" \
     173                ] -row 1 -column 0 -sticky news
     174        grid [scrollbar $w.msg.rscr  -command "$w.msg.t yview" \
     175                ] -row 1 -column 1 -sticky ns
     176        # give extra space to the text box
     177        grid columnconfigure $w.msg 0 -weight 1
     178        grid rowconfigure $w.msg 1 -weight 1
     179        $w.msg.t insert end $data(-message)
     180    } else {
     181        if {![string compare $tcl_platform(platform) "macintosh"]} {
     182            option add *Dialog.msg.font system widgetDefault
    114183        } else {
    115             set suffix {}
    116         }
     184            option add *Dialog.msg.font {Times 18} widgetDefault
     185        }
     186        label $w.msg -justify left -text $data(-message)
     187    }
     188    pack $w.msg -in $w.top -side right -expand 1 -fill both -padx 3m -pady 3m
     189    if {[string compare $data(-icon) ""]} {
     190        label $w.bitmap -bitmap $data(-icon)
     191        pack $w.bitmap -in $w.top -side left -padx 3m -pady 3m
     192    }
     193
     194    # 5. Create a row of buttons at the bottom of the dialog.
     195
     196    set i 0
     197    foreach but $buttons {
     198        set name [lindex $but 0]
     199        set opts [lrange $but 1 end]
     200      if {![llength $opts]} {
     201            # Capitalize the first letter of $name
     202          set capName [string toupper \
     203                    [string index $name 0]][string range $name 1 end]
     204            set opts [list -text $capName]
     205        }
     206
     207      eval button [list $w.$name] $opts [list -command [list set tkPriv(button) $name]]
     208
     209        if {![string compare $name [string tolower $data(-default)]]} {
     210            $w.$name configure -default active
     211        }
     212      pack $w.$name -in $w.bot -side left -expand 1 -padx 3m -pady 2m
     213
     214        # create the binding for the key accelerator, based on the underline
    117215        #
    118         if $wait  {
    119             append command "\; echo -n Press Enter to continue \; read x"
    120         }
    121         if !$background {wm iconify .}
    122         catch {eval exec xterm $termopts -title [list $title] \
    123                 -e /bin/sh -c [list $command] $suffix} errmsg
    124         if $expgui(debug) {puts "xterm result = $errmsg"}
    125         if !$background {wm deiconify .}
    126     }
     216        set underIdx [$w.$name cget -under]
     217        if {$underIdx >= 0} {
     218            set key [string index [$w.$name cget -text] $underIdx]
     219          bind $w <Alt-[string tolower $key]>  [list $w.$name invoke]
     220          bind $w <Alt-[string toupper $key]>  [list $w.$name invoke]
     221        }
     222        incr i
     223    }
     224
     225    # 6. Create a binding for <Return> on the dialog if there is a
     226    # default button.
     227
     228    if {[string compare $data(-default) ""]} {
     229      bind $w <Return> [list tkButtonInvoke $w.[string tolower $data(-default)]]
     230    }
     231
     232    # 7. Withdraw the window, then update all the geometry information
     233    # so we know how big it wants to be, then center the window in the
     234    # display and de-iconify it.
     235
     236    wm withdraw $w
     237    update idletasks
     238    set wp $data(-parent)
     239    # center the new window in the middle of the parent
     240    set x [expr [winfo x $wp] + [winfo width $wp]/2 - \
     241            [winfo reqwidth $w]/2 - [winfo vrootx $wp]]
     242    set y [expr [winfo y $wp] + [winfo height $wp]/2 - \
     243            [winfo reqheight $w]/2 - [winfo vrooty $wp]]
     244    # make sure that we can see the entire window
     245    set xborder 10
     246    set yborder 25
     247    if {$x < 0} {set x 0}
     248    if {$x+[winfo reqwidth $w] +$xborder > [winfo screenwidth $w]} {
     249        incr x [expr \
     250                [winfo screenwidth $w] - ($x+[winfo reqwidth $w] + $xborder)]
     251    }
     252    if {$y < 0} {set y 0}
     253    if {$y+[winfo reqheight $w] +$yborder > [winfo screenheight $w]} {
     254        incr y [expr \
     255                [winfo screenheight $w] - ($y+[winfo reqheight $w] + $yborder)]
     256    }
     257    wm geom $w +$x+$y
     258    wm deiconify $w
     259
     260    # 8. Set a grab and claim the focus too.
     261
     262    catch {set oldFocus [focus]}
     263    catch {set oldGrab [grab current $w]}
     264    catch {
     265        grab $w
     266        if {[string compare $data(-default) ""]} {
     267            focus $w.[string tolower $data(-default)]
     268        } else {
     269            focus $w
     270        }
     271    }
     272
     273    # 9. Wait for the user to respond, then restore the focus and
     274    # return the index of the selected button.  Restore the focus
     275    # before deleting the window, since otherwise the window manager
     276    # may take the focus away so we can't redirect it.  Finally,
     277    # restore any grab that was in effect.
     278
     279    tkwait variable tkPriv(button)
     280    catch {focus $oldFocus}
     281    destroy $w
     282    catch {grab $oldGrab}
     283    return $tkPriv(button)
     284}
     285
     286# tell'em what is happening
     287proc pleasewait {{message {}}} {
     288    catch {destroy .msg}
     289    toplevel .msg
     290    wm transient .msg [winfo toplevel .]
     291    pack [frame .msg.f -bd 4 -relief groove]
     292    pack [message .msg.f.m -text "Please wait $message"]
     293    wm withdraw .msg
     294    update idletasks
     295    # place the message on top of the main window
     296    set x [expr [winfo x .] + [winfo width .]/2 - \
     297            [winfo reqwidth .msg]/2 - [winfo vrootx .]]
     298    if {$x < 0} {set x 0}
     299    set y [expr [winfo y .] + [winfo height .]/2 - \
     300            [winfo reqheight .msg]/2 - [winfo vrooty .]]
     301    if {$y < 0} {set y 0}
     302    wm geom .msg +$x+$y
     303    wm deiconify .msg
     304    global makenew
     305    set makenew(OldGrab) ""
     306    set makenew(OldFocus) ""
     307    # save focus & grab
     308    catch {set makenew(OldFocus) [focus]}
     309    catch {set makenew(OldGrab) [grab current .msg]}
     310    catch {grab .msg}
     311    update
     312}
     313
     314# clear the message
     315proc donewait {} {
     316    global makenew
     317    catch {destroy .msg}
     318    # reset focus & grab
     319    catch {
     320        if {$makenew(OldFocus) != ""} {
     321            focus $makenew(OldFocus)
     322        }
     323    }
     324    catch {
     325        if {$makenew(OldGrab) != ""} {
     326            grab $makenew(OldGrab)
     327        }
     328    }
     329}
     330
     331proc putontop {w} {
     332    # center window $w above its parent and make it stay on top
     333    set wp [winfo parent $w]
     334    wm transient $w [winfo toplevel $wp]
     335    wm withdraw $w
     336    update idletasks
     337    # center the new window in the middle of the parent
     338    set x [expr [winfo x $wp] + [winfo width $wp]/2 - \
     339            [winfo reqwidth $w]/2 - [winfo vrootx $wp]]
     340    if {$x < 0} {set x 0}
     341    set xborder 10
     342    if {$x+[winfo reqwidth $w] +$xborder > [winfo screenwidth $w]} {
     343        incr x [expr \
     344                [winfo screenwidth $w] - ($x+[winfo reqwidth $w] + $xborder)]
     345    }
     346    set y [expr [winfo y $wp] + [winfo height $wp]/2 - \
     347            [winfo reqheight $w]/2 - [winfo vrooty $wp]]
     348    if {$y < 0} {set y 0}
     349    set yborder 25
     350    if {$y+[winfo reqheight $w] +$yborder > [winfo screenheight $w]} {
     351        incr y [expr \
     352                [winfo screenheight $w] - ($y+[winfo reqheight $w] + $yborder)]
     353    }
     354    wm geom $w +$x+$y
     355    wm deiconify $w
     356
     357    global makenew
     358    set makenew(OldGrab) ""
     359    set makenew(OldFocus) ""
     360    catch {set makenew(OldFocus) [focus]}
     361    catch {set makenew(OldGrab) [grab current $w]}
     362    catch {grab $w}
     363}
     364
     365proc afterputontop {} {
     366    # restore focus
     367    global makenew
     368    # reset focus & grab
     369    catch {
     370        if {$makenew(OldFocus) != ""} {
     371            focus $makenew(OldFocus)
     372        }
     373    }
     374    catch {
     375        if {$makenew(OldGrab) != ""} {
     376            grab $makenew(OldGrab)
     377        }
     378    }
     379}
     380
     381proc ShowBigMessage {win labeltext msg "optionlist OK" "link {}"} {
     382    catch {destroy $win}
     383    toplevel $win
     384
     385    pack [label $win.l1 -text $labeltext] -side top
     386    pack [frame $win.f1] -side top -expand yes -fill both
     387    grid [text  $win.f1.t  \
     388            -height 20 -width 55  -wrap none -font Courier \
     389            -xscrollcommand "$win.f1.bscr set" \
     390            -yscrollcommand "$win.f1.rscr set" \
     391            ] -row 1 -column 0 -sticky news
     392    grid [scrollbar $win.f1.bscr -orient horizontal \
     393            -command "$win.f1.t xview" \
     394            ] -row 2 -column 0 -sticky ew
     395    grid [scrollbar $win.f1.rscr  -command "$win.f1.t yview" \
     396            ] -row 1 -column 1 -sticky ns
     397    # give extra space to the text box
     398    grid columnconfigure $win.f1 0 -weight 1
     399    grid rowconfigure $win.f1 1 -weight 1
     400    $win.f1.t insert end $msg
     401
     402    global makenew
     403    set makenew(result) 0
     404    bind $win <Return> "destroy $win"
     405    bind $win <KeyPress-Prior> "$win.f1.t yview scroll -1 page"
     406    bind $win <KeyPress-Next> "$win.f1.t yview scroll 1 page"
     407    bind $win <KeyPress-Right> "$win.f1.t xview scroll 1 unit"
     408    bind $win <KeyPress-Left> "$win.f1.t xview scroll -1 unit"
     409    bind $win <KeyPress-Up> "$win.f1.t yview scroll -1 unit"
     410    bind $win <KeyPress-Down> "$win.f1.t yview scroll 1 unit"
     411    bind $win <KeyPress-Home> "$win.f1.t yview 0"
     412    bind $win <KeyPress-End> "$win.f1.t yview end"
     413    set i 0
     414    foreach item $optionlist {
     415        pack [button $win.q[incr i] \
     416                -command "set makenew(result) $i; destroy $win" -text $item] -side left
     417    }
     418    if {$link != ""} {
     419        pack [button $win.help -text Help -bg yellow \
     420            -command "MakeWWWHelp $link"] \
     421            -side right
     422        bind $win <Key-F1> "MakeWWWHelp $link"
     423    }
     424    putontop $win
     425    tkwait window $win
     426
     427    # fix grab...
     428    afterputontop
     429    return $makenew(result)
    127430}
    128431
     
    166469}
    167470
    168 # run a GSAS program that does not require an experiment file
    169 proc runGSASprog {proglist} {
    170     global expgui tcl_platform
    171     set cmd {}
    172     foreach prog $proglist {
     471#------------------------------------------------------------------------------
     472# profile/symmetry routines
     473#------------------------------------------------------------------------------
     474# profile terms
     475array set expgui {
     476    prof-T-1 {alp-0 alp-1 bet-0 bet-1 sig-0 sig-1 sig-2 rstr rsta \
     477            rsca s1ec s2ec }
     478    prof-T-2 {alp-0 alp-1 beta switch sig-0 sig-1 sig-2 gam-0 gam-1 \
     479            gam-2 ptec stec difc difa zero }
     480    prof-T-3 {alp bet-0 bet-1 sig-0 sig-1 sig-2 gam-0 gam-1 \
     481            gam-2 gsf g1ec g2ec rstr rsta rsca L11 L22 L33 L12 L13 L23 }
     482    prof-T-4 {alp bet-0 bet-1 sig-1 sig-2 gam-2 g2ec gsf \
     483            rstr rsta rsca eta}
     484    prof-C-1 {GU GV GW asym F1 F2 }
     485    prof-C-2 {GU GV GW LX LY trns asym shft GP stec ptec sfec \
     486            L11 L22 L33 L12 L13 L23 }
     487    prof-C-3 {GU GV GW GP LX LY S/L H/L trns shft stec ptec sfec \
     488            L11 L22 L33 L12 L13 L23 }
     489    prof-C-4 {GU GV GW GP LX ptec trns shft sfec S/L H/L eta}
     490    prof-E-1 {A B C ds cds}
     491}
     492
     493# number of profile terms depends on the histogram type
     494# the LAUE symmetry and the profile number
     495proc GetProfileTerms {phase hist ptype} {
     496    global expmap expgui
     497    if {$hist == "C" || $hist == "T" || $hist == "E"} {
     498        set htype $hist
     499    } else {
     500        set htype [string range $expmap(htype_$hist) 2 2]
     501    }
     502    # get the cached copy of the profile term labels, when possible
     503    set lbls {}
     504    catch {
     505        set lbls $expmap(ProfileTerms${phase}_${ptype}_${htype})
     506    }
     507    if {$lbls != ""} {return $lbls}
     508
     509    catch {set lbls $expgui(prof-$htype-$ptype)}
     510    if {$lbls == ""} {return}
     511    # add terms based on the Laue symmetry
     512    if {($htype == "C" || $htype == "T") && $ptype == 4} {
     513        set laueaxis [GetLaue [phaseinfo $phase spacegroup]]
     514        eval lappend lbls [Profile4Terms $laueaxis]
     515    }
     516    set expmap(ProfileTerms${phase}_${ptype}_${htype}) $lbls
     517    return $lbls
     518}
     519
     520proc Profile4Terms {laueaxis} {
     521    switch -exact $laueaxis {
     522        1bar {return \
     523                "S400 S040 S004 S220 S202 S022 S310 S103 S031 \
     524                S130 S301 S013 S211 S121 S112"}
     525        2/ma {return "S400 S040 S004 S220 S202 S022 S013 S031 S211"}
     526        2/mb {return "S400 S040 S004 S220 S202 S022 S301 S103 S121"}
     527        2/mc {return "S400 S040 S004 S220 S202 S022 S130 S310 S112"}
     528        mmm  {return "S400 S040 S004 S220 S202 S022"}
     529        4/m  {return "S400 S004 S220 S202"}
     530        4/mmm {return "S400 S004 S220 S202"}
     531        3barR     {return "S400 S220 S310 S211"}
     532        "3bar mR" {return "S400 S220 S310 S211"}
     533        3bar    {return "S400 S004 S202 S211"}
     534        3barm1 {return "S400 S004 S202"}
     535        3bar1m  {return "S400 S004 S202 S211"}
     536        6/m    {return "S400 S004 S202"}
     537        6/mmm  {return "S400 S004 S202"}
     538        "m 3"  {return "S400 S220"}
     539        m3m    {return "S400 S220"}
     540        default {return ""}
     541    }
     542}
     543
     544proc GetLaue {spg} {
     545    global tcl_platform expgui
     546    # check the space group
     547    set fp [open spg.in w]
     548    puts $fp "N"
     549    puts $fp "N"
     550    puts $fp $spg
     551    puts $fp "Q"
     552    close $fp
     553    catch {
    173554        if {$tcl_platform(platform) == "windows"} {
    174             append cmd " \"$expgui(gsasexe)/${prog}.exe \" "
     555            exec [file join $expgui(gsasexe) spcgroup.exe] < spg.in >& spg.out
    175556        } else {
    176             if {$cmd != ""} {append cmd "\;"}
    177             append cmd "[file join $expgui(gsasexe) $prog]"
    178         }
    179     }
    180     forknewterm $prog $cmd 0 1 1
    181 }
    182 
    183 # run a GSAS program that requires an experiment file for input/output
    184 proc runGSASwEXP {proglist "concurrent 0"} {
    185     global expgui tcl_platform
    186     # Save the current exp file
    187     savearchiveexp
    188     # load the changed .EXP file automatically?
    189     if {$expgui(autoexpload)} {
    190         # disable the file changed monitor
    191         set expgui(expModifiedLast) 0
    192     }
    193     set cmd {}
    194     set expnam [file root [file tail $expgui(expfile)]]
    195     foreach prog $proglist {
    196         if {$prog == "expedt" && $expgui(archive)} archiveexp
    197         if {$tcl_platform(platform) == "windows"} {
    198             append cmd " \"$expgui(gsasexe)/${prog}.exe $expnam \" "
     557            exec [file join $expgui(gsasexe) spcgroup] < spg.in >& spg.out
     558        }
     559    }
     560    set fp [open spg.out r]
     561    set laue {}
     562    set uniqueaxis {}
     563    while {[gets $fp line] >= 0} {
     564        regexp {Laue symmetry (.*)} $line junk laue
     565        regexp {The unique axis is (.*)} $line junk uniqueaxis
     566    }
     567    close $fp
     568    catch {file delete -force spg.in spg.out}
     569    set laue [string trim $laue]
     570    # add a R suffix for rhombohedral settings
     571    if {[string range [string trim $spg] end end] == "R"} {
     572        return "${laue}${uniqueaxis}R"
     573    }
     574    return "${laue}$uniqueaxis"
     575}
     576
     577# set up to change the profile type for a series of histogram/phase entries
     578# (histlist & phaselist should be lists of the same length)
     579#
     580proc ChangeProfileType {histlist phaselist} {
     581    global expgui expmap
     582    set w .profile
     583    catch {destroy $w}
     584    toplevel $w -bg beige
     585    wm title $w "Change Profile Function"
     586   
     587    # all histogram/phases better be the same type, so we can just use the 1st
     588    set hist [lindex $histlist 0]
     589    set phase [lindex $phaselist 0]
     590    set ptype [string trim [hapinfo $hist $phase proftype]]
     591
     592    # get list of allowed profile terms for the current histogram type
     593    set i 1
     594    while {[set lbls [GetProfileTerms $phase $hist $i]] != ""} {
     595        lappend lbllist $lbls
     596        incr i
     597    }
     598    # labels for the current type
     599    set i $ptype
     600    set oldlbls [lindex $lbllist [incr i -1]]
     601   
     602    if {[llength $histlist] == 1} {
     603        pack [label $w.a -bg beige \
     604                -text "Change profile function for Histogram #$hist Phase #$phase" \
     605                ] -side top
     606    } else {
     607        # make a list of histograms by phase
     608        foreach h $histlist p $phaselist {
     609            lappend phlist($p) $h
     610        }
     611        set num 0
     612        pack [frame $w.a -bg beige] -side top
     613        pack [label $w.a.$num -bg beige \
     614                -text "Change profile function for:" \
     615                ] -side top -anchor w
     616        foreach i [lsort [array names phlist]] {
     617            incr num
     618            pack [label $w.a.$num -bg beige -text \
     619                    "\tPhase #$i, Histograms [CompressList $phlist($i)]" \
     620                    ] -side top -anchor w
     621        }
     622    }
     623    pack [label $w.e1 \
     624            -text "Current function is type $ptype." \
     625            -bg beige] -side top -anchor w
     626    pack [frame $w.e -bg beige] -side top -expand yes -fill both
     627    pack [label $w.e.1 \
     628            -text "Set function to type" \
     629            -bg beige] -side left
     630    set menu [tk_optionMenu $w.e.2 expgui(newpeaktype) junk]
     631    pack $w.e.2 -side left -anchor w
     632
     633    pack [radiobutton $w.e.4 -bg beige -variable expgui(DefaultPeakType) \
     634            -command "set expgui(newpeaktype) $ptype; \
     635            FillChangeProfileType $w.c $hist $phase $ptype [list $oldlbls] [list $oldlbls]" \
     636            -value 1 -text "Current value overrides"] -side right
     637    pack [radiobutton $w.e.3 -bg beige -variable expgui(DefaultPeakType) \
     638            -command \
     639            "set expgui(newpeaktype) $ptype; \
     640            FillChangeProfileType $w.c $hist $phase $ptype [list $oldlbls] [list $oldlbls]" \
     641            -value 0 -text "Default value overrides"] -side right
     642
     643    $w.e.2 config -bg beige
     644    pack [frame $w.c -bg beige] -side top -expand yes -fill both
     645    pack [frame $w.d -bg beige] -side top -expand yes -fill both
     646    pack [button $w.d.2 -text Set  \
     647            -command "SaveChangeProfileType $w.c $histlist $phaselist; destroy $w"\
     648            ] -side left
     649    pack [button $w.d.3 -text Quit \
     650            -command "destroy $w"] -side left
     651    pack [button $w.d.help -text Help -bg yellow \
     652            -command "MakeWWWHelp expgui5.html ChangeType"] \
     653            -side right
     654    bind $w <Key-F1> "MakeWWWHelp expgui5.html ChangeType"
     655    bind $w <Return> "destroy $w"
     656
     657    $menu delete 0 end
     658    set i 0
     659    foreach lbls $lbllist {
     660        incr i
     661        $menu add command -label $i -command \
     662                "set expgui(newpeaktype) $i; \
     663                FillChangeProfileType $w.c $hist $phase $i [list $lbls] [list $oldlbls]"
     664    }
     665    set expgui(newpeaktype) $ptype
     666    FillChangeProfileType $w.c $hist $phase $ptype $oldlbls $oldlbls
     667
     668    # force the window to stay on top
     669    putontop $w
     670    focus $w.e.2
     671    tkwait window $w
     672    afterputontop
     673    sethistlist
     674}
     675
     676# save the changes to the profile
     677proc SaveChangeProfileType {w histlist phaselist} {
     678    global expgui
     679    foreach phase $phaselist hist $histlist {
     680        hapinfo $hist $phase proftype set $expgui(newpeaktype)
     681        hapinfo $hist $phase profterms set $expgui(newProfileTerms)
     682        for {set i 1} {$i <=  $expgui(newProfileTerms)} {incr i} {
     683            hapinfo $hist $phase pterm$i set [$w.ent${i} get]
     684            hapinfo $hist $phase pref$i set $expgui(ProfRef$i)
     685        }
     686        set i [expr 1+$expgui(newProfileTerms)]
     687        hapinfo $hist $phase pcut set [$w.ent$i get]
     688        incr expgui(changed) [expr 3 + $expgui(newProfileTerms)]
     689    }
     690}
     691
     692# file the contents of the "Change Profile Type" Menu
     693proc FillChangeProfileType {w hist phase newtype lbls oldlbls} {
     694    global expgui expmap
     695    set ptype [string trim [hapinfo $hist $phase proftype]]
     696    catch {unset oldval}
     697    # loop through the old terms and set up an array of starting values
     698    set num 0
     699    foreach term $oldlbls {
     700        incr num
     701        set oldval($term) [hapinfo $hist $phase pterm$num]
     702    }
     703    set oldval(Peak\nCutoff) [hapinfo $hist $phase pcut]
     704
     705    # is the new type the same as the current?
     706    if {$ptype == $newtype} {
     707        set nterms [hapinfo $hist $phase profterms]
     708    } else {
     709        set nterms [llength $lbls]
     710    }
     711    set expgui(newProfileTerms) $nterms
     712    set expgui(CurrentProfileTerms) $nterms
     713    # which default profile set matches the new type
     714    set setnum {}
     715    foreach j {" " 1 2 3 4 5 6 7 8 9} {
     716        set i [profdefinfo $hist $j proftype]
     717        if {$i == ""} continue
     718        if {$i == $newtype} {
     719            set setnum $j
     720            break
     721        }
     722    }
     723
     724    eval destroy [winfo children $w]
     725
     726    set colstr 0
     727    set row 2
     728    set maxrow [expr $row + $nterms/2]
     729    for { set num 1 } { $num <= $nterms + 1} { incr num } {
     730        # get the default value (originally from the in .INS file)
     731        set val {}
     732        if {$setnum != ""} {
     733            set val 0.0
     734            catch {
     735                set val [profdefinfo $hist $setnum pterm$num]
     736                # pretty up the number
     737                if {$val == 0.0} {
     738                    set val 0.0
     739                } elseif {abs($val) < 1e-2 || abs($val) > 1e6} {
     740                    set val [format %.3e $val]
     741                } elseif {abs($val) > 1e-2 && abs($val) < 10} {
     742                    set val [format %.5f $val]
     743                } elseif {abs($val) < 9999} {
     744                    set val [format %.2f $val]
     745                } elseif {abs($val) < 1e6} {
     746                    set val [format %.0f $val]
     747                }
     748            }
     749        }
     750        # heading
     751        if {$row == 2} {
     752            set col $colstr
     753            grid [label $w.h0${num} -text "lbl" -bg beige] \
     754                -row $row -column $col
     755            grid [label $w.h2${num} -text "ref" -bg beige] \
     756                -row $row -column [incr col]
     757            grid [label $w.h3${num} -text "next value" -bg beige] \
     758                -row $row -column [incr col]
     759            grid [label $w.h4${num} -text "default" -bg beige] \
     760                -row $row -column [incr col]
     761            grid [label $w.h5${num} -text "current" -bg beige] \
     762                -row $row -column [incr col]
     763        }
     764        set col $colstr
     765        incr row
     766        set term {}
     767        catch {set term [lindex $lbls [expr $num-1]]}
     768        if {$term == ""} {set term $num}
     769        if {$num == $nterms + 1} {
     770            set term "Peak\nCutoff"
     771            set val {}
     772            if {$setnum != ""} {
     773                set val 0.0
     774                catch {set val [profdefinfo $hist $setnum pcut]}
     775            }
     776        }
     777
     778        grid [label $w.l${num} -text "$term" -bg beige] \
     779                -row $row -column $col
     780        grid [checkbutton $w.chk${num} -variable expgui(ProfRef$num) \
     781                -bg beige -activebackground beige] -row $row -column [incr col]
     782        grid [entry $w.ent${num} \
     783                -width 12] -row $row -column [incr col]
     784        if {$val != ""} {
     785            grid [button $w.def${num} -text $val -command \
     786                    "$w.ent${num} delete 0 end; $w.ent${num} insert end $val" \
     787                    ] -row $row -column [incr col] -sticky ew
    199788        } else {
    200             if {$cmd != ""} {append cmd "\;"}
    201             append cmd "[file join $expgui(gsasexe) $prog] $expnam"
    202         }
    203     }
    204     forknewterm "$prog -- $expnam" $cmd $concurrent 1 1
    205     # load the changed .EXP file automatically?
    206     if {$expgui(autoexpload)} {
    207         # load the revised exp file
    208         loadexp $expgui(expfile)
    209     }
    210 }
    211 
    212 # run liveplot
    213 proc liveplot {} {
    214     global expgui liveplot wishshell
    215     set expnam [file root [file tail $expgui(expfile)]]
    216     exec $wishshell [file join $expgui(scriptdir) liveplot] \
    217             $expnam $liveplot(hst) $liveplot(legend) &
    218 }
    219 
    220 # run lstview
    221 proc lstview {} {
    222     global expgui wishshell
    223     set expnam [file root [file tail $expgui(expfile)]]
    224     exec $wishshell [file join $expgui(scriptdir) lstview] $expnam &
    225 }
    226 
    227 # run widplt
    228 proc widplt {} {
    229     global expgui wishshell
    230     exec $wishshell [file join $expgui(scriptdir) widplt] \
    231             $expgui(expfile) &
     789            grid [label $w.def${num} -text (none) \
     790                    ] -row $row -column [incr col]
     791        }
     792        set curval {}
     793        catch {
     794            set curval [expr $oldval($term)]
     795            # pretty up the number
     796            if {$curval == 0.0} {
     797                set curval 0.0
     798            } elseif {abs($curval) < 1e-2 || abs($curval) > 1e6} {
     799                set curval [format %.3e $curval]
     800            } elseif {abs($curval) > 1e-2 && abs($curval) < 10} {
     801                set curval [format %.5f $curval]
     802            } elseif {abs($curval) < 9999} {
     803                set curval [format %.2f $curval]
     804            } elseif {abs($curval) < 1e6} {
     805                set curval [format %.0f $curval]
     806            }
     807            grid [button $w.cur${num} -text $curval -command  \
     808                    "$w.ent${num} delete 0 end; $w.ent${num} insert end $curval" \
     809                    ] -row $row -column [incr col] -sticky ew
     810        }
     811        # set default values for flag and value
     812        set ref 0
     813        if {$setnum != ""} {
     814            catch {
     815                if {[profdefinfo $hist $setnum pref$num] == "Y"} {set ref 1}
     816            }
     817        }
     818        set expgui(ProfRef$num) $ref
     819       
     820        $w.ent${num} delete 0 end
     821        if {!$expgui(DefaultPeakType) && $val != ""} {
     822            $w.ent${num} insert end $val
     823        } elseif {$curval != ""} {
     824            $w.ent${num} insert end $curval
     825        } elseif {$val != ""} {
     826            $w.ent${num} insert end $val
     827        } else {
     828            $w.ent${num} insert end 0.0
     829        }
     830        if {$row > $maxrow} {
     831            set row 2
     832            incr colstr 5
     833        }
     834    }
     835}
     836
     837#------------------------------------------------------------------------------
     838# WWW/help routines
     839#------------------------------------------------------------------------------
     840# browse a WWW page with URL. The URL may contain a #anchor
     841# On UNIX assume netscape is in the path or env(BROWSER) is loaded.
     842# On Windows search the registry for a browser. Mac branch not tested.
     843# This is taken from http://mini.net/cgi-bin/wikit/557.html with many thanks
     844# to the contributers
     845proc urlOpen {url} {
     846    global env tcl_platform
     847    switch $tcl_platform(platform) {
     848        "unix" {
     849            if {![info exists env(BROWSER)]} {
     850                set progs [auto_execok netscape]
     851                if {[llength $progs]} {
     852                    set env(BROWSER) [list $progs]
     853                }
     854            }
     855            if {[info exists env(BROWSER)]} {
     856                if {[catch {exec $env(BROWSER) -remote openURL($url)}]} {
     857                    # perhaps browser doesn't understand -remote flag
     858                    if {[catch {exec $env(BROWSER) $url &} emsg]} {
     859                        error "Error displaying $url in browser\n$emsg"
     860                    }
     861                }
     862            } else {
     863                tk_dialog .warn "No Browser" \
     864                        "Could not find a browser. Netscape is not in path. Define environment variable BROWSER to be full path name of browser." \
     865                        warn 0 OK
     866            }
     867        }
     868        "windows" {
     869            package require registry
     870            # Look for the application under
     871            # HKEY_CLASSES_ROOT
     872            set root HKEY_CLASSES_ROOT
     873
     874            # Get the application key for HTML files
     875            set appKey [registry get $root\\.html ""]
     876
     877            # Get the command for opening HTML files
     878            set appCmd [registry get \
     879                    $root\\$appKey\\shell\\open\\command ""]
     880
     881            # Substitute the HTML filename into the command for %1
     882            regsub %1 $appCmd $url appCmd
     883           
     884            # Double up the backslashes for eval (below)
     885            regsub -all {\\} $appCmd  {\\\\} appCmd
     886           
     887            # Invoke the command
     888            eval exec $appCmd &
     889        }
     890        "macintosh" {
     891            if {0 == [info exists env(BROWSER)]} {
     892                set env(BROWSER) "Browse the Internet"
     893            }
     894            if {[catch {
     895                AppleScript execute\
     896                    "tell application \"$env(BROWSER)\"
     897                         open url \"$url\"
     898                     end tell
     899                "} emsg]
     900            } then {
     901                error "Error displaying $url in browser\n$emsg"
     902            }
     903        }
     904    }
     905}
     906
     907proc NetHelp {file anchor localloc netloc} {
     908    if {[file exists [file join $localloc $file]]} {
     909        set url "[file join $localloc $file]"
     910    } else {
     911        set url "http://$netloc/$file"
     912    }
     913    catch {
     914        pleasewait "Starting web browser..."
     915        after 2000 donewait
     916    }
     917    if {$anchor != ""} {
     918        append url # $anchor
     919    }
     920    urlOpen $url
     921}
     922
     923proc MakeWWWHelp {"topic {}" "anchor {}"} {
     924    global expgui
     925    if {$topic == ""} {
     926        foreach item $expgui(notebookpagelist) {
     927            if {[lindex $item 0] == $expgui(pagenow)} {
     928                NetHelp [lindex $item 5] [lindex $item 6] $expgui(docdir) ""
     929                return
     930            }
     931        }
     932        # this should not happen
     933        NetHelp expgui.html "" $expgui(docdir) ""       
     934    } elseif {$topic == "menu"} {
     935        NetHelp expguic.html "" $expgui(docdir) ""
     936    } else {
     937        NetHelp $topic $anchor $expgui(docdir) ""
     938    }
    232939}
    233940
     
    279986}
    280987
     988
     989#------------------------------------------------------------------------------
     990# utilities
     991#------------------------------------------------------------------------------
     992# run liveplot
     993proc liveplot {} {
     994    global expgui liveplot wishshell
     995    set expnam [file root [file tail $expgui(expfile)]]
     996    exec $wishshell [file join $expgui(scriptdir) liveplot] \
     997            $expnam $liveplot(hst) $liveplot(legend) &
     998}
     999
     1000# run lstview
     1001proc lstview {} {
     1002    global expgui wishshell
     1003    set expnam [file root [file tail $expgui(expfile)]]
     1004    exec $wishshell [file join $expgui(scriptdir) lstview] $expnam &
     1005}
     1006
     1007# run widplt
     1008proc widplt {} {
     1009    global expgui wishshell
     1010    exec $wishshell [file join $expgui(scriptdir) widplt] \
     1011            $expgui(expfile) &
     1012}
     1013
    2811014# compute the composition for each phase and display in a dialog
    2821015proc composition {} {
     
    3291062}
    3301063
    331 # write text to the .LST file
    332 proc writelst {text} {
    333     global expgui
    334     set lstnam [file rootname $expgui(expfile)].LST
    335     set fp [open $lstnam a]
    336     puts $fp "\n-----------------------------------------------------------------"
    337     puts $fp $text
    338     puts $fp "-----------------------------------------------------------------\n"
    339     close $fp
    340 }
    341 
    3421064# save coordinates in an MSI .xtl file
    3431065proc exp2xtl {} {
     
    4671189}
    4681190
    469 
    470 # convert a file
    471 proc convfile {} {
    472     global tcl_platform
    473     if {$tcl_platform(platform) == "windows"} {
    474         convwin
    475     } else {
    476         convunix
    477     }
    478 }
    479 
    480 # file conversions for UNIX (convstod convdtos)
    481 proc convunix {} {
    482     global expgui infile outfile
    483     set frm .file
    484     catch {destroy $frm}
    485     toplevel $frm
    486     wm title $frm "Convert File"
    487     bind $frm <Key-F1> "MakeWWWHelp expgui.html ConvertUnix"
    488 
    489     pack [frame [set frm0 $frm.0] -bd 2 -relief groove] \
    490             -padx 3 -pady 3 -side top -fill x
    491     pack [frame $frm.mid] -side top
    492     pack [frame [set frmA $frm.mid.1] -bd 2 -relief groove] \
    493             -padx 3 -pady 3 -side left
    494     pack [label $frmA.0 -text "Select an input file"] -side top -anchor center
    495     pack [frame [set frmB $frm.mid.2] -bd 2 -relief groove] \
    496             -padx 3 -pady 3 -side left
    497     pack [label $frmB.0 -text "Enter an output file"] -side top -anchor center
    498     pack [frame [set frmC $frm.3] ] -padx 3 -pady 3 -side top -fill x -expand y
    499 
    500     pack [label $frm0.1 -text "Convert to:"] -side top -anchor center
    501     pack [frame $frm0.2] -side top -anchor center
    502     pack [radiobutton $frm0.2.d -text "direct access" -value convstod \
    503             -command setoutfile \
    504             -variable outfile(type)] -side left -anchor center
    505     pack [radiobutton $frm0.2.s -text "sequential" -value convdtos \
    506             -command setoutfile \
    507             -variable outfile(type)] -side right -anchor center
    508     set outfile(type) ""
    509 
    510     pack [button $frmC.b -text Convert -command "valid_conv_unix"] -side left
    511     pack [button $frmC.q -text Quit -command "set infile(done) 1"] -side left
    512     pack [button $frmC.help -text Help -bg yellow \
    513             -command "MakeWWWHelp expgui.html ConvertUnix"] \
    514             -side right
    515    
    516     unixcnvbox $frmA infile 1
    517     unixcnvbox $frmB outfile 0
    518     set infile(done) 0
    519     bind $frm <Return> "valid_conv_unix"
    520     # force the window to stay on top
    521     putontop $frm
    522     focus $frmC.q
    523     update
    524     tkwait variable infile(done)
    525     destroy $frm
    526     afterputontop
    527 }
    528 
    529 # validate the files and make the conversion -- unix
    530 proc valid_conv_unix {} {
    531     global infile outfile expgui
    532     set error {}
    533     if {$outfile(type) == "convstod" || $outfile(type) == "convdtos"} {
    534         set convtype $outfile(type)
    535     } else {
    536         append error "You must specify a conversion method: to direct access or to sequential.\n"
    537     }
    538     if {$infile(name) == ""} {
    539         append error "You must specify an input file to convert.\n"
    540     }
    541     if {$outfile(name) == ""} {
    542         append error "You must specify an output file name for the converted file.\n"
    543     }
    544     if {$error != ""} {
    545         tk_dialog .warn Notify $error warning 0 OK
    546         return
    547     }
    548 
    549     if {$infile(name) == $outfile(name)} {
    550         MyMessageBox -parent . -title Notify \
    551                 -message "Sorry, filenames must differ" \
    552                 -icon warning -helplink "expguierr.html ConvSameName"
    553         tk_dialog .warn Notify "Sorry, filenames must differ" warning 0 OK
    554         return
    555     }
    556     if ![file exists [file join $infile(dir) $infile(name)]] {
    557         MyMessageBox -parent . -title Notify \
    558                 -message "Sorry, file $infile(name) not found in $infile(dir)" \
    559                 -icon warning -helplink "expguierr.html ConvNotFound"
    560         return
    561     }
    562     if [file exists [file join $outfile(dir) $outfile(name)]] {
    563         set ans [MyMessageBox -parent . -title "Overwrite?" \
    564                 -message "Warning: file $outfile(name) exists in $outfile(dir). OK to overwrite?" \
    565                 -icon warning -type {Overwrite Cancel} -default Overwrite \
    566                 -helplink "expguierr.html OverwriteCnv"]
    567         if {[string tolower $ans] == "cancel"} return
    568     }
    569     if [catch {
    570         exec [file join $expgui(gsasexe) $convtype] < \
    571                 [file join $infile(dir) $infile(name)] > \
    572                 [file join $outfile(dir) $outfile(name)]
    573     } errmsg] {
    574         tk_dialog .warn Notify "Error in conversion:\n$errmsg" warning 0 OK
    575     } else {
    576         if [tk_dialog .converted Notify \
    577                 "File converted. Convert more files?" \
    578                 ""  0 Yes No] {set infile(done) 1}
    579     }
    580 }
    581 
    582 # create a file box for UNIX conversions
    583 proc unixcnvbox {bx filvar diropt} {
    584     global ${filvar} expgui
    585     pack [frame $bx.top] -side top
    586     pack [label $bx.top.a -text "Directory" ] -side left
    587     set ${filvar}(FileDirButtonMenu) [tk_optionMenu $bx.top.d ${filvar}(dir) [pwd] ]
    588     pack $bx.top.d -side left
    589     set ${filvar}(dir) [pwd]
    590 
    591 #    pack [label $bx.d -textvariable ${filvar}(dir) -bd 2 -relief raised ] -side top
    592 #    set ${filvar}(dir) [pwd]
    593 
    594     pack [frame $bx.a -width 200 -height 75] -side top -expand yes -fill both
    595     listbox $bx.a.files -relief raised -bd 2 \
    596             -yscrollcommand "$bx.a.scroll set" \
    597             -height 15 -width 0 -exportselection 0
    598     scrollbar $bx.a.scroll -command "$bx.a.files yview"
    599     unixFilChoose $bx $bx.a.files $filvar $diropt
    600     if {$filvar == "infile"} {
    601         bind $bx.a.files <ButtonRelease-1> \
    602                 "unixFilChoose $bx $bx.a.files $filvar $diropt; setoutfile"
    603     } else {
    604         bind $bx.a.files <ButtonRelease-1> \
    605                 "unixFilChoose $bx $bx.a.files $filvar $diropt"
    606     }
    607     pack $bx.a.scroll -side left -fill y
    608     pack $bx.a.files -side left -fill both -expand yes
    609     pack [entry $bx.c -textvariable ${filvar}(name)] -side top
    610 }
    611 
    612 # select a file or directory, also called when box is created to fill it
    613 proc unixFilChoose {frm box filvar {dironly 1}} {
    614     global $filvar
    615     set select [$box curselection]
    616     if {$select == ""} {
    617         set file .
    618     } else {
    619         set file [string trim [$box get $select]]
    620     }
    621     if [file isdirectory [file join [set ${filvar}(dir)] $file]] {
    622         if {$file == ".."} {
    623             set ${filvar}(dir) [file dirname [set ${filvar}(dir)] ]
    624         } elseif {$file != "."} {
    625             set ${filvar}(dir) [file join [set ${filvar}(dir)] $file]
    626         }
    627         [set ${filvar}(FileDirButtonMenu)] delete 0 end
    628         set list ""
    629         set dir ""
    630         foreach subdir [file split [set ${filvar}(dir)]] {
    631             set dir [file join $dir $subdir]
    632             lappend list $dir
    633         }
    634         foreach path $list {
    635             [set ${filvar}(FileDirButtonMenu)] add command -label $path \
    636                 -command "[list set ${filvar}(dir) $path]; \
    637                 unixFilChoose $frm $box $filvar $dironly"
    638         }
    639         set ${filvar}(name) {}
    640         $box delete 0 end
    641         $box insert end {..   }
    642         foreach file [lsort [glob -nocomplain \
    643                 [file join [set ${filvar}(dir)] *] ] ] {
    644             if {[file isdirectory $file]} {
    645                 # is this / needed here? Does it cause a problem in MacGSAS?
    646                 $box insert end [file tail $file]/
    647             } elseif {$dironly == 1} {
    648                 $box insert end [file tail $file]
    649             } elseif {$dironly == 2 && [file extension $file] == ".EXP"} {
    650                 $box insert end [file tail $file]
    651             }
    652         }
    653         return
    654     }
    655     set ${filvar}(name) [file tail $file]
    656 }
    657 
    658 # set new file name from old -- used for convunix
    659 proc setoutfile {} {
    660     global infile outfile
    661     if {$outfile(type) == "convstod"} {
    662         set lfile [string toupper $infile(name)]
    663     } elseif {$outfile(type) == "convdtos"} {
    664         set lfile [string tolower $infile(name)]
    665     } else {
    666         set lfile ""
    667     }
    668     if {$infile(name) == $lfile} {
    669         set outfile(name) {}
    670     } else {
    671         set outfile(name) $lfile
    672     }
    673 }
    674 
    675 #------------------------------------------------------------------------------
    676 # file conversions for Windows
    677 #------------------------------------------------------------------------------
    678 proc convwin {} {
    679     global expgui
    680     set frm .file
    681     catch {destroy $frm}
    682     toplevel $frm
    683     wm title $frm "Convert File"
    684     bind $frm <Key-F1> "MakeWWWHelp expgui.html ConvertWin"
    685     pack [frame [set frmA $frm.1] -bd 2 -relief groove] -padx 3 -pady 3 -side left
    686     pack [frame [set frmC $frm.3] ] -padx 3 -pady 3 \
    687             -side left -fill y -expand yes
    688     pack [button $frmC.help -text Help -bg yellow \
    689             -command "MakeWWWHelp expgui.html ConvertWin"] -side top
    690     pack [button $frmC.q -text Quit -command "destroy $frm"] -side bottom
    691     pack [button $frmC.b -text Convert -command "ValidWinCnv $frm"] \
    692             -side bottom
    693     pack [label $frmA.0 -text "Select a file to convert"] -side top -anchor center
    694     winfilebox $frm
    695     bind $frm <Return> "ValidWinCnv $frm"
    696 
    697     # force the window to stay on top
    698     putontop $frm
    699     focus $frmC.q
    700     tkwait window $frm
    701     afterputontop
    702 }
    703 
    704 # validate the files and make the conversion
    705 proc ValidWinCnv {frm} {
    706     global expgui
    707     # change backslashes to something sensible
    708     regsub -all {\\} $expgui(FileMenuCnvName) / expgui(FileMenuCnvName)
    709     # allow entry of D: for D:/ and D:TEST for d:/TEST
    710     if {[string first : $expgui(FileMenuCnvName)] != -1 && \
    711             [string first :/ $expgui(FileMenuCnvName)] == -1} {
    712         regsub : $expgui(FileMenuCnvName) :/ expgui(FileMenuCnvName)
    713     }
    714     if {$expgui(FileMenuCnvName) == "<Parent>"} {
    715         set expgui(FileMenuDir) [file dirname [set expgui(FileMenuDir)] ]
    716         ChooseWinCnv $frm
    717         return
    718     } elseif [file isdirectory \
    719             [file join $expgui(FileMenuDir) $expgui(FileMenuCnvName)]] {
    720         if {$expgui(FileMenuCnvName) != "."} {
    721             set expgui(FileMenuDir) \
    722                 [file join $expgui(FileMenuDir) $expgui(FileMenuCnvName)]
    723         }
    724         ChooseWinCnv $frm
    725         return
    726     }
    727  
    728     set file [file join $expgui(FileMenuDir) $expgui(FileMenuCnvName)]
    729     if ![file exists $file] {
    730         tk_dialog .warn "Convert Error" \
    731                 "File $file does not exist" question 0 "OK"
    732         return
    733     }
    734 
    735     set tmpname "[file join [file dirname $file] tempfile.xxx]"
    736     set oldname "[file rootname $file].org"
    737     if [file exists $oldname] {
    738         set ans [MyMessageBox -parent . -title "Overwrite?" \
    739                 -message "File [file tail $oldname] exists in [file dirname $oldname]. OK to overwrite?" \
    740                 -icon warning -type {Overwrite Cancel} -default Overwrite \
    741                 -helplink "expguierr.html OverwriteCnv"]
    742         if {[string tolower $ans] == "cancel"} return
    743         catch {file delete $oldname}
    744     }
    745 
    746     if [catch {
    747         set in [open $file r]
    748         set out [open $tmpname w]
    749         set len [gets $in line]
    750         if {$len > 160} {
    751             # this is a UNIX file. Hope there are no control characters
    752             set i 0
    753             set j 79
    754             while {$j < $len} {
    755                 puts $out [string range $line $i $j]
    756                 incr i 80
    757                 incr j 80
    758             }
    759         } else {
    760             while {$len >= 0} {
    761                 append line "                                        "
    762                 append line "                                        "
    763                 set line [string range $line 0 79]
    764                 puts $out $line
    765                 set len [gets $in line]
    766             }
    767         }
    768         close $in
    769         close $out
    770         file rename -force $file $oldname
    771         file rename -force $tmpname $file
    772     } errmsg] {
    773         tk_dialog .warn Notify "Error in conversion:\n$errmsg" warning 0 OK
    774     } else {
    775         if [tk_dialog .converted Notify \
    776                 "File [file tail $file] converted. (Original saved as [file tail $oldname]).\n\n Convert more files?" \
    777                 ""  0 Yes No] {destroy $frm}
    778     }
    779 }
    780 
    781 # create a file box
    782 proc winfilebox {frm} {
    783     global expgui
    784     set bx $frm.1
    785     pack [frame $bx.top] -side top
    786     pack [label $bx.top.a -text "Directory" ] -side left
    787     set expgui(FileDirButtonMenu) [tk_optionMenu $bx.top.d expgui(FileMenuDir) [pwd] ]
    788     pack $bx.top.d -side left
    789     set expgui(FileMenuDir) [pwd]
    790     # the icon below is from tk8.0/tkfbox.tcl
    791     set upfolder [image create bitmap -data {
    792 #define updir_width 28
    793 #define updir_height 16
    794 static char updir_bits[] = {
    795    0x00, 0x00, 0x00, 0x00, 0x80, 0x1f, 0x00, 0x00, 0x40, 0x20, 0x00, 0x00,
    796    0x20, 0x40, 0x00, 0x00, 0xf0, 0xff, 0xff, 0x01, 0x10, 0x00, 0x00, 0x01,
    797    0x10, 0x02, 0x00, 0x01, 0x10, 0x07, 0x00, 0x01, 0x90, 0x0f, 0x00, 0x01,
    798    0x10, 0x02, 0x00, 0x01, 0x10, 0x02, 0x00, 0x01, 0x10, 0x02, 0x00, 0x01,
    799    0x10, 0xfe, 0x07, 0x01, 0x10, 0x00, 0x00, 0x01, 0x10, 0x00, 0x00, 0x01,
    800    0xf0, 0xff, 0xff, 0x01};}]
    801 
    802     pack [button $bx.top.b -image $upfolder \
    803             -command "updir; ChooseWinCnv $frm" ]
    804     pack [frame $bx.a -width 200 -height 75] -side top -expand yes -fill both
    805     listbox $bx.a.files -relief raised -bd 2  -exportselection 0 \
    806             -yscrollcommand "sync2boxes $bx.a.files $bx.a.dates $bx.a.scroll" \
    807             -height 15 -width 0
    808     listbox $bx.a.dates -relief raised -bd 2  -exportselection 0 \
    809             -yscrollcommand "sync2boxes $bx.a.dates $bx.a.files $bx.a.scroll" \
    810             -height 15 -width 0 -takefocus 0
    811     scrollbar $bx.a.scroll -command "move2boxesY \" $bx.a.files $bx.a.dates \" "
    812     ChooseWinCnv $frm
    813     bind $bx.a.files <ButtonRelease-1> "ReleaseWinCnv $frm"
    814     bind $bx.a.dates <ButtonRelease-1> "ReleaseWinCnv $frm"
    815     bind $bx.a.files <Double-1> "SelectWinCnv $frm"
    816     bind $bx.a.dates <Double-1> "SelectWinCnv $frm"
    817     pack $bx.a.scroll -side left -fill y
    818     pack $bx.a.files $bx.a.dates -side left -fill both -expand yes
    819     pack [entry $bx.c -textvariable expgui(FileMenuCnvName)] -side top
    820 }
    821 
    822 # set the box or file in the selection window
    823 proc ReleaseWinCnv {frm} {
    824     global expgui
    825     set files $frm.1.a.files
    826     set dates $frm.1.a.dates
    827     set select [$files curselection]
    828     if {$select == ""} {
    829         set select [$dates curselection]
    830     }
    831     if {$select == ""} {
    832         set expgui(FileMenuCnvName) ""
    833     } else {
    834         set expgui(FileMenuCnvName) [string trim [$files get $select]]
    835     }
    836     if {$expgui(FileMenuCnvName) == "<Parent>"} {
    837         set expgui(FileMenuDir) [file dirname $expgui(FileMenuDir)]
    838         ChooseWinCnv $frm
    839     } elseif [file isdirectory \
    840             [file join [set expgui(FileMenuDir)] $expgui(FileMenuCnvName)]] {
    841         if {$expgui(FileMenuCnvName) != "."} {
    842             set expgui(FileMenuDir) [file join $expgui(FileMenuDir) $expgui(FileMenuCnvName)]
    843             ChooseWinCnv $frm
    844         }
    845     }
    846     return
    847 }
    848 
    849 # select a file or directory -- called on double click
    850 proc SelectWinCnv {frm} {
    851     global expgui
    852     set files $frm.1.a.files
    853     set dates $frm.1.a.dates
    854     set select [$files curselection]
    855     if {$select == ""} {
    856         set select [$dates curselection]
    857     }
    858     if {$select == ""} {
    859         set file .
    860     } else {
    861         set file [string trim [$files get $select]]
    862     }
    863     if {$file == "<Parent>"} {
    864         set expgui(FileMenuDir) [file dirname [set expgui(FileMenuDir)] ]
    865         ChooseWinCnv $frm
    866     } elseif [file isdirectory [file join [set expgui(FileMenuDir)] $file]] {
    867         if {$file != "."} {
    868             set expgui(FileMenuDir) [file join [set expgui(FileMenuDir)] $file]
    869             ChooseWinCnv $frm
    870         }
    871     } else {
    872         set expgui(FileMenuCnvName) [file tail $file]
    873         ValidWinCnv $frm
    874     }
    875 }
    876 
    877 # fill the files & dates & Directory selection box with current directory,
    878 # also called when box is created to fill it
    879 proc ChooseWinCnv {frm} {
    880     global expgui
    881     set files $frm.1.a.files
    882     set dates $frm.1.a.dates
    883     set expgui(FileMenuCnvName) {}
    884     $files delete 0 end
    885     $dates delete 0 end
    886     $files insert end {<Parent>}
    887     $dates insert end {(Directory)}
    888     set filelist [glob -nocomplain \
    889             [file join [set expgui(FileMenuDir)] *] ]
    890     foreach file [lsort -dictionary $filelist] {
    891         if {[file isdirectory $file]} {
    892             $files insert end [file tail $file]
    893             $dates insert end {(Directory)}
    894         }
    895     }
    896     foreach file [lsort -dictionary $filelist] {
    897         if {![file isdirectory $file]} {
    898             set modified [clock format [file mtime $file] -format "%T %D"]
    899             $files insert end [file tail $file]
    900             $dates insert end $modified
    901         }
    902     }
    903     $expgui(FileDirButtonMenu)  delete 0 end
    904     set list ""
    905     catch {set list [file volume]}
    906     set dir ""
    907     foreach subdir [file split [set expgui(FileMenuDir)]] {
    908         set dir [file join $dir $subdir]
    909         lappend list $dir
    910     }
    911     foreach path $list {
    912         $expgui(FileDirButtonMenu) add command -label $path \
    913                 -command "[list set expgui(FileMenuDir) $path]; \
    914                 ChooseWinCnv $frm"
    915     }
    916     return
    917 }
    918 
    919 #------------------------------------------------------------------------------
    920 # set options for liveplot
    921 proc liveplotopt {} {
    922     global liveplot expmap
    923     set frm .file
    924     catch {destroy $frm}
    925     toplevel $frm
    926     pack [frame [set frmA $frm.1] -bd 2 -relief groove] -padx 3 -pady 3 -side left
    927     set last [lindex [lsort -integer $expmap(powderlist)] end]
    928     if {$last == ""} {set last 1}
    929     pack [scale  $frmA.1 -label "Histogram number" -from 1 -to $last \
    930             -length  150 -orient horizontal -variable liveplot(hst)] -side top
    931     pack [checkbutton $frmA.2 -text {include plot legend}\
    932             -variable liveplot(legend)] -side top
    933     pack [button $frm.2 -text OK \
    934             -command {if ![catch {expr $liveplot(hst)}] "destroy .file"} \
    935             ] -side top
    936     bind $frm <Return> {if ![catch {expr $liveplot(hst)}] "destroy .file"}
    937    
    938     # force the window to stay on top
    939     putontop $frm
    940     focus $frm.2
    941     tkwait window $frm
    942     afterputontop
    943 }
    944 
    945 #------------------------------------------------------------------------------
    946 # get an experiment file name
    947 #------------------------------------------------------------------------------
    948 proc getExpFileName {mode} {
    949     global expgui
    950     set frm .file
    951     catch {destroy $frm}
    952     toplevel $frm
    953     wm title $frm "Experiment file"
    954     bind $frm <Key-F1> "MakeWWWHelp expguierr.html open"
    955     pack [frame [set frmA $frm.1] -bd 2 -relief groove] -padx 3 -pady 3 -side left
    956     pack [frame [set frmC $frm.3] ] -padx 3 -pady 3 -side left \
    957             -fill y -expand yes
    958     pack [button $frmC.help -text Help -bg yellow \
    959             -command "MakeWWWHelp expguierr.html open"] \
    960             -side top -anchor e
    961     pack [label $frmC.2 -text "Sort .EXP files by" ] -side top
    962     pack [radiobutton $frmC.1 -text "File Name" -value 1 \
    963             -variable expgui(filesort) -command "ChooseExpFil $frmA"] -side top
    964     pack [radiobutton $frmC.0 -text "Mod. Date" -value 0 \
    965             -variable expgui(filesort) -command "ChooseExpFil $frmA"] -side top
    966     pack [button $frmC.b -text Read \
    967             -command "valid_exp_file $frmA $mode"] -side bottom
    968     if {$mode == "new"} {
    969         $frmC.b config -text Save
    970     }
    971     pack [button $frmC.q -text Quit \
    972             -command "set expgui(FileMenuEXPNAM) {}; destroy $frm"] -side bottom
    973     bind $frm <Return> "$frmC.b invoke"
    974 
    975     if {$mode == "new"} {
    976         pack [label $frmA.0 -text "Enter an experiment file to create"] \
    977                 -side top -anchor center
    978     } else {
    979         pack [label $frmA.0 -text "Select an experiment file to read"] \
    980                 -side top -anchor center
    981     }
    982     expfilebox $frmA $mode
    983     # force the window to stay on top
    984     putontop $frm
    985     focus $frmC.b
    986     tkwait window $frm
    987     afterputontop
    988     if {$expgui(FileMenuEXPNAM) == ""} return
    989     return [file join $expgui(FileMenuDir) $expgui(FileMenuEXPNAM)]
    990 }
    991 
    992 # validation routine
    993 proc valid_exp_file {frm mode} {
    994     global expgui tcl_platform
    995     # windows fixes
    996     if {$tcl_platform(platform) == "windows"} {
    997         # change backslashes to something sensible
    998         regsub -all {\\} $expgui(FileMenuEXPNAM) / expgui(FileMenuEXPNAM)
    999         # allow entry of D: for D:/ and D:TEST for d:/TEST
    1000         if {[string first : $expgui(FileMenuEXPNAM)] != -1 && \
    1001                 [string first :/ $expgui(FileMenuEXPNAM)] == -1} {
    1002             regsub : $expgui(FileMenuEXPNAM) :/ expgui(FileMenuEXPNAM)
    1003         }
    1004     }
    1005     if {$expgui(FileMenuEXPNAM) == "<Parent>"} {
    1006         set expgui(FileMenuDir) [file dirname [set expgui(FileMenuDir)] ]
    1007         ChooseExpFil $frm
    1008         return
    1009     } elseif [file isdirectory \
    1010             [file join $expgui(FileMenuDir) $expgui(FileMenuEXPNAM)]] {
    1011         if {$expgui(FileMenuEXPNAM) != "."} {
    1012             set expgui(FileMenuDir) \
    1013                 [file join $expgui(FileMenuDir) $expgui(FileMenuEXPNAM)]
    1014         }
    1015         ChooseExpFil $frm
    1016         return
    1017     }
    1018     # append a .EXP if not present
    1019     if {[file extension $expgui(FileMenuEXPNAM)] == ""} {
    1020         append expgui(FileMenuEXPNAM) ".EXP"
    1021     }
    1022     # flag files that end in something other than .EXP .exp or .Exp...
    1023     if {[string toupper [file extension $expgui(FileMenuEXPNAM)]] != ".EXP"} {
    1024         tk_dialog .expFileErrorMsg "File Open Error" \
    1025             "File [file tail $expgui(FileMenuEXPNAM)] is not a valid name. Experiment files must end in \".EXP\"" \
    1026             error 0 OK
    1027         return
    1028     }
    1029     # check on the file status
    1030     set file [file join $expgui(FileMenuDir) $expgui(FileMenuEXPNAM)]
    1031     if {$mode == "new" && [file exists $file]} {
    1032         set ans [
    1033         MyMessageBox -parent . -title "File Open Error" \
    1034                 -message "File [file tail $file] already exists in [file dirname $file]. OK to overwrite?" \
    1035                 -icon question -type {"Select other" "Overwrite"} -default "select other" \
    1036                 -helplink "expguierr.html OverwriteErr"
    1037         ]
    1038         if {[string tolower $ans] == "overwrite"} {destroy .file}
    1039         if $ans {destroy .file}
    1040         return
    1041     }
    1042     # if file does not exist in case provided, set the name to all
    1043     # upper case letters, since that is the best choice.
    1044     # if it does exist, read from it as is. For UNIX we will force uppercase later.
    1045     if {![file exists $file]} {
    1046         set expgui(FileMenuEXPNAM) [string toupper $expgui(FileMenuEXPNAM)]
    1047         set file [file join $expgui(FileMenuDir) $expgui(FileMenuEXPNAM)]
    1048     }
    1049     if {$mode == "old" && ![file exists $file]} {
    1050         set ans [
    1051         MyMessageBox -parent . -title "File Open Error" \
    1052                 -message "File [file tail $file] does not exist in [file dirname $file]. OK to create?" \
    1053                 -icon question -type {"Select other" "Create"} -default "select other" \
    1054                 -helplink "expguierr.html OpenErr"
    1055         ]
    1056         if {[string tolower $ans] == "create"} {destroy .file}
    1057         return
    1058     }
    1059     destroy .file
    1060 }
    1061 
    1062 proc updir {} {
    1063     global expgui
    1064     set expgui(FileMenuDir) [file dirname [set expgui(FileMenuDir)]]
    1065 }
    1066 
    1067 # create a file box
    1068 proc expfilebox {bx mode} {
    1069     global expgui
    1070     pack [frame $bx.top] -side top
    1071     pack [label $bx.top.a -text "Directory" ] -side left
    1072     set expgui(FileDirButtonMenu) [tk_optionMenu $bx.top.d expgui(FileMenuDir) [pwd] ]
    1073     pack $bx.top.d -side left
    1074     set expgui(FileMenuDir) [pwd]
    1075     # the icon below is from tk8.0/tkfbox.tcl
    1076     set upfolder [image create bitmap -data {
    1077 #define updir_width 28
    1078 #define updir_height 16
    1079 static char updir_bits[] = {
    1080    0x00, 0x00, 0x00, 0x00, 0x80, 0x1f, 0x00, 0x00, 0x40, 0x20, 0x00, 0x00,
    1081    0x20, 0x40, 0x00, 0x00, 0xf0, 0xff, 0xff, 0x01, 0x10, 0x00, 0x00, 0x01,
    1082    0x10, 0x02, 0x00, 0x01, 0x10, 0x07, 0x00, 0x01, 0x90, 0x0f, 0x00, 0x01,
    1083    0x10, 0x02, 0x00, 0x01, 0x10, 0x02, 0x00, 0x01, 0x10, 0x02, 0x00, 0x01,
    1084    0x10, 0xfe, 0x07, 0x01, 0x10, 0x00, 0x00, 0x01, 0x10, 0x00, 0x00, 0x01,
    1085    0xf0, 0xff, 0xff, 0x01};}]
    1086 
    1087     pack [button $bx.top.b -image $upfolder \
    1088             -command "updir; ChooseExpFil $bx" ]
    1089     pack [frame $bx.a -width 200 -height 75] -side top -expand yes -fill both
    1090     listbox $bx.a.files -relief raised -bd 2 \
    1091             -yscrollcommand "sync2boxes $bx.a.files $bx.a.dates $bx.a.scroll" \
    1092             -height 15 -width 0
    1093     listbox $bx.a.dates -relief raised -bd 2 \
    1094             -yscrollcommand "sync2boxes $bx.a.dates $bx.a.files $bx.a.scroll" \
    1095             -height 15 -width 0 -takefocus 0
    1096     scrollbar $bx.a.scroll -command "move2boxesY \" $bx.a.files $bx.a.dates \" "
    1097     ChooseExpFil $bx
    1098     bind $bx.a.files <ButtonRelease-1> "ReleaseExpFil $bx"
    1099     bind $bx.a.dates <ButtonRelease-1> "ReleaseExpFil $bx"
    1100     bind $bx.a.files <Double-1> "SelectExpFil $bx $mode"
    1101     bind $bx.a.dates <Double-1> "SelectExpFil $bx $mode"
    1102     pack $bx.a.scroll -side left -fill y
    1103     pack $bx.a.files $bx.a.dates -side left -fill both -expand yes
    1104     pack [entry $bx.c -textvariable expgui(FileMenuEXPNAM)] -side top
    1105 }
    1106 proc sync2boxes {master slave scroll args} {
    1107     $slave yview moveto [lindex [$master yview] 0]
    1108     eval $scroll set $args
    1109 }
    1110 proc move2boxesY {boxlist args} {
    1111     foreach listbox $boxlist {
    1112         eval $listbox yview $args
    1113     }
    1114 }
    1115 
    1116 # set the box or file in the selection window
    1117 proc ReleaseExpFil {frm} {
    1118     global expgui
    1119     set files $frm.a.files
    1120     set dates $frm.a.dates
    1121     set select [$files curselection]
    1122     if {$select == ""} {
    1123         set select [$dates curselection]
    1124     }
    1125     if {$select == ""} {
    1126         set expgui(FileMenuEXPNAM) ""
    1127     } else {
    1128         set expgui(FileMenuEXPNAM) [string trim [$files get $select]]
    1129     }
    1130     if {$expgui(FileMenuEXPNAM) == "<Parent>"} {
    1131         set expgui(FileMenuDir) [file dirname $expgui(FileMenuDir)]
    1132         ChooseExpFil $frm
    1133     } elseif [file isdirectory \
    1134             [file join [set expgui(FileMenuDir)] $expgui(FileMenuEXPNAM)]] {
    1135         if {$expgui(FileMenuEXPNAM) != "."} {
    1136             set expgui(FileMenuDir) [file join $expgui(FileMenuDir) $expgui(FileMenuEXPNAM)]
    1137             ChooseExpFil $frm
    1138         }
    1139     }
    1140     return
    1141 }
    1142 
    1143 # select a file or directory -- called on double click
    1144 proc SelectExpFil {frm mode} {
    1145     global expgui
    1146     set files $frm.a.files
    1147     set dates $frm.a.dates
    1148     set select [$files curselection]
    1149     if {$select == ""} {
    1150         set select [$dates curselection]
    1151     }
    1152     if {$select == ""} {
    1153         set file .
    1154     } else {
    1155         set file [string trim [$files get $select]]
    1156     }
    1157     if {$file == "<Parent>"} {
    1158         set expgui(FileMenuDir) [file dirname [set expgui(FileMenuDir)] ]
    1159         ChooseExpFil $frm
    1160     } elseif [file isdirectory [file join [set expgui(FileMenuDir)] $file]] {
    1161         if {$file != "."} {
    1162             set expgui(FileMenuDir) [file join [set expgui(FileMenuDir)] $file]
    1163             ChooseExpFil $frm
    1164         }
    1165     } else {
    1166         set expgui(FileMenuEXPNAM) [file tail $file]
    1167         valid_exp_file $frm $mode
    1168     }
    1169 }
    1170 
    1171 # fill the files & dates & Directory selection box with current directory,
    1172 # also called when box is created to fill it
    1173 proc ChooseExpFil {frm} {
    1174     global expgui tcl_platform
    1175     set files $frm.a.files
    1176     set dates $frm.a.dates
    1177     set expgui(FileMenuEXPNAM) {}
    1178     $files delete 0 end
    1179     $dates delete 0 end
    1180     $files insert end {<Parent>}
    1181     $dates insert end {(Directory)}
    1182     set filelist [glob -nocomplain \
    1183             [file join [set expgui(FileMenuDir)] *] ]
    1184     foreach file [lsort -dictionary $filelist] {
    1185         if {[file isdirectory $file]} {
    1186             $files insert end [file tail $file]
    1187             $dates insert end {(Directory)}
    1188         }
    1189     }
    1190     set pairlist {}
    1191     foreach file [lsort -dictionary $filelist] {
    1192         if {![file isdirectory $file]  && \
    1193                 [string toupper [file extension $file]] == ".EXP"} {
    1194             set modified [file mtime $file]
    1195             lappend pairlist [list $file $modified]
    1196         }
    1197     }
    1198     if {$expgui(filesort) == 0} {
    1199         foreach pair [lsort -index 1 -integer $pairlist] {
    1200             set file [lindex $pair 0]
    1201             set modified [clock format [lindex $pair 1] -format "%T %D"]
    1202             $files insert end [file tail $file]
    1203             $dates insert end $modified
    1204         }
    1205     } else {
    1206         foreach pair [lsort -dictionary -index 0 $pairlist] {
    1207             set file [lindex $pair 0]
    1208             set modified [clock format [lindex $pair 1] -format "%T %D"]
    1209             $files insert end [file tail $file]
    1210             $dates insert end $modified
    1211         }
    1212     }
    1213     $expgui(FileDirButtonMenu)  delete 0 end
    1214     set list ""
    1215     if {$tcl_platform(platform) == "windows"} {
    1216         catch {set list [file volume]}
    1217     }
    1218    
    1219     set dir ""
    1220     foreach subdir [file split [set expgui(FileMenuDir)]] {
    1221         set dir [file join $dir $subdir]
    1222         lappend list $dir
    1223     }
    1224     foreach path $list {
    1225         $expgui(FileDirButtonMenu) add command -label $path \
    1226                 -command "[list set expgui(FileMenuDir) $path]; \
    1227                 ChooseExpFil $frm"
    1228     }
    1229     # highlight the current experiment -- if present
    1230     for {set i 0} {$i < [$files size]} {incr i} {
    1231         set file [$files get $i]
    1232         if {$expgui(expfile) == [file join $expgui(FileMenuDir) $file]} {
    1233             $files selection set $i
    1234         }
    1235     }
    1236     return
    1237 }
    1238 
    1239 proc putontop {w} {
    1240     # center window $w above its parent and make it stay on top
    1241     set wp [winfo parent $w]
    1242     wm transient $w [winfo toplevel $wp]
    1243     wm withdraw $w
    1244     update idletasks
    1245     # center the new window in the middle of the parent
    1246     set x [expr [winfo x $wp] + [winfo width $wp]/2 - \
    1247             [winfo reqwidth $w]/2 - [winfo vrootx $wp]]
    1248     if {$x < 0} {set x 0}
    1249     set xborder 10
    1250     if {$x+[winfo reqwidth $w] +$xborder > [winfo screenwidth $w]} {
    1251         incr x [expr \
    1252                 [winfo screenwidth $w] - ($x+[winfo reqwidth $w] + $xborder)]
    1253     }
    1254     set y [expr [winfo y $wp] + [winfo height $wp]/2 - \
    1255             [winfo reqheight $w]/2 - [winfo vrooty $wp]]
    1256     if {$y < 0} {set y 0}
    1257     set yborder 25
    1258     if {$y+[winfo reqheight $w] +$yborder > [winfo screenheight $w]} {
    1259         incr y [expr \
    1260                 [winfo screenheight $w] - ($y+[winfo reqheight $w] + $yborder)]
    1261     }
    1262     wm geom $w +$x+$y
    1263     wm deiconify $w
    1264 
    1265     global makenew
    1266     set makenew(OldGrab) ""
    1267     catch {set makenew(OldFocus) [focus]}
    1268     catch {set makenew(OldGrab) [grab current $w]}
    1269     catch {grab $w}
    1270 }
    1271 
    1272 proc afterputontop {} {
    1273     # restore focus
    1274     global makenew
    1275     catch {focus $makenew(OldFocus)}
    1276     catch {
    1277         if {$makenew(OldGrab) != ""} {
    1278             grab $makenew(OldGrab)
    1279         }
    1280     }
    1281 }
    1282 
    1283 proc ShowBigMessage {win labeltext msg "optionlist OK" "link {}"} {
    1284     catch {destroy $win}
    1285     toplevel $win
    1286 
    1287     pack [label $win.l1 -text $labeltext] -side top
    1288     pack [frame $win.f1] -side top -expand yes -fill both
    1289     grid [text  $win.f1.t  \
    1290             -height 20 -width 55  -wrap none -font Courier \
    1291             -xscrollcommand "$win.f1.bscr set" \
    1292             -yscrollcommand "$win.f1.rscr set" \
    1293             ] -row 1 -column 0 -sticky news
    1294     grid [scrollbar $win.f1.bscr -orient horizontal \
    1295             -command "$win.f1.t xview" \
    1296             ] -row 2 -column 0 -sticky ew
    1297     grid [scrollbar $win.f1.rscr  -command "$win.f1.t yview" \
    1298             ] -row 1 -column 1 -sticky ns
    1299     # give extra space to the text box
    1300     grid columnconfigure $win.f1 0 -weight 1
    1301     grid rowconfigure $win.f1 1 -weight 1
    1302     $win.f1.t insert end $msg
    1303 
    1304     global makenew
    1305     set makenew(result) 0
    1306     bind $win <Return> "destroy $win"
    1307     bind $win <KeyPress-Prior> "$win.f1.t yview scroll -1 page"
    1308     bind $win <KeyPress-Next> "$win.f1.t yview scroll 1 page"
    1309     bind $win <KeyPress-Right> "$win.f1.t xview scroll 1 unit"
    1310     bind $win <KeyPress-Left> "$win.f1.t xview scroll -1 unit"
    1311     bind $win <KeyPress-Up> "$win.f1.t yview scroll -1 unit"
    1312     bind $win <KeyPress-Down> "$win.f1.t yview scroll 1 unit"
    1313     bind $win <KeyPress-Home> "$win.f1.t yview 0"
    1314     bind $win <KeyPress-End> "$win.f1.t yview end"
    1315     set i 0
    1316     foreach item $optionlist {
    1317         pack [button $win.q[incr i] \
    1318                 -command "set makenew(result) $i; destroy $win" -text $item] -side left
    1319     }
    1320     if {$link != ""} {
    1321         pack [button $win.help -text Help -bg yellow \
    1322             -command "MakeWWWHelp $link"] \
    1323             -side right
    1324         bind $win <Key-F1> "MakeWWWHelp $link"
    1325     }
    1326     putontop $win
    1327     tkwait window $win
    1328 
    1329     # fix focus...
    1330     afterputontop
    1331     return $makenew(result)
    1332 }
    1333 
    1334 #       Message box code that centers the message box over the parent.
    1335 #          or along the edge, if too close,
    1336 #          but leave a border along +x & +y for reasons I don't remember
    1337 #       It also allows the button names to be defined using
    1338 #            -type $list  -- where $list has a list of button names
    1339 #       larger messages are placed in a scrolled text widget
    1340 #       capitalization is now ignored for -default
    1341 #       The command returns the name button in all lower case letters
    1342 #       otherwise see  tk_messageBox for a description
    1343 #
    1344 #       This is a modification of tkMessageBox (msgbox.tcl v1.5)
    1345 #
    1346 proc MyMessageBox {args} {
    1347     global tkPriv tcl_platform
    1348 
    1349     set w tkPrivMsgBox
    1350     upvar #0 $w data
    1351 
    1352     #
    1353     # The default value of the title is space (" ") not the empty string
    1354     # because for some window managers, a
    1355     #           wm title .foo ""
    1356     # causes the window title to be "foo" instead of the empty string.
    1357     #
    1358     set specs {
    1359         {-default "" "" ""}
    1360         {-icon "" "" "info"}
    1361         {-message "" "" ""}
    1362         {-parent "" "" .}
    1363         {-title "" "" " "}
    1364         {-type "" "" "ok"}
    1365         {-helplink "" "" ""}
    1366     }
    1367 
    1368     tclParseConfigSpec $w $specs "" $args
    1369 
    1370     if {[lsearch {info warning error question} $data(-icon)] == -1} {
    1371         error "bad -icon value \"$data(-icon)\": must be error, info, question, or warning"
    1372     }
    1373     if {![string compare $tcl_platform(platform) "macintosh"]} {
    1374       switch -- $data(-icon) {
    1375           "error"     {set data(-icon) "stop"}
    1376           "warning"   {set data(-icon) "caution"}
    1377           "info"      {set data(-icon) "note"}
    1378         }
    1379     }
    1380 
    1381     if {![winfo exists $data(-parent)]} {
    1382         error "bad window path name \"$data(-parent)\""
    1383     }
    1384 
    1385     switch -- $data(-type) {
    1386         abortretryignore {
    1387             set buttons {
    1388                 {abort  -width 6 -text Abort -under 0}
    1389                 {retry  -width 6 -text Retry -under 0}
    1390                 {ignore -width 6 -text Ignore -under 0}
    1391             }
    1392         }
    1393         ok {
    1394             set buttons {
    1395                 {ok -width 6 -text OK -under 0}
    1396             }
    1397           if {![string compare $data(-default) ""]} {
    1398                 set data(-default) "ok"
    1399             }
    1400         }
    1401         okcancel {
    1402             set buttons {
    1403                 {ok     -width 6 -text OK     -under 0}
    1404                 {cancel -width 6 -text Cancel -under 0}
    1405             }
    1406         }
    1407         retrycancel {
    1408             set buttons {
    1409                 {retry  -width 6 -text Retry  -under 0}
    1410                 {cancel -width 6 -text Cancel -under 0}
    1411             }
    1412         }
    1413         yesno {
    1414             set buttons {
    1415                 {yes    -width 6 -text Yes -under 0}
    1416                 {no     -width 6 -text No  -under 0}
    1417             }
    1418         }
    1419         yesnocancel {
    1420             set buttons {
    1421                 {yes    -width 6 -text Yes -under 0}
    1422                 {no     -width 6 -text No  -under 0}
    1423                 {cancel -width 6 -text Cancel -under 0}
    1424             }
    1425         }
    1426         default {
    1427 #           error "bad -type value \"$data(-type)\": must be abortretryignore, ok, okcancel, retrycancel, yesno, or yesnocancel"
    1428             foreach item $data(-type) {
    1429                 lappend buttons [list [string tolower $item] -text $item -under 0]
    1430             }
    1431         }
    1432     }
    1433 
    1434     if {[string compare $data(-default) ""]} {
    1435         set valid 0
    1436         foreach btn $buttons {
    1437             if {![string compare [lindex $btn 0] [string tolower $data(-default)]]} {
    1438                 set valid 1
    1439                 break
    1440             }
    1441         }
    1442         if {!$valid} {
    1443             error "invalid default button \"$data(-default)\""
    1444         }
    1445     }
    1446 
    1447     # 2. Set the dialog to be a child window of $parent
    1448     #
    1449     #
    1450     if {[string compare $data(-parent) .]} {
    1451         set w $data(-parent).__tk__messagebox
    1452     } else {
    1453         set w .__tk__messagebox
    1454     }
    1455 
    1456     # 3. Create the top-level window and divide it into top
    1457     # and bottom parts.
    1458 
    1459     catch {destroy $w}
    1460     toplevel $w -class Dialog
    1461     wm title $w $data(-title)
    1462     wm iconname $w Dialog
    1463     wm protocol $w WM_DELETE_WINDOW { }
    1464     wm transient $w $data(-parent)
    1465     if {![string compare $tcl_platform(platform) "macintosh"]} {
    1466         unsupported1 style $w dBoxProc
    1467     }
    1468 
    1469     frame $w.bot
    1470     pack $w.bot -side bottom -fill both
    1471     frame $w.top
    1472     pack $w.top -side top -fill both -expand 1
    1473     if {$data(-helplink) != ""} {
    1474 #       frame $w.help
    1475 #       pack $w.help -side top -fill both
    1476         pack [button $w.top.1 -text Help -bg yellow \
    1477                 -command "MakeWWWHelp $data(-helplink)"] \
    1478                 -side right -anchor ne
    1479         bind $w <Key-F1> "MakeWWWHelp $data(-helplink)"
    1480     }
    1481     if {[string compare $tcl_platform(platform) "macintosh"]} {
    1482         $w.bot configure -relief raised -bd 1
    1483         $w.top configure -relief raised -bd 1
    1484     }
    1485 
    1486     # 4. Fill the top part with bitmap and message (use the option
    1487     # database for -wraplength and -font so that they can be
    1488     # overridden by the caller).
    1489 
    1490     option add *Dialog.msg.wrapLength 3i widgetDefault
    1491 
    1492     if {[string length $data(-message)] > 300} {
    1493         if {![string compare $tcl_platform(platform) "macintosh"]} {
    1494             option add *Dialog.msg.t.font system widgetDefault
    1495         } else {
    1496             option add *Dialog.msg.t.font {Times 18} widgetDefault
    1497         }
    1498         frame $w.msg
    1499         grid [text  $w.msg.t  \
    1500                 -height 20 -width 55 -relief flat -wrap word \
    1501                 -yscrollcommand "$w.msg.rscr set" \
    1502                 ] -row 1 -column 0 -sticky news
    1503         grid [scrollbar $w.msg.rscr  -command "$w.msg.t yview" \
    1504                 ] -row 1 -column 1 -sticky ns
    1505         # give extra space to the text box
    1506         grid columnconfigure $w.msg 0 -weight 1
    1507         grid rowconfigure $w.msg 1 -weight 1
    1508         $w.msg.t insert end $data(-message)
    1509     } else {
    1510         if {![string compare $tcl_platform(platform) "macintosh"]} {
    1511             option add *Dialog.msg.font system widgetDefault
    1512         } else {
    1513             option add *Dialog.msg.font {Times 18} widgetDefault
    1514         }
    1515         label $w.msg -justify left -text $data(-message)
    1516     }
    1517     pack $w.msg -in $w.top -side right -expand 1 -fill both -padx 3m -pady 3m
    1518     if {[string compare $data(-icon) ""]} {
    1519         label $w.bitmap -bitmap $data(-icon)
    1520         pack $w.bitmap -in $w.top -side left -padx 3m -pady 3m
    1521     }
    1522 
    1523     # 5. Create a row of buttons at the bottom of the dialog.
    1524 
    1525     set i 0
    1526     foreach but $buttons {
    1527         set name [lindex $but 0]
    1528         set opts [lrange $but 1 end]
    1529       if {![llength $opts]} {
    1530             # Capitalize the first letter of $name
    1531           set capName [string toupper \
    1532                     [string index $name 0]][string range $name 1 end]
    1533             set opts [list -text $capName]
    1534         }
    1535 
    1536       eval button [list $w.$name] $opts [list -command [list set tkPriv(button) $name]]
    1537 
    1538         if {![string compare $name [string tolower $data(-default)]]} {
    1539             $w.$name configure -default active
    1540         }
    1541       pack $w.$name -in $w.bot -side left -expand 1 -padx 3m -pady 2m
    1542 
    1543         # create the binding for the key accelerator, based on the underline
    1544         #
    1545         set underIdx [$w.$name cget -under]
    1546         if {$underIdx >= 0} {
    1547             set key [string index [$w.$name cget -text] $underIdx]
    1548           bind $w <Alt-[string tolower $key]>  [list $w.$name invoke]
    1549           bind $w <Alt-[string toupper $key]>  [list $w.$name invoke]
    1550         }
    1551         incr i
    1552     }
    1553 
    1554     # 6. Create a binding for <Return> on the dialog if there is a
    1555     # default button.
    1556 
    1557     if {[string compare $data(-default) ""]} {
    1558       bind $w <Return> [list tkButtonInvoke $w.[string tolower $data(-default)]]
    1559     }
    1560 
    1561     # 7. Withdraw the window, then update all the geometry information
    1562     # so we know how big it wants to be, then center the window in the
    1563     # display and de-iconify it.
    1564 
    1565     wm withdraw $w
    1566     update idletasks
    1567     set wp $data(-parent)
    1568     # center the new window in the middle of the parent
    1569     set x [expr [winfo x $wp] + [winfo width $wp]/2 - \
    1570             [winfo reqwidth $w]/2 - [winfo vrootx $wp]]
    1571     set y [expr [winfo y $wp] + [winfo height $wp]/2 - \
    1572             [winfo reqheight $w]/2 - [winfo vrooty $wp]]
    1573     # make sure that we can see the entire window
    1574     set xborder 10
    1575     set yborder 25
    1576     if {$x < 0} {set x 0}
    1577     if {$x+[winfo reqwidth $w] +$xborder > [winfo screenwidth $w]} {
    1578         incr x [expr \
    1579                 [winfo screenwidth $w] - ($x+[winfo reqwidth $w] + $xborder)]
    1580     }
    1581     if {$y < 0} {set y 0}
    1582     if {$y+[winfo reqheight $w] +$yborder > [winfo screenheight $w]} {
    1583         incr y [expr \
    1584                 [winfo screenheight $w] - ($y+[winfo reqheight $w] + $yborder)]
    1585     }
    1586     wm geom $w +$x+$y
    1587     wm deiconify $w
    1588 
    1589     # 8. Set a grab and claim the focus too.
    1590 
    1591     catch {set oldFocus [focus]}
    1592     catch {set oldGrab [grab current $w]}
    1593     catch {
    1594         grab $w
    1595         if {[string compare $data(-default) ""]} {
    1596             focus $w.[string tolower $data(-default)]
    1597         } else {
    1598             focus $w
    1599         }
    1600     }
    1601 
    1602     # 9. Wait for the user to respond, then restore the focus and
    1603     # return the index of the selected button.  Restore the focus
    1604     # before deleting the window, since otherwise the window manager
    1605     # may take the focus away so we can't redirect it.  Finally,
    1606     # restore any grab that was in effect.
    1607 
    1608     tkwait variable tkPriv(button)
    1609     catch {focus $oldFocus}
    1610     destroy $w
    1611     catch {grab $oldGrab}
    1612     return $tkPriv(button)
    1613 }
    1614 
    1615 #------------------------------------------------------------------------------
    16161191# Delete History Records
    16171192proc DeleteHistoryRecords {{msg ""}} {
     
    16541229    afterputontop
    16551230}
     1231
     1232#------------------------------------------------------------------------------
     1233# GSAS interface routines
     1234#------------------------------------------------------------------------------
     1235# run a GSAS program that does not require an experiment file
     1236proc runGSASprog {proglist} {
     1237    global expgui tcl_platform
     1238    set cmd {}
     1239    foreach prog $proglist {
     1240        if {$tcl_platform(platform) == "windows"} {
     1241            append cmd " \"$expgui(gsasexe)/${prog}.exe \" "
     1242        } else {
     1243            if {$cmd != ""} {append cmd "\;"}
     1244            append cmd "[file join $expgui(gsasexe) $prog]"
     1245        }
     1246    }
     1247    forknewterm $prog $cmd 0 1 1
     1248}
     1249
     1250# run a GSAS program that requires an experiment file for input/output
     1251proc runGSASwEXP {proglist "concurrent 0"} {
     1252    global expgui tcl_platform
     1253    # Save the current exp file
     1254    savearchiveexp
     1255    # load the changed .EXP file automatically?
     1256    if {$expgui(autoexpload)} {
     1257        # disable the file changed monitor
     1258        set expgui(expModifiedLast) 0
     1259    }
     1260    set cmd {}
     1261    set expnam [file root [file tail $expgui(expfile)]]
     1262    foreach prog $proglist {
     1263        if {$prog == "expedt" && $expgui(archive)} archiveexp
     1264        if {$tcl_platform(platform) == "windows"} {
     1265            append cmd " \"$expgui(gsasexe)/${prog}.exe $expnam \" "
     1266        } else {
     1267            if {$cmd != ""} {append cmd "\;"}
     1268            append cmd "[file join $expgui(gsasexe) $prog] $expnam"
     1269        }
     1270    }
     1271    forknewterm "$prog -- $expnam" $cmd $concurrent 1 1
     1272    # load the changed .EXP file automatically?
     1273    if {$expgui(autoexpload)} {
     1274        # load the revised exp file
     1275        loadexp $expgui(expfile)
     1276    }
     1277}
     1278
     1279# write text to the .LST file
     1280proc writelst {text} {
     1281    global expgui
     1282    set lstnam [file rootname $expgui(expfile)].LST
     1283    set fp [open $lstnam a]
     1284    puts $fp "\n-----------------------------------------------------------------"
     1285    puts $fp $text
     1286    puts $fp "-----------------------------------------------------------------\n"
     1287    close $fp
     1288}
     1289
    16561290
    16571291# optionally run disagl as a windowless process, w/results in a separate window
     
    17391373    }
    17401374}
    1741 # tell'em what is happening
    1742 proc pleasewait {{message {}}} {
    1743     catch {destroy .msg}
    1744     toplevel .msg
    1745     wm transient .msg [winfo toplevel .]
    1746     pack [frame .msg.f -bd 4 -relief groove]
    1747     pack [message .msg.f.m -text "Please wait $message"]
    1748     wm withdraw .msg
    1749     update idletasks
    1750     # place the message on top of the main window
    1751     set x [expr [winfo x .] + [winfo width .]/2 - \
    1752             [winfo reqwidth .msg]/2 - [winfo vrootx .]]
    1753     if {$x < 0} {set x 0}
    1754     set y [expr [winfo y .] + [winfo height .]/2 - \
    1755             [winfo reqheight .msg]/2 - [winfo vrooty .]]
    1756     if {$y < 0} {set y 0}
    1757     wm geom .msg +$x+$y
    1758     wm deiconify .msg
    1759     global makenew
    1760     set makenew(OldGrab) ""
    1761     #catch {set makenew(OldFocus) [focus]}
    1762     catch {set makenew(OldGrab) [grab current .msg]}
    1763     catch {grab .msg}
    1764     catch {focus .msg}
     1375
     1376#------------------------------------------------------------------------------
     1377# file conversion for UNIX
     1378#------------------------------------------------------------------------------
     1379# convert a file
     1380proc convfile {} {
     1381    global tcl_platform
     1382    if {$tcl_platform(platform) == "windows"} {
     1383        convwin
     1384    } else {
     1385        convunix
     1386    }
     1387}
     1388
     1389# file conversions for UNIX (convstod convdtos)
     1390proc convunix {} {
     1391    global expgui infile outfile
     1392    set frm .file
     1393    catch {destroy $frm}
     1394    toplevel $frm
     1395    wm title $frm "Convert File"
     1396    bind $frm <Key-F1> "MakeWWWHelp expgui.html ConvertUnix"
     1397
     1398    pack [frame [set frm0 $frm.0] -bd 2 -relief groove] \
     1399            -padx 3 -pady 3 -side top -fill x
     1400    pack [frame $frm.mid] -side top
     1401    pack [frame [set frmA $frm.mid.1] -bd 2 -relief groove] \
     1402            -padx 3 -pady 3 -side left
     1403    pack [label $frmA.0 -text "Select an input file"] -side top -anchor center
     1404    pack [frame [set frmB $frm.mid.2] -bd 2 -relief groove] \
     1405            -padx 3 -pady 3 -side left
     1406    pack [label $frmB.0 -text "Enter an output file"] -side top -anchor center
     1407    pack [frame [set frmC $frm.3] ] -padx 3 -pady 3 -side top -fill x -expand y
     1408
     1409    pack [label $frm0.1 -text "Convert to:"] -side top -anchor center
     1410    pack [frame $frm0.2] -side top -anchor center
     1411    pack [radiobutton $frm0.2.d -text "direct access" -value convstod \
     1412            -command setoutfile \
     1413            -variable outfile(type)] -side left -anchor center
     1414    pack [radiobutton $frm0.2.s -text "sequential" -value convdtos \
     1415            -command setoutfile \
     1416            -variable outfile(type)] -side right -anchor center
     1417    set outfile(type) ""
     1418
     1419    pack [button $frmC.b -text Convert -command "valid_conv_unix"] -side left
     1420    pack [button $frmC.q -text Quit -command "set infile(done) 1"] -side left
     1421    pack [button $frmC.help -text Help -bg yellow \
     1422            -command "MakeWWWHelp expgui.html ConvertUnix"] \
     1423            -side right
     1424   
     1425    unixcnvbox $frmA infile 1
     1426    unixcnvbox $frmB outfile 0
     1427    set infile(done) 0
     1428    bind $frm <Return> "valid_conv_unix"
     1429    # force the window to stay on top
     1430    putontop $frm
     1431    focus $frmC.q
    17651432    update
    1766 }
    1767 # clear the message
    1768 proc donewait {} {
    1769     global makenew
    1770     catch {focus $makenew(OldFocus)}
    1771     catch {destroy .msg}
    1772     catch {
    1773         if {$makenew(OldGrab) != ""} {
    1774             grab $makenew(OldGrab)
    1775         }
    1776     }
    1777 }
    1778 
    1779 
    1780 # profile terms
    1781 array set expgui {
    1782     prof-T-1 {alp-0 alp-1 bet-0 bet-1 sig-0 sig-1 sig-2 rstr rsta \
    1783             rsca s1ec s2ec }
    1784     prof-T-2 {alp-0 alp-1 beta switch sig-0 sig-1 sig-2 gam-0 gam-1 \
    1785             gam-2 ptec stec difc difa zero }
    1786     prof-T-3 {alp bet-0 bet-1 sig-0 sig-1 sig-2 gam-0 gam-1 \
    1787             gam-2 gsf g1ec g2ec rstr rsta rsca L11 L22 L33 L12 L13 L23 }
    1788     prof-T-4 {alp bet-0 bet-1 sig-1 sig-2 gam-2 g2ec gsf \
    1789             rstr rsta rsca eta}
    1790     prof-C-1 {GU GV GW asym F1 F2 }
    1791     prof-C-2 {GU GV GW LX LY trns asym shft GP stec ptec sfec \
    1792             L11 L22 L33 L12 L13 L23 }
    1793     prof-C-3 {GU GV GW GP LX LY S/L H/L trns shft stec ptec sfec \
    1794             L11 L22 L33 L12 L13 L23 }
    1795     prof-C-4 {GU GV GW GP LX ptec trns shft sfec S/L H/L eta}
    1796     prof-E-1 {A B C ds cds}
    1797 }
    1798 
    1799 # number of profile terms depends on the histogram type
    1800 # the LAUE symmetry and the profile number
    1801 proc GetProfileTerms {phase hist ptype} {
    1802     global expmap expgui
    1803     if {$hist == "C" || $hist == "T" || $hist == "E"} {
    1804         set htype $hist
    1805     } else {
    1806         set htype [string range $expmap(htype_$hist) 2 2]
    1807     }
    1808     # get the cached copy of the profile term labels, when possible
    1809     catch {
    1810         set lbls $expmap(ProfileTerms${phase}_${ptype}_${htype})
     1433    tkwait variable infile(done)
     1434    destroy $frm
     1435    afterputontop
     1436}
     1437
     1438# validate the files and make the conversion -- unix
     1439proc valid_conv_unix {} {
     1440    global infile outfile expgui
     1441    set error {}
     1442    if {$outfile(type) == "convstod" || $outfile(type) == "convdtos"} {
     1443        set convtype $outfile(type)
     1444    } else {
     1445        append error "You must specify a conversion method: to direct access or to sequential.\n"
     1446    }
     1447    if {$infile(name) == ""} {
     1448        append error "You must specify an input file to convert.\n"
     1449    }
     1450    if {$outfile(name) == ""} {
     1451        append error "You must specify an output file name for the converted file.\n"
     1452    }
     1453    if {$error != ""} {
     1454        tk_dialog .warn Notify $error warning 0 OK
    18111455        return
    18121456    }
    1813     set lbls {}
    1814     catch {set lbls $expgui(prof-$htype-$ptype)}
    1815     if {$lbls == ""} {return}
    1816     # add terms based on the Laue symmetry
    1817     if {($htype == "C" || $htype == "T") && $ptype == 4} {
    1818         set laueaxis [GetLaue [phaseinfo $phase spacegroup]]
    1819         eval lappend lbls [Profile4Terms $laueaxis]
    1820     }
    1821     set expmap(ProfileTerms${phase}_${ptype}_${htype}) $lbls
    1822     return $lbls
    1823 }
    1824 
    1825 proc Profile4Terms {laueaxis} {
    1826     switch -exact $laueaxis {
    1827         1bar {return \
    1828                 "S400 S040 S004 S220 S202 S022 S310 S103 S031 \
    1829                 S130 S301 S013 S211 S121 S112"}
    1830         2/ma {return "S400 S040 S004 S220 S202 S022 S013 S031 S211"}
    1831         2/mb {return "S400 S040 S004 S220 S202 S022 S301 S103 S121"}
    1832         2/mc {return "S400 S040 S004 S220 S202 S022 S130 S310 S112"}
    1833         mmm  {return "S400 S040 S004 S220 S202 S022"}
    1834         4/m  {return "S400 S004 S220 S202"}
    1835         4/mmm {return "S400 S004 S220 S202"}
    1836         3barR     {return "S400 S220 S310 S211"}
    1837         "3bar mR" {return "S400 S220 S310 S211"}
    1838         3bar    {return "S400 S004 S202 S211"}
    1839         3barm1 {return "S400 S004 S202"}
    1840         3bar1m  {return "S400 S004 S202 S211"}
    1841         6/m    {return "S400 S004 S202"}
    1842         6/mmm  {return "S400 S004 S202"}
    1843         "m 3"  {return "S400 S220"}
    1844         m3m    {return "S400 S220"}
    1845         default {return ""}
    1846     }
    1847 }
    1848 
    1849 proc GetLaue {spg} {
    1850     global tcl_platform expgui
    1851     # check the space group
    1852     set fp [open spg.in w]
    1853     puts $fp "N"
    1854     puts $fp "N"
    1855     puts $fp $spg
    1856     puts $fp "Q"
    1857     close $fp
    1858     catch {
     1457
     1458    if {$infile(name) == $outfile(name)} {
     1459        MyMessageBox -parent . -title Notify \
     1460                -message "Sorry, filenames must differ" \
     1461                -icon warning -helplink "expguierr.html ConvSameName"
     1462        tk_dialog .warn Notify "Sorry, filenames must differ" warning 0 OK
     1463        return
     1464    }
     1465    if ![file exists [file join $infile(dir) $infile(name)]] {
     1466        MyMessageBox -parent . -title Notify \
     1467                -message "Sorry, file $infile(name) not found in $infile(dir)" \
     1468                -icon warning -helplink "expguierr.html ConvNotFound"
     1469        return
     1470    }
     1471    if [file exists [file join $outfile(dir) $outfile(name)]] {
     1472        set ans [MyMessageBox -parent . -title "Overwrite?" \
     1473                -message "Warning: file $outfile(name) exists in $outfile(dir). OK to overwrite?" \
     1474                -icon warning -type {Overwrite Cancel} -default Overwrite \
     1475                -helplink "expguierr.html OverwriteCnv"]
     1476        if {[string tolower $ans] == "cancel"} return
     1477    }
     1478    if [catch {
     1479        exec [file join $expgui(gsasexe) $convtype] < \
     1480                [file join $infile(dir) $infile(name)] > \
     1481                [file join $outfile(dir) $outfile(name)]
     1482    } errmsg] {
     1483        tk_dialog .warn Notify "Error in conversion:\n$errmsg" warning 0 OK
     1484    } else {
     1485        if [tk_dialog .converted Notify \
     1486                "File converted. Convert more files?" \
     1487                ""  0 Yes No] {set infile(done) 1}
     1488    }
     1489}
     1490
     1491# create a file box for UNIX conversions
     1492proc unixcnvbox {bx filvar diropt} {
     1493    global ${filvar} expgui
     1494    pack [frame $bx.top] -side top
     1495    pack [label $bx.top.a -text "Directory" ] -side left
     1496    set ${filvar}(FileDirButtonMenu) [tk_optionMenu $bx.top.d ${filvar}(dir) [pwd] ]
     1497    pack $bx.top.d -side left
     1498    set ${filvar}(dir) [pwd]
     1499
     1500#    pack [label $bx.d -textvariable ${filvar}(dir) -bd 2 -relief raised ] -side top
     1501#    set ${filvar}(dir) [pwd]
     1502
     1503    pack [frame $bx.a -width 200 -height 75] -side top -expand yes -fill both
     1504    listbox $bx.a.files -relief raised -bd 2 \
     1505            -yscrollcommand "$bx.a.scroll set" \
     1506            -height 15 -width 0 -exportselection 0
     1507    scrollbar $bx.a.scroll -command "$bx.a.files yview"
     1508    unixFilChoose $bx $bx.a.files $filvar $diropt
     1509    if {$filvar == "infile"} {
     1510        bind $bx.a.files <ButtonRelease-1> \
     1511                "unixFilChoose $bx $bx.a.files $filvar $diropt; setoutfile"
     1512    } else {
     1513        bind $bx.a.files <ButtonRelease-1> \
     1514                "unixFilChoose $bx $bx.a.files $filvar $diropt"
     1515    }
     1516    pack $bx.a.scroll -side left -fill y
     1517    pack $bx.a.files -side left -fill both -expand yes
     1518    pack [entry $bx.c -textvariable ${filvar}(name)] -side top
     1519}
     1520
     1521# select a file or directory, also called when box is created to fill it
     1522proc unixFilChoose {frm box filvar {dironly 1}} {
     1523    global $filvar
     1524    set select [$box curselection]
     1525    if {$select == ""} {
     1526        set file .
     1527    } else {
     1528        set file [string trim [$box get $select]]
     1529    }
     1530    if [file isdirectory [file join [set ${filvar}(dir)] $file]] {
     1531        if {$file == ".."} {
     1532            set ${filvar}(dir) [file dirname [set ${filvar}(dir)] ]
     1533        } elseif {$file != "."} {
     1534            set ${filvar}(dir) [file join [set ${filvar}(dir)] $file]
     1535        }
     1536        [set ${filvar}(FileDirButtonMenu)] delete 0 end
     1537        set list ""
     1538        set dir ""
     1539        foreach subdir [file split [set ${filvar}(dir)]] {
     1540            set dir [file join $dir $subdir]
     1541            lappend list $dir
     1542        }
     1543        foreach path $list {
     1544            [set ${filvar}(FileDirButtonMenu)] add command -label $path \
     1545                -command "[list set ${filvar}(dir) $path]; \
     1546                unixFilChoose $frm $box $filvar $dironly"
     1547        }
     1548        set ${filvar}(name) {}
     1549        $box delete 0 end
     1550        $box insert end {..   }
     1551        foreach file [lsort -dictionary [glob -nocomplain \
     1552                [file join [set ${filvar}(dir)] *] ] ] {
     1553            if {[file isdirectory $file]} {
     1554                # is this / needed here? Does it cause a problem in MacGSAS?
     1555                $box insert end [file tail $file]/
     1556            } elseif {$dironly == 1} {
     1557                $box insert end [file tail $file]
     1558            } elseif {$dironly == 2 && [file extension $file] == ".EXP"} {
     1559                $box insert end [file tail $file]
     1560            }
     1561        }
     1562        return
     1563    }
     1564    set ${filvar}(name) [file tail $file]
     1565}
     1566
     1567# set new file name from old -- used for convunix
     1568proc setoutfile {} {
     1569    global infile outfile
     1570    if {$outfile(type) == "convstod"} {
     1571        set lfile [string toupper $infile(name)]
     1572    } elseif {$outfile(type) == "convdtos"} {
     1573        set lfile [string tolower $infile(name)]
     1574    } else {
     1575        set lfile ""
     1576    }
     1577    if {$infile(name) == $lfile} {
     1578        set outfile(name) {}
     1579    } else {
     1580        set outfile(name) $lfile
     1581    }
     1582}
     1583
     1584#------------------------------------------------------------------------------
     1585# file conversions for Windows
     1586#------------------------------------------------------------------------------
     1587proc convwin {} {
     1588    global expgui
     1589    set frm .file
     1590    catch {destroy $frm}
     1591    toplevel $frm
     1592    wm title $frm "Convert File"
     1593    bind $frm <Key-F1> "MakeWWWHelp expgui.html ConvertWin"
     1594    pack [frame [set frmA $frm.1] -bd 2 -relief groove] -padx 3 -pady 3 -side left
     1595    pack [frame [set frmC $frm.3] ] -padx 3 -pady 3 \
     1596            -side left -fill y -expand yes
     1597    pack [button $frmC.help -text Help -bg yellow \
     1598            -command "MakeWWWHelp expgui.html ConvertWin"] -side top
     1599    pack [button $frmC.q -text Quit -command "destroy $frm"] -side bottom
     1600    pack [button $frmC.b -text Convert -command "ValidWinCnv $frm"] \
     1601            -side bottom
     1602    pack [label $frmA.0 -text "Select a file to convert"] -side top -anchor center
     1603    winfilebox $frm
     1604    bind $frm <Return> "ValidWinCnv $frm"
     1605
     1606    # force the window to stay on top
     1607    putontop $frm
     1608    focus $frmC.q
     1609    tkwait window $frm
     1610    afterputontop
     1611}
     1612
     1613# validate the files and make the conversion
     1614proc ValidWinCnv {frm} {
     1615    global expgui
     1616    # change backslashes to something sensible
     1617    regsub -all {\\} $expgui(FileMenuCnvName) / expgui(FileMenuCnvName)
     1618    # allow entry of D: for D:/ and D:TEST for d:/TEST
     1619    if {[string first : $expgui(FileMenuCnvName)] != -1 && \
     1620            [string first :/ $expgui(FileMenuCnvName)] == -1} {
     1621        regsub : $expgui(FileMenuCnvName) :/ expgui(FileMenuCnvName)
     1622    }
     1623    if {$expgui(FileMenuCnvName) == "<Parent>"} {
     1624        set expgui(FileMenuDir) [file dirname [set expgui(FileMenuDir)] ]
     1625        ChooseWinCnv $frm
     1626        return
     1627    } elseif [file isdirectory \
     1628            [file join $expgui(FileMenuDir) $expgui(FileMenuCnvName)]] {
     1629        if {$expgui(FileMenuCnvName) != "."} {
     1630            set expgui(FileMenuDir) \
     1631                [file join $expgui(FileMenuDir) $expgui(FileMenuCnvName)]
     1632        }
     1633        ChooseWinCnv $frm
     1634        return
     1635    }
     1636 
     1637    set file [file join $expgui(FileMenuDir) $expgui(FileMenuCnvName)]
     1638    if ![file exists $file] {
     1639        tk_dialog .warn "Convert Error" \
     1640                "File $file does not exist" question 0 "OK"
     1641        return
     1642    }
     1643
     1644    set tmpname "[file join [file dirname $file] tempfile.xxx]"
     1645    set oldname "[file rootname $file].org"
     1646    if [file exists $oldname] {
     1647        set ans [MyMessageBox -parent . -title "Overwrite?" \
     1648                -message "File [file tail $oldname] exists in [file dirname $oldname]. OK to overwrite?" \
     1649                -icon warning -type {Overwrite Cancel} -default Overwrite \
     1650                -helplink "expguierr.html OverwriteCnv"]
     1651        if {[string tolower $ans] == "cancel"} return
     1652        catch {file delete $oldname}
     1653    }
     1654
     1655    if [catch {
     1656        set in [open $file r]
     1657        set out [open $tmpname w]
     1658        set len [gets $in line]
     1659        if {$len > 160} {
     1660            # this is a UNIX file. Hope there are no control characters
     1661            set i 0
     1662            set j 79
     1663            while {$j < $len} {
     1664                puts $out [string range $line $i $j]
     1665                incr i 80
     1666                incr j 80
     1667            }
     1668        } else {
     1669            while {$len >= 0} {
     1670                append line "                                        "
     1671                append line "                                        "
     1672                set line [string range $line 0 79]
     1673                puts $out $line
     1674                set len [gets $in line]
     1675            }
     1676        }
     1677        close $in
     1678        close $out
     1679        file rename -force $file $oldname
     1680        file rename -force $tmpname $file
     1681    } errmsg] {
     1682        tk_dialog .warn Notify "Error in conversion:\n$errmsg" warning 0 OK
     1683    } else {
     1684        if [tk_dialog .converted Notify \
     1685                "File [file tail $file] converted. (Original saved as [file tail $oldname]).\n\n Convert more files?" \
     1686                ""  0 Yes No] {destroy $frm}
     1687    }
     1688}
     1689
     1690# create a file box
     1691proc winfilebox {frm} {
     1692    global expgui
     1693    set bx $frm.1
     1694    pack [frame $bx.top] -side top
     1695    pack [label $bx.top.a -text "Directory" ] -side left
     1696    set expgui(FileDirButtonMenu) [tk_optionMenu $bx.top.d expgui(FileMenuDir) [pwd] ]
     1697    pack $bx.top.d -side left
     1698    set expgui(FileMenuDir) [pwd]
     1699    # the icon below is from tk8.0/tkfbox.tcl
     1700    set upfolder [image create bitmap -data {
     1701#define updir_width 28
     1702#define updir_height 16
     1703static char updir_bits[] = {
     1704   0x00, 0x00, 0x00, 0x00, 0x80, 0x1f, 0x00, 0x00, 0x40, 0x20, 0x00, 0x00,
     1705   0x20, 0x40, 0x00, 0x00, 0xf0, 0xff, 0xff, 0x01, 0x10, 0x00, 0x00, 0x01,
     1706   0x10, 0x02, 0x00, 0x01, 0x10, 0x07, 0x00, 0x01, 0x90, 0x0f, 0x00, 0x01,
     1707   0x10, 0x02, 0x00, 0x01, 0x10, 0x02, 0x00, 0x01, 0x10, 0x02, 0x00, 0x01,
     1708   0x10, 0xfe, 0x07, 0x01, 0x10, 0x00, 0x00, 0x01, 0x10, 0x00, 0x00, 0x01,
     1709   0xf0, 0xff, 0xff, 0x01};}]
     1710
     1711    pack [button $bx.top.b -image $upfolder \
     1712            -command "updir; ChooseWinCnv $frm" ]
     1713    pack [frame $bx.a -width 200 -height 75] -side top -expand yes -fill both
     1714    listbox $bx.a.files -relief raised -bd 2 \
     1715            -yscrollcommand "sync2boxes $bx.a.files $bx.a.dates $bx.a.scroll" \
     1716            -height 15 -width 0 -exportselection 0
     1717    listbox $bx.a.dates -relief raised -bd 2 \
     1718            -yscrollcommand "sync2boxes $bx.a.dates $bx.a.files $bx.a.scroll" \
     1719            -height 15 -width 0 -takefocus 0 -exportselection 0
     1720    scrollbar $bx.a.scroll -command "move2boxesY \" $bx.a.files $bx.a.dates \" "
     1721    ChooseWinCnv $frm
     1722    bind $bx.a.files <ButtonRelease-1> "ReleaseWinCnv $frm"
     1723    bind $bx.a.dates <ButtonRelease-1> "ReleaseWinCnv $frm"
     1724    bind $bx.a.files <Double-1> "SelectWinCnv $frm"
     1725    bind $bx.a.dates <Double-1> "SelectWinCnv $frm"
     1726    pack $bx.a.scroll -side left -fill y
     1727    pack $bx.a.files $bx.a.dates -side left -fill both -expand yes
     1728    pack [entry $bx.c -textvariable expgui(FileMenuCnvName)] -side top
     1729}
     1730
     1731# set the box or file in the selection window
     1732proc ReleaseWinCnv {frm} {
     1733    global expgui
     1734    set files $frm.1.a.files
     1735    set dates $frm.1.a.dates
     1736    set select [$files curselection]
     1737    if {$select == ""} {
     1738        set select [$dates curselection]
     1739    }
     1740    if {$select == ""} {
     1741        set expgui(FileMenuCnvName) ""
     1742    } else {
     1743        set expgui(FileMenuCnvName) [string trim [$files get $select]]
     1744    }
     1745    if {$expgui(FileMenuCnvName) == "<Parent>"} {
     1746        set expgui(FileMenuDir) [file dirname $expgui(FileMenuDir)]
     1747        ChooseWinCnv $frm
     1748    } elseif [file isdirectory \
     1749            [file join [set expgui(FileMenuDir)] $expgui(FileMenuCnvName)]] {
     1750        if {$expgui(FileMenuCnvName) != "."} {
     1751            set expgui(FileMenuDir) [file join $expgui(FileMenuDir) $expgui(FileMenuCnvName)]
     1752            ChooseWinCnv $frm
     1753        }
     1754    }
     1755    return
     1756}
     1757
     1758# select a file or directory -- called on double click
     1759proc SelectWinCnv {frm} {
     1760    global expgui
     1761    set files $frm.1.a.files
     1762    set dates $frm.1.a.dates
     1763    set select [$files curselection]
     1764    if {$select == ""} {
     1765        set select [$dates curselection]
     1766    }
     1767    if {$select == ""} {
     1768        set file .
     1769    } else {
     1770        set file [string trim [$files get $select]]
     1771    }
     1772    if {$file == "<Parent>"} {
     1773        set expgui(FileMenuDir) [file dirname [set expgui(FileMenuDir)] ]
     1774        ChooseWinCnv $frm
     1775    } elseif [file isdirectory [file join [set expgui(FileMenuDir)] $file]] {
     1776        if {$file != "."} {
     1777            set expgui(FileMenuDir) [file join [set expgui(FileMenuDir)] $file]
     1778            ChooseWinCnv $frm
     1779        }
     1780    } else {
     1781        set expgui(FileMenuCnvName) [file tail $file]
     1782        ValidWinCnv $frm
     1783    }
     1784}
     1785
     1786# fill the files & dates & Directory selection box with current directory,
     1787# also called when box is created to fill it
     1788proc ChooseWinCnv {frm} {
     1789    global expgui
     1790    set files $frm.1.a.files
     1791    set dates $frm.1.a.dates
     1792    set expgui(FileMenuCnvName) {}
     1793    $files delete 0 end
     1794    $dates delete 0 end
     1795    $files insert end {<Parent>}
     1796    $dates insert end {(Directory)}
     1797    set filelist [glob -nocomplain \
     1798            [file join [set expgui(FileMenuDir)] *] ]
     1799    foreach file [lsort -dictionary $filelist] {
     1800        if {[file isdirectory $file]} {
     1801            $files insert end [file tail $file]
     1802            $dates insert end {(Directory)}
     1803        }
     1804    }
     1805    foreach file [lsort -dictionary $filelist] {
     1806        if {![file isdirectory $file]} {
     1807            set modified [clock format [file mtime $file] -format "%T %D"]
     1808            $files insert end [file tail $file]
     1809            $dates insert end $modified
     1810        }
     1811    }
     1812    $expgui(FileDirButtonMenu)  delete 0 end
     1813    set list ""
     1814    global tcl_version
     1815    if {$tcl_version > 8.0} {
     1816        catch {set list [string tolower [file volume]]}
     1817    }
     1818    set dir ""
     1819    foreach subdir [file split [set expgui(FileMenuDir)]] {
     1820        set dir [string tolower [file join $dir $subdir]]
     1821        if {[lsearch $list $dir] == -1} {lappend list $dir}
     1822    }
     1823    foreach path $list {
     1824        $expgui(FileDirButtonMenu) add command -label $path \
     1825                -command "[list set expgui(FileMenuDir) $path]; \
     1826                ChooseWinCnv $frm"
     1827    }
     1828    return
     1829}
     1830
     1831#------------------------------------------------------------------------------
     1832# set options for liveplot
     1833proc liveplotopt {} {
     1834    global liveplot expmap
     1835    set frm .file
     1836    catch {destroy $frm}
     1837    toplevel $frm
     1838    pack [frame [set frmA $frm.1] -bd 2 -relief groove] -padx 3 -pady 3 -side left
     1839    set last [lindex [lsort -integer $expmap(powderlist)] end]
     1840    if {$last == ""} {set last 1}
     1841    pack [scale  $frmA.1 -label "Histogram number" -from 1 -to $last \
     1842            -length  150 -orient horizontal -variable liveplot(hst)] -side top
     1843    pack [checkbutton $frmA.2 -text {include plot legend}\
     1844            -variable liveplot(legend)] -side top
     1845    pack [button $frm.2 -text OK \
     1846            -command {if ![catch {expr $liveplot(hst)}] "destroy .file"} \
     1847            ] -side top
     1848    bind $frm <Return> {if ![catch {expr $liveplot(hst)}] "destroy .file"}
     1849   
     1850    # force the window to stay on top
     1851    putontop $frm
     1852    focus $frm.2
     1853    tkwait window $frm
     1854    afterputontop
     1855}
     1856
     1857#------------------------------------------------------------------------------
     1858# get an experiment file name
     1859#------------------------------------------------------------------------------
     1860proc getExpFileName {mode} {
     1861    global expgui
     1862    set frm .file
     1863    catch {destroy $frm}
     1864    toplevel $frm
     1865    wm title $frm "Experiment file"
     1866    bind $frm <Key-F1> "MakeWWWHelp expguierr.html open"
     1867    pack [frame [set frmA $frm.1] -bd 2 -relief groove] -padx 3 -pady 3 -side left
     1868    pack [frame [set frmC $frm.3] ] -padx 3 -pady 3 -side left \
     1869            -fill y -expand yes
     1870    pack [button $frmC.help -text Help -bg yellow \
     1871            -command "MakeWWWHelp expguierr.html open"] \
     1872            -side top -anchor e
     1873    pack [label $frmC.2 -text "Sort .EXP files by" ] -side top
     1874    pack [radiobutton $frmC.1 -text "File Name" -value 1 \
     1875            -variable expgui(filesort) -command "ChooseExpFil $frmA"] -side top
     1876    pack [radiobutton $frmC.0 -text "Mod. Date" -value 0 \
     1877            -variable expgui(filesort) -command "ChooseExpFil $frmA"] -side top
     1878    pack [button $frmC.b -text Read \
     1879            -command "valid_exp_file $frmA $mode"] -side bottom
     1880    if {$mode == "new"} {
     1881        $frmC.b config -text Save
     1882    }
     1883    pack [button $frmC.q -text Quit \
     1884            -command "set expgui(FileMenuEXPNAM) {}; destroy $frm"] -side bottom
     1885    bind $frm <Return> "$frmC.b invoke"
     1886
     1887    if {$mode == "new"} {
     1888        pack [label $frmA.0 -text "Enter an experiment file to create"] \
     1889                -side top -anchor center
     1890    } else {
     1891        pack [label $frmA.0 -text "Select an experiment file to read"] \
     1892                -side top -anchor center
     1893    }
     1894    expfilebox $frmA $mode
     1895    # force the window to stay on top
     1896    putontop $frm
     1897    focus $frmC.b
     1898    tkwait window $frm
     1899    afterputontop
     1900    if {$expgui(FileMenuEXPNAM) == ""} return
     1901    return [file join $expgui(FileMenuDir) $expgui(FileMenuEXPNAM)]
     1902}
     1903
     1904# validation routine
     1905proc valid_exp_file {frm mode} {
     1906    global expgui tcl_platform
     1907    # windows fixes
     1908    if {$tcl_platform(platform) == "windows"} {
     1909        # change backslashes to something sensible
     1910        regsub -all {\\} $expgui(FileMenuEXPNAM) / expgui(FileMenuEXPNAM)
     1911        # allow entry of D: for D:/ and D:TEST for d:/TEST
     1912        if {[string first : $expgui(FileMenuEXPNAM)] != -1 && \
     1913                [string first :/ $expgui(FileMenuEXPNAM)] == -1} {
     1914            regsub : $expgui(FileMenuEXPNAM) :/ expgui(FileMenuEXPNAM)
     1915        }
     1916    }
     1917    if {$expgui(FileMenuEXPNAM) == "<Parent>"} {
     1918        set expgui(FileMenuDir) [file dirname [set expgui(FileMenuDir)] ]
     1919        ChooseExpFil $frm
     1920        return
     1921    } elseif [file isdirectory \
     1922            [file join $expgui(FileMenuDir) $expgui(FileMenuEXPNAM)]] {
     1923        if {$expgui(FileMenuEXPNAM) != "."} {
     1924            set expgui(FileMenuDir) \
     1925                [file join $expgui(FileMenuDir) $expgui(FileMenuEXPNAM)]
     1926        }
     1927        ChooseExpFil $frm
     1928        return
     1929    }
     1930    # append a .EXP if not present
     1931    if {[file extension $expgui(FileMenuEXPNAM)] == ""} {
     1932        append expgui(FileMenuEXPNAM) ".EXP"
     1933    }
     1934    # flag files that end in something other than .EXP .exp or .Exp...
     1935    if {[string toupper [file extension $expgui(FileMenuEXPNAM)]] != ".EXP"} {
     1936        tk_dialog .expFileErrorMsg "File Open Error" \
     1937            "File [file tail $expgui(FileMenuEXPNAM)] is not a valid name. Experiment files must end in \".EXP\"" \
     1938            error 0 OK
     1939        return
     1940    }
     1941    # check on the file status
     1942    set file [file join $expgui(FileMenuDir) $expgui(FileMenuEXPNAM)]
     1943    if {$mode == "new" && [file exists $file]} {
     1944        set ans [
     1945        MyMessageBox -parent . -title "File Open Error" \
     1946                -message "File [file tail $file] already exists in [file dirname $file]. OK to overwrite?" \
     1947                -icon question -type {"Select other" "Overwrite"} -default "select other" \
     1948                -helplink "expguierr.html OverwriteErr"
     1949        ]
     1950        if {[string tolower $ans] == "overwrite"} {destroy .file}
     1951        if $ans {destroy .file}
     1952        return
     1953    }
     1954    # if file does not exist in case provided, set the name to all
     1955    # upper case letters, since that is the best choice.
     1956    # if it does exist, read from it as is. For UNIX we will force uppercase later.
     1957    if {![file exists $file]} {
     1958        set expgui(FileMenuEXPNAM) [string toupper $expgui(FileMenuEXPNAM)]
     1959        set file [file join $expgui(FileMenuDir) $expgui(FileMenuEXPNAM)]
     1960    }
     1961    if {$mode == "old" && ![file exists $file]} {
     1962        set ans [
     1963        MyMessageBox -parent . -title "File Open Error" \
     1964                -message "File [file tail $file] does not exist in [file dirname $file]. OK to create?" \
     1965                -icon question -type {"Select other" "Create"} -default "select other" \
     1966                -helplink "expguierr.html OpenErr"
     1967        ]
     1968        if {[string tolower $ans] == "create"} {destroy .file}
     1969        return
     1970    }
     1971    destroy .file
     1972}
     1973
     1974proc updir {} {
     1975    global expgui
     1976    set expgui(FileMenuDir) [file dirname [set expgui(FileMenuDir)]]
     1977}
     1978
     1979# create a file box
     1980proc expfilebox {bx mode} {
     1981    global expgui
     1982    pack [frame $bx.top] -side top
     1983    pack [label $bx.top.a -text "Directory" ] -side left
     1984    set expgui(FileDirButtonMenu) [tk_optionMenu $bx.top.d expgui(FileMenuDir) [pwd] ]
     1985    pack $bx.top.d -side left
     1986    set expgui(FileMenuDir) [pwd]
     1987    # the icon below is from tk8.0/tkfbox.tcl
     1988    set upfolder [image create bitmap -data {
     1989#define updir_width 28
     1990#define updir_height 16
     1991static char updir_bits[] = {
     1992   0x00, 0x00, 0x00, 0x00, 0x80, 0x1f, 0x00, 0x00, 0x40, 0x20, 0x00, 0x00,
     1993   0x20, 0x40, 0x00, 0x00, 0xf0, 0xff, 0xff, 0x01, 0x10, 0x00, 0x00, 0x01,
     1994   0x10, 0x02, 0x00, 0x01, 0x10, 0x07, 0x00, 0x01, 0x90, 0x0f, 0x00, 0x01,
     1995   0x10, 0x02, 0x00, 0x01, 0x10, 0x02, 0x00, 0x01, 0x10, 0x02, 0x00, 0x01,
     1996   0x10, 0xfe, 0x07, 0x01, 0x10, 0x00, 0x00, 0x01, 0x10, 0x00, 0x00, 0x01,
     1997   0xf0, 0xff, 0xff, 0x01};}]
     1998
     1999    pack [button $bx.top.b -image $upfolder \
     2000            -command "updir; ChooseExpFil $bx" ]
     2001    pack [frame $bx.a -width 200 -height 75] -side top -expand yes -fill both
     2002    listbox $bx.a.files -relief raised -bd 2 \
     2003            -yscrollcommand "sync2boxes $bx.a.files $bx.a.dates $bx.a.scroll" \
     2004            -height 15 -width 0 -exportselection 0
     2005    listbox $bx.a.dates -relief raised -bd 2 \
     2006            -yscrollcommand "sync2boxes $bx.a.dates $bx.a.files $bx.a.scroll" \
     2007            -height 15 -width 0 -takefocus 0 -exportselection 0
     2008    scrollbar $bx.a.scroll -command "move2boxesY \" $bx.a.files $bx.a.dates \" "
     2009    ChooseExpFil $bx
     2010    bind $bx.a.files <ButtonRelease-1> "ReleaseExpFil $bx"
     2011    bind $bx.a.dates <ButtonRelease-1> "ReleaseExpFil $bx"
     2012    bind $bx.a.files <Double-1> "SelectExpFil $bx $mode"
     2013    bind $bx.a.dates <Double-1> "SelectExpFil $bx $mode"
     2014    pack $bx.a.scroll -side left -fill y
     2015    pack $bx.a.files $bx.a.dates -side left -fill both -expand yes
     2016    pack [entry $bx.c -textvariable expgui(FileMenuEXPNAM)] -side top
     2017}
     2018proc sync2boxes {master slave scroll args} {
     2019    $slave yview moveto [lindex [$master yview] 0]
     2020    eval $scroll set $args
     2021}
     2022proc move2boxesY {boxlist args} {
     2023    foreach listbox $boxlist {
     2024        eval $listbox yview $args
     2025    }
     2026}
     2027
     2028# set the box or file in the selection window
     2029proc ReleaseExpFil {frm} {
     2030    global expgui
     2031    set files $frm.a.files
     2032    set dates $frm.a.dates
     2033    set select [$files curselection]
     2034    if {$select == ""} {
     2035        set select [$dates curselection]
     2036    }
     2037    if {$select == ""} {
     2038        set expgui(FileMenuEXPNAM) ""
     2039    } else {
     2040        set expgui(FileMenuEXPNAM) [string trim [$files get $select]]
     2041    }
     2042    if {$expgui(FileMenuEXPNAM) == "<Parent>"} {
     2043        set expgui(FileMenuDir) [file dirname $expgui(FileMenuDir)]
     2044        ChooseExpFil $frm
     2045    } elseif [file isdirectory \
     2046            [file join [set expgui(FileMenuDir)] $expgui(FileMenuEXPNAM)]] {
     2047        if {$expgui(FileMenuEXPNAM) != "."} {
     2048            set expgui(FileMenuDir) [file join $expgui(FileMenuDir) $expgui(FileMenuEXPNAM)]
     2049            ChooseExpFil $frm
     2050        }
     2051    }
     2052    return
     2053}
     2054
     2055# select a file or directory -- called on double click
     2056proc SelectExpFil {frm mode} {
     2057    global expgui
     2058    set files $frm.a.files
     2059    set dates $frm.a.dates
     2060    set select [$files curselection]
     2061    if {$select == ""} {
     2062        set select [$dates curselection]
     2063    }
     2064    if {$select == ""} {
     2065        set file .
     2066    } else {
     2067        set file [string trim [$files get $select]]
     2068    }
     2069    if {$file == "<Parent>"} {
     2070        set expgui(FileMenuDir) [file dirname [set expgui(FileMenuDir)] ]
     2071        ChooseExpFil $frm
     2072    } elseif [file isdirectory [file join [set expgui(FileMenuDir)] $file]] {
     2073        if {$file != "."} {
     2074            set expgui(FileMenuDir) [file join [set expgui(FileMenuDir)] $file]
     2075            ChooseExpFil $frm
     2076        }
     2077    } else {
     2078        set expgui(FileMenuEXPNAM) [file tail $file]
     2079        valid_exp_file $frm $mode
     2080    }
     2081}
     2082
     2083# fill the files & dates & Directory selection box with current directory,
     2084# also called when box is created to fill it
     2085proc ChooseExpFil {frm} {
     2086    global expgui
     2087    set files $frm.a.files
     2088    set dates $frm.a.dates
     2089    set expgui(FileMenuEXPNAM) {}
     2090    $files delete 0 end
     2091    $dates delete 0 end
     2092    $files insert end {<Parent>}
     2093    $dates insert end {(Directory)}
     2094    set filelist [glob -nocomplain \
     2095            [file join [set expgui(FileMenuDir)] *] ]
     2096    foreach file [lsort -dictionary $filelist] {
     2097        if {[file isdirectory $file]} {
     2098            $files insert end [file tail $file]
     2099            $dates insert end {(Directory)}
     2100        }
     2101    }
     2102    set pairlist {}
     2103    foreach file [lsort -dictionary $filelist] {
     2104        if {![file isdirectory $file]  && \
     2105                [string toupper [file extension $file]] == ".EXP"} {
     2106            set modified [file mtime $file]
     2107            lappend pairlist [list $file $modified]
     2108        }
     2109    }
     2110    if {$expgui(filesort) == 0} {
     2111        foreach pair [lsort -index 1 -integer $pairlist] {
     2112            set file [lindex $pair 0]
     2113            set modified [clock format [lindex $pair 1] -format "%T %D"]
     2114            $files insert end [file tail $file]
     2115            $dates insert end $modified
     2116        }
     2117    } else {
     2118        foreach pair [lsort -dictionary -index 0 $pairlist] {
     2119            set file [lindex $pair 0]
     2120            set modified [clock format [lindex $pair 1] -format "%T %D"]
     2121            $files insert end [file tail $file]
     2122            $dates insert end $modified
     2123        }
     2124    }
     2125    $expgui(FileDirButtonMenu)  delete 0 end
     2126    set list ""
     2127    global tcl_platform tcl_version
     2128    if {$tcl_platform(platform) == "windows" && $tcl_version > 8.0} {
     2129        catch {set list [string tolower [file volume]]}
     2130    }
     2131    set dir ""
     2132    foreach subdir [file split [set expgui(FileMenuDir)]] {
     2133        set dir [file join $dir $subdir]
    18592134        if {$tcl_platform(platform) == "windows"} {
    1860             exec [file join $expgui(gsasexe) spcgroup.exe] < spg.in >& spg.out
     2135            set dir [string tolower $dir]
     2136            if {[lsearch $list $dir] == -1} {lappend list $dir}
    18612137        } else {
    1862             exec [file join $expgui(gsasexe) spcgroup] < spg.in >& spg.out
    1863         }
    1864     }
    1865     set fp [open spg.out r]
    1866     set laue {}
    1867     set uniqueaxis {}
    1868     while {[gets $fp line] >= 0} {
    1869         regexp {Laue symmetry (.*)} $line junk laue
    1870         regexp {The unique axis is (.*)} $line junk uniqueaxis
    1871     }
    1872     close $fp
    1873     catch {file delete -force spg.in spg.out}
    1874     set laue [string trim $laue]
    1875     # add a R suffix for rhombohedral settings
    1876     if {[string range [string trim $spg] end end] == "R"} {
    1877         return "${laue}${uniqueaxis}R"
    1878     }
    1879     return "${laue}$uniqueaxis"
    1880 }
    1881 
    1882 
    1883 # set up to change the profile type for a series of histogram/phase entries
    1884 # (histlist & phaselist should be lists of the same length)
    1885 #
    1886 proc ChangeProfileType {histlist phaselist} {
    1887     global expgui expmap
    1888     set w .profile
    1889     catch {destroy $w}
    1890     toplevel $w -bg beige
    1891     wm title $w "Change Profile Function"
    1892    
    1893     # all histogram/phases better be the same type, so we can just use the 1st
    1894     set hist [lindex $histlist 0]
    1895     set phase [lindex $phaselist 0]
    1896     set ptype [string trim [hapinfo $hist $phase proftype]]
    1897 
    1898     # get list of allowed profile terms for the current histogram type
    1899     set i 1
    1900     while {[set lbls [GetProfileTerms $phase $hist $i]] != ""} {
    1901         lappend lbllist $lbls
    1902         incr i
    1903     }
    1904     # labels for the current type
    1905     set i $ptype
    1906     set oldlbls [lindex $lbllist [incr i -1]]
    1907    
    1908     if {[llength $histlist] == 1} {
    1909         pack [label $w.a -bg beige \
    1910                 -text "Change profile function for Histogram #$hist Phase #$phase" \
    1911                 ] -side top
    1912     } else {
    1913         # make a list of histograms by phase
    1914         foreach h $histlist p $phaselist {
    1915             lappend phlist($p) $h
    1916         }
    1917         set num 0
    1918         pack [frame $w.a -bg beige] -side top
    1919         pack [label $w.a.$num -bg beige \
    1920                 -text "Change profile function for:" \
    1921                 ] -side top -anchor w
    1922         foreach i [lsort [array names phlist]] {
    1923             incr num
    1924             pack [label $w.a.$num -bg beige -text \
    1925                     "\tPhase #$i, Histograms [CompressList $phlist($i)]" \
    1926                     ] -side top -anchor w
    1927         }
    1928     }
    1929     pack [label $w.e1 \
    1930             -text "Current function is type $ptype." \
    1931             -bg beige] -side top -anchor w
    1932     pack [frame $w.e -bg beige] -side top -expand yes -fill both
    1933     pack [label $w.e.1 \
    1934             -text "Set function to type" \
    1935             -bg beige] -side left
    1936     set menu [tk_optionMenu $w.e.2 expgui(newpeaktype) junk]
    1937     pack $w.e.2 -side left -anchor w
    1938 
    1939     pack [radiobutton $w.e.4 -bg beige -variable expgui(DefaultPeakType) \
    1940             -command "set expgui(newpeaktype) $ptype; \
    1941             FillChangeProfileType $w.c $hist $phase $ptype [list $oldlbls] [list $oldlbls]" \
    1942             -value 1 -text "Current value overrides"] -side right
    1943     pack [radiobutton $w.e.3 -bg beige -variable expgui(DefaultPeakType) \
    1944             -command \
    1945             "set expgui(newpeaktype) $ptype; \
    1946             FillChangeProfileType $w.c $hist $phase $ptype [list $oldlbls] [list $oldlbls]" \
    1947             -value 0 -text "Default value overrides"] -side right
    1948 
    1949     $w.e.2 config -bg beige
    1950     pack [frame $w.c -bg beige] -side top -expand yes -fill both
    1951     pack [frame $w.d -bg beige] -side top -expand yes -fill both
    1952     pack [button $w.d.2 -text Set  \
    1953             -command "SaveChangeProfileType $w.c $histlist $phaselist; destroy $w"\
    1954             ] -side left
    1955     pack [button $w.d.3 -text Quit \
    1956             -command "destroy $w"] -side left
    1957     pack [button $w.d.help -text Help -bg yellow \
    1958             -command "MakeWWWHelp expgui5.html ChangeType"] \
    1959             -side right
    1960     bind $w <Key-F1> "MakeWWWHelp expgui5.html ChangeType"
    1961     bind $w <Return> "destroy $w"
    1962 
    1963     $menu delete 0 end
    1964     set i 0
    1965     foreach lbls $lbllist {
    1966         incr i
    1967         $menu add command -label $i -command \
    1968                 "set expgui(newpeaktype) $i; \
    1969                 FillChangeProfileType $w.c $hist $phase $i [list $lbls] [list $oldlbls]"
    1970     }
    1971     set expgui(newpeaktype) $ptype
    1972     FillChangeProfileType $w.c $hist $phase $ptype $oldlbls $oldlbls
    1973 
    1974     # force the window to stay on top
    1975     putontop $w
    1976     focus $w.e.2
    1977     tkwait window $w
    1978     afterputontop
    1979     sethistlist
    1980 }
    1981 
    1982 # save the changes to the profile
    1983 proc SaveChangeProfileType {w histlist phaselist} {
    1984     global expgui
    1985     foreach phase $phaselist hist $histlist {
    1986         hapinfo $hist $phase proftype set $expgui(newpeaktype)
    1987         hapinfo $hist $phase profterms set $expgui(newProfileTerms)
    1988         for {set i 1} {$i <=  $expgui(newProfileTerms)} {incr i} {
    1989             hapinfo $hist $phase pterm$i set [$w.ent${i} get]
    1990             hapinfo $hist $phase pref$i set $expgui(ProfRef$i)
    1991         }
    1992         set i [expr 1+$expgui(newProfileTerms)]
    1993         hapinfo $hist $phase pcut set [$w.ent$i get]
    1994         incr expgui(changed) [expr 3 + $expgui(newProfileTerms)]
    1995     }
    1996 }
    1997 
    1998 # file the contents of the "Change Profile Type" Menu
    1999 proc FillChangeProfileType {w hist phase newtype lbls oldlbls} {
    2000     global expgui expmap
    2001     set ptype [string trim [hapinfo $hist $phase proftype]]
    2002     catch {unset oldval}
    2003     # loop through the old terms and set up an array of starting values
    2004     set num 0
    2005     foreach term $oldlbls {
    2006         incr num
    2007         set oldval($term) [hapinfo $hist $phase pterm$num]
    2008     }
    2009     set oldval(Peak\nCutoff) [hapinfo $hist $phase pcut]
    2010 
    2011     # is the new type the same as the current?
    2012     if {$ptype == $newtype} {
    2013         set nterms [hapinfo $hist $phase profterms]
    2014     } else {
    2015         set nterms [llength $lbls]
    2016     }
    2017     set expgui(newProfileTerms) $nterms
    2018     set expgui(CurrentProfileTerms) $nterms
    2019     # which default profile set matches the new type
    2020     set setnum {}
    2021     foreach j {" " 1 2 3 4 5 6 7 8 9} {
    2022         set i [profdefinfo $hist $j proftype]
    2023         if {$i == ""} continue
    2024         if {$i == $newtype} {
    2025             set setnum $j
    2026             break
    2027         }
    2028     }
    2029 
    2030     eval destroy [winfo children $w]
    2031 
    2032     set colstr 0
    2033     set row 2
    2034     set maxrow [expr $row + $nterms/2]
    2035     for { set num 1 } { $num <= $nterms + 1} { incr num } {
    2036         # get the default value (originally from the in .INS file)
    2037         set val {}
    2038         if {$setnum != ""} {
    2039             set val 0.0
    2040             catch {
    2041                 set val [profdefinfo $hist $setnum pterm$num]
    2042                 # pretty up the number
    2043                 if {$val == 0.0} {
    2044                     set val 0.0
    2045                 } elseif {abs($val) < 1e-2 || abs($val) > 1e6} {
    2046                     set val [format %.3e $val]
    2047                 } elseif {abs($val) > 1e-2 && abs($val) < 10} {
    2048                     set val [format %.5f $val]
    2049                 } elseif {abs($val) < 9999} {
    2050                     set val [format %.2f $val]
    2051                 } elseif {abs($val) < 1e6} {
    2052                     set val [format %.0f $val]
    2053                 }
    2054             }
    2055         }
    2056         # heading
    2057         if {$row == 2} {
    2058             set col $colstr
    2059             grid [label $w.h0${num} -text "lbl" -bg beige] \
    2060                 -row $row -column $col
    2061             grid [label $w.h2${num} -text "ref" -bg beige] \
    2062                 -row $row -column [incr col]
    2063             grid [label $w.h3${num} -text "next value" -bg beige] \
    2064                 -row $row -column [incr col]
    2065             grid [label $w.h4${num} -text "default" -bg beige] \
    2066                 -row $row -column [incr col]
    2067             grid [label $w.h5${num} -text "current" -bg beige] \
    2068                 -row $row -column [incr col]
    2069         }
    2070         set col $colstr
    2071         incr row
    2072         set term {}
    2073         catch {set term [lindex $lbls [expr $num-1]]}
    2074         if {$term == ""} {set term $num}
    2075         if {$num == $nterms + 1} {
    2076             set term "Peak\nCutoff"
    2077             set val {}
    2078             if {$setnum != ""} {
    2079                 set val 0.0
    2080                 catch {set val [profdefinfo $hist $setnum pcut]}
    2081             }
    2082         }
    2083 
    2084         grid [label $w.l${num} -text "$term" -bg beige] \
    2085                 -row $row -column $col
    2086         grid [checkbutton $w.chk${num} -variable expgui(ProfRef$num) \
    2087                 -bg beige -activebackground beige] -row $row -column [incr col]
    2088         grid [entry $w.ent${num} \
    2089                 -width 12] -row $row -column [incr col]
    2090         if {$val != ""} {
    2091             grid [button $w.def${num} -text $val -command \
    2092                     "$w.ent${num} delete 0 end; $w.ent${num} insert end $val" \
    2093                     ] -row $row -column [incr col] -sticky ew
     2138            lappend list $dir
     2139        }
     2140    }
     2141    foreach path $list {
     2142        $expgui(FileDirButtonMenu) add command -label $path \
     2143                -command "[list set expgui(FileMenuDir) $path]; \
     2144                ChooseExpFil $frm"
     2145    }
     2146    # highlight the current experiment -- if present
     2147    for {set i 0} {$i < [$files size]} {incr i} {
     2148        set file [$files get $i]
     2149        if {$expgui(expfile) == [file join $expgui(FileMenuDir) $file]} {
     2150            $files selection set $i
     2151        }
     2152    }
     2153    return
     2154}
     2155
     2156
     2157#------------------------------------------------------------------------------
     2158# platform-specific definitions
     2159if {$tcl_platform(platform) == "windows" && $tcl_platform(os) == "Windows 95"} {
     2160    # this creates a DOS box to run a program in
     2161    proc forknewterm {title command "background 0" "scrollbar 1" "wait 1"} {
     2162        global env expgui
     2163        # Windows environment variables
     2164        # -95 does not seem to be able to use these
     2165        set env(GSAS) [file nativename $expgui(gsasdir)]
     2166        # PGPLOT_FONT is needed by PGPLOT
     2167        set env(PGPLOT_FONT) [file nativename [file join $expgui(gsasdir) fonts grfont.dat]]
     2168        # this is the number of lines/page in the .LST (etc.) file
     2169        set env(LENPAGE) 60
     2170        set pwd [file nativename [pwd]]
     2171       
     2172        # check the path -- can DOS use it?
     2173        if {[string first // [pwd]] != -1} {
     2174            MyMessageBox -parent . -title "Invalid Path" \
     2175                    -message {Error -- Use "Map network drive" to access this directory with a letter (e.g. F:) GSAS can't directly access a network drive} \
     2176                    -icon error -type ok -default ok \
     2177                    -helplink "expgui_Win_readme.html NetPath"
     2178            return
     2179        }
     2180        # all winexec commands are background commands
     2181        #       if $background
     2182       
     2183        # pause is hard coded in the .BAT file
     2184           
     2185        # replace the forward slashes with backward
     2186        regsub -all / $command \\ command
     2187        # Win95 does not seem to inherit the environment from Tcl env vars
     2188        # so define it in the .BAT file
     2189        winexec -d [file nativename [pwd]] \
     2190                [file join $expgui(scriptdir) gsastcl.bat] \
     2191                "[file nativename $expgui(gsasdir)] $command"
     2192    }
     2193} elseif {$tcl_platform(platform) == "windows"} {
     2194    # now for - brain-dead Windows-NT
     2195    # this creates a DOS box to run a program in
     2196    proc forknewterm {title command "background 0" "scrollbar 1" "wait 1"} {
     2197        global env expgui
     2198        # Windows environment variables
     2199        set env(GSAS) [file nativename $expgui(gsasdir)]
     2200        # PGPLOT_FONT is needed by PGPLOT
     2201        set env(PGPLOT_FONT) [file nativename [file join $expgui(gsasdir) fonts grfont.dat]]
     2202        # this is the number of lines/page in the .LST (etc.) file
     2203        set env(LENPAGE) 60
     2204        # all winexec commands are background commands -- ignore background arg
     2205        # can't get pause to work! -- ignore wait
     2206       
     2207        set prevcmd {}
     2208        foreach cmd $command {
     2209            if {$prevcmd != ""} {
     2210                tk_dialog .done_yet Confirm "Press OK to start command $cmd" "" 0 OK
     2211            }
     2212            # replace the forward slashes with backward
     2213            regsub -all / $cmd \\ cmd
     2214            # cmd.exe must be in the path -- lets hope that at least works!
     2215            winexec -d [file nativename [pwd]] cmd.exe "/c $cmd"
     2216            set prevcmd $cmd
     2217        }
     2218    }
     2219} else {
     2220    # this creates a xterm window to run a program in
     2221    proc forknewterm {title command "background 0" "scrollbar 1" "wait 1"} {
     2222        global env expgui
     2223        # UNIX environment variables
     2224        set env(GSASEXE) $expgui(gsasexe)
     2225        set env(ATOMDATA) [file join $expgui(gsasdir) data atmdata.dat]
     2226        set env(ATMXSECT) [file join $expgui(gsasdir) data atmxsect.dat]
     2227        # PGPLOT_DIR is needed by PGPLOT
     2228        set env(PGPLOT_DIR) [file join $expgui(gsasdir) pgl]
     2229        # this is the number of lines/page in the .LST (etc.) file
     2230        set env(LENPAGE) 60
     2231        set termopts {}
     2232        if $env(GSASBACKSPACE) {
     2233            append termopts \
     2234                    {-xrm "xterm*VT100.Translations: #override\\n <KeyPress>BackSpace: string(\\177)"}
     2235        }
     2236        if $scrollbar {
     2237            append termopts " -sb"
    20942238        } else {
    2095             grid [label $w.def${num} -text (none) \
    2096                     ] -row $row -column [incr col]
    2097         }
    2098         set curval {}
    2099         catch {
    2100             set curval [expr $oldval($term)]
    2101             # pretty up the number
    2102             if {$curval == 0.0} {
    2103                 set curval 0.0
    2104             } elseif {abs($curval) < 1e-2 || abs($curval) > 1e6} {
    2105                 set curval [format %.3e $curval]
    2106             } elseif {abs($curval) > 1e-2 && abs($curval) < 10} {
    2107                 set curval [format %.5f $curval]
    2108             } elseif {abs($curval) < 9999} {
    2109                 set curval [format %.2f $curval]
    2110             } elseif {abs($curval) < 1e6} {
    2111                 set curval [format %.0f $curval]
    2112             }
    2113             grid [button $w.cur${num} -text $curval -command  \
    2114                     "$w.ent${num} delete 0 end; $w.ent${num} insert end $curval" \
    2115                     ] -row $row -column [incr col] -sticky ew
    2116         }
    2117         # set default values for flag and value
    2118         set ref 0
    2119         if {$setnum != ""} {
    2120             catch {
    2121                 if {[profdefinfo $hist $setnum pref$num] == "Y"} {set ref 1}
    2122             }
    2123         }
    2124         set expgui(ProfRef$num) $ref
    2125        
    2126         $w.ent${num} delete 0 end
    2127         if {!$expgui(DefaultPeakType) && $val != ""} {
    2128             $w.ent${num} insert end $val
    2129         } elseif {$curval != ""} {
    2130             $w.ent${num} insert end $curval
    2131         } elseif {$val != ""} {
    2132             $w.ent${num} insert end $val
     2239            append termopts " +sb"
     2240        }
     2241        if $background {
     2242            set suffix {&}
    21332243        } else {
    2134             $w.ent${num} insert end 0.0
    2135         }
    2136         if {$row > $maxrow} {
    2137             set row 2
    2138             incr colstr 5
    2139         }
    2140     }
    2141 }
    2142 
    2143 # browse a WWW page with URL. The URL may contain a #anchor
    2144 # On UNIX assume netscape is in the path or env(BROWSER) is loaded.
    2145 # On Windows search the registry for a browser. Mac branch not tested.
    2146 # This is taken from http://mini.net/cgi-bin/wikit/557.html with many thanks
    2147 # to the contributers
    2148 proc urlOpen {url} {
    2149     global env tcl_platform
    2150     switch $tcl_platform(platform) {
    2151         "unix" {
    2152             if {![info exists env(BROWSER)]} {
    2153                 set progs [auto_execok netscape]
    2154                 if {[llength $progs]} {
    2155                     set env(BROWSER) [list $progs]
    2156                 }
    2157             }
    2158             if {[info exists env(BROWSER)]} {
    2159                 if {[catch {exec $env(BROWSER) -remote openURL($url)}]} {
    2160                     # perhaps browser doesn't understand -remote flag
    2161                     if {[catch {exec $env(BROWSER) $url &} emsg]} {
    2162                         error "Error displaying $url in browser\n$emsg"
    2163                     }
    2164                 }
    2165             } else {
    2166                 tk_dialog .warn "No Browser" \
    2167                         "Could not find a browser. Netscape is not in path. Define environment variable BROWSER to be full path name of browser." \
    2168                         warn 0 OK
    2169             }
    2170         }
    2171         "windows" {
    2172             package require registry
    2173             # Look for the application under
    2174             # HKEY_CLASSES_ROOT
    2175             set root HKEY_CLASSES_ROOT
    2176 
    2177             # Get the application key for HTML files
    2178             set appKey [registry get $root\\.html ""]
    2179 
    2180             # Get the command for opening HTML files
    2181             set appCmd [registry get \
    2182                     $root\\$appKey\\shell\\open\\command ""]
    2183 
    2184             # Substitute the HTML filename into the command for %1
    2185             regsub %1 $appCmd $url appCmd
    2186            
    2187             # Double up the backslashes for eval (below)
    2188             regsub -all {\\} $appCmd  {\\\\} appCmd
    2189            
    2190             # Invoke the command
    2191             eval exec $appCmd &
    2192         }
    2193         "macintosh" {
    2194             if {0 == [info exists env(BROWSER)]} {
    2195                 set env(BROWSER) "Browse the Internet"
    2196             }
    2197             if {[catch {
    2198                 AppleScript execute\
    2199                     "tell application \"$env(BROWSER)\"
    2200                          open url \"$url\"
    2201                      end tell
    2202                 "} emsg]
    2203             } then {
    2204                 error "Error displaying $url in browser\n$emsg"
    2205             }
    2206         }
    2207     }
    2208 }
    2209 
    2210 proc NetHelp {file anchor localloc netloc} {
    2211     if {[file exists [file join $localloc $file]]} {
    2212         set url "[file join $localloc $file]"
    2213     } else {
    2214         set url "http://$netloc/$file"
    2215     }
    2216     catch {
    2217         pleasewait "Starting web browser..."
    2218         after 2000 donewait
    2219     }
    2220     if {$anchor != ""} {
    2221         append url # $anchor
    2222     }
    2223     urlOpen $url
    2224 }
    2225 
    2226 proc MakeWWWHelp {"topic {}" "anchor {}"} {
    2227     global expgui
    2228     if {$topic == ""} {
    2229         foreach item $expgui(notebookpagelist) {
    2230             if {[lindex $item 0] == $expgui(pagenow)} {
    2231                 NetHelp [lindex $item 5] [lindex $item 6] $expgui(docdir) ""
    2232                 return
    2233             }
    2234         }
    2235         # this should not happen
    2236         NetHelp expgui.html "" $expgui(docdir) ""       
    2237     } elseif {$topic == "menu"} {
    2238         NetHelp expguic.html "" $expgui(docdir) ""
    2239     } else {
    2240         NetHelp $topic $anchor $expgui(docdir) ""
    2241     }
    2242 }
     2244            set suffix {}
     2245        }
     2246        #
     2247        if $wait  {
     2248            append command "\; echo -n Press Enter to continue \; read x"
     2249        }
     2250        if !$background {wm iconify .}
     2251        catch {eval exec xterm $termopts -title [list $title] \
     2252                -e /bin/sh -c [list $command] $suffix} errmsg
     2253        if $expgui(debug) {puts "xterm result = $errmsg"}
     2254        if !$background {wm deiconify .}
     2255    }
     2256}
Note: See TracChangeset for help on using the changeset viewer.