function h = render_designparams(hFig,h_actionbtnsFr,typesNmethods)
% RENDER_DESIGNPARAMS Renders all the frames that contain the uicontrols
%                     to set the filter design parameters.
% Inputs:
%   hFig          - handle to fdatool's figure.
%   h_actionbtnsFr- handle to the action buttons frame.
%   typesNmethods - a structure containing the list of filter types and methods used 
%                   in the Type and Method menus.
% Outputs:
%   h - a structure containing handles to all the uicontrols rendered by this function.
%       The top level fields of h are:
%
%           h.filtTypes      - handles to Filter Type frame and uicontrols.
%           h.methods        - handles to Design Method mframe and uicontrols.
%           h.filtOrder      - handles to Filter Order frame and uicontrols.
%           h.specs          - handles to Magnitude and Frequency spec frames and uis.

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

% Render the Filter Type frame with all the filter types as radio buttons.
h.filtTypes = type_frame(hFig,h_actionbtnsFr,typesNmethods.type);

% Render the Design Method frame and its uicontrols.
h.methods = methods_frame(hFig,h.filtTypes.frame(1),typesNmethods.method);

% Render the Filter Order frame
h.filtOrder = filterorder_frame(hFig,h.filtTypes.frame(1));

% Render the FIR Window frame.
[h.methods,h.firwindow] = firwindow_frame(hFig,h.filtTypes,...
    h.methods,typesNmethods.method);

% Render the Specs frames. Note that these are the freq. specs.
% and mag. specs frames. This function is implemented in a separate file.
h.specs = render_specsframes(hFig,h.filtOrder.frame(1));

%----------------------------------------------------------------------
function h_filtTypes = type_frame(hFig,h_actionbtnsFr,filtTypes)
% TYPE_FRAME Renders the Filter Type frame and its uicontrols.
%
% Inputs:
%   hFig           - handle to the Filter Design GUI figure.
%   h_actionbtnsFr - handle to the action buttons frame.
%   filtTypes      - a structure containing the list of filter types used
%                    in the Type menu.
% Outputs:
%   filtTypes - a structure containingfd the handles to the frame and the
%               uicontrols (radio buttons and popup menu) inside the frame.

% Sizes and spaces used in building the Filter Design GUI.
sz = gui_sizes;

% Cache the figure and adjacent frame position.
figPos = get(hFig,'Position');
adjFrPos = get(h_actionbtnsFr,'Position');

% Filter Type frame. sz.fh* are frame heights predifined in gui_sizes.
frHeight = sz.fh2+sz.fh3+sz.vffs; %9*(sz.uh+sz.uuvs)
yPos = adjFrPos(2)-frHeight-sz.vffs;% adjFrPos(2)-sz.fh1-frHeight-2*sz.vffs;
frameWidth = (figPos(3)-5.5*sz.ffs)/4;
framePos = [1.5*sz.ffs yPos frameWidth frHeight];
bgc = get(hFig,'Color');
frame = framewlabel(hFig,framePos,'Filter Type','design_params',bgc);

rb_hndls = type_frame_uis(hFig,framePos,filtTypes);

h_filtTypes.frame = frame;
h_filtTypes.rb = rb_hndls;

%----------------------------------------------------------------------
function typeHandles = type_frame_uis(hFig,framePos,filtTypes)
% TYPE_FRAME_UIS Renders the filter type radio buttons and popup.
%
% Inputs:
%   hFig      - handle to the Filter Design GUI figure.
%   framePos  - position of the Filter Type frame.
%   filtTypes - a structure containing two cell arrays of strings; one cell array
%               of the common filter types (lowpass, higpass, bandpass and bandstop)
%               and the second cell array containing all remaining filter types.
% Outputs:
%   typeHandles  - a structure containing the handles to the frame and the
%                  uicontrols (radio buttons and popup menus) inside the frame.

% NOTE: The filtType_cbs function updates the figure's userdata.
designparamscbs = designparams_cbs;
cbStr = designparamscbs.filtType_cbs;

% sz is a structure containing sizes and spaces used in rendering the GUI.
sz = gui_sizes;
uiWidth = largestuiwidth(filtTypes.common,'popup'); % 137;

% Get figure's background color.
bgc = get(hFig,'color');

% Put up the common filter types (lowpass, highpass, bandpass, and bandstop).
for n=1:length(filtTypes.common),
    upperEdge = framePos(2)+framePos(4);
    x = framePos(1)+sz.hfus;
    y = upperEdge-sz.uuvs;
    pos = [x y uiWidth sz.uh]-n*[0 sz.uh 0 0];
    
    typeHandles(n)=uicontrol('style','radiobutton',...
        'BackgroundColor',bgc,...%[1 0.9608 0.8848],... %[.9 .86 .7956],...
        'position',pos,...
        'visible','on',...
        'string',filtTypes.common{n},...
        'Tag','design_params',...
        'callback',cbStr);
end

% Radio button for the popupmenu which contains the remaining filter types.
rbWidth = largestuiwidth(filtTypes.other,'popup'); % 137;
pos = [pos(1) pos(2)-sz.uh-sz.uuvs/2 rbWidth pos(4)];
typeHandles(end+1)=uicontrol('style','radiobutton',...
        'BackgroundColor',bgc,...
        'position',pos,...
        'visible','on',...
        'string','',...
        'Tag','design_params',...
        'callback',cbStr);

% Popupmenu
popupPos = pos+[18 0 0 0]*sz.pixf;
typeHandles(end+1) = uicontrol('style','popupmenu',...
    'BackgroundColor','white',...
    'position',popupPos,...
    'visible','on',...
    'string',filtTypes.other,...
    'Tag','design_params',...
    'callback',cbStr);
    
%----------------------------------------------------------------------
function methods = methods_frame(hFig,h_filtTypeFr,designMethods)
% METHODS_FRAME Renders the Design Method frame and its uicontrols.
%
% Inputs:
%   hFig          - handle to the Filter Design GUI figure.
%   h_filtTypeFr  - handle to the Filter Type frame.
%   designMethods - a structure containing the list of filter types and methods used 
%                   in the Type and Method menus.
% Outputs:
%   methods - a structure containing the handles to the frame and the
%             uicontrols (radio buttons and popup menus) inside the frame.

% Sizes and spaces used in building the Filter Design GUI.
sz = gui_sizes;

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

% Get position of frame that will use to determine our position.
typeFrPos = get(h_filtTypeFr,'Position');

% Filter Methods frame.
frHeight = 2*sz.uh + 3*sz.uuvs; % Lots of sapcing between UIs.
framePos = [typeFrPos(1) typeFrPos(2) typeFrPos(3) frHeight];
h_frame = framewlabel(hFig,framePos,'Design Method','design_params',bgc);

% Render the uicontrols within the frame.
[h_rb, h_pop] = method_uis(framePos,sz,bgc,designMethods);

methods.frame = h_frame;
methods.rb    = h_rb;
methods.pop   = h_pop;

%----------------------------------------------------------------------
function [h_rb, h_pop] = method_uis(frPos,sz,bgc,designMethods)
% METHOD_UIS Renders the uicontrols within the Design Method frame.
%
% Inputs:
%   frPos - position of Design Method frame.
%   sz    - structure containing uicontrol sizes and spacing.
%   bgc   - color for uicontrols.
%   designMethods - structure of three cell arrays containing a list of 
%                   the filter design methods available.
%                       designMethods.iir
%                       designMethods.fir
%                       designMethods.window
% Outputs:
%   h_rb  - handles to radio buttons uicontrols.
%   h_pop - handles to popup menu uicontorls.

% Render the Radio buttons.
rbStrs = {'IIR','FIR'}; % Radio button strings.
h_rb = methodsradiobtns(rbStrs,frPos,sz,bgc);

% Render the Popupmenu uicontrols.
h_pop = methodspopupbtns(designMethods,frPos,h_rb,sz,bgc);

%----------------------------------------------------------------------
function h_rb = methodsradiobtns(rbStrs,frPos,sz,bgc)
%METHODSRADIOBTNS Render the IIR and FIR radio buttons for the 
%                 Desin Method Frame.
% Inputs:
%   rbStrs- strings for the radio buttons.
%   frPos - position of Design Method frame.
%   sz    - structure containing uicontrol sizes and spacing.
%   bgc   - colot for uicontrols.
%
% Outputs:
%   h_rb  - handles to radio buttons uicontrols.

rbWidth = largestuiwidth(rbStrs,'radiobutton'); % 39; 

% Callback string.
designparamscbs = designparams_cbs;
cbStr = designparamscbs.filtMethod_cbs;

% Set up uicontrol HG properties that are common to both radio buttons.
rb_props = {...
        'Style','Radiobutton',...
        'BackgroundColor',bgc,...
        'Callback',cbStr,...
        'Tag','design_params'};

% IIR Radio button.
rb_pos.iir = [frPos(1)+sz.hfus frPos(2)+frPos(4)-sz.uh-3*sz.vfus rbWidth sz.uh];
h_rb.iir = uicontrol(rb_props{:},'String',rbStrs{1},'Position',rb_pos.iir);

% FIR Radio button.
rb_pos.fir = rb_pos.iir - [0 sz.uh+sz.uuvs/2 0 0];
h_rb.fir = uicontrol(rb_props{:},'String',rbStrs{2},'Position',rb_pos.fir);

%----------------------------------------------------------------------
function h_pop = methodspopupbtns(designMethods,frPos,h_rb,sz,bgc)
%METHODSPOPUPBTNS Render the poupmenu buttons for the filter design methods.
%
% Inputs:
%   designMethods - structure of three cell arrays containing a list of 
%                   the filter design methods available. See calling fcn 
%                   for details.
%   frPos - position of Design Method frame.
%   h_rb  - handles to radio buttons uicontrols.
%   sz    - structure containing uicontrol sizes and spacing.
%   bgc   - color for uicontrols.
%
% Outputs:
%   h_pop - handles to popup menu uicontrols.

% Callback string.
designparamscbs = designparams_cbs;
cbStr = designparamscbs.methodPopup_cbs;

% Cache IIR and FIR radio buttons positions.
rb_iirpos = get(h_rb.iir,'Position');
rb_firpos = get(h_rb.fir,'Position');

% Determine how wide to make the popups by getting the extend of the widest 
% method listed in both IIR and FIR methods.
methodStrs = {designMethods.iir{:}, designMethods.fir{:}};
popupWidth = largestuiwidth(methodStrs,'popup');   % 114; 

% Make sure popup uicontrols don't extend outside the frame.
widthLimit = frPos(3) - (rb_iirpos(1)-frPos(1)) - rb_iirpos(3) - sz.hfus/2;
if popupWidth > widthLimit, popupWidth = widthLimit; end;

% Set up uicontrol HG properties that are common to popupmenus.
pop_props = {...
        'Style','Popupmenu',...
        'Callback',cbStr,...
    	'BackgroundColor','white',...
    	'Tag','design_params'};

% IIR Popupemnu uicontrol.
pop_pos.iir = [rb_iirpos(1)+rb_iirpos(3) rb_iirpos(2) popupWidth  rb_iirpos(4)];
h_pop.iir = uicontrol(pop_props{:},...
    'Position',pop_pos.iir,...
    'String',designMethods.iir);

% FIR Popupmenu uicontrol.
pop_pos.fir = [rb_firpos(1)+rb_firpos(3) rb_firpos(2) popupWidth rb_firpos(4)];
h_pop.fir = uicontrol(pop_props{:},...
    'Position',pop_pos.fir,...
    'String',designMethods.fir);

%------------------------------------------------------------------------------
function filterorder = filterorder_frame(hFig,hfiltMethod)
%FILTERORDER_FRAME Render the Filter Order frame.
% 
% Inputs:
%   hFig        - handle to the GUI's figure.
%   hfiltMethod - handle to the design methods frame.
%
% Outputs:
%   filteroder  - structure containing handles to the filter order
%                 frame and UIs.

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

% Use one of the three frame heights defined in gui_sizes.
frameHeight = sz.fh3; %192*sz.pixf;

% Get position of adjancent frame.
filtMethod_pos = get(hfiltMethod,'position');

% Calculate this frame's position and then render the frame.
framePos = [filtMethod_pos(1)+filtMethod_pos(3)+sz.ffs,...
        filtMethod_pos(2)+sz.fh2+sz.vffs, ...
        filtMethod_pos(3),...
        frameHeight];
frame = framewlabel(hFig,framePos,'Filter Order','design_params',bgc);

% Put up radio buttons
orderStrs = {'Specify order:','Minimum order'};
% These other choices are not available in current functionality.
% ,'Min. even order','Min. odd order'};

[orderrbs,orderlbls,orderebs] = order_frame_uis(hFig,framePos,orderStrs,sz);

filterorder.frame = frame;
filterorder.rb    = orderrbs;
filterorder.lb    = orderlbls;
filterorder.eb    = orderebs;

%----------------------------------------------------------------------
function [orderrbs,orderlbls,orderebs] = ...
    order_frame_uis(hFig,framePos,orderStrs,sz)
% ORDER_FRAME_UIS Render the filter order radio buttons and edit box.
%

% NOTE: The filtOrder_cbs function updates the figure's userdata.
designparamscbs = designparams_cbs;
cbStr = designparamscbs.filtOrder_cbs;

uiHeight = sz.uh;
uiWidth = largestuiwidth(orderStrs,'radiobutton'); % 90;
xoffset = sz.hfus; % space from the frame to the ui's

% Get figure's background color.
bgc = get(hFig,'color');

% Find the upper edge of the filter order frame 
upperEdge = framePos(2)+framePos(4);
% Put up the radio buttons
for n=1:length(orderStrs),
    
    pos = [framePos(1)+xoffset,...
            upperEdge-sz.vfus-n*uiHeight-n*sz.uuvs*2,...
            uiWidth,...
            uiHeight];

    orderrbs(n)=uicontrol('style','radiobutton',...
        'BackgroundColor',bgc,...%[1 0.9608 0.8848],... %[.9 .86 .7956],...
        'position',pos,...
        'visible','off',...
        'string',orderStrs{n},...
        'Tag','design_params',...
        'callback',cbStr);
end

% Put up the edit box in front of the specify order option

% First find the position of specify order
i = strmatch('Specify order',orderStrs);
specorder_pos = get(orderrbs(i),'position');

% Now allocate the appropriate position
width = 40;
pos = [specorder_pos(1)+specorder_pos(3) specorder_pos(2) width sz.uh];

designparamscbs = designparams_cbs;
specordereditbox_cbStr = designparamscbs.specOrdereb_cb;

orderrbs(end+1) = uicontrol('style','edit',...
    'BackgroundColor','white',...
        'position',pos,...
        'visible','off',...
        'HorizontalAlignment','left',...
        'Tag','design_params',...
        'callback',specordereditbox_cbStr);
    
% Put up the numerator order and denominator order labels and edit boxes
numdenstrs = {'Numerator order:','Denominator order:'};
numdenlbwidth = largestuiwidth(numdenstrs);

% Put up the labels buttons
for n=1:length(numdenstrs),
    
    pos = [framePos(1)+xoffset,...
            upperEdge-sz.vfus-n*uiHeight-n*sz.uuvs*2,...
            numdenlbwidth,...
            uiHeight];

    orderlbls(n)=uicontrol('style','text',...
        'BackgroundColor',bgc,...
        'position',pos,...
        'visible','off',...
        'Horizontalalignment','left',...
        'string',numdenstrs{n},...
        'Tag','design_params');
end

lblpos = get(orderlbls(1),'position');
numdenordereditbox_cbStr = designparamscbs.numdenOrdereb_cb;

% Put the edit boxes in front of the labels
for n=1:length(numdenstrs),
    
    pos = [lblpos(1)+lblpos(3),...
            upperEdge-sz.vfus-n*uiHeight-n*sz.uuvs*2,...
            width,...
            uiHeight];

    orderebs(n)=uicontrol('style','edit',...
        'BackgroundColor','white',...
        'Horizontalalignment','left',...
        'position',pos,...
        'enable','on',...
        'visible','off',...
        'callback',numdenordereditbox_cbStr,...
        'Tag','design_params');
end


%------------------------------------------------------------------------------
function [h_methods,firwindow] = firwindow_frame(hFig,h_types,h_methods,designMethods)
%FIRWINDOW_FRAME Render the Window specifications frame.
%
% Inputs:
%   hFig          - handle to GUI figure window.
%   h_methods     - handle to the methods frame and UIs.
%   designMethods - cell array of strings containing the design methods.
%
% Outputs:
%   h_methods - the handles to the methods frames and UIs including the window
%               design method UIs and frame.
%   firwindow - handle to the Window Specifications frame.

% Cache the filter color and the sizes and spacings used in the GUI.
bgc = get(hFig,'Color');
sz = gui_sizes;

% Use one of the three available frame sizes.
frameHeight = sz.fh2;  

% Get position of adjancent frame.
%filtMethod_pos = get(h_methods.frame(1),'position');
filtType_pos = get(h_types.frame(1),'Position');

% Calculate this frame's position and then render the frame.
%   framePos = [filtMethod_pos(1)+filtMethod_pos(3)+sz.ffs,...
%           filtType_pos(2)+filtType_pos(4)-frameHeight, ...
%           filtMethod_pos(3),...
%           frameHeight];
framePos = [filtType_pos(1)+filtType_pos(3)+sz.ffs,...
        filtType_pos(2), ...
        filtType_pos(3),...
        frameHeight];
frame = framewlabel(hFig,framePos,'Window Specifications','design_params',bgc);

% Render the Window Popupmenu w/ label. Add the handles to the same structure.
h_pop = h_methods.pop;
h_pop = winpopup(hFig,h_pop,designMethods.window,framePos);

% Render a window parameter UI w/ label. Add the handles to the same structure.
h_pop = winParamUI(hFig,h_pop);

% Return the structure with the handles.
firwindow.frame = frame;
h_methods.pop = h_pop;

%------------------------------------------------------------------------------
function h_pop = winpopup(hFig,h_pop,winMethods,frPos)
%WINPOPUP Render the Window popupmenu.
%
% Inputs:
%   hFig       - handle to GUI's figure.
%   h_pop      - handles to design methods popupmenu uicontrols.
%   winMethods - cell array of strings containing a list of window
%                design methods available.
%   frPos      - position of window specifications frame.
%
% Outputs:
%   h_pop - handles to popup menu uicontrols with the window popupmenu
%           handle added.

% Cache the userdata structure stored in the figure.
ud = get(hFig,'Userdata');

% Cache the figure's color and the GUI's sizes/spacing.
bgc = get(hFig,'Color');
sz = gui_sizes;

% Callback string.
designparamscbs = designparams_cbs;
cbStr = designparamscbs.winMethPopup_cb;

% Get position of the FIR methods popup.
pop_pos.fir = get(h_pop.fir,'Position');

% Position for Window Popupmenu.
xOffset = 20*sz.pixf;
xPos = frPos(1) + sz.hfus + 2.6*xOffset;
yPos = frPos(2) + frPos(4) - sz.uh - 2*sz.vfus;
width = pop_pos.fir(3) - xOffset;
height = sz.uh;
pop_pos.firWin = [xPos yPos width height];

% Window Popupmenu.  By default it's disabled.
h_pop.firWin = uicontrol(...
    'Style','Popupmenu',...
    'Callback',cbStr,...
    'HorizontalAlignment','left',...
    'BackgroundColor',[.8 .8 .8],...
    'Tag','design_params',...
    'Position',pop_pos.firWin,...
    'Enable','Off',...
    'String',winMethods);

% Now that we've positioned the popup let's add a label.
% Render Window popupmenu label.
label = {'Window:'};
labelWidth = largestuiwidth(label)+10*sz.pixf; % 56+10*sz.pixf; 

x = pop_pos.firWin(1)-labelWidth;
y = pop_pos.firWin(2)-sz.lblTweak;
pop_pos.winLabel = [x y labelWidth pop_pos.firWin(4)];
h_pop.winLabel = uicontrol(...
     'Style','Text',...
     'Enable','Off',...
     'HorizontalAlignment','left',...
     'BackgroundColor',bgc,...
     'Tag','design_params',...
     'Position',pop_pos.winLabel,...
     'Enable','Off',...
     'String',label);

%----------------------------------------------------------------------
function h_pop = winParamUI(hFig,h_pop);
%WINPARAMUI Renders the window parameter for FIR Window filter methods.
%
% Inputs:
%   hFig       - handle to GUI's figure.
%   h_pop      - handles to design methods popupmenu uicontrols.
%
% Outputs:
%   h_pop - handles to popup menu uicontrols with the window parameter
%           popupmenu handle added.

% Cache the figure's color and the GUI's sizes/spacing.
bgc = get(hFig,'Color');
sz = gui_sizes;

% Callback string for the edit box.
designparamscbs = designparams_cbs;
cbStr = designparamscbs.winParamEditbox_cb;

% Get the position for the Window label and popupmenu.
pos.winLabel = get(h_pop.winLabel,'Position');
pos.firWin   = get(h_pop.firWin,'Position');

pos_label = pos.winLabel+[0 -(sz.uuvs/2+sz.uh) 0 0];
h_label = uicontrol('style','Text',...
    'HorizontalAlignment','left',...
    'BackgroundColor',bgc,...
    'Position',pos_label,...
    'Enable','off',...
    'Visible','on',...
    'String','Parameter:',...
    'Tag','design_params');

pos_eb = [pos.firWin(1) pos_label(2) pos.firWin(3) pos.firWin(4)];
h_eb = uicontrol('style','Edit',...
    'HorizontalAlignment','left',...
    'BackgroundColor',bgc,...
    'position',pos_eb,...
    'Enable','off',...
    'Visible','on',...
    'String','1',...
    'Tag','design_params',...
    'Callback',cbStr);

h_pop.eb      = h_eb;
h_pop.eblabel = h_label;

% [EOF] render_designparams.m
