function fig = prepare_figure( fig, xy, wh )
%PREPARE_FIGURE  Prepare figure on a given coordinates.

% (c) 2020-02-20 Martin Matousek
% Last change: $Date$
%              $Revision$

  try
    close( fig )
  catch
  end

  screen_size = get( 0, 'ScreenSize' );
  screen_area = getappdata( 0, 'SCREEN_AREA' );
  if isempty( screen_area )
    screen_area = screen_size;
  end

  screen_area_size = screen_area(3:4) - screen_area(1:2) + 1;

  xy = real( xy ) + imag( xy ) .* screen_area_size;
  wh = real( wh ) + imag( wh ) .* screen_area_size;

  xy(1) = min( xy(1) + screen_area( 1 ) - 1, ...
               screen_area( 1 ) + screen_area( 3 ) - wh( 1 ) + 1 );
  xy(2) = max( screen_size(4) - ( screen_area( 2 ) + xy(2) + wh(2) - 3 ), ...
               screen_size(4) - ( screen_area( 2 ) + screen_area( 4 ) - 1 ) +1);

  figure( fig )

  clf
  set( fig, 'MenuBar', 'none' );
  set( fig, 'OuterPosition', [ xy(:); wh(:) ]' );

end % fcn.
