;+
;
; NAME: 
;       BACKG_MARK
;
; PURPOSE:
;
; Prompt operator to mark first background interval. Next, prompt to mark
; second background interval or to type 'Q' to finish with only one interval
; specified.
;
;
; CATEGORY:
;	BATSE
;
; CALLING SEQUENCE:
;	backg_mark,b_start,b_end,bnum,abort
;
; CALLS:
;	none
;
; INPUTS:
;       none explicit, only through commons;
;
; OPTIONAL INPUTS:
;	none
;
; OUTPUTS:
;       b_start: start times of 1 or 2 background intervals
;	b_end: end times of 1 or 2 background intervals
;	bnum: 1 or 2, number of background intervals selected
;	abort: if set, then no background is selected
;
; OPTIONAL OUTPUTS:
;	none
;
; KEYWORDS:
;	none
; COMMON BLOCKS:
;	none
;
; SIDE EFFECTS:
;	none
;
; RESTRICTIONS:
;	none
;
; PROCEDURE:
;	none
;
; MODIFICATION HISTORY:
; Shelby Kennard                                  1Mar1991
; Version 2, RAS, 8 Aug 1996 to work with X windows cursors!
;-
pro backg_mark,b_start,b_end,bnum,abort

xi = !x.s
l = !x.window(0)
r = !x.window(1)
b = !y.window(0)
t = !y.window(1)
b_start = dblarr(2)
b_end = dblarr(2)

if !d.name ne 'X' then begin
;
;******************************************************************************
;
abort = 0
err_test = [27,27]
err_q = [81,113]
;
;
for j=0,1 do begin
  start:
  cursor,x1,y1
;
; Make sure key pressed was a valid letter.
;
  if !err lt 65 or !err gt 122 then begin
    goto,start
  endif
;
; Determine if 'Q' was press. If so, abort.
;
  if !err eq err_q(0) or !err eq err_q(1) then begin
    abort = 1
    goto,kill
  endif
;
; If on second background and if 'F' was pressed, get out.
;
  if !err eq err_test(0) or !err eq err_test(1) then goto,finish
;
  if j eq 1 and x1 le b_end(0) then begin
    xyouts,l+.02,t-.09,'Second background can not start ealier or within'+$
    ' first background, try again.',/normal
    goto,start
  endif
;
  vert_line,x1,2
;
; Label start line with 'B' character.
;
  nx1 = xi(0) + xi(1)*x1
  nx1lab = nx1 + .0081
  ylab = t - .03
  xyouts,nx1lab,ylab,'B',/normal
;
  restart:
  cursor,x2,y2
;
; Make sure key pressed was a valid letter.
;
  if !err lt 65 or !err gt 122 then begin
    goto,restart
  endif
;
; Determine if 'Q' was press. If so, abort.
;
  if !err eq err_q(0) or !err eq err_q(1) then begin
    abort = 1
    goto,kill
  endif
;
  if x1 ge x2 then begin
    xyouts,l+.02,t-.12,'Background end can not be ealier or equal to start,'+$
    ' try again.',/normal
    goto,restart
  endif
  vert_line,x2,3
;
  if j eq 0 then begin
    err_test = [70,102]
  endif
;
  b_start(j) = x1
  b_end(j) = x2
  bnum = j + 1
;
endfor
;
finish:
;
kill:
;
endif else begin
;
;******************************************************************************
;	X WINDOWS BACKGROUND SELECTION
xstart:
buttons = ['Select in Plot Window','Quit without Selection']
val_buttons=['X','T']
abort = 0

point, b1, yblabel, /drawline, /widget, linesty=2, npoints=2, buttons=buttons, $
	val_buttons=val_buttons, newlabel='Select 1st Background Interval'
b_start(0) = min(b1)
b_end(0)   = max(b1)
bnum=1
;
; Determine if 'Quit' was pressed. If so, abort.
;
if datatype(b1) eq 'STR' then $
if b1(0) eq 'null' then begin
    abort = 1
    goto,xkill
  endif
;
; Label start line with 'B' character.
;
xyouts,b_start(0),yblabel,'B'
;
;
; If on second background and if 'F' was pressed, get out.
;
point, b1, yblabel, /drawline, /widget, thick=2, linesty=2, npoints=2, buttons=buttons, $
	val_buttons=val_buttons, newlabel='Select 2nd Background Interval',/continue

;
; Determine if 'Quit' was pressed. If so, abort.
;
if datatype(b1) eq 'STR' then $
if b1(0) eq 'null' then begin
    abort = 1
    goto,xkill
  endif


if b1(1) le b_end(0) then begin
    xyouts,l+.02,t-.09,'Second background can not start earlier or within'+$
    ' first background, try again.',/normal

    goto,xstart
  endif
;
;
; Label start line with 'B' character.
;
xyouts,b_start(0),yblabel,'B'

b_start(1) = min(b1)
b_end(1)   = max(b1)

bnum =2

;
;
;
xkill:
;
endelse

end
