
proc aie_modify_anno_pick {type args} {
   global anno_data

   set Type [string toupper [string index $type 0]][string range $type 1 end]
   set Name annoModify${Type}DEZ
   set Namespace [::AIEnv::GUIManagement::GetNamespace DEZ $Name]

   set ${Namespace}::_cur_name $Name

   namespace eval $Namespace {
       variable _cur_name
       if {[info command on${_cur_name}Apply] != ""} {
          eval [info body on${_cur_name}Apply]
       }
   }
}


proc aie_anno_cmd {what args} {
   global anno_data tdv_current_viewer

   if {![info exist tdv_current_viewer] || \
                ![winfo exist $tdv_current_viewer]} {
      mess "No current graphic window to operate\n" red
      return ""
   }
      
   set w $tdv_current_viewer

   if {$what == "create"} {
      annotationDEZ show
      annotationDEZ set annotationEXGVal 0
   } else {

      if {![info exist anno_data(gm,0,$w)] || \
          ([info command $anno_data(gm,0,$w)] == "") || \
          ([$anno_data(gm,0,$w) list] == "")} {
         mess "No annotation exist in the current window\n" red
         return ""
      }
      
      switch -- $what {
         modify {do_anno_cmd $w 0 modify}
         move   {do_anno_cmd $w 0 move}
         remove {
            if {[lindex $args 0] == "direct"} {
               return [do_anno_cmd $w 0 delete]
            } elseif {[lindex $args 0] == "need_comfirm"} {
               annotationDEZ show
               annotationDEZ set annotationEXGVal 3
            }

            set anno_data(tobe_deleted) \
                  [do_anno_cmd $w 0 pick_to_delete]
         }
         apply_delete {
            if [info exist anno_data(tobe_deleted)] {
               eval do_anno_cmd $w 0 direct_delete \
                           $anno_data(tobe_deleted)
               unset anno_data(tobe_deleted)
            }
         }
         cancel_delete {
            if [info exist anno_data(tobe_deleted)] {
               eval do_anno_cmd $w 0 cancel_delete \
                               $anno_data(tobe_deleted)
               unset anno_data(tobe_deleted)
            }
         }
         clear  {do_anno_cmd $w 0 clear}
         reset  {anno_reset_mouse_binding $w}
      }
   }
}

proc update_anno_misc_cmd {what args} {
   global anno_data

   switch -- $what {
      raise_def {
         if {[info exist anno_data(no_reload_dez)] \
                && $anno_data(no_reload_dez)} {
            return ""
         }

         if [info exist anno_data(dez_cmd)] {
            set dez_cmd $anno_data(dez_cmd)
         } else {
            set dez_cmd annotationDEZ
         }
         $dez_cmd show
      }
      modify_spec {
         set type [lindex $args 0]
         if {$type == ""} { set type $anno_data(picked_type) }
         set Type [string toupper $type 0 0]

         set Name annoModify${Type}DEZ
         if {[info command $Name] != ""} { $Name show; return "" }

         set Name oned${Type}DEZ
         if {[info command $Name] != ""} { $Name show; return "" }
      }
      create_fm {
         set dez_cmd [lindex $args 0]
         set anno_data(dez_cmd) $dez_cmd

       # $dez_cmd set anno_data_type ""
       # $dez_cmd set anno_data_List [list  Text "Utf Text" \
       #         Mark Line Box Circle Polygon Pixmap "Colormap Bar"]

       # foreach v {angle linew marksize} {
       #     $dez_cmd set anno_data:$v $anno_data($v)
       # }

         global tdv_current_viewer

         set w [lindex $args 1]

       # mess " create_fm $w --- $tdv_current_viewer\n"

         if {![winfo exist $w] || ([winfo class $w] != "Tdv_Viewer")} {
            if [info exist tdv_current_viewer] {
               set w $tdv_current_viewer
            }
         }

         if {[winfo exist $w] && ([winfo class $w] == "Tdv_Viewer")} {
            if {[info exist anno_data(win)] && \
                [winfo exist $anno_data(win)] &&
                ([winfo class $anno_data(win)] == "Tdv_Viewer")} {
               set p $anno_data(win)
               while {$p != $w} {
                  set p [vframe_cmd $p parent]
                  if {$p == ""} {break}
               }
               if {$p == $w} {
                  set w $anno_data(win)
               }
            }
            if ![info exist anno_data(button_states,$w)] {
               anno_init_win $w Current button_states($w) \
                                button_bindings($w) button_events($w)
            }
            if ![info exist anno_data(gm,0,$w)] { anno_init $w 0 "" }
            set anno_data(win) $w
         }
      }
   }
}


proc onannoModifyLoad {} {
   aie_anno_cmd modify
}

proc onannoMoveLoad {} {
   aie_anno_cmd move
}

proc onannoDeleteLoad {} {
   aie_anno_cmd remove
}

proc onannoClearLoad {} {
   aie_anno_cmd clear
}


proc onannoModifyVframeDEZLoad {} {
}

proc onannoModifyVframeDEZApply {} {
   modify_anno_pick vframe
}

proc onannoModifyTextDEZLoad {} {
   global anno_data
}

proc onannoModifyTextDEZApply {} {
   modify_anno_pick text
}

proc onannoModifyUtftextDEZLoad {} {
}

proc onannoModifyUtftextDEZApply {} {
   modify_anno_pick utftext
}

proc onannoModifyPixmapDEZLoad {} {
}

proc onannoModifyPixmapDEZApply {} {
   modify_anno_pick pixmap
}

proc onannoModifyCmapDEZLoad {} {
}

proc onannoModifyCmapDEZApply {} {
   modify_anno_pick cmap
}

proc onannoModifyCircleDEZLoad {} {
}

proc onannoModifyCircleDEZApply {} {
   modify_anno_pick circle 
}

proc onannoModifyBoxDEZLoad {} {
}

proc onannoModifyBoxDEZApply {} {
   modify_anno_pick box
}

proc onannoModifyLineDEZLoad {} {
   set rr $::anno_data(line,pts)
   set n [llength $rr]
   if {$n > 6} {
      set i0 [expr $n - 2]
      set i1 [expr $n - 1]
      if {([lindex $rr 0] == [lindex $rr $i0]) && \
          ([lindex $rr 1] == [lindex $rr $i1])} {
         annoModifyLineDEZ cancel
         update idletasks
         annoModifyPolygonDEZ show
      }
   }
}

proc onannoModifyLineDEZApply {} {
   modify_anno_pick line
}

proc onannoModifyPolygonDEZLoad {} {
   set n [expr [llength $::anno_data(line,pts)] -3]
   set ::anno_data(line,pts) [lrange $::anno_data(line,pts) 0 $n]
}

proc onannoModifyPolygonDEZApply {} {
   set x0 [lindex $::anno_data(line,pts) 0]
   set y0 [lindex $::anno_data(line,pts) 1]
   lappend ::anno_data(line,pts) $x0 $y0

   update idletasks

   modify_anno_pick line

   update idletasks
   set n [expr [llength $::anno_data(line,pts)] -3]
   set ::anno_data(line,pts) [lrange $::anno_data(line,pts) 0 $n]
}


proc onannoModifyMarkDEZLoad {} {
}

proc onannoModifyMarkDEZApply {} {
   modify_anno_pick mark
}



proc onannoCreateDEZLoad {} {
   #update_anno_misc_cmd create_fm annoCreateDEZ
}

proc onannotationDEZLoad {} {
   variable AllowActiveReload
   set AllowActiveReload 1

   set win ""
   global anno_data
   if {[info exist anno_data(to_win)]} {
      set win $anno_data(to_win)
   }

  #mess "from onannotationDEZLoad --- $win\n"

   update_anno_misc_cmd create_fm annotationDEZ $win

   if [info exist anno_data(to_create)] {
      variable CreateAnnoType
      
      switch -- $anno_data(to_create) {
         text     { set CreateAnnoType 0 }
         utftext  { set CreateAnnoType 1 }
         line     { set CreateAnnoType 2 }
         box      { set CreateAnnoType 3 }
         circle   { set CreateAnnoType 4 }
         polygon  { set CreateAnnoType 5 }
         mark     { set CreateAnnoType 6 }
         pixmap   { set CreateAnnoType 7 }
         cmap     { set CreateAnnoType 8}
      }
   }
}

proc onannotationDEZApply {} {

   set which [::AIEnv::GUIManagement::ExclusiveGroup::GetCurrentGroupName \
                       annotationEXG]

   switch -- $which {
      annoDelete { aie_anno_cmd apply_delete }
      annoCreate {
         global anno_data
         if [info exist anno_data(in_create)] { return "" }
 
       # variable anno_data_type
       # if {$anno_data_type != ""} {
       #    onAnnoCreateTypeModify annotationDEZ $anno_data_type
       # }
         set Type [::AIEnv::GUIManagement::ExclusiveGroup::GetCurrentGroupName \
                             annoCreateTypeEG]
         set type ""
         switch -- $Type {
            annoCreateText     { set type text }
            annoCreateUtfText  { set type utftext }
            annoCreateLines    { set type line }
            annoCreateBox      { set type box }
            annoCreateCircle   { set type circle }
            annoCreatePolygon  { set type polygon }
            annoCreateMarks    { set type mark }
            annoCreatePixmap   { set type pixmap }
            annoCreateCmapBar  { set type cmap }
            default { set type "" }
         }
         if {$type == ""} { return "" }

         annotationDEZ setclean

#         set anno_data(no_reload_dez) 1
         update_anno_mg create $type
#         unset anno_data(no_reload_dez)

         annotationDEZ setclean
      }
   }
}

proc onannotationDEZCancel {} {
   global anno_data

   set which [::AIEnv::GUIManagement::ExclusiveGroup::GetCurrentGroupName \
                       annotationEXG]

   switch -- $which {
      annoDelete { aie_anno_cmd cancel_delete }
   }

   aie_anno_cmd reset 
}

proc onAnnoCreateTypeModify {args} {

   set Type [lindex $args 1]

   set type ""
   switch -- $Type {
      Text             { set type text }
      "Utf Text"       { set type utftext }
      Mark             { set type mark }
      Line             { set type line }
      Box              { set type box }
      Circle           { set type circle }
      Polygon          { set type polygon }
      Pixmap           { set type pixmap }
      "Colormap Bar"   { set type cmap }
      default          { set type "" }
   }
   if {$type == ""} { return "" }

   global anno_data
   foreach v {angle linew marksize} {
      set anno_data($v) [$anno_data(dez_cmd) get anno_data:$v]
   }
   update idletasks

   update_anno_mg create $type 

   annotationDEZ setclean
}

proc aie_annos_iconbar {what args} {
   switch -- $what {
      create {annoCreateDEZ show}
      exg { annotationDEZ show }
   }
}


# ----------------------- vpColorMapDEZ

proc aie_toggle_vpcolormap {theTree theNode} {
   global cmap_info

   if ![info exist cmap_info($theNode,toggle)] {return ""}

   set id [lindex [$theTree itemcget $theNode -data] 1]

   if $cmap_info($theNode,toggle) {
      $theTree itemconfigure $theNode -image \
             $::AIEnv::GUIManagement::Tree::VisibilityImages(notVisImg)
      set cmap_info($theNode,toggle) 0
      aie_vpcolormap_cmd modify_bartype $theNode $id hidden

      if {$::AIEnv::GUIManagement::ActiveDEZ == "annoModifyCmapDEZ"} {
         annoModifyCmapDEZ cancel
      }
   } else {
      $theTree itemconfigure $theNode -image \
                $::AIEnv::GUIManagement::Tree::VisibilityImages(isVisImg)
      set cmap_info($theNode,toggle) 1
      aie_vpcolormap_cmd modify_bartype $theNode $id
   } 
}


proc aie_editnode_vpcolormap {theTree theNode id} {
   aie_vpcolormap_cmd modify_entries $theNode $id
}

proc aie_vpcolormap_menu {mm theTree theNode id} {
   $mm add command -label "Modify Entries" \
              -command "aie_vpcolormap_cmd modify_entries $theNode $id"
   $mm add command -label "Modify Display" \
              -command "aie_vpcolormap_cmd modify_display $theNode $id"
   $mm add command -label "Move" \
              -command "aie_vpcolormap_cmd move $theNode $id"
   $mm add command -label "Rescaling with mouse" \
              -command "aie_vpcolormap_cmd rescale $theNode $id"
   $mm add command -label "Inverse the map" \
              -command "aie_vpcolormap_cmd inverse $theNode $id"
}

proc aie_vpcolormap_cmd {what node id args} {
   global cmap_info anno_data

   set top_tree [set ::AIEnv::GUIManagement::Tree::aieTree]
   if {$id != [V3_colormap_get cmap]} {
      set cmap_info(cmap) $id
      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]
         }
      }
   }

   if {[info exist cmap_info($node,toggle)] && !$cmap_info($node,toggle)} {
      switch -- $what {
         modify_display - move - rescale - modify_entries {
            aie_toggle_vpcolormap $top_tree $node
         }
      } 
   }

   set topnode $node
   while {[set p [$top_tree parent $topnode]] != "root"} {set topnode $p}
   set win  [lindex [$top_tree itemcget $topnode -data] 1]

   switch -- $what {
      modify_entries {
         vpColorMapDEZ show
      }
      modify_display {
         set anno_data(win) $win
         set anno_data(model) 0

         update_anno_pick [lindex $cmap_info($id,anno_list) 1]
         annoModifyCmapDEZ show 
      }
      modify_bartype {
         set anno_data(win) $win
         set anno_data(model) 0
         set noshow ""
         if {[lindex $args 0] == "hidden"} {set noshow 1}
         update_anno_pick [lindex $cmap_info($id,anno_list) 1] $noshow
         set bt [lindex $args 0]
         if {$bt == ""} {
            set bt $anno_data(cmap,bartype_last)
         } else {
            set anno_data(cmap,bartype_last) $anno_data(cmap,bartype)
         }
         set anno_data(cmap,bartype) [lindex $args 0]
         modify_anno_pick cmap
      }
      move {
         set anno_data(win) $win
         set anno_data(model) 0
         anno_action move 0 0 [lindex $cmap_info($id,anno_list) 1]
      }
      rescale {
         set anno_data(win) $win
         set anno_data(model) 0
         set cmap [lindex [lindex [anno_action \
                    rescale_cmap 0 0 [lindex $cmap_info($id,anno_list) 1]] 0] end]
         if {$cmap == ""} {return ""}

         aie_modify_vpcmap reset_entries
      }
      inverse {
         set rr [V3_colormap inverse]
         if {$rr != ""} { aie_modify_vpcmap reset_entries }
      }
   }
}

proc onvpColorMapDEZLoad {} {
   global cmap_info

   variable vpCMapPreList
   variable vpCMapPreOpt
   if {![info exist vpCMapPreList] || ($vpCMapPreList == "")} {
      set vpCMapPreList [list Default Default2 Shade(Black) Shade(White) Gray]
      set vpCMapPreOpt Default
   }

   aie_modify_vpcmap reset_entries
}

proc onvpColorMapDEZApply {} {
   global cmap_info

   set did 0
   foreach n {nentries minv maxv} {
      if {$cmap_info($n) != [V3_colormap_get $n]} { incr did }
   }
   if {$did} {
      V3_colormap resetval $cmap_info(minv) $cmap_info(maxv) \
                              $cmap_info(nentries)
   } else {
      set val [vpColorMapDEZ get vpCMapEnVal]
      if {[lsearch $cmap_info(entries) $val] < 0} {
         aie_modify_vpcmap set_ecol
      }
   }
}


proc aie_modify_vpcmap {what args} {
   global cmap_info

   switch -- $what {
      mscale - intensity {
         set val [vpColorMapDEZ get cmap_info:$what]
         set rr [V3_colormap $what $val]
         if {$rr != ""} { aie_modify_vpcmap reset_entries } 
      }
      strip {
         set val [vpColorMapDEZ get cmap_info:strip]
         set nn  $cmap_info(nentries)
         set rr [V3_colormap $what $val]
         if {$rr != ""} {
            V3_colormap resetval $cmap_info(minv) $cmap_info(maxv) $nn
            aie_modify_vpcmap reset_entries
         }
      }
      mscale2 {
         set val [vpColorMapDEZ get cmap_info:$what]
         set rr [V3_colormap $what $val 0.52]
         if {$rr != ""} { aie_modify_vpcmap reset_entries }
      }
      set_ecol {
         set val [vpColorMapDEZ get vpCMapEnVal]
         set oldID [vpColorMapDEZ get __vpCMapMEnVal_Last_Index]
         if {$oldID == -1} {return ""}

         onvpCMapMEnValWroteEntry $oldID $val

         if {[lindex $cmap_info(entries) $oldID] != $val} {
            set cmap_info(entries) [lreplace $cmap_info(entries) $oldID $oldID $val]
         }
      }
      add_entry {
         set val [vpColorMapDEZ get cmap_info:addval]
         set rr [V3_colormap add_entry $val]
         if {$rr != ""} {
            set cmap_info(entry) [lindex $rr 0]
            set cmap_info(minv)  [lindex $rr 1]
            set cmap_info(maxv)  [lindex $rr 2]

            incr cmap_info(nentries)
            set cmap_info(entries) [V3_colormap_get entries]
         } 
         if {$::AIEnv::GUIManagement::ActiveDEZ == "vpColorMapDEZ"} {
            vpColorMapDEZ set cmap_info:addval ""
         }
      }
      reset_entries {
         foreach n {nentries minv maxv} { set cmap_info($n) [V3_colormap_get $n] }

         set cmap_info(entries) [V3_colormap_get entries]
         set val [vpColorMapDEZ get vpCMapEnVal]
         if {[set j [lsearch $cmap_info(entries) $val]] < 0} {
            set rr [V3_colormap_get entry 0]
            if {$::AIEnv::GUIManagement::ActiveDEZ == "vpColorMapDEZ"} {
               vpColorMapDEZ set vpCMapEnVal [lindex $rr 0]
            }
            set cmap_info(ecol) [lindex $rr 1]
         } else {
            set rr [V3_colormap_get entry $j]
            set cmap_info(ecol) [lindex $rr 1]
         }

         set cmap_info(delentry) ""
         if {$::AIEnv::GUIManagement::ActiveDEZ == "vpColorMapDEZ"} {
            vpColorMapDEZ set cmap_info:addval ""
         }
      }
   }
}

proc onVpColorMapPreSetModify {args} {
   global cmap_info

   set Type [lindex $args 1]

   set rr ""
   switch -- $Type {
      Default          { lappend rr default }
      Default2         { lappend rr default2 }
      Gray             { lappend rr gray }
      Shade(Black) -  Shade(White) {
         set c [string tolower [string index $Type 6]]
         set col [tk_chooseColor -initialcolor $cmap_info(${c}s_color) \
                    -title "Choose $Type Color"]
         if {$col != ""} {set cmap_info(${c}s_color) $col}
         lappend rr ${c}shade $cmap_info(${c}s_color) 
      }
   }
   if {$rr == ""} { return "" }

   set cmap_info(opt) [lindex $rr 0]

   if {[eval V3_colormap preset $rr] == ""} { return "" }
   
   aie_modify_vpcmap reset_entries
}

proc onVpCMapMEnValModify {args} {
   global cmap_info

   set val [lindex $args 1]
   set i [lsearch $cmap_info(entries) $val]
   if {$i == -1} {
      mess "onVpCMapMEnValModify $args : no such value\n" red
      return ""
   }

   ::AIEnv::GUIManagement::ToggleGrey vpCMapMEnCol 0
   set cmap_info(ecol) [lindex [V3_colormap_get entry $i] 1]
}

proc onvpCMapMEnValWroteEntry {oldID newVal} {
   global cmap_info

   if {$newVal != ""} {
      set rr [V3_colormap set_entry $oldID $newVal $cmap_info(ecol)]
      if {$rr != ""} {
         set cmap_info(minv)  [lindex $rr 1]
         set cmap_info(maxv)  [lindex $rr 2]
      }
   } else {
      set rr [V3_colormap del_entry $oldID]
      if {$rr != ""} {
         set cmap_info(nentries) [lindex $rr 0]
         set cmap_info(minv)  [lindex $rr 1]
         set cmap_info(maxv)  [lindex $rr 2]

         ::AIEnv::GUIManagement::ToggleGrey vpCMapMEnCol 1
      }
   }
}
