function dummy = selectgain(Editor)
%SELECTGAIN  Selects gain value by clicking on the root locus plot.
%
%   See also SISOTOOL

%   Author(s): P. Gahinet
%   Copyright 1986-2000 The MathWorks, Inc. 
%   $Revision: 1.6 $  $Date: 2000/06/14 20:21:51 $

% REVISIT:
dummy = [];

% Enabled only in idle mode
if ~strcmp(Editor.EditMode,'idle')
    return
end
HG = Editor.HG;
LoopData = Editor.LoopData;

% Get ZPK data for normalized open-loop model
[OLz,OLp,OLk] = zpkdata(LoopData.OpenLoop,'v');

% Compute new gain value
CP = get(HG.PlotAxes,'CurrentPoint');
P = CP(1,1) + 1i * CP(1,2);
NumP = OLk * prod(P-OLz);  % evaluate norm. ol numerator at P
DenP = prod(P-OLp);        % evaluate norm. ol denominator at P
NewGainMag = abs(DenP/NumP);

% Freeze axis limits in Root Locus Editor (axis rescaling gives the 
% illusion of missing the selected point)
XlimMode = Editor.XlimMode;
YlimMode = Editor.YlimMode;
Editor.XlimMode = {'manual'};
Editor.YlimMode = {'manual'};

% Commit gain change
GainData = LoopData.Compensator.Gain;
NewGain = GainData.Sign * NewGainMag;
setgain(LoopData,NewGain);

% Notify status bar and history listeners
Status = sprintf('Loop gain changed to %0.3g',NewGain);
Editor.up.send('StatusChanged',...
    sisodata.dataevent(Editor.up,'StatusChanged',{Status,Status}));

% Restore initial axis limit modes
Editor.XlimMode = XlimMode;
Editor.YlimMode = YlimMode;

