#!/bin/sh
#Calculator  -*- TCL -*- Created: ramsan Sep-1996, Modified: ramsan Sep-1996

#         $Id: Calculator.tcl,v 1.21 2004/04/28 11:44:51 ramsan Exp $



#----------------------CALCULATOR ALONE---------------------------
# the next line restarts using wish \
exec wish "$0" "$@"

set CalcRunningAlone 0
catch {
    if {$argv0 == [info script] } {
        set CalcRunningAlone 1
        set tcl_interactive 0
        set tk_strictMotif 1
        wm withdraw .
        
        if { [catch { set GIDDEFAULT $env(GIDDEFAULT)}]!=1 } {
            set GIDDEFAULTTCL $GIDDEFAULT
            catch { set GIDDEFAULTTCL $env(GIDDEFAULTTCL) }
            set auto_path "$GIDDEFAULTTCL"
            source $GIDDEFAULTTCL/init.tcl
            source $GIDDEFAULTTCL/tk.tcl
        } else { set GIDDEFAULT "~/GiD Project" }
        
        package require msgcat
        
        #interp alias "" ::_ "" ::msgcat::mc
        proc ::_ { args } {
            if { [regexp {(.*)#C#(.*)} [lindex $args 0] {} str comm] } {
                set args [lreplace $args 0 0 $str]
            }
            return [uplevel 1 ::msgcat::mc $args]
        }
    }
}

#  LOOK AT THE END OF THE FILE !!!
#-------------------------------------------------------------------

# interface generated by SpecTcl version 1.0 from vec-help.ui
#   root     is the parent window for this user interface

set CalcPi 3.14159265358979323846

proc vec-help_ui {root args} {
    
    # this treats "." as a special case
    
    if {$root == "."} {
        set base ""
    } else {
        set base $root
    }
    
    button $base.button#4 \
            -text [_ "Help"] \
            -width 5
    
    button $base.button#6 \
            -text [_ "Close"] \
            -width 5
    
    menubutton $base.menubutton#2 \
            -width 5 \
            -borderwidth 1 \
            -relief raised \
            -text [_ "Move"] -menu $base.menubutton#2.m
    
    menu $base.menubutton#2.m -tearoff 0
    $base.menubutton#2.m add command -label [_ "Get point"] \
            -accelerator "Control-a" \
            -command "CalcGetCalcVectorFromGid"
    $base.menubutton#2.m add command -label [_ "Get distance"] \
            -accelerator "Control-d" \
            -command "CalcGetDistanceFromGid"
    $base.menubutton#2.m add command -label [_ "Get vector"] \
            -accelerator "Control-b" \
            -command "CalcGetVectorFromGid"
    $base.menubutton#2.m add command -label [_ "Get angle"] \
            -accelerator "Control-e" \
            -command "CalcGetAngleFromGid"
    $base.menubutton#2.m add command -label [_ "Write"] -accelerator "Control-w" \
            -command CalcWriteVectorOrScalar
    
    set bt $base
    while { [winfo class $bt] != "Toplevel" } { set bt [winfo parent $bt] }
    
    
    bind $bt <Control-a> "$base.menubutton#2.m invoke 0 ; break"
    bind $bt <Control-b> "$base.menubutton#2.m invoke 2 ; break"
    bind $bt <Control-e> "$base.menubutton#2.m invoke 3 ; break"
    bind $bt <Control-d> "$base.menubutton#2.m invoke 1 ; break"
    bind $bt <Control-w> "$base.menubutton#2.m invoke 4 ; break"
    
    
    bind $base.menubutton#2 <2> "CalcShowHelp $base.menubutton#2 2 Move"
    bind $base.menubutton#2 <3> "CalcShowHelp $base.menubutton#2 3 Move"
    
    # Geometry management
    
    grid $base.button#4 -in $root        -row 1 -column 1
    grid $base.button#6 -in $root        -row 2 -column 1
    grid $base.menubutton#2 -in $root        -row 3 -column 1
    
    # Resize behavior management
    
    grid rowconfigure $root 1 -weight 0 -minsize 30
    grid rowconfigure $root 2 -weight 0 -minsize 30
    grid rowconfigure $root 3 -weight 0 -minsize 30
    grid columnconfigure $root 1 -weight 0 -minsize 30
    # additional interface code
    # end additional interface code
    
}


# interface generated by SpecTcl version 1.0 from vect-disp.ui
#   root     is the parent window for this user interface

proc vect-disp_ui {root args} {
    global CalcAngleUnits
    # this treats "." as a special case
    
    if {$root == "."} {
        set base ""
    } else {
        set base $root
    }
    
    label $base.label#1 \
            -textvariable CalcAngleUnits -width 3
    
    listbox $base.listbox#1 \
            -height 4 \
            -width 10 \
            -xscrollcommand "$base.scrollbar#1 set" \
            -yscrollcommand "$base.scrollbar#2 set" \
            -setgrid 0 -selectmode single -exportselection 0
    
    scrollbar $base.scrollbar#2 \
            -command "$base.listbox#1 yview" \
            -orient v
    
    label $base.label#2 \
            -textvariable CalculatorPriv(inverse)
    
    label $base.label#3 \
            -textvariable CalculatorPriv(displayeng)
    
    scrollbar $base.scrollbar#1 \
            -command "$base.listbox#1 xview" \
            -orient h
    
    
    # Geometry management
    
    grid $base.label#1 -in $root        -row 1 -column 1  \
            -sticky s
    grid $base.listbox#1 -in $root        -row 1 -column 2  \
            -rowspan 3 \
            -sticky nesw
    grid $base.scrollbar#2 -in $root        -row 1 -column 3  \
            -rowspan 3 \
            -sticky ns
    grid $base.label#2 -in $root        -row 2 -column 1  \
            -sticky n
    grid $base.label#3 -in $root        -row 3 -column 1  \
            -sticky n
    grid $base.scrollbar#1 -in $root        -row 4 -column 2  \
            -sticky ew
    
    # Resize behavior management
    
    grid rowconfigure $root 1 -weight 1
    grid rowconfigure $root 2 -weight 0
    grid rowconfigure $root 3 -weight 0
    grid columnconfigure $root 1 -weight 0
    grid columnconfigure $root 2 -weight 1
    grid columnconfigure $root 3 -weight 0
    # additional interface code
    # end additional interface code
    
}


# interface generated by SpecTcl version 1.0 from vect-func.ui
#   root     is the parent window for this user interface

proc vect-func_ui {root args} {
    
    # this treats "." as a special case
    
    if {$root == "."} {
        set base ""
    } else {
        set base $root
    }
    
    button $base.button#4 \
            -text SQRT \
            -width 3
    
    button $base.button#5 \
            -text PI -width 3
    
    button $base.button#13 \
            -text <- \
            -width 3
    
    button $base.button#22 \
            -text CHS \
            -width 03
    
    button $base.button#6 \
            -text sin \
            -width 3
    
    button $base.button#7 \
            -text cos \
            -width 3
    
    button $base.button#14 \
            -text tan \
            -width 03
    
    button $base.button#23 \
            -text EEX \
            -width 03
    
    button $base.button#8 \
            -text STO \
            -width 3
    
    button $base.button#9 \
            -text RCL \
            -width 03
    
    button $base.button#15 \
            -text x<>y \
            -width 3
    
    button $base.button#24 \
            -text ENT \
            -width 3
    
    button $base.button#19 \
            -text CLS \
            -width 03
    
    button $base.button#20 \
            -text DRG \
            -width 03
    
    button $base.button#21 \
            -text INV \
            -width 03
    
    
    # Geometry management
    
    grid $base.button#4 -in $root        -row 1 -column 1
    grid $base.button#5 -in $root        -row 1 -column 2
    grid $base.button#13 -in $root        -row 1 -column 3
    grid $base.button#22 -in $root        -row 1 -column 4
    grid $base.button#6 -in $root        -row 2 -column 1
    grid $base.button#7 -in $root        -row 2 -column 2
    grid $base.button#14 -in $root        -row 2 -column 3
    grid $base.button#23 -in $root        -row 2 -column 4
    grid $base.button#8 -in $root        -row 3 -column 1
    grid $base.button#9 -in $root        -row 3 -column 2
    grid $base.button#15 -in $root        -row 3 -column 3
    grid $base.button#24 -in $root        -row 3 -column 4  \
            -rowspan 2 \
            -sticky nesw
    grid $base.button#19 -in $root        -row 4 -column 1
    grid $base.button#20 -in $root        -row 4 -column 2
    grid $base.button#21 -in $root        -row 4 -column 3
    
    # Resize behavior management
    
    grid rowconfigure $root 1 -weight 0 -minsize 30
    grid rowconfigure $root 2 -weight 0 -minsize 30
    grid rowconfigure $root 3 -weight 0 -minsize 30
    grid rowconfigure $root 4 -weight 0 -minsize 30
    grid columnconfigure $root 1 -weight 0 -minsize 30
    grid columnconfigure $root 2 -weight 0 -minsize 30
    grid columnconfigure $root 3 -weight 0 -minsize 30
    grid columnconfigure $root 4 -weight 0 -minsize 30
    # additional interface code
    # end additional interface code
    
}


# interface generated by SpecTcl version 1.0 from vect-num.ui
#   root     is the parent window for this user interface

proc vect-num_ui {root args} {
    
    # this treats "." as a special case
    
    if {$root == "."} {
        set base ""
    } else {
        set base $root
    }
    
    button $base.button#4 \
            -text 7 \
            -width 3
    
    button $base.button#5 \
            -text 8 \
            -width 3
    
    button $base.button#13 \
            -text 9 \
            -width 3
    
    button $base.button#22 \
            -text / \
            -width 03
    
    button $base.button#6 \
            -text 4 \
            -width 3
    
    button $base.button#7 \
            -text 5 \
            -width 3
    
    button $base.button#14 \
            -text 6 \
            -width 03
    
    button $base.button#23 \
            -text * \
            -width 03
    
    button $base.button#8 \
            -text 1 \
            -width 3
    
    button $base.button#9 \
            -text 2 \
            -width 03
    
    button $base.button#15 \
            -text 3 \
            -width 3
    
    button $base.button#24 \
            -text - \
            -width 3
    
    button $base.button#19 \
            -text 0 \
            -width 03
    
    button $base.button#20 \
            -text . \
            -width 03
    
    button $base.button#21 \
            -text v/m \
            -width 03
    
    button $base.button#25 \
            -text + \
            -width 03
    
    
    # Geometry management
    
    grid $base.button#4 -in $root        -row 1 -column 1
    grid $base.button#5 -in $root        -row 1 -column 2
    grid $base.button#13 -in $root        -row 1 -column 3
    grid $base.button#22 -in $root        -row 1 -column 4
    grid $base.button#6 -in $root        -row 2 -column 1
    grid $base.button#7 -in $root        -row 2 -column 2
    grid $base.button#14 -in $root        -row 2 -column 3
    grid $base.button#23 -in $root        -row 2 -column 4
    grid $base.button#8 -in $root        -row 3 -column 1
    grid $base.button#9 -in $root        -row 3 -column 2
    grid $base.button#15 -in $root        -row 3 -column 3
    grid $base.button#24 -in $root        -row 3 -column 4  \
            -sticky nesw
    grid $base.button#19 -in $root        -row 4 -column 1
    grid $base.button#20 -in $root        -row 4 -column 2
    grid $base.button#21 -in $root        -row 4 -column 3
    grid $base.button#25 -in $root        -row 4 -column 4
    
    # Resize behavior management
    
    grid rowconfigure $root 1 -weight 0 -minsize 30
    grid rowconfigure $root 2 -weight 0 -minsize 30
    grid rowconfigure $root 3 -weight 0 -minsize 30
    grid rowconfigure $root 4 -weight 0 -minsize 30
    grid columnconfigure $root 1 -weight 0 -minsize 30
    grid columnconfigure $root 2 -weight 0 -minsize 30
    grid columnconfigure $root 3 -weight 0 -minsize 30
    grid columnconfigure $root 4 -weight 0 -minsize 30
    # additional interface code
    # end additional interface code
    
}


# interface generated by SpecTcl version 1.0 from vect-vec.ui
#   root     is the parent window for this user interface

proc vect-vec_ui {root args} {
    
    # this treats "." as a special case
    
    if {$root == "."} {
        set base ""
    } else {
        set base $root
    }
    
    button $base.button#4 \
            -text x^y \
            -width 3
    
    button $base.button#5 \
            -text |x| \
            -width 3
    
    button $base.button#6 \
            -text x1 \
            -width 3
    
    button $base.button#7 \
            -text x<y \
            -width 3
    
    button $base.button#8 \
            -text ENG \
            -width 3
    
    button $base.button#9 \
            -width 03
    
    
    # Geometry management
    
    grid $base.button#4 -in $root        -row 1 -column 1
    grid $base.button#5 -in $root        -row 1 -column 2
    grid $base.button#6 -in $root        -row 2 -column 1
    grid $base.button#7 -in $root        -row 2 -column 2
    grid $base.button#8 -in $root        -row 3 -column 1
    grid $base.button#9 -in $root        -row 3 -column 2
    
    # Resize behavior management
    
    grid rowconfigure $root 1 -weight 0 -minsize 30
    grid rowconfigure $root 2 -weight 0 -minsize 30
    grid rowconfigure $root 3 -weight 0 -minsize 30
    grid columnconfigure $root 1 -weight 0 -minsize 30
    grid columnconfigure $root 2 -weight 0 -minsize 30
    # additional interface code
    # end additional interface code
    
}

proc CalcGetCalcVectorFromGid {} {
    set xyz [::GidUtils::GetCoordinates]
    if { $xyz != "" } {
        CalcPushNum "v [lindex $xyz 0],[lindex $xyz 1],[lindex $xyz 2]"
    }
}

proc CalcGetDistanceFromGid {} {
    set dist [::GidUtils::GetDistance]
    if { $dist != "" } {
        CalcPushNum "$dist"
    }
}

proc CalcGetVectorFromGid {} {
    set p [::GidUtils::GetVector]
    if { $p != "" } {
        CalcPushNum "v [lindex $p 0],[lindex $p 1],[lindex $p 2]"
    }
}

proc CalcGetAngleFromGid {} {
    global CalcAngleUnits
    
    set angle [::GidUtils::GetAngle]
    if { $angle == "" } { return }
    
    switch $CalcAngleUnits {
        rad { set angle [expr {$angle*$MathUtils::Pi/180.0}] }
        gra { set angle [expr {$angle*200.0/180.0}] }
    }
    CalcPushNum "$angle"
}

proc CalcWriteVectorOrScalar {} {
    global EntryVar EntryVarHilit
    
    CalcEndInsertion
    set num [CalcPopNum]
    CalcPushNum $num
    regexp {v (.*)} $num trash num
    set EntryVar $num
    set EntryVarHilit 1
}

proc CalcFlashlabel { num black w } {
    
    if { ![winfo exists $w] } { return }
    
    bind $w <1> "destroy $w"
    
    incr num
    if { $black == 0 } {
        $w conf -fg white
        set black 1
    } else {
        $w conf -fg black
        set black 0
    }
    if { $num < 10 } {
        after 300 CalcFlashlabel $num $black $w
    } else { destroy $w }
}

proc CalcShowError { buf } {
    global CalculatorPriv
    label $CalculatorPriv(listbox).errr -text $buf -relief ridge
    place  $CalculatorPriv(listbox).errr -x 0 -rely 1 -in \
            $CalculatorPriv(listbox) -anchor sw
    tkwait visibility $CalculatorPriv(listbox).errr
    
    CalcFlashlabel 0 0 $CalculatorPriv(listbox).errr
}

proc CalcInsEnd { text } {
    global CalculatorPriv
    
    if { $CalculatorPriv(displayeng) == "" } {
        $CalculatorPriv(listbox) ins end $text
    } else {
        if { [regexp {([^0-9]*)([^,]+),([^,]+),(.*)} $text {} pref x y z] } {
            set text [format "%s%g,%g,%g" $pref $x $y $z]
        } elseif { [regexp {([^0-9]*)(.*)} $text {} pref num] } {
            set text [format "%s%g" $pref $num]
        }
        $CalculatorPriv(listbox) ins end $text
    }
}

proc CalcPushNum { num } {
    global CalculatorPriv
    
    set x [string range [$CalculatorPriv(listbox) get end] 3 end]
    $CalculatorPriv(listbox) del end
    
    set y [string range [$CalculatorPriv(listbox) get end] 3 end]
    $CalculatorPriv(listbox) del end
    
    set z [string range [$CalculatorPriv(listbox) get end] 3 end]
    $CalculatorPriv(listbox) del end
    
    set t [string range [$CalculatorPriv(listbox) get end] 3 end]
    $CalculatorPriv(listbox) del end
    
    CalcInsEnd $t
    CalcInsEnd "t: $z"
    CalcInsEnd "z: $y"
    CalcInsEnd "y: $x"
    CalcInsEnd "x: $num"
    $CalculatorPriv(listbox) see end
    
    #      $CalculatorPriv(listbox) ins end $t
    #      $CalculatorPriv(listbox) ins end "t: $z"
    #      $CalculatorPriv(listbox) ins end "z: $y"
    #      $CalculatorPriv(listbox) ins end "y: $x"
    #      $CalculatorPriv(listbox) ins end "x: $num"
    #      $CalculatorPriv(listbox) see end
}

proc CalcPopNum {} {
    global CalculatorPriv
    
    set x [string range [$CalculatorPriv(listbox) get end] 3 end]
    $CalculatorPriv(listbox) del end
    
    set y [string range [$CalculatorPriv(listbox) get end] 3 end]
    $CalculatorPriv(listbox) del end
    
    set z [string range [$CalculatorPriv(listbox) get end] 3 end]
    $CalculatorPriv(listbox) del end
    
    set t [string range [$CalculatorPriv(listbox) get end] 3 end]
    $CalculatorPriv(listbox) del end
    
    set other 0.0
    if { [$CalculatorPriv(listbox) size] >0 } {
        set other [$CalculatorPriv(listbox) get end]
        $CalculatorPriv(listbox) del end
    }
    
    CalcInsEnd "t: $other"
    CalcInsEnd "z: $t"
    CalcInsEnd "y: $z"
    CalcInsEnd "x: $y"
    $CalculatorPriv(listbox) see end
    
    #      $CalculatorPriv(listbox) ins end "t: $other"
    #      $CalculatorPriv(listbox) ins end "z: $t"
    #      $CalculatorPriv(listbox) ins end "y: $z"
    #      $CalculatorPriv(listbox) ins end "x: $y"
    #      $CalculatorPriv(listbox) see end
    
    return $x
}

proc CalcRedisplay {} {
    global CalculatorPriv
    
    set x [string range [$CalculatorPriv(listbox) get end] 3 end]
    $CalculatorPriv(listbox) del end
    
    set y [string range [$CalculatorPriv(listbox) get end] 3 end]
    $CalculatorPriv(listbox) del end
    
    set z [string range [$CalculatorPriv(listbox) get end] 3 end]
    $CalculatorPriv(listbox) del end
    
    set t [string range [$CalculatorPriv(listbox) get end] 3 end]
    $CalculatorPriv(listbox) del end
    
    CalcInsEnd "t: $t"
    CalcInsEnd "z: $z"
    CalcInsEnd "y: $y"
    CalcInsEnd "x: $x"
    $CalculatorPriv(listbox) see end
}

proc CalcConvertToValid { val } {
    if { ![regexp \
                {^[  ]*([+-]?)0*([0-9]*)(\.?)([0-9]*)([eE][+-]?([0-9]*))?[  ]*$}\
                $val trash sign bp p ap exp expval] } { return NaN }
    if { $bp == "" } { set bp 0}
    if { $p == "" } { set p .}
    if { [string length $ap] == 0 } { set ap 0}
    if { $exp != "" && $expval == "" } { append exp 1 }
    set num $sign$bp$p$ap$exp
    if { $num == "-0.0" } { set num 0.0 }
    return $num
}

proc CalcConvertToValidVec { val } {
    if { ![regexp {v[ ]*([^,]*),?([^,]*),?([^,]*)} \
                $val trash v(1) v(2) v(3)] } {
        return [CalcConvertToValid $val]
    }
    
    set v(1) [CalcConvertToValid $v(1)]
    set v(2) [CalcConvertToValid $v(2)]
    set v(3) [CalcConvertToValid $v(3)]
    
    return "v $v(1),$v(2),$v(3)"
}

proc CalcConvertToValidVecExtern { val } {
    if { ![regexp {[ ]*([^,]+),([^,]+),?([-+0-9.eE]*)} \
                $val trash v(1) v(2) v(3)] } {
        
        if { ![regexp {[ ]*([^ ]+)[ ]+([^ ]+)[ ]*([-+0-9.eE]*)} \
                    $val trash v(1) v(2) v(3)] } {
            return [CalcConvertToValid $val]
        }
    }
    
    set v(1) [CalcConvertToValid $v(1)]
    set v(2) [CalcConvertToValid $v(2)]
    set v(3) [CalcConvertToValid $v(3)]
    
    if { $v(1) == "NaN" || $v(2) == "NaN" || $v(3) == "NaN" } {
        return NaN
    }
    
    return "v $v(1),$v(2),$v(3)"
}

proc CalcChangeSignToNum { num { inexponent  0 } } {
    
    set isv [regexp {v[ ]*([^,]*),?([^,]*),?([^,]*)} \
            $num trash v(1) v(2) v(3)]
    
    set endi 1
    if { $isv } { set endi 3 }
    
    for {set i 1} {$i <= $endi} {incr i} {
        
        if { $isv } { set num $v($i) }
        
        if { !$inexponent || ![string match *E* $num] } {
            if { [string index $num 0] == "+" } {
                regsub {\+} $num {-} num
            } elseif { [string index $num 0] == "-" } {
                regsub -- {-} $num {} num
            } else { set num -$num }
        } elseif { [string match *E\+* $num] } {
            regsub {E\+} $num {E-} num
        } else { regsub {E\-} $num {E+} num }
        
        if { $isv } { set v($i) $num }
    }
    
    if { $isv } { set num "v $v(1),$v(2),$v(3)" }
    return $num
}

proc CalcInsert { what } {
    global CalculatorPriv
    
    if { $CalculatorPriv(insert) != ""} {
        $CalculatorPriv(listbox) del end
    }
    
    set isv [regexp {v[ ]*([^,]*)(,?)([^,]*)(,?)([^,]*)} \
            $CalculatorPriv(insert) trash v(1) sp1 v(2) sp2 v(3)]
    
    if { !$isv } {
        set num $CalculatorPriv(insert)
        set pos 0
    } elseif { $sp2 != "" } {
        set num $v(3)
        set pos 3
    } elseif { $sp1 != "" } {
        set num $v(2)
        set pos 2
    } else {
        set num $v(1)
        set pos 1
    }
    
    switch -- $what {
        CHS {
            set num [CalcChangeSignToNum $num 1]
        }
        EEX {
            if { ![string match *E* $num] } {
                if { $num == "" } { set num 1.0 }
                set num ${num}E+
            } else {
                regsub {E.*} $num {} num
            }
        }
        . {
            if { ![string match *E* $num] && ![string match *.* $num] } {
                append num .
            }
        }
        v {
            if { $pos == 0 } {
                if { $num != "" } {
                    set v(1) [CalcConvertToValid $num]
                    set pos 2
                    set num ""
                } else { set pos 1 }
            } elseif { $pos < 3 } {
                set v($pos) [CalcConvertToValid $num]
                incr pos
                set num ""
            } else {
                $CalculatorPriv(listbox) ins end $CalculatorPriv(insert)
                CalcEndInsertion
                return
            }
        }
        <- {
            if { $num != "" } {
                set num [string range $num 0 [expr [string length $num]-2]]
            } elseif { $pos > 1 } {
                incr pos -1
                set num $v($pos)
            } elseif { $pos == 1 } { set pos 0 }
        }
        default {
            append num $what
        }
    }
    
    if { !$pos } {
        set CalculatorPriv(insert) $num
    } elseif { $pos == 3 } {
        set CalculatorPriv(insert) "v $v(1),$v(2),$num"
    } elseif { $pos == 2 } {
        set CalculatorPriv(insert) "v $v(1),$num"
    } else { set CalculatorPriv(insert) "v $num" }
    
    if { $CalculatorPriv(insert)!= "" } {
        $CalculatorPriv(listbox) ins end $CalculatorPriv(insert)
    }
    $CalculatorPriv(listbox) see end
}

proc CalcEndInsertion {} {
    global CalculatorPriv
    
    if { $CalculatorPriv(insert) != ""} {
        $CalculatorPriv(listbox) del end
        CalcPushNum [CalcConvertToValidVec $CalculatorPriv(insert)]
        set CalculatorPriv(insert) ""
    }
}

proc CalcGeneralFunc { what } {
    global CalculatorPriv CalcAngleUnits CalcPi
    
    switch $what {
        CHS {
            if { $CalculatorPriv(insert) != ""} {
                CalcInsert CHS
            } else {
                set num [CalcChangeSignToNum [CalcPopNum]]
                CalcPushNum $num
            }
        }
        CLS {
            if { $CalculatorPriv(insert) != ""} {
                $CalculatorPriv(listbox) del end
                set CalculatorPriv(insert) ""
            } else {
                CalcPopNum
            }
        }
        ENT {
            if { $CalculatorPriv(insert) != ""} {
                CalcEndInsertion
            } else {
                set num [CalcPopNum]
                CalcPushNum $num
                CalcPushNum $num
            }
        }
        inv {
            if { $CalculatorPriv(inverse) == "" } {
                set CalculatorPriv(inverse) inv
            } else {
                set CalculatorPriv(inverse) ""
            }
        }
        PI {
            if { $CalculatorPriv(insert) != ""} {
                CalcEndInsertion
            }
            CalcPushNum $CalcPi
        }
        STO {
            if { $CalculatorPriv(insert) != ""} {
                CalcEndInsertion
            }
            set CalculatorPriv(STO) [CalcPopNum]
            CalcPushNum $CalculatorPriv(STO)
        }
        RCL {
            if { $CalculatorPriv(STO) != ""} {
                if { $CalculatorPriv(insert) != ""} {
                    CalcEndInsertion
                }
                CalcPushNum $CalculatorPriv(STO)
            }
        }
        DRG {
            if { $CalculatorPriv(inverse) == "" } {
                if { $CalcAngleUnits == "rad" } {
                    set CalcAngleUnits deg
                } elseif { $CalcAngleUnits == "deg" } {
                    set CalcAngleUnits gra
                } else { set CalcAngleUnits rad }
            } else {
                if { $CalcAngleUnits == "rad" } {
                    set CalcAngleUnits gra
                } elseif { $CalcAngleUnits == "deg" } {
                    set CalcAngleUnits rad
                } else { set CalcAngleUnits deg }
                set CalculatorPriv(inverse) ""
            }
        }
        x<>y {
            CalcEndInsertion
            set num1 [CalcPopNum]
            set num2 [CalcPopNum]
            CalcPushNum $num1
            CalcPushNum $num2
        }
        ENG {
            if { $CalculatorPriv(displayeng) == "eng" } {
                set CalculatorPriv(displayeng) ""
            } else {
                set CalculatorPriv(displayeng) eng
            }
            CalcEndInsertion
            CalcRedisplay
        }
    }
    
}


proc CalcBinaryOp { op { printerror yes } } {
    global CalculatorPriv
    
    if { $CalculatorPriv(inverse) == "inv" } {
        CalcShowError "error: $op have no inverse"
        set CalculatorPriv(inverse) ""
        return
    }
    
    CalcEndInsertion
    
    set num2 [CalcPopNum]
    set num1 [CalcPopNum]
    
    set isv1 [regexp {v[ ]*([^,]*),?([^,]*),?([^,]*)} $num1 trash v1(1) \
            v1(2) v1(3)]
    set isv2 [regexp {v[ ]*([^,]*),?([^,]*),?([^,]*)} $num2 trash v2(1) \
            v2(2) v2(3)]
    
    set error ""
    
    if { $op == "x<y" } {
        if { !$isv1 || !$isv2 } {
            set error "operation angle only for vectors"
        } else {
            CalcPushNum $num1
            set error [CalcFuncOperation x1 no]
            if { $error != "" } {
                set error "vector y is a zero vector"
                CalcPopNum
            } else {
                CalcPushNum $num2
                set error [CalcFuncOperation x1 no]
                if { $error != "" } {
                    set error "vector x is a zero vector"
                    CalcPopNum
                } else {
                    CalcBinaryOp *
                    CalcGeneralFunc inv
                    CalcFuncOperation cos
                    set res [CalcPopNum]
                }
            }
        }
    } elseif { $op == "^" && ( !$isv1 || !$isv2 ) } {
        set error "cross product only for vectors"
    } elseif { !$isv2 && $op == "/" && $num2 == 0.0 } {
        set error "divide by 0"
    } elseif { $op == "/" && $isv2 } {
        set error "can't divide by a vector"
    } elseif { !$isv1 && !$isv2 } {
        set res [expr $num1 $op $num2]
    } elseif { $isv1 && $isv2 } {
        if { $op == "^" } {
            set resv(1) [expr $v1(2)*$v2(3)-$v2(2)*$v1(3)]
            set resv(2) [expr $v1(3)*$v2(1)-$v1(1)*$v2(3)]
            set resv(3) [expr $v1(1)*$v2(2)-$v1(2)*$v2(1)]
        } else {
            for {set i 1} {$i <= 3} {incr i} {
                set resv($i) [expr $v1($i) $op $v2($i)]
            }
        }
        if { $op == "*"} { set res [expr $resv(1)+$resv(2)+$resv(3)] }
    } elseif { $op == "+" || $op == "-" } {
        set error "can't add or substract scalar and vector"
    } else {
        for {set i 1} {$i <= 3} {incr i} {
            if { $isv1 } {
                set resv($i) [expr $v1($i) $op $num2]
            } else { set resv($i) [expr $num1 $op $v2($i)] }
        }
    }
    
    if { $error != "" } {
        if { $printerror == "yes" } {
            CalcShowError "error: $error"
        }
        CalcPushNum $num1
        CalcPushNum $num2
        return error
    } elseif { [info exists res] } {
        CalcPushNum $res
    } else {
        CalcPushNum "v $resv(1),$resv(2),$resv(3)"
    }
    return ""
}


proc CalcConvertAngleUnits { from to angle } {
    global CalcPi
    
    if { $from == "rad" && $to == "deg" } {
        return [expr $angle*180.0/$CalcPi]
    } elseif { $from == "rad" && $to == "gra" } {
        return [expr $angle*200.0/$CalcPi]
    } elseif { $from == "deg" && $to == "rad" } {
        return [expr $angle*$CalcPi/180.0]
    } elseif { $from == "deg" && $to == "gra" } {
        return [expr $angle*200.0/180.0]
    } elseif { $from == "gra" && $to == "rad" } {
        return [expr $angle*$CalcPi/200.0]
    } else {
        return [expr $angle*180.0/200.0]
    }
}

proc CalcFuncOperation { func { printerror yes } } {
    global CalculatorPriv CalcAngleUnits
    
    CalcEndInsertion
    set num [CalcPopNum]
    
    set isv [regexp {v[ ]*([^,]*),?([^,]*),?([^,]*)} $num trash v(1) \
            v(2) v(3)]
    
    set error ""
    
    if { $CalculatorPriv(inverse) == "inv" } {
        if { $func == "sin" } {
            set func asin
        } elseif { $func == "cos" } {
            set func acos
        } elseif { $func == "tan" } {
            set func atan
        } else {
            CalcShowError "error: $func have no inverse"
            CalcPushNum $num
            set CalculatorPriv(inverse) ""
            return error
        }
    }
    
    if { $isv && $func == "abs" } {
        CalcPushNum $num
        CalcPushNum $num
        CalcBinaryOp *
        CalcFuncOperation sqrt
    } elseif { $func == "x1" } {
        if { !$isv } {
            set error "x1 (x norma) can only  be performed on vector"
        } else {
            CalcPushNum $num
            CalcPushNum $num
            CalcFuncOperation abs
            set error [CalcBinaryOp / no]
            if { $error != "" } {
                set error "Can't normalize a zero vector"
                CalcPopNum
                CalcPopNum
            }
        }
    } elseif { $isv } {
        set error "$func can't be performed on vector"
    } elseif { $func == "sqrt" && $num < 0.0 } {
        set error "sqrt of negative number"
    } elseif { ( $func == "asin" || $func == "acos") && \
                ($num < -1.0 || $num > 1.0)} {
        set error "func $func not defined in $num"
    } else {
        if { $CalcAngleUnits != "rad" && ( $func == "sin" || $func == "cos" || \
                    $func == "tan") } {
            set num [CalcConvertAngleUnits $CalcAngleUnits rad $num]
        }
        set res [expr $func\($num)]
        if { $CalculatorPriv(inverse) == "inv" && $CalcAngleUnits != "rad" && \
                    ( $func == "asin" || $func == "acos" || \
                    $func == "atan" ) } {
            set res [CalcConvertAngleUnits rad $CalcAngleUnits $res]
        }
        CalcPushNum $res
    }
    set CalculatorPriv(inverse) ""
    if { $error != "" } {
        if { $printerror == "yes" } {
            CalcShowError "error: $error"
        }
        CalcPushNum $num
    }
    
}

proc CalcPackStuff {} {
    global CalculatorPriv
    
    for {set i 0} {$i < 3} {incr i} {
        set fr [lindex $CalculatorPriv(MainFrames) $i]
        set isbig 0
        set isvoid 1
        foreach j $CalculatorPriv(frame$i) {
            if { ![string match *disp $j] } {
                pack $j -in $fr -side left -expand no -fill none
            } else {
                pack $j -in $fr -side left -expand yes -fill both
                set isbig 1
            }
            set isvoid 0
        }
        pack forget $fr
        if { !$isbig } {
            pack $fr -side top -expand no -fill none
        } elseif { !$isvoid } {
            pack $fr -side top -expand yes -fill both
        }
    }
}


proc CalcBeginFrameMov { wmain w } {
    global CalculatorPriv
    
    $w config -relief sunken
    set CalculatorPriv(lastframe) [winfo parent $w]
    set CalculatorPriv(lastmovebut) $w
    
    raise $CalculatorPriv(lastframe)
    pack propagate $wmain 0
    grab -global $wmain
    bind $wmain <Motion> "CalcDoFrameMov $wmain %W %x %y"
    bind $wmain <ButtonRelease-1> "CalcFinishFrameMov $wmain %W %x %y ; break"
    
}

proc CalcDoFrameMov { wmain w x y } {
    global CalculatorPriv
    
    if { ![info exists CalculatorPriv(lastframe)] } {
        bind $wmain <Motion> ""
        bind $wmain <ButtonRelease-1> ""
        return
    }
    
    while { $w != $wmain } {
        set x [expr $x+[winfo x $w]]
        set y [expr $y+[winfo y $w]]
        set w [winfo parent $w]
    }
    
    place $CalculatorPriv(lastframe) -x $x -y $y \
            -in $wmain -anchor nw
}

proc CalcFinishFrameMov { wmain w x y } {
    global CalculatorPriv
    
    $CalculatorPriv(lastmovebut) config -relief raised
    bind $wmain <Motion> ""
    bind $wmain <ButtonRelease-1> ""
    grab release $wmain
    
    
    while { $w != $wmain } {
        set x [expr $x+[winfo x $w]]
        set y [expr $y+[winfo y $w]]
        set w [winfo parent $w]
    }
    
    if { ![info exists CalculatorPriv(lastframe)] } { return }
    
    for {set i 0} {$i < 3} {incr i} {
        set pos [lsearch $CalculatorPriv(frame$i) $CalculatorPriv(lastframe)]
        if { $pos != -1 } {
            set CalculatorPriv(frame$i) [lreplace $CalculatorPriv(frame$i) \
                    $pos $pos]
            break
        }
    }
    update
    set ibest -1
    for {set i 0} {$i < 3} {incr i} {
        set fr [lindex $CalculatorPriv(MainFrames) $i]
        set ymid [expr [winfo y $fr]+[winfo height $fr]/2]
        if { $ibest == -1 && ( $y < $ymid || $i == 2) } {
            set ibest $i
            set jnum 0
            foreach j $CalculatorPriv(frame$i) {
                set xmid [expr [winfo x $fr]+[winfo x $j]+[winfo width $j]/2]
                if { $x < $xmid } { break }
                incr jnum
            }
        }
    }
    
    set CalculatorPriv(frame$ibest) [linsert \
            $CalculatorPriv(frame$ibest) \
            $jnum $CalculatorPriv(lastframe)]
    
    
    if { $CalculatorPriv(frame0) == "" } {
        set CalculatorPriv(frame0) $CalculatorPriv(frame1)
        set CalculatorPriv(frame1) $CalculatorPriv(frame2)
    }
    
    CalcPackStuff
    unset CalculatorPriv(lastframe)
    unset CalculatorPriv(lastmovebut)
    pack propagate $wmain 1
}

proc CalcAddCommandToButChildren { w } {
    
    foreach i [winfo children $w] {
        if { [winfo class $i] == "Frame" } {
            CalcAddCommandToButChildren $i
        } elseif { [winfo class $i] == "Button" } {
            set text [$i cget -text]
            if { $text != "" } {
                $i conf -command "CalcMainOp $text"
                bind $i <2> "CalcShowHelp $i 2 $text"
                bind $i <3> "CalcShowHelp $i 3 $text"
            }
        }
    }
}

proc InitOperations {} {
    global CalculatorPriv tcl_platform CalcRunningAlone
    
    set CalculatorPriv(name) ""
    set CalculatorPriv(op) ""
    
    set val {7 8 9 / 4 5 6 * 1 2 3 - 0 . v/m +}
    if { $tcl_platform(platform) != "windows" } {
        set acc {{KeyPress-7 KP_Home} {KeyPress-8 KP_Up} {KeyPress-9 KP_Prior} \
                    {slash KP_Divide} {KeyPress-4 KP_Left} {KeyPress-5 KP_Begin} \
                    {KeyPress-6 KP_Right} {asterisk KP_Multiply} {KeyPress-1 KP_End} \
                    {KeyPress-2 KP_Down} {KeyPress-3 KP_Next} {minus KP_Subtract} \
                    {KeyPress-0 KP_Insert} {period KP_Delete} {comma space} \
                    {plus KP_Add}}
    } else {
        set acc {KeyPress-7 KeyPress-8 KeyPress-9 slash KeyPress-4 KeyPress-5 \
                    KeyPress-6 asterisk KeyPress-1 KeyPress-2 KeyPress-3 minus \
                    KeyPress-0 period {comma space} plus }
    }
    set func { "CalcInsert 7" "CalcInsert 8" "CalcInsert 9" "CalcBinaryOp /" \
                "CalcInsert 4" "CalcInsert 5" "CalcInsert 6" "CalcBinaryOp *" \
                "CalcInsert 1" "CalcInsert 2" "CalcInsert 3" "CalcBinaryOp -" \
                "CalcInsert 0" "CalcInsert ." "CalcInsert v" "CalcBinaryOp +"}
    
    append CalculatorPriv(name) " $val"
    append CalculatorPriv(acc) " $acc"
    append CalculatorPriv(op) " $func"
    
    set val {CHS EEX ENT}
    if { $tcl_platform(platform) != "windows" } {
        set acc { m   e   {Return KP_Enter}}
    } else {
        set acc { m   e Return }
    }
    set func { "CalcGeneralFunc CHS" "CalcInsert EEX" "CalcGeneralFunc ENT"}
    
    append CalculatorPriv(name) " $val"
    append CalculatorPriv(acc) " $acc"
    append CalculatorPriv(op) " $func"
    
    set val {SQRT "PI" "<-" sin cos tan STO RCL x<>y CLS DRG INV ENG}
    set acc { r p "BackSpace Delete" s c t "" "" "" l d i g}
    set func { "CalcFuncOperation sqrt" "CalcGeneralFunc PI" "CalcInsert <-" \
                "CalcFuncOperation sin" \
                "CalcFuncOperation cos" "CalcFuncOperation tan" \
                "CalcGeneralFunc STO" "CalcGeneralFunc RCL" \
                "CalcGeneralFunc x<>y"  "CalcGeneralFunc CLS" \
                "CalcGeneralFunc DRG" "CalcGeneralFunc inv" "CalcGeneralFunc ENG"}
    
    append CalculatorPriv(name) " $val"
    append CalculatorPriv(acc) " $acc"
    append CalculatorPriv(op) " $func"
    
    set val [list [_ "Help"] [_ "Close"]]
    set acc { h Control-q}
    if { $CalcRunningAlone == 1 } {
        set func { CalcOpenHelpWindow "destroy ." }
    } else { set func { CalcOpenHelpWindow "destroy $CalculatorPriv(wCalcul)" } }
    
    append CalculatorPriv(name) " $val"
    append CalculatorPriv(acc) " $acc"
    append CalculatorPriv(op) " $func"
    
    
    set val {x^y "|x|" "x1" "x<y" "" ""}
    set acc { "" "" "" "" "" ""}
    set func { "CalcBinaryOp ^" "CalcFuncOperation abs" "CalcFuncOperation x1" \
                "CalcBinaryOp x<y" "" "" ""}
    
    append CalculatorPriv(name) " $val"
    append CalculatorPriv(acc) " $acc"
    append CalculatorPriv(op) " $func"
    
    
    set inum 0
    foreach i $CalculatorPriv(op) {
        if { $i != "" } {
            set acc [lindex $CalculatorPriv(acc) $inum]
            foreach j $acc {
                if { $j != "" } {
                    bind $CalculatorPriv(wCalcul) <$j> $i
                }
            }
        }
        incr inum
    }
    
    
    
}


proc CalcMainOp { what } {
    global CalculatorPriv
    
    set pos [lsearch -exact $CalculatorPriv(name) $what]
    if { $pos != -1 } { eval [lindex $CalculatorPriv(op) $pos] }
}


proc CalculatorV { { w .wcalc } args } {
    global tcl_precision CalcAngleUnits CalculatorPriv GIDDEFAULT tcl_platform
    
    set tcl_precision 17
    set CalculatorPriv(displayeng) ""
    
    if { [catch {
            InitWindow $w [_ "Vector Calculator"] CalculatorVVWindowGeom CalculatorV
        }] } {
        toplevel $w
    }
    
    set CalculatorPriv(wCalcul) $w
    set CalculatorPriv(MainFrames) "$w.up $w.mid $w.down"
    
    foreach i $CalculatorPriv(MainFrames) {
        frame $i
    }
    
    set funcs "vect-disp_ui vect-vec_ui vec-help_ui vect-func_ui vect-num_ui"
    set frames "$w.disp $w.vec $w.help $w.func $w.num"
    
    for {set i 0} {$i < 5} {incr i} {
        eval [lindex $funcs $i] [frame [lindex $frames $i] -bd 2 -relief ridge]
        set but [button [lindex $frames $i].aspa -bitmap \
                @$GIDDEFAULT/bitmaps/aspa]
        place $but -x 0 -y 2 -anchor w
        bind $but <ButtonPress-1>  "CalcBeginFrameMov $w $but ; break"
    }
    
    set CalculatorPriv(frame0) "$w.disp $w.vec $w.help"
    set CalculatorPriv(frame1) "$w.func $w.num"
    set CalculatorPriv(frame2) ""
    
    CalcPackStuff
    
    set CalculatorPriv(insert) ""
    set CalculatorPriv(listbox) $w.disp.listbox#1
    if { ![info exists CalcAngleUnits] || ($CalcAngleUnits != "rad" && \
                $CalcAngleUnits != "deg" && $CalcAngleUnits != "gra") } {
        set CalcAngleUnits "rad"
    }
    set CalculatorPriv(inverse) ""
    
    $CalculatorPriv(listbox) insert end "t: 0.0"
    $CalculatorPriv(listbox) insert end "z: 0.0"
    $CalculatorPriv(listbox) insert end "y: 0.0"
    $CalculatorPriv(listbox) insert end "x: 0.0"
    
    selection handle $CalculatorPriv(listbox) CalcReturnSelectedL
    bind $CalculatorPriv(listbox) <ButtonRelease-1> \
            "selection own -command CalcLooseSelectionL $CalculatorPriv(listbox)"
    
    bind $CalculatorPriv(listbox) <2> {
        catch {
            CalcPasteNum [string trim [selection get] " "]
        }
        break
    }
    bind $CalculatorPriv(listbox) <B2-Motion> "break"
    
    if { $tcl_platform(platform) == "windows"} {
        bind $CalculatorPriv(wCalcul) <Control-c> "CalcReturnSelectedL 0 0 ; break"
        #bind all <Control-c> "CalcReturnSelectedL 0 0 ; break"
        bind $CalculatorPriv(wCalcul) <Control-v> "CalcPasteFromClipboard ; break"
        #bind all <Control-v> "CalcPasteFromClipboard ; break"
    }
    
    InitOperations
    CalcAddCommandToButChildren $w
}

proc CalcPasteFromClipboard {} {
    catch {
        CalcPasteNum [string trim [selection get -selection CLIPBOARD] " "]
    }
}

proc CalcReturnSelectedL { offset maxBytes } {
    global CalculatorPriv tcl_platform
    
    set cur [$CalculatorPriv(listbox) curselection]
    
    set num ""
    if { $cur != "" } {
        set num [$CalculatorPriv(listbox) get $cur]
        regexp {(x|y|z|t): (.*)} $num trash trash2 num
        regexp {v (.*)} $num trash num
    }
    
    if { $tcl_platform(platform) == "windows"} {
        clipboard clear -displayof .
        clipboard append -displayof . $num
    } else {
        return $num
    }
}

proc CalcLooseSelectionL {} {
    global CalculatorPriv
    $CalculatorPriv(listbox) selection clear 0 end
}

proc CalcPasteNum { val } {
    set val [CalcConvertToValidVecExtern $val]
    if { $val == "NaN" } {
        CalcShowError "error: Not valid pasted scalar or vector"
    } else {
        CalcPushNum $val
    }
}

proc CalcOpenHelpWindow {} {
    global CalculatorPriv
    set w $CalculatorPriv(wCalcul).mhelp
    catch { destroy $w}
    
    toplevel $w
    wm title $w [_ "CalculatorV Help"]
    wm iconname $w "CalcV H"
    wm geom $w \
            +[winfo x $CalculatorPriv(wCalcul)]+[winfo y $CalculatorPriv(wCalcul)]
    
    frame $w.f
    text $w.f.t -width 50 -height 20 -yscroll "$w.f.yscroll set"
    scrollbar $w.f.yscroll -relief sunken -command "$w.f.t yview"
    pack $w.f.yscroll -side right -fill y
    pack $w.f.t  -expand yes -fill y -anchor nw
    $w.f.t ins end $CalculatorPriv(HelpCalcVariable)
    $w.f.t conf -state disabled
    button $w.b -text [_ "Close"] -command "destroy $w"
    pack $w.f -side top
    pack $w.b -side bottom
}

proc CalcShowHelp { w but what } {
    global CalculatorPriv
    
    if { $what == "" } { return }
    
    set whatsub $what
    foreach i "* + | ^ ." {
        regsub -all "\\$i" $whatsub "\\$i" whatsub
    }
    regexp -- "\n\[*\]($whatsub\[^*\]*)" \
            $CalculatorPriv(HelpCalcVariable) \
            trash text
    
    if { [info exists text] } {
        set text [string trimright $text \n]
    } else {
        set num [lsearch -exact $CalculatorPriv(name) $what]
        set acc [lindex $CalculatorPriv(acc) $num]
        set text "$what\nshortcuts: $acc"
    }
    
    catch { destroy $CalculatorPriv(wCalcul).ghelp }
    
    
    set x 0
    set y 0
    set wtmp $w
    while { $wtmp != $CalculatorPriv(wCalcul) } {
        set x [expr $x+[winfo x $wtmp]]
        set y [expr $y+[winfo y $wtmp]]
        set wtmp [winfo parent $wtmp]
    }
    if { $y < [expr [winfo height $CalculatorPriv(wCalcul)]/2.0] } {
        set anchorw n
        set ry 0
    } else {
        set anchorw s
        set ry 1
    }
    if { $x < [expr [winfo width $CalculatorPriv(wCalcul)]/2.0] } {
        set rx 0
        append anchorw w
    } else {
        set rx 1
        append anchorw e
    }
    
    label $CalculatorPriv(wCalcul).ghelp -relief ridge -bg white -fg black \
            -justify left -text $text
    place $CalculatorPriv(wCalcul).ghelp -in $w -anchor $anchorw -relx $rx -rely $ry
    
    
    bind $w <ButtonRelease-$but> " \
            catch { destroy $CalculatorPriv(wCalcul).ghelp } ; \
            bind $w <ButtonRelease-$but> {}"
    
    
}

# variable for HELP
set CalculatorPriv(HelpCalcVariable)  "\
        ESCALAR AND VECTOR CALCULATOR

The operations are based on Reversed-polish
notation.

Valid format for vectors:

2.3,67,9e-5
45,-67.3
12.3 45.8 12

Pressing middle mouse button over the stack,
a scalar or a vector can be inserted in
register x:.

left mouse button over one register: selects
it to paste in another place.

right or middle mouse button over a key:
short help.


LIST OF FUNCTION KEYS

*<- backspace
Deletes last digit when entering one number
shortcuts: Backspace Delete

*CHS Change sign
Changes sign of number in register x:
If entering exponent of a number, changes
its exponent sign
shortcuts: m

*EEX Enter exponent
When entering a number,enter its exponent
if no mantisa entered, it's supposed to be 1.0
shortcuts: e

*ENT Enter
Enter number into register x or
copies register x into register y
shortcuts: Return Enter

*v/m Enter vector component
To enter in vector mode or
change to next component
shortcuts: comma space

*SQRT Square root
Argument must be scalar and > 0.0
shortcuts: r

*PI number PI
Enter number PI into register x:
shortcuts: p

*sin sinus
Argument must be scalar
Change units with DRG
if INV is set: arcsin
argument scalar -1.0<=a<=1.0
shortcuts: s

*cos cosinus
Argument must be scalar
Change units with DRG
if INV is set: arccos
argument scalar -1.0<=a<=1.0
shortcuts: c

*tan tangent
Argument must be scalar
Change units with DRG
if INV is set: arctan
argument scalar
shortcuts: t

*CLS Drop register x:
shortcuts: l

*DRG Change degree units
Pressing it goes to:
rad   radians
deg   degrees
gra   Centesimal degrees
shortcuts: d

*INV inverse of functions
Set or unset inverse. Functions that have
one inverse will use it.
example: sin converts to arcsin
see every function for details.
shortcuts: i

*STO Save register x: in memory position
Use RCL to recover.
No shortcuts

*RCL Recovers number saved in memory by
STO to register x:
No shortcuts

*x<>y Swap register x: and y:
No shortcuts

*x^y cross product of vectors
No shortcuts

*Close - Close calculator
shortcuts: Control q

*HELP opens help window
shortcuts: h

*|x| norma of vector
returns the norma of the vector
absolute value of one scalar
no shortcuts

*x1 normalize vector
Converts vector to normalized
no shortcuts

*x<y angle of vectors
Give the angle between 2 vectors
from 0 to 180 degrees in current
angle unit
no shortcuts

*ENG Change display of numbers
If it is activated, small and big
numbers are displayed with
scientific style
shortcuts: g

*x^y cross product
Cross product of 2 vectors
no shortcuts

*Move transfer info with the rest of
the program.
-Get point: one point from the program
is selected and pushed in stack.
shortcuts: Control-a
-Get distance: Distance between 2 points
is selected and pushed in stack.
shortcuts: Control-d
-Write: Value in x register is transferred
to input line in main program.
shortcuts: Control-w
"


#----------------------CALCULATOR ALONE---------------------------
if { $CalcRunningAlone == 1 } {
    CalculatorV .wcalc $argv
}
#-----------------------------------------------------------------
