;time slice tools
 ;===============================================================================
;;subr xyslice_link_callback
;; xmtextfieldsetstring, $xyslicewin_num, string($link_window)
;; endsubr
 ;========================================================
subr xyselect_callback
 ;does a cross hairs
 w1 = fix(xmtextfieldgetstring ($xyslicewin_num)
 xwin,w1
 ;get first position and draw it inverted
 $view_cb_case(w1) = 5
 xtloop, 1
 !motif = 1
 !kb=0
 xinvertline,0,!iy,!ixhigh,!iy,w1
 xinvertline,!ix,0,!ix,!iyhigh,w1
 xflush
 ix=!ix		iy=!iy
 while (1) {
 if !ix ne ix or !iy ne iy then {
  ;undo the last cross hairs
  xinvertline,0,iy,!ixhigh,iy,w1
  xinvertline,ix,0,ix,!iyhigh,w1
  ix=!ix<!ixhigh	iy=!iy<!iyhigh
  ix=ix>0	iy=iy>0
  ;draw the cross hairs
  xinvertline,0,iy,!ixhigh,iy,w1
  xinvertline,ix,0,ix,!iyhigh,w1
  ixd = ix	iyd = iy
  convertfordata, ixd, iyd, w1
  xmtextfieldsetstring,$xyslicet6, ist(ixd,4)
  xmtextfieldsetstring,$xyslicet7, ist(iyd,4)
  xflush
  xtloop, 2
 }
 xquery,w1
 if !kb eq 0 then break
 } 
 endsubr
 ;===============================================================================
subr xyslice_callback
 ;currently the windows are fixed at 13 and 14
 wx = 13
 wy = 14
 ;generalize for different file sources 10/28/99
 ;check if mode supported yet
 if $xys_movie_type eq 2 then {
 	errormess,'movie source type\nnot supported yet'  return }

 if $xys_movie_type eq 3 then {
 ;if a single file, do some checking here, this is now done with a call
 ;to lightsetup_3d_fits if we have a mif file
 if $light_mif_flag then {
  xys_3d_fits_cb
  ;if we have xcen, ycen, and xscale then we can do position stuff, if
  ;just xscale is missing, we prompt for it,  if $mif_xscale is le 0,
  ;then positions will be invalid
  $xys_positions_valid = 0
  if isarray($mif_xcen) and isarray($mif_ycen) then {
  ;might have something
   if isarray($mif_xscale) then $light_positions_valid = 1 else {
   ;give the user a chance to enter a scale
     prompt_for_scale, scq
     if scq gt 0.0 then {
     $mif_xscale = zero(fltarr(dimen($mif_xcen,0))) + scq
     $xys_positions_valid = 1
     }
   }
  }
   } else {
 ;some of this duplicates code in imageread
  fname = xmtextfieldgetstring($xys_soptx(3))
  fty = data_type(fname, h, params)
  ;check if a 3-D file
  if invalid_3d_check(params) then return
  if nt2 eq 0 or nt2 gt params(4) then {
   ;probably forgot to set the limits, set them here and pop a message
   sq = 'bad limits for 3-D file\nsetting them now\nplease check and run again'
   xmtextfieldsetstring, $xyslicet3, '0'
   xmtextfieldsetstring, $xyslicet4, string(params(5)-1)
   errormess, sq
   return
  }
 } 
 }

 ;the file name template
 if $xys_movie_type eq 0 then {
 ;the range
 ;we read the text window so user doesn't have to hit return after a new entry
 first_file = fix(xmtextfieldgetstring ($xyslicet3)
 last_file = fix(xmtextfieldgetstring ($xyslicet4)
 $ix = fix(xmtextfieldgetstring ($xyslicet6)
 $iy = fix(xmtextfieldgetstring ($xyslicet7)
 dt = fix(xmtextfieldgetstring ($xyslicet5)
 dt = dt > 1
 ;get the file name template locally
 name = get_ft($xys_soptx(0))
 nt = 1 + (last_file - first_file)/dt
 if nt lt 0 then { ty,'illegal file number ranges'  return }
 ;read the first to get sizes, etc
 if dataread(x,fns(name, first_file),h) eq 1 then {
 nx = dimen(x,0)		ny = dimen(x,1)
 } else {
 xmmessage,$mess4, $f7, 'red'
 return
 }
 }
 
 ;more work needed obviously
 ;set up the xt and yt arrays
 xt = array(symdtype(x), nx, nt)
 yt = array(symdtype(x), ny, nt)
 ;now read all the specified files and make the slices
 h=''
 ;ty,'$ix, $iy =', $ix, $iy
 dump, xt, yt, x
 iq=0
 $mvprogress_flag = 1
 w = mvprogress_widget(first_file, last_file)
 xmsetmodal, w, 1
 if w ge 0 then xtmanage, w
 xmgetwidgetsize, w, dwx, dwy
 xmposition, w, $bw_ix - dwx, !screen_height - dwy - 5

 for i = first_file, last_file, dt do {
   xmsetlabel, $mvprogtext1, istring(i, 1)
   xtloop, 2
   if $mvprogress_flag ne 1 then break	;stop, but keep what we have
   if dataread(x,fns(name, i),h) ne 1 then {
     errormess, $mess4
     ;but use the lsat one rather than abort
     xt(0,iq) = xt(*,iq-1)  yt(0,iq) = yt(*,iq-1)
   } else {
     xt(0, iq) = x(*, $iy)	yt(0, iq) = x($ix, *)
   }
   iq = iq + 1
 }

 ;now save these and display them
 switch,xt,eval('$data'+istring(wx,1))
 switch,yt,eval('$data'+istring(wy,1))
 view, wx
 view, wy
 if defined($mvprogwidget) eq 1 then xtunmanage, $mvprogwidget
 if defined($mvproghelp) eq 1 then xtunmanage, $mvproghelp
 endsubr
 ;===============================================================================
subr mark,ix,iy,win
 xdrawline, ix-3, iy, ix+3,iy, win
 xdrawline, ix, iy-3, ix,iy+3, win
 endsubr
 ;=======================================================================
subr draw_selected_mark, win, color
 ;plot all the points as little pluses
 pencolor, color
 x1 = $events(0,$selected_event)
 y1 = $events(1,$selected_event)
 convertforview, x1, y1, win
 mark, x1, y1, win
 endsubr
 ;===============================================================================
subr draw_all_marks, win
 if $nevent le 0 then return
 ;plot all the points as little pluses
 ;allow (11/22/96) for 2 colors at present
 xwin, win
 color = 'red'
 for i=0,$nevent-1 do {
 x1 = $events(0,i)
 y1 = $events(1,i)
 convertforview, x1, y1, win
 ncase $events(4,i)
   color = 'red'
   color = 'purple'
 endcase
 pencolor, color
 ;ty,'i, $events(4,i), color =', i, $events(4,i), '  ',color
 mark, x1, y1, win
 }
 endsubr
 ;===============================================================================
subr trackmark_cb
 win = fix(xmtextfieldgetstring ($tracktext1)
 ;if we are already tracking, a press on this button turns it off
 if $view_cb_case(win) eq 3 then { trackend, win  return }
 ;if in modify, just turn off
 if $view_cb_case(win) eq 2 then  track_modify_end, win
 ;handle a still active select button ($view_cb_case = 1), by just turning off
 if $view_cb_case(win) eq 1 then track_select_end, win

 xmsetlabel, $trackmark_but, 'tracking'
 xmsetcolors, $trackmark_but, 'red'
 xmsize, $trackmark_but, 70, 50

 $view_cb_case(win) = 3
 endsubr
 ;===============================================================================
subr track_mark_pt, win
 if !button eq 0 then return
 nxm = $view_nx(win) - 1		nym = $view_ny(win) - 1
 ;our escape is right button, a value of 1024 for !kb
 ;if the right button (# 3), just cancel and reset
 if !button eq 3 then { trackend, win   return }
 ix = !ix	iy = !iy
 convertfordata, ix, iy, win

 if $nevent ge dimen($events,1) then {
 	ty,'feature list size exceeded, please save and clear'
	return
 }
 ;ok, get coords and add to event list, note current zoom factor
 $events(0, $nevent) = ix
 $events(1, $nevent) = iy
 $events(4, $nevent) = 0
 ;time is relevant only for a movie cube
 if win eq #play_window then
 	tq = fix(xmtextfieldgetstring($mvplay_nowtext))  else tq = 0
 $events(2, $nevent) = tq
 $events(3, $nevent) = $event_group
 ;also show in widget
 xmtextfieldsetstring, $tracktext3(1), string($nevent)
 xmtextfieldsetstring, $tracktext3(2), string(ix)
 xmtextfieldsetstring, $tracktext3(3), string(iy)
 xmtextfieldsetstring, $tracktext3(4), string(tq)
 xmtextfieldsetstring, $tracktext3(5), string($event_group)
 $nevent += 1
 xmtextfieldsetstring, $trackneventstext, string($nevent)
 
 ty,'nevent =', $nevent
 
 ;don't load the movie with marks until we can update this
 ;if win eq #play_window then {
 ;if $nevent eq 1 then {
 ;our first one, compute $track_ind_base
 ;nt = get_nt($data15)
 ;$track_ind_base = indgen(lonarr(nt)) * $view_nx(win) * $view_ny(win)
 ;}
 ;ixy = ix + iy * $view_nx(win)
 ;ind = $track_ind_base + ixy
 ;save_tmp = $data15(ind)
 ;$data15(ind) = !red_pixel

 ;if $nevent eq 1 then {
 ;again our first one, setup $save_cube_data
 ;$save_cube_data = save_tmp
 ;$tracking_cube_flag = 1
 ;redim, $save_cube_data, num_elem(save_tmp), 1
 ;} else {
 ;some already saved, append the latest, watch for mixtures
 ;if $tracking_cube_flag eq 1 then {
 ;$save_cube_data = [$save_cube_data, [save_tmp] ] } else {
 ;track_mix_err } 
 ;}
 ;} else {
 ;if  $tracking_cube_flag eq 1 then { track_mix_err }
 ;}
 draw_all_marks, win
 beep

 endsubr
 ;===============================================================================
subr track_mix_err
 sq ='You are mixing a movie and\na single image view in'
 sq += '\na tracking set\nPlease refrain from this!'
 sq += '\nYou must link back to last\nwindow and delete path'
 sq += '\nbefore coming to this new window'
 xmmessage, sq, $f7, 'red'
 endsubr
 ;===============================================================================
subr trackend, win
 ;stop tracking, reset button
 $tracker_flag = 0
 xmsetlabel, $trackmark_but, 'add new\npoints'
 xmsetcolors, $trackmark_but, 'gray'
 xmsize, $trackmark_but, 70, 50
 $view_cb_case(win) = 0
 endsubr
 ;===============================================================================
subr trackplot_cb
 win = fix(xmtextfieldgetstring ($tracktext1)
 if $nevent le 0 then return
 ;plot all the points as little pluses
 draw_all_marks, win
 endsubr
 ;===============================================================================
subr trackerase_cb
 win = fix(xmtextfieldgetstring ($tracktext1)
 if $nevent le 0 then return
 ;not much to do unless a movie cube
   ;if win eq #play_window then {
   ;if $tracking_cube_flag eq 1 then {
   ;restore our saved values
   ;inxy = lonarr($nevent)
   ;nx = dimen($data15,0)
   ;ny = dimen($data15,1)
   ;nt = dimen($data15,2)
   ;for i=0,$nevent-1 do {
   ;inxy(i) = $events(0,i) + nx * $events(1,i)
   ;}
   ;load the relevant data values
   ;for k= 0, nt -1 do {
   ;iq = inxy + k*nx*ny
   ;$data15(iq) = $save_cube_data(k,*)
   ;}
   ;} else { track_mix_err }
   ;}
 view_redraw, win
 endsubr
 ;===============================================================================
subr tracksave_cb
 if $nevent le 0 then return
 ;get the file name
 name = xmtextfieldgetstring($tracksavetext)
 fzwrite, $events(*,0:($nevent-1)),name,'(x,y,t,g) for data cube, g is a group #'
 endsubr
 ;===============================================================================
subr trackload_cb
 ;get the file name
 name = xmtextfieldgetstring($tracksavetext)
 if fzread(x,name) ne 1 then xmmessage,'can''t read\nfile', $f7, 'red'
 ;might not have groups
 if num_dim(x) ne 2 then { not_a_track_file return }
 id = dimen(x,0)
 if id ne 3 and id ne 4 then { not_a_track_file return }
 ;now safe to delete the current crop, if any
 if $nevent ne 0 then trackdeletepath_cb
 
 $nevent=dimen(x,1)
 zero, $events
 ;if no groups in x, then we get group 0
 for i=0,$nevent-1 do $events(0,i) = x(*,i)

 ;now load in cube if this is a movie window, else just draw
 win = fix(xmtextfieldgetstring ($tracktext1)
 
 ;if win eq #play_window then {
 ;nt = dimen($data15, 2)
 ;$track_ind_base = indgen(lonarr(nt)) * $view_nx(win) * $view_ny(win)
 ;inxy = lonarr($nevent)
 ;nx = dimen($data15,0)
 ;ny = dimen($data15,1)
 ;nt = dimen($data15,2)
 ;$save_cube_data = bytarr(nt, $nevent)
 ;for i=0,$nevent-1 do {
 ;inxy(i) = $events(0,i) + nx * $events(1,i)
 ;ind = $track_ind_base + inxy(i)
 ;$save_cube_data(0, i) = $data15(ind)
 ;$data15(ind) = !red_pixel
 ;view_redraw, win
 ;}
 ;} else { trackplot_cb }
 trackplot_cb
 endsubr
 ;===============================================================================
subr not_a_track_file
 xmmessage,'data in file\nnot a path set', $f7, 'red'
 endsubr
 ;===============================================================================
subr trackdeletepath_cb
 win = fix(xmtextfieldgetstring ($tracktext1)
 ;we erase the points in the cube and delete the path, the latter just involves
 ;setting the event count to zip and setting the label widgets
 trackerase_cb
 $nevent =0
 $last_selected_pt = 0
 $tracking_cube_flag = 0
 for i=1,5 do  xmtextfieldsetstring, $tracktext3(i), ''
 xmtextfieldsetstring, $trackneventstext, string($nevent)
 view_redraw, win
 endsubr
 ;===============================================================================
subr no_points
 xmmessage,'no points\ndefined', $f7, 'red'
 endsubr
 ;===============================================================================
subr illegal_point
 xmmessage,sprintf('selected point %d\nnot defined',$selected_event),$f7,'red'
 endsubr
 ;===============================================================================
subr trackdeletept_cb
 win = fix(xmtextfieldgetstring ($tracktext1)
 ;remove a point from the list and decrement the count
 if $nevent eq 0 then { no_points	return }
 $selected_event =  fix(xmtextgetstring($tracktext3(1))
 if $selected_event lt 0 or $selected_event ge $nevent then {
  illegal_point  return }
 ty,'trying to delete', $selected_event
 xa = $events(0,$selected_event)
 ya = $events(1,$selected_event)

 ;if win eq #play_window then {
 ;if $tracking_cube_flag eq 1 then {
 ;restore this point to the original values (unmark)
 ;ixy = xa + ya*$view_nx(win)
 ;ind = $track_ind_base + ixy
 ;restore data cube
 ;$data15(ind) = $save_cube_data(*, $selected_event)
 ;} else { track_mix_err }
 ;}

 $nevent = $nevent - 1
 xmtextfieldsetstring, $trackneventstext, string($nevent)
 ;if we are deleting the last point or there are none left, we don't need
 ;to re-arrange
 if $selected_event ne $nevent and $nevent ne 0 then {

 ;must be in the middle somewhere (or beginning)
 for i=$selected_event, $nevent-1 do {
 	$events(0,i) = $events(*,i+1)
 	;if win eq #play_window and $tracking_cube_flag eq 1 then {
 	;	$save_cube_data(0,i) = $save_cube_data(*,i+1)
	;}
 }
 }
 ;and we have to crop $save_cube_data because of the way it gets bumped
 ;if win eq #play_window and $tracking_cube_flag eq 1 then {
 ;$save_cube_data = $save_cube_data(*, 0:($nevent-1))
 ;view_redraw, win
 ;} else {
 view_redraw, win
 draw_all_marks, win
 ;}	
 endsubr
 ;===============================================================================
subr trackselect_cb
 win = fix(xmtextfieldgetstring ($tracktext1)
 ;if in modify, just turn off
 if $view_cb_case(win) eq 2 then  track_modify_end, win
 ;handle an active mark by turning it off
 if $view_cb_case(win) eq 3 then trackend, win

 if $nevent eq 0 then { no_points	return }
 change_button_label, $trackselect_but, 'please\nselect', 'red'
 xmsize, $trackselect_but, 70, 50
 $view_cb_case(win) = 1
 endsubr
 ;===============================================================================
subr trackmodify_cb
 win = fix(xmtextfieldgetstring ($tracktext1)
 ;if in modify, just turn off
 if $view_cb_case(win) eq 2 then { track_modify_end, win   return }
 ;handle a still active select button ($view_cb_case = 1), by just turning off
 if $view_cb_case(win) eq 1 then track_select_end, win
 ;handle an active mark by turning it off
 if $view_cb_case(win) eq 3 then trackend, win

 if $nevent eq 0 then { no_points	return }
 xmsetlabel, $trackmodify_but, 'modifying'
 xmsetcolors, $trackmodify_but, 'red'
 xmsize, $trackmodify_but, 70, 50
 $view_cb_case(win) = 2
 endsubr
 ;===============================================================================
subr trackgroup_cb
 win = fix(xmtextfieldgetstring ($tracktext1)
 if $view_cb_case(win) eq 4 then { track_group_end, win   return }
 ;if in modify, just turn off
 if $view_cb_case(win) eq 2 then { track_modify_end, win   return }
 ;handle a still active select button ($view_cb_case = 1), by just turning off
 if $view_cb_case(win) eq 1 then track_select_end, win
 ;handle an active mark by turning it off
 if $view_cb_case(win) eq 3 then trackend, win
 
 if $nevent eq 0 then { no_points	return }
 xmsetlabel, $track_group_but, 'grouping'
 xmsetcolors, $track_group_but, 'red'
 xmsize, $track_group_but, 70, 50

 $view_cb_case(win) = 4
 endsubr
 ;===============================================================================
subr track_modify_end, win
 win = fix(xmtextfieldgetstring ($tracktext1)
 xmsetlabel, $trackmodify_but, 'modify\npoint'
 xmsetcolors, $trackmodify_but, 'gray'
 xmsize, $trackmodify_but, 70, 50
 $view_cb_case(win) = 0
 endsubr
 ;===============================================================================
subr track_select_pt, win
 ;called by the callback when $view_cb_case(win) = 1
 ;selects the closest point on a button down and then resets
 if !button eq 0 then return
 ;compare this position with all the events (if any)
 if $nevent le 0 then return ;if the right button (# 3), just cancel and reset
 if !button ne 3 then {
 ;for 1 or 2 do the select
 x = !ix
 y = !iy
 convertfordata, x, y, win
 d = (x-$events(0,*))^2 + (y-$events(1,*))^2
 ;d is square of distances, find the min
 iq = minloc(d)
 $selected_event = iq(0)
 ;xmtextfieldsetstring, $track_text2, istring($selected_event, 2)
 
 ;if win eq #play_window then {
 ;ixy = $events(0,$selected_event) + $events(1,$selected_event)*$view_nx(win)
 ;ind = $track_ind_base + ixy
 ;if isarray($last_selected_pt) then $data15($last_selected_pt) = !red_pixel
 ;$data15(ind) = !green_pixel
 ;$last_selected_pt = ind
 ;view_redraw, win
 ;} else {
 draw_selected_mark, win, 'green'
 ;}

 }
 track_select_end, win
 ;also show in widget
 xmtextfieldsetstring, $tracktext3(1), string($selected_event)
 xmtextfieldsetstring, $tracktext3(2), string($events(0, $selected_event))
 xmtextfieldsetstring, $tracktext3(3), string($events(1, $selected_event))
 xmtextfieldsetstring, $tracktext3(4), string($events(2, $selected_event))
 xmtextfieldsetstring, $tracktext3(5), string($events(3, $selected_event))
 endsubr
 ;===============================================================================
subr track_select_end, win
 xmsetlabel, $trackselect_but, 'select\npoint'
 xmsetcolors, $trackselect_but, 'gray'
 xmsize, $trackselect_but, 70, 50
 $view_cb_case(win) = 0
 endsubr
 ;===============================================================================
subr track_modify_pt, win
 ;called by the callback when $view_cb_case(win) = 2
 ;reads the selected point number and moves it toward mouse with each click
 if !button eq 0 then return
 ;compare this position with all the events (if any)
 ;if the right button (# 3), just cancel and reset
 if !button eq 3 or $nevent le 0 then {
 xmsetlabel, $trackmodify_but, 'modify\npoint'
 xmsetcolors, $trackmodify_but, 'gray'
 xmsize, $trackmodify_but, 70, 50
 $view_cb_case(win) = 0
 return
 }
 ;for 1 or 2 do the modify
 x = !ix
 y = !iy
 convertfordata, x, y, win
 $selected_event =  fix(xmtextgetstring($tracktext3(1))
 ;make sure it is valid
 if $selected_event ge $nevent then { illegal_point  return }
 xa = $events(0,$selected_event)
 ya = $events(1,$selected_event)


 dx = (x - xa)
 dy = (y - ya)
 ;only move in the direction with the largest difference
 xb = xa	yb = ya
 if abs(dx) gt abs(dy) then {
 if dx ne 0 then xb = xa + !button * dx/abs(dx)
 } else {
 if dy ne 0 then yb = ya + !button * dy/abs(dy)
 }
 $events(0,$selected_event) = xb
 $events(1,$selected_event) = yb
 xmtextfieldsetstring, $tracktext3(2), string(xb)
 xmtextfieldsetstring, $tracktext3(3), string(yb)

 ;if a movie cube, shift the marked pixels
 ;if win eq #play_window then {
 ;restore this point to the original values (unmark)
 ;ixy = xa + ya*$view_nx(win)
 ;ind = $track_ind_base + ixy
 ;$data15(ind) = $save_cube_data(*, $selected_event)
 ;ixy = xb + yb*$view_nx(win)
 ;ind = $track_ind_base + ixy
 ;$save_cube_data(0, $selected_event) = $data15(ind)
 ;$data15(ind) = !green_pixel
 ;$last_selected_pt = ind
 ;view_redraw, win
 ;} else {
 ;view_redraw, win
 ;draw_all_marks, win
 ;draw_selected_mark, win, 'green'
 ;}

 endsubr
 ;===============================================================================
subr track_group_pt, win
 ;called by the callback when $view_cb_case(win) = 4
 ;selects the closest point on a button down and then resets its group id
 ;turns off by clicking button again
 if !button eq 0 then return
 ;compare this position with all the events (if any)
 if $nevent le 0 then return
 ;if the right button (# 3), just cancel and reset
 if !button eq 3 or $nevent le 0 then { track_group_end, win   return }
 ;for 1 or 2 do the select
 x = !ix
 y = !iy
 convertfordata, x, y, win
 d = (x-$events(0,*))^2 + (y-$events(1,*))^2
 ;d is square of distances, find the min
 iq = minloc(d)
 $selected_event = iq(0)
 ;xmtextfieldsetstring, $track_text2, istring($selected_event, 2)
 ;get the group number
 $event_group = fix(xmtextfieldgetstring($track_group_text))
 $events(3, $selected_event) = $event_group
 
 ;if win eq #play_window then {
 ;ixy = $events(0,$selected_event) + $events(1,$selected_event)*$view_nx(win)
 ;ind = $track_ind_base + ixy
 ;don't de-select the last one
 ;$data15(ind) = !green_pixel
 ;$last_selected_pt = ind
 ;view_redraw, win
 ;} else {
 draw_selected_mark, win, 'green'
 ;}

 ;also show in widget
 xmtextfieldsetstring, $tracktext3(1), string($selected_event)
 xmtextfieldsetstring, $tracktext3(2), string($events(0, $selected_event))
 xmtextfieldsetstring, $tracktext3(3), string($events(1, $selected_event))
 xmtextfieldsetstring, $tracktext3(4), string($events(2, $selected_event))
 xmtextfieldsetstring, $tracktext3(5), string($events(3, $selected_event))
 endsubr
 ;===============================================================================
subr track_group_end, win
 xmsetlabel, $track_group_but, 'put in\ngroup'
 xmsetcolors, $track_group_but, 'gray'
 xmsize, $track_group_but, 70, 50
 $view_cb_case(win) = 0
 endsubr
 ;===============================================================================
subr color_group, mode
 if mode eq 1 then { pix = !green_pixel  color ='green' }
 if mode eq 0 then { pix = !red_pixel  color ='red' }
 if $nevent le 0 then return
 win = fix(xmtextfieldgetstring ($tracktext1)
 ;get the group number
 $event_group = fix(xmtextfieldgetstring($track_group_text))
 ;ty,'group: ', $event_group
 ;now find all with this group and mark them with the selection color
 for i=0, $nevent-1 do {
 if $events(3,i) eq $event_group then {
  $events(4,i) = mode	;keep the mode for future redraws
  ;if win eq #play_window then {
  ;ixy = $events(0,i) + $events(1,i)*$view_nx(win)
  ;ind = $track_ind_base + ixy
  ;$data15(ind) = pix
  ;}
  }
 }
 ;if win eq #play_window then view_redraw, win else draw_all_marks, win
 draw_all_marks, win
 endsubr
 ;===============================================================================
subr trackshowgroup_cb
 color_group, 1
 endsubr
 ;===============================================================================
subr trackunshowgroup_cb
 color_group, 0
 endsubr
 ;========================================================
subr xys_loadname_cb
 ;get the temporal range from the movelist widget if defined
 if defined($movfrwidget) eq 1 then {
 xmtextfieldsetstring,$xys_soptx(0), xmtextfieldgetstring($movfrwidget_t1)
 xmtextfieldsetstring,$xyslicet3, xmtextfieldgetstring($first_file_widget)
 xmtextfieldsetstring,$xyslicet4, xmtextfieldgetstring($last_file_widget)
 xmtextfieldsetstring,$xyslicet5, '1'
 }
 endsubr
 ;========================================================
subr sob_source_cb, movie_type, sopt, current_sop
 ;called by option menu, determines the type of input for movie creator
 movie_type = $option_value
 if defined(sopt($option_value)) eq 0 then {
   errormess,'undefined movie source\ntype specified'
   return
 }
 xtunmanage, current_sop
 xtmanage, sopt($option_value)
 current_sop = sopt($option_value)
 endsubr
 ;===============================================================================
subr movie_source_setup, wq, prefix
 ;option menu for source type
 s1='file name template'  s2='image list (TRACE)'  s3='file list'
 s4='multi-image file'
 arg1 = prefix+'_movie_type'
 arg2 = prefix+'_sopf'
 arg3 = prefix+'_current_sop'
 arg4 = prefix+'_soptx'
 ;for subroutine names, drop the $ if any in the prefix
 presub = prefix
 if presub(0) eq '$' then presub = presub(1:*)
 ws = intarr(4)
 sq = 'sob_source_cb,eval('+arg1+'),eval('+arg2+'),eval('+arg3+')'
 sop = xmoptionmenu(wq, sq, $f4,'gray','Source',s1,s2,s3,s4)
 xmposition, sop(0), 232, 0
 ix = 0	iy = 45
 ;ws will hold the 4 types as 4 frame widgets, then set arg2 to it
 ;there is also a text widget associated with each source option
 ws = intarr(4)
 ws(0) = file_template_field(wq, ix, iy, t1, prefix+'_loadname_cb')
 equate, eval(arg3), ws(0)
 ws(1) = image_list_field(wq, ix, iy, t2, 'loadtracelist_cb')
 ws(2) = file_list_field(wq, ix, iy, t3, presub+'_loadlist_cb')
 ws(3) = file_3d_field(wq, ix, iy, t4,presub+'_3d_fits_cb')
 xtunmanage, ws(1)
 xtunmanage, ws(2)
 xtunmanage, ws(3)
 equate, eval(arg1), 0
 equate, eval(arg2), ws
 equate, eval(arg4), [t1,t2,t3,t4]
 endsubr
 ;==========================================================
subr xys_3d_fits_cb
 ;read the name in the text field and check it out
 fname = xmtextfieldgetstring($xys_soptx(3))
 fname = strtrim(fname)      ;remove trailing blanks
 ;this helps later on, put the clean version back in the text field
 xmtextfieldsetstring, $xys_soptx(3), fname
 ;we are still in the era of a single mif file, so check if name is the same
 ;as current one and don't re-load if it is; this means we can also use this
 ;routine to check for a new file just before actually loading the data
 if defined($xys_mif_name) then {
   if $mif_name eq $xys_mif_name and fname eq $mif_name then return }
 fty = data_type(fname, h, params)
 if fty lt 0 then { errormess,sprintf('File not readable\n%s', fname)  return }
 $xys_mif_flag = $multi_image_flag
 if $xys_mif_flag then {
   iq = mif_open_file(fname)
   $xys_mif_name = $mif_name
   if iq eq 0 then return
   ;load some sizes
   params(5) = $mif_nimages
   params(3) = $mif_nx_out(0)	;try first image size
   params(4) = $mif_ny_out(0)
 } else {
 ;check if a 3-D file
 if invalid_3d_check(params) then return
 }
 xmtextfieldsetstring, $xyslicet3, '0'
 xmtextfieldsetstring, $xyslicet4, string(params(5)-1)
 xmtextfieldsetstring,$xyslicet5, '1'
 endsubr
 ;===============================================================================
func xyslice_widget(iq)
 ;make an xy slice from a movie
 if defined($xyslicewidget) eq 0 then {
 $xyslicewidget = xmtoplevel_board(0,0, 'X/Y Slice Tool',10,10)
 ;;topbuttonsetup, '$xyslice', 'xyslice_link_callback'
 wq = topbuttonsetupsanslink_tl('$xyslice')
 xmposition, wq, 5, 5

 ;;ix = 10	iy = 50
 ;;$xyslice_sopt1 = file_template_field($xyslicewidget,ix,iy,$xyslicet1,'xyslice_loadname')
 movie_source_setup, wq, '$xys'
 ;source window #
 ix = 10		iy = 120
 $xyslicewin_num = source_window_frame($xyslicewidget, ix,iy)
 dx = 190

 link_cb_string = 'generic_link_cb,'+string($xyslicewin_num)
 sq = '{$link_action='''+link_cb_string+''' link_setup }'
 bq = xmbutton(wq, 'Link', sq, $f4, 'darkgreen')
 xmposition, bq, 144, 0, 55, 30

 ;the range stuff and dt
 ix = dx+dx+30
 range_delta_field, $xyslicewidget, ix, iy, $xyslicet3, $xyslicet4, $xyslicet5

 ;the "do it" button
 bq = xmbutton($xyslicewidget,'do it !', 'xyslice_callback', $f2,'red')
 xmposition,bq, 10, 170, 80, 50
 
 ;the select x/y button and the positions
 ix=210
 fw=xmframe($xyslicewidget)
 xmposition,fw, ix, iy
 board=xmboard(fw, 140, 80)
 bq = xmbutton(board,'select\n x/y', 'xyselect_callback', $f4,'gray')
 xmposition,bq, 10, 15, 60, 50
 $xyslicet6 = xmtextfield(board,'',4,'',$f3,'white')
 xmposition,$xyslicet6, 90, 10, 50, 30
 iq = xmlabel(board,'x',$f4)
 xmposition,iq, 78, 15
 $xyslicet7 = xmtextfield(board,'',4,'',$f3,'white')
 xmposition,$xyslicet7, 90, 40, 50, 30
 iq = xmlabel(board,'y',$f4)
 xmposition,iq, 78, 45

 xyslice_loadname
 }
 return, $xyslicewidget
 endfunc
 ;=============================================================================== 
subr track_widget
 if defined($trackwidget) eq 0 then {
   compile_file, getenv('ANA_WLIB') + '/trackwidgettool'
 } else { xtpopup, $trackwidget }
 endsubr
 ;===============================================================================
