
set move(lastX) 0
set move(lastY) 0

proc hxB2decN {hnm} {
    set ln [string length $hnm]
    if $ln!=2 then {return "input must be 1 byte"}
    set y1 [binary format H2 $hnm]
    binary scan $y1 c* y2
    set y2 [expr {($y2 + 0x100 ) % 0x100}]
    return $y2
}

proc decN2Hx {dn} {
     if $dn>255 then {return "input must be 1 byte"}
     binary scan [binary format c $dn] H2 hc
     return $hc
}

proc plotMove {w i x y} {
	global move
	global PLOT
	if {$x - $PLOT($i,x) > [expr $PLOT($i,w) / 2]} {
		incr PLOT($i,w) [expr {$x-$move(lastX)}]
		incr PLOT($i,h) [expr {$y-$move(lastY)}]
	} else {
		incr PLOT($i,x) [expr {$x-$move(lastX)}]
		incr PLOT($i,y) [expr {$y-$move(lastY)}]

		.n.f0.frameC.curve move "curve_frame_$i" [expr {$x-$move(lastX)}] [expr {$y-$move(lastY)}]
		.n.f0.frameC.curve move "curve_scale_$i" [expr {$x-$move(lastX)}] [expr {$y-$move(lastY)}]
		.n.f0.frameC.curve move "curve_bg_$i" [expr {$x-$move(lastX)}] [expr {$y-$move(lastY)}]
		.n.f0.frameC.curve move "curve_$i" [expr {$x-$move(lastX)}] [expr {$y-$move(lastY)}]
	}
	set move(lastX) $x
	set move(lastY) $y
	if {$x - $PLOT($i,x) > [expr $PLOT($i,w) / 2]} {
		plot_bg $i
		plot_draw_data $i
	}
}

proc plotSet {x y} {
	global move
	set move(lastX) $x
	set move(lastY) $y
}

proc plotMoveEnd {i} {
	plot_redraw $i
}

proc set_point {i L POINT UPDOWN} {
	global PLOT
	set VAL [expr [lindex $PLOT($i,ydata,$L) $POINT] + $UPDOWN]
	set PLOT($i,ydata,$L) [lreplace $PLOT($i,ydata,$L) $POINT $POINT $VAL]
	eval .frame$L.point_$POINT.label configure -text "[lindex $PLOT($i,ydata,$L) $POINT]"
	plot_draw_data 1
	puts "## $L $POINT $UPDOWN ##"
}

proc plot_draw_data {i} {
	global PLOT
	global tk_library

	.n.f0.frameC.curve delete "curve_$i"
	.n.f0.frameC.curve delete "curve_scale_$i"

	set X_OFF $PLOT($i,x)
	set Y_OFF $PLOT($i,y)
	set W $PLOT($i,w)
	set H $PLOT($i,h)
	set TITLE_X $PLOT($i,title_x)
	set TITLE $PLOT($i,title)
	set PAD_X $PLOT($i,pad_x1)
	set PAD_X2 $PLOT($i,pad_x2)
	set PAD_Y $PLOT($i,pad_y2)
	set PAD_Y2 $PLOT($i,pad_y1)

	set L [expr ($W - $PAD_X - $PAD_X2) / 200.0]
	set VMIN 1000000
	set VMAX -1000000
	foreach POINT_VAL $PLOT($i,udata) {
		if {$VMIN > $POINT_VAL} {
			set VMIN $POINT_VAL
		} elseif {$VMAX < $POINT_VAL} {
			set VMAX $POINT_VAL
		}
	}

	set VMIN [expr $VMIN - 0.0]
	set VMAX [expr $VMAX + 0.0]
	set MAX [expr $VMAX - $VMIN]

	set PM 0
	while {[info exist PLOT($i,ydata,$PM)]} {
		incr PM
	}
	if {$PAD_X2 == -1} {
		set PAD_X2 [expr ($PM - 1) * $PAD_X]
	}
	if {$PAD_Y == -1} {
		set PAD_Y 40
	}

	set L_X $L
	set VMIN_X $VMIN
	set VMAX_X $VMAX
	set MAX_X $MAX



	# Border
	.n.f0.frameC.curve create line [expr $X_OFF + $PAD_X] [expr $Y_OFF + $H - $PAD_Y] [expr $X_OFF + $W - $PAD_X2] [expr $Y_OFF + $H - $PAD_Y] -width 2 -fill "$PLOT($i,hl_color_x)" -tag "curve_scale_$i"
	.n.f0.frameC.curve create line [expr $X_OFF + $W - $PAD_X2] [expr $Y_OFF + $PAD_Y2] [expr $X_OFF + $W - $PAD_X2] [expr $Y_OFF + $H - $PAD_Y] -width 2 -fill "$PLOT($i,hl_color_y)" -tag "curve_scale_$i"
	.n.f0.frameC.curve create line [expr $X_OFF + $PAD_X] [expr $Y_OFF + $PAD_Y2] [expr $X_OFF + $W - $PAD_X2] [expr $Y_OFF + $PAD_Y2] -width 2 -fill "$PLOT($i,hl_color_x)" -tag "curve_scale_$i"
	.n.f0.frameC.curve create line [expr $X_OFF + $PAD_X] [expr $Y_OFF + $PAD_Y2] [expr $X_OFF + $PAD_X] [expr $Y_OFF + $H - $PAD_Y] -width 2 -fill "$PLOT($i,hl_color_y)" -tag "curve_scale_$i"


	# Scale X
	if {$PLOT($i,udata_text) == ""} {
		set XSDATA $PLOT($i,udata)
	} else {
		set XSDATA $PLOT($i,udata_text)
	}
	set NUM 0
	foreach POINT_VAL $XSDATA {
		set X [expr $PAD_X + ([expr $W - $PAD_X - $PAD_X2] * ([lindex $PLOT($i,udata) $NUM] + ($VMIN_X * -1)) / $MAX_X)]
		.n.f0.frameC.curve create line [expr $X_OFF + $X] [expr $Y_OFF + $PAD_Y2] [expr $X_OFF + $X] [expr $Y_OFF + $H - $PAD_Y + 2] -width 1 -fill "$PLOT($i,hl_color_x)" -tag "curve_$i"
#		.n.f0.frameC.curve create text [expr $X_OFF + $X + 1] [expr $Y_OFF + $H - $PAD_Y / 3 * 2 + 1] -text "[expr round($POINT_VAL * 100) / 100.0]" -fill "$PLOT($i,shadow_color)" -anchor c -tag "curve_$i"
#		.n.f0.frameC.curve create text [expr $X_OFF + $X] [expr $Y_OFF + $H - $PAD_Y / 3 * 2] -text "[expr round($POINT_VAL * 100) / 100.0]" -fill "$PLOT($i,title_color)" -anchor c -tag "curve_$i"
		.n.f0.frameC.curve create text [expr $X_OFF + $X + 1] [expr $Y_OFF + $H - $PAD_Y + 5 + 1] -text "[string trim $POINT_VAL " "]" -fill "$PLOT($i,shadow_color)" -anchor n -tag "curve_$i"  -width 1
		.n.f0.frameC.curve create text [expr $X_OFF + $X] [expr $Y_OFF + $H - $PAD_Y + 5] -text "[string trim $POINT_VAL " "]" -fill "$PLOT($i,title_color)" -anchor n -tag "curve_$i"  -width 1
		incr NUM
	}

	set PN 0
	while {[info exist PLOT($i,ydata,$PN)]} {

		if {! [info exist PLOT($i,title_y,$PN)]} {
			set PLOT($i,title_y,$PN) "title-y $PN"
		}
		if {! [info exist PLOT($i,data_color,$PN)]} {
			set PLOT($i,data_color,$PN) "#ABABAB"
		}

		set VMIN 1000000
		set VMAX -1000000

		if {$PLOT($i,ysync) == 1} {
			set PN2 0
			while {[info exist PLOT($i,ydata,$PN2)]} {
				foreach POINT_VAL $PLOT($i,ydata,$PN2) {
					if {$VMIN > $POINT_VAL} {
						set VMIN $POINT_VAL
					} elseif {$VMAX < $POINT_VAL} {
						set VMAX $POINT_VAL
					}
				}
				incr PN2
			}
		} else {
			foreach POINT_VAL $PLOT($i,ydata,$PN) {
				if {$VMIN > $POINT_VAL} {
					set VMIN $POINT_VAL
				} elseif {$VMAX < $POINT_VAL} {
					set VMAX $POINT_VAL
				}
			}
		}
		set VMIN [expr $VMIN - 1.0]
		set VMAX [expr $VMAX + 1.0]
		if {$PLOT($i,min,$PN) != ""} {
			set VMIN $PLOT($i,min,$PN)
		}
		if {$PLOT($i,max,$PN) != ""} {
			set VMAX $PLOT($i,max,$PN)
		}

		set MAX [expr $VMAX - $VMIN]
		set DMAX [llength $PLOT($i,udata)]

		set X $PAD_X
		set Y [expr [expr $H - $PAD_Y - $PAD_Y2] - ([expr $H - $PAD_Y - $PAD_Y2] * ([lindex $PLOT($i,ydata,$PN) 0] + ($VMIN * -1)) / $MAX)]
		set LAST_Y $Y
		set LAST_X $X
		set NUM 0
		foreach POINT_VAL $PLOT($i,ydata,$PN) {
			set Y [expr [expr $H - $PAD_Y - $PAD_Y2] - ([expr $H - $PAD_Y - $PAD_Y2] * ($POINT_VAL + ($VMIN * -1)) / $MAX)]
			set X [expr $PAD_X + ([expr $W - $PAD_X - $PAD_X2] * ([lindex $PLOT($i,udata) $NUM] + ($VMIN_X * -1)) / $MAX_X)]
			if {$PN == 0 && $PLOT($i,fill,$PN) == 1} {
				.n.f0.frameC.curve create polygon \
					[expr $X_OFF + $LAST_X] \
					[expr $Y_OFF + $LAST_Y + $PAD_Y2] \
					[expr $X_OFF + $X] \
					[expr $Y_OFF + $Y + $PAD_Y2] \
					[expr $X_OFF + $X] \
					[expr $Y_OFF + $H - $PAD_Y] \
					[expr $X_OFF + $LAST_X] \
					[expr $Y_OFF + $H - $PAD_Y] \
				-fill "#ABABAB" \
				-tag "curve_$i"
			} elseif {$PLOT($i,fill,$PN) == 1} {
				.n.f0.frameC.curve create polygon \
					[expr $X_OFF + $LAST_X] \
					[expr $Y_OFF + $LAST_Y + $PAD_Y2] \
					[expr $X_OFF + $X] \
					[expr $Y_OFF + $Y + $PAD_Y2] \
					[expr $X_OFF + $X] \
					[expr $Y_OFF + $H - $PAD_Y] \
					[expr $X_OFF + $LAST_X] \
					[expr $Y_OFF + $H - $PAD_Y] \
				-fill "#DEDEDE" \
				-stipple @[file join $tk_library demos images gray25.bmp] \
				-tag "curve_$i"
			}

			if {$PLOT($i,bar,$PN) > 0} {
				if {$PLOT($i,udata_color) == ""} {
					set COLOR $PLOT($i,data_color_fill,$PN)
				} else {
					set COLOR [lindex $PLOT($i,udata_color) $NUM]
				}
				.n.f0.frameC.curve create rectangle [expr $X_OFF + $X + ($PN * 5) - ($PLOT($i,bar,$PN) / 2)] [expr $Y_OFF + $PAD_Y2 + $Y] [expr $X_OFF + $X + ($PN * 5) + ($PLOT($i,bar,$PN) / 2 + 1)] [expr $Y_OFF + $H - $PAD_Y] -width 1 -fill "$COLOR" -outline "$COLOR" -tag "curve_$i"
			} else {
				.n.f0.frameC.curve create line [expr $X_OFF + $LAST_X] [expr $Y_OFF + $LAST_Y + $PAD_Y2] [expr $X_OFF + $X] [expr $Y_OFF + $Y + $PAD_Y2] -width 1 -fill "$PLOT($i,data_color,$PN)" -tag "curve_$i"
#				.n.f0.frameC.curve create line [expr $X_OFF + $LAST_X] [expr $Y_OFF + $LAST_Y + $PAD_Y2] [expr $X_OFF + $X] [expr $Y_OFF + $Y + $PAD_Y2] -width 4 -fill "#005500" -tag "curve_$i"
#				.n.f0.frameC.curve create line [expr $X_OFF + $LAST_X] [expr $Y_OFF + $LAST_Y + $PAD_Y2] [expr $X_OFF + $X] [expr $Y_OFF + $Y + $PAD_Y2] -width 1 -fill "#00FF00" -tag "curve_$i"
			}


			set LAST_X $X
			set LAST_Y $Y
			incr NUM
		}

		set X $PAD_X
		set Y [expr [expr $H - $PAD_Y - $PAD_Y2] - ([expr $H - $PAD_Y - $PAD_Y2] * ([lindex $PLOT($i,ydata,$PN) 0] + ($VMIN * -1)) / $MAX)]
		set LAST_Y $Y
		set LAST_X $X
		set NUM 0
		foreach POINT_VAL $PLOT($i,ydata,$PN) {
			set Y [expr [expr $H - $PAD_Y - $PAD_Y2] - ([expr $H - $PAD_Y - $PAD_Y2] * ($POINT_VAL + ($VMIN * -1)) / $MAX)]
			set X [expr $PAD_X + ([expr $W - $PAD_X - $PAD_X2] * ([lindex $PLOT($i,udata) $NUM] + ($VMIN_X * -1)) / $MAX_X)]
			if {$NUM > 0} {
				if {$PLOT($i,point,$PN) == 1} {
					.n.f0.frameC.curve create text [expr $X_OFF + $X - 1] [expr $Y_OFF + $Y + $PAD_Y2 - 1] -text "[lindex $PLOT($i,udata) $NUM].[expr round($POINT_VAL * 100) / 100.0]" -fill "$PLOT($i,shadow_color)" -anchor s -tag "curve_scale_$i"
					.n.f0.frameC.curve create text [expr $X_OFF + $X + 1] [expr $Y_OFF + $Y + $PAD_Y2 + 1] -text "[lindex $PLOT($i,udata) $NUM].[expr round($POINT_VAL * 100) / 100.0]" -fill "$PLOT($i,shadow_color)" -anchor s -tag "curve_scale_$i"
					.n.f0.frameC.curve create text [expr $X_OFF + $X - 1] [expr $Y_OFF + $Y + $PAD_Y2 - 1] -text "[lindex $PLOT($i,udata) $NUM].[expr round($POINT_VAL * 100) / 100.0]" -fill "$PLOT($i,shadow_color)" -anchor s -tag "curve_scale_$i"
					.n.f0.frameC.curve create text [expr $X_OFF + $X + 1] [expr $Y_OFF + $Y + $PAD_Y2 + 1] -text "[lindex $PLOT($i,udata) $NUM].[expr round($POINT_VAL * 100) / 100.0]" -fill "$PLOT($i,shadow_color)" -anchor s -tag "curve_scale_$i"
					.n.f0.frameC.curve create text [expr $X_OFF + $X] [expr $Y_OFF + $Y + $PAD_Y2] -text "[lindex $PLOT($i,udata) $NUM].[expr round($POINT_VAL * 100) / 100.0]" -fill "$PLOT($i,data_color,$PN)" -anchor s -tag "curve_scale_$i"
				}

				#.n.f0.frameC.curve delete "curve_scale-a_$i-$PN-$NUM"
				#.n.f0.frameC.curve delete "curve_scale-b_$i-$PN-$NUM"
				#.n.f0.frameC.curve create rectangle [expr $X_OFF + $X - 5] [expr $Y_OFF + $Y + $PAD_Y2 - 10] [expr $X_OFF + $X + 5] [expr $Y_OFF + $Y + $PAD_Y2] -fill "$PLOT($i,data_color,$PN)" -tag "curve_scale-a_$i-$PN-$NUM"
				#.n.f0.frameC.curve create rectangle [expr $X_OFF + $X - 5] [expr $Y_OFF + $Y + $PAD_Y2] [expr $X_OFF + $X + 5] [expr $Y_OFF + $Y + $PAD_Y2 + 10] -fill "$PLOT($i,data_color,$PN)" -tag "curve_scale-b_$i-$PN-$NUM"
				#eval .n.f0.frameC.curve bind "curve_scale-a_$i-$PN-$NUM" <ButtonPress-1> \{set_point $i $PN $NUM 1\}
				#eval .n.f0.frameC.curve bind "curve_scale-b_$i-$PN-$NUM" <ButtonPress-1> \{set_point $i $PN $NUM -1\}
			}
			set LAST_X $X
			set LAST_Y $Y
			incr NUM
		}

		# Scale Y
		set L [expr ($H - $PAD_Y - $PAD_Y2) / 25.0]
		for {set POINT_VAL $VMIN} {$POINT_VAL <= $VMAX} {set POINT_VAL [expr $POINT_VAL + ($MAX / $L)]} {
			set Y [expr [expr $H - $PAD_Y - $PAD_Y2] - ([expr $H - $PAD_Y - $PAD_Y2] * ($POINT_VAL + ($VMIN * -1)) / $MAX)]
			if {$PN == 0} {
				if {$PLOT($i,ysync) == 1} {
					.n.f0.frameC.curve create line [expr $X_OFF + $PAD_X - 5] [expr $Y_OFF + $Y + $PAD_Y2] [expr $X_OFF + $W - $PAD_X2] [expr $Y_OFF + $Y + $PAD_Y2] -width 1 -fill "$PLOT($i,hl_color_y)" -tag "curve_scale_$i"
				} else {
					.n.f0.frameC.curve create line [expr $X_OFF + $PAD_X - 5] [expr $Y_OFF + $Y + $PAD_Y2] [expr $X_OFF + $PAD_X] [expr $Y_OFF + $Y + $PAD_Y2] -width 1 -fill "$PLOT($i,data_color,$PN)" -tag "curve_scale_$i"
				}
				.n.f0.frameC.curve create text [expr $X_OFF + $PAD_X / 3 * 2 + 1] [expr $Y_OFF + $Y + $PAD_Y2 + 1] -text "[expr round($POINT_VAL * 100) / 100]" -fill "$PLOT($i,shadow_color)" -anchor c -tag "curve_scale_$i"
				.n.f0.frameC.curve create text [expr $X_OFF + $PAD_X / 3 * 2] [expr $Y_OFF + $Y + $PAD_Y2] -text "[expr round($POINT_VAL * 100) / 100]" -fill "$PLOT($i,data_color,$PN)" -anchor c -tag "curve_scale_$i"
			} else {
				.n.f0.frameC.curve create line [expr $X_OFF + $W - $PAD_X2 + $PAD_X2 / (($PM - 1) * 3) * (($PN - 1) * 3 + 0)] [expr $Y_OFF + $Y + $PAD_Y2] [expr $X_OFF + $W - $PAD_X2 + $PAD_X2 / (($PM - 1) * 3) * (($PN - 1) * 3 + 0) + 5] [expr $Y_OFF + $Y + $PAD_Y2] -width 1 -fill "$PLOT($i,data_color,$PN)" -tag "curve_scale_$i"
				.n.f0.frameC.curve create text [expr $X_OFF + $W - $PAD_X2 + $PAD_X2 / (($PM - 1) * 3) * (($PN - 1) * 3 + 1) + 1] [expr $Y_OFF + $Y + $PAD_Y2 + 1] -text "[expr round($POINT_VAL * 100) / 100.0]" -fill "$PLOT($i,shadow_color)" -anchor c -tag "curve_scale_$i"
				.n.f0.frameC.curve create text [expr $X_OFF + $W - $PAD_X2 + $PAD_X2 / (($PM - 1) * 3) * (($PN - 1) * 3 + 1)] [expr $Y_OFF + $Y + $PAD_Y2] -text "[expr round($POINT_VAL * 100) / 100.0]" -fill "$PLOT($i,data_color,$PN)" -anchor c -tag "curve_scale_$i"
			}
		}


		destroy .n.f0.frameC.curve.sync2-$i-$PN
		eval checkbutton .n.f0.frameC.curve.sync2-$i-$PN -text \"\" -variable wipers -relief flat -variable PLOT($i,point,$PN) -command \{ \
			plot_redraw $i \
		\}

		destroy .n.f0.frameC.curve.sync3-$i-$PN
		eval checkbutton .n.f0.frameC.curve.sync3-$i-$PN -text \"\" -variable wipers -relief flat -variable PLOT($i,fill,$PN) -command \{ \
			plot_redraw $i \
		\}

		destroy .n.f0.frameC.curve.sync4-$i-$PN
		eval checkbutton .n.f0.frameC.curve.sync4-$i-$PN -text \"\" -variable wipers -relief flat -variable PLOT($i,bar,$PN) -command \{ \
			plot_redraw $i \
		\}

		if {$PN == 0} {
			.n.f0.frameC.curve create text [expr $X_OFF + $PAD_X / 3 + 1] [expr $Y_OFF + ($H - $PAD_Y - $PAD_Y2) / 2 + $PAD_Y2 + 1] -width 1 -text "$PLOT($i,title_y,$PN)" -fill "$PLOT($i,shadow_color)" -anchor c -justify center -tag "curve_scale_$i"
			.n.f0.frameC.curve create text [expr $X_OFF + $PAD_X / 3] [expr $Y_OFF + ($H - $PAD_Y - $PAD_Y2) / 2 + $PAD_Y2] -width 1 -text "$PLOT($i,title_y,$PN)" -fill "$PLOT($i,data_color,$PN)" -anchor c -justify center -tag "curve_scale_$i"

#			.n.f0.frameC.curve create window [expr $X_OFF + $PAD_X / 3] [expr $Y_OFF + $H - 5] -anchor s -window .n.f0.frameC.curve.sync2-$i-$PN -tag "curve_bg_$i-$PN"
#			.n.f0.frameC.curve create window [expr $X_OFF + $PAD_X / 3 + 15] [expr $Y_OFF + $H - 5] -anchor s -window .n.f0.frameC.curve.sync3-$i-$PN -tag "curve_bg_$i-$PN"
#			.n.f0.frameC.curve create window [expr $X_OFF + $PAD_X / 3 + 30] [expr $Y_OFF + $H - 5] -anchor s -window .n.f0.frameC.curve.sync4-$i-$PN -tag "curve_bg_$i-$PN"
		} else {
			.n.f0.frameC.curve create line [expr $X_OFF + $W - $PAD_X2 + $PAD_X2 / (($PM - 1) * 3) * (($PN - 1) * 3 + 0) + 1] [expr $Y_OFF + $PAD_Y2] [expr $X_OFF + $W - $PAD_X2 + $PAD_X2 / (($PM - 1) * 3) * (($PN - 1) * 3 + 0) + 1] [expr $Y_OFF + $H - $PAD_Y] -width 1 -fill "$PLOT($i,data_color,$PN)" -tag "curve_scale_$i"
			.n.f0.frameC.curve create text [expr $X_OFF + $W - $PAD_X2 + $PAD_X2 / (($PM - 1) * 3) * (($PN - 1) * 3 + 2) + 1] [expr $Y_OFF + ($H - $PAD_Y - $PAD_Y2) / 2 + $PAD_Y2 + 1] -width 1 -text "$PLOT($i,title_y,$PN)" -fill "$PLOT($i,shadow_color)" -anchor c -justify center -tag "curve_scale_$i"
			.n.f0.frameC.curve create text [expr $X_OFF + $W - $PAD_X2 + $PAD_X2 / (($PM - 1) * 3) * (($PN - 1) * 3 + 2)] [expr $Y_OFF + ($H - $PAD_Y - $PAD_Y2) / 2 + $PAD_Y2] -width 1 -text "$PLOT($i,title_y,$PN)" -fill "$PLOT($i,data_color,$PN)" -anchor c -justify center -tag "curve_scale_$i"

#			.n.f0.frameC.curve create window [expr $X_OFF + $W - $PAD_X2 + $PAD_X2 / (($PM - 1) * 3) * (($PN - 1) * 3 + 1) + 0] [expr $Y_OFF + $H - 5] -anchor s -window .n.f0.frameC.curve.sync2-$i-$PN -tag "curve_bg_$i-$PN"
#			.n.f0.frameC.curve create window [expr $X_OFF + $W - $PAD_X2 + $PAD_X2 / (($PM - 1) * 3) * (($PN - 1) * 3 + 1) + 15] [expr $Y_OFF + $H - 5] -anchor s -window .n.f0.frameC.curve.sync3-$i-$PN -tag "curve_bg_$i-$PN"
#			.n.f0.frameC.curve create window [expr $X_OFF + $W - $PAD_X2 + $PAD_X2 / (($PM - 1) * 3) * (($PN - 1) * 3 + 1) + 30] [expr $Y_OFF + $H - 5] -anchor s -window .n.f0.frameC.curve.sync4-$i-$PN -tag "curve_bg_$i-$PN"
		}
		incr PN
	}
}

proc plot_bg {i} {
	global PLOT
	set X_OFF $PLOT($i,x)
	set Y_OFF $PLOT($i,y)
	set W $PLOT($i,w)
	set H $PLOT($i,h)
	set PAD_X $PLOT($i,pad_x1)
	set PAD_X2 $PLOT($i,pad_x2)
	set PAD_Y $PLOT($i,pad_y2)
	set TITLE_X $PLOT($i,title_x)
	set TITLE $PLOT($i,title)
	set PAD_X $PLOT($i,pad_x1)
	set PAD_X2 $PLOT($i,pad_x2)
	set PAD_Y $PLOT($i,pad_y2)
	set PAD_Y2 $PLOT($i,pad_y1)

	.n.f0.frameC.curve delete "curve_bg_$i"

	set PM 0
	while {[info exist PLOT($i,ydata,$PM)]} {
		incr PM
	}
	if {$PAD_X2 == -1} {
		set PAD_X2 [expr ($PM - 1) * $PAD_X]
	}
	if {$PAD_Y == -1} {
		set PAD_Y 40
	}

	.n.f0.frameC.curve create rectangle $X_OFF $Y_OFF [expr $X_OFF + $W] [expr $Y_OFF + $H] -width 1 -fill "$PLOT($i,frame_color)" -tag "curve_frame_$i"
	.n.f0.frameC.curve create rectangle [expr $X_OFF + $PAD_X] [expr $Y_OFF + $PAD_Y2] [expr $X_OFF + $W - $PAD_X2] [expr $Y_OFF + $H - $PAD_Y] -width 0 -fill "$PLOT($i,bg_color)" -tag "curve_bg_$i"


	for {set SY 0} {$SY < $H} {incr SY} {
		set R 0
		set G 0
		set B [expr $SY * 40 / $H]
		.n.f0.frameC.curve create line $X_OFF [expr $SY + $Y_OFF] [expr $X_OFF + $W] [expr $SY + $Y_OFF] -width 1 -fill "#[decN2Hx $R][decN2Hx $G][decN2Hx $B]" -tag "curve_frame_$i"
	}


	.n.f0.frameC.curve create text [expr $X_OFF + $W / 2 + 1] [expr $Y_OFF + $PAD_Y2 / 2 + 1] -text "$TITLE" -fill "$PLOT($i,title_bg_color)" -anchor c -justify center -tag "curve_bg_$i" -font "12x24"
	.n.f0.frameC.curve create text [expr $X_OFF + $W / 2] [expr $Y_OFF + $PAD_Y2 / 2] -text "$TITLE" -fill "$PLOT($i,title_color)" -anchor c -justify center -tag "curve_bg_$i" -font "12x24"

	.n.f0.frameC.curve create line $X_OFF [expr $Y_OFF + $H] [expr $X_OFF + $W] [expr $Y_OFF + $H] -width 2 -fill "#ABABAB" -tag "curve_bg_$i"
	.n.f0.frameC.curve create line [expr $X_OFF + $W] $Y_OFF [expr $X_OFF + $W] [expr $Y_OFF + $H] -width 2 -fill "#ABABAB" -tag "curve_bg_$i"
	.n.f0.frameC.curve create line $X_OFF $Y_OFF [expr $X_OFF + $W] $Y_OFF -width 2 -fill "#EFEFEF" -tag "curve_bg_$i"
	.n.f0.frameC.curve create line $X_OFF $Y_OFF $X_OFF [expr $Y_OFF + $H] -width 2 -fill "#EFEFEF" -tag "curve_bg_$i"

	destroy .n.f0.frameC.curve.sync-$i
	eval checkbutton .n.f0.frameC.curve.sync-$i -text \"Sync-Y\" -variable wipers -relief flat -variable PLOT($i,ysync) -command \{ \
		plot_redraw $i\
	\}
#	.n.f0.frameC.curve create window [expr $X_OFF + 10] [expr $Y_OFF + 10] -anchor nw -window .n.f0.frameC.curve.sync-$i -tag "curve_bg_$i"

#	destroy .n.f0.frameC.curve.padx-$i
#	spinbox .n.f0.frameC.curve.padx-$i -from 0 -to 300 -increment 5 -format %05.2f -width 10 -textvariable PLOT($i,pad_x1) -command {
#		plot_redraw $i\
#	}
#	.n.f0.frameC.curve create window [expr $X_OFF + 110] [expr $Y_OFF + 10] -anchor nw -window .n.f0.frameC.curve.padx-$i -tag "curve_bg_$i"
#	eval bind .n.f0.frameC.curve.padx-$i <ButtonPress-4> \{set PLOT($i,pad_x1) \[expr \$PLOT($i,pad_x1) + 1\]\nplot_bg $i\nplot_draw_data $i\}
#	eval bind .n.f0.frameC.curve.padx-$i <ButtonPress-5> \{set PLOT($i,pad_x1) \[expr \$PLOT($i,pad_x1) - 1\]\nplot_bg $i\nplot_draw_data $i\}

	.n.f0.frameC.curve bind curve_frame_$i <B1-Motion> "plotMove .n.f0.frameC.curve $i %x %y"
	.n.f0.frameC.curve bind curve_frame_$i <ButtonPress-1> "plotSet %x %y"
	.n.f0.frameC.curve bind curve_frame_$i <ButtonRelease-1> "plotMoveEnd $i"
}

proc plot_redraw {i} {
	.n.f0.frameC.curve delete curve_frame_$i
	plot_bg $i
	plot_draw_data $i
}

proc plot_add {NUM DATA} {
	global PLOT
	set PN 0
	while {[info exist PLOT($NUM,ydata,$PN)]} {
		incr PN
	}
	set PLOT($NUM,ydata,$PN) $DATA
	set PLOT($NUM,title_y,$PN) "title_y0"
	if {$PN == 1} {
		set PLOT($NUM,data_color,$PN) "#00FF00"
		set PLOT($NUM,data_color_fill,$PN) "#00FFF0"
	} elseif {$PN == 2} {
		set PLOT($NUM,data_color,$PN) "#0000FF"
		set PLOT($NUM,data_color_fill,$PN) "#000FFF"
	} else {
		set PLOT($NUM,data_color,$PN) "#FFFF00"
		set PLOT($NUM,data_color_fill,$PN) "#FFF000"
	}

	set PLOT($NUM,point,$PN) "0"
	set PLOT($NUM,fill,$PN) "0"
	set PLOT($NUM,bar,$PN) "0"
	set PLOT($NUM,min,$PN) ""
	set PLOT($NUM,max,$PN) ""
}

proc plot_new {X Y W H UDATA DATA} {
	global PLOT
	set NUM 1
	while {[info exist PLOT($NUM,x)]} {
		incr NUM
	}
	set PLOT($NUM,udata) $UDATA
	set PLOT($NUM,udata_text) ""
	set PLOT($NUM,udata_color) ""
	set PLOT($NUM,x) $X
	set PLOT($NUM,y) $Y
	set PLOT($NUM,w) $W
	set PLOT($NUM,h) $H
	set PLOT($NUM,pad_x1) 70
	set PLOT($NUM,pad_x2) -1
	set PLOT($NUM,pad_y1) 70
	set PLOT($NUM,pad_y2) 70
	set PLOT($NUM,title_x) "title_x"
	set PLOT($NUM,title) "title"

	set PLOT($NUM,shadow_color) "#676767"

	set PLOT($NUM,ydata,0) $DATA
	set PLOT($NUM,title_y,0) "title_y0"
	set PLOT($NUM,data_color,0) "#FF0000"
	set PLOT($NUM,data_color_fill,0) "#FFF000"
	set PLOT($NUM,point,0) "0"
	set PLOT($NUM,fill,0) "0"
	set PLOT($NUM,bar,0) "0"
	set PLOT($NUM,min,0) ""
	set PLOT($NUM,max,0) ""

#	set PLOT($NUM,frame_color) "#BCBCBC"
	set PLOT($NUM,frame_color) "#000000"

#	set PLOT($NUM,bg_color) "#343434"
	set PLOT($NUM,bg_color) "#000000"

	set PLOT($NUM,title_color) "#FFFFFF"
	set PLOT($NUM,title_bg_color) "#000000"
	set PLOT($NUM,hl_color_x) "#440000"
	set PLOT($NUM,hl_color_y) "#440000"

	set PLOT($NUM,ysync) "1"

	return $NUM
}

proc plot_configure {NUM NAME VALUE} {
	global PLOT
	set PLOT($NUM,$NAME) $VALUE
}


















proc LabelFrame:create {w args} {
  frame $w -bd 0
  label $w.l
  frame $w.f -bd 2 -relief groove
  frame $w.f.f
  pack $w.f.f
  set text {}
  set font {}
  set padx 3
  set pady 7
  set ipadx 2
  set ipady 9
  foreach {tag value} $args {
    switch -- $tag {
      -font  {set font $value}
      -text  {set text $value}
      -padx  {set padx $value}
      -pady  {set pady $value}
      -ipadx {set ipadx $value}
      -ipady {set ipady $value}
      -bd     {$w.f config -bd $value}
      -relief {$w.f config -relief $value}
    }
  }
  if {"$font"!=""} {
    $w.l config -font $font
  }
  $w.l config -text $text
  pack $w.f -padx $padx -pady $pady -fill both -expand 1
  place $w.l -x [expr $padx+10] -y $pady -anchor w
  pack $w.f.f -padx $ipadx -pady $ipady -fill both -expand 1
  raise $w.l
  return $w.f.f
}

#################################
# The following code implements an example of using the
# labeled frame widget.
#
#set f1 [LabelFrame:create .f1 -text "Frame One"]
#label $f1.l1 -text "The interior\nof Frame One" -bg skyblue
#pack $f1.l1 -fill both -expand 1
#pack .f1 -side right -fill both -expand 1 -padx 5 -pady 5
#
#set f2 [LabelFrame:create .f2 -text "Frame Two" -font fixed]
#label $f2.l -text "This frame has a\nfixed pitch font\non its label"
#pack $f2.l -fill both -expand 1
#pack .f2 -side bottom -fill both -expand 1 -padx 5 -pady 5
#
#set f3 [LabelFrame:create .f3 -text "Buttons" -relief ridge]
#button $f3.b -text OK -command exit
#button $f3.c -text Cancel -command exit
#pack $f3.b $f3.c -side left
#pack .f3 -side top -fill both -expand 1 -padx 5 -pady 5

###################################################################################################

proc Notebook:create {w args} {
  global Notebook
  set Notebook($w,width) 400
  set Notebook($w,height) 300
  set Notebook($w,pages) {}
  set Notebook($w,top) 0
  set Notebook($w,pad) 5
  set Notebook($w,fg,on) black
  set Notebook($w,fg,off) grey50
  canvas $w -bd 0 -highlightthickness 0 -takefocus 0
  set Notebook($w,bg) [$w cget -bg]
  bind $w <1> "Notebook:click $w %x %y"
  bind $w <Configure> "Notebook:scheduleExpand $w"
  eval Notebook:config $w $args
}

#
# Change configuration options for the notebook widget
#
proc Notebook:config {w args} {
  global Notebook
  foreach {tag value} $args {
    switch -- $tag {
      -width {
        set Notebook($w,width) $value
      }
      -height {
        set Notebook($w,height) $value
      }
      -pages {
        set Notebook($w,pages) $value
      }
      -pad {
        set Notebook($w,pad) $value
      }
      -bg {
        set Notebook($w,bg) $value
      }
      -fg {
        set Notebook($w,fg,on) $value
      }
      -disabledforeground {
        set Notebook($w,fg,off) $value
      }
    }
  }

  #
  # After getting new configuration values, reconstruct the widget
  #
  $w delete all
  set Notebook($w,x1) $Notebook($w,pad)
  set Notebook($w,x2) [expr $Notebook($w,x1)+2]
  set Notebook($w,x3) [expr $Notebook($w,x2)+$Notebook($w,width)]
  set Notebook($w,x4) [expr $Notebook($w,x3)+2]
  set Notebook($w,y1) [expr $Notebook($w,pad)+2]
  set Notebook($w,y2) [expr $Notebook($w,y1)+2]
  set Notebook($w,y5) [expr $Notebook($w,y1)+30]
  set Notebook($w,y6) [expr $Notebook($w,y5)+2]
  set Notebook($w,y3) [expr $Notebook($w,y6)+$Notebook($w,height)]
  set Notebook($w,y4) [expr $Notebook($w,y3)+2]
  set x $Notebook($w,x1)
  set cnt 0
  set y7 [expr $Notebook($w,y1)+10]
  foreach p $Notebook($w,pages) {
    set Notebook($w,p$cnt,x5) $x
    set id [$w create text 0 0 -text $p -anchor nw -tags "p$cnt t$cnt"]
    set bbox [$w bbox $id]
    set width [lindex $bbox 2]
    $w move $id [expr $x+10] $y7
    $w create line \
       $x $Notebook($w,y5)\
       $x $Notebook($w,y2) \
       [expr $x+2] $Notebook($w,y1) \
       [expr $x+$width+16] $Notebook($w,y1) \
       -width 2 -fill white -tags p$cnt
    $w create line \
       [expr $x+$width+16] $Notebook($w,y1) \
       [expr $x+$width+18] $Notebook($w,y2) \
       [expr $x+$width+18] $Notebook($w,y5) \
       -width 2 -fill black -tags p$cnt
    set x [expr $x+$width+20]
    set Notebook($w,p$cnt,x6) [expr $x-2]
    if {![winfo exists $w.f$cnt]} {
      frame $w.f$cnt -bd 0
    }
    $w.f$cnt config -bg $Notebook($w,bg)
    place $w.f$cnt -x $Notebook($w,x2) -y $Notebook($w,y6) \
      -width $Notebook($w,width) -height $Notebook($w,height)
    incr cnt
  }
  $w create line \
     $Notebook($w,x1) [expr $Notebook($w,y5)-2] \
     $Notebook($w,x1) $Notebook($w,y3) \
     -width 2 -fill white
  $w create line \
     $Notebook($w,x1) $Notebook($w,y3) \
     $Notebook($w,x2) $Notebook($w,y4) \
     $Notebook($w,x3) $Notebook($w,y4) \
     $Notebook($w,x4) $Notebook($w,y3) \
     $Notebook($w,x4) $Notebook($w,y6) \
     $Notebook($w,x3) $Notebook($w,y5) \
     -width 2 -fill black
  $w config -width [expr $Notebook($w,x4)+$Notebook($w,pad)] \
            -height [expr $Notebook($w,y4)+$Notebook($w,pad)] \
            -bg $Notebook($w,bg)
  set top $Notebook($w,top)
  set Notebook($w,top) -1
  Notebook:raise.page $w $top
}

#
# This routine is called whenever the mouse-button is pressed over
# the notebook.  It determines if any page should be raised and raises
# that page.
#
proc Notebook:click {w x y} {
  global Notebook
  if {$y<$Notebook($w,y1) || $y>$Notebook($w,y6)} return
  set N [llength $Notebook($w,pages)]
  for {set i 0} {$i<$N} {incr i} {
    if {$x>=$Notebook($w,p$i,x5) && $x<=$Notebook($w,p$i,x6)} {
      Notebook:raise.page $w $i
      break
    }
  }
}

#
# For internal use only.  This procedure raised the n-th page of
# the notebook
#
proc Notebook:raise.page {w n} {
  global Notebook
  if {$n<0 || $n>=[llength $Notebook($w,pages)]} return
  set top $Notebook($w,top)
  if {$top>=0 && $top<[llength $Notebook($w,pages)]} {
    $w move p$top 0 2
  }
  $w move p$n 0 -2
  $w delete topline
  if {$n>0} {
    $w create line \
       $Notebook($w,x1) $Notebook($w,y6) \
       $Notebook($w,x2) $Notebook($w,y5) \
       $Notebook($w,p$n,x5) $Notebook($w,y5) \
       $Notebook($w,p$n,x5) [expr $Notebook($w,y5)-2] \
       -width 2 -fill white -tags topline
  }
  $w create line \
    $Notebook($w,p$n,x6) [expr $Notebook($w,y5)-2] \
    $Notebook($w,p$n,x6) $Notebook($w,y5) \
    -width 2 -fill white -tags topline
  $w create line \
    $Notebook($w,p$n,x6) $Notebook($w,y5) \
    $Notebook($w,x3) $Notebook($w,y5) \
    -width 2 -fill white -tags topline
  set Notebook($w,top) $n
  raise $w.f$n
}

#
# Change the page-specific configuration options for the notebook
#
proc Notebook:pageconfig {w name args} {
  global Notebook
  set i [lsearch $Notebook($w,pages) $name]
  if {$i<0} return
  foreach {tag value} $args {
    switch -- $tag {
      -state {
        if {"$value"=="disabled"} {
          $w itemconfig t$i -fg $Notebook($w,fg,off)
        } else {
          $w itemconfig t$i -fg $Notebook($w,fg,on)
        }
      }
      -onexit {
        set Notebook($w,p$i,onexit) $value
      }
    }
  }
}

#
# This procedure raises a notebook page given its name.  But first
# we check the "onexit" procedure for the current page (if any) and
# if it returns false, we don't allow the raise to proceed.
#
proc Notebook:raise {w name} {
  global Notebook
  set i [lsearch $Notebook($w,pages) $name]
  if {$i<0} return
  if {[info exists Notebook($w,p$i,onexit)]} {
    set onexit $Notebook($w,p$i,onexit)
    if {"$onexit"!="" && [eval uplevel #0 $onexit]!=0} {
      Notebook:raise.page $w $i
    }
  } else {
    Notebook:raise.page $w $i
  }
}

#
# Return the frame associated with a given page of the notebook.
#
proc Notebook:frame {w name} {
  global Notebook
  set i [lsearch $Notebook($w,pages) $name]
  if {$i>=0} {
    return $w.f$i
  } else {
    return {}
  }
}

#
# Try to resize the notebook to the next time we become idle.
#
proc Notebook:scheduleExpand w {
  global Notebook
  if {[info exists Notebook($w,expand)]} return
  set Notebook($w,expand) 1
  after idle "Notebook:expand $w"
}

#
# Resize the notebook to fit inside its containing widget.
#
proc Notebook:expand w {
  global Notebook
  set wi [expr [winfo width $w]-($Notebook($w,pad)*2+4)]
  set hi [expr [winfo height $w]-($Notebook($w,pad)*2+36)]
  Notebook:config $w -width $wi -height $hi
  catch {unset Notebook($w,expand)}
}


