function fcns = exportdlg_cbs
%EXPORTDLG_CBS Handles all the callbacks executed by the uicontrols in the Export 
%              Dialog (figure).

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

% "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.exportTo_cb       = @exportTo_cb;
fcns.exportcoeffeb_cb  = @exportcoeffeb_cb;
fcns.varChkBox_cb      = @varChkBox_cb;
fcns.okPushBtn_cb      = @okPushBtn_cb;    
fcns.cancelPushBtn_cb  = @cancelPushBtn_cb;
fcns.applyPushBtn_cb   = @applyPushBtn_cb;

%-------------------------------------------------------------------------
function exportTo_cb(hcbo, eventStruct)
% EXPORTTO_CB Callback for the Popupmenu in the "Export To" frame
%
% Inputs:
%   hcbo        - Handle to the current callback object (Popupmenu). 
%   eventStruct - Not used.
%
% This callback will update FDATool's UserData (fdatoolud.specs.export) 
% structure with the current "Export To" option 

hExportdlg = gcbf;
exportdlgud = get(hExportdlg,'UserData');

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

% Determine currently selected "Export To" option 
exportToStrs = get(hcbo,'String');
exportToVal  = get(hcbo,'Value');
currentExportToStr = exportToStrs{exportToVal};

% Store the currentExportToStr option in FDATool's UserData
fdatoolud.specs.export.exportto = currentExportToStr;
    
% Save the data in FDATool's UserData
set(hFig,'UserData',fdatoolud);

% Set the appropriate UI Enable/BackgroundColor within this frame.
setExportUisEnable(hFig, hExportdlg);


%-------------------------------------------------------------------------
function exportcoeffeb_cb(hcbo, eventStruct)
% EXPORTCOEFFEB_CB Callback for the Filter Coefficient Edit boxes.
%
% Inputs:
%   hcbo        - Not used. 
%   eventStruct - Not used.
%
% This callback function will update FDATool's UserData 
% (fdatoolud.specs.export.*VarStrs) structure with the current 
% Variable strings (to be exported).

hExportdlg = gcbf;
exportdlgud = get(hExportdlg,'UserData');

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

% Handles to all the Export Coefficient uicontrols 
h_exportCoeffuis = exportdlgud.h.exportcoeffuis.editboxes;

% Cache current Filter Structure
filtStruct = getfilterstructure(fdatoolud.filter);

switch filtStruct,
case {'df1','df2','df1t','df2t','dffir','dftfir','dfsymfir','dfasymfir'},    
    indx = [1:2];
    % If filter is in SOS form, export the SOS matrix
    if issos(fdatoolud.filter)  & nsections(fdatoolud.filter) > 1,
        fsStrs = 'sosVarStrs';
    else
        fsStrs = 'tfVarStrs';
    end
    
case 'ssd'
    indx = [1:4];
    fsStrs = 'ssVarStrs';
    
case 'latcallpass',          
    indx = [1];
    fsStrs = 'latcapsVarStrs';
    
case 'latcma',  
    indx = [1];
    fsStrs = 'latcmaminVarStrs';
    
case 'latcmax',  
    indx = [1];
    fsStrs = 'latcmamaxVarStrs';
    
case 'latcarma',             
    indx = [1:2];
    fsStrs = 'latcarmaVarStrs';
    
case {'latcca','latccapc'},
    % Currently exporting b & a for lattice coupled-allpass structures     
    indx = [1:2];
    fsStrs = 'tfVarStrs';
    
case 'Quantized filter (Qfilt object)', 
    indx = [1];
    fsStrs = 'quantizedVarStrs';
    
otherwise
    % For example: "Parallel Filter"
    indx = [1:2];
    fsStrs = 'tfVarStrs';
end

% Index into the array of handles for the edit boxes.
h_ebs = h_exportCoeffuis(indx);

% Deblank the user entered variable strings and re-set them in the 
% uicontrols. Set the Variable strings back to UD
fdatoolud.specs.export = ...
    setfield(fdatoolud.specs.export, fsStrs, fixup_uiedit(h_ebs));

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


%-------------------------------------------------------------------------
function varChkBox_cb(hcbo, eventStruct)
% VARCHKBOX_CB Callback function for the Variable collision checkbox.
%
% Inputs:
%   hcbo        - Handle to the current callback object (Checkbox)
%   eventStruct - Not used.

hExportdlg = gcbf;
exportdlgud = get(hExportdlg,'UserData');

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

chkVal = get(hcbo,'Value'); 

% Store the checkbox value in UD
fdatoolud.specs.export.overwritechk = chkVal;

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


%-------------------------------------------------------------------------
function okPushBtn_cb(hcbo, eventStruct)
% OKPUSHBTN_CB Callback function for the "OK" pushbutton
%
% Inputs:
%   hcbo        - Not used. 
%   eventStruct - Not used.

hExportdlg = gcbf;
exportdlgud = get(hExportdlg,'UserData');
hFig = exportdlgud.h.fig.fdatool;

% Export the coefficients 
update_exportdlg('exportFilter',hFig);

% Get fdatoolud after the call to update_exportdlg because it 
% could set the "fdatoolud.flags.exportwarnoccurred" flag
fdatoolud = get(hFig,'UserData');

% If warning occurred, don't delete the dialog.
if isfield(fdatoolud.flags,'exportwarnoccurred') ...
        & fdatoolud.flags.exportwarnoccurred == 1;
    return; 
end

% Close the Convert dialog.
delete(hExportdlg);


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

delete(gcbf);


%-------------------------------------------------------------------------
function applyPushBtn_cb(hcbo, evenStruct)
% APPLYPUSHBTN_CB Callback function for the "Apply" pushbutton
%
% Inputs:
%   hcbo        - Not used. 
%   eventStruct - Not used.
%
% The Apply button will export your coefficients, disable itself, and
% leave the dialog.

hExportdlg = gcbf;
exportdlgud = get(hExportdlg,'UserData');

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

% Export the coefficients 
update_exportdlg('exportFilter',hFig);


%-------------------------------------------------------------------------
function setExportUisEnable(hFig, hExportdlg)
% SETEXPORTUISENABLE Set the Enable/BackgroundColor properties for the
%                    uicontrols within the Export To frame.
% 
% Inputs:
%   hFig       - Handle to the Filter Design GUI figure.
%   hExportdlg - Handle to the Export Coefficients dialog (figure).


fdatoolud = get(hFig,'UserData');
exportdlgud  = get(hExportdlg,'UserData');

% Current Export To selection
exportToOption = fdatoolud.specs.export.exportto;

hchkbox = exportdlgud.h.overwritechk;
heboxes = exportdlgud.h.exportcoeffuis.editboxes;

switch exportToOption,
case 'Workspace',
    set(hchkbox,'Enable','On');
    set(heboxes,'Enable','On','BackgroundColor','White');
case 'Text-file',
    set([hchkbox heboxes],'Enable','Off','BackgroundColor',[.8 .8 .8]);
case 'MAT-file',
    set(hchkbox,'Enable','Off');
    set(heboxes,'Enable','On','BackgroundColor','White');
end

% [EOF] exportdlg_cbs.m
