function dummy = updatelims(Editor)
%UPDATELIMS  Resets axis limits.

%   Author(s): P. Gahinet
%   Copyright 1986-2000 The MathWorks, Inc. 
%   $Revision: 1.8 $  $Date: 2000/07/31 16:04:01 $

% REVISIT
dummy = [];

% Sample time and editor modes
% RE: Protect against uninitialized call through SISO prefs (g84952)
EditorOn = ~strcmp(Editor.EditMode,'off');
Ts = getTs(Editor.LoopData);
AutoX = EditorOn & strcmp(Editor.XlimMode,'auto');
AutoY = EditorOn & strcmp(Editor.YlimMode,'auto');
GridOn = strcmp(Editor.Grid,'on');
HG = Editor.HG;

% Reset X limits if in autoscale mode, ignore new values otherwise
if AutoX
    Xlim = Editor.XlimOpt;
    if Ts & GridOn
        % Make sure to include unit circle
        Xlim(1) = min(Xlim(1),-1);
        Xlim(2) = max(Xlim(2),1);
    end
    set(HG.PlotAxes,'Xlim',Xlim)
else
    Xlim = get(HG.PlotAxes,'Xlim');
end

% Reset Y limits if in autoscale mode, ignore new values otherwise
if AutoY
    Ylim = Editor.YlimOpt;
    if Ts & GridOn
        % Make sure to include unit circle
        Ylim(1) = min(Ylim(1),-1);
        Ylim(2) = -Ylim(1);
    end
    set(HG.PlotAxes,'Ylim',Ylim)
else
    Ylim = get(HG.PlotAxes,'Ylim');
end

% Adjust limits if equal aspect ratio is on
if strcmpi(Editor.AxisEqual,'on')
    [Xlim,Ylim] = localAxisEqual(Xlim,Ylim,HG.PlotAxes);
end

% Update extent of X and Y axis lines
if all(ishandle(HG.AxisLines)),
    set(HG.AxisLines(1),'Xdata',Xlim);
    set(HG.AxisLines(2),'Ydata',Ylim);
end

% Redraw grid if on
if GridOn
    plotgrid(Editor,Ts);   
elseif Ts
    % Add unit circle in discrete time
    plotcircle(Editor);
end

% Redraw design constraint lines
plotconstr(Editor,Ts);

% Reorder axes children
if EditorOn
	HG = Editor.HG;
	TopKids = [HG.ClosedLoop ; comphandles(Editor) ; ...
			HG.System.Pole ; HG.System.Zero ; HG.Locus]; 
	BottomKids = [HG.AxisLines ; HG.ConstraintLines; ...
			HG.GridLines];
	set(HG.PlotAxes,'Children',[TopKids;BottomKids(ishandle(BottomKids))])
end

%%%%%%%%%%%%%%%%%%
% localAxisEqual %
%%%%%%%%%%%%%%%%%%
function [Xlim,Ylim] = localAxisEqual(Xlim,Ylim,Ax)
% Update limits to show equal aspect ratio
 units = get(Ax,'Units');
 if ~strcmpi(units,'pixels')
    set(Ax,'Units','pixels');
    p = get(Ax,'Position');
    set(Ax,'Units',units);
 else
    p = get(Ax,'Position');
 end
 %---Pixel extent
  px = p(3);
  py = p(4);
 %---Data extent
  dx = abs(diff(Xlim)); 
  dy = abs(diff(Ylim));
 %---Effective extent
  xf = dx*py;
  yf = dy*px;
 %---Update limits
 if xf>yf
    %---Effective Xlim is larger, adjust Ylim
     dd = xf/px-dy;
     Ylim = [Ylim(1)-dd/2 Ylim(2)+dd/2];
     set(Ax,'Ylim',Ylim);
 elseif yf>xf
    %---Effective Ylim is larger, adjust Xlim
     dd = yf/py-dx;
     Xlim = [Xlim(1)-dd/2 Xlim(2)+dd/2];
     set(Ax,'Xlim',Xlim);
 end
