

array set oned_data {
   win_list    ""
   delplt_list ""
   vppts_tobe ""
   vppts_list ""
   plotted_vppts_list ""
   dat,vppts ""
   dat,vp_tobe ""
   dat,new_npt 0
   dat,exlines_node ""
   dat,exlines_nodes ""
   dat,exlines_mod start
   dat,plt_win ""
   dat,is3d    0
   wf,xaxis_opt  x
   paxis_swt  tick
   xaxis_swt  tick
   xfit       1
   x0         0.0
   x1         10.0
   xlinewidth 1
   xcolor     #ff11bb
   xmajorstep 1.0
   xsubtick   4
   xticksize  16.0
   xmtom      2
   xtickup    0
   x_log      0
   xgrid_on   0
   xgwidth    1
   xgcolor    #bb0099
   xmgrid_on  0
   xmgwidth   1
   xmgcolor   #990066
   xvar_on    0
   xnumsize   1
   xnumfont   0
   xnumpos    "0.0 0.0"
   xnumfmt    "%g"
   xarrows    2
   yaxis_swt  tick
   yfit       1
   y0         0.0
   y1         10.0
   ylinewidth 1
   ycolor     #ff11bb
   ymajorstep 1.0
   ysubtick   4
   yticksize  16.0
   ymtom      2.5
   ytickup    1
   y_log      0
   ygrid_on   0
   ygwidth    1
   ygcolor    #bb0099
   ymgrid_on  0
   ymgwidth   1
   ymgcolor   #990066
   yvar_on    0
   ynumsize   1
   ynumfont   0
   ynumpos    "0.0 0.0"
   ynumfmt    "%g"
   yarrows   2
   ang_0     0
   ang_1     360
   ang_relative 1
   ang_ax    0
   pcolor     #ff11bb
   plinewidth 1.0
   pmajorstep 30.0
   psubtick   5
   pticksize  16.0
   pmtom      2.5
   pgrid_on   1
   pgwidth    1
   pgcolor    #bb0099 
   pmgrid_on  0
   pmgwidth   1
   pmgcolor   #990066
   pnumfont   0
   pnumpos    "0.0 0.0"
   parrows    0
   leg,pos    "350 50"
   leg,pad    "5 5" 
   leg,fr_on    1
   leg,fr_color #ffffff
   leg,fr_bcol  #000000
   leg,fr_width 1
   leg,fr_padx  10
   leg,fr_pady  5
   leg,vn_on    1
   leg,vn_color #ffffff
   leg,vn_font  0
   plt,symbols 1
   plt,stype   1
   plt,ssize   6
   plt,swidth  1
   plt,scolor  #2222ff
   plt,fill    0
   plt,line    1
   plt,lwidth  1
   plt,lpttn   0
   plt,lcolor  #2222ff
   plt,axisfit 1
   plt,fit_x0  1
   plt,fit_x1  1
   plt,fit_y0  1
   plt,fit_y1  1
   plt,deformx 0
   plt,rx      1.0
   plt,x0      0.0
   plt,ry      1.0
   plt,y0      0.0
   plt,deformy 0  
   plt,x_type 1
   plt,p0,x   0
   plt,p0,y   0
   plt,p0,z   0
   plt,dist0  0
   plt,A      1
   plt,B      0
   plt,C      0
   plt,D      0
   plt,axis   "0 0 1"
   plt,center "0 0 0"
   plt,zero   "1 0 0"   
   plt,vppts_is3d  1
   plt,y_type ""
   plt_swt    pm
   plt_id     ""
}

namespace eval OneDWin {
   variable count
   variable wid 
   variable last_wid
   variable width
   variable height
   variable background
   variable win_data
   variable leg_data
   variable plt_data
   variable plt_swt
}

proc OneDWin::init {} {
   global use_xwindows black_and_white is_win graphics_type

   variable count  0
   variable width  600
   variable height 600
   variable background black

   if {$black_and_white} { set background white }

   variable wid ""
}

proc OneDWin::var {how vn args} {
   variable $vn
   return [eval $how $vn $args]
}


proc  OneDWin::reg {name w ww hh bg} {
   global oned_data

   variable width
   variable height
   variable background
   variable wid
   variable last_wid

   if {$ww != ""} {set width $ww}
   if {$hh != ""} {set height $hh}
   if {$bg != ""} {set background $bg}

   lappend oned_data(win_list) $name $w
   set last_wid $wid
   set wid $w
   set oned_data($w,name) $name
}


proc OneDWin::new {name {ww ""} {hh ""} {bg ""}} {
   global oned_data env tcl_platform

   variable count
   variable width
   variable height
   variable background
   variable wid
   variable last_wid

   incr count
   set win .d1_$count
   top_window $win 1 $name withdraw

   set w $win.view 
   reg $name $w $ww $hh $bg

   setup_view $w $name $width $height "" $bg

   set oned_data($w,ctrls) $win.ctrls
   frame [set d $win.ctrls]
if 0 {
   menubutton $d.save -text Save -underline 0 -menu $d.save.menu
   menubutton $d.load -text Load -underline 0 -menu $d.load.menu
} else {
   button $d.save -text Save -command "OneDWin::cmd $w save"
   button $d.load -text Load -command "OneDWin::cmd $w load"
}
   menubutton $d.hcopy -text Hardcopy -underline 0 -menu $d.hcopy.menu
   menubutton $d.edit -text Modify -underline 0 -menu $d.edit.menu
   menubutton $d.create -text Create -underline 0 \
           -menu $d.create.menu
   menubutton $d.view -text View -underline 0 -menu $d.view.menu
 
   pack $d.save $d.load $d.hcopy $d.edit $d.create $d.view \
           -side left -padx 2 -pady 2 -ipadx 2 -ipady 2


   if [info exist env(V3P_WITH_DISMISS)] {
     #button $d.delete -text Delete -command "OneDWin::cmd $w delete"

      button $d.clear -text Dismiss -command "OneDWin::cmd $w dismiss"
      pack $d.clear $d.delete -side right -ipadx 2 -ipady 2
   }

   pack $w  -side top -fill both -expand 1
   
if 0 {
   menu $d.save.menu -tearoff 0
   $d.save.menu add command -label "Save" -command \
              "OneDWin::cmd $w save"
   $d.save.menu add command -label "Save as ..." -command \
              "OneDWin::cmd $w save_as"

   menu $d.load.menu -tearoff 0
   $d.load.menu add command -label "Load" -command \
              "OneDWin::cmd $w load"
   $d.load.menu add command -label "Load as ..." -command \
             "OneDWin::cmd $w load_as"
}
   menu $d.hcopy.menu -tearoff 0
   $d.hcopy.menu add command -label "Setup" -command \
              "OneDWin::cmd $w set_hcp"
   $d.hcopy.menu add command -label "Write" -command \
              "OneDWin::cmd $w do_hcp"

   menu $d.edit.menu -tearoff 0
   $d.edit.menu add command -label "Modify X Axis" -command \
           "OneDWin::cmd $w axis x"
   $d.edit.menu add command -label "Modify Y Axis" -command \
           "OneDWin::cmd $w axis y"
   $d.edit.menu add command -label "Modify Legend" -command \
           "OneDWin::cmd $w legend"
   $d.edit.menu add command -label "Modify Plots" -command \
           "OneDWin::cmd $w plots"
   $d.edit.menu add command -label "Modify X Title" -command \
           "OneDWin::cmd $w title x"
   $d.edit.menu add command -label "Modify Y Title" -command \
           "OneDWin::cmd $w title y"
   $d.edit.menu add command -label "Delete Others" -command \
           "OneDWin::cmd $w delete"

   menu [set fw $d.create.menu] -tearoff 0
   $fw add command -label "Text" -command "OneDWin::cmd $w create text"
   $fw add command -label "UtfText" -command "OneDWin::cmd $w create utftext"
   $fw add command -label "Mark" -command "OneDWin::cmd $w create mark"
   $fw add command -label "Line" -command "OneDWin::cmd $w create line"
   $fw add command -label "Box"  -command "OneDWin::cmd $w create box"
   $fw add command -label "Circle"  -command "OneDWin::cmd $w create circle"
   $fw add command -label "Polygon" -command "OneDWin::cmd $w create polygon"
   $fw add command -label "Pixmap"  -command "OneDWin::cmd $w create pixmap"

   menu [set fv $d.view.menu] -tearoff 0
   $fv add command -label "Zoom in plots" \
        -command "OneDWin::cmd $w zoomin_plots"
   $fv add command -label "Zoom out axes" \
        -command "OneDWin::cmd $w zoomout_axes"
   $fv add  cascade  -label "Scale plots" -menu $fv.splt
      menu $fv.splt -tearoff 0
      $fv.splt add command -label "to 50%" \
          -command "OneDWin::cmd $w scaling_plots 0.5"
      $fv.splt add command -label "to 75%" \
          -command "OneDWin::cmd $w scaling_plots 0.75"
      $fv.splt add command -label "to 110%" \
          -command "OneDWin::cmd $w scaling_plots 1.10"
      $fv.splt add command -label "to 150%" \
          -command "OneDWin::cmd $w scaling_plots 1.50"
      $fv.splt add command -label "to 200%" \
          -command "OneDWin::cmd $w scaling_plots 2.0"
      $fv.splt add command -label "with mouse binding" \
          -command "OneDWin::cmd $w scale plots"
   $fv add cascade -label "Scale axes" -menu $fv.sax
      menu $fv.sax -tearoff 0
      $fv.sax add command -label "to 50%" \
          -command "OneDWin::cmd $w scaling_axes 0.5"
      $fv.sax add command -label "to 75%" \
          -command "OneDWin::cmd $w scaling_axes 0.75"
      $fv.sax add command -label "to 110%" \
          -command "OneDWin::cmd $w scaling_axes 1.10"
      $fv.sax add command -label "to 150%" \
          -command "OneDWin::cmd $w scaling_axes 1.50"
      $fv.sax add command -label "to 200%" \
          -command "OneDWin::cmd $w scaling_axes 2.00"
      $fv.sax add command -label "with mouse binding" \
          -command "OneDWin::cmd $w scale axes"

    $fv add command -label "Move" -command "OneDWin::cmd $w move"
    $fv add cascade -label "Move specific" -menu $fv.mv
      menu $fv.mv -tearoff 0
      $fv.mv add command -label "All togather" \
           -command "OneDWin::cmd $w shift all"
      $fv.mv add command -label "X axis" \
          -command "OneDWin::cmd $w shift x"
      $fv.mv add command -label "Y axis" \
          -command "OneDWin::cmd $w shift y"
      $fv.mv add command -label "Both axes" \
          -command "OneDWin::cmd $w shift both"
      $fv.mv add command -label "X title" \
         -command "OneDWin::cmd $w shift xtitle"
      $fv.mv add command -label "Y title" \
         -command "OneDWin::cmd $w shift ytitle"
      $fv.mv add command -label "Legend" \
         -command "OneDWin::cmd $w shift leg" 

   setup_oned_params_sub $win.params
   pack $win.params $win.ctrls -before $win.view -side bottom -fill x

   if {$tcl_platform(platform) == "windows"} {
      update idletasks

      set dw [expr $width - [winfo width $w]]
      set dh [expr $height - [winfo height $w]]

      if {($dw != 0) && ($dh != 0)} {
         tdv_win_change $w $width $height
         $w configure -width $width -height $height

         top_resize_direct $win $dw $dh
         update idletasks
      }
   }

   top_geometry $win -0+16
   top_on_screen_xy $win

   return $w
}



proc OneDWin::setup_view {w name width height {gtype ""} {bg ""}} {
   global oned_data anno_data
   global gwin_list tcl_platform
   global tdv_width tdv_height tdv_pos tdv_rot tdv_scale tdv_center
   global tdv_pickable_tags tdv_win_type

   if {$gtype == ""} {
      global use_xwindows is_win graphics_type
      set gtype $graphics_type
      switch $use_xwindows {
         1 { set gtype X11 }
         2 { set gtype OpenGL }
      }
      if {$is_win && ($gtype == "X11")} { set gtype Mswin }
   }
   if {$bg == ""} {
      variable background
      set bg $background
   } 

   if {$tcl_platform(platform) == "windows"} {
      set w2 [expr $width * 2]
      set h2 [expr $height * 2]
   } else {
      set w2 $width
      set h2 $height
   }

   set tdv_width($w) $w2
   set tdv_height($w) $h2

   tdv_viewer $w -background $bg -borderwidth 0 -debug 0 \
     -gsys $gtype -sizechange "tdv_win_change $w" -dbuffer 1 \
     -directlight 0 -width $w2 -height $h2 -twod 1

   set tdv_pos($w) "0 0 0"
   set tdv_rot($w) "0 0 0 1"
   set tdv_scale($w) "1 1 1"
   set tdv_center($w) "0 0 0"
   set tdv_pickable_tags($w) ""
   set tdv_win_type($w) 1d

   lappend gwin_list $name $w

   bind $w <Control-Button-1> "tdv_post_cmd %W post_menu %R %X %Y"
   bind $w <Enter> "tdv_unfocus_entries"

   anno_init_win $w $name button_states($w) \
                   button_bindings($w) button_events($w)

   anno_init $w 0 ""
}

proc setup_oned_params_sub {fw} {
   global oned_data anno_data
   global arrowlines_predefined symbols_predefined vpost_library

   set fws [form_init_sub $fw]
     $fws configure -relief flat -borderwidth 0

   form_frame $fws.font sunken 1 {0 0 -cols 3}
   form_label $fws.font.sizel "font:" {0 0}
    
   form_text_optionMenu $fws.font.size anno_data(font) \
          [list small 0 med 1 large 2]  "" {0 1 -cols 2 -sticky ew} \
          sunken 2 0 {-height 1 -width 6} anno_data(font_m_list) {-ipady 2}
               
   form_frame $fws.angle sunken 1 {0 3 -cols 2}
   form_label $fws.angle.l "angle:" {0 0}
   form_entry $fws.angle.degree anno_data(angle) float {0 1}
       $fws.angle.degree configure -width 3

   form_frame $fws.line sunken 1 {0 5 -cols 2}
   form_label  $fws.line.widthl "width:" {0 0}
   form_entry  $fws.line.width anno_data(linew) int {0 1}
       $fws.line.width configure -width 2

   form_frame $fws.arrow sunken 1 {0 7 -cols 1}
   form_bitmap_optionMenu $fws.arrow.status anno_data(arrow) \
           $vpost_library $arrowlines_predefined \
           "" {0 1} raised 0 0 {-padx 2} anno_data(arrow_m_list)

   form_frame $fws.symbol sunken 1 {0 8 -cols 1}
   form_bitmap_optionMenu $fws.symbol.status anno_data(stype) \
          $vpost_library  $symbols_predefined \
          "" {0 1} raised 0 6 {-padx 2 -pady 2} anno_data(stype_m_list)

   form_frame $fws.lcolor sunken 1 {0 9 -cols 2}
   form_label $fws.lcolor.l "color:" {0 0}

   futil_colorentry  $fws.lcolor.color anno_data(lcolor) {0 1}
  #futil_coloredit_button $fws.lcolor.color anno_data(lcolor) "" "" "" {0 1}
     $fws.lcolor.color configure -width 2

   form_finish
}

proc OneDWin::cmd {w what args} {
   global oned_data anno_data
   global tdv_width tdv_height

   switch -- $what {
     save {
        set file [lindex $args 0]
        if {$file == ""} {
           set file [file_select "Select the file to save the graph to." \
                          "" "File selection" 0 1]
           if {$file == ""} {return ""}
        }
        if [catch {set fp [open $file w]} err_msg] {
           mess "$err_msg\n" red
           return ""
        }
        set dirname [file dirname $file]

        puts $fp title
        puts $fp $oned_data($w,name)

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

        set gm $anno_data(gm,0,$w)
        set rr [$gm dump $oned_data($w,axis_id)]
        if {[lindex $rr 0] == "group"} {
           puts $fp xy
           puts $fp $rr
        } else {
           puts $fp polar
           puts $fp $rr
        }

        foreach c {x y p} {
           if [info exist oned_data($w,${c}_axis)] {
               puts $fp ${c}_axis
               puts $fp [$gm dump $oned_data($w,${c}_axis)]
           }
        }

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

        foreach {n plt} $oned_data($w,plt_list) {
           set rr [$gm dump $plt]
           set j [llength $rr]
           for {set i 0} {$i < $j} {incr i} {
              if {[lindex $rr $i] == "-vppts"} {break}
           }
           if {$i == $j} {continue}

           puts $fp $n
           puts $fp $rr

           set vp [lindex $rr [expr $i+1]]
           set kk [$vp info]

           set name [join $n ""]

           if [catch {set pp [open [file join $dirname $name] w]} emg] {
              set n ""
              for {set i 0} {$i < [string length $name]} {incr i} {
                 set c [string index $name $i]
                 if [string is ascii $c] {
                    if [string is alnum $c] {
                       append n $c
                    } else {
                       append n " "
                    }
                 }
              }
              set name $n
           } else {
              close $pp
           }
           if [catch {set pp [open [file join $dirname $name] w]} emg] {
              mess "failed to write file [file join $dirname $name]\n" red
              return ""
           } else {
              close $pp
           }
 
           set n $name; set i 0
           while 1 {
              set fn [file join $dirname $n]
              if ![file isfile $fn] {break}
              incr i
              set n $name
              append n $i
           }
           if {[lindex $kk 1]} {
              if {[update_vppoints save_vppts $vp $fn] != ""} { puts $fp $n }
           } else {
              if {[update_vppoints save_xydata $vp $fn] != ""} { puts $fp $n }
           }
        }
        close $fp
     }
     save_as {
     }
     load {
        set file [lindex $args 0]
        if {$file == ""} {
           set file [file_select "Select the file to read the data." \
                          "" "File selection" 0 0]
           if {$file == ""} return
        }
        if {[set fp [dfile_open $file r]] == ""} {return ""}
        if {[gets $fp] != "title"} {
           close $fp
           return
        }
        set wtitle [gets $fp]
        set wtype  [gets $fp]
        set axis   [gets $fp]

        set xtitle ""
        set ytitle ""
        set n [gets $fp]
        while {![eof $fp]} {
          switch -- $n {
             x_axis { set xtitle [gets $fp] }
             y_axis { set ytitle [gets $fp] }
             default {break}
          }
          set n [gets $fp]
        }

        if ![winfo exist $w] {
           if {$wtype == "polar"} {
              update_oned_mg polar_win
           } else {
              update_oned_mg new_win
           }
        }

        set dirname [file dirname $file]

        while {![eof $fp]} {
           set rr [gets $fp]
           set fn [gets $fp]
           if {[file pathtype $fn] =="relative"} {
              set fn [file join $dirname $fn]
           }
           if [file isdirectory $fn] {
              set vp [update_vppoints create $n]
              update_vppoints load $vp $fn
              update_vppoints xyplot $vp
           } elseif [file isfile $fn] {
              create_plots read_xydata $n $fn
           }
           set n [gets $fp]
        }
        close $fp 

      # ----------------------- for xtitle and ytitle

        foreach x {x y} {
           set rr [set ${x}title]
           if {([lindex $rr 0] == "utftext") && ([llength $rr] == 6)} {
              modify_axis title $x
        #                    x 1 y 2 lcolor 3 font 4 text 5
              foreach {n j} {lcolor 3 font 4 text 5} {
                 set anno_data(utftext,$n) [lindex $rr $j]
              }
              modify_anno_pick utftext
           }
        }

      # ----------------------- for axis

        if {[lindex $axis 0] == "group"} {
           foreach rr [lrange $axis 1 end] {
              if {[lindex $rr 0] == "tickaxis"} {

              }
           }
        }
     }
     load_as {
     }
     set_hcp {
        hardcopy_form $w
     }
     do_hcp {
        hardcopy_create $w
     }
     background {
        if {$args != ""} {
           set oned_data($w,background) [lindex $args 0]
        } else {
           set oned_data($w,background) [edit_one_color \
             "Please define the background color." $oned_data($w,background) 0]
        }
        variable background $oned_data($w,background)
        $w configure -background $oned_data($w,background)
     }
     update_anno_win {
        if {$anno_data(win) != $w} {
           set anno_data(win) $w
           if [info exist anno_data(win_m)] {
              #update_text_optionMenu  $anno_data(win_m)
           }
        }
     }
     create {
        OneDWin::cmd $w update_anno_win
        set anno_data(model) 0
        anno_action [lindex $args 0]
     }
     move {
        OneDWin::cmd $w update_anno_win
        set anno_data(model) 0
        anno_action move
     }
     delete {
        OneDWin::cmd $w update_anno_win
        set anno_data(model) 0
        anno_action delete
     }
     dismiss   { top_withdraw [winfo top $w] }
     axis {
        setup_oned_mg
        OneDWin::cmd $w win
        if [info exist oned_data(wf,notebook)] {
           set x [lindex $args 0]
           if {$x != "y"} {set x x}
           $oned_data(wf,notebook) raise $x
        }
     }
     legend {
        setup_oned_mg
        OneDWin::cmd $w win
        if [info exist oned_data(wf,notebook)] {
           $oned_data(wf,notebook) raise leg
        }
     }
     plots {
        setup_oned_mg
        OneDWin::cmd $w win
        if [info exist oned_data(wf,notebook)] {
           $oned_data(wf,notebook) raise plt
        }
     }
     title {
        set x [lindex $args 0]
        if {$x != "y"} {set x x}
        if [info exist oned_data($w,${x}_axis)] {
           OneDWin::cmd $w update_anno_win
           set anno_data(model) 0
           setup_anno_mg
           update_anno_pick $oned_data($w,${x}_axis)
        }
     }
     setup_mouse_binding {
        OneDWin::cmd $w update_anno_win
        set gm $anno_data(gm,0,$w)
        if {[anno_setup_mouse_binding $w] == 0} {return ""}

        set oned_data($w,binding) 1
        return "$gm $anno_data(button_events,$w)" 
     }
     reset_mouse_binding {
        if [info exist oned_data($w,binding)] {
           anno_reset_mouse_binding $w
           unset oned_data($w,binding)
        }
     }
     get_rubbox {
        OneDWin::cmd $w win
        set rr [cmd $w setup_mouse_binding]
        if {$rr == ""} {return ""}

        set bvarName [lindex $rr 1]
        upvar #0 $bvarName but_var
        catch {$w configure -cursor {crosshair red white}}
        set $bvarName ""
        while 1 {
           tkwait variable $bvarName
           set p1 [lrange $but_var 1 2]
           if {[lindex $but_var 0] >= 0} break
        }
        catch {$w configure -cursor {bottom_right_corner red white}}
        while 1 {
           tkwait variable $bvarName 
           if {[lindex $but_var 0] < 0} break
           set p2 [lrange $but_var 1 2]
           if ![info exists rubbox] {
              set rubbox [tdv_rubbox_im -color orange -transform 0 \
                   -overlay 1 -x1 [lindex $p1 0] -y1 [lindex $p1 1] \
                              -x2 [lindex $p2 0] -y2 [lindex $p2 1]]
              $w add_image $rubbox
           } 
           $rubbox configure -x1 [lindex $p1 0] -y1 [lindex $p1 1] \
                             -x2 [lindex $p2 0] -y2 [lindex $p2 1]
        } 

        if [info exists rubbox] { $rubbox delete }
        catch {$w configure -cursor {left_ptr red white}}
        OneDWin::cmd $w reset_mouse_binding

        if ![info exists p2] { bell; return "" }

        foreach c {x y} i {0 1} {
           set L [lindex $p1 $i]
           set R [lindex $p2 $i]
           if {$L > $R} {
              set ${c}L $R
              set ${c}R $L
           } else {
              set ${c}L $L
              set ${c}R $R
           }
        }
        return [list "$xL $yL" "$xR $yR"]
     }
     zoomin_plots {
        OneDWin::cmd $w win
        set rr [OneDWin::cmd $w get_rubbox]
        if {$rr == ""} {return ""}
 
        set p0 [lindex $rr 0]
        set p1 [lindex $rr 1]

        foreach c {x y} i {0 1} {
           set L [lindex $oned_data(${c}_p0) $i]
           set R [lindex $oned_data(${c}_p1) $i]
           set pL [lindex $p0 $i]
           set pR [lindex $p1 $i]
           if {($R - $L) * ($pR - $pL) < 0} {
              set RR $pR
              set pR $pL
              set pL $RR
           }
           set L [expr $L + $oned_data(${c}s0)]
           set R [expr $R - $oned_data(${c}s1)]
           
           set t0 $oned_data(${c}0)
           set t1 $oned_data(${c}1)
           if {$oned_data(${c}_log)} {
              t0 = [expr log10( $t0 )]
              t1 = [expr log10( $t1 )]
           }
           set t0 [expr $t0 - $oned_data(${c}_s0)]
           set t1 [expr $t1 + $oned_data(${c}_s1)]
           set ds [expr ($t1 - $t0) / double($R - $L)]
           
           set t0 [expr $ds * ($pL - $L) + $t0 ]
           set t1 [expr $ds * ($pR - $L) + $t0 ]
 
           if {$oned_data(${c}_log)} {
              set kp [expr log(10.0)]
              set oned_data(${c}0) [expr exp( $t0 * $kp)]
              set oned_data(${c}1) [expr exp( $t1 * $kp)]
           } else {
              set oned_data(${c}0) $t0
              set oned_data(${c}1) $t1
           }
           modify_axis range $c
        }
        if {[info exist oned_data($w,zoom_func)] && \
                ($oned_data($w,zoom_func) != "")} {
           $oned_data($w,zoom_func) [lindex $p0 0] [lindex $p0 1] \
                     [lindex $p1 0] [lindex $p1 1]
        }
     }
     zoomout_axes {
        OneDWin::cmd $w win
        set rr [OneDWin::cmd $w get_rubbox]
        if {$rr == ""} {return ""}

        set q0 [lindex $rr 0]
        set q1 [lindex $rr 1]

        set rr [OneDWin::cmd $w get_bbox]
        set p0 [lindex $rr 0]
        set p1 [lindex $rr 1]

        foreach c {x y} i {0 1} {
           set qL [lindex $q0 $i]
           set qR [lindex $q1 $i]
           set pL [lindex $p0 $i]
           set pR [lindex $p1 $i]
           set r$c [expr double($qR - $qL) / double($pR - $pL)]
           set d$c [expr $qL - $pL * [set r$c]] 
        }
        
        foreach k {x_p0 x_p1 y_p0 y_p1} {
           foreach c {x y} i { 0 1} {
              set p$c [expr [set d$c] + [set r$c] * \
                               [lindex $oned_data($k) $i]]
           }
           set oned_data($k) "$px $py"
        }
        modify_axis pos x
        modify_axis pos y
     }
     scaling_plots {
        if {$w != ""} {OneDWin::cmd $w win}

        set rx [lindex $args 0]
        set ry [lindex $args 1]
        if {$rx == ""} {return} \
        elseif {$ry == ""} {set ry $rx}

        foreach c {x y} {
           set t0 $oned_data(${c}0)
           set t1 $oned_data(${c}1)
           set tm [expr 0.5 * ($t0 + $t1)]
           set r [set r$c]
           set oned_data(${c}0) [expr $tm + ($t0 - $tm) / $r] 
           set oned_data(${c}1) [expr $tm + ($t1 - $tm) / $r]

           modify_axis range $c
        } 
     }
     scale {
        set sub [lindex $args 0]
        if {$sub != "plots"} {set sub axes}

        OneDWin::cmd $w win
        set rr [cmd $w setup_mouse_binding]
        if {$rr == ""} {return ""}

        set hh [winfo height $w]

        set bvarName [lindex $rr 1]
        upvar #0 $bvarName but_var

        tdv_cur $w scale

        set $bvarName ""
        while 1 {
           tkwait variable $bvarName
           set p1 [lrange $but_var 1 2]
           if {[lindex $but_var 0] >= 0} break
        }
        set p1y [lindex $p1 1]
        while 1 {
           tkwait variable $bvarName
           if {[lindex $but_var 0] < 0} break
           set p2 [lrange $but_var 1 2]
           set p2y [lindex $p2 1]
           if {abs($p2y - $p1y) > 2.0} {
              set r [expr double($p2y - $p1y) / $hh] 
              OneDWin::cmd "" scaling_$sub [expr 1.0 + 0.5 * $r] 
              set p1y $p2y
           }
        }
        catch {$w configure -cursor {left_ptr red white}}
        OneDWin::cmd $w reset_mouse_binding
     }
     get_bbox {
        foreach c {x y} i {0 1} {
           set L [lindex $oned_data(x_p0) $i]
           set R $L
           foreach n {x_p1 y_p0 y_p1} {
              set v [lindex $oned_data($n) $i] 
              if {$v < $L} {set L $v} \
              elseif {$v > $R} {set R $v}
           }
           set ${c}L $L
           set ${c}R $R
        }
        return [list "$xL $yL" "$xR $yR"]
     }
     scaling_axes {
        set rx [lindex $args 0]
        set ry [lindex $args 1]
        if {$rx == ""} {return} \
        elseif {$ry == ""} {set ry $rx}

        if {$w == ""} {
           variable wid
           set w $wid
        } else {
           OneDWin::cmd $w win
        } 

        set rr [OneDWin::cmd $w get_bbox]
        set pLL [lindex $rr 0]
        set pRR [lindex $rr 1]
        set px [expr ([lindex $pLL 0] + [lindex $pRR 0]) / 2]
        set py [expr ([lindex $pLL 1] + [lindex $pRR 1]) / 2]

        set gm $anno_data(gm,0,$w)

        foreach c {x y} {
           foreach i {0 1} {
              set x [lindex $oned_data(${c}_p$i) 0]
              set y [lindex $oned_data(${c}_p$i) 1]
              set x [expr $px + $rx * ($x - $px)]
              set y [expr $py + $ry * ($y - $py)]
              set oned_data(${c}_p$i) "$x $y"
           }
           modify_axis pos $c

           update_anno_pick $oned_data($w,${c}_axis)
           set x $anno_data(utftext,x)
           set y $anno_data(utftext,y)
           set dx [expr ($rx - 1.0) * ($x - $px)]
           set dy [expr ($ry - 1.0) * ($y - $py)]
           $gm move $oned_data($w,${c}_axis) $dx $dy
        }

        set x [lindex $oned_data(leg,pos) 0]
        set y [lindex $oned_data(leg,pos) 1]
        set dx [expr ($rx - 1.0) * ($x - $px)]
        set dy [expr ($ry - 1.0) * ($y - $py)]
        $gm move $oned_data(leg_id) $dx $dy
        set x [expr $x + $dx]
        set y [expr $y + $dy]
        set oned_data(leg,pos) "$x $y"
     }
     shift {
        OneDWin::cmd $w win
        set gm $anno_data(gm,0,$w)
        
        set sub [lindex $args 0]
        switch -- $sub {
           x - y - both  {
               modify_axis move $sub
           }
           xtitle {
              OneDWin::cmd $w update_anno_win
              anno_action move 0 0 $oned_data($w,x_axis)
           }
           ytitle {
              OneDWin::cmd $w update_anno_win
              anno_action move 0 0 $oned_data($w,y_axis)
           }
           leg {
              OneDWin::cmd $w update_anno_win
              anno_action move 0 0 $oned_data(leg_id)
           }
           all {
              set xo [lindex $oned_data(x_p0) 0]
              set yo [lindex $oned_data(x_p0) 1]

              modify_axis move both

              set dx [expr [lindex $oned_data(x_p0) 0] - $xo]
              set dy [expr [lindex $oned_data(x_p0) 1] - $yo]

              $gm move $oned_data(leg_id) $dx $dy
              $gm move $oned_data($w,x_axis) $dx $dy
              $gm move $oned_data($w,y_axis) $dx $dy
           }
        }
     }
     coords_type {
        variable wid
        if {$w == ""} {set w $wid}
        if [info exist oned_data($wid,axis_id)] {
           if {[$anno_data(gm,0,$w) get_group $oned_data(axis_id)] == ""} {
              return p
           } else {
              return y
           } 
        } else {
           return ""
        }
     }
     win {
        variable wid
        variable wid_last $wid
        set wid $w

        if {($wid_last != "") && ($wid != $wid_last)} {
           if [info exist oned_data($wid,axis_id)] {
              set oned_data(axis_id) $oned_data($wid,axis_id)
              if {[$anno_data(gm,0,$w) get_group $oned_data(axis_id)] == ""} {
                 $oned_data(wf,notebook) itemconfigure x -text Arc
                 $oned_data(wf,notebook) itemconfigure y -text Radius
                 set oned_data(wf,xaxis_opt) p
              } else {
                 $oned_data(wf,notebook) itemconfigure x -text "X Axis"
                 $oned_data(wf,notebook) itemconfigure y -text "Y Axis"
                 set oned_data(wf,xaxis_opt) x
              }
              update_axis axis_id ""
           }
           if [info exist oned_data($wid,leg_id)] {
              set oned_data(leg_id) $oned_data($wid,leg_id)
              update_leg leg_id
           }
           if [info exist oned_data($wid,plt_id)] {
              set oned_data(plt_id) $oned_data($wid,plt_id)
              update_plots plt_id
           }
        }
     }
   }
}


proc OneDWin::update_axis {which x args} {
   global oned_data anno_data
   variable wid

   if ![winfo exist $wid] {return} 
   set w $wid
   if {![info exist oned_data(axis_id)] || ![info exist anno_data(gm,0,$w)] \
              || ([info command $anno_data(gm,0,$w)] == "")} {
      mess "unable to acess an invalid axis\n" red
      return ""
   }
   set gm $anno_data(gm,0,$w)

   set what $which
   if {$x == "p"} {set what polar_$which}

   if {$what != "axis_id"} {
      if {[$gm get_group $oned_data(axis_id)] == ""} {
         set rr [$gm get_tickaxis $oned_data(axis_id) $what]
      } else {
         set rr [$gm get_tickaxis $oned_data(axis_id,$x) $what]
      }
      set i 0
   }
   switch -- $what {
      line {
         foreach n {color linewidth arrows} {
            set oned_data(${x}$n) [lindex $rr $i]
            incr i
         }
      }
      pos {
         set oned_data(${x}_p0) [lrange $rr 0 1]
         set oned_data(${x}_p1) [lrange $rr 3 4]
      }
      range {
         foreach n {0 1 _s0 _s1 s0 s1} {
            set oned_data(${x}$n) [lindex $rr $i]
            incr i
         }
      }
      tick {
         foreach n {tickup majorstep subtick ticksize mtom _log _tlen} {
             set oned_data(${x}$n) [lindex $rr $i]
             incr i
         }
      }
      number {
         set oned_data(${x}numsize) [lindex $rr 0]
         set oned_data(${x}numfont) [lindex $rr 1]
         set oned_data(${x}numpos)  [lrange $rr 2 3]
         set oned_data(${x}numfmt)  [lindex $rr 4]
      }
      grid {
         foreach n {grid_on glength gwidth gcolor \
                    mgrid_on mgwidth mgcolor} {
            set oned_data(${x}$n) [lindex $rr $i]
            incr i
         }
      }
      polar_line {
         foreach n {color linewidth} {
            set oned_data(p$n) [lindex $rr $i]
            incr i
         }
      }
      polar_range {
         foreach n {relative 0 1 ax} {
            set oned_data(ang_$n) [lindex $rr $i]
            incr i
         }
      }
      polar_tick {
         foreach n {majorstep subtick ticksize mtom} {
             set oned_data(p$n) [lindex $rr $i]
             incr i
         }
      }
      polar_grid {
         foreach n {grid_on gwidth gcolor mgrid_on mgwidth mgcolor} {
            set oned_data(p$n) [lindex $rr $i]
            incr i
         }
      }
      axis_id {
         set oned_data($wid,axis_id) $oned_data(axis_id)
         set ids [$gm get_group $oned_data(axis_id)]
         if {[lindex $ids 0] != 2} {
            set oned_data(axis_id,y) $oned_data(axis_id)
            foreach k {pos range line tick number grid} {
               update_axis $k y
            }
            set oned_data(yrival) 0
            set oned_data(axis_id,p) $oned_data(axis_id)
            foreach j {range line tick grid} {
               update_axis $j p
            }
         } else {
            foreach x {x y} rr [lrange $ids 1 2] {
               set oned_data(axis_id,$x) $rr
               foreach k {pos range line tick number grid} {
                  update_axis $k $x
               }
            }
            set oned_data(xrival) $oned_data(axis_id,y)
            set oned_data(yrival) $oned_data(axis_id,x)
         }
      }
      default {
         puts "update_axis $what $x $args: unknown"
      }
   }
}


proc OneDWin::create_polar {args} {
   global oned_data anno_data tcl_platform
   variable wid

   if ![winfo exist $wid] {return}
   if [info exist oned_data($wid,axis_id)] {return}

   set w $wid
   set ww [$w get -width]
   set oned_data(yp0) [expr $ww / 2]
   set oned_data(yp1) [expr $ww - $ww / 8]
   set hh2 [expr [$w get -height] / 2]
   lappend oned_data(yp0) $hh2
   lappend oned_data(yp1) $hh2

   set oned_data(y_s0) 0
   set oned_data(y_s1) 0
   set oned_data(ys0) 0
   set oned_data(ys1) 0
  #set oned_data(parrows) 0
   set oned_data(yglength) [expr ($hh2 * 3) /4]

   set gm $anno_data(gm,0,$w)

   set oned_data($wid,axis_id) [eval $gm create tickaxis \
           $oned_data(yp0) $oned_data(yp1) $oned_data(y0) \
           $oned_data(y1) $oned_data(y_s0) $oned_data(y_s1) \
           $oned_data(ys0) $oned_data(ys1) \
        -tick $oned_data(ytickup) $oned_data(ymajorstep) \
           $oned_data(ysubtick) $oned_data(yticksize) \
           $oned_data(ymtom) $oned_data(y_log) \
        -grid $oned_data(ygrid_on) $oned_data(yglength) \
           $oned_data(ygwidth) $oned_data(ygcolor) \
           $oned_data(ymgrid_on) $oned_data(ymgwidth) \
           $oned_data(ymgcolor)     -line $oned_data(ycolor) \
           $oned_data(ylinewidth) $oned_data(parrows) \
        -polar_range $oned_data(ang_relative) $oned_data(ang_0) \
           $oned_data(ang_1) $oned_data(ang_ax) \
        -polar_line $oned_data(pcolor) $oned_data(plinewidth) \
        -polar_tick $oned_data(pmajorstep) $oned_data(psubtick) \
           $oned_data(pticksize) $oned_data(pmtom) \
        -polar_grid $oned_data(pgrid_on) $oned_data(pgwidth) \
           $oned_data(pgcolor) $oned_data(pmgrid_on) \
           $oned_data(pmgwidth) $oned_data(pmgcolor)]

   set oned_data(axis_id)   $oned_data($wid,axis_id)
   set oned_data(axis_id,y) $oned_data($wid,axis_id)
   set oned_data(axis_id,p) $oned_data($wid,axis_id)

   $oned_data(wf,notebook) itemconfigure x -text Arc
   $oned_data(wf,notebook) itemconfigure y -text Radius
   set oned_data(wf,xaxis_opt) p

   update_axis axis_id ""
}


proc OneDWin::create_axis {{xtitle ""} args} {
   global oned_data anno_data tcl_platform
   variable wid

   if ![winfo exist $wid] {return}
   if [info exist oned_data($wid,axis_id)] {return}

   set w $wid
   set ww [$w get -width]
   set hh [$w get -height]
   set xL [expr $ww / 8]; set xR [expr $ww / 12]
   set yB [expr $hh / 8]; set yT [expr $hh / 12]
   set oned_data(xp0) $xL;
   lappend oned_data(xp0) [expr $hh - $yB]
   set oned_data(xp1) [expr $ww - $xR]
   lappend oned_data(xp1) [expr $hh - $yB]

   set oned_data(yp0) $oned_data(xp0)
   set oned_data(yp1) "$xL $yT"
   
   set oned_data(xglength) [expr ($hh * 3) / 4]
   set oned_data(yglength) [expr -($ww * 3) / 4]
   set oned_data(xrival) 0
   set oned_data(yrival) 0

   set oned_data(x_s0) 0
   set oned_data(x_s1) 0
   set oned_data(xs0) 0
   set oned_data(xs1) 30
   set oned_data(y_s0) 0
   set oned_data(y_s1) 0
   set oned_data(ys0) 0
   set oned_data(ys1) 30
 # set oned_data(xarrows) 2
 # set oned_data(yarrows) 2

   set gm $anno_data(gm,0,$w)

   foreach x {x y} {
      set oned_data($wid,axis_id,$x) [eval $gm create tickaxis \
           $oned_data(${x}p0) $oned_data(${x}p1) $oned_data(${x}0) \
           $oned_data(${x}1) $oned_data(${x}_s0) $oned_data(${x}_s1) \
           $oned_data(${x}s0) $oned_data(${x}s1) \
        -tick $oned_data(${x}tickup) $oned_data(${x}majorstep) \
           $oned_data(${x}subtick) $oned_data(${x}ticksize) \
           $oned_data(${x}mtom) $oned_data(${x}_log) \
        -grid $oned_data(${x}grid_on) $oned_data(${x}glength) \
           $oned_data(${x}gwidth) $oned_data(${x}gcolor) \
           $oned_data(${x}mgrid_on) $oned_data(${x}mgwidth) \
           $oned_data(${x}mgcolor)     -line $oned_data(${x}color) \
           $oned_data(${x}linewidth) $oned_data(${x}arrows)]
   }

   set oned_data($wid,axis_id) [$gm create group \
             $oned_data($wid,axis_id,x) $oned_data($wid,axis_id,y)] 

   set oned_data(axis_id) $oned_data($wid,axis_id)
   set oned_data(axis_id,x) $oned_data($wid,axis_id,x)
   set oned_data(axis_id,y) $oned_data($wid,axis_id,y)

   $oned_data(wf,notebook) itemconfigure x -text "X Axis"
   $oned_data(wf,notebook) itemconfigure y -text "Y Axis"
   set oned_data(wf,xaxis_opt) x

   update_axis axis_id ""
      
   set xt_pos [expr $ww /3]; lappend xt_pos [expr ($hh * 29) / 30]
   set yt_pos [expr ($ww * 2)/15]; lappend yt_pos [expr $hh / 12]

   global tcl_platform
   if {$tcl_platform(os) == "IRIX"} {
      set yt_pos 28; lappend yt_pos [expr $hh /2]
   }

   if {$xtitle == ""} {set xtitle "X title"}
   set oned_data($wid,x_axis) [eval $gm create utftext $xt_pos \
                                 $anno_data(lcolor) 1 \{$xtitle\} 0]
   if {$args == ""} {
      set ytitle "Y title"
   } else {
      set ytitle [lindex $args 0]
   }
   if {$ytitle != ""} {
      set oned_data($wid,y_axis) [eval $gm create utftext $yt_pos \
                                 $anno_data(lcolor) 1 \{$ytitle\} 270] 
   }
}

proc OneDWin::modify_axis {which x args} {
   global oned_data anno_data
   variable wid

   set w $wid
   if {![info exist oned_data(axis_id)] || \
          ![info exist anno_data(gm,0,$w)]} {
      mess "unable to modify an invalid axis\n" red
      return ""
   }
   set gm $anno_data(gm,0,$w)
   if {([info command $gm] == "") || \
               ([$gm isvalid $oned_data(axis_id)] == "")} {
      mess "unable to modify an invalid axis\n" red
      return ""
   }

   if {$x == "x"} {set i 0} else {set i 1}
   set j $i
   if {[$gm get_group $oned_data(axis_id)] == ""} { set i "" }

   set what $which
   set fit_which $x
   if {$x == "p"} {
      set what polar_$which
      set j 0
      set fit_which x
   }

   switch -- $what {
      title {
         cmd $wid update_anno_win
         if [info exist oned_data($wid,${x}_axis)] {
            if {[lindex $args 0] != ""} { setup_anno_mg $w }
            update_anno_pick $oned_data($wid,${x}_axis)
         }
      }
      move {
         cmd $wid update_anno_win
         if {($x == "x") || ($x == "y")} {
            anno_action move 0 0 $oned_data(axis_id,$x)
            update_axis pos $x
            modify_axis pos $x 
         } else {
            anno_action move 0 0 $oned_data(axis_id)
            update_axis pos x
            update_axis pos y
            modify_axis pos x
            modify_axis pos y
         }   
      }
      pos {
         eval $gm modify $oned_data(axis_id) $i -pos \
                        $oned_data(${x}_p0) $oned_data(${x}_p1)
      }
      range {
         eval $gm modify $oned_data(axis_id) $i -range \
                 $oned_data(${x}0) $oned_data(${x}1) $oned_data(${x}_s0) \
                 $oned_data(${x}_s1) $oned_data(${x}s0) $oned_data(${x}s1)
      }
      line {
         eval $gm modify $oned_data(axis_id) $i -line \
                 $oned_data(${x}color) $oned_data(${x}linewidth) \
                 $oned_data(${x}arrows)
      }
      grid {
         eval $gm modify $oned_data(axis_id) $i -grid \
                 $oned_data(${x}grid_on) $oned_data(${x}rival) \
                 $oned_data(${x}gwidth) $oned_data(${x}gcolor) \
                 $oned_data(${x}mgrid_on) $oned_data(${x}mgwidth) \
                 $oned_data(${x}mgcolor)
      }
      polar_range {
          $gm modify $oned_data(axis_id) -polar_range \
                $oned_data(ang_relative) $oned_data(ang_0) \
                $oned_data(ang_1) $oned_data(ang_ax)
      }
      polar_grid {
         $gm modify $oned_data(axis_id) -polar_grid \
                 $oned_data(pgrid_on) $oned_data(pgwidth) \
                 $oned_data(pgcolor) $oned_data(pmgrid_on) \
                 $oned_data(pmgwidth) $oned_data(pmgcolor)
      }
      polar_line {
         $gm modify $oned_data(axis_id) -polar_line \
                 $oned_data(pcolor) $oned_data(plinewidth)
      }
      polar_tick {
          $gm modify $oned_data(axis_id) -polar_tick \
                  $oned_data(pmajorstep) $oned_data(psubtick) \
                  $oned_data(pticksize) $oned_data(pmtom)
      }
      number {
         eval $gm modify $oned_data(axis_id) $i -number \
                 $oned_data(${x}numsize) $oned_data(${x}numfont) \
                 $oned_data(${x}numpos) $oned_data(${x}numfmt)
      }
      log {
         eval $gm modify $oned_data(axis_id) $i -log \
                 $oned_data(${x}_log)
         if {!$oned_data(${x}_log)} {
            eval $gm modify $oned_data(axis_id) $i -fit $j 1 1
            update_axis range $x
            update_axis number $x
            update_axis tick $x
            modify_axis range $x
         } else {
            update_axis number $x
            update_axis tick $x
         }
      }
      tick {
         eval $gm modify $oned_data(axis_id) $i -tick \
                 $oned_data(${x}tickup) $oned_data(${x}majorstep) \
                 $oned_data(${x}subtick) $oned_data(${x}ticksize) \
                 $oned_data(${x}mtom) $oned_data(${x}_log)
      }
      fit_plots - polar_fit_plots {
         if {$oned_data(${fit_which}fit)} {
            eval $gm modify $oned_data(axis_id) $i -fit $j 1 1
            update_axis range $x
            if {$i != ""} {update_axis number $x}
            update_axis tick $x
            modify_axis range $x
         }
      }
      disable_axis_fit {
         disable_axis_fit $wid $which
      }
      default {
         puts "unknown: modify_axis $which $x $args"
      }
   }
}

proc OneDWin::axis_choiceframe {fm x X} {
   global oned_data
   variable wid 

   form_frame $fm.type flat 0 {0 0 -cols 2}
   form_frame $fm.sub  sunken 1 {0 2 -cols 6}

   if {$x != "p"} {
      form_radiobutton $fm.type.pos  Position \
                      oned_data(${x}axis_swt) pos  {0 0}

      set fmp $fm.sub.pos
      form_choiceframe $fm.sub.pos
         form_label $fmp.sl "  Start " {0 0}
         form_label $fmp.el "   End " {1 0}
         form_entry $fmp.s oned_data(${x}_p0) string {0 2}
         form_entry $fmp.e oned_data(${x}_p1) string {1 2}

         foreach s {s e} {bind $fmp.$s <Return> "OneDWin::modify_axis pos $x"}

         form_button  $fmp.b "  Get the current position" \
                "OneDWin::update_axis pos $x" {5 0 -cols -}
      set pc $x
   } else {
      set pc x
   }

   form_radiobutton $fm.type.rang Range    oned_data(${x}axis_swt) rang {1 0}
   form_radiobutton $fm.type.line Line     oned_data(${x}axis_swt) line {2 0}
   form_radiobutton $fm.type.tick Ticks    oned_data(${x}axis_swt) tick {3 0}

   set fmr $fm.sub.rang
   form_choiceframe $fmr
      form_checkbutton $fmr.fit "Fit Data" oned_data(${pc}fit) {0 0 -cols -}
         $fmr.fit configure -command "OneDWin::modify_axis fit_plots $x"
      form_label $fmr.sl "   From "  {1 0}
      form_label $fmr.el "    To " {2 0}
      if {$x == "p"} {
         form_label $fmr.axl "Rad axis at" {3 0}
         form_entry $fmr.s  oned_data(ang_0) float {1 2}
         form_entry $fmr.e  oned_data(ang_1) float {2 2}
         form_entry $fmr.ax oned_data(ang_ax) float {3 2}

         bind $fmr.ax <Return> "OneDWin::modify_axis range p"
      } else {
         form_entry $fmr.s oned_data(${x}0) float {1 2}
         form_entry $fmr.e oned_data(${x}1) float {2 2}
      }

      foreach s {s e} {bind $fmr.$s <Return> "OneDWin::modify_axis range $x"}

   set fml $fm.sub.line
   form_choiceframe $fml
      form_label $fml.l "Scalar Line:" {0 0 -cols 2}
      form_label $fml.widthl "    width" {1 0}
      form_entry $fml.width oned_data(${x}linewidth) int {1 1}
          bind $fml.width <Return> "OneDWin::modify_axis line $x"
      form_label $fml.colorl "color" {1 3}

      futil_colorentry $fml.color oned_data(${x}color) {1 4} \
             "" "" "" "OneDWin::modify_axis line $x"
     #futil_coloredit_button $fml.color oned_data(${x}color) "" "" \
     #       "OneDWin::modify_axis line $x" {1 4}

      form_checkbutton $fml.grid_on "Draw major grid with:" \
                oned_data(${x}grid_on) {2 0 -cols -}
      form_label $fml.gwidthl "    width" {3 0}
      form_entry $fml.gwidth  oned_data(${x}gwidth) int {3 1}
      form_label $fml.gcolorl "color" {3 3}

      futil_colorentry $fml.gcolor oned_data(${x}gcolor) {3 4} \
              "" "" "" "OneDWin::modify_axis grid $x"
     #futil_coloredit_button $fml.gcolor oned_data(${x}gcolor) "" "" \
     #        "OneDWin::modify_axis grid $x" {3 4}

      form_checkbutton $fml.mg_on "Draw minor grid with:" \
               oned_data(${x}mgrid_on) {4 0 -cols -}
      form_label $fml.mgwidthl "    width" {5 0}
      form_entry $fml.mgwidth  oned_data(${x}mgwidth) int {5 1}
      form_label $fml.mgcolorl "color"  {5 3}

      futil_colorentry $fml.mgcolor oned_data(${x}mgcolor) {5 4} \
              "" "" "" "OneDWin::modify_axis grid $x" 
     #futil_coloredit_button $fml.mgcolor oned_data(${x}mgcolor) "" "" \
     #        "OneDWin::modify_axis grid $x" {5 4}
      
        foreach s {grid_on mg_on} {
           $fml.$s configure -command "OneDWin::modify_axis grid $x"
        }
        foreach s {gwidth mgwidth} {
           bind $fml.$s <Return> "OneDWin::modify_axis grid $x"
        }
        foreach s {width gwidth mgwidth} { $fml.$s configure -width 1 }
        foreach s {color gcolor mgcolor} { $fml.$s configure -width 2 }

   set fmt $fm.sub.tick
   form_choiceframe $fmt
      form_label $fmt.tickmajorl "Major space " {0 0}
      form_label $fmt.tickminorl "Minor number " {1 0}
      form_label $fmt.ticksizel  "Tick size "   {2 0}
      form_label $fmt.mtoml      "Subtick ratio" {3 0}
      form_entry $fmt.tickmajor oned_data(${x}majorstep) float {0 2}
      form_entry $fmt.tickminor oned_data(${x}subtick) int {1 2}
      form_entry $fmt.ticksize  oned_data(${x}ticksize) float {2 2}
      form_entry $fmt.mtom   oned_data(${x}mtom) float {3 2}

         foreach s {tickmajor tickminor ticksize mtom} {
            bind $fmt.$s <Return> "OneDWin::modify_axis tick $x"
         }

      if {$x != "p"} {
         form_checkbutton $fmt.tickdirection "Tick Up" \
                       oned_data(${x}tickup) {5 0}
         form_checkbutton  $fmt.log  "$X log" oned_data(${x}_log) {5 2}
            $fmt.log configure -relief raised -borderwidth 2 \
                        -command "OneDWin::modify_axis log $x"

         $fmt.tickdirection configure -command "OneDWin::modify_axis tick $x"
      }

   if {$x != "p"} {
      form_radiobutton $fm.type.num  Numbers  \
                   oned_data(${x}axis_swt) num  {4 0}

      set fmn $fm.sub.num
      form_choiceframe $fmn
         form_label $fmn.fmtl   " Format: " {0 0}
         form_label $fmn.shiftl " Shift: " {1 0}
         form_label $fmn.fontl  " Font: " {2 0}
         form_entry $fmn.fmt oned_data(${x}numfmt) string {0 2}
         form_entry $fmn.shift oned_data(${x}numpos) string {1 2}

         form_text_optionMenu $fmn.font oned_data(${x}numfont) \
             [list small 0 med 1 large 2] "OneDWin::modify_axis number $x" \
             {2 2 -sticky ew} sunken 2 0 {-height 1 -width 5} "" {-ipady 2}

           foreach s {fmt shift} {
              bind $fmn.$s <Return> "OneDWin::modify_axis number $x"
           }
   }
      
   futil_setup_switch $fm.sub oned_data(${x}axis_swt)
}


proc OneDWin::create_leg {} {
   global oned_data anno_data
   variable wid

   if ![winfo exist $wid] {return}
   if [info exist oned_data($wid,leg_id)] {return}

   set w $wid
   set pos_x [expr [$w get -width] - 200]
   set pos_y 80
   set padx 5
   set pady 5
   set fr_width $oned_data(leg,fr_width)
   if {!$oned_data(leg,fr_on)} { set fr_width -1 }
   set vn_font $oned_data(leg,vn_font)
   if {$oned_data(leg,vn_on)} { set vn_font -1 }

   set gm $anno_data(gm,0,$w)
   set oned_data($wid,leg_id) [eval $gm create legend $pos_x $pos_y \
           $padx $pady    -frame $fr_width $oned_data(leg,fr_padx) \
           $oned_data(leg,fr_pady) $oned_data(leg,fr_color) \
           $oned_data(leg,fr_bcol) -name $vn_font $oned_data(leg,vn_color)]

   set oned_data(leg_id) $oned_data($wid,leg_id)

   set oned_data(leg,xyplots) ""
   update_leg leg_id
}


proc OneDWin::update_leg {what args} {
   global oned_data anno_data
   variable wid

   set w $wid
   if {![info exist oned_data(leg_id)] || \
               ![info exist anno_data(gm,0,$w)]} {
      mess "unable to access an invalid legend\n" red
      return ""
   }
   set gm $anno_data(gm,0,$w)
   if {([info command $gm] == "") || \
               ([$gm isvalid $oned_data(leg_id)] == "")} {
      mess "unable to access an invalid legend\n" red
      return ""
   }

   switch -- $what {
      frame {
         set fr_width $oned_data(leg,fr_width)
         if {!$oned_data(leg,fr_on)} { set fr_width -1 }
         $gm modify $oned_data(leg_id) -frame $fr_width \
                      $oned_data(leg,fr_padx) $oned_data(leg,fr_pady) \
                      $oned_data(leg,fr_color) $oned_data(leg,fr_bcol)
      }
      leg_id {
         set oned_data($wid,leg_id) $oned_data(leg_id)
         set rr [$gm dump $oned_data(leg_id)]
         if {[lindex $rr 0] != "legend"} {
            puts "Error in update_leg leg_id = $oned_data(leg_id)"
            return -1
         }
         set oned_data(leg,pos) [lrange $rr 1 2]
         set oned_data(leg,padx) [lindex $rr 3]
         set oned_data(leg,pady) [lindex $rr 4]
         set i 5
         while {[lindex $rr $i] != ""} {
            switch -- [lindex $rr $i] {
               "-frame" {
                    foreach n {fr_width fr_padx fr_pady fr_color fr_bcol} {
                       incr i
                       set oned_data($n) [lindex $rr $i]
                    }
               }
               "-name" {
                    foreach n {vn_font vn_color} {
                       incr i
                       set oned_data($n) [lindex $rr $i]
                    }
               }
               "-xyp" {
                    incr i
                    set oned_data(leg,xyplots) [lrange $rr $i end]     
                    set i [llength $rr]
               }
               default {puts [lrange $rr $i end]}
            }
            incr i
         }
      }
      var {
         set vn_font $oned_data(leg,vn_font)
         if {!$oned_data(leg,vn_on)} { set vn_font -1 }
         $gm modify $oned_data(leg_id) -name $vn_font $oned_data(leg,vn_color)
      }
      misc {
         $gm modify $oned_data(leg_id) -misc \
                      $oned_data(leg,padx) $oned_data(leg,pady)
      } 
      xyplots {
         eval $gm modify $oned_data(leg_id) -xyp $oned_data(leg,xyplots) 
      }
   }
}
 
proc OneDWin::legend_choiceframe {fm} {
   global oned_data
   variable wid

   form_checkbutton $fm.frame "Draw frame" \
                 oned_data(leg,fr_on) {0 0 -cols 2}
     $fm.frame configure -command "OneDWin::update_leg frame"

   form_label $fm.fr_sp "   " {1 0}
   form_frame $fm.fr sunken 1 {1 1 -cols 2}
   form_label $fm.fr_end "    " {1 3}
   form_label $fm.fr.colorl "Frame color:" {0 0}
   form_label $fm.fr.bcoll  "Back color:" {1 0}
   form_label $fm.fr.widthl "Width:" {2 0}
   form_label $fm.fr.padxl  "Padx:"  {3 0}
   form_label $fm.fr.padyl  "Pady:"  {4 0}

   futil_colorentry $fm.fr.color oned_data(leg,fr_color) {0 1} \
             "" "" "" "OneDWin::update_leg frame"
   futil_colorentry $fm.fr.bcol  oned_data(leg,fr_bcol) {1 1} \
             "" "" "" "OneDWin::update_leg frame" 

  #futil_coloredit_button $fm.fr.color oned_data(leg,fr_color) "" "" \
  #          "OneDWin::update_leg frame" {0 1}
  #futil_coloredit_button $fm.fr.bcol  oned_data(leg,fr_bcol) "" "" \
  #          "OneDWin::update_leg frame" {1 1}

   form_entry $fm.fr.width oned_data(leg,fr_width) int {2 1 -cols 2}
   form_entry $fm.fr.padx  oned_data(leg,fr_padx) int  {3 1 -cols 2}
   form_entry $fm.fr.pady  oned_data(leg,fr_pady) int  {4 1 -cols 2}

      foreach s {width padx pady} {
         bind $fm.fr.$s <Return> "OneDWin::update_leg frame"
      }

   form_checkbutton $fm.name "Var names" \
                 oned_data(leg,vn_on) {5 0 -cols 2}
      $fm.name configure -command "OneDWin::update_leg var"

   form_label $fm.nm_sp "   " {6 0}
   form_frame $fm.nm sunken 1 {6 1 -cols 2}
   form_label $fm.nm_end "    " {6 3}
   form_label $fm.nm.colorl "Color:" {0 0}
   form_label $fm.nm.fontl  "Font:"  {1 0}

   futil_colorentry $fm.nm.color oned_data(leg,vn_color) {0 1} \
             "" "" "" "OneDWin::update_leg var"
  #futil_coloredit_button $fm.nm.color oned_data(leg,vn_color) "" "" \
  #          "OneDWin::update_leg var" {0 1}

   form_text_optionMenu $fm.nm.font oned_data(leg,vn_font) \
        [list small 0 med 1 large 2] "OneDWin::update_leg var" {1 1 -cols 2} \
           sunken 2 0 {-height 1 -width 6} "" {-ipady 2}

  #form_frame $fm.nm.font sunken 2 {1 1 -cols 2}
  #text_optionMenu $fm.nm.font.m oned_data(leg,vn_font) 0 \
  #     {-height 1 -width 6} "OneDWin::update_leg var" \
  #     [list small 0 med 1 large 2] 
  #   pack $fm.nm.font.m -ipady 2
}


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

   set vp [lindex $args 0]
   switch -glob -- $what {
      load - delete - save_* {
         if {[info command $vp] == ""} { return "" }
      }
      load_* {
         if {[info command $vp] == ""} { return "" }
         if {[set fp1 [dfile_open [lindex $args 1] r]] == ""} {return ""}
      }
   }

   switch -- $what {
      create {
         set name [lindex $args 0]
         if {$name == ""} {
            set name [question_box .text "Curve's Name" \
                        "Enter a name for a new curve" [lindex $args 1]]
         }
         if {$name == ""} {return ""}
         set vppts [vppoints -title $name]
         if {$vppts == ""} {return ""}
         lappend oned_data(vppts_list) $vppts
         return $vppts
      }
      list {
         set rr ""
         foreach ss {vppts_list delplt_list} {
            foreach vp $oned_data($ss) {
               if {[info commands $vp] != ""} {
                  lappend rr [$vp get -title] $vp
               }
            }
         }
         return $rr
      }
      delete {
         set k [lsearch $oned_data(plotted_vppts_list) $vp]
         if {$k >= 0} {
            if [info exist oned_data($vp,plots)] {
               foreach {w plt} $oned_data($vp,plots) {
                  set oned_data(wid) $w
                  update_oned_mg win
                  set oned_data(plt_id) $plt
                  update_plots plt_id
                  modify_plots delete 
               }
            } else {
               set oned_data(plotted_vppts_list) [lreplace \
                    $oned_data(plotted_vppts_list) $k $k]
            }
         }
         foreach ss {vppts_list delplt_list} {
            set j [lsearch $oned_data($ss) $vp]
            if {$j >= 0} {
               set oned_data($ss) [lreplace $oned_data($ss) $j $j]
               return $vp 
            } 
         } 
         return 1
      }
      set_xydata {
         if {[llength $args] < 2} {return ""}
         set name [lindex $args 0] 
         if {$name == ""} {
            set name [question_box .text "XY Plot's Name" \
                        "Enter a name for new XY plot" $name]
         }
         set vppts [OneDWin::update_vppoints create $name]
         if {$vppts == ""} {return}
         eval $vppts set_xydata \{$name\} [lrange $args 1 end]
         return $vppts
      }
      save_xydata {
         set file [lindex $args 1]
         if {$file == ""} {
            set file [file_select "Select file name to save xydata" \
                         xyplot "File selection" 0 1]
            if {$file == ""} {return ""}
         }
         $vp save_xydata $file
         return $file
      }
      save_vppts {
         set dir [lindex $args 1]
         if {$dir == ""} {
            set dir [file_select "Select directory name to save vppts data" \
                         vppts "Directory selection" 1 1]
            if {$dir == ""} {return ""}
         }
         file mkdir $dir
         set file [file join $dir index]
         set fp [open $file w]

         set kk [$vp info]
         if {[lindex $kk 1]} {
            update_vppoints save_pts $vp [file join $dir pts]
            puts $fp "pts pts"
         }
         if {[lindex $kk 2] == 1} {
            update_vppoints save_pairs $vp [file join $dir pairs]
            puts $fp "pairs pairs"
         }

         update_vppoints save_xydata $vp [file join $dir xydata]
         puts $fp "xydata xydata"

         if {[lindex $kk 3] == 1} {
            update_vppoints save_vector $vp [file join $dir vector]
            puts $fp "vector vector"
         }
         close $fp

         set xt [expr [$vp get -xvar_type] & 0x00ff]
         if {$xt >= 2} {
            set fp1 [dfile_open [file join $dir xvar_type] w]
            if {$fp1 != ""} {
               puts $fp1 [list $xt [$vp get_xvar]]
               close $fp1
            }
         }

         set yt [expr [$vp get -yvar_type] >> 8]
         set fp1 [dfile_open [file join $dir yvar_type] w]
         if {$fp1 != ""} {
            puts $fp1 [list $yt [lindex [lindex [$vp list_vars 1] $yt] 0]]
            close $fp1
         }

         return $dir
      }
      save_pts - save_pairs - save_ptsvals - save_vector {
         set n [string range $what 5 end]
         set file [lindex $args 1]
         if {$file == ""} {
            set file [file_select "Select file name to save $n" \
                         v3p_$n "File selection" 0 1]
            if {$file == ""} {return ""}
         }
         $vp $what $file
      }
      load_pts {
         set r1 [dfile_skip_lines_til_numbers $fp1]
         while 1 {
            if {[llength $r1] >= 3} { $vp add_pt_only $r1 }
            if [eof $fp1] {break}
            set r1 [gets $fp1]
         }
         close $fp1
      }
      load_xydata {
         set k0 [lindex $args 2]
         set k1 [lindex $args 3]
         if {$k0 == ""} {set k0 0}

         set r1 [dfile_skip_lines_til_numbers $fp1]
         set npts [lindex $r1 0]
         set nv [lindex $r1 1]
         if {($k1 != "") && ($k1 < $nv)} {set nv $k1}

         set p1 [tell $fp1]
         for {set k $k0} {$k < $nv} {incr k} {
            set vn Sv_${k}
            for {set j 0} {$j < $npts} {incr j} {
               set val [gets $fp1]
               $vp add_val $vn [lindex $val $k]
            }
            seek $fp1 $p1 start
         }
         close $fp1
      }
      load_ptsval {
         set kp [lindex $args 2]
         if {$kp == ""} {set kp 0}
         if ![string is digit $kp] {set kp ""}

         set r1 [dfile_skip_lines_til_numbers $fp1]
         set p1 [tell $fp1]
         set nv [llength $r1]

         if {($kp != "") && ($nv - $kp) >= 3} {
            set rr $r1
            set kp2 [expr $kp +2]
            while {[llength $rr] >= $nv} {
               $vp add_pt_only [lrange $rr $kp $kp2]
               set rr [gets $fp1]
            }
            seek $fp1 $p1 start
         } else {
            set kp 0; set kp2 -1 
         }
         for {set k 0} {$k < $nv} {incr k} {
            if {($k >= $kp) && ($k <= $kp2)} {continue}
            set vn Sv_${k}
            set val $r1
            while {[llength $val] >= $nv} {
               $vp add_val $vn [lindex $val $k]
               set val [gets $fp1]
            }
            seek $fp1 $p1 start
         }
         close $fp1
      }
      load_vector {
         set rr [dfile_is_numline $fp1]
         if [lindex $rr 0] {
            set name [file tail [lindex $args 1]]
            set r1 [lindex $rr 1]
         } else {
            foreach name [lindex $rr 1] {
               if {![string is double $name]} {break}
            }
            set r1 [dfile_skip_lines_til_numbers $fp1]
         }
         while 1 {
            if {[llength $r1] >= 3} {$vp add_vec $name $r1}
            if [eof $fp1] {break}
            set r1 [gets $fp1]
         }
         close $fp1
      }
      load_pairs {
         set r1 [dfile_skip_lines_til_numbers $fp1]
         while 1 {
            if {[llength $r1] >= 2} {
               $vp add_pair [lindex $r1 0] [lindex $r1 1]
            }
            if [eof $fp1] {break}
            set r1 [gets $fp1]
         }
         close $fp1

         $vp set_xvar 2 "Distance to Plane"
      }
      load {
         set dir [lindex $args 1]
         if {$dir == ""} {
            set dir [file_select "Select directory name to save vppts data" \
                         vppts "Directory selection" 1 0]
            if {$dir == ""} {return ""}
         }
         set fp [open [file join $dir index] r]
         set rr [gets $fp]
         while {![eof $fp]} {
            set fn [file join $dir [lindex $rr 1]]
            switch -- [lindex $rr 0] {
               pts { update_vppoints load_pts $vp $fn }
               pairs { update_vppoints load_pairs $vp $fn }
               xydata {
                  set k0 0
                  if {[lindex [$vp info] 1] && \
                          ([llength [$vp list_vars 1]] == 1)} {set k0 1}
                  update_vppoints load_xydata $vp $fn $k0
               }
               vector { update_vppoints load_vector $vp $fn }
            }
            set rr [gets $fp]
         }
         close $fp

         set fp1 [dfile_open [file join $dir xvar_type] r]
         if {$fp1 != ""} {
            set rr [gets $fp1]
            switch -- [lindex $rr 0] {
               0 { $vp set_xvar 0 "Point Number" }
               1 { eval $vp set_xvar 1 "Arc Length" [lindex $rr 1] }
               2 { eval $vp set_xvar 2 "Distance" [lindex $rr 1] }
               3 { eval $vp set_xvar 3 "Degree" [lindex $rr 1] }
            }
            close $fp1
         } else {
            $vp set_xvar 1 "Arc Length"
         }
         set fp1 [dfile_open [file join $dir yvar_type] r]
         if {$fp1 != ""} {
            set rr [gets $fp1]
            set yt [lindex $rr 0]
            if {($yt != "") && [string is digit $yt]} {
               $vp set_yvar [expr $yt << 8] [lrange $rr 1 end]
            }
            close $fp1
         }
      }
      xyplot {
         if {$vp == ""} {
            if ![info exist oned_data(vppts_tobe)] { return ""}
            set vp $oned_data(vppts_tobe)
         }
         if {[info command $vp] == ""} {return ""}
         if {[create_plots_setup] == ""} {return ""}

         if {[lindex [$vp info] 1] == 1} {
            if {[OneDWin::cmd "" coords_type] == "p"} {
               if {[$vp get -xvar_type] != 3} {$vp set_xvar 3 "Degree" 0}
            }
         }

         create_plots_with_vppts $vp

         set k [lsearch $oned_data(vppts_list) $vp]
         if {$k >= 0} {
            set oned_data(vppts_list) [lreplace $oned_data(vppts_list) $k $k]
         }

         set n [lsearch $oned_data(delplt_list) $vp]
         if {$n >= 0} {
            set oned_data(delplt_list) \
                        [lreplace $oned_data(delplt_list) $n $n]
         }

         set oned_data(vppts_tobe) ""
      }
   }
}


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 commands $vp] == ""} {return ""}
         global v3p_vars v3p_iscal v3p_ivec v3p_ithr

         set is [lindex $args 0]
         set iv [lindex $args 1]
         if {$is == ""} {set is $v3p_iscal}
         if {$iv == ""} {set iv $v3p_ivec}

         interp_vppts $vp $is $v3p_vars($is,name) \
                      $iv $v3p_vars($iv,name)
      }
      int_res {
         if {[info commands $vp] == ""} {return ""}
         global v3p_vars v3p_iscal v3p_ivec v3p_ithr
        
         set ct [lindex $args 0]
         switch -- $ct {
            do {
               set is [get_v3p_var_index 1 $oned_data(dat,int_svn)]
               if {$is == ""} {update_vppt_fm int_res def_sv}
               set iv [get_v3p_var_index 2 $oned_data(dat,int_vvn)]
               if {$iv == ""} {update_vppt_fm int_res def_vv}

               update_vppt_fm interpolate_vars $is $iv
            }
            sv - vv {
               set vn [decode_complex_partname $oned_data(dat,int_${ct}nsub)]
               for {set i 1} {$i <= $v3p_vars(nsubnames)} {incr i} {
                  set vn ${vn}@$oned_data(dat,int_${ct}nsub,$i)
               }
               set oned_data(dat,int_${ct}n) $vn
            }
            def_sv {
               if [info exists v3p_vars($v3p_iscal,name)] {
                  set oned_data(dat,int_svn) $v3p_vars($v3p_iscal,name)
                  set oned_data(dat,int_svnsub) \
                      [update_vartab subname "" $oned_data(dat,int_svn)]
                  for {set i 1} {$i <= $v3p_vars(nsubnames)} {incr i} {
                     set s [decode_val_from_varname_sub \
                             [update_vartab subname $i $oned_data(dat,int_svn)]]
                     set oned_data(dat,int_svnsub,$i)  [lindex $s 0]
                  }
               }
            }
            def_vv {
              if [info exists v3p_vars($v3p_ivec,name)] {
                 set oned_data(dat,int_vvn) $v3p_vars($v3p_ivec,name)
                  set oned_data(dat,int_vvnsub) \
                      [update_vartab subname "" $oned_data(dat,int_vvn)]
                  for {set i 1} {$i <= $v3p_vars(nsubnames)} {incr i} {
                      set s [decode_val_from_varname_sub \
                             [update_vartab subname $i $oned_data(dat,int_vvn)]]
                     set oned_data(dat,int_vvnsub,$i)  [lindex $s 0]
                  } 
               }
            }
         }
      }
      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 v3p_vars v3p_iscal v3p_ivec v3p_ithr
   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) \
        "update_vartab sv_namelist" {1 1 -cols 2} "" \
        "OneDWin::update_vppt_fm int_res sv"
   for {set i 1} {$i <= $v3p_vars(nsubnames)} {incr i} {
      form_label $fmr.sv_${i}l [lindex $v3p_vars(subnames) [expr $i-1]] \
             {2 0 -sticky nws}
      form_text_optionMenu_nt $fmr.sv_$i oned_data(dat,int_svnsub,$i) \
         [update_vartab 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) \
        "update_vartab vv_namelist" {5 1 -cols 2} "" \
        "OneDWin::update_vppt_fm int_res vv"
   for {set i 1} {$i <= $v3p_vars(nsubnames)} {incr i} {
      form_label $fmr.vv_${i}l [lindex $v3p_vars(subnames) [expr $i-1]] \
             {6 0 -sticky nws}
      form_text_optionMenu_nt $fmr.vv_$i oned_data(dat,int_vvnsub,$i) \
         [update_vartab 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}
}

proc OneDWin::update_plots {what args} {
   global oned_data anno_data
   variable wid

   if ![winfo exist $wid] {return}
   set w $wid
   if {![info exist anno_data(gm,0,$w)] || \
          ([info command $anno_data(gm,0,$w)] == "")} {return ""}
   set gm $anno_data(gm,0,$w)

   switch -- $what {
      plt_list {
         if ![info exist oned_data($wid,plt_list)] {return ""}
         return $oned_data($wid,plt_list)
      }
      plt_id {
         if ![info exist oned_data(plt_id)] {return ""}

         set oned_data($wid,plt_id) $oned_data(plt_id)
         set rr [$gm dump $oned_data(plt_id)]
         if {[lindex $rr 0] != "xyplot"} {
            set id $oned_data(plt_id)
            puts "Error OneDWin::update_plots plt_id wid = $wid $id"
            return -1
         }
         set axis_id [lindex $rr 1]
         if {($axis_id != $oned_data(axis_id)) || \
             ($oned_data(axis_id) != $oned_data($wid,axis_id))} {
            set id0 $oned_data(axis_id)
            set id1 $oned_data($wid,axis_id)
            puts "Warning update_plots $id0 != $id1"
            set oned_data(axis_id) $axis_id
            set oned_data($wid,axis_id) $axis_id
         }
         set fit_xs [lindex $rr 2]
         set oned_data(plt,fit_x0) [expr $fit_xs & 0x0f]
         set oned_data(plt,fit_x1) [expr $fit_xs >> 4]
         set fit_ys [lindex $rr 3]
         set oned_data(plt,fit_y0) [expr $fit_ys & 0x0f]
         set oned_data(plt,fit_y1) [expr $fit_ys >> 4]
         set i 4
         while {[lindex $rr $i] != ""} {
            switch -- [lindex $rr $i] {
               "-line" {
                   incr i
                   set lwidth [lindex $rr $i]
                   if {$lwidth < 0} {
                      set oned_data(plt,line) 0
                      set oned_data(plt,lwidth) [expr -$lwidth]
                   } else {
                      set oned_data(plt,line) 1
                      set oned_data(plt,lwidth) $lwidth
                   }
                   foreach s {lpttn lcolor} {
                      incr i
                      set oned_data(plt,$s) [lindex $rr $i]
                   }
               }
               "-symbol" {
                   incr i
                   set stype [lindex $rr $i]
                   if {$stype > 0} {
                      set oned_data(plt,symbols) 1 
                      set oned_data(plt,stype) $stype
                   } else {
                      set oned_data(plt,symbols) 0
                      set oned_data(plt,stype) [expr -$stype]
                   }
                   foreach s {ssize swidth scolor fill} {
                      incr i
                      set oned_data(plt,$s) [lindex $rr $i]
                   }
               }
               "-vppts" {
                   incr i
                   set oned_data(plt,vppts) [lindex $rr $i]
                   update_plots x_type
                   if {[info command $oned_data(plt,vppts)] != ""} {
                      set rr [$oned_data(plt,vppts) info]
                      set oned_data(plt,vppts_ready)  [lindex $rr 0]
                      set oned_data(plt,vppts_is3d)   [lindex $rr 1]
                      set oned_data(plt,vppts_ispair) [lindex $rr 2]
                   }
                   set oned_data(plt,y_type) \
                       [expr [$oned_data(plt,vppts) get -yvar_type] >> 8]
               }
               default { puts [lrange $rr $i end] }
            }
            incr i
         } 
         if {$fit_xs || $fit_ys} { update_axis axis_id "" }
         set oned_data(plt,axisfit) 0
         if [info exist oned_data(plt,type_m)] {
            #update_text_optionMenu $oned_data(plt,type_m)
         }
      }
      y_type {
         set vp $oned_data(plt,vppts)
         if {[info command $vp] == ""} {return ""}
         set rr ""
         foreach s [lrange [$vp list_vars 1] 1 end] {
            lappend rr [lindex $s 0] [lindex $s 1]
         }
         return $rr
      }
      x_type {
         set vp $oned_data(plt,vppts)
         if {[info command $vp] == ""} {return}
         set oned_data(plt,x_type) [expr [$vp get -xvar_type] & 0x00ff]

         set rr [$vp get_xvar]
         switch -- $oned_data(plt,x_type) {
            1 { set oned_data(plt,dist0) [$vp get -dist0] }
            2 {
               if {[llength $rr] == 4} {
                  foreach {c i} {A 0 B 1 C 2 D 3} {
                    set oned_data(plt,$c) [lindex $rr $i]
                  }   
               }
            }
            3 {
               if {[llength $rr] == 3} {
                  set oned_data(plt,center) [lindex $rr 0]
                  set oned_data(plt,zero)   [lindex $rr 1]
                  set oned_data(plt,axis)   [lindex $rr 2]
               } 
            }
         }
      }
   }
}


proc OneDWin::create_plots_setup {} {
   global oned_data anno_data
   variable wid

   if {$oned_data(win_list) == ""} {
      setup_oned_mg
      update_oned_mg new_win
      create_axis
      create_leg
   }

   if ![winfo exist $wid] {return ""}
   if ![info exist oned_data($wid,plt_list)] {
      set oned_data($wid,plt_list) ""
   }
   if ![info exist oned_data($wid,axis_id)] { create_axis }
   if ![info exist oned_data($wid,leg_id)] { create_leg }

   return $wid
}


proc OneDWin::create_plots {type {name ""} args}  {
   global oned_data anno_data
   variable wid

   if {[create_plots_setup] == ""} {return}

   set vppts ""

   switch -- $type {
      read_xydata {
         set file [lindex $args 0]
         if {$file == ""} {
            set file [file_selection -message "Select XY plot's data file"]
            if {$file == ""} {return}
         }
         if {$name == ""} {
            set name [file tail $file]
            set name [question_box .text "XY Plot's Name" \
                        "Enter a name for new XY plot" $name]
         } 
         set vppts [OneDWin::update_vppoints create $name]
         if {$vppts == ""} {return} 
         $vppts read_xydata $file
      }
      set_xydata {
         if {$args == ""} {return} 
         if {$name == ""} {
            set name [question_box .text "XY Plot's Name" \
                        "Enter a name for new XY plot" $name]
         }
         set vppts [OneDWin::update_vppoints create $name]
         if {$vppts == ""} {return}
         eval $vppts set_xydata \{$name\} $args
         if [info exist anno_data($wid,x_axis)] {
            set xt [[$anno_data(gm,0,$wid) dump $anno_data($wid,x_axis)] 5]
            if {$xt != ""} { $vppts set_xvar 0xe $xt }
         }
      }
   }

   update_vppoints xyplot $vppts
}


proc OneDWin::create_plots_with_vppts {vppts args}  {
   global oned_data anno_data
   variable wid

   if {$vppts == ""} {return}
   set oned_data(plt,vppts) $vppts

   set w $wid
   set gm $anno_data(gm,0,$w)

   foreach x {x y} {
      if {[$gm get_group $oned_data(axis_id)] != ""} {
         modify_axis title $x
         set anno_data(utftext,text) [$vppts get_varnames ${x}v]
         modify_anno_pick utftext
      }

      set fit 0
      if {$oned_data(${x}fit)} {set fit 0x11}
      if {$oned_data(plt,axisfit)} {
         if {$oned_data(plt,fit_${x}0)} { set fit [expr $fit | 0x02] }
         if {$oned_data(plt,fit_${x}1)} { set fit [expr $fit | 0x20] }
      }
      set ${x}fit [expr $fit]
   }

   set oned_data($wid,plt_id) [$gm create xyplot $oned_data(axis_id) "" \
                 $xfit $yfit 0 -vppts $oned_data(plt,vppts)]

#           -symbol $oned_data(plt,stype) \
#           $oned_data(plt,ssize) $oned_data(plt,swidth) \
#           $oned_data(plt,scolor)   -line $oned_data(plt,lwidth) \
#           $oned_data(plt,lpttn) $oned_data(plt,lcolor)]

   set name [$vppts get -title]
   lappend oned_data($wid,plt_list) $name $oned_data($wid,plt_id)
   set oned_data(plt_id) $oned_data($wid,plt_id)

   lappend oned_data(plotted_vppts_list) $vppts
   lappend oned_data($vppts,plots) $wid $oned_data($wid,plt_id)

   update_plots plt_id

   set oned_data(plt,axisfit) 0
   lappend oned_data(leg,xyplots) $oned_data(plt_id)
   update_leg xyplots 
   update_leg frame
   update_leg var

   update_oned_mg raise $w
}


proc OneDWin::modify_plots {what args} {
   global oned_data anno_data
   variable wid

   set w $wid
   if {![info exist oned_data(plt_id)] || \
              ![info exist anno_data(gm,0,$w)]} {
      mess "unable to modify an invalid plot\n" red
      return ""
   }
   set gm $anno_data(gm,0,$w)
   if {([info command $gm] == "") ||  \
               ([$gm isvalid $oned_data(plt_id)] == "")} {
      mess "unable to modify an invalid plot\n" red
      return ""
   }

   switch -- $what {
      line {
         set lwidth [expr $oned_data(plt,line) ? $oned_data(plt,lwidth) \
                                 : -$oned_data(plt,lwidth)]
         $gm modify $oned_data(plt_id) -line $lwidth \
               $oned_data(plt,lpttn) $oned_data(plt,lcolor)
         update_leg xyplots
      }
      symbol {
         set stype [expr $oned_data(plt,symbols) ? $oned_data(plt,stype) \
                               :  -$oned_data(plt,stype)]
         $gm modify $oned_data(plt_id) -symbol $stype \
               $oned_data(plt,ssize) $oned_data(plt,swidth) \
               $oned_data(plt,scolor) $oned_data(plt,fill)
         update_leg xyplots
      }
      axis {
         if ![info exist oned_data($wid,axis_id)] {
            create_axis
         }
         foreach x {x y} {
            set fit 0
            if {$oned_data(${x}fit)} {set fit 0x11}
            if {$oned_data(plt,axisfit)} {
               if {$oned_data(plt,fit_${x}0)} { set fit [expr $fit | 0x02] }
               if {$oned_data(plt,fit_${x}1)} { set fit [expr $fit | 0x20] }
            }
            set ${x}fit $fit
         }
         $gm modify $oned_data(plt_id) -axis \
               $oned_data(axis_id) "" $xfit $yfit 0
      }
      xy {
         if {[info exist oned_data(plt,vppts)] && \
                  ($oned_data(plt,vppts) != "")} {
            $gm modify $oned_data(plt_id) -vppts $oned_data(plt,vppts)
         }
      }
      y_type {
         set vp $oned_data(plt,vppts)
         if {[info command $vp] == ""} {return}
         set yt [expr [$vp get -yvar_type] >> 8]
         if {$yt != $oned_data(plt,y_type)} {
            $vp set_yvar [expr $oned_data(plt,y_type) << 8] ""

            set axisfit $oned_data(plt,axisfit)
            set oned_data(plt,axisfit) 1

            modify_plots axis

            set oned_data(plt,axisfit) $axisfit
         }
      }
      x_type {
         set sub [lindex $args 0]
         set vp $oned_data(plt,vppts)
         if {[info command $vp] == ""} {return}
         set xt [expr [$vp get -xvar_type] & 0x00ff]

         set axisfit $oned_data(plt,axisfit)
         set oned_data(plt,axisfit) 1

         if {$sub == ""} {
            if {$xt == $oned_data(plt,x_type)} { set rr [$vp get_xvar] }
            switch -- $oned_data(plt,x_type) {
               0 {
                  if {$xt != 0} {
                     $vp set_xvar 0 "Point Number"
                     modify_plots axis
                  }
               }
               1 {
                  if {$xt != 1} {
                     $vp set_xvar 1 "Arc Length"
                     modify_plots axis
                  }
                  set oned_data(plt,dist0) [$vp get -dist0]
               }
               2 {
                  if {$xt != 2} {
                     set rr [lindex [$vp set_xvar 2 "Distance"] 1]
                     modify_plots axis
                  }
                  if {[llength $rr] == 4} {
                     foreach {c i} {A 0 B 1 C 2 D 3} {
                       set oned_data(plt,$c) [lindex $rr $i]
                     }
                  }
               }
               3 {
                  if {$xt != 3} {
                     set rr [lrange [$vp set_xvar 3 "Degree"] 1 end]
                     modify_plots axis
                  }
                  if {[llength $rr] == 3} {
                     set oned_data(plt,center) [lindex $rr 0]
                     set oned_data(plt,zero)   [lindex $rr 1]
                     set oned_data(plt,axis)   [lindex $rr 2]
                  }
               }
            }
         } else {
            switch -- $sub {
               0 {$vp set_xvar 0 "Point Number"}
               1 {
                  $vp set_xvar 1 "Arc Length" $oned_data(plt,dist0)
               }
               2 {
                  $vp set_xvar 2 "Distance" $oned_data(plt,A) \
                       $oned_data(plt,B) $oned_data(plt,C) $oned_data(plt,D)
               }
               3 {
                  eval $vp set_xvar 3 "Degree" $oned_data(plt,center) \
                           $oned_data(plt,zero) $oned_data(plt,axis)
               }
            }
            modify_plots axis
         }
         set oned_data(plt,axisfit) $axisfit
      }
      rename {
         if {[info command $oned_data(plt,vppts)] == ""} {return}

         set n [lsearch $oned_data($wid,plt_list) $oned_data(plt_id)]
         if {$n < 1} {return}
         set n1 [expr $n -1]

         set name [lindex $args 0]
         if {$name == ""} {
            set name [lindex $oned_data($wid,plt_list) $n1]
            set name [question_box .text "Change XY Plot's Name" \
                        "Edit name for XY plot" $name]
         }
         set oned_data($wid,plt_list) \
               [lreplace $oned_data($wid,plt_list) $n1 $n1 $name]
         set oned_data(plt_id) $oned_data($wid,plt_id)

         $oned_data(plt,vppts) configure -title "$name" 
         
         OneDWin::update_leg var
      }
      save_xydata {
         if {[info command $oned_data(plt,vppts)] == ""} {return}
              
         set name [do_list oned_data($wid,plt_list) $oned_data(plt_id)]
         set file [file_select "Set file name to save plot $name" $name \
                         "File selection" 0 1]
         if {$file == ""} {return}
         
         $oned_data(plt,vppts) save_xydata $file
      }
      delete {
         set plt_id $oned_data(plt_id)

         set n [lsearch $oned_data(leg,xyplots) $plt_id]
         if {$n >= 0} {
            set oned_data(leg,xyplots) [lreplace $oned_data(leg,xyplots) $n $n]
            update_leg xyplots
         }

         set n [lsearch $oned_data($wid,plt_list) $plt_id]
         if {$n < 1} {return}
         set n1 [expr $n -1]
         set oned_data($wid,plt_list) \
               [lreplace $oned_data($wid,plt_list) $n1 $n]

         set vp $oned_data(plt,vppts)

         set k [lsearch $oned_data($vp,plots) $plt_id]
         if {$k > 0} {
            set k1 [expr $k -1]
            set oned_data($vp,plots) [lreplace $oned_data($vp,plots) $k1 $k]
            if {$oned_data($vp,plots) == ""} {
               unset oned_data($vp,plots)
               lappend oned_data(delplt_list) $vp
            }
         }

         set k [lsearch $oned_data(plotted_vppts_list) $vp]
         if {($k >= 0) && ![info exist oned_data($vp,plots)]} {
            set oned_data(plotted_vppts_list) \
                  [lreplace $oned_data(plotted_vppts_list) $k $k]
         }

         $gm remove $oned_data(plt_id)
         if {[llength $oned_data($wid,plt_list)] > $n} {
            set oned_data(plt_id) [lindex $oned_data($wid,plt_list) $n]
         } else {
            set oned_data(plt_id) [lindex $oned_data($wid,plt_list) 1]
         }

         if {$oned_data(plt_id) != ""} {
            update_plots plt_id
         }
      }
      undelete {
         if {$oned_data(delplt_list) != ""} {
            if {$args != ""} {
               set vppts [lindex $args 0]
               set n [lsearch $oned_data(delplt_list) $vppts]
            } else {
               set vppts [lindex $oned_data(delplt_list) end]
               set n [expr [llength $oned_data(delplt_list)] -1]
            }
            if {$n >= 0} {
               OneDWin::update_vppoints xyplot $vppts
            }
         }
      }
      empty_recycle {
         foreach vp $oned_data(delplt_list) {
            if {[info command $vp] != ""} { $vp delete }
         }
         set oned_data(delplt_list) "" 
         return
      }
   }
   update_oned_mg raise $w
}

proc OneDWin::plots_choiceframe {fm} {
   global arrowlines_predefined symbols_predefined vpost_library
   global oned_data
   variable wid

   form_frame $fm.type flat 0 {0 0 -cols - -sticky ew}
   form_label $fm.type.l "Current Plot" {0 0 -cols 2}

   form_text_optionMenu_dynamic $fm.type.c oned_data(plt_id) \
          "OneDWin::update_plots plt_list"  "OneDWin::update_plots plt_id" \
          {0 2 -cols 4 -sticky ew} flat 0 0 {-height 1 -width 16} \
          oned_data(plt,type_m) {-ipady 2}

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

   form_radiobutton $fm.p.dswt.xt "X type" oned_data(plt_swt) xt {0 0}
   form_radiobutton $fm.p.dswt.yt "Y type" oned_data(plt_swt) yt {1 0}
   form_radiobutton $fm.p.dswt.pm "S or L" oned_data(plt_swt) pm {2 0}
   form_radiobutton $fm.p.dswt.rt "Ratios" oned_data(plt_swt) rt {3 0}

   set fmx $fm.p.dsub.xt
   form_choiceframe $fmx

     set act {{oned_data(plt,vppts_is3d) 1}}

     form_frame $fmx.type flat 0 {0 0}
     form_frame $fmx.sub  ridge 1 {1 0}

     form_radiobutton $fmx.type.xnp "Number of point" \
             oned_data(plt,x_type) 0 {0 1 -cols 3} $act
     form_radiobutton $fmx.type.xal "Arc length of curve" \
             oned_data(plt,x_type) 1 {1 1 -cols 3} $act
     form_radiobutton $fmx.type.xds "Distance from plane" \
             oned_data(plt,x_type) 2 {2 1 -cols 3} $act
     form_radiobutton $fmx.type.xag "Angles" \
             oned_data(plt,x_type) 3 {3 1 -cols 3} $act

        foreach a {xnp xal xds xag} {
           $fmx.type.$a configure -command "OneDWin::modify_plots x_type"
        }

     form_choiceframe $fmx.sub.0 
        # empty

     form_choiceframe $fmx.sub.1
       if 0 {
       form_label $fmx.sub.1.l "Start point:" {0 0 -cols 6}
       form_label $fmx.sub.1.xl "   X " {1 0 -cols 4}
       form_label $fmx.sub.1.yl "   Y " {2 0 -cols 4}
       form_label $fmx.sub.1.zl "   Z " {3 0 -cols 4}
       form_entry $fmx.sub.1.x  oned_data(plt,p0,x) float {1 4 -cols 4}
       form_entry $fmx.sub.1.y  oned_data(plt,p0,y) float {2 4 -cols 4} 
       form_entry $fmx.sub.1.z  oned_data(plt,p0,z) float {3 4 -cols 4} 
       }
       form_label $fmx.sub.1.vl "Start length:" {5 0 -cols 6}
       form_entry $fmx.sub.1.v oned_data(plt,dist0) float {5 6 -cols 2}

         bind $fmx.sub.1.v <Return> "OneDWin::modify_plots x_type 1"
    
     form_choiceframe $fmx.sub.2
       form_label $fmx.sub.2.l "Reference plane:" {0 0 -cols 6}
       form_label $fmx.sub.2.al "   A " {1 0 -cols 4}
       form_label $fmx.sub.2.bl "   B " {2 0 -cols 4}
       form_label $fmx.sub.2.cl "   C " {3 0 -cols 4}
       form_label $fmx.sub.2.dl "   D " {4 0 -cols 4}
       form_entry $fmx.sub.2.a  oned_data(plt,A) float {1 4 -cols 4} 
       form_entry $fmx.sub.2.b  oned_data(plt,B) float {2 4 -cols 4} 
       form_entry $fmx.sub.2.c  oned_data(plt,C) float {3 4 -cols 4} 
       form_entry $fmx.sub.2.d  oned_data(plt,D) float {4 4 -cols 4}

       foreach s {a b c d} {
          bind $fmx.sub.2.$s <Return> "OneDWin::modify_plots x_type 2"
       } 

     form_choiceframe $fmx.sub.3
       form_label $fmx.sub.3.l "Cylinderical system's" {0 0 -cols 6}
       form_label $fmx.sub.3.zxl "  Z axis " {1 0 -cols 4}
       form_label $fmx.sub.3.ctl "  Origin " {2 0 -cols 4}
       form_label $fmx.sub.3.p0l "  Zero pt" {3 0 -cols 4}

       form_entry $fmx.sub.3.zx oned_data(plt,axis) string {1 4 -cols 4}
       form_entry $fmx.sub.3.ct oned_data(plt,center) string {2 4 -cols 4}
       form_entry $fmx.sub.3.p0 oned_data(plt,zero) string {3 4 -cols 4}

       foreach s {zx ct p0} {
          bind $fmx.sub.3.$s <Return> "OneDWin::modify_plots x_type 3"
       }

     futil_setup_switch $fmx.sub oned_data(plt,x_type)

   set fmy $fm.p.dsub.yt
   form_choiceframe $fmy

     form_label $fmy.svl "Select a scalar to plot" {0 0 -cols - -sticky w}
     form_label $fmy.sv_sp "  " {1 0}
     form_text_optionMenu_dynamic $fmy.sv oned_data(plt,y_type) \
           "OneDWin::update_plots y_type" "OneDWin::modify_plots y_type" \
           {1 1 -cols 2 -sticky ew} flat 0 0 {-height 1 -width 10}

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

     form_checkbutton $fmp.symbols "with Symbols:    " \
                oned_data(plt,symbols) {0 0 -cols -}
        $fmp.symbols configure -command "OneDWin::modify_plots symbol"
     form_label $fmp.sym_tsp "   " {1 0}
     form_label $fmp.sym_ssp "   " {2 0}
     form_label $fmp.sym_fsp "   " {3 0}
     form_label $fmp.sym_csp "   " {4 0}
     form_label $fmp.sym_typel "Type: " {1 1}
     form_label $fmp.sym_sizel "Size: " {2 1}
     form_label $fmp.sym_fsp2 "     " {3 1}
     form_label $fmp.sym_colorl "Color: " {4 1}

     form_bitmap_optionMenu  $fmp.sym_type oned_data(plt,stype) \
          $vpost_library $symbols_predefined \
          "OneDWin::modify_plots symbol" {1 2} raised 0 6

     form_entry $fmp.sym_size oned_data(plt,ssize) int {2 2}
        bind $fmp.sym_size <Return> "OneDWin::modify_plots symbol"

     form_checkbutton $fmp.sym_fill "fill symbol" \
               oned_data(plt,fill) {3 2} 
         $fmp.sym_fill configure -command "OneDWin::modify_plots symbol"

     futil_colorentry $fmp.sym_color oned_data(plt,scolor) {4 2} \
               "" "" "" "OneDWin::modify_plots symbol"
    #futil_coloredit_button $fmp.sym_color oned_data(plt,scolor) "" "" \
    #          "OneDWin::modify_plots symbol" {4 2}

     form_checkbutton $fmp.line "with Line:   " \
              oned_data(plt,line) {5 0 -cols -}
        $fmp.line configure -command "OneDWin::modify_plots line"
     form_label $fmp.ln_wsp "   " {6 0}
    #form_label $fmp.ln_psp "   " {7 0}
     form_label $fmp.ln_csp "   " {8 0}
     form_label $fmp.ln_wl "Line width: " {6 1}
    #form_label $fmp.ln_pl "Pattern: " {7 1}
     form_label $fmp.ln_cl "Color: " {8 1}
      
     form_entry $fmp.ln_w oned_data(plt,lwidth) int {6 2}
        bind $fmp.ln_w <Return> "OneDWin::modify_plots line"
    #form_entry $fmp.ln_p oned_data(plt,lpttn)  int {7 2}

     futil_colorentry $fmp.ln_color oned_data(plt,lcolor) {8 2} \
                "" "" "" "OneDWin::modify_plots line"
    #futil_coloredit_button $fmp.ln_color oned_data(plt,lcolor) "" "" \
    #           "OneDWin::modify_plots line" {8 2}
   
   set fmr $fm.p.dsub.rt
   form_choiceframe $fmr

     form_checkbutton $fmr.axisfit "Axis fit with:" \
                     oned_data(plt,axisfit) {0 0 -cols -}
     form_label $fmr.fx_sp "   " {1 0}
     form_label $fmr.fy_sp "   " {2 0}
     form_checkbutton $fmr.fitx0 Xmin oned_data(plt,fit_x0) {1 1}
     form_checkbutton $fmr.fitx1 Xmax oned_data(plt,fit_x1) {1 2}
     form_checkbutton $fmr.fity0 Ymin oned_data(plt,fit_y0) {2 1}
     form_checkbutton $fmr.fity1 Ymax oned_data(plt,fit_y1) {2 2}

        foreach s {axisfit fitx0 fitx1 fity0 fity1} {
           $fmr.$s configure -command "OneDWin::modify_plots axis"
        }

   if 0 {
     form_checkbutton $fmr.deformx "Deform X values with:" \
                     oned_data(plt,deformx) {5 0 -cols -}
     form_label $fmr.dxr_sp "   " {6 0}
     form_label $fmr.dx0_sp "   " {7 0}
     form_label $fmr.dxr_l "rate " {6 1}
     form_label $fmr.dx0_l "shift " {7 1}
     form_entry $fmr.dx_r  oned_data(plt,rx) float {6 2}
     form_entry $fmr.dx_x0 oned_data(plt,x0) float {7 2}

     form_checkbutton $fmr.deformy "Deform Y values with:" \
                     oned_data(plt,deformy) {10 0 -cols -} 
     form_label $fmr.dyr_sp "   " {11 0}
     form_label $fmr.dy0_sp "   " {12 0}
     form_label $fmr.dyr_l "rate "  {11 1}
     form_label $fmr.dy0_l "shift " {12 1}
     form_entry $fmr.dy_r  oned_data(plt,ry) float {11 2}
     form_entry $fmr.dy_y0 oned_data(plt,y0) float {12 2}
   }

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



proc update_oned_mg {what args} {
   global oned_data anno_data

   switch -- $what {
      list_win {
         if {$args != ""} {
            set j [lindex $args 0]
            if [info exist oned_data($i,name)] {
               return $oned_data($i,name)
            } else {
               return ""
            }
         } else {
            return $oned_data(win_list)
         }
      }
      win {
         OneDWin::cmd $oned_data(wid) win
         set k [lsearch $oned_data(win_list) $oned_data(wid)]
         if {$k > 0} {
            incr k -1
            top_title .oned_mg [concat on [lindex $oned_data(win_list) $k]]
            update_oned_mg raise $oned_data(wid) 
         }
      }
      raise {
         set w [lindex $args 0]
         if [winfo exist $w] {
            set win .[lindex [split $w .] 1]
            if ![winfo ismapped $win] { top_deiconify $win }
            top_raise $win
        }
      }
      new_win {
         if ![winfo exist .oned_mg] {setup_oned_mg}
         if {$args == ""} {
            set name [question_box .text "1D Window Name" \
                      "Enter a name for new 1D window" "1D window"]
           if {$name == ""} {return}
         } else {
            set name [lindex $args 0]
         }
         set oned_data(wid) [OneDWin::new $name] 

         OneDWin::cmd $oned_data(wid) win

         top_title .oned_mg "on $name"
      }
      polar_win {
         if {$args == ""} {
            set name [question_box .text "Polar Window Name" \
                      "Enter a name for new polar window" "Polar window"]
            if {$name == ""} {return}
         } else {
            set name [lindex $args 0]
         }
         update_oned_mg new_win $name
         OneDWin::create_polar 
      }
      del_win {
         if {![info exist oned_data(wid)] || \
                ![winfo exist $oned_data(wid)]} {
            mess "The current 1D window is unknown\n" red
            return ""
         }
         if {[tk_messageBox -title "Confirm To Delete" -parent .oned_mg \
                  -message "Deleting an 1D window would permanently \
                          remove its content.\n Are you sure to delete?" \
                  -type okcancel] != "ok"} {
            return ""
         }

         set w $oned_data(wid)
         set gm $anno_data(gm,0,$w)

         foreach {n plt} [OneDWin::update_plots plt_list] {
            set oned_data(plt_id) $plt
            OneDWin::update_plots plt_id
            OneDWin::modify_plots delete
         }
         unset oned_data($w,plt_list)

         foreach t {x_axis y_axis leg_id} {
            if {[info exist oned_data($w,$t)]} {
               $gm remove $oned_data($w,$t)
               unset oned_data($w,$t)
            }
         }

         foreach s [array name oned_data $w,*] {
            unset oned_data($s)
         }

         $gm delete

         global gwin_list
         foreach vn {gwin_list oned_data(win_list)} {
            if ![info exist $vn] {continue}
            set k [lsearch [set $vn] $w]
            if {$k > 0} {
               set k1 [expr $k -1]
               set $vn [lreplace [set $vn]  $k1 $k]
            }
         }

         global tdv_post_menu
         if {[info exist tdv_post_menu($w)] && \
                [winfo exist $tdv_post_menu($w)]} {
            destory $tdv_post_menu($w)
            unset tdv_post_menu($w)
         }
  
         $w delete
         top_destroy .[lindex [split $w .] 1]

         unset anno_data(gm,0,$w)
      }
      ed_new {
         set vn [lindex $args 0]
         set val [OneDWin::var set $vn]
         switch -- $vn {
            background {
               set val [tk_chooseColor -initialcolor $val -parent .oned_mg\
                         -title  "Set Background Color for New 1D Window"]
            }
            width - height {
               set val [question_box .integer "Set Window Size" \
                     "Enter $vn for new 1D window" $val]
            } 
         }
         if {$val != ""} {OneDWin::var set $vn $val}
      }
      ed_win {
         set wid $oned_data(wid)
         if {![winfo exist $wid]} {return}

         set vn [lindex $args 0]
         set val [$wid get -$vn]
         switch -- $vn {
            background {
               set val [tk_chooseColor -initialcolor $val -parent .oned_mg\
                     -title  "Set Background Color for $oned_data($wid,name)"]
            }
            width - height {
               set val [question_box .integer "Set Window Size" \
                     "Enter $vn for $oned_data($wid,name)" $val]
            }
         }
         if {$val != ""} {$wid configure -$vn $val}
      }
      ed_axis {
         if [info exist oned_data(wf,notebook)] {
            $oned_data(wf,notebook) raise [$oned_data(wf,notebook) page 1]
         }
      }
      ed_legend {
         if [info exist oned_data(wf,notebook)] {
            $oned_data(wf,notebook) raise [$oned_data(wf,notebook) page 2]
         }
      }
      ed_plots {
         if [info exist oned_data(wf,notebook)] {
            $oned_data(wf,notebook) raise [$oned_data(wf,notebook) page 3]
         }
      }
      save {
      }
      delete {
         OneDWin::modify_plots delete
      }
      undelete {
         OneDWin::modify_plots undelete
      }
      opt { 
         puts "update_oned_mg option: not yet"
      }
      tool {
         puts "update_oned_mg tool: not yet"
      }
      default {
         puts "unknown update_oned_mg $what"
      }
   }
}


proc setup_oned_mg {{map 1} {geo ""}} {
   global oned_data env

   set mb_bb_nb [start_mg_top .oned_mg $map "XY Plots:" withdraw]
   if {$mb_bb_nb == ""} {
      if {$geo != ""} {top_geometry .oned_mg $geo}
      return .oned_mg
   }

   OneDWin::init

   set mb [lindex $mb_bb_nb 0]
   menubutton $mb.win -text Windows -underline 0 -menu  $mb.win.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

   menu $mb.win.menu -tearoff 0
   $mb.win.menu add command -label "Create new window" \
              -command "update_oned_mg new_win"
   $mb.win.menu add command -label "Create polar window" \
              -command "update_oned_mg polar_win"
   $mb.win.menu add cascade -label "Select window" -menu $mb.win.menu.menu
     menu $mb.win.menu.menu -postcommand [list text_optionMenu_postcommand \
             $mb.win.menu.menu oned_data(wid) "update_oned_mg list_win" \
              0 "update_oned_mg win"] -tearoff 0

   $mb.win.menu add command -label "Delete window" \
              -command "update_oned_mg del_win"

   $mb.win.menu add command -label "Empty Recycle Pool" \
               -command "OneDWin::modify_plots empty_recycle"

   set edm [menu $mb.edit.menu  -tearoff 0]
   $edm add cascade -label "Set new window's:" -menu $edm.new
      menu $edm.new -tearoff 0
      $edm.new add command -label "Background color" \
              -command "update_oned_mg ed_new background"
      $edm.new add command -label "Width" \
              -command "update_oned_mg ed_new width"
      $edm.new add command -label "Height" \
              -command "update_oned_mg ed_new height"
   $edm add cascade -label "Set current window's:" -menu $edm.win
      menu $edm.win -tearoff 0
      $edm.win add command -label "Background color" \
              -command "update_oned_mg ed_win background"
      $edm.win add command -label "Width" \
              -command "update_oned_mg ed_win width"
      $edm.win add command -label "Height" \
              -command "update_oned_mg ed_win height"
   $edm add command -label "Modify title for X axis" \
              -command "OneDWin::modify_axis title x 1"
   $edm add command -label "Modify title for Y axis" \
              -command "OneDWin::modify_axis title y 1"

   $edm add command -label "Rename current plot" \
              -command "OneDWin::modify_plots rename"
           

   menu $mb.opt.menu  -tearoff 0
   $mb.opt.menu add command -label "Create Axes" \
                -command "OneDWin::create_axis"
   $mb.opt.menu add command -label "Make Polar System" \
                 -command "OneDWin::create_polar"
   $mb.opt.menu add command -label "Create Legend" \
                -command "OneDWin::create_leg"

   menu $mb.tool.menu -tearoff 0

   $mb.tool.menu add cascade -label "Make an xyplot from" \
                     -menu  $mb.tool.menu.mk
      menu $mb.tool.menu.mk -postcommand [list text_optionMenu_postcommand \
             $mb.tool.menu.mk oned_data(vppts_tobe) \
             "OneDWin::update_vppoints list" 0 \
             "OneDWin::update_vppoints xyplot"]

   $mb.tool.menu add command -label "Make an xyplot from data file" \
              -command "OneDWin::create_plots read_xydata"
   $mb.tool.menu add command -label "Save current plot to file" \
              -command "OneDWin::modify_plots save_xydata"
   
   pack $mb.win $mb.edit $mb.opt $mb.tool  -side left

   set bb [lindex $mb_bb_nb 1]
   Button $bb.save  -image [Bitmap::get save] -highlightthickness 0 \
        -takefocus 0 -relief link -borderwidth 1 -padx 1 -pady 1 \
        -helptext "Save current window" -command \
        "update_oned_mg save"
   Button $bb.del -image [Bitmap::get del2] -highlightthickness 0 \
        -takefocus 0 -relief link -borderwidth 1 -padx 1 -pady 1 \
        -helptext "Delete" -command "update_oned_mg delete"
   Button $bb.undel -image [Bitmap::get undo2] -highlightthickness 0 \
        -takefocus 0 -relief link -borderwidth 1 -padx 1 -pady 1 \
        -helptext "Undelete" -command "update_oned_mg undelete"

   if [info exist env(V3P_WITH_DISMISS)] {
      Separator $bb.sep -orient vertical
      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 .oned_mg"

      pack $bb.dismiss $bb.sep $bb.undel $bb.del $bb.save  \
         -anchor e -side right
   } else {
      pack $bb.undel $bb.del $bb.save -anchor e -side right
   }

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

   set xfm [$notebook insert end x -text "X Axis"]
   set oned_data(wf,xaxis) $xfm.fm
   set fwx [form_init_sub $oned_data(wf,xaxis)]
   if {$fwx != ""} {
      form_choiceframe $fwx.x
      OneDWin::axis_choiceframe $fwx.x x X
      form_choiceframe $fwx.p
      OneDWin::axis_choiceframe $fwx.p p Arc

      futil_setup_switch $fwx oned_data(wf,xaxis_opt)
      form_finish
   }
   pack $oned_data(wf,xaxis) -side top  -expand yes -fill both

   set yfm [$notebook insert end y -text "Y Axis"]
   set oned_data(wf,yaxis) $yfm.fm
   set fwy [form_init_sub $oned_data(wf,yaxis)]
   if {$fwy != ""} {
      OneDWin::axis_choiceframe $fwy y Y
      form_finish
   }
   pack $oned_data(wf,yaxis) -side top  -expand yes -fill both

   set gfm [$notebook insert end leg -text "Legend"]
   set oned_data(wf,legend) $gfm.fm
   set fwg [form_init_sub $oned_data(wf,legend)]
   if {$fwg != ""} {
      OneDWin::legend_choiceframe $fwg
      form_finish
   }
   pack $oned_data(wf,legend) -side top  -expand yes -fill both

   set pfm [$notebook insert end plt -text "Plots"]
   set oned_data(wf,plots) $pfm.fm
   set fwp [form_init_sub $oned_data(wf,plots)]
   if {$fwp != ""} {
      OneDWin::plots_choiceframe $fwp
      form_finish
   }
   pack $oned_data(wf,plots) -side top  -expand yes -fill both

   set dfm [$notebook insert end dat -text "Data"]
   set oned_data(wf,data) $dfm.fm
   set fwd [form_init_sub $oned_data(wf,data)]
   if {$fwd != ""} {
      OneDWin::data_choiceframe $fwd
      form_finish
   }
   pack $oned_data(wf,data) -side top  -expand yes -fill both


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

   update idletasks

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

   return .oned_mg
}
