

proc anno_do_pick {w anno_gm eventVarName} {
   global [lindex [split $eventVarName (] 0]
   upvar #0 $eventVarName but_var

   banner "Select the annotations with left button,\
                     confirm = middle/right." $w
   catch {$w configure -cursor {hand2 red white}}
   set $eventVarName ""

   set last_anno ""
   set a_picked ""
   while 1 {
      tkwait variable $eventVarName
      if ![info exist $eventVarName] {
          if {$a_picked != ""} {
             if [info exists a_color] {
                $anno_gm lowlight $a_picked $a_color
                unset a_color
             }
             lappend last_anno $a_picked
          }
          break
      }
      set val [set but_var]
      set x0 [lindex $val 1]; set y0 [lindex $val 2]
      set ptr_status [lindex $val 0]
      if {$ptr_status >= 1} {
         if {$a_picked != ""} {
            if [info exists a_color] {
               $anno_gm lowlight $a_picked $a_color
               unset a_color
            }
         }
         if {$ptr_status > 1} {
            lappend last_anno $a_picked
            break
         }
      }  
      if {$ptr_status == 1} {
         set a_picked [$anno_gm select $x0 $y0]
         if {$a_picked != ""} {
            set a_color [lindex [$anno_gm highlight $a_picked] 0]
         }
      }
   }
   return $last_anno
}


proc anno_do_rescale_cmap {w anno_gm eventVarName {an_tomove ""}} {
   global cmap_info

   global [lindex [split $eventVarName (] 0]
   upvar #0 $eventVarName but_var

   banner "Select and rescale colormap bar with the left button,\
                    middle/right = done" $w
   set last_cmap ""

   catch {$w configure -cursor {hand2 red white}}
   set $eventVarName ""
   tkwait variable $eventVarName

   while 1 {
      if ![info exist $eventVarName] {break}
      set ok 0
      set val [set but_var]
      set x0 [lindex $val 1]; set y0 [lindex $val 2]
      set ptr_status [lindex $val 0]
      set a ""
      if {$ptr_status == 1} {
         if {$an_tomove != ""} {
            set a $an_tomove
         } else {
            set a [$anno_gm select $x0 $y0]
            set rr [$anno_gm dump $a]
            if {[lindex $rr 0] != "cmap"} {set a ""}
         }
         if {$a != ""} {
            set rr [$anno_gm dump $a]
            if {[lindex $rr 0] == "cmap"} {
               set cmap [lindex $rr 1]
               set j [lsearch $rr "-size"]
               set width [lindex $rr [incr j]]
               set size  [lindex $rr [incr j]]

               set dx [expr [lindex $val 1] - [lindex $rr 2]]
               set cmbar_y0 [lindex $rr 3]
               set dy [expr $cmbar_y0 - [lindex $val 2]]

               if {($dx >= 0) && ($dx <= $width) && \
                   ($dy >= 0) && ($dy <= $size)} {
                  set ok 1
                  set rescale_d [expr 1.0 / $size]
                  set rescale_0 [expr $dy * $rescale_d] 

                  set a_color [lindex [$anno_gm highlight $a] 0]
                  catch {$w configure -cursor {crosshair red white}}

                  if {[lsearch $last_cmap $cmap] < 0} {
                     lappend last_cmap $cmap
                  }
               }
            }
         }
      } elseif {$ptr_status > 1} {
         break
      }
      while {$ok} {
         tkwait variable $eventVarName
         set val [set but_var]
         set dy [expr $cmbar_y0 - [lindex $val 2]]
         set yr [expr $dy * $rescale_d]
         if {$yr > $rescale_0} {
            catch {$w configure -cursor {top_side red white}}
         } else {
            catch {$w configure -cursor {bottom_side red white}}
         }
         
         if {abs($yr - $rescale_0) >= 0.01} {
            set yr [expr 0.372 * $yr + 0.628 * $rescale_0]
            V3_colormap rescale $rescale_0 $yr
            set rescale_0 $yr
         } 

         if {[lindex $val 0] != 0} {
            if [info exists a_color] { $anno_gm lowlight $a $a_color }
            catch {
               unset a_color
               $w configure -cursor {crosshair red white}
               unset rescale_d
            }
            set ok 0; break
         }
      }

      catch {$w configure -cursor {hand2 red white}}
      tkwait variable $eventVarName
   }
   return $last_cmap
}


proc anno_do_move {w anno_gm eventVarName {an_tomove ""}} {
   global [lindex [split $eventVarName (] 0]
   upvar #0 $eventVarName but_var

   set typename annotations
   if {$an_tomove != ""} {
      global anno_data
      if {[info exist anno_data($an_tomove,name)] &&
                 ($anno_data($an_tomove,name) != "")} {
         set typename $anno_data($an_tomove,name)
      } else {
         set rr [$anno_gm dump $an_tomove]
         if {$rr != ""} {
            switch -- [lindex $rr 0] {
               cmap { set typename "Colormap Bar" }
               default { set typename [lindex $rr 0] }
            }
         }
      }
   }

   banner "Select and move the $typename with the left button,\
                    middle/right = done" $w
   set last_anno ""

   catch {$w configure -cursor {hand2 red white}}
   set $eventVarName ""
   tkwait variable $eventVarName

   while 1 {
      if ![info exist $eventVarName] {break}
      set ok 0
      set val [set but_var]
      set x0 [lindex $val 1]; set y0 [lindex $val 2]
      set ptr_status [lindex $val 0]
      set a ""
      if {$ptr_status == 1} {
         if {$an_tomove != ""} {
            set a $an_tomove
         } else {
            set a [$anno_gm select $x0 $y0]
         }
         if {$a != ""} {
            set a_color [lindex [$anno_gm highlight $a] 0]
            lappend last_anno $a
            set ok 1
            catch {$w configure -cursor {fleur red white}}
         }
      } elseif {$ptr_status > 1} {
         break
      }
      while {$ok} {
         tkwait variable $eventVarName
         set val [set but_var]
         set xp [lindex $val 1]; set yp [lindex $val 2]
         set dx [expr $xp -$x0]; set dy [expr $yp - $y0]
         set b [$anno_gm move $a $dx $dy]
         if {$b != $a} {break}
         set x0 $xp; set y0 $yp
         if {[lindex $val 0] != 0} {
            if [info exists a_color] { $anno_gm lowlight $a $a_color }
            catch {unset a_color}
            break
         }
      }

      global anno_data
      if {[info exist anno_data(picked_anno)] && \
                    ($anno_data(picked_anno) == $a)} {
         update_anno_pick $a
      }

      catch {$w configure -cursor {hand2 red white}}
      tkwait variable $eventVarName
   }
   return $last_anno
}


proc anno_do_delete {w anno_gm eventVarName listVarName args} {
   global [lindex [split $eventVarName (] 0]
   upvar #0 $eventVarName but_var

   set what [lindex $args 0]
   if {$what == "cancel"} {
      foreach {a c} [lrange $args 1 end] {
         $anno_gm lowlight $a $c
      }
      return ""
   }

   global [lindex [split $listVarName (] 0]
   upvar #0 $listVarName list_var

   if {![info exist $listVarName] || ($list_var == "")} {
      mess "no annotaion to be deleted in the selected window\n"
      return ""
   }

   if {$what == ""} {
      banner "Select the annotations to delete with left button,\
                middle button = confirm, right = cancel." $w
   } elseif {$what == "pick"} {
      banner "Select the annotations to delete with left button,\
                escape with middle/right button." $w
   } else {
      foreach {a c} [lrange $args 1 end] {
         $anno_gm remove $a
         set p [lsearch $list_var $a]
         if {$p >= 0} { set list_var [lreplace $list_var $p $p] }
      }
      return ""
   }

   catch {$w configure -cursor {draped_box red white}}
   set $eventVarName ""

   set to_del ""
   while 1 {
      tkwait variable $eventVarName
      if ![info exist $eventVarName] {break}
      set val [set but_var]
      set ptr_status [lindex $val 0]
      if {$ptr_status == 1} {
         set a [$anno_gm select [lindex $val 1] [lindex $val 2]]
         set p [lsearch $list_var $a]
         if {$p >= 0} {
            if {[lsearch $to_del $a] < 0} {
               set a_color [$anno_gm highlight $a]
               lappend to_del $a $a_color
            }
         }
      } elseif {$ptr_status == 3} {
         if {$what == ""} {
            foreach {a c} $to_del {$anno_gm lowlight $a $c}
            return ""
         } else {
            return $to_del
         }
      } elseif {$ptr_status == 2} {
         if {$what != ""} { return $to_del }
         break
      }
   }

   foreach {a c} $to_del {
      $anno_gm remove $a
      set p [lsearch $list_var $a]
      if {$p >= 0} { set list_var [lreplace $list_var $p $p] }
   }

   return $to_del
}
