function update_menus(fcn,hFig)
%UPDATE_MENUS   Updates the menus.
%
%   Inputs:
%     hFig - handle to the main gui figure
%     fcn  - string to be fevaled. This will indicate what main local
%            function to call within this function. The main local
%            functions are:
%            update_file - updates the file menu.
%            update_type - updates the type menu.
%            update_method - updates the method menu.
%            update_analysis - updates the analysis menu.

%   Author(s): R. Losada
%   Copyright 1988-2000 The MathWorks, Inc.
%   $Revision: 1.12 $  $Date: 2000/06/13 21:23:49 $ 

feval(fcn,hFig);

%-------------------------------------------------------------------------
function update_all(hFig);
% UPDATE_ALL Calls all the update functions to update the menus.

update_filter(hFig);   % Update the filter menu.
update_type(hFig);     % Update the type menu.
update_method(hFig);   % Update the method menu.
update_analysis(hFig); % Update the analysis menu

%-----------------------------------------------------------------------
%   **************       Main  Local Functions    ********************
%-----------------------------------------------------------------------

%------------------------------------------------------------------------
function update_filter(hFig)
%UPDATE_FILTER   Update the filter menu.
%
%   Inputs:
%     hFig - handle to the figure

% Get structure stored in figure's userdata.
ud = get(hFig,'userdata');

% Read the current parameters panel from ud.
mode = ud.mode.current;
switch mode,
case 1,
    panel = 'design';
case 2, 
    panel = 'specifycoef';
otherwise
    return;  % Only case 1 and case 2 are available in the menu!
end

% Cache array of handles to filter menu items.
h_filter = ud.h.menus.filter;

% Uncheck everything.
set(h_filter,'checked','off');

% Find handle of current parameters panel.
indx = get_str_indx(h_filter,panel);

% Check the current parameters panel.
set(h_filter(indx),'checked','on');

% Update the Undo Changes choice under the Filter menu.
update_undochanges(hFig);

%------------------------------------------------------------------------
function update_undochanges(hFig)
%UPDATE_UNDOCHANGES Update the Undo Changes menu choice under the 
%                   Filter menu.
% Inputs:
%   hFig - handle to the figure

ud = get(hFig,'Userdata');
mode = ud.mode.current;
h_menu = ud.h.menus.filter(3);
undo = ud.flags.undochanges;

switch mode,
case 1, % Design tab
    enable = undo.design;  % Cache state of Undo Changes.
    
case 2, % Import tab
    enable = undo.import; % Cache state of Undo Changes.
    
case 3, % Quantize tab
    if ud.flags.quantizationswitch,
        enable = undo.quantize; % Cache state of Undo Changes.
    else
        % Disable menu choice when entire quantize tab is disabled.
        enable = 0; 
    end
    
end

if enable,
    enableState = 'on';   % Changes have have been made.
else
    enableState = 'off';  % Changes have been committed (applied).
end
% Enable/disable Undo Changes menu choice under Filter.
set(h_menu,'Enable',enableState);

%------------------------------------------------------------------------
function update_type(hFig)
%UPDATE_TYPE   Update the type menu.
%
%   Inputs:
%     hFig - handle to the figure

% Get user data
ud = get(hFig,'userdata');

% Get handle to type menu
hndl = findobj(hFig,'tag','type');
indx = find(ud.h.menus.main == hndl);

% Get current mode from ud
switch ud.mode.current,
case {2,3},
    % Quantized mode, disable menu and return
    set(ud.h.menus.main(indx),'enable','off');
case 1,
    % Enable menu in case it was disabled previously
    set(ud.h.menus.main(indx),'enable','on');
end


% Cache array of handles to type menu
h_type = ud.h.menus.type;
h_other = ud.h.menus.other_type;


% Form an array of handles to all types
all_types = [h_type(:);h_other(:)];

% Read type from ud
current_type = ud.specs.design.type;

% Check the right type
check_type(all_types,current_type,hFig);

%------------------------------------------------------------------------
function update_method(hFig)
%UPDATE_METHOD   Update the method menu.
%
%   Inputs:
%     hFig - handle to the figure

% Get user data
ud = get(hFig,'userdata');

% Get handle to type menu
hndl = findobj(hFig,'tag','method');
indx = find(ud.h.menus.main == hndl);

% Get current mode from ud
switch ud.mode.current,
case {2,3},
    % Quantized mode, disable menu and return
    set(ud.h.menus.main(indx),'enable','off');
case 1,
    % Enable menu in case it was disabled previously
    set(ud.h.menus.main(indx),'enable','on');
end

% Cache handles to fir, iir and fir window methods
h_fir     = ud.h.menus.fir_methods;
h_iir     = ud.h.menus.iir_methods;
h_fir_win = ud.h.menus.fir_window_methods;

% Enable all methods and uncheck them all
all_methods = [ud.h.menus.method(:);h_fir(:);...
        h_iir(:);h_fir_win(:)];

set(all_methods,'enable','on','checked','off');

% Read type from ud
current_type = ud.specs.design.type;

% Disable methods depending on type
disable_methods(current_type,ud);

% Check the right method
check_method(current_type,hFig);

%------------------------------------------------------------------------
function update_analysis(hFig)
%UPDATE_ANALYSIS   Update the analysis menu.
%
%   Inputs:
%     hFig - handle to the figure

% Get user data
ud = get(hFig,'userdata');

% Cache array of handles to analysis items
h_analysis = ud.h.menus.analysis;

% Uncheck everything
set(h_analysis,'checked','off');

% Read the current analysis from ud
analysis = ud.toolbar.analysis;

% Find handle of current analysis
indx = get_str_indx(h_analysis,analysis);

% Check the current analysis
set(h_analysis(indx),'checked','on');


%-------------------------------------------------------------------------
%               Local functions to the main ones above
%--------------------------------------------------------------------------
function check_type(all_types,current_type,hFig)
%CHECK_TYPE   Check the curren type in ud
%
%   Inputs:
%     all_types - array of handles to all types
%     current_type - string with current type in ud
%     hFig - handle to the figure

% Uncheck everything
set(all_types,'checked','off');

% Get index of handle whose label is the current type
indx = get_str_indx(all_types,current_type);

% Check the type 
set(all_types(indx),'checked','on');

% Check its parent if type belongs to 'other'
parent = get(all_types(indx),'parent');
if parent ~= hFig,
    set(parent,'checked','on');
end

%--------------------------------------------------------------------------
function check_method(current_type,hFig)
%CHECK METHOD  Check the correct method.
%
%   Inputs:
%     current_type - current filter type in ud
%     ud - user data stored in figure
%     hFig - handle to the figure

% Get userdata from figure
ud = get(hFig,'userdata');

% Get method from ud
method = ud.specs.design.method;

% Cache handles to fir, iir and fir window methods
h_fir     = ud.h.menus.fir_methods;
h_iir     = ud.h.menus.iir_methods;

% Form array of all methods
all_methods = [h_fir(:);h_iir(:)];

% Get index of handle whose label is method
indx = get_str_indx(all_methods,method);

% Probe if method is enabled, and return a flag
is_enabled = probe_enabled(all_methods(indx));

if ~is_enabled
    % If current method is disabled select the default for given type.
    method = select_defaultmethod(current_type,hFig);
    
    % Get index of handle whose label is method
    indx = get_str_indx(all_methods,method);  
    
    % Save new method to ud
    ud.specs.design.method = method;
    set(hFig,'userdata',ud);
end

% Check the correct method and its parent
check_on(all_methods(indx),hFig);

%-------------------------------------------------------------------------
function is_enabled = probe_enabled(h_currentmethod)
%CHECK_IF_ENABLED   Probe if the input METHOD is enabled. 
%
%   Inputs:
%     h_currentmethod - handle to the current method.
%
%   Outputs:
%     is_enabled - flag: 1 if method is enabled, 0 otherwise.


% Probe if method is enabled
enabled = get(h_currentmethod,'enable');
switch enabled,
case 'on',
    is_enabled = 1;
       
case 'off',
    is_enabled = 0;
end

%-----------------------------------------------------------------------
function check_on(h_method,hFig)
%CHECK_ON   Set check of method given by its handle and its parent.
%
%   Inputs:
%     h_method - handle to current method
%     hFig - handle to the figure

set(h_method,'checked','on');
h_parent = get(h_method,'parent');
set(h_parent,'checked','on');

% Check window if new method is a window
if strcmp(get(h_method,'tag'),'fir1'),
    ud = get(hFig,'userdata');
    indx = get_str_indx(ud.h.menus.fir_window_methods,ud.specs.design.window);
    set(ud.h.menus.fir_window_methods(indx),'checked','on');
end

%----------------------------------------------------------------------
function disable_methods(current_type,ud)
%DISABLE_METHODS   Disable methods in the menu depending on current_type.
%
%   Inputs:
%     current_type - string containing the current filter type: lowpass, etc.
%     ud   - user data

% Get methods to disable
[disable_all,disable_iir,disable_fir] = methods2disable(current_type,ud);

switch disable_all,
case 'method',
    % Disable the entire method menu
    disable_items(ud.h.menus.main,{'method'});
    
case 'iir',
    % Disable the iir item in the method menu
    disable_items(ud.h.menus.method,{'iir'});
    
case 'fir',
    % Disable the fir item in the method menu
    disable_items(ud.h.menus.method,{'fir'});
    
end

disable_items(ud.h.menus.iir_methods,disable_iir);

disable_items(ud.h.menus.fir_methods,disable_fir);
      
%----------------------------------------------------------------------
function [disable_all,disable_iir,disable_fir] = methods2disable(current_type,ud)
%METHODS2DISABLE   Determine what methods to disable depending on current_type.
%
%   Inputs:
%     current_type - string. Lowpass, highpass, etc.
%
%   Outputs:
%     disable_all - string with many many methods to disable, e.g. 'iir'
%     disable_iir - cell array with iir methods to disable
%     disable_fir - cell array with fir methods to disable


fdqexists =  ud.flags.fdqexists;

% Initialize
disable_all = ''; % Assign when you want to disable many methods,
                  % e.g. all iir methods
disable_iir = {}; % Assign to individual iir methods to disable
disable_fir = {}; % Assign to individual fir methods to disable

% Overwrite methods to disable
switch current_type,
case {'lp','hp','bp','bs'},
    if fdqexists,
        disable_iir = {disable_iir{:},'iirlpnorm','iirlpnormc'};
    end
    
case {'diff','hilb','multiband'},
    disable_all = 'iir';
    disable_fir = {'fir1'};
    disable_iir = {'butter','cheb1','cheb2','ellip'};
    if fdqexists,
        disable_iir = {disable_iir{:},'iirlpnorm','iirlpnormc'};
    end
        
case 'arbitrarymag',
    disable_fir = {'fir1'};
    disable_iir = {'butter','cheb1','cheb2','ellip'};
    
case 'arbitrarygrp',
    disable_fir = {'fir1','remez','firls'};
    disable_all = 'fir';
    disable_iir = {'butter','cheb1','cheb2','ellip'};
    if fdqexists,
        disable_iir = {disable_iir{:},'iirlpnorm'};
    end

    
case 'sincx',
    disable_fir = {'fir1','firls'};
    disable_all = 'iir';
    disable_iir = {'butter','cheb1','cheb2','ellip'};
    
case 'raised',
    disable_fir = {'firls','remez'};
    disable_all = 'iir';
    disable_iir = {'butter','cheb1','cheb2','ellip','iirlpnorm','iirlpnormc'};
end

%----------------------------------------------------------------------
function disable_items(h,strs)
%DISABLE_ITEMS   Disable the items specified.
%
%   Inputs:
%
%     h - array of handles to items which contains the items to be disabled
%     strs - cell array of string corresponding to the items to be disabled

for n=1:length(strs),
   % Get index of handle whose tag is method
   indx = get_str_indx(h,strs{n});
   
   % Disable method
   set(h(indx),'enable','off');
end

%----------------------------------------------------------------------
function method = select_defaultmethod(current_type,hFig)
%SELECT_DEFAULT   Selects the default method depending on current type.
%
%   Inputs:
%    current_type - string containing the current type selcted.
%
%   Outputs:
%     method - string containing the default method depending on type.

% Get userdata from figure
ud = get(hFig,'userdata');

% Get the default method per type
switch current_type,
case {'lp','hp','bp','bs',...
        'diff','hilb',...
        'multiband','sincx'},
   method = 'remez';
   
case 'arbitrarymag',
    if ud.flags.fdqexists,
        method = 'iirlpnorm'; 
    else
        method = 'remez';
    end
    
case 'arbitrarygrp',
    method = 'iirlpnormc'; 
    
case 'matchedimp',
   method = 'prony';
     
case 'raised',
   method = 'window';
   
end

%----------------------------------------------------------------------
function indx = get_str_indx(h,str)
%GET_STR_INDX(H,STR)   Get index to the handle whose label equals STR.
%
%   Inputs:
%     h - array of handles to uicontrols.
%     str - string of label to matched.

strs = get(h,'tag');

indx = find(strcmpi(str,lower(strs)));

%----------------------------------------------------------------------

% [EOF] update_menus.m




    



