
array set anno_data {
   picked_type none
   win_list   ""
   font_m_list ""
   stype_m_list ""
   arrow_m_list ""
   red   1.0
   green 1.0
   blue  0.0
   linew   1
   font    1
   angle   0
   arrow_s 0
   arrow_e 0
   arrow   0
   stype   1
   symbol_fill 0
   model 0
   win  .v.view
   lcolor  #ffff00
   fcolor  #ff0000
   text,font   1
   text,lcolor #ffff00
   font_name small
   marksize  6
}

set symbols_predefined { \
    sym_0000.xbm sym_0010.xbm sym_0020.xbm sym_0040.xbm \
    sym_0001.xbm sym_0002.xbm sym_0100.xbm sym_0200.xbm \
    sym_0400.xbm sym_0800.xbm sym_0011.xbm sym_0021.xbm \
    sym_0041.xbm sym_0101.xbm sym_0201.xbm sym_0401.xbm \
    sym_0801.xbm sym_0012.xbm sym_0022.xbm sym_0042.xbm \
    sym_0102.xbm sym_0202.xbm sym_0402.xbm sym_0802.xbm }

set arrowlines_predefined { \
    arrowline_0.xbm arrowline_1.xbm arrowline_2.xbm arrowline_3.xbm }


proc control_anno_defaults_choiceframe {d} {
   global anno_data
   global symbols_predefined arrowlines_predefined vpost_library

   set wintitle [update_anno_mg win_list $anno_data(win)]
   set nn "Create anno on $wintitle"
   form_label $d.win $nn {0 0 -cols - -sticky ew}
     set anno_data(def,win_m) $d.win

   form_frame $d.params sunken 2 {5 0 -cols -}

   form_label $d.params.fontsizel "Font:" {0 0}
   form_label $d.params.anglel "Text angle:" {1 0}
   form_label  $d.params.widthl "Line width:" {2 0}
   form_label  $d.params.marksizel "Symbol size:" {3 0}

   form_text_optionMenu $d.params.fontsize anno_data(font) \
        [list small 0 med 1 large 2] "" {0 1} sunken 2 \
        0 {-height 1 -width 6} anno_data(font_m_list)
            
   form_entry $d.params.angle anno_data(angle) float {1 1}
   form_entry  $d.params.width anno_data(linew) int {2 1}
   form_entry  $d.params.marksize anno_data(marksize) int {3 1}
   
   form_label  $d.params.arrowl "Arrow:" {4 0}
   form_bitmap_optionMenu  $d.params.arrowstatus anno_data(arrow) \
           $vpost_library  $arrowlines_predefined \
           "" {4 1} flat 0 0 {-padx 2} anno_data(arrow_m_list)

   form_label $d.params.symboll "Symbol:" {5 0}
   form_bitmap_optionMenu $d.params.symbolstatus anno_data(stype) \
          $vpost_library  $symbols_predefined \
          "" {5 1} flat 0 6 {-padx 2 -pady 2} anno_data(stype_m_list)

   form_label $d.params.lcolorl "Color:" {6 0}
   futil_colorentry $d.params.lcolor anno_data(lcolor) {6 1} 
  #futil_coloredit_button $d.params.lcolor anno_data(lcolor) "" "" "" {6 1}
}


proc control_anno3d_choiceframe {fm} {
   global anno_data anno3d_data

   set wintitle [update_anno_mg win_list $anno_data(win)]
   set nn "Create anno3D on $wintitle"
   form_label $fm.win $nn {0 0 -cols - -sticky ew}
     set anno3d_data(win_m) $fm.win

   set fms $fm.s
   form_frame $fms ridge 1 {5 0 -cols - -sticky news}

   form_choiceframe $fms.line
   control_anno3d_line_choiceframe $fms.line

   form_choiceframe $fms.disc
   control_anno3d_disc_choiceframe $fms.disc

   form_choiceframe $fms.plane
   control_anno3d_plane_choiceframe $fms.plane 
   
   futil_setup_switch $fms anno3d_data(type)
}

proc control_anno3d_plane_choiceframe {fm} {
   global anno3d_data

}

proc control_anno3d_disc_choiceframe {fm} {
   global anno3d_data

}


proc control_anno3d_disc_choiceframe {fm} {
   global anno3d_data

}

proc control_anno3d_line_choiceframe {fm} {
   global anno3d_data

   form_label $fm.x1_l " X1 : " {0 0}
   form_label $fm.y1_l " Y1 : " {1 0}
   form_label $fm.z1_l " Z1 : " {2 0}

   form_entry $fm.x1 anno3d_data(line,x1) float {0 1 -cols 2}
   form_entry $fm.y1 anno3d_data(line,y1) float {1 1 -cols 2}
   form_entry $fm.z1 anno3d_data(line,z1) float {2 1 -cols 2}

   form_label $fm.x2_l " X2 : " {3 0}
   form_label $fm.y2_l " Y2 : " {4 0}
   form_label $fm.z2_l " Z2 : " {5 0}

   form_entry $fm.x2 anno3d_data(line,x2) float {3 1 -cols 2}
   form_entry $fm.y2 anno3d_data(line,y2) float {4 1 -cols 2}
   form_entry $fm.z2 anno3d_data(line,z2) float {5 1 -cols 2}
}


proc control_anno_specific_choiceframe {fm} {
   global anno_data

   set wintitle [update_anno_mg win_list $anno_data(win)]
   form_label $fm.win "Modify Anno on $wintitle" {0 0 -cols - -sticky ew}
      set anno_data(modify_win_m) $fm.win

   set fms $fm.s
   form_frame $fms ridge 1 {5 0 -cols - -sticky news}
   form_choiceframe $fms.none
     message $fms.none.msg  -width 8c -justify center -pady 10\
                -text "None annotation \n\n is specified" -fg red \
                -font "helvetica 14 bold"
         pack $fms.none.msg -padx 10 -pady 10

   foreach k {legend tickaxis xyplot} n {legend "XY axes" "XY plot"} {
      form_choiceframe $fms.$k
         message $fms.$k.msg  -width 8c -justify center -pady 10 \
                -text "Modify the $n \n on window XY Plots" \
                -fg red -font "helvetica 12 bold"
           pack $fms.$k.msg -padx 10 -pady 10
   }

   form_choiceframe $fms.text
   control_anno_text_choiceframe $fms.text

   form_choiceframe $fms.utftext
   control_anno_utftext_choiceframe $fms.utftext

   form_choiceframe $fms.mark
   control_anno_mark_choiceframe $fms.mark

   form_choiceframe $fms.circle
   control_anno_circle_choiceframe $fms.circle

   form_choiceframe $fms.box
   control_anno_box_choiceframe $fms.box
 
   form_choiceframe $fms.line
   control_anno_line_choiceframe $fms.line

   form_choiceframe $fms.vframe
   control_anno_vframe_choiceframe $fms.vframe

   form_choiceframe $fms.cmap
   control_anno_cmap_choiceframe $fms.cmap

   #futil_setup_switch $fms anno_data(picked_type) "-anchor w"
   futil_setup_switch $fms anno_data(picked_type)
}


proc control_anno_mark_choiceframe {fmp} {
   global anno_data
   global symbols_predefined symbols_predefined vpost_library

   form_label $fmp.sym_typel "Type: " {0 1}
   form_label $fmp.sym_sizel "Size: " {1 1}
   form_label $fmp.sym_lnwdl "Line width: " {2 1}
   form_label $fmp.sym_colorl "Color: " {3 1}

   form_bitmap_optionMenu  $fmp.sym_type anno_data(mark,stype) \
          $vpost_library  $symbols_predefined \
          "modify_anno_pick mark" {0 2} raised 0 6

   form_entry $fmp.sym_size anno_data(mark,ssize) int {1 2}
        bind $fmp.sym_size <Return> "modify_anno_pick mark"

   form_entry $fmp.sym_lnwd anno_data(mark,lnwidth) int {2 2}

   futil_colorentry $fmp.sym_color anno_data(mark,scolor) {3 2} \
          "" "" "" "modify_anno_pick mark"
  #futil_coloredit_button $fmp.sym_color anno_data(mark,scolor) "" "" \
  #     "modify_anno_pick mark" {3 2}

   form_checkbutton $fmp.sym_fill "Fill the symbol" \
         anno_data(mark,fill) {4 1 -cols 2}
      $fmp.sym_fill configure -command "modify_anno_pick mark"
}


proc control_anno_text_choiceframe {d} {
   global anno_data

   form_frame $d.params sunken 2 {0 0 -cols -}
   
   form_label $d.params.fontsizel "Font:" {0 0}
   form_label $d.params.anglel "Text angle:" {1 0}

   form_text_optionMenu $d.params.fontsize anno_data(text,font) \
        [list small 0 med 1 large 2] "modify_anno_pick text" {0 1} \
        sunken 2 0 {-height 1 -width 6}

   form_entry $d.params.angle anno_data(text,angle) float {1 1}
      bind $d.params.angle <Return> "modify_anno_pick text"

   form_label $d.params.lcolorl "Color:" {6 0}

   futil_colorentry $d.params.lcolor anno_data(text,lcolor) {6 1} \
             "" "" "" "modify_anno_pick text"
  #futil_coloredit_button $d.params.lcolor anno_data(text,lcolor) "" "" \
  #           "modify_anno_pick text" {6 1}

   form_frame $d.txt flat 0 {10 0 -cols - -sticky ew}
   form_label $d.txt.l "Text content:" {0 0}
   form_entry $d.txt.c anno_data(text,text) string {1 0 -cols - -sticky ew}
      bind $d.txt.c <Return> "modify_anno_pick text"
}


proc control_anno_utftext_choiceframe {d} {
   global anno_data

   form_frame $d.params sunken 2 {0 0 -cols -}

   form_label $d.params.fontsizel "Font:" {0 0}
  #form_label $d.params.anglel "Text angle:" {1 0}

   form_text_optionMenu $d.params.fontsize anno_data(utftext,font) \
        [list small 0 med 1 large 2] "modify_anno_pick utftext" {0 1} \
        sunken 2 0 {-height 1 -width 6}

  #form_entry $d.params.angle anno_data(utftext,angle) float {1 1}
  #   bind $d.params.angle <Return> "modify_anno_pick utftext"

   form_label $d.params.lcolorl "Color:" {6 0}

   futil_colorentry $d.params.lcolor anno_data(utftext,lcolor) {6 1} \
             "" "" "" "modify_anno_pick utftext"

   form_frame $d.txt flat 0 {10 0 -cols - -sticky ew}
   form_label $d.txt.l "Utf Text content:" {0 0}
   form_entry $d.txt.c anno_data(utftext,text) string {1 0 -cols - -sticky ew}
      bind $d.txt.c <Return> "modify_anno_pick utftext"
}


proc control_anno_circle_choiceframe {fmp} {
   global anno_data

   form_label $fmp.lnwdl "Line width: " {0 1}
   form_label $fmp.radl  "Radius:" {1 1}
   form_label $fmp.colorl "Color: " {3 1}

   form_entry $fmp.lnwd anno_data(circle,lnwidth) int {0 2}
   form_entry $fmp.rad  anno_data(circle,rad) float {1 2}

      foreach s {lnwd rad} {
         bind $fmp.$s <Return> "modify_anno_pick circle"
      }
  
   futil_colorentry $fmp.color anno_data(circle,color) {3 2} \
           "" "" "" "modify_anno_pick circle" 
  #futil_coloredit_button $fmp.color anno_data(circle,color)  "" "" \
  #           "modify_anno_pick circle" {3 2}

   form_checkbutton $fmp.fill "Fill the circle" \
         anno_data(circle,fill) {4 1 -cols 2}
     $fmp.fill configure -command "modify_anno_pick circle"
}


proc control_anno_box_choiceframe {fmp} {
   global anno_data

   form_label $fmp.lnwdl "Line width: " {0 1}
   form_label $fmp.xl    "X length:" {1 1}
   form_label $fmp.yl    "Y length:" {2 1}

   form_entry $fmp.lnwd anno_data(box,lnwidth) int {0 2}
   form_entry $fmp.x anno_data(box,xl) float {1 2}
   form_entry $fmp.y anno_data(box,yl) float {2 2}

      foreach s {lnwd x y} {
         bind $fmp.$s <Return> "modify_anno_pick box"
      }

   form_label $fmp.colorl "Color: " {3 1}

   futil_colorentry $fmp.color anno_data(box,color) {3 2} \
                    "" "" "" "modify_anno_pick box" 
  #futil_coloredit_button $fmp.color anno_data(box,color)  "" "" \
  #           "modify_anno_pick box" {3 2}

   form_checkbutton $fmp.fill "Fill the box" \
         anno_data(box,fill) {4 1 -cols 2}
     $fmp.fill configure -command "modify_anno_pick box"
}

proc control_anno_line_choiceframe {d} {
   global anno_data
   global arrowlines_predefined arrowlines_predefined vpost_library

   set fmp $d.params
   form_frame $fmp sunken 2 {0 0 -cols -}
   form_label $fmp.lnwdl "Line width: " {0 1}
   form_entry $fmp.lnwd anno_data(line,lnwidth) int {0 2}
      bind $fmp.lnwd <Return> "modify_anno_pick line"

   form_label $fmp.arrowl "Arrow:" {1 1}
   form_bitmap_optionMenu $fmp.arrowstatus anno_data(line,arrow) \
           $vpost_library  $arrowlines_predefined \
           "modify_anno_pick line" {1 2} flat 0 0 {-padx 2}

   form_label $fmp.colorl "Color: " {3 1}

   futil_colorentry $fmp.color anno_data(line,color) {3 2} \
          "" "" "" "modify_anno_pick line" 
  #futil_coloredit_button $fmp.color anno_data(line,color)  "" "" \
  #       "modify_anno_pick line" {3 2}

   form_frame $d.pts flat 0 {10 0 -cols - -sticky ew}
   form_label $d.pts.l "Line points:" {0 0}
   form_entry $d.pts.c anno_data(line,pts) string {1 0 -cols - -sticky ew}
      bind $d.pts.c <Return> "modify_anno_pick line"
}


proc control_anno_cmap_choiceframe {fmp} {
   global anno_data

   form_label $fmp.typel "Type:" {0 0}
   futil_menuentry $fmp.type anno_data(cmap,bartype) \
           "list spectrum uniform linescale  discrete hidden" \
                 {0 1 -cols 2} "" "modify_anno_pick cmap"
   form_label $fmp.widthl  "Width:" {1 0}
   form_entry $fmp.width  anno_data(cmap,width) float {1 1}
   form_label $fmp.heightl "Height:" {1 2}
   form_entry $fmp.height anno_data(cmap,height) float {1 3}
      foreach s {width height} {
         bind $fmp.$s <Return> "modify_anno_pick cmap"
         $fmp.$s configure -width 4
      }
   form_frame $fmp.c flat 0 {2 0 -cols -}
   form_label $fmp.c.formatl "Num Format:" {0 0}
   form_entry $fmp.c.format anno_data(cmap,format) string {0 1 -cols 2}
      bind $fmp.c.format <Return> "modify_anno_pick cmap"
   form_checkbutton $fmp.c.inv "Inverse" anno_data(cmap,inverse) {2 0}
      $fmp.c.inv configure -command "modify_anno_pick cmap"
}

proc control_anno_vframe_choiceframe {fmp} {
   global anno_data

   form_label $fmp.xl    "X length:" {0 1}
   form_label $fmp.yl    "Y length:" {1 1}

   form_entry $fmp.x anno_data(vframe,xL) float {0 2}
   form_entry $fmp.y anno_data(vframe,yL) float {1 2}

   form_label $fmp.bd_rfl "Border relief: " {2 1}
   form_label $fmp.lnwdl  "Border width: " {3 1}
   form_label $fmp.colorl "Border color: " {4 1}
   form_label $fmp.bgcoll "Background: " {5 1}

   futil_menuentry $fmp.bd_rf anno_data(vframe,relief) \
        "list flat ridge groove sunken raised solid" {2 2} \
        "" "modify_anno_pick vframe"
   form_entry $fmp.lnwd anno_data(vframe,borderwidth) int {3 2}

   futil_colorentry $fmp.color anno_data(vframe,bordercolor) {4 2} \
             "" "" "" "modify_anno_pick vframe" 
  #futil_coloredit_button $fmp.color anno_data(vframe,bordercolor) "" "" \
  #          "modify_anno_pick vframe" {4 2}

   futil_colorentry $fmp.bgcolor anno_data(vframe,background) {5 2} \
             "" "" "" "modify_anno_pick vframe"
  #futil_coloredit_button $fmp.bgcolor anno_data(vframe,background) "" "" \
  #           "modify_anno_pick vframe" {5 2}

      foreach s {lnwd x y} {
         bind $fmp.$s <Return> "modify_anno_pick vframe"
      }

   form_checkbutton $fmp.dr_im "Static image" anno_data(vframe,draw_im) \
            {9 1 -cols 2}
      $fmp.dr_im configure -command "modify_anno_pick vframe"
}


proc modify_anno_pick {what args} {
   global anno_data

   if {[info commands $anno_data(picked_gm)] == ""} {return}
   if {$anno_data(picked_type) != "$what"} {
      puts "modify_anno_pick $what $args"
      return
   }
 
   switch -- $what {
      mark {
         set anno_data(mark,scolor) [from_tkcolor_get value $anno_data(mark,scolor)]
         $anno_data(picked_gm) modify $anno_data(picked_anno) \
              $anno_data(mark,lnwidth) $anno_data(mark,scolor) \
              $anno_data(mark,x) $anno_data(mark,y) \
              $anno_data(mark,ssize) $anno_data(mark,stype) \
              $anno_data(mark,fill)
      }
      text {
         set anno_data($what,lcolor) [from_tkcolor_get value $anno_data($what,lcolor)]
         $anno_data(picked_gm) modify $anno_data(picked_anno) \
              $anno_data(text,lcolor) $anno_data(text,font) \
              $anno_data(text,text) $anno_data(text,angle)
      }
      utftext {
         set anno_data($what,lcolor) [from_tkcolor_get value $anno_data($what,lcolor)]
         set anno_data(utftext,text) \
                        [ucode_scan_entry $anno_data(utftext,text)]
         $anno_data(picked_gm) modify $anno_data(picked_anno) \
              $anno_data(utftext,lcolor) $anno_data(utftext,font) \
              $anno_data(utftext,text)
      }
      circle {
         set anno_data($what,color) [from_tkcolor_get value $anno_data($what,color)]
         set y1 [expr $anno_data(circle,y)  + $anno_data(circle,rad)]
         $anno_data(picked_gm) modify $anno_data(picked_anno) \
              $anno_data(circle,lnwidth) $anno_data(circle,color) \
              $anno_data(circle,x) $anno_data(circle,y) \
              $anno_data(circle,x) $y1 $anno_data(circle,fill)
      }
      box {
         set anno_data($what,color) [from_tkcolor_get value $anno_data($what,color)]
         foreach s {x y} {
            set c [expr ($anno_data(box,${s}0) + $anno_data(box,${s}1))*0.5]
            set L [expr $anno_data(box,${s}l) * 0.5]
            set anno_data(box,${s}0) [expr $c - $L]
            set anno_data(box,${s}1) [expr $c + $L]
         }
         $anno_data(picked_gm) modify $anno_data(picked_anno) \
              $anno_data(box,lnwidth) $anno_data(box,color) \
              $anno_data(box,x0) $anno_data(box,y0) \
              $anno_data(box,x1) $anno_data(box,y1) $anno_data(box,fill) 
      }
      cmap {
         if {$anno_data(cmap,bartype) == "hidden"} {
            $anno_data(picked_gm) visible $anno_data(picked_anno) 0
            return 0
         } else {
            $anno_data(picked_gm) visible $anno_data(picked_anno) 1
         } 
         set linescale 0; set discrete 0; set uniform 0
         switch -- $anno_data(cmap,bartype) {
            uniform - linescale - discrete { set $anno_data(cmap,bartype) 1 }
            default { set discrete 0 }
         }
         $anno_data(picked_gm) modify $anno_data(picked_anno) \
              -type $linescale $discrete $uniform $anno_data(cmap,inverse) \
              -size $anno_data(cmap,width) $anno_data(cmap,height) \
              -format $anno_data(cmap,format)
      }
      vframe {
         set anno_data($what,color) [from_tkcolor_get value $anno_data($what,color)]
         if {$anno_data(vframe,draw_frame)} {
            if {$anno_data(vframe,framewidth) < 1} {
               set anno_data(vframe,framewidth) 1
            }
         } 
         if {[info exist anno_data(vframe,level)] && $anno_data(vframe,level)} {
            set anno_data(vframe,draw_im) 1
         }
         $anno_data(picked_gm) modify $anno_data(picked_anno) \
              $anno_data(vframe,xp) $anno_data(vframe,yp) \
              $anno_data(vframe,xL) $anno_data(vframe,yL) \
              $anno_data(vframe,viewer) $anno_data(vframe,source) \
              $anno_data(vframe,draw_frame) $anno_data(vframe,framewidth) \
              $anno_data(vframe,draw_im) $anno_data(vframe,color)

         set vf $anno_data(vframe,viewer)
         if {[winfo exist $vf]  && ([winfo class $vf] == "Tdv_Viewer")} {
            $vf configure -borderwidth $anno_data(vframe,borderwidth) \
                          -bordercolor $anno_data(vframe,bordercolor) \
                          -relief $anno_data(vframe,relief) \
                          -background $anno_data(vframe,background)

            if {$anno_data(vframe,draw_im)} {
               vframe_going $vf forget
            } else {
               vframe_going $vf modify \
                     $anno_data(vframe,xp) $anno_data(vframe,yp) \
                     $anno_data(vframe,xL) $anno_data(vframe,yL)
            }
            $vf redraw
         }
      }
      line {
         set anno_data($what,color) [from_tkcolor_get value $anno_data($what,color)]
         eval $anno_data(picked_gm) modify $anno_data(picked_anno) \
              $anno_data(line,lnwidth) $anno_data(line,color) \
              $anno_data(line,arrow) $anno_data(line,pts)
      }
      pixmap {
      }
   }

   if [info exist anno_data($what,level)] {
      $anno_data(picked_gm) set_level $anno_data(picked_anno) $anno_data($what,level)
      if {[info exist anno_data(win)] && [winfo exist $anno_data(win)]} {
         set k [lsearch [$anno_data(win) list_bgimages] $anno_data(picked_gm)]
         if {$anno_data($what,level)} {
            if {$k < 0} {
               $anno_data(win) add_bgimage $anno_data(picked_gm)
            }
         } else {
            #set n [$anno_data(picked_gm) set_level]
            # if all annos in $anno_data(picked_gm) have level 0 and $k >= 0
            # $anno_data(win) remove_bgimage $anno_data(picked_gm)
         }
      }
   } 
}


proc update_anno_pick {{a ""} {noshow ""}} {
   global anno_data oned_data

   if {$a == ""} {
      set a [lindex [lindex [anno_action pick] 0] 0]
   }
   if {$a == ""} {return}
   set m $anno_data(model)
   set w $anno_data(win)
   set anno_data(picked_gm) $anno_data(gm,$m,$w)
   set anno_data(picked_anno) $a
   set rr [$anno_data(picked_gm) dump $a]
   set anno_data(picked_type) [lindex $rr 0]

   set level [$anno_data(picked_gm) set_level $a]

   set type $anno_data(picked_type)
   set tt ""; set to_raise ""
   switch -- $anno_data(picked_type) {
      text {
         set i 1
         foreach n {x y lcolor font text angle} {
            set anno_data(text,$n) [lindex $rr $i]
            incr i
         }
         set tt "Text Anno"
         set anno_data(text,level) $level
      }
      utftext {
         set i 1
         foreach n {x y lcolor font text} {
            set anno_data(utftext,$n) [lindex $rr $i]
            incr i
         }
         set tt "Utf Text Anno"
         set anno_data(utftext,level) $level
      }
      pixmap {
         set i 1
         foreach n {x y} {
            set anno_data(pixmap,$n) [lindex $rr $i]
            incr i
         }
         set tt "Pixmap Anno"
         set anno_data(pixmap,level) $level
      }
      mark {
         set i 1
         foreach n {lnwidth scolor x y ssize stype fill} {
            set anno_data(mark,$n) [lindex $rr $i]
            incr i
         }
         set tt "Mark Anno"
         set anno_data(mark,level) $level
      }
      line {
         set i 1
         foreach n {lnwidth color arrow} {
            set anno_data(line,$n) [lindex $rr $i]
            incr i
         }
         set anno_data(line,pts) [lrange $rr $i end]
         set tt "Line Anno"
         set anno_data(line,level) $level
      }
      box {
         set i 1
         foreach n {lnwidth color x0 y0 x1 y1 fill} {
            set anno_data(box,$n) [lindex $rr $i]
            incr i
         }
         set anno_data(box,xl) [expr abs($anno_data(box,x1) \
                                       - $anno_data(box,x0))]
         set anno_data(box,yl) [expr abs($anno_data(box,y1) \
                                       - $anno_data(box,y0))]
         set tt "Box2D Anno"
         set anno_data(box,level) $level
      }
      cmap {
         set i 1
         foreach n {obj xp yp} {
            set anno_data(cmap,$n) [lindex $rr $i]
            incr i
         }
         while {[lindex $rr $i] != ""} {
            switch -- [lindex $rr $i] {
               -type { 
                   incr i;  set linescale [lindex $rr $i]
                   incr i;  set discret   [lindex $rr $i]
                   incr i;  set uniform   [lindex $rr $i]
                   incr i; set anno_data(cmap,inverse) [lindex $rr $i]

                   set bartype ""
                   foreach n {linescale discret uniform} {
                      if [set $n] {set bartype $n}
                   }
                   if {$bartype == ""} {set bartype spectrum}

                   set anno_data(cmap,bartype) $bartype
               }
               -size {
                   incr i; set anno_data(cmap,width) [lindex $rr $i]
                   incr i; set anno_data(cmap,height) [lindex $rr $i]
               }
               -format {
                   incr i; set anno_data(cmap,format) [lindex $rr $i]
               }
            }
            incr i
         }
         set tt "Colormap Bar"
         set anno_data(cmap,level) $level
      }
      vframe {
         set i 1
         foreach n {xp yp xL yL viewer source \
                       draw_frame framewidth draw_im color} {
            set anno_data(vframe,$n) [lindex $rr $i]
            incr i
         }
         set tt VFrame

         set vf $anno_data(vframe,viewer)
         if {[winfo exist $vf]  && ([winfo class $vf] == "Tdv_Viewer")} {
            foreach n {borderwidth relief} {
               set anno_data(vframe,$n) [$vf get -$n]
            }
            foreach n {background bordercolor} {
               set col [$vf get -$n]
               if {[string index $col 0] != "#"} {
                  switch -- $col {
                     black  {set col "#000000"}
                     white  {set col "#ffffff"}
                     red    {set col "#ff0000"}
                     blue   {set col "#0000ff"}
                     green  {set col "#00ff00"}
                     yellow {set col "#ffff00"}
                     default {set col "#111111"}
                  }
               }
               set anno_data(vframe,$n) $col
            }
         }
         set anno_data(vframe,level) $level
      }
      circle {
         set i 1
         foreach n {lnwidth color x y rad fill} {
            set anno_data(circle,$n) [lindex $rr $i]
            incr i
         }
         set tt "Circle Anno"
         set anno_data(circle,level) $level
      } 
      group {
         set rr [$anno_data(picked_gm) get_group $a]
         foreach b [lrange $rr 1 end] {
            update_anno_pick $b
         }
      }
      legend {
         set oned_data(wid) .[lindex [split $anno_data(win) .] 1]
         set wid $oned_data(wid)
         if [info exist oned_data($wid,leg_id)] {
            update_oned_mg win
            if [info exist oned_data(wf,notebook)] {
               set to_raise [list $oned_data(wf,notebook) raise leg]
            }
         }
         set tt Legend
      }
      tickaxis {
         set oned_data(wid) .[lindex [split $anno_data(win) .] 1]
         set wid $oned_data(wid)
         if [info exist oned_data($wid,axis_id)] { 
            update_oned_mg win
            foreach c {x y p} {
               if {[info exist oned_data(axis_id,$c)] && \
                         ($oned_data(axis_id,$c) == $a)} {
                  set type $c 
               } 
            }
            if [info exist oned_data(wf,notebook)] {
               set ty $type; if {$type == "p"} {set ty x}
               set to_raise [list $oned_data(wf,notebook) raise $ty]
            }
            if {$type == "p"} {
               set type axisA
            } else {
               set type axis[string toupper $type]
            }
         }
         set tt "X and Y Axes"
      } 
      xyplot {
         set oned_data(wid) .[lindex [split $anno_data(win) .] 1]
         set wid $oned_data(wid)
         if [info exist oned_data($wid,plt_id)] {
            update_oned_mg win
            if [info exist oned_data(wf,notebook)] {
               set to_raise [list $oned_data(wf,notebook) raise plt]
            }
         }
         set type plot
         set tt "XY Plot"
      }
      default {
         set anno_data(picked_type) none
         set tt "  "
      }
   }

   if {$noshow != ""} {return ""}

   if {[info command update_anno_misc_cmd] != ""} {
      update_anno_misc_cmd modify_spec $type
   } else {
      if {$to_raise != ""} {eval $to_raise}
      if {[info exist anno_data(modify_win_m)] && ($tt != "")} {
         set wt [update_anno_mg win_list $anno_data(win)]
         $anno_data(modify_win_m) configure -text "$tt on $wt"
      }
      setup_anno_mg
      $anno_data(wf,notebook) raise spec
   }
}


proc update_anno_mg {what args} {
   global anno_data 
   global cmap_list

   if [info exist anno_data(model)] { set m $anno_data(model) }
   set w $anno_data(win)

   switch -- $what {
      win {
         if [winfo exist .anno_mg] {
            set wintitle [update_anno_mg win_list $w]
            top_title  .anno_mg "Anno for $wintitle"
         }
      }
      create {
         set type [lindex $args 0]

         if {[info command update_anno_misc_cmd] != ""} {
            if {$type != ""} {set anno_data(to_create) $type}
            update_anno_misc_cmd raise_def
            if {$type != ""} {unset anno_data(to_create)}
         } else {
            if {[info exist anno_data(def,win_m)] && \
                    [winfo exist $anno_data(def,win_m)]} {
               set wintitle [update_anno_mg win_list $anno_data(win)]
               set nn "Create anno on $wintitle"

               $anno_data(def,win_m) configure -text $nn
               $anno_data(wf,notebook) raise def
            }
         }
         if {$type != ""} {
            set anno_data(in_create) $type
            anno_action $type
            unset anno_data(in_create)
         }
      }
      raise - lower {
         if [info exist anno_data(picked_anno)] {
            if {[info commands $anno_data(picked_gm)] == ""} {return}
            $anno_data(picked_gm) $what $anno_data(picked_anno)
         }
      }
      init {
         set cm ""
         if [info exist cmap_list($w)] {set cm [lindex $cmap_list($w) 0]}
         if {$args != ""} {set cm [lindex $args 0]}

         anno_init $w $m $cm
      }
      save {
         if {![info exist anno_data(list,$m,$w)] || \
                      ![info exist anno_data(gm,$m,$w)] } {return}
         set file [futil_get_file "Enter a file name to save the\
                annotations to." [pwd] visual3_annotations]
         if {$file != ""} {
            set fp [open $file w]
            foreach a $anno_data(list,$m,$w) {
               puts $fp [$anno_data(gm,$m,$w) dump $a]
            }
            close $fp
         }
      }
      load {
         update_anno_mg init
         set file [futil_get_file "Select a file name to load the\
                   annotations from." [pwd] visual3_annotations]
         if {$file != ""} {
            set fp [open $file r]
            while {![eof $fp]} {
                set line [string trim [gets $fp]]
                if {($line != "") && ([string index $line 0] != "\#")} {
                   set a [eval $anno_data(gm,$m,$w) create $line]
                   lappend anno_data(list,$m,$w) $a
                }
            }
            close $fp
         }
      }
      win_list {
         if ![info exist anno_data(win_list)] {return ""}
         if {$args != ""} {
            set w1 [lindex $args 0]
            foreach {n v} $anno_data(win_list) {
               if {$v == $w1} {return $n}
            }
         }
         return $anno_data(win_list)
      }
      clear {
         if ![info exist anno_data(gm,$m,$w)] {return}

       # if ![winfo exist .anno_mg] {setup_anno_mg $w}

         set wintitle [update_anno_mg win_list $w]
         if {[tk_messageBox -title "Confirm To Delete" -parent . \
               -message "Delete all annotations\n in $wintitle window" \
               -type okcancel] == "ok"} {
           if {($m == 0) && [info exist anno_data($w,2d_annos)]} {
               foreach a $anno_data($w,2d_annos) {
                  if {[$anno_data(gm,0,$w) isvalid $a] != ""} {
                     $anno_data(gm,0,$w) remove $a
                  }
               }
               set anno_data($w,2d_annos) ""
            }

            if ![info exist anno_data(list,$m,$w)] {return}
            set rr [$anno_data(gm,$m,$w) clear_by_type 31]
            set nn ""
            foreach a $anno_data(list,$m,$w) {
               if {[lsearch $rr $a] < 0} { lappend nn $a }
            }
            set anno_data(list,$m,$w) $nn
         }
      } 
   }
}

proc setup_anno_mg {{win ""} {map 1} {geo ""} {with_win_list 1}} {
   global anno_data env tcl_platform

   if {$win != ""} {
      set anno_data(win) $win
      if [winfo exist .anno_mg] { update_anno_mg win }
   }

   set mb_bb_nb [start_mg_top .anno_mg $map "Annotation Manager" withdraw]
   if {$mb_bb_nb == ""} {
      if {$geo != ""} {top_geometry .anno_mg $geo}
      return .anno_mg
   }

   set mb [lindex $mb_bb_nb 0]
   menubutton $mb.file -text File -underline 0 -menu  $mb.file.menu
   menubutton $mb.create -text Create -underline 0 -menu  $mb.create.menu
   menubutton $mb.edit  -text Edit  -underline 0 -menu  $mb.edit.menu
 # menubutton $mb.opt  -text Options -underline 0 -menu  $mb.opt.menu
   menubutton $mb.tool -text Tools -underline 0 -menu  $mb.tool.menu

   set fm [menu  $mb.file.menu -tearoff 0] 
   if {$with_win_list} {
      $fm add cascade -label "Select window" -menu $fm.menu
         menu $fm.menu -postcommand [list text_optionMenu_postcommand \
                $fm.menu anno_data(win) "update_anno_mg win_list" 0 \
                "update_anno_mg win"]
   }

   $fm add command -label "Save" -command "update_anno_mg save"
   $fm add command -label "Load" -command "update_anno_mg load"
   $fm add command -label "Clear" -command "update_anno_mg clear"

   set fw [menu $mb.create.menu -tearoff 0]  
   $fw add command -label "Text" -command "update_anno_mg create text"
   $fw add command -label "Utf Text" -command "update_anno_mg create utftext"
   $fw add command -label "Mark" -command "update_anno_mg create mark"
   $fw add command -label "Line" -command "update_anno_mg create line"
   $fw add command -label "Box" -command "update_anno_mg create box"
   $fw add command -label "Circle" -command "update_anno_mg create circle"
   $fw add command -label "Polygon" -command "update_anno_mg create polygon"
   $fw add command -label "Pixmap" -command "update_anno_mg create pixmap"
   $fw add command -label "Colormap Bar" \
                    -command "update_anno_mg create cmap"

 # $fw add command -label "Colormap" -command "update_anno_mg create cmap"

   set fe [menu $mb.edit.menu -tearoff 0] 
   $fe add command -label "Modify" -command "update_anno_pick"
 # $fe add command -label "Copy"   -command "anno_action copy"
   $fe add command -label "Move"   -command "anno_action move"
   $fe add command -label "Raise Specific"  -command "update_anno_mg raise"
   $fe add command -label "Lower Specific"  -command "update_anno_mg lower"
   $fe add command -label "Remove" -command "anno_action delete"

 # menu $mb.opt.menu  -tearoff 0
 # $mb.opt.menu add command -label "Options" -command "update_oned_mg opt"

 # pack $mb.file $mb.create $mb.edit $mb.opt -side left
   pack $mb.file $mb.create $mb.edit -side left

   set bb [lindex $mb_bb_nb 1]
   if {[info exist env(V3P_WITH_DISMISS)] || \
          (![info exist env(ICEM_ONE_WINDOW)] && \
                     ($tcl_platform(platform) == "unix"))} {
      Button $bb.dismiss -image [Bitmap::get close] -highlightthickness 0 \
        -takefocus 0 -relief link -borderwidth 1 -padx 1 -pady 1 \
        -helptext "Dismiss the window" -command "top_withdraw .anno_mg"

      pack $bb.dismiss -anchor e -side right
   }

   set notebook [lindex $mb_bb_nb 2]
   set anno_data(wf,notebook) $notebook

   set dfm [$notebook insert end def -text "Defaults"]
   set anno_data(wf,defaults) $dfm.fm
   set fwd [form_init_sub $anno_data(wf,defaults)]
   if {$fwd != ""} {
      control_anno_defaults_choiceframe $fwd
      form_finish
   }
   pack $anno_data(wf,defaults) -side top  -expand yes -fill both

   set sfm [$notebook insert end spec -text "Specific"]
   set anno_data(wf,specific) $sfm.fm
   set fws [form_init_sub $anno_data(wf,specific)]
   if {$fws != ""} {
      control_anno_specific_choiceframe $fws
      form_finish
   }
   pack $anno_data(wf,specific) -side top  -expand yes -fill both

 # set tfm [$notebook insert end text -text "Text"]
 # set anno_data(wf,text) $tfm.fm
 # set fwt [form_init_sub $anno_data(wf,text)]
 # if {$fwt != ""} {
 #    control_anno_text_choiceframe $fwt
 #    form_finish
 # }
 # pack $anno_data(wf,text) -side top  -expand yes -fill both

 # set mfm [$notebook insert end mark -text "Mark"]
 # set anno_data(wf,mark) $mfm.fm
 # set fwm [form_init_sub $anno_data(wf,mark)]
 # if {$fwm != ""} {
 #    control_anno_mark_choiceframe $fwm
 #    form_finish
 # }
 # pack $anno_data(wf,mark) -side top  -expand yes -fill both

   $notebook compute_size
   $notebook raise [$notebook page 0]

   update idletasks

   if {$geo == ""} {set geo -0-0}
   top_geometry .anno_mg $geo

   return .anno_mg
}


proc do_anno_cmd {w m what args} {
   global anno_data

   set anno_data(win) $w
   set anno_data(model) $m

   set anno_data(to_win) $w
   set rr ""

   switch -- $what {
      create {
         if {$args != ""} {
            set type [lindex $args 0]
            update_anno_mg create $type  
         }
      }
      move {
         anno_action move
      }
      delete {
         anno_action delete
      }
      pick_to_delete {
         set rr [anno_action pick_to_delete]
      }
      direct_delete {
         if [info exist anno_data(gm,$m,$w)] {
            set rr [eval anno_do_delete $w $anno_data(gm,$m,$w) \
                   $anno_data(button_events,$w) anno_data(list,$m,$w) \
                   delete $args]
         }
      }
      cancel_delete {
         if [info exist anno_data(gm,$m,$w)] {
            set rr [eval anno_do_delete $w $anno_data(gm,$m,$w) \
                   $anno_data(button_events,$w) anno_data(list,$m,$w) \
                   cancel $args]            
         }
      }
      modify {
         update_anno_pick
      }
      default {
         eval update_anno_mg $what $args
      }
   }

   catch {unset anno_data(to_win)}
   return $rr
}
