function fcns = converttdlg_cbs
%CONVERTDLG_CBS Handles all the callbacks executed by the uicontrols in the Convert 
%               Dialog (figure).

%   Author(s): P. Costa
%   Copyright 1988-2000 The MathWorks, Inc.
%   $Revision: 1.11 $  $Date: 2000/06/13 21:23:24 $ 

% "Export" the handles to all local functions via a structure.
% The caller will get this, and grab the handles they need.
% This will allow callers to gain direct access to local functions.
fcns.convertTo_cb      = @convertTo_cb;
fcns.sosChkBox_cb      = @sosChkBox_cb;
fcns.scalePop_cb       = @scalePop_cb;
fcns.okPushBtn_cb      = @okPushBtn_cb;    
fcns.cancelPushBtn_cb  = @cancelPushBtn_cb;
fcns.applyPushBtn_cb   = @applyPushBtn_cb;


%-------------------------------------------------------------------------
function convertTo_cb(hcbo, eventStruct)
% CONVERTTO_CB Callback for the listbox in the "Convert To" frame
%
% Inputs:
%   hcbo        - Handle to the current callback object (listbox). 
%   eventStruct - Not used.

% This callback will update FDATool's UserData (fdatoolud.specs.convert) 
% structure with the current target filter structure (filter structure 
% to be converted to)

hConvertdlg = gcbf;
convertdlgud = get(hConvertdlg,'UserData');

hFig = convertdlgud.h.fig.fdatool;
fdatoolud = get(hFig,'UserData');

% Determine currently selected "Convert To" option 
convertToStrs = get(hcbo,'String');
convertToVal  = get(hcbo,'Value');
currentConvertToStr = convertToStrs{convertToVal};

% Set target filter structure back in FDATool's UserData.
if fdatoolud.flags.quantizationswitch & fdatoolud.flags.fdqexists,
    
    % Cache previous filter structure selection; Used in the
    % case when "Cancel" is clicked.
    fdatoolud.specs.convert.cancel.qconvertto = ...
        fdatoolud.specs.convert.qconvertto;
    
    fdatoolud.specs.convert.qconvertto = currentConvertToStr;
else
    fdatoolud.specs.convert.cancel.dconvertto = ...
        fdatoolud.specs.convert.dconvertto;

    fdatoolud.specs.convert.dconvertto = currentConvertToStr;
end

% Enable or disable the SOS popupmenu
enableSOSfromChk(convertdlgud);

% Save the data in FDATool's UserData
set(hFig,'UserData',fdatoolud);


%-------------------------------------------------------------------------
function sosChkBox_cb(hcbo, eventStruct)
% SOSCHKBOX_CB Callback for the "Use section-order sections" checkbox.
%
% Inputs:
%   hcbo        - Handle to the current callback object (Filter Design
%                 Toolbox specific checkbox.
%   eventStruct - Not used.

hConvertdlg = gcbf;
convertdlgud = get(hConvertdlg,'UserData');

hFig = convertdlgud.h.fig.fdatool;
fdatoolud = get(hFig,'UserData');

% Cache previous checkbox value.
fdatoolud.specs.convert.cancel.soschkbox = ...
    fdatoolud.specs.convert.soschkbox;

% Store checkbox value in FDATool's UserData 
fdatoolud.specs.convert.soschkbox = get(hcbo,'Value');

% Enable or disable the SOS popupmenu
enableSOSfromChk(convertdlgud);

% Save the data in FDATool's UserData
set(hFig,'UserData',fdatoolud);


%-------------------------------------------------------------------------
function scalePop_cb(hcbo, eventStruct)
% SCALEPOP_CB Callback for the Scale (Second-order sections) popupmenu.
%
% Inputs:
%   hcbo        - Handle to the Scale popupmenu. 
%   eventStruct - Not used.

% Sets FDATool's UserData strcuture with appropriate Scale string.

hConvertdlg = gcbf;
convertdlgud = get(hConvertdlg,'UserData');

hFig = convertdlgud.h.fig.fdatool;
fdatoolud = get(hFig,'UserData');

% Get SOS scale option from the popupmenu
scaleStrs = get(hcbo,'String');
selectedScale = get(hcbo,'Value');

% Cache previous SOS scale option.
fdatoolud.specs.convert.cancel.sosScale = ...
    fdatoolud.specs.convert.sosScale;

% Save current SOS scale option to FDATool's UserData
fdatoolud.specs.convert.sosScale = scaleStrs{selectedScale};

set(hFig,'UserData',fdatoolud);


%-------------------------------------------------------------------------
function okPushBtn_cb(hcbo, eventStruct)
% OKPUSHBTN_CB Callback function for the "OK" pushbutton.
%              The OK button will convert your filter structure 
%              and close the dialog.
%
% Inputs:
%   hcbo        - Not used. 
%   eventStruct - Not used.
%

hConvertdlg = gcbf;
convertdlgud = get(hConvertdlg,'UserData');
hFig = convertdlgud.h.fig.fdatool; 

% Convert the Filter Structure
update_convertdlg('convertFilter',hFig);

% Close the Convert dialog.
delete(hConvertdlg);


%-------------------------------------------------------------------------
function cancelPushBtn_cb(hcbo, evenStruct)
% CANCELPUSHBTN_CB Callback function for the "Cancel" pushbutton
%
% Inputs:
%   hcbo        - Not used. 
%   eventStruct - Not used.

hConvertdlg = gcbf;
convertdlgud = get(hConvertdlg,'UserData');
hFig = convertdlgud.h.fig.fdatool; 
fdatoolud = get(hFig,'UserData');

if isfield(fdatoolud.specs.convert,'cancel'),
    
    % Get the names of the Convert dialog selections to restore
    canceledChoices = fieldnames(fdatoolud.specs.convert.cancel);
    
    % Restore the previous choices in FDATool's UserData
    for n = 1:length(canceledChoices),
        fdatoolud.specs.convert = ...
            setfield(fdatoolud.specs.convert,canceledChoices{n},...
            getfield(fdatoolud.specs.convert.cancel,canceledChoices{n}));
    end
    set(hFig,'UserData',fdatoolud);
end

% Delete the dialog (figure)
delete(gcbf); 


%-------------------------------------------------------------------------
function applyPushBtn_cb(hcbo, evenStruct)
% APPLYPUSHBTN_CB Callback function for the "Apply" pushbutton
%                 The Apply button will convert your filter 
%                 structure and leave the dialog on top (current figure).
%
% Inputs:
%   hcbo        - Not used. 
%   eventStruct - Not used.
%

hConvertdlg = gcbf;
convertdlgud = get(hConvertdlg,'UserData');
hFig = convertdlgud.h.fig.fdatool; 

% Convert the Filter Structure
update_convertdlg('convertFilter',hFig);


%-------------------------------------------------------------------
%                       Utility Functions
%-------------------------------------------------------------------
function enableSOSfromChk(convertdlgud)
% ENABLESOSFROMCHK Enable the SOS scale label and popupmenu
%                  based on the FD Toolbox SOS Checkbox.
%                
% Input:
%   convertdlgud - Convert dialog UserData structure 

chkVal = get(convertdlgud.h.convertto.soschkbox,'Value');
bgc = get(convertdlgud.h.fig.convertdlg,'Color');

if chkVal,
    set(convertdlgud.h.sosscale.lblNpopup,'Enable','On');
    set(convertdlgud.h.sosscale.lblNpopup(2),'BackgroundColor','White');
else
    set(convertdlgud.h.sosscale.lblNpopup,'Enable','Off');
    set(convertdlgud.h.sosscale.lblNpopup,'BackgroundColor',bgc);
end

% [EOF] convertdlg_cbs.m
