pro xread_hist_event, event
;
;
;
common xread_hist_blk2, result, result0, sea_str
;
qdebug = 1
if (qdebug) then print, event_name(event)
;if (qdebug) then help, /st, event
widget_control,event.top,get_uvalue=all
;
qtime = (strtrim(arr2str(all.text_vals(0:1),delim=''),2) ne '') and $
	(strtrim(arr2str(all.text_vals(2:3),delim=''),2) ne '')

case (strtrim(event_name(event),2)) of
   ;----------------- Button Events ------------------------
   "BUTTON":begin                               ; option selection
      case strupcase(get_wvalue(event.id)) of
	 ;------ Main buttons
         "QUIT": begin
		widget_control, event.top,/destroy
	    endcase
	 "CLEARPLOT": begin
		clearplot
		clear_utplot
	    endcase
	 "HARDCOPY": begin
		out = ['XREAD_HIST  Program Run: ' + !stime, ' ', result]
		outfil = concat_dir(getenv('HOME'), 'xread_hist.temp')
		prstr, out, file=outfil
		dprint, outfil
		file_delete, outfil
            endcase
	 "LAST N HOURS": begin
			entim = ut_time()
			dur = float(all.text_vals(4)) * 60.*60	;in seconds
			sttim = anytim2ints(entim, off=-dur)

			all.text_vals(0)	= gt_day(sttim, /str)
			all.text_vals(1)	= gt_time(sttim, /str)
			all.text_vals(2)	= gt_day(entim, /str)
			all.text_vals(3)	= gt_time(entim, /str)

			for i=0,3 do widget_control, all.text_id(i), set_value=all.text_vals(i)
		end
	 "EXECUTE CALL": begin
		if (qtime) then begin
			sttim = anytim2ex( arr2str(all.text_vals(0:1), delim=' ') )
			entim = anytim2ex( arr2str(all.text_vals(2:3), delim=' ') )
			dur = (int2secarr(entim, sttim) / 60. /60. )<99		;duration in hours
			dur_hr = fix(dur)
			dur_min = (dur - dur_hr)*60.
			timrange = gt_day(sttim, /str, /leadzero) + ',' + gt_time(sttim, /str) + '-' + $
					string(dur_hr, dur_min, format='(i2.2, ":", i2.2)')
			;;timrange = ex2fid(sttim) + '-' + ex2fid(entim)
			;;timrange = str_replace(timrange, '.', '_')
			options = arr2str(all.options, delim=' ')

			;cmd = all.cmd
			cmd = 'read_hist -s gmt ' + options + ' ' + timrange 
			widget_control, all.id_cmd, set_value=cmd
			;cmd = 'read_hist -s gmt -a 950425_233140_24-950425_233140_26'		;temporary

			widget_control, all.id_info, set_value='*** Making READ_HIST Call *** '
			mapx, event.top,/map,/show,sensitive=0
			spawn, cmd, result
			mapx, event.top,/map,/show,sensitive=1
			widget_control, all.id_info, set_value=result
			result0 = result
		end else begin
			tbeep, 3
			out = ' '
			if (not qtime) then out = [out, 'You must enter the start/end time first']
			widget_control, all.id_info, set_value=out
		end
	     endcase
	 "SET SEARCH STR": begin
		xlabel = 'Search String ' + strtrim(indgen(n_elements(sea_str))+1,2)
		xmenu_gen_input, xlabel, sea_str, group=event.top, nchar=20
		widget_control, all.id_sea, set_value=sea_str
	     endcase
	 "EXECUTE SEARCH": begin
		ss22 = where(strtrim(sea_str,2) ne '', nss22)
		if (nss22 ne 0) then begin
		    n = n_elements(result)
		    qok = bytarr(n)
		    for i=0,nss22-1 do begin
			ss33 = wc_where(result, '*'+sea_str(ss22(i))+'*', nss33, /case_ignore)
			if (nss33 ne 0) then qok(ss33) = qok(ss33) + 1
		    end
		    if (all.qAND_logic) then ss44 = where(qok eq nss22, nss44) $
					else ss44 = where(qok ne 0, nss44)
		    if (nss44 eq 0) then result = '' $
				else result = result(ss44)
		    widget_control, all.id_info, set_value=result
		end else begin
		    print, 'No search strings set...'
		end
	     endcase
	 "RESTORE UNSEARCHED LIST":  begin
		result = result0
		widget_control, all.id_info, set_value=result
	     end
	 "RQID SEA": begin
		widget_control, all.id_sea, set_value='RQID'
		ss33 = wc_where(result0, '*RQID*', nss33, /case_ignore)
		if (nss33 eq 0) then begin
		    result = '*** No Request ID (RQID) Items Found ***'
		end else begin
		    result = result0(ss33)
		    ;23:44:26: >23:44:26 RQID: 00041, Rejected - format error found in message received, reason code: 4
		    ss44 = bsort(strmid(result, 25, 6))
		    result = result(ss44)
		end
		widget_control, all.id_info, set_value=result
	     end

	 "DISPLAY OOTC AND BLOCK TELECOMMANDS":  if (event.select) then all.options(0) = '-t' else all.options(0) = ''
	 "DISPLAY LIMIT CHANGES AND TRANSITIONS":if (event.select) then all.options(1) = '-x' else all.options(1) = ''
	 "DISPLAY PROCEDURE EXECUTION":          if (event.select) then all.options(2) = '-l' else all.options(2) = ''
	 "DISPLAY ALL CLI COMMANDS":             if (event.select) then all.options(3) = '-c' else all.options(3) = ''
	 "DISPLAY OPERATOR LOG MESSAGES":        if (event.select) then all.options(4) = '-o' else all.options(4) = ''
	 "DISPLAY SCI160K DATA PRODUCT MSGS":    if (event.select) then all.options(5) = '-d' else all.options(5) = ''
	 "DISPLAY DIRECTORY MANAGER MESSAGES":   if (event.select) then all.options(6) = '-j' else all.options(6) = ''
	 "ENABLE EOF FORMATTED HISTORY FILES":   if (event.select) then all.options(7) = '-e' else all.options(7) = ''
	 "DISPLAY ALL MESSAGES":                 if (event.select) then all.options(8) = '-a' else all.options(8) = ''
	 "DISPLAY ALL INTERNAL DEBUGGING MSGS":  if (event.select) then all.options(9) = '-i' else all.options(9) = ''

	 "USE AND LOGIC":			 all.qAND_logic = event.select
	 "USE OR LOGIC":			 all.qAND_logic = not event.select

          else: message,/info,"UNKNOWN BUTTON:   " + strupcase(get_wvalue(event.id))
       endcase
   endcase 
   ;----------------- Text Events ------------------------
   "TEXT_CH": begin
		newv = get_wvalue(event.id)
		ss = where(all.text_id eq event.id)
		ss = ss(0)
		if (ss le 3) then begin
		    if ((ss mod 2) eq 0) then newv2 = gt_day(newv,/str) $
					else newv2 = gt_time(newv,/str)
		    all.text_vals(ss) = newv2
		    if (ss eq 0) and (all.text_vals(2) eq '') then all.text_vals(2) = all.text_vals(0)

		    for i=0,3 do widget_control, all.text_id(i), set_value=all.text_vals(i)
		end else begin
		    barr = byte(strcompress(newv, /remove))
		    if (min(barr) lt 48) or (max(barr) gt 57) then begin	;not a number
			tbeep, 3
			widget_control, all.id_info, set_value='The "DURATION" value must be an integer'
		    end else begin
			all.text_vals(ss) = newv
		    end
		    widget_control, all.text_id(4), set_value=all.text_vals(4)
		end
	end

   ;----------------- List Events ------------------------
   "LIST": begin
	case event.id of
	    all.id_sea: begin
	       end
	endcase
   endcase
endcase

widget_control,event.top, set_uvalue=all, bad_id=destroyed	;update structure holding all of the info

return
end

;-------------------------------------------------------------
pro xread_hist, sttim, entim, mnem, infil=infil, every=every, eof=eof, all_opt=all_opt
;
;+
;NAME:
;	xread_hist
;PURPOSE:
;	To accept user specification for the start/end time and
;	options to call the C READ_HIST module and to spawn the
;	call
;SAMPLE CALLING SEQUENCE:
;	xread_hist
;	xread_hist, '5-jan-95', '5-jan-95 3:00', mnem
;HISTORY:
;	Created 3-May-95 by M.Morrison
;	 5-May-95 (MDM) - Patch to work on FAULT (it was hanging
;			  because of a WIDGET_CONTROL call before
;			  the /realize command !?!?!?
;			- Added /EOF switch 
;	25-May-95 (MDM) - Added "-d", "-j", "-i" and "-a" options
;			  through to read_hist
;			- Coded the search option
;	25-Jul-95 (MDM) - Added option to search and sort through
;			  the Request ID items
;			- Changed the results window to be 100 chars
;			  instead of 80
;-

common xread_hist_blk2, result, result0, sea_str

sea_str = strarr(5)
result = ''
result0 = ''
;
font = get_xfont(closest=13,/only_one)
if (getenv('MDI_DEF_FONT') ne "") then font = getenv('MDI_DEF_FONT')
widget_control, default_font=font
device, font=font

base0=widget_base(/column,title='X READ_HIST', xoff=0, yoff=0)
xmenu, ['QUIT', 'Execute Call', 'Hardcopy', 'Last N Hours', $
	'Set Search Str', 'Execute Search', 'Restore Unsearched List', $
	'RQID Sea'], $
	base0, $
	buttons=main_buts, /row

;--------------

base_group1=widget_base(base0, /row, /frame)

base_col1 = widget_base(base_group1, /column, /frame)
lab_base_col1 = widget_label(base_col1, value = 'Reading HK Database')

timebase = widget_base(base_col1, /row)
xx       = widget_label(timebase, value = 'Start Date:')
stdat_rd = widget_text(timebase, /editable, xsize=10, ysize=1)
xx       = widget_label(timebase, value = ' Time:')
sttim_rd = widget_text(timebase, /editable, xsize=10, ysize=1)

timebase = widget_base(base_col1, /row)
xx       = widget_label(timebase, value = 'End Date:  ')
endat_rd = widget_text(timebase, /editable, xsize=10, ysize=1)
xx       = widget_label(timebase, value = ' Time:')
entim_rd = widget_text(timebase, /editable, xsize=10, ysize=1)

basexx   = widget_base(base_col1, /row)
xx       = widget_label(basexx, value = '"Last N Hours" Duration (hrs): ')
id_dur   = widget_text(basexx, /editable, xsize=4, ysize=1, value='12')

base_col2 = widget_base(base_group1, /column, /frame)
xmenu, ['Display OOTC and Block Telecommands', $
	'Display Limit Changes and Transitions', $
	'Display Procedure Execution', $
	'Display all CLI Commands', $
	'Display Operator Log Messages', $
	'Display SCI160K Data Product Msgs', $
	'Display Directory Manager Messages', $
	'Enable EOF Formatted History Files', $
	'Display All messages', $
	'Display All Internal Debugging Msgs'], $
	base_col2, /column, /nonexclusive, /frame, buttons=but_option

base_col3 = widget_base(base_group1, /column, /frame)
base1a = widget_base(base_col3, /column, /frame)
lab1a  = widget_label(base1a, value='Search Strings to Match')
id_sea = widget_list(base1a, xsize=15, ysize=5, value='')

xmenu, ['Use AND Logic', $
	'Use OR Logic'], $
	base_col3, /column, /exclusive, buttons=but_logic
;-------------------

base_group2 = widget_base(base0, /row, /frame)
base2_col1 = widget_base(base_group2, /column, /frame)
lab_base2_col1 = widget_label(base2_col1, value = 'Command Line')
id_cmd = widget_text(base2_col1, xsize=100, ysize=1, value='')

;-------------------------

base_group3=widget_base(base0, /row, /frame)
base3_col1 = widget_base(base_group3, /column, /frame)
lab_base3_col1 = widget_label(base3_col1, value = 'Results')
id_info = widget_text(base3_col1, xsize=100, ysize=20, /scroll)
widget_control, id_info, /append

;--------------------------

all = {base:base0, $
	text_vals: strarr(5), $		;see which goes where below
	text_id: [stdat_rd, sttim_rd, endat_rd, entim_rd, $
		  id_dur], $
	id_info: id_info, $
	id_cmd:  id_cmd, $
	id_sea:  id_sea, $
	id_options: but_option, $
	id_logic:   but_logic, $
	qAND_logic: 0b, $
	options: strarr(10), $
	junk:0}
all.text_vals(4) = '4'

if (keyword_set(sttim)) then begin
    all.text_vals(0) = gt_day(sttim, /str)
    all.text_vals(1) = gt_time(sttim, /str)
end
if (keyword_set(entim)) then begin
    all.text_vals(2) = gt_day(entim, /str)
    all.text_vals(3) = gt_time(entim, /str)
end
if (n_elements(dur) ne 0) then all.text_vals(8) = strtrim(every,2)

widget_control,set_uvalue=all, base0
widget_control,base0,/realize
;---- Update widgets which may have been updated/written to
for i=0,4 do widget_control, all.text_id(i), set_value=all.text_vals(i)
if (keyword_set(eof)) then begin
    all.options(7) = '-e'
    widget_control, all.id_options(7), /set_button
end
if (keyword_set(all_opt)) then begin
    all.options(8) = '-a'
    widget_control, all.id_options(8), /set_button
end
widget_control, all.id_logic(1), /set_button
widget_control,set_uvalue=all, base0
xmanager, 'xread_hist', base0

return
end
