pro circle_cursor, x0, y0, r, INIT = init, FIXED_SIZE = fixed_size, $
    MESSAGE = message

device, get_graphics = old, set_graphics = 6  ;Set xor
col = !d.n_colors -1

if keyword_set(message) then begin
    print, "Drag Left button to move circle."
    If keyword_set(fixed) then $
      print, "Drag Middle button near a corner to resize circle."
    print, "Hit Right button when finished positioning circle."
    endif

if keyword_set(init) eq 0 then begin  ;Supply default values for circle:
    if keyword_set(fixed_size) eq 0 then begin
        r = !d.x_size/8   ;no fixed size.
    endif

        x0 = !d.x_size/2. - r/2.
        y0 = !d.y_size/2. - r/2.
;        x0 = !d.x_size/2
;        y0 = !d.y_size/2
endif

    px0 = cos(findgen(101)*2.*!pi/100)      ;X points
    py0 = sin(findgen(101)*2.*!pi/100)      ;Y points

button = 0
goto, middle

while 1 do begin
    old_button = button
    cursor, x, y, 2, /dev   ;Wait for a button
    button = !err
    if (old_button eq 0) and (button ne 0) then begin
        mx0 = x     ;For dragging, mouse locn...
        my0 = y     
        x00 = x0    ;Orig start of center
        y00 = y0
        endif
    if !err eq 1 then begin  ;Drag entire circle?
        x0 = x00 + x - mx0
        y0 = y00 + y - my0
        endif
    if (!err eq 2) and (keyword_set(fixed_size) eq 0) then begin ;New size?
        r0 = r
        dx = x - x0 & dy = y - y0       ; Distance dragged...

        r = sqrt(dx*dx + dy*dy)     ; New radius
        endif
    plots, px, py, col=col, /dev, thick=1, lines=0  ; Erase last circle
    empty               ;Decwindow bug

    if !err eq 4 then begin  ;Quitting?
        device,set_graphics = old
;PLOTS,PX,PY,/DEV,col=!d.n_colors-1,thick=3,lines=0
        return
        endif
middle:

;   x0 = x0 > 0
;   y0 = y0 > 0
;   x0 = x0 < (!d.x_size-1 - r) ;Never outside window
;   y0 = y0 < (!d.y_size-1 - r)

    px = r*px0 + x0     ;X points
    py = r*py0 + y0     ;Y points

    plots,px, py, col=col, /dev, thick=1, lines=0  ;Draw the circle
    wait, .1        ;Dont hog it all
    endwhile
end