function update_analysisframe(hFig,update_staticresp)
% UPDATE_ANALYSISFRAME Updates the plotting area and calls appropriate
%                      update functions to update relevant parts of the GUI.
% Inputs:
%   hFig              - handle to the GUI's figure.
%   update_staticresp - Optional boolean flag used to update the static resp
%                       ONLY. Used in the case when switching filter types or 
%                       methods.

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

% Cache figure's userdata.
ud = get(hFig,'Userdata');

if nargin > 1 & update_staticresp...
        & ~strcmp(ud.toolbar.analysis,'filterresponsespecs'),
    % Don't update the plot if Filter Specifications (static response), 
    % is not the current analysis!
    return;
end

% Don't show static response in certain modes.
set_defaultanalysis(hFig);

% Prepare the analysis axes properties and the frame label.
prepare_frame(hFig);

% Update the plot with the the current analysis. This also 
% updated the static response when a different filter types are chosen.
update_plot(hFig);

%-------------------------------------------------------------------------
function set_defaultanalysis(hFig)
% SET_DEFAULTANALYSIS Set the default analysis based on current mode.
%
%  Input:
%  hFig - Handle to the Filter Design GUI figure.

% Get the userdata structure
ud = get(hFig,'Userdata');

% Get the current mode and analysis; 
% FDATool can be in one of the following three modes:
%    1 - Filter Design Mode [default]
%    2 - Import Mode
%    3 - Quantize Mode
currentmode = ud.mode.current;
currentAnalysis = ud.toolbar.analysis;

% Magnitude Response is the default analysis for both
% Specify and Quantize modes.
if strcmpi(currentAnalysis,'filterresponsespecs') & ...
        any(currentmode == [2 3]),
    
    ud.toolbar.analysis = 'magresp';
    set(hFig,'UserData',ud);

    % Update static resp toolbar icon and the analysis menu selection.
    update_toolbar('update_filterspecenable',hFig);
    update_menus('update_analysis',hFig);
end

%-------------------------------------------------------------------------
function prepare_frame(hFig)
% PREPARE_FRAME Prepare the analysis frame axis and frame label.
%
% Input:
%  hFig            - Handle to the Filter Design GUI figure.

% Reset the Target axes
reset_targetaxes(hFig);

% Update the analysis area frame label
update_analysisframelbl(hFig); 

%-------------------------------------------------------------------------
function reset_targetaxes(hFig)
% RESET_TARGETSAXES Reset the Target axes portion of the analysis area.
%
%  Input:
%    hFig   - Handle to the Filter Designer GUI 

% Get the userdata structure
ud = get(hFig,'Userdata');

% Handle to the target axes
hax = ud.h.analysis.ax;

% Make the axis handle available and reset it; clean it up.
hvis = get(hFig,'HandleVisibility');
set(hFig,'HandleVisibility','on');

% Make hFig the current figure.
figure(hFig);
set(hFig,'CurrentAxes',hax(2)); cla reset;
set(hFig,'CurrentAxes',hax(1)); cla reset;

% Reset the showhiddenhandles propety.
set(hFig,'HandleVisibility',hvis);

% Turn the Visibilty of the Target axes and
% Filter Coefficients Edit box to 'Off'
h_analysisarea = [hax,ud.h.analysis.filtcoeff_eb];
set(h_analysisarea,'Visible','Off');

%-------------------------------------------------------------------------
function update_analysisframelbl(hFig)
% UPDATE_ANALYSISFRLBL Update the analysis area frame label
%                     based on UD (ud.toolbar.analysis)
%
%  Inputs:
%    hFig   - Handle to the Filter Designer GUI 
%    hFrame - Handles to the analysis area frames 

% Uicontrol sizes and spaces
sz = gui_sizes;

% Get the userdata structure.
ud = get(hFig,'Userdata');
hFrame = ud.h.analysis.frame;

% Turn the previous label visibility to 'Off'
set(hFrame(2),'Visible','Off');

% Get the new frame label and determine its width
dispStr = getFrameLblStr(ud.toolbar.analysis);
frLblW = largestuiwidth({dispStr}); % Pixels units

% Temporarily change the frame label units to Pixels 
% so that frLblW is unit compatible
frLblUnits = get(hFrame(2),'Units'); % Normalized units
set(hFrame(2),'Units','Pixels');

% New frame label position based on X and Y of old label
frLblpos = get(hFrame(2),'Position');
frameLblPos = [frLblpos(1) frLblpos(2) frLblW sz.lh];

% Set the frame properties
set(hFrame(2),'String',dispStr,...
    'Visible','On',...
    'Position',frameLblPos);

% Set units back to previous state
set(hFrame(2),'Units',frLblUnits);

%-------------------------------------------------------------------------
function dispStr = getFrameLblStr(analysisplotTag)
% GETFRAMELBLSTR Get the analysis area frame label string.
%
%  Input:
%    analysisplotTag  - Analysis plot tag.
%                       
%  Output:
%    dispStr    - Frame label display string. 

switch analysisplotTag,
case {'filterresponsespecs'}
    dispStr = 'Filter Specifications';
    
case{'magresp'}
    dispStr = 'Magnitude Response';
    
case{'phaseresp'}
    dispStr = 'Phase Response';
    
case{'magnphaseresp'}
    dispStr = 'Magnitude and Phase Response';
    
case{'groupdelay'}
    dispStr = 'Group Delay';
    
case{'impresp'}
    dispStr = 'Impulse Response';
    
case{'stepresp'}
    dispStr = 'Step Response';
    
case{'pzplot'}
    dispStr = 'Pole/Zero Plot';
    
case{'filtercoeffs'}
    dispStr = 'Filter Coefficients';
end

% [EOF] update_analysisframe.m
