;$Id: pos_widget.pro,v 1.3 2005/02/11 16:41:38 azuah Exp $
;====================================================================
;
;  POS_WIDGET.PRO   5-21-97   DSFoster
;
;  Function to position a widget onscreen according to the following:
;
;    If a valid parent widget is provided, center the widget
;    within the parent widget. Otherwise, place the widget at the
;    center of the screen. In both cases, account for the size of
;    the widget to be positioned. Also, ensure that the widget
;    is completely visible.
;
;  This function may be called before or after a widget has been
;  realized.
;
; Modifications:
;
;  5-21-97 DSF Created.
;

FUNCTION pos_widget, base, parent=parent, no_offset=no_offset, $
        cascade=cascade, right=right

status = 0

if ( widget_info(long(base), /valid_id ) eq 0 ) then begin
    message, 'Invalid widget ID specified', /continued
    status = -1
endif else begin

    device , get_screen_size=s_size                     ; Screen size
    geom = widget_info( base, /geometry )
    b_size = [ geom.scr_xsize + (2 * geom.margin), $    ; Size of widget
            geom.scr_ysize + (2 * geom.margin) ]

    if (keyword_set(PARENT)) then begin        ; Center within parent
           if ( widget_info(parent, /valid_id) ne 1 ) then begin
            message, 'Keyword PARENT not a valid widget ID', /continue
            xpos = s_size(0)/2 - b_size(0)/2
            ypos = s_size(1)/2 - b_size(1)/2
        endif else begin
            geom = widget_info( parent, /geometry )
            ; Position of parent
            p_offset = [ geom.xoffset, geom.yoffset ]
            p_size = [ geom.scr_xsize + (2 * geom.margin), $
                    geom.scr_ysize + (2 * geom.margin) ]
            if (keyword_set(CASCADE)) then begin
                xpos = p_offset(0) + p_size(0)/5
                ypos = p_offset(1) + p_size(1)/5
            endif else if (keyword_set(RIGHT)) then begin
                xpos = p_offset(0) + p_size(0)
                ypos = p_offset(1) + p_size(1)/2 - b_size(1)/2
            endif else begin
                xpos = p_offset(0) + p_size(0)/2 - b_size(0)/2
                ypos = p_offset(1) + p_size(1)/2 - b_size(1)/2
            endelse
        endelse
    endif else if (keyword_set(NO_OFFSET)) then begin
        xpos = 1
        ypos = 1
    endif else begin
        xpos = s_size(0)/2 - b_size(0)/2
        ypos = s_size(1)/2 - b_size(1)/2
    endelse

    ; Make sure widget is entirely visible

    xpos = ( 1 > xpos ) < (s_size(0) - b_size(0))
    ypos = ( 1 > ypos ) < (s_size(1) - b_size(1))

    widget_control, base, tlb_set_xoffset=xpos, tlb_set_yoffset=ypos
endelse

return, status
END
;=========== End of POS_WIDGET.PRO ==================================
