function fcns = specs_cbs
%SPECS_CBS  Callback function for uicontrols within the specs. frames
%  

%   Author(s): R. Losada
%   Copyright 1988-2000 The MathWorks, Inc.
%   $Revision: 1.8 $  $Date: 2000/06/13 21:23:38 $ 

	% "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.freq_popup = @freq_popup;
	fcns.mag_popup = @mag_popup;
    fcns.freqmag_popup = @freqmag_popup;
    fcns.fs_eb = @fs_eb;
	fcns.eb = @eb;

%------------------------------------------------------------------------------
function freq_popup(hcbo, eventStruct)
%FREQ_POPUP   Units popup callback for the freq. frame.

hFig = gcbf;

[prev_units,new_units] = popup(hFig,'freq');

% If we change the Hz scale (Hz, kHz, MHz), don't update the frame
if ~( any(strcmp(prev_units,{'hz','khz','mhz'})) & ...
        any(strcmp(new_units,{'hz','khz','mhz'})) ),
    % Update labels and enabling/disabling Fs    
    update_specs('update_freqlbls',hFig,new_units);
end

%------------------------------------------------------------------------------
function mag_popup(hcbo, eventStruct)
%MAG_POPUP   Units popup callback for the magnitude frame.

hFig = gcbf;

[prev_units,new_units] = popup(hFig,'mag');

% Update the labels 
update_specs('update_maglbls',hFig,new_units);

%------------------------------------------------------------------------------
function freqmag_popup(hcbo, eventStruct)
%FREQMAG_POPUP   Units popup callback for the freqmag (freq, amplitude and weights) frame.

hFig = gcbf;

[prev_units,new_units] = popup(hFig,'freqmag');

% If we change the Hz scale (Hz, kHz, MHz), don't update Fs edit box
if ~( any(strcmp(prev_units,{'hz','khz','mhz'})) & ...
        any(strcmp(new_units,{'hz','khz','mhz'})) ),
    % Update labels and enabling/disabling Fs    
    update_specs('update_fs',hFig,new_units,'freqmag');
end

%------------------------------------------------------------------------
function [prev_units,new_units] = popup(hFig,frame)
%POPUP Common code shared by callback functions for the units popup.

% Get the user data stored in the figure
ud = get(hFig,'Userdata');

% Get previous freq. units from ud
prev_units = get_units(ud,frame);

% Get new units from popup
% First cache handle to the popup
h_UnitsPop = getfield(ud.h.design_params.specs,frame,'popup');

% Deteremine which units were selected
UnitStrs = get(h_UnitsPop,'String');
selected_Units = get(h_UnitsPop,'Value');
new_units = lower(UnitStrs{selected_Units});

% If we select the same units, do nothing
if strcmp(prev_units,new_units), return, end

% Else save the new units to ud
save_units(hFig,new_units,frame);

% Set dirty flags.
set_flagsdirty(hFig);  % Modifies userdata

% Enable the Apply button, something has changed
update_actionbtn('update_enablestate',hFig);

% Update analysis area if static response is current analysis.
update_staticresp = 1;
update_analysisframe(hFig,update_staticresp); 

%------------------------------------------------------------------------------
function fs_eb(hcbo,eventStruct,frame)
%FS_EB  Callback function for the sampling freq. edit box.

hFig = gcbf;

% Set dirty flags.
set_flagsdirty(hFig);  % Modifies userdata

% Get user data
ud = get(hFig,'userdata');

% Cache current filter type
filterType = ud.specs.design.type;

% Deblank string and display it
newvalue = fixup_uiedit(hcbo);

% Assign newvalue is a cell that contains Fs
fs = newvalue{1};
% Save to ud
ud.specs.design = setfield(ud.specs.design,frame,filterType,'fs',fs);

% Save the data in the figure
set(hFig,'userdata',ud);

% Enable the Apply button, something has changed
update_actionbtn('update_enablestate',hFig);

%------------------------------------------------------------------------------
function eb(hcbo, eventStruct,frame)
%EB  Common code shared by callback functions to edit boxes.

hFig = gcbf;

% Set dirty flags.
set_flagsdirty(hFig);  % Modifies userdata

% Get user data
ud = get(hFig,'userdata');

% Cache current filter type
filterType = ud.specs.design.type;
method = ud.specs.design.method;
filterOrder = ud.specs.design.order.calc;
window = ud.specs.design.window;

% Determine the specs mode on the fly, this value is not stored in ud
specs_mode = determine_specsmode(frame,method,filterOrder,window);

% Get handle to edit boxes
h_eb = getfield(ud.h.design_params.specs,frame,'eb');

% Look for the edit box that issues the callback
indx = find(h_eb == hcbo);

% Deblank string and display it
newvalue = fixup_uiedit(hcbo);

% newvalue is a cell, make it a string
newvalue = newvalue{1};

% Get values
values = getfield(ud.specs.design,frame,filterType,specs_mode);

% Replace old value with new value
values{indx} = newvalue;

% Save to ud
ud.specs.design = ...
    setfield(ud.specs.design,frame,filterType,specs_mode,values);

% Save the data in the figure
set(hFig,'userdata',ud);

% Enable the Apply button, something has changed
update_actionbtn('update_enablestate',hFig);

%---------------------------------------------------------------------

function units = get_units(ud,frame)
%GET_UNITS   Get freq. or mag. units depending on frame

% Cache current filter type
filterType = ud.specs.design.type;

% Get the current units from ud
units = getfield(ud.specs.design,frame,filterType,'units');

%-----------------------------------------------------------------------
function save_units(hFig,new_units,frame)
%SAVE_UNITS   Save freq. or mag. units to ud depending on frame

% Get user data
ud = get(hFig,'userdata');

% Cache current filter type
filterType = ud.specs.design.type;

ud.specs.design = setfield(ud.specs.design,frame,filterType,'units',new_units);

% Save ud
set(hFig,'userdata',ud);


%-----------------------------------------------------------------------
% [EOF] - specs_cbs.m

