



proc aie_calc_panel {cmd dtype var {dez_cmd ""}} {
   set w .calc_$dtype
   if {$cmd == "show"} {
      switch -- $dtype {
         scalar {set C S}
         vector {set C V}
         default {return ""}
      }

      set ent [lindex [$dez_cmd path v3Def${C}VarE] 1]
      set x [expr [winfo rootx $ent] + [winfo width $ent] + 30]
      set y [winfo rooty $ent]

      set d [form_init $w "Function calculator panel" "" dismiss]
      if {$d != ""} {
         calculator_panel $d $ent $var
         form_finish +${x}+${y}
      } else {
         wm geometry .calc_$dtype +${x}+${y}
      }
   } elseif {$cmd == "dismiss"} {
      if [winfo exist $w] {wm withdraw $w}
   }
}

proc onv3DefSVarDEZLoad {} {
   aie_calc_panel show scalar user_func v3DefSVarDEZ
}

proc onv3DefSVarDEZApply {} {
   aie_v3p_defvar DefSVarDEZ sv_apply 
}


proc onv3DefSVarDEZCancel {} {
    aie_calc_panel dismiss scalar user_func
}

proc onv3DefVVarDEZLoad {} {
   aie_calc_panel show vector user_func v3DefVVarDEZ
}

proc onv3DefVVarDEZApply {} {
   aie_v3p_defvar v3DefVVarDEZ vv_apply
}


proc onv3DefVVarDEZCancel {} {
    aie_calc_panel dismiss vector user_func
}

proc aie_v3p_defvar {dez_cmd what args} {
   
   switch -- $what {
      sv_apply {
         set vn [$dez_cmd get v3SVTitle]
         set ep [$dez_cmd get v3SVEval]
         set num [V3_define_variable $vn scalar $ep \
                                user [$dez_cmd get v3SVAct]]
         if ![string is integer -strict $num] {
             mess "Failed to define scalar $vn by \"$ep\"\n" red
         }
      }
      vv_apply {
         set vn [$dez_cmd get v3VVTitle]
         set ep [$dez_cmd get v3VVEval]
         set num [V3_define_variable $vn vector $ep \
                              user   [$dez_cmd get v3VVAct]]
         if ![string is integer -strict $num] {
            mess "Failed to define vector $vn by \"$ep\"\n" red
         }
      }
   }
}


proc onv3VarsDEZLoad {} {
   aie_v3p_variables update_cur
}

proc onv3VarsDEZApply {} {
   aie_v3p_variables apply_cur
}

proc onV3SVarMenuModify {args} {
   set vn [lindex $args 1]
 
   update_gvars_form select 1 [aie_v3p_variables get_cur v3pSVarCur] $vn
   update idletasks

   global var_tab
   aie_v3p_variables set_cur var_tab:svmin $var_tab(svmin)
   aie_v3p_variables set_cur var_tab:svmax $var_tab(svmax)
}

proc onV3VVarMenuModify {args} {
   set vn [lindex $args 1]

   update_gvars_form select 2 [aie_v3p_variables get_cur v3pVVarCur] $vn
   update idletasks

   global var_tab
   aie_v3p_variables set_cur var_tab:vec_max $var_tab(vec_max)
}

proc aie_v3p_variables {what args} {
   global var_tab

   set dez_cmd $var_tab(aie_dez)
  # ------ v3VarsDEZ or v3VarsGDEZ

   switch -- $what {
      set_cur {
         $dez_cmd set [lindex $args 0] [lindex $args 1]
      }
      get_cur {
         return [$dez_cmd get [lindex $args 0]]
      }
      update_cur {
         $dez_cmd set v3pSVarList [V3_variables_get sv_namelist]
         $dez_cmd set v3pSVarCur  [V3_variables_get current_s_vn]
         $dez_cmd set v3pVVarList [V3_variables_get vv_namelist]
         $dez_cmd set v3pVVarCur  [V3_variables_get current_v_vn]
        
         foreach s {svmin svmax vec_max} {
            if [info exist var_tab($s)] {
               $dez_cmd set var_tab:$s $var_tab($s)
            }
         }
      }
      apply_cur {
         set did 0
         foreach s {svmin svmax} {
            set val [$dez_cmd get var_tab:$s]
            if {$var_tab($s) != $val} {incr did; set var_tab($s) $val}
         }
         if {$did} {update_gvars_form set_sv_minmax 1}

         set val [$dez_cmd get var_tab:vec_max]
         if {$val != $var_tab(vec_max)} {
            set var_tab(vec_max) $val
            update idletasks
            update_gvars_form set_vec_max 2
         }
      }
   }
}

proc onv3VarsGDEZLoad {} {
   variable v3VarsEXGVal
   if [info exist v3VarsEXGVal] {
      set val [v3VarsGDEZ get v3VarsEXGVal]
   } else {
      set val -1
   }

   if {$val < 0} {set val 0; v3VarsGDEZ set v3VarsEXGVal 0}
   switch -- $val {
      0 { aie_v3p_variables update_cur }
      1 { aie_calc_panel show scalar user_func v3VarsGDEZ}
      2 { aie_calc_panel show vector user_func v3VarsGDEZ}
   }
}

proc onv3VarsGDEZApply {} {
   set val [v3VarsGDEZ get v3VarsEXGVal]
   switch -- $val {
      0 { aie_v3p_variables apply_cur }
      1 { aie_v3p_defvar v3VarsGDEZ sv_apply }
      2 { aie_v3p_defvar v3VarsGDEZ vv_apply }
   }
}

proc onv3VarsGDEZCancel {} {
   aie_calc_panel dismiss scalar user_func
   aie_calc_panel dismiss vector user_func
}

proc onv3SelectVarsLoad {} {
   aie_calc_panel dismiss scalar user_func
   aie_calc_panel dismiss vector user_func
   aie_v3p_variables update_cur
}

proc onv3DefSVarLoad {} {
   aie_calc_panel dismiss vector user_func
   aie_calc_panel show scalar user_func v3VarsGDEZ
}

proc onv3DefVVarLoad {} {
   aie_calc_panel dismiss scalar user_func
   aie_calc_panel show vector user_func v3VarsGDEZ
}



proc onV3TransTimeStepModify {args} {
   set ts [lindex $args 1]

   set i [lsearch $::transient_info(steptimes) $ts]
   if {$i > -1} {
      set ::transient_info(step) $i
      V3_transient_cmd stepin $i
   }
}


proc onV3TransTimeIDEZLoad {} {
   global transient_info

   set transient_info(steps_list) [V3_transient_get steps_list]
   set transient_info(steptimes)  [V3_transient_get steptimes]
}

proc V3TransTimePDEZLoad {} {
}



proc aie_v3p_update_timesteps {what args} {
   global transient_info

   switch -- $what {
      init {
         set transient_info(pause) [V3_transient_cmd init]
         set transient_info(steps_list) [V3_transient_get steps_list]
         set transient_info(steptimes)  [V3_transient_get steptimes]

         set i_or_p I
         if {[V3_transient_get list] == ""} { set i_or_p P }
         set transient_info(i_or_p) $i_or_p

         global animate_data
         set animate_data(transient) 1

         v3TransTime${i_or_p}DEZ show
      }
      states {
         V3_transient_cmd states $transient_info(pause)
      }
      stepin {
         V3_transient_cmd stepin $transient_info(step)
      }
      show {
         if [info exist transient_info(i_or_p)] {
            set i_or_p $transient_info(i_or_p)
            v3TransTime${i_or_p}DEZ show
         }
      }
      tmlabel {
         global dset_info anno_data

         if {[info exist transient_info(tmlabel)] && \
                        ($transient_info(tmlabel) == 1)} {
            foreach {n w} [array get dset_info *,gwin] {
               set anno_data(tmlabel_on,$w) 1
            }
         } else {
            foreach {n w} [array get dset_info *,gwin] {
               set gm $anno_data(gm,0,$w)
               if {[info exist transient_info(tmlabel,$w)] && \
                    ([$gm isvalid $transient_info(tmlabel,$w)] == 1)} {
                  $gm remove $transient_info(tmlabel,$w)
                  unset transient_info(tmlabel,$w)
               }
               if [info exist anno_data(tmlabel_on,$w)] {
                  unset anno_data(tmlabel_on,$w)
               }
            }
         }
      }
   }
}

proc aie_v3p_compare {what args} {
   global dset_info opt_data surf_info stat_info

   if {[info command aie_diff_start] == ""} {return ""}

   switch -- $what {
      after {
         set w [lindex $args 0]
         #V3_surface_group update_selected $dset_info(diff,surfroot)

         SfgTree::selecttree 0 $surf_info(wf,tree) $dset_info(diff,surfroot)

if 0 {
         set rr [V3_surface_var_get varinfo s]
         if {$rr != ""} {
            foreach {n v} $rr {set $n $v}
            if {[info exist smin] && [info exist smax]} {
               global cmap_info
               set cmap_info(cmap) $dset_info($w,cmap)
               set rr [V3_colormap cmap $cmap_info(cmap)]
               if {$rr != ""} {
                  foreach i {0 1 2 3} n {minv maxv nentries varname} {
                     set cmap_info($n) [lindex $rr $i]
                  }
               }
               V3_colormap resetval $smin $smax $cmap_info(nentries)
            } 
         }
     }
         global v3p_vmax
         if {[info exist v3p_vmax] && [string is double -strict $v3p_vmax]} {
            $w set_gvsize $v3p_vmax
         }
         tdv_maxview $w 
      }
      setup {
          aie_diff_start Compare

          set cmap ""
          if [info exist dset_info(diff,win)] {
             set w $dset_info(diff,win)
             if [winfo exist $w] {
                set stat_info(add_after_cmd) "aie_v3p_compare after $w"
                if [info exist dset_info($w,cmap)] {
                   set cmap $dset_info($w,cmap)
                }
             }

             V3_surface_group set_gwin $w
          }
          return $cmap
      }
      dynamic {
          set cmap [aie_v3p_compare setup]

          set obj ""
          if [info exist dset_info(diff,dynamic)] {
             set obj $dset_info(diff,dynamic)
          }
          set rr [V3_diff_surface dynamic $obj 1 2 $cmap]
          set dset_info(diff,dynamic) [lindex $rr 1]
          set dset_info(diff,dynamic,title) [lindex $rr 0]

        # dvalid_draw_errball 

        # set stat_info(add_after_cmd) ""
      }
      specific {
          set cmap [aie_v3p_compare setup]

          V3_diff_surface specific "" [lindex $args 0] \
                        [lindex $args 1] $cmap
      }
      domain {
          set sfname [lindex $args 0]
          if {$sfname == ""} {
             set c1 $surf_info(selected)
             if {![info exist surf_info($c1,node)] || \
                 ![info exist surf_info($c1,cid)]} {
                mess "Surface $c1 is not a domain surface\n"
                return ""
             }
             set sfname [lindex [SfgTree::from_nodes \
                       $surf_info(wf,tree) $surf_info($c1,node)] 1]
          } else {
             set c1 [lindex [V3_surface_group_get "" from_surfpath $sfname] 0]
          }

          set cmap [aie_v3p_compare setup]

          set cid [expr 2 - $surf_info($c1,cid)]
        #  ------ we need to do something more if cid <= 0

          set rr [V3_diff_surface domain "" $sfname $cid $cmap]
        # set stat_info(add_after_cmd) ""
      }
   }
}


proc onv3CompareDEZLoad {} {
   variable skipinterp
   if ![string is integer -strict $skipinterp] {
      set skipinterp 1
   }
   foreach v {sf1name sf2name boxPt0 boxPt1 boxPt2 boxPt3} {
      variable $v
      set $v ""
   }
}


proc onv3CompareDEZApply {} {
   global surf_info dset_info
   set gwin $surf_info(default_viewer)

   set type [::AIEnv::GUIManagement::ExclusiveGroup::GetCurrentGroupName v3Compare]
   switch -- $type {
      v3CompareTwoSurf {
         variable skipinterp
         variable sf1name
         variable sf2name

         if {$sf1name == ""} {
            aie_v3p_compare domain $sf2name
         } elseif {$sf2name == ""} {
            aie_v3p_compare domain $sf1name
         } else { 
            if {$skipinterp} {
               aie_v3p_compare specific $sf1name $sf2name
            } else {
               aie_v3p_compare domain $sf1name
            }
         }
      }
      v3Compare3DBox {
         foreach p {boxPt0 boxPt1 boxPt2 boxPt3} {
            variable $p
            set val [set $p]
            if {![string is double -strict [lindex $val 0]]  || \
                ![string is double -strict [lindex $val 1]]  || \
                ![string is double -strict [lindex $val 2]]} {
               mess "Invalid coords for $p\n" red
               return ""
            }
         }
         foreach n {boxPtNum1 boxPtNum2 boxPtNum3} {
            variable $n
            set val [set $n]
            if {![string is integer -strict $val] || ($val < 1)} {
               set $n 1
            }
         }

         variable v3Compare3DBoxName

         set cmap [aie_v3p_compare setup]

         V3_compare_3dpts $v3Compare3DBoxName \
                          $boxPt0 $boxPt1 $boxPt2 $boxPt3 \
                          $boxPtNum1 $boxPtNum2 $boxPtNum3 $cmap

        #V3_compare_3dbox $v3Compare3DBoxName \
        #                 $boxPt0 $boxPt1 $boxPt2 $boxPt3 \
        #                 $boxPtNum1 $boxPtNum2 $boxPtNum3 $cmap
      }
      v3CompareDynSurf {
         aie_v3p_compare dynamic
      }
   }

   V3_surface_group set_gwin $gwin
}
