

proc onv3ProbePtDEZOK {} {
   record_probe_points end
}


proc onv3ProbePtDEZLoad {} {
   foreach v {sfname x y z p id sv vx vy vz vertices wts values} {
      variable probe_data:$v; set probe_data:$v ""
   }

   global probe_data
   if {![info exist ::probe_data(record)] || \
       ![string is integer -strict $::probe_data(record)]} {
      set ::probe_data(record) 0
   }
   if ![info exist ::probe_data(file)] {set ::probe_data(file) ""}

   [v3ProbePtDEZ path v3ProbePtRc] actframe
}

proc onv3ProbePtDEZCancel {} {
   global probe_data

   if {[info exist probe_data(doing)] && ($probe_data(doing) == 1)} {
      record_probe_points end
      set probe_data(doing) 0
   }
   update idletasks
   if [info exist probe_data(last_win)] {
      anno_reset_mouse_binding $probe_data(last_win)
      unset probe_data(last_win)
   }
}
 
proc aie_surf_probe_file {args} {
   global probe_data

   if {$probe_data(record)} {
      set cc 1
      if {($probe_data(file) == "") || \
          ![file writable [file dirname $probe_data(file)]]} {
         set cc 0
      }

      while {!$cc} {
         set probe_data(file) [file_selection -message \
                     "Probe Data file" -new_ok 1]
         if {$probe_data(file) == ""} {
            set probe_data(record) 0
            [v3ProbePtDEZ path v3ProbePtRc] actframe
            return
         }
         set cc [file writable [file dirname $probe_data(file)]]
         if {!$cc} {mess "File $probe_data(file) is not writable\n" red}
      }
   }
}


proc aie_v3p_surfppts {what args} {
   global surf_info

   if ![info exist surf_info(selected,$what)] {return ""}

   set val [v3SurfacePropertyDEZ get surf_info:$what]
   if {$surf_info(selected,$what) != $val} {
      set surf_info(selected,$what) $val
      update idletasks
      update_all_surfaces_selected $what
   }
}


proc aie_v3surfgrp_opts {what args} {
   global surf_info

   switch -- $what {
      set_featurelines {
         V3_surface_cmd selected featurelines
      }
      remove_featurelines {
         V3_surface_cmd selected featurelines remove
      }
      shading {
         V3_surface_cmd selected shading [lindex $args 0]
      }
      set_clip_contours {
         V3_surface_cmd selected cut_contourhigh 1
         V3_surface_cmd selected cut_contourlow 1
      }
      remove_clip_contours {
         V3_surface_cmd selected cut_contourhigh 0
         V3_surface_cmd selected cut_contourlow 0
      }
      unidir_polys {
         V3_surface_cmd selected unidir_polys
      }
      interpolate_vars {
         V3_surface_group client_id
         V3_surface_cmd selected interpolate_vars
      }
      transp_mode {
         set val $surf_info(wf,transp_mode)
         V3_set_option transp_mode $val
      }
   }
}

proc onv3SurfacePropertyDEZApply {} {
   global surf_info
   foreach n {transp contourline_width grid_width edge_width \
              vec_size arrow_size v2d_size dots_size ntufts} {
      variable surf_info:$n
      if {$surf_info(selected,$n) != [set surf_info:$n]} {
         set surf_info(selected,$n) [set surf_info:$n]
         update idletasks
         update_all_surfaces_selected $n
      }
   }
}


proc onv3SurfacePropertyDEZLoad {} {
   global surf_info
   foreach n {transp contourline_width grid_width edge_width \
              vec_size arrow_size v2d_size dots_size ntufts} {
      variable surf_info:$n; set surf_info:$n $surf_info(selected,$n)
   }

   [v3SurfacePropertyDEZ path v3SurfaceTuft] actframe
}


proc aie_v3p_surfvars {what c args} {
   global surf_info sfvars_data

   set C [string toupper $c]

   switch -- $what {
      save_v3pcurrent {
      }
      global {
         if {[info command v3Surf${C}VarDEZ] != ""} {
            set fm [v3Surf${C}VarDEZ path v3Surf${C}VarL]
            if {$fm != ""} {$fm actframe}
         }
         if {[info command v3SurfVarSVDEZ] != ""} {
            set fm [v3SurfVarSVDEZ path v3SurfVar${C}L]
            if {$fm != ""} {$fm actframe}
         }

         update_surf_vars_selected set_v3pslot $c
      }
      local {
         if {[info command v3Surf${C}VarDEZ] != ""} {
            [v3Surf${C}VarDEZ path v3Surf${C}VarG] actframe
         }
         if {[info command v3SurfVarSVDEZ] != ""} {
            [v3SurfVarSVDEZ path v3SurfVar${C}G] actframe
         }
 
         update_surf_vars_selected set_var $c sfvars_data(l${c}n)
      }
   }
}

proc onv3SurfVarSVDEZLoad {} {
   global sfvars_data

   update_surf_vars_selected data s

   variable v3pSfSVList
   set v3pSfSVList $sfvars_data(svar_list)
   if {$v3pSfSVList == ""} {
      ::AIEnv::GUIManagement::ToggleGrey v3SurfVarSL 1
   } else {
      ::AIEnv::GUIManagement::ToggleGrey v3SurfVarSL 0
   }

   variable v3pSVarList
   set v3pSVarList [V3_variables_get sv_namelist]
   if {$v3pSVarList == ""} {
      ::AIEnv::GUIManagement::ToggleGrey v3SurfVarSG 1
   } else {
      ::AIEnv::GUIManagement::ToggleGrey v3SurfVarSG 0
   }

   update_surf_vars_selected data v

   variable v3pSfVVList
   set v3pSfVVList $sfvars_data(vvar_list)
   if {$v3pSfVVList == ""} {
      ::AIEnv::GUIManagement::ToggleGrey v3SurfVarVL 1
   } else {
      ::AIEnv::GUIManagement::ToggleGrey v3SurfVarVL 0
   }

   variable v3pVVarList
   set v3pVVarList [V3_variables_get vv_namelist]
   if {$v3pVVarList == ""} {
      ::AIEnv::GUIManagement::ToggleGrey v3SurfVarVG 1
   } else {
      ::AIEnv::GUIManagement::ToggleGrey v3SurfVarVG 0
   }
}

proc onV3SurfVarSGNModify {args} {
   global sfvars_data

   update_surf_vars_form set_v3pslot 1 s sfvars_data(gsn)
}

proc onV3SurfVarSLNModify {args} {
   global sfvars_data

   update_surf_vars_form set_var s sfvars_data(lsn)
}

proc onV3SurfVarVGNModify {args} {
   global sfvars_data

   update_surf_vars_form set_v3pslot 1 v sfvars_data(gvn)
}

proc onV3SurfVarVLNModify {args} {
   global sfvars_data

   update_surf_vars_form set_var v sfvars_data(lvn)
}

proc onv3SurfSVarDEZLoad {} {
   global sfvars_data

   update_surf_vars_selected data s

   variable v3pSfSVList
   set v3pSfSVList $sfvars_data(svar_list)
   if {$v3pSfSVList == ""} {
      set sfvars_data(sop) 0
      ::AIEnv::GUIManagement::ToggleGrey v3SurfSVarL 1
   } else {
      ::AIEnv::GUIManagement::ToggleGrey v3SurfSVarL 0    
   }

   variable v3pSVarList
   set v3pSVarList [V3_variables_get sv_namelist]
   if {$v3pSVarList == ""} {
      ::AIEnv::GUIManagement::ToggleGrey v3SurfSVarG 1
   } else {
      ::AIEnv::GUIManagement::ToggleGrey v3SurfSVarG 0
   } 
}

proc onV3SurfSVarGNModify {args} {
   global sfvars_data

   update_surf_vars_form set_v3pslot 1 s sfvars_data(gsn)
}

proc onV3SurfSVarLNModify {args} {
   global sfvars_data

   update_surf_vars_form set_var s sfvars_data(lsn)
}


proc onv3SurfVVarDEZLoad {} {
   global sfvars_data

   update_surf_vars_selected data v

   variable v3pSfVVList
   set v3pSfVVList $sfvars_data(vvar_list)
   if {$v3pSfVVList == ""} {
      ::AIEnv::GUIManagement::ToggleGrey v3SurfVVarL 1
   } else {
      ::AIEnv::GUIManagement::ToggleGrey v3SurfVVarL 0
   }

   variable v3pVVarList
   set v3pVVarList [V3_variables_get vv_namelist]
   if {$v3pVVarList == ""} {
      ::AIEnv::GUIManagement::ToggleGrey v3SurfVVarG 1
   } else {
      ::AIEnv::GUIManagement::ToggleGrey v3SurfVVarG 0
   }
}

proc onV3SurfVVarGNModify {args} {
   global sfvars_data

   update_surf_vars_form set_v3pslot 1 v sfvars_data(gvn)
}

proc onV3SurfVVarLNModify {args} {
   global sfvars_data

   update_surf_vars_form set_var v sfvars_data(lvn)
}



proc aie_update_integral {args} {
   global integrate_data

   V3_surface_integral $integrate_data(flux_how) $integrate_data(flux_dir)

   set integrate_data(total_flow) [list $integrate_data(total_flowx) \
             $integrate_data(total_flowy) $integrate_data(total_flowz)]
   set integrate_data(avg_flow) [list $integrate_data(avg_flowx) \
             $integrate_data(avg_flowy) $integrate_data(avg_flowz)]
}


proc onv3SurfIntegalDEZLoad {} {
   global surf_info integrate_data
   if {$surf_info(selected) != ""} { aie_update_integral }
}


proc aie_update_surface_flow {what args} {
   global surf_info sfstrm_data

   set which $surf_info(selected)
   if {[V3_misc_get info commands $which] == ""} {return ""}

   switch -- $what {
      data  {
         V3_surface_flow get_info selected
      }
      vec_away {
         V3_surface_group client_id
         V3_surface_cmd selected set_interpolate_away $sfstrm_data(vec_away)
         V3_surface_cmd selected interpolate_vars
      }
      lic {
         set sfstrm_data(tex_draw) [expr $sfstrm_data(lic)]
         if {$sfstrm_data(tex_single)} {
            set sfstrm_data(tex_maxnum) 1
         } else {
            set sfstrm_data(tex_maxnum) 100
         }
         V3_surface_flow lic selected  $sfstrm_data(tex_draw) \
                        -tex_msize $sfstrm_data(tex_msize) \
                        -tex_maxnum $sfstrm_data(tex_maxnum) \
                        -coloring $sfstrm_data(lic_coloring) \
                        -shading  $sfstrm_data(lic_shading)
         ::AIEnv::GUIManagement::ToggleGrey v3SurfFlowPmSize $sfstrm_data(lic)
         ::AIEnv::GUIManagement::ToggleGrey v3SurfFlowPmNum $sfstrm_data(lic)
         ::AIEnv::GUIManagement::ToggleGrey v3SurfFlowEnh  \
                                          [expr !$sfstrm_data(lic)]
      }
      lines {
         V3_surface_flow lines selected $sfstrm_data(draw_surfstrm)
      }
      enhance_lic {
         V3_surface_flow enhance_lic $sfstrm_data(nenhanced)
      }
      lic_coloring - lic_shading - line_color {
         V3_surface_flow $what selected $sfstrm_data($what)
      }
   }
   return $which
}

proc onv3SurfFlowDEZLoad {} {
   global sfstrm_data

   if {[aie_update_surface_flow data] == ""} {
      foreach n {v3SurfFlines v3SurfFlinesCol \
                 v3SurfFlow v3SurfFlowPm v3SurfFlowEnh} {
         ::AIEnv::GUIManagement::ToggleGrey $n 1
      }
   } else {
      foreach n {v3SurfFlines v3SurfFlinesCol v3SurfFlow v3SurfFlowPmCol} {
         ::AIEnv::GUIManagement::ToggleGrey $n 0
      }
      ::AIEnv::GUIManagement::ToggleGrey v3SurfFlowPmSize $sfstrm_data(lic)
      ::AIEnv::GUIManagement::ToggleGrey v3SurfFlowPmNum $sfstrm_data(lic)
      ::AIEnv::GUIManagement::ToggleGrey v3SurfFlowEnh [expr !$sfstrm_data(lic)]
   }
   if {$sfstrm_data(tex_maxnum) > 1} {
      set sfstrm_data(tex_single) 0
   } else {
      set sfstrm_data(tex_single) 1
   }
}



proc onv3ImportSfDEZLoad {} {
   global defuv_info surf_info v3p_states

   if ![info exist v3p_states(preview_surf)] {
      ::AIEnv::GUIManagement::ToggleGrey v3ExtSfStopPre 1
   }
   global dset_info
   if {![info exist dset_info(nclients)] || ($dset_info(nclients) < 2)} {
      ::AIEnv::GUIManagement::ToggleGrey v3ExtSfInAll 1
   } else {
      ::AIEnv::GUIManagement::ToggleGrey v3ExtSfInAll 0
   }
}

proc onv3ImportSfDEZApply {} {
   global surf_info dset_info

   if {$::surf_info(wf,in_all)} {
      set dset $dset_info(1,v3dset)
      SfgTree::selecttree 0 $surf_info(wf,tree) $surf_info($dset,sg_root) $dset
      V3_surface_group client_id
   }
 
   set type [::AIEnv::GUIManagement::ExclusiveGroup::GetCurrentGroupName v3ImportSf]

   switch -- $type {
      v3ParamSfSph { 
         set surf_info(wf,interp_params) $surf_info(wf,interp_tetin)
         update_surface_paramsf sphere 
         update_surface_paramsf accept
      }
      v3ParamSfCyl {
         set surf_info(wf,interp_params) $surf_info(wf,interp_tetin)
         update_surface_paramsf cylinder
         update_surface_paramsf accept
      }
      v3ParamSfQuad {
         set surf_info(wf,interp_params) $surf_info(wf,interp_tetin)
         update_surface_paramsf quad
         update_surface_paramsf accept
      }
      v3ParamSfCone {
         set surf_info(wf,interp_params) $surf_info(wf,interp_tetin)
         update_surface_paramsf cone
         update_surface_paramsf accept
      }
      v3ParamSfEq {
         set surf_info(wf,interp_params) $surf_info(wf,interp_tetin)
         update_surface_paramsf accept
      }
      v3TetinSf {
         global tetin_info
         set t [::AIEnv::GUIManagement::ExclusiveGroup::GetCurrentGroupName v3TetinSfSelect]
         switch $t {
            v3TetinSfAll {
               update_surface_tetin mkall
            }
            v3TetinSfFam {
               set sf [v3ImportSfDEZ get v3TetinSfgroup]
               if {$sf != ""} {
                  set tetin_info(group) $sf
                  update_surface_tetin mkgrp
               }
            }
            v3TetinSfSel {
               foreach sf $tetin_info(selected) {
                  set tetin_info(surfname) $sf
                  update_surface_tetin mksurf
               }
            }
            v3TetinSfReset {
               # shouldn't be here
            }
         }
      }
      v3LoadPtsSf {
         set file [v3ImportSfDEZ get v3LoadPtsSfFileVar]
         if ![file readable $file] {set file ""}

         load_surface_cmd 3dpts $file

         v3ImportSfDEZ set v3LoadPtsSfFileVar ""
      }
      v3LoadDomainSf {
         set file [v3ImportSfDEZ get v3LoadDomainSfFileVar]
         if ![file readable $file] {set file ""}

         load_surface_cmd domain $file

         v3ImportSfDEZ set v3LoadDomainSfFileVar ""
      }
   }
}

proc onv3ParamSfSphLoad {} {
   set ::defuv_info(preset) sphere
   set ::defuv_info(name) Sphere
   ::AIEnv::GUIManagement::ToggleGrey v3ExtSfPreview 0
}

proc onv3ParamSfCylLoad {} {
   set ::defuv_info(preset) cylinder
   set ::defuv_info(name) Cylinder
   ::AIEnv::GUIManagement::ToggleGrey v3ExtSfPreview 0
}

proc onv3ParamSfQuadLoad {} {
   set ::defuv_info(preset) quad
   set ::defuv_info(name) Quad
   ::AIEnv::GUIManagement::ToggleGrey v3ExtSfPreview 0
}

proc onv3ParamSfConeLoad {} {
   set ::defuv_info(preset) cone
   set ::defuv_info(name) Cone
   ::AIEnv::GUIManagement::ToggleGrey v3ExtSfPreview 0
}

proc onv3ParamSfEqLoad {} {
   set ::defuv_info(preset) equation
   set ::defuv_info(name) Param
   ::AIEnv::GUIManagement::ToggleGrey v3ExtSfPreview 0
}

proc onv3TetinSfLoad {} {
   ::AIEnv::GUIManagement::ToggleGrey v3ExtSfPreview 1

   if {[V3_surface_tetin_get grouplist] == ""} {
      aie_v3p_tetin_cmd load
   }
}

proc onv3TetinSfFamLoad {} {
   variable v3TetinSflsgrp
   set v3TetinSflsgrp [V3_surface_tetin_get grouplist]
}

proc onv3TetinSfSelLoad {} {
   global tetin_info
   set tetin_info(selected) ""
   update idletasks

   aie_v3p_tetin_cmd select
}

proc onv3TetinSfResetLoad {} {
   if {[V3_surface_tetin_get grouplist] != ""} {
      set r [tk_messageBox -title "Confirm to clean" \
               -message "Confirm to clean the current geometry?" \
               -type yesno]
      if {$r == "no"} {
         v3ImportSfDEZ set V3TetinSelect -1
         return ""
      }

      V3_surface_tetin clean

      v3ImportSfDEZ set v3TetinSflsgrp ""
      v3ImportSfDEZ set v3TetinSflsgrp ""
   }
   aie_v3p_tetin_cmd load
   v3ImportSfDEZ set V3TetinSelect -1
} 

proc aie_v3p_tetin_cmd {what args} {
   global tetin_info file_name
   switch -- $what {
      load {
         set ft $file_name(tetin)
         if ![file isfile $ft] {
            set ft [tk_getOpenFile -initialdir $file_name(project_dir)  \
                  -filetypes {{{Geometry Files} {.tin}} {{All Files} *}} \
                  -initialfile [file tail $ft] -multiple 1]
           if {$ft == ""} return
         }
         set tetin_info(file) $ft

         update_surface_tetin load
      }
      select {
      }
   }
}

proc onv3LoadPtsSfLoad {} {
   ::AIEnv::GUIManagement::ToggleGrey v3ExtSfPreview 1
}

proc onv3LoadDomainSfLoad {} {
   ::AIEnv::GUIManagement::ToggleGrey v3ExtSfPreview 1
}


proc aie_v3p_user_surf {what args} {
   global surf_info defuv_info

   switch -- $what {
      preview {
         update_surface_paramsf preview
         ::AIEnv::GUIManagement::ToggleGrey v3ExtSfStopPre 0
      }
      stop_preview {
         v3p_preview_cmd stop
         ::AIEnv::GUIManagement::ToggleGrey v3ExtSfStopPre 1
      }  
   }
}

proc aie_edit_v3surface {name} {
   global surf_info

   if ![info exist surf_info($name,node)] {return ""}

   set node $surf_info($name,node)
   SfgTree::selecttree 1 $surf_info(wf,tree) $node

   v3SurfacePropertyDEZ show
}

proc aie_edit_v3surfgrp {name} {
   global surf_info

   if ![info exist surf_info($name,node)] {return ""}

   set node $surf_info($name,node)
   SfgTree::selecttree 1 $surf_info(wf,tree) $node

   v3SurfacePropertyDEZ show
}

proc aie_edit_v3surfgrp_root {name} {
   global surf_info

   if ![info exist surf_info($name,node)] {return ""}

   set node $surf_info($name,node)
   SfgTree::selecttree 1 $surf_info(wf,tree) $node

   v3SurfacePropertyDEZ show
}


proc aie_toggle_v3surfgrp_root {theTree theNode} {
   SfgTree::toggle $theTree $theNode


   global surf_info dset_info
   set dset [aie_set_v3surfgrp_v3dset $theTree $theNode]
   if {[info exist dset_info($dset,gwin)] && \
       [winfo exist $dset_info($dset,gwin)]} {
      $dset_info($dset,gwin) redraw
   }
}

proc aie_toggle_v3surfgrp {theTree theNode} {
   SfgTree::toggle $theTree $theNode

   global surf_info dset_info
   set dset [aie_set_v3surfgrp_v3dset $theTree $theNode]
   if {[info exist dset_info($dset,gwin)] && \
       [winfo exist $dset_info($dset,gwin)]} {
      $dset_info($dset,gwin) redraw
   }
}

proc aie_toggle_v3surface {theTree theNode} {
   SfgTree::toggle $theTree $theNode

   global surf_info dset_info
   set dset [aie_set_v3surfgrp_v3dset $theTree $theNode]
   if {[info exist dset_info($dset,gwin)] && \
       [winfo exist $dset_info($dset,gwin)]} {
      $dset_info($dset,gwin) redraw
   }
}


proc aie_set_v3surfgrp_v3dset {theTree theNode} {
   global surf_info dset_info

   while {[set p [$theTree parent $theNode]] != "root"} {set theNode $p}

   return $theNode
}


proc aie_v3surfgrp_menu {mm theTree theNode} {
   set dset [aie_set_v3surfgrp_v3dset $theTree $theNode]

   SfgTree::plotmenu $mm $theTree $theNode

   $mm add separator

   $mm add command -label "Save in Domain Format" \
         -command "aie_v3surfgrp_tree save $theNode"
   $mm add command -label "Remove Permanently" \
         -command "aie_v3surfgrp_tree remove $theNode"
}

proc aie_v3surfgrp_root_menu {mm theTree theNode} {
   set dset [aie_set_v3surfgrp_v3dset $theTree $theNode]

   SfgTree::plotmenu $mm $theTree $theNode

   $mm add separator

   $mm add command -label "Save in Domain Format" \
         -command "aie_v3surfgrp_tree save $theNode"
   $mm add command -label "Remove Permanently" \
         -command "aie_v3surfgrp_tree remove $theNode"

   global dset_info
   if {$dset_info(nclients) > 1} {
      $mm add command -label "Import from Other DataSet" \
        -command "aie_v3surfgrp_tree import $theNode $dset"
   }
}

proc aie_v3surfgrp_cycle_menu {mm theTree theNode} {
   set dset [aie_set_v3surfgrp_v3dset $theTree $theNode]

   SfgTree::plotmenu $mm $theTree $theNode
}

proc aie_v3surface_menu {mm theTree theNode} {
   set dset [aie_set_v3surfgrp_v3dset $theTree $theNode]

   SfgTree::plotmenu $mm $theTree $theNode

   $mm add separator

   $mm add command -label "Save in Domain Format" \
         -command "aie_v3surfgrp_tree save $theNode"
   $mm add command -label "Remove Permanently" \
         -command "aie_v3surfgrp_tree remove $theNode"

   global dset_info
   if {$dset_info(nclients) > 1} {
      $mm add command -label "Export to Other DataSet" \
        -command "aie_v3surfgrp_tree export $theNode"
   }
}


proc aie_v3surfgrp_tree {what node args} {
   global surf_info dset_info

   set top_tree [set ::AIEnv::GUIManagement::Tree::aieTree]

   switch -- $what {
      delete {
      }
      remove {
         set nn ""; set dd ""; set dyns [V3_dynamic_surface_get list]
         foreach n $node {
            lappend nn [$top_tree itemcget $n -text]
            set id [lindex [$top_tree itemcget $n -data] 1]
            if {[lsearch $dyns $id] >= 0} {lappend dd $id}
         }
         set mm "Permanently remove $nn ?"
         if {$dd != ""} {
            set mm "$mm\n Dynamic Surface is needed to activite a dyanmic surface."
         }
         set ret [tk_dialog .diag_surfgrp_tree "Confirm" $mm "" 0 Apply Cancel]
         if {$ret} {return ""}

         foreach n $node {
            set id [lindex [$top_tree itemcget $n -data] 1]
            V3_surface_group remove_group $id
         } 
      }
      save {
         write_domain_surface_cmd selected
      }
      export {
         set surf_info(tobe_exported) ""
         foreach n $node {
            set id [lindex [$top_tree itemcget $n -data] 1]
            eval lappend surf_info(tobe_exported) [V3_surface_get $id count]
         }
      }
      import {
         if {![info exist surf_info(tobe_exported)] || \
               ($surf_info(tobe_exported) == "")} {
            mess "Empty surfaces to be exported from other dataset\n" red
            return ""
         }

         set dset [lindex $args 0]
         if {$dset == ""} { 
            set dset [aie_set_v3surfgrp_v3dset $top_tree $node]
         }
         if ![info exist dset_info($dset,client_id)] {return ""}

         eval V3_surface_group import $dset_info($dset,client_id) \
                     $surf_info(tobe_exported)

         set surf_info(tobe_exported) ""
      }
   }
}
