function hExportdlg = render_exportdlg(hFig)
% RENDER_EXPORTDLG Render the Export Coefficients dialog box.
%
% Input:
%   hFig - Handle to the Filter Design GUI figure.
%
% Outputs:
%  hExportdlg - Handle to the Export Coefficients dialog figure.

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

% Render all the uicontrols and return their handles in the structure lud.h.

% FDATool's UserData structure
fdatoolud = get(hFig,'UserData');

% Render the "Export" dialog (figure).
lud.h.fig.exportdlg = render_dialog(hFig);
hExportdlg = lud.h.fig.exportdlg;

% Render the "Export To" frame.
lud.h.exportto = exportTo_frame(hExportdlg);

% Render the "Variable Names" frame.
lud.h.exportcoeffuis = exportcoeff_frame(hExportdlg, lud.h.exportto.frame);

% Render the Overwrite variable collision checkbox
lud.h.overwritechk = overwriteVarChk(hExportdlg,lud.h.exportcoeffuis.frame);

% Render the Control buttons ("OK" & "Close" & "Apply")
lud.h.ctrlbtns = ctrlbtns(hExportdlg,lud.h.exportcoeffuis.frame);

% Store all local handles in the dialog's UserData.
lud.h.fig.fdatool   = hFig;
set(hExportdlg,'UserData',lud);

% Store dialog handle back in FDATool's UserData 
fdatoolud.h.dlgs.export = hExportdlg;
set(hFig,'UserData',fdatoolud);


%----------------------------------------------------------------------
function hExportdlg = render_dialog(hFig)
% RENDER_DIALOG Render the Export Coefficients dialog (figure).
%
% Input:
%   hFig - Handle to the Filter Design GUI figure.
%
% Output:
%  hExportdlg -  Handle to the Export Coefficients dialog (figure).

sz = gui_sizes;

% Render the dialog (figure).
hExportdlg = dialog('Visible','Off');

% Set figure position and center in on FDATool.
set(hExportdlg,'Position',[600 500 205 225]*sz.pixf);
centerfigonfig(hFig,hExportdlg);

% Dialog Properties
dlgProperties = {'Name','Export',...
        'Color',[.8 .8 .8],...
        'Tag','ExportDialog',...
        'WindowStyle','Normal'};
set(hExportdlg,dlgProperties{:});


%----------------------------------------------------------------------
function exportto = exportTo_frame(hExportdlg)
%EXPORTTO_FRAME Render the "Export To" frame and Popupmenu.
%
% Input:
%   hExportdlg  - Handle to the Export Coefficients dialog (figure).
%
% Output:
%  exportto  - Structure containing handles to all the uicontrols in
%              the "Export To" frame.

sz = gui_sizes;
bgc = get(hExportdlg,'Color');

% Get Export dialog figure position
dlgPos = get(hExportdlg,'Position'); 

% Render "Export To" frame
frWidth = 183.1250*sz.pixf;  % Filter Order frame width (173.125)
frHeight = sz.fh4;           % Defined in gui_sizes (35*pf)
frYpos = dlgPos(4) - sz.vffs - frHeight; 

% Center the Frame in the dialog
frXpos= (dlgPos(3)-frWidth)/2;
frPos = [frXpos frYpos frWidth frHeight];
frame = framewlabel(hExportdlg,frPos,'Export To',...
    'exportto_frame',bgc);
set(frame,'Visible','Off');

% Render the popupmenu in the "Export To" frame.
exportToStrs = {'Workspace',...
        'Text-file',...
        'MAT-file'};
popupWidth = largestuiwidth(exportToStrs,'popup')+15*sz.pixf; 

% Center popupmenu in frame (both X and Y)
xPosOffset = (frPos(3)-popupWidth)/2;
frXpos = frPos(1)+xPosOffset;
yPosOffset = (frPos(4)-sz.uh)/2;
frYpos = frPos(2)+yPosOffset;
popupPos = [frXpos frYpos popupWidth sz.uh];

% NOTE: The exportdialog_cbs function updates the figure's userdata
exportdlgcbs = exportdlg_cbs;
cbStr = exportdlgcbs.exportTo_cb;
      
h_exportToPop = uicontrol(hExportdlg,...
    'Style','Popup',...
    'BackgroundColor','White',...
    'Position',popupPos,...
    'Visible','Off',...
    'String',exportToStrs,...
    'Tag','exportto_popup',...
    'Callback',cbStr);

exportto.frame = frame;
exportto.popup = h_exportToPop;
    

%----------------------------------------------------------------------
function exportCoeff = exportcoeff_frame(hExportdlg,h_exporttoframe)
%EXPORTCOEFF Render the "Variable Names" frame.
%
% Inputs:
%   hExportdlg      - Handle to the Export Coefficients dialog (figure).
%   h_exporttoframe - Handle to the "Export To" frame.
%
% Output:
%  exportCoeff   - Structure containing handles to all the uicontrols in
%                  the Variable Names frame.

% Sizes, Positions, and Color
sz = gui_sizes;
dlgPos = get(hExportdlg,'Position'); 
exportToPos = get(h_exporttoframe(1),'Position');
bgc = get(hExportdlg,'Color');

% Render "Variable Names" frame
frWidth = 183.1250*sz.pixf;   % Filter Order frame width (173.125)
frHeight = sz.fh3+10*sz.pixf; % Defined in gui_sizes (61*pf)
ypos = dlgPos(4) - 2*sz.vffs - frHeight - exportToPos(4); 
frPos = [exportToPos(1) ypos frWidth frHeight];

frame = framewlabel(hExportdlg,frPos,'Variable Names',...
    'export_varnamesframe',bgc);
set(frame,'Visible','Off');

% Render the edit boxes to display coefficient variables.
h_coeffEditBoxes = coeffeditboxes(hExportdlg,frPos);

% Render the coefficient labels (for the edit boxes).
h_coeffLbls = coefflbls(hExportdlg,frPos);

exportCoeff.frame      = frame;
exportCoeff.editboxes  = h_coeffEditBoxes;
exportCoeff.lbls       = h_coeffLbls;


%----------------------------------------------------------------------
function h_overwritechk = overwriteVarChk(hExportdlg,h_exportcoeffsframe)
% OVERWRITEVARCHK Renders the Overwrite variable collision checkbox.
%
% Inputs:
%   hExportdlg          - Handle to the Export Coefficients dialog (figure).
%   h_exportcoeffsframe - Handle to the "Variable Names" frame.
%
% Outputs:
%   h_overwritechk  - Handle to the Overwrite variable collision checkbox.

% Sizes, Positions, and Color
sz = gui_sizes;
dlgPos = get(hExportdlg,'Position'); 
varNamesFrPos = get(h_exportcoeffsframe(1),'Position');
bgc = get(hExportdlg,'Color');

% Checkbox width
label = {'Overwrite existing variables'};
chkWidth = largestuiwidth(label,'check')+15*sz.pixf;

% X position for the Edit boxes
chkXPos = varNamesFrPos(1)+sz.hfus;
chkPos = [chkXPos varNamesFrPos(2)+sz.vfus chkWidth sz.uh];

exportdlgcbs = exportdlg_cbs;
varChkCbStr = exportdlgcbs.varChkBox_cb;

h_overwritechk = uicontrol(hExportdlg,...
    'Style','Check',...
    'BackgroundColor',bgc,...
    'Position',chkPos,...
    'CallBack',varChkCbStr,...
    'Value',0,...
    'Visible','Off',...
    'String',label,...
    'Tag','export_variablechk');


%----------------------------------------------------------------------
function h_ctrlbtns = ctrlbtns(hExportdlg, h_exportcoeffsframe);
% CTRLBTNS Renders the Control buttons ("OK" & "Cancel" & "Apply") 
%
% These control buttons are used on the "Export" dialog.
%
% Inputs:
%   hExportdlg          - Handle to the Export Coefficients dialog (figure).
%   h_exportcoeffsframe - Handle to the Variable Names Frame. 
%
% Output:
%   h_ctrlbtns  - A vector of three handles for the control buttons 
%                 OK, Cancel, and APPLY. 

% Sizes, Positions, and Color
sz = gui_sizes;
dlgPos = get(hExportdlg,'Position');
frPos = get(h_exportcoeffsframe(1),'Position');
bgc = get(hExportdlg,'Color');

% Control button positions (from the right to the left)
ctrlStrs = {sprintf('Apply'),sprintf('Cancel'),'OK'};
uiWidth = largestuiwidth(ctrlStrs,'Pushbutton')+10*sz.pixf;

% Apply button Position
applyXpos = frPos(1)+frPos(3)-uiWidth-sz.hfus;
applyPbPos = [applyXpos 2*sz.vfus uiWidth sz.bh];

% Cancel button Position
cancelLbl = {'Cancel'};

% X distance between OK & Apply
distXpos = frPos(3)-2*sz.hfus-2*uiWidth; 
xOffset = (distXpos-uiWidth)/2;
cancelxpos = frPos(1)+sz.hfus+uiWidth + xOffset;
cancelPbPos = [cancelxpos 2*sz.vfus uiWidth sz.bh];

% OK button Position
okXpos = frPos(1)+sz.hfus;
okPbPos = [okXpos cancelPbPos(2) uiWidth sz.bh];

% NOTE: The exportdialog_cbs function updates the figure's userdata
exportdlgcbs = exportdlg_cbs;
okCbStr = exportdlgcbs.okPushBtn_cb;

% Render the "OK" pushbutton 
h_ctrlbtns(1) = uicontrol(hExportdlg,...
    'Style','Push',...
    'BackgroundColor',bgc,...
    'Position',okPbPos,...
    'Visible','On',...
    'Enable','On',...
    'String',ctrlStrs{3},...
    'Tag','export_okpushbutton',...
    'Callback',okCbStr);

% Render the "Cancel" pushbutton 
cancelCbStr = exportdlgcbs.cancelPushBtn_cb;
h_ctrlbtns(2) = uicontrol(hExportdlg,...
    'Style','Push',...
    'BackgroundColor',bgc,...
    'Position',cancelPbPos,...
    'Visible','On',...
    'Enable','On',...
    'String',ctrlStrs{2},...
    'Tag','export_closepushbutton',...
    'Callback',cancelCbStr);

% Render the "Apply" pushbutton 
applyCbStr = exportdlgcbs.applyPushBtn_cb;
h_ctrlbtns(3) = uicontrol(hExportdlg,...
    'Style','Push',...
    'BackgroundColor',bgc,...
    'Position',applyPbPos,...
    'Visible','On',...
    'Enable','On',...
    'String',ctrlStrs{1},...
    'Tag','export_applypushbutton',...
    'Callback',applyCbStr);


%-------------------------------------------------------------------
%                       Utility Functions
%-------------------------------------------------------------------
function h_coeffEditBoxes = coeffeditboxes(hExportdlg, frPos)
% COEFFEDITBOXES Render the Filter Coefficients Edit boxes.
%
% Renders a fixed number of edit boxes (four due to the State-space 
% case) for displaying the Filter structure specific variables (edit
% box strings) to be exported.
%
% Inputs:
%   hExportdlg  - Handle to the Export Coefficients dialog (figure).
%   frPos       - Variable Names frame position.
%
% Output:
%   h_coeffEditBoxes  - Handles to the filter coefficients labels

sz = gui_sizes;
defaultStr = {'Test'};

% Starting uicontrol Y position
uiUpperEdge = frPos(2)+frPos(4)-2*sz.vfus; 

% X position for the Edit boxes
editBoxXpos = frPos(1)+frPos(3) - sz.hfus - sz.ebw;

% NOTE: The exportdialog_cbs function updates the figure's userdata
exportdlgcbs  = exportdlg_cbs;
exportCoeffCbStr = exportdlgcbs.exportcoeffeb_cb;


% Render the maximum number of filter coefficient labels (4 for State-space) 
for n = 1:4,
    editBoxPos = [editBoxXpos uiUpperEdge sz.ebw sz.lh]-n*[0 sz.uh+sz.lblTweak 0 0];
    h_coeffEditBoxes(n) = uicontrol(hExportdlg,...
        'Style','Edit',...
        'BackgroundColor','White',...
        'Position',editBoxPos,...
        'Visible','Off',...
        'HorizontalAlignment','Left',...
        'String',defaultStr,...
        'Callback',exportCoeffCbStr,...
        'Tag','exportfiltercoeffs_uis');
end


%----------------------------------------------------------------------
function h_coeffLbls = coefflbls(hExportdlg,frPos)
% COEFFLBLS Render the Filter Coefficient labels.
%
% Renders a fixed number of Text uicontrols (four due to the State-space 
% case) for displaying the Filter structure specific labels in front of 
% coefficient edit boxes.
%
% Inputs:
%   hExportdlg  - Handle to the Export Coefficients dialog (figure).
%   frPos       - Variable Names frame position.
%
% Output:
%   h_coeffLbls  - Handles to the static filter coefficients labels

sz = gui_sizes;
bgc = get(hExportdlg,'Color');

% "Quantized filter:" was arbitrarily selected for the default
% uicontrol width. The final width is set in update_exportdlg.
defaultStr = {'Quantized filter:'};
lblWidth = largestuiwidth(defaultStr,'Text'); 

% Starting uicontrol Y position
uiUpperEdge = frPos(2)+frPos(4)-2*sz.vfus; 

% Render the maximum number of filter coefficient labels (4 for State-space) 
lblXpos = frPos(1)+5*sz.pixf; % Using 5*sz.pixf instead of sz.hfus to accomidate
                              % the "Quantized filter:" label.
for n = 1:4,
    lblpos = [lblXpos uiUpperEdge lblWidth sz.lh]-n*[0 sz.uh+sz.lblTweak 0 0];
    h_coeffLbls(n) = uicontrol(hExportdlg,...
        'Style','Text',...
        'BackgroundColor',bgc,...
        'Position',lblpos,...
        'Visible','Off',...
        'String',defaultStr,...
        'Tag','exportfiltercoeffs_lbls',...
        'HorizontalAlignment','Left');
end

% [EOF] render_exportdlg.m
