function h_analysis = render_analysisarea(hFig,h_adjFrame);
% RENDER_ANALYSISAREA Renders the frame where all the analysis plots and
%                     the filter coefficients will be displayed.
% 
% Inputs:
%   hFig       - handle to fdatool figure.
%   h_adjFrame - handle to the frame which is adjacent to the analysis frame
%                whose position we'll use to determine this frame's position.
% Outputs:
%   h_analysis - a structure containing all the handles to the uicontrols, axis,
%                etc., in the analysis area.

%   Author(s): P. Pacheco, P. Costa
%   Copyright 1988-2000 The MathWorks, Inc.
%   $Revision: 1.4 $  $Date: 2000/06/13 21:23:22 $ 

% Create a frame (using an axis) with a target axis inside where the
% analysis plots will render.
h_analysis = render_framewaxis(hFig,h_adjFrame);

% Render the uicontrols that will be used to display the filter measurements.
h_analysis.meas = render_measArea(hFig,h_analysis);

% Render the Filter Coefficient Display Area.
h_analysis.filtcoeff_eb = render_fcDispArea(h_analysis.ax(1));

%----------------------------------------------------------------------
function h_analysis = render_framewaxis(hFig,h_adjFrame);
% RENDER_FRAMEWAXIS Creates a frame (using an axis) with a target axis
%                   inside where the analysis plots will be displayed.
% Inputs:
%   hFig       - handle to fdatool figure.
%   h_adjFrame - handle to the frame which is adjacent to the analysis frame
%                whose position we'll use to determine this frame's position.
% Outputs:
%   h_analysis - a structure containing the handles to the frame, label,
%                and target axis in the analysis area.

% Uicontrol sizes and spaces.
sz = gui_sizes;

bgc = get(hFig,'Color');

% Get the figure's position and the position of an adjacent frame
% to determine this frame's position.
figPos = get(hFig,'Position');
pos = get(h_adjFrame,'Position');

% Position of the frame (axis).
x = pos(1)+pos(3)+sz.ffs;
y = figPos(4)-sz.fh1-sz.vffs+1;  % Add a pixel; y pos of an axis is different then a frame.
w = figPos(3) - x - sz.ffs;
h = sz.fh1-1;  % Remove the pixel added a couple of lines above.

% Create an axis to be used as the frame.
h_analysis.frame(1) = axes(...
   'Units','pixels',....
   'Position',[x y w h],...
   'Box','On',...
   'Color',bgc,...
   'Tag','analyisframeaxis',...
   'Visible','on',...
   'Xtick',[],...
   'Ytick',[],...
   'Xticklabel',[],...
   'Yticklabel',[]);

% Set axis units to normalize so that we can get free cheap resizing.
% NOTE: The function that sets all the units to normalized doesn't catch this axis
% because this axis' handlevisibilty is set to off to avoid zooming.
set(h_analysis.frame(1),'Units','Normalized');

% Create axis which will be the target for the analysis plots.
axPos  = [x+60 y+50 w-130 h-83];
h_analysis.ax(1) = axes(...
   'units','pixels',....
   'Position',axPos,...
   'Color',[1 1 1]*1,...
   'Tag','analysisaxis',...
   'Visible','on');

% Create a seond axis which will be used for PLOTYY cases.
h_analysis.ax(2) = axes(...
   'units','pixels',....
   'Position',axPos,...
   'Color',[1 1 1]*1,...
   'Tag','analysisaxis',...
   'Visible','on');

% Determine the width extent of the label for the frame.
frLabel = {'Filter Specifications'};
frLabelW = largestuiwidth(frLabel); % 95;

% Render the label.
frameLabelPos = [x+sz.lfs (y+h)-sz.lh/2 frLabelW sz.lh];
h_analysis.frame(2) = uicontrol(...
   'Style','Text',...
   'BackgroundColor',bgc,...
   'HorizontalAlignment','center', ... 
   'Position',frameLabelPos,...
   'Tag','analysisframelabel',...
   'String',frLabel);

% Don't allow zooming in frame (axis).
set(h_analysis.frame(1),'HandleVisibility','off');  

%----------------------------------------------------------------------
function h_meas = render_measArea(hFig,h_analysis)
%RENDER_MEASAREA Render the uicontrols that will display the filter
%                 measurements on the right edge of the analysis area.
% Inputs:
%   hFig       - handle to fdatool figure.
%   h_analysis - a structure containing the handles to the frame, label,
%                and target axis in the analysis area.
% Outputs:
%   h_meas     - a structure containing handles to all the uicontrols 
%                used to display the filter measurements.

% Cache the figure's background color and position.
bgc = get(hFig,'Color');

% Uicontrol sizes and spacing.
sz = gui_sizes;

% Using the y position of the frame determine how far up to go to start
% rendering the uicontrols from top to bottom.
target_axPos = get(h_analysis.ax(1),'Position');
yPos = target_axPos(2) + target_axPos(4) - 2*sz.uh;
xPos = target_axPos(1) + target_axPos(3) + 2*sz.hfus;

% Determine the extent of the title.
label = {'Measurements'};
uiWidth = largestuiwidth(label); % 73;

% Render title for the filter measurements area.
titlePos = [xPos yPos uiWidth sz.lh];
h_title = uicontrol(...
    'Style','Text',...
    'Tag','measurements',...
    'BackgroundColor',bgc,...
    'Position',titlePos,...
    'Visible','off',...
    'String',label);

% Determine the extent of the string label Order.
label = {'Order:'};
uiWidth = largestuiwidth(label); % 33; 

% Set up properties for the uicontrol that will display the filter order.
labelProps = {...
        'Style','Text',...
        'BackgroundColor',bgc,...
        'HorizontalAlignment','Left',...
        'Tag','measurements',...
        'Visible','off'};

% Render the uicontrol to display the filter order.
orderPos = [xPos titlePos(2)-1.2*sz.uh uiWidth sz.uh];
order.lbl = uicontrol(labelProps{:},...
    'Position',orderPos,...
    'String','Order:');

order.value = uicontrol(labelProps{:},...
    'Position',orderPos+[uiWidth 0 0 0],...
    'String','-');

% Render the remaining uicontrols.  Render the uicontrols for all 
% filter types with visibility off.
h_meas = render_measArea_uis(hFig,xPos,orderPos(2));

% Add the handles o structure.
h_meas.title = h_title;
h_meas.order = order;

%----------------------------------------------------------------------
function h_meas = render_measArea_uis(hFig,xPos,yPos)
% RENDER_MEASAREA_UIS Render the uicontrols in the measurements area.
%                     This function renders the uis for all the filter
%                     types and sets one type (lowpass) vibility on.
% Inputs:
%   hFig    - handle to fdatool figure.
%   xPos    - horizontal starting position.
%   yPos    - vertical starting position.
%
% Outputs:
%   h_meas  - a structure containing handles to all the uicontrols 
%             used to display the filter measurements.

% Get the labels used in the specs frames so that we can use the same 
% labels to display the filter measurements.

% Magnitude               
mag.lp.labels = {'test','test'}; 
mag.hp.labels = {'test','test'}; 
mag.bp.labels = {'test','test','test','test'}; 
mag.bs.labels = {'test','test','test','test'}; 

% Determine the widest string used in displaying filter measurements.
labelStrs = {...
        mag.lp.labels{:},...
        mag.hp.labels{:},...
		mag.bp.labels{:},...
        mag.bs.labels{:},...
        'Order:'};
uiWidth = largestuiwidth(labelStrs);  % 33;

% Render the uicontrols to display the filter magnitude measurements 
% for all the filter types.

% Lowpass
h_meas.mag = measArea_maguis(hFig,xPos,yPos,mag.lp.labels,'lp',uiWidth);

% Highpass
h_meas.mag = measArea_maguis(hFig,xPos,yPos,mag.hp.labels,'hp',uiWidth);

% Bandpass
h_meas.mag = measArea_maguis(hFig,xPos,yPos,mag.bp.labels,'bp',uiWidth);

% Bandstop
h_meas.mag = measArea_maguis(hFig,xPos,yPos,mag.bs.labels,'bs',uiWidth);

%----------------------------------------------------------------------
function h_magAtten = measArea_maguis(hFig,xPos,yPos,labels,tag,uiWidth)
% MEASAREA_MAGUIS Renders the magnitude measurements uicontrols in the 
%                 filter measurements area.
%
% Inputs:
%   hFig    - handle to fdatool figure.
%   xPos    - horizontal starting position.
%   yPos    - vertical starting position.
%   labels  - strings to be used as the labels for the filter measurements.
%   tag     - piece of the string used for the tag of the uicontrols - this
%             piece will help differentiate the filter type being measured.
%   uiWidth - largest width of all the strings that will be used as a label
%             in the filter measurements area.
% Outputs:
%   magAtten - a structure containing the handles to the uicontrols that 
%              will display the filter measurements.

% Cache the figure's background color and position.
bgc = get(hFig,'Color');

% Uicontrol sizes and spacing.
sz = gui_sizes;

% Set up the properties to be used by all the uicontrols.
labelProps = {...
        'Style','Text',...
        'BackgroundColor',bgc,...
        'HorizontalAlignment','Left',...
        'Tag',[tag,'_measurements'],...
        'Visible','off'};

for k = 1:length(labels),
    magAttenPos{k} = [xPos yPos-k*sz.uh uiWidth sz.uh];
    h_magAtten.lbl{k} = uicontrol(labelProps{:},...
        'Position',magAttenPos{k},...
        'String',labels{k});
    
%     h_magAtten.value{k} = uicontrol(labelProps{:},...
%         'Position',magAttenPos{k}+[uiWidth 0 0 0],...
%         'String','-');
end

%----------------------------------------------------------------------
function h_eb = render_fcDispArea(h_ax)
%RENDER_FCDISPAREA  Render the Filter Coefficient Display Area 
%                   (Edit box)
%  Input:
%      h_ax  - Handle to the analysis area target axes.
%
%  Output:
%      h_eb  - Handle to the Filter Coefficient Display area.
%              

% Get Analysis axes position
axesPos = get(h_ax,'Position');

% Render the Filter Coefficient Display area (Edit box)
h_eb = uicontrol('Style','Listbox',...
    'BackgroundColor','White',...
    'Position',axesPos,...
    'Enable','On',...
    'Visible','Off',...
    'Max',2,...
    'String','',...
    'Tag','analysis_viewcoeffs',...
    'HorizontalAlignment','Left',...
    'FontName','FixedWidth',...
    'FontSize',12);  % Fixed-width can't go smaller than 12pt


% [EOF] render_analysisarea.m
