;+---------------------------------------------
; NAME:
;       progressw
; PURPOSE:
;       show progress bar until done, or the user clicks the STOP button.
; USE:
;	customize for your use for progress indications.
; CATEGORY:
;       Demo; Real Time applications
; CALLING SEQUENCE:
;       IDL> done = progressw( seconds )
; INPUTS:
;       seconds - total seconds for finish
; KEYWORD PARAMETERS:
;    Optional:
;	print - if set, will print time elapsed
;	waitSeconds - seconds to wait (pause) after a timer event is fielded
;			default = 1/2 second.
;	xsize - length in pixels of progress bar
;	ysize - width in pixels of progress bar
;	group_leader - higher level widget to place this on top of 
; OUTPUTS:
;       done - 1, if time expired, or 0 if user hit STOP
; COMMON BLOCKS:
;       NONE
; EXAMPLE:
;	IDL> done = ProgressW( 20 )	; seconds expected for motor to run
;	IDL> IF NOT done THEN stopmotor
; RELATED ROUTINE:
;	BarometerW
; NOTES:
;	Only works on X.
; MODIFICATION HISTORY:
;	19-Jul-00 Written by Bill Davis
;----------------------------------------------------------------
FUNCTION progressw, length,  $
         waitSeconds=waitSeconds, xsize=xsize, ysize=ysize, $
         group_leader=group_leader, print=print

if NOT usingXwindows() THEN return, 0
	 
IF N_ELEMENTS( length ) EQ 0 THEN length = 5	; seconds

IF N_ELEMENTS( waitSeconds ) EQ 0 THEN waitSeconds = 0.5

IF N_ELEMENTS( xsize ) EQ 0 THEN xsize = 300
IF N_ELEMENTS( ysize ) EQ 0 THEN ysize = 50 

green = mk_color('green')
bar = REPLICATE( green, xsize, ysize )

IF N_ELEMENTS( group_leader ) GT 0 THEN $
   wid = widget_base(title='Progress', /floating, group_leader=group_leader) $
   ELSE wid = widget_base(title='Progress')

wCol  = widget_base( wid, /col )

wDraw = WIDGET_DRAW( wCol, xsize=xsize, ysize=ysize )

wStopButton = widget_button ( wCol, value='STOP' )

WIDGET_CONTROL, wid, /REALIZE
WIDGET_CONTROL, wDraw, GET_VALUE=drawID	; get the graphics window ID
WIDGET_CONTROL, wDraw, TIMER=0.0

t0 = SYSTIME(1)

while 1 do begin

   event = widget_event( wid )

   IF (TAG_NAMES(event, /STRUCTURE_NAME) EQ 'WIDGET_TIMER') THEN BEGIN
      seconds = (SYSTIME(1)-t0)
      IF KEYWORD_SET( print) THEN print,seconds, ' seconds'
      len = ((xsize-1)*seconds/length) < (xsize-1)
      wset, drawID
      TV, bar[ 0:len, *]
      IF seconds GE length THEN BEGIN
         widget_control, wid, /DESTROY
         RETURN, 1	; ----------->
      ENDIF
      WIDGET_CONTROL, event.id, TIMER=waitSeconds	; reset timer
   ENDIF ELSE BEGIN
      if event.id ne 0 then begin

	 if tag_names(event, /structure_name) eq $
               'WIDGET_KILL_REQUEST' then begin
	    widget_control, wid, /DESTROY
	    return, 0
	 endif

	 if event.id eq wStopButton then begin
	    widget_control, wid, /DESTROY
	    return, 0
	 endif

      end
  ENDELSE
endwhile

end