function specs = render_specsframes(hFig,foframe_hndl)
%RENDER_SPECSFRAMES Render the frames and uicontrols for the specs frames:
%
%   The following frames are rendered:
%   - Frequency Specs.
%   - Magnitude Specs.
%   - FREQMAG (freq. amplitude and weights) specs.
%
%   Inputs:
%     hFig         - handle to the main gui figure
%     foframe_hndl - filter order frame handle
%
%   Outputs:
%     specs - structure detailed in local functions below.

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


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

bgc = get(hFig,'Color');

% Tag for all uicontrols
tag = 'design_params';

indent = 10*sz.pixf; % Define indentation for the edit boxes to follow


% Build the freq. specs frame
specs.freq = render_freq_frame(hFig,bgc,sz,foframe_hndl,tag,...
    indent);

% Build the mag. specs frame
specs.mag = render_mag_frame(hFig,bgc,sz,specs.freq.frame(1),...
    tag,indent);


% Build the freqmag (frequency, amplitude and weights) specs frame (this is used
% for arbitrary shape design), it takes the real estate of both the freq. specs
% and mag. specs
specs.freqmag = render_freqmag_frame(hFig,bgc,sz,foframe_hndl,tag,indent);

%------------------------------------------------------------------
function freq = render_freq_frame(hFig,bgc,sz,foframe_hndl,tag,indent)
%RENDER_FREQ_FRAME   Render the frequency specifications frame.
%
%   Renders the frame, the frame label, the popup, the popup
%   label, the sampling frequency label and the sampling frequency
%   edit box of the frequency specifications frame.  Returns
%   handles to all of these.
%
%   Inputs: same as above plus
%     bgc    - Background color to be used throughout
%     sz     - structure with size info for uicontrols.
%     tag    - tag to be used on all uicontrols.
%     indent - indentation between the units label and all other labels. 
%     freq_eb_cbStr - string to be used as callback for all freq. edit boxes.
%
%   Outputs:
%     freq - structure:
%
%     freq.frame     - two element array of handles to the freq. frame.
%     freq.popup_lbl - handle to the 'Units:' label.
%     freq.popup     - handle to the units popup. 
%     freq.fs_lbl    - handle to the fs label.
%     freq.fs_eb     - handle to the fs edit box.
%     freq.lbl       - handle to all other labels
%     freq.eb        - handle to all other edit boxes

% Put up the freq. specs frame
freq_frame_lbl = 'Frequency Specifications';
freq.frame = render_frame(hFig,bgc,sz,foframe_hndl,...
    freq_frame_lbl,tag,'freq');

% Put up the label for the freq. units pop-up 
freq.popup_lbl = render_popup_lbl(hFig,bgc,sz,freq.frame(1),tag,'Units:');

% Put up the freq. units pop-up in front of the popup label
freq_popup_strs = {'Hz','kHz','MHz','Normalized (0 to 1)'};

specscbs = specs_cbs;
freqpopup_cbStr = specscbs.freq_popup;
freq_fseb_cbStr = {specscbs.fs_eb,'freq'};
freq_eb_cbStr = {specscbs.eb,'freq'};

freq.popup = ...
    render_popup(hFig,sz,freq.popup_lbl,freq_popup_strs,indent,freqpopup_cbStr,tag);

% Put up the sampling frequency label
freq.fs_lbl = render_fs_lbl(hFig,bgc,sz,freq.popup_lbl,...
    freq.popup,indent,tag,'below');

% Put up the sampling frequency edit box
freq.fs_eb = ...
    render_fs_eb(hFig,sz,freq.fs_lbl,freq.popup,indent,...
    freq_fseb_cbStr,tag,'below');

freq = render_freq_specs(hFig,bgc,sz,tag,freq,...
    freq_eb_cbStr);

%---------------------------------------------------------------------------
function mag = render_mag_frame(hFig,bgc,sz,h_freq_frame,...
    tag,indent)
%RENDER_MAG_FRAME   Render the magnitude specifications frame.
%
%   Renders the frame, the frame label, the popup, and the popup
%   label of the magnitude specifications frame.  Returns
%   handles to all of these.
%
%   Inputs: same as above plus
%     h_freq_frame - handle to the freq. specs frame
%
%   Outputs:
%     mag - structure:
%
%     mag.frame     - two element array of handles to the mag. frame.
%     mag.popup_lbl - handle to the 'Units:' label.
%     mag.popup     - handle to the units popup.
%     mag.lbl       - handle to all other labels
%     mag.eb        - handle to all edit boxes


% Put up the mag. specs frame
mag_frame_lbl = 'Magnitude Specifications';
mag.frame = render_frame(hFig,bgc,sz,h_freq_frame,mag_frame_lbl,...
    tag,'mag');

% Put up the label for the mag. units pop-up 
mag.popup_lbl = render_popup_lbl(hFig,bgc,sz,mag.frame(1),tag,'Units:');

% Put up the mag. units pop-up in front of the popup label
mag_popup_strs = {'test'};

specscbs = specs_cbs;
mag_cbStr = specscbs.mag_popup;
mag_eb_cbStr  = {specscbs.eb,'mag'};

mag.popup = ...
    render_popup(hFig,sz,mag.popup_lbl,mag_popup_strs,indent,mag_cbStr,tag);

mag = render_mag_specs(hFig,bgc,sz,tag,mag,indent,...
    mag_eb_cbStr);

%------------------------------------------------------------------
function freqmag = render_freqmag_frame(hFig,bgc,sz,foframe_hndl,tag,indent)
%RENDER_FREQMAG_FRAME   Render the freqmag (freq, amplitude and weights) specifications frame.
%
%   Renders the frame, the frame label, the popup, the popup
%   label, the sampling frequency label and the sampling frequency
%   edit box of the freqmag specifications frame.  Returns
%   handles to all of these.
%
%   Inputs: same as above 
%
%   Outputs:
%     freqmag - structure:
%
%     freqmag.frame     - two element array of handles to the freq. frame.
%     freqmag.popup_lbl - handle to the 'Units:' label.
%     freqmag.popup     - handle to the units popup. 
%     freqmag.fs_lbl    - handle to the fs label.
%     freqmag.fs_eb     - handle to the fs edit box.
%     freqmag.lbl       - handle to all other labels
%     freqmag.eb        - handle to all other edit boxes


% Put up the freqmag specs frame
freqmag_frame_lbl = 'Frequency and Magnitude Specifications';
freqmag.frame = render_frame(hFig,bgc,sz,foframe_hndl,freqmag_frame_lbl,...
    tag,'freqmag');
set(freqmag.frame,'Visible','off');  % Keep it off until updates are done.

% Put up the label for the freq. units pop-up 
freqmag.popup_lbl = render_popup_lbl(hFig,bgc,sz,freqmag.frame(1),tag,'Frequency Units:');

% Put up the freq. units pop-up in front of the popup label
freq_popup_strs = {'Hz','kHz','MHz','Normalized (0 to 1)'};

specscbs = specs_cbs;
freqmag_cbStr = specscbs.freqmag_popup;
freqmag_fseb_cbStr = {specscbs.fs_eb,'freqmag'};
freqmag_eb_cbStr = {specscbs.eb,'freqmag'};

freqmag.popup = ...
    render_popup(hFig,sz,freqmag.popup_lbl,freq_popup_strs,indent,...
    freqmag_cbStr,tag); 

% Put up the sampling frequency label in front of the freq. units popup
freqmag.fs_lbl = render_fs_lbl(hFig,bgc,sz,freqmag.popup_lbl,...
    freqmag.popup,indent,tag,'beside');

% Put up the sampling frequency edit box in front of the Fs label
freqmag.fs_eb = ...
    render_fs_eb(hFig,sz,freqmag.fs_lbl,freqmag.popup,...
    indent,freqmag_fseb_cbStr,tag,'beside');

% Render the edit boxes and labels for freq vector, mag vector, weights, etc.
freqmag = render_freqmag_specs(hFig,bgc,sz,tag,freqmag,freqmag_eb_cbStr);

%----------------------------------------------------------------------------
function h_frame = render_frame(hFig,bgc,sz,adj_frame_hndl,framelabel,tag,frame)
%RENDER_FRAME   Renders a frame with label given an adjacent frame handle.
%
%   This function is used to render both the freq. specs frame and the
%   mag. specs. frame.
%
%   Inputs: same as above plus
%     adj_frame_hndl - handle to the adjacent frame.
%     framelabel     - string containing the label to put on the frame.
%     frame          - string indicating which frame to build (freq, mag or freqmag)
%
%   Outputs:
%     h_frame - two element array of handles returned by framewlabel.

% Get the position of the adjacent frame
adj_frame_pos = get(adj_frame_hndl,'position');

% Define the frame height
frHeight = sz.fh2+sz.fh3+sz.vffs;

switch frame,
case {'freq','mag'},
     
    % Set up the frame position
    framePos = [...
            adj_frame_pos(1)+adj_frame_pos(3)+sz.ffs...
            adj_frame_pos(2)+adj_frame_pos(4)-frHeight...
            adj_frame_pos(3)...
            frHeight];
        
case 'freqmag',
    
    % Set up the frame position    
    framePos = [...
            adj_frame_pos(1)+adj_frame_pos(3)+sz.ffs...
            adj_frame_pos(2)+adj_frame_pos(4)-frHeight...
            2*adj_frame_pos(3)+sz.ffs ...
            frHeight];
    
end

% Generate the frame with the label and return vector of handles
h_frame = framewlabel(hFig,framePos,framelabel,tag,bgc);
%----------------------------------------------------------------------------
function popup_lbl = render_popup_lbl(hFig,bgc,sz,frame,tag,lbl)
%RENDER_POPUP_LBL   Renders the label to the popup in a given frame.
%
%    Renders the 'Units:' label in both the freq. specs. frame and the
%    mag. specs. frame.
%
%   Inputs: same as above plus
%     frame - handle to the frame in which the label will live.
%
%   Outputs:
%     popup_lbl - handle to the popup label.

framePos = get(frame,'position');
% Put up popup label
units_lbl_width = largestuiwidth({lbl});
units_lbl_pos = [framePos(1)+sz.hfus, ...
        framePos(2)+framePos(4)-sz.uh-2*sz.vfus-sz.lblTweak,...
        units_lbl_width,...
        sz.uh];

popup_lbl = uicontrol('style','text',...
        'BackgroundColor',bgc,...
        'position',units_lbl_pos,...
        'visible','off',...
        'string',lbl,...
        'tag',tag,...
        'HorizontalAlignment','left');
    
%----------------------------------------------------------------------------
function popup = render_popup(hFig,sz,popup_lbl,popup_strs,indent,cbStr,tag)
%RENDER_POPUP   Renders the popup in a given frame.
%
%    Renders the units popup in both the freq. specs. frame and the
%    mag. specs. frame.
%
%   Inputs: same as above plus
%     popup_lbl - handle to the popup label.
%     cbStr     - string to be used as the callback string.
%
%   Outputs:
%     popup - handle to the popup.

units_lbl_pos = get(popup_lbl,'position');
% Define the width of the popup
popup_width = sz.ebw+3*indent;

popup_pos = [units_lbl_pos(1)+units_lbl_pos(3),...
        units_lbl_pos(2)+sz.lblTweak, ...
        popup_width,...
        sz.uh];

popup = uicontrol('style','popup',...
    'BackgroundColor','white',...
    'position',popup_pos,...
    'string',popup_strs,...
    'tag',tag,...
    'visible','off',...
    'callback',cbStr);

%----------------------------------------------------------------------------
function fs_lbl = render_fs_lbl(hFig,bgc,sz,freq_popup_lbl,freq_popup,indent,tag,location)
%RENDER_Fs_LBL   Renders the sampling frequency label.
%
%
%   Inputs: same as above plus
%     freq_popup_lbl - handle to the freq. units popup label
%     freq_popup     - handle to the freq. units popup
%
%   Outputs:
%     fs_lbl - handle to the fs label.

lbl = 'Fs:';

units_lbl_pos = get(freq_popup_lbl,'position');
units_popup_pos = get(freq_popup,'position');

% Get lbl width
uiWidth = largestuiwidth({lbl});

switch location,
case 'below',
    lbl_pos = [units_lbl_pos(1)+indent,...
            units_lbl_pos(2)-(sz.uh+sz.uuvs+sz.lblTweak),...
            uiWidth,...
            sz.uh];
case 'beside',
    lbl_pos = [units_popup_pos(1)+units_popup_pos(3)+sz.uuvs,...
            units_lbl_pos(2),...
            uiWidth,...
            sz.uh];
end

fs_lbl = uicontrol('style','text',...
    'BackgroundColor',bgc,...
    'position',lbl_pos,...
    'visible','off',...
    'string',lbl,...
    'tag',tag,...
    'HorizontalAlignment','left');

%-----------------------------------------------------------------------------
function fs_eb = render_fs_eb(hFig,sz,fs_lbl,freq_popup,indent,eb_cbStr,tag,location)
%RENDER_Fs_EB   Renders the sampling frequency edit box.
%
%    Renders the edit boxin front of the 'Fs:' label.
%
%   Inputs: same as above plus
%     freq_popup - handle to the popup in the freq. specs frame.
%
%   Outputs:
%     fs_eb - handle to the fs edit box.

fs_lbl_pos = get(fs_lbl,'position');
popup_pos  = get(freq_popup,'position');

switch location,
case 'below',
    eb_pos = [popup_pos(1)+2*indent , ...
            fs_lbl_pos(2)+sz.lblTweak,...
            sz.ebw+indent,...
            sz.uh];
case 'beside',
    eb_pos = [fs_lbl_pos(1)+fs_lbl_pos(3)+sz.uuvs, ...
            fs_lbl_pos(2)+sz.lblTweak,...
            sz.ebw,...
            sz.uh];
end


fs_eb = uicontrol('style','edit',...
    'BackgroundColor','white',...
    'position',eb_pos,...
    'tag',tag,...
    'visible','off',...
    'HorizontalAlignment','left',...
    'callback',eb_cbStr);

%-----------------------------------------------------------------------------
function freq = render_freq_specs(hFig,bgc,sz,tag,freq,...
    freq_eb_cbStr)
%RENDER_FREQ_SPECS   Render edit boxes and labels for the freq.
%                       specs. frame, for all methods.

% There are up to four labels and edit boxes
teststr = 'teststring';
freq_lbls = {teststr,teststr,teststr,teststr};

freq.lbl = render_freq_lbl(hFig,bgc,sz,freq.fs_lbl,freq_lbls,tag);
freq.eb = render_freq_eb(hFig,sz,freq.fs_eb,freq_lbls,freq_eb_cbStr,tag);

%-----------------------------------------------------------------------------
function mag = render_mag_specs(hFig,bgc,sz,tag,mag,indent,...
    mag_eb_cbStr)
%RENDER_LP_MAG_SPECS   Render edit boxes and labels for the mag.
%                           specs. frame, for all methods.

% There are up to three labels and edit boxes
teststr = 'teststring';
mag_lbls = {teststr,teststr,teststr};

mag.lbl = ...
    render_mag_lbl(hFig,bgc,sz,mag.popup_lbl,mag_lbls,indent,tag);

mag.eb = ...
    render_mag_eb(hFig,sz,mag.popup,mag_lbls,mag_eb_cbStr,indent,tag);


%----------------------------------------------------------------------
function freqmag = render_freqmag_specs(hFig,bgc,sz,tag,freqmag,freqmag_eb_cbStr)
%RENDER_FREQMAG_SPECS  Render edit boxes and labels for the freqmag (freq, amplitude and weight)
%                  specs frame.

% Render for labels and four long edit boxes
teststr = 'teststring';
freqmag_lbls = {teststr,teststr,teststr,teststr,teststr};

freqmag.lbl = ...
    render_freqmag_lbls(hFig,bgc,sz,tag,freqmag_lbls,freqmag.popup_lbl);

freqmag.eb = ...
    render_freqmag_ebs(hFig,sz,tag,freqmag_lbls,freqmag.popup,...
    freqmag.fs_eb,freqmag.fs_lbl,freqmag_eb_cbStr);

%-----------------------------------------------------------------------------
function freq_lbl = render_freq_lbl(hFig,bgc,sz,fs_lbl,labels,tag)
%RENDER_FREQ_LBL   Renders all labels (except Fs) in the freq. specs. frame.
%
%    Renders the labels for all types (lowpass,highpass,bandpass,bandstop)
%    in the freq. specs. frame (e.g. Fpass, etc.).
%
%   Inputs: same as above plus
%     fs_lbl - handle to the fs label in the freq. specs frame.
%     labels - cell array containig the labels to be rendered.
%
%   Outputs:
%     freq_lbl - array of handles to the labels.

fs_lbl_pos = get(fs_lbl,'position');

% Find uiextent width of longest label
uiWidth = largestuiwidth(labels);

for n=1:length(labels),
    
    % Put up the labels 
    lbl_pos = [fs_lbl_pos(1),...
            fs_lbl_pos(2)-n*(sz.uh+sz.uuvs+sz.lblTweak),...
            uiWidth,...
            sz.uh];
    
    freq_lbl(n) = uicontrol('style','text',...
        'BackgroundColor',bgc,...
        'position',lbl_pos,...
        'visible','off',... % visibility is turned on in update_specs
        'string',labels{n},...
        'tag',tag,...
        'HorizontalAlignment','left');
end

%--------------------------------------------------------------------------------
function freq_eb = render_freq_eb(hFig,sz,fs_eb,labels,freq_eb_cbStr,tag)
%RENDER_FREQ_EB   Renders all edit boxes (except Fs) in the freq. specs. frame.
%
%    Renders the edit boxes for all types (lowpass,highpass,bandpass,bandstop)
%    in the freq. specs. frame.
%
%   Inputs: same as above plus
%     fs_eb - handle to the fs edit box.
%     labels - cell array containig the labels to be rendered.
%
%   Outputs:
%     freq_eb - array of handles to the edit boxes.

fs_eb_pos = get(fs_eb,'position');

for n=1:length(labels),
    
    % Put up the edit boxes
    eb_pos = [fs_eb_pos(1),...
            fs_eb_pos(2)-n*(sz.uh+sz.uuvs+sz.lblTweak),...
            fs_eb_pos(3),...
            sz.uh];
    
    freq_eb(n) = uicontrol('style','edit',...
        'BackgroundColor','white',...
        'position',eb_pos,...
        'tag',tag,...
        'visible','off',... % visibility is turned on in update_specs
        'HorizontalAlignment','left',...
        'callback',freq_eb_cbStr);
end

%-------------------------------------------------------------------------------
function mag_lbl = render_mag_lbl(hFig,bgc,sz,mag_popup_lbl,labels,indent,tag);
%RENDER_MAG_LBL   Renders all labels in the mag. specs. frame.
%
%    Renders the labels for all types (lowpass,highpass,bandpass,bandstop)
%    in the mag. specs. frame (e.g. Apass, etc.).
%
%   Inputs: same as above plus
%     mag_popup_lbl - handle to the 'Units:' label.
%     labels - cell array containig the labels to be rendered.
%
%   Outputs:
%     mag_lbl - array of handles to the labels.

mag_popup_lbl_pos = get(mag_popup_lbl,'position');

% Find uiextent width of longest label
uiWidth = largestuiwidth(labels);

for n=1:length(labels),
    
    % Put up the labels 
    lbl_pos = [mag_popup_lbl_pos(1)+indent,...
            mag_popup_lbl_pos(2)-(n+1)*(sz.uh+sz.uuvs+sz.lblTweak),...
            uiWidth,...
            sz.uh];
    
    mag_lbl(n) = uicontrol('style','text',...
        'BackgroundColor',bgc,...
        'position',lbl_pos,...
        'visible','off',... 
        'string',labels{n},...
        'tag',tag,...
        'HorizontalAlignment','left');
end

%--------------------------------------------------------------------------------
function mag_eb = render_mag_eb(hFig,sz,mag_popup,labels,mag_eb_cbStr,indent,tag)
%RENDER_MAG_EB   Renders all edit boxes in the mag. specs. frame.
%
%    Renders the edit boxes for all types (lowpass,highpass,bandpass,bandstop)
%    in the mag. specs. frame.
%
%   Inputs: same as above plus
%     mag_popup - handle to the units popup.
%     labels - cell array containig the labels to be rendered.
%
%   Outputs:
%     mag_eb - array of handles to the edit boxes.

mag_popup_pos = get(mag_popup,'position');

for n=1:length(labels),
    
    % Define position
    eb_pos = [mag_popup_pos(1)+2*indent,...
            mag_popup_pos(2)-(n+1)*(sz.uh+sz.uuvs+sz.lblTweak),...
            sz.ebw+indent,...
            sz.uh];
    % Render the edit boxes invisible
    mag_eb(n) = uicontrol('style','edit',...
        'BackgroundColor','white',...
        'position',eb_pos,...
        'tag',tag,...
        'visible','off',...
        'HorizontalAlignment','left',...
        'callback',mag_eb_cbStr);
end
%--------------------------------------------------------------------
function freqmag_lbls = ...
    render_freqmag_lbls(hFig,bgc,sz,tag,labels,freq_popup_lbl)
%RENDER_FREQMAG_LBLS  Render the labels for the freqmag (freq, amplitude and weights)
%                 specs frame.

freq_popuplbl_pos = get(freq_popup_lbl,'position');

% Find uiextent width of longest label
uiWidth = largestuiwidth(labels);

for n = 1:length(labels),
    
    lbl_pos = [freq_popuplbl_pos(1),...
            freq_popuplbl_pos(2)-n*(sz.uh+sz.uuvs+sz.lblTweak),...
            uiWidth,...
            sz.uh];
    
    freqmag_lbls(n) = uicontrol('style','text',...
        'BackgroundColor',bgc,...
        'position',lbl_pos,...
        'visible','off',... 
        'string',labels{n},...
        'tag',tag,...
        'HorizontalAlignment','left');

end

%--------------------------------------------------------------------
function freqmag_ebs = ...
    render_freqmag_ebs(hFig,sz,tag,labels,freq_popup,fs_eb,fs_lbl,freqmag_eb_cbStr)
%RENDER_FREQMAG_LBLS  Render the labels for the freqmag (freq, amplitude and weights)
%                 specs frame.

freq_popup_pos = get(freq_popup,'position');
fs_eb_pos = get(fs_eb,'position');
fs_lbl_pos = get(fs_lbl,'position');


for n = 1:length(labels),
    
    eb_pos = [freq_popup_pos(1),...
            freq_popup_pos(2)-n*(sz.uh+sz.uuvs+sz.lblTweak),...
            freq_popup_pos(3)+2*sz.uuvs+fs_eb_pos(3)+fs_lbl_pos(3),...
            sz.uh];
    
    freqmag_ebs(n) = uicontrol('style','edit',...
        'BackgroundColor','white',...
        'position',eb_pos,...
        'tag',tag,...
        'visible','off',...
        'HorizontalAlignment','left',...
        'callback',freqmag_eb_cbStr);

end

% Correct the position of the last edit box, i.e. make it narrower
lasteb_pos = get(freqmag_ebs(end),'position');
lasteb_pos(3) = 35;
set(freqmag_ebs(end),'position',lasteb_pos);

    
    
% [EOF] specs_frames.m
