function fcns = importparams_cbs
%IMPORTPARAMS_CBS Handles all the callbacks executed by the uicontrols 
%                 when the GUI is in the import mode.

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

% "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.impFiltStruct_cbs = @impFiltStruct_cbs;
fcns.importcoeff_eb    = @importcoeff_eb;
fcns.clearpush_cb      = @clearpush_cb;
fcns.import_fsUnits    = @import_fsUnits;
fcns.import_fs         = @import_fs;


%-------------------------------------------------------------------------
function impFiltStruct_cbs(hcbo, eventStruct)
%IMPFILTSTRUCT_CBS Filter Structure Popup callback (Import mode).

hFig = gcbf;
ud = get(hFig,'UserData');

% Cache previous filter structure string from ud
prevFiltStruct = ud.specs.import.impfiltstruct.struct;

% Cache handle to the filter structure popup
h_pop = ud.h.import_params.importFilter.pop;
structStrs = get(h_pop,'String');
selectedStruct = get(h_pop,'Value');

% Determine currently selected filter structure 
currentFiltStruct = structStrs{selectedStruct};

% Update ud if filter structure has changed
if ~strcmp(prevFiltStruct, currentFiltStruct),
    % Save current filter structure to ud
    ud.specs.import.impfiltstruct.struct = currentFiltStruct;
    set(hFig,'Userdata',ud);

    set_flagsdirty(hFig);
        
    % Update the Filter Structure frame
    update_importparams('update_impfiltstruct',hFig);
    update_importparams('update_coffsframe',hFig);
end


%-------------------------------------------------------------------------
function importcoeff_eb(hcbo, eventStruct)
%IMPORTCOEFF_EB  Get the coefficients from the Edit boxes (variables 
%                or values) and build the filter object.
    
hFig = gcbf;
ud = get(hFig,'Userdata');

% Handles to Filter coefficient Edit Boxes.
h_eb = ud.h.import_params.importFilter.coeffebs;

% Convert the strings used in the filter structure popupmenu and stored
% in the userdata to strings used throughout the code.
filtstructStr = importstructurestr(ud, ud.specs.import.impfiltstruct.struct);

% Determine how many uicontrols to display.
switch  filtstructStr,
case 'df1',       
    hCoeffs  = h_eb(1:2);
    varStrs = 'tfVarStrs';
case 'df2',       
    hCoeffs  = h_eb(1:2);
    varStrs = 'tfVarStrs';
case 'df1t',      
    hCoeffs  = h_eb(1:2);
    varStrs = 'tfVarStrs';
case 'df2t',      
    hCoeffs  = h_eb(1:2);
    varStrs = 'tfVarStrs';
case 'sos',       
    hCoeffs  = h_eb(1:2);
    varStrs = 'sosVarStrs';
case 'ss',        
    hCoeffs  = h_eb(1:4);
    varStrs = 'ssVarStrs';
case 'lattaps',   
    hCoeffs = h_eb(1:2);
    varStrs = 'lattapsVarStrs';
case 'lattmamin', 
    hCoeffs = h_eb(1);
    varStrs = 'lattmaminVarStrs';
case 'lattmamax', 
    hCoeffs = h_eb(1);
    varStrs = 'lattmamaxVarStrs';
case 'lattarma',  
    hCoeffs = h_eb(1:2);
    varStrs = 'lattarmaVarStrs';
case 'qfilt',     
    hCoeffs = h_eb(1);
    varStrs = 'qfiltVarStrs';
end

% Deblank the user entered variable strings and re-set them in the 
% uicontrols. Set the Variable strings back to UD
ud.specs.import.impfiltstruct = ...
    setfield(ud.specs.import.impfiltstruct, varStrs, fixup_uiedit(hCoeffs));
set(hFig,'Userdata',ud);

set_flagsdirty(hFig);

% Update the Apply button.
update_actionbtn('update_enablestate',hFig);


%-------------------------------------------------------------------------
function clearpush_cb(hcbo, eventStruct)
%CLEARPUSH_CB Callback for the "Clear" pushbuttons in the "Specify
%             Filter Coefficients" frame.

% Cache handle to current callback object (Clear push button)
h_currentCLR = gcbo;

hFig = gcbf;   % Cache figure handle
ud = get(hFig,'Userdata');

% Handles to Filter Coefficient Edit Boxes.
h_eb = ud.h.import_params.importFilter.coeffebs;

% Handles to the Filter coeficient "Clear" buttons.
h_pb = ud.h.import_params.importFilter.clrpbs;

% Find the index of the currently clicked "Clear" button in the vector
% of all Clear button handles and set the String of the corresponding
% Edit box to empty.
idx = find(h_currentCLR == h_pb);
set(h_eb(idx),'String','');

% Get a shorten version of the strings used in the filter structure popupmenu. 
filtstructStr = importstructurestr(ud, ud.specs.import.impfiltstruct.struct);

% Determine how many uicontrols to display.
switch  filtstructStr,
case 'df1',       varstr = 'tfVarStrs';
case 'df2',       varstr = 'tfVarStrs';
case 'df1t',      varstr = 'tfVarStrs';
case 'df2t',      varstr = 'tfVarStrs';
case 'sos',       varstr = 'sosVarStrs';
case 'ss',        varstr = 'ssVarStrs';
case 'lattaps',   varstr = 'lattapsVarStrs';
case 'lattmamin', varstr = 'lattmaminVarStrs';
case 'lattmamax', varstr = 'lattmamaxVarStrs';
case 'lattarma',  varstr = 'lattarmaVarStrs';
case 'qfilt',     varstr = 'qfiltVarStrs';
end

ud.specs.import.impfiltstruct = setfield(ud.specs.import.impfiltstruct,...
    varstr, get(h_eb(idx),'String'));

set(hFig,'Userdata',ud);

set_flagsdirty(hFig);
update_actionbtn('update_enablestate',hFig);


%-------------------------------------------------------------------------
function import_fsUnits(hcbo, eventStruct)
%IMPORT_FSUNITS Sampling Frequency Units Popupmenu callback (Specify Coeffs. tab)

hFig = gcbf;
ud = get(hFig,'UserData');

% Cache previous sampling frequency units 
prev_fsUnits = ud.specs.import.fs.freqUnits;

% Get new units from the popupmenu
h_fsPop = ud.h.import_params.importFs.fspop;
fsUnitStrs = get(h_fsPop,'String');
indx = get(h_fsPop,'Value');
current_fsUnits = fsUnitStrs{indx};

% Update ud if Sampling Frequency units have changed
if ~strcmp(prev_fsUnits, current_fsUnits),
    % Save current sampling frequency units to ud
    ud.specs.import.fs.freqUnits = current_fsUnits;
    set(hFig,'Userdata',ud);

    set_flagsdirty(hFig);
    update_importparams('update_importFsPopup',hFig);
    update_actionbtn('update_enablestate',hFig);
end


%-------------------------------------------------------------------------
function import_fs(hcbo, eventStruct)
%IMPORT_FS Sampling Frequency editbox callback (Specify Coeffs. tab)

hFig = gcbf;
ud = get(hFig,'UserData');

% Cache previous Sampling Frequency
prevFs = ud.specs.import.fs.Fs;

% Get new Sampling Frequency from the editbox
h_fsEb = ud.h.import_params.importFs.fseb;

% Deblank Sampling Frequency string and display it back in the editbox.
currentFs = fixup_uiedit(h_fsEb);

% Update ud if the Sampling Frequency has changed
if ~strcmp(prevFs, char(currentFs{1})),
    % Save current sampling frequency (as a string) to UD
    ud.specs.import.fs = ...
        setfield(ud.specs.import.fs,'Fs',char(currentFs{1}));
    set(hFig,'Userdata',ud);

    set_flagsdirty(hFig);
    update_actionbtn('update_enablestate',hFig);
end

% [EOF] importparams_cbs.m
