function dummy = layout(sisodb)
%LAYOUT  Positions graphical editors in SISO Tool figure.

%   Author(s): K. Gondoy, P. Gahinet
%   Copyright 1986-2000 The MathWorks, Inc. 
%   $Revision: 1.7 $  $Date: 2000/06/14 20:22:14 $

% REVISIT
dummy = [];

% Active editors
isActive = strcmp(get(sisodb.PlotEditors,'Visible'),'on');
ActivePlotEditors = find(isActive);
NumActive = length(ActivePlotEditors);

% Parameters for axis positioning
FigPos = get(sisodb.Figure,'Position');
FigW = FigPos(3);
FigH = FigPos(4);
Border = 3;  % 3 character border
if NumActive==1 | FigW>120
    Offset = 8;   % Offset for LHS axis border
    YlabelVis = 'on';
else    
    % Don't display Y labels if figure is too small
    Offset = 4;  
    YlabelVis = 'off';
end
AxesW1 = FigW-2*Border-Offset;        % Axes width in single-column config
AxesW2 = (FigW-3*Border-2*Offset)/2;  % Axes width in two-column config
AxesH1 = FigH-13;                     % Axes height in single-row config
AxesH2 = (FigH-15)/2;                 % Axes height in two-row config

% Compute new normalized positions
Xs1 = Border+Offset;
Xs2 = FigW-Border-AxesW2;
switch NumActive
case 1
    XlabelVis = 'on';
    NewPos = {[Xs1 5.8 AxesW1 AxesH1]};
case 2
    XlabelVis = 'on';
    NewPos = {[Xs1 5.8 AxesW2 AxesH1] ; [Xs2 5.8 AxesW2 AxesH1]};
case 3
    XlabelVis = 'off';
    NewPos = {[Xs1 7.5+AxesH2 AxesW2 AxesH2] ; ...
            [Xs1 4.25 AxesW2 AxesH2] ; ...
            [Xs2 4.25 AxesW2 FigH-11]};
    % If Bode Editor is active, make sure it uses the full height
    if any(ActivePlotEditors==2)
        ActivePlotEditors = [ActivePlotEditors(ActivePlotEditors~=2),2];
    end
case 4
    XlabelVis = 'off';
    NewPos = {[Xs1 4.25 AxesW2 AxesH2] ; [Xs1 7.5+AxesH2 AxesW2 AxesH2] ;...
            [Xs2 4.25 AxesW2 AxesH2] ; [Xs2 7.5+AxesH2 AxesW2 AxesH2]};
end


% Reset positions of active editors and adjust label visibility
% RE: axes position is expected in normalized units!
for ct=1:NumActive,
    Editor = sisodb.PlotEditors(ActivePlotEditors(ct));
    % New position
    Editor.Position = NewPos{ct}./[FigW FigH FigW FigH];
    % Label visibility
    % REVISIT: collapse following 3 into 1
    Xlabel = Editor.Xlabel;
    Xlabel.Visible = XlabelVis;
    Editor.Xlabel = Xlabel;
    % Editor.Xlabel.Visible = XlabelVis;
    Ylabel = Editor.Ylabel;
    Ylabel.Visible = YlabelVis;
    Editor.Ylabel = Ylabel;
    % Editor.Ylabel.Visible = YlabelVis;
    % Make sure listener is triggered by first setting visibility off
    % (guarantees proper axes title is turned on in Bode plot)
    Title = Editor.Title;
    Title.Visible = 'off';  
    Editor.Title = Title;
    Title.Visible = 'on';
    Editor.Title = Title;
    % Editor.Title.Visible = 'on';
end




