function dummy = refreshgain(Editor,action)
%REFRESHGAIN  Refreshes plot after gain update.

%   Author(s): P. Gahinet
%   Copyright 1986-2000 The MathWorks, Inc. 
%   $Revision: 1.12 $  $Date: 2000/08/15 20:15:00 $

% REVISIT
dummy = [];

persistent GainListener

switch action
    
case 'init'
    % Initialization for dynamic gain update (drag)
    % Switch editor's RefreshMode to quick
    Editor.RefreshMode = 'quick';
    
    % Get initial Y location of poles/zeros (for normalized compensator)
    HG = Editor.HG;
    hPZ = [HG.System(1).Pole; HG.System(1).Zero; comphandles(Editor,'mag')];
    W = get(hPZ,{'Xdata'});
    W = unitconv(cat(1,W{:}),Editor.FrequencyUnits,'rad/sec');
    MagPZ = interpmag(Editor,Editor.Frequency,Editor.Magnitude,W);  % in abs units
    
    % Initialize parameters used to adjust mag limits
    LocalUpdatePlot([],[],Editor);
    
    % Install listener on gain data
    CompData = Editor.LoopData.Compensator;
    GainListener = handle.listener(CompData,findprop(CompData,'Gain'),...
        'PropertyPostSet',{@LocalUpdatePlot Editor MagPZ hPZ});
    
case 'finish'
    % Return editor's RefreshMode to normal
    Editor.RefreshMode = 'normal';
    
    % Delete listener
    delete(GainListener);
    GainListener = [];
    
end


%-------------------------Local Functions-------------------------

%%%%%%%%%%%%%%%%%%%%%%%
%%% LocalUpdatePlot %%%
%%%%%%%%%%%%%%%%%%%%%%%
function LocalUpdatePlot(hSrcProp,event,Editor,MagPZ,hPZ)
% Updates mag curve position

persistent AutoScaleOn MagRange MagLims  % limit management
persistent OldGdB

% REVISIT: eliminate
HG = Editor.HG;

if nargin==3
    % Initialize persistent variables
    AutoScaleOn = ...
        strcmp(Editor.EditMode,'idle') & strcmp(Editor.YlimMode{1},'auto');
    if AutoScaleOn
        % Interval of normalized mag values (in dB)
        MagRange = 20*log10(yextent(Editor,'mag'));
        MagLims = get(HG.PlotAxes(1),'Ylim'); 
    else
        MagRange = [];   MagLims = []; 
    end
    % Current gain mag
    OldGdB = 20*log10(abs(getgain(Editor.LoopData)));
    
else
    % Adjust position of magnitude plot
    % REVISIT: merge
    NewGain = event.NewValue.Magnitude; 
    NewGdB = 20*log10(NewGain);

    % Update magnitude plot
    % RE: Gain sign can't change in drag mode!
    set(HG.BodePlot(1),'Ydata',...
        unitconv(Editor.Magnitude * NewGain,'abs',Editor.MagnitudeUnits))
    set(hPZ,{'Ydata'},num2cell(...
        unitconv(MagPZ * NewGain,'abs',Editor.MagnitudeUnits)))
    
    % Update stability margins (using interpolation)
    refreshmargin(Editor);
    
    % Determine if mag limits must be updated
    if isinf(OldGdB) 
        % No plot previously shown: set limits
        updatelims(Editor);
    elseif AutoScaleOn
        % Keep plot within focus by shifting Y limits
        % REVISIT: merge
        out = shiftlims(Editor,MagLims,MagRange+NewGdB,NewGdB-OldGdB);
        MagLims = out{1};
    end
    
    % Store new gain 
    OldGdB = NewGdB;
    
end
    





