function h = render_quantizeparams(hFig,h_filtTypesFr,h_filtOrderFr)
% RENDER_QUANTIZEPARAMS Renders all the frames and uicontrols for the 
%                       quantized filter coefficients mode.

%   Thomas A. Bryan
%   Copyright 1988-2000 The MathWorks, Inc.
%   $Revision: 1.9 $  $Date: 2000/07/24 20:59:34 $ 

% Notation:
%   lud:  Local user data.

cb = quantize_cbs;

% Render the Quantized Filter Objects frame.
h.qfiltobjs = quant_qfiltobjs_frame(hFig,h_filtTypesFr,cb);

% Render the Quantizer Properties frame.
h.quantizerprops = quant_quantizerprops_frame(hFig,h_filtTypesFr,cb);

% Make a list of all the handles on the quantized page that can be enabled 
enablehandles = findobj(hFig,'Tag','quantize_params');
for k=1:length(enablehandles)
  v = get(enablehandles(k));
  if ~isfield(v,'Enable')
    % If the object cannot be enabled, then remove it from the list
    enablehandles(k) = [];
  end
end

% Store the handles so they will end up in the figure's user data.
h.enablehandles = enablehandles;


                         %%%%%%%%%%%%%%%%%%%%%
                         %  Local Functions  %
                         %%%%%%%%%%%%%%%%%%%%%

%----------------------------------------------------------------------
function qfiltobjs = quant_qfiltobjs_frame(hFig,h_filtTypesFr,cb)
% QUANT_qfiltobjS_FRAME Renders the qfilt objects frame.


% Uicontrol sizes and spaces.
sz = gui_sizes;

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

% Render Quantize Filter Objects frame.
framePos = get(h_filtTypesFr,'position');

% frame is a 2 element vector containing the handles to the frame & its label.
frame = framewlabel(hFig,framePos,'',...
    'quantize_params',bgc);
% Keep frame invisible until user selects Quantize Coeffs... mode.
set(frame, 'visible','off'); 

qfiltobjs.frame = frame;

xleft = framePos(1) + sz.vfus;
ytop = framePos(2) + framePos(4) - sz.hfus;

x = xleft;
y = ytop;
h = sz.uh;
skip = sz.uuvs;

y = y - h - skip;
label = sprintf('Scale transfer-fcn coeffs <= 1');
w = largestuiwidth({label},'pushbutton');
qfiltobjs.normalize = ...
    uicontrol('style','push', ...
    'string',label, ...
    'position',[x y w h], ...
    'backgroundcolor',bgc, ...
    'foregroundcolor','k', ...
    'visible','off', ...
    'tag','quantize_params', ...
    'callback',{cb.normalizefilter,hFig});

y = y - h - skip;
label = sprintf('Input/output scaling:');
w = largestuiwidth({label},'text');
qfiltobjs.scaleheader = ...
    uicontrol('style','text',...
    'position',[x y w h], ...
    'string',label, ...
    'horizontalalignment','center',...
    'backgroundcolor',bgc,...
    'visible','off',...
    'tag','quantize_params');

y = y - h; % - floor(skip/2);
w = framePos(3)-2*sz.hfus;
qfiltobjs.scalevalues = ...
    uicontrol('style','edit', ...
    'string','1', ...
    'position',[x y w h], ...
    'backgroundcolor','w',...
    'foregroundcolor','k',...
    'visible','off',...
    'horizontalalignment','left',...
    'tag','quantize_params',...
    'callback',{cb.setscalevalues,hFig});

y = y - h - 2*skip;
label = sprintf('Show filter structures...');
w = largestuiwidth({label},'pushbutton');
qfiltobjs.showfilterstructures = ...
    uicontrol('style','push',...
    'position',[x y w h], ...
    'string',label, ...
    'horizontalalignment','center',...
    'backgroundcolor',bgc,...
    'visible','off',...
    'callback','qfiltconstruction',...
    'tag','quantize_params');


%----------------------------------------------------------------------
function quantizerprops = quant_quantizerprops_frame(hFig,h_filtTypesFr,cb)
%QUANT_QUANTIZERPROPS_FRAME renders the quantizer properties frames.

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

% Uicontrol sizes and spaces.
sz = gui_sizes;

framePos = get(h_filtTypesFr,'position');
% framePos = frame position [x y w h].
framePos(1) = framePos(1) + framePos(3) + sz.ffs;
framePos(3) = figPos(3)-framePos(1) - sz.hfus;

% frame is a 2 element vector containing the handles to the frame & its label.
frame= framewlabel(hFig,framePos,'','quantize_params',bgc);
% Keep frame invisible until user selects Quantize Coeffs... mode.
set(frame, 'visible','off'); 

quantizerprops.frame = frame;

xleft = framePos(1) + sz.vfus;
ytop =  framePos(2) + framePos(4) - sz.hfus;

% Put up labels
formatlabels = {'Coefficient:','Input:','Output:','Multiplicand:','Product:','Sum:'};
x = xleft;
y = ytop;
w = largestuiwidth(formatlabels,'text');
h = sz.uh;
skip = sz.uuvs;
popwtweak = sz.popwTweak;
% Position: [x y width height]
% x is measured from the left
% y is measured from the bottom
for k=1:length(formatlabels)
  y = y - h - skip;
  quantizerprops.formatlabels(k) = ...
      uicontrol('style','text',...
      'position',[x y w h], ...
      'string',formatlabels{k}, ...
      'horizontalalignment','right',...
      'backgroundcolor',bgc,...
      'visible','off',...
      'tag','quantize_params');
end

% Put up grid of quantization properties
[quantizerprops.quantizerhead,quantizerprops.quantizers,x,w] = popupmenus(...
    hFig,...
    'Quantizer type:',...
    {'quantizer','unitquantizer'},...
    [1,1,1,1,1,1],x,ytop,w,h,skip,popwtweak,bgc,cb.setlengths);


[quantizerprops.modehead,quantizerprops.modes,x,w] = popupmenus(...
    hFig,...
    'Mode:',...
    {'fixed','float','double','single'},...
    [1,1,1,1,1,1],x,ytop,w,h,skip,popwtweak,bgc,cb.setmode);

[quantizerprops.roundhead,quantizerprops.roundmodes,x,w] = popupmenus(...
    hFig,...
    'Round mode:',...
    {'ceil','convergent','fix','floor','round'},...
    [5,4,4,4,4,4],x,ytop,w,h,skip,popwtweak,bgc,cb.setlengths);

[quantizerprops.overflowhead,quantizerprops.overflowmodes,x,w] = popupmenus(...
    hFig,...
    'Overflow mode:',...
    {'saturate','wrap'},...
    [1,1,1,1,1,1],x,ytop,w,h,skip,popwtweak,bgc,cb.setlengths);

[quantizerprops.formathead,quantizerprops.formats,x,w] = editboxes(...
    hFig,...
    'Format:',...
    {'[16,  15]','[32,  30]'},...
    [1,1,1,1,2,2],x,ytop,w,h,skip,bgc,cb.setlengths);
    
initmodes(quantizerprops)
for k=1:6
  feval(cb.storemodes,quantizerprops,k)
end

feval(cb.setfixed,quantizerprops,1:6)

%----------------------------------------------------------------------
function [headhandle,handles,x,w] = popupmenus(hFig,head,labels,defaults,...
    xold,ytop,wold,h,skip,popwtweak,bgc,fun)
%POPUPMENUS renders the popup menus.

x = xold + wold + skip;     % Move x over
y = ytop;             % Reset y
w1 = largestuiwidth(labels,'popup');
w2 = largestuiwidth({head},'text');
w = max(w1,w2);
handles = zeros(1,6);
% Write header
headhandle = ...
    uicontrol(hFig,'style','text',...
    'position',[x y w h], ...
    'string',head, ...
    'horizontalalignment','center',...
    'backgroundcolor',bgc,...
    'visible','off',...
    'tag','quantize_params');
lud.labels = labels;
% Write popups
for k=1:6
  y = y - h - skip;
  handles(k) = ...
      uicontrol(hFig,'style','popupmenu', ...
      'string',labels, ...
      'value',defaults(k), ...
      'position',[x y w h], ...
      'backgroundcolor','w', ...
      'foregroundcolor','k',...
      'visible','off',...
      'tag','quantize_params',...
      'userdata',lud,...
      'callback',{fun,hFig,k});
end

%----------------------------------------------------------------------
function [headhandle,handles,x,w] = editboxes(hFig,head,labels,defaults,...
    xold,ytop,wold,h,skip,bgc,fun)
%EDITBOXES renders the edit boxes.

x = xold + wold + skip;     % Move x over
y = ytop;             % Reset y
w1 = largestuiwidth(labels,'text');
w2 = largestuiwidth({head},'popup');
w = max(2*w1,w2);
handles = zeros(1,6);
str = labels{1};
% Write header
headhandle = ...
    uicontrol(hFig,'style','text',...
    'position',[x y w h], ...
    'string',head, ...
    'horizontalalignment','center',...
    'backgroundcolor',bgc,...
    'visible','off',...
    'tag','quantize_params');
lud.labels = labels;
% Put up edit boxes
for k=1:6
  y = y - h - skip;
  handles(k) = ...
      uicontrol(hFig,'style','edit', ...
      'string',labels{defaults(k)}, ...
      'position',[x y w h], ...
      'backgroundcolor','w',...
      'foregroundcolor','k',...
      'visible','off',...
      'tag','quantize_params',...
      'userdata',lud,...
      'callback',{fun,hFig,k});
end

%----------------------------------------------------------------------
function initmodes(h)
%INITMODES Initialize modes.
%    Store the values for the row into the user data of the mode uicontrol. 
for k=1:6
  lud = get(h.modes(k),'userdata');
  lud.roundmode = get(h.roundmodes(k),'value');
  lud.overflowmode = get(h.overflowmodes(k),'value');
  lud.format = get(h.formats(k),'string');
  set(h.modes(k),'userdata',lud);
end

