function ud = render_gui(ud)
%RENDER_GUI Renders the filter design GUI FDTool.  
%           This function renders all the possible uicontrols that will 
%           display in FDTool.  However, the visibility of many of the 
%           uicontrols are set to off until their corresponding update
%           function sets the visibility to on.

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

% Render figure.
hFig = render_fig('FilterDesigner');

% Cache the current default font size and use our own size.
oldUIfontsize = get(hFig,'DefaultUIControlFontSize');
set(hFig,'DefaultUIControlFontSize',8);

% Render all the uicontrols and return their handles in the structure h.
ud.h = render_all_uis(hFig,ud);
ud.h.Fig = hFig;  % Also store figure handle in userdata.

% Set the units of all the uicontrols to normalized.
setAlluiUnits(ud.h,'normalized'); % Allows cheap resizing.

% Set the font size back to original and make figure visible.
set(hFig,'DefaultUIControlFontSize',oldUIfontsize);

% Make the GUI visible but hide the GUI's figure handle from gcf, etc.
set(hFig,'Visible','on','HandleVisibility','Callback'); 

%----------------------------------------------------------------------
                      %%%%%%%%%%%%%%%%%%%%%
                      %  Local Functions  %
                      %%%%%%%%%%%%%%%%%%%%%
                         
%----------------------------------------------------------------------
function hFig = render_fig(figTag)
% RENDER_FIG Renders a figure for the Filter Design Tool.
%

% Keep figure invisible until all the uicontrols are displayed.
hFig = figure('visible','off','IntegerHandle','off');

% Determine the figure position relative to screen size and resolution
figurePos = figpos(720, 549); % (width, height)

% Set up figure HG properties.
menuscbs = menus_cbs;  % get all function handles
fig_props = {...
        'CloseRequestFcn', menuscbs.exit_cb, ...
        'Color','default',...
        'MenuBar','none',...
        'Name','Test',... 
        'NextPlot','add',...
        'NumberTitle','off',...
        'Units','pixels',...
        'WindowStyle','normal',...
        'HandleVisibility','on',...
        'Tag',figTag,... 
        'Position',figurePos};

set(hFig,fig_props{:});

%----------------------------------------------------------------------
function h = render_all_uis(hFig,ud)
% RENDER_ALL_UIS Renders all the uicontrols in GUI.
%
% Input:
%   hFig - handle to GUI's figure.
%   ud   - data structure containing the state of the GUI.
%
% Output:
%   h - a structure containing the handles to all the uicontrols rendered
%       in FDTool (including the handle to the figure).

% Render the menus.
[h.menus,typesNmethods] = render_menus(hFig,ud);

% Render the toolbar.
h.toolbar = render_toolbar(hFig,ud);

% Render the frame which contains the current filter information.
h.currentfilterinfo = render_currentfilterinfo(hFig, ud);
h_currentfiltFr = h.currentfilterinfo.frame(1);

% Render the Quantization switch frame if the FD Toolbox is installed.
if ud.flags.fdqexists,
end

% Render the Analysis frame.
h.analysis = render_analysisarea(hFig,h_currentfiltFr);

% Render the mode buttons and their frame.
h.modeFr = render_modebtns(hFig,ud.flags.fdqexists);
h_modeFr = h.modeFr.frame;

% Render recessed frame where all other frames will be rendered.
h.recessedFr = recessedFrame(hFig,h_modeFr);

% Render the design parameter frames and their contents.
h.design_params = render_designparams(hFig,h_modeFr,typesNmethods);
h_filtTypesFr = h.design_params.filtTypes.frame(1);
h_filtOrderFr = h.design_params.filtOrder.frame(1);

% Render the Specify Filter Coefficients frame and their contents.
h.import_params = render_importparams(hFig,ud,h_filtTypesFr);

% Render the quantization switch frame and quantization parameters tab 
% if the FD Toolbox is installed.
if ud.flags.fdqexists,
    h.qswitch = render_quantizationswitch(hFig);
    h_qswitchFr = h.qswitch.frame(1);
    h.quantize_params = render_quantizeparams(hFig,h_filtTypesFr,h_filtOrderFr);
end

% Render the action button and frame.
h.actionFr = render_actionbtn(hFig,h_filtTypesFr);

% Render the status box.
h.status = status_box(hFig);

%----------------------------------------------------------------------
function h_frame = recessedFrame(hFig,h_adjacentFrame)
% Renders the recessed frame where all the frames will be rendered.

% Uicontrol sizes and spaces.
sz = gui_sizes;

% Get info about figure.
bgc = get(hFig,'color');

% Get the figure's position and the position of an adjancent frame
% to determine this frame's position.
figPos = get(hFig,'Position');
pos = get(h_adjacentFrame,'Position');

x = sz.ffs/2;
w = figPos(3) - sz.ffs/2;
y = sz.uh+sz.ffs/2;
h = pos(2)-y; %figPos(4)-2.8*sz.uh;

% Render the recessed frame that occupies the space between the 
% mode buttons and the status text.
h_frame = recessedbox([x y w h]);

%----------------------------------------------------------------------
function h_status = status_box(hFig)
% STATUS_BOX Renders the status box at the bottom of the GUI.

% Uicontrol sizes and spaces.
sz = gui_sizes;

% Get info about figure.
bgc = get(hFig,'color');

figPos = get(hFig,'Position');
x = sz.ffs;
w = figPos(3) - sz.ffs/2;

% Default status text - "Ready"
statusTextPos = [x,.002*sz.pixf,w-4,sz.lh]; %[.015 .016 1-.03 .032]
h_status.Text = uicontrol('parent',hFig, ...
   'Style','text', ...
   'Units','pixels', ...
   'Position',statusTextPos, ...
   'HorizontalAlignment','left', ...
   'BackgroundColor',bgc, ...
   'Tag','status_box',...
   'String','-Test-');

%----------------------------------------------------------------------

                         %%%%%%%%%%%%%%%%%%%%%%%%%%%%%
                         %  Local Utility Functions  %
                         %%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
%----------------------------------------------------------------------
function figurePos = figpos(width, height)
%FIGPOS - Get a figure Position which is independent of the 
%         Screen Resolution

% Cache sizes and spacing used in the GUI.
sz = gui_sizes;

% Figure out screen resolution.
oldRootUnits = get(0,'Units');
set(0, 'Units', 'pixels');
pixfactor = sz.pixf;

% Calculate figure position according to resolution.
figurePos = get(0,'DefaultFigurePosition');
figurePos(3:4) = [width height];
figurePos = figurePos * pixfactor;

% Make sure the title bar of the window isn't off the screen
rootScreenSize = get(0,'ScreenSize');

% (position is [x(from left) y(bottom edge from bottom) width height]
% check left edge and right edge
if ((figurePos(1) < 1) ...
      | (figurePos(1)+figurePos(3) > rootScreenSize(3)))
   figurePos(1) = 30;
end

% Make sure top of figure is not off the screen.
if ((figurePos(2) < 1) ...
      | (figurePos(2)+figurePos(4) > rootScreenSize(4)))
   figurePos(2) = figurePos(2)-((figurePos(2)+figurePos(4)+100)-rootScreenSize(4));
end

set(0, 'Units', oldRootUnits);

%----------------------------------------------------------------------
function h_frame = recessedbox(pos)
%RECESSEDBOX Uses an axis and lines to give the illusion of a recessed box.
%
% Input: 
%   pos - the x,y position width and height of the recessed box.

% Use axis to create the frame.
h_frame = axes(...
    'Units','pixels',....
    'Visible','off',...
    'Tag','recessedframe',...
    'Position',pos);
set(h_frame,...
    'units','normalized',...
    'xlim',[0 1],'ylim',[0 1]);

% Create the recessed visual effect with lines.
h1 = line([0 1 1],[0 0 1],'color','white','linewidth',2);
h2 = line([0 0 1],[0 1 1],'color',[1 1 1]*0,'linewidth',1);

set(h_frame,'HandleVisibility','off');  % Don't allow zooming.

%----------------------------------------------------------------------
function setAlluiUnits(h,units)
% setAlluiUnits Sets all (except uimenus and uitoolbar) uicontrol
%               (in hFig) units to the input arg units.
% Inputs:
%   h     - structure containing the handles to GUI's figure & uicontrols.
%   units - HG uicontrol units such as: pixels, normalized, etc.

% Cache handle to the GUI's figure.
hFig = h.Fig;

% Use default units if no units was specified.
if nargin < 2, units = 'Normalized'; end

% Find the handles to all uicontrols and from that list remove
% uimenus and uitoolbar.  Then set all units to normalized.
hChil = get(hFig,'Children');
all_ui_types = get(hChil, 'Type');
indx = find(~strcmp(all_ui_types,'uitoolbar') &...
   ~strcmp(all_ui_types,'uimenu'));

% Set the units of all uicontrols.
set(hChil(indx),'Units',units);

% Can't use findobj on the following two axes because their
% handlevisibiltity are set to off.  So, use the explicit handle name.
set(h.analysis.frame(1),'Units',units);
set(h.recessedFr,'Units',units);

% [EOF] render_gui.m


