; $Id: ddregion.pro,v 1.2 2002/01/14 20:59:26 amunter Exp $
PRO ddreplot,xmin,xmax,ymin,ymax
;plots,[xmin,xmin]/128.,[ymin,ymax]/128.,/normal
;plots,xmax/128.,ymax/128.,/continue,/normal
;plots,xmax/128.,ymin/128.,/continue,/normal
;plots,xmin/128.,ymin/128.,/continue,/normal
;da=bytarr(128,128)
;da(xmin:xmax,127-ymax:127-ymin)=255
;tv,da
polyfill,[xmin,xmin,xmax,xmax]/128.,[ymin,ymax,ymax,ymin]/128.,/normal
end

PRO ddregion_event,ev,des

COMMON SEARP,sear,ddmask
COMMON ddbases,ddbase,drbase,base,xmit,xmat,ymit,ymat

widget_control,ev.id,get_value=value
type=tag_names(ev,/structure)
IF (type eq 'WIDGET_BUTTON') THEN BEGIN
    CASE value of
        'OK':BEGIN
            widget_control,xmit,get_value=xmin
            xmin=float(xmin(0))
            widget_control,xmat,get_value=xmax
            xmax=float(xmax(0))
            widget_control,ymit,get_value=ymin
            ymin=float(ymin(0))
            widget_control,ymat,get_value=ymax
            ymax=float(ymax(0))
            IF xmin gt xmax THEN xmin = xmax
            IF ymin gt ymax THEN ymin = ymax
            IF xmin lt 0 THEN xmin=0
            IF xmax gt 127 THEN xmax =127
            IF ymin lt 0 THEN ymin=0
            IF ymax gt 127 THEN ymax =127
            ddmask(xmin:xmax,ymin:ymax)=1
            widget_control,ev.top,/destroy
            des=1
        END
        'Defaults':BEGIN
            xmin=0
            xmax=127
            ymin=0
            ymax=127
            ddmask(xmin:xmax,ymin:ymax)=1
            widget_control,ev.top,/destroy
            des=1
        END
        'Aux':BEGIN
            SEAR=0
            xmin=0
            xmax=127
            ymin=0
            ymax=127
            ddmask(xmin:xmax,ymin:ymax)=1
            widget_control,ev.top,/destroy
            des=1
        END
    ENDCASE
;print,'widget_button'
ENDIF  
IF (ev.id eq xmit) or (ev.id eq xmat) or (ev.id eq ymit) or (ev.id eq ymat) $
  THEN  BEGIN
    widget_control,xmit,get_value=xmin
    xmin=float(xmin(0))
    widget_control,xmat,get_value=xmax
    xmax=float(xmax(0))
    widget_control,ymit,get_value=ymin
    ymin=float(ymin(0))
    widget_control,ymat,get_value=ymax
    ymax=float(ymax(0))
    IF xmin gt xmax THEN xmin = xmax
    IF ymin gt ymax THEN ymin = ymax
    IF xmin lt 0 THEN xmin=0
    IF xmax gt 127 THEN xmax =127
    IF ymin lt 0 THEN ymin=0
    IF ymax gt 127 THEN ymax =127
    erase
    ddreplot,xmin,xmax,ymin,ymax
ENDIF

END

PRO ddregion
;;
;;  By Guangyong Xu, September 1996
;;
;;  Put a mask on the area detector
;;  This subroutine may be replaced by any subroutine called ddregion
;;  It should return the mask array (ddmask) for the area detector

COMMON SEARP,sear,ddmask
COMMON ddbases,ddbase,drbase,base,xmit,xmat,ymit,ymat
xmin=0
xmax=127
ymin=0
ymax=127
ddbase=widget_base(TITLE='Please select a region',XSIZE=420,YSIZE=256,/row)
drbase=widget_draw(ddbase,/BUTTON_EVENTS,XSIZE=256,YSIZE=256)
base=widget_base(ddbase,/column)
xmi=widget_base(base,/row,/frame)
xmil=widget_label(xmi,value='Xmin :')
xmit=widget_text(xmi,/editable,xsize=10,ysize=1,uvalue='xmin',value=strcompress(xmin))
xma=widget_base(base,/row,/frame)
xmal=widget_label(xma,value='Xmax :')
xmat=widget_text(xma,/editable,xsize=10,ysize=1,uvalue='xmax',value=strcompress(xmax))
ymi=widget_base(base,/row,/frame)
ymil=widget_label(ymi,value='Ymin :')
ymit=widget_text(ymi,/editable,xsize=10,ysize=1,uvalue='ymin',value=strcompress(ymin))
yma=widget_base(base,/row,/frame)
ymal=widget_label(yma,value='Ymax :')
ymat=widget_text(yma,/editable,xsize=10,ysize=1,uvalue='ymax',value=strcompress(ymax))
okb=widget_button(base,value='OK')
def=widget_button(base,value='Defaults')
auxf=widget_button(base,value='Aux')

widget_control,ddbase,/realize
ddreplot,xmin,xmax,ymin,ymax

destroy=0
while destroy eq 0 do begin
    ev=widget_event(ddbase,bad_id=badid)
    ddregion_event,ev,destroy
endwhile
;print,ddbase,drbase,base,xmit,xmat,ymit,ymat

;xmanager,'ddregion',ddbase
;print,'finished'
end
