pro IDACZoomBase_event,Event

;help,event,/struct

    ZoomBaseID = Event.top
    chid = widget_info(ZoomBaseID,/child)
    widget_control, chid, get_uvalue=pinfo

    zm = (*pinfo).zmNew
    x = fix(Event.x/zm)
    y = fix(Event.y/zm)
    coordStr = string(format='("    X: ",i3.3,"     Y: ",i3.3)',x,y)
    retval = fltarr(3)
    if (*pinfo).helio then begin
        ; Convert from pixel to heliographic coords
        retval = sxi_pix2hel(x, y,pinfo)
        if n_elements(retval) gt 1 then begin
            if retval(0) eq 0. then begin
                lat=retval(1) & lon = retval(2)
                if lat ge 0 then t1='  N' else t1='  S'
                if lon ge 0 then t2='  W' else t2='  E'
                lat = abs(lat) & lon = abs(lon)
                v1 = lat & v2 = lon
            endif else begin
                pa = retval(1) & rv = retval(2)
                t1 = "PA=" & t2 = "RV="
                v1 = pa & v2 = rv
            endelse
        endif
        coordStr = string(format='(A3,F7.2,1x,A3,F7.2)',t1,v1,t2,v2)
    endif

    ; Compose and display title, showing zoom%, FTS data value and coords
    fmt = '("IDAC Zoom ",i3,"%",5x,"Intensity:",f11.2,10x,"'+ coordStr + '")'

    ttl = string(format=fmt,zm*100.,(*pinfo).ftsData(x,y))
    widget_control, ZoomBaseID, tlb_set_title=ttl

    ; If profiles window is active, update it
    device,window_state=ws
    if ws[(*pinfo).dwin] eq 1 then IDACUpdateProfiles,x,y,pinfo

end
;=================================================================================
pro IDACZoomNew, IDACBaseID

    ; Get info structure pointer (pinfo)
    chid = widget_info(IDACBaseID,/child)
    widget_control, chid, get_uvalue=pinfo
	widget_control,/hourglass

    zm = (*pinfo).zmNew
    ;print,fix(zm*100.),'%'

    ; Get size of original image
    isize = size((*pinfo).ftsData)
    iy = isize[2]
    ix = isize[1]

    if (*pinfo).newz eq 0 then begin
    	; Get desktop size (sx,sy)
    	device,get_screen_size = xy
    	sx = xy[0] & sy = xy[1]
    	print,'Screen size: ',xy
    	sy = .88 * sy ; Allow space for Windows 'Start' menu
    	sx = .95 * sx ; Save a little space in x direction, too
    	; Create the zoom window--it has to fit on the desktop
    	ttl = "IDAC Zoom"+string(fix(zm*100.))+'%'
    	yzm = zm * iy
    	xzm = zm * ix

    	if yzm le sy then begin
        	; Zoomed window will fit on the desktop w/o scrolling
        	print,'No scrolling'
        	IDACZoomBase = widget_base(group_leader=IDACbaseID,uname='IDACZoomBase', $
            	xoffset = 100,title=ttl,space=3,xpad=3,column=1, $
            	;xsize=xzm+30, ysize=yzm+30,kill_notify='IDACOnDestroyZoom',$
            	xsize=xzm+16, ysize=yzm+16,kill_notify='IDACOnDestroyZoom',$
            	uvalue=pinfo)
        	IDACZoomDraw = widget_draw(IDACZoomBase,uname='IDACZoomDraw', $
            	;xoffset=3,yoffset =3,scr_xsize = xzm+16, scr_ysize=yzm+16, $
            	xoffset=3,yoffset =3,scr_xsize = xzm, scr_ysize=yzm, $
            	xsize = xzm, ysize=yzm,retain=2,/motion_events) ;,/button_events)
    	endif else begin
        	; Zoomed window will need scroll bars
        	print,'Scrolling'
        	yzm = sy
        	if xzm gt sx then xzm = sx
        	IDACZoomBase = widget_base(group_leader=IDACbaseID,uname='IDACZoomBase', $
            	xoffset = 100,title=ttl,space=3,xpad=3,column=1, $
            	xsize=xzm+30,ysize=yzm+30,scr_xsize=xzm+30,scr_ysize=yzm+60,$
            	kill_notify='IDACOnDestroyZoom',uvalue=pinfo)
        	IDACZoomDraw = widget_draw(IDACZoomBase,uname='IDACZoomDraw', $
            	xoffset=3,yoffset =3,scr_xsize = xzm+16, scr_ysize=yzm+16, $
            	xsize = xzm, ysize=yzm,retain=2,/scroll,/motion_events);, $
            	;/button_events)
    	endelse
    	(*pinfo).zoomID = IDACZoomBase

    	widget_control,/realize,IDACZoomBase

    	idDraw = widget_info(IDACZoomBase, find_by_uname='IDACZoomDraw')
    	if(idDraw gt 0) then begin
        	; Make the draw widget the current, active window
        	widget_control, idDraw, get_value=drawWin
        	wset, drawWin
    	endif

    	; Store draw window# of this zoom widget
    	(*pinfo).zwin = drawWin

    endif else begin
      	; Make the draw widget the current, active window
        IDACZoomBase = (*pinfo).zoomID
        idDraw = widget_info(IDACZoomBase, find_by_uname='IDACZoomDraw')
       	;widget_control, idDraw, get_value=drawWin
       	;wset, drawWin
       	wset,(*pinfo).zwin
    endelse

;print,'In idacZoomNew:'
;print,'   zwin:    ',(*pinfo).zwin
;print,'   zoomID:  ',(*pinfo).zoomID
;print,'   newz:    ',(*pinfo).newz

    ; Create zoomed image
    zimage = congrid((*pinfo).ftsData,ix * zm, $
    	iy * zm,/interp)

;device,window_state=ws
;print,'zwin state=',ws[(*pinfo).zwin]

    ; Put new image into draw window
    ;widget_control,idDraw,set_uvalue = zimage

    ; Adjust scrollbars to accommodate zoomed image
    widget_control,idDraw,draw_xsize=ix * zm, $
            draw_ysize=iy * zm

    ; Apply scaling to image and display it
	if (*pinfo).logscl then begin
    	IDAClogtv,zimage,pinfo,(*pinfo).zwin   ; **************
	endif else begin
		tvscl,zimage
	endelse
    ;IDAClogtv,zimage,pinfo,(*pinfo).zwin    ; ******************

	(*pinfo).newz = 1

    ; Save pinfo in child of this base
    chid = widget_info(IDACZoomBase,/child)
    widget_control,chid,set_uvalue=pinfo

	XManager,'IDACZoomBase',IDACZoomBase,/no_block
end
