;   SAFE_TVRD.PRO   6-11-97   DSFoster
;
; This function is a safer version of IDL's TVRD() function. First,
; there was a bug related to the reading from a scrollable draw. Also,
; the TVRD() function uses an X routine that has problems if the
; window is obscured or iconified. This routine uses the DEVICE, COPY=
; command to first copy the window contents to a new window pixmap,
; and then reads from this pixmap into the array.
;
; Modifications
;
;  6-11-97 DSF Check validity of draw widget.

FUNCTION safe_tvrd, draw_widget, xsize, ysize

on_error, 2

if (widget_info(draw_widget, /valid_id) eq 0) then begin
	return, -1
endif else if (widget_info(draw_widget, /name) ne 'DRAW') then begin
	return, -1
endif else begin
	old_window = !d.window
	window, xsize=xsize, ysize=ysize, /free, /pixmap     ; Create new window

	widget_control, draw_widget, get_value=window
	device, copy=[0,0, xsize,ysize, 0,0, window]         ; Copy into new window

	image = tvrd()                                       ; Read into array
	wdelete, !d.window
	if (old_window ne -1) then wset, old_window
	return, image
endelse

END

