function ViewerObj = plotpos(ViewerObj);
%PLOTPOS  Determines new locations for response plots in the viewer
%         object based on the current figure size/position.
%         This function also adds new background axes to the viewer
%         as needed.

%   Authors: Adam DiVergilio, 11-99
%   Copyright 1986-2000 The MathWorks, Inc. 
%   $Revision: 1.7 $  $Date: 2000/06/14 20:03:28 $

%---ViewerObj info
BackgroundAxes = ViewerObj.BackgroundAxes;

%---Total number of BackgroundAxes to position
N = length(BackgroundAxes);

%---Number of existing BackgroundAxes
ExistIdx = find(BackgroundAxes>0);
ExistN = length(ExistIdx);

%---Number of BackgroundAxes to add
NewIdx = find(BackgroundAxes<=0);
NewN = length(NewIdx);

%---Figure position (pixels)
oldunits = get(ViewerObj.Handle,'Units');
if ~strcmpi(oldunits,'pixels')
   set([ViewerObj.Handle;ViewerObj.StatusFrame],'Units','pixels');
end
FigPos = get(ViewerObj.Handle,'Position');

%---Adjusted figure position (accounts for status bar, if visible)
sp = get(ViewerObj.StatusFrame,'Position');
if strcmpi(get(ViewerObj.StatusFrame,'Visible'),'on')
   bedge = sp(2)+sp(4);
else
   bedge = sp(2);
end
FigPos = [0 bedge FigPos(3)-24 FigPos(4)-bedge];

%---Spacing values
tedge = 26;
bedge = 46;
ledge = 66;

%---Calculate new viewer axes positions (pixels)
%--- (width and height will be limited to 1 pixel to avoid invalid position vector)
switch N
case 1
   aw = max(1,FigPos(3)/1 - (ledge));
   ah = max(1,FigPos(4)/1 - (tedge+bedge));
   AxPo = [[FigPos(1)+ledge FigPos(2)+bedge aw ah]];
case 2
   aw = max(1,FigPos(3)/1 - (ledge));
   ah = max(1,FigPos(4)/2 - (tedge+bedge));
   AxPo = [[FigPos(1)+ledge FigPos(2)+2*bedge+1*tedge+1*ah aw ah];...
           [FigPos(1)+ledge FigPos(2)+1*bedge+0*tedge+0*ah aw ah]];  
case 3
   aw = max(1,FigPos(3)/1 - (ledge));
   ah = max(1,FigPos(4)/3 - (tedge+bedge));
   AxPo = [[FigPos(1)+ledge FigPos(2)+3*bedge+2*tedge+2*ah aw ah];...
           [FigPos(1)+ledge FigPos(2)+2*bedge+1*tedge+1*ah aw ah];...
           [FigPos(1)+ledge FigPos(2)+1*bedge+0*tedge+0*ah aw ah]];  
case 4
   aw = max(1,FigPos(3)/2 - (ledge));
   ah = max(1,FigPos(4)/2 - (tedge+bedge));
   AxPo = [[FigPos(1)+1*ledge+0*aw FigPos(2)+2*bedge+1*tedge+1*ah aw ah];...
           [FigPos(1)+2*ledge+1*aw FigPos(2)+2*bedge+1*tedge+1*ah aw ah];...
           [FigPos(1)+1*ledge+0*aw FigPos(2)+1*bedge+0*tedge+0*ah aw ah];...
           [FigPos(1)+2*ledge+1*aw FigPos(2)+1*bedge+0*tedge+0*ah aw ah]];  
case 5
   aw1 = max(1,FigPos(3)/2 - (ledge));
   aw2 = max(1,FigPos(3)/3 - (ledge));
   ah  = max(1,FigPos(4)/2 - (tedge+bedge));
   AxPo = [[FigPos(1)+1*ledge+0*aw1 FigPos(2)+2*bedge+1*tedge+1*ah aw1 ah];...
           [FigPos(1)+2*ledge+1*aw1 FigPos(2)+2*bedge+1*tedge+1*ah aw1 ah];...
           [FigPos(1)+1*ledge+0*aw2 FigPos(2)+1*bedge+0*tedge+0*ah aw2 ah];...
           [FigPos(1)+2*ledge+1*aw2 FigPos(2)+1*bedge+0*tedge+0*ah aw2 ah];...
           [FigPos(1)+3*ledge+2*aw2 FigPos(2)+1*bedge+0*tedge+0*ah aw2 ah]];  
case 6
   aw = max(1,FigPos(3)/3 - (ledge));
   ah = max(1,FigPos(4)/2 - (tedge+bedge));
   AxPo = [[FigPos(1)+1*ledge+0*aw FigPos(2)+2*bedge+1*tedge+1*ah aw ah];...
           [FigPos(1)+2*ledge+1*aw FigPos(2)+2*bedge+1*tedge+1*ah aw ah];...
           [FigPos(1)+3*ledge+2*aw FigPos(2)+2*bedge+1*tedge+1*ah aw ah];...
           [FigPos(1)+1*ledge+0*aw FigPos(2)+1*bedge+0*tedge+0*ah aw ah];...
           [FigPos(1)+2*ledge+1*aw FigPos(2)+1*bedge+0*tedge+0*ah aw ah];...
           [FigPos(1)+3*ledge+2*aw FigPos(2)+1*bedge+0*tedge+0*ah aw ah]];  
end

%---Remap existing BackgroundAxes
for ct=1:ExistN
   n = ExistIdx(ct);
   set(BackgroundAxes(n),'Units','pixels','Position',AxPo(n,:));
end

%---If additional BackgroundAxes are needed, add them
if NewN
   for ct=1:NewN
      n = NewIdx(ct);
      BackgroundAxes(n) = axes('Parent',ViewerObj.Handle,...
         'Visible','off',...
         'XLim',[0 1],'YLim',[0 1],...
         'XTick',[],'YTick',[],...
         'XTickLabel','','YTickLabel','',...
         'Units','pixels',...
         'Position',AxPo(n,:));
   end
   %---Update ViewerObj
   ViewerObj.BackgroundAxes = BackgroundAxes;
end

if ~strcmpi(oldunits,'pixels')
   set([ViewerObj.Handle;ViewerObj.StatusFrame],'Units',oldunits);
end
