
proc anno_modify_cmd {w what {model 0}} {
    global anno_data 

    set anno_data(win) $w
    set anno_data(model) $model
    anno_modify $what 
}



proc anno_modify {what} {
    global anno_data

    set w $anno_data(win)
    set m $anno_data(model)    
    if ![info exist anno_data(gm,$m,$w)] {
       error "$w $m need to be initialized for annotations"
       return ""
    }

    set anno_gm $anno_data(gm,$m,$w)
    upvar #0 $anno_data(button_events,$w) but_var

    if {[anno_setup_mouse_binding $w] == 0} {return}

    switch $what {
       move {
          banner "Select and move the annotations with the left button,\
                   middle/right = finish." $w
          catch {$w configure -cursor {hand2 red white}}
          set $anno_data(button_events,$w) ""
          tkwait variable $anno_data(button_events,$w)
          while 1 {
             set ok 0
             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} {
                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} {
                set a ""; break
             }
             while {$ok} {
                tkwait variable $anno_data(button_events,$w)
                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
                }
             }
             catch {$w configure -cursor {hand2 red white}}
             tkwait variable $anno_data(button_events,$w)
          }
       }
       rotate {
          banner "Select and rotate the annotations with the left button,\
                    middle/right = done" $w
          catch {$w configure -cursor {hand2 red white}}
          set $anno_data(button_events,$w) ""
          tkwait variable $anno_data(button_events,$w)
          while 1 {
             set ok 0
             set val [set but_var]
             set x1 [lindex $val 1]; set y1 [lindex $val 2]
             set ptr_status [lindex $val 0]
             if {$ptr_status == 1} {
                set a [$anno_gm select $x1 $y1]
                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} {
                set a ""; break
             }
             while {$ok} {
                tkwait variable $anno_data(button_events,$w)
                set val [set but_var]
                set x0 $x1; set y0 $y1
                set x1 [lindex $val 1]; set y1 [lindex $val 2]
                set b [$anno_gm rotate $a $x0 $y0 $x1 $y1]
                if {$b != $a} {break}
                if {[lindex $val 0] != 0} {
                   if [info exists a_color] {$anno_gm lowlight $a $a_color}
                   catch {unset a_color}
                   break
                }
             }
             catch {$w configure -cursor {hand2 red white}}
             tkwait variable $anno_data(button_events,$w)
          }
       }
       delete {
          banner "Select the annotations to delete with left button,\
                middle button = confirm, right = cancel." $w

          catch {$w configure -cursor {draped_box red white}}
          set $anno_data(button_events,$w) ""
          set to_del ""
          set ok 1
          while {$ok} {
             tkwait variable $anno_data(button_events,$w)
             set val [set but_var]
             switch -- [lindex $val 0] {
                1 {
                    set a [$anno_gm  select [lindex $val 1] [lindex $val 2]]
                    set p [lsearch $anno_data(list,$m,$w) $a]
                    if {$p < 0} {set a ""}
                    if {$a != ""} {
                       if {[lsearch $to_del $a] < 0} {
                           set a_color [$anno_gm highlight $a]
                           lappend to_del $a
                       }
                    }
                  }
                2 {
                    foreach a $to_del {
                       $anno_gm remove $a
                       set p [lsearch $anno_data(list,$m,$w) $a]
                       if {$p >= 0} {
                          set anno_data(list,$m,$w) \
                                  [lreplace $anno_data(list,$m,$w) $p $p]
                       }
                    }
                    set ok 0
                  }
                3 { set ok 0 }
             }
          }
       }
    }
    banner "" $w
    anno_reset_mouse_binding $w
    catch {$w configure -cursor {left_ptr red white}}
    return $last_anno
}


proc move_and_rotate_anno {w an} {
   global anno_data

   if {[anno_setup_mouse_binding $w] == 0} {return}

   banner "Move with left button, \
              rotate with middle, \
              finish with right button." $w

   catch {$w configure -cursor {hand2 red white}}
   
   set anno_gm $anno_data(gm,1,$w)
   upvar #0 $anno_data(button_events,$w) but_var

   tkwait variable $anno_data(button_events,$w)
   while 1 {
       set ok 0
       set val [set but_var]
       set x1 [lindex $val 1]; set y1 [lindex $val 2]
       set ptr_status [lindex $val 0]
       if {$ptr_status > 2} {
          break
       } else {
          set a_color [lindex [$anno_gm highlight $an] 0]
          set ok 1
          if {$ptr_status == 1} {
             catch {$w configure -cursor {fleur red white}}
          } elseif {$ptr_status == 2} {
             catch {$w configure -cursor {fleur red white}} 
          }
       }
       while {$ok} {
          tkwait variable $anno_data(button_events,$w)
          set val [set but_var]
          set x0 $x1; set y0 $y1
          set x1 [lindex $val 1]; set y1 [lindex $val 2]
          if {$ptr_status == 1} {
             set dx [expr $x1 -$x0]
             set dy [expr $y1 - $y0]
             set b [$anno_gm move $an $dx $dy]
             if {$b != $an} {break}
          } elseif {$ptr_status == 2} {
             set b [$anno_gm rotate $an $x0 $y0 $x1 $y1]
             if {$b != $an} {break}
          }
          if {[lindex $val 0] != 0} {
             if [info exists a_color] {$anno_gm lowlight $an $a_color}
             catch {unset a_color}
             break
          }
       }
       catch {$w configure -cursor {hand2 red white}}
       tkwait variable $anno_data(button_events,$w)
   }

   banner "" $w
   anno_reset_mouse_binding $w
   catch {$w configure -cursor {left_ptr red white}}
}
