%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function constrainToWorkArea(h_figure)
%constrainToWorkArea Shift figure to fit inside a screen's work area.
%   constrainToWorkArea(h_figure) translates a figure horizontally and
%   vertically if necessary to put the figure inside a screen's work
%   area.  On a PC running Windows, the work area is the region of the
%   screen not obscured by the task bar.  On other systems, the work area
%   is the entire display.

%   Copyright 1993-2004 The MathWorks, Inc.
%   $Revision: 1.1.8.1 $  $Date: 2004/08/10 01:50:02 $

wa = getWorkArea;

old_units = get(h_figure, 'Units');
set(h_figure, 'Units', 'pixels');

fig_outer_position = get(h_figure, 'OuterPosition');

% This is a workaround for G226797, G226799
% The outer position is wrong on the MAC.
if strcmpi('mac',computer);
  fig_outer_position = get(h_figure, 'Position');
end

left_correction = max(0, wa.left - fig_outer_position(1));
right_correction = max(0, (fig_outer_position(1) - left_correction + ...
                           fig_outer_position(3)) - wa.right);
horizontal_correction = left_correction - right_correction;

bottom_correction = max(0, wa.bottom - fig_outer_position(2));
top_correction = max(0, (fig_outer_position(2) - bottom_correction + ...
                         fig_outer_position(4)) - wa.top);
vertical_correction = bottom_correction - top_correction;

set(h_figure, 'Position', get(h_figure, 'Position') + ...
              [horizontal_correction vertical_correction 0 0]);

set(h_figure, 'Units', old_units);
