

proc ononedLineDEZLoad {} {
   variable npts
   if {![info exist npts] || \
       ![string is integer -strict $npts]} {
      set npts 50
   }

   variable name
   variable iline
   if ![info exist iline] { set iline 1 }

   set name "Line $iline"
}

proc ononedLineDEZApply {} {
   variable npts
   variable p0
   variable p1
   variable name
 
   V3_vppoint create $name
   V3_vppoint add_segment $npts $p0 $p1
   V3_vppoint interpolate
   V3_vppoint set_xvar arc_length "Arc Length" 0

   V3_vppoint current
}

proc ononedWinDEZLoad {} {

   variable onedXYWinTitle
   if {![info exist onedXYWinTitle] || ($onedXYWinTitle == "")} {
      set onedXYWinTitle "XY Window"
   }

   variable onedPolarWinTitle
   if {[info exist onedPolarWinTitle] || ($onedPolarWinTitle == "")} {
      set onedPolarWinTitle "Polar Window"
   }

   variable onedWinEXGVal
   if {![info exist onedWinEXGVal] || ($onedWinEXGVal == "")} {
      set onedWinEXGVal 0
   }
}

proc ononedWinDEZApply {} {

   set wintype [onedWinDEZ get onedWinEXGVal]

   switch -- $wintype {
      0 {
         set wtitle [onedWinDEZ get onedXYWinTitle]
         V3_oned_win_cmd create xy $wtitle 
      }
      1 {
         set wtitle [onedWinDEZ get onedPolarWinTitle]
         V3_oned_win_cmd create polar $wtitle
      }
   }
}

proc aie_new_1d_win {name ax_type {width 500} {height 500} {bg ""} {xtitle ""}} {
   global oned_data black_and_white tdv_workspace

   if {$width == ""} {set width 500}
   if {$height == ""} {set height 500}

   if ![info exist OneDWin::count] { OneDWin::init }
   set count [incr OneDWin::count]

   if {$bg == ""} {
      set bg black
      if {$black_and_white} {set bg white}
   }

   set w $tdv_workspace.1d_$count

   OneDWin::reg $name $w $width $height $bg
   OneDWin::setup_view $w $name $width $height "" $bg

   workspace_add $w $name
   update idletasks

   global tdv_current_viewer
   set tdv_current_viewer $w

   set oned_data(wid) $w

   OneDWin::cmd $oned_data(wid) win

 # axes

   if {$ax_type == "polar"} {
      OneDWin::create_polar
   } else {
      OneDWin::create_axis $xtitle
   }

 # legend

   OneDWin::create_leg

 # ------ add to the tree

   set top_tree [set ::AIEnv::GUIManagement::Tree::aieTree]
   if [$top_tree exists modelNode] { $top_tree closetree modelNode }

   set onednode OnedWin$count
   $top_tree insert end root $onednode -text $name -open 1 \
                 -data "onedwin $w *"
   

   global dset_info
   set dset_info($w,vframe_root) [VFrameTree::init $top_tree $onednode \
                            "Multi-Views" $w -varName mview_data \
          -on_image $::AIEnv::GUIManagement::Tree::VisibilityImages(isVisImg) \
          -off_image $::AIEnv::GUIManagement::Tree::VisibilityImages(notVisImg)]


   $top_tree insert end $onednode OnedAxes$count -text "Axes" -open 1 \
                 -data "onedaxes axes *"
   if {$ax_type == "polar"} {
      $top_tree insert end OnedAxes$count OnedX$count -text "Arc" \
                 -open 1 -data "onedaxis p *"
      $top_tree insert end OnedAxes$count OnedY$count -text "Radius" \
                 -open 1 -data "onedaxis y *"
   } else {
      $top_tree insert end OnedAxes$count OnedX$count -text "X axis" \
                 -open 1 -data "onedaxis x *"
      $top_tree insert end OnedAxes$count OnedY$count -text "Y axis" \
                 -open 1 -data "onedaxis y *"
   }

   $top_tree insert end $onednode OnedTx$count -text "X Title" \
                 -open 1 -data "onedaxtitle x *"
   $top_tree insert end $onednode OnedTy$count -text "Y Title" \
                 -open 1 -data "onedaxtitle y *"

   $top_tree insert end $onednode OnedLeg$count -text "Legend" -open 1 \
                 -data "onedlegend legend *"

   $top_tree insert end $onednode OnedPlots$count -text "Plots" -open 1 \
                 -data "onedplots plots *"

   set oned_data($w,topNode) $onednode
   set oned_data($w,plotsNode) OnedPlots$count
   set oned_data($w,legendNode) OnedLeg$count
   set oned_data($w,axesNode) OnedAxes$count
   set oned_data($w,txNode) OnedTx$count
   set oned_data($w,tyNode) OnedTy$count

   return $w
}

proc aie_delete_1d_win {w} {
   global oned_data anno_data

   if [info exist oned_data($w,topNode)] {
      set top_tree [set ::AIEnv::GUIManagement::Tree::aieTree]
      $top_tree delete $oned_data($w,topNode)  
   }

   V3_oned_win_cmd delete_win $w
}

proc create_xyplot_after_cmd {name w plt vp} {
   global oned_data

   if ![info exist oned_data($w,plotsNode)] {return ""}

   set top_tree [set ::AIEnv::GUIManagement::Tree::aieTree]

   if [$top_tree exists OnedPlot$plt] { $top_tree delete OnedPlot$plt } 

   $top_tree insert end $oned_data($w,plotsNode) OnedPlot$plt \
              -text "$name" -open 1 -data "onedplot $plt *"

   return OnedPlot$plt
}

proc delete_xyplot_after_cmd {w plt vp} {
  global oned_data

  set top_tree [set ::AIEnv::GUIManagement::Tree::aieTree]

  if [$top_tree exists OnedPlot$plt] { $top_tree delete OnedPlot$plt }
}  


proc aie_modify_legend {what args} {
   global oned_data

   if {$what == "var"} {
      set val [onedLegendDEZ get oned_data:leg:vn_on]
      if {$val != $oned_data(leg,vn_on)} {set oned_data(leg,vn_on) $val}
      ::AIEnv::GUIManagement::ToggleGrey onedLegendVn [expr !$val]

      if {$what == "var"} { OneDWin::update_leg var}
   } else {
      set n 0
      foreach s {fr_width fr_padx fr_pady fr_on} {
         set val [onedLegendDEZ get oned_data:leg:$s]
         if {$val != $oned_data(leg,$s)} {incr n; set oned_data(leg,$s) $val}
      }
      ::AIEnv::GUIManagement::ToggleGrey onedLegendFr [expr !$val]

      if {$n || ($what == "frame")} { OneDWin::update_leg frame }
   }
}


proc ononedLegendDEZLoad {} {
   global oned_data
   foreach s {fr_on fr_width fr_padx fr_pady vn_on} {
      variable oned_data:leg:$s; set oned_data:leg:$s $oned_data(leg,$s)
   }

   ::AIEnv::GUIManagement::ToggleGrey onedLegendVn [expr !$oned_data(leg,vn_on)]
   ::AIEnv::GUIManagement::ToggleGrey onedLegendFr [expr !$oned_data(leg,fr_on)]
}

proc ononedLegendDEZApply {} {
   aie_modify_legend ""
}

proc aie_modify_xyplots {what args} {
   global oned_data

   set rr [list line {lwidth line} symbol {symbols ssize} \
                axis {axisfit fit_x0 fit_x1 fit_y0 fit_y1}]
   if {[onedPlotDEZ get oned_data:plt:x_type] > -1} {
      lappend rr x_type {x_type A B C D dist0 axis center zero}
   }

   set to_do ""
   foreach {t mm} $rr {
      set n 0
      foreach s $mm {
         set val [onedPlotDEZ get oned_data:plt:$s]
         if {$val != $oned_data(plt,$s)} {incr n; set oned_data(plt,$s)  $val}
      }
      if {$n} {lappend to_do $t}
   }

   if {[lsearch $to_do $what] < 0} {lappend to_do $what}

   ::AIEnv::GUIManagement::ToggleGrey onedPlotSymFm [expr !$oned_data(plt,symbols)]
   ::AIEnv::GUIManagement::ToggleGrey onedPlotLnFm [expr !$oned_data(plt,line)]
   ::AIEnv::GUIManagement::ToggleGrey onedPlotFitFm [expr !$oned_data(plt,axisfit)]

   update idletasks
   foreach d $to_do { OneDWin::modify_plots $d }
}

proc ononedPlotDEZLoad {} {
   global oned_data
   foreach s {symbols ssize line lwidth dist0 A B C D axis \
              center zero axisfit fit_x0 fit_x1 fit_y0 fit_y1} {
      variable oned_data:plt:$s; set oned_data:plt:$s $oned_data(plt,$s)
   }
   set oned_data(plt,plt_list) [OneDWin::update_plots plt_list]
   set oned_data(plt,y_list) [OneDWin::update_plots y_type]

   variable oned_data:plt:x_type
   if {$oned_data(plt,x_type) > 7} {
      set oned_data:plt:x_type -1
      ::AIEnv::GUIManagement::ToggleGrey onedPlotXTy 1
   } else {
      set oned_data:plt:x_type $oned_data(plt,x_type)
      ::AIEnv::GUIManagement::ToggleGrey onedPlotXTy 0
   }
   ::AIEnv::GUIManagement::ToggleGrey onedPlotSymFm [expr !$oned_data(plt,symbols)]
   ::AIEnv::GUIManagement::ToggleGrey onedPlotLnFm [expr !$oned_data(plt,line)]
   ::AIEnv::GUIManagement::ToggleGrey onedPlotFitFm [expr !$oned_data(plt,axisfit)]

   update idletasks
}

proc ononedPlotDEZApply {} {
   aie_modify_xyplots ""
}

proc ononedAxisADEZLoad {} {
   global oned_data
   foreach s {xfit ang_0 ang_1 ang_ax} {
      variable oned_data:$s; set oned_data:$s $oned_data($s)
   }
   foreach s {linewidth grid_on gwidth mgrid_on mgwidth \
              majorstep subtick ticksize mtom} {
      variable oned_data:p$s; set oned_data:p$s $oned_data(p$s)
   }

   ::AIEnv::GUIManagement::ToggleGrey onedAxisAMjG [expr !$oned_data(pgrid_on)]
   ::AIEnv::GUIManagement::ToggleGrey onedAxisAMnG [expr !$oned_data(pmgrid_on)]
}
 
proc ononedAxisADEZApply {} {
   aie_modify_axis "" p
}


proc aie_modify_axis {what x args} {
   global oned_data
 
   set do_list "";  set rrs "";  set chks ""

   if {$x == "p"} {
      set Name onedAxisADEZ
      lappend chks xfit

      set n 0
      foreach s {ang_0 ang_1 ang_ax} {
         set val [$Name get oned_data:$s]
         if {$val != $oned_data($s)} {incr n; set oned_data($s) $val}
      }
      if {$n} {lappend do_list range}
   } else {
      set Name onedAxis[string toupper $x]DEZ
      lappend chks ${x}fit
      lappend rrs pos {_p0 _p1} range {0 1} number {numfmt numpos}
   }

   lappend chks ${x}grid_on ${x}mgrid_on
   foreach s $chks {
      set val [$Name get oned_data:$s]
      if {$val != $oned_data($s)} {set oned_data($s)  $val}
   }
   ::AIEnv::GUIManagement::ToggleGrey [string range $Name 0 8]MjG \
                  [expr !$oned_data(${x}grid_on)]
   ::AIEnv::GUIManagement::ToggleGrey [string range $Name 0 8]MnG \
                  [expr !$oned_data(${x}mgrid_on)]
 
   lappend rrs line {linewidth}
   lappend rrs grid {gwidth mgwidth}
   lappend rrs tick {majorstep subtick ticksize mtom}

   foreach {t mm} $rrs {
      set n 0
      foreach s $mm {
         set val [$Name get oned_data:${x}$s]
         if {$val != $oned_data(${x}$s)} {incr n; set oned_data(${x}$s)  $val}
      }
      if {$n} {lappend do_list $t}
   }
   
   if {$what != ""} {lappend do_list $what}

   foreach d $do_list {
      OneDWin::modify_axis $d $x
   }
}

proc ononedAxisXDEZLoad {} {
   global oned_data
   foreach s {_p0 _p1 fit 0 1 linewidth grid_on gwidth mgrid_on \
              mgwidth majorstep subtick ticksize mtom numfmt numpos} {
      variable oned_data:x$s; set oned_data:x$s $oned_data(x$s)
   }

   ::AIEnv::GUIManagement::ToggleGrey onedAxisXMjG [expr !$oned_data(xgrid_on)]
   ::AIEnv::GUIManagement::ToggleGrey onedAxisXMnG [expr !$oned_data(xmgrid_on)]
}

proc ononedAxisXDEZApply {} {
   aie_modify_axis "" x
}

proc ononedAxisYDEZLoad {} {
   global oned_data
   foreach s {_p0 _p1 fit 0 1 linewidth grid_on gwidth mgrid_on \
              mgwidth majorstep subtick ticksize mtom numfmt numpos} {
      variable oned_data:y$s; set oned_data:y$s $oned_data(y$s)
   }

   ::AIEnv::GUIManagement::ToggleGrey onedAxisYMjG [expr !$oned_data(ygrid_on)]
   ::AIEnv::GUIManagement::ToggleGrey onedAxisYMnG [expr !$oned_data(ymgrid_on)]
}
 
proc ononedAxisYDEZApply {} {
   aie_modify_axis "" y
}


proc aie_oned_iconbar {what args} {
   switch -- $what {
      exg    {onedWinDEZ show}
      axis_p {onedAxisADEZ show}
      axis_x {onedAxisXDEZ show}
      axis_y {onedAxisYDEZ show}
      legend {onedLegendDEZ show}
      plot   {onedPlotDEZ show}
   }
}


proc aie_oned_tree {what node args} {
   global oned_data tdv_current_viewer
   
   set top_tree [set ::AIEnv::GUIManagement::Tree::aieTree]
   set topnode $node
   while {$topnode != "root"} {
      if [scan $topnode "OnedWin%d" kj] {break}
      set topnode [$top_tree parent $topnode]
   }
   if {$topnode == "root"} {return ""}
   
   set dd [$top_tree itemcget $topnode -data]
   set w [lindex $dd 1]
   if ![winfo exist $w] {return ""}

   OneDWin::cmd $w win

   if {$tdv_current_viewer != $w} {
      workspace_add $w [$top_tree itemcget $topnode -text]
      set tdv_current_viewer $w
   }
   
   switch -- $what {
      modify_axtitle {
         annoModifyUtftextDEZ cancel
         OneDWin::cmd $w title [lindex $args 0]
      }
      move_axtitle {
         set c [string index [lindex $args 0] 0]
         OneDWin::cmd $w shift ${c}title
      }
      modify_axis {
         set C [string toupper [lindex $args 0]]
         if {$C == "P"} {set C A}
         if {[info command onedAxis${C}DEZ] != ""} {
            onedAxis${C}DEZ show 
         } 
      }
      move_axis { OneDWin::cmd $w shift [lindex $args 0]   }
      zoomout_axes { OneDWin::cmd $w zoomout_axes } 
      scaling_axes_to { OneDWin::cmd $w scaling_axes [lindex $args 0] }
      scale_axes { OneDWin::cmd $w scale axes }
      move_axes { OneDWin::cmd $w shift both }
      modify_legend {
         onedLegendDEZ show
      }
      move_legend {
         OneDWin::cmd $w shift leg
      }
      zoomin_plots {
         OneDWin::cmd $w zoomin_plots
         onedPlotDEZ show
      }
      scaling_plots_to { OneDWin::cmd $w scaling_plots [lindex $args 0] }
      scale_plots { OneDWin::cmd $w scale plots }
      modify_plot {
         if {$oned_data(plt_id) != [lindex $args 0]} {
            set oned_data(plt_id) [lindex $args 0]
            OneDWin::update_plots plt_id 
         }

         onedPlotDEZ show
      }
      rename_plot {
         if {$oned_data(plt_id) != [lindex $args 0]} {
            set oned_data(plt_id) [lindex $args 0]
            OneDWin::update_plots plt_id
         }
         set vp $oned_data(plt,vppts)

         global v3pts_data
         if [info exist v3pts_data($vp,node)] {
            V3_vppoint current $vp
            aie_v3pts_tree rename $v3pts_data($vp,node) $vp
         } else {
            OneDWin::modify_plots rename
         }
         $top_tree itemconfigure $node -text [$vp get -title]
      }
      save_plot {
         if {$oned_data(plt_id) != [lindex $args 0]} {
            set oned_data(plt_id) [lindex $args 0]
            OneDWin::update_plots plt_id
         }
         OneDWin::modify_plots save_xydata
      }
      delete_plot {
         if {$oned_data(plt_id) != [lindex $args 0]} {
            set oned_data(plt_id) [lindex $args 0]
            OneDWin::update_plots plt_id
         }
         #V3_oned_win_cmd delete_plot
         OneDWin::modify_plots delete
         $top_tree delete $node
      }
   }
}

proc aie_oned_win_menu {mm theNode win} {
   global oned_data

 # set m [tdv_post_menu_cmd $win]
 # set i0 0; set i1 6
 # set dd [$m cget -tearoff]
 # incr i0 $dd; incr i1 $dd

 # copy_menu_entries ar $m $mm $i0 $i1

   v3p_win_menu_add $mm $win vframe "Multi-Views"
   v3p_win_menu_add $mm $win anno "Annotation"
   v3p_win_menu_add $mm $win hardcopy
   v3p_win_menu_add $mm $win record "Record ... "

   $mm add command -label "Fit window" \
                 -command "OneDWin::cmd $win home_fit"
   $mm add command -label "Move together" \
                 -command "OneDWin::cmd $win shift all"
   $mm add command -label "Pick and Move" \
                 -command "OneDWin::cmd $win move"
   return $mm 
}

proc aie_oned_plots_menu {mm theNode nodeinfo} {
   global oned_data

   $mm add command -label "Zoom in plots" -command \
             [list aie_oned_tree zoomin_plots $theNode $nodeinfo]
   $mm add cascade -label "Scale plots to" -menu $mm.splt
      catch {destroy $mm.splt}
      menu $mm.splt -tearoff 0
      foreach p {50 75 120 150 200} {
         $mm.splt add command -label "${p}%" -command \
             [list aie_oned_tree scaling_plots_to $theNode [expr $p /100.0]]
      }
   $mm add command -label "Scale with mouse moving" -command \
             [list aie_oned_tree scale_plots $theNode]

   set oned_data(plots,menu) $mm
   return $mm
}

proc aie_oned_plot_menu {mm theNode plt_id} {
   global oned_data

   $mm add command -label "Modify" -command \
             [list aie_oned_tree modify_plot $theNode $plt_id]
   $mm add command -label "Rename" -command \
             [list aie_oned_tree rename_plot $theNode $plt_id]
   $mm add command -label "Save" -command \
             [list aie_oned_tree save_plot $theNode $plt_id]
   $mm add command -label "Delete" -command \
             [list aie_oned_tree delete_plot $theNode $plt_id]

   set oned_data(plot,menu) $mm
   return $mm
}

proc aie_oned_legend_menu {mm theNode nodeinfo} {
   global oned_data

   $mm add command -label "Modify" -command \
             [list aie_oned_tree modify_legend $theNode $nodeinfo]
   $mm add command -label "Move" -command \
             [list aie_oned_tree move_legend $theNode $nodeinfo]

   set oned_data(legend,menu) $mm
   return $mm
}

proc aie_oned_axis_menu {mm theNode c} {
   global oned_data

   $mm add command -label "Modify" -command \
             [list aie_oned_tree modify_axis $theNode $c]
   $mm add command -label "Move" -command \
             [list aie_oned_tree move_axis $theNode $c]

   set oned_data(axis,menu) $mm
   return $mm
}

proc aie_oned_axes_menu {mm theNode nodeinfo} {
   global oned_data

   $mm add command -label "Zoom out axes" -command \
             [list aie_oned_tree zoomout_axes $theNode $nodeinfo]
   $mm add cascade -label "Scale axes to" -menu $mm.sax
      catch {destroy $mm.sax} 
      menu $mm.sax -tearoff 0
      foreach p {50 75 120 150 200} {
         $mm.sax add command -label "${p}%" -command \
             [list aie_oned_tree scaling_axes_to $theNode [expr $p /100.0]]
      }
   $mm add command -label "Scale with mouse moving" -command \
             [list aie_oned_tree scale_axes $theNode]
   $mm add command -label "Move axes" -command \
             [list aie_oned_tree move_axes $theNode $nodeinfo]

   set oned_data(axes,menu) $mm
   return $mm
}

proc aie_oned_axtitle_menu {mm theNode c} {
   global oned_data

   $mm add command -label "Modify" -command \
             [list aie_oned_tree modify_axtitle $theNode $c]
   $mm add command -label "Move" -command \
             [list aie_oned_tree move_axtitle $theNode $c]

   set oned_data(axtitle,menu) $mm
   return $mm
}


proc aie_editnode_onedaxis {theTree theNode nodeinfo} {
   aie_oned_tree modify_axis $theNode $nodeinfo
}

proc aie_editnode_onedlegend {theTree theNode nodeinfo} {
   aie_oned_tree modify_legend $theNode $nodeinfo
}

proc aie_editnode_onedaxtitle {theTree theNode nodeinfo} {
   aie_oned_tree modify_axtitle $theNode $nodeinfo
}

proc aie_editnode_onedplot {theTree theNode plt_id} {
   aie_oned_tree modify_plot $theNode $plt_id
}
