function hConvertdlg = render_convertdlg(hFig)
% RENDER_CONVERTTDLG Render the Convert Filter Structure dialog (figure).
%                    This function renders the dialog and all its uicontrols. 
%                    It returns their handles in the structure lud.h (local UserData).
%
% Input:
%   hFig - Handle to the Filter Design GUI figure.
%
% Outputs:
%  hConvertdlg - Handle to the Convert filter structure dialog (figure).
%

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

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

% Render the "Convert" dialog (figure).
lud.h.fig.convertdlg = render_dialog(hFig);
hConvertdlg = lud.h.fig.convertdlg;

% Render the "Convert To" frame.
lud.h.convertto = convertTo_frame(hConvertdlg, hFig);

% Render the "Second-order sections" frame.
lud.h.sosscale = scaling_frame(hConvertdlg, lud.h.convertto.frame);

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

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

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


%----------------------------------------------------------------------
function hConvertdlg = render_dialog(hFig)
% RENDER_DIALOG Render the Convert dialog (figure).
%
% Input:     
%   hFig - Handle to the Filter Design GUI figure.
%
% Output:
%   hConvertdlg -  Handle to the Convert filter structure dialog (figure).

sz = gui_sizes;

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

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

% Dialog Properties
dlgProperties = {'Name','Convert structure',...
        'Color',[.8 .8 .8],...
        'Tag','ConvertDialog',...
        'WindowStyle','Normal'};
set(hConvertdlg,dlgProperties{:});


%----------------------------------------------------------------------
function convertto = convertTo_frame(hConvertdlg, hFig)
%CONVERTTO_FRAME Render the "Convert To" frame and listbox.
%
% Inputs:
%   hConvertdlg - Handle to the Convert filter structure dialog.
%   hFig        - Handle to the Filter Design GUI figure.
%
% Output:
%  convertto  - Structure containing handles to all the uicontrols in
%               the "Convert To" frame.

sz = gui_sizes;
bgc = get(hConvertdlg,'Color');
fdatoolud = get(hFig,'UserData');

% Get Convert dialog figure position
dlgPos = get(hConvertdlg,'Position'); 

% Render "Convert To" frame
frWidth = 183.1250*sz.pixf;  % Filter Order frame width (173.125)
frHeight = sz.fh3;
frYpos = dlgPos(4) - sz.vffs - frHeight; 
frXpos= (dlgPos(3)-frWidth)/2; % Center in dialog
frPos = [frXpos frYpos frWidth frHeight];
frame = framewlabel(hConvertdlg,frPos,'Convert To',...
    'convertto_frame',bgc);
set(frame,'Visible','Off');


% Render the listbox in the "Convert To" frame.
strs = getconvertstructchoices(hFig); 

% Get the Listbox Position
width = 160*sz.pixf; 
xPosOffset = (frPos(3)-width)/2; % Center in X 
x = frPos(1)+xPosOffset;
chkboxUpperEdge = frPos(2) + sz.vfus + sz.uh;
y = chkboxUpperEdge + sz.uuvs;    
height = 70*sz.pixf;

listBoxPos = [x y width height];

% NOTE: The convertdlg_cbs function updates the figure's userdata
convertdlgcbs = convertdlg_cbs;
cbStr = convertdlgcbs.convertTo_cb;
      
h_convertToListBx = uicontrol(hConvertdlg,...
    'Style','listbox',...
    'BackgroundColor','White',...
    'Position',listBoxPos,...
    'Visible','Off',...
    'String',strs,...
    'Tag','convertto_listbox',...
    'CallBack',cbStr);

% Render the "Use second-order sections" checkbox.
h_soschkbox = soschkbox(hConvertdlg, frPos, listBoxPos(1));

convertto.frame     = frame;
convertto.soschkbox = h_soschkbox;     
convertto.listbox   = h_convertToListBx;


%----------------------------------------------------------------------
function sosscale = scaling_frame(hConvertdlg,h_converttoframe)
%SCALING_FRAME  Render the "Second-order sections" frame and its uicontrols.
%
% Inputs:
%   hConvertdlg      - Handle to the Convert filter structure dialog.
%   h_converttoframe - Handle to the "Convert To" frame.
%
% Output:
%  sosscale  - Structure containing handles to all the uicontrols in
%              the Scaling frame.
%
% This frame (and uicontrols) are enabled only for Second-order section 
% filter structure.

% Sizes, Positions, and Color
sz = gui_sizes;
dlgPos = get(hConvertdlg,'Position'); 
convertToPos = get(h_converttoframe(1),'Position');
bgc = get(hConvertdlg,'Color');

% Render "Scaling" frame
frWidth = 183.1250*sz.pixf;   % Filter Order frame width (173.125)
frHeight = sz.fh4+10; % Defined in gui_sizes
ypos = dlgPos(4) - 2*sz.vffs - frHeight - convertToPos(4); 
frPos = [convertToPos(1) ypos frWidth frHeight];

frame = framewlabel(hConvertdlg,frPos,'Second-Order Sections',...
    'convert_sosscaleframe',bgc);
set(frame,'Visible','On');

% Render the Second-order sections label and popup (uicontrols).
h_sosscaleuis = render_sosscaleuis(hConvertdlg, frPos);

sosscale.frame     = frame;
sosscale.lblNpopup = h_sosscaleuis;


%----------------------------------------------------------------------
function h_ctrlbtns = ctrlbtns(hConvertdlg, h_convertcoeffsframe);
% CTRLBTNS Renders the Control buttons ("OK" & "Cancel" & "Apply") 
%
% These control buttons are used on the "Convert" dialog.
%
% Inputs:
%   hConvertdlg          - Handle to the Convert Coefficients dialog (figure).
%   h_convertcoeffsframe - 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(hConvertdlg,'Position');
frPos = get(h_convertcoeffsframe(1),'Position');
bgc = get(hConvertdlg,'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 converttdlg_cbs function updates the figure's userdata
convertdlgcbs = convertdlg_cbs;
okCbStr = convertdlgcbs.okPushBtn_cb;

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

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

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


%-------------------------------------------------------------------
%                       Utility Functions
%-------------------------------------------------------------------
function h_sosscaleuis = render_sosscaleuis(hConvertdlg, frPos)
% RENDER_SOSSCALEUIS Renders the SOS Scale popupmenu and label in the
%                    Scaling frame.
%
% Inputs:
%   hConvertdlg - Handle to the Convert dialog (figure).
%   frPos       - Variable Names frame position.
%
% Output:
%   h_sosscaleuis - A vector of handles to the Second-order section 
%                   label and popupmenu (uicontrols).

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

lbl = {'Scale:'};
strs = {'None','L-2','L-infinity'};
uiWidth = largestuiwidth(strs,'popup'); % 63;
lblWidth = largestuiwidth(lbl,'text');  % 34 ;
totalWidth =  uiWidth + lblWidth + sz.uuhs;

% Render SOS Label (text uicontrol)
xOffset = (frPos(3)- totalWidth)/2; % Center in both X & Y
lblXpos = frPos(1) + xOffset;
yOffset = (frPos(4) - sz.uh)/2;
lblYpos = frPos(2) + yOffset - sz.lblTweak;
lblPos = [lblXpos lblYpos lblWidth sz.uh];

h_soslbl = uicontrol(hConvertdlg,...
    'Style','Text',...
    'BackgroundColor',bgc,...
    'Position',lblPos,...
    'Enable','Off',...
    'Visible','On',...
    'String',lbl,...
    'Tag','convert_sosscalelbl',...
    'HorizontalAlignment','Left');

% Render SOS popupmenu
popupXPos = lblXpos + lblWidth + sz.uuhs;
popupYpos = lblYpos + sz.lblTweak;
popupPos = [popupXPos popupYpos uiWidth sz.uh];

convertdlgcbs = convertdlg_cbs;
cbStr = convertdlgcbs.scalePop_cb;

h_sospop = uicontrol(hConvertdlg,...
    'Style','Popup',...
    'BackgroundColor',bgc,...
    'Position',popupPos,...
    'Enable','Off',...
    'Visible','On',...
    'String',strs,...
    'Tag','convert_sosscalepop',...
    'Callback',cbStr);

h_sosscaleuis = [h_soslbl h_sospop];


%----------------------------------------------------------------------
function h_soschkbox = soschkbox(hConvertdlg, frPos, listbxXpos);
% SOSCHKBOX  Render the "Use second-order sections" checkbox 
%            within the "Convert To" frame.
%
% Inputs:
%   hConvertdlg  - Handle to the Convert dialog (figure).
%   frPos        - Convert To frame Position.
%   listbxXpos   - X position of the listbox in the "Convert To" frame.
%
% Outputs:
%   h_soschkbox  - Handle to the SOS checkbox.

% Sizes, Positions, and Color
sz = gui_sizes;
bgc = get(hConvertdlg,'Color');

% Checkbox width
label = {'Use second-order sections'};
chkWidth = largestuiwidth(label,'check')+15*sz.pixf;

% Check box position 
chkPos = [listbxXpos frPos(2)+sz.vfus chkWidth sz.uh];

convertdlgcbs = convertdlg_cbs;
soschkbox = convertdlgcbs.sosChkBox_cb;

h_soschkbox = uicontrol(hConvertdlg,...
    'Style','Check',...
    'BackgroundColor',bgc,...
    'Position',chkPos,...
    'CallBack',soschkbox,...
    'Value',0,...
    'Visible','Off',...
    'String',label,...
    'Tag','convert_soschkbox');

% [EOF] render_convertdlg.m
