

proc vframe_create {src parent {name ""} {bg ""} args} {
   global mview_data anno_data gwin_list tdv_win_type

   if ![winfo exist $parent] {return ""}
   if {$src != ""} {
      if {![winfo exist $src] || \
          ([winfo class $src] != "Tdv_Viewer")} {return ""}
      if {$bg == ""} {set bg [$src get -background]}
   }

   set anno_data(win) $parent
   set anno_data(model) 0
   set anno_gm $anno_data(gm,0,$parent)

   if {[llength $args] < 4} {
      set an [lindex [anno_action box] 0]
      if {($an == "") || ($an == 0)} {return ""}
      set rr [$anno_gm dump $an]
      $anno_gm remove $an
      set p [lsearch $anno_data(list,0,$parent) $an]
      if {$p >= 0} {
         set anno_data(list,0,$parent) \
                  [lreplace $anno_data(list,0,$parent) $p $p]
      }
      if {[lindex $rr 0] != "box"} {return ""} 
      set xp [lindex $rr 3]
      set yp [lindex $rr 4]
      set x1 [lindex $rr 5]
      set y1 [lindex $rr 6]
      if {$x1 < $xp} {set k $x1; set x1 $xp; set xp $k}
      if {$y1 < $yp} {set k $y1; set y1 $yp; set yp $k}
      set ww [expr $x1 - $xp]
      set hh [expr $y1 - $yp]
   } else {
      set xp [lindex $args 0]
      set yp [lindex $args 1]
      set ww [lindex $args 2]
      set hh [lindex $args 3]
   }

   if ![info exist mview_data($parent,vframes)] {
      set mview_data($parent,vframes) ""
      set mview_data($parent,count) 0
      if [info exist mview_data($parent,parent)] {
         set mview_data($parent,top_w) $parent
      } else {
         set mview_data($parent,top_w) [winfo parent $parent]
      }
   }

   incr mview_data($parent,count)

   set top_w $mview_data($parent,top_w)
   if ![info exist mview_data(all_count)] {set mview_data(all_count) 0}
   set count [incr mview_data(all_count)]

   set win $top_w.mv$count
   if {$name == ""} {
      set pn [vframe_cmd $parent w_title]
      set name [question_box .text "VFrame Window Name" \
              "Enter a name for a new window" "$pn: View $mview_data($parent,count)"]
   }
 
   if {$src == ""} {
       OneDWin::reg $name $win $ww $hh $bg
       OneDWin::setup_view $win $name $ww $hh "" $bg
   } else {
      tdv_init $win $ww $hh [$src get -gsys] $bg \
          [$src get -debug] [$src get -dbuffer] [$src get -twod]

      tdv_dynamics_bindings $win
      bind $win <Control-Button-1> "tdv_post_cmd %W post_menu %R %X %Y"

      if [info exist gwin_list] { lappend gwin_list $name $win }

      anno_init_win $win $name button_states($win) \
                button_bindings($win) button_events($win)
      anno_init $win 0 ""

      if ![info exist mview_data($src,dups)] { set mview_data($src,dups) "" }
      lappend mview_data($src,dups) $win
   }

   lappend mview_data($parent,vframes) $name $win

   set mview_data($win,name) $name
   set mview_data($win,source) $src
   set mview_data($win,parent) $parent

   set mview_data($win,vfm) [$anno_gm create vframe $xp $yp $ww $hh $win $src]
   lappend anno_data(list,0,$parent) $mview_data($win,vfm)

   place $win -in $parent -x $xp -y $yp

   if {$src != ""} {
      vframe_cmd $win update
      tdv_home_init $win
      global v3p_vmax
      if [info exist v3p_vmax] { $win set_gvsize $v3p_vmax }
   }
   $win configure -bordercolor #aa8822 -borderwidth 3 -relief ridge

   return $win
}


proc vframe_cmd {w how args} {
   global tdv_no_redraw_ms tdv_dynamics_speed_thresh
   global mview_data gwin_list

   switch -- $how {
      count - vframes - dups - name - vfm - parent - source {
         if [info exist mview_data($w,$how)] {
            return $mview_data($w,$how)
         }
      }
      w_title {
         if [info exist mview_data($w,name)] {
            return $mview_data($w,name)
         } else {
            if [info exist gwin_list] {
               set n [lsearch $gwin_list $w]
               if {$n > 0} { return [lindex $gwin_list [expr $n -1]] }
            }
         }
         return ""
      }
      update {
         if ![info exist mview_data($w,source)] {return}
         set src $mview_data($w,source)
         if {![winfo exist $src] || \
             ([winfo class $src] != "Tdv_Viewer")} {return ""}

         $src enable_multiview
         $w enable_multiview

if 1 {
         if {$args != ""} {
            set im_tlist $args
         } else {
            set im_tlist [list stripsurf_im_ streams_im_ vppoints_]
         }
         foreach v [$src list_images] {
            foreach t $im_tlist {
               if [regexp ^$t* $v] { $w add_image $v }
            }
         }
} else {
         foreach v [$src list_images] {
            if ![$v get -gone_with_viewer] {
               $w add_image $v
            }
         }
}
         global v3p_vmax
         if [info exist v3p_vmax] { $w set_gvsize $v3p_vmax }
      }
      copy_1d {
         set src [lindex $args 0]

         OneDWin::create_plots_setup
         foreach {n p} $oned_data($src,plt_list) {
            
         }
      }
      clean {
         foreach v [$w list_images] {
            $w remove_image $v
         }
      }
      show {
         set  wsub [lindex $args 0]
         if ![winfo exist $wsub] {return}

         global anno_data
         if {![info exist anno_data(list,0,$w)] || \
                      ![info exist anno_data(gm,0,$w)] } {return}

         if {[$anno_data(gm,0,$w) isvalid $mview_data($wsub,vfm)] != ""} {
            $anno_data(gm,0,$w) visible $mview_data($wsub,vfm) 1
            set rr [$anno_data(gm,0,$w) dump $mview_data($wsub,vfm)]   
            if {[lindex $rr 0] != "vframe"} {return}
            set xp [lindex $rr 1]; set yp [lindex $rr 2]
            set xL [lindex $rr 3]; set yL [lindex $rr 4]
            vframe_going $wsub modify $xp $yp $xL $yL
         }
      }
      hide {
         if ![info exist mview_data($w,parent)] {return}
         set p $mview_data($w,parent)

         vframe_going $w forget
         global anno_data
         if {![info exist anno_data(list,0,$p)] || \
                      ![info exist anno_data(gm,0,$p)] } {return}

         if {[$anno_data(gm,0,$p) isvalid $mview_data($w,vfm)] != ""} {
            $anno_data(gm,0,$p) visible $mview_data($w,vfm) 0
         }
      }
      remove {
         if ![info exist mview_data($w,parent)] {return}
         set p $mview_data($w,parent)

         global anno_data
         if {![info exist anno_data(list,0,$p)] || \
                      ![info exist anno_data(gm,0,$p)] } {return}

         if {[lindex $args 0] != 1} {
            if {[tk_messageBox -title "Confirm To Delete" -parent $p \
                  -message "Delete Vframe $mview_data($w,name)"  \
                  -type okcancel] != "ok"} {return}
         }

         if {[$anno_data(gm,0,$p) isvalid $mview_data($w,vfm)] != ""} {
            for {set m 0} {$m < 2} {incr m} {
               if {[info exist anno_data(gm,$m,$w)] && \
                       ([info command $anno_data(gm,$m,$w)] != "")} {
                  catch {
                     $anno_data(gm,$m,$w) delete
                     unset anno_data(gm,$m,$w)
                  }
               }
            }
            update idletasks
            $anno_data(gm,0,$p) remove $mview_data($w,vfm)
         } 
      }
      default {
         return [eval vframe_going $w $how $args]
      }
   }
   return ""
}

proc vframe_going {w how args} {
   global tdv_no_redraw_ms tdv_dynamics_speed_thresh
   global mview_data gwin_list

   switch -- $how {
      raise {
         #raise $w
         top_raise $w
         if [info exist mview_data($w,parent)] {
            set parent $mview_data($w,parent)
            if [info exist mview_data($parent,vframes)] {
               set n [lsearch $mview_data($parent,vframes) $w]
               if {$n > 0} {
                  set n1 [expr $n -1]
                  set fn [lindex $mview_data($parent,vframes) $n1] 
                  set fv [lindex $mview_data($parent,vframes) $n]
                  set mview_data($parent,vframes) \
                       [lreplace $mview_data($parent,vframes) $n1 $n]
                  lappend mview_data($parent,vframes) $fn $fv
               }
            }
         }
      }
      lower {
         if [info exist mview_data($w,parent)] {
            set parent $mview_data($w,parent)
            if [info exist mview_data($parent,vframes)] {
               set n [lsearch $mview_data($parent,vframes) $w]
               if {$n > 2} {
                  set n1 [expr $n -1]
                  set fn [lindex $mview_data($parent,vframes) $n1] 
                  set fv [lindex $mview_data($parent,vframes) $n]
                  set mview_data($parent,vframes) \
                       [lreplace $mview_data($parent,vframes) $n1 $n]
                  set mview_data($parent,vframes) \
                       [linsert $mview_data($parent,vframes) 0  $fn $fv]
                  lower $w [lindex $mview_data($parent,vframes) 3]
               }
            }
         }
      }
      remove {
         vframe_going $w forget
         $w delete
          
         if [info exist gwin_list] {
            set n [lsearch $gwin_list $w]
            if {$n > 0} {
               set gwin_list [lreplace $gwin_list [expr $n -1] $n]
            }
         }

         if [info exist mview_data($w,parent)] {
            set parent $mview_data($w,parent)
            if [info exist mview_data($parent,vframes)] {
               set n [lsearch $mview_data($parent,vframes) $w]
               if {$n > 0} {
                  set mview_data($parent,vframes) \
                       [lreplace $mview_data($parent,vframes) [expr $n -1] $n]
               }
            }
            unset mview_data($w,parent)
         }
         if [info exist mview_data($w,source)] {
            set src $mview_data($w,source)
            if [info exist mview_data($src,dups)] {
               set n [lsearch $mview_data($src,dups)]
               if {$n > -1} {
                  set mview_data($src,dups) \
                         [lreplace mview_data($src,dups) $n $n]
               }
            }
            unset mview_data($w,source)
         }
         subarray delete mview_data $w
         # there still something left in tdv_*
      }
      begin {
         if [info exist mview_data($w,end_op_after)] {
            after cancel $mview_data($w,end_op_after)
            unset mview_data($w,end_op_after)
         }
         if {$tdv_no_redraw_ms && ([$w get -last_speed] > $tdv_no_redraw_ms)} {
            $w configure -active 0
            set mview_data($w,going) 0
         } else {
            set mview_data($w,going) 1
         }
         if ![$w get -in_dynamics] {
            set max_sp $tdv_dynamics_speed_thresh
            if {[$w get -last_speed] > $max_sp} {
               $w configure -in_dynamics 2
            } else {
               $w configure -in_dynamics 1
            }
         }
      }
      end {
         if {[info exists mview_data($w,going)] && !$mview_data($w,going)} {
            $w configure -active 1
         }
         catch {unset mview_data($w,going)}
         set cmd "$w configure -in_dynamics 0; $w redraw"
         if ![info exists mview_data($w,end_op_after)] {
            set  mview_data($w,end_op_after) [after 500 $cmd]
         }
      }
      move {
         if {[llength $args] > 1} {
            place configure $w -x [lindex $args 0] -y [lindex $args 1]
         }
      }
      modify - zoom {
         if {[llength $args] > 3} {
            if {[place info $w] == ""} {
               place $w -in $mview_data($w,parent) \
                           -x [lindex $args 0] -y [lindex $args 1]
            } else {
               place configure $w -x [lindex $args 0] -y [lindex $args 1]
            }
            $w configure -width [lindex $args 2] -height [lindex $args 3]
         } 
      }
      set_pixmap {
         top_raise $w
         update idletasks

         $w just_redraw 1

         if {[lindex $args 0] != ""} { eval $w set_pixmap $args }
      }
      forget {
         if {[place info $w] != ""} { place forget $w }
      }
   }
   return ""
}
