PRO qImage_cw_DrawCross, state

;+
; NAME:
;	qImage_cw_DrawCross
; PURPOSE:
;	Draw cross section across top and along left edge of main draw widget
; CATEGORY:
;	Compound widget qImage_cw
; CALLING SEQUENCE:
;	qImage_cw_DrawCross, state
; INPUTS:
;	state			array[1]; type: structure
;						qImage_cw state structure
; OPTIONAL INPUT PARAMETERS:
; OUTPUTS:
; OPTIONAL OUTPUT PARAMETERS:
; INCLUDE:
	@compile_opt.pro			; On error, return to caller
; CALLS:
;	qImage_cw_Box, qImage_cw_BoxImage, qImage_cw_MinMax
;	qImage_cw_BoxZoom, MagnifyArray, IsType
; SIDE EFFECTS:
; RESTRICTIONS:
; PROCEDURE:
; STATE INFO USED:
;	widget_control, state.wid_xcross , get_value=window_number
;	widget_control, state.wid_ycross , get_value=window_number
;	widget_control, state.wid_xcross, get_uvalue=x, /no_copy
;	widget_control, state.wid_ycross, get_uvalue=y, /no_copy
;	widget_control, state.wid_tool  , get_uvalue=tool
; STATE INFO MODIFIED:
;	widget_control, state.wid_xcross, set_uvalue=x, /no_copy
;	widget_control, state.wid_ycross, set_uvalue=y, /no_copy
; MODIFICATION HISTORY:
;	FEB-2000, Paul Hick (UCSD/CASS; pphick@ucsd.edu)
;-

qImage_cw_Box, state, box, /get							; Current box definition
screen = qImage_cw_Property(state, /screen_size)
widget_control, state.wid_tool, get_uvalue=tool_state, /no_copy
zoom = tool_state.send.zoom_uval
nbin = tool_state.send.rebin_uval
widget_control, state.wid_tool, set_uvalue=tool_state, /no_copy
parents = qImage_cw_Transform(state, fromscreen=[[0,0],[screen-1]], boxedge=zoom LT 1, /toimage, /parent)

IF widget_info(state.wid_xcross, /valid_id) THEN BEGIN

	; Redraw the cross section at the top

	widget_control, state.wid_xcross, get_value=wnew

	wold = !d.window
	IF wnew NE wold THEN wset, wnew

	erase, color=!p.background

	; In vertical direction, only sum over over height of current box (box[1] to box[3])

	p = [ [parents[0],box[1]], [parents[2], box[3]] ]

	IF qImage_cw_BoxImage(state, p, sub_img=sub_img, sub_box=sub_box) THEN BEGIN

		qImage_cw_BoxZoom, state, sub_img, sub_box, screen, zoom, nbin, /xzoom

		IF total(finite(sub_img)) GT 0 THEN BEGIN
			x = make_array(type=IsType(sub_img),dim=screen[0], value=BadValue(image))
			x[sub_box[0]:sub_box[1]] = sub_img
			x = qImage_cw_MinMax(state, x)
			plot, pos=[0,0,1,1], indgen(screen[0]), x, xstyle=5, ystyle=5, /noerase	; Plot cross section
		ENDIF

	ENDIF

	IF wnew NE wold THEN wset, wold

ENDIF

IF widget_info(state.wid_ycross, /valid_id) THEN BEGIN

	; Plot cross section on left

	widget_control, state.wid_ycross, get_value=wnew

	wold = !d.window
	IF wnew NE wold THEN wset, wnew

	erase, color=!p.background

	; Calculate cross sections to be plotted
	; y is plotted on right; x is plotted across top

	; In horizontal direction, only sum over over width of current box (box[0] to box[2])

	p = [ [box[0],parents[1]], [box[2],parents[3]] ]

	IF qImage_cw_BoxImage(state, p, sub_img=sub_img, sub_box=sub_box) THEN BEGIN

		qImage_cw_BoxZoom, state, sub_img, sub_box, screen, zoom, nbin, /yzoom

		IF total(finite(sub_img)) GT 0 THEN BEGIN
			y = make_array(type=IsType(sub_img),dim=screen[1], value=BadValue(image))
			y[sub_box[0]:sub_box[1]] = sub_img
			y = qImage_cw_MinMax(state, y)
			y = max(y)-y
			plot, pos=[0,0,1,1], y, indgen(screen[1]), xstyle=5, ystyle=5, /noerase
		ENDIF

	ENDIF

	IF wnew NE wold THEN wset, wold

ENDIF

RETURN  &  END
