function h = render_importparams(hFig,ud,h_filtTypeFr)
%RENDER_IMPORTPARAMS Renders all the frames and uicontrols for the 
%                    import filter tab.
% Inputs:
%   hFig         - Handle to the Filter Design GUI figure.
%   ud           - UserData structure.
%   h_filtTypeFr - Handle to the Filter Type frame (Design Mode).

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

% Render the "Specify Filter Coefficients" frame, the Filter 
% Structure specific labels, and the coefficient edit boxes.
h.importFilter = coeffs_frame(hFig, ud, h_filtTypeFr);

% Render the Sampling Frequency uicontrols 
h.importFs = samplingFreqUis(hFig,h.importFilter.frame(1), h.importFilter.clrpbs(1));


%----------------------------------------------------------------------
function importFilter = coeffs_frame(hFig, ud, h_filtTypeFr)
%COEFFS_FRAME Render the Specify Filter Coefficients frame.
%
% Inputs:
%   hFig         - Handle to the Filter Design GUI figure.
%   ud           - Userdata structure.
%   h_filtTypeFr - Handle to the Filter Type frame.
%
% Output:
%  importFilter    - Structure containing the handles to the 
%                    specified filter coefficients..

% Uicontrol sizes and spaces
sz = gui_sizes;

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

% Get the Filter Type frame position
filtTypePos = get(h_filtTypeFr,'Position');

% Render the Specify Filter Coefficients frame
xpos = filtTypePos(1);
ypos = filtTypePos(2);
hgt = filtTypePos(4);
width = figPos(3)-(2.5*sz.ffs);
framePos = [xpos ypos width hgt];
frame = framewlabel(hFig,framePos,'Filter Coefficients',...
    'import_params',bgc);

% Keep frame invisible until "Specify Filter Coefficients" is selected.
set(frame, 'Visible','Off'); 

% Render the Filter Structure label and popup
[h_fsPopup, h_fsLbl, fsPopPos] = render_fsuis(ud,framePos,sz,bgc);

% Render the Edit boxes where the coefficients will be specified.
h_ebs = importEditBoxes(hFig,framePos,fsPopPos);

% Render the "Transfer Function" labels
tfLblStrs = {'Numerator:','Denominator:'};
h_tflbls = importLabels(hFig,framePos,fsPopPos,tfLblStrs);

% Render the "Second-order sections" labels
sosLblStrs = {'SOS matrix:','Gain:'};
h_soslbls = importLabels(hFig,framePos,fsPopPos,sosLblStrs);

% Render the "State-space" labels
ssLblStrs = {'A:','B:','C:','D:'};
h_sslbls = importLabels(hFig,framePos,fsPopPos,ssLblStrs);

% Render the "Lattice allpass" labels
lattLblStrs = {'Lattice coeff:','Ladder coeff:'};
h_lattAPSlbls = importLabels(hFig,framePos,fsPopPos,lattLblStrs);

% Render the "Lattice MA (min. phase)" labels
h_lattMAMinlbls = importLabels(hFig,framePos,fsPopPos,lattLblStrs);

% Render the "Lattice MA (max. phase)" labels
h_lattMAMaxlbls = importLabels(hFig,framePos,fsPopPos,lattLblStrs);

% Render the "Lattice (ARMA)" labels
h_lattARMAlbls = importLabels(hFig,framePos,fsPopPos,lattLblStrs);

% Render the Quantized Filter labels only if the FD Toolbox is installed
if ud.flags.fdqexists,
    qfiltLblStr = {'Quantized Filter:'};
    h_qfiltlbls = importLabels(hFig,framePos,fsPopPos,qfiltLblStr);
end

% Render the "Clear" push buttons for reseting the coefficients edit boxes.
h_clrPbs = importClearPushBtns(hFig,framePos,fsPopPos,h_ebs);

% Store filter structure handles back in UD
importFilter.frame           = frame;
importFilter.pop             = h_fsPopup;
importFilter.popLbl          = h_fsLbl;
importFilter.coeffebs        = h_ebs;
importFilter.tflbls          = h_tflbls;
importFilter.soslbls         = h_soslbls;
importFilter.sslbls          = h_sslbls;
importFilter.lattarmalbls    = h_lattARMAlbls;
importFilter.lattapslbls     = h_lattAPSlbls;
importFilter.lattmaminlbls   = h_lattMAMinlbls;
importFilter.lattmamaxlbls   = h_lattMAMaxlbls;
importFilter.clrpbs          = h_clrPbs;
if ud.flags.fdqexists,
    importFilter.qfiltlbls   = h_qfiltlbls;
end



%----------------------------------------------------------------------
function [h_fsPopup, h_fsLbl, fsPopPos] = render_fsuis(ud,framePos,sz,bgc);
%RENDER_FSUIS Render the Filter Structure uicontrols
%
% Inputs:
%  ud       - Figure's UserData structure.
%  framePos - Specify Filter Coefficients frame position
%  sz       - Uicontrol sizes and spaces structure
%  bgc      - Figure's background color
%
% Outputs:
%  h_fsPopup  - Handle to the Filter Structure popupmenu
%  h_fsLbl    - Handle to the Filter Structure label
%  fsPopPos   - Filter Structure popupmenu position


% Filter Structure options for the popupmenu.
fsStrs = importstructurestr(ud);

fsUiWdth = largestuiwidth(fsStrs,'popup'); 
fsLblWdth = largestuiwidth({'Filter Structure:'},'text');
frameUpperEdge = framePos(2)+framePos(4);
fsLblPos = [framePos(1)+sz.lfs frameUpperEdge-40 fsLblWdth sz.uh];
fsPopPos = [framePos(1)+sz.lfs fsLblPos(2)-sz.uh fsUiWdth sz.uh];

% Render the Filter Structure label
h_fsLbl = uicontrol('Style','text',...
    'BackGroundColor',bgc,...
    'Position',fsLblPos,...
    'String','Filter Structure:',...
    'Visible','Off',...
    'Tag','import_params');

% NOTE: The importparams_cbs function updates the figure's userdata.
importparamscbs = importparams_cbs;
cbStr = importparamscbs.impFiltStruct_cbs;

% Render the Filter Structure popupmenu
h_fsPopup = uicontrol('Style','Popup',...
    'BackgroundColor','White',...
    'Position',fsPopPos,...
    'Visible','Off',...
    'String',fsStrs,...
    'Tag','import_params',...
    'Callback',cbStr);


%----------------------------------------------------------------------
function h_ebs = importEditBoxes(hFig,framePos,fsPopPos)
% IMPORTEDITBOXES Render the Filter Coefficients edit boxes.
%
% Inputs:
%   hFig      - Handle to the Filter Design GUI figure.
%   framePos  - Specify Filter Coefficients frame position.
%   fsPopPos  - Filter Structure popupmenu position.
%
% Outputs:
%  h_ebs   - Handles to the Filter coefficient edit boxes.

% Cache uicontrol sizes and figure's background color
sz = gui_sizes;
bgc = get(hFig,'Color');

% NOTE: The importparams_cbs function updates the figure's userdata
% This callback string is for all Import Coefficients Edit Boxes.
importparamscbs = importparams_cbs;
cbStr     = importparamscbs.importcoeff_eb;
clr_cbStr = importparamscbs.clearpush_cb;

% Filter Coefficient Edit Box width
uiebWidth = 175;

% Since the largest edit box label is about 80, we added 90 to X to 
% keep the X location fixed which keeps the edit boxes from moving around.
xOffset = 90;
uiXpos = fsPopPos(1)+fsPopPos(3)+sz.uuhs+xOffset;

% Render the maximum number of filter coefficient edit boxes which 
% is four (due to the State-space case) 
for n = 1:4,
    uipos=[uiXpos fsPopPos(2) uiebWidth sz.uh]-(n-1)*[0 sz.uuvs+sz.uh 0 0];
    h_ebs(n) = uicontrol('Style','Edit',...
        'BackgroundColor','White',...
        'Position',uipos,...
        'Visible','Off',...
        'HorizontalAlignment','Left',...
        'Tag','import_params',...
        'String','',...
        'Callback',cbStr);
end


%----------------------------------------------------------------------
function h_lbls = importLabels(hFig,framePos,fsPopPos,lblStrs)

% Inputs:
%   hFig      - Handle to the Filter Design GUI figure.
%   framePos  - Specify Filter Coefficients frame position.
%   fsPopPos  - Filter Structure popupmenu position.
%   lblStrs   - Filter Structure uicontrol labels (strings).
%
% Outputs:
%  h_lbls  - Handles to the Filter Structure labels.

% Cache uicontrol sizes and figure's background color.
sz = gui_sizes;
bgc = get(hFig,'Color');

uiLblWidth = largestuiwidth(lblStrs); 

% Since the largest edit box label is about 80, add 100 to X so that
% the edit boxes do not move around.
xOffset = 90;
uiXpos = fsPopPos(1)+fsPopPos(3)+sz.uuhs+xOffset;

% Label X Position with respect to the edit boxes
lblXpos = uiXpos-sz.uuhs-uiLblWidth;

% Render the edit box labels
for n = 1:length(lblStrs),
    lblpos=[lblXpos fsPopPos(2) uiLblWidth sz.lh]-(n-1)*[0 sz.uuvs+sz.uh 0 0];
    h_lbls(n) = uicontrol('Style','Text',...
        'BackgroundColor',bgc,...
        'Position',lblpos,...
        'Visible','Off',...
        'String',lblStrs{n},...
        'Tag','import_params',...
        'HorizontalAlignment','Right');
end


%----------------------------------------------------------------------
function h_clrPbs = importClearPushBtns(hFig,framePos,fsPopPos,h_ebs);
% IMPORTCLEARPUSHBTNS  Render the "Clear" push buttons for reseting 
%                      the coefficients edit boxes.
%
% Inputs:
%   hFig      - Handle to the Filter Design GUI figure.
%   framePos  - Specify Filter Coefficients frame position.
%   fsPopPos  - Filter Structure popupmenu position.
%
% Outputs:
%  h_ebs   - Handles to the Filter coefficient edit boxes.

% Cache uicontrol sizes and figure's background color
sz = gui_sizes;
bgc = get(hFig,'Color');

% NOTE: The importparams_cbs function updates the figure's userdata
% This callback string is for all Import Coefficients Edit Boxes.
importparamscbs = importparams_cbs;
clr_cbStr = importparamscbs.clearpush_cb;


% Clear Push buttons Position
uiclrWidth = largestuiwidth({'Clear'})+10*sz.pixf;
editboxPos = get(h_ebs(1),'Position');
clrXpos = editboxPos(1) + editboxPos(3) + sz.uuhs;

% Render maximum number of "Clear" push buttons which is four (due to 
% the State-space case) 
for n = 1:4,
    uipos=[clrXpos fsPopPos(2) uiclrWidth sz.uh]-(n-1)*[0 sz.uuvs+sz.uh 0 0];
    h_clrPbs(n) = uicontrol('Style','Push',...
        'BackgroundColor',bgc,...
        'Position',uipos,...
        'Visible','Off',...
        'Tag','import_params',...
        'String',sprintf('Clear'),...
        'Callback',clr_cbStr);
end


%----------------------------------------------------------------------
function importFs = samplingFreqUis(hFig, h_frame, h_clearPb)
% SAMPLINGFREQUIS  Render the Sampling Frequency labels and uicontrols
%                  (for the Specify Filter Coefficients tab)
%
% Inputs:
%   hFig      - Handle to the Filter Design GUI figure.
%   h_frame   - Handle to the Specify Filter Coefficients frame.
%   h_clearPb - Handle to a "Clear" Pushbutton
%
% Output:
%  importFs - Strcuture containing the handles to the Sampling Frequency
%             uicontrols.

sz = gui_sizes;
bgc = get(hFig,'Color');
framePos = get(h_frame,'Position');

% Determine the position of the "Clear" pushbutton.
clrBtnPos = get(h_clearPb,'Position');

% Render the frequency units popup 
%popupStrs = {'Hz','kHz','MHz','GHz','Normalized (0 to 1)'}; % Geck 75494
popupStrs = {'Normalized (0 to 1)','Hz','kHz','MHz'};
h_fsPop = render_units_popup(sz,popupStrs,framePos,clrBtnPos);

% Render the sampling frequency edit box
defaultFsStr = {'Fs'};
h_fsEb = render_fs_eb(sz,defaultFsStr,h_fsPop);

% Render the labels after the uicontrols so that tabbing works as expected.
% "Units:" label
fsPopupLbl = {'Units:'};
h_fsPopLbl = render_popup_lbl(sz,bgc,h_fsPop,fsPopupLbl);

% "Sampling Frequency" title
fsTitle = {'Sampling Frequency:'};
h_fsTitle = render_fs_title(sz,bgc,framePos,fsTitle,h_fsPopLbl);

% Sampling Frequency edit box label ("Fs:")
fsEbLbl = {'Fs:'};
h_fsEbLbl = render_fs_lbl(sz,bgc,h_fsEb,fsEbLbl);

% Store sampling frequency handles back in UD
importFs.fstitle  = h_fsTitle;
importFs.fspop    = h_fsPop;
importFs.fseb     = h_fsEb;
importFs.fslbl    = h_fsPopLbl;
importFs.fseblbl  = h_fsEbLbl;


%----------------------------------------------------------------------------
function h_fsTitle = render_fs_title(sz,bgc,framePos,fsTitle,h_fsPopLbl);
% RENDER_FS_TITLE Render the "Sampling Frequency" title.
%
% Inputs:
%  framePos   - Specify Filter Coefficients frame position
%  sz         - Uicontrol sizes and spaces structure
%  bgc        - Figure's background color
%  fsTitle    - Sampling Frequency title
%  h_fsPopLbl - Handle to the "Units:" label
%
% Output:
%  h_fsTitle - Handle to the sampling frequency title.


% Title Position
titleWidth = largestuiwidth(fsTitle,'text'); 
frameUpperEdge = framePos(2)+framePos(4);
fsPopPos = get(h_fsPopLbl,'Position');
titlePos = [fsPopPos(1) frameUpperEdge-40 titleWidth sz.uh];

h_fsTitle = uicontrol('Style','text',...
        'BackgroundColor',bgc,...
        'position',titlePos,...
        'Visible','Off',...
        'string',fsTitle,...
        'Tag','import_params',...
        'HorizontalAlignment','left');

    
%----------------------------------------------------------------------------
function h_fsPop = render_units_popup(sz,popupStrs,framePos,clrBtnPos)
%RENDER_UNITS_POPUP   Renders the sampling frequency units popup menu.
%
% Inputs:
%  sz         - Uicontrol sizes and spaces structure.
%  framePos   - Specify Filter Coefficients frame position.
%  popupStrs  - Sampling Frequency popupmenu items.
%  clrBtnPos  - Position of the "Clear" button for the Numerator edit
%               box (for the Transfer function case).
%
% Output:
%     h_fsPop - Handle to the sampling frequency popupmenu.

% Popup menu position
popupWidth = largestuiwidth(popupStrs,'popup');
frameRightEdge = framePos(1)+framePos(3);
popupPos = [frameRightEdge-popupWidth-sz.lfs clrBtnPos(2) popupWidth sz.uh];

importparamscbs = importparams_cbs;
cbStr = importparamscbs.import_fsUnits;

h_fsPop = uicontrol('Style','popup',...
    'BackgroundColor','White',...
    'Position',popupPos,...
    'String',popupStrs,...
    'Tag','import_params',...
    'Visible','Off',...
    'Callback',cbStr);


%-----------------------------------------------------------------------------
function h_fsEb = render_fs_eb(sz,defaultFsStr,h_fsPop)
%RENDER_FS_EB   Renders the sampling frequency edit box.
%
% Inputs:
%  sz            - Uicontrol sizes and spaces structure.
%  defaultFsStr  - "Fs:" string for the Sampling Frequency edit box.
%  h_fsPop       - Handle to the sampling frequency popupmenu.
%
% Output:
%     h_fsEb - Handle to the sampling frequency edit box.

% Edit box position
fsPopPos = get(h_fsPop,'Position');
eb_pos = [fsPopPos(1), ...
        fsPopPos(2)-(sz.uh+sz.uuvs),...
        fsPopPos(3),...
        sz.uh];

importparamscbs = importparams_cbs;
cbStr = importparamscbs.import_fs;

h_fsEb = uicontrol('style','edit',...
    'BackgroundColor','white',...
    'position',eb_pos,...
    'tag','import_params',...
    'Visible','Off',...
    'String',defaultFsStr,...
    'HorizontalAlignment','left',...
    'callback',cbStr);


%----------------------------------------------------------------------------
function h_fsPopLbl = render_popup_lbl(sz,bgc,h_fsPop,fsPopupLbl)
%RENDER_POPUP_LBL   Renders the 'Units:' label 
%
% Inputs: 
%  sz         - Uicontrol sizes and spaces structure
%  bgc        - Figure's background color
%  h_fsPop    - Handle to the sampling frequency popupmenu.
%  fsPopupLbl - Sampling frequency units popupmenu label string.
%
%   Output:
%     h_fsPopLbl - Handle to the "Units:" label.

% Label Position
lblWidth = largestuiwidth(fsPopupLbl);
fsPopPos = get(h_fsPop,'Position');
lblPos = [(fsPopPos(1)-(sz.uuhs+lblWidth)),...
        fsPopPos(2)-sz.lblTweak lblWidth sz.uh];

h_fsPopLbl = uicontrol('Style','text',...
        'BackgroundColor',bgc,...
        'position',lblPos,...
        'Visible','Off',...
        'string',fsPopupLbl,...
        'Tag','import_params',...
        'HorizontalAlignment','left');
 
    
%----------------------------------------------------------------------------
function h_fsLbl = render_fs_lbl(sz,bgc,h_fsEb,fsEbLbl)
%RENDER_FS_LBL   Renders the sampling frequency label.
%
%
% Inputs: 
%  sz      - Uicontrol sizes and spaces structure
%  bgc     - Figure's background color
%  h_fsEb  - Handle to the sampling frequency edit box.
%  fsEbLbl - Sampling frequency edit box label string.
%
%   Output:
%     h_fsLbl - Handle to the "Fs:" label.


% Label Position
lblWidth = largestuiwidth(fsEbLbl);
fsEbPos = get(h_fsEb,'Position');
lblPos = [(fsEbPos(1)-(sz.uuhs+lblWidth)),...
        fsEbPos(2)-sz.lblTweak lblWidth sz.uh];

h_fsLbl = uicontrol('Style','text',...
        'BackgroundColor',bgc,...
        'position',lblPos,...
        'Visible','Off',...
        'string',fsEbLbl,...
        'Tag','import_params',...
        'HorizontalAlignment','left');

%----------------------------------------------------------------------
% [EOF] render_importparams.m
