function h_modebtns = render_modebtns(hFig,fdqexists)
% RENDER_MODEBTNS Renders the "toolbar" (frame) and the mode buttons, such
%                 as Import Mode, Design Mode, and Quantize Mode.
% Input:
%   hFig - handle to fdatool's figure.
%
% Output:
%   h_modebtns - handles to the frame and the pushbutton uicontrols.

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

% Cache figure's position.
figPos = get(hFig,'Position');

% Uicontrol sizes and spaces.
sz = gui_sizes;

% Render frame.
x = 0;
w = figPos(3)+2;
h = sz.bh;
y = figPos(4)-sz.fh1-1.6*sz.vffs-sz.uh;
bgColor = [1 1 1]*.75;
% framewlabel returns a two element vector containing the handles 
% to the frame and its label.
h_frame = framewlabel(hFig,[x y w h],'','modebtns',bgColor);
set(h_frame(1),'ForegroundColor',[1 1 1]*.6);

% Render buttons... return handles to the buttons.
[h_impbtn,h_desbtn,h_quantbtn]=mode_btns(hFig,h_frame,bgColor,fdqexists);

h_modebtns.frame     = h_frame;
h_modebtns.importbtn = h_impbtn;
h_modebtns.designbtn = h_desbtn;
h_modebtns.quantizebtn = h_quantbtn;

%----------------------------------------------------------------------
function [h_importbtn,h_designbtn,h_quantizebtn] = ...
                            mode_btns(hFig,h_frame,frColor,fdqexists)

% Cache frame position
frPos = get(h_frame,'Position');

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

% Uicontrol sizes and spaces.
sz = gui_sizes;

% Mode (tab) buttons labels.
btnStrs = {...
        sprintf('Design Filter'),...
        sprintf('Import Filter'),...
        sprintf('Set Quantization Parameters')};

tagStrs = {...
        'design',...
        'specifycoef',...
        'quantize'};

for k=1:length(btnStrs),
    uiWidth(k) = largestuiwidth(btnStrs(k),'pushbutton')+36*sz.pixf; % Add fudge factor bc
end                                                                  % they'll be bolded

% Render all three mode buttons (tabs).
designbtnPos = [frPos(1)+sz.ffs frPos(2) uiWidth(1) sz.bh];
cbStr = {@modebtns_cbs,1};
h_designbtn = tab_btn(designbtnPos,cbStr,btnStrs{1},tagStrs{1},bgc,'on');

% Use the same pos since only one will appear.
importbtnPos = designbtnPos;
cbStr = {@modebtns_cbs,2};
h_importbtn = tab_btn(importbtnPos,cbStr,btnStrs{2},tagStrs{2},bgc,'off');

% Render the quantization parameters tab only if the FDQ toolbox is installed.
if fdqexists,
    quantizebtnPos = [designbtnPos(1)+uiWidth(1) designbtnPos(2) uiWidth(3) designbtnPos(4)];
    cbStr = {@modebtns_cbs,3};
    h_quantizebtn = tab_btn(quantizebtnPos,cbStr,btnStrs{3},tagStrs{3},bgc,'off');
else
    h_quantizebtn = [];
end

%-------------------------------------------------------------------------
function h_btn = tab_btn(pos,cbStr,label,tag,bgc,state)
%TAB_BTN Renders each of the tab buttons (faked with pushbuttons).
%
% Inputs:
%   pos     - position to render the button.
%   cbStr   - the callback string.
%   label   - the label on the pushbutton
%   tag     - the uicontrol tag property string.
%   state   - string indicating if the tab is selected (on) or not (off).
%
% Output:
%   h_btn - handle to the pushbutton.

% Render pusbutton (tab)
h_btn = uicontrol(...
    'Style','pushbutton',...
    'BackgroundColor','default',...
    'HorizontalAlignment','Left',...
    'Position',pos,...
    'Tag',tag,...
    'String',label,...
    'Callback',cbStr);

% Create the effect of a tab by connecting the button to the frame.
connectbtn2frame(h_btn,bgc,state);

%-------------------------------------------------------------------------
function connectbtn2frame(h_btn,frColor,visState);
%CONNECTBTN2FRMAME Place an edit box below the pushbutton with a slight 
%                  overlapp to produce the illusion of a tab.

% GUI sizes and spacing.
sz = gui_sizes;

% Pushbutton's position.
btnPos = get(h_btn,'Position');

% Determine if the button should be rendered in the ON (selected) position.
if strmatch(visState, 'on'), 
    font = 'Bold';
    btnColor = frColor;
else
    font = 'Normal';
    btnColor = 'default';
end
set(h_btn,'backgroundcolor',btnColor,'Fontweight',font);

stripPos = btnPos+[1 -6 -3 -11]*sz.pixf;
uiud.hTxt = uicontrol(...
    'style','text',...           % Use an edit box for the strip.
    'Visible',visState,...
    'backgroundcolor',btnColor,...
    'position',stripPos);

% Save the handle to the edit box in the pushbutton's userdata.
set(h_btn,'Userdata',uiud);

% [EOF] render_modebtns.m
