
proc OneDWin::update_vppt_fm {what args} {
   global oned_data
   variable wid

   if ![info exist oned_data(dat,vppts)] {return ""}
   set vp $oned_data(dat,vppts)

   switch -- $what {
      listall {
         set rr [OneDWin::update_vppoints list]
         foreach vp $oned_data(plotted_vppts_list) {
            if {[info commands $vp] != ""} {
               lappend rr [$vp get -title] $vp
            }
         }
         return $rr
      }
      list3d {
         set rr ""
         foreach {n vp} [update_vppt_fm listall] {
            if {[lindex [$vp info] 1] == 1} {lappend rr $n $vp}
         }
         return $rr
      }
      list_win {
         set rr [update_oned_mg list_win]
         lappend rr "New 1D window" new "New polar window" polar
         return $rr
      }
      xyplot {
         if {[info commands $vp] == ""} {return ""}
         if [info exist oned_data($vp,plots)] {
            set n [$vp get -title]
            if {[tk_messageBox -title "Confirm To Re-plot" -parent .oned_mg \
                  -message "$n plotted already. Duplicate one more times ?" \
                  -type okcancel] != "ok"} {
               return ""
            }
         } 

         if {$oned_data(dat,plt_win) == "polar"} {
            update_oned_mg polar_win
         } elseif {$oned_data(dat,plt_win) == "new"} {
            update_oned_mg new_win
         } else {
            set oned_data(wid) $oned_data(dat,plt_win)
            update_oned_mg win
         }
         OneDWin::update_vppoints xyplot $vp
      }
      cur_vp {
         if {[info commands $vp] == ""} {return ""}
         foreach n {line_draw line_color line_width cmap updated \
                    dots_draw dots_size dots_color vec_draw \
                    vec_color vec_size arrow_size} {
            set oned_data(dat,$n) [$vp get -$n]
         }
         set rr [$vp info]
         set oned_data(dat,ready)  [lindex $rr 0]
         set oned_data(dat,is3d)   [lindex $rr 1]
         set oned_data(dat,ispair) [lindex $rr 2]

         if {[info exist oned_data(dat,vp_text)] && \
                 [winfo exist $oned_data(dat,vp_text)]} {
            if ![info exist oned_data($vp,plots)] {
               if $oned_data(dat,ready) {
                  $oned_data(dat,vp_text) configure -text \
                       "Ready to make a XY plot"
               } else {
                  $oned_data(dat,vp_text) configure -text \
                       "Need to setup X and Y values"
               }
            } else {
               set txt ""
               foreach {w plt} $oned_data($vp,plots) {
                  set j [lsearch [update_oned_mg list_win] $w]
                  if {$j > 0} {
                     set n [lindex [update_oned_mg list_win] [expr $j -1]]
                     set txt [concat $txt $n]
                  }
               }
               if {$txt != ""} { set txt [concat "Ploted in : " $txt] }
               $oned_data(dat,vp_text) configure -text $txt
            } 
         }

         set oned_data(dat,win) .v.view
         if {[lsearch [$oned_data(dat,win) list_images] $vp] < 0} {
            $oned_data(dat,win) add_image $vp 
            $vp configure -updated 3 
            set oned_data(dat,updated) 3

            global cmap_cr
            if [info exist cmap_cr] { $vp configure -cmap $cmap_cr }
         }
         set oned_data(dat,plt_win) ""
      }
      line_draw - line_color - line_width - dots_draw \
                - dots_color - dots_size - vec_draw - vec_color \
                - vec_size - arrow_size {
         if {[info commands $vp] == ""} {return ""}
         if {[lindex [$vp info] 1] != 1} {
            set n [$vp get -title]
            tk_messageBox -title "Warning" -parent .oned_mg \
                 -message "$n is not a 3D curve, no drawing" -type ok
         }
         $vp configure -$what $oned_data(dat,$what)
      }
      list_vec {
         if {[info commands $vp] == ""} {return ""}
         return [join [$vp list_vars 3]]
      }
      interpolate_vars {
         if {[info command V3_vppoint] == ""} { return ""}

         return [eval V3_vppoint interpolate $vp $args]
      }
      int_res {
         if {[info command V3_vppoint] == ""} { return ""}

         set ct [lindex $args 0]
         switch -- $ct {
            do {
               set is [V3_variables_get vn2ind_s $oned_data(dat,int_svn)]
               if {$is == ""} {update_vppt_fm int_res def_sv}
               set iv [V3_variables_get vn2ind_v $oned_data(dat,int_vvn)]
               if {$iv == ""} {update_vppt_fm int_res def_vv}

               return [V3_vppoint interpolate $vp $is $iv]  
            }
            def_sv - def_vv {
               set c [string index $ct 4]
               set rr [V3_variables_get current_${c}_subs]
               if {$rr != ""} {
                  set oned_data(dat,int_${c}vn) [lindex $rr 0]
                  set oned_data(dat,int_${c}vnsub) [lindex $rr 1]
                  set i 1
                  foreach s [lrange $rr 2 end] {
                     set oned_data(dat,int_${c}vnsub,$i) [lindex $s 0]
                     incr i
                  }
               }
            }
            sv - vv {
               set vn [V3_variables_get \
                          complex_partname $oned_data(dat,int_${ct}nsub)]
               for {set i 1} {$i <= [V3_variables_get nsubnames} {incr i} {
                  set vn ${vn}@$oned_data(dat,int_${ct}nsub,$i)
               }
               set oned_data(dat,int_${ct}n) $vn
            }
         }
      }
      lnint {
         if {[info commands $vp] == ""} {return ""}
         set yt [expr ($oned_data(dat,vec) << 8) + 0x0f]
         set oned_data(dat,lnint) [$vp get_veclineint $yt]
         return $oned_data(dat,lnint)
      }
      create_param {
      }
      refine_segments {
         set more_pts [lindex $args 1]
         set end_pts  [lindex $args 2]

         if {[llength $end_pts] < 2} {return ""}
         set oned_data(dat,vp_tobe) \
                     [update_vppoints create [lindex $args 0]]
         if {$oned_data(dat,vp_tobe) == ""} {return ""}
         set vp $oned_data(dat,vp_tobe)

         set dd [expr 1.0 / ($more_pts + 1.0)]
         set pt0 [lindex $end_pts 0]
         $vp add_pt_only $pt0
         foreach pt [lrange $end_pts 1 end] {
            for {set j 1} {$j <= $more_pts} {incr j} {
                  set ptm [eval coords_misc interp $pt0 $pt [expr $j * $dd]]
                  $vp add_pt_only $ptm
            }
            $vp add_pt_only $pt
            set pt0 $pt
         }

         global opt_data
         if {$opt_data(xyplot_line_draw) || $opt_data(xyplot_dots_draw)} {

            .v.view add_image $vp

            if {$opt_data(xyplot_line_draw)} { $vp configure -line_draw 2 }
            if {$opt_data(xyplot_dots_draw)} { $vp configure -dots_draw 2 }
         }

         return $vp
      }
      exlines {
         if {[llength $oned_data(dat,exlines_nodes)] < 2} {return ""}
         set oned_data(dat,vp_tobe) \
                     [update_vppoints create [lindex $args 0]]
         if {$oned_data(dat,vp_tobe) == ""} {return ""}
         if {$oned_data(dat,new_npt) < \
                   [llength $oned_data(dat,exlines_nodes)]} {
            foreach pt $oned_data(dat,exlines_nodes) {
               $oned_data(dat,vp_tobe) add_pt_only $pt
            }
         } else {
            set larc ""; set larc_sum 0
            set pt0 [lindex $oned_data(dat,exlines_nodes) 0]
            foreach pt [lrange $oned_data(dat,exlines_nodes) 1 end] {
               set as [eval coords_misc dist2 $pt0 $pt]
               lappend larc $as
               set larc_sum [expr $larc_sum + $as]
               set pt0 $pt 
            }
            set darc [expr $larc_sum / ($oned_data(dat,new_npt) -1.0)]
            set pt0 [lindex $oned_data(dat,exlines_nodes) 0]
            $oned_data(dat,vp_tobe) add_pt_only $pt0
            foreach pt [lrange $oned_data(dat,exlines_nodes) 1 end] {
               set as [eval coords_misc dist2 $pt0 $pt]
               set dd [expr $as / $darc + 0.5]
               set nd [expr int($dd)]
               if {$nd < 1} {set nd 1} 
               set dd [expr 1.0 / $nd]
               for {set j 1} {$j < $nd} {incr j} {
                  set ptm [eval coords_misc interp $pt0 $pt [expr $j * $dd]]
                  $oned_data(dat,vp_tobe) add_pt_only $ptm
               }
               $oned_data(dat,vp_tobe) add_pt_only $pt
               set pt0 $pt
            }
         }
         set oned_data(dat,exlines_nodes) ""
         return $oned_data(dat,vp_tobe)
      }
      exlines_nodes {
         if {$oned_data(dat,exlines_mod) == "start"} {
            set oned_data(dat,exlines_nodes) ""
         }
         if {[llength $oned_data(dat,exlines_node)] == 3} {
            lappend oned_data(dat,exlines_nodes) \
                               $oned_data(dat,exlines_node)
         }
         if {$oned_data(dat,exlines_mod) == "end"} {
            return [update_vppt_fm exlines]
         } else {
            return [llength $oned_data(dat,exlines_node)]
         }
      }
   }
}


proc OneDWin::data_choiceframe {fm} {
   global oned_data
   variable wid

   form_frame $fm.cur raised 0 {0 0 -cols - -sticky n}
   form_label $fm.cur.l "Data Set:" {0 0 -cols 2}

   form_text_optionMenu_dynamic $fm.cur.c oned_data(dat,vppts) \
      "OneDWin::update_vppt_fm listall" "OneDWin::update_vppt_fm cur_vp" \
      {0 2 -cols 4 -sticky ew} flat 0 0 {-height 1 -width 16} \
          oned_data(dat,vppts_m) {-ipady 2}

   form_frame $fm.p ridge 0 {10 0 -cols - -sticky news}
   form_frame $fm.p.dswt flat 0 {0 0 -cols 2}
   form_frame $fm.p.dsub  sunken 1 {0 2 -cols -}

   form_radiobutton $fm.p.dswt.dis "Drawing"  oned_data(dat_swt) dis {0 0}
   form_radiobutton $fm.p.dswt.int "Integral" oned_data(dat_swt) int {1 0}
   form_radiobutton $fm.p.dswt.xy  "XY Plot"  oned_data(dat_swt) xy {2 0}
   form_radiobutton $fm.p.dswt.new "New Data" oned_data(dat_swt) new {3 0}

   if {[info command interp_vppts] != ""} {
      form_radiobutton $fm.p.dswt.res "Results"  oned_data(dat_swt) res {4 0}

      set fmr $fm.p.dsub.res
      form_choiceframe $fmr
      OneDWin::data_res_choiceframe $fmr 

      set oned_data(dat,res_fm) $fmr
   }

   set fmn $fm.p.dsub.new
   form_choiceframe $fmn

      form_frame $fmn.npts flat 1 {0 0 -cols - -sticky ew}
      form_label $fmn.npts.l "Number of points" {0 0 -cols 2}
      form_entry $fmn.npts.v oned_data(dat,new_npt) int {0 2}
         $fmn.npts.v configure -width 3

      form_frame $fmn.pm sunken 1 {1 0 -cols - -sticky ew}
      form_button  $fmn.pm.do "on parametric curve(u) : " \
         "OneDWin::update_vppt_fm create_param" {0 0 -cols - -sticky ew} 
      form_label $fmn.pm.xl " X " {1 0}
      form_label $fmn.pm.yl " Y " {2 0}
      form_label $fmn.pm.zl " Z " {3 0}
      form_entry $fmn.pm.x oned_data(dat,xfunc) string {1 1}
      form_entry $fmn.pm.y oned_data(dat,yfunc) string {2 1}
      form_entry $fmn.pm.z oned_data(dat,zfunc) string {3 1}

      form_frame $fmn.ln sunken 1 {5 0 -cols - -sticky ew}
      form_button $fmn.ln.do "on explicit lines : " \
         "OneDWin::update_vppt_fm exlines" {0 0 -cols - -sticky ew}
      form_text_optionMenu_nt $fmn.ln.mod oned_data(dat,exlines_mod) \
          [list " Start with : " start " Add node : " add " End" end] \
          "OneDWin::update_vppt_fm exlines_nodes" {1 0 -cols - -sticky w} \
               flat 0 0 {-height 1 -width 10 -anchor w}
      form_entry $fmn.ln.node oned_data(dat,exlines_node) coords \
                     {2 0 -cols - -sticky ew}

   set fmd $fm.p.dsub.dis
   form_choiceframe $fmd

      form_text_optionMenu_nt $fmd.ln_on oned_data(dat,line_draw) \
          [list " Line plot with result colors" 1 \
                " Line plot with color:" 2 " No line plot" 0] \
          "OneDWin::update_vppt_fm line_draw" {0 0 -cols 6 -sticky ew} \
               flat 0 0 {-height 1 -width 18 -anchor w}
      futil_colorentry $fmd.ln_col oned_data(dat,line_color) \
          {0 7 -sticky ew} "" "" "" "OneDWin::update_vppt_fm line_color"

      form_label $fmd.ln_psp "   " {3 0}
      form_frame $fmd.ln_p flat 0 {3 1 -cols - -sticky ew}
        form_label $fmd.ln_p.l "Linewidth:" {0 0}
        form_entry $fmd.ln_p.lw oned_data(dat,line_width) int {0 1}

       bind $fmd.ln_p.lw <Return> "OneDWin::update_vppt_fm line_width"

      form_text_optionMenu_nt $fmd.pt_on oned_data(dat,dots_draw) \
         [list " No dot plot" 0  " Dot plot with result colors" 1 \
                    " Dot plot with color:" 2] \
         "OneDWin::update_vppt_fm dots_draw" {5 0 -cols 6 -sticky ew} \
               flat 0 0 {-height 1 -width 18 -anchor w}

      futil_colorentry $fmd.pt_col oned_data(dat,dots_color) \
         {5 7 -sticky ew} "" "" "" "OneDWin::update_vppt_fm dots_color"

      form_label $fmd.pt_psp "   " {7 0}
      form_frame $fmd.pt_p flat 0 {7 1 -cols - -sticky ew}
        form_label $fmd.pt_p.l "Dot size:" {0 0}
        form_entry $fmd.pt_p.sz oned_data(dat,dots_size) float {0 1}

        bind  $fmd.pt_p.sz <Return> "OneDWin::update_vppt_fm dots_size"

      form_text_optionMenu_nt $fmd.vec_on oned_data(dat,vec_draw) \
         [list " No vector plot" 0 " Vector with variable colors" 1 \
                 " Vector with color:" 2] \
         "OneDWin::update_vppt_fm vec_draw" {9 0 -cols 6 -sticky ew} \
               flat 0 0 {-height 1 -width 18 -anchor w}
 
      futil_colorentry $fmd.vec_col oned_data(dat,vec_color) \
         {9 7 -sticky ew} "" "" "" "OneDWin::update_vppt_fm vec_color"
      form_label $fmd.vec_psp "   " {11 0}
      form_frame $fmd.vec_p flat 0 {11 1 -cols - -sticky ew}
       form_label $fmd.vec_p.szl "Vector size:" {0 0}
       form_entry $fmd.vec_p.sz  oned_data(dat,vec_size) float {0 1}
       form_label $fmd.vec_p.arrl "Arrow size:" {1 0}
       form_entry $fmd.vec_p.asz  oned_data(dat,arrow_size) float {1 1}
 
        bind $fmd.vec_p.sz <Return> "OneDWin::update_vppt_fm vec_size"
        bind $fmd.vec_p.asz <Return> "OneDWin::update_vppt_fm arrow_size"

   set fmi $fm.p.dsub.int
   form_choiceframe $fmi

   form_button $fmi.ln_do "Compute Line Integral" \
               "OneDWin::update_vppt_fm lnint" {0 0 -cols - -sticky ew}

   form_frame $fmi.ln flat 0 {1 0 -cols - -sticky ew}
   form_label $fmi.ln.sp "  " {0 0}
   form_text_optionMenu_dynamic $fmi.ln.vec oned_data(dat,vec) \
       "OneDWin::update_vppt_fm list_vec" "OneDWin::update_vppt_fm lnint" \
       {0 1 -cols 2 -sticky ew} flat 0 0 {-height 1 -width 10} \
         oned_data(dat,vname) {-ipady 2}
   form_entry $fmi.ln.val oned_data(dat,lnint) float {1 1 -cols 2}


   set fmp $fm.p.dsub.xy
   form_choiceframe $fmp

   form_frame $fmp.txt flat 0 {0 0 -cols - -sticky ew}
   form_label $fmp.txt.c "   " {0 0 -cols - -rows 2}
   set oned_data(dat,vp_text) $fmp.txt.c

   form_label $fmp.sp1 "   " {1 0}

   form_button $fmp.plt "Make new plot in :" \
            "OneDWin::update_vppt_fm xyplot" {2 0  -cols - -sticky ew}
   form_label $fmp.sp2 "   " {3 0}
   form_text_optionMenu_dynamic $fmp.win oned_data(dat,plt_win) \
       "OneDWin::update_vppt_fm list_win" "OneDWin::update_vppt_fm xyplot" \
       {3 1  -cols 2 -sticky ew} flat 0 0 {-height 1 -width 18} 
   set lst [OneDWin::update_vppt_fm list_win]
   set oned_data(dat,plt_win) ""
   lappend oned_data(dat,plt_win) [lindex $lst 0] [lindex $lst 1]

   futil_setup_switch $fm.p.dsub oned_data(dat_swt)
}


proc OneDWin::data_res_choiceframe {fmr} {
   global oned_data
   variable wid

   OneDWin::update_vppt_fm int_res def_sv
   OneDWin::update_vppt_fm int_res def_vv

   form_button $fmr.do "Interpolate Results" \
     "OneDWin::update_vppt_fm int_res do" {0 0 -cols - -sticky ew}
   form_label $fmr.svl "Scalar " {1 0}
   futil_menuentry $fmr.sv oned_data(dat,int_svnsub) \
        "V3_variables_get sv_namelist" {1 1 -cols 2} "" \
        "OneDWin::update_vppt_fm int_res sv"
   for {set i 1} {$i <= [V3_variables_get nsubnames]} {incr i} {
      form_label $fmr.sv_${i}l [lindex [V3_variables_get subnames] \
                                       [expr $i-1]] {2 0 -sticky nws}
      form_text_optionMenu_nt $fmr.sv_$i oned_data(dat,int_svnsub,$i) \
         [V3_variables_get sv_sublist $i] \
         "OneDWin::update_vppt_fm int_res sv $i" \
         {2 1 -cols 2  -sticky ew} flat 0 0 {-height 1 -width 8 -anchor w}
   }
   form_button $fmr.sv_def "Default" \
       "OneDWin::update_vppt_fm int_res def_sv" {3 1 -cols 2 -sticky ew}

   form_label $fmr.sp "  " {4 0}

   form_label $fmr.vvl "Vector " {5 0}
   futil_menuentry $fmr.vv oned_data(dat,int_vvnsub) \
        "V3_variables_get vv_namelist" {5 1 -cols 2} "" \
        "OneDWin::update_vppt_fm int_res vv"
   for {set i 1} {$i <= [V3_variables_get nsubnames]} {incr i} {
      form_label $fmr.vv_${i}l [lindex [V3_variables_get subnames] \
                                       [expr $i-1]] {6 0 -sticky nws}
      form_text_optionMenu_nt $fmr.vv_$i oned_data(dat,int_vvnsub,$i) \
         [V3_variables_get vv_sublist $i] \
         "OneDWin::update_vppt_fm int_res vv $i" \
         {6 1 -cols 2  -sticky ew} flat 0 0 {-height 1 -width 8 -anchor w}
   }

   form_button $fmr.vv_def "Default" \
      "OneDWin::update_vppt_fm int_res def_vv" {7 1 -cols 2 -sticky ew}
}
