function initialize(Editor)
%INITIALIZE  Creates Pole/Zero Editor UI.

%   Author(s): P. Gahinet
%   Copyright 1986-2004 The MathWorks, Inc.
%   $Revision: 1.17.4.2 $ $Date: 2004/04/10 23:14:15 $

% Render editor
Editor.HG = LocalCreate(Editor);

%----------------- Local functions -----------------


%%%%%%%%%%%%%%%%%%%
%%% LocalCreate %%%
%%%%%%%%%%%%%%%%%%%
function HG = LocalCreate(Editor)

StdUnit = 'Character';
StdColor = get(0,'DefaultUicontrolBackgroundColor');
FigHeight = 12;
FrameHeight = 6;

% Initialize HG db
HG = struct(...
   'Figure',[],...
   'PoleEdit',[],...
   'TopHandles',[],...
   'ZeroEdit',[],...
   'ZPFrame',[],...
   'ZPText',[]);

% PZ Editor's figure
PZfig = figure('Color',[0.8 0.8 0.8], ...
   'MenuBar','none', ...
   'Units',StdUnit, ...
   'DefaultUiControlFontSize',Editor.FontSize,...
   'DefaultTextFontSize',Editor.FontSize,...
   'Resize','off', ...
   'Position',[50 5 90.6 FigHeight], ...
   'IntegerHandle','off', ...
   'HandleVisibility','callback',...
   'CloseRequestFcn',{@LocalHide Editor},...
   'NumberTitle','off', ...
   'Visible','off',...
   'UserData',Editor,...
   'DockControls', 'off');

centerfig(PZfig,Editor.Parent.Figure);
HG.Figure = PZfig;

% Init
HG.TopHandles = zeros(5,1);
HG.ZPFrame = zeros(1,2);
HG.ZPText = zeros(4,2);
HTKey = 'sisoeditcompensator';

%---Name and Format controls
HG.TopHandles(1) = uicontrol('Parent',PZfig, ...
   'Units',StdUnit, ...
   'Position',[0.8 FigHeight-3 89.2 2.6], ...
   'Style','frame');
HG.TopHandles(2) = uicontrol('Parent',PZfig, ...
   'Units',StdUnit, ...
   'Position',[2.2 FigHeight-2.7 10 1.5], ...
   'String','Gain:', ...
   'Horiz','left', ...
   'FontWeight','Bold', ...
   'HelpTopicKey',HTKey,...
   'Style','text');

% Gain edit box
Gain = Editor.EditedObject.getgain;
HG.TopHandles(3) = uicontrol('Parent',PZfig, ...
   'Units',StdUnit, ...
   'Callback',@LocalEditGain, ...
   'Position',[10 FigHeight-2.5 20 1.5], ...
   'String',sprintf('%0.3g',Gain), ...
   'UserData',Gain, ...
   'Horiz','left', ...
   'BackgroundColor',[1 1 1],...
   'HelpTopicKey',HTKey,...
   'Style','Edit');

HG.TopHandles(4) = uicontrol('Parent',PZfig, ...
   'Units',StdUnit, ...
   'Position',[40 FigHeight-2.7 10 1.5], ...
   'String','Format:', ...
   'Horiz','left', ...
   'FontWeight','Bold', ...
   'HelpTopicKey','sisopolezeroformat',...
   'Style','text');
HG.TopHandles(5) = uicontrol('Parent',PZfig, ...
   'Units',StdUnit, ...
   'Callback',{@LocalChangeFormat Editor}, ...
   'Position',[49 FigHeight-2.5 39 1.5], ...
   'String','Zero/Pole Location|Damping/Natural Frequency', ...
   'BackgroundColor',[1 1 1],...
   'Value',1,...
   'HelpTopicKey','sisopolezeroformat',...
   'Style','Popupmenu');

% Zeros frame
HG.ZeroEdit = zeros(0,5);
HG.ZPFrame(1) = uicontrol('Parent',PZfig, ...
   'Units',StdUnit, ...
   'Tag','ResizeFrame', ...
   'Position',[0.8 2.6 44 FrameHeight], ...
   'HelpTopicKey',HTKey,...
   'Style','frame');
HG.ZPText(1,1) = uicontrol('Parent',PZfig, ...
   'Units',StdUnit, ...
   'Position',[11 0.8+FrameHeight 20 1.5], ...
   'String','Zeros', ...
   'FontWeight','bold', ...
   'HelpTopicKey',HTKey,...
   'Style','text');
HG.ZPText(2,1) = uicontrol('Parent',PZfig, ...
   'Units',StdUnit, ...
   'Position',[2.2 FrameHeight-0.5 10 1.5], ...
   'Horizontal','left',...
   'String','Delete', ...
   'HelpTopicKey','sisopolezerodelete',...
   'Style','text'); 
HG.ZPText(3,1) = uicontrol('Parent',PZfig, ...
   'Units',StdUnit, ...
   'Position',[10.8 FrameHeight-0.5 10 1.5], ...
   'String','Real', ...
   'HelpTopicKey',HTKey,...
   'Style','text');
HG.ZPText(4,1) = uicontrol('Parent',PZfig, ...
   'Units',StdUnit, ...
   'Position',[25.5 FrameHeight-0.5 15 1.5], ...
   'String','Imaginary', ...
   'HelpTopicKey',HTKey,...
   'Style','text');
uicontrol('Parent',PZfig,...
   'Units',StdUnit, ...
   'Position',[0.8+4 3 16 2], ...
   'Callback',{@LocalAdd Editor 'Zero' 'Real'}, ...
   'HelpTopicKey','sisopolezeroadd',...
   'String','Add Real Zero');
uicontrol('Parent',PZfig,...
   'Units',StdUnit, ...
   'Position',[0.8+20 3 20 2], ...
   'Callback',{@LocalAdd Editor 'Zero' 'Complex'}, ...
   'HelpTopicKey','sisopolezeroadd',...
   'String','Add Complex Zero');

% Poles frame
HG.PoleEdit = zeros(0,5);
HG.ZPFrame(2) = uicontrol('Parent',PZfig, ...
   'Units',StdUnit, ...
   'Position',[45.8 2.6 44 FrameHeight], ...
   'HelpTopicKey',HTKey,...
   'Style','frame');
HG.ZPText(1,2) = uicontrol('Parent',PZfig, ...
   'Units',StdUnit, ...
   'Position',[55 0.8+FrameHeight 20 1.5], ...
   'String','Poles', ...
   'FontWeight','Bold', ...
   'HelpTopicKey',HTKey,...
   'Style','text');
HG.ZPText(2,2) = uicontrol('Parent',PZfig, ...
   'Units',StdUnit, ...
   'Horizontal','left',...
   'Position',[47.2 FrameHeight-0.5 10 1.5], ...
   'Enable','inactive',...
   'String','Delete', ...
   'HelpTopicKey','sisopolezerodelete',...
   'Style','text');
HG.ZPText(3,2) = uicontrol('Parent',PZfig, ...
   'Units',StdUnit, ...
   'Position',[55.3 FrameHeight-0.5 10 1.5], ...
   'String','Real', ...
   'HelpTopicKey',HTKey,...
   'Style','text');
HG.ZPText(4,2) = uicontrol('Parent',PZfig, ...
   'Units',StdUnit, ...
   'Position',[70.5 FrameHeight-0.5 15 1.5], ...
   'String','Imaginary', ...
   'HelpTopicKey',HTKey,...
   'Style','text');
uicontrol('Parent',PZfig,...
   'Units',StdUnit, ...
   'Callback',{@LocalAdd Editor 'Pole' 'Real'}, ...
   'Position',[45.8+4 3 16 2], ...
   'HelpTopicKey','sisopolezeroadd',...
   'String','Add Real Pole');
uicontrol('Parent',PZfig,...
   'Units',StdUnit, ...
   'Callback',{@LocalAdd Editor 'Pole' 'Complex'}, ...
   'Position',[45.8+20 3 20 2], ...
   'HelpTopicKey','sisopolezeroadd',...
   'String','Add Complex Pole');

% Bottom buttons
xgap = 1;  xw = 12;  xM = 45.3;
uicontrol('Parent',PZfig, ...
   'Units',StdUnit, ...
   'Position',[xM-1.5*xgap-2*xw 0.3 xw 2], ...
   'Callback',{@LocalOK Editor}, ...
   'String','OK');
uicontrol('Parent',PZfig, ...
   'Units',StdUnit, ...
   'Position',[xM-0.5*xgap-xw 0.3 xw 2 ], ...
   'Callback',{@LocalHide Editor}, ...
   'String','Cancel');
uicontrol('Parent',PZfig, ...
   'Units',StdUnit, ...
   'Position',[xM+0.5*xgap 0.3 xw 2], ...
   'Callback','ctrlguihelp(''sisoeditcompensator'');', ...
   'String','Help');
uicontrol('Parent',PZfig, ...
   'Units',StdUnit, ...
   'Position',[xM+1.5*xgap+xw 0.3 xw 2], ...
   'Callback',{@LocalApply Editor}, ...
   'String','Apply');


%%%%%%%%%%%%%%%%%
%%% LocalHide %%%
%%%%%%%%%%%%%%%%%
function LocalHide(hSrc,event,Editor)
% Hide editor
set(Editor,'Visible','off');


%%%%%%%%%%%%%%%%%%%%%
%%% LocalEditGain %%%
%%%%%%%%%%%%%%%%%%%%%
function LocalEditGain(hSrc,event)
% Validate compensator name edit
NewValue = str2num(get(hSrc,'String'));
if isequal(size(NewValue),[1 1]) & isreal(NewValue) & isfinite(NewValue) 
    % Store new value 
    set(hSrc,'UserData',NewValue)
else
    % Revert to previous name
    set(hSrc,'String',sprintf('%0.3g',get(hSrc,'UserData')))
end
    

%%%%%%%%%%%%%%%%%%%%%
% LocalChangeFormat %
%%%%%%%%%%%%%%%%%%%%%
function LocalChangeFormat(hSrc,event,Editor)
% Change the display format
Formats = {'RealImag' , 'Damping'};
Editor.Format = Formats{get(hSrc,'Value')};  % new format


%%%%%%%%%%%%%%
% LocalApply %
%%%%%%%%%%%%%%
function LocalApply(hSrc,event,Editor)
% Callback for compensator name listener
exportdata(Editor);


%%%%%%%%%%%
% LocalOK %
%%%%%%%%%%%
function LocalOK(hSrc,event,Editor)
% Callback for compensator name listener
AbortFlag = exportdata(Editor);
if ~AbortFlag
    % Successful apply
    set(Editor,'Visible','off');
end

%%%%%%%%%%%%
% LocalAdd %
%%%%%%%%%%%%
function LocalAdd(hSrc,event,varargin)
% Callback for compensator name listener
addpz(varargin{:});
