;+
; zcurs_ok
;
; PURPOSE
;	To flash up a bunch of images and allow the user to 
;	accept or reject 'em -- speeds manual inspection of 
;	data cubes. Each image in `cube' is displayed, and the
;	user can accept or reject is by clicking the mouse
;	button in the display window.
;
;
; USAGE
;	ok_arr = zcurs_ok(cube)
;
; RETURNS
;	A boolean array indicating whether each image in `cube' was
; 	accepted by the user or not.
;
; HISTORY
;	Written sometime in early 1998, Craig DeForest
;	Modified 24-Nov-98:  If the images are small enough, 
;	or if we feed in aflag by hand, we do a "mosaic" type 
;	scan.  Much more sophisticated browser.
;
;-
pro draw_zcurs_ok_panel,index,cube
common zcurs_ok_common_block,w,h,nw,nh,checked,out,pageno,exmark,checkmark

	x0 = 1 + ((index - pageno*nw*nh) mod nw)*w
	y0 = 1 + (nh - ((index - pageno*nw*nh) /  nw) - 1)*h
	
; Draw the image (and superposed number and mark) or just a slash
	if(index lt nlm(cube(0,0,*))) then begin
		tv,cube(*,*,index),x0,y0 
		; If it has been checked, draw the appropriate mark
		if(checked(index)) then begin
			if(out(index)) then pattern = checkmark else pattern = exmark
			plots,x0+w/4+(w/2)*pattern(*,0),y0+(h/4)+(h/2)*pattern(*,1),/device,color=255,thick=3
		end
	end else $
		plots,[x0,x0+w-1],[y0,y0+h-1],color=255,/device,thick=3

; Label the image
		xyouts,x0+2,y0+2,fug(index),color=255,charsize=0.67,/device


end


function zcurs_ok,cube,maxwsiz0=maxwsiz0
common zcurs_ok_common_block,w,h,nw,nh,checked,out,pageno,exmark,checkmark

out = bytarr(n_elements(cube(0,0,*)))
checked = bytarr(n_elements(cube(0,0,*)))

if isvalid(maxwsiz0) then maxwsiz = maxwsiz0 else maxwsiz = 800

n = nlm(cube(0,0,*))
w = nlm(cube(*,0,0))+1
h = nlm(cube(0,*,0))+1

nw = (maxwsiz/nlm(cube(*,0,0))>1)
nh = (maxwsiz/nlm(cube(0,*,0))>1)

topmargin = 35

window,14,xsiz=1 + (long(w)*nw)<long(maxwsiz),ysiz=1 + (long(h)*nh)<maxwsiz + topmargin

checkmark = [[0,0.2,1.0],[0.2,0,1.0]]
exmark = [[0,1,0.5,0,1],[0,1,0.5,1,0]]

npages = (n+nw*nh-1)/(nw*nh)


pageno = 0
repeat begin
	erase
	for i=0,nw+1 do plots,i*w*[1,1],[0,nh*h],color=255,/device
	for i=0,nh+1 do plots,[0,nw*w],i*h*[1,1],color=255,/device

	if(pageno gt 0) then xyouts,3,h*nh+3,"<PREV PAGE",color=255,/device,align=0
	if(pageno lt npages-1) then xyouts,w*nw-3,h*nh+3,"NEXT PAGE>",color=255,/device,align=1
	xyouts,(w*nw)/2,h*nh+3,"QUIT",color=255,/device,align=0.5

	xyouts,3*(w*nw)/10,h*nh+3,"Accept pg",color=255,/device,align=0.5,charsize=0.8
	xyouts,7*((w*nw)/10),h*nh+3,"Reject pg",color=255,/device,align=0.5,charsize=0.8

	xyouts,3,h*nh+23,'Page '+fug(pageno)+' of '+fug(npages),color=255,/device,align=0,charsize=0.8

	; Draw the page...
	print,'Page ',fug(pageno),' of ',fug(npages),'...'
	for i=0,nw-1 do for j=0,nh-1 do begin
		index = pageno*nw*nh + j*nw + i
		draw_zcurs_ok_panel,index,cube
	end

	prevpage =0
	nextpage=0
	quit=0
	repeat begin
		; Gather info
		xyouts,(nw*w)/2,nh*h+23,align=0.5,"READING MOUSE",color=255,/device,charsize=0.8
		cursor,x,y,3,/device
		xyouts,(nw*w)/2,nh*h+23,align=0.5,"READING MOUSE",color=0,/device,charsize=0.8
		xp = x/w
		yp = y/h
		panel = pageno*(nh*nw) + (nh - yp - 1)*nw + xp
		x0 = 1 + xp*w
		y0 = 1 + yp*h


		bottom_pane    = (((pageno+1)*nh*nw)<n)
		top_pane = pageno*nh*nw
		panelct = bottom_pane - top_pane
		ckct_pg    = total(checked(top_pane:bottom_pane-1))
		ckct_total = total(checked)
;		print,"ckct_pg = ",ckct_pg,", ckct_total=",ckct_total


		if(yp lt nh) then begin ; Not in the top margin - clicked a panel
			mb = !MOUSE.BUTTON
;			print,'MOUSEBUTTON=',mb



			; Figure mouse buttons...
			if(mb eq 1) then begin		; Left button: accept
				if(checked(panel) eq 1) and (out(panel) eq 1) then begin
					nextpage = (ckct_pg eq panelct) and (pageno lt n-1)
					quit =     (ckct_total eq n)
				end
				out(panel) = 1
				checked(panel) = 1
				nextpage = nextpage or (nh eq 1 and nw eq 1)
			end
			if(mb eq 4) then begin
				if(checked(panel) eq 1) and (out(panel) eq 0) then begin
					nextpage = (ckct_pg eq panelct) and (pageno lt n-1)
					quit = (ckct_total eq n)
				end

				out(panel) = 0
				checked(panel) = 1
				nextpage = nextpage or (nh eq 1 and nw eq 1)
			end
			if(mb eq 2) then begin
				prevpage = (ckct_pg eq 0)
				checked(panel) = 0
			end

			if(mb ne 0) then $
				draw_zcurs_ok_panel,pageno*nh*nw+xp+(nh-yp-1)*nw,cube
		end else begin ; in the top margin - clicked a button 
			xz  = (x / ((w*nw)/5))
;			print,'MENU BAR: xz=',xz
			if(xz eq 0) and pageno gt 0 then prevpage=1
			if(xz eq 4) and pageno lt n-1 then nextpage=1
			if(xz eq 2) then quit=1

			if(xz eq 1) or (xz eq 3) then begin
				accept = (xz eq 1)
				for zz=top_pane,bottom_pane-1 do $
					if not checked(zz) then begin
						checked(zz) = 1
						out(zz) = accept
						draw_zcurs_ok_panel,zz,cube
					end
				nextpage = (pageno lt npages-1)
			end

		end	
	end until (prevpage or nextpage or quit)
	if prevpage then pageno = pageno - 1
	if nextpage then pageno = pageno + 1
	
	if(quit) and (ckct_total lt n) then begin
		tbeep
		print,'WARNING - not all images were checked.  Rejecting unchecked ones...'
		out(where(checked eq 0)) = -2
	end
	
end until (quit)
wdelete,14
return,out
end
