function update_modebtns(hFig,varargin)
% UPDATE_MODEBTNS Updates the appropriate tab depending on the new mode.
% 
% Inputs:
%   hFig     - handle to the GUI's figure.
%   varargin - an optional string, 'initialize', indicating to update the
%              tabs regardless if the new tab is the same as the old tab.

%   Author(s): P. Pacheco
%   Copyright 1988-2000 The MathWorks, Inc.
%   $Revision: 1.11 $  $Date: 2000/07/24 20:59:34 $ 

setptr(hFig,'watch');                 % Put up watch cursor.

% Get the data structure stored in the figure's userdata.
ud = get(hFig,'Userdata');
newmode = ud.mode.current;  % Cache new mode.
oldmode = ud.mode.old;      % Cache old mode.

if ( nargin>1 & strcmpi(varargin{1},'initialize') ) | newmode~=oldmode,
    % Update tabs during tool initialization & when a new mode is selected.
    update_tabs(hFig,newmode,oldmode); 
end
setptr(hFig,'arrow');                 % Reset cursor.

%----------------------------------------------------------------------
function update_tabs(hFig,newmode,oldmode)
%UPDATE_TABS Updates all the tabs.
%
% Inputs:
%   hFig    - handle to GUI's figure.
%   oldmode - previously selected mode (tab).
%   newmode - newly selected mode (tab).

% Make sure old & new modes are valid, otherwise set them to defaults.
[newmode,oldmode,validmodes] = validate_modes(hFig,newmode,oldmode);

% Make unselected tabs invisible.
hide_oldmodes(hFig,oldmode,newmode,validmodes);

% Set old mode btn (just the button not the entire tab) visible.
show_unselected_modebtn(hFig,newmode,oldmode);

% Show entire newly selected tab (mode).
show_newmode(hFig,newmode);

%-------------------------------------------------------------------------
function [newmode,oldmode,validmodes] = validate_modes(hFig,newmode,oldmode)
% VALIDATE_MODES Verifies and updates if necessary, the new and old
%                modes when loading a session. Validity is based on
%                what toolboxes are installed.
% Inputs:
%   hFig       - handle to GUI's figure.
%   oldmode    - previously selected mode (tab).
%   newmode    - newly selected mode (tab).
%
% Outputs:
%   validmodes - indexes of valid modes; depends on tbxes installed.
%                    1 = Design; 2 = Import; 3 = Quantize
%   oldmode    - previously selected mode based on what tbx is installed.
%   newmode    - newly selected mode, based on what tbx is installed.

% Cache GUI's data structure.
ud = get(hFig,'Userdata');

% Define valid modes. 
validmodes = [1 2 3];  % Assuming FD tbx is installed.

if ~ud.flags.fdqexists % Quantize tab is not available.
    validmodes = [1 2];  
    if ~any(newmode==validmodes),
        % Can't be on the quantize tab if the toolbox doesn't exist!
        % This can happen when loading a session that was saved while
        % FD tbx was installed.
        newmode = validmodes(1); 
    end
    if ~any(oldmode==[1 2]),
        oldmode = validmodes(1); 
    end
end

% Make sure that one of the other two tabs, Design or Import, 
% show up with the quantize tab.
if all([oldmode newmode]==3),  
    % This will only happen when the modes are set manually in ud.
    oldmode = validmodes(1); 
end

%----------------------------------------------------------------------
function hide_oldmodes(hFig,oldmode,newmode,validmodes)
% HIDE_OLDMODES Hides all but the currently selected tab.
%
% Inputs:
%   hFig       - handle to GUI's figure.
%   oldmode    - previously selected mode (tab).
%   newmode    - newly selected mode (tab).
%   validmodes - indexes of valid modes; depends on tbxes installed.
%                    1 = Design; 2 = Import; 3 = Quantize

% Get the user data stored in the figure.
ud = get(hFig,'Userdata');

% Determine which tabs to set invisible.
indx = find(validmodes~=newmode); % Find all but newly selected tab.

% Set the unselected mode buttons invisible.
h_btns=[ud.h.modeFr.designbtn,...
        ud.h.modeFr.importbtn,...
        ud.h.modeFr.quantizebtn];
set(h_btns(indx),'Visible','off');

% Set the tabs that are not selected invisible.
modeStr={'design_params',...
        'import_params',...
        'quantize_params'};
for n = 1:length(indx),
    disconnectbtnFromframe(h_btns(indx(n)));      % Undo tab effect.
    set_tab_visibility(hFig,modeStr{indx(n)},'off');
end

%----------------------------------------------------------------------
function show_newmode(hFig,newmode)
% SHOW_NEWMODE Shows the new tab: Design, Import or Quantize.
%
% Inputs:
%   hFig    - handle to GUI's figure.
%   newmode - newly selected mode (tab).

% Show the newly selected tab (mode).  Returns a string, corresponding
% to the tab, to update the status box
statusStr = show_newtab(hFig,newmode);

%
% Update all other relevant parts of the GUI
%

% Update the menus.
update_menus('update_all',hFig);

% Update analysis area if static response is current analysis.
update_staticresp = 1;
update_analysisframe(hFig,update_staticresp); 

% Update the state of the Filter Specifications icon 
update_toolbar('update_all',hFig);

% Update the action button.
update_actionbtn('update_all',hFig);

% Update the status box.
update_status(hFig,statusStr);    

%-------------------------------------------------------------------------
function show_unselected_modebtn(hFig,newmode,oldmode)
% SHOW_UNSELECTED_MODEBTN Makes visible the mode button of the unselected
%                         tab.
% Inputs:
%   hFig    - handle to GUI's figure.
%   oldmode - previously selected mode (tab).
%   newmode - newly selected mode (tab).

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

% Make the mode button of the unselected tabs visible.
switch oldmode,
case 1,    oldmodeBtn = 'designbtn';
case 2,    oldmodeBtn = 'importbtn';
case 3,    oldmodeBtn = 'quantizebtn';
end
if ~( any(newmode==[1 2]) & any(oldmode==[1 2]) ),
    % Show one btn when both old & new modes are one of Design or Import.
    % This is because both buttons (Design & Import) share the same pos.
    set(getfield(ud.h.modeFr,oldmodeBtn),'Visible','on');
end
if ud.flags.fdqexists, % This tab is always available if tbx is installed.
    set(ud.h.modeFr.quantizebtn,'Visible','on');
end

%-------------------------------------------------------------------------
function statusStr = show_newtab(hFig,newmode)
% SHOW_NEWTAB Makes the newly selected tab (mode) visible.  This includes
%             setting the mode buttons, the frame and its contents.
%
% Inputs:
%   hFig    - GUI's figure handle.
%   newmode - currently selected mode (tab).
%
% Outputs:
%   statusStr - a string for the status text.

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

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

% Set new parameters panel (mode) visible.
switch newmode
    
case 1, % Design mode was selected.
    h_btn = ud.h.modeFr.designbtn;
    statusStr = update_designtab(hFig);
    
case 2, % Import mode was selected.
    h_btn = ud.h.modeFr.importbtn;
    statusStr = update_importtab(hFig);
    
case 3, % Quantize mode was selected.
    h_btn = ud.h.modeFr.quantizebtn;
    statusStr = update_quantizetab(hFig);  % Could potentially modify ud!
    
end
% Create the effect of a tab by connecting the pushbutton to the frame.
connectbtn2frame(h_btn,bgc);

%-------------------------------------------------------------------------
function set_tab_visibility(hFig,tag,state)
% SET_TAB_VISIBILITY Sets the visibiliy of the current panel (mode).
% 
% Inputs:
%   hFig  - handle to the GUI's figure.
%   tag   - string of the tag used for the uicontrols in that tab.
%   state - visibility state.

% Flat vector of the uicontrol handles in the tab.
h_Alluis = findobj(hFig,'Tag',tag);
set(h_Alluis,'Visible',state);

%----------------------------------------------------------------------
function statusStr = update_designtab(hFig)
% Update the Design Filter parameters tab.
%
% Inputs:
%   hFig - handle to the GUI's figure.
%
% Outputs:
%   statusStr - a string containing the status text.

% Update the Design parameters tab and set it visible.
update_designparams('update_all',hFig);
update_specs('update_all',hFig);

% Update the status string.
statusStr = sprintf(['Specify filter design parameters.']);

%----------------------------------------------------------------------
function statusStr = update_importtab(hFig)
% Update the Import Filter tab.
%
% Inputs:
%   hFig - handle to the GUI's figure.
%
% Outputs:
%   statusStr - a string containing the status text.

% Update the Import tab and set it visible.
update_importparams('update_all',hFig);

% Update the status string.
statusStr = sprintf(['Specify filter coefficients directly ', ...
        'or variables defined in the MATLAB workspace.']);

%----------------------------------------------------------------------
function statusStr = update_quantizetab(hFig)
% Update the Set Quantization Parameters tab.
%
% Inputs:
%   hFig - handle to the GUI's figure.
%
% Outputs:
%   statusStr - a string containing the status text.

ud = get(hFig,'Userdata');

if ud.flags.fdqexists,  % Filter Design Toolbox exists.
    % Update the Quantize parameters tab and set it visible.
    update_quantizeparams('update_all',hFig);
    
    % Update the status string.
    if ud.flags.quantizationswitch,
        statusStr = ['Specify quantization parameters.'];
    else    
        chkboxLabel = get(ud.h.qswitch.quantizeSwitch,'String');
        statusStr = ['Select "',chkboxLabel{:},...
                '" to apply these quantization parameters.'];
    end
    
else
    statusStr = 'Ready';
    % This is the case where you have loaded a session that was 
    % saved with mode(tab) 3 as the default, but now fdatool is being
    % launched with the Filter Design Tbx not installed.
    warnmsg = ['Filter Design Toolbox is not available. Reverting back to Filter Design (tab).'];
    lastwarn(warnmsg);
    fdatoolwarning(hFig);
    ud.mode.current = 1;
    set(hFig,'UserData',ud);
    
    % Start from the beginning again!
    update_gui(hFig);
    return;
end

%-------------------------------------------------------------------------
function connectbtn2frame(h_btn,frColor);
% CONNECTBTN2FRAME Updates the position and color of the text UI (a strip)
%                  so that it connects the pushbutton with the frame to 
%                  produce the illusion that it's a tab. 
% Inputs:
%   h_btn   - pushbutton's handle.
%   frColor - color of the frame; to be used by the pushbutton.


% Get the GUI sizes and spacing.
sz = gui_sizes;

% Set the button color the same as the frame and make the font bold 
% (indicating it's selected).  Also, make it visible in case it wasn't.
set(h_btn,'backgroundcolor',frColor,'fontweight','bold','Visible','On');

% Get the handle to the text UI that is used to connect the
% pusbutton and the frame (to form a tab).
uiud = get(h_btn,'Userdata');

% Cache the button's and text box's old units.
pboldUnits = get(h_btn,'Units');
tboldUnits = get(uiud.hTxt,'Units');

% Set the units of both UIs to pixels.
set([h_btn,uiud.hTxt],'Units','Pixels');

% Using the pushbutton's position, position the strip so that 
% it connects the button & the frame.
btnPos = get(h_btn,'Position');
stripPos = btnPos+[1 -6 -3 -11]*sz.pixf; % Slightly shorter & lower

% Update the strip's (text box) position, etc. so that it looks like a tab.
set(uiud.hTxt,...
    'Backgroundcolor',frColor,...
    'Position',stripPos,... 
    'Visible','on');

% Reset the units back to original.
set(h_btn,'Units',pboldUnits);
set(uiud.hTxt,'Units',tboldUnits);

%-------------------------------------------------------------------------
function disconnectbtnFromframe(h_btn);
% DISCONNECTBTNFRAME Disconnects the pushbutton from the frame - udoing 
%                    the tab effect.
% Inputs:
%   h_btn - handle to the pushbutton.

btnPos = get(h_btn,'Position');
set(h_btn,'backgroundcolor','default','fontweight','normal');

uiud = get(h_btn,'Userdata');
if isfield(uiud,'hTxt') & ishandle(uiud.hTxt),
    set(uiud.hTxt,'Visible','off');
end

% [EOF] udpate_modebtns.m
